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