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