/
physfs_archiver_grp.c
116 lines (93 loc) · 3.2 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 ...)
*
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"
30
31
#if PHYSFS_SUPPORTS_GRP
32
static int grpLoadEntries(PHYSFS_Io *io, const PHYSFS_uint32 count, void *arc)
33
{
34
PHYSFS_uint32 pos = 16 + (16 * count); /* past sig+metadata. */
35
PHYSFS_uint32 i;
36
37
for (i = 0; i < count; i++)
38
{
39
40
41
42
43
44
45
46
char *ptr;
char name[13];
PHYSFS_uint32 size;
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, name, 12), 0);
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &size, 4), 0);
name[12] = '\0'; /* name isn't null-terminated in file. */
if ((ptr = strchr(name, ' ')) != NULL)
47
48
*ptr = '\0'; /* trim extra spaces. */
49
size = PHYSFS_swapULE32(size);
50
BAIL_IF_ERRPASS(!UNPK_addEntry(arc, name, 0, -1, -1, pos, size), 0);
51
52
pos += size;
53
} /* for */
54
55
return 1;
56
} /* grpLoadEntries */
57
58
59
60
static void *GRP_openArchive(PHYSFS_Io *io, const char *name,
int forWriting, int *claimed)
61
{
62
PHYSFS_uint8 buf[12];
63
PHYSFS_uint32 count = 0;
64
void *unpkarc = NULL;
65
66
assert(io != NULL); /* shouldn't ever happen. */
67
68
BAIL_IF(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
69
70
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, buf, sizeof (buf)), NULL);
71
if (memcmp(buf, "KenSilverman", sizeof (buf)) != 0)
72
BAIL(PHYSFS_ERR_UNSUPPORTED, NULL);
73
74
75
*claimed = 1;
76
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &count, sizeof(count)), NULL);
77
count = PHYSFS_swapULE32(count);
78
79
unpkarc = UNPK_openArchive(io);
80
81
82
83
BAIL_IF_ERRPASS(!unpkarc, NULL);
if (!grpLoadEntries(io, count, unpkarc))
{
84
UNPK_abandonArchive(unpkarc);
85
86
87
88
return NULL;
} /* if */
return unpkarc;
89
90
91
} /* GRP_openArchive */
92
93
const PHYSFS_Archiver __PHYSFS_Archiver_GRP =
{
94
CURRENT_PHYSFS_ARCHIVER_API_VERSION,
95
96
97
98
{
"GRP",
"Build engine Groupfile format",
"Ryan C. Gordon <icculus@icculus.org>",
99
"https://icculus.org/physfs/",
100
0, /* supportsSymlinks */
101
},
102
GRP_openArchive,
103
UNPK_enumerate,
104
105
106
107
108
UNPK_openRead,
UNPK_openWrite,
UNPK_openAppend,
UNPK_remove,
UNPK_mkdir,
109
110
UNPK_stat,
UNPK_closeArchive
111
112
};
113
114
#endif /* defined PHYSFS_SUPPORTS_GRP */
115
/* end of physfs_archiver_grp.c ... */