Sync to Wine-20050524:
[reactos.git] / reactos / lib / cabinet / cabinet.h
1 /*
2 * cabinet.h
3 *
4 * Copyright 2002 Greg Turner
5 * Copyright 2005 Gerold Jens Wucherpfennig
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21 #ifndef __WINE_CABINET_H
22 #define __WINE_CABINET_H
23
24 #include <stdarg.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winnt.h"
29 #include "fdi.h"
30 #include "fci.h"
31
32 /* from msvcrt/sys/stat.h */
33 #define _S_IWRITE 0x0080
34 #define _S_IREAD 0x0100
35
36 #define CAB_SPLITMAX (10)
37
38 #define CAB_SEARCH_SIZE (32*1024)
39
40 typedef unsigned char cab_UBYTE; /* 8 bits */
41 typedef UINT16 cab_UWORD; /* 16 bits */
42 typedef UINT32 cab_ULONG; /* 32 bits */
43 typedef INT32 cab_LONG; /* 32 bits */
44
45 typedef UINT32 cab_off_t;
46
47 /* number of bits in a ULONG */
48 #ifndef CHAR_BIT
49 # define CHAR_BIT (8)
50 #endif
51 #define CAB_ULONG_BITS (sizeof(cab_ULONG) * CHAR_BIT)
52
53 /* structure offsets */
54 #define cfhead_Signature (0x00)
55 #define cfhead_CabinetSize (0x08)
56 #define cfhead_FileOffset (0x10)
57 #define cfhead_MinorVersion (0x18)
58 #define cfhead_MajorVersion (0x19)
59 #define cfhead_NumFolders (0x1A)
60 #define cfhead_NumFiles (0x1C)
61 #define cfhead_Flags (0x1E)
62 #define cfhead_SetID (0x20)
63 #define cfhead_CabinetIndex (0x22)
64 #define cfhead_SIZEOF (0x24)
65 #define cfheadext_HeaderReserved (0x00)
66 #define cfheadext_FolderReserved (0x02)
67 #define cfheadext_DataReserved (0x03)
68 #define cfheadext_SIZEOF (0x04)
69 #define cffold_DataOffset (0x00)
70 #define cffold_NumBlocks (0x04)
71 #define cffold_CompType (0x06)
72 #define cffold_SIZEOF (0x08)
73 #define cffile_UncompressedSize (0x00)
74 #define cffile_FolderOffset (0x04)
75 #define cffile_FolderIndex (0x08)
76 #define cffile_Date (0x0A)
77 #define cffile_Time (0x0C)
78 #define cffile_Attribs (0x0E)
79 #define cffile_SIZEOF (0x10)
80 #define cfdata_CheckSum (0x00)
81 #define cfdata_CompressedSize (0x04)
82 #define cfdata_UncompressedSize (0x06)
83 #define cfdata_SIZEOF (0x08)
84
85 /* flags */
86 #define cffoldCOMPTYPE_MASK (0x000f)
87 #define cffoldCOMPTYPE_NONE (0x0000)
88 #define cffoldCOMPTYPE_MSZIP (0x0001)
89 #define cffoldCOMPTYPE_QUANTUM (0x0002)
90 #define cffoldCOMPTYPE_LZX (0x0003)
91 #define cfheadPREV_CABINET (0x0001)
92 #define cfheadNEXT_CABINET (0x0002)
93 #define cfheadRESERVE_PRESENT (0x0004)
94 #define cffileCONTINUED_FROM_PREV (0xFFFD)
95 #define cffileCONTINUED_TO_NEXT (0xFFFE)
96 #define cffileCONTINUED_PREV_AND_NEXT (0xFFFF)
97 #define cffile_A_RDONLY (0x01)
98 #define cffile_A_HIDDEN (0x02)
99 #define cffile_A_SYSTEM (0x04)
100 #define cffile_A_ARCH (0x20)
101 #define cffile_A_EXEC (0x40)
102 #define cffile_A_NAME_IS_UTF (0x80)
103
104 /****************************************************************************/
105 /* our archiver information / state */
106
107 /* MSZIP stuff */
108 #define ZIPWSIZE 0x8000 /* window size */
109 #define ZIPLBITS 9 /* bits in base literal/length lookup table */
110 #define ZIPDBITS 6 /* bits in base distance lookup table */
111 #define ZIPBMAX 16 /* maximum bit length of any code */
112 #define ZIPN_MAX 288 /* maximum number of codes in any set */
113
114 struct Ziphuft {
115 cab_UBYTE e; /* number of extra bits or operation */
116 cab_UBYTE b; /* number of bits in this code or subcode */
117 union {
118 cab_UWORD n; /* literal, length base, or distance base */
119 struct Ziphuft *t; /* pointer to next level of table */
120 } v;
121 };
122
123 struct ZIPstate {
124 cab_ULONG window_posn; /* current offset within the window */
125 cab_ULONG bb; /* bit buffer */
126 cab_ULONG bk; /* bits in bit buffer */
127 cab_ULONG ll[288+32]; /* literal/length and distance code lengths */
128 cab_ULONG c[ZIPBMAX+1]; /* bit length count table */
129 cab_LONG lx[ZIPBMAX+1]; /* memory for l[-1..ZIPBMAX-1] */
130 struct Ziphuft *u[ZIPBMAX]; /* table stack */
131 cab_ULONG v[ZIPN_MAX]; /* values in order of bit length */
132 cab_ULONG x[ZIPBMAX+1]; /* bit offsets, then code stack */
133 cab_UBYTE *inpos;
134 };
135
136 /* Quantum stuff */
137
138 struct QTMmodelsym {
139 cab_UWORD sym, cumfreq;
140 };
141
142 struct QTMmodel {
143 int shiftsleft, entries;
144 struct QTMmodelsym *syms;
145 cab_UWORD tabloc[256];
146 };
147
148 struct QTMstate {
149 cab_UBYTE *window; /* the actual decoding window */
150 cab_ULONG window_size; /* window size (1Kb through 2Mb) */
151 cab_ULONG actual_size; /* window size when it was first allocated */
152 cab_ULONG window_posn; /* current offset within the window */
153
154 struct QTMmodel model7;
155 struct QTMmodelsym m7sym[7+1];
156
157 struct QTMmodel model4, model5, model6pos, model6len;
158 struct QTMmodelsym m4sym[0x18 + 1];
159 struct QTMmodelsym m5sym[0x24 + 1];
160 struct QTMmodelsym m6psym[0x2a + 1], m6lsym[0x1b + 1];
161
162 struct QTMmodel model00, model40, model80, modelC0;
163 struct QTMmodelsym m00sym[0x40 + 1], m40sym[0x40 + 1];
164 struct QTMmodelsym m80sym[0x40 + 1], mC0sym[0x40 + 1];
165 };
166
167 /* LZX stuff */
168
169 /* some constants defined by the LZX specification */
170 #define LZX_MIN_MATCH (2)
171 #define LZX_MAX_MATCH (257)
172 #define LZX_NUM_CHARS (256)
173 #define LZX_BLOCKTYPE_INVALID (0) /* also blocktypes 4-7 invalid */
174 #define LZX_BLOCKTYPE_VERBATIM (1)
175 #define LZX_BLOCKTYPE_ALIGNED (2)
176 #define LZX_BLOCKTYPE_UNCOMPRESSED (3)
177 #define LZX_PRETREE_NUM_ELEMENTS (20)
178 #define LZX_ALIGNED_NUM_ELEMENTS (8) /* aligned offset tree #elements */
179 #define LZX_NUM_PRIMARY_LENGTHS (7) /* this one missing from spec! */
180 #define LZX_NUM_SECONDARY_LENGTHS (249) /* length tree #elements */
181
182 /* LZX huffman defines: tweak tablebits as desired */
183 #define LZX_PRETREE_MAXSYMBOLS (LZX_PRETREE_NUM_ELEMENTS)
184 #define LZX_PRETREE_TABLEBITS (6)
185 #define LZX_MAINTREE_MAXSYMBOLS (LZX_NUM_CHARS + 50*8)
186 #define LZX_MAINTREE_TABLEBITS (12)
187 #define LZX_LENGTH_MAXSYMBOLS (LZX_NUM_SECONDARY_LENGTHS+1)
188 #define LZX_LENGTH_TABLEBITS (12)
189 #define LZX_ALIGNED_MAXSYMBOLS (LZX_ALIGNED_NUM_ELEMENTS)
190 #define LZX_ALIGNED_TABLEBITS (7)
191
192 #define LZX_LENTABLE_SAFETY (64) /* we allow length table decoding overruns */
193
194 #define LZX_DECLARE_TABLE(tbl) \
195 cab_UWORD tbl##_table[(1<<LZX_##tbl##_TABLEBITS) + (LZX_##tbl##_MAXSYMBOLS<<1)];\
196 cab_UBYTE tbl##_len [LZX_##tbl##_MAXSYMBOLS + LZX_LENTABLE_SAFETY]
197
198 struct LZXstate {
199 cab_UBYTE *window; /* the actual decoding window */
200 cab_ULONG window_size; /* window size (32Kb through 2Mb) */
201 cab_ULONG actual_size; /* window size when it was first allocated */
202 cab_ULONG window_posn; /* current offset within the window */
203 cab_ULONG R0, R1, R2; /* for the LRU offset system */
204 cab_UWORD main_elements; /* number of main tree elements */
205 int header_read; /* have we started decoding at all yet? */
206 cab_UWORD block_type; /* type of this block */
207 cab_ULONG block_length; /* uncompressed length of this block */
208 cab_ULONG block_remaining; /* uncompressed bytes still left to decode */
209 cab_ULONG frames_read; /* the number of CFDATA blocks processed */
210 cab_LONG intel_filesize; /* magic header value used for transform */
211 cab_LONG intel_curpos; /* current offset in transform space */
212 int intel_started; /* have we seen any translatable data yet? */
213
214 LZX_DECLARE_TABLE(PRETREE);
215 LZX_DECLARE_TABLE(MAINTREE);
216 LZX_DECLARE_TABLE(LENGTH);
217 LZX_DECLARE_TABLE(ALIGNED);
218 };
219
220 struct lzx_bits {
221 cab_ULONG bb;
222 int bl;
223 cab_UBYTE *ip;
224 };
225
226 /* CAB data blocks are <= 32768 bytes in uncompressed form. Uncompressed
227 * blocks have zero growth. MSZIP guarantees that it won't grow above
228 * uncompressed size by more than 12 bytes. LZX guarantees it won't grow
229 * more than 6144 bytes.
230 */
231 #define CAB_BLOCKMAX (32768)
232 #define CAB_INPUTMAX (CAB_BLOCKMAX+6144)
233
234 struct cab_file {
235 struct cab_file *next; /* next file in sequence */
236 struct cab_folder *folder; /* folder that contains this file */
237 LPCSTR filename; /* output name of file */
238 HANDLE fh; /* open file handle or NULL */
239 cab_ULONG length; /* uncompressed length of file */
240 cab_ULONG offset; /* uncompressed offset in folder */
241 cab_UWORD index; /* magic index number of folder */
242 cab_UWORD time, date, attribs; /* MS-DOS time/date/attributes */
243 };
244
245
246 struct cab_folder {
247 struct cab_folder *next;
248 struct cabinet *cab[CAB_SPLITMAX]; /* cabinet(s) this folder spans */
249 cab_off_t offset[CAB_SPLITMAX]; /* offset to data blocks */
250 cab_UWORD comp_type; /* compression format/window size */
251 cab_ULONG comp_size; /* compressed size of folder */
252 cab_UBYTE num_splits; /* number of split blocks + 1 */
253 cab_UWORD num_blocks; /* total number of blocks */
254 struct cab_file *contfile; /* the first split file */
255 };
256
257 struct cabinet {
258 struct cabinet *next; /* for making a list of cabinets */
259 LPCSTR filename; /* input name of cabinet */
260 HANDLE *fh; /* open file handle or NULL */
261 cab_off_t filelen; /* length of cabinet file */
262 cab_off_t blocks_off; /* offset to data blocks in file */
263 struct cabinet *prevcab, *nextcab; /* multipart cabinet chains */
264 char *prevname, *nextname; /* and their filenames */
265 char *previnfo, *nextinfo; /* and their visible names */
266 struct cab_folder *folders; /* first folder in this cabinet */
267 struct cab_file *files; /* first file in this cabinet */
268 cab_UBYTE block_resv; /* reserved space in datablocks */
269 cab_UBYTE flags; /* header flags */
270 };
271
272 typedef struct cds_forward {
273 struct cab_folder *current; /* current folder we're extracting from */
274 cab_ULONG offset; /* uncompressed offset within folder */
275 cab_UBYTE *outpos; /* (high level) start of data to use up */
276 cab_UWORD outlen; /* (high level) amount of data to use up */
277 cab_UWORD split; /* at which split in current folder? */
278 int (*decompress)(int, int, struct cds_forward *); /* chosen compress fn */
279 cab_UBYTE inbuf[CAB_INPUTMAX+2]; /* +2 for lzx bitbuffer overflows! */
280 cab_UBYTE outbuf[CAB_BLOCKMAX];
281 cab_UBYTE q_length_base[27], q_length_extra[27], q_extra_bits[42];
282 cab_ULONG q_position_base[42];
283 cab_ULONG lzx_position_base[51];
284 cab_UBYTE extra_bits[51];
285 union {
286 struct ZIPstate zip;
287 struct QTMstate qtm;
288 struct LZXstate lzx;
289 } methods;
290 } cab_decomp_state;
291
292 /* _Int as in "Internal" fyi */
293
294 typedef struct {
295 unsigned int FCI_Intmagic;
296 PERF perf;
297 PFNFCIFILEPLACED pfnfiledest;
298 PFNFCIALLOC pfnalloc;
299 PFNFCIFREE pfnfree;
300 PFNFCIOPEN pfnopen;
301 PFNFCIREAD pfnread;
302 PFNFCIWRITE pfnwrite;
303 PFNFCICLOSE pfnclose;
304 PFNFCISEEK pfnseek;
305 PFNFCIDELETE pfndelete;
306 PFNFCIGETTEMPFILE pfnfcigtf;
307 PCCAB pccab;
308 void *pv;
309 } FCI_Int, *PFCI_Int;
310
311 typedef struct {
312 unsigned int FDI_Intmagic;
313 PFNALLOC pfnalloc;
314 PFNFREE pfnfree;
315 PFNOPEN pfnopen;
316 PFNREAD pfnread;
317 PFNWRITE pfnwrite;
318 PFNCLOSE pfnclose;
319 PFNSEEK pfnseek;
320 PERF perf;
321 } FDI_Int, *PFDI_Int;
322
323 /* cast an HFCI into a PFCI_Int */
324 #define PFCI_INT(hfci) ((PFCI_Int)(hfci))
325
326 /* cast an HFDI into a PFDI_Int */
327 #define PFDI_INT(hfdi) ((PFDI_Int)(hfdi))
328
329 /* quickie pfdi method invokers */
330 #define PFDI_ALLOC(hfdi, size) ((*PFDI_INT(hfdi)->pfnalloc) (size))
331 #define PFDI_FREE(hfdi, ptr) ((*PFDI_INT(hfdi)->pfnfree) (ptr))
332 #define PFDI_OPEN(hfdi, file, flag, mode) ((*PFDI_INT(hfdi)->pfnopen) (file, flag, mode))
333 #define PFDI_READ(hfdi, hf, pv, cb) ((*PFDI_INT(hfdi)->pfnread) (hf, pv, cb))
334 #define PFDI_WRITE(hfdi, hf, pv, cb) ((*PFDI_INT(hfdi)->pfnwrite) (hf, pv, cb))
335 #define PFDI_CLOSE(hfdi, hf) ((*PFDI_INT(hfdi)->pfnclose) (hf))
336 #define PFDI_SEEK(hfdi, hf, dist, type) ((*PFDI_INT(hfdi)->pfnseek) (hf, dist, type))
337
338 #define FCI_INT_MAGIC 0xfcfcfc05
339 #define FDI_INT_MAGIC 0xfdfdfd05
340
341 #define REALLY_IS_FCI(hfci) ( \
342 (((void *) hfci) != NULL) && \
343 (PFCI_INT(hfci)->FCI_Intmagic == FCI_INT_MAGIC) )
344
345 #define REALLY_IS_FDI(hfdi) ( \
346 (((void *) hfdi) != NULL) && \
347 (PFDI_INT(hfdi)->FDI_Intmagic == FDI_INT_MAGIC) )
348
349 /*
350 * the rest of these are somewhat kludgy macros which are shared between fdi.c
351 * and cabextract.c.
352 */
353
354 #define ZIPNEEDBITS(n) {while(k<(n)){cab_LONG c=*(ZIP(inpos)++);\
355 b|=((cab_ULONG)c)<<k;k+=8;}}
356 #define ZIPDUMPBITS(n) {b>>=(n);k-=(n);}
357
358 /* endian-neutral reading of little-endian data */
359 #define EndGetI32(a) ((((a)[3])<<24)|(((a)[2])<<16)|(((a)[1])<<8)|((a)[0]))
360 #define EndGetI16(a) ((((a)[1])<<8)|((a)[0]))
361
362 #define CAB(x) (decomp_state->x)
363 #define ZIP(x) (decomp_state->methods.zip.x)
364 #define QTM(x) (decomp_state->methods.qtm.x)
365 #define LZX(x) (decomp_state->methods.lzx.x)
366 #define DECR_OK (0)
367 #define DECR_DATAFORMAT (1)
368 #define DECR_ILLEGALDATA (2)
369 #define DECR_NOMEMORY (3)
370 #define DECR_CHECKSUM (4)
371 #define DECR_INPUT (5)
372 #define DECR_OUTPUT (6)
373 #define DECR_USERABORT (7)
374
375 /* Bitstream reading macros (Quantum / normal byte order)
376 *
377 * Q_INIT_BITSTREAM should be used first to set up the system
378 * Q_READ_BITS(var,n) takes N bits from the buffer and puts them in var.
379 * unlike LZX, this can loop several times to get the
380 * requisite number of bits.
381 * Q_FILL_BUFFER adds more data to the bit buffer, if there is room
382 * for another 16 bits.
383 * Q_PEEK_BITS(n) extracts (without removing) N bits from the bit
384 * buffer
385 * Q_REMOVE_BITS(n) removes N bits from the bit buffer
386 *
387 * These bit access routines work by using the area beyond the MSB and the
388 * LSB as a free source of zeroes. This avoids having to mask any bits.
389 * So we have to know the bit width of the bitbuffer variable. This is
390 * defined as ULONG_BITS.
391 *
392 * ULONG_BITS should be at least 16 bits. Unlike LZX's Huffman decoding,
393 * Quantum's arithmetic decoding only needs 1 bit at a time, it doesn't
394 * need an assured number. Retrieving larger bitstrings can be done with
395 * multiple reads and fills of the bitbuffer. The code should work fine
396 * for machines where ULONG >= 32 bits.
397 *
398 * Also note that Quantum reads bytes in normal order; LZX is in
399 * little-endian order.
400 */
401
402 #define Q_INIT_BITSTREAM do { bitsleft = 0; bitbuf = 0; } while (0)
403
404 #define Q_FILL_BUFFER do { \
405 if (bitsleft <= (CAB_ULONG_BITS - 16)) { \
406 bitbuf |= ((inpos[0]<<8)|inpos[1]) << (CAB_ULONG_BITS-16 - bitsleft); \
407 bitsleft += 16; inpos += 2; \
408 } \
409 } while (0)
410
411 #define Q_PEEK_BITS(n) (bitbuf >> (CAB_ULONG_BITS - (n)))
412 #define Q_REMOVE_BITS(n) ((bitbuf <<= (n)), (bitsleft -= (n)))
413
414 #define Q_READ_BITS(v,n) do { \
415 (v) = 0; \
416 for (bitsneed = (n); bitsneed; bitsneed -= bitrun) { \
417 Q_FILL_BUFFER; \
418 bitrun = (bitsneed > bitsleft) ? bitsleft : bitsneed; \
419 (v) = ((v) << bitrun) | Q_PEEK_BITS(bitrun); \
420 Q_REMOVE_BITS(bitrun); \
421 } \
422 } while (0)
423
424 #define Q_MENTRIES(model) (QTM(model).entries)
425 #define Q_MSYM(model,symidx) (QTM(model).syms[(symidx)].sym)
426 #define Q_MSYMFREQ(model,symidx) (QTM(model).syms[(symidx)].cumfreq)
427
428 /* GET_SYMBOL(model, var) fetches the next symbol from the stated model
429 * and puts it in var. it may need to read the bitstream to do this.
430 */
431 #define GET_SYMBOL(m, var) do { \
432 range = ((H - L) & 0xFFFF) + 1; \
433 symf = ((((C - L + 1) * Q_MSYMFREQ(m,0)) - 1) / range) & 0xFFFF; \
434 \
435 for (i=1; i < Q_MENTRIES(m); i++) { \
436 if (Q_MSYMFREQ(m,i) <= symf) break; \
437 } \
438 (var) = Q_MSYM(m,i-1); \
439 \
440 range = (H - L) + 1; \
441 H = L + ((Q_MSYMFREQ(m,i-1) * range) / Q_MSYMFREQ(m,0)) - 1; \
442 L = L + ((Q_MSYMFREQ(m,i) * range) / Q_MSYMFREQ(m,0)); \
443 while (1) { \
444 if ((L & 0x8000) != (H & 0x8000)) { \
445 if ((L & 0x4000) && !(H & 0x4000)) { \
446 /* underflow case */ \
447 C ^= 0x4000; L &= 0x3FFF; H |= 0x4000; \
448 } \
449 else break; \
450 } \
451 L <<= 1; H = (H << 1) | 1; \
452 Q_FILL_BUFFER; \
453 C = (C << 1) | Q_PEEK_BITS(1); \
454 Q_REMOVE_BITS(1); \
455 } \
456 \
457 QTMupdatemodel(&(QTM(m)), i); \
458 } while (0)
459
460 /* Bitstream reading macros (LZX / intel little-endian byte order)
461 *
462 * INIT_BITSTREAM should be used first to set up the system
463 * READ_BITS(var,n) takes N bits from the buffer and puts them in var
464 *
465 * ENSURE_BITS(n) ensures there are at least N bits in the bit buffer.
466 * it can guarantee up to 17 bits (i.e. it can read in
467 * 16 new bits when there is down to 1 bit in the buffer,
468 * and it can read 32 bits when there are 0 bits in the
469 * buffer).
470 * PEEK_BITS(n) extracts (without removing) N bits from the bit buffer
471 * REMOVE_BITS(n) removes N bits from the bit buffer
472 *
473 * These bit access routines work by using the area beyond the MSB and the
474 * LSB as a free source of zeroes. This avoids having to mask any bits.
475 * So we have to know the bit width of the bitbuffer variable.
476 */
477
478 #define INIT_BITSTREAM do { bitsleft = 0; bitbuf = 0; } while (0)
479
480 /* Quantum reads bytes in normal order; LZX is little-endian order */
481 #define ENSURE_BITS(n) \
482 while (bitsleft < (n)) { \
483 bitbuf |= ((inpos[1]<<8)|inpos[0]) << (CAB_ULONG_BITS-16 - bitsleft); \
484 bitsleft += 16; inpos+=2; \
485 }
486
487 #define PEEK_BITS(n) (bitbuf >> (CAB_ULONG_BITS - (n)))
488 #define REMOVE_BITS(n) ((bitbuf <<= (n)), (bitsleft -= (n)))
489
490 #define READ_BITS(v,n) do { \
491 if (n) { \
492 ENSURE_BITS(n); \
493 (v) = PEEK_BITS(n); \
494 REMOVE_BITS(n); \
495 } \
496 else { \
497 (v) = 0; \
498 } \
499 } while (0)
500
501 /* Huffman macros */
502
503 #define TABLEBITS(tbl) (LZX_##tbl##_TABLEBITS)
504 #define MAXSYMBOLS(tbl) (LZX_##tbl##_MAXSYMBOLS)
505 #define SYMTABLE(tbl) (LZX(tbl##_table))
506 #define LENTABLE(tbl) (LZX(tbl##_len))
507
508 /* BUILD_TABLE(tablename) builds a huffman lookup table from code lengths.
509 * In reality, it just calls make_decode_table() with the appropriate
510 * values - they're all fixed by some #defines anyway, so there's no point
511 * writing each call out in full by hand.
512 */
513 #define BUILD_TABLE(tbl) \
514 if (make_decode_table( \
515 MAXSYMBOLS(tbl), TABLEBITS(tbl), LENTABLE(tbl), SYMTABLE(tbl) \
516 )) { return DECR_ILLEGALDATA; }
517
518 /* READ_HUFFSYM(tablename, var) decodes one huffman symbol from the
519 * bitstream using the stated table and puts it in var.
520 */
521 #define READ_HUFFSYM(tbl,var) do { \
522 ENSURE_BITS(16); \
523 hufftbl = SYMTABLE(tbl); \
524 if ((i = hufftbl[PEEK_BITS(TABLEBITS(tbl))]) >= MAXSYMBOLS(tbl)) { \
525 j = 1 << (CAB_ULONG_BITS - TABLEBITS(tbl)); \
526 do { \
527 j >>= 1; i <<= 1; i |= (bitbuf & j) ? 1 : 0; \
528 if (!j) { return DECR_ILLEGALDATA; } \
529 } while ((i = hufftbl[i]) >= MAXSYMBOLS(tbl)); \
530 } \
531 j = LENTABLE(tbl)[(var) = i]; \
532 REMOVE_BITS(j); \
533 } while (0)
534
535 /* READ_LENGTHS(tablename, first, last) reads in code lengths for symbols
536 * first to last in the given table. The code lengths are stored in their
537 * own special LZX way.
538 */
539 #define READ_LENGTHS(tbl,first,last,fn) do { \
540 lb.bb = bitbuf; lb.bl = bitsleft; lb.ip = inpos; \
541 if (fn(LENTABLE(tbl),(first),(last),&lb,decomp_state)) { \
542 return DECR_ILLEGALDATA; \
543 } \
544 bitbuf = lb.bb; bitsleft = lb.bl; inpos = lb.ip; \
545 } while (0)
546
547 /* Tables for deflate from PKZIP's appnote.txt. */
548
549 #define THOSE_ZIP_CONSTS \
550 static const cab_UBYTE Zipborder[] = /* Order of the bit length code lengths */ \
551 { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; \
552 static const cab_UWORD Zipcplens[] = /* Copy lengths for literal codes 257..285 */ \
553 { 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, \
554 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; \
555 static const cab_UWORD Zipcplext[] = /* Extra bits for literal codes 257..285 */ \
556 { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, \
557 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */ \
558 static const cab_UWORD Zipcpdist[] = /* Copy offsets for distance codes 0..29 */ \
559 { 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, \
560 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577}; \
561 static const cab_UWORD Zipcpdext[] = /* Extra bits for distance codes */ \
562 { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, \
563 10, 11, 11, 12, 12, 13, 13}; \
564 /* And'ing with Zipmask[n] masks the lower n bits */ \
565 static const cab_UWORD Zipmask[17] = { \
566 0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff, \
567 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff \
568 }
569
570 struct ExtractFileList {
571 LPSTR filename;
572 struct ExtractFileList *next;
573 BOOL unknown; /* always 1L */
574 } ;
575
576 /* the first parameter of the function extract */
577 typedef struct {
578 long result1; /* 0x000 */
579 long unknown1[3]; /* 0x004 */
580 struct ExtractFileList *filelist; /* 0x010 */
581 long filecount; /* 0x014 */
582 long unknown2; /* 0x018 */
583 char directory[0x104]; /* 0x01c */
584 char lastfile[0x20c]; /* 0x120 */
585 } EXTRACTdest;
586
587
588 /* from cabextract.c */
589 BOOL process_cabinet(LPCSTR cabname, LPCSTR dir, BOOL fix, BOOL lower, EXTRACTdest *dest);
590 void QTMupdatemodel(struct QTMmodel *model, int sym);
591 int make_decode_table(cab_ULONG nsyms, cab_ULONG nbits, cab_UBYTE *length, cab_UWORD *table);
592 cab_ULONG checksum(cab_UBYTE *data, cab_UWORD bytes, cab_ULONG csum);
593
594 #endif /* __WINE_CABINET_H */