summaryrefslogtreecommitdiff
path: root/utils/rbutilqt/mspack/lzx.h
diff options
context:
space:
mode:
Diffstat (limited to 'utils/rbutilqt/mspack/lzx.h')
-rw-r--r--utils/rbutilqt/mspack/lzx.h221
1 files changed, 221 insertions, 0 deletions
diff --git a/utils/rbutilqt/mspack/lzx.h b/utils/rbutilqt/mspack/lzx.h
new file mode 100644
index 0000000000..a6152f622b
--- /dev/null
+++ b/utils/rbutilqt/mspack/lzx.h
@@ -0,0 +1,221 @@
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 MSPACK_LZX_H
14#define MSPACK_LZX_H 1
15
16#ifdef __cplusplus
17extern "C" {
18#endif
19
20/* LZX compression / decompression definitions */
21
22/* some constants defined by the LZX specification */
23#define LZX_MIN_MATCH (2)
24#define LZX_MAX_MATCH (257)
25#define LZX_NUM_CHARS (256)
26#define LZX_BLOCKTYPE_INVALID (0) /* also blocktypes 4-7 invalid */
27#define LZX_BLOCKTYPE_VERBATIM (1)
28#define LZX_BLOCKTYPE_ALIGNED (2)
29#define LZX_BLOCKTYPE_UNCOMPRESSED (3)
30#define LZX_PRETREE_NUM_ELEMENTS (20)
31#define LZX_ALIGNED_NUM_ELEMENTS (8) /* aligned offset tree #elements */
32#define LZX_NUM_PRIMARY_LENGTHS (7) /* this one missing from spec! */
33#define LZX_NUM_SECONDARY_LENGTHS (249) /* length tree #elements */
34
35/* LZX huffman defines: tweak tablebits as desired */
36#define LZX_PRETREE_MAXSYMBOLS (LZX_PRETREE_NUM_ELEMENTS)
37#define LZX_PRETREE_TABLEBITS (6)
38#define LZX_MAINTREE_MAXSYMBOLS (LZX_NUM_CHARS + 290*8)
39#define LZX_MAINTREE_TABLEBITS (12)
40#define LZX_LENGTH_MAXSYMBOLS (LZX_NUM_SECONDARY_LENGTHS+1)
41#define LZX_LENGTH_TABLEBITS (12)
42#define LZX_ALIGNED_MAXSYMBOLS (LZX_ALIGNED_NUM_ELEMENTS)
43#define LZX_ALIGNED_TABLEBITS (7)
44#define LZX_LENTABLE_SAFETY (64) /* table decoding overruns are allowed */
45
46#define LZX_FRAME_SIZE (32768) /* the size of a frame in LZX */
47
48struct lzxd_stream {
49 struct mspack_system *sys; /* I/O routines */
50 struct mspack_file *input; /* input file handle */
51 struct mspack_file *output; /* output file handle */
52
53 off_t offset; /* number of bytes actually output */
54 off_t length; /* overall decompressed length of stream */
55
56 unsigned char *window; /* decoding window */
57 unsigned int window_size; /* window size */
58 unsigned int ref_data_size; /* LZX DELTA reference data size */
59 unsigned int num_offsets; /* number of match_offset entries in table */
60 unsigned int window_posn; /* decompression offset within window */
61 unsigned int frame_posn; /* current frame offset within in window */
62 unsigned int frame; /* the number of 32kb frames processed */
63 unsigned int reset_interval; /* which frame do we reset the compressor? */
64
65 unsigned int R0, R1, R2; /* for the LRU offset system */
66 unsigned int block_length; /* uncompressed length of this LZX block */
67 unsigned int block_remaining; /* uncompressed bytes still left to decode */
68
69 signed int intel_filesize; /* magic header value used for transform */
70 signed int intel_curpos; /* current offset in transform space */
71
72 unsigned char intel_started; /* has intel E8 decoding started? */
73 unsigned char block_type; /* type of the current block */
74 unsigned char header_read; /* have we started decoding at all yet? */
75 unsigned char input_end; /* have we reached the end of input? */
76 unsigned char is_delta; /* does stream follow LZX DELTA spec? */
77
78 int error;
79
80 /* I/O buffering */
81 unsigned char *inbuf, *i_ptr, *i_end, *o_ptr, *o_end;
82 unsigned int bit_buffer, bits_left, inbuf_size;
83
84 /* huffman code lengths */
85 unsigned char PRETREE_len [LZX_PRETREE_MAXSYMBOLS + LZX_LENTABLE_SAFETY];
86 unsigned char MAINTREE_len [LZX_MAINTREE_MAXSYMBOLS + LZX_LENTABLE_SAFETY];
87 unsigned char LENGTH_len [LZX_LENGTH_MAXSYMBOLS + LZX_LENTABLE_SAFETY];
88 unsigned char ALIGNED_len [LZX_ALIGNED_MAXSYMBOLS + LZX_LENTABLE_SAFETY];
89
90 /* huffman decoding tables */
91 unsigned short PRETREE_table [(1 << LZX_PRETREE_TABLEBITS) +
92 (LZX_PRETREE_MAXSYMBOLS * 2)];
93 unsigned short MAINTREE_table[(1 << LZX_MAINTREE_TABLEBITS) +
94 (LZX_MAINTREE_MAXSYMBOLS * 2)];
95 unsigned short LENGTH_table [(1 << LZX_LENGTH_TABLEBITS) +
96 (LZX_LENGTH_MAXSYMBOLS * 2)];
97 unsigned short ALIGNED_table [(1 << LZX_ALIGNED_TABLEBITS) +
98 (LZX_ALIGNED_MAXSYMBOLS * 2)];
99 unsigned char LENGTH_empty;
100
101 /* this is used purely for doing the intel E8 transform */
102 unsigned char e8_buf[LZX_FRAME_SIZE];
103};
104
105/**
106 * Allocates and initialises LZX decompression state for decoding an LZX
107 * stream.
108 *
109 * This routine uses system->alloc() to allocate memory. If memory
110 * allocation fails, or the parameters to this function are invalid,
111 * NULL is returned.
112 *
113 * @param system an mspack_system structure used to read from
114 * the input stream and write to the output
115 * stream, also to allocate and free memory.
116 * @param input an input stream with the LZX data.
117 * @param output an output stream to write the decoded data to.
118 * @param window_bits the size of the decoding window, which must be
119 * between 15 and 21 inclusive for regular LZX
120 * data, or between 17 and 25 inclusive for
121 * LZX DELTA data.
122 * @param reset_interval the interval at which the LZX bitstream is
123 * reset, in multiples of LZX frames (32678
124 * bytes), e.g. a value of 2 indicates the input
125 * stream resets after every 65536 output bytes.
126 * A value of 0 indicates that the bitstream never
127 * resets, such as in CAB LZX streams.
128 * @param input_buffer_size the number of bytes to use as an input
129 * bitstream buffer.
130 * @param output_length the length in bytes of the entirely
131 * decompressed output stream, if known in
132 * advance. It is used to correctly perform the
133 * Intel E8 transformation, which must stop 6
134 * bytes before the very end of the
135 * decompressed stream. It is not otherwise used
136 * or adhered to. If the full decompressed
137 * length is known in advance, set it here.
138 * If it is NOT known, use the value 0, and call
139 * lzxd_set_output_length() once it is
140 * known. If never set, 4 of the final 6 bytes
141 * of the output stream may be incorrect.
142 * @param is_delta should be zero for all regular LZX data,
143 * non-zero for LZX DELTA encoded data.
144 * @return a pointer to an initialised lzxd_stream structure, or NULL if
145 * there was not enough memory or parameters to the function were wrong.
146 */
147extern struct lzxd_stream *lzxd_init(struct mspack_system *system,
148 struct mspack_file *input,
149 struct mspack_file *output,
150 int window_bits,
151 int reset_interval,
152 int input_buffer_size,
153 off_t output_length,
154 char is_delta);
155
156/* see description of output_length in lzxd_init() */
157extern void lzxd_set_output_length(struct lzxd_stream *lzx,
158 off_t output_length);
159
160/**
161 * Reads LZX DELTA reference data into the window and allows
162 * lzxd_decompress() to reference it.
163 *
164 * Call this before the first call to lzxd_decompress().
165
166 * @param lzx the LZX stream to apply this reference data to
167 * @param system an mspack_system implementation to use with the
168 * input param. Only read() will be called.
169 * @param input an input file handle to read reference data using
170 * system->read().
171 * @param length the length of the reference data. Cannot be longer
172 * than the LZX window size.
173 * @return an error code, or MSPACK_ERR_OK if successful
174 */
175extern int lzxd_set_reference_data(struct lzxd_stream *lzx,
176 struct mspack_system *system,
177 struct mspack_file *input,
178 unsigned int length);
179
180/**
181 * Decompresses entire or partial LZX streams.
182 *
183 * The number of bytes of data that should be decompressed is given as the
184 * out_bytes parameter. If more bytes are decoded than are needed, they
185 * will be kept over for a later invocation.
186 *
187 * The output bytes will be passed to the system->write() function given in
188 * lzxd_init(), using the output file handle given in lzxd_init(). More than
189 * one call may be made to system->write().
190
191 * Input bytes will be read in as necessary using the system->read()
192 * function given in lzxd_init(), using the input file handle given in
193 * lzxd_init(). This will continue until system->read() returns 0 bytes,
194 * or an error. Errors will be passed out of the function as
195 * MSPACK_ERR_READ errors. Input streams should convey an "end of input
196 * stream" by refusing to supply all the bytes that LZX asks for when they
197 * reach the end of the stream, rather than return an error code.
198 *
199 * If any error code other than MSPACK_ERR_OK is returned, the stream
200 * should be considered unusable and lzxd_decompress() should not be
201 * called again on this stream.
202 *
203 * @param lzx LZX decompression state, as allocated by lzxd_init().
204 * @param out_bytes the number of bytes of data to decompress.
205 * @return an error code, or MSPACK_ERR_OK if successful
206 */
207extern int lzxd_decompress(struct lzxd_stream *lzx, off_t out_bytes);
208
209/**
210 * Frees all state associated with an LZX data stream. This will call
211 * system->free() using the system pointer given in lzxd_init().
212 *
213 * @param lzx LZX decompression state to free.
214 */
215void lzxd_free(struct lzxd_stream *lzx);
216
217#ifdef __cplusplus
218}
219#endif
220
221#endif