/
archiver_hog.c
497 lines (391 loc) · 12.9 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
/*
* HOG support routines for PhysicsFS.
*
* This driver handles Descent I/II HOG archives.
*
* The format is very simple:
*
* The file always starts with the 3-byte signature "DHF" (Descent
* HOG file). After that the files of a HOG are just attached after
* another, divided by a 17 bytes header, which specifies the name
* and length (in bytes) of the forthcoming file! So you just read
* the header with its information of how big the following file is,
* and then skip exact that number of bytes to get to the next file
* in that HOG.
*
* char sig[3] = {'D', 'H', 'F'}; // "DHF"=Descent HOG File
*
* struct {
* char file_name[13]; // Filename, padded to 13 bytes with 0s
* int file_size; // filesize in bytes
* char data[file_size]; // The file data
* } FILE_STRUCT; // Repeated until the end of the file.
*
* (That info is from http://www.descent2.com/ddn/specs/hog/)
*
26
* Please see the file LICENSE.txt in the source's root directory.
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
*
* This file written by Bradley Bell.
* Based on grp.c by Ryan C. Gordon.
*/
#if (defined PHYSFS_SUPPORTS_HOG)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "physfs.h"
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
/*
* One HOGentry is kept for each file in an open HOG archive.
*/
typedef struct
{
char name[13];
PHYSFS_uint32 startPos;
PHYSFS_uint32 size;
} HOGentry;
/*
* One HOGinfo is kept for each open HOG archive.
*/
typedef struct
{
char *filename;
PHYSFS_uint32 entryCount;
HOGentry *entries;
} HOGinfo;
/*
* One HOGfileinfo is kept for each open file in a HOG archive.
*/
typedef struct
{
void *handle;
HOGentry *entry;
PHYSFS_uint32 curPos;
} HOGfileinfo;
73
74
75
76
77
78
static inline int readAll(void *fh, void *buf, const PHYSFS_uint64 len)
{
return (__PHYSFS_platformRead(fh, buf, len) == len);
} /* readAll */
79
static void HOG_dirClose(dvoid *opaque)
81
HOGinfo *info = ((HOGinfo *) opaque);
82
83
84
allocator.Free(info->filename);
allocator.Free(info->entries);
allocator.Free(info);
85
86
87
} /* HOG_dirClose */
88
static PHYSFS_sint64 HOG_read(fvoid *opaque, void *buffer, PHYSFS_uint64 len)
90
HOGfileinfo *finfo = (HOGfileinfo *) opaque;
91
92
const HOGentry *entry = finfo->entry;
const PHYSFS_uint64 bytesLeft = (PHYSFS_uint64)(entry->size-finfo->curPos);
93
94
PHYSFS_sint64 rc;
95
96
if (bytesLeft < len)
len = bytesLeft;
98
rc = __PHYSFS_platformRead(finfo->handle, buffer, len);
100
finfo->curPos += (PHYSFS_uint32) rc;
103
104
105
} /* HOG_read */
106
static PHYSFS_sint64 HOG_write(fvoid *f, const void *buf, PHYSFS_uint64 len)
107
108
109
110
111
{
BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
} /* HOG_write */
112
static int HOG_eof(fvoid *opaque)
114
HOGfileinfo *finfo = (HOGfileinfo *) opaque;
115
HOGentry *entry = finfo->entry;
116
return (finfo->curPos >= entry->size);
117
118
119
} /* HOG_eof */
120
static PHYSFS_sint64 HOG_tell(fvoid *opaque)
122
return ((HOGfileinfo *) opaque)->curPos;
123
124
125
} /* HOG_tell */
126
static int HOG_seek(fvoid *opaque, PHYSFS_uint64 offset)
128
HOGfileinfo *finfo = (HOGfileinfo *) opaque;
129
130
131
132
133
134
135
136
137
HOGentry *entry = finfo->entry;
int rc;
BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0);
BAIL_IF_MACRO(offset >= entry->size, ERR_PAST_EOF, 0);
rc = __PHYSFS_platformSeek(finfo->handle, entry->startPos + offset);
if (rc)
finfo->curPos = (PHYSFS_uint32) offset;
139
140
141
} /* HOG_seek */
142
static PHYSFS_sint64 HOG_fileLength(fvoid *opaque)
144
HOGfileinfo *finfo = (HOGfileinfo *) opaque;
145
return ((PHYSFS_sint64) finfo->entry->size);
146
147
148
} /* HOG_fileLength */
149
static int HOG_fileClose(fvoid *opaque)
151
HOGfileinfo *finfo = (HOGfileinfo *) opaque;
152
BAIL_IF_MACRO(!__PHYSFS_platformClose(finfo->handle), NULL, 0);
153
allocator.Free(finfo);
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
} /* HOG_fileClose */
static int hog_open(const char *filename, int forWriting,
void **fh, PHYSFS_uint32 *count)
{
PHYSFS_uint8 buf[13];
PHYSFS_uint32 size;
PHYSFS_sint64 pos;
*count = 0;
*fh = NULL;
BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
*fh = __PHYSFS_platformOpenRead(filename);
BAIL_IF_MACRO(*fh == NULL, NULL, 0);
173
if (!readAll(*fh, buf, 3))
174
175
176
177
178
179
180
181
goto openHog_failed;
if (memcmp(buf, "DHF", 3) != 0)
{
__PHYSFS_setError(ERR_UNSUPPORTED_ARCHIVE);
goto openHog_failed;
} /* if */
182
while (1)
184
if (!readAll(*fh, buf, 13))
185
break; /* eof here is ok */
187
if (!readAll(*fh, &size, sizeof (PHYSFS_uint32)))
188
189
190
191
192
193
goto openHog_failed;
size = PHYSFS_swapULE32(size);
(*count)++;
194
/* Skip over entry... */
195
196
197
198
199
pos = __PHYSFS_platformTell(*fh);
if (pos == -1)
goto openHog_failed;
if (!__PHYSFS_platformSeek(*fh, pos + size))
goto openHog_failed;
200
} /* while */
202
/* Rewind to start of entries... */
203
204
205
if (!__PHYSFS_platformSeek(*fh, 3))
goto openHog_failed;
207
208
209
210
211
212
213
openHog_failed:
if (*fh != NULL)
__PHYSFS_platformClose(*fh);
*count = -1;
*fh = NULL;
215
216
217
218
219
} /* hog_open */
static int hog_entry_cmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
{
220
221
222
if (one != two)
{
const HOGentry *a = (const HOGentry *) _a;
223
return __PHYSFS_stricmpASCII(a[one].name, a[two].name);
224
225
226
} /* if */
return 0;
227
228
229
230
231
} /* hog_entry_cmp */
static void hog_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
{
232
233
234
235
236
237
238
239
240
if (one != two)
{
HOGentry tmp;
HOGentry *first = &(((HOGentry *) _a)[one]);
HOGentry *second = &(((HOGentry *) _a)[two]);
memcpy(&tmp, first, sizeof (HOGentry));
memcpy(first, second, sizeof (HOGentry));
memcpy(second, &tmp, sizeof (HOGentry));
} /* if */
241
242
243
244
245
246
247
248
249
250
251
} /* hog_entry_swap */
static int hog_load_entries(const char *name, int forWriting, HOGinfo *info)
{
void *fh = NULL;
PHYSFS_uint32 fileCount;
HOGentry *entry;
BAIL_IF_MACRO(!hog_open(name, forWriting, &fh, &fileCount), NULL, 0);
info->entryCount = fileCount;
252
info->entries = (HOGentry *) allocator.Malloc(sizeof(HOGentry)*fileCount);
253
254
255
256
257
258
259
260
if (info->entries == NULL)
{
__PHYSFS_platformClose(fh);
BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
} /* if */
for (entry = info->entries; fileCount > 0; fileCount--, entry++)
{
261
262
if ( (!readAll(fh, &entry->name, 13)) ||
(!readAll(fh, &entry->size, sizeof (PHYSFS_uint32))) )
263
264
{
__PHYSFS_platformClose(fh);
266
267
268
} /* if */
entry->size = PHYSFS_swapULE32(entry->size);
269
entry->startPos = (unsigned int) __PHYSFS_platformTell(fh);
271
/* Skip over entry */
272
273
if ( (entry->startPos == -1) ||
(!__PHYSFS_platformSeek(fh, entry->startPos + entry->size)) )
274
275
{
__PHYSFS_platformClose(fh);
277
} /* if */
278
279
280
281
282
283
} /* for */
__PHYSFS_platformClose(fh);
__PHYSFS_sort(info->entries, info->entryCount,
hog_entry_cmp, hog_entry_swap);
285
286
287
} /* hog_load_entries */
288
static void *HOG_openArchive(const char *name, int forWriting)
290
HOGinfo *info = (HOGinfo *) allocator.Malloc(sizeof (HOGinfo));
292
BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, NULL);
293
memset(info, '\0', sizeof (HOGinfo));
294
info->filename = (char *) allocator.Malloc(strlen(name) + 1);
295
GOTO_IF_MACRO(!info->filename, ERR_OUT_OF_MEMORY, HOG_openArchive_failed);
296
297
298
299
300
if (!hog_load_entries(name, forWriting, info))
goto HOG_openArchive_failed;
strcpy(info->filename, name);
303
304
HOG_openArchive_failed:
305
if (info != NULL)
307
if (info->filename != NULL)
308
allocator.Free(info->filename);
309
if (info->entries != NULL)
310
311
allocator.Free(info->entries);
allocator.Free(info);
312
313
} /* if */
315
316
317
} /* HOG_openArchive */
318
static void HOG_enumerateFiles(dvoid *opaque, const char *dname,
319
320
int omitSymLinks, PHYSFS_EnumFilesCallback cb,
const char *origdir, void *callbackdata)
321
322
{
/* no directories in HOG files. */
323
if (*dname == '\0')
324
325
326
327
328
{
HOGinfo *info = (HOGinfo *) opaque;
HOGentry *entry = info->entries;
PHYSFS_uint32 max = info->entryCount;
PHYSFS_uint32 i;
330
for (i = 0; i < max; i++, entry++)
331
cb(callbackdata, origdir, entry->name);
333
334
335
} /* HOG_enumerateFiles */
336
static HOGentry *hog_find_entry(const HOGinfo *info, const char *name)
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
{
char *ptr = strchr(name, '.');
HOGentry *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)
{
middle = lo + ((hi - lo) / 2);
356
rc = __PHYSFS_stricmpASCII(name, a[middle].name);
357
if (rc == 0) /* found it! */
358
return &a[middle];
359
360
361
362
363
364
365
366
367
368
else if (rc > 0)
lo = middle + 1;
else
hi = middle - 1;
} /* while */
BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
} /* hog_find_entry */
369
static int HOG_exists(dvoid *opaque, const char *name)
371
return (hog_find_entry(((HOGinfo *) opaque), name) != NULL);
372
373
374
} /* HOG_exists */
375
static int HOG_isDirectory(dvoid *opaque, const char *name, int *fileExists)
377
*fileExists = HOG_exists(opaque, name);
378
return 0; /* never directories in a groupfile. */
379
380
381
} /* HOG_isDirectory */
382
static int HOG_isSymLink(dvoid *opaque, const char *name, int *fileExists)
384
*fileExists = HOG_exists(opaque, name);
385
return 0; /* never symlinks in a groupfile. */
386
387
388
} /* HOG_isSymLink */
389
static fvoid *HOG_openRead(dvoid *opaque, const char *fnm, int *fileExists)
391
HOGinfo *info = ((HOGinfo *) opaque);
392
393
394
395
396
397
398
HOGfileinfo *finfo;
HOGentry *entry;
entry = hog_find_entry(info, fnm);
*fileExists = (entry != NULL);
BAIL_IF_MACRO(entry == NULL, NULL, NULL);
399
finfo = (HOGfileinfo *) allocator.Malloc(sizeof (HOGfileinfo));
400
BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL);
401
402
403
404
405
finfo->handle = __PHYSFS_platformOpenRead(info->filename);
if ( (finfo->handle == NULL) ||
(!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) )
{
406
allocator.Free(finfo);
408
409
410
411
} /* if */
finfo->curPos = 0;
finfo->entry = entry;
413
414
415
} /* HOG_openRead */
416
static fvoid *HOG_openWrite(dvoid *opaque, const char *name)
417
418
419
420
421
{
BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
} /* HOG_openWrite */
422
static fvoid *HOG_openAppend(dvoid *opaque, const char *name)
423
424
425
426
427
{
BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
} /* HOG_openAppend */
428
static int HOG_remove(dvoid *opaque, const char *name)
429
430
431
432
433
{
BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
} /* HOG_remove */
434
static int HOG_mkdir(dvoid *opaque, const char *name)
435
436
437
438
{
BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
} /* HOG_mkdir */
440
static int HOG_stat(dvoid *opaque, const char *filename, int *exists,
441
442
443
444
445
446
447
448
449
450
451
PHYSFS_Stat *stat)
{
const HOGinfo *info = (const HOGinfo *) opaque;
const HOGentry *entry = hog_find_entry(info, filename);
*exists = (entry != 0);
if (!entry)
return 0;
stat->filesize = entry->size;
stat->filetype = PHYSFS_FILETYPE_REGULAR;
452
453
stat->modtime = -1;
stat->createtime = -1;
454
455
456
stat->accesstime = -1;
stat->readonly = 1;
458
459
460
} /* HOG_stat */
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_HOG =
{
"HOG",
HOG_ARCHIVE_DESCRIPTION,
"Bradley Bell <btb@icculus.org>",
"http://icculus.org/physfs/",
};
const PHYSFS_Archiver __PHYSFS_Archiver_HOG =
{
&__PHYSFS_ArchiveInfo_HOG,
HOG_openArchive, /* openArchive() method */
HOG_enumerateFiles, /* enumerateFiles() method */
HOG_exists, /* exists() method */
HOG_isDirectory, /* isDirectory() method */
HOG_isSymLink, /* isSymLink() method */
HOG_openRead, /* openRead() method */
HOG_openWrite, /* openWrite() method */
HOG_openAppend, /* openAppend() method */
HOG_remove, /* remove() method */
HOG_mkdir, /* mkdir() method */
HOG_dirClose, /* dirClose() method */
484
HOG_stat, /* stat() method */
485
486
487
488
489
490
HOG_read, /* read() method */
HOG_write, /* write() method */
HOG_eof, /* eof() method */
HOG_tell, /* tell() method */
HOG_seek, /* seek() method */
HOG_fileLength, /* fileLength() method */
491
HOG_fileClose /* fileClose() method */
494
495
496
#endif /* defined PHYSFS_SUPPORTS_HOG */
/* end of hog.c ... */