diff options
Diffstat (limited to 'rbutil/rbutilqt/mspack/chmd.c')
-rw-r--r-- | rbutil/rbutilqt/mspack/chmd.c | 1346 |
1 files changed, 1346 insertions, 0 deletions
diff --git a/rbutil/rbutilqt/mspack/chmd.c b/rbutil/rbutilqt/mspack/chmd.c new file mode 100644 index 0000000000..6e4a1bf24f --- /dev/null +++ b/rbutil/rbutilqt/mspack/chmd.c | |||
@@ -0,0 +1,1346 @@ | |||
1 | /* This file is part of libmspack. | ||
2 | * (C) 2003-2011 Stuart Caie. | ||
3 | * | ||
4 | * libmspack is free software; you can redistribute it and/or modify it under | ||
5 | * the terms of the GNU Lesser General Public License (LGPL) version 2.1 | ||
6 | * | ||
7 | * For further details, see the file COPYING.LIB distributed with libmspack | ||
8 | */ | ||
9 | |||
10 | /* CHM decompression implementation */ | ||
11 | |||
12 | #include <system.h> | ||
13 | #include <chm.h> | ||
14 | |||
15 | /* prototypes */ | ||
16 | static struct mschmd_header * chmd_open( | ||
17 | struct mschm_decompressor *base, const char *filename); | ||
18 | static struct mschmd_header * chmd_fast_open( | ||
19 | struct mschm_decompressor *base, const char *filename); | ||
20 | static struct mschmd_header *chmd_real_open( | ||
21 | struct mschm_decompressor *base, const char *filename, int entire); | ||
22 | static void chmd_close( | ||
23 | struct mschm_decompressor *base, struct mschmd_header *chm); | ||
24 | static int chmd_read_headers( | ||
25 | struct mspack_system *sys, struct mspack_file *fh, | ||
26 | struct mschmd_header *chm, int entire); | ||
27 | static int chmd_fast_find( | ||
28 | struct mschm_decompressor *base, struct mschmd_header *chm, | ||
29 | const char *filename, struct mschmd_file *f_ptr, int f_size); | ||
30 | static unsigned char *read_chunk( | ||
31 | struct mschm_decompressor_p *self, struct mschmd_header *chm, | ||
32 | struct mspack_file *fh, unsigned int chunk); | ||
33 | static int search_chunk( | ||
34 | struct mschmd_header *chm, const unsigned char *chunk, const char *filename, | ||
35 | const unsigned char **result, const unsigned char **result_end); | ||
36 | static inline int compare( | ||
37 | const char *s1, const char *s2, int l1, int l2); | ||
38 | static int chmd_extract( | ||
39 | struct mschm_decompressor *base, struct mschmd_file *file, | ||
40 | const char *filename); | ||
41 | static int chmd_sys_write( | ||
42 | struct mspack_file *file, void *buffer, int bytes); | ||
43 | static int chmd_init_decomp( | ||
44 | struct mschm_decompressor_p *self, struct mschmd_file *file); | ||
45 | static int read_reset_table( | ||
46 | struct mschm_decompressor_p *self, struct mschmd_sec_mscompressed *sec, | ||
47 | int entry, off_t *length_ptr, off_t *offset_ptr); | ||
48 | static int read_spaninfo( | ||
49 | struct mschm_decompressor_p *self, struct mschmd_sec_mscompressed *sec, | ||
50 | off_t *length_ptr); | ||
51 | static int find_sys_file( | ||
52 | struct mschm_decompressor_p *self, struct mschmd_sec_mscompressed *sec, | ||
53 | struct mschmd_file **f_ptr, const char *name); | ||
54 | static unsigned char *read_sys_file( | ||
55 | struct mschm_decompressor_p *self, struct mschmd_file *file); | ||
56 | static int chmd_error( | ||
57 | struct mschm_decompressor *base); | ||
58 | static int read_off64( | ||
59 | off_t *var, unsigned char *mem, struct mspack_system *sys, | ||
60 | struct mspack_file *fh); | ||
61 | |||
62 | /* filenames of the system files used for decompression. | ||
63 | * Content and ControlData are essential. | ||
64 | * ResetTable is preferred, but SpanInfo can be used if not available | ||
65 | */ | ||
66 | static const char *content_name = "::DataSpace/Storage/MSCompressed/Content"; | ||
67 | static const char *control_name = "::DataSpace/Storage/MSCompressed/ControlData"; | ||
68 | static const char *spaninfo_name = "::DataSpace/Storage/MSCompressed/SpanInfo"; | ||
69 | static const char *rtable_name = "::DataSpace/Storage/MSCompressed/Transform/" | ||
70 | "{7FC28940-9D31-11D0-9B27-00A0C91E9C7C}/InstanceData/ResetTable"; | ||
71 | |||
72 | /*************************************** | ||
73 | * MSPACK_CREATE_CHM_DECOMPRESSOR | ||
74 | *************************************** | ||
75 | * constructor | ||
76 | */ | ||
77 | struct mschm_decompressor * | ||
78 | mspack_create_chm_decompressor(struct mspack_system *sys) | ||
79 | { | ||
80 | struct mschm_decompressor_p *self = NULL; | ||
81 | |||
82 | if (!sys) sys = mspack_default_system; | ||
83 | if (!mspack_valid_system(sys)) return NULL; | ||
84 | |||
85 | if ((self = (struct mschm_decompressor_p *) sys->alloc(sys, sizeof(struct mschm_decompressor_p)))) { | ||
86 | self->base.open = &chmd_open; | ||
87 | self->base.close = &chmd_close; | ||
88 | self->base.extract = &chmd_extract; | ||
89 | self->base.last_error = &chmd_error; | ||
90 | self->base.fast_open = &chmd_fast_open; | ||
91 | self->base.fast_find = &chmd_fast_find; | ||
92 | self->system = sys; | ||
93 | self->error = MSPACK_ERR_OK; | ||
94 | self->d = NULL; | ||
95 | } | ||
96 | return (struct mschm_decompressor *) self; | ||
97 | } | ||
98 | |||
99 | /*************************************** | ||
100 | * MSPACK_DESTROY_CAB_DECOMPRESSOR | ||
101 | *************************************** | ||
102 | * destructor | ||
103 | */ | ||
104 | void mspack_destroy_chm_decompressor(struct mschm_decompressor *base) { | ||
105 | struct mschm_decompressor_p *self = (struct mschm_decompressor_p *) base; | ||
106 | if (self) { | ||
107 | struct mspack_system *sys = self->system; | ||
108 | if (self->d) { | ||
109 | if (self->d->infh) sys->close(self->d->infh); | ||
110 | if (self->d->state) lzxd_free(self->d->state); | ||
111 | sys->free(self->d); | ||
112 | } | ||
113 | sys->free(self); | ||
114 | } | ||
115 | } | ||
116 | |||
117 | /*************************************** | ||
118 | * CHMD_OPEN | ||
119 | *************************************** | ||
120 | * opens a file and tries to read it as a CHM file. | ||
121 | * Calls chmd_real_open() with entire=1. | ||
122 | */ | ||
123 | static struct mschmd_header *chmd_open(struct mschm_decompressor *base, | ||
124 | const char *filename) | ||
125 | { | ||
126 | return chmd_real_open(base, filename, 1); | ||
127 | } | ||
128 | |||
129 | /*************************************** | ||
130 | * CHMD_FAST_OPEN | ||
131 | *************************************** | ||
132 | * opens a file and tries to read it as a CHM file, but does not read | ||
133 | * the file headers. Calls chmd_real_open() with entire=0 | ||
134 | */ | ||
135 | static struct mschmd_header *chmd_fast_open(struct mschm_decompressor *base, | ||
136 | const char *filename) | ||
137 | { | ||
138 | return chmd_real_open(base, filename, 0); | ||
139 | } | ||
140 | |||
141 | /*************************************** | ||
142 | * CHMD_REAL_OPEN | ||
143 | *************************************** | ||
144 | * the real implementation of chmd_open() and chmd_fast_open(). It simply | ||
145 | * passes the "entire" parameter to chmd_read_headers(), which will then | ||
146 | * either read all headers, or a bare mininum. | ||
147 | */ | ||
148 | static struct mschmd_header *chmd_real_open(struct mschm_decompressor *base, | ||
149 | const char *filename, int entire) | ||
150 | { | ||
151 | struct mschm_decompressor_p *self = (struct mschm_decompressor_p *) base; | ||
152 | struct mschmd_header *chm = NULL; | ||
153 | struct mspack_system *sys; | ||
154 | struct mspack_file *fh; | ||
155 | int error; | ||
156 | |||
157 | if (!base) return NULL; | ||
158 | sys = self->system; | ||
159 | |||
160 | if ((fh = sys->open(sys, filename, MSPACK_SYS_OPEN_READ))) { | ||
161 | if ((chm = (struct mschmd_header *) sys->alloc(sys, sizeof(struct mschmd_header)))) { | ||
162 | chm->filename = filename; | ||
163 | error = chmd_read_headers(sys, fh, chm, entire); | ||
164 | if (error) { | ||
165 | /* if the error is DATAFORMAT, and there are some results, return | ||
166 | * partial results with a warning, rather than nothing */ | ||
167 | if (error == MSPACK_ERR_DATAFORMAT && (chm->files || chm->sysfiles)) { | ||
168 | sys->message(fh, "WARNING; contents are corrupt"); | ||
169 | error = MSPACK_ERR_OK; | ||
170 | } | ||
171 | else { | ||
172 | chmd_close(base, chm); | ||
173 | chm = NULL; | ||
174 | } | ||
175 | } | ||
176 | self->error = error; | ||
177 | } | ||
178 | else { | ||
179 | self->error = MSPACK_ERR_NOMEMORY; | ||
180 | } | ||
181 | sys->close(fh); | ||
182 | } | ||
183 | else { | ||
184 | self->error = MSPACK_ERR_OPEN; | ||
185 | } | ||
186 | return chm; | ||
187 | } | ||
188 | |||
189 | /*************************************** | ||
190 | * CHMD_CLOSE | ||
191 | *************************************** | ||
192 | * frees all memory associated with a given mschmd_header | ||
193 | */ | ||
194 | static void chmd_close(struct mschm_decompressor *base, | ||
195 | struct mschmd_header *chm) | ||
196 | { | ||
197 | struct mschm_decompressor_p *self = (struct mschm_decompressor_p *) base; | ||
198 | struct mschmd_file *fi, *nfi; | ||
199 | struct mspack_system *sys; | ||
200 | unsigned int i; | ||
201 | |||
202 | if (!base) return; | ||
203 | sys = self->system; | ||
204 | |||
205 | self->error = MSPACK_ERR_OK; | ||
206 | |||
207 | /* free files */ | ||
208 | for (fi = chm->files; fi; fi = nfi) { | ||
209 | nfi = fi->next; | ||
210 | sys->free(fi); | ||
211 | } | ||
212 | for (fi = chm->sysfiles; fi; fi = nfi) { | ||
213 | nfi = fi->next; | ||
214 | sys->free(fi); | ||
215 | } | ||
216 | |||
217 | /* if this CHM was being decompressed, free decompression state */ | ||
218 | if (self->d && (self->d->chm == chm)) { | ||
219 | if (self->d->infh) sys->close(self->d->infh); | ||
220 | if (self->d->state) lzxd_free(self->d->state); | ||
221 | sys->free(self->d); | ||
222 | self->d = NULL; | ||
223 | } | ||
224 | |||
225 | /* if this CHM had a chunk cache, free it and contents */ | ||
226 | if (chm->chunk_cache) { | ||
227 | for (i = 0; i < chm->num_chunks; i++) sys->free(chm->chunk_cache[i]); | ||
228 | sys->free(chm->chunk_cache); | ||
229 | } | ||
230 | |||
231 | sys->free(chm); | ||
232 | } | ||
233 | |||
234 | /*************************************** | ||
235 | * CHMD_READ_HEADERS | ||
236 | *************************************** | ||
237 | * reads the basic CHM file headers. If the "entire" parameter is | ||
238 | * non-zero, all file entries will also be read. fills out a pre-existing | ||
239 | * mschmd_header structure, allocates memory for files as necessary | ||
240 | */ | ||
241 | |||
242 | /* The GUIDs found in CHM headers */ | ||
243 | static const unsigned char guids[32] = { | ||
244 | /* {7C01FD10-7BAA-11D0-9E0C-00A0-C922-E6EC} */ | ||
245 | 0x10, 0xFD, 0x01, 0x7C, 0xAA, 0x7B, 0xD0, 0x11, | ||
246 | 0x9E, 0x0C, 0x00, 0xA0, 0xC9, 0x22, 0xE6, 0xEC, | ||
247 | /* {7C01FD11-7BAA-11D0-9E0C-00A0-C922-E6EC} */ | ||
248 | 0x11, 0xFD, 0x01, 0x7C, 0xAA, 0x7B, 0xD0, 0x11, | ||
249 | 0x9E, 0x0C, 0x00, 0xA0, 0xC9, 0x22, 0xE6, 0xEC | ||
250 | }; | ||
251 | |||
252 | /* reads an encoded integer into a variable; 7 bits of data per byte, | ||
253 | * the high bit is used to indicate that there is another byte */ | ||
254 | #define READ_ENCINT(var) do { \ | ||
255 | (var) = 0; \ | ||
256 | do { \ | ||
257 | if (p > end) goto chunk_end; \ | ||
258 | (var) = ((var) << 7) | (*p & 0x7F); \ | ||
259 | } while (*p++ & 0x80); \ | ||
260 | } while (0) | ||
261 | |||
262 | static int chmd_read_headers(struct mspack_system *sys, struct mspack_file *fh, | ||
263 | struct mschmd_header *chm, int entire) | ||
264 | { | ||
265 | unsigned int section, name_len, x, errors, num_chunks; | ||
266 | unsigned char buf[0x54], *chunk = NULL, *name, *p, *end; | ||
267 | struct mschmd_file *fi, *link = NULL; | ||
268 | off_t offset, length; | ||
269 | int num_entries; | ||
270 | |||
271 | /* initialise pointers */ | ||
272 | chm->files = NULL; | ||
273 | chm->sysfiles = NULL; | ||
274 | chm->chunk_cache = NULL; | ||
275 | chm->sec0.base.chm = chm; | ||
276 | chm->sec0.base.id = 0; | ||
277 | chm->sec1.base.chm = chm; | ||
278 | chm->sec1.base.id = 1; | ||
279 | chm->sec1.content = NULL; | ||
280 | chm->sec1.control = NULL; | ||
281 | chm->sec1.spaninfo = NULL; | ||
282 | chm->sec1.rtable = NULL; | ||
283 | |||
284 | /* read the first header */ | ||
285 | if (sys->read(fh, &buf[0], chmhead_SIZEOF) != chmhead_SIZEOF) { | ||
286 | return MSPACK_ERR_READ; | ||
287 | } | ||
288 | |||
289 | /* check ITSF signature */ | ||
290 | if (EndGetI32(&buf[chmhead_Signature]) != 0x46535449) { | ||
291 | return MSPACK_ERR_SIGNATURE; | ||
292 | } | ||
293 | |||
294 | /* check both header GUIDs */ | ||
295 | if (mspack_memcmp(&buf[chmhead_GUID1], &guids[0], 32L) != 0) { | ||
296 | D(("incorrect GUIDs")) | ||
297 | return MSPACK_ERR_SIGNATURE; | ||
298 | } | ||
299 | |||
300 | chm->version = EndGetI32(&buf[chmhead_Version]); | ||
301 | chm->timestamp = EndGetM32(&buf[chmhead_Timestamp]); | ||
302 | chm->language = EndGetI32(&buf[chmhead_LanguageID]); | ||
303 | if (chm->version > 3) { | ||
304 | sys->message(fh, "WARNING; CHM version > 3"); | ||
305 | } | ||
306 | |||
307 | /* read the header section table */ | ||
308 | if (sys->read(fh, &buf[0], chmhst3_SIZEOF) != chmhst3_SIZEOF) { | ||
309 | return MSPACK_ERR_READ; | ||
310 | } | ||
311 | |||
312 | /* chmhst3_OffsetCS0 does not exist in version 1 or 2 CHM files. | ||
313 | * The offset will be corrected later, once HS1 is read. | ||
314 | */ | ||
315 | if (read_off64(&offset, &buf[chmhst_OffsetHS0], sys, fh) || | ||
316 | read_off64(&chm->dir_offset, &buf[chmhst_OffsetHS1], sys, fh) || | ||
317 | read_off64(&chm->sec0.offset, &buf[chmhst3_OffsetCS0], sys, fh)) | ||
318 | { | ||
319 | return MSPACK_ERR_DATAFORMAT; | ||
320 | } | ||
321 | |||
322 | /* seek to header section 0 */ | ||
323 | if (sys->seek(fh, offset, MSPACK_SYS_SEEK_START)) { | ||
324 | return MSPACK_ERR_SEEK; | ||
325 | } | ||
326 | |||
327 | /* read header section 0 */ | ||
328 | if (sys->read(fh, &buf[0], chmhs0_SIZEOF) != chmhs0_SIZEOF) { | ||
329 | return MSPACK_ERR_READ; | ||
330 | } | ||
331 | if (read_off64(&chm->length, &buf[chmhs0_FileLen], sys, fh)) { | ||
332 | return MSPACK_ERR_DATAFORMAT; | ||
333 | } | ||
334 | |||
335 | /* seek to header section 1 */ | ||
336 | if (sys->seek(fh, chm->dir_offset, MSPACK_SYS_SEEK_START)) { | ||
337 | return MSPACK_ERR_SEEK; | ||
338 | } | ||
339 | |||
340 | /* read header section 1 */ | ||
341 | if (sys->read(fh, &buf[0], chmhs1_SIZEOF) != chmhs1_SIZEOF) { | ||
342 | return MSPACK_ERR_READ; | ||
343 | } | ||
344 | |||
345 | chm->dir_offset = sys->tell(fh); | ||
346 | chm->chunk_size = EndGetI32(&buf[chmhs1_ChunkSize]); | ||
347 | chm->density = EndGetI32(&buf[chmhs1_Density]); | ||
348 | chm->depth = EndGetI32(&buf[chmhs1_Depth]); | ||
349 | chm->index_root = EndGetI32(&buf[chmhs1_IndexRoot]); | ||
350 | chm->num_chunks = EndGetI32(&buf[chmhs1_NumChunks]); | ||
351 | chm->first_pmgl = EndGetI32(&buf[chmhs1_FirstPMGL]); | ||
352 | chm->last_pmgl = EndGetI32(&buf[chmhs1_LastPMGL]); | ||
353 | |||
354 | if (chm->version < 3) { | ||
355 | /* versions before 3 don't have chmhst3_OffsetCS0 */ | ||
356 | chm->sec0.offset = chm->dir_offset + (chm->chunk_size * chm->num_chunks); | ||
357 | } | ||
358 | |||
359 | /* ensure chunk size is large enough for signature and num_entries */ | ||
360 | if (chm->chunk_size < (pmgl_Entries + 2)) { | ||
361 | return MSPACK_ERR_DATAFORMAT; | ||
362 | } | ||
363 | |||
364 | /* if we are doing a quick read, stop here! */ | ||
365 | if (!entire) { | ||
366 | return MSPACK_ERR_OK; | ||
367 | } | ||
368 | |||
369 | /* seek to the first PMGL chunk, and reduce the number of chunks to read */ | ||
370 | if ((x = chm->first_pmgl) != 0) { | ||
371 | if (sys->seek(fh,(off_t) (x * chm->chunk_size), MSPACK_SYS_SEEK_CUR)) { | ||
372 | return MSPACK_ERR_SEEK; | ||
373 | } | ||
374 | } | ||
375 | num_chunks = chm->last_pmgl - x + 1; | ||
376 | |||
377 | if (!(chunk = (unsigned char *) sys->alloc(sys, (size_t)chm->chunk_size))) { | ||
378 | return MSPACK_ERR_NOMEMORY; | ||
379 | } | ||
380 | |||
381 | /* read and process all chunks from FirstPMGL to LastPMGL */ | ||
382 | errors = 0; | ||
383 | while (num_chunks--) { | ||
384 | /* read next chunk */ | ||
385 | if (sys->read(fh, chunk, (int)chm->chunk_size) != (int)chm->chunk_size) { | ||
386 | sys->free(chunk); | ||
387 | return MSPACK_ERR_READ; | ||
388 | } | ||
389 | |||
390 | /* process only directory (PMGL) chunks */ | ||
391 | if (EndGetI32(&chunk[pmgl_Signature]) != 0x4C474D50) continue; | ||
392 | |||
393 | if (EndGetI32(&chunk[pmgl_QuickRefSize]) < 2) { | ||
394 | sys->message(fh, "WARNING; PMGL quickref area is too small"); | ||
395 | } | ||
396 | if (EndGetI32(&chunk[pmgl_QuickRefSize]) > | ||
397 | ((int)chm->chunk_size - pmgl_Entries)) | ||
398 | { | ||
399 | sys->message(fh, "WARNING; PMGL quickref area is too large"); | ||
400 | } | ||
401 | |||
402 | p = &chunk[pmgl_Entries]; | ||
403 | end = &chunk[chm->chunk_size - 2]; | ||
404 | num_entries = EndGetI16(end); | ||
405 | |||
406 | while (num_entries--) { | ||
407 | READ_ENCINT(name_len); name = p; p += name_len; | ||
408 | READ_ENCINT(section); | ||
409 | READ_ENCINT(offset); | ||
410 | READ_ENCINT(length); | ||
411 | |||
412 | /* empty files and directory names are stored as a file entry at | ||
413 | * offset 0 with length 0. We want to keep empty files, but not | ||
414 | * directory names, which end with a "/" */ | ||
415 | if ((offset == 0) && (length == 0)) { | ||
416 | if ((name_len > 0) && (name[name_len-1] == '/')) continue; | ||
417 | } | ||
418 | |||
419 | if (section > 1) { | ||
420 | sys->message(fh, "invalid section number '%u'.", section); | ||
421 | continue; | ||
422 | } | ||
423 | |||
424 | if (!(fi = (struct mschmd_file *) sys->alloc(sys, sizeof(struct mschmd_file) + name_len + 1))) { | ||
425 | sys->free(chunk); | ||
426 | return MSPACK_ERR_NOMEMORY; | ||
427 | } | ||
428 | |||
429 | fi->next = NULL; | ||
430 | fi->filename = (char *) &fi[1]; | ||
431 | fi->section = ((section == 0) ? (struct mschmd_section *) (&chm->sec0) | ||
432 | : (struct mschmd_section *) (&chm->sec1)); | ||
433 | fi->offset = offset; | ||
434 | fi->length = length; | ||
435 | sys->copy(name, fi->filename, (size_t) name_len); | ||
436 | fi->filename[name_len] = '\0'; | ||
437 | |||
438 | if (name[0] == ':' && name[1] == ':') { | ||
439 | /* system file */ | ||
440 | if (mspack_memcmp(&name[2], &content_name[2], 31L) == 0) { | ||
441 | if (mspack_memcmp(&name[33], &content_name[33], 8L) == 0) { | ||
442 | chm->sec1.content = fi; | ||
443 | } | ||
444 | else if (mspack_memcmp(&name[33], &control_name[33], 11L) == 0) { | ||
445 | chm->sec1.control = fi; | ||
446 | } | ||
447 | else if (mspack_memcmp(&name[33], &spaninfo_name[33], 8L) == 0) { | ||
448 | chm->sec1.spaninfo = fi; | ||
449 | } | ||
450 | else if (mspack_memcmp(&name[33], &rtable_name[33], 72L) == 0) { | ||
451 | chm->sec1.rtable = fi; | ||
452 | } | ||
453 | } | ||
454 | fi->next = chm->sysfiles; | ||
455 | chm->sysfiles = fi; | ||
456 | } | ||
457 | else { | ||
458 | /* normal file */ | ||
459 | if (link) link->next = fi; else chm->files = fi; | ||
460 | link = fi; | ||
461 | } | ||
462 | } | ||
463 | |||
464 | /* this is reached either when num_entries runs out, or if | ||
465 | * reading data from the chunk reached a premature end of chunk */ | ||
466 | chunk_end: | ||
467 | if (num_entries >= 0) { | ||
468 | D(("chunk ended before all entries could be read")) | ||
469 | errors++; | ||
470 | } | ||
471 | |||
472 | } | ||
473 | sys->free(chunk); | ||
474 | return (errors > 0) ? MSPACK_ERR_DATAFORMAT : MSPACK_ERR_OK; | ||
475 | } | ||
476 | |||
477 | /*************************************** | ||
478 | * CHMD_FAST_FIND | ||
479 | *************************************** | ||
480 | * uses PMGI index chunks and quickref data to quickly locate a file | ||
481 | * directly from the on-disk index. | ||
482 | * | ||
483 | * TODO: protect against infinite loops in chunks (where pgml_NextChunk | ||
484 | * or a PGMI index entry point to an already visited chunk) | ||
485 | */ | ||
486 | static int chmd_fast_find(struct mschm_decompressor *base, | ||
487 | struct mschmd_header *chm, const char *filename, | ||
488 | struct mschmd_file *f_ptr, int f_size) | ||
489 | { | ||
490 | struct mschm_decompressor_p *self = (struct mschm_decompressor_p *) base; | ||
491 | struct mspack_system *sys; | ||
492 | struct mspack_file *fh; | ||
493 | const unsigned char *chunk, *p, *end; | ||
494 | int err = MSPACK_ERR_OK, result = -1; | ||
495 | unsigned int n, sec; | ||
496 | |||
497 | if (!self || !chm || !f_ptr || (f_size != sizeof(struct mschmd_file))) { | ||
498 | return MSPACK_ERR_ARGS; | ||
499 | } | ||
500 | sys = self->system; | ||
501 | |||
502 | /* clear the results structure */ | ||
503 | memset(f_ptr, 0, f_size); | ||
504 | |||
505 | if (!(fh = sys->open(sys, chm->filename, MSPACK_SYS_OPEN_READ))) { | ||
506 | return MSPACK_ERR_OPEN; | ||
507 | } | ||
508 | |||
509 | /* go through PMGI chunk hierarchy to reach PMGL chunk */ | ||
510 | if (chm->index_root < chm->num_chunks) { | ||
511 | n = chm->index_root; | ||
512 | for (;;) { | ||
513 | if (!(chunk = read_chunk(self, chm, fh, n))) { | ||
514 | sys->close(fh); | ||
515 | return self->error; | ||
516 | } | ||
517 | |||
518 | /* search PMGI/PMGL chunk. exit early if no entry found */ | ||
519 | if ((result = search_chunk(chm, chunk, filename, &p, &end)) <= 0) { | ||
520 | break; | ||
521 | } | ||
522 | |||
523 | /* found result. loop around for next chunk if this is PMGI */ | ||
524 | if (chunk[3] == 0x4C) break; else READ_ENCINT(n); | ||
525 | } | ||
526 | } | ||
527 | else { | ||
528 | /* PMGL chunks only, search from first_pmgl to last_pmgl */ | ||
529 | for (n = chm->first_pmgl; n <= chm->last_pmgl; | ||
530 | n = EndGetI32(&chunk[pmgl_NextChunk])) | ||
531 | { | ||
532 | if (!(chunk = read_chunk(self, chm, fh, n))) { | ||
533 | err = self->error; | ||
534 | break; | ||
535 | } | ||
536 | |||
537 | /* search PMGL chunk. exit if file found */ | ||
538 | if ((result = search_chunk(chm, chunk, filename, &p, &end)) > 0) { | ||
539 | break; | ||
540 | } | ||
541 | } | ||
542 | } | ||
543 | |||
544 | /* if we found a file, read it */ | ||
545 | if (result > 0) { | ||
546 | READ_ENCINT(sec); | ||
547 | f_ptr->section = (sec == 0) ? (struct mschmd_section *) &chm->sec0 | ||
548 | : (struct mschmd_section *) &chm->sec1; | ||
549 | READ_ENCINT(f_ptr->offset); | ||
550 | READ_ENCINT(f_ptr->length); | ||
551 | } | ||
552 | else if (result < 0) { | ||
553 | err = MSPACK_ERR_DATAFORMAT; | ||
554 | } | ||
555 | |||
556 | sys->close(fh); | ||
557 | return self->error = err; | ||
558 | |||
559 | chunk_end: | ||
560 | D(("read beyond end of chunk entries")) | ||
561 | sys->close(fh); | ||
562 | return self->error = MSPACK_ERR_DATAFORMAT; | ||
563 | } | ||
564 | |||
565 | /* reads the given chunk into memory, storing it in a chunk cache | ||
566 | * so it doesn't need to be read from disk more than once | ||
567 | */ | ||
568 | static unsigned char *read_chunk(struct mschm_decompressor_p *self, | ||
569 | struct mschmd_header *chm, | ||
570 | struct mspack_file *fh, | ||
571 | unsigned int chunk_num) | ||
572 | { | ||
573 | struct mspack_system *sys = self->system; | ||
574 | unsigned char *buf; | ||
575 | |||
576 | /* check arguments - most are already checked by chmd_fast_find */ | ||
577 | if (chunk_num > chm->num_chunks) return NULL; | ||
578 | |||
579 | /* ensure chunk cache is available */ | ||
580 | if (!chm->chunk_cache) { | ||
581 | size_t size = sizeof(unsigned char *) * chm->num_chunks; | ||
582 | if (!(chm->chunk_cache = (unsigned char **) sys->alloc(sys, size))) { | ||
583 | self->error = MSPACK_ERR_NOMEMORY; | ||
584 | return NULL; | ||
585 | } | ||
586 | memset(chm->chunk_cache, 0, size); | ||
587 | } | ||
588 | |||
589 | /* try to answer out of chunk cache */ | ||
590 | if (chm->chunk_cache[chunk_num]) return chm->chunk_cache[chunk_num]; | ||
591 | |||
592 | /* need to read chunk - allocate memory for it */ | ||
593 | if (!(buf = (unsigned char *) sys->alloc(sys, chm->chunk_size))) { | ||
594 | self->error = MSPACK_ERR_NOMEMORY; | ||
595 | return NULL; | ||
596 | } | ||
597 | |||
598 | /* seek to block and read it */ | ||
599 | if (sys->seek(fh, (off_t) (chm->dir_offset + (chunk_num * chm->chunk_size)), | ||
600 | MSPACK_SYS_SEEK_START)) | ||
601 | { | ||
602 | self->error = MSPACK_ERR_SEEK; | ||
603 | sys->free(buf); | ||
604 | return NULL; | ||
605 | } | ||
606 | if (sys->read(fh, buf, (int)chm->chunk_size) != (int)chm->chunk_size) { | ||
607 | self->error = MSPACK_ERR_READ; | ||
608 | sys->free(buf); | ||
609 | return NULL; | ||
610 | } | ||
611 | |||
612 | /* check the signature. Is is PMGL or PMGI? */ | ||
613 | if (!((buf[0] == 0x50) && (buf[1] == 0x4D) && (buf[2] == 0x47) && | ||
614 | ((buf[3] == 0x4C) || (buf[3] == 0x49)))) | ||
615 | { | ||
616 | self->error = MSPACK_ERR_SEEK; | ||
617 | sys->free(buf); | ||
618 | return NULL; | ||
619 | } | ||
620 | |||
621 | /* all OK. Store chunk in cache and return it */ | ||
622 | return chm->chunk_cache[chunk_num] = buf; | ||
623 | } | ||
624 | |||
625 | /* searches a PMGI/PMGL chunk for a given filename entry. Returns -1 on | ||
626 | * data format error, 0 if entry definitely not found, 1 if entry | ||
627 | * found. In the latter case, *result and *result_end are set pointing | ||
628 | * to that entry's data (either the "next chunk" ENCINT for a PMGI or | ||
629 | * the section, offset and length ENCINTs for a PMGL). | ||
630 | * | ||
631 | * In the case of PMGL chunks, the entry has definitely been | ||
632 | * found. In the case of PMGI chunks, the entry which points to the | ||
633 | * chunk that may eventually contain that entry has been found. | ||
634 | */ | ||
635 | static int search_chunk(struct mschmd_header *chm, | ||
636 | const unsigned char *chunk, | ||
637 | const char *filename, | ||
638 | const unsigned char **result, | ||
639 | const unsigned char **result_end) | ||
640 | { | ||
641 | const unsigned char *start, *end, *p; | ||
642 | unsigned int qr_size, num_entries, qr_entries, qr_density, name_len; | ||
643 | unsigned int L, R, M, sec, fname_len, entries_off, is_pmgl; | ||
644 | int cmp; | ||
645 | |||
646 | fname_len = strlen(filename); | ||
647 | |||
648 | /* PMGL chunk or PMGI chunk? (note: read_chunk() has already | ||
649 | * checked the rest of the characters in the chunk signature) */ | ||
650 | if (chunk[3] == 0x4C) { | ||
651 | is_pmgl = 1; | ||
652 | entries_off = pmgl_Entries; | ||
653 | } | ||
654 | else { | ||
655 | is_pmgl = 0; | ||
656 | entries_off = pmgi_Entries; | ||
657 | } | ||
658 | |||
659 | /* Step 1: binary search first filename of each QR entry | ||
660 | * - target filename == entry | ||
661 | * found file | ||
662 | * - target filename < all entries | ||
663 | * file not found | ||
664 | * - target filename > all entries | ||
665 | * proceed to step 2 using final entry | ||
666 | * - target filename between two searched entries | ||
667 | * proceed to step 2 | ||
668 | */ | ||
669 | qr_size = EndGetI32(&chunk[pmgl_QuickRefSize]); | ||
670 | start = &chunk[chm->chunk_size - 2]; | ||
671 | end = &chunk[chm->chunk_size - qr_size]; | ||
672 | num_entries = EndGetI16(start); | ||
673 | qr_density = 1 + (1 << chm->density); | ||
674 | qr_entries = (num_entries + qr_density-1) / qr_density; | ||
675 | |||
676 | if (num_entries == 0) { | ||
677 | D(("chunk has no entries")) | ||
678 | return -1; | ||
679 | } | ||
680 | |||
681 | if (qr_size > chm->chunk_size) { | ||
682 | D(("quickref size > chunk size")) | ||
683 | return -1; | ||
684 | } | ||
685 | |||
686 | *result_end = end; | ||
687 | |||
688 | if (((int)qr_entries * 2) > (start - end)) { | ||
689 | D(("WARNING; more quickrefs than quickref space")) | ||
690 | qr_entries = 0; /* but we can live with it */ | ||
691 | } | ||
692 | |||
693 | if (qr_entries > 0) { | ||
694 | L = 0; | ||
695 | R = qr_entries - 1; | ||
696 | do { | ||
697 | /* pick new midpoint */ | ||
698 | M = (L + R) >> 1; | ||
699 | |||
700 | /* compare filename with entry QR points to */ | ||
701 | p = &chunk[entries_off + (M ? EndGetI16(start - (M << 1)) : 0)]; | ||
702 | READ_ENCINT(name_len); | ||
703 | if (p + name_len > end) goto chunk_end; | ||
704 | cmp = compare(filename, (char *)p, fname_len, name_len); | ||
705 | |||
706 | if (cmp == 0) break; | ||
707 | else if (cmp < 0) { if (M) R = M - 1; else return 0; } | ||
708 | else if (cmp > 0) L = M + 1; | ||
709 | } while (L <= R); | ||
710 | M = (L + R) >> 1; | ||
711 | |||
712 | if (cmp == 0) { | ||
713 | /* exact match! */ | ||
714 | p += name_len; | ||
715 | *result = p; | ||
716 | return 1; | ||
717 | } | ||
718 | |||
719 | /* otherwise, read the group of entries for QR entry M */ | ||
720 | p = &chunk[entries_off + (M ? EndGetI16(start - (M << 1)) : 0)]; | ||
721 | num_entries -= (M * qr_density); | ||
722 | if (num_entries > qr_density) num_entries = qr_density; | ||
723 | } | ||
724 | else { | ||
725 | p = &chunk[entries_off]; | ||
726 | } | ||
727 | |||
728 | /* Step 2: linear search through the set of entries reached in step 1. | ||
729 | * - filename == any entry | ||
730 | * found entry | ||
731 | * - filename < all entries (PMGI) or any entry (PMGL) | ||
732 | * entry not found, stop now | ||
733 | * - filename > all entries | ||
734 | * entry not found (PMGL) / maybe found (PMGI) | ||
735 | * - | ||
736 | */ | ||
737 | *result = NULL; | ||
738 | while (num_entries-- > 0) { | ||
739 | READ_ENCINT(name_len); | ||
740 | if (p + name_len > end) goto chunk_end; | ||
741 | cmp = compare(filename, (char *)p, fname_len, name_len); | ||
742 | p += name_len; | ||
743 | |||
744 | if (cmp == 0) { | ||
745 | /* entry found */ | ||
746 | *result = p; | ||
747 | return 1; | ||
748 | } | ||
749 | |||
750 | if (cmp < 0) { | ||
751 | /* entry not found (PMGL) / maybe found (PMGI) */ | ||
752 | break; | ||
753 | } | ||
754 | |||
755 | /* read and ignore the rest of this entry */ | ||
756 | if (is_pmgl) { | ||
757 | READ_ENCINT(R); /* skip section */ | ||
758 | READ_ENCINT(R); /* skip offset */ | ||
759 | READ_ENCINT(R); /* skip length */ | ||
760 | } | ||
761 | else { | ||
762 | *result = p; /* store potential final result */ | ||
763 | READ_ENCINT(R); /* skip chunk number */ | ||
764 | } | ||
765 | } | ||
766 | |||
767 | /* PMGL? not found. PMGI? maybe found */ | ||
768 | return (is_pmgl) ? 0 : (*result ? 1 : 0); | ||
769 | |||
770 | chunk_end: | ||
771 | D(("reached end of chunk data while searching")) | ||
772 | return -1; | ||
773 | } | ||
774 | |||
775 | #if HAVE_TOWLOWER | ||
776 | # if HAVE_WCTYPE_H | ||
777 | # include <wctype.h> | ||
778 | # endif | ||
779 | # define TOLOWER(x) towlower(x) | ||
780 | #elif HAVE_TOLOWER | ||
781 | # if HAVE_CTYPE_H | ||
782 | # include <ctype.h> | ||
783 | # endif | ||
784 | # define TOLOWER(x) tolower(x) | ||
785 | #else | ||
786 | # define TOLOWER(x) (((x)<0||(x)>256)?(x):mspack_tolower_map[(x)]) | ||
787 | /* Map of char -> lowercase char for the first 256 chars. Generated with: | ||
788 | * LC_CTYPE=en_GB.utf-8 perl -Mlocale -le 'print map{ord(lc chr).","} 0..255' | ||
789 | */ | ||
790 | static const unsigned char mspack_tolower_map[256] = { | ||
791 | 0,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, | ||
792 | 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, | ||
793 | 53,54,55,56,57,58,59,60,61,62,63,64,97,98,99,100,101,102,103,104,105,106, | ||
794 | 107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,91,92,93,94, | ||
795 | 95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114, | ||
796 | 115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133, | ||
797 | 134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152, | ||
798 | 153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171, | ||
799 | 172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190, | ||
800 | 191,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241, | ||
801 | 242,243,244,245,246,215,248,249,250,251,252,253,254,223,224,225,226,227,228, | ||
802 | 229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247, | ||
803 | 248,249,250,251,252,253,254,255 | ||
804 | }; | ||
805 | #endif | ||
806 | |||
807 | /* decodes a UTF-8 character from s[] into c. Will not read past e. */ | ||
808 | #define GET_UTF8_CHAR(s, e, c) do { \ | ||
809 | unsigned char x = *s++; \ | ||
810 | if (x < 0x80) c = x; \ | ||
811 | else if (x < 0xC0) c = -1; \ | ||
812 | else if (x < 0xE0) { \ | ||
813 | c = (s >= e) ? -1 : ((x & 0x1F) << 6) | (*s++ & 0x3F); \ | ||
814 | } \ | ||
815 | else if (x < 0xF0) { \ | ||
816 | c = (s+2 > e) ? -1 : ((x & 0x0F) << 12) | ((s[0] & 0x3F) << 6) \ | ||
817 | | (s[1] & 0x3F); \ | ||
818 | s += 2; \ | ||
819 | } \ | ||
820 | else if (x < 0xF8) { \ | ||
821 | c = (s+3 > e) ? -1 : ((x & 0x07) << 18) | ((s[0] & 0x3F) << 12) \ | ||
822 | | ((s[1] & 0x3F) << 6) | (s[2] & 0x3F); \ | ||
823 | s += 3; \ | ||
824 | } \ | ||
825 | else if (x < 0xFC) { \ | ||
826 | c = (s+4 > e) ? -1 : ((x & 0x03) << 24) | ((s[0] & 0x3F) << 18) \ | ||
827 | | ((s[1] & 0x3F) << 12)|((s[2] & 0x3F) << 6)|(s[3] & 0x3F); \ | ||
828 | s += 4; \ | ||
829 | } \ | ||
830 | else if (x < 0xFE) { \ | ||
831 | c = (s+5>e)?-1:((x&1)<<30)|((s[0]&0x3F)<<24)|((s[1]&0x3F)<<18)| \ | ||
832 | ((s[2] & 0x3F) << 12) | ((s[3] & 0x3F) << 6)|(s[4] & 0x3F); \ | ||
833 | s += 5; \ | ||
834 | } \ | ||
835 | else c = -1; \ | ||
836 | } while (0) | ||
837 | |||
838 | /* case-insensitively compares two UTF8 encoded strings. String length for | ||
839 | * both strings must be provided, null bytes are not terminators */ | ||
840 | static inline int compare(const char *s1, const char *s2, int l1, int l2) { | ||
841 | register const unsigned char *p1 = (const unsigned char *) s1; | ||
842 | register const unsigned char *p2 = (const unsigned char *) s2; | ||
843 | register const unsigned char *e1 = p1 + l1, *e2 = p2 + l2; | ||
844 | int c1, c2; | ||
845 | |||
846 | while (p1 < e1 && p2 < e2) { | ||
847 | GET_UTF8_CHAR(p1, e1, c1); | ||
848 | GET_UTF8_CHAR(p2, e2, c2); | ||
849 | if (c1 == c2) continue; | ||
850 | c1 = TOLOWER(c1); | ||
851 | c2 = TOLOWER(c2); | ||
852 | if (c1 != c2) return c1 - c2; | ||
853 | } | ||
854 | return l1 - l2; | ||
855 | } | ||
856 | |||
857 | |||
858 | /*************************************** | ||
859 | * CHMD_EXTRACT | ||
860 | *************************************** | ||
861 | * extracts a file from a CHM helpfile | ||
862 | */ | ||
863 | static int chmd_extract(struct mschm_decompressor *base, | ||
864 | struct mschmd_file *file, const char *filename) | ||
865 | { | ||
866 | struct mschm_decompressor_p *self = (struct mschm_decompressor_p *) base; | ||
867 | struct mspack_system *sys; | ||
868 | struct mschmd_header *chm; | ||
869 | struct mspack_file *fh; | ||
870 | off_t bytes; | ||
871 | |||
872 | if (!self) return MSPACK_ERR_ARGS; | ||
873 | if (!file || !file->section) return self->error = MSPACK_ERR_ARGS; | ||
874 | sys = self->system; | ||
875 | chm = file->section->chm; | ||
876 | |||
877 | /* create decompression state if it doesn't exist */ | ||
878 | if (!self->d) { | ||
879 | self->d = (struct mschmd_decompress_state *) sys->alloc(sys, sizeof(struct mschmd_decompress_state)); | ||
880 | if (!self->d) return self->error = MSPACK_ERR_NOMEMORY; | ||
881 | self->d->chm = chm; | ||
882 | self->d->offset = 0; | ||
883 | self->d->state = NULL; | ||
884 | self->d->sys = *sys; | ||
885 | self->d->sys.write = &chmd_sys_write; | ||
886 | self->d->infh = NULL; | ||
887 | self->d->outfh = NULL; | ||
888 | } | ||
889 | |||
890 | /* open input chm file if not open, or the open one is a different chm */ | ||
891 | if (!self->d->infh || (self->d->chm != chm)) { | ||
892 | if (self->d->infh) sys->close(self->d->infh); | ||
893 | if (self->d->state) lzxd_free(self->d->state); | ||
894 | self->d->chm = chm; | ||
895 | self->d->offset = 0; | ||
896 | self->d->state = NULL; | ||
897 | self->d->infh = sys->open(sys, chm->filename, MSPACK_SYS_OPEN_READ); | ||
898 | if (!self->d->infh) return self->error = MSPACK_ERR_OPEN; | ||
899 | } | ||
900 | |||
901 | /* open file for output */ | ||
902 | if (!(fh = sys->open(sys, filename, MSPACK_SYS_OPEN_WRITE))) { | ||
903 | return self->error = MSPACK_ERR_OPEN; | ||
904 | } | ||
905 | |||
906 | /* if file is empty, simply creating it is enough */ | ||
907 | if (!file->length) { | ||
908 | sys->close(fh); | ||
909 | return self->error = MSPACK_ERR_OK; | ||
910 | } | ||
911 | |||
912 | self->error = MSPACK_ERR_OK; | ||
913 | |||
914 | switch (file->section->id) { | ||
915 | case 0: /* Uncompressed section file */ | ||
916 | /* simple seek + copy */ | ||
917 | if (sys->seek(self->d->infh, file->section->chm->sec0.offset | ||
918 | + file->offset, MSPACK_SYS_SEEK_START)) | ||
919 | { | ||
920 | self->error = MSPACK_ERR_SEEK; | ||
921 | } | ||
922 | else { | ||
923 | unsigned char buf[512]; | ||
924 | off_t length = file->length; | ||
925 | while (length > 0) { | ||
926 | int run = sizeof(buf); | ||
927 | if ((off_t)run > length) run = (int)length; | ||
928 | if (sys->read(self->d->infh, &buf[0], run) != run) { | ||
929 | self->error = MSPACK_ERR_READ; | ||
930 | break; | ||
931 | } | ||
932 | if (sys->write(fh, &buf[0], run) != run) { | ||
933 | self->error = MSPACK_ERR_WRITE; | ||
934 | break; | ||
935 | } | ||
936 | length -= run; | ||
937 | } | ||
938 | } | ||
939 | break; | ||
940 | |||
941 | case 1: /* MSCompressed section file */ | ||
942 | /* (re)initialise compression state if we it is not yet initialised, | ||
943 | * or we have advanced too far and have to backtrack | ||
944 | */ | ||
945 | if (!self->d->state || (file->offset < self->d->offset)) { | ||
946 | if (self->d->state) { | ||
947 | lzxd_free(self->d->state); | ||
948 | self->d->state = NULL; | ||
949 | } | ||
950 | if (chmd_init_decomp(self, file)) break; | ||
951 | } | ||
952 | |||
953 | /* seek to input data */ | ||
954 | if (sys->seek(self->d->infh, self->d->inoffset, MSPACK_SYS_SEEK_START)) { | ||
955 | self->error = MSPACK_ERR_SEEK; | ||
956 | break; | ||
957 | } | ||
958 | |||
959 | /* get to correct offset. */ | ||
960 | self->d->outfh = NULL; | ||
961 | if ((bytes = file->offset - self->d->offset)) { | ||
962 | self->error = lzxd_decompress(self->d->state, bytes); | ||
963 | } | ||
964 | |||
965 | /* if getting to the correct offset was error free, unpack file */ | ||
966 | if (!self->error) { | ||
967 | self->d->outfh = fh; | ||
968 | self->error = lzxd_decompress(self->d->state, file->length); | ||
969 | } | ||
970 | |||
971 | /* save offset in input source stream, in case there is a section 0 | ||
972 | * file between now and the next section 1 file extracted */ | ||
973 | self->d->inoffset = sys->tell(self->d->infh); | ||
974 | |||
975 | /* if an LZX error occured, the LZX decompressor is now useless */ | ||
976 | if (self->error) { | ||
977 | if (self->d->state) lzxd_free(self->d->state); | ||
978 | self->d->state = NULL; | ||
979 | } | ||
980 | break; | ||
981 | } | ||
982 | |||
983 | sys->close(fh); | ||
984 | return self->error; | ||
985 | } | ||
986 | |||
987 | /*************************************** | ||
988 | * CHMD_SYS_WRITE | ||
989 | *************************************** | ||
990 | * chmd_sys_write is the internal writer function which the decompressor | ||
991 | * uses. If either writes data to disk (self->d->outfh) with the real | ||
992 | * sys->write() function, or does nothing with the data when | ||
993 | * self->d->outfh == NULL. advances self->d->offset. | ||
994 | */ | ||
995 | static int chmd_sys_write(struct mspack_file *file, void *buffer, int bytes) { | ||
996 | struct mschm_decompressor_p *self = (struct mschm_decompressor_p *) file; | ||
997 | self->d->offset += bytes; | ||
998 | if (self->d->outfh) { | ||
999 | return self->system->write(self->d->outfh, buffer, bytes); | ||
1000 | } | ||
1001 | return bytes; | ||
1002 | } | ||
1003 | |||
1004 | /*************************************** | ||
1005 | * CHMD_INIT_DECOMP | ||
1006 | *************************************** | ||
1007 | * Initialises the LZX decompressor to decompress the compressed stream, | ||
1008 | * from the nearest reset offset and length that is needed for the given | ||
1009 | * file. | ||
1010 | */ | ||
1011 | static int chmd_init_decomp(struct mschm_decompressor_p *self, | ||
1012 | struct mschmd_file *file) | ||
1013 | { | ||
1014 | int window_size, window_bits, reset_interval, entry, err; | ||
1015 | struct mspack_system *sys = self->system; | ||
1016 | struct mschmd_sec_mscompressed *sec; | ||
1017 | unsigned char *data; | ||
1018 | off_t length, offset; | ||
1019 | |||
1020 | sec = (struct mschmd_sec_mscompressed *) file->section; | ||
1021 | |||
1022 | /* ensure we have a mscompressed content section */ | ||
1023 | err = find_sys_file(self, sec, &sec->content, content_name); | ||
1024 | if (err) return self->error = err; | ||
1025 | |||
1026 | /* ensure we have a ControlData file */ | ||
1027 | err = find_sys_file(self, sec, &sec->control, control_name); | ||
1028 | if (err) return self->error = err; | ||
1029 | |||
1030 | /* read ControlData */ | ||
1031 | if (sec->control->length < lzxcd_SIZEOF) { | ||
1032 | D(("ControlData file is too short")) | ||
1033 | return self->error = MSPACK_ERR_DATAFORMAT; | ||
1034 | } | ||
1035 | if (!(data = read_sys_file(self, sec->control))) { | ||
1036 | D(("can't read mscompressed control data file")) | ||
1037 | return self->error; | ||
1038 | } | ||
1039 | |||
1040 | /* check LZXC signature */ | ||
1041 | if (EndGetI32(&data[lzxcd_Signature]) != 0x43585A4C) { | ||
1042 | sys->free(data); | ||
1043 | return self->error = MSPACK_ERR_SIGNATURE; | ||
1044 | } | ||
1045 | |||
1046 | /* read reset_interval and window_size and validate version number */ | ||
1047 | switch (EndGetI32(&data[lzxcd_Version])) { | ||
1048 | case 1: | ||
1049 | reset_interval = EndGetI32(&data[lzxcd_ResetInterval]); | ||
1050 | window_size = EndGetI32(&data[lzxcd_WindowSize]); | ||
1051 | break; | ||
1052 | case 2: | ||
1053 | reset_interval = EndGetI32(&data[lzxcd_ResetInterval]) * LZX_FRAME_SIZE; | ||
1054 | window_size = EndGetI32(&data[lzxcd_WindowSize]) * LZX_FRAME_SIZE; | ||
1055 | break; | ||
1056 | default: | ||
1057 | D(("bad controldata version")) | ||
1058 | sys->free(data); | ||
1059 | return self->error = MSPACK_ERR_DATAFORMAT; | ||
1060 | } | ||
1061 | |||
1062 | /* free ControlData */ | ||
1063 | sys->free(data); | ||
1064 | |||
1065 | /* find window_bits from window_size */ | ||
1066 | switch (window_size) { | ||
1067 | case 0x008000: window_bits = 15; break; | ||
1068 | case 0x010000: window_bits = 16; break; | ||
1069 | case 0x020000: window_bits = 17; break; | ||
1070 | case 0x040000: window_bits = 18; break; | ||
1071 | case 0x080000: window_bits = 19; break; | ||
1072 | case 0x100000: window_bits = 20; break; | ||
1073 | case 0x200000: window_bits = 21; break; | ||
1074 | default: | ||
1075 | D(("bad controldata window size")) | ||
1076 | return self->error = MSPACK_ERR_DATAFORMAT; | ||
1077 | } | ||
1078 | |||
1079 | /* validate reset_interval */ | ||
1080 | if (reset_interval % LZX_FRAME_SIZE) { | ||
1081 | D(("bad controldata reset interval")) | ||
1082 | return self->error = MSPACK_ERR_DATAFORMAT; | ||
1083 | } | ||
1084 | |||
1085 | /* which reset table entry would we like? */ | ||
1086 | entry = file->offset / reset_interval; | ||
1087 | /* convert from reset interval multiple (usually 64k) to 32k frames */ | ||
1088 | entry *= reset_interval / LZX_FRAME_SIZE; | ||
1089 | |||
1090 | /* read the reset table entry */ | ||
1091 | if (read_reset_table(self, sec, entry, &length, &offset)) { | ||
1092 | /* the uncompressed length given in the reset table is dishonest. | ||
1093 | * the uncompressed data is always padded out from the given | ||
1094 | * uncompressed length up to the next reset interval */ | ||
1095 | length += reset_interval - 1; | ||
1096 | length &= -reset_interval; | ||
1097 | } | ||
1098 | else { | ||
1099 | /* if we can't read the reset table entry, just start from | ||
1100 | * the beginning. Use spaninfo to get the uncompressed length */ | ||
1101 | entry = 0; | ||
1102 | offset = 0; | ||
1103 | err = read_spaninfo(self, sec, &length); | ||
1104 | } | ||
1105 | if (err) return self->error = err; | ||
1106 | |||
1107 | /* get offset of compressed data stream: | ||
1108 | * = offset of uncompressed section from start of file | ||
1109 | * + offset of compressed stream from start of uncompressed section | ||
1110 | * + offset of chosen reset interval from start of compressed stream */ | ||
1111 | self->d->inoffset = file->section->chm->sec0.offset + sec->content->offset + offset; | ||
1112 | |||
1113 | /* set start offset and overall remaining stream length */ | ||
1114 | self->d->offset = entry * LZX_FRAME_SIZE; | ||
1115 | length -= self->d->offset; | ||
1116 | |||
1117 | /* initialise LZX stream */ | ||
1118 | self->d->state = lzxd_init(&self->d->sys, self->d->infh, | ||
1119 | (struct mspack_file *) self, window_bits, | ||
1120 | reset_interval / LZX_FRAME_SIZE, | ||
1121 | 4096, length); | ||
1122 | if (!self->d->state) self->error = MSPACK_ERR_NOMEMORY; | ||
1123 | return self->error; | ||
1124 | } | ||
1125 | |||
1126 | /*************************************** | ||
1127 | * READ_RESET_TABLE | ||
1128 | *************************************** | ||
1129 | * Reads one entry out of the reset table. Also reads the uncompressed | ||
1130 | * data length. Writes these to offset_ptr and length_ptr respectively. | ||
1131 | * Returns non-zero for success, zero for failure. | ||
1132 | */ | ||
1133 | static int read_reset_table(struct mschm_decompressor_p *self, | ||
1134 | struct mschmd_sec_mscompressed *sec, | ||
1135 | int entry, off_t *length_ptr, off_t *offset_ptr) | ||
1136 | { | ||
1137 | struct mspack_system *sys = self->system; | ||
1138 | unsigned char *data; | ||
1139 | int pos, entrysize; | ||
1140 | |||
1141 | /* do we have a ResetTable file? */ | ||
1142 | int err = find_sys_file(self, sec, &sec->rtable, rtable_name); | ||
1143 | if (err) return 0; | ||
1144 | |||
1145 | /* read ResetTable file */ | ||
1146 | if (sec->rtable->length < lzxrt_headerSIZEOF) { | ||
1147 | D(("ResetTable file is too short")) | ||
1148 | return 0; | ||
1149 | } | ||
1150 | if (!(data = read_sys_file(self, sec->rtable))) { | ||
1151 | D(("can't read reset table")) | ||
1152 | return 0; | ||
1153 | } | ||
1154 | |||
1155 | /* check sanity of reset table */ | ||
1156 | if (EndGetI32(&data[lzxrt_FrameLen]) != LZX_FRAME_SIZE) { | ||
1157 | D(("bad reset table frame length")) | ||
1158 | sys->free(data); | ||
1159 | return 0; | ||
1160 | } | ||
1161 | |||
1162 | /* get the uncompressed length of the LZX stream */ | ||
1163 | if (read_off64(length_ptr, data, sys, self->d->infh)) { | ||
1164 | sys->free(data); | ||
1165 | return 0; | ||
1166 | } | ||
1167 | |||
1168 | entrysize = EndGetI32(&data[lzxrt_EntrySize]); | ||
1169 | pos = EndGetI32(&data[lzxrt_TableOffset]) + (entry * entrysize); | ||
1170 | |||
1171 | /* ensure reset table entry for this offset exists */ | ||
1172 | if (entry < EndGetI32(&data[lzxrt_NumEntries]) && | ||
1173 | ((pos + entrysize) <= sec->rtable->length)) | ||
1174 | { | ||
1175 | switch (entrysize) { | ||
1176 | case 4: | ||
1177 | *offset_ptr = EndGetI32(&data[pos]); | ||
1178 | err = 0; | ||
1179 | break; | ||
1180 | case 8: | ||
1181 | err = read_off64(offset_ptr, &data[pos], sys, self->d->infh); | ||
1182 | break; | ||
1183 | default: | ||
1184 | D(("reset table entry size neither 4 nor 8")) | ||
1185 | err = 1; | ||
1186 | break; | ||
1187 | } | ||
1188 | } | ||
1189 | else { | ||
1190 | D(("bad reset interval")) | ||
1191 | err = 1; | ||
1192 | } | ||
1193 | |||
1194 | /* free the reset table */ | ||
1195 | sys->free(data); | ||
1196 | |||
1197 | /* return success */ | ||
1198 | return (err == 0); | ||
1199 | } | ||
1200 | |||
1201 | /*************************************** | ||
1202 | * READ_SPANINFO | ||
1203 | *************************************** | ||
1204 | * Reads the uncompressed data length from the spaninfo file. | ||
1205 | * Returns zero for success or a non-zero error code for failure. | ||
1206 | */ | ||
1207 | static int read_spaninfo(struct mschm_decompressor_p *self, | ||
1208 | struct mschmd_sec_mscompressed *sec, | ||
1209 | off_t *length_ptr) | ||
1210 | { | ||
1211 | struct mspack_system *sys = self->system; | ||
1212 | unsigned char *data; | ||
1213 | |||
1214 | /* find SpanInfo file */ | ||
1215 | int err = find_sys_file(self, sec, &sec->spaninfo, spaninfo_name); | ||
1216 | if (err) return MSPACK_ERR_DATAFORMAT; | ||
1217 | |||
1218 | /* check it's large enough */ | ||
1219 | if (sec->spaninfo->length != 8) { | ||
1220 | D(("SpanInfo file is wrong size")) | ||
1221 | return MSPACK_ERR_DATAFORMAT; | ||
1222 | } | ||
1223 | |||
1224 | /* read the SpanInfo file */ | ||
1225 | if (!(data = read_sys_file(self, sec->spaninfo))) { | ||
1226 | D(("can't read SpanInfo file")) | ||
1227 | return self->error; | ||
1228 | } | ||
1229 | |||
1230 | /* get the uncompressed length of the LZX stream */ | ||
1231 | err = read_off64(length_ptr, data, sys, self->d->infh); | ||
1232 | |||
1233 | sys->free(data); | ||
1234 | return (err) ? MSPACK_ERR_DATAFORMAT : MSPACK_ERR_OK; | ||
1235 | } | ||
1236 | |||
1237 | /*************************************** | ||
1238 | * FIND_SYS_FILE | ||
1239 | *************************************** | ||
1240 | * Uses chmd_fast_find to locate a system file, and fills out that system | ||
1241 | * file's entry and links it into the list of system files. Returns zero | ||
1242 | * for success, non-zero for both failure and the file not existing. | ||
1243 | */ | ||
1244 | static int find_sys_file(struct mschm_decompressor_p *self, | ||
1245 | struct mschmd_sec_mscompressed *sec, | ||
1246 | struct mschmd_file **f_ptr, const char *name) | ||
1247 | { | ||
1248 | struct mspack_system *sys = self->system; | ||
1249 | struct mschmd_file result; | ||
1250 | |||
1251 | /* already loaded */ | ||
1252 | if (*f_ptr) return MSPACK_ERR_OK; | ||
1253 | |||
1254 | /* try using fast_find to find the file - return DATAFORMAT error if | ||
1255 | * it fails, or successfully doesn't find the file */ | ||
1256 | if (chmd_fast_find((struct mschm_decompressor *) self, sec->base.chm, | ||
1257 | name, &result, (int)sizeof(result)) || !result.section) | ||
1258 | { | ||
1259 | return MSPACK_ERR_DATAFORMAT; | ||
1260 | } | ||
1261 | |||
1262 | if (!(*f_ptr = (struct mschmd_file *) sys->alloc(sys, sizeof(result)))) { | ||
1263 | return MSPACK_ERR_NOMEMORY; | ||
1264 | } | ||
1265 | |||
1266 | /* copy result */ | ||
1267 | *(*f_ptr) = result; | ||
1268 | (*f_ptr)->filename = (char *) name; | ||
1269 | |||
1270 | /* link file into sysfiles list */ | ||
1271 | (*f_ptr)->next = sec->base.chm->sysfiles; | ||
1272 | sec->base.chm->sysfiles = *f_ptr; | ||
1273 | return MSPACK_ERR_OK; | ||
1274 | } | ||
1275 | |||
1276 | /*************************************** | ||
1277 | * READ_SYS_FILE | ||
1278 | *************************************** | ||
1279 | * Allocates memory for a section 0 (uncompressed) file and reads it into | ||
1280 | * memory. | ||
1281 | */ | ||
1282 | static unsigned char *read_sys_file(struct mschm_decompressor_p *self, | ||
1283 | struct mschmd_file *file) | ||
1284 | { | ||
1285 | struct mspack_system *sys = self->system; | ||
1286 | unsigned char *data = NULL; | ||
1287 | int len; | ||
1288 | |||
1289 | if (!file || !file->section || (file->section->id != 0)) { | ||
1290 | self->error = MSPACK_ERR_DATAFORMAT; | ||
1291 | return NULL; | ||
1292 | } | ||
1293 | |||
1294 | len = (int) file->length; | ||
1295 | |||
1296 | if (!(data = (unsigned char *) sys->alloc(sys, (size_t) len))) { | ||
1297 | self->error = MSPACK_ERR_NOMEMORY; | ||
1298 | return NULL; | ||
1299 | } | ||
1300 | if (sys->seek(self->d->infh, file->section->chm->sec0.offset | ||
1301 | + file->offset, MSPACK_SYS_SEEK_START)) | ||
1302 | { | ||
1303 | self->error = MSPACK_ERR_SEEK; | ||
1304 | sys->free(data); | ||
1305 | return NULL; | ||
1306 | } | ||
1307 | if (sys->read(self->d->infh, data, len) != len) { | ||
1308 | self->error = MSPACK_ERR_READ; | ||
1309 | sys->free(data); | ||
1310 | return NULL; | ||
1311 | } | ||
1312 | return data; | ||
1313 | } | ||
1314 | |||
1315 | /*************************************** | ||
1316 | * CHMD_ERROR | ||
1317 | *************************************** | ||
1318 | * returns the last error that occurred | ||
1319 | */ | ||
1320 | static int chmd_error(struct mschm_decompressor *base) { | ||
1321 | struct mschm_decompressor_p *self = (struct mschm_decompressor_p *) base; | ||
1322 | return (self) ? self->error : MSPACK_ERR_ARGS; | ||
1323 | } | ||
1324 | |||
1325 | /*************************************** | ||
1326 | * READ_OFF64 | ||
1327 | *************************************** | ||
1328 | * Reads a 64-bit signed integer from memory in Intel byte order. | ||
1329 | * If running on a system with a 64-bit off_t, this is simply done. | ||
1330 | * If running on a system with a 32-bit off_t, offsets up to 0x7FFFFFFF | ||
1331 | * are accepted, offsets beyond that cause an error message. | ||
1332 | */ | ||
1333 | static int read_off64(off_t *var, unsigned char *mem, | ||
1334 | struct mspack_system *sys, struct mspack_file *fh) | ||
1335 | { | ||
1336 | #ifdef LARGEFILE_SUPPORT | ||
1337 | *var = EndGetI64(mem); | ||
1338 | #else | ||
1339 | *var = EndGetI32(mem); | ||
1340 | if ((*var & 0x80000000) || EndGetI32(mem+4)) { | ||
1341 | sys->message(fh, (char *)largefile_msg); | ||
1342 | return 1; | ||
1343 | } | ||
1344 | #endif | ||
1345 | return 0; | ||
1346 | } | ||