source: project/z3/z3blib.h @ 137

Last change on this file since 137 was 137, checked in by felix winkelmann, 14 years ago

z3 update, ajax-dep. on message-digest

File size: 9.4 KB
Line 
1/*
2 * z3lib (c)1993,2000,2005,2006/GPL,BSD Oskar Schirmer, schirmer@scara.com
3 * combined huffman/lz compression library
4 * now rfc1951 conformant
5 */
6
7#ifndef __Z3BLIB_H__
8#define __Z3BLIB_H__
9
10/* private stuff: */
11
12#define Z3B_DICTBITS 15
13#define Z3B_CODEENDOFBLOCK 256
14#define Z3B_NBLENGTHCODES 29
15#define Z3B_NBCODES (Z3B_CODEENDOFBLOCK+1+Z3B_NBLENGTHCODES)
16#define Z3B_NBCODESPLUSUNUSED (Z3B_NBCODES+2)
17#define Z3B_NBDISTANCECODES 30
18#define Z3B_NBDISTANCECODESPLUSUNUSED (Z3B_NBDISTANCECODES+2)
19#define Z3B_NBCODELENGTHSCODES (16+3)
20#define Z3B_LZMINLENGTH 3
21#define Z3B_LZMAXLENGTH 258
22
23#define Z3B_BTYPE_NC            0
24#define Z3B_BTYPE_FIXED         1
25#define Z3B_BTYPE_DYNAMIC       2
26#define Z3B_NB_BTYPE            3
27
28#ifndef Z3LIB_DECODE_ONLY
29#define Z3BE_HASHBITS   12
30#define Z3BE_HASHSIZE   ((1 << Z3BE_HASHBITS) + (1 << 8))
31#define Z3BE_REF3TOOLARGE_MAX     8
32
33enum z3be_state {
34  z3be_state_init,
35  z3be_state_nc,
36  z3be_state_ncdata,
37  z3be_state_clc,
38  z3be_state_ll,
39  z3be_state_dc,
40  z3be_state_data,
41  z3be_state_distance,
42  z3be_state_distance2,
43  z3be_state_end
44};
45
46struct z3be_histogram {
47  __u32 literal[Z3B_NBCODES];
48  __u32 distance[Z3B_NBDISTANCECODES];
49};
50
51struct z3be_handle {
52  __u8 *data; /* reference into 2nd half of mem */
53  __u32 dsize; /* maximum number of uncompressed data */
54  __u32 dpos; /* current position up to which data is filled */
55  __u32 dshift; /* shift hashtab against uncompressed data */
56  __u32 ipos; /* position of input bytes still to be processed */
57  __u32 pbits;
58  int pbits_nb;
59  enum z3be_state state;
60  __u32 match; /* index of longest match found so far */
61  __u32 mlen; /* length of longest match found so far */
62  __u32 mrunl; /* in case of runlength, length of it minus 1 (2..257), else 0 */
63  __u32 limitwp; /* limit for subsequent put given by last tell */
64  __u32 count;
65  __u32 lcode;
66  struct z3be_handle_huff {
67    __u16 *code;
68    __u8 *size;
69  } ll, dc;
70  struct z3be_histogram histogram;
71  __u32 longermatch;
72  __u32 longermlen;
73  __u32 *histogram3; /* when checking short refs for usability: [8][256] */
74  __u8 runlength;
75  __u8 bfinal;
76  __s16 hashtab[Z3BE_HASHSIZE]; /* hash chains root table */
77  __s16 hashforw[1 << Z3B_DICTBITS]; /* hash chains buffer */
78  __u8 huff_dc_size[Z3B_NBDISTANCECODES];
79  __u16 huff_dc_code[Z3B_NBDISTANCECODES];
80  __u8 huff_ll_size[Z3B_NBCODESPLUSUNUSED];
81  __u16 huff_ll_code[Z3B_NBCODESPLUSUNUSED];
82  __u16 temp_dynh_i[3][Z3B_NBCODESPLUSUNUSED];
83  __u32 temp_dynh_cumul[Z3B_NBCODESPLUSUNUSED-1];
84  __u32 temp_tell_clcnt[Z3B_NBCODELENGTHSCODES];
85  __u8 mem[(2 << Z3B_DICTBITS) + (1 << 10)]; /* must be last field */
86};
87
88struct z3be_weighing {
89  int btype;
90  __u32 wpos; /* number of input bytes taken into account for weighing */
91  __u32 len[Z3B_NB_BTYPE]; /* size expected for the three types of coding */
92  __u32 limit3; /* maximum distance for ref:length=3, longer go literal */
93  /* dh sizes: */
94  __u16 hlit;
95  __u8 hdist;
96  __u8 hclen;
97  /* dh tables: */
98  __u8 codelen_ll[Z3B_NBCODESPLUSUNUSED];
99  __u8 codelen_dc[Z3B_NBDISTANCECODES];
100  __u8 codelen_clc[Z3B_NBCODELENGTHSCODES];
101  struct z3be_histogram histogram;
102};
103#endif /* Z3LIB_DECODE_ONLY */
104
105#ifndef Z3LIB_ENCODE_ONLY
106enum z3bd_state {
107  z3bd_state_init,
108  z3bd_state_nc,
109  z3bd_state_nclen,
110  z3bd_state_ncdata,
111  z3bd_state_dh,
112  z3bd_state_codelengths,
113  z3bd_state_codeliterals,
114  z3bd_state_codeliterals2,
115  z3bd_state_codedistances,
116  z3bd_state_codedistances2,
117  z3bd_state_data,
118  z3bd_state_data1,
119  z3bd_state_literal,
120  z3bd_state_length,
121  z3bd_state_length2,
122  z3bd_state_distance,
123  z3bd_state_distance1,
124  z3bd_state_distance2,
125  z3bd_state_end
126};
127
128struct z3bd_handle {
129  __u32 pbits;
130  int pbits_nb;
131  __u32 din; /* current input position */
132  __u32 dout; /* current output position */
133  enum z3bd_state state;
134  int bfinal;
135  int error;
136  __u16 count;
137  __u32 value;
138  __u32 distance;
139  __u8 clen;
140  __u32 alternate;
141  struct z3bd_handle_huff {
142    __u8 size[1 << 8];
143    __u16 code[1 << 8];
144    __u16 *bit1;
145    __u16 *bit0;
146    __u8 *length;
147  } ll, dc, clc;
148  __u16 huff_ll_bit1[Z3B_NBCODESPLUSUNUSED];
149  __u16 huff_ll_bit0[Z3B_NBCODESPLUSUNUSED];
150  __u16 huff_dc_bit1[Z3B_NBDISTANCECODESPLUSUNUSED];
151  __u16 huff_dc_bit0[Z3B_NBDISTANCECODESPLUSUNUSED];
152  __u16 huff_clc_bit1[Z3B_NBCODELENGTHSCODES];
153  __u16 huff_clc_bit0[Z3B_NBCODELENGTHSCODES];
154  __u8 huff_ll_length[Z3B_NBCODESPLUSUNUSED];
155  __u8 huff_dc_length[Z3B_NBDISTANCECODESPLUSUNUSED];
156  __u8 huff_clc_length[Z3B_NBCODELENGTHSCODES];
157  __u8 data[1 << Z3B_DICTBITS]; /* 32k string ring buffer */
158};
159#endif /* Z3LIB_ENCODE_ONLY */
160
161/* public stuff: */
162
163#ifndef Z3LIB_DECODE_ONLY
164#define Z3BE_MEMSIZE_MIN (sizeof(struct z3be_handle))
165#define Z3BE_MEMSIZE_EXTRA3 (Z3BE_REF3TOOLARGE_MAX*sizeof(__u32)*((1<<8)+2))
166/*
167 * start the compression of one or more rfc1951 blocks
168 * input:
169 *    memory, memsize: memory reserved for the compressor, memsize must
170 *      not be less than Z3BE_MEMSIZE_MIN (plus Z3BE_MEMSIZE_EXTRA3, when
171 *      limitlength3 is non-zero)
172 *    preferlonger: when non-zero, the compressor will try to find a longer
173 *      match at n+1 and prefer it over a previous match at n
174 *    limitlength3: when non-zero, then for codes with length 3 and large
175 *      distance, check whether coding as literal is better
176 * return: handle on success, NULL otherwise
177 */
178struct z3be_handle *z3be_start(void *memory, unsigned int memsize,
179                        int preferlonger, int limitlength3);
180
181/*
182 * fill an amount of data into the compressor
183 * input:
184 *    data, datasize: a number of bytes that are available to compress
185 * return:
186 *    the number of bytes actually taken, in range 0..datasize
187 *    when 0, z3be_tell should be called
188 */
189__u32 z3be_put(struct z3be_handle *zh, __u8 *data, __u32 datasize);
190
191/*
192 * flush the internal compressor pipe
193 * should be called when the last byte has been delivered through z3be_put
194 */
195void z3be_push(struct z3be_handle *zh);
196
197/*
198 * calculate expected codesize for current position
199 * output:
200 *    weighing: description of the weight for the current position
201 *    inpipe: number of bytes currently in the internal compressor pipe
202 * return:
203 *    the number of bytes that will be accepted with subsequent calls
204 *    of z3be_put. note, that anyhow z3be_put might ask to call z3be_tell
205 *    again at several positions.
206 *    when 0, the internal buffer is full and z3be_get should be called
207 */
208__u32 z3be_tell(struct z3be_handle *zh, struct z3be_weighing *weighing,
209                        __u32 *inpipe);
210
211/*
212 * decide for last or previous weighing and get some code
213 * input:
214 *    weighing: the weighing determined by the last or by the
215 *      next to last call to z3be_tell
216 *    codesize: space available in code buffer
217 * output:
218 *    code: a number of compressed bytes are stored hereto
219 * return:
220 *    the number of bytes stored into the code buffer, in range 0..codesize
221 *    when 0, the block is empty and the next block may be started
222 *    using z3be_put
223 */
224__u32 z3be_get(struct z3be_handle *zh, struct z3be_weighing *weighing,
225                        __u8 *code, __u32 codesize);
226
227/*
228 * flush all internal pipes and return last code byte
229 * output:
230 *    code: the last code byte will be stored hereto
231 * return:
232 *    the number of valid bits in the last byte
233 */
234__u32 z3be_finish(struct z3be_handle *zh, __u8 *code);
235#endif /* Z3LIB_DECODE_ONLY */
236
237#ifndef Z3LIB_ENCODE_ONLY
238#define Z3BD_MEMSIZE (sizeof(struct z3bd_handle))
239/*
240 * start the decompression of one or more rfc1951 blocks
241 * input:
242 *    pending_bits, pending_nb: a number of bits possibly left over from
243 *      the previous block; (0, 0) for none
244 *    memory, memsize: memory reserved for the decompressor, memsize shall
245 *      be and must not be less than Z3BD_MEMSIZE
246 * return: handle on success, NULL otherwise
247 */
248struct z3bd_handle *z3bd_start(__u32 pending_bits, int pending_nb,
249                        void *memory, unsigned int memsize);
250
251/*
252 * fill an amount of code into the decompressor
253 * input:
254 *    code, codesize: a number of bytes that are available to decompress
255 * return:
256 *    the number of bytes actually taken, in range 0..codesize
257 *    on error, 0 is returned
258 */
259__u32 z3bd_put(struct z3bd_handle *zh, __u8 *code, __u32 codesize);
260
261/*
262 * pick up decompressed data from the decompressor
263 * output:
264 *    data: a pointer to a number of bytes readily decompressed,
265 *      NULL on error and on end-of-block
266 * return:
267 *    the number of bytes available at **data,
268 *      0 on error and on end-of-block
269 * note:
270 *    possibly there are more bytes available than denoted by the return
271 *    value. thus, this function must be called repeatedly, until it return 0,
272 *    to ensure no data will be lost when calling z3bd_put again
273 */
274__u32 z3bd_get(struct z3bd_handle *zh, __u8 **data);
275
276/*
277 * finish the decompression of an rfc1951 block
278 * output:
279 *    pending_bits, pending_nb: a number of bits left over from this block
280 * return:
281 *    if the last block was decompressed completely and without error and
282 *    it was not the last block, z3err_bd_notbfinal is returned;
283 *    otherwise, the handle is invalidated and the last error code is
284 *    returned (z3err_none for none)
285 * note:
286 *    when this function returns z3err_bd_notbfinal, the user has the choice
287 *    to either start over with the next block without calling z3bd_start,
288 *    or to abort decompression by calling z3bd_finish once again
289 */
290int z3bd_finish(struct z3bd_handle *zh,
291                        __u32 *pending_bits, int *pending_nb);
292#endif /* Z3LIB_ENCODE_ONLY */
293#endif /* __Z3BLIB_H__ */
Note: See TracBrowser for help on using the repository browser.