Skip to content

Latest commit

 

History

History
530 lines (416 loc) · 14 KB

archiver_hog.c

File metadata and controls

530 lines (416 loc) · 14 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/)
*
Mar 11, 2007
Mar 11, 2007
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
73
*
* 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_sint64 last_mod_time;
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;
Aug 21, 2010
Aug 21, 2010
74
75
76
77
78
79
static inline int readAll(void *fh, void *buf, const PHYSFS_uint64 len)
{
return (__PHYSFS_platformRead(fh, buf, len) == len);
} /* readAll */
Sep 26, 2004
Sep 26, 2004
80
static void HOG_dirClose(dvoid *opaque)
Sep 26, 2004
Sep 26, 2004
82
HOGinfo *info = ((HOGinfo *) opaque);
Mar 14, 2005
Mar 14, 2005
83
84
85
allocator.Free(info->filename);
allocator.Free(info->entries);
allocator.Free(info);
86
87
88
} /* HOG_dirClose */
Aug 21, 2010
Aug 21, 2010
89
static PHYSFS_sint64 HOG_read(fvoid *opaque, void *buffer, PHYSFS_uint64 len)
Sep 26, 2004
Sep 26, 2004
91
HOGfileinfo *finfo = (HOGfileinfo *) opaque;
Aug 21, 2010
Aug 21, 2010
92
93
const HOGentry *entry = finfo->entry;
const PHYSFS_uint64 bytesLeft = (PHYSFS_uint64)(entry->size-finfo->curPos);
94
95
PHYSFS_sint64 rc;
Aug 21, 2010
Aug 21, 2010
96
97
if (bytesLeft < len)
len = bytesLeft;
Aug 21, 2010
Aug 21, 2010
99
rc = __PHYSFS_platformRead(finfo->handle, buffer, len);
Aug 21, 2010
Aug 21, 2010
101
finfo->curPos += (PHYSFS_uint32) rc;
Jan 28, 2010
Jan 28, 2010
103
return rc;
104
105
106
} /* HOG_read */
Aug 21, 2010
Aug 21, 2010
107
static PHYSFS_sint64 HOG_write(fvoid *f, const void *buf, PHYSFS_uint64 len)
108
109
110
111
112
{
BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
} /* HOG_write */
Sep 26, 2004
Sep 26, 2004
113
static int HOG_eof(fvoid *opaque)
Sep 26, 2004
Sep 26, 2004
115
HOGfileinfo *finfo = (HOGfileinfo *) opaque;
116
HOGentry *entry = finfo->entry;
Jan 28, 2010
Jan 28, 2010
117
return (finfo->curPos >= entry->size);
118
119
120
} /* HOG_eof */
Sep 26, 2004
Sep 26, 2004
121
static PHYSFS_sint64 HOG_tell(fvoid *opaque)
Jan 28, 2010
Jan 28, 2010
123
return ((HOGfileinfo *) opaque)->curPos;
124
125
126
} /* HOG_tell */
Sep 26, 2004
Sep 26, 2004
127
static int HOG_seek(fvoid *opaque, PHYSFS_uint64 offset)
Sep 26, 2004
Sep 26, 2004
129
HOGfileinfo *finfo = (HOGfileinfo *) opaque;
130
131
132
133
134
135
136
137
138
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;
Jan 28, 2010
Jan 28, 2010
139
return rc;
140
141
142
} /* HOG_seek */
Sep 26, 2004
Sep 26, 2004
143
static PHYSFS_sint64 HOG_fileLength(fvoid *opaque)
Sep 26, 2004
Sep 26, 2004
145
HOGfileinfo *finfo = (HOGfileinfo *) opaque;
Jan 28, 2010
Jan 28, 2010
146
return ((PHYSFS_sint64) finfo->entry->size);
147
148
149
} /* HOG_fileLength */
Sep 26, 2004
Sep 26, 2004
150
static int HOG_fileClose(fvoid *opaque)
Sep 26, 2004
Sep 26, 2004
152
HOGfileinfo *finfo = (HOGfileinfo *) opaque;
153
BAIL_IF_MACRO(!__PHYSFS_platformClose(finfo->handle), NULL, 0);
Mar 14, 2005
Mar 14, 2005
154
allocator.Free(finfo);
Jan 28, 2010
Jan 28, 2010
155
return 1;
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
} /* 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);
Aug 21, 2010
Aug 21, 2010
174
if (!readAll(*fh, buf, 3))
175
176
177
178
179
180
181
182
goto openHog_failed;
if (memcmp(buf, "DHF", 3) != 0)
{
__PHYSFS_setError(ERR_UNSUPPORTED_ARCHIVE);
goto openHog_failed;
} /* if */
Dec 29, 2003
Dec 29, 2003
183
while (1)
Aug 21, 2010
Aug 21, 2010
185
if (!readAll(*fh, buf, 13))
Dec 29, 2003
Dec 29, 2003
186
break; /* eof here is ok */
Aug 21, 2010
Aug 21, 2010
188
if (!readAll(*fh, &size, sizeof (PHYSFS_uint32)))
189
190
191
192
193
194
goto openHog_failed;
size = PHYSFS_swapULE32(size);
(*count)++;
Dec 29, 2003
Dec 29, 2003
195
/* Skip over entry... */
196
197
198
199
200
pos = __PHYSFS_platformTell(*fh);
if (pos == -1)
goto openHog_failed;
if (!__PHYSFS_platformSeek(*fh, pos + size))
goto openHog_failed;
Dec 29, 2003
Dec 29, 2003
201
} /* while */
Dec 29, 2003
Dec 29, 2003
203
/* Rewind to start of entries... */
204
205
206
if (!__PHYSFS_platformSeek(*fh, 3))
goto openHog_failed;
Jan 28, 2010
Jan 28, 2010
207
return 1;
208
209
210
211
212
213
214
openHog_failed:
if (*fh != NULL)
__PHYSFS_platformClose(*fh);
*count = -1;
*fh = NULL;
Jan 28, 2010
Jan 28, 2010
215
return 0;
216
217
218
219
220
221
222
223
224
225
226
227
} /* hog_open */
static int HOG_isArchive(const char *filename, int forWriting)
{
void *fh;
PHYSFS_uint32 fileCount;
int retval = hog_open(filename, forWriting, &fh, &fileCount);
if (fh != NULL)
__PHYSFS_platformClose(fh);
Jan 28, 2010
Jan 28, 2010
228
return retval;
229
230
231
232
233
} /* HOG_isArchive */
static int hog_entry_cmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
{
Feb 20, 2008
Feb 20, 2008
234
235
236
if (one != two)
{
const HOGentry *a = (const HOGentry *) _a;
Jan 28, 2010
Jan 28, 2010
237
return __PHYSFS_stricmpASCII(a[one].name, a[two].name);
Feb 20, 2008
Feb 20, 2008
238
239
240
} /* if */
return 0;
241
242
243
244
245
} /* hog_entry_cmp */
static void hog_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
{
Feb 20, 2008
Feb 20, 2008
246
247
248
249
250
251
252
253
254
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 */
255
256
257
258
259
260
261
262
263
264
265
} /* 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;
Mar 14, 2005
Mar 14, 2005
266
info->entries = (HOGentry *) allocator.Malloc(sizeof(HOGentry)*fileCount);
267
268
269
270
271
272
273
274
if (info->entries == NULL)
{
__PHYSFS_platformClose(fh);
BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
} /* if */
for (entry = info->entries; fileCount > 0; fileCount--, entry++)
{
Aug 21, 2010
Aug 21, 2010
275
276
if ( (!readAll(fh, &entry->name, 13)) ||
(!readAll(fh, &entry->size, sizeof (PHYSFS_uint32))) )
277
278
{
__PHYSFS_platformClose(fh);
Jan 28, 2010
Jan 28, 2010
279
return 0;
280
281
282
} /* if */
entry->size = PHYSFS_swapULE32(entry->size);
Aug 9, 2003
Aug 9, 2003
283
entry->startPos = (unsigned int) __PHYSFS_platformTell(fh);
Dec 29, 2003
Dec 29, 2003
285
/* Skip over entry */
Aug 21, 2010
Aug 21, 2010
286
287
if ( (entry->startPos == -1) ||
(!__PHYSFS_platformSeek(fh, entry->startPos + entry->size)) )
288
289
{
__PHYSFS_platformClose(fh);
Jan 28, 2010
Jan 28, 2010
290
return 0;
Aug 21, 2010
Aug 21, 2010
291
} /* if */
292
293
294
295
296
297
} /* for */
__PHYSFS_platformClose(fh);
__PHYSFS_sort(info->entries, info->entryCount,
hog_entry_cmp, hog_entry_swap);
Jan 28, 2010
Jan 28, 2010
298
return 1;
299
300
301
} /* hog_load_entries */
Sep 26, 2004
Sep 26, 2004
302
static void *HOG_openArchive(const char *name, int forWriting)
303
304
{
PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name);
Mar 14, 2005
Mar 14, 2005
305
HOGinfo *info = (HOGinfo *) allocator.Malloc(sizeof (HOGinfo));
Sep 26, 2004
Sep 26, 2004
307
BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, 0);
308
memset(info, '\0', sizeof (HOGinfo));
Mar 14, 2005
Mar 14, 2005
309
info->filename = (char *) allocator.Malloc(strlen(name) + 1);
Mar 13, 2005
Mar 13, 2005
310
GOTO_IF_MACRO(!info->filename, ERR_OUT_OF_MEMORY, HOG_openArchive_failed);
311
312
313
314
315
316
if (!hog_load_entries(name, forWriting, info))
goto HOG_openArchive_failed;
strcpy(info->filename, name);
info->last_mod_time = modtime;
Sep 26, 2004
Sep 26, 2004
317
Jan 28, 2010
Jan 28, 2010
318
return info;
319
320
HOG_openArchive_failed:
Sep 26, 2004
Sep 26, 2004
321
if (info != NULL)
Sep 26, 2004
Sep 26, 2004
323
if (info->filename != NULL)
Mar 14, 2005
Mar 14, 2005
324
allocator.Free(info->filename);
Sep 26, 2004
Sep 26, 2004
325
if (info->entries != NULL)
Mar 14, 2005
Mar 14, 2005
326
327
allocator.Free(info->entries);
allocator.Free(info);
Jan 28, 2010
Jan 28, 2010
330
return NULL;
331
332
333
} /* HOG_openArchive */
Sep 29, 2004
Sep 29, 2004
334
static void HOG_enumerateFiles(dvoid *opaque, const char *dname,
Sep 18, 2005
Sep 18, 2005
335
336
int omitSymLinks, PHYSFS_EnumFilesCallback cb,
const char *origdir, void *callbackdata)
337
338
{
/* no directories in HOG files. */
Mar 28, 2007
Mar 28, 2007
339
if (*dname == '\0')
Sep 29, 2004
Sep 29, 2004
340
341
342
343
344
{
HOGinfo *info = (HOGinfo *) opaque;
HOGentry *entry = info->entries;
PHYSFS_uint32 max = info->entryCount;
PHYSFS_uint32 i;
Sep 29, 2004
Sep 29, 2004
346
for (i = 0; i < max; i++, entry++)
Sep 18, 2005
Sep 18, 2005
347
cb(callbackdata, origdir, entry->name);
Sep 29, 2004
Sep 29, 2004
348
} /* if */
349
350
351
} /* HOG_enumerateFiles */
Feb 15, 2010
Feb 15, 2010
352
static HOGentry *hog_find_entry(const HOGinfo *info, const char *name)
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
{
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);
Mar 15, 2007
Mar 15, 2007
372
rc = __PHYSFS_stricmpASCII(name, a[middle].name);
373
if (rc == 0) /* found it! */
Jan 28, 2010
Jan 28, 2010
374
return &a[middle];
375
376
377
378
379
380
381
382
383
384
else if (rc > 0)
lo = middle + 1;
else
hi = middle - 1;
} /* while */
BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
} /* hog_find_entry */
Sep 26, 2004
Sep 26, 2004
385
static int HOG_exists(dvoid *opaque, const char *name)
Jan 28, 2010
Jan 28, 2010
387
return (hog_find_entry(((HOGinfo *) opaque), name) != NULL);
388
389
390
} /* HOG_exists */
Sep 26, 2004
Sep 26, 2004
391
static int HOG_isDirectory(dvoid *opaque, const char *name, int *fileExists)
Sep 26, 2004
Sep 26, 2004
393
*fileExists = HOG_exists(opaque, name);
Jan 28, 2010
Jan 28, 2010
394
return 0; /* never directories in a groupfile. */
395
396
397
} /* HOG_isDirectory */
Sep 26, 2004
Sep 26, 2004
398
static int HOG_isSymLink(dvoid *opaque, const char *name, int *fileExists)
Sep 26, 2004
Sep 26, 2004
400
*fileExists = HOG_exists(opaque, name);
Jan 28, 2010
Jan 28, 2010
401
return 0; /* never symlinks in a groupfile. */
402
403
404
} /* HOG_isSymLink */
Sep 26, 2004
Sep 26, 2004
405
static PHYSFS_sint64 HOG_getLastModTime(dvoid *opaque,
406
407
408
const char *name,
int *fileExists)
{
Sep 26, 2004
Sep 26, 2004
409
HOGinfo *info = ((HOGinfo *) opaque);
410
411
412
413
PHYSFS_sint64 retval = -1;
*fileExists = (hog_find_entry(info, name) != NULL);
if (*fileExists) /* use time of HOG itself in the physical filesystem. */
Sep 26, 2004
Sep 26, 2004
414
retval = info->last_mod_time;
Jan 28, 2010
Jan 28, 2010
416
return retval;
417
418
419
} /* HOG_getLastModTime */
Sep 26, 2004
Sep 26, 2004
420
static fvoid *HOG_openRead(dvoid *opaque, const char *fnm, int *fileExists)
Sep 26, 2004
Sep 26, 2004
422
HOGinfo *info = ((HOGinfo *) opaque);
423
424
425
426
427
428
429
HOGfileinfo *finfo;
HOGentry *entry;
entry = hog_find_entry(info, fnm);
*fileExists = (entry != NULL);
BAIL_IF_MACRO(entry == NULL, NULL, NULL);
Mar 14, 2005
Mar 14, 2005
430
finfo = (HOGfileinfo *) allocator.Malloc(sizeof (HOGfileinfo));
Sep 26, 2004
Sep 26, 2004
431
BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL);
432
433
434
435
436
finfo->handle = __PHYSFS_platformOpenRead(info->filename);
if ( (finfo->handle == NULL) ||
(!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) )
{
Mar 14, 2005
Mar 14, 2005
437
allocator.Free(finfo);
Jan 28, 2010
Jan 28, 2010
438
return NULL;
439
440
441
442
} /* if */
finfo->curPos = 0;
finfo->entry = entry;
Jan 28, 2010
Jan 28, 2010
443
return finfo;
444
445
446
} /* HOG_openRead */
Sep 26, 2004
Sep 26, 2004
447
static fvoid *HOG_openWrite(dvoid *opaque, const char *name)
448
449
450
451
452
{
BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
} /* HOG_openWrite */
Sep 26, 2004
Sep 26, 2004
453
static fvoid *HOG_openAppend(dvoid *opaque, const char *name)
454
455
456
457
458
{
BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
} /* HOG_openAppend */
Sep 26, 2004
Sep 26, 2004
459
static int HOG_remove(dvoid *opaque, const char *name)
460
461
462
463
464
{
BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
} /* HOG_remove */
Sep 26, 2004
Sep 26, 2004
465
static int HOG_mkdir(dvoid *opaque, const char *name)
466
467
468
469
{
BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
} /* HOG_mkdir */
Sep 29, 2004
Sep 29, 2004
470
Aug 22, 2010
Aug 22, 2010
471
static int HOG_stat(dvoid *opaque, const char *filename, int *exists,
Feb 15, 2010
Feb 15, 2010
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
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;
stat->modtime = info->last_mod_time;
stat->createtime = info->last_mod_time;
stat->accesstime = -1;
stat->readonly = 1;
Aug 21, 2010
Aug 21, 2010
488
return 1;
Feb 15, 2010
Feb 15, 2010
489
490
491
} /* HOG_stat */
Sep 29, 2004
Sep 29, 2004
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
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_isArchive, /* isArchive() method */
HOG_openArchive, /* openArchive() method */
HOG_enumerateFiles, /* enumerateFiles() method */
HOG_exists, /* exists() method */
HOG_isDirectory, /* isDirectory() method */
HOG_isSymLink, /* isSymLink() method */
HOG_getLastModTime, /* getLastModTime() 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 */
Aug 22, 2010
Aug 22, 2010
517
HOG_stat, /* stat() method */
Sep 29, 2004
Sep 29, 2004
518
519
520
521
522
523
HOG_read, /* read() method */
HOG_write, /* write() method */
HOG_eof, /* eof() method */
HOG_tell, /* tell() method */
HOG_seek, /* seek() method */
HOG_fileLength, /* fileLength() method */
Aug 22, 2010
Aug 22, 2010
524
HOG_fileClose /* fileClose() method */
Sep 29, 2004
Sep 29, 2004
525
526
};
527
528
529
#endif /* defined PHYSFS_SUPPORTS_HOG */
/* end of hog.c ... */