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