Delete all Trailing spaces in code.
[reactos.git] / reactos / dll / win32 / 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 BOOL fPrevCab;
309 BOOL fNextCab;
310 BOOL fSplitFolder;
311 cab_ULONG statusFolderCopied;
312 cab_ULONG statusFolderTotal;
313 BOOL fGetNextCabInVain;
314 void *pv;
315 char szPrevCab[CB_MAX_CABINET_NAME]; /* previous cabinet name */
316 char szPrevDisk[CB_MAX_DISK_NAME]; /* disk name of previous cabinet */
317 CCAB oldCCAB;
318 char* data_in; /* uncompressed data blocks */
319 cab_UWORD cdata_in;
320 char* data_out; /* compressed data blocks */
321 ULONG cCompressedBytesInFolder;
322 cab_UWORD cFolders;
323 cab_UWORD cFiles;
324 cab_ULONG cDataBlocks;
325 cab_ULONG cbFileRemainer; /* uncompressed, yet to be written data */
326 /* of spanned file of a spanning folder of a spanning cabinet */
327 char szFileNameCFDATA1[CB_MAX_FILENAME];
328 int handleCFDATA1;
329 char szFileNameCFFILE1[CB_MAX_FILENAME];
330 int handleCFFILE1;
331 char szFileNameCFDATA2[CB_MAX_FILENAME];
332 int handleCFDATA2;
333 char szFileNameCFFILE2[CB_MAX_FILENAME];
334 int handleCFFILE2;
335 char szFileNameCFFOLDER[CB_MAX_FILENAME];
336 int handleCFFOLDER;
337 cab_ULONG sizeFileCFDATA1;
338 cab_ULONG sizeFileCFFILE1;
339 cab_ULONG sizeFileCFDATA2;
340 cab_ULONG sizeFileCFFILE2;
341 cab_ULONG sizeFileCFFOLDER;
342 BOOL fNewPrevious;
343 cab_ULONG estimatedCabinetSize;
344 } FCI_Int, *PFCI_Int;
345
346 typedef struct {
347 unsigned int FDI_Intmagic;
348 PFNALLOC pfnalloc;
349 PFNFREE pfnfree;
350 PFNOPEN pfnopen;
351 PFNREAD pfnread;
352 PFNWRITE pfnwrite;
353 PFNCLOSE pfnclose;
354 PFNSEEK pfnseek;
355 PERF perf;
356 } FDI_Int, *PFDI_Int;
357
358 /* cast an HFCI into a PFCI_Int */
359 #define PFCI_INT(hfci) ((PFCI_Int)(hfci))
360
361 /* cast an HFDI into a PFDI_Int */
362 #define PFDI_INT(hfdi) ((PFDI_Int)(hfdi))
363
364 /* quick pfci method invokers */
365 #define PFCI_ALLOC(hfdi, size) ((*PFCI_INT(hfdi)->pfnalloc) (size))
366 #define PFCI_FREE(hfdi, ptr) ((*PFCI_INT(hfdi)->pfnfree) (ptr))
367 #define PFCI_GETTEMPFILE(hfci,name,length) ((*PFCI_INT(hfci)->pfnfcigtf)(name,length,PFCI_INT(hfci)->pv))
368 #define PFCI_DELETE(hfci,name,err,pv) ((*PFCI_INT(hfci)->pfndelete)(name,err,pv))
369 #define PFCI_OPEN(hfci,name,oflag,pmode,err,pv) ((*PFCI_INT(hfci)->pfnopen)(name,oflag,pmode,err,pv))
370 #define PFCI_READ(hfci,hf,memory,cb,err,pv)((*PFCI_INT(hfci)->pfnread)(hf,memory,cb,err,pv))
371 #define PFCI_WRITE(hfci,hf,memory,cb,err,pv) ((*PFCI_INT(hfci)->pfnwrite)(hf,memory,cb,err,pv))
372 #define PFCI_CLOSE(hfci,hf,err,pv) ((*PFCI_INT(hfci)->pfnclose)(hf,err,pv))
373 #define PFCI_SEEK(hfci,hf,dist,seektype,err,pv)((*PFCI_INT(hfci)->pfnseek)(hf,dist,seektype,err,pv))
374 #define PFCI_FILEPLACED(hfci,pccab,name,cb,cont,pv)((*PFCI_INT(hfci)->pfnfiledest)(pccab,name,cb,cont,pv))
375
376 /* quickie pfdi method invokers */
377 #define PFDI_ALLOC(hfdi, size) ((*PFDI_INT(hfdi)->pfnalloc) (size))
378 #define PFDI_FREE(hfdi, ptr) ((*PFDI_INT(hfdi)->pfnfree) (ptr))
379 #define PFDI_OPEN(hfdi, file, flag, mode) ((*PFDI_INT(hfdi)->pfnopen) (file, flag, mode))
380 #define PFDI_READ(hfdi, hf, pv, cb) ((*PFDI_INT(hfdi)->pfnread) (hf, pv, cb))
381 #define PFDI_WRITE(hfdi, hf, pv, cb) ((*PFDI_INT(hfdi)->pfnwrite) (hf, pv, cb))
382 #define PFDI_CLOSE(hfdi, hf) ((*PFDI_INT(hfdi)->pfnclose) (hf))
383 #define PFDI_SEEK(hfdi, hf, dist, type) ((*PFDI_INT(hfdi)->pfnseek) (hf, dist, type))
384
385 #define FCI_INT_MAGIC 0xfcfcfc05
386 #define FDI_INT_MAGIC 0xfdfdfd05
387
388 #define REALLY_IS_FCI(hfci) ( \
389 (((void *) hfci) != NULL) && \
390 (PFCI_INT(hfci)->FCI_Intmagic == FCI_INT_MAGIC) )
391
392 #define REALLY_IS_FDI(hfdi) ( \
393 (((void *) hfdi) != NULL) && \
394 (PFDI_INT(hfdi)->FDI_Intmagic == FDI_INT_MAGIC) )
395
396 /*
397 * the rest of these are somewhat kludgy macros which are shared between fdi.c
398 * and cabextract.c.
399 */
400
401 #define ZIPNEEDBITS(n) {while(k<(n)){cab_LONG c=*(ZIP(inpos)++);\
402 b|=((cab_ULONG)c)<<k;k+=8;}}
403 #define ZIPDUMPBITS(n) {b>>=(n);k-=(n);}
404
405 /* endian-neutral reading of little-endian data */
406 #define EndGetI32(a) ((((a)[3])<<24)|(((a)[2])<<16)|(((a)[1])<<8)|((a)[0]))
407 #define EndGetI16(a) ((((a)[1])<<8)|((a)[0]))
408
409 #define CAB(x) (decomp_state->x)
410 #define ZIP(x) (decomp_state->methods.zip.x)
411 #define QTM(x) (decomp_state->methods.qtm.x)
412 #define LZX(x) (decomp_state->methods.lzx.x)
413 #define DECR_OK (0)
414 #define DECR_DATAFORMAT (1)
415 #define DECR_ILLEGALDATA (2)
416 #define DECR_NOMEMORY (3)
417 #define DECR_CHECKSUM (4)
418 #define DECR_INPUT (5)
419 #define DECR_OUTPUT (6)
420 #define DECR_USERABORT (7)
421
422 /* Bitstream reading macros (Quantum / normal byte order)
423 *
424 * Q_INIT_BITSTREAM should be used first to set up the system
425 * Q_READ_BITS(var,n) takes N bits from the buffer and puts them in var.
426 * unlike LZX, this can loop several times to get the
427 * requisite number of bits.
428 * Q_FILL_BUFFER adds more data to the bit buffer, if there is room
429 * for another 16 bits.
430 * Q_PEEK_BITS(n) extracts (without removing) N bits from the bit
431 * buffer
432 * Q_REMOVE_BITS(n) removes N bits from the bit buffer
433 *
434 * These bit access routines work by using the area beyond the MSB and the
435 * LSB as a free source of zeroes. This avoids having to mask any bits.
436 * So we have to know the bit width of the bitbuffer variable. This is
437 * defined as ULONG_BITS.
438 *
439 * ULONG_BITS should be at least 16 bits. Unlike LZX's Huffman decoding,
440 * Quantum's arithmetic decoding only needs 1 bit at a time, it doesn't
441 * need an assured number. Retrieving larger bitstrings can be done with
442 * multiple reads and fills of the bitbuffer. The code should work fine
443 * for machines where ULONG >= 32 bits.
444 *
445 * Also note that Quantum reads bytes in normal order; LZX is in
446 * little-endian order.
447 */
448
449 #define Q_INIT_BITSTREAM do { bitsleft = 0; bitbuf = 0; } while (0)
450
451 #define Q_FILL_BUFFER do { \
452 if (bitsleft <= (CAB_ULONG_BITS - 16)) { \
453 bitbuf |= ((inpos[0]<<8)|inpos[1]) << (CAB_ULONG_BITS-16 - bitsleft); \
454 bitsleft += 16; inpos += 2; \
455 } \
456 } while (0)
457
458 #define Q_PEEK_BITS(n) (bitbuf >> (CAB_ULONG_BITS - (n)))
459 #define Q_REMOVE_BITS(n) ((bitbuf <<= (n)), (bitsleft -= (n)))
460
461 #define Q_READ_BITS(v,n) do { \
462 (v) = 0; \
463 for (bitsneed = (n); bitsneed; bitsneed -= bitrun) { \
464 Q_FILL_BUFFER; \
465 bitrun = (bitsneed > bitsleft) ? bitsleft : bitsneed; \
466 (v) = ((v) << bitrun) | Q_PEEK_BITS(bitrun); \
467 Q_REMOVE_BITS(bitrun); \
468 } \
469 } while (0)
470
471 #define Q_MENTRIES(model) (QTM(model).entries)
472 #define Q_MSYM(model,symidx) (QTM(model).syms[(symidx)].sym)
473 #define Q_MSYMFREQ(model,symidx) (QTM(model).syms[(symidx)].cumfreq)
474
475 /* GET_SYMBOL(model, var) fetches the next symbol from the stated model
476 * and puts it in var. it may need to read the bitstream to do this.
477 */
478 #define GET_SYMBOL(m, var) do { \
479 range = ((H - L) & 0xFFFF) + 1; \
480 symf = ((((C - L + 1) * Q_MSYMFREQ(m,0)) - 1) / range) & 0xFFFF; \
481 \
482 for (i=1; i < Q_MENTRIES(m); i++) { \
483 if (Q_MSYMFREQ(m,i) <= symf) break; \
484 } \
485 (var) = Q_MSYM(m,i-1); \
486 \
487 range = (H - L) + 1; \
488 H = L + ((Q_MSYMFREQ(m,i-1) * range) / Q_MSYMFREQ(m,0)) - 1; \
489 L = L + ((Q_MSYMFREQ(m,i) * range) / Q_MSYMFREQ(m,0)); \
490 while (1) { \
491 if ((L & 0x8000) != (H & 0x8000)) { \
492 if ((L & 0x4000) && !(H & 0x4000)) { \
493 /* underflow case */ \
494 C ^= 0x4000; L &= 0x3FFF; H |= 0x4000; \
495 } \
496 else break; \
497 } \
498 L <<= 1; H = (H << 1) | 1; \
499 Q_FILL_BUFFER; \
500 C = (C << 1) | Q_PEEK_BITS(1); \
501 Q_REMOVE_BITS(1); \
502 } \
503 \
504 QTMupdatemodel(&(QTM(m)), i); \
505 } while (0)
506
507 /* Bitstream reading macros (LZX / intel little-endian byte order)
508 *
509 * INIT_BITSTREAM should be used first to set up the system
510 * READ_BITS(var,n) takes N bits from the buffer and puts them in var
511 *
512 * ENSURE_BITS(n) ensures there are at least N bits in the bit buffer.
513 * it can guarantee up to 17 bits (i.e. it can read in
514 * 16 new bits when there is down to 1 bit in the buffer,
515 * and it can read 32 bits when there are 0 bits in the
516 * buffer).
517 * PEEK_BITS(n) extracts (without removing) N bits from the bit buffer
518 * REMOVE_BITS(n) removes N bits from the bit buffer
519 *
520 * These bit access routines work by using the area beyond the MSB and the
521 * LSB as a free source of zeroes. This avoids having to mask any bits.
522 * So we have to know the bit width of the bitbuffer variable.
523 */
524
525 #define INIT_BITSTREAM do { bitsleft = 0; bitbuf = 0; } while (0)
526
527 /* Quantum reads bytes in normal order; LZX is little-endian order */
528 #define ENSURE_BITS(n) \
529 while (bitsleft < (n)) { \
530 bitbuf |= ((inpos[1]<<8)|inpos[0]) << (CAB_ULONG_BITS-16 - bitsleft); \
531 bitsleft += 16; inpos+=2; \
532 }
533
534 #define PEEK_BITS(n) (bitbuf >> (CAB_ULONG_BITS - (n)))
535 #define REMOVE_BITS(n) ((bitbuf <<= (n)), (bitsleft -= (n)))
536
537 #define READ_BITS(v,n) do { \
538 if (n) { \
539 ENSURE_BITS(n); \
540 (v) = PEEK_BITS(n); \
541 REMOVE_BITS(n); \
542 } \
543 else { \
544 (v) = 0; \
545 } \
546 } while (0)
547
548 /* Huffman macros */
549
550 #define TABLEBITS(tbl) (LZX_##tbl##_TABLEBITS)
551 #define MAXSYMBOLS(tbl) (LZX_##tbl##_MAXSYMBOLS)
552 #define SYMTABLE(tbl) (LZX(tbl##_table))
553 #define LENTABLE(tbl) (LZX(tbl##_len))
554
555 /* BUILD_TABLE(tablename) builds a huffman lookup table from code lengths.
556 * In reality, it just calls make_decode_table() with the appropriate
557 * values - they're all fixed by some #defines anyway, so there's no point
558 * writing each call out in full by hand.
559 */
560 #define BUILD_TABLE(tbl) \
561 if (make_decode_table( \
562 MAXSYMBOLS(tbl), TABLEBITS(tbl), LENTABLE(tbl), SYMTABLE(tbl) \
563 )) { return DECR_ILLEGALDATA; }
564
565 /* READ_HUFFSYM(tablename, var) decodes one huffman symbol from the
566 * bitstream using the stated table and puts it in var.
567 */
568 #define READ_HUFFSYM(tbl,var) do { \
569 ENSURE_BITS(16); \
570 hufftbl = SYMTABLE(tbl); \
571 if ((i = hufftbl[PEEK_BITS(TABLEBITS(tbl))]) >= MAXSYMBOLS(tbl)) { \
572 j = 1 << (CAB_ULONG_BITS - TABLEBITS(tbl)); \
573 do { \
574 j >>= 1; i <<= 1; i |= (bitbuf & j) ? 1 : 0; \
575 if (!j) { return DECR_ILLEGALDATA; } \
576 } while ((i = hufftbl[i]) >= MAXSYMBOLS(tbl)); \
577 } \
578 j = LENTABLE(tbl)[(var) = i]; \
579 REMOVE_BITS(j); \
580 } while (0)
581
582 /* READ_LENGTHS(tablename, first, last) reads in code lengths for symbols
583 * first to last in the given table. The code lengths are stored in their
584 * own special LZX way.
585 */
586 #define READ_LENGTHS(tbl,first,last,fn) do { \
587 lb.bb = bitbuf; lb.bl = bitsleft; lb.ip = inpos; \
588 if (fn(LENTABLE(tbl),(first),(last),&lb,decomp_state)) { \
589 return DECR_ILLEGALDATA; \
590 } \
591 bitbuf = lb.bb; bitsleft = lb.bl; inpos = lb.ip; \
592 } while (0)
593
594 /* Tables for deflate from PKZIP's appnote.txt. */
595
596 #define THOSE_ZIP_CONSTS \
597 static const cab_UBYTE Zipborder[] = /* Order of the bit length code lengths */ \
598 { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; \
599 static const cab_UWORD Zipcplens[] = /* Copy lengths for literal codes 257..285 */ \
600 { 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, \
601 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; \
602 static const cab_UWORD Zipcplext[] = /* Extra bits for literal codes 257..285 */ \
603 { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, \
604 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */ \
605 static const cab_UWORD Zipcpdist[] = /* Copy offsets for distance codes 0..29 */ \
606 { 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, \
607 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577}; \
608 static const cab_UWORD Zipcpdext[] = /* Extra bits for distance codes */ \
609 { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, \
610 10, 11, 11, 12, 12, 13, 13}; \
611 /* And'ing with Zipmask[n] masks the lower n bits */ \
612 static const cab_UWORD Zipmask[17] = { \
613 0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff, \
614 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff \
615 }
616
617 /* EXTRACTdest flags */
618 #define EXTRACT_FILLFILELIST 0x00000001
619 #define EXTRACT_EXTRACTFILES 0x00000002
620
621 struct ExtractFileList {
622 LPSTR filename;
623 struct ExtractFileList *next;
624 BOOL flag;
625 } ;
626
627 /* the first parameter of the function extract */
628 typedef struct {
629 long result1; /* 0x000 */
630 long unknown1[3]; /* 0x004 */
631 struct ExtractFileList *filelist; /* 0x010 */
632 long filecount; /* 0x014 */
633 DWORD flags; /* 0x018 */
634 char directory[MAX_PATH]; /* 0x01c */
635 char lastfile[MAX_PATH]; /* 0x120 */
636 char unknown2[MAX_PATH]; /* 0x224 */
637 struct ExtractFileList *filterlist; /* 0x328 */
638 } EXTRACTdest;
639
640
641 /* from fdi.c */
642 void QTMupdatemodel(struct QTMmodel *model, int sym);
643 int make_decode_table(cab_ULONG nsyms, cab_ULONG nbits, const cab_UBYTE *length, cab_UWORD *table);
644 cab_ULONG checksum(const cab_UBYTE *data, cab_UWORD bytes, cab_ULONG csum);
645
646 #endif /* __WINE_CABINET_H */