Debian Codegraph @

src/EWS/addressbook/mspack/lzx.h

raw
1 /* This file is part of libmspack.
2 * (C) 2003-2013 Stuart Caie.
3 *
4 * The LZX method was created by Jonathan Forbes and Tomi Poutanen, adapted
5 * by Microsoft Corporation.
6 *
7 * libmspack is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Lesser General Public License (LGPL) version 2.1
9 *
10 * For further details, see the file COPYING.LIB distributed with libmspack
11 */
12
13 #ifndef LZX_H
14 #define LZX_H 1
15
16 #include <fcntl.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20
21 #define D(x) do { printf("%s:%d (%s) \n",__FILE__, __LINE__, __func__); \
22 printf x ; fputc('\n', stdout); fflush(stdout);} while (0);
23
24 /* LZX compression / decompression definitions */
25
26 /* some constants defined by the LZX specification */
27 #define LZX_MIN_MATCH (2)
28 #define LZX_MAX_MATCH (257)
29 #define LZX_NUM_CHARS (256)
30 #define LZX_BLOCKTYPE_INVALID (0) /* also blocktypes 4-7 invalid */
31 #define LZX_BLOCKTYPE_VERBATIM (1)
32 #define LZX_BLOCKTYPE_ALIGNED (2)
33 #define LZX_BLOCKTYPE_UNCOMPRESSED (3)
34 #define LZX_PRETREE_NUM_ELEMENTS (20)
35 #define LZX_ALIGNED_NUM_ELEMENTS (8) /* aligned offset tree #elements */
36 #define LZX_NUM_PRIMARY_LENGTHS (7) /* this one missing from spec! */
37 #define LZX_NUM_SECONDARY_LENGTHS (249) /* length tree #elements */
38
39 /* LZX huffman defines: tweak tablebits as desired */
40 #define LZX_PRETREE_MAXSYMBOLS (LZX_PRETREE_NUM_ELEMENTS)
41 #define LZX_PRETREE_TABLEBITS (6)
42 #define LZX_MAINTREE_MAXSYMBOLS (LZX_NUM_CHARS + 290*8)
43 #define LZX_MAINTREE_TABLEBITS (12)
44 #define LZX_LENGTH_MAXSYMBOLS (LZX_NUM_SECONDARY_LENGTHS+1)
45 #define LZX_LENGTH_TABLEBITS (12)
46 #define LZX_ALIGNED_MAXSYMBOLS (LZX_ALIGNED_NUM_ELEMENTS)
47 #define LZX_ALIGNED_TABLEBITS (7)
48 #define LZX_LENTABLE_SAFETY (64) /* table decoding overruns are allowed */
49
50 #define LZX_FRAME_SIZE (32768) /* the size of a frame in LZX */
51
52 /* --- error codes --------------------------------------------------------- */
53
54 /** Error code: no error */
55 #define LZX_ERR_OK (0)
56 /** Error code: bad arguments to method */
57 #define LZX_ERR_ARGS (1)
58 /** Error code: error opening file */
59 #define LZX_ERR_OPEN (2)
60 /** Error code: error reading file */
61 #define LZX_ERR_READ (3)
62 /** Error code: error writing file */
63 #define LZX_ERR_WRITE (4)
64 /** Error code: seek error */
65 #define LZX_ERR_SEEK (5)
66 /** Error code: out of memory */
67 #define LZX_ERR_NOMEMORY (6)
68 /** Error code: bad "magic id" in file */
69 #define LZX_ERR_SIGNATURE (7)
70 /** Error code: bad or corrupt file format */
71 #define LZX_ERR_DATAFORMAT (8)
72 /** Error code: bad checksum or CRC */
73 #define LZX_ERR_CHECKSUM (9)
74 /** Error code: error during compression */
75 #define LZX_ERR_CRUNCH (10)
76 /** Error code: error during decompression */
77 #define LZX_ERR_DECRUNCH (11)
78
79 struct lzxd_stream {
80 FILE *input; /* input file handle */
81 FILE *output; /* output file handle */
82
83 off_t offset; /* number of bytes actually output */
84 off_t length; /* overall decompressed length of stream */
85
86 unsigned char *window; /* decoding window */
87 unsigned int window_size; /* window size */
88 unsigned int ref_data_size; /* LZX DELTA reference data size */
89 unsigned int num_offsets; /* number of match_offset entries in table */
90 unsigned int window_posn; /* decompression offset within window */
91 unsigned int frame_posn; /* current frame offset within in window */
92 unsigned int frame; /* the number of 32kb frames processed */
93 unsigned int reset_interval; /* which frame do we reset the compressor? */
94
95 unsigned int R0, R1, R2; /* for the LRU offset system */
96 unsigned int block_length; /* uncompressed length of this LZX block */
97 unsigned int block_remaining; /* uncompressed bytes still left to decode */
98
99 signed int intel_filesize; /* magic header value used for transform */
100 signed int intel_curpos; /* current offset in transform space */
101
102 unsigned char intel_started; /* has intel E8 decoding started? */
103 unsigned char block_type; /* type of the current block */
104 unsigned char header_read; /* have we started decoding at all yet? */
105 unsigned char input_end; /* have we reached the end of input? */
106 unsigned char is_delta; /* does stream follow LZX DELTA spec? */
107
108 int error;
109
110 /* I/O buffering */
111 unsigned char *inbuf, *i_ptr, *i_end, *o_ptr, *o_end;
112 unsigned int bit_buffer, bits_left, inbuf_size;
113
114 /* huffman code lengths */
115 unsigned char PRETREE_len [LZX_PRETREE_MAXSYMBOLS + LZX_LENTABLE_SAFETY];
116 unsigned char MAINTREE_len [LZX_MAINTREE_MAXSYMBOLS + LZX_LENTABLE_SAFETY];
117 unsigned char LENGTH_len [LZX_LENGTH_MAXSYMBOLS + LZX_LENTABLE_SAFETY];
118 unsigned char ALIGNED_len [LZX_ALIGNED_MAXSYMBOLS + LZX_LENTABLE_SAFETY];
119
120 /* huffman decoding tables */
121 unsigned short PRETREE_table [(1 << LZX_PRETREE_TABLEBITS) +
122 (LZX_PRETREE_MAXSYMBOLS * 2)];
123 unsigned short MAINTREE_table[(1 << LZX_MAINTREE_TABLEBITS) +
124 (LZX_MAINTREE_MAXSYMBOLS * 2)];
125 unsigned short LENGTH_table [(1 << LZX_LENGTH_TABLEBITS) +
126 (LZX_LENGTH_MAXSYMBOLS * 2)];
127 unsigned short ALIGNED_table [(1 << LZX_ALIGNED_TABLEBITS) +
128 (LZX_ALIGNED_MAXSYMBOLS * 2)];
129 unsigned char LENGTH_empty;
130
131 /* this is used purely for doing the intel E8 transform */
132 unsigned char e8_buf[LZX_FRAME_SIZE];
133 };
134
135 /**
136 * Allocates and initialises LZX decompression state for decoding an LZX
137 * stream.
138 *
139 * This routine uses malloc() to allocate memory. If memory
140 * allocation fails, or the parameters to this function are invalid,
141 * NULL is returned.
142 *
143 * @param input an input stream with the LZX data.
144 * @param output an output stream to write the decoded data to.
145 * @param window_bits the size of the decoding window, which must be
146 * between 15 and 21 inclusive for regular LZX
147 * data, or between 17 and 25 inclusive for
148 * LZX DELTA data.
149 * @param reset_interval the interval at which the LZX bitstream is
150 * reset, in multiples of LZX frames (32678
151 * bytes), e.g. a value of 2 indicates the input
152 * stream resets after every 65536 output bytes.
153 * A value of 0 indicates that the bitstream never
154 * resets, such as in CAB LZX streams.
155 * @param input_buffer_size the number of bytes to use as an input
156 * bitstream buffer.
157 * @param output_length the length in bytes of the entirely
158 * decompressed output stream, if known in
159 * advance. It is used to correctly perform the
160 * Intel E8 transformation, which must stop 6
161 * bytes before the very end of the
162 * decompressed stream. It is not otherwise used
163 * or adhered to. If the full decompressed
164 * length is known in advance, set it here.
165 * If it is NOT known, use the value 0, and call
166 * lzxd_set_output_length() once it is
167 * known. If never set, 4 of the final 6 bytes
168 * of the output stream may be incorrect.
169 * @param is_delta should be zero for all regular LZX data,
170 * non-zero for LZX DELTA encoded data.
171 * @return a pointer to an initialised lzxd_stream structure, or NULL if
172 * there was not enough memory or parameters to the function were wrong.
173 */
174 extern struct lzxd_stream *ews_lzxd_init(FILE *input,
175 FILE *output,
176 int window_bits,
177 int reset_interval,
178 int input_buffer_size,
179 off_t output_length,
180 char is_delta);
181
182 /* see description of output_length in lzxd_init() */
183 extern void ews_lzxd_set_output_length(struct lzxd_stream *lzx,
184 off_t output_length);
185
186 /**
187 * Reads LZX DELTA reference data into the window and allows
188 * lzxd_decompress() to reference it.
189 *
190 * Call this before the first call to lzxd_decompress().
191
192 * @param lzx the LZX stream to apply this reference data to
193 * @param input an input file handle to read reference data
194 * @param length the length of the reference data. Cannot be longer
195 * than the LZX window size.
196 * @return an error code, or LZX_ERR_OK if successful
197 */
198 extern int ews_lzxd_set_reference_data(struct lzxd_stream *lzx,
199 FILE *input,
200 unsigned int length);
201
202 /**
203 * Decompresses entire or partial LZX streams.
204 *
205 * The number of bytes of data that should be decompressed is given as the
206 * out_bytes parameter. If more bytes are decoded than are needed, they
207 * will be kept over for a later invocation.
208 *
209 * The output bytes will be passed to the write() function given in
210 * lzxd_init(), using the output file handle given in lzxd_init(). More than
211 * one call may be made to write().
212
213 * Input bytes will be read in as necessary using the read()
214 * function given in lzxd_init(), using the input file handle given in
215 * lzxd_init(). This will continue until read() returns 0 bytes,
216 * or an error. Errors will be passed out of the function as
217 * LZX_ERR_READ errors. Input streams should convey an "end of input
218 * stream" by refusing to supply all the bytes that LZX asks for when they
219 * reach the end of the stream, rather than return an error code.
220 *
221 * If any error code other than LZX_ERR_OK is returned, the stream
222 * should be considered unusable and lzxd_decompress() should not be
223 * called again on this stream.
224 *
225 * @param lzx LZX decompression state, as allocated by lzxd_init().
226 * @param out_bytes the number of bytes of data to decompress.
227 * @return an error code, or LZX_ERR_OK if successful
228 */
229 extern int ews_lzxd_decompress(struct lzxd_stream *lzx, off_t out_bytes);
230
231 /**
232 * Frees all state associated with an LZX data stream. This will call
233 * free() using the system pointer given in lzxd_init().
234 *
235 * @param lzx LZX decompression state to free.
236 */
237 void ews_lzxd_free(struct lzxd_stream *lzx);
238
239 #endif