Skip to content

Latest commit

 

History

History
110 lines (89 loc) · 3.62 KB

archiver_grp.c

File metadata and controls

110 lines (89 loc) · 3.62 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
* GRP support routines for PhysicsFS.
*
* This driver handles BUILD engine archives ("groupfiles"). This format
* (but not this driver) was put together by Ken Silverman.
*
* The format is simple enough. In Ken's words:
*
* What's the .GRP file format?
*
* The ".grp" file format is just a collection of a lot of files stored
* into 1 big one. I tried to make the format as simple as possible: The
* first 12 bytes contains my name, "KenSilverman". The next 4 bytes is
* the number of files that were compacted into the group file. Then for
* each file, there is a 16 byte structure, where the first 12 bytes are
* the filename, and the last 4 bytes are the file's size. The rest of
* the group file is just the raw data packed one after the other in the
* same order as the list of files.
*
* (That info is from http://www.advsys.net/ken/build.htm ...)
*
Mar 11, 2007
Mar 11, 2007
22
* Please see the file LICENSE.txt in the source's root directory.
23
24
25
26
27
28
29
*
* This file written by Ryan C. Gordon.
*/
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
Mar 23, 2012
Mar 23, 2012
30
31
#if PHYSFS_SUPPORTS_GRP
Sep 6, 2010
Sep 6, 2010
32
static UNPKentry *grpLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount)
Jul 23, 2002
Jul 23, 2002
34
PHYSFS_uint32 location = 16; /* sizeof sig. */
Sep 6, 2010
Sep 6, 2010
35
36
37
UNPKentry *entries = NULL;
UNPKentry *entry = NULL;
char *ptr = NULL;
Jul 23, 2002
Jul 23, 2002
38
Sep 6, 2010
Sep 6, 2010
39
entries = (UNPKentry *) allocator.Malloc(sizeof (UNPKentry) * fileCount);
Mar 20, 2012
Mar 20, 2012
40
BAIL_IF_MACRO(entries == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
Jul 23, 2002
Jul 23, 2002
41
Jul 23, 2002
Jul 23, 2002
42
43
location += (16 * fileCount);
Sep 6, 2010
Sep 6, 2010
44
for (entry = entries; fileCount > 0; fileCount--, entry++)
Jul 23, 2002
Jul 23, 2002
45
{
Mar 20, 2012
Mar 20, 2012
46
47
GOTO_IF_MACRO(!__PHYSFS_readAll(io, &entry->name, 12), ERRPASS, failed);
GOTO_IF_MACRO(!__PHYSFS_readAll(io, &entry->size, 4), ERRPASS, failed);
Jul 23, 2002
Jul 23, 2002
48
49
50
51
52
53
entry->name[12] = '\0'; /* name isn't null-terminated in file. */
if ((ptr = strchr(entry->name, ' ')) != NULL)
*ptr = '\0'; /* trim extra spaces. */
entry->size = PHYSFS_swapULE32(entry->size);
entry->startPos = location;
Jul 23, 2002
Jul 23, 2002
54
location += entry->size;
Jul 23, 2002
Jul 23, 2002
55
56
} /* for */
Sep 6, 2010
Sep 6, 2010
57
58
return entries;
Mar 9, 2012
Mar 9, 2012
59
failed:
Sep 6, 2010
Sep 6, 2010
60
61
62
allocator.Free(entries);
return NULL;
} /* grpLoadEntries */
Jul 23, 2002
Jul 23, 2002
63
64
Aug 30, 2010
Aug 30, 2010
65
static void *GRP_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
Jul 23, 2002
Jul 23, 2002
66
{
Aug 30, 2010
Aug 30, 2010
67
PHYSFS_uint8 buf[12];
Mar 9, 2012
Mar 9, 2012
68
PHYSFS_uint32 count = 0;
Sep 6, 2010
Sep 6, 2010
69
UNPKentry *entries = NULL;
Aug 30, 2010
Aug 30, 2010
70
71
assert(io != NULL); /* shouldn't ever happen. */
Mar 23, 2012
Mar 23, 2012
73
BAIL_IF_MACRO(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
Mar 20, 2012
Mar 20, 2012
75
BAIL_IF_MACRO(!__PHYSFS_readAll(io, buf, sizeof (buf)), ERRPASS, NULL);
Aug 30, 2010
Aug 30, 2010
76
if (memcmp(buf, "KenSilverman", sizeof (buf)) != 0)
Mar 20, 2012
Mar 20, 2012
77
BAIL_MACRO(PHYSFS_ERR_UNSUPPORTED, NULL);
Mar 25, 2002
Mar 25, 2002
78
Mar 20, 2012
Mar 20, 2012
79
BAIL_IF_MACRO(!__PHYSFS_readAll(io, &count, sizeof(count)), ERRPASS, NULL);
Mar 9, 2012
Mar 9, 2012
80
count = PHYSFS_swapULE32(count);
Mar 25, 2002
Mar 25, 2002
81
Mar 9, 2012
Mar 9, 2012
82
entries = grpLoadEntries(io, count);
Mar 20, 2012
Mar 20, 2012
83
BAIL_IF_MACRO(!entries, ERRPASS, NULL);
Mar 9, 2012
Mar 9, 2012
84
return UNPK_openArchive(io, entries, count);
85
86
87
} /* GRP_openArchive */
Sep 29, 2004
Sep 29, 2004
88
89
const PHYSFS_Archiver __PHYSFS_Archiver_GRP =
{
Mar 25, 2012
Mar 25, 2012
90
91
92
93
94
95
{
"GRP",
"Build engine Groupfile format",
"Ryan C. Gordon <icculus@icculus.org>",
"http://icculus.org/physfs/",
},
Sep 29, 2004
Sep 29, 2004
96
GRP_openArchive, /* openArchive() method */
Sep 6, 2010
Sep 6, 2010
97
98
99
100
101
102
UNPK_enumerateFiles, /* enumerateFiles() method */
UNPK_openRead, /* openRead() method */
UNPK_openWrite, /* openWrite() method */
UNPK_openAppend, /* openAppend() method */
UNPK_remove, /* remove() method */
UNPK_mkdir, /* mkdir() method */
Mar 24, 2012
Mar 24, 2012
103
UNPK_closeArchive, /* closeArchive() method */
Sep 6, 2010
Sep 6, 2010
104
UNPK_stat /* stat() method */
Sep 29, 2004
Sep 29, 2004
105
106
};
May 10, 2002
May 10, 2002
107
108
#endif /* defined PHYSFS_SUPPORTS_GRP */
Nov 27, 2012
Nov 27, 2012
109
/* end of archiver_grp.c ... */