Skip to content

Latest commit

 

History

History
118 lines (100 loc) · 3.29 KB

archiver_hog.c

File metadata and controls

118 lines (100 loc) · 3.29 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
*
* This file written by Bradley Bell.
* Based on grp.c by Ryan C. Gordon.
*/
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
Mar 23, 2012
Mar 23, 2012
35
36
#if PHYSFS_SUPPORTS_HOG
Sep 6, 2010
Sep 6, 2010
37
static UNPKentry *hogLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 *_entCount)
Aug 30, 2010
Aug 30, 2010
39
40
41
const PHYSFS_uint64 iolen = io->length(io);
PHYSFS_uint32 entCount = 0;
void *ptr = NULL;
Sep 6, 2010
Sep 6, 2010
42
43
UNPKentry *entries = NULL;
UNPKentry *entry = NULL;
Aug 30, 2010
Aug 30, 2010
44
45
46
47
PHYSFS_uint32 size = 0;
PHYSFS_uint32 pos = 3;
while (pos < iolen)
Aug 30, 2010
Aug 30, 2010
49
entCount++;
Sep 6, 2010
Sep 6, 2010
50
ptr = allocator.Realloc(ptr, sizeof (UNPKentry) * entCount);
Mar 20, 2012
Mar 20, 2012
51
GOTO_IF_MACRO(ptr == NULL, PHYSFS_ERR_OUT_OF_MEMORY, failed);
Sep 6, 2010
Sep 6, 2010
52
53
entries = (UNPKentry *) ptr;
entry = &entries[entCount-1];
Aug 30, 2010
Aug 30, 2010
54
Mar 20, 2012
Mar 20, 2012
55
GOTO_IF_MACRO(!__PHYSFS_readAll(io, &entry->name, 13), ERRPASS, failed);
Aug 30, 2010
Aug 30, 2010
56
pos += 13;
Mar 20, 2012
Mar 20, 2012
57
GOTO_IF_MACRO(!__PHYSFS_readAll(io, &size, 4), ERRPASS, failed);
Aug 30, 2010
Aug 30, 2010
58
59
60
61
62
63
pos += 4;
entry->size = PHYSFS_swapULE32(size);
entry->startPos = pos;
pos += size;
Sep 6, 2010
Sep 6, 2010
64
/* skip over entry */
Mar 20, 2012
Mar 20, 2012
65
GOTO_IF_MACRO(!io->seek(io, pos), ERRPASS, failed);
Sep 6, 2010
Sep 6, 2010
66
} /* while */
Sep 6, 2010
Sep 6, 2010
68
69
*_entCount = entCount;
return entries;
Mar 9, 2012
Mar 9, 2012
71
failed:
Sep 6, 2010
Sep 6, 2010
72
73
74
allocator.Free(entries);
return NULL;
} /* hogLoadEntries */
Aug 30, 2010
Aug 30, 2010
77
static void *HOG_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
Aug 30, 2010
Aug 30, 2010
79
PHYSFS_uint8 buf[3];
Mar 9, 2012
Mar 9, 2012
80
PHYSFS_uint32 count = 0;
Sep 6, 2010
Sep 6, 2010
81
UNPKentry *entries = NULL;
Aug 30, 2010
Aug 30, 2010
83
assert(io != NULL); /* shouldn't ever happen. */
Mar 20, 2012
Mar 20, 2012
84
85
86
BAIL_IF_MACRO(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
BAIL_IF_MACRO(!__PHYSFS_readAll(io, buf, 3), ERRPASS, NULL);
BAIL_IF_MACRO(memcmp(buf, "DHF", 3) != 0, PHYSFS_ERR_UNSUPPORTED, NULL);
Sep 26, 2004
Sep 26, 2004
87
Mar 9, 2012
Mar 9, 2012
88
entries = hogLoadEntries(io, &count);
Mar 20, 2012
Mar 20, 2012
89
BAIL_IF_MACRO(!entries, ERRPASS, NULL);
Mar 9, 2012
Mar 9, 2012
90
return UNPK_openArchive(io, entries, count);
91
92
93
} /* HOG_openArchive */
Sep 29, 2004
Sep 29, 2004
94
95
const PHYSFS_Archiver __PHYSFS_Archiver_HOG =
{
Nov 28, 2012
Nov 28, 2012
96
CURRENT_PHYSFS_ARCHIVER_API_VERSION,
Mar 25, 2012
Mar 25, 2012
97
98
99
100
{
"HOG",
"Descent I/II HOG file format",
"Bradley Bell <btb@icculus.org>",
Feb 25, 2016
Feb 25, 2016
101
"https://icculus.org/physfs/",
Nov 30, 2012
Nov 30, 2012
102
0, /* supportsSymlinks */
Mar 25, 2012
Mar 25, 2012
103
},
Nov 30, 2012
Nov 30, 2012
104
105
106
107
108
109
110
HOG_openArchive,
UNPK_enumerateFiles,
UNPK_openRead,
UNPK_openWrite,
UNPK_openAppend,
UNPK_remove,
UNPK_mkdir,
Nov 30, 2012
Nov 30, 2012
111
112
UNPK_stat,
UNPK_closeArchive
Sep 29, 2004
Sep 29, 2004
113
114
};
115
116
#endif /* defined PHYSFS_SUPPORTS_HOG */
Nov 27, 2012
Nov 27, 2012
117
/* end of archiver_hog.c ... */