4 * Copyright 2002 Greg Turner
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.
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.
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
20 #ifndef __WINE_CABINET_H
21 #define __WINE_CABINET_H
25 #define CAB_SPLITMAX (10)
27 #define CAB_SEARCH_SIZE (32*1024)
29 typedef unsigned char cab_UBYTE
; /* 8 bits */
30 typedef UINT16 cab_UWORD
; /* 16 bits */
31 typedef UINT32 cab_ULONG
; /* 32 bits */
32 typedef INT32 cab_LONG
; /* 32 bits */
34 typedef UINT32 cab_off_t
;
36 /* number of bits in a ULONG */
40 #define CAB_ULONG_BITS (sizeof(cab_ULONG) * CHAR_BIT)
42 /* endian-neutral reading of little-endian data */
43 #define EndGetI32(a) ((((a)[3])<<24)|(((a)[2])<<16)|(((a)[1])<<8)|((a)[0]))
44 #define EndGetI16(a) ((((a)[1])<<8)|((a)[0]))
46 /* structure offsets */
47 #define cfhead_Signature (0x00)
48 #define cfhead_CabinetSize (0x08)
49 #define cfhead_FileOffset (0x10)
50 #define cfhead_MinorVersion (0x18)
51 #define cfhead_MajorVersion (0x19)
52 #define cfhead_NumFolders (0x1A)
53 #define cfhead_NumFiles (0x1C)
54 #define cfhead_Flags (0x1E)
55 #define cfhead_SetID (0x20)
56 #define cfhead_CabinetIndex (0x22)
57 #define cfhead_SIZEOF (0x24)
58 #define cfheadext_HeaderReserved (0x00)
59 #define cfheadext_FolderReserved (0x02)
60 #define cfheadext_DataReserved (0x03)
61 #define cfheadext_SIZEOF (0x04)
62 #define cffold_DataOffset (0x00)
63 #define cffold_NumBlocks (0x04)
64 #define cffold_CompType (0x06)
65 #define cffold_SIZEOF (0x08)
66 #define cffile_UncompressedSize (0x00)
67 #define cffile_FolderOffset (0x04)
68 #define cffile_FolderIndex (0x08)
69 #define cffile_Date (0x0A)
70 #define cffile_Time (0x0C)
71 #define cffile_Attribs (0x0E)
72 #define cffile_SIZEOF (0x10)
73 #define cfdata_CheckSum (0x00)
74 #define cfdata_CompressedSize (0x04)
75 #define cfdata_UncompressedSize (0x06)
76 #define cfdata_SIZEOF (0x08)
79 #define cffoldCOMPTYPE_MASK (0x000f)
80 #define cffoldCOMPTYPE_NONE (0x0000)
81 #define cffoldCOMPTYPE_MSZIP (0x0001)
82 #define cffoldCOMPTYPE_QUANTUM (0x0002)
83 #define cffoldCOMPTYPE_LZX (0x0003)
84 #define cfheadPREV_CABINET (0x0001)
85 #define cfheadNEXT_CABINET (0x0002)
86 #define cfheadRESERVE_PRESENT (0x0004)
87 #define cffileCONTINUED_FROM_PREV (0xFFFD)
88 #define cffileCONTINUED_TO_NEXT (0xFFFE)
89 #define cffileCONTINUED_PREV_AND_NEXT (0xFFFF)
90 #define cffile_A_RDONLY (0x01)
91 #define cffile_A_HIDDEN (0x02)
92 #define cffile_A_SYSTEM (0x04)
93 #define cffile_A_ARCH (0x20)
94 #define cffile_A_EXEC (0x40)
95 #define cffile_A_NAME_IS_UTF (0x80)
97 /****************************************************************************/
98 /* our archiver information / state */
101 #define ZIPWSIZE 0x8000 /* window size */
102 #define ZIPLBITS 9 /* bits in base literal/length lookup table */
103 #define ZIPDBITS 6 /* bits in base distance lookup table */
104 #define ZIPBMAX 16 /* maximum bit length of any code */
105 #define ZIPN_MAX 288 /* maximum number of codes in any set */
108 cab_UBYTE e
; /* number of extra bits or operation */
109 cab_UBYTE b
; /* number of bits in this code or subcode */
111 cab_UWORD n
; /* literal, length base, or distance base */
112 struct Ziphuft
*t
; /* pointer to next level of table */
117 cab_ULONG window_posn
; /* current offset within the window */
118 cab_ULONG bb
; /* bit buffer */
119 cab_ULONG bk
; /* bits in bit buffer */
120 cab_ULONG ll
[288+32]; /* literal/length and distance code lengths */
121 cab_ULONG c
[ZIPBMAX
+1]; /* bit length count table */
122 cab_LONG lx
[ZIPBMAX
+1]; /* memory for l[-1..ZIPBMAX-1] */
123 struct Ziphuft
*u
[ZIPBMAX
]; /* table stack */
124 cab_ULONG v
[ZIPN_MAX
]; /* values in order of bit length */
125 cab_ULONG x
[ZIPBMAX
+1]; /* bit offsets, then code stack */
132 cab_UWORD sym
, cumfreq
;
136 int shiftsleft
, entries
;
137 struct QTMmodelsym
*syms
;
138 cab_UWORD tabloc
[256];
142 cab_UBYTE
*window
; /* the actual decoding window */
143 cab_ULONG window_size
; /* window size (1Kb through 2Mb) */
144 cab_ULONG actual_size
; /* window size when it was first allocated */
145 cab_ULONG window_posn
; /* current offset within the window */
147 struct QTMmodel model7
;
148 struct QTMmodelsym m7sym
[7+1];
150 struct QTMmodel model4
, model5
, model6pos
, model6len
;
151 struct QTMmodelsym m4sym
[0x18 + 1];
152 struct QTMmodelsym m5sym
[0x24 + 1];
153 struct QTMmodelsym m6psym
[0x2a + 1], m6lsym
[0x1b + 1];
155 struct QTMmodel model00
, model40
, model80
, modelC0
;
156 struct QTMmodelsym m00sym
[0x40 + 1], m40sym
[0x40 + 1];
157 struct QTMmodelsym m80sym
[0x40 + 1], mC0sym
[0x40 + 1];
162 /* some constants defined by the LZX specification */
163 #define LZX_MIN_MATCH (2)
164 #define LZX_MAX_MATCH (257)
165 #define LZX_NUM_CHARS (256)
166 #define LZX_BLOCKTYPE_INVALID (0) /* also blocktypes 4-7 invalid */
167 #define LZX_BLOCKTYPE_VERBATIM (1)
168 #define LZX_BLOCKTYPE_ALIGNED (2)
169 #define LZX_BLOCKTYPE_UNCOMPRESSED (3)
170 #define LZX_PRETREE_NUM_ELEMENTS (20)
171 #define LZX_ALIGNED_NUM_ELEMENTS (8) /* aligned offset tree #elements */
172 #define LZX_NUM_PRIMARY_LENGTHS (7) /* this one missing from spec! */
173 #define LZX_NUM_SECONDARY_LENGTHS (249) /* length tree #elements */
175 /* LZX huffman defines: tweak tablebits as desired */
176 #define LZX_PRETREE_MAXSYMBOLS (LZX_PRETREE_NUM_ELEMENTS)
177 #define LZX_PRETREE_TABLEBITS (6)
178 #define LZX_MAINTREE_MAXSYMBOLS (LZX_NUM_CHARS + 50*8)
179 #define LZX_MAINTREE_TABLEBITS (12)
180 #define LZX_LENGTH_MAXSYMBOLS (LZX_NUM_SECONDARY_LENGTHS+1)
181 #define LZX_LENGTH_TABLEBITS (12)
182 #define LZX_ALIGNED_MAXSYMBOLS (LZX_ALIGNED_NUM_ELEMENTS)
183 #define LZX_ALIGNED_TABLEBITS (7)
185 #define LZX_LENTABLE_SAFETY (64) /* we allow length table decoding overruns */
187 #define LZX_DECLARE_TABLE(tbl) \
188 cab_UWORD tbl##_table[(1<<LZX_##tbl##_TABLEBITS) + (LZX_##tbl##_MAXSYMBOLS<<1)];\
189 cab_UBYTE tbl##_len [LZX_##tbl##_MAXSYMBOLS + LZX_LENTABLE_SAFETY]
192 cab_UBYTE
*window
; /* the actual decoding window */
193 cab_ULONG window_size
; /* window size (32Kb through 2Mb) */
194 cab_ULONG actual_size
; /* window size when it was first allocated */
195 cab_ULONG window_posn
; /* current offset within the window */
196 cab_ULONG R0
, R1
, R2
; /* for the LRU offset system */
197 cab_UWORD main_elements
; /* number of main tree elements */
198 int header_read
; /* have we started decoding at all yet? */
199 cab_UWORD block_type
; /* type of this block */
200 cab_ULONG block_length
; /* uncompressed length of this block */
201 cab_ULONG block_remaining
; /* uncompressed bytes still left to decode */
202 cab_ULONG frames_read
; /* the number of CFDATA blocks processed */
203 cab_LONG intel_filesize
; /* magic header value used for transform */
204 cab_LONG intel_curpos
; /* current offset in transform space */
205 int intel_started
; /* have we seen any translatable data yet? */
207 LZX_DECLARE_TABLE(PRETREE
);
208 LZX_DECLARE_TABLE(MAINTREE
);
209 LZX_DECLARE_TABLE(LENGTH
);
210 LZX_DECLARE_TABLE(ALIGNED
);
214 #define CAB(x) (decomp_state.x)
215 #define ZIP(x) (decomp_state.methods.zip.x)
216 #define QTM(x) (decomp_state.methods.qtm.x)
217 #define LZX(x) (decomp_state.methods.lzx.x)
219 #define DECR_DATAFORMAT (1)
220 #define DECR_ILLEGALDATA (2)
221 #define DECR_NOMEMORY (3)
222 #define DECR_CHECKSUM (4)
223 #define DECR_INPUT (5)
224 #define DECR_OUTPUT (6)
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.
231 #define CAB_BLOCKMAX (32768)
232 #define CAB_INPUTMAX (CAB_BLOCKMAX+6144)
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 */
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 */
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 */
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); /* the chosen compression func */
279 cab_UBYTE inbuf
[CAB_INPUTMAX
+2]; /* +2 for lzx bitbuffer overflows! */
280 cab_UBYTE outbuf
[CAB_BLOCKMAX
];
288 /* from cabextract.c */
289 BOOL
process_cabinet(LPCSTR cabname
, LPCSTR dir
, BOOL fix
, BOOL lower
);
291 #endif /* __WINE_CABINET_H */