/
archiver_grp.c
462 lines (359 loc) · 12.2 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 ...)
*
22
* Please see the file LICENSE.txt in the source's root directory.
23
24
25
26
*
* This file written by Ryan C. Gordon.
*/
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"
37
38
39
typedef struct
{
char name[13];
40
41
PHYSFS_uint32 startPos;
PHYSFS_uint32 size;
42
43
} GRPentry;
44
45
typedef struct
{
46
char *filename;
47
48
PHYSFS_uint32 entryCount;
GRPentry *entries;
49
50
51
52
} GRPinfo;
typedef struct
{
53
void *handle;
54
GRPentry *entry;
55
PHYSFS_uint32 curPos;
56
57
58
} GRPfileinfo;
59
60
61
62
63
64
static inline int readAll(void *fh, void *buf, const PHYSFS_uint64 len)
{
return (__PHYSFS_platformRead(fh, buf, len) == len);
} /* readAll */
65
static void GRP_dirClose(dvoid *opaque)
66
{
67
GRPinfo *info = ((GRPinfo *) opaque);
68
69
70
allocator.Free(info->filename);
allocator.Free(info->entries);
allocator.Free(info);
71
} /* GRP_dirClose */
72
73
74
static PHYSFS_sint64 GRP_read(fvoid *opaque, void *buffer, PHYSFS_uint64 len)
75
{
76
GRPfileinfo *finfo = (GRPfileinfo *) opaque;
77
78
const GRPentry *entry = finfo->entry;
const PHYSFS_uint64 bytesLeft = (PHYSFS_uint64)(entry->size-finfo->curPos);
79
PHYSFS_sint64 rc;
80
81
82
if (bytesLeft < len)
len = bytesLeft;
83
84
rc = __PHYSFS_platformRead(finfo->handle, buffer, len);
85
if (rc > 0)
86
finfo->curPos += (PHYSFS_uint32) rc;
87
88
return rc;
89
90
91
} /* GRP_read */
92
static PHYSFS_sint64 GRP_write(fvoid *f, const void *buf, PHYSFS_uint64 len)
93
94
95
96
97
{
BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
} /* GRP_write */
98
static int GRP_eof(fvoid *opaque)
99
{
100
GRPfileinfo *finfo = (GRPfileinfo *) opaque;
101
GRPentry *entry = finfo->entry;
102
return (finfo->curPos >= entry->size);
103
104
105
} /* GRP_eof */
106
static PHYSFS_sint64 GRP_tell(fvoid *opaque)
107
{
108
return ((GRPfileinfo *) opaque)->curPos;
109
110
111
} /* GRP_tell */
112
static int GRP_seek(fvoid *opaque, PHYSFS_uint64 offset)
113
{
114
GRPfileinfo *finfo = (GRPfileinfo *) opaque;
115
116
GRPentry *entry = finfo->entry;
int rc;
117
118
BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0);
119
120
BAIL_IF_MACRO(offset >= entry->size, ERR_PAST_EOF, 0);
rc = __PHYSFS_platformSeek(finfo->handle, entry->startPos + offset);
121
if (rc)
122
finfo->curPos = (PHYSFS_uint32) offset;
123
124
return rc;
125
126
127
} /* GRP_seek */
128
static PHYSFS_sint64 GRP_fileLength(fvoid *opaque)
129
{
130
GRPfileinfo *finfo = (GRPfileinfo *) opaque;
131
return ((PHYSFS_sint64) finfo->entry->size);
132
133
134
} /* GRP_fileLength */
135
static int GRP_fileClose(fvoid *opaque)
136
{
137
GRPfileinfo *finfo = (GRPfileinfo *) opaque;
138
BAIL_IF_MACRO(!__PHYSFS_platformClose(finfo->handle), NULL, 0);
139
allocator.Free(finfo);
140
return 1;
141
142
143
} /* GRP_fileClose */
144
145
static int grp_open(const char *filename, int forWriting,
void **fh, PHYSFS_uint32 *count)
146
{
147
PHYSFS_uint8 buf[12];
148
149
150
151
*fh = NULL;
BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
152
153
*fh = __PHYSFS_platformOpenRead(filename);
BAIL_IF_MACRO(*fh == NULL, NULL, 0);
154
155
if (!readAll(*fh, buf, 12))
156
157
158
159
160
161
162
goto openGrp_failed;
if (memcmp(buf, "KenSilverman", 12) != 0)
{
__PHYSFS_setError(ERR_UNSUPPORTED_ARCHIVE);
goto openGrp_failed;
} /* if */
163
164
if (!readAll(*fh, count, sizeof (PHYSFS_uint32)))
165
goto openGrp_failed;
166
167
*count = PHYSFS_swapULE32(*count);
168
169
return 1;
170
171
172
173
174
175
176
openGrp_failed:
if (*fh != NULL)
__PHYSFS_platformClose(*fh);
*count = -1;
*fh = NULL;
177
return 0;
178
} /* grp_open */
179
180
181
static int grp_entry_cmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
182
{
183
184
185
if (one != two)
{
const GRPentry *a = (const GRPentry *) _a;
186
return (strcmp(a[one].name, a[two].name));
187
188
189
} /* if */
return 0;
190
} /* grp_entry_cmp */
191
192
193
static void grp_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
194
{
195
196
197
198
199
200
201
202
203
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 */
204
} /* grp_entry_swap */
205
206
207
static int grp_load_entries(const char *name, int forWriting, GRPinfo *info)
208
{
209
void *fh = NULL;
210
PHYSFS_uint32 fileCount;
211
PHYSFS_uint32 location = 16; /* sizeof sig. */
212
213
214
215
216
GRPentry *entry;
char *ptr;
BAIL_IF_MACRO(!grp_open(name, forWriting, &fh, &fileCount), NULL, 0);
info->entryCount = fileCount;
217
info->entries = (GRPentry *) allocator.Malloc(sizeof(GRPentry)*fileCount);
218
219
220
221
222
223
if (info->entries == NULL)
{
__PHYSFS_platformClose(fh);
BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
} /* if */
224
225
location += (16 * fileCount);
226
227
for (entry = info->entries; fileCount > 0; fileCount--, entry++)
{
228
229
if ( (!readAll(fh, &entry->name, 12)) ||
(!readAll(fh, &entry->size, sizeof (PHYSFS_uint32))) )
230
231
{
__PHYSFS_platformClose(fh);
232
return 0;
233
234
235
236
237
238
239
240
} /* if */
entry->name[12] = '\0'; /* name isn't null-terminated in file. */
if ((ptr = strchr(entry->name, ' ')) != NULL)
*ptr = '\0'; /* trim extra spaces. */
entry->size = PHYSFS_swapULE32(entry->size);
entry->startPos = location;
241
location += entry->size;
242
243
244
245
} /* for */
__PHYSFS_platformClose(fh);
246
247
__PHYSFS_sort(info->entries, info->entryCount,
grp_entry_cmp, grp_entry_swap);
248
return 1;
249
250
251
} /* grp_load_entries */
252
static void *GRP_openArchive(const char *name, int forWriting)
253
{
254
GRPinfo *info = (GRPinfo *) allocator.Malloc(sizeof (GRPinfo));
255
256
BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, 0);
257
258
memset(info, '\0', sizeof (GRPinfo));
259
info->filename = (char *) allocator.Malloc(strlen(name) + 1);
260
GOTO_IF_MACRO(!info->filename, ERR_OUT_OF_MEMORY, GRP_openArchive_failed);
261
262
if (!grp_load_entries(name, forWriting, info))
263
264
goto GRP_openArchive_failed;
265
strcpy(info->filename, name);
266
267
return info;
268
269
GRP_openArchive_failed:
270
if (info != NULL)
271
{
272
if (info->filename != NULL)
273
allocator.Free(info->filename);
274
if (info->entries != NULL)
275
276
allocator.Free(info->entries);
allocator.Free(info);
277
278
} /* if */
279
return NULL;
280
281
282
} /* GRP_openArchive */
283
static void GRP_enumerateFiles(dvoid *opaque, const char *dname,
284
285
int omitSymLinks, PHYSFS_EnumFilesCallback cb,
const char *origdir, void *callbackdata)
286
{
287
/* no directories in GRP files. */
288
if (*dname == '\0')
289
290
291
292
293
{
GRPinfo *info = (GRPinfo *) opaque;
GRPentry *entry = info->entries;
PHYSFS_uint32 max = info->entryCount;
PHYSFS_uint32 i;
294
295
for (i = 0; i < max; i++, entry++)
296
cb(callbackdata, origdir, entry->name);
297
} /* if */
298
299
300
} /* GRP_enumerateFiles */
301
static GRPentry *grp_find_entry(const GRPinfo *info, const char *name)
302
{
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
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)
319
{
320
321
322
middle = lo + ((hi - lo) / 2);
rc = strcmp(name, a[middle].name);
if (rc == 0) /* found it! */
323
return &a[middle];
324
325
326
327
328
else if (rc > 0)
lo = middle + 1;
else
hi = middle - 1;
} /* while */
329
330
331
BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
} /* grp_find_entry */
332
333
334
static int GRP_exists(dvoid *opaque, const char *name)
335
{
336
return (grp_find_entry((GRPinfo *) opaque, name) != NULL);
337
338
339
} /* GRP_exists */
340
static int GRP_isDirectory(dvoid *opaque, const char *name, int *fileExists)
341
{
342
*fileExists = GRP_exists(opaque, name);
343
return 0; /* never directories in a groupfile. */
344
345
346
} /* GRP_isDirectory */
347
static int GRP_isSymLink(dvoid *opaque, const char *name, int *fileExists)
348
{
349
*fileExists = GRP_exists(opaque, name);
350
return 0; /* never symlinks in a groupfile. */
351
352
353
} /* GRP_isSymLink */
354
static fvoid *GRP_openRead(dvoid *opaque, const char *fnm, int *fileExists)
355
{
356
GRPinfo *info = (GRPinfo *) opaque;
357
GRPfileinfo *finfo;
358
GRPentry *entry;
359
360
361
entry = grp_find_entry(info, fnm);
*fileExists = (entry != NULL);
362
BAIL_IF_MACRO(entry == NULL, NULL, NULL);
363
364
finfo = (GRPfileinfo *) allocator.Malloc(sizeof (GRPfileinfo));
365
BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL);
366
367
finfo->handle = __PHYSFS_platformOpenRead(info->filename);
368
if ( (finfo->handle == NULL) ||
369
(!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) )
370
{
371
allocator.Free(finfo);
372
return NULL;
373
374
} /* if */
375
376
finfo->curPos = 0;
finfo->entry = entry;
377
return finfo;
378
379
} /* GRP_openRead */
380
381
static fvoid *GRP_openWrite(dvoid *opaque, const char *name)
382
383
384
385
386
{
BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
} /* GRP_openWrite */
387
static fvoid *GRP_openAppend(dvoid *opaque, const char *name)
388
389
390
391
392
{
BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
} /* GRP_openAppend */
393
static int GRP_remove(dvoid *opaque, const char *name)
394
395
396
397
398
{
BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
} /* GRP_remove */
399
static int GRP_mkdir(dvoid *opaque, const char *name)
400
401
402
403
{
BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
} /* GRP_mkdir */
404
405
static int GRP_stat(dvoid *opaque, const char *filename, int *exists,
406
407
408
409
410
411
412
413
414
415
416
PHYSFS_Stat *stat)
{
const GRPinfo *info = (const GRPinfo *) opaque;
const GRPentry *entry = grp_find_entry(info, filename);
*exists = (entry != 0);
if (!entry)
return 0;
stat->filesize = entry->size;
stat->filetype = PHYSFS_FILETYPE_REGULAR;
417
418
stat->modtime = -1;
stat->createtime = -1;
419
420
421
stat->accesstime = -1;
stat->readonly = 1;
422
return 1;
423
424
425
} /* GRP_stat */
426
427
428
429
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_GRP =
{
"GRP",
GRP_ARCHIVE_DESCRIPTION,
430
"Ryan C. Gordon <icculus@icculus.org>",
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
"http://icculus.org/physfs/",
};
const PHYSFS_Archiver __PHYSFS_Archiver_GRP =
{
&__PHYSFS_ArchiveInfo_GRP,
GRP_openArchive, /* openArchive() method */
GRP_enumerateFiles, /* enumerateFiles() method */
GRP_exists, /* exists() method */
GRP_isDirectory, /* isDirectory() method */
GRP_isSymLink, /* isSymLink() 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 */
449
GRP_stat, /* stat() method */
450
451
452
453
454
455
GRP_read, /* read() method */
GRP_write, /* write() method */
GRP_eof, /* eof() method */
GRP_tell, /* tell() method */
GRP_seek, /* seek() method */
GRP_fileLength, /* fileLength() method */
456
GRP_fileClose /* fileClose() method */
457
458
};
459
460
#endif /* defined PHYSFS_SUPPORTS_GRP */
461
/* end of grp.c ... */