Skip to content

Latest commit

 

History

History
475 lines (370 loc) · 12.8 KB

grp.c

File metadata and controls

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 ...)
*
Mar 11, 2007
Mar 11, 2007
22
* Please see the file LICENSE.txt in the source's root directory.
23
24
25
26
*
* This file written by Ryan C. Gordon.
*/
May 10, 2002
May 10, 2002
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"
Jul 23, 2002
Jul 23, 2002
37
38
39
typedef struct
{
char name[13];
Aug 28, 2002
Aug 28, 2002
40
41
PHYSFS_uint32 startPos;
PHYSFS_uint32 size;
Jul 23, 2002
Jul 23, 2002
42
43
} GRPentry;
44
45
typedef struct
{
Mar 25, 2002
Mar 25, 2002
46
char *filename;
Jul 23, 2002
Jul 23, 2002
47
48
49
PHYSFS_sint64 last_mod_time;
PHYSFS_uint32 entryCount;
GRPentry *entries;
50
51
52
53
} GRPinfo;
typedef struct
{
Mar 25, 2002
Mar 25, 2002
54
void *handle;
Jul 23, 2002
Jul 23, 2002
55
GRPentry *entry;
Aug 28, 2002
Aug 28, 2002
56
PHYSFS_uint32 curPos;
57
58
59
} GRPfileinfo;
Sep 26, 2004
Sep 26, 2004
60
static void GRP_dirClose(dvoid *opaque)
Sep 2, 2001
Sep 2, 2001
61
{
Sep 26, 2004
Sep 26, 2004
62
GRPinfo *info = ((GRPinfo *) opaque);
Mar 14, 2005
Mar 14, 2005
63
64
65
allocator.Free(info->filename);
allocator.Free(info->entries);
allocator.Free(info);
Sep 2, 2001
Sep 2, 2001
66
} /* GRP_dirClose */
Sep 26, 2004
Sep 26, 2004
69
static PHYSFS_sint64 GRP_read(fvoid *opaque, void *buffer,
Mar 24, 2002
Mar 24, 2002
70
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
Sep 26, 2004
Sep 26, 2004
72
GRPfileinfo *finfo = (GRPfileinfo *) opaque;
Jul 23, 2002
Jul 23, 2002
73
GRPentry *entry = finfo->entry;
Aug 28, 2002
Aug 28, 2002
74
75
PHYSFS_uint32 bytesLeft = entry->size - finfo->curPos;
PHYSFS_uint32 objsLeft = (bytesLeft / objSize);
Jul 23, 2002
Jul 23, 2002
76
PHYSFS_sint64 rc;
77
78
if (objsLeft < objCount)
Aug 28, 2002
Aug 28, 2002
79
objCount = objsLeft;
Jul 23, 2002
Jul 23, 2002
81
82
rc = __PHYSFS_platformRead(finfo->handle, buffer, objSize, objCount);
if (rc > 0)
Aug 28, 2002
Aug 28, 2002
83
finfo->curPos += (PHYSFS_uint32) (rc * objSize);
Jul 23, 2002
Jul 23, 2002
84
85
return(rc);
86
87
88
} /* GRP_read */
Sep 26, 2004
Sep 26, 2004
89
static PHYSFS_sint64 GRP_write(fvoid *opaque, const void *buffer,
Aug 21, 2002
Aug 21, 2002
90
91
92
93
94
95
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
{
BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
} /* GRP_write */
Sep 26, 2004
Sep 26, 2004
96
static int GRP_eof(fvoid *opaque)
Sep 26, 2004
Sep 26, 2004
98
GRPfileinfo *finfo = (GRPfileinfo *) opaque;
Jul 23, 2002
Jul 23, 2002
99
GRPentry *entry = finfo->entry;
Aug 28, 2002
Aug 28, 2002
100
return(finfo->curPos >= entry->size);
101
102
103
} /* GRP_eof */
Sep 26, 2004
Sep 26, 2004
104
static PHYSFS_sint64 GRP_tell(fvoid *opaque)
Sep 26, 2004
Sep 26, 2004
106
return(((GRPfileinfo *) opaque)->curPos);
107
108
109
} /* GRP_tell */
Sep 26, 2004
Sep 26, 2004
110
static int GRP_seek(fvoid *opaque, PHYSFS_uint64 offset)
Sep 26, 2004
Sep 26, 2004
112
GRPfileinfo *finfo = (GRPfileinfo *) opaque;
Jul 23, 2002
Jul 23, 2002
113
114
GRPentry *entry = finfo->entry;
int rc;
115
116
BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0);
Aug 28, 2002
Aug 28, 2002
117
118
BAIL_IF_MACRO(offset >= entry->size, ERR_PAST_EOF, 0);
rc = __PHYSFS_platformSeek(finfo->handle, entry->startPos + offset);
Jul 23, 2002
Jul 23, 2002
119
if (rc)
Aug 28, 2002
Aug 28, 2002
120
finfo->curPos = (PHYSFS_uint32) offset;
Jul 23, 2002
Jul 23, 2002
121
122
return(rc);
123
124
125
} /* GRP_seek */
Sep 26, 2004
Sep 26, 2004
126
static PHYSFS_sint64 GRP_fileLength(fvoid *opaque)
Jul 9, 2001
Jul 9, 2001
127
{
Sep 26, 2004
Sep 26, 2004
128
GRPfileinfo *finfo = (GRPfileinfo *) opaque;
Aug 28, 2002
Aug 28, 2002
129
return((PHYSFS_sint64) finfo->entry->size);
Jul 9, 2001
Jul 9, 2001
130
131
132
} /* GRP_fileLength */
Sep 26, 2004
Sep 26, 2004
133
static int GRP_fileClose(fvoid *opaque)
Sep 26, 2004
Sep 26, 2004
135
GRPfileinfo *finfo = (GRPfileinfo *) opaque;
Jul 23, 2002
Jul 23, 2002
136
BAIL_IF_MACRO(!__PHYSFS_platformClose(finfo->handle), NULL, 0);
Mar 14, 2005
Mar 14, 2005
137
allocator.Free(finfo);
138
139
140
141
return(1);
} /* GRP_fileClose */
Jul 23, 2002
Jul 23, 2002
142
143
static int grp_open(const char *filename, int forWriting,
void **fh, PHYSFS_uint32 *count)
Mar 25, 2002
Mar 25, 2002
145
PHYSFS_uint8 buf[12];
146
147
148
149
*fh = NULL;
BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
Mar 25, 2002
Mar 25, 2002
150
151
*fh = __PHYSFS_platformOpenRead(filename);
BAIL_IF_MACRO(*fh == NULL, NULL, 0);
Mar 25, 2002
Mar 25, 2002
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 */
Jul 23, 2002
Jul 23, 2002
162
if (__PHYSFS_platformRead(*fh, count, sizeof (PHYSFS_uint32), 1) != 1)
Mar 25, 2002
Mar 25, 2002
163
goto openGrp_failed;
Jul 23, 2002
Jul 23, 2002
165
*count = PHYSFS_swapULE32(*count);
Apr 5, 2002
Apr 5, 2002
166
167
return(1);
Mar 25, 2002
Mar 25, 2002
168
169
170
171
172
173
174
175
openGrp_failed:
if (*fh != NULL)
__PHYSFS_platformClose(*fh);
*count = -1;
*fh = NULL;
return(0);
Jul 23, 2002
Jul 23, 2002
176
} /* grp_open */
177
178
179
180
static int GRP_isArchive(const char *filename, int forWriting)
{
Mar 25, 2002
Mar 25, 2002
181
void *fh;
Jul 23, 2002
Jul 23, 2002
182
183
PHYSFS_uint32 fileCount;
int retval = grp_open(filename, forWriting, &fh, &fileCount);
184
185
if (fh != NULL)
Mar 25, 2002
Mar 25, 2002
186
__PHYSFS_platformClose(fh);
187
188
189
190
191
return(retval);
} /* GRP_isArchive */
Aug 20, 2002
Aug 20, 2002
192
static int grp_entry_cmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
Jul 23, 2002
Jul 23, 2002
193
{
Feb 20, 2008
Feb 20, 2008
194
195
196
197
198
199
200
if (one != two)
{
const GRPentry *a = (const GRPentry *) _a;
return(strcmp(a[one].name, a[two].name));
} /* if */
return 0;
Aug 20, 2002
Aug 20, 2002
201
} /* grp_entry_cmp */
Jul 23, 2002
Jul 23, 2002
202
203
Aug 20, 2002
Aug 20, 2002
204
static void grp_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
Jul 23, 2002
Jul 23, 2002
205
{
Feb 20, 2008
Feb 20, 2008
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 */
Aug 20, 2002
Aug 20, 2002
215
} /* grp_entry_swap */
Jul 23, 2002
Jul 23, 2002
216
217
218
static int grp_load_entries(const char *name, int forWriting, GRPinfo *info)
Mar 25, 2002
Mar 25, 2002
220
void *fh = NULL;
Jul 23, 2002
Jul 23, 2002
221
PHYSFS_uint32 fileCount;
Jul 23, 2002
Jul 23, 2002
222
PHYSFS_uint32 location = 16; /* sizeof sig. */
Jul 23, 2002
Jul 23, 2002
223
224
225
226
227
GRPentry *entry;
char *ptr;
BAIL_IF_MACRO(!grp_open(name, forWriting, &fh, &fileCount), NULL, 0);
info->entryCount = fileCount;
Mar 14, 2005
Mar 14, 2005
228
info->entries = (GRPentry *) allocator.Malloc(sizeof(GRPentry)*fileCount);
Jul 23, 2002
Jul 23, 2002
229
230
231
232
233
234
if (info->entries == NULL)
{
__PHYSFS_platformClose(fh);
BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
} /* if */
Jul 23, 2002
Jul 23, 2002
235
236
location += (16 * fileCount);
Jul 23, 2002
Jul 23, 2002
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
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
257
location += entry->size;
Jul 23, 2002
Jul 23, 2002
258
259
260
261
} /* for */
__PHYSFS_platformClose(fh);
Aug 20, 2002
Aug 20, 2002
262
263
__PHYSFS_sort(info->entries, info->entryCount,
grp_entry_cmp, grp_entry_swap);
Jul 23, 2002
Jul 23, 2002
264
265
266
267
return(1);
} /* grp_load_entries */
Sep 26, 2004
Sep 26, 2004
268
static void *GRP_openArchive(const char *name, int forWriting)
Jul 23, 2002
Jul 23, 2002
269
270
{
PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name);
Mar 14, 2005
Mar 14, 2005
271
GRPinfo *info = (GRPinfo *) allocator.Malloc(sizeof (GRPinfo));
Sep 26, 2004
Sep 26, 2004
273
BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, 0);
Jul 23, 2002
Jul 23, 2002
275
memset(info, '\0', sizeof (GRPinfo));
Mar 14, 2005
Mar 14, 2005
276
info->filename = (char *) allocator.Malloc(strlen(name) + 1);
Mar 13, 2005
Mar 13, 2005
277
GOTO_IF_MACRO(!info->filename, ERR_OUT_OF_MEMORY, GRP_openArchive_failed);
Jul 23, 2002
Jul 23, 2002
279
if (!grp_load_entries(name, forWriting, info))
Mar 25, 2002
Mar 25, 2002
280
281
goto GRP_openArchive_failed;
Jul 23, 2002
Jul 23, 2002
282
283
strcpy(info->filename, name);
info->last_mod_time = modtime;
Sep 26, 2004
Sep 26, 2004
284
285
return(info);
Mar 25, 2002
Mar 25, 2002
286
287
GRP_openArchive_failed:
Sep 26, 2004
Sep 26, 2004
288
if (info != NULL)
Mar 25, 2002
Mar 25, 2002
289
{
Sep 26, 2004
Sep 26, 2004
290
if (info->filename != NULL)
Mar 14, 2005
Mar 14, 2005
291
allocator.Free(info->filename);
Sep 26, 2004
Sep 26, 2004
292
if (info->entries != NULL)
Mar 14, 2005
Mar 14, 2005
293
294
allocator.Free(info->entries);
allocator.Free(info);
Mar 25, 2002
Mar 25, 2002
295
296
297
} /* if */
return(NULL);
298
299
300
} /* GRP_openArchive */
Sep 29, 2004
Sep 29, 2004
301
static void GRP_enumerateFiles(dvoid *opaque, const char *dname,
Sep 18, 2005
Sep 18, 2005
302
303
int omitSymLinks, PHYSFS_EnumFilesCallback cb,
const char *origdir, void *callbackdata)
Jul 23, 2002
Jul 23, 2002
305
/* no directories in GRP files. */
Mar 28, 2007
Mar 28, 2007
306
if (*dname == '\0')
Sep 29, 2004
Sep 29, 2004
307
308
309
310
311
{
GRPinfo *info = (GRPinfo *) opaque;
GRPentry *entry = info->entries;
PHYSFS_uint32 max = info->entryCount;
PHYSFS_uint32 i;
Sep 29, 2004
Sep 29, 2004
313
for (i = 0; i < max; i++, entry++)
Sep 18, 2005
Sep 18, 2005
314
cb(callbackdata, origdir, entry->name);
Sep 29, 2004
Sep 29, 2004
315
} /* if */
316
317
318
} /* GRP_enumerateFiles */
Jul 23, 2002
Jul 23, 2002
319
static GRPentry *grp_find_entry(GRPinfo *info, const char *name)
Jul 23, 2002
Jul 23, 2002
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)
Jul 23, 2002
Jul 23, 2002
338
339
340
341
342
343
344
345
346
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
348
349
BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
} /* grp_find_entry */
Sep 26, 2004
Sep 26, 2004
352
static int GRP_exists(dvoid *opaque, const char *name)
Sep 26, 2004
Sep 26, 2004
354
return(grp_find_entry((GRPinfo *) opaque, name) != NULL);
355
356
357
} /* GRP_exists */
Sep 26, 2004
Sep 26, 2004
358
static int GRP_isDirectory(dvoid *opaque, const char *name, int *fileExists)
Sep 26, 2004
Sep 26, 2004
360
*fileExists = GRP_exists(opaque, name);
361
362
363
364
return(0); /* never directories in a groupfile. */
} /* GRP_isDirectory */
Sep 26, 2004
Sep 26, 2004
365
static int GRP_isSymLink(dvoid *opaque, const char *name, int *fileExists)
Sep 26, 2004
Sep 26, 2004
367
*fileExists = GRP_exists(opaque, name);
368
369
370
371
return(0); /* never symlinks in a groupfile. */
} /* GRP_isSymLink */
Sep 26, 2004
Sep 26, 2004
372
static PHYSFS_sint64 GRP_getLastModTime(dvoid *opaque,
Aug 21, 2002
Aug 21, 2002
373
374
const char *name,
int *fileExists)
Jun 6, 2002
Jun 6, 2002
375
{
Sep 26, 2004
Sep 26, 2004
376
GRPinfo *info = (GRPinfo *) opaque;
Aug 21, 2002
Aug 21, 2002
377
PHYSFS_sint64 retval = -1;
Jul 25, 2002
Jul 25, 2002
378
Aug 21, 2002
Aug 21, 2002
379
380
*fileExists = (grp_find_entry(info, name) != NULL);
if (*fileExists) /* use time of GRP itself in the physical filesystem. */
Sep 26, 2004
Sep 26, 2004
381
retval = info->last_mod_time;
Aug 21, 2002
Aug 21, 2002
382
383
return(retval);
Jun 6, 2002
Jun 6, 2002
384
385
386
} /* GRP_getLastModTime */
Sep 26, 2004
Sep 26, 2004
387
static fvoid *GRP_openRead(dvoid *opaque, const char *fnm, int *fileExists)
Sep 26, 2004
Sep 26, 2004
389
GRPinfo *info = (GRPinfo *) opaque;
390
GRPfileinfo *finfo;
Jul 23, 2002
Jul 23, 2002
391
GRPentry *entry;
Aug 21, 2002
Aug 21, 2002
393
394
entry = grp_find_entry(info, fnm);
*fileExists = (entry != NULL);
Jul 23, 2002
Jul 23, 2002
395
BAIL_IF_MACRO(entry == NULL, NULL, NULL);
Mar 14, 2005
Mar 14, 2005
397
finfo = (GRPfileinfo *) allocator.Malloc(sizeof (GRPfileinfo));
Sep 26, 2004
Sep 26, 2004
398
BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL);
Jul 23, 2002
Jul 23, 2002
400
finfo->handle = __PHYSFS_platformOpenRead(info->filename);
Mar 25, 2002
Mar 25, 2002
401
if ( (finfo->handle == NULL) ||
Jul 23, 2002
Jul 23, 2002
402
(!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) )
Mar 25, 2002
Mar 25, 2002
403
{
Mar 14, 2005
Mar 14, 2005
404
allocator.Free(finfo);
Mar 25, 2002
Mar 25, 2002
405
406
407
return(NULL);
} /* if */
Jul 23, 2002
Jul 23, 2002
408
409
finfo->curPos = 0;
finfo->entry = entry;
Sep 26, 2004
Sep 26, 2004
410
return(finfo);
411
412
} /* GRP_openRead */
Aug 21, 2002
Aug 21, 2002
413
Sep 26, 2004
Sep 26, 2004
414
static fvoid *GRP_openWrite(dvoid *opaque, const char *name)
Aug 21, 2002
Aug 21, 2002
415
416
417
418
419
{
BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
} /* GRP_openWrite */
Sep 26, 2004
Sep 26, 2004
420
static fvoid *GRP_openAppend(dvoid *opaque, const char *name)
Aug 21, 2002
Aug 21, 2002
421
422
423
424
425
{
BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
} /* GRP_openAppend */
Sep 26, 2004
Sep 26, 2004
426
static int GRP_remove(dvoid *opaque, const char *name)
Aug 21, 2002
Aug 21, 2002
427
428
429
430
431
{
BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
} /* GRP_remove */
Sep 26, 2004
Sep 26, 2004
432
static int GRP_mkdir(dvoid *opaque, const char *name)
Aug 21, 2002
Aug 21, 2002
433
434
435
436
{
BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
} /* GRP_mkdir */
Sep 29, 2004
Sep 29, 2004
437
438
439
440
441
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_GRP =
{
"GRP",
GRP_ARCHIVE_DESCRIPTION,
Jan 1, 2006
Jan 1, 2006
442
"Ryan C. Gordon <icculus@icculus.org>",
Sep 29, 2004
Sep 29, 2004
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 */
};
May 10, 2002
May 10, 2002
472
473
#endif /* defined PHYSFS_SUPPORTS_GRP */
474
/* end of grp.c ... */