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