summaryrefslogtreecommitdiff
path: root/rbutil/rbutilqt/mspack/lzx.h
diff options
context:
space:
mode:
Diffstat (limited to 'rbutil/rbutilqt/mspack/lzx.h')
-rw-r--r--rbutil/rbutilqt/mspack/lzx.h194
1 files changed, 194 insertions, 0 deletions
diff --git a/rbutil/rbutilqt/mspack/lzx.h b/rbutil/rbutilqt/mspack/lzx.h
new file mode 100644
index 0000000000..fc69928c96
--- /dev/null
+++ b/rbutil/rbutilqt/mspack/lzx.h
@@ -0,0 +1,194 @@
1/* This file is part of libmspack.
2 * (C) 2003-2004 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 + 50*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 window_posn; /* decompression offset within window */
59 unsigned int frame_posn; /* current frame offset within in window */
60 unsigned int frame; /* the number of 32kb frames processed */
61 unsigned int reset_interval; /* which frame do we reset the compressor? */
62
63 unsigned int R0, R1, R2; /* for the LRU offset system */
64 unsigned int block_length; /* uncompressed length of this LZX block */
65 unsigned int block_remaining; /* uncompressed bytes still left to decode */
66
67 signed int intel_filesize; /* magic header value used for transform */
68 signed int intel_curpos; /* current offset in transform space */
69
70 unsigned char intel_started; /* has intel E8 decoding started? */
71 unsigned char block_type; /* type of the current block */
72 unsigned char header_read; /* have we started decoding at all yet? */
73 unsigned char posn_slots; /* how many posn slots in stream? */
74 unsigned char input_end; /* have we reached the end of input? */
75
76 int error;
77
78 /* I/O buffering */
79 unsigned char *inbuf, *i_ptr, *i_end, *o_ptr, *o_end;
80 unsigned int bit_buffer, bits_left, inbuf_size;
81
82 /* huffman code lengths */
83 unsigned char PRETREE_len [LZX_PRETREE_MAXSYMBOLS + LZX_LENTABLE_SAFETY];
84 unsigned char MAINTREE_len [LZX_MAINTREE_MAXSYMBOLS + LZX_LENTABLE_SAFETY];
85 unsigned char LENGTH_len [LZX_LENGTH_MAXSYMBOLS + LZX_LENTABLE_SAFETY];
86 unsigned char ALIGNED_len [LZX_ALIGNED_MAXSYMBOLS + LZX_LENTABLE_SAFETY];
87
88 /* huffman decoding tables */
89 unsigned short PRETREE_table [(1 << LZX_PRETREE_TABLEBITS) +
90 (LZX_PRETREE_MAXSYMBOLS * 2)];
91 unsigned short MAINTREE_table[(1 << LZX_MAINTREE_TABLEBITS) +
92 (LZX_MAINTREE_MAXSYMBOLS * 2)];
93 unsigned short LENGTH_table [(1 << LZX_LENGTH_TABLEBITS) +
94 (LZX_LENGTH_MAXSYMBOLS * 2)];
95 unsigned short ALIGNED_table [(1 << LZX_ALIGNED_TABLEBITS) +
96 (LZX_ALIGNED_MAXSYMBOLS * 2)];
97 unsigned char LENGTH_empty;
98
99 /* this is used purely for doing the intel E8 transform */
100 unsigned char e8_buf[LZX_FRAME_SIZE];
101};
102
103/**
104 * Allocates and initialises LZX decompression state for decoding an LZX
105 * stream.
106 *
107 * This routine uses system->alloc() to allocate memory. If memory
108 * allocation fails, or the parameters to this function are invalid,
109 * NULL is returned.
110 *
111 * @param system an mspack_system structure used to read from
112 * the input stream and write to the output
113 * stream, also to allocate and free memory.
114 * @param input an input stream with the LZX data.
115 * @param output an output stream to write the decoded data to.
116 * @param window_bits the size of the decoding window, which must be
117 * between 15 and 21 inclusive.
118 * @param reset_interval the interval at which the LZX bitstream is
119 * reset, in multiples of LZX frames (32678
120 * bytes), e.g. a value of 2 indicates the input
121 * stream resets after every 65536 output bytes.
122 * A value of 0 indicates that the bistream never
123 * resets, such as in CAB LZX streams.
124 * @param input_buffer_size the number of bytes to use as an input
125 * bitstream buffer.
126 * @param output_length the length in bytes of the entirely
127 * decompressed output stream, if known in
128 * advance. It is used to correctly perform the
129 * Intel E8 transformation, which must stop 6
130 * bytes before the very end of the
131 * decompressed stream. It is not otherwise used
132 * or adhered to. If the full decompressed
133 * length is known in advance, set it here.
134 * If it is NOT known, use the value 0, and call
135 * lzxd_set_output_length() once it is
136 * known. If never set, 4 of the final 6 bytes
137 * of the output stream may be incorrect.
138 * @return a pointer to an initialised lzxd_stream structure, or NULL if
139 * there was not enough memory or parameters to the function were wrong.
140 */
141extern struct lzxd_stream *lzxd_init(struct mspack_system *system,
142 struct mspack_file *input,
143 struct mspack_file *output,
144 int window_bits,
145 int reset_interval,
146 int input_buffer_size,
147 off_t output_length);
148
149/* see description of output_length in lzxd_init() */
150extern void lzxd_set_output_length(struct lzxd_stream *lzx,
151 off_t output_length);
152
153/**
154 * Decompresses entire or partial LZX streams.
155 *
156 * The number of bytes of data that should be decompressed is given as the
157 * out_bytes parameter. If more bytes are decoded than are needed, they
158 * will be kept over for a later invocation.
159 *
160 * The output bytes will be passed to the system->write() function given in
161 * lzxd_init(), using the output file handle given in lzxd_init(). More than
162 * one call may be made to system->write().
163
164 * Input bytes will be read in as necessary using the system->read()
165 * function given in lzxd_init(), using the input file handle given in
166 * lzxd_init(). This will continue until system->read() returns 0 bytes,
167 * or an error. Errors will be passed out of the function as
168 * MSPACK_ERR_READ errors. Input streams should convey an "end of input
169 * stream" by refusing to supply all the bytes that LZX asks for when they
170 * reach the end of the stream, rather than return an error code.
171 *
172 * If any error code other than MSPACK_ERR_OK is returned, the stream
173 * should be considered unusable and lzxd_decompress() should not be
174 * called again on this stream.
175 *
176 * @param lzx LZX decompression state, as allocated by lzxd_init().
177 * @param out_bytes the number of bytes of data to decompress.
178 * @return an error code, or MSPACK_ERR_OK if successful
179 */
180extern int lzxd_decompress(struct lzxd_stream *lzx, off_t out_bytes);
181
182/**
183 * Frees all state associated with an LZX data stream. This will call
184 * system->free() using the system pointer given in lzxd_init().
185 *
186 * @param lzx LZX decompression state to free.
187 */
188void lzxd_free(struct lzxd_stream *lzx);
189
190#ifdef __cplusplus
191}
192#endif
193
194#endif