1 | /* |
2 | * HOG support routines for PhysicsFS. |
3 | * |
4 | * This driver handles Descent I/II/III HOG archives. |
5 | * |
6 | * The Descent I/II format is very simple: |
7 | * |
8 | * The file always starts with the 3-byte signature "DHF" (Descent |
9 | * HOG file). After that the files of a HOG are just attached after |
10 | * another, divided by a 17 bytes header, which specifies the name |
11 | * and length (in bytes) of the forthcoming file! So you just read |
12 | * the header with its information of how big the following file is, |
13 | * and then skip exact that number of bytes to get to the next file |
14 | * in that HOG. |
15 | * |
16 | * char sig[3] = {'D', 'H', 'F'}; // "DHF"=Descent HOG File |
17 | * |
18 | * struct { |
19 | * char file_name[13]; // Filename, padded to 13 bytes with 0s |
20 | * int file_size; // filesize in bytes |
21 | * char data[file_size]; // The file data |
22 | * } FILE_STRUCT; // Repeated until the end of the file. |
23 | * |
24 | * (That info is from http://www.descent2.com/ddn/specs/hog/) |
25 | * |
26 | * Descent 3 moved to HOG2 format, which starts with the chars "HOG2", |
27 | * then 32-bits for the number of contained files, 32 bits for the offset |
28 | * to the first file's data, then 56 bytes of 0xFF (reserved?). Then for |
29 | * each file, there's 36 bytes for filename (null-terminated, rest of bytes |
30 | * are garbage), 32-bits unknown/reserved (always zero?), 32-bits of length |
31 | * of file data, 32-bits of time since Unix epoch. Then immediately following, |
32 | * for each file is their uncompressed content, you can find its offset |
33 | * by starting at the initial data offset and adding the filesize of each |
34 | * prior file. |
35 | * |
36 | * This information was found at: |
37 | * https://web.archive.org/web/20020213004051/http://descent-3.com/ddn/specs/hog/ |
38 | * |
39 | * |
40 | * Please see the file LICENSE.txt in the source's root directory. |
41 | * |
42 | * This file written by Bradley Bell and Ryan C. Gordon. |
43 | */ |
44 | |
45 | #define __PHYSICSFS_INTERNAL__ |
46 | #include "physfs_internal.h" |
47 | |
48 | #if PHYSFS_SUPPORTS_HOG |
49 | |
50 | static int readui32(PHYSFS_Io *io, PHYSFS_uint32 *val) |
51 | { |
52 | PHYSFS_uint32 v; |
53 | BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &v, sizeof (v)), 0); |
54 | *val = PHYSFS_swapULE32(v); |
55 | return 1; |
56 | } /* readui32 */ |
57 | |
58 | static int hog1LoadEntries(PHYSFS_Io *io, void *arc) |
59 | { |
60 | const PHYSFS_uint64 iolen = io->length(io); |
61 | PHYSFS_uint32 pos = 3; |
62 | |
63 | while (pos < iolen) |
64 | { |
65 | PHYSFS_uint32 size; |
66 | char name[13]; |
67 | |
68 | BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, name, 13), 0); |
69 | BAIL_IF_ERRPASS(!readui32(io, &size), 0); |
70 | name[12] = '\0'; /* just in case. */ |
71 | pos += 13 + 4; |
72 | |
73 | BAIL_IF_ERRPASS(!UNPK_addEntry(arc, name, 0, -1, -1, pos, size), 0); |
74 | pos += size; |
75 | |
76 | /* skip over entry */ |
77 | BAIL_IF_ERRPASS(!io->seek(io, pos), 0); |
78 | } /* while */ |
79 | |
80 | return 1; |
81 | } /* hogLoadEntries */ |
82 | |
83 | static int hog2LoadEntries(PHYSFS_Io *io, void *arc) |
84 | { |
85 | PHYSFS_uint32 numfiles; |
86 | PHYSFS_uint32 pos; |
87 | PHYSFS_uint32 i; |
88 | |
89 | BAIL_IF_ERRPASS(!readui32(io, &numfiles), 0); |
90 | BAIL_IF_ERRPASS(!readui32(io, &pos), 0); |
91 | BAIL_IF_ERRPASS(!io->seek(io, 68), 0); /* skip to end of header. */ |
92 | |
93 | for (i = 0; i < numfiles; i++) { |
94 | char name[37]; |
95 | PHYSFS_uint32 reserved; |
96 | PHYSFS_uint32 size; |
97 | PHYSFS_uint32 mtime; |
98 | BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, name, 36), 0); |
99 | BAIL_IF_ERRPASS(!readui32(io, &reserved), 0); |
100 | BAIL_IF_ERRPASS(!readui32(io, &size), 0); |
101 | BAIL_IF_ERRPASS(!readui32(io, &mtime), 0); |
102 | name[36] = '\0'; /* just in case */ |
103 | BAIL_IF_ERRPASS(!UNPK_addEntry(arc, name, 0, mtime, mtime, pos, size), 0); |
104 | pos += size; |
105 | } |
106 | |
107 | return 1; |
108 | } /* hog2LoadEntries */ |
109 | |
110 | |
111 | static void *HOG_openArchive(PHYSFS_Io *io, const char *name, |
112 | int forWriting, int *claimed) |
113 | { |
114 | PHYSFS_uint8 buf[3]; |
115 | void *unpkarc = NULL; |
116 | int hog1 = 0; |
117 | |
118 | assert(io != NULL); /* shouldn't ever happen. */ |
119 | BAIL_IF(forWriting, PHYSFS_ERR_READ_ONLY, NULL); |
120 | BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, buf, 3), NULL); |
121 | |
122 | if (memcmp(buf, "DHF" , 3) == 0) |
123 | hog1 = 1; /* original HOG (Descent 1 and 2) archive */ |
124 | else |
125 | { |
126 | BAIL_IF(memcmp(buf, "HOG" , 3) != 0, PHYSFS_ERR_UNSUPPORTED, NULL); /* Not HOG2 */ |
127 | BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, buf, 1), NULL); |
128 | BAIL_IF(buf[0] != '2', PHYSFS_ERR_UNSUPPORTED, NULL); /* Not HOG2 */ |
129 | } /* else */ |
130 | |
131 | *claimed = 1; |
132 | |
133 | unpkarc = UNPK_openArchive(io, 0, 1); |
134 | BAIL_IF_ERRPASS(!unpkarc, NULL); |
135 | |
136 | if (!(hog1 ? hog1LoadEntries(io, unpkarc) : hog2LoadEntries(io, unpkarc))) |
137 | { |
138 | UNPK_abandonArchive(unpkarc); |
139 | return NULL; |
140 | } /* if */ |
141 | |
142 | return unpkarc; |
143 | } /* HOG_openArchive */ |
144 | |
145 | |
146 | const PHYSFS_Archiver __PHYSFS_Archiver_HOG = |
147 | { |
148 | CURRENT_PHYSFS_ARCHIVER_API_VERSION, |
149 | { |
150 | "HOG" , |
151 | "Descent I/II/III HOG file format" , |
152 | "Bradley Bell <btb@icculus.org>" , |
153 | "https://icculus.org/physfs/" , |
154 | 0, /* supportsSymlinks */ |
155 | }, |
156 | HOG_openArchive, |
157 | UNPK_enumerate, |
158 | UNPK_openRead, |
159 | UNPK_openWrite, |
160 | UNPK_openAppend, |
161 | UNPK_remove, |
162 | UNPK_mkdir, |
163 | UNPK_stat, |
164 | UNPK_closeArchive |
165 | }; |
166 | |
167 | #endif /* defined PHYSFS_SUPPORTS_HOG */ |
168 | |
169 | /* end of physfs_archiver_hog.c ... */ |
170 | |
171 | |