Skip to content

Latest commit

 

History

History
467 lines (363 loc) · 12.7 KB

grp.c

File metadata and controls

467 lines (363 loc) · 12.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
#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
{
Aug 20, 2002
Aug 20, 2002
194
195
196
GRPentry *a = (GRPentry *) _a;
return(strcmp(a[one].name, a[two].name));
} /* grp_entry_cmp */
Jul 23, 2002
Jul 23, 2002
197
198
Aug 20, 2002
Aug 20, 2002
199
static void grp_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
Jul 23, 2002
Jul 23, 2002
200
201
{
GRPentry tmp;
Aug 20, 2002
Aug 20, 2002
202
203
204
205
206
207
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
208
209
210
static int grp_load_entries(const char *name, int forWriting, GRPinfo *info)
Mar 25, 2002
Mar 25, 2002
212
void *fh = NULL;
Jul 23, 2002
Jul 23, 2002
213
PHYSFS_uint32 fileCount;
Jul 23, 2002
Jul 23, 2002
214
PHYSFS_uint32 location = 16; /* sizeof sig. */
Jul 23, 2002
Jul 23, 2002
215
216
217
218
219
GRPentry *entry;
char *ptr;
BAIL_IF_MACRO(!grp_open(name, forWriting, &fh, &fileCount), NULL, 0);
info->entryCount = fileCount;
Mar 14, 2005
Mar 14, 2005
220
info->entries = (GRPentry *) allocator.Malloc(sizeof(GRPentry)*fileCount);
Jul 23, 2002
Jul 23, 2002
221
222
223
224
225
226
if (info->entries == NULL)
{
__PHYSFS_platformClose(fh);
BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
} /* if */
Jul 23, 2002
Jul 23, 2002
227
228
location += (16 * fileCount);
Jul 23, 2002
Jul 23, 2002
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
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
249
location += entry->size;
Jul 23, 2002
Jul 23, 2002
250
251
252
253
} /* for */
__PHYSFS_platformClose(fh);
Aug 20, 2002
Aug 20, 2002
254
255
__PHYSFS_sort(info->entries, info->entryCount,
grp_entry_cmp, grp_entry_swap);
Jul 23, 2002
Jul 23, 2002
256
257
258
259
return(1);
} /* grp_load_entries */
Sep 26, 2004
Sep 26, 2004
260
static void *GRP_openArchive(const char *name, int forWriting)
Jul 23, 2002
Jul 23, 2002
261
262
{
PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name);
Mar 14, 2005
Mar 14, 2005
263
GRPinfo *info = (GRPinfo *) allocator.Malloc(sizeof (GRPinfo));
Sep 26, 2004
Sep 26, 2004
265
BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, 0);
Jul 23, 2002
Jul 23, 2002
267
memset(info, '\0', sizeof (GRPinfo));
Mar 14, 2005
Mar 14, 2005
268
info->filename = (char *) allocator.Malloc(strlen(name) + 1);
Mar 13, 2005
Mar 13, 2005
269
GOTO_IF_MACRO(!info->filename, ERR_OUT_OF_MEMORY, GRP_openArchive_failed);
Jul 23, 2002
Jul 23, 2002
271
if (!grp_load_entries(name, forWriting, info))
Mar 25, 2002
Mar 25, 2002
272
273
goto GRP_openArchive_failed;
Jul 23, 2002
Jul 23, 2002
274
275
strcpy(info->filename, name);
info->last_mod_time = modtime;
Sep 26, 2004
Sep 26, 2004
276
277
return(info);
Mar 25, 2002
Mar 25, 2002
278
279
GRP_openArchive_failed:
Sep 26, 2004
Sep 26, 2004
280
if (info != NULL)
Mar 25, 2002
Mar 25, 2002
281
{
Sep 26, 2004
Sep 26, 2004
282
if (info->filename != NULL)
Mar 14, 2005
Mar 14, 2005
283
allocator.Free(info->filename);
Sep 26, 2004
Sep 26, 2004
284
if (info->entries != NULL)
Mar 14, 2005
Mar 14, 2005
285
286
allocator.Free(info->entries);
allocator.Free(info);
Mar 25, 2002
Mar 25, 2002
287
288
289
} /* if */
return(NULL);
290
291
292
} /* GRP_openArchive */
Sep 29, 2004
Sep 29, 2004
293
static void GRP_enumerateFiles(dvoid *opaque, const char *dname,
Sep 18, 2005
Sep 18, 2005
294
295
int omitSymLinks, PHYSFS_EnumFilesCallback cb,
const char *origdir, void *callbackdata)
Jul 23, 2002
Jul 23, 2002
297
/* no directories in GRP files. */
Sep 29, 2004
Sep 29, 2004
298
299
300
301
302
303
if (*dname != '\0')
{
GRPinfo *info = (GRPinfo *) opaque;
GRPentry *entry = info->entries;
PHYSFS_uint32 max = info->entryCount;
PHYSFS_uint32 i;
Sep 29, 2004
Sep 29, 2004
305
for (i = 0; i < max; i++, entry++)
Sep 18, 2005
Sep 18, 2005
306
cb(callbackdata, origdir, entry->name);
Sep 29, 2004
Sep 29, 2004
307
} /* if */
308
309
310
} /* GRP_enumerateFiles */
Jul 23, 2002
Jul 23, 2002
311
static GRPentry *grp_find_entry(GRPinfo *info, const char *name)
Jul 23, 2002
Jul 23, 2002
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
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
330
331
332
333
334
335
336
337
338
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
340
341
BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
} /* grp_find_entry */
Sep 26, 2004
Sep 26, 2004
344
static int GRP_exists(dvoid *opaque, const char *name)
Sep 26, 2004
Sep 26, 2004
346
return(grp_find_entry((GRPinfo *) opaque, name) != NULL);
347
348
349
} /* GRP_exists */
Sep 26, 2004
Sep 26, 2004
350
static int GRP_isDirectory(dvoid *opaque, const char *name, int *fileExists)
Sep 26, 2004
Sep 26, 2004
352
*fileExists = GRP_exists(opaque, name);
353
354
355
356
return(0); /* never directories in a groupfile. */
} /* GRP_isDirectory */
Sep 26, 2004
Sep 26, 2004
357
static int GRP_isSymLink(dvoid *opaque, const char *name, int *fileExists)
Sep 26, 2004
Sep 26, 2004
359
*fileExists = GRP_exists(opaque, name);
360
361
362
363
return(0); /* never symlinks in a groupfile. */
} /* GRP_isSymLink */
Sep 26, 2004
Sep 26, 2004
364
static PHYSFS_sint64 GRP_getLastModTime(dvoid *opaque,
Aug 21, 2002
Aug 21, 2002
365
366
const char *name,
int *fileExists)
Jun 6, 2002
Jun 6, 2002
367
{
Sep 26, 2004
Sep 26, 2004
368
GRPinfo *info = (GRPinfo *) opaque;
Aug 21, 2002
Aug 21, 2002
369
PHYSFS_sint64 retval = -1;
Jul 25, 2002
Jul 25, 2002
370
Aug 21, 2002
Aug 21, 2002
371
372
*fileExists = (grp_find_entry(info, name) != NULL);
if (*fileExists) /* use time of GRP itself in the physical filesystem. */
Sep 26, 2004
Sep 26, 2004
373
retval = info->last_mod_time;
Aug 21, 2002
Aug 21, 2002
374
375
return(retval);
Jun 6, 2002
Jun 6, 2002
376
377
378
} /* GRP_getLastModTime */
Sep 26, 2004
Sep 26, 2004
379
static fvoid *GRP_openRead(dvoid *opaque, const char *fnm, int *fileExists)
Sep 26, 2004
Sep 26, 2004
381
GRPinfo *info = (GRPinfo *) opaque;
382
GRPfileinfo *finfo;
Jul 23, 2002
Jul 23, 2002
383
GRPentry *entry;
Aug 21, 2002
Aug 21, 2002
385
386
entry = grp_find_entry(info, fnm);
*fileExists = (entry != NULL);
Jul 23, 2002
Jul 23, 2002
387
BAIL_IF_MACRO(entry == NULL, NULL, NULL);
Mar 14, 2005
Mar 14, 2005
389
finfo = (GRPfileinfo *) allocator.Malloc(sizeof (GRPfileinfo));
Sep 26, 2004
Sep 26, 2004
390
BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL);
Jul 23, 2002
Jul 23, 2002
392
finfo->handle = __PHYSFS_platformOpenRead(info->filename);
Mar 25, 2002
Mar 25, 2002
393
if ( (finfo->handle == NULL) ||
Jul 23, 2002
Jul 23, 2002
394
(!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) )
Mar 25, 2002
Mar 25, 2002
395
{
Mar 14, 2005
Mar 14, 2005
396
allocator.Free(finfo);
Mar 25, 2002
Mar 25, 2002
397
398
399
return(NULL);
} /* if */
Jul 23, 2002
Jul 23, 2002
400
401
finfo->curPos = 0;
finfo->entry = entry;
Sep 26, 2004
Sep 26, 2004
402
return(finfo);
403
404
} /* GRP_openRead */
Aug 21, 2002
Aug 21, 2002
405
Sep 26, 2004
Sep 26, 2004
406
static fvoid *GRP_openWrite(dvoid *opaque, const char *name)
Aug 21, 2002
Aug 21, 2002
407
408
409
410
411
{
BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
} /* GRP_openWrite */
Sep 26, 2004
Sep 26, 2004
412
static fvoid *GRP_openAppend(dvoid *opaque, const char *name)
Aug 21, 2002
Aug 21, 2002
413
414
415
416
417
{
BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
} /* GRP_openAppend */
Sep 26, 2004
Sep 26, 2004
418
static int GRP_remove(dvoid *opaque, const char *name)
Aug 21, 2002
Aug 21, 2002
419
420
421
422
423
{
BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
} /* GRP_remove */
Sep 26, 2004
Sep 26, 2004
424
static int GRP_mkdir(dvoid *opaque, const char *name)
Aug 21, 2002
Aug 21, 2002
425
426
427
428
{
BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
} /* GRP_mkdir */
Sep 29, 2004
Sep 29, 2004
429
430
431
432
433
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_GRP =
{
"GRP",
GRP_ARCHIVE_DESCRIPTION,
Jan 1, 2006
Jan 1, 2006
434
"Ryan C. Gordon <icculus@icculus.org>",
Sep 29, 2004
Sep 29, 2004
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
"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
464
465
#endif /* defined PHYSFS_SUPPORTS_GRP */
466
/* end of grp.c ... */