2
* This file includes code from libmspack adapted for libclamav by
5
* Copyright (C) 2003-2004 Stuart Caie
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 version 2.1 as published by the Free Software Foundation.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
25
#include <sys/types.h>
29
/***************************************************************************
30
* MS-ZIP decompression definitions *
31
***************************************************************************/
33
#define MSZIP_FRAME_SIZE (32768) /* size of LZ history window */
34
#define MSZIP_MAX_HUFFBITS (16) /* maximum huffman code length */
35
#define MSZIP_LITERAL_MAXSYMBOLS (288) /* literal/length huffman tree */
36
#define MSZIP_LITERAL_TABLEBITS (9)
37
#define MSZIP_DISTANCE_MAXSYMBOLS (32) /* distance huffman tree */
38
#define MSZIP_DISTANCE_TABLEBITS (6)
40
/* if there are less direct lookup entries than symbols, the longer
41
* code pointers will be <= maxsymbols. This must not happen, or we
42
* will decode entries badly */
43
#if (1 << MSZIP_LITERAL_TABLEBITS) < (MSZIP_LITERAL_MAXSYMBOLS * 2)
44
# define MSZIP_LITERAL_TABLESIZE (MSZIP_LITERAL_MAXSYMBOLS * 4)
46
# define MSZIP_LITERAL_TABLESIZE ((1 << MSZIP_LITERAL_TABLEBITS) + \
47
(MSZIP_LITERAL_MAXSYMBOLS * 2))
50
#if (1 << MSZIP_DISTANCE_TABLEBITS) < (MSZIP_DISTANCE_MAXSYMBOLS * 2)
51
# define MSZIP_DISTANCE_TABLESIZE (MSZIP_DISTANCE_MAXSYMBOLS * 4)
53
# define MSZIP_DISTANCE_TABLESIZE ((1 << MSZIP_DISTANCE_TABLEBITS) + \
54
(MSZIP_DISTANCE_MAXSYMBOLS * 2))
58
int ofd; /* output file descriptor */
60
/* inflate() will call this whenever the window should be emptied. */
61
int (*flush_window)(struct mszip_stream *, unsigned int);
63
int error, repair_mode, bytes_output, input_end;
66
unsigned char *inbuf, *i_ptr, *i_end, *o_ptr, *o_end;
67
unsigned int bit_buffer, bits_left, inbuf_size;
69
unsigned int window_posn; /* offset within window */
71
/* huffman code lengths */
72
unsigned char LITERAL_len[MSZIP_LITERAL_MAXSYMBOLS];
73
unsigned char DISTANCE_len[MSZIP_DISTANCE_MAXSYMBOLS];
75
/* huffman decoding tables */
76
unsigned short LITERAL_table [MSZIP_LITERAL_TABLESIZE];
77
unsigned short DISTANCE_table[MSZIP_DISTANCE_TABLESIZE];
79
/* 32kb history window */
80
unsigned char window[MSZIP_FRAME_SIZE];
82
/* cabinet related stuff */
83
struct cab_file *file;
84
int (*read_cb)(struct cab_file *, unsigned char *, int);
86
unsigned char wflag; /* write flag */
87
unsigned int last; /* prior end of content buffer */
91
struct mszip_stream *mszip_init(int ofd,
92
int input_buffer_size,
94
struct cab_file *file,
95
int (*read_cb)(struct cab_file *, unsigned char *, int));
97
extern int mszip_decompress(struct mszip_stream *zip, uint32_t out_bytes);
99
void mszip_free(struct mszip_stream *zip);
102
/***************************************************************************
103
* Quantum decompression definitions *
104
***************************************************************************/
106
/* Quantum compression / decompression definitions */
108
#define QTM_FRAME_SIZE (32768)
110
struct qtm_modelsym {
111
unsigned short sym, cumfreq;
115
int shiftsleft, entries;
116
struct qtm_modelsym *syms;
120
int ofd; /* output file descriptor */
122
unsigned char *window; /* decoding window */
123
unsigned int window_size; /* window size */
124
unsigned int window_posn; /* decompression offset within window */
125
unsigned int frame_start; /* start of current frame within window */
127
unsigned short H, L, C; /* high/low/current: arith coding state */
128
unsigned char header_read; /* have we started decoding a new frame? */
129
unsigned char wflag; /* write flag */
131
int error, input_end;
134
unsigned int position_base[42];
135
unsigned char extra_bits[42], length_base[27], length_extra[27];
137
/* four literal models, each representing 64 symbols
138
* model0 for literals from 0 to 63 (selector = 0)
139
* model1 for literals from 64 to 127 (selector = 1)
140
* model2 for literals from 128 to 191 (selector = 2)
141
* model3 for literals from 129 to 255 (selector = 3) */
142
struct qtm_model model0, model1, model2, model3;
144
/* three match models.
145
* model4 for match with fixed length of 3 bytes
146
* model5 for match with fixed length of 4 bytes
147
* model6 for variable length match, encoded with model6len model */
148
struct qtm_model model4, model5, model6, model6len;
150
/* selector model. 0-6 to say literal (0,1,2,3) or match (4,5,6) */
151
struct qtm_model model7;
153
/* symbol arrays for all models */
154
struct qtm_modelsym m0sym[64 + 1];
155
struct qtm_modelsym m1sym[64 + 1];
156
struct qtm_modelsym m2sym[64 + 1];
157
struct qtm_modelsym m3sym[64 + 1];
158
struct qtm_modelsym m4sym[24 + 1];
159
struct qtm_modelsym m5sym[36 + 1];
160
struct qtm_modelsym m6sym[42 + 1], m6lsym[27 + 1];
161
struct qtm_modelsym m7sym[7 + 1];
164
unsigned int bit_buffer;
166
/* cabinet related stuff */
167
struct cab_file *file;
168
int (*read_cb)(struct cab_file *, unsigned char *, int);
171
unsigned char *inbuf, *i_ptr, *i_end, *o_ptr, *o_end;
172
unsigned int inbuf_size;
173
unsigned char bits_left;
177
extern struct qtm_stream *qtm_init(int ofd,
179
int input_buffer_size,
180
struct cab_file *file,
181
int (*read_cb)(struct cab_file *, unsigned char *, int));
183
extern int qtm_decompress(struct qtm_stream *qtm, uint32_t out_bytes);
185
void qtm_free(struct qtm_stream *qtm);
187
/***************************************************************************
188
* LZX decompression definitions *
189
***************************************************************************/
191
/* some constants defined by the LZX specification */
192
#define LZX_MIN_MATCH (2)
193
#define LZX_MAX_MATCH (257)
194
#define LZX_NUM_CHARS (256)
195
#define LZX_BLOCKTYPE_INVALID (0) /* also blocktypes 4-7 invalid */
196
#define LZX_BLOCKTYPE_VERBATIM (1)
197
#define LZX_BLOCKTYPE_ALIGNED (2)
198
#define LZX_BLOCKTYPE_UNCOMPRESSED (3)
199
#define LZX_PRETREE_NUM_ELEMENTS (20)
200
#define LZX_ALIGNED_NUM_ELEMENTS (8) /* aligned offset tree #elements */
201
#define LZX_NUM_PRIMARY_LENGTHS (7) /* this one missing from spec! */
202
#define LZX_NUM_SECONDARY_LENGTHS (249) /* length tree #elements */
204
/* LZX huffman defines: tweak tablebits as desired */
205
#define LZX_PRETREE_MAXSYMBOLS (LZX_PRETREE_NUM_ELEMENTS)
206
#define LZX_PRETREE_TABLEBITS (6)
207
#define LZX_MAINTREE_MAXSYMBOLS (LZX_NUM_CHARS + 50*8)
208
#define LZX_MAINTREE_TABLEBITS (12)
209
#define LZX_LENGTH_MAXSYMBOLS (LZX_NUM_SECONDARY_LENGTHS+1)
210
#define LZX_LENGTH_TABLEBITS (12)
211
#define LZX_ALIGNED_MAXSYMBOLS (LZX_ALIGNED_NUM_ELEMENTS)
212
#define LZX_ALIGNED_TABLEBITS (7)
213
#define LZX_LENTABLE_SAFETY (64) /* table decoding overruns are allowed */
215
#define LZX_FRAME_SIZE (32768) /* the size of a frame in LZX */
218
int ofd; /* output file descriptor */
220
off_t offset; /* number of bytes actually output */
221
off_t length; /* overall decompressed length of stream */
223
unsigned char *window; /* decoding window */
224
unsigned int window_size; /* window size */
225
unsigned int window_posn; /* decompression offset within window */
226
unsigned int frame_posn; /* current frame offset within in window */
227
unsigned int frame; /* the number of 32kb frames processed */
228
unsigned int reset_interval; /* which frame do we reset the compressor? */
230
unsigned int R0, R1, R2; /* for the LRU offset system */
231
unsigned int block_length; /* uncompressed length of this LZX block */
232
unsigned int block_remaining; /* uncompressed bytes still left to decode */
234
signed int intel_filesize; /* magic header value used for transform */
235
signed int intel_curpos; /* current offset in transform space */
237
unsigned char intel_started; /* has intel E8 decoding started? */
238
unsigned char block_type; /* type of the current block */
239
unsigned char header_read; /* have we started decoding at all yet? */
240
unsigned char posn_slots; /* how many posn slots in stream? */
245
unsigned char *inbuf, *i_ptr, *i_end, *o_ptr, *o_end;
246
unsigned int bit_buffer, bits_left, inbuf_size;
248
/* huffman code lengths */
249
unsigned char PRETREE_len [LZX_PRETREE_MAXSYMBOLS + LZX_LENTABLE_SAFETY];
250
unsigned char MAINTREE_len [LZX_MAINTREE_MAXSYMBOLS + LZX_LENTABLE_SAFETY];
251
unsigned char LENGTH_len [LZX_LENGTH_MAXSYMBOLS + LZX_LENTABLE_SAFETY];
252
unsigned char ALIGNED_len [LZX_ALIGNED_MAXSYMBOLS + LZX_LENTABLE_SAFETY];
254
/* huffman decoding tables */
255
unsigned short PRETREE_table [(1 << LZX_PRETREE_TABLEBITS) +
256
(LZX_PRETREE_MAXSYMBOLS * 2)];
257
unsigned short MAINTREE_table[(1 << LZX_MAINTREE_TABLEBITS) +
258
(LZX_MAINTREE_MAXSYMBOLS * 2)];
259
unsigned short LENGTH_table [(1 << LZX_LENGTH_TABLEBITS) +
260
(LZX_LENGTH_MAXSYMBOLS * 2)];
261
unsigned short ALIGNED_table [(1 << LZX_ALIGNED_TABLEBITS) +
262
(LZX_ALIGNED_MAXSYMBOLS * 2)];
263
unsigned char input_end; /* have we reached the end of input? */
264
unsigned char wflag; /* write flag */
266
/* this is used purely for doing the intel E8 transform */
267
unsigned char e8_buf[LZX_FRAME_SIZE];
269
unsigned int position_base[51];
271
/* cabinet related stuff */
272
struct cab_file *file;
273
int (*read_cb)(struct cab_file *, unsigned char *, int);
275
unsigned char extra_bits[51];
279
struct lzx_stream *lzx_init(int ofd,
282
int input_buffer_size,
284
struct cab_file *file,
285
int (*read_cb)(struct cab_file *, unsigned char *, int));
287
extern void lzx_set_output_length(struct lzx_stream *lzx,
288
off_t output_length);
290
extern int lzx_decompress(struct lzx_stream *lzx, uint32_t out_bytes);
292
void lzx_free(struct lzx_stream *lzx);