Skip to content

Latest commit

 

History

History
492 lines (392 loc) · 13.7 KB

grp.c

File metadata and controls

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