Fixed binary search: no more infinite loops when vendor is unknown.
[tangerine.git] / compiler / libjpeg / main / jdhuff.c
blob6f2eb944cb2e2cc999aedb669265b249d0639c94
1 /*
2 $Id$
3 */
5 /*
6 * jdhuff.c
8 * Copyright (C) 1991-1998, Thomas G. Lane.
9 * This file is part of the Independent JPEG Group's software.
10 * For conditions of distribution and use, see the accompanying README file.
12 * This file contains Huffman entropy decoding routines which are shared
13 * by the sequential, progressive and lossless decoders.
16 #define JPEG_INTERNALS
17 #include "jinclude.h"
18 #include "jpeglib.h"
19 #include "jlossy.h" /* Private declarations for lossy codec */
20 #include "jlossls.h" /* Private declarations for lossless codec */
21 #include "jdhuff.h" /* Declarations shared with jd*huff.c */
25 * Compute the derived values for a Huffman table.
26 * This routine also performs some validation checks on the table.
29 JGLOBAL(void)
30 jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno,
31 d_derived_tbl ** pdtbl)
33 JHUFF_TBL *htbl;
34 d_derived_tbl *dtbl;
35 int p, i, l, si, numsymbols;
36 int lookbits, ctr;
37 char huffsize[257];
38 unsigned int huffcode[257];
39 unsigned int code;
41 /* Note that huffsize[] and huffcode[] are filled in code-length order,
42 * paralleling the order of the symbols themselves in htbl->huffval[].
45 /* Find the input Huffman table */
46 if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
47 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
48 htbl =
49 isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
50 if (htbl == NULL)
51 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
53 /* Allocate a workspace if we haven't already done so. */
54 if (*pdtbl == NULL)
55 *pdtbl = (d_derived_tbl *)
56 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
57 SIZEOF(d_derived_tbl));
58 dtbl = *pdtbl;
59 dtbl->pub = htbl; /* fill in back link */
61 /* Figure C.1: make table of Huffman code length for each symbol */
63 p = 0;
64 for (l = 1; l <= 16; l++) {
65 i = (int) htbl->bits[l];
66 if (i < 0 || p + i > 256) /* protect against table overrun */
67 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
68 while (i--)
69 huffsize[p++] = (char) l;
71 huffsize[p] = 0;
72 numsymbols = p;
74 /* Figure C.2: generate the codes themselves */
75 /* We also validate that the counts represent a legal Huffman code tree. */
77 code = 0;
78 si = huffsize[0];
79 p = 0;
80 while (huffsize[p]) {
81 while (((int) huffsize[p]) == si) {
82 huffcode[p++] = code;
83 code++;
85 /* code is now 1 more than the last code used for codelength si; but
86 * it must still fit in si bits, since no code is allowed to be all ones.
88 if (((INT32) code) >= (((INT32) 1) << si))
89 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
90 code <<= 1;
91 si++;
94 /* Figure F.15: generate decoding tables for bit-sequential decoding */
96 p = 0;
97 for (l = 1; l <= 16; l++) {
98 if (htbl->bits[l]) {
99 /* valoffset[l] = huffval[] index of 1st symbol of code length l,
100 * minus the minimum code of length l
102 dtbl->valoffset[l] = (INT32) p - (INT32) huffcode[p];
103 p += htbl->bits[l];
104 dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */
105 } else {
106 dtbl->maxcode[l] = -1; /* -1 if no codes of this length */
109 dtbl->maxcode[17] = 0xFFFFFL; /* ensures jpeg_huff_decode terminates */
111 /* Compute lookahead tables to speed up decoding.
112 * First we set all the table entries to 0, indicating "too long";
113 * then we iterate through the Huffman codes that are short enough and
114 * fill in all the entries that correspond to bit sequences starting
115 * with that code.
118 MEMZERO(dtbl->look_nbits, SIZEOF(dtbl->look_nbits));
120 p = 0;
121 for (l = 1; l <= HUFF_LOOKAHEAD; l++) {
122 for (i = 1; i <= (int) htbl->bits[l]; i++, p++) {
123 /* l = current code's length, p = its index in huffcode[] & huffval[]. */
124 /* Generate left-justified code followed by all possible bit sequences */
125 lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l);
126 for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--) {
127 dtbl->look_nbits[lookbits] = l;
128 dtbl->look_sym[lookbits] = htbl->huffval[p];
129 lookbits++;
134 /* Validate symbols as being reasonable.
135 * For AC tables, we make no check, but accept all byte values 0..255.
136 * For DC tables, we require the symbols to be in range 0..16.
137 * (Tighter bounds could be applied depending on the data depth and mode,
138 * but this is sufficient to ensure safe decoding.)
140 if (isDC) {
141 for (i = 0; i < numsymbols; i++) {
142 int sym = htbl->huffval[i];
143 if (sym < 0 || sym > 16)
144 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
151 * Out-of-line code for bit fetching.
152 * See jdhuff.h for info about usage.
153 * Note: current values of get_buffer and bits_left are passed as parameters,
154 * but are returned in the corresponding fields of the state struct.
156 * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width
157 * of get_buffer to be used. (On machines with wider words, an even larger
158 * buffer could be used.) However, on some machines 32-bit shifts are
159 * quite slow and take time proportional to the number of places shifted.
160 * (This is true with most PC compilers, for instance.) In this case it may
161 * be a win to set MIN_GET_BITS to the minimum value of 15. This reduces the
162 * average shift distance at the cost of more calls to jpeg_fill_bit_buffer.
165 #ifdef SLOW_SHIFT_32
166 #define MIN_GET_BITS 15 /* minimum allowable value */
167 #else
168 #define MIN_GET_BITS (BIT_BUF_SIZE-7)
169 #endif
172 JGLOBAL(boolean)
173 jpeg_fill_bit_buffer (bitread_working_state * state,
174 register bit_buf_type get_buffer, register int bits_left,
175 int nbits)
176 /* Load up the bit buffer to a depth of at least nbits */
178 /* Copy heavily used state fields into locals (hopefully registers) */
179 register const JOCTET * next_input_byte = state->next_input_byte;
180 register size_t bytes_in_buffer = state->bytes_in_buffer;
181 j_decompress_ptr cinfo = state->cinfo;
183 /* Attempt to load at least MIN_GET_BITS bits into get_buffer. */
184 /* (It is assumed that no request will be for more than that many bits.) */
185 /* We fail to do so only if we hit a marker or are forced to suspend. */
187 if (cinfo->unread_marker == 0) { /* cannot advance past a marker */
188 while (bits_left < MIN_GET_BITS) {
189 register int c;
191 /* Attempt to read a byte */
192 if (bytes_in_buffer == 0) {
193 if (! (*cinfo->src->fill_input_buffer) (cinfo))
194 return FALSE;
195 next_input_byte = cinfo->src->next_input_byte;
196 bytes_in_buffer = cinfo->src->bytes_in_buffer;
198 bytes_in_buffer--;
199 c = GETJOCTET(*next_input_byte++);
201 /* If it's 0xFF, check and discard stuffed zero byte */
202 if (c == 0xFF) {
203 /* Loop here to discard any padding FF's on terminating marker,
204 * so that we can save a valid unread_marker value. NOTE: we will
205 * accept multiple FF's followed by a 0 as meaning a single FF data
206 * byte. This data pattern is not valid according to the standard.
208 do {
209 if (bytes_in_buffer == 0) {
210 if (! (*cinfo->src->fill_input_buffer) (cinfo))
211 return FALSE;
212 next_input_byte = cinfo->src->next_input_byte;
213 bytes_in_buffer = cinfo->src->bytes_in_buffer;
215 bytes_in_buffer--;
216 c = GETJOCTET(*next_input_byte++);
217 } while (c == 0xFF);
219 if (c == 0) {
220 /* Found FF/00, which represents an FF data byte */
221 c = 0xFF;
222 } else {
223 /* Oops, it's actually a marker indicating end of compressed data.
224 * Save the marker code for later use.
225 * Fine point: it might appear that we should save the marker into
226 * bitread working state, not straight into permanent state. But
227 * once we have hit a marker, we cannot need to suspend within the
228 * current MCU, because we will read no more bytes from the data
229 * source. So it is OK to update permanent state right away.
231 cinfo->unread_marker = c;
232 /* See if we need to insert some fake zero bits. */
233 goto no_more_bytes;
237 /* OK, load c into get_buffer */
238 get_buffer = (get_buffer << 8) | c;
239 bits_left += 8;
240 } /* end while */
241 } else {
242 no_more_bytes:
243 /* We get here if we've read the marker that terminates the compressed
244 * data segment. There should be enough bits in the buffer register
245 * to satisfy the request; if so, no problem.
247 if (nbits > bits_left) {
248 /* Uh-oh. Report corrupted data to user and stuff zeroes into
249 * the data stream, so that we can produce some kind of image.
250 * We use a nonvolatile flag to ensure that only one warning message
251 * appears per data segment.
253 huffd_common_ptr huffd;
254 if (cinfo->process == JPROC_LOSSLESS)
255 huffd = (huffd_common_ptr) ((j_lossless_d_ptr) cinfo->codec)->entropy_private;
256 else
257 huffd = (huffd_common_ptr) ((j_lossy_d_ptr) cinfo->codec)->entropy_private;
258 if (! huffd->insufficient_data) {
259 WARNMS(cinfo, JWRN_HIT_MARKER);
260 huffd->insufficient_data = TRUE;
262 /* Fill the buffer with zero bits */
263 get_buffer <<= MIN_GET_BITS - bits_left;
264 bits_left = MIN_GET_BITS;
268 /* Unload the local registers */
269 state->next_input_byte = next_input_byte;
270 state->bytes_in_buffer = bytes_in_buffer;
271 state->get_buffer = get_buffer;
272 state->bits_left = bits_left;
274 return TRUE;
279 * Out-of-line code for Huffman code decoding.
280 * See jdhuff.h for info about usage.
283 JGLOBAL(int)
284 jpeg_huff_decode (bitread_working_state * state,
285 register bit_buf_type get_buffer, register int bits_left,
286 d_derived_tbl * htbl, int min_bits)
288 register int l = min_bits;
289 register INT32 code;
291 /* HUFF_DECODE has determined that the code is at least min_bits */
292 /* bits long, so fetch that many bits in one swoop. */
294 CHECK_BIT_BUFFER(*state, l, return -1);
295 code = GET_BITS(l);
297 /* Collect the rest of the Huffman code one bit at a time. */
298 /* This is per Figure F.16 in the JPEG spec. */
300 while (code > htbl->maxcode[l]) {
301 code <<= 1;
302 CHECK_BIT_BUFFER(*state, 1, return -1);
303 code |= GET_BITS(1);
304 l++;
307 /* Unload the local registers */
308 state->get_buffer = get_buffer;
309 state->bits_left = bits_left;
311 /* With garbage input we may reach the sentinel value l = 17. */
313 if (l > 16) {
314 WARNMS(state->cinfo, JWRN_HUFF_BAD_CODE);
315 return 0; /* fake a zero as the safest result */
318 return htbl->pub->huffval[ (int) (code + htbl->valoffset[l]) ];