1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
/*
* This file includes code from libmspack adapted for libclamav by
* tkojm@clamav.net
*
* Copyright (C) 2003-2004 Stuart Caie
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
#ifndef __MSPACK_H
#define __MSPACK_H
#include <sys/types.h>
#include "cab.h"
/***************************************************************************
* MS-ZIP decompression definitions *
***************************************************************************/
#define MSZIP_FRAME_SIZE (32768) /* size of LZ history window */
#define MSZIP_MAX_HUFFBITS (16) /* maximum huffman code length */
#define MSZIP_LITERAL_MAXSYMBOLS (288) /* literal/length huffman tree */
#define MSZIP_LITERAL_TABLEBITS (9)
#define MSZIP_DISTANCE_MAXSYMBOLS (32) /* distance huffman tree */
#define MSZIP_DISTANCE_TABLEBITS (6)
/* if there are less direct lookup entries than symbols, the longer
* code pointers will be <= maxsymbols. This must not happen, or we
* will decode entries badly */
#if (1 << MSZIP_LITERAL_TABLEBITS) < (MSZIP_LITERAL_MAXSYMBOLS * 2)
# define MSZIP_LITERAL_TABLESIZE (MSZIP_LITERAL_MAXSYMBOLS * 4)
#else
# define MSZIP_LITERAL_TABLESIZE ((1 << MSZIP_LITERAL_TABLEBITS) + \
(MSZIP_LITERAL_MAXSYMBOLS * 2))
#endif
#if (1 << MSZIP_DISTANCE_TABLEBITS) < (MSZIP_DISTANCE_MAXSYMBOLS * 2)
# define MSZIP_DISTANCE_TABLESIZE (MSZIP_DISTANCE_MAXSYMBOLS * 4)
#else
# define MSZIP_DISTANCE_TABLESIZE ((1 << MSZIP_DISTANCE_TABLEBITS) + \
(MSZIP_DISTANCE_MAXSYMBOLS * 2))
#endif
struct mszip_stream {
int ofd; /* output file descriptor */
/* inflate() will call this whenever the window should be emptied. */
int (*flush_window)(struct mszip_stream *, unsigned int);
int error, repair_mode, bytes_output, input_end;
/* I/O buffering */
unsigned char *inbuf, *i_ptr, *i_end, *o_ptr, *o_end;
unsigned int bit_buffer, bits_left, inbuf_size;
unsigned int window_posn; /* offset within window */
/* huffman code lengths */
unsigned char LITERAL_len[MSZIP_LITERAL_MAXSYMBOLS];
unsigned char DISTANCE_len[MSZIP_DISTANCE_MAXSYMBOLS];
/* huffman decoding tables */
unsigned short LITERAL_table [MSZIP_LITERAL_TABLESIZE];
unsigned short DISTANCE_table[MSZIP_DISTANCE_TABLESIZE];
/* 32kb history window */
unsigned char window[MSZIP_FRAME_SIZE];
/* cabinet related stuff */
struct cab_file *file;
int (*read_cb)(struct cab_file *, unsigned char *, int);
unsigned char wflag; /* write flag */
unsigned int last; /* prior end of content buffer */
};
struct mszip_stream *mszip_init(int ofd,
int input_buffer_size,
int repair_mode,
struct cab_file *file,
int (*read_cb)(struct cab_file *, unsigned char *, int));
extern int mszip_decompress(struct mszip_stream *zip, uint32_t out_bytes);
void mszip_free(struct mszip_stream *zip);
/***************************************************************************
* Quantum decompression definitions *
***************************************************************************/
/* Quantum compression / decompression definitions */
#define QTM_FRAME_SIZE (32768)
struct qtm_modelsym {
unsigned short sym, cumfreq;
};
struct qtm_model {
int shiftsleft, entries;
struct qtm_modelsym *syms;
};
struct qtm_stream {
int ofd; /* output file descriptor */
unsigned char *window; /* decoding window */
unsigned int window_size; /* window size */
unsigned int window_posn; /* decompression offset within window */
unsigned int frame_start; /* start of current frame within window */
unsigned short H, L, C; /* high/low/current: arith coding state */
unsigned char header_read; /* have we started decoding a new frame? */
unsigned char wflag; /* write flag */
int error, input_end;
/* data tables */
unsigned int position_base[42];
unsigned char extra_bits[42], length_base[27], length_extra[27];
/* four literal models, each representing 64 symbols
* model0 for literals from 0 to 63 (selector = 0)
* model1 for literals from 64 to 127 (selector = 1)
* model2 for literals from 128 to 191 (selector = 2)
* model3 for literals from 129 to 255 (selector = 3) */
struct qtm_model model0, model1, model2, model3;
/* three match models.
* model4 for match with fixed length of 3 bytes
* model5 for match with fixed length of 4 bytes
* model6 for variable length match, encoded with model6len model */
struct qtm_model model4, model5, model6, model6len;
/* selector model. 0-6 to say literal (0,1,2,3) or match (4,5,6) */
struct qtm_model model7;
/* symbol arrays for all models */
struct qtm_modelsym m0sym[64 + 1];
struct qtm_modelsym m1sym[64 + 1];
struct qtm_modelsym m2sym[64 + 1];
struct qtm_modelsym m3sym[64 + 1];
struct qtm_modelsym m4sym[24 + 1];
struct qtm_modelsym m5sym[36 + 1];
struct qtm_modelsym m6sym[42 + 1], m6lsym[27 + 1];
struct qtm_modelsym m7sym[7 + 1];
/* I/O buffers - 1*/
unsigned int bit_buffer;
/* cabinet related stuff */
struct cab_file *file;
int (*read_cb)(struct cab_file *, unsigned char *, int);
/* I/O buffers - 2*/
unsigned char *inbuf, *i_ptr, *i_end, *o_ptr, *o_end;
unsigned int inbuf_size;
unsigned char bits_left;
};
extern struct qtm_stream *qtm_init(int ofd,
int window_bits,
int input_buffer_size,
struct cab_file *file,
int (*read_cb)(struct cab_file *, unsigned char *, int));
extern int qtm_decompress(struct qtm_stream *qtm, uint32_t out_bytes);
void qtm_free(struct qtm_stream *qtm);
/***************************************************************************
* LZX decompression definitions *
***************************************************************************/
/* some constants defined by the LZX specification */
#define LZX_MIN_MATCH (2)
#define LZX_MAX_MATCH (257)
#define LZX_NUM_CHARS (256)
#define LZX_BLOCKTYPE_INVALID (0) /* also blocktypes 4-7 invalid */
#define LZX_BLOCKTYPE_VERBATIM (1)
#define LZX_BLOCKTYPE_ALIGNED (2)
#define LZX_BLOCKTYPE_UNCOMPRESSED (3)
#define LZX_PRETREE_NUM_ELEMENTS (20)
#define LZX_ALIGNED_NUM_ELEMENTS (8) /* aligned offset tree #elements */
#define LZX_NUM_PRIMARY_LENGTHS (7) /* this one missing from spec! */
#define LZX_NUM_SECONDARY_LENGTHS (249) /* length tree #elements */
/* LZX huffman defines: tweak tablebits as desired */
#define LZX_PRETREE_MAXSYMBOLS (LZX_PRETREE_NUM_ELEMENTS)
#define LZX_PRETREE_TABLEBITS (6)
#define LZX_MAINTREE_MAXSYMBOLS (LZX_NUM_CHARS + 50*8)
#define LZX_MAINTREE_TABLEBITS (12)
#define LZX_LENGTH_MAXSYMBOLS (LZX_NUM_SECONDARY_LENGTHS+1)
#define LZX_LENGTH_TABLEBITS (12)
#define LZX_ALIGNED_MAXSYMBOLS (LZX_ALIGNED_NUM_ELEMENTS)
#define LZX_ALIGNED_TABLEBITS (7)
#define LZX_LENTABLE_SAFETY (64) /* table decoding overruns are allowed */
#define LZX_FRAME_SIZE (32768) /* the size of a frame in LZX */
struct lzx_stream {
int ofd; /* output file descriptor */
off_t offset; /* number of bytes actually output */
off_t length; /* overall decompressed length of stream */
unsigned char *window; /* decoding window */
unsigned int window_size; /* window size */
unsigned int window_posn; /* decompression offset within window */
unsigned int frame_posn; /* current frame offset within in window */
unsigned int frame; /* the number of 32kb frames processed */
unsigned int reset_interval; /* which frame do we reset the compressor? */
unsigned int R0, R1, R2; /* for the LRU offset system */
unsigned int block_length; /* uncompressed length of this LZX block */
unsigned int block_remaining; /* uncompressed bytes still left to decode */
signed int intel_filesize; /* magic header value used for transform */
signed int intel_curpos; /* current offset in transform space */
unsigned char intel_started; /* has intel E8 decoding started? */
unsigned char block_type; /* type of the current block */
unsigned char header_read; /* have we started decoding at all yet? */
unsigned char posn_slots; /* how many posn slots in stream? */
int error;
/* I/O buffering */
unsigned char *inbuf, *i_ptr, *i_end, *o_ptr, *o_end;
unsigned int bit_buffer, bits_left, inbuf_size;
/* huffman code lengths */
unsigned char PRETREE_len [LZX_PRETREE_MAXSYMBOLS + LZX_LENTABLE_SAFETY];
unsigned char MAINTREE_len [LZX_MAINTREE_MAXSYMBOLS + LZX_LENTABLE_SAFETY];
unsigned char LENGTH_len [LZX_LENGTH_MAXSYMBOLS + LZX_LENTABLE_SAFETY];
unsigned char ALIGNED_len [LZX_ALIGNED_MAXSYMBOLS + LZX_LENTABLE_SAFETY];
/* huffman decoding tables */
unsigned short PRETREE_table [(1 << LZX_PRETREE_TABLEBITS) +
(LZX_PRETREE_MAXSYMBOLS * 2)];
unsigned short MAINTREE_table[(1 << LZX_MAINTREE_TABLEBITS) +
(LZX_MAINTREE_MAXSYMBOLS * 2)];
unsigned short LENGTH_table [(1 << LZX_LENGTH_TABLEBITS) +
(LZX_LENGTH_MAXSYMBOLS * 2)];
unsigned short ALIGNED_table [(1 << LZX_ALIGNED_TABLEBITS) +
(LZX_ALIGNED_MAXSYMBOLS * 2)];
unsigned char input_end; /* have we reached the end of input? */
unsigned char wflag; /* write flag */
/* this is used purely for doing the intel E8 transform */
unsigned char e8_buf[LZX_FRAME_SIZE];
unsigned int position_base[51];
/* cabinet related stuff */
struct cab_file *file;
int (*read_cb)(struct cab_file *, unsigned char *, int);
unsigned char extra_bits[51];
};
struct lzx_stream *lzx_init(int ofd,
int window_bits,
int reset_interval,
int input_buffer_size,
off_t output_length,
struct cab_file *file,
int (*read_cb)(struct cab_file *, unsigned char *, int));
extern void lzx_set_output_length(struct lzx_stream *lzx,
off_t output_length);
extern int lzx_decompress(struct lzx_stream *lzx, uint32_t out_bytes);
void lzx_free(struct lzx_stream *lzx);
#endif
|