/
archiver_grp.c
475 lines (370 loc) · 12.8 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
*
* This file written by Ryan C. Gordon.
*/
27
28
#if (defined PHYSFS_SUPPORTS_GRP)
29
30
31
32
33
34
35
36
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "physfs.h"
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
37
38
39
typedef struct
{
char name[13];
40
41
PHYSFS_uint32 startPos;
PHYSFS_uint32 size;
42
43
} GRPentry;
44
45
typedef struct
{
46
char *filename;
47
48
49
PHYSFS_sint64 last_mod_time;
PHYSFS_uint32 entryCount;
GRPentry *entries;
50
51
52
53
} GRPinfo;
typedef struct
{
54
void *handle;
55
GRPentry *entry;
56
PHYSFS_uint32 curPos;
57
58
59
} GRPfileinfo;
60
static void GRP_dirClose(dvoid *opaque)
61
{
62
GRPinfo *info = ((GRPinfo *) opaque);
63
64
65
allocator.Free(info->filename);
allocator.Free(info->entries);
allocator.Free(info);
66
} /* GRP_dirClose */
67
68
69
static PHYSFS_sint64 GRP_read(fvoid *opaque, void *buffer,
70
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
71
{
72
GRPfileinfo *finfo = (GRPfileinfo *) opaque;
73
GRPentry *entry = finfo->entry;
74
75
PHYSFS_uint32 bytesLeft = entry->size - finfo->curPos;
PHYSFS_uint32 objsLeft = (bytesLeft / objSize);
76
PHYSFS_sint64 rc;
77
78
if (objsLeft < objCount)
79
objCount = objsLeft;
80
81
82
rc = __PHYSFS_platformRead(finfo->handle, buffer, objSize, objCount);
if (rc > 0)
83
finfo->curPos += (PHYSFS_uint32) (rc * objSize);
84
85
return rc;
86
87
88
} /* GRP_read */
89
static PHYSFS_sint64 GRP_write(fvoid *opaque, const void *buffer,
90
91
92
93
94
95
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
{
BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
} /* GRP_write */
96
static int GRP_eof(fvoid *opaque)
97
{
98
GRPfileinfo *finfo = (GRPfileinfo *) opaque;
99
GRPentry *entry = finfo->entry;
100
return (finfo->curPos >= entry->size);
101
102
103
} /* GRP_eof */
104
static PHYSFS_sint64 GRP_tell(fvoid *opaque)
105
{
106
return ((GRPfileinfo *) opaque)->curPos;
107
108
109
} /* GRP_tell */
110
static int GRP_seek(fvoid *opaque, PHYSFS_uint64 offset)
111
{
112
GRPfileinfo *finfo = (GRPfileinfo *) opaque;
113
114
GRPentry *entry = finfo->entry;
int rc;
115
116
BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0);
117
118
BAIL_IF_MACRO(offset >= entry->size, ERR_PAST_EOF, 0);
rc = __PHYSFS_platformSeek(finfo->handle, entry->startPos + offset);
119
if (rc)
120
finfo->curPos = (PHYSFS_uint32) offset;
121
122
return rc;
123
124
125
} /* GRP_seek */
126
static PHYSFS_sint64 GRP_fileLength(fvoid *opaque)
127
{
128
GRPfileinfo *finfo = (GRPfileinfo *) opaque;
129
return ((PHYSFS_sint64) finfo->entry->size);
130
131
132
} /* GRP_fileLength */
133
static int GRP_fileClose(fvoid *opaque)
134
{
135
GRPfileinfo *finfo = (GRPfileinfo *) opaque;
136
BAIL_IF_MACRO(!__PHYSFS_platformClose(finfo->handle), NULL, 0);
137
allocator.Free(finfo);
138
return 1;
139
140
141
} /* GRP_fileClose */
142
143
static int grp_open(const char *filename, int forWriting,
void **fh, PHYSFS_uint32 *count)
144
{
145
PHYSFS_uint8 buf[12];
146
147
148
149
*fh = NULL;
BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
150
151
*fh = __PHYSFS_platformOpenRead(filename);
BAIL_IF_MACRO(*fh == NULL, NULL, 0);
152
153
154
155
156
157
158
159
160
if (__PHYSFS_platformRead(*fh, buf, 12, 1) != 1)
goto openGrp_failed;
if (memcmp(buf, "KenSilverman", 12) != 0)
{
__PHYSFS_setError(ERR_UNSUPPORTED_ARCHIVE);
goto openGrp_failed;
} /* if */
161
162
if (__PHYSFS_platformRead(*fh, count, sizeof (PHYSFS_uint32), 1) != 1)
163
goto openGrp_failed;
164
165
*count = PHYSFS_swapULE32(*count);
166
167
return 1;
168
169
170
171
172
173
174
openGrp_failed:
if (*fh != NULL)
__PHYSFS_platformClose(*fh);
*count = -1;
*fh = NULL;
175
return 0;
176
} /* grp_open */
177
178
179
180
static int GRP_isArchive(const char *filename, int forWriting)
{
181
void *fh;
182
183
PHYSFS_uint32 fileCount;
int retval = grp_open(filename, forWriting, &fh, &fileCount);
184
185
if (fh != NULL)
186
__PHYSFS_platformClose(fh);
187
188
return retval;
189
190
191
} /* GRP_isArchive */
192
static int grp_entry_cmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
193
{
194
195
196
if (one != two)
{
const GRPentry *a = (const GRPentry *) _a;
197
return (strcmp(a[one].name, a[two].name));
198
199
200
} /* if */
return 0;
201
} /* grp_entry_cmp */
202
203
204
static void grp_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
205
{
206
207
208
209
210
211
212
213
214
if (one != two)
{
GRPentry tmp;
GRPentry *first = &(((GRPentry *) _a)[one]);
GRPentry *second = &(((GRPentry *) _a)[two]);
memcpy(&tmp, first, sizeof (GRPentry));
memcpy(first, second, sizeof (GRPentry));
memcpy(second, &tmp, sizeof (GRPentry));
} /* if */
215
} /* grp_entry_swap */
216
217
218
static int grp_load_entries(const char *name, int forWriting, GRPinfo *info)
219
{
220
void *fh = NULL;
221
PHYSFS_uint32 fileCount;
222
PHYSFS_uint32 location = 16; /* sizeof sig. */
223
224
225
226
227
GRPentry *entry;
char *ptr;
BAIL_IF_MACRO(!grp_open(name, forWriting, &fh, &fileCount), NULL, 0);
info->entryCount = fileCount;
228
info->entries = (GRPentry *) allocator.Malloc(sizeof(GRPentry)*fileCount);
229
230
231
232
233
234
if (info->entries == NULL)
{
__PHYSFS_platformClose(fh);
BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
} /* if */
235
236
location += (16 * fileCount);
237
238
239
240
241
for (entry = info->entries; fileCount > 0; fileCount--, entry++)
{
if (__PHYSFS_platformRead(fh, &entry->name, 12, 1) != 1)
{
__PHYSFS_platformClose(fh);
242
return 0;
243
244
245
246
247
248
249
250
251
} /* if */
entry->name[12] = '\0'; /* name isn't null-terminated in file. */
if ((ptr = strchr(entry->name, ' ')) != NULL)
*ptr = '\0'; /* trim extra spaces. */
if (__PHYSFS_platformRead(fh, &entry->size, 4, 1) != 1)
{
__PHYSFS_platformClose(fh);
252
return 0;
253
254
255
256
} /* if */
entry->size = PHYSFS_swapULE32(entry->size);
entry->startPos = location;
257
location += entry->size;
258
259
260
261
} /* for */
__PHYSFS_platformClose(fh);
262
263
__PHYSFS_sort(info->entries, info->entryCount,
grp_entry_cmp, grp_entry_swap);
264
return 1;
265
266
267
} /* grp_load_entries */
268
static void *GRP_openArchive(const char *name, int forWriting)
269
270
{
PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name);
271
GRPinfo *info = (GRPinfo *) allocator.Malloc(sizeof (GRPinfo));
272
273
BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, 0);
274
275
memset(info, '\0', sizeof (GRPinfo));
276
info->filename = (char *) allocator.Malloc(strlen(name) + 1);
277
GOTO_IF_MACRO(!info->filename, ERR_OUT_OF_MEMORY, GRP_openArchive_failed);
278
279
if (!grp_load_entries(name, forWriting, info))
280
281
goto GRP_openArchive_failed;
282
283
strcpy(info->filename, name);
info->last_mod_time = modtime;
284
285
return info;
286
287
GRP_openArchive_failed:
288
if (info != NULL)
289
{
290
if (info->filename != NULL)
291
allocator.Free(info->filename);
292
if (info->entries != NULL)
293
294
allocator.Free(info->entries);
allocator.Free(info);
295
296
} /* if */
297
return NULL;
298
299
300
} /* GRP_openArchive */
301
static void GRP_enumerateFiles(dvoid *opaque, const char *dname,
302
303
int omitSymLinks, PHYSFS_EnumFilesCallback cb,
const char *origdir, void *callbackdata)
304
{
305
/* no directories in GRP files. */
306
if (*dname == '\0')
307
308
309
310
311
{
GRPinfo *info = (GRPinfo *) opaque;
GRPentry *entry = info->entries;
PHYSFS_uint32 max = info->entryCount;
PHYSFS_uint32 i;
312
313
for (i = 0; i < max; i++, entry++)
314
cb(callbackdata, origdir, entry->name);
315
} /* if */
316
317
318
} /* GRP_enumerateFiles */
319
static GRPentry *grp_find_entry(GRPinfo *info, const char *name)
320
{
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
char *ptr = strchr(name, '.');
GRPentry *a = info->entries;
PHYSFS_sint32 lo = 0;
PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
PHYSFS_sint32 middle;
int rc;
/*
* Rule out filenames to avoid unneeded processing...no dirs,
* big filenames, or extensions > 3 chars.
*/
BAIL_IF_MACRO((ptr) && (strlen(ptr) > 4), ERR_NO_SUCH_FILE, NULL);
BAIL_IF_MACRO(strlen(name) > 12, ERR_NO_SUCH_FILE, NULL);
BAIL_IF_MACRO(strchr(name, '/') != NULL, ERR_NO_SUCH_FILE, NULL);
while (lo <= hi)
337
{
338
339
340
middle = lo + ((hi - lo) / 2);
rc = strcmp(name, a[middle].name);
if (rc == 0) /* found it! */
341
return &a[middle];
342
343
344
345
346
else if (rc > 0)
lo = middle + 1;
else
hi = middle - 1;
} /* while */
347
348
349
BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
} /* grp_find_entry */
350
351
352
static int GRP_exists(dvoid *opaque, const char *name)
353
{
354
return (grp_find_entry((GRPinfo *) opaque, name) != NULL);
355
356
357
} /* GRP_exists */
358
static int GRP_isDirectory(dvoid *opaque, const char *name, int *fileExists)
359
{
360
*fileExists = GRP_exists(opaque, name);
361
return 0; /* never directories in a groupfile. */
362
363
364
} /* GRP_isDirectory */
365
static int GRP_isSymLink(dvoid *opaque, const char *name, int *fileExists)
366
{
367
*fileExists = GRP_exists(opaque, name);
368
return 0; /* never symlinks in a groupfile. */
369
370
371
} /* GRP_isSymLink */
372
static PHYSFS_sint64 GRP_getLastModTime(dvoid *opaque,
373
374
const char *name,
int *fileExists)
375
{
376
GRPinfo *info = (GRPinfo *) opaque;
377
PHYSFS_sint64 retval = -1;
378
379
380
*fileExists = (grp_find_entry(info, name) != NULL);
if (*fileExists) /* use time of GRP itself in the physical filesystem. */
381
retval = info->last_mod_time;
382
383
return retval;
384
385
386
} /* GRP_getLastModTime */
387
static fvoid *GRP_openRead(dvoid *opaque, const char *fnm, int *fileExists)
388
{
389
GRPinfo *info = (GRPinfo *) opaque;
390
GRPfileinfo *finfo;
391
GRPentry *entry;
392
393
394
entry = grp_find_entry(info, fnm);
*fileExists = (entry != NULL);
395
BAIL_IF_MACRO(entry == NULL, NULL, NULL);
396
397
finfo = (GRPfileinfo *) allocator.Malloc(sizeof (GRPfileinfo));
398
BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL);
399
400
finfo->handle = __PHYSFS_platformOpenRead(info->filename);
401
if ( (finfo->handle == NULL) ||
402
(!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) )
403
{
404
allocator.Free(finfo);
405
return NULL;
406
407
} /* if */
408
409
finfo->curPos = 0;
finfo->entry = entry;
410
return finfo;
411
412
} /* GRP_openRead */
413
414
static fvoid *GRP_openWrite(dvoid *opaque, const char *name)
415
416
417
418
419
{
BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
} /* GRP_openWrite */
420
static fvoid *GRP_openAppend(dvoid *opaque, const char *name)
421
422
423
424
425
{
BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
} /* GRP_openAppend */
426
static int GRP_remove(dvoid *opaque, const char *name)
427
428
429
430
431
{
BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
} /* GRP_remove */
432
static int GRP_mkdir(dvoid *opaque, const char *name)
433
434
435
436
{
BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
} /* GRP_mkdir */
437
438
439
440
441
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_GRP =
{
"GRP",
GRP_ARCHIVE_DESCRIPTION,
442
"Ryan C. Gordon <icculus@icculus.org>",
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
"http://icculus.org/physfs/",
};
const PHYSFS_Archiver __PHYSFS_Archiver_GRP =
{
&__PHYSFS_ArchiveInfo_GRP,
GRP_isArchive, /* isArchive() method */
GRP_openArchive, /* openArchive() method */
GRP_enumerateFiles, /* enumerateFiles() method */
GRP_exists, /* exists() method */
GRP_isDirectory, /* isDirectory() method */
GRP_isSymLink, /* isSymLink() method */
GRP_getLastModTime, /* getLastModTime() method */
GRP_openRead, /* openRead() method */
GRP_openWrite, /* openWrite() method */
GRP_openAppend, /* openAppend() method */
GRP_remove, /* remove() method */
GRP_mkdir, /* mkdir() method */
GRP_dirClose, /* dirClose() method */
GRP_read, /* read() method */
GRP_write, /* write() method */
GRP_eof, /* eof() method */
GRP_tell, /* tell() method */
GRP_seek, /* seek() method */
GRP_fileLength, /* fileLength() method */
GRP_fileClose /* fileClose() method */
};
472
473
#endif /* defined PHYSFS_SUPPORTS_GRP */
474
/* end of grp.c ... */