#ifndef __WINE_CABINET_H
#define __WINE_CABINET_H
+#include <stdarg.h>
+
+#include "windef.h"
+#include "winbase.h"
#include "winnt.h"
+#include "fdi.h"
+#include "fci.h"
+
+/* from msvcrt/sys/stat.h */
+#define _S_IWRITE 0x0080
+#define _S_IREAD 0x0100
#define CAB_SPLITMAX (10)
#endif
#define CAB_ULONG_BITS (sizeof(cab_ULONG) * CHAR_BIT)
-/* endian-neutral reading of little-endian data */
-#define EndGetI32(a) ((((a)[3])<<24)|(((a)[2])<<16)|(((a)[1])<<8)|((a)[0]))
-#define EndGetI16(a) ((((a)[1])<<8)|((a)[0]))
-
/* structure offsets */
#define cfhead_Signature (0x00)
#define cfhead_CabinetSize (0x08)
LZX_DECLARE_TABLE(ALIGNED);
};
-/* generic stuff */
-#define CAB(x) (decomp_state.x)
-#define ZIP(x) (decomp_state.methods.zip.x)
-#define QTM(x) (decomp_state.methods.qtm.x)
-#define LZX(x) (decomp_state.methods.lzx.x)
-#define DECR_OK (0)
-#define DECR_DATAFORMAT (1)
-#define DECR_ILLEGALDATA (2)
-#define DECR_NOMEMORY (3)
-#define DECR_CHECKSUM (4)
-#define DECR_INPUT (5)
-#define DECR_OUTPUT (6)
+struct lzx_bits {
+ cab_ULONG bb;
+ int bl;
+ cab_UBYTE *ip;
+};
/* CAB data blocks are <= 32768 bytes in uncompressed form. Uncompressed
* blocks have zero growth. MSZIP guarantees that it won't grow above
struct cab_file {
struct cab_file *next; /* next file in sequence */
struct cab_folder *folder; /* folder that contains this file */
- LPCSTR filename; /* output name of file */
+ LPCSTR filename; /* output name of file */
HANDLE fh; /* open file handle or NULL */
cab_ULONG length; /* uncompressed length of file */
cab_ULONG offset; /* uncompressed offset in folder */
struct cabinet {
struct cabinet *next; /* for making a list of cabinets */
- LPCSTR filename; /* input name of cabinet */
+ LPCSTR filename; /* input name of cabinet */
HANDLE *fh; /* open file handle or NULL */
cab_off_t filelen; /* length of cabinet file */
cab_off_t blocks_off; /* offset to data blocks in file */
struct cabinet *prevcab, *nextcab; /* multipart cabinet chains */
char *prevname, *nextname; /* and their filenames */
char *previnfo, *nextinfo; /* and their visible names */
- struct cab_folder *folders; /* first folder in this cabinet */
- struct cab_file *files; /* first file in this cabinet */
+ struct cab_folder *folders; /* first folder in this cabinet */
+ struct cab_file *files; /* first file in this cabinet */
cab_UBYTE block_resv; /* reserved space in datablocks */
cab_UBYTE flags; /* header flags */
};
-struct {
+typedef struct cds_forward {
struct cab_folder *current; /* current folder we're extracting from */
cab_ULONG offset; /* uncompressed offset within folder */
cab_UBYTE *outpos; /* (high level) start of data to use up */
cab_UWORD outlen; /* (high level) amount of data to use up */
cab_UWORD split; /* at which split in current folder? */
- int (*decompress)(int, int); /* the chosen compression func */
- cab_UBYTE inbuf[CAB_INPUTMAX+2]; /* +2 for lzx bitbuffer overflows! */
+ int (*decompress)(int, int, struct cds_forward *); /* chosen compress fn */
+ cab_UBYTE inbuf[CAB_INPUTMAX+2]; /* +2 for lzx bitbuffer overflows! */
cab_UBYTE outbuf[CAB_BLOCKMAX];
+ cab_UBYTE q_length_base[27], q_length_extra[27], q_extra_bits[42];
+ cab_ULONG q_position_base[42];
+ cab_ULONG lzx_position_base[51];
+ cab_UBYTE extra_bits[51];
union {
struct ZIPstate zip;
struct QTMstate qtm;
struct LZXstate lzx;
} methods;
-} decomp_state;
+} cab_decomp_state;
+
+/* _Int as in "Internal" fyi */
+
+typedef struct {
+ unsigned int FCI_Intmagic;
+} FCI_Int, *PFCI_Int;
+
+typedef struct {
+ unsigned int FDI_Intmagic;
+ PFNALLOC pfnalloc;
+ PFNFREE pfnfree;
+ PFNOPEN pfnopen;
+ PFNREAD pfnread;
+ PFNWRITE pfnwrite;
+ PFNCLOSE pfnclose;
+ PFNSEEK pfnseek;
+ PERF perf;
+} FDI_Int, *PFDI_Int;
+
+/* cast an HFCI into a PFCI_Int */
+#define PFCI_INT(hfci) ((PFDI_Int)(hfci))
+
+/* cast an HFDI into a PFDI_Int */
+#define PFDI_INT(hfdi) ((PFDI_Int)(hfdi))
+
+/* quickie pfdi method invokers */
+#define PFDI_ALLOC(hfdi, size) ((*PFDI_INT(hfdi)->pfnalloc) (size))
+#define PFDI_FREE(hfdi, ptr) ((*PFDI_INT(hfdi)->pfnfree) (ptr))
+#define PFDI_OPEN(hfdi, file, flag, mode) ((*PFDI_INT(hfdi)->pfnopen) (file, flag, mode))
+#define PFDI_READ(hfdi, hf, pv, cb) ((*PFDI_INT(hfdi)->pfnread) (hf, pv, cb))
+#define PFDI_WRITE(hfdi, hf, pv, cb) ((*PFDI_INT(hfdi)->pfnwrite) (hf, pv, cb))
+#define PFDI_CLOSE(hfdi, hf) ((*PFDI_INT(hfdi)->pfnclose) (hf))
+#define PFDI_SEEK(hfdi, hf, dist, type) ((*PFDI_INT(hfdi)->pfnseek) (hf, dist, type))
+
+#define FCI_INT_MAGIC 0xfcfcfc05
+#define FDI_INT_MAGIC 0xfdfdfd05
+
+#define REALLY_IS_FCI(hfci) ( \
+ (((void *) hfci) != NULL) && \
+ (PFCI_INT(hfci)->FCI_Intmagic == FCI_INT_MAGIC) )
+
+#define REALLY_IS_FDI(hfdi) ( \
+ (((void *) hfdi) != NULL) && \
+ (PFDI_INT(hfdi)->FDI_Intmagic == FDI_INT_MAGIC) )
+
+/*
+ * the rest of these are somewhat kludgy macros which are shared between fdi.c
+ * and cabextract.c.
+ */
+
+#define ZIPNEEDBITS(n) {while(k<(n)){cab_LONG c=*(ZIP(inpos)++);\
+ b|=((cab_ULONG)c)<<k;k+=8;}}
+#define ZIPDUMPBITS(n) {b>>=(n);k-=(n);}
+
+/* endian-neutral reading of little-endian data */
+#define EndGetI32(a) ((((a)[3])<<24)|(((a)[2])<<16)|(((a)[1])<<8)|((a)[0]))
+#define EndGetI16(a) ((((a)[1])<<8)|((a)[0]))
+
+#define CAB(x) (decomp_state->x)
+#define ZIP(x) (decomp_state->methods.zip.x)
+#define QTM(x) (decomp_state->methods.qtm.x)
+#define LZX(x) (decomp_state->methods.lzx.x)
+#define DECR_OK (0)
+#define DECR_DATAFORMAT (1)
+#define DECR_ILLEGALDATA (2)
+#define DECR_NOMEMORY (3)
+#define DECR_CHECKSUM (4)
+#define DECR_INPUT (5)
+#define DECR_OUTPUT (6)
+#define DECR_USERABORT (7)
+
+/* Bitstream reading macros (Quantum / normal byte order)
+ *
+ * Q_INIT_BITSTREAM should be used first to set up the system
+ * Q_READ_BITS(var,n) takes N bits from the buffer and puts them in var.
+ * unlike LZX, this can loop several times to get the
+ * requisite number of bits.
+ * Q_FILL_BUFFER adds more data to the bit buffer, if there is room
+ * for another 16 bits.
+ * Q_PEEK_BITS(n) extracts (without removing) N bits from the bit
+ * buffer
+ * Q_REMOVE_BITS(n) removes N bits from the bit buffer
+ *
+ * These bit access routines work by using the area beyond the MSB and the
+ * LSB as a free source of zeroes. This avoids having to mask any bits.
+ * So we have to know the bit width of the bitbuffer variable. This is
+ * defined as ULONG_BITS.
+ *
+ * ULONG_BITS should be at least 16 bits. Unlike LZX's Huffman decoding,
+ * Quantum's arithmetic decoding only needs 1 bit at a time, it doesn't
+ * need an assured number. Retrieving larger bitstrings can be done with
+ * multiple reads and fills of the bitbuffer. The code should work fine
+ * for machines where ULONG >= 32 bits.
+ *
+ * Also note that Quantum reads bytes in normal order; LZX is in
+ * little-endian order.
+ */
+
+#define Q_INIT_BITSTREAM do { bitsleft = 0; bitbuf = 0; } while (0)
+
+#define Q_FILL_BUFFER do { \
+ if (bitsleft <= (CAB_ULONG_BITS - 16)) { \
+ bitbuf |= ((inpos[0]<<8)|inpos[1]) << (CAB_ULONG_BITS-16 - bitsleft); \
+ bitsleft += 16; inpos += 2; \
+ } \
+} while (0)
+
+#define Q_PEEK_BITS(n) (bitbuf >> (CAB_ULONG_BITS - (n)))
+#define Q_REMOVE_BITS(n) ((bitbuf <<= (n)), (bitsleft -= (n)))
+
+#define Q_READ_BITS(v,n) do { \
+ (v) = 0; \
+ for (bitsneed = (n); bitsneed; bitsneed -= bitrun) { \
+ Q_FILL_BUFFER; \
+ bitrun = (bitsneed > bitsleft) ? bitsleft : bitsneed; \
+ (v) = ((v) << bitrun) | Q_PEEK_BITS(bitrun); \
+ Q_REMOVE_BITS(bitrun); \
+ } \
+} while (0)
+
+#define Q_MENTRIES(model) (QTM(model).entries)
+#define Q_MSYM(model,symidx) (QTM(model).syms[(symidx)].sym)
+#define Q_MSYMFREQ(model,symidx) (QTM(model).syms[(symidx)].cumfreq)
+
+/* GET_SYMBOL(model, var) fetches the next symbol from the stated model
+ * and puts it in var. it may need to read the bitstream to do this.
+ */
+#define GET_SYMBOL(m, var) do { \
+ range = ((H - L) & 0xFFFF) + 1; \
+ symf = ((((C - L + 1) * Q_MSYMFREQ(m,0)) - 1) / range) & 0xFFFF; \
+ \
+ for (i=1; i < Q_MENTRIES(m); i++) { \
+ if (Q_MSYMFREQ(m,i) <= symf) break; \
+ } \
+ (var) = Q_MSYM(m,i-1); \
+ \
+ range = (H - L) + 1; \
+ H = L + ((Q_MSYMFREQ(m,i-1) * range) / Q_MSYMFREQ(m,0)) - 1; \
+ L = L + ((Q_MSYMFREQ(m,i) * range) / Q_MSYMFREQ(m,0)); \
+ while (1) { \
+ if ((L & 0x8000) != (H & 0x8000)) { \
+ if ((L & 0x4000) && !(H & 0x4000)) { \
+ /* underflow case */ \
+ C ^= 0x4000; L &= 0x3FFF; H |= 0x4000; \
+ } \
+ else break; \
+ } \
+ L <<= 1; H = (H << 1) | 1; \
+ Q_FILL_BUFFER; \
+ C = (C << 1) | Q_PEEK_BITS(1); \
+ Q_REMOVE_BITS(1); \
+ } \
+ \
+ QTMupdatemodel(&(QTM(m)), i); \
+} while (0)
+
+/* Bitstream reading macros (LZX / intel little-endian byte order)
+ *
+ * INIT_BITSTREAM should be used first to set up the system
+ * READ_BITS(var,n) takes N bits from the buffer and puts them in var
+ *
+ * ENSURE_BITS(n) ensures there are at least N bits in the bit buffer.
+ * it can guarantee up to 17 bits (i.e. it can read in
+ * 16 new bits when there is down to 1 bit in the buffer,
+ * and it can read 32 bits when there are 0 bits in the
+ * buffer).
+ * PEEK_BITS(n) extracts (without removing) N bits from the bit buffer
+ * REMOVE_BITS(n) removes N bits from the bit buffer
+ *
+ * These bit access routines work by using the area beyond the MSB and the
+ * LSB as a free source of zeroes. This avoids having to mask any bits.
+ * So we have to know the bit width of the bitbuffer variable.
+ */
+
+#define INIT_BITSTREAM do { bitsleft = 0; bitbuf = 0; } while (0)
+
+/* Quantum reads bytes in normal order; LZX is little-endian order */
+#define ENSURE_BITS(n) \
+ while (bitsleft < (n)) { \
+ bitbuf |= ((inpos[1]<<8)|inpos[0]) << (CAB_ULONG_BITS-16 - bitsleft); \
+ bitsleft += 16; inpos+=2; \
+ }
+
+#define PEEK_BITS(n) (bitbuf >> (CAB_ULONG_BITS - (n)))
+#define REMOVE_BITS(n) ((bitbuf <<= (n)), (bitsleft -= (n)))
+
+#define READ_BITS(v,n) do { \
+ if (n) { \
+ ENSURE_BITS(n); \
+ (v) = PEEK_BITS(n); \
+ REMOVE_BITS(n); \
+ } \
+ else { \
+ (v) = 0; \
+ } \
+} while (0)
+
+/* Huffman macros */
+
+#define TABLEBITS(tbl) (LZX_##tbl##_TABLEBITS)
+#define MAXSYMBOLS(tbl) (LZX_##tbl##_MAXSYMBOLS)
+#define SYMTABLE(tbl) (LZX(tbl##_table))
+#define LENTABLE(tbl) (LZX(tbl##_len))
+
+/* BUILD_TABLE(tablename) builds a huffman lookup table from code lengths.
+ * In reality, it just calls make_decode_table() with the appropriate
+ * values - they're all fixed by some #defines anyway, so there's no point
+ * writing each call out in full by hand.
+ */
+#define BUILD_TABLE(tbl) \
+ if (make_decode_table( \
+ MAXSYMBOLS(tbl), TABLEBITS(tbl), LENTABLE(tbl), SYMTABLE(tbl) \
+ )) { return DECR_ILLEGALDATA; }
+
+/* READ_HUFFSYM(tablename, var) decodes one huffman symbol from the
+ * bitstream using the stated table and puts it in var.
+ */
+#define READ_HUFFSYM(tbl,var) do { \
+ ENSURE_BITS(16); \
+ hufftbl = SYMTABLE(tbl); \
+ if ((i = hufftbl[PEEK_BITS(TABLEBITS(tbl))]) >= MAXSYMBOLS(tbl)) { \
+ j = 1 << (CAB_ULONG_BITS - TABLEBITS(tbl)); \
+ do { \
+ j >>= 1; i <<= 1; i |= (bitbuf & j) ? 1 : 0; \
+ if (!j) { return DECR_ILLEGALDATA; } \
+ } while ((i = hufftbl[i]) >= MAXSYMBOLS(tbl)); \
+ } \
+ j = LENTABLE(tbl)[(var) = i]; \
+ REMOVE_BITS(j); \
+} while (0)
+
+/* READ_LENGTHS(tablename, first, last) reads in code lengths for symbols
+ * first to last in the given table. The code lengths are stored in their
+ * own special LZX way.
+ */
+#define READ_LENGTHS(tbl,first,last,fn) do { \
+ lb.bb = bitbuf; lb.bl = bitsleft; lb.ip = inpos; \
+ if (fn(LENTABLE(tbl),(first),(last),&lb,decomp_state)) { \
+ return DECR_ILLEGALDATA; \
+ } \
+ bitbuf = lb.bb; bitsleft = lb.bl; inpos = lb.ip; \
+} while (0)
+
+/* Tables for deflate from PKZIP's appnote.txt. */
+
+#define THOSE_ZIP_CONSTS \
+static const cab_UBYTE Zipborder[] = /* Order of the bit length code lengths */ \
+{ 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; \
+static const cab_UWORD Zipcplens[] = /* Copy lengths for literal codes 257..285 */ \
+{ 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, \
+ 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; \
+static const cab_UWORD Zipcplext[] = /* Extra bits for literal codes 257..285 */ \
+{ 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, \
+ 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */ \
+static const cab_UWORD Zipcpdist[] = /* Copy offsets for distance codes 0..29 */ \
+{ 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, \
+513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577}; \
+static const cab_UWORD Zipcpdext[] = /* Extra bits for distance codes */ \
+{ 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, \
+10, 11, 11, 12, 12, 13, 13}; \
+/* And'ing with Zipmask[n] masks the lower n bits */ \
+static const cab_UWORD Zipmask[17] = { \
+ 0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff, \
+ 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff \
+}
+
+struct ExtractFileList {
+ LPSTR filename;
+ struct ExtractFileList *next;
+ BOOL unknown; /* always 1L */
+} ;
+
+/* the first parameter of the function extract */
+typedef struct {
+ long result1; /* 0x000 */
+ long unknown1[3]; /* 0x004 */
+ struct ExtractFileList *filelist; /* 0x010 */
+ long filecount; /* 0x014 */
+ long unknown2; /* 0x018 */
+ char directory[0x104]; /* 0x01c */
+ char lastfile[0x20c]; /* 0x120 */
+} EXTRACTdest;
+
/* from cabextract.c */
-BOOL process_cabinet(LPCSTR cabname, LPCSTR dir, BOOL fix, BOOL lower);
+BOOL process_cabinet(LPCSTR cabname, LPCSTR dir, BOOL fix, BOOL lower, EXTRACTdest *dest);
+void QTMupdatemodel(struct QTMmodel *model, int sym);
+int make_decode_table(cab_ULONG nsyms, cab_ULONG nbits, cab_UBYTE *length, cab_UWORD *table);
+cab_ULONG checksum(cab_UBYTE *data, cab_UWORD bytes, cab_ULONG csum);
#endif /* __WINE_CABINET_H */