msvcrt: Use fpclass constants from public header.
[wine/zf.git] / dlls / cabinet / cabinet.h
blob6193622cb11763c4cfda11bc9720ba25dd471057
1 /*
2 * cabinet.h
4 * Copyright 2002 Greg Turner
5 * Copyright 2005 Gerold Jens Wucherpfennig
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.
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.
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
21 #ifndef __WINE_CABINET_H
22 #define __WINE_CABINET_H
24 #include <stdarg.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winnt.h"
29 #include "fdi.h"
30 #include "fci.h"
32 #define CAB_SPLITMAX (10)
34 #define CAB_SEARCH_SIZE (32*1024)
36 typedef unsigned char cab_UBYTE; /* 8 bits */
37 typedef UINT16 cab_UWORD; /* 16 bits */
38 typedef UINT32 cab_ULONG; /* 32 bits */
39 typedef INT32 cab_LONG; /* 32 bits */
41 typedef UINT32 cab_off_t;
43 /* number of bits in a ULONG */
44 #ifndef CHAR_BIT
45 # define CHAR_BIT (8)
46 #endif
47 #define CAB_ULONG_BITS (sizeof(cab_ULONG) * CHAR_BIT)
49 /* structure offsets */
50 #define cfhead_Signature (0x00)
51 #define cfhead_CabinetSize (0x08)
52 #define cfhead_FileOffset (0x10)
53 #define cfhead_MinorVersion (0x18)
54 #define cfhead_MajorVersion (0x19)
55 #define cfhead_NumFolders (0x1A)
56 #define cfhead_NumFiles (0x1C)
57 #define cfhead_Flags (0x1E)
58 #define cfhead_SetID (0x20)
59 #define cfhead_CabinetIndex (0x22)
60 #define cfhead_SIZEOF (0x24)
61 #define cfheadext_HeaderReserved (0x00)
62 #define cfheadext_FolderReserved (0x02)
63 #define cfheadext_DataReserved (0x03)
64 #define cfheadext_SIZEOF (0x04)
65 #define cffold_DataOffset (0x00)
66 #define cffold_NumBlocks (0x04)
67 #define cffold_CompType (0x06)
68 #define cffold_SIZEOF (0x08)
69 #define cffile_UncompressedSize (0x00)
70 #define cffile_FolderOffset (0x04)
71 #define cffile_FolderIndex (0x08)
72 #define cffile_Date (0x0A)
73 #define cffile_Time (0x0C)
74 #define cffile_Attribs (0x0E)
75 #define cffile_SIZEOF (0x10)
76 #define cfdata_CheckSum (0x00)
77 #define cfdata_CompressedSize (0x04)
78 #define cfdata_UncompressedSize (0x06)
79 #define cfdata_SIZEOF (0x08)
81 /* flags */
82 #define cffoldCOMPTYPE_MASK (0x000f)
83 #define cffoldCOMPTYPE_NONE (0x0000)
84 #define cffoldCOMPTYPE_MSZIP (0x0001)
85 #define cffoldCOMPTYPE_QUANTUM (0x0002)
86 #define cffoldCOMPTYPE_LZX (0x0003)
87 #define cfheadPREV_CABINET (0x0001)
88 #define cfheadNEXT_CABINET (0x0002)
89 #define cfheadRESERVE_PRESENT (0x0004)
90 #define cffileCONTINUED_FROM_PREV (0xFFFD)
91 #define cffileCONTINUED_TO_NEXT (0xFFFE)
92 #define cffileCONTINUED_PREV_AND_NEXT (0xFFFF)
93 #define cffile_A_RDONLY (0x01)
94 #define cffile_A_HIDDEN (0x02)
95 #define cffile_A_SYSTEM (0x04)
96 #define cffile_A_ARCH (0x20)
97 #define cffile_A_EXEC (0x40)
98 #define cffile_A_NAME_IS_UTF (0x80)
100 /****************************************************************************/
101 /* our archiver information / state */
103 /* MSZIP stuff */
104 #define ZIPWSIZE 0x8000 /* window size */
105 #define ZIPLBITS 9 /* bits in base literal/length lookup table */
106 #define ZIPDBITS 6 /* bits in base distance lookup table */
107 #define ZIPBMAX 16 /* maximum bit length of any code */
108 #define ZIPN_MAX 288 /* maximum number of codes in any set */
110 struct Ziphuft {
111 cab_UBYTE e; /* number of extra bits or operation */
112 cab_UBYTE b; /* number of bits in this code or subcode */
113 union {
114 cab_UWORD n; /* literal, length base, or distance base */
115 struct Ziphuft *t; /* pointer to next level of table */
116 } v;
119 struct ZIPstate {
120 cab_ULONG window_posn; /* current offset within the window */
121 cab_ULONG bb; /* bit buffer */
122 cab_ULONG bk; /* bits in bit buffer */
123 cab_ULONG ll[288+32]; /* literal/length and distance code lengths */
124 cab_ULONG c[ZIPBMAX+1]; /* bit length count table */
125 cab_LONG lx[ZIPBMAX+1]; /* memory for l[-1..ZIPBMAX-1] */
126 struct Ziphuft *u[ZIPBMAX]; /* table stack */
127 cab_ULONG v[ZIPN_MAX]; /* values in order of bit length */
128 cab_ULONG x[ZIPBMAX+1]; /* bit offsets, then code stack */
129 cab_UBYTE *inpos;
132 /* Quantum stuff */
134 struct QTMmodelsym {
135 cab_UWORD sym, cumfreq;
138 struct QTMmodel {
139 int shiftsleft, entries;
140 struct QTMmodelsym *syms;
141 cab_UWORD tabloc[256];
144 struct QTMstate {
145 cab_UBYTE *window; /* the actual decoding window */
146 cab_ULONG window_size; /* window size (1Kb through 2Mb) */
147 cab_ULONG actual_size; /* window size when it was first allocated */
148 cab_ULONG window_posn; /* current offset within the window */
150 struct QTMmodel model7;
151 struct QTMmodelsym m7sym[7+1];
153 struct QTMmodel model4, model5, model6pos, model6len;
154 struct QTMmodelsym m4sym[0x18 + 1];
155 struct QTMmodelsym m5sym[0x24 + 1];
156 struct QTMmodelsym m6psym[0x2a + 1], m6lsym[0x1b + 1];
158 struct QTMmodel model00, model40, model80, modelC0;
159 struct QTMmodelsym m00sym[0x40 + 1], m40sym[0x40 + 1];
160 struct QTMmodelsym m80sym[0x40 + 1], mC0sym[0x40 + 1];
163 /* LZX stuff */
165 /* some constants defined by the LZX specification */
166 #define LZX_MIN_MATCH (2)
167 #define LZX_MAX_MATCH (257)
168 #define LZX_NUM_CHARS (256)
169 #define LZX_BLOCKTYPE_INVALID (0) /* also blocktypes 4-7 invalid */
170 #define LZX_BLOCKTYPE_VERBATIM (1)
171 #define LZX_BLOCKTYPE_ALIGNED (2)
172 #define LZX_BLOCKTYPE_UNCOMPRESSED (3)
173 #define LZX_PRETREE_NUM_ELEMENTS (20)
174 #define LZX_ALIGNED_NUM_ELEMENTS (8) /* aligned offset tree #elements */
175 #define LZX_NUM_PRIMARY_LENGTHS (7) /* this one missing from spec! */
176 #define LZX_NUM_SECONDARY_LENGTHS (249) /* length tree #elements */
178 /* LZX huffman defines: tweak tablebits as desired */
179 #define LZX_PRETREE_MAXSYMBOLS (LZX_PRETREE_NUM_ELEMENTS)
180 #define LZX_PRETREE_TABLEBITS (6)
181 #define LZX_MAINTREE_MAXSYMBOLS (LZX_NUM_CHARS + 50*8)
182 #define LZX_MAINTREE_TABLEBITS (12)
183 #define LZX_LENGTH_MAXSYMBOLS (LZX_NUM_SECONDARY_LENGTHS+1)
184 #define LZX_LENGTH_TABLEBITS (12)
185 #define LZX_ALIGNED_MAXSYMBOLS (LZX_ALIGNED_NUM_ELEMENTS)
186 #define LZX_ALIGNED_TABLEBITS (7)
188 #define LZX_LENTABLE_SAFETY (64) /* we allow length table decoding overruns */
190 #define LZX_DECLARE_TABLE(tbl) \
191 cab_UWORD tbl##_table[(1<<LZX_##tbl##_TABLEBITS) + (LZX_##tbl##_MAXSYMBOLS<<1)];\
192 cab_UBYTE tbl##_len [LZX_##tbl##_MAXSYMBOLS + LZX_LENTABLE_SAFETY]
194 struct LZXstate {
195 cab_UBYTE *window; /* the actual decoding window */
196 cab_ULONG window_size; /* window size (32Kb through 2Mb) */
197 cab_ULONG actual_size; /* window size when it was first allocated */
198 cab_ULONG window_posn; /* current offset within the window */
199 cab_ULONG R0, R1, R2; /* for the LRU offset system */
200 cab_UWORD main_elements; /* number of main tree elements */
201 int header_read; /* have we started decoding at all yet? */
202 cab_UWORD block_type; /* type of this block */
203 cab_ULONG block_length; /* uncompressed length of this block */
204 cab_ULONG block_remaining; /* uncompressed bytes still left to decode */
205 cab_ULONG frames_read; /* the number of CFDATA blocks processed */
206 cab_LONG intel_filesize; /* magic header value used for transform */
207 cab_LONG intel_curpos; /* current offset in transform space */
208 int intel_started; /* have we seen any translatable data yet? */
210 LZX_DECLARE_TABLE(PRETREE);
211 LZX_DECLARE_TABLE(MAINTREE);
212 LZX_DECLARE_TABLE(LENGTH);
213 LZX_DECLARE_TABLE(ALIGNED);
216 struct lzx_bits {
217 cab_ULONG bb;
218 int bl;
219 cab_UBYTE *ip;
222 /* CAB data blocks are <= 32768 bytes in uncompressed form. Uncompressed
223 * blocks have zero growth. MSZIP guarantees that it won't grow above
224 * uncompressed size by more than 12 bytes. LZX guarantees it won't grow
225 * more than 6144 bytes.
227 #define CAB_BLOCKMAX (32768)
228 #define CAB_INPUTMAX (CAB_BLOCKMAX+6144)
230 struct cab_file {
231 struct cab_file *next; /* next file in sequence */
232 struct cab_folder *folder; /* folder that contains this file */
233 LPCSTR filename; /* output name of file */
234 HANDLE fh; /* open file handle or NULL */
235 cab_ULONG length; /* uncompressed length of file */
236 cab_ULONG offset; /* uncompressed offset in folder */
237 cab_UWORD index; /* magic index number of folder */
238 cab_UWORD time, date, attribs; /* MS-DOS time/date/attributes */
242 struct cab_folder {
243 struct cab_folder *next;
244 struct cabinet *cab[CAB_SPLITMAX]; /* cabinet(s) this folder spans */
245 cab_off_t offset[CAB_SPLITMAX]; /* offset to data blocks */
246 cab_UWORD comp_type; /* compression format/window size */
247 cab_ULONG comp_size; /* compressed size of folder */
248 cab_UBYTE num_splits; /* number of split blocks + 1 */
249 cab_UWORD num_blocks; /* total number of blocks */
250 struct cab_file *contfile; /* the first split file */
253 struct cabinet {
254 struct cabinet *next; /* for making a list of cabinets */
255 LPCSTR filename; /* input name of cabinet */
256 HANDLE *fh; /* open file handle or NULL */
257 cab_off_t filelen; /* length of cabinet file */
258 cab_off_t blocks_off; /* offset to data blocks in file */
259 struct cabinet *prevcab, *nextcab; /* multipart cabinet chains */
260 char *prevname, *nextname; /* and their filenames */
261 char *previnfo, *nextinfo; /* and their visible names */
262 struct cab_folder *folders; /* first folder in this cabinet */
263 struct cab_file *files; /* first file in this cabinet */
264 cab_UBYTE block_resv; /* reserved space in datablocks */
265 cab_UBYTE flags; /* header flags */
268 typedef struct cds_forward {
269 struct cab_folder *current; /* current folder we're extracting from */
270 cab_ULONG offset; /* uncompressed offset within folder */
271 cab_UBYTE *outpos; /* (high level) start of data to use up */
272 cab_UWORD outlen; /* (high level) amount of data to use up */
273 cab_UWORD split; /* at which split in current folder? */
274 int (*decompress)(int, int, struct cds_forward *); /* chosen compress fn */
275 cab_UBYTE inbuf[CAB_INPUTMAX+2]; /* +2 for lzx bitbuffer overflows! */
276 cab_UBYTE outbuf[CAB_BLOCKMAX];
277 cab_UBYTE q_length_base[27], q_length_extra[27], q_extra_bits[42];
278 cab_ULONG q_position_base[42];
279 cab_ULONG lzx_position_base[51];
280 cab_UBYTE extra_bits[51];
281 union {
282 struct ZIPstate zip;
283 struct QTMstate qtm;
284 struct LZXstate lzx;
285 } methods;
286 } cab_decomp_state;
289 * the rest of these are somewhat kludgy macros which are shared between fdi.c
290 * and cabextract.c.
293 /* Bitstream reading macros (Quantum / normal byte order)
295 * Q_INIT_BITSTREAM should be used first to set up the system
296 * Q_READ_BITS(var,n) takes N bits from the buffer and puts them in var.
297 * unlike LZX, this can loop several times to get the
298 * requisite number of bits.
299 * Q_FILL_BUFFER adds more data to the bit buffer, if there is room
300 * for another 16 bits.
301 * Q_PEEK_BITS(n) extracts (without removing) N bits from the bit
302 * buffer
303 * Q_REMOVE_BITS(n) removes N bits from the bit buffer
305 * These bit access routines work by using the area beyond the MSB and the
306 * LSB as a free source of zeroes. This avoids having to mask any bits.
307 * So we have to know the bit width of the bitbuffer variable. This is
308 * defined as ULONG_BITS.
310 * ULONG_BITS should be at least 16 bits. Unlike LZX's Huffman decoding,
311 * Quantum's arithmetic decoding only needs 1 bit at a time, it doesn't
312 * need an assured number. Retrieving larger bitstrings can be done with
313 * multiple reads and fills of the bitbuffer. The code should work fine
314 * for machines where ULONG >= 32 bits.
316 * Also note that Quantum reads bytes in normal order; LZX is in
317 * little-endian order.
320 #define Q_INIT_BITSTREAM do { bitsleft = 0; bitbuf = 0; } while (0)
322 #define Q_FILL_BUFFER do { \
323 if (bitsleft <= (CAB_ULONG_BITS - 16)) { \
324 bitbuf |= ((inpos[0]<<8)|inpos[1]) << (CAB_ULONG_BITS-16 - bitsleft); \
325 bitsleft += 16; inpos += 2; \
327 } while (0)
329 #define Q_PEEK_BITS(n) (bitbuf >> (CAB_ULONG_BITS - (n)))
330 #define Q_REMOVE_BITS(n) ((bitbuf <<= (n)), (bitsleft -= (n)))
332 #define Q_READ_BITS(v,n) do { \
333 (v) = 0; \
334 for (bitsneed = (n); bitsneed; bitsneed -= bitrun) { \
335 Q_FILL_BUFFER; \
336 bitrun = (bitsneed > bitsleft) ? bitsleft : bitsneed; \
337 (v) = ((v) << bitrun) | Q_PEEK_BITS(bitrun); \
338 Q_REMOVE_BITS(bitrun); \
340 } while (0)
342 #define Q_MENTRIES(model) (QTM(model).entries)
343 #define Q_MSYM(model,symidx) (QTM(model).syms[(symidx)].sym)
344 #define Q_MSYMFREQ(model,symidx) (QTM(model).syms[(symidx)].cumfreq)
346 /* GET_SYMBOL(model, var) fetches the next symbol from the stated model
347 * and puts it in var. it may need to read the bitstream to do this.
349 #define GET_SYMBOL(m, var) do { \
350 range = ((H - L) & 0xFFFF) + 1; \
351 symf = ((((C - L + 1) * Q_MSYMFREQ(m,0)) - 1) / range) & 0xFFFF; \
353 for (i=1; i < Q_MENTRIES(m); i++) { \
354 if (Q_MSYMFREQ(m,i) <= symf) break; \
356 (var) = Q_MSYM(m,i-1); \
358 range = (H - L) + 1; \
359 H = L + ((Q_MSYMFREQ(m,i-1) * range) / Q_MSYMFREQ(m,0)) - 1; \
360 L = L + ((Q_MSYMFREQ(m,i) * range) / Q_MSYMFREQ(m,0)); \
361 while (1) { \
362 if ((L & 0x8000) != (H & 0x8000)) { \
363 if ((L & 0x4000) && !(H & 0x4000)) { \
364 /* underflow case */ \
365 C ^= 0x4000; L &= 0x3FFF; H |= 0x4000; \
367 else break; \
369 L <<= 1; H = (H << 1) | 1; \
370 Q_FILL_BUFFER; \
371 C = (C << 1) | Q_PEEK_BITS(1); \
372 Q_REMOVE_BITS(1); \
375 QTMupdatemodel(&(QTM(m)), i); \
376 } while (0)
378 /* Bitstream reading macros (LZX / intel little-endian byte order)
380 * INIT_BITSTREAM should be used first to set up the system
381 * READ_BITS(var,n) takes N bits from the buffer and puts them in var
383 * ENSURE_BITS(n) ensures there are at least N bits in the bit buffer.
384 * it can guarantee up to 17 bits (i.e. it can read in
385 * 16 new bits when there is down to 1 bit in the buffer,
386 * and it can read 32 bits when there are 0 bits in the
387 * buffer).
388 * PEEK_BITS(n) extracts (without removing) N bits from the bit buffer
389 * REMOVE_BITS(n) removes N bits from the bit buffer
391 * These bit access routines work by using the area beyond the MSB and the
392 * LSB as a free source of zeroes. This avoids having to mask any bits.
393 * So we have to know the bit width of the bitbuffer variable.
396 #define INIT_BITSTREAM do { bitsleft = 0; bitbuf = 0; } while (0)
398 /* Quantum reads bytes in normal order; LZX is little-endian order */
399 #define ENSURE_BITS(n) \
400 while (bitsleft < (n)) { \
401 bitbuf |= ((inpos[1]<<8)|inpos[0]) << (CAB_ULONG_BITS-16 - bitsleft); \
402 bitsleft += 16; inpos+=2; \
405 #define PEEK_BITS(n) (bitbuf >> (CAB_ULONG_BITS - (n)))
406 #define REMOVE_BITS(n) ((bitbuf <<= (n)), (bitsleft -= (n)))
408 #define READ_BITS(v,n) do { \
409 if (n) { \
410 ENSURE_BITS(n); \
411 (v) = PEEK_BITS(n); \
412 REMOVE_BITS(n); \
414 else { \
415 (v) = 0; \
417 } while (0)
419 /* Huffman macros */
421 #define TABLEBITS(tbl) (LZX_##tbl##_TABLEBITS)
422 #define MAXSYMBOLS(tbl) (LZX_##tbl##_MAXSYMBOLS)
423 #define SYMTABLE(tbl) (LZX(tbl##_table))
424 #define LENTABLE(tbl) (LZX(tbl##_len))
426 /* BUILD_TABLE(tablename) builds a huffman lookup table from code lengths.
427 * In reality, it just calls make_decode_table() with the appropriate
428 * values - they're all fixed by some #defines anyway, so there's no point
429 * writing each call out in full by hand.
431 #define BUILD_TABLE(tbl) \
432 if (make_decode_table( \
433 MAXSYMBOLS(tbl), TABLEBITS(tbl), LENTABLE(tbl), SYMTABLE(tbl) \
434 )) { return DECR_ILLEGALDATA; }
436 /* READ_HUFFSYM(tablename, var) decodes one huffman symbol from the
437 * bitstream using the stated table and puts it in var.
439 #define READ_HUFFSYM(tbl,var) do { \
440 ENSURE_BITS(16); \
441 hufftbl = SYMTABLE(tbl); \
442 if ((i = hufftbl[PEEK_BITS(TABLEBITS(tbl))]) >= MAXSYMBOLS(tbl)) { \
443 j = 1 << (CAB_ULONG_BITS - TABLEBITS(tbl)); \
444 do { \
445 j >>= 1; i <<= 1; i |= (bitbuf & j) ? 1 : 0; \
446 if (!j) { return DECR_ILLEGALDATA; } \
447 } while ((i = hufftbl[i]) >= MAXSYMBOLS(tbl)); \
449 j = LENTABLE(tbl)[(var) = i]; \
450 REMOVE_BITS(j); \
451 } while (0)
453 /* READ_LENGTHS(tablename, first, last) reads in code lengths for symbols
454 * first to last in the given table. The code lengths are stored in their
455 * own special LZX way.
457 #define READ_LENGTHS(tbl,first,last,fn) do { \
458 lb.bb = bitbuf; lb.bl = bitsleft; lb.ip = inpos; \
459 if (fn(LENTABLE(tbl),(first),(last),&lb,decomp_state)) { \
460 return DECR_ILLEGALDATA; \
462 bitbuf = lb.bb; bitsleft = lb.bl; inpos = lb.ip; \
463 } while (0)
465 /* Tables for deflate from PKZIP's appnote.txt. */
467 #define THOSE_ZIP_CONSTS \
468 static const cab_UBYTE Zipborder[] = /* Order of the bit length code lengths */ \
469 { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; \
470 static const cab_UWORD Zipcplens[] = /* Copy lengths for literal codes 257..285 */ \
471 { 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, \
472 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; \
473 static const cab_UWORD Zipcplext[] = /* Extra bits for literal codes 257..285 */ \
474 { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, \
475 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */ \
476 static const cab_UWORD Zipcpdist[] = /* Copy offsets for distance codes 0..29 */ \
477 { 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, \
478 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577}; \
479 static const cab_UWORD Zipcpdext[] = /* Extra bits for distance codes */ \
480 { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, \
481 10, 11, 11, 12, 12, 13, 13}; \
482 /* And'ing with Zipmask[n] masks the lower n bits */ \
483 static const cab_UWORD Zipmask[17] = { \
484 0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff, \
485 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff \
488 /* SESSION Operation */
489 #define EXTRACT_FILLFILELIST 0x00000001
490 #define EXTRACT_EXTRACTFILES 0x00000002
492 struct FILELIST{
493 LPSTR FileName;
494 struct FILELIST *next;
495 BOOL DoExtract;
498 typedef struct {
499 INT FileSize;
500 ERF Error;
501 struct FILELIST *FileList;
502 INT FileCount;
503 INT Operation;
504 CHAR Destination[MAX_PATH];
505 CHAR CurrentFile[MAX_PATH];
506 CHAR Reserved[MAX_PATH];
507 struct FILELIST *FilterList;
508 } SESSION;
510 #endif /* __WINE_CABINET_H */