1 /*
2  * Adapted from Wine fdi.c: File Decompression Interface
3  *
4  * Copyright 2000-2002 Stuart Caie
5  * Copyright 2002 Patrik Stridvall
6  * Copyright 2003 Greg Turner
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22 
23 #ifndef MSZIP_H_
24 # define MSZIP_H_
25 
26 #include <glib.h>
27 
28 #define DECR_ILLEGALDATA -1
29 #define DECR_DATAFORMAT -2
30 #define DECR_NOMEMORY -3
31 #define DECR_OK 1
32 
33 /* Bitstream reading macros (LZX / intel little-endian byte order)
34  *
35  * INIT_BITSTREAM    should be used first to set up the system
36  * READ_BITS(var,n)  takes N bits from the buffer and puts them in var
37  *
38  * ENSURE_BITS(n)    ensures there are at least N bits in the bit buffer.
39  *                   it can guarantee up to 17 bits (i.e. it can read in
40  *                   16 new bits when there is down to 1 bit in the buffer,
41  *                   and it can read 32 bits when there are 0 bits in the
42  *                   buffer).
43  * PEEK_BITS(n)      extracts (without removing) N bits from the bit buffer
44  * REMOVE_BITS(n)    removes N bits from the bit buffer
45  *
46  * These bit access routines work by using the area beyond the MSB and the
47  * LSB as a free source of zeroes. This avoids having to mask any bits.
48  * So we have to know the bit width of the bitbuffer variable.
49  */
50 
51 #define INIT_BITSTREAM do { bitsleft = 0; bitbuf = 0; } while (0)
52 
53 /* Quantum reads bytes in normal order; LZX is little-endian order */
54 #define ENSURE_BITS(n)                                                    \
55   while (bitsleft < (n)) {                                                \
56     bitbuf |= ((inpos[1]<<8)|inpos[0]) << (CAB_ULONG_BITS-16 - bitsleft); \
57     bitsleft += 16; inpos+=2;                                             \
58   }
59 
60 #define PEEK_BITS(n)   (bitbuf >> (CAB_ULONG_BITS - (n)))
61 #define REMOVE_BITS(n) ((bitbuf <<= (n)), (bitsleft -= (n)))
62 
63 #define READ_BITS(v,n) do {                                             \
64   if (n) {                                                              \
65     ENSURE_BITS(n);                                                     \
66     (v) = PEEK_BITS(n);                                                 \
67     REMOVE_BITS(n);                                                     \
68   }                                                                     \
69   else {                                                                \
70     (v) = 0;                                                            \
71   }                                                                     \
72 } while (0)
73 
74 /* Huffman macros */
75 
76 #define TABLEBITS(tbl)   (LZX_##tbl##_TABLEBITS)
77 #define MAXSYMBOLS(tbl)  (LZX_##tbl##_MAXSYMBOLS)
78 #define SYMTABLE(tbl)    (LZX(tbl##_table))
79 #define LENTABLE(tbl)    (LZX(tbl##_len))
80 
81 /* BUILD_TABLE(tablename) builds a huffman lookup table from code lengths.
82  * In reality, it just calls make_decode_table() with the appropriate
83  * values - they're all fixed by some #defines anyway, so there's no point
84  * writing each call out in full by hand.
85  */
86 #define BUILD_TABLE(tbl)                                                \
87   if (make_decode_table(                                                \
88     MAXSYMBOLS(tbl), TABLEBITS(tbl), LENTABLE(tbl), SYMTABLE(tbl)       \
89   )) { return DECR_ILLEGALDATA; }
90 
91 /* READ_HUFFSYM(tablename, var) decodes one huffman symbol from the
92  * bitstream using the stated table and puts it in var.
93  */
94 #define READ_HUFFSYM(tbl,var) do {                                      \
95   ENSURE_BITS(16);                                                      \
96   hufftbl = SYMTABLE(tbl);                                              \
97   if ((i = hufftbl[PEEK_BITS(TABLEBITS(tbl))]) >= MAXSYMBOLS(tbl)) {    \
98     j = 1 << (CAB_ULONG_BITS - TABLEBITS(tbl));                         \
99     do {                                                                \
100       j >>= 1; i <<= 1; i |= (bitbuf & j) ? 1 : 0;                      \
101       if (!j) { return DECR_ILLEGALDATA; }                              \
102     } while ((i = hufftbl[i]) >= MAXSYMBOLS(tbl));                      \
103   }                                                                     \
104   j = LENTABLE(tbl)[(var) = i];                                         \
105   REMOVE_BITS(j);                                                       \
106 } while (0)
107 
108 /* READ_LENGTHS(tablename, first, last) reads in code lengths for symbols
109  * first to last in the given table. The code lengths are stored in their
110  * own special LZX way.
111  */
112 #define READ_LENGTHS(tbl,first,last,fn) do { \
113   lb.bb = bitbuf; lb.bl = bitsleft; lb.ip = inpos; \
114   if (fn(LENTABLE(tbl),(first),(last),&lb,decomp_state)) { \
115     return DECR_ILLEGALDATA; \
116   } \
117   bitbuf = lb.bb; bitsleft = lb.bl; inpos = lb.ip; \
118 } while (0)
119 
120 /* CAB data blocks are <= 32768 bytes in uncompressed form. Uncompressed
121  * blocks have zero growth. MSZIP guarantees that it won't grow above
122  * uncompressed size by more than 12 bytes. LZX guarantees it won't grow
123  * more than 6144 bytes.
124  */
125 #define CAB_BLOCKMAX (32768)
126 #define CAB_INPUTMAX (CAB_BLOCKMAX+6144)
127 
128 typedef guint8        cab_UBYTE; /* 8 bits  */
129 typedef guint16       cab_UWORD; /* 16 bits */
130 typedef guint32       cab_ULONG; /* 32 bits */
131 typedef gint32        cab_LONG;  /* 32 bits */
132 
133 /* number of bits in a ULONG */
134 #ifndef CHAR_BIT
135 # define CHAR_BIT (8)
136 #endif
137 #define CAB_ULONG_BITS (sizeof(cab_ULONG) * CHAR_BIT)
138 
139 /* MSZIP stuff */
140 #define ZIPWSIZE 	0x8000  /* window size */
141 #define ZIPLBITS	9	/* bits in base literal/length lookup table */
142 #define ZIPDBITS	6	/* bits in base distance lookup table */
143 #define ZIPBMAX		16      /* maximum bit length of any code */
144 #define ZIPN_MAX	288     /* maximum number of codes in any set */
145 
146 struct Ziphuft {
147     cab_UBYTE e;                /* number of extra bits or operation */
148     cab_UBYTE b;                /* number of bits in this code or subcode */
149     union {
150         cab_UWORD n;              /* literal, length base, or distance base */
151         struct Ziphuft *t;        /* pointer to next level of table */
152     } v;
153 };
154 struct ZIPstate {
155     cab_ULONG window_posn;      /* current offset within the window        */
156     cab_ULONG bb;               /* bit buffer */
157     cab_ULONG bk;               /* bits in bit buffer */
158     cab_ULONG ll[288+32];       /* literal/length and distance code lengths */
159     cab_ULONG c[ZIPBMAX+1];     /* bit length count table */
160     cab_LONG  lx[ZIPBMAX+1];    /* memory for l[-1..ZIPBMAX-1] */
161     struct Ziphuft *u[ZIPBMAX];	/* table stack */
162     cab_ULONG v[ZIPN_MAX];      /* values in order of bit length */
163     cab_ULONG x[ZIPBMAX+1];     /* bit offsets, then code stack */
164     cab_UBYTE *inpos;
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 typedef struct
227 {
228     gpointer (*alloc) (gsize nbyte);
229     void (*free) (gpointer mem);
230 } FDI_Int;
231 
232 typedef struct fdi_cds_fwd {
233     FDI_Int *fdi;                    /* the hfdi we are using                 */
234     cab_UBYTE *inbuf; /* +2 for lzx bitbuffer overflows!       */
235     cab_UBYTE *outbuf;
236 
237     cab_ULONG lzx_position_base[51];
238     cab_UBYTE extra_bits[51];
239 
240     union {
241         struct ZIPstate zip;
242         struct LZXstate lzx;
243     } methods;
244     int comptype;
245 } fdi_decomp_state;
246 
247 int ZIPfdi_decomp(int inlen, int outlen, fdi_decomp_state *decomp_state);
248 int LZXfdi_decomp(int inlen, int outlen, fdi_decomp_state *decomp_state);
249 int LZXfdi_init(int window, fdi_decomp_state *decomp_state);
250 void LZXfdi_clear(fdi_decomp_state *decomp_state);
251 
252 #endif
253