Skip to content

Latest commit

 

History

History
455 lines (359 loc) · 13 KB

grp.c

File metadata and controls

455 lines (359 loc) · 13 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
27
28
29
30
/*
* 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 ...)
*
* As it was never a concern in the DOS-based Duke Nukem days, I treat these
* archives as CASE-INSENSITIVE. Opening "myfile.txt" and "MYFILE.TXT" both
* work, and get the same file.
*
* Please see the file LICENSE in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
May 10, 2002
May 10, 2002
31
32
33
34
35
36
#if HAVE_CONFIG_H
# include <config.h>
#endif
#if (defined PHYSFS_SUPPORTS_GRP)
37
38
39
40
41
42
43
44
45
46
47
48
49
#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"
typedef struct
{
Mar 25, 2002
Mar 25, 2002
50
51
void *handle;
char *filename;
Mar 24, 2002
Mar 24, 2002
52
PHYSFS_uint32 totalEntries;
53
54
55
56
} GRPinfo;
typedef struct
{
Mar 25, 2002
Mar 25, 2002
57
58
59
void *handle;
PHYSFS_uint64 startPos;
PHYSFS_uint64 size;
60
61
62
} GRPfileinfo;
Sep 2, 2001
Sep 2, 2001
63
static void GRP_dirClose(DirHandle *h);
Mar 24, 2002
Mar 24, 2002
64
65
static PHYSFS_sint64 GRP_read(FileHandle *handle, void *buffer,
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
Sep 2, 2001
Sep 2, 2001
66
static int GRP_eof(FileHandle *handle);
Mar 24, 2002
Mar 24, 2002
67
68
69
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
70
71
72
73
74
75
76
77
78
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
79
static PHYSFS_sint64 GRP_getLastModTime(DirHandle *h, const char *name);
Sep 2, 2001
Sep 2, 2001
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
static FileHandle *GRP_openRead(DirHandle *h, const char *name);
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 =
{
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
102
GRP_getLastModTime, /* getLastModTime() method */
Sep 2, 2001
Sep 2, 2001
103
104
105
106
107
108
109
110
111
112
113
114
GRP_openRead, /* openRead() method */
NULL, /* openWrite() method */
NULL, /* openAppend() method */
NULL, /* remove() method */
NULL, /* mkdir() method */
GRP_dirClose /* dirClose() method */
};
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_GRP =
{
"GRP",
"Build engine Groupfile format",
Sep 14, 2001
Sep 14, 2001
115
"Ryan C. Gordon <icculus@clutteredmind.org>",
Sep 2, 2001
Sep 2, 2001
116
117
118
119
120
121
122
"http://www.icculus.org/physfs/",
};
static void GRP_dirClose(DirHandle *h)
{
Mar 25, 2002
Mar 25, 2002
123
124
__PHYSFS_platformClose( ((GRPinfo *) h->opaque)->handle );
free(((GRPinfo *) h->opaque)->filename);
Sep 2, 2001
Sep 2, 2001
125
126
127
free(h->opaque);
free(h);
} /* GRP_dirClose */
Mar 24, 2002
Mar 24, 2002
130
131
static PHYSFS_sint64 GRP_read(FileHandle *handle, void *buffer,
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
132
133
{
GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
Mar 25, 2002
Mar 25, 2002
134
135
136
void *fh = finfo->handle;
PHYSFS_sint64 curPos = __PHYSFS_platformTell(fh);
PHYSFS_uint64 bytesLeft = (finfo->startPos + finfo->size) - curPos;
Apr 3, 2002
Apr 3, 2002
137
PHYSFS_uint64 objsLeft = (bytesLeft / objSize);
138
139
if (objsLeft < objCount)
Apr 12, 2002
Apr 12, 2002
140
objCount = (PHYSFS_uint32) objsLeft;
Mar 25, 2002
Mar 25, 2002
142
return(__PHYSFS_platformRead(fh, buffer, objSize, objCount));
143
144
145
146
147
148
} /* GRP_read */
static int GRP_eof(FileHandle *handle)
{
GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
Mar 25, 2002
Mar 25, 2002
149
void *fh = finfo->handle;
Apr 12, 2002
Apr 12, 2002
150
151
152
PHYSFS_sint64 pos = __PHYSFS_platformTell(fh);
BAIL_IF_MACRO(pos < 0, NULL, 1); /* (*shrug*) */
return(pos >= (PHYSFS_sint64) (finfo->startPos + finfo->size));
153
154
155
} /* GRP_eof */
Mar 24, 2002
Mar 24, 2002
156
static PHYSFS_sint64 GRP_tell(FileHandle *handle)
157
158
{
GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
Mar 25, 2002
Mar 25, 2002
159
return(__PHYSFS_platformTell(finfo->handle) - finfo->startPos);
160
161
162
} /* GRP_tell */
Mar 24, 2002
Mar 24, 2002
163
static int GRP_seek(FileHandle *handle, PHYSFS_uint64 offset)
164
165
{
GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
Apr 3, 2002
Apr 3, 2002
166
PHYSFS_uint64 newPos = (finfo->startPos + offset);
167
168
169
BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0);
BAIL_IF_MACRO(newPos > finfo->startPos + finfo->size, ERR_PAST_EOF, 0);
Mar 25, 2002
Mar 25, 2002
170
return(__PHYSFS_platformSeek(finfo->handle, newPos));
171
172
173
} /* GRP_seek */
Mar 24, 2002
Mar 24, 2002
174
static PHYSFS_sint64 GRP_fileLength(FileHandle *handle)
Jul 9, 2001
Jul 9, 2001
175
176
177
178
179
180
{
GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
return(finfo->size);
} /* GRP_fileLength */
181
182
static int GRP_fileClose(FileHandle *handle)
{
Mar 25, 2002
Mar 25, 2002
183
184
185
GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
BAIL_IF_MACRO(__PHYSFS_platformClose(finfo->handle), NULL, 0);
186
187
188
189
190
191
free(handle->opaque);
free(handle);
return(1);
} /* GRP_fileClose */
Mar 25, 2002
Mar 25, 2002
192
193
static int openGrp(const char *filename, int forWriting,
void **fh, PHYSFS_sint32 *count)
Mar 25, 2002
Mar 25, 2002
195
PHYSFS_uint8 buf[12];
196
197
198
199
*fh = NULL;
BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
Mar 25, 2002
Mar 25, 2002
200
201
*fh = __PHYSFS_platformOpenRead(filename);
BAIL_IF_MACRO(*fh == NULL, NULL, 0);
Mar 25, 2002
Mar 25, 2002
203
204
205
206
207
208
209
210
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 */
Mar 25, 2002
Mar 25, 2002
212
213
if (__PHYSFS_platformRead(*fh, count, sizeof (PHYSFS_sint32), 1) != 1)
goto openGrp_failed;
Apr 5, 2002
Apr 5, 2002
215
216
*count = PHYSFS_swapSLE32(*count);
217
return(1);
Mar 25, 2002
Mar 25, 2002
218
219
220
221
222
223
224
225
openGrp_failed:
if (*fh != NULL)
__PHYSFS_platformClose(*fh);
*count = -1;
*fh = NULL;
return(0);
226
227
228
229
230
} /* openGrp */
static int GRP_isArchive(const char *filename, int forWriting)
{
Mar 25, 2002
Mar 25, 2002
231
void *fh;
232
233
234
235
int fileCount;
int retval = openGrp(filename, forWriting, &fh, &fileCount);
if (fh != NULL)
Mar 25, 2002
Mar 25, 2002
236
__PHYSFS_platformClose(fh);
237
238
239
240
241
242
243
return(retval);
} /* GRP_isArchive */
static DirHandle *GRP_openArchive(const char *name, int forWriting)
{
Mar 25, 2002
Mar 25, 2002
244
void *fh = NULL;
245
246
247
248
249
250
251
int fileCount;
DirHandle *retval = malloc(sizeof (DirHandle));
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
retval->opaque = malloc(sizeof (GRPinfo));
if (retval->opaque == NULL)
{
Mar 25, 2002
Mar 25, 2002
252
253
__PHYSFS_setError(ERR_OUT_OF_MEMORY);
goto GRP_openArchive_failed;
254
255
} /* if */
Mar 25, 2002
Mar 25, 2002
256
257
((GRPinfo *) retval->opaque)->filename = (char *) malloc(strlen(name) + 1);
if (((GRPinfo *) retval->opaque)->filename == NULL)
Mar 25, 2002
Mar 25, 2002
259
260
__PHYSFS_setError(ERR_OUT_OF_MEMORY);
goto GRP_openArchive_failed;
261
262
} /* if */
Mar 25, 2002
Mar 25, 2002
263
264
265
266
if (!openGrp(name, forWriting, &fh, &fileCount))
goto GRP_openArchive_failed;
strcpy(((GRPinfo *) retval->opaque)->filename, name);
267
268
269
270
((GRPinfo *) retval->opaque)->handle = fh;
((GRPinfo *) retval->opaque)->totalEntries = fileCount;
retval->funcs = &__PHYSFS_DirFunctions_GRP;
return(retval);
Mar 25, 2002
Mar 25, 2002
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
GRP_openArchive_failed:
if (retval != NULL)
{
if (retval->opaque != NULL)
{
if ( ((GRPinfo *) retval->opaque)->filename != NULL )
free( ((GRPinfo *) retval->opaque)->filename );
free(retval->opaque);
} /* if */
free(retval);
} /* if */
if (fh != NULL)
__PHYSFS_platformClose(fh);
return(NULL);
288
289
290
} /* GRP_openArchive */
Jul 16, 2001
Jul 16, 2001
291
292
293
static LinkedStringList *GRP_enumerateFiles(DirHandle *h,
const char *dirname,
int omitSymLinks)
Mar 25, 2002
Mar 25, 2002
295
PHYSFS_uint8 buf[16];
296
GRPinfo *g = (GRPinfo *) (h->opaque);
Mar 25, 2002
Mar 25, 2002
297
void *fh = g->handle;
Apr 3, 2002
Apr 3, 2002
298
PHYSFS_uint32 i;
299
300
301
302
LinkedStringList *retval = NULL;
LinkedStringList *l = NULL;
LinkedStringList *prev = NULL;
Mar 25, 2002
Mar 25, 2002
303
/* !!! FIXME: Does this consider "/" ? */
Jul 15, 2001
Jul 15, 2001
304
305
306
if (*dirname != '\0') /* no directories in GRP files. */
return(NULL);
307
/* jump to first file entry... */
Mar 25, 2002
Mar 25, 2002
308
BAIL_IF_MACRO(!__PHYSFS_platformSeek(fh, 16), NULL, NULL);
309
310
311
for (i = 0; i < g->totalEntries; i++)
{
Mar 25, 2002
Mar 25, 2002
312
BAIL_IF_MACRO(__PHYSFS_platformRead(fh, buf, 16, 1) != 1, NULL, retval);
313
314
315
316
buf[12] = '\0'; /* FILENAME.EXT is all you get. */
l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
Jul 16, 2001
Jul 16, 2001
317
if (l == NULL)
318
319
break;
Apr 1, 2002
Apr 1, 2002
320
l->str = (char *) malloc(strlen((const char *) buf) + 1);
321
322
323
324
325
326
if (l->str == NULL)
{
free(l);
break;
} /* if */
Apr 1, 2002
Apr 1, 2002
327
strcpy(l->str, (const char *) buf);
Jul 16, 2001
Jul 16, 2001
328
329
330
331
332
333
334
335
336
337
338
339
340
341
if (retval == NULL)
retval = l;
else
prev->next = l;
prev = l;
l->next = NULL;
} /* for */
return(retval);
} /* GRP_enumerateFiles */
Mar 24, 2002
Mar 24, 2002
342
static PHYSFS_sint32 getFileEntry(DirHandle *h, const char *name,
Apr 1, 2002
Apr 1, 2002
343
PHYSFS_uint32 *size)
Mar 25, 2002
Mar 25, 2002
345
PHYSFS_uint8 buf[16];
346
GRPinfo *g = (GRPinfo *) (h->opaque);
Mar 25, 2002
Mar 25, 2002
347
void *fh = g->handle;
Apr 3, 2002
Apr 3, 2002
348
PHYSFS_uint32 i;
349
char *ptr;
Jul 9, 2001
Jul 9, 2001
350
int retval = (g->totalEntries + 1) * 16; /* offset of raw file data */
351
352
353
354
355
356
357
358
359
360
361
362
/* Rule out filenames to avoid unneeded file i/o... */
if (strchr(name, '/') != NULL) /* no directories in groupfiles. */
return(-1);
ptr = strchr(name, '.');
if ((ptr) && (strlen(ptr) > 4)) /* 3 char extension at most. */
return(-1);
if (strlen(name) > 12)
return(-1);
Mar 25, 2002
Mar 25, 2002
363
364
/* jump to first file entry... */
BAIL_IF_MACRO(!__PHYSFS_platformSeek(fh, 16), NULL, -1);
365
366
367
for (i = 0; i < g->totalEntries; i++)
{
Mar 24, 2002
Mar 24, 2002
368
PHYSFS_sint32 l = 0;
Mar 25, 2002
Mar 25, 2002
370
371
BAIL_IF_MACRO(__PHYSFS_platformRead(fh, buf, 12, 1) != 1, NULL, -1);
BAIL_IF_MACRO(__PHYSFS_platformRead(fh, &l, sizeof (l), 1) != 1, NULL, -1);
Apr 5, 2002
Apr 5, 2002
372
l = PHYSFS_swapSLE32(l);
373
374
375
buf[12] = '\0'; /* FILENAME.EXT is all you get. */
Apr 1, 2002
Apr 1, 2002
376
if (__PHYSFS_platformStricmp((const char *) buf, name) == 0)
377
378
{
if (size != NULL)
Mar 24, 2002
Mar 24, 2002
379
*size = l;
380
381
382
return(retval);
} /* if */
Mar 24, 2002
Mar 24, 2002
383
retval += l;
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
} /* for */
return(-1); /* not found. */
} /* getFileEntry */
static int GRP_exists(DirHandle *h, const char *name)
{
return(getFileEntry(h, name, NULL) != -1);
} /* 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
408
409
410
411
412
413
414
static PHYSFS_sint64 GRP_getLastModTime(DirHandle *h, const char *name)
{
/* Just return the time of the GRP itself in the physical filesystem. */
return(__PHYSFS_platformGetLastModTime(((GRPinfo *) h->opaque)->filename));
} /* GRP_getLastModTime */
415
416
static FileHandle *GRP_openRead(DirHandle *h, const char *name)
{
Mar 25, 2002
Mar 25, 2002
417
const char *filename = ((GRPinfo *) h->opaque)->filename;
418
419
FileHandle *retval;
GRPfileinfo *finfo;
Mar 25, 2002
Mar 25, 2002
420
PHYSFS_uint32 size;
Mar 24, 2002
Mar 24, 2002
421
PHYSFS_sint32 offset;
422
423
424
425
426
427
428
429
430
431
432
433
434
offset = getFileEntry(h, name, &size);
BAIL_IF_MACRO(offset == -1, ERR_NO_SUCH_FILE, NULL);
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);
BAIL_IF_MACRO(1, ERR_OUT_OF_MEMORY, NULL);
} /* if */
Mar 25, 2002
Mar 25, 2002
435
436
437
438
439
440
441
442
443
finfo->handle = __PHYSFS_platformOpenRead(filename);
if ( (finfo->handle == NULL) ||
(!__PHYSFS_platformSeek(finfo->handle, offset)) )
{
free(finfo);
free(retval);
return(NULL);
} /* if */
444
445
446
447
448
449
450
451
finfo->startPos = offset;
finfo->size = size;
retval->opaque = (void *) finfo;
retval->funcs = &__PHYSFS_FileFunctions_GRP;
retval->dirHandle = h;
return(retval);
} /* GRP_openRead */
May 10, 2002
May 10, 2002
452
453
#endif /* defined PHYSFS_SUPPORTS_GRP */
454
/* end of grp.c ... */