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