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