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