xref: /OK3568_Linux_fs/u-boot/fs/jffs2/mini_inflate.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*-------------------------------------------------------------------------
2*4882a593Smuzhiyun  * Filename:      mini_inflate.c
3*4882a593Smuzhiyun  * Version:       $Id: mini_inflate.c,v 1.3 2002/01/24 22:58:42 rfeany Exp $
4*4882a593Smuzhiyun  * Copyright:     Copyright (C) 2001, Russ Dill
5*4882a593Smuzhiyun  * Author:        Russ Dill <Russ.Dill@asu.edu>
6*4882a593Smuzhiyun  * Description:   Mini inflate implementation (RFC 1951)
7*4882a593Smuzhiyun  *-----------------------------------------------------------------------*/
8*4882a593Smuzhiyun /*
9*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
10*4882a593Smuzhiyun  */
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <config.h>
13*4882a593Smuzhiyun #include <jffs2/mini_inflate.h>
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun /* The order that the code lengths in section 3.2.7 are in */
16*4882a593Smuzhiyun static unsigned char huffman_order[] = {16, 17, 18,  0,  8,  7,  9,  6, 10,  5,
17*4882a593Smuzhiyun 					11,  4, 12,  3, 13,  2, 14,  1, 15};
18*4882a593Smuzhiyun 
cramfs_memset(int * s,const int c,size n)19*4882a593Smuzhiyun inline void cramfs_memset(int *s, const int c, size n)
20*4882a593Smuzhiyun {
21*4882a593Smuzhiyun 	n--;
22*4882a593Smuzhiyun 	for (;n > 0; n--) s[n] = c;
23*4882a593Smuzhiyun 	s[0] = c;
24*4882a593Smuzhiyun }
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun /* associate a stream with a block of data and reset the stream */
init_stream(struct bitstream * stream,unsigned char * data,void * (* inflate_memcpy)(void *,const void *,size))27*4882a593Smuzhiyun static void init_stream(struct bitstream *stream, unsigned char *data,
28*4882a593Smuzhiyun 			void *(*inflate_memcpy)(void *, const void *, size))
29*4882a593Smuzhiyun {
30*4882a593Smuzhiyun 	stream->error = NO_ERROR;
31*4882a593Smuzhiyun 	stream->memcpy = inflate_memcpy;
32*4882a593Smuzhiyun 	stream->decoded = 0;
33*4882a593Smuzhiyun 	stream->data = data;
34*4882a593Smuzhiyun 	stream->bit = 0;	/* The first bit of the stream is the lsb of the
35*4882a593Smuzhiyun 				 * first byte */
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun 	/* really sorry about all this initialization, think of a better way,
38*4882a593Smuzhiyun 	 * let me know and it will get cleaned up */
39*4882a593Smuzhiyun 	stream->codes.bits = 8;
40*4882a593Smuzhiyun 	stream->codes.num_symbols = 19;
41*4882a593Smuzhiyun 	stream->codes.lengths = stream->code_lengths;
42*4882a593Smuzhiyun 	stream->codes.symbols = stream->code_symbols;
43*4882a593Smuzhiyun 	stream->codes.count = stream->code_count;
44*4882a593Smuzhiyun 	stream->codes.first = stream->code_first;
45*4882a593Smuzhiyun 	stream->codes.pos = stream->code_pos;
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun 	stream->lengths.bits = 16;
48*4882a593Smuzhiyun 	stream->lengths.num_symbols = 288;
49*4882a593Smuzhiyun 	stream->lengths.lengths = stream->length_lengths;
50*4882a593Smuzhiyun 	stream->lengths.symbols = stream->length_symbols;
51*4882a593Smuzhiyun 	stream->lengths.count = stream->length_count;
52*4882a593Smuzhiyun 	stream->lengths.first = stream->length_first;
53*4882a593Smuzhiyun 	stream->lengths.pos = stream->length_pos;
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun 	stream->distance.bits = 16;
56*4882a593Smuzhiyun 	stream->distance.num_symbols = 32;
57*4882a593Smuzhiyun 	stream->distance.lengths = stream->distance_lengths;
58*4882a593Smuzhiyun 	stream->distance.symbols = stream->distance_symbols;
59*4882a593Smuzhiyun 	stream->distance.count = stream->distance_count;
60*4882a593Smuzhiyun 	stream->distance.first = stream->distance_first;
61*4882a593Smuzhiyun 	stream->distance.pos = stream->distance_pos;
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun /* pull 'bits' bits out of the stream. The last bit pulled it returned as the
66*4882a593Smuzhiyun  * msb. (section 3.1.1)
67*4882a593Smuzhiyun  */
pull_bits(struct bitstream * stream,const unsigned int bits)68*4882a593Smuzhiyun inline unsigned long pull_bits(struct bitstream *stream,
69*4882a593Smuzhiyun 			       const unsigned int bits)
70*4882a593Smuzhiyun {
71*4882a593Smuzhiyun 	unsigned long ret;
72*4882a593Smuzhiyun 	int i;
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	ret = 0;
75*4882a593Smuzhiyun 	for (i = 0; i < bits; i++) {
76*4882a593Smuzhiyun 		ret += ((*(stream->data) >> stream->bit) & 1) << i;
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 		/* if, before incrementing, we are on bit 7,
79*4882a593Smuzhiyun 		 * go to the lsb of the next byte */
80*4882a593Smuzhiyun 		if (stream->bit++ == 7) {
81*4882a593Smuzhiyun 			stream->bit = 0;
82*4882a593Smuzhiyun 			stream->data++;
83*4882a593Smuzhiyun 		}
84*4882a593Smuzhiyun 	}
85*4882a593Smuzhiyun 	return ret;
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun 
pull_bit(struct bitstream * stream)88*4882a593Smuzhiyun inline int pull_bit(struct bitstream *stream)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun 	int ret = ((*(stream->data) >> stream->bit) & 1);
91*4882a593Smuzhiyun 	if (stream->bit++ == 7) {
92*4882a593Smuzhiyun 		stream->bit = 0;
93*4882a593Smuzhiyun 		stream->data++;
94*4882a593Smuzhiyun 	}
95*4882a593Smuzhiyun 	return ret;
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun /* discard bits up to the next whole byte */
discard_bits(struct bitstream * stream)99*4882a593Smuzhiyun static void discard_bits(struct bitstream *stream)
100*4882a593Smuzhiyun {
101*4882a593Smuzhiyun 	if (stream->bit != 0) {
102*4882a593Smuzhiyun 		stream->bit = 0;
103*4882a593Smuzhiyun 		stream->data++;
104*4882a593Smuzhiyun 	}
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun /* No decompression, the data is all literals (section 3.2.4) */
decompress_none(struct bitstream * stream,unsigned char * dest)108*4882a593Smuzhiyun static void decompress_none(struct bitstream *stream, unsigned char *dest)
109*4882a593Smuzhiyun {
110*4882a593Smuzhiyun 	unsigned int length;
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	discard_bits(stream);
113*4882a593Smuzhiyun 	length = *(stream->data++);
114*4882a593Smuzhiyun 	length += *(stream->data++) << 8;
115*4882a593Smuzhiyun 	pull_bits(stream, 16);	/* throw away the inverse of the size */
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	stream->decoded += length;
118*4882a593Smuzhiyun 	stream->memcpy(dest, stream->data, length);
119*4882a593Smuzhiyun 	stream->data += length;
120*4882a593Smuzhiyun }
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun /* Read in a symbol from the stream (section 3.2.2) */
read_symbol(struct bitstream * stream,struct huffman_set * set)123*4882a593Smuzhiyun static int read_symbol(struct bitstream *stream, struct huffman_set *set)
124*4882a593Smuzhiyun {
125*4882a593Smuzhiyun 	int bits = 0;
126*4882a593Smuzhiyun 	int code = 0;
127*4882a593Smuzhiyun 	while (!(set->count[bits] && code < set->first[bits] +
128*4882a593Smuzhiyun 					     set->count[bits])) {
129*4882a593Smuzhiyun 		code = (code << 1) + pull_bit(stream);
130*4882a593Smuzhiyun 		if (++bits > set->bits) {
131*4882a593Smuzhiyun 			/* error decoding (corrupted data?) */
132*4882a593Smuzhiyun 			stream->error = CODE_NOT_FOUND;
133*4882a593Smuzhiyun 			return -1;
134*4882a593Smuzhiyun 		}
135*4882a593Smuzhiyun 	}
136*4882a593Smuzhiyun 	return set->symbols[set->pos[bits] + code - set->first[bits]];
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun /* decompress a stream of data encoded with the passed length and distance
140*4882a593Smuzhiyun  * huffman codes */
decompress_huffman(struct bitstream * stream,unsigned char * dest)141*4882a593Smuzhiyun static void decompress_huffman(struct bitstream *stream, unsigned char *dest)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun 	struct huffman_set *lengths = &(stream->lengths);
144*4882a593Smuzhiyun 	struct huffman_set *distance = &(stream->distance);
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	int symbol, length, dist, i;
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	do {
149*4882a593Smuzhiyun 		if ((symbol = read_symbol(stream, lengths)) < 0) return;
150*4882a593Smuzhiyun 		if (symbol < 256) {
151*4882a593Smuzhiyun 			*(dest++) = symbol; /* symbol is a literal */
152*4882a593Smuzhiyun 			stream->decoded++;
153*4882a593Smuzhiyun 		} else if (symbol > 256) {
154*4882a593Smuzhiyun 			/* Determine the length of the repitition
155*4882a593Smuzhiyun 			 * (section 3.2.5) */
156*4882a593Smuzhiyun 			if (symbol < 265) length = symbol - 254;
157*4882a593Smuzhiyun 			else if (symbol == 285) length = 258;
158*4882a593Smuzhiyun 			else {
159*4882a593Smuzhiyun 				length = pull_bits(stream, (symbol - 261) >> 2);
160*4882a593Smuzhiyun 				length += (4 << ((symbol - 261) >> 2)) + 3;
161*4882a593Smuzhiyun 				length += ((symbol - 1) % 4) <<
162*4882a593Smuzhiyun 					  ((symbol - 261) >> 2);
163*4882a593Smuzhiyun 			}
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 			/* Determine how far back to go */
166*4882a593Smuzhiyun 			if ((symbol = read_symbol(stream, distance)) < 0)
167*4882a593Smuzhiyun 				return;
168*4882a593Smuzhiyun 			if (symbol < 4) dist = symbol + 1;
169*4882a593Smuzhiyun 			else {
170*4882a593Smuzhiyun 				dist = pull_bits(stream, (symbol - 2) >> 1);
171*4882a593Smuzhiyun 				dist += (2 << ((symbol - 2) >> 1)) + 1;
172*4882a593Smuzhiyun 				dist += (symbol % 2) << ((symbol - 2) >> 1);
173*4882a593Smuzhiyun 			}
174*4882a593Smuzhiyun 			stream->decoded += length;
175*4882a593Smuzhiyun 			for (i = 0; i < length; i++) {
176*4882a593Smuzhiyun 				*dest = dest[-dist];
177*4882a593Smuzhiyun 				dest++;
178*4882a593Smuzhiyun 			}
179*4882a593Smuzhiyun 		}
180*4882a593Smuzhiyun 	} while (symbol != 256); /* 256 is the end of the data block */
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun /* Fill the lookup tables (section 3.2.2) */
fill_code_tables(struct huffman_set * set)184*4882a593Smuzhiyun static void fill_code_tables(struct huffman_set *set)
185*4882a593Smuzhiyun {
186*4882a593Smuzhiyun 	int code = 0, i, length;
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	/* fill in the first code of each bit length, and the pos pointer */
189*4882a593Smuzhiyun 	set->pos[0] = 0;
190*4882a593Smuzhiyun 	for (i = 1; i < set->bits; i++) {
191*4882a593Smuzhiyun 		code = (code + set->count[i - 1]) << 1;
192*4882a593Smuzhiyun 		set->first[i] = code;
193*4882a593Smuzhiyun 		set->pos[i] = set->pos[i - 1] + set->count[i - 1];
194*4882a593Smuzhiyun 	}
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 	/* Fill in the table of symbols in order of their huffman code */
197*4882a593Smuzhiyun 	for (i = 0; i < set->num_symbols; i++) {
198*4882a593Smuzhiyun 		if ((length = set->lengths[i]))
199*4882a593Smuzhiyun 			set->symbols[set->pos[length]++] = i;
200*4882a593Smuzhiyun 	}
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	/* reset the pos pointer */
203*4882a593Smuzhiyun 	for (i = 1; i < set->bits; i++) set->pos[i] -= set->count[i];
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun 
init_code_tables(struct huffman_set * set)206*4882a593Smuzhiyun static void init_code_tables(struct huffman_set *set)
207*4882a593Smuzhiyun {
208*4882a593Smuzhiyun 	cramfs_memset(set->lengths, 0, set->num_symbols);
209*4882a593Smuzhiyun 	cramfs_memset(set->count, 0, set->bits);
210*4882a593Smuzhiyun 	cramfs_memset(set->first, 0, set->bits);
211*4882a593Smuzhiyun }
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun /* read in the huffman codes for dynamic decoding (section 3.2.7) */
decompress_dynamic(struct bitstream * stream,unsigned char * dest)214*4882a593Smuzhiyun static void decompress_dynamic(struct bitstream *stream, unsigned char *dest)
215*4882a593Smuzhiyun {
216*4882a593Smuzhiyun 	/* I tried my best to minimize the memory footprint here, while still
217*4882a593Smuzhiyun 	 * keeping up performance. I really dislike the _lengths[] tables, but
218*4882a593Smuzhiyun 	 * I see no way of eliminating them without a sizable performance
219*4882a593Smuzhiyun 	 * impact. The first struct table keeps track of stats on each bit
220*4882a593Smuzhiyun 	 * length. The _length table keeps a record of the bit length of each
221*4882a593Smuzhiyun 	 * symbol. The _symbols table is for looking up symbols by the huffman
222*4882a593Smuzhiyun 	 * code (the pos element points to the first place in the symbol table
223*4882a593Smuzhiyun 	 * where that bit length occurs). I also hate the initization of these
224*4882a593Smuzhiyun 	 * structs, if someone knows how to compact these, lemme know. */
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	struct huffman_set *codes = &(stream->codes);
227*4882a593Smuzhiyun 	struct huffman_set *lengths = &(stream->lengths);
228*4882a593Smuzhiyun 	struct huffman_set *distance = &(stream->distance);
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 	int hlit = pull_bits(stream, 5) + 257;
231*4882a593Smuzhiyun 	int hdist = pull_bits(stream, 5) + 1;
232*4882a593Smuzhiyun 	int hclen = pull_bits(stream, 4) + 4;
233*4882a593Smuzhiyun 	int length, curr_code, symbol, i, last_code;
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	last_code = 0;
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 	init_code_tables(codes);
238*4882a593Smuzhiyun 	init_code_tables(lengths);
239*4882a593Smuzhiyun 	init_code_tables(distance);
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun 	/* fill in the count of each bit length' as well as the lengths
242*4882a593Smuzhiyun 	 * table */
243*4882a593Smuzhiyun 	for (i = 0; i < hclen; i++) {
244*4882a593Smuzhiyun 		length = pull_bits(stream, 3);
245*4882a593Smuzhiyun 		codes->lengths[huffman_order[i]] = length;
246*4882a593Smuzhiyun 		if (length) codes->count[length]++;
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun 	}
249*4882a593Smuzhiyun 	fill_code_tables(codes);
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	/* Do the same for the length codes, being carefull of wrap through
252*4882a593Smuzhiyun 	 * to the distance table */
253*4882a593Smuzhiyun 	curr_code = 0;
254*4882a593Smuzhiyun 	while (curr_code < hlit) {
255*4882a593Smuzhiyun 		if ((symbol = read_symbol(stream, codes)) < 0) return;
256*4882a593Smuzhiyun 		if (symbol == 0) {
257*4882a593Smuzhiyun 			curr_code++;
258*4882a593Smuzhiyun 			last_code = 0;
259*4882a593Smuzhiyun 		} else if (symbol < 16) { /* Literal length */
260*4882a593Smuzhiyun 			lengths->lengths[curr_code] =  last_code = symbol;
261*4882a593Smuzhiyun 			lengths->count[symbol]++;
262*4882a593Smuzhiyun 			curr_code++;
263*4882a593Smuzhiyun 		} else if (symbol == 16) { /* repeat the last symbol 3 - 6
264*4882a593Smuzhiyun 					    * times */
265*4882a593Smuzhiyun 			length = 3 + pull_bits(stream, 2);
266*4882a593Smuzhiyun 			for (;length; length--, curr_code++)
267*4882a593Smuzhiyun 				if (curr_code < hlit) {
268*4882a593Smuzhiyun 					lengths->lengths[curr_code] =
269*4882a593Smuzhiyun 						last_code;
270*4882a593Smuzhiyun 					lengths->count[last_code]++;
271*4882a593Smuzhiyun 				} else { /* wrap to the distance table */
272*4882a593Smuzhiyun 					distance->lengths[curr_code - hlit] =
273*4882a593Smuzhiyun 						last_code;
274*4882a593Smuzhiyun 					distance->count[last_code]++;
275*4882a593Smuzhiyun 				}
276*4882a593Smuzhiyun 		} else if (symbol == 17) { /* repeat a bit length 0 */
277*4882a593Smuzhiyun 			curr_code += 3 + pull_bits(stream, 3);
278*4882a593Smuzhiyun 			last_code = 0;
279*4882a593Smuzhiyun 		} else { /* same, but more times */
280*4882a593Smuzhiyun 			curr_code += 11 + pull_bits(stream, 7);
281*4882a593Smuzhiyun 			last_code = 0;
282*4882a593Smuzhiyun 		}
283*4882a593Smuzhiyun 	}
284*4882a593Smuzhiyun 	fill_code_tables(lengths);
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 	/* Fill the distance table, don't need to worry about wrapthrough
287*4882a593Smuzhiyun 	 * here */
288*4882a593Smuzhiyun 	curr_code -= hlit;
289*4882a593Smuzhiyun 	while (curr_code < hdist) {
290*4882a593Smuzhiyun 		if ((symbol = read_symbol(stream, codes)) < 0) return;
291*4882a593Smuzhiyun 		if (symbol == 0) {
292*4882a593Smuzhiyun 			curr_code++;
293*4882a593Smuzhiyun 			last_code = 0;
294*4882a593Smuzhiyun 		} else if (symbol < 16) {
295*4882a593Smuzhiyun 			distance->lengths[curr_code] = last_code = symbol;
296*4882a593Smuzhiyun 			distance->count[symbol]++;
297*4882a593Smuzhiyun 			curr_code++;
298*4882a593Smuzhiyun 		} else if (symbol == 16) {
299*4882a593Smuzhiyun 			length = 3 + pull_bits(stream, 2);
300*4882a593Smuzhiyun 			for (;length; length--, curr_code++) {
301*4882a593Smuzhiyun 				distance->lengths[curr_code] =
302*4882a593Smuzhiyun 					last_code;
303*4882a593Smuzhiyun 				distance->count[last_code]++;
304*4882a593Smuzhiyun 			}
305*4882a593Smuzhiyun 		} else if (symbol == 17) {
306*4882a593Smuzhiyun 			curr_code += 3 + pull_bits(stream, 3);
307*4882a593Smuzhiyun 			last_code = 0;
308*4882a593Smuzhiyun 		} else {
309*4882a593Smuzhiyun 			curr_code += 11 + pull_bits(stream, 7);
310*4882a593Smuzhiyun 			last_code = 0;
311*4882a593Smuzhiyun 		}
312*4882a593Smuzhiyun 	}
313*4882a593Smuzhiyun 	fill_code_tables(distance);
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun 	decompress_huffman(stream, dest);
316*4882a593Smuzhiyun }
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun /* fill in the length and distance huffman codes for fixed encoding
319*4882a593Smuzhiyun  * (section 3.2.6) */
decompress_fixed(struct bitstream * stream,unsigned char * dest)320*4882a593Smuzhiyun static void decompress_fixed(struct bitstream *stream, unsigned char *dest)
321*4882a593Smuzhiyun {
322*4882a593Smuzhiyun 	/* let gcc fill in the initial values */
323*4882a593Smuzhiyun 	struct huffman_set *lengths = &(stream->lengths);
324*4882a593Smuzhiyun 	struct huffman_set *distance = &(stream->distance);
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 	cramfs_memset(lengths->count, 0, 16);
327*4882a593Smuzhiyun 	cramfs_memset(lengths->first, 0, 16);
328*4882a593Smuzhiyun 	cramfs_memset(lengths->lengths, 8, 144);
329*4882a593Smuzhiyun 	cramfs_memset(lengths->lengths + 144, 9, 112);
330*4882a593Smuzhiyun 	cramfs_memset(lengths->lengths + 256, 7, 24);
331*4882a593Smuzhiyun 	cramfs_memset(lengths->lengths + 280, 8, 8);
332*4882a593Smuzhiyun 	lengths->count[7] = 24;
333*4882a593Smuzhiyun 	lengths->count[8] = 152;
334*4882a593Smuzhiyun 	lengths->count[9] = 112;
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun 	cramfs_memset(distance->count, 0, 16);
337*4882a593Smuzhiyun 	cramfs_memset(distance->first, 0, 16);
338*4882a593Smuzhiyun 	cramfs_memset(distance->lengths, 5, 32);
339*4882a593Smuzhiyun 	distance->count[5] = 32;
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun 	fill_code_tables(lengths);
343*4882a593Smuzhiyun 	fill_code_tables(distance);
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun 	decompress_huffman(stream, dest);
347*4882a593Smuzhiyun }
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun /* returns the number of bytes decoded, < 0 if there was an error. Note that
350*4882a593Smuzhiyun  * this function assumes that the block starts on a byte boundry
351*4882a593Smuzhiyun  * (non-compliant, but I don't see where this would happen). section 3.2.3 */
decompress_block(unsigned char * dest,unsigned char * source,void * (* inflate_memcpy)(void *,const void *,size))352*4882a593Smuzhiyun long decompress_block(unsigned char *dest, unsigned char *source,
353*4882a593Smuzhiyun 		      void *(*inflate_memcpy)(void *, const void *, size))
354*4882a593Smuzhiyun {
355*4882a593Smuzhiyun 	int bfinal, btype;
356*4882a593Smuzhiyun 	struct bitstream stream;
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun 	init_stream(&stream, source, inflate_memcpy);
359*4882a593Smuzhiyun 	do {
360*4882a593Smuzhiyun 		bfinal = pull_bit(&stream);
361*4882a593Smuzhiyun 		btype = pull_bits(&stream, 2);
362*4882a593Smuzhiyun 		if (btype == NO_COMP) decompress_none(&stream, dest + stream.decoded);
363*4882a593Smuzhiyun 		else if (btype == DYNAMIC_COMP)
364*4882a593Smuzhiyun 			decompress_dynamic(&stream, dest + stream.decoded);
365*4882a593Smuzhiyun 		else if (btype == FIXED_COMP) decompress_fixed(&stream, dest + stream.decoded);
366*4882a593Smuzhiyun 		else stream.error = COMP_UNKNOWN;
367*4882a593Smuzhiyun 	} while (!bfinal && !stream.error);
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun #if 0
370*4882a593Smuzhiyun 	putstr("decompress_block start\r\n");
371*4882a593Smuzhiyun 	putLabeledWord("stream.error = ",stream.error);
372*4882a593Smuzhiyun 	putLabeledWord("stream.decoded = ",stream.decoded);
373*4882a593Smuzhiyun 	putLabeledWord("dest = ",dest);
374*4882a593Smuzhiyun 	putstr("decompress_block end\r\n");
375*4882a593Smuzhiyun #endif
376*4882a593Smuzhiyun 	return stream.error ? -stream.error : stream.decoded;
377*4882a593Smuzhiyun }
378