Debian Codegraph @

libclamav/lzw/lzwdec.c

raw symbols SCIP
1 /*
2 * Copyright (c) 1988-1997 Sam Leffler
3 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
4 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that (i) the above copyright notices and this permission notice appear in
8 * all copies of the software and related documentation, and (ii) the names of
9 * Sam Leffler and Silicon Graphics may not be used in any advertising or
10 * publicity relating to the software without the specific, prior written
11 * permission of Sam Leffler and Silicon Graphics.
12 *
13 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
16 *
17 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22 * OF THIS SOFTWARE.
23 */
24 /*
25 * Portions Copyright (C) 2016 Cisco and/or its affiliates. All rights reserved.
26 *
27 * Modified by: Kevin Lin
28 *
29 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License version 2 as
31 * published by the Free Software Foundation.
32 *
33 * This program is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 * GNU General Public License for more details.
37 *
38 * You should have received a copy of the GNU General Public License
39 * along with this program; if not, write to the Free Software
40 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
41 * MA 02110-1301, USA.
42 *
43 * In addition, as a special exception, the copyright holders give
44 * permission to link the code of portions of this program with the
45 * OpenSSL library under certain conditions as described in each
46 * individual source file, and distribute linked combinations
47 * including the two.
48 *
49 * You must obey the GNU General Public License in all respects
50 * for all of the code used other than OpenSSL. If you modify
51 * file(s) with this exception, you may extend this exception to your
52 * version of the file(s), but you are not obligated to do so. If you
53 * do not wish to do so, delete this exception statement from your
54 * version. If you delete this exception statement from all source
55 * files in the program, then also delete it here.
56 */
57 #include <stdio.h>
58
59 #include <assert.h>
60 #include <stdint.h>
61
62 #include "lzwdec.h"
63
64 // libclamav
65 #include "others.h"
66
67 #define MAXCODE(n) ((1L << (n)) - 1)
68 /*
69 * The spec specifies that encoded bit
70 * strings SHOULD range from 9 to 12 bits.
71 */
72 #define BITS_MIN 9 /* start with 9 bits */
73 #define BITS_VALID 12 /* 12 bit codes are the max valid */
74 #define BITS_MAX 14 /* max of 14 bit codes (2 bits extension) */
75 /* predefined codes */
76 #define CODE_BASIC 256 /* last basic code + 1 */
77 #define CODE_CLEAR 256 /* code to clear string table */
78 #define CODE_EOI 257 /* end-of-information code */
79 #define CODE_FIRST 258 /* first free code entry */
80 #define CODE_VALID MAXCODE(BITS_VALID)
81 #define CODE_MAX MAXCODE(BITS_MAX)
82
83 #define CSIZE (MAXCODE(BITS_MAX) + 1L)
84
85 typedef uint16_t hcode_t; /* codes fit in 16 bits */
86
87 /*
88 * Decoding-specific state.
89 */
90 typedef struct code_ent {
91 struct code_ent *next;
92 uint16_t length; /* string len, including this token */
93 uint8_t value; /* data value */
94 uint8_t firstchar; /* first token of string */
95 } code_t;
96
97 struct lzw_internal_state {
98 /* general state */
99 uint16_t nbits; /* # of bits/code */
100 unsigned long nextdata; /* next bits of i/o */
101 long nextbits; /* # of valid bits in lzw_nextdata */
102
103 /* decoding-specific state */
104 long dec_nbitsmask; /* lzw_nbits 1 bits, right adjusted */
105 long dec_restart; /* restart count */
106 code_t *dec_codep; /* current recognized code */
107 code_t *dec_oldcodep; /* previously recognized code */
108 code_t *dec_free_entp; /* next free entry */
109 code_t *dec_maxcodep; /* max available entry */
110 code_t *dec_codetab; /* kept separate for small machines */
111 };
112
113 #define LZW_ENABLE_PRINT_DEBUGGING 0
114 #if LZW_ENABLE_PRINT_DEBUGGING
115 static void code_print(code_t *code);
116 static void dict_print(code_t *codetab, uint16_t start, uint16_t maxcode);
117 #endif
118
119 #define GetNextCode(code) \
120 { \
121 if (have == 0) \
122 break; \
123 nextdata = nextdata << 8 | *(from)++; \
124 have--; \
125 nextbits += 8; \
126 if (nextbits < nbits) { \
127 if (have == 0) \
128 break; \
129 nextdata = nextdata << 8 | *(from)++; \
130 have--; \
131 nextbits += 8; \
132 } \
133 code = (hcode_t)((nextdata >> (nextbits - nbits)) & nbitsmask); \
134 nextbits -= nbits; \
135 }
136
137 #define CodeClear(code) \
138 { \
139 free_code = CODE_FIRST; \
140 free_entp = state->dec_codetab + CODE_FIRST; \
141 nbits = BITS_MIN; \
142 nbitsmask = MAXCODE(BITS_MIN); \
143 maxcodep = state->dec_codetab + nbitsmask - 1; \
144 while (code == CODE_CLEAR) /* clears out consecutive CODE_CLEARs */ \
145 GetNextCode(code); \
146 if (code < CODE_BASIC) \
147 *to++ = code, left--; \
148 else if (code == CODE_EOI) \
149 ret = LZW_STREAM_END; \
150 else if (code >= CODE_FIRST) { \
151 /* cannot reference unpopulated dictionary entries */ \
152 strm->msg = "cannot reference unpopulated dictionary entries"; \
153 ret = LZW_DATA_ERROR; \
154 } \
155 oldcodep = state->dec_codetab + code; \
156 }
157
158 int lzwInit(lzw_streamp strm)
159 {
160 struct lzw_internal_state *state;
161 hcode_t code;
162
163 state = malloc(sizeof(struct lzw_internal_state));
164 if (state == NULL) {
165 strm->msg = "failed to allocate state";
166 return LZW_MEM_ERROR;
167 }
168
169 /* general state setup */
170 state->nbits = BITS_MIN;
171 state->nextdata = 0;
172 state->nextbits = 0;
173
174 /* dictionary setup */
175 state->dec_codetab = cli_max_calloc(CSIZE, sizeof(code_t));
176 if (state->dec_codetab == NULL) {
177 free(state);
178 strm->msg = "failed to allocate code table";
179 return LZW_MEM_ERROR;
180 }
181
182 for (code = 0; code < CODE_BASIC; code++) {
183 state->dec_codetab[code].next = NULL;
184 state->dec_codetab[code].length = 1;
185 state->dec_codetab[code].value = code;
186 state->dec_codetab[code].firstchar = code;
187 }
188
189 state->dec_restart = 0;
190 state->dec_nbitsmask = MAXCODE(BITS_MIN);
191 state->dec_free_entp = state->dec_codetab + CODE_FIRST;
192 state->dec_oldcodep = &state->dec_codetab[CODE_CLEAR];
193 state->dec_maxcodep = &state->dec_codetab[state->dec_nbitsmask - 1];
194
195 strm->state = state;
196 return LZW_OK;
197 }
198
199 int lzwInflate(lzw_streamp strm)
200 {
201 struct lzw_internal_state *state;
202 uint8_t *from, *to;
203 unsigned in, out;
204 unsigned have, left;
205 long nbits, nextbits, nbitsmask;
206 unsigned long nextdata;
207 code_t *codep, *free_entp, *maxcodep, *oldcodep;
208
209 uint8_t *wp;
210 hcode_t code, free_code;
211 int echg, cext, ret = LZW_OK;
212 uint32_t flags;
213
214 if (strm == NULL || strm->state == NULL || strm->next_out == NULL ||
215 (strm->next_in == NULL && strm->avail_in != 0))
216 return LZW_STREAM_ERROR;
217
218 /* load state */
219 to = strm->next_out;
220 out = left = strm->avail_out;
221
222 from = strm->next_in;
223 in = have = strm->avail_in;
224
225 flags = strm->flags;
226 state = strm->state;
227
228 nbits = state->nbits;
229 nextdata = state->nextdata;
230 nextbits = state->nextbits;
231 nbitsmask = state->dec_nbitsmask;
232 oldcodep = state->dec_oldcodep;
233 free_entp = state->dec_free_entp;
234 maxcodep = state->dec_maxcodep;
235
236 echg = flags & LZW_FLAG_EARLYCHG;
237 cext = flags & LZW_FLAG_EXTNCODE;
238 free_code = free_entp - &state->dec_codetab[0];
239
240 if (oldcodep == &state->dec_codetab[CODE_EOI])
241 return LZW_STREAM_END;
242
243 /*
244 * Restart interrupted output operation.
245 */
246 if (state->dec_restart) {
247 long residue;
248
249 codep = state->dec_codep;
250 residue = codep->length - state->dec_restart;
251 if (residue > left) {
252 /*
253 * Residue from previous decode is sufficient
254 * to satisfy decode request. Skip to the
255 * start of the decoded string, place decoded
256 * values in the output buffer, and return.
257 */
258 state->dec_restart += left;
259 do {
260 codep = codep->next;
261 } while (--residue > left);
262 to = wp = to + left;
263 do {
264 *--wp = codep->value;
265 codep = codep->next;
266 } while (--left);
267 goto inf_end;
268 }
269 /*
270 * Residue satisfies only part of the decode request.
271 */
272 to += residue, left -= residue;
273 wp = to;
274 do {
275 *--wp = codep->value;
276 codep = codep->next;
277 } while (--residue);
278 state->dec_restart = 0;
279 }
280
281 /* guarantee valid initial state */
282 if (left > 0 && (oldcodep == &state->dec_codetab[CODE_CLEAR])) {
283 code = CODE_CLEAR;
284 CodeClear(code);
285 if (ret != LZW_OK)
286 goto inf_end;
287 }
288
289 while (left > 0) {
290 GetNextCode(code);
291 if (code == CODE_EOI) {
292 ret = LZW_STREAM_END;
293 break;
294 }
295 if (code == CODE_CLEAR) {
296 CodeClear(code);
297 if (ret != LZW_OK)
298 break;
299 continue;
300 }
301 codep = state->dec_codetab + code;
302
303 /* cap dictionary codes to valid range (12-bits) */
304 if (free_code < CODE_VALID + 1 || cext) {
305 /* non-earlychange bit expansion */
306 if (!echg && free_entp > maxcodep) {
307 if (++nbits > BITS_VALID) {
308 if (!cext)
309 nbits = BITS_VALID;
310 else if (nbits > BITS_MAX)
311 nbits = BITS_MAX;
312 }
313 nbitsmask = MAXCODE(nbits);
314 maxcodep = state->dec_codetab + nbitsmask - 1;
315 }
316 /*
317 * Add the new entry to the code table.
318 */
319 if (&state->dec_codetab[0] > free_entp || free_entp >= &state->dec_codetab[CSIZE]) {
320 cli_dbgmsg("%p <= %p, %p < %p(%ld)\n", &state->dec_codetab[0], free_entp, free_entp, &state->dec_codetab[CSIZE], CSIZE);
321 strm->msg = "full dictionary, cannot add new entry";
322 flags |= LZW_FLAG_FULLDICT;
323 ret = LZW_DICT_ERROR;
324 break;
325 }
326 free_entp->next = oldcodep;
327 free_entp->firstchar = free_entp->next->firstchar;
328 free_entp->length = free_entp->next->length + 1;
329 free_entp->value = (codep < free_entp) ? codep->firstchar : free_entp->firstchar;
330 free_entp++;
331 /* earlychange bit expansion */
332 if (echg && free_entp > maxcodep) {
333 if (++nbits > BITS_VALID) {
334 if (!cext)
335 nbits = BITS_VALID;
336 else if (nbits > BITS_MAX)
337 nbits = BITS_MAX;
338 }
339 nbitsmask = MAXCODE(nbits);
340 maxcodep = state->dec_codetab + nbitsmask - 1;
341 }
342 if (free_code++ > CODE_VALID)
343 flags |= LZW_FLAG_EXTNCODEUSE;
344 oldcodep = codep;
345 } else
346 flags |= LZW_FLAG_FULLDICT;
347 if (code >= CODE_BASIC) {
348 /* check if code is valid */
349 if (code >= free_code) {
350 strm->msg = "cannot reference unpopulated dictionary entries";
351 flags |= LZW_FLAG_INVALIDCODE;
352 ret = LZW_DATA_ERROR;
353 break;
354 }
355
356 /*
357 * Code maps to a string, copy string
358 * value to output (written in reverse).
359 */
360 if (codep->length > left) {
361 /*
362 * String is too long for decode buffer,
363 * locate portion that will fit, copy to
364 * the decode buffer, and setup restart
365 * logic for the next decoding call.
366 */
367 state->dec_codep = codep;
368 do {
369 codep = codep->next;
370 } while (codep->length > left);
371 state->dec_restart = left;
372 to = wp = to + left;
373 do {
374 *--wp = codep->value;
375 codep = codep->next;
376 } while (--left);
377 goto inf_end;
378 }
379
380 to += codep->length, left -= codep->length;
381 wp = to;
382 do {
383 *--wp = codep->value;
384 codep = codep->next;
385 } while (codep != NULL);
386 } else
387 *to++ = code, left--;
388 }
389
390 inf_end:
391 /* restore state */
392 strm->next_out = to;
393 strm->avail_out = left;
394 strm->next_in = from;
395 strm->avail_in = have;
396 strm->flags = flags;
397
398 state->nbits = (uint16_t)nbits;
399 state->nextdata = nextdata;
400 state->nextbits = nextbits;
401 state->dec_nbitsmask = nbitsmask;
402 state->dec_oldcodep = oldcodep;
403 state->dec_free_entp = free_entp;
404 state->dec_maxcodep = maxcodep;
405
406 /* update state */
407 in -= strm->avail_in;
408 out -= strm->avail_out;
409 strm->total_in += in;
410 strm->total_out += out;
411
412 if ((in == 0 && out == 0) && ret == LZW_OK) {
413 strm->msg = "no data was processed";
414 ret = LZW_BUF_ERROR;
415 }
416 return ret;
417 }
418
419 int lzwInflateEnd(lzw_streamp strm)
420 {
421 free(strm->state->dec_codetab);
422 free(strm->state);
423 strm->state = NULL;
424 return LZW_OK;
425 }
426
427 #if LZW_ENABLE_PRINT_DEBUGGING
428 static void code_print(code_t *code)
429 {
430 code_t *cpt = code;
431 uint8_t *string;
432 int i = 0;
433
434 string = cli_max_calloc(code->length + 1, sizeof(uint8_t));
435 if (!string)
436 return;
437
438 while (cpt && (i < code->length)) {
439 if (isalnum(cpt->value))
440 string[code->length - i - 1] = cpt->value;
441 else
442 string[code->length - i - 1] = '*';
443
444 i++;
445 cpt = cpt->next;
446 }
447
448 printf("%s\n", string);
449 free(string);
450 }
451
452 static void dict_print(code_t *codetab, uint16_t start, uint16_t maxcode)
453 {
454 int i;
455
456 for (i = start; i < maxcode; i++) {
457 printf("%d: ", i);
458 code_print(codetab + i);
459 }
460 }
461 #endif