Line data Source code
1 : /*
2 : * Copyright (c) 1988-1997 Sam Leffler
3 : * Copyright (c) 1991-1997 Silicon Graphics, Inc.
4 : * Copyright (c) 2022 Even Rouault
5 : *
6 : * Permission to use, copy, modify, distribute, and sell this software and
7 : * its documentation for any purpose is hereby granted without fee, provided
8 : * that (i) the above copyright notices and this permission notice appear in
9 : * all copies of the software and related documentation, and (ii) the names of
10 : * Sam Leffler and Silicon Graphics may not be used in any advertising or
11 : * publicity relating to the software without the specific, prior written
12 : * permission of Sam Leffler and Silicon Graphics.
13 : *
14 : * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
15 : * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
16 : * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
17 : *
18 : * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
19 : * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
20 : * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
21 : * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
22 : * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
23 : * OF THIS SOFTWARE.
24 : */
25 :
26 : #include "tiffiop.h"
27 : #ifdef LZW_SUPPORT
28 : /*
29 : * TIFF Library.
30 : * Rev 5.0 Lempel-Ziv & Welch Compression Support
31 : *
32 : * This code is derived from the compress program whose code is
33 : * derived from software contributed to Berkeley by James A. Woods,
34 : * derived from original work by Spencer Thomas and Joseph Orost.
35 : *
36 : * The original Berkeley copyright notice appears below in its entirety.
37 : */
38 : #include "tif_predict.h"
39 :
40 : #include <stdbool.h>
41 : #include <stdio.h>
42 : #include <stdlib.h>
43 :
44 : /* Select the plausible largest natural integer type for the architecture */
45 : #define SIZEOF_WORDTYPE SIZEOF_SIZE_T
46 : typedef size_t WordType;
47 :
48 : /*
49 : * NB: The 5.0 spec describes a different algorithm than Aldus
50 : * implements. Specifically, Aldus does code length transitions
51 : * one code earlier than should be done (for real LZW).
52 : * Earlier versions of this library implemented the correct
53 : * LZW algorithm, but emitted codes in a bit order opposite
54 : * to the TIFF spec. Thus, to maintain compatibility w/ Aldus
55 : * we interpret MSB-LSB ordered codes to be images written w/
56 : * old versions of this library, but otherwise adhere to the
57 : * Aldus "off by one" algorithm.
58 : *
59 : * Future revisions to the TIFF spec are expected to "clarify this issue".
60 : */
61 : #define LZW_COMPAT /* include backwards compatibility code */
62 :
63 : #define MAXCODE(n) ((1L << (n)) - 1)
64 : /*
65 : * The TIFF spec specifies that encoded bit
66 : * strings range from 9 to 12 bits.
67 : */
68 : #define BITS_MIN 9 /* start with 9 bits */
69 : #define BITS_MAX 12 /* max of 12 bit strings */
70 : /* predefined codes */
71 : #define CODE_CLEAR 256 /* code to clear string table */
72 : #define CODE_EOI 257 /* end-of-information code */
73 : #define CODE_FIRST 258 /* first free code entry */
74 : #define CODE_MAX MAXCODE(BITS_MAX)
75 : #define HSIZE 9001L /* 91% occupancy */
76 : #define HSHIFT (13 - 8)
77 : #ifdef LZW_COMPAT
78 : /* NB: +1024 is for compatibility with old files */
79 : #define CSIZE (MAXCODE(BITS_MAX) + 1024L)
80 : #else
81 : #define CSIZE (MAXCODE(BITS_MAX) + 1L)
82 : #endif
83 :
84 : /*
85 : * State block for each open TIFF file using LZW
86 : * compression/decompression. Note that the predictor
87 : * state block must be first in this data structure.
88 : */
89 : typedef struct
90 : {
91 : TIFFPredictorState predict; /* predictor super class */
92 :
93 : unsigned short nbits; /* # of bits/code */
94 : unsigned short maxcode; /* maximum code for lzw_nbits */
95 : unsigned short free_ent; /* next free entry in hash table */
96 : WordType nextdata; /* next bits of i/o */
97 : long nextbits; /* # of valid bits in lzw_nextdata */
98 :
99 : int rw_mode; /* preserve rw_mode from init */
100 : } LZWBaseState;
101 :
102 : #define lzw_nbits base.nbits
103 : #define lzw_maxcode base.maxcode
104 : #define lzw_free_ent base.free_ent
105 : #define lzw_nextdata base.nextdata
106 : #define lzw_nextbits base.nextbits
107 :
108 : /*
109 : * Encoding-specific state.
110 : */
111 : typedef uint16_t hcode_t; /* codes fit in 16 bits */
112 : typedef struct
113 : {
114 : long hash;
115 : hcode_t code;
116 : } hash_t;
117 :
118 : /*
119 : * Decoding-specific state.
120 : */
121 : typedef struct code_ent
122 : {
123 : struct code_ent *next;
124 : unsigned short length; /* string len, including this token */
125 : /* firstchar should be placed immediately before value in this structure */
126 : unsigned char firstchar; /* first token of string */
127 : unsigned char value; /* data value */
128 : bool repeated;
129 : } code_t;
130 :
131 : typedef int (*decodeFunc)(TIFF *, uint8_t *, tmsize_t, uint16_t);
132 :
133 : typedef struct
134 : {
135 : LZWBaseState base;
136 :
137 : /* Decoding specific data */
138 : long dec_nbitsmask; /* lzw_nbits 1 bits, right adjusted */
139 : tmsize_t dec_restart; /* restart count */
140 : uint64_t dec_bitsleft; /* available bits in raw data */
141 : tmsize_t old_tif_rawcc; /* value of tif_rawcc at the end of the previous
142 : TIFLZWDecode() call */
143 : decodeFunc dec_decode; /* regular or backwards compatible */
144 : code_t *dec_codep; /* current recognized code */
145 : code_t *dec_oldcodep; /* previously recognized code */
146 : code_t *dec_free_entp; /* next free entry */
147 : code_t *dec_maxcodep; /* max available entry */
148 : code_t *dec_codetab; /* kept separate for small machines */
149 : int read_error; /* whether a read error has occurred, and which should cause
150 : further reads in the same strip/tile to be aborted */
151 :
152 : /* Encoding specific data */
153 : int enc_oldcode; /* last code encountered */
154 : tmsize_t enc_checkpoint; /* point at which to clear table */
155 : #define CHECK_GAP 10000 /* enc_ratio check interval */
156 : tmsize_t enc_ratio; /* current compression ratio */
157 : tmsize_t enc_incount; /* (input) data bytes encoded */
158 : tmsize_t enc_outcount; /* encoded (output) bytes */
159 : uint8_t *enc_rawlimit; /* bound on tif_rawdata buffer */
160 : hash_t *enc_hashtab; /* kept separate for small machines */
161 : } LZWCodecState;
162 :
163 : #define LZWState(tif) ((LZWBaseState *)(tif)->tif_data)
164 : #define LZWDecoderState(tif) ((LZWCodecState *)LZWState(tif))
165 : #define LZWEncoderState(tif) ((LZWCodecState *)LZWState(tif))
166 :
167 : static int LZWDecode(TIFF *tif, uint8_t *op0, tmsize_t occ0, uint16_t s);
168 : #ifdef LZW_COMPAT
169 : static int LZWDecodeCompat(TIFF *tif, uint8_t *op0, tmsize_t occ0, uint16_t s);
170 : #endif
171 :
172 : /*
173 : * LZW Decoder.
174 : */
175 :
176 4854 : static int LZWFixupTags(TIFF *tif)
177 : {
178 : (void)tif;
179 4854 : return (1);
180 : }
181 :
182 2630 : static int LZWSetupDecode(TIFF *tif)
183 : {
184 : static const char module[] = "LZWSetupDecode";
185 2630 : LZWCodecState *sp = LZWDecoderState(tif);
186 : int code;
187 :
188 2630 : if (sp == NULL)
189 : {
190 : /*
191 : * Allocate state block so tag methods have storage to record
192 : * values.
193 : */
194 0 : tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(LZWCodecState));
195 0 : if (tif->tif_data == NULL)
196 : {
197 0 : TIFFErrorExtR(tif, module, "No space for LZW state block");
198 0 : return (0);
199 : }
200 :
201 0 : sp = LZWDecoderState(tif);
202 0 : sp->dec_codetab = NULL;
203 0 : sp->dec_decode = NULL;
204 :
205 : /*
206 : * Setup predictor setup.
207 : */
208 0 : (void)TIFFPredictorInit(tif);
209 : }
210 :
211 2629 : if (sp->dec_codetab == NULL)
212 : {
213 2625 : sp->dec_codetab = (code_t *)_TIFFmallocExt(tif, CSIZE * sizeof(code_t));
214 2627 : if (sp->dec_codetab == NULL)
215 : {
216 1 : TIFFErrorExtR(tif, module, "No space for LZW code table");
217 0 : return (0);
218 : }
219 : /*
220 : * Pre-load the table.
221 : */
222 2626 : code = 255;
223 19125 : do
224 : {
225 671387 : sp->dec_codetab[code].firstchar = (unsigned char)code;
226 671387 : sp->dec_codetab[code].value = (unsigned char)code;
227 671387 : sp->dec_codetab[code].repeated = true;
228 671387 : sp->dec_codetab[code].length = 1;
229 671387 : sp->dec_codetab[code].next = NULL;
230 671387 : } while (code--);
231 : /*
232 : * Zero-out the unused entries */
233 : /* Silence false positive */
234 : /* coverity[overrun-buffer-arg] */
235 2626 : memset(&sp->dec_codetab[CODE_CLEAR], 0,
236 : (CODE_FIRST - CODE_CLEAR) * sizeof(code_t));
237 : }
238 2630 : return (1);
239 : }
240 :
241 : /*
242 : * Setup state for decoding a strip.
243 : */
244 6165 : static int LZWPreDecode(TIFF *tif, uint16_t s)
245 : {
246 : static const char module[] = "LZWPreDecode";
247 6165 : LZWCodecState *sp = LZWDecoderState(tif);
248 :
249 : (void)s;
250 6165 : assert(sp != NULL);
251 6165 : if (sp->dec_codetab == NULL)
252 : {
253 110 : tif->tif_setupdecode(tif);
254 110 : if (sp->dec_codetab == NULL)
255 0 : return (0);
256 : }
257 :
258 : /*
259 : * Check for old bit-reversed codes.
260 : */
261 6165 : if (tif->tif_rawcc >= 2 && tif->tif_rawdata[0] == 0 &&
262 104 : (tif->tif_rawdata[1] & 0x1))
263 : {
264 : #ifdef LZW_COMPAT
265 104 : if (!sp->dec_decode)
266 : {
267 3 : TIFFWarningExtR(tif, module, "Old-style LZW codes, convert file");
268 : /*
269 : * Override default decoding methods with
270 : * ones that deal with the old coding.
271 : * Otherwise the predictor versions set
272 : * above will call the compatibility routines
273 : * through the dec_decode method.
274 : */
275 3 : tif->tif_decoderow = LZWDecodeCompat;
276 3 : tif->tif_decodestrip = LZWDecodeCompat;
277 3 : tif->tif_decodetile = LZWDecodeCompat;
278 : /*
279 : * If doing horizontal differencing, must
280 : * re-setup the predictor logic since we
281 : * switched the basic decoder methods...
282 : */
283 3 : (*tif->tif_setupdecode)(tif);
284 3 : sp->dec_decode = LZWDecodeCompat;
285 : }
286 104 : sp->lzw_maxcode = MAXCODE(BITS_MIN);
287 : #else /* !LZW_COMPAT */
288 : if (!sp->dec_decode)
289 : {
290 : TIFFErrorExtR(tif, module, "Old-style LZW codes not supported");
291 : sp->dec_decode = LZWDecode;
292 : }
293 : return (0);
294 : #endif /* !LZW_COMPAT */
295 : }
296 : else
297 : {
298 6061 : sp->lzw_maxcode = MAXCODE(BITS_MIN) - 1;
299 6061 : sp->dec_decode = LZWDecode;
300 : }
301 6165 : sp->lzw_nbits = BITS_MIN;
302 6165 : sp->lzw_nextbits = 0;
303 6165 : sp->lzw_nextdata = 0;
304 :
305 6165 : sp->dec_restart = 0;
306 6165 : sp->dec_nbitsmask = MAXCODE(BITS_MIN);
307 6165 : sp->dec_bitsleft = 0;
308 6165 : sp->old_tif_rawcc = 0;
309 6165 : sp->dec_free_entp = sp->dec_codetab - 1; // + CODE_FIRST;
310 : /*
311 : * Zero entries that are not yet filled in. We do
312 : * this to guard against bogus input data that causes
313 : * us to index into undefined entries. If you can
314 : * come up with a way to safely bounds-check input codes
315 : * while decoding then you can remove this operation.
316 : */
317 6165 : sp->dec_oldcodep = &sp->dec_codetab[0];
318 6165 : sp->dec_maxcodep = &sp->dec_codetab[sp->dec_nbitsmask - 1];
319 6165 : sp->read_error = 0;
320 6165 : return (1);
321 : }
322 :
323 : /*
324 : * Decode a "hunk of data".
325 : */
326 :
327 : /* Get the next 32 or 64-bit from the input data */
328 : #ifdef WORDS_BIGENDIAN
329 : #define GetNextData(nextdata, bp) memcpy(&nextdata, bp, sizeof(nextdata))
330 : #elif SIZEOF_WORDTYPE == 8
331 : #if defined(_M_X64)
332 : #define GetNextData(nextdata, bp) nextdata = _byteswap_uint64(*(uint64_t *)(bp))
333 : #elif defined(__GNUC__)
334 : #define GetNextData(nextdata, bp) \
335 : memcpy(&nextdata, bp, sizeof(nextdata)); \
336 : nextdata = __builtin_bswap64(nextdata)
337 : #else
338 : #define GetNextData(nextdata, bp) \
339 : nextdata = (((uint64_t)bp[0]) << 56) | (((uint64_t)bp[1]) << 48) | \
340 : (((uint64_t)bp[2]) << 40) | (((uint64_t)bp[3]) << 32) | \
341 : (((uint64_t)bp[4]) << 24) | (((uint64_t)bp[5]) << 16) | \
342 : (((uint64_t)bp[6]) << 8) | (((uint64_t)bp[7]))
343 : #endif
344 : #elif SIZEOF_WORDTYPE == 4
345 : #if defined(_M_X86)
346 : #define GetNextData(nextdata, bp) \
347 : nextdata = _byteswap_ulong(*(unsigned long *)(bp))
348 : #elif defined(__GNUC__)
349 : #define GetNextData(nextdata, bp) \
350 : memcpy(&nextdata, bp, sizeof(nextdata)); \
351 : nextdata = __builtin_bswap32(nextdata)
352 : #else
353 : #define GetNextData(nextdata, bp) \
354 : nextdata = (((uint32_t)bp[0]) << 24) | (((uint32_t)bp[1]) << 16) | \
355 : (((uint32_t)bp[2]) << 8) | (((uint32_t)bp[3]))
356 : #endif
357 : #else
358 : #error "Unhandled SIZEOF_WORDTYPE"
359 : #endif
360 :
361 : #define GetNextCodeLZW() \
362 : do \
363 : { \
364 : nextbits -= nbits; \
365 : if (nextbits < 0) \
366 : { \
367 : if (dec_bitsleft >= 8 * SIZEOF_WORDTYPE) \
368 : { \
369 : unsigned codetmp = (unsigned)(nextdata << (-nextbits)); \
370 : GetNextData(nextdata, bp); \
371 : bp += SIZEOF_WORDTYPE; \
372 : nextbits += 8 * SIZEOF_WORDTYPE; \
373 : dec_bitsleft -= 8 * SIZEOF_WORDTYPE; \
374 : code = (WordType)((codetmp | (nextdata >> nextbits)) & \
375 : nbitsmask); \
376 : break; \
377 : } \
378 : else \
379 : { \
380 : if (dec_bitsleft < 8) \
381 : { \
382 : goto no_eoi; \
383 : } \
384 : nextdata = (nextdata << 8) | *(bp)++; \
385 : nextbits += 8; \
386 : dec_bitsleft -= 8; \
387 : if (nextbits < 0) \
388 : { \
389 : if (dec_bitsleft < 8) \
390 : { \
391 : goto no_eoi; \
392 : } \
393 : nextdata = (nextdata << 8) | *(bp)++; \
394 : nextbits += 8; \
395 : dec_bitsleft -= 8; \
396 : } \
397 : } \
398 : } \
399 : code = (WordType)((nextdata >> nextbits) & nbitsmask); \
400 : } while (0)
401 :
402 114276 : static int LZWDecode(TIFF *tif, uint8_t *op0, tmsize_t occ0, uint16_t s)
403 : {
404 : static const char module[] = "LZWDecode";
405 114276 : LZWCodecState *sp = LZWDecoderState(tif);
406 114276 : uint8_t *op = (uint8_t *)op0;
407 114276 : tmsize_t occ = occ0;
408 : uint8_t *bp;
409 : long nbits, nextbits, nbitsmask;
410 : WordType nextdata;
411 : code_t *free_entp, *maxcodep, *oldcodep;
412 :
413 : (void)s;
414 114276 : assert(sp != NULL);
415 114276 : assert(sp->dec_codetab != NULL);
416 :
417 114276 : if (sp->read_error)
418 : {
419 0 : memset(op, 0, (size_t)occ);
420 0 : TIFFErrorExtR(tif, module,
421 : "LZWDecode: Scanline %" PRIu32 " cannot be read due to "
422 : "previous error",
423 : tif->tif_row);
424 0 : return 0;
425 : }
426 :
427 : /*
428 : * Restart interrupted output operation.
429 : */
430 114276 : if (sp->dec_restart)
431 : {
432 : tmsize_t residue;
433 :
434 104113 : code_t *codep = sp->dec_codep;
435 104113 : residue = codep->length - sp->dec_restart;
436 104113 : if (residue > occ)
437 : {
438 : /*
439 : * Residue from previous decode is sufficient
440 : * to satisfy decode request. Skip to the
441 : * start of the decoded string, place decoded
442 : * values in the output buffer, and return.
443 : */
444 99868 : sp->dec_restart += occ;
445 0 : do
446 : {
447 3160750 : codep = codep->next;
448 3160750 : } while (--residue > occ && codep);
449 99868 : if (codep)
450 : {
451 99868 : uint8_t *tp = op + occ;
452 0 : do
453 : {
454 176900 : *--tp = codep->value;
455 176900 : codep = codep->next;
456 176900 : } while (--occ && codep);
457 : }
458 99868 : return (1);
459 : }
460 : /*
461 : * Residue satisfies only part of the decode request.
462 : */
463 4245 : op += residue;
464 4245 : occ -= residue;
465 4245 : uint8_t *tp = op;
466 0 : do
467 : {
468 6912 : *--tp = codep->value;
469 6912 : codep = codep->next;
470 6912 : } while (--residue && codep);
471 4245 : sp->dec_restart = 0;
472 : }
473 :
474 14408 : bp = (uint8_t *)tif->tif_rawcp;
475 14408 : sp->dec_bitsleft += (((uint64_t)tif->tif_rawcc - sp->old_tif_rawcc) << 3);
476 14408 : uint64_t dec_bitsleft = sp->dec_bitsleft;
477 14408 : nbits = sp->lzw_nbits;
478 14408 : nextdata = sp->lzw_nextdata;
479 14408 : nextbits = sp->lzw_nextbits;
480 14408 : nbitsmask = sp->dec_nbitsmask;
481 14408 : oldcodep = sp->dec_oldcodep;
482 14408 : free_entp = sp->dec_free_entp;
483 14408 : maxcodep = sp->dec_maxcodep;
484 14408 : code_t *const dec_codetab = sp->dec_codetab;
485 : code_t *codep;
486 :
487 14408 : if (occ == 0)
488 : {
489 2276 : goto after_loop;
490 : }
491 :
492 12132 : begin:
493 : {
494 : WordType code;
495 26449306 : GetNextCodeLZW();
496 26449306 : codep = dec_codetab + code;
497 26449306 : if (code >= CODE_FIRST)
498 22007916 : goto code_above_or_equal_to_258;
499 4441370 : if (code < 256)
500 4429627 : goto code_below_256;
501 11856 : if (code == CODE_EOI)
502 10 : goto after_loop;
503 11856 : goto code_clear;
504 :
505 4429627 : code_below_256:
506 : {
507 4429627 : if (codep > free_entp)
508 0 : goto error_code;
509 4429627 : free_entp->next = oldcodep;
510 4429627 : free_entp->firstchar = oldcodep->firstchar;
511 4429627 : free_entp->length = oldcodep->length + 1;
512 4429627 : free_entp->value = (uint8_t)code;
513 4429627 : free_entp->repeated =
514 4429627 : (bool)(oldcodep->repeated & (oldcodep->value == code));
515 4429627 : if (++free_entp > maxcodep)
516 : {
517 4531 : if (++nbits > BITS_MAX) /* should not happen for a conformant encoder */
518 0 : nbits = BITS_MAX;
519 4531 : nbitsmask = MAXCODE(nbits);
520 4531 : maxcodep = dec_codetab + nbitsmask - 1;
521 4531 : if (free_entp >= &dec_codetab[CSIZE])
522 : {
523 : /* At that point, the next valid states are either EOI or a */
524 : /* CODE_CLEAR. If a regular code is read, at the next */
525 : /* attempt at registering a new entry, we will error out */
526 : /* due to setting free_entp before any valid code */
527 0 : free_entp = dec_codetab - 1;
528 : }
529 : }
530 4429627 : oldcodep = codep;
531 4429627 : *op++ = (uint8_t)code;
532 4429627 : occ--;
533 4429627 : if (occ == 0)
534 2735 : goto after_loop;
535 4426891 : goto begin;
536 : }
537 :
538 22007916 : code_above_or_equal_to_258:
539 : {
540 : /*
541 : * Add the new entry to the code table.
542 : */
543 :
544 22007916 : if (codep >= free_entp)
545 : {
546 3406939 : if (codep != free_entp)
547 2 : goto error_code;
548 3406938 : free_entp->value = oldcodep->firstchar;
549 : }
550 : else
551 : {
552 18600947 : free_entp->value = codep->firstchar;
553 : }
554 22007915 : free_entp->repeated =
555 22007915 : (bool)(oldcodep->repeated & (oldcodep->value == free_entp->value));
556 22007915 : free_entp->next = oldcodep;
557 :
558 22007915 : free_entp->firstchar = oldcodep->firstchar;
559 22007915 : free_entp->length = oldcodep->length + 1;
560 22007915 : if (++free_entp > maxcodep)
561 : {
562 18482 : if (++nbits > BITS_MAX) /* should not happen for a conformant encoder */
563 0 : nbits = BITS_MAX;
564 18482 : nbitsmask = MAXCODE(nbits);
565 18482 : maxcodep = dec_codetab + nbitsmask - 1;
566 18482 : if (free_entp >= &dec_codetab[CSIZE])
567 : {
568 : /* At that point, the next valid states are either EOI or a */
569 : /* CODE_CLEAR. If a regular code is read, at the next */
570 : /* attempt at registering a new entry, we will error out */
571 : /* due to setting free_entp before any valid code */
572 0 : free_entp = dec_codetab - 1;
573 : }
574 : }
575 22007915 : oldcodep = codep;
576 :
577 : /*
578 : * Code maps to a string, copy string
579 : * value to output (written in reverse).
580 : */
581 : /* tiny bit faster on x86_64 to store in unsigned short than int */
582 22007915 : unsigned short len = codep->length;
583 :
584 22007915 : if (len < 3) /* equivalent to len == 2 given all other conditions */
585 : {
586 3810619 : if (occ <= 2)
587 : {
588 767 : if (occ == 2)
589 : {
590 720 : memcpy(op, &(codep->firstchar), 2);
591 720 : op += 2;
592 720 : occ -= 2;
593 720 : goto after_loop;
594 : }
595 47 : goto too_short_buffer;
596 : }
597 :
598 3809846 : memcpy(op, &(codep->firstchar), 2);
599 3809846 : op += 2;
600 3809846 : occ -= 2;
601 3809846 : goto begin; /* we can save the comparison occ > 0 */
602 : }
603 :
604 18197326 : if (len == 3)
605 : {
606 2722806 : if (occ <= 3)
607 : {
608 343 : if (occ == 3)
609 : {
610 281 : op[0] = codep->firstchar;
611 281 : op[1] = codep->next->value;
612 281 : op[2] = codep->value;
613 281 : op += 3;
614 281 : occ -= 3;
615 281 : goto after_loop;
616 : }
617 62 : goto too_short_buffer;
618 : }
619 :
620 2722465 : op[0] = codep->firstchar;
621 2722465 : op[1] = codep->next->value;
622 2722465 : op[2] = codep->value;
623 2722465 : op += 3;
624 2722465 : occ -= 3;
625 2722465 : goto begin; /* we can save the comparison occ > 0 */
626 : }
627 :
628 15474460 : if (len > occ)
629 : {
630 4380 : goto too_short_buffer;
631 : }
632 :
633 15470060 : if (codep->repeated)
634 : {
635 899461 : memset(op, codep->value, len);
636 899461 : op += len;
637 899461 : occ -= len;
638 899461 : if (occ == 0)
639 3347 : goto after_loop;
640 896114 : goto begin;
641 : }
642 :
643 14570668 : uint8_t *tp = op + len;
644 :
645 14570668 : assert(len >= 4);
646 :
647 14570668 : *--tp = codep->value;
648 14570668 : codep = codep->next;
649 14570668 : *--tp = codep->value;
650 14570668 : codep = codep->next;
651 14570668 : *--tp = codep->value;
652 14570668 : codep = codep->next;
653 14570668 : *--tp = codep->value;
654 14570668 : if (tp > op)
655 : {
656 309809 : do
657 : {
658 113460991 : codep = codep->next;
659 113460991 : *--tp = codep->value;
660 113460991 : } while (tp > op);
661 : }
662 :
663 14570668 : assert(occ >= len);
664 14570668 : op += len;
665 14570668 : occ -= len;
666 14570668 : if (occ == 0)
667 519 : goto after_loop;
668 14570152 : goto begin;
669 : }
670 :
671 11856 : code_clear:
672 : {
673 11856 : free_entp = dec_codetab + CODE_FIRST;
674 11856 : nbits = BITS_MIN;
675 11856 : nbitsmask = MAXCODE(BITS_MIN);
676 11856 : maxcodep = dec_codetab + nbitsmask - 1;
677 772 : do
678 : {
679 11831 : GetNextCodeLZW();
680 11831 : } while (code == CODE_CLEAR); /* consecutive CODE_CLEAR codes */
681 11856 : if (code == CODE_EOI)
682 0 : goto after_loop;
683 11856 : if (code > CODE_EOI)
684 : {
685 0 : goto error_code;
686 : }
687 11856 : *op++ = (uint8_t)code;
688 11856 : occ--;
689 11856 : oldcodep = dec_codetab + code;
690 11856 : if (occ == 0)
691 29 : goto after_loop;
692 11827 : goto begin;
693 : }
694 : }
695 :
696 4489 : too_short_buffer:
697 : {
698 : /*
699 : * String is too long for decode buffer,
700 : * locate portion that will fit, copy to
701 : * the decode buffer, and setup restart
702 : * logic for the next decoding call.
703 : */
704 4489 : sp->dec_codep = codep;
705 0 : do
706 : {
707 202680 : codep = codep->next;
708 202680 : } while (codep->length > occ);
709 :
710 4489 : sp->dec_restart = occ;
711 4489 : uint8_t *tp = op + occ;
712 0 : do
713 : {
714 24918 : *--tp = codep->value;
715 24918 : codep = codep->next;
716 24918 : } while (--occ);
717 : }
718 :
719 4489 : after_loop:
720 14406 : tif->tif_rawcc -= (tmsize_t)((uint8_t *)bp - tif->tif_rawcp);
721 14406 : tif->tif_rawcp = (uint8_t *)bp;
722 14406 : sp->old_tif_rawcc = tif->tif_rawcc;
723 14406 : sp->dec_bitsleft = dec_bitsleft;
724 14406 : sp->lzw_nbits = (unsigned short)nbits;
725 14406 : sp->lzw_nextdata = nextdata;
726 14406 : sp->lzw_nextbits = nextbits;
727 14406 : sp->dec_nbitsmask = nbitsmask;
728 14406 : sp->dec_oldcodep = oldcodep;
729 14406 : sp->dec_free_entp = free_entp;
730 14406 : sp->dec_maxcodep = maxcodep;
731 :
732 14406 : if (occ > 0)
733 : {
734 10 : memset(op, 0, (size_t)occ);
735 10 : sp->read_error = 1;
736 10 : TIFFErrorExtR(tif, module,
737 : "Not enough data at scanline %" PRIu32 " (short %" PRIu64
738 : " bytes)",
739 : tif->tif_row, (uint64_t)occ);
740 10 : return (0);
741 : }
742 14396 : return (1);
743 :
744 0 : no_eoi:
745 0 : memset(op, 0, (size_t)occ);
746 0 : sp->read_error = 1;
747 0 : TIFFErrorExtR(tif, module,
748 : "LZWDecode: Strip %" PRIu32 " not terminated with EOI code",
749 : tif->tif_curstrip);
750 0 : return 0;
751 2 : error_code:
752 2 : memset(op, 0, (size_t)occ);
753 2 : sp->read_error = 1;
754 2 : TIFFErrorExtR(tif, tif->tif_name, "Using code not yet in table");
755 2 : return 0;
756 : }
757 :
758 : #ifdef LZW_COMPAT
759 :
760 : /*
761 : * This check shouldn't be necessary because each
762 : * strip is suppose to be terminated with CODE_EOI.
763 : */
764 : #define NextCode(_tif, _sp, _bp, _code, _get, dec_bitsleft) \
765 : { \
766 : if (dec_bitsleft < (uint64_t)nbits) \
767 : { \
768 : TIFFWarningExtR(_tif, module, \
769 : "LZWDecode: Strip %" PRIu32 \
770 : " not terminated with EOI code", \
771 : _tif->tif_curstrip); \
772 : _code = CODE_EOI; \
773 : } \
774 : else \
775 : { \
776 : _get(_sp, _bp, _code); \
777 : dec_bitsleft -= nbits; \
778 : } \
779 : }
780 :
781 : /*
782 : * Decode a "hunk of data" for old images.
783 : */
784 : #define GetNextCodeCompat(sp, bp, code) \
785 : { \
786 : nextdata |= (unsigned long)*(bp)++ << nextbits; \
787 : nextbits += 8; \
788 : if (nextbits < nbits) \
789 : { \
790 : nextdata |= (unsigned long)*(bp)++ << nextbits; \
791 : nextbits += 8; \
792 : } \
793 : code = (hcode_t)(nextdata & nbitsmask); \
794 : nextdata >>= nbits; \
795 : nextbits -= nbits; \
796 : }
797 :
798 104 : static int LZWDecodeCompat(TIFF *tif, uint8_t *op0, tmsize_t occ0, uint16_t s)
799 : {
800 : static const char module[] = "LZWDecodeCompat";
801 104 : LZWCodecState *sp = LZWDecoderState(tif);
802 104 : uint8_t *op = (uint8_t *)op0;
803 104 : tmsize_t occ = occ0;
804 : uint8_t *tp;
805 : uint8_t *bp;
806 : int code, nbits;
807 : int len;
808 : long nextbits, nbitsmask;
809 : WordType nextdata;
810 : code_t *codep, *free_entp, *maxcodep, *oldcodep;
811 :
812 : (void)s;
813 104 : assert(sp != NULL);
814 :
815 : /*
816 : * Restart interrupted output operation.
817 : */
818 104 : if (sp->dec_restart)
819 : {
820 : tmsize_t residue;
821 :
822 0 : codep = sp->dec_codep;
823 0 : residue = codep->length - sp->dec_restart;
824 0 : if (residue > occ)
825 : {
826 : /*
827 : * Residue from previous decode is sufficient
828 : * to satisfy decode request. Skip to the
829 : * start of the decoded string, place decoded
830 : * values in the output buffer, and return.
831 : */
832 0 : sp->dec_restart += occ;
833 0 : do
834 : {
835 0 : codep = codep->next;
836 0 : } while (--residue > occ);
837 0 : tp = op + occ;
838 0 : do
839 : {
840 0 : *--tp = codep->value;
841 0 : codep = codep->next;
842 0 : } while (--occ);
843 0 : return (1);
844 : }
845 : /*
846 : * Residue satisfies only part of the decode request.
847 : */
848 0 : op += residue;
849 0 : occ -= residue;
850 0 : tp = op;
851 0 : do
852 : {
853 0 : *--tp = codep->value;
854 0 : codep = codep->next;
855 0 : } while (--residue);
856 0 : sp->dec_restart = 0;
857 : }
858 :
859 104 : bp = (uint8_t *)tif->tif_rawcp;
860 :
861 104 : sp->dec_bitsleft += (((uint64_t)tif->tif_rawcc - sp->old_tif_rawcc) << 3);
862 104 : uint64_t dec_bitsleft = sp->dec_bitsleft;
863 :
864 104 : nbits = sp->lzw_nbits;
865 104 : nextdata = sp->lzw_nextdata;
866 104 : nextbits = sp->lzw_nextbits;
867 104 : nbitsmask = sp->dec_nbitsmask;
868 104 : oldcodep = sp->dec_oldcodep;
869 104 : free_entp = sp->dec_free_entp;
870 104 : maxcodep = sp->dec_maxcodep;
871 :
872 208335 : while (occ > 0)
873 : {
874 208231 : NextCode(tif, sp, bp, code, GetNextCodeCompat, dec_bitsleft);
875 208231 : if (code == CODE_EOI)
876 0 : break;
877 208231 : if (code == CODE_CLEAR)
878 : {
879 0 : do
880 : {
881 104 : free_entp = sp->dec_codetab + CODE_FIRST;
882 104 : _TIFFmemset(free_entp, 0,
883 : (CSIZE - CODE_FIRST) * sizeof(code_t));
884 104 : nbits = BITS_MIN;
885 104 : nbitsmask = MAXCODE(BITS_MIN);
886 104 : maxcodep = sp->dec_codetab + nbitsmask;
887 104 : NextCode(tif, sp, bp, code, GetNextCodeCompat, dec_bitsleft);
888 104 : } while (code == CODE_CLEAR); /* consecutive CODE_CLEAR codes */
889 104 : if (code == CODE_EOI)
890 0 : break;
891 104 : if (code > CODE_CLEAR)
892 : {
893 0 : TIFFErrorExtR(
894 0 : tif, tif->tif_name,
895 : "LZWDecode: Corrupted LZW table at scanline %" PRIu32,
896 : tif->tif_row);
897 0 : return (0);
898 : }
899 104 : *op++ = (uint8_t)code;
900 104 : occ--;
901 104 : oldcodep = sp->dec_codetab + code;
902 104 : continue;
903 : }
904 208127 : codep = sp->dec_codetab + code;
905 :
906 : /*
907 : * Add the new entry to the code table.
908 : */
909 208127 : if (free_entp < &sp->dec_codetab[0] ||
910 208127 : free_entp >= &sp->dec_codetab[CSIZE])
911 : {
912 0 : TIFFErrorExtR(tif, module,
913 : "Corrupted LZW table at scanline %" PRIu32,
914 : tif->tif_row);
915 0 : return (0);
916 : }
917 :
918 208127 : free_entp->next = oldcodep;
919 208127 : if (free_entp->next < &sp->dec_codetab[0] ||
920 208127 : free_entp->next >= &sp->dec_codetab[CSIZE])
921 : {
922 0 : TIFFErrorExtR(tif, module,
923 : "Corrupted LZW table at scanline %" PRIu32,
924 : tif->tif_row);
925 0 : return (0);
926 : }
927 208127 : free_entp->firstchar = free_entp->next->firstchar;
928 208127 : free_entp->length = free_entp->next->length + 1;
929 208127 : free_entp->value =
930 123 : (codep < free_entp) ? codep->firstchar : free_entp->firstchar;
931 208127 : if (++free_entp > maxcodep)
932 : {
933 8825 : if (++nbits > BITS_MAX) /* should not happen */
934 8645 : nbits = BITS_MAX;
935 8825 : nbitsmask = MAXCODE(nbits);
936 8825 : maxcodep = sp->dec_codetab + nbitsmask;
937 : }
938 208127 : oldcodep = codep;
939 208127 : if (code >= 256)
940 : {
941 : /*
942 : * Code maps to a string, copy string
943 : * value to output (written in reverse).
944 : */
945 69239 : if (codep->length == 0)
946 : {
947 0 : TIFFErrorExtR(
948 : tif, module,
949 : "Wrong length of decoded "
950 : "string: data probably corrupted at scanline %" PRIu32,
951 : tif->tif_row);
952 0 : return (0);
953 : }
954 69239 : if (codep->length > occ)
955 : {
956 : /*
957 : * String is too long for decode buffer,
958 : * locate portion that will fit, copy to
959 : * the decode buffer, and setup restart
960 : * logic for the next decoding call.
961 : */
962 0 : sp->dec_codep = codep;
963 0 : do
964 : {
965 0 : codep = codep->next;
966 0 : } while (codep->length > occ);
967 0 : sp->dec_restart = occ;
968 0 : tp = op + occ;
969 0 : do
970 : {
971 0 : *--tp = codep->value;
972 0 : codep = codep->next;
973 0 : } while (--occ);
974 0 : break;
975 : }
976 69239 : len = codep->length;
977 69239 : tp = op + len;
978 7556 : do
979 : {
980 658192 : *--tp = codep->value;
981 658192 : codep = codep->next;
982 658192 : } while (codep && tp > op);
983 69239 : assert(occ >= len);
984 69239 : op += len;
985 69239 : occ -= len;
986 : }
987 : else
988 : {
989 138888 : *op++ = (uint8_t)code;
990 138888 : occ--;
991 : }
992 : }
993 :
994 104 : tif->tif_rawcc -= (tmsize_t)((uint8_t *)bp - tif->tif_rawcp);
995 104 : tif->tif_rawcp = (uint8_t *)bp;
996 :
997 104 : sp->old_tif_rawcc = tif->tif_rawcc;
998 104 : sp->dec_bitsleft = dec_bitsleft;
999 :
1000 104 : sp->lzw_nbits = (unsigned short)nbits;
1001 104 : sp->lzw_nextdata = nextdata;
1002 104 : sp->lzw_nextbits = nextbits;
1003 104 : sp->dec_nbitsmask = nbitsmask;
1004 104 : sp->dec_oldcodep = oldcodep;
1005 104 : sp->dec_free_entp = free_entp;
1006 104 : sp->dec_maxcodep = maxcodep;
1007 :
1008 104 : if (occ > 0)
1009 : {
1010 0 : TIFFErrorExtR(tif, module,
1011 : "Not enough data at scanline %" PRIu32 " (short %" PRIu64
1012 : " bytes)",
1013 : tif->tif_row, (uint64_t)occ);
1014 0 : return (0);
1015 : }
1016 104 : return (1);
1017 : }
1018 : #endif /* LZW_COMPAT */
1019 :
1020 : #ifndef LZW_READ_ONLY
1021 :
1022 : static void cl_hash(LZWCodecState *);
1023 :
1024 : /*
1025 : * LZW Encoding.
1026 : */
1027 :
1028 11744 : static int LZWSetupEncode(TIFF *tif)
1029 : {
1030 : static const char module[] = "LZWSetupEncode";
1031 11744 : LZWCodecState *sp = LZWEncoderState(tif);
1032 :
1033 11744 : assert(sp != NULL);
1034 11744 : sp->enc_hashtab = (hash_t *)_TIFFmallocExt(tif, HSIZE * sizeof(hash_t));
1035 11744 : if (sp->enc_hashtab == NULL)
1036 : {
1037 0 : TIFFErrorExtR(tif, module, "No space for LZW hash table");
1038 0 : return (0);
1039 : }
1040 11744 : return (1);
1041 : }
1042 :
1043 : /*
1044 : * Reset encoding state at the start of a strip.
1045 : */
1046 13512 : static int LZWPreEncode(TIFF *tif, uint16_t s)
1047 : {
1048 13512 : LZWCodecState *sp = LZWEncoderState(tif);
1049 :
1050 : (void)s;
1051 13512 : assert(sp != NULL);
1052 :
1053 13512 : if (sp->enc_hashtab == NULL)
1054 : {
1055 2 : tif->tif_setupencode(tif);
1056 : }
1057 :
1058 13512 : sp->lzw_nbits = BITS_MIN;
1059 13512 : sp->lzw_maxcode = MAXCODE(BITS_MIN);
1060 13512 : sp->lzw_free_ent = CODE_FIRST;
1061 13512 : sp->lzw_nextbits = 0;
1062 13512 : sp->lzw_nextdata = 0;
1063 13512 : sp->enc_checkpoint = CHECK_GAP;
1064 13512 : sp->enc_ratio = 0;
1065 13512 : sp->enc_incount = 0;
1066 13512 : sp->enc_outcount = 0;
1067 : /*
1068 : * The 4 here insures there is space for 2 max-sized
1069 : * codes in LZWEncode and LZWPostDecode.
1070 : */
1071 13512 : sp->enc_rawlimit = tif->tif_rawdata + tif->tif_rawdatasize - 1 - 4;
1072 13512 : cl_hash(sp); /* clear hash table */
1073 13512 : sp->enc_oldcode = (hcode_t)-1; /* generates CODE_CLEAR in LZWEncode */
1074 13512 : return (1);
1075 : }
1076 :
1077 : #define CALCRATIO(sp, rat) \
1078 : { \
1079 : if (incount > 0x007fffff) \
1080 : { /* NB: shift will overflow */ \
1081 : rat = outcount >> 8; \
1082 : rat = (rat == 0 ? 0x7fffffff : incount / rat); \
1083 : } \
1084 : else \
1085 : rat = (incount << 8) / outcount; \
1086 : }
1087 :
1088 : /* Explicit 0xff masking to make icc -check=conversions happy */
1089 : #define PutNextCode(op, c) \
1090 : { \
1091 : nextdata = (nextdata << nbits) | c; \
1092 : nextbits += nbits; \
1093 : *op++ = (unsigned char)((nextdata >> (nextbits - 8)) & 0xff); \
1094 : nextbits -= 8; \
1095 : if (nextbits >= 8) \
1096 : { \
1097 : *op++ = (unsigned char)((nextdata >> (nextbits - 8)) & 0xff); \
1098 : nextbits -= 8; \
1099 : } \
1100 : outcount += nbits; \
1101 : }
1102 :
1103 : /*
1104 : * Encode a chunk of pixels.
1105 : *
1106 : * Uses an open addressing double hashing (no chaining) on the
1107 : * prefix code/next character combination. We do a variant of
1108 : * Knuth's algorithm D (vol. 3, sec. 6.4) along with G. Knott's
1109 : * relatively-prime secondary probe. Here, the modular division
1110 : * first probe is gives way to a faster exclusive-or manipulation.
1111 : * Also do block compression with an adaptive reset, whereby the
1112 : * code table is cleared when the compression ratio decreases,
1113 : * but after the table fills. The variable-length output codes
1114 : * are re-sized at this point, and a CODE_CLEAR is generated
1115 : * for the decoder.
1116 : */
1117 35508 : static int LZWEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
1118 : {
1119 35508 : register LZWCodecState *sp = LZWEncoderState(tif);
1120 : register long fcode;
1121 : register hash_t *hp;
1122 : register int h, c;
1123 : hcode_t ent;
1124 : long disp;
1125 : tmsize_t incount, outcount, checkpoint;
1126 : WordType nextdata;
1127 : long nextbits;
1128 : int free_ent, maxcode, nbits;
1129 : uint8_t *op;
1130 : uint8_t *limit;
1131 :
1132 : (void)s;
1133 35508 : if (sp == NULL)
1134 0 : return (0);
1135 :
1136 35508 : assert(sp->enc_hashtab != NULL);
1137 :
1138 : /*
1139 : * Load local state.
1140 : */
1141 35508 : incount = sp->enc_incount;
1142 35508 : outcount = sp->enc_outcount;
1143 35508 : checkpoint = sp->enc_checkpoint;
1144 35508 : nextdata = sp->lzw_nextdata;
1145 35508 : nextbits = sp->lzw_nextbits;
1146 35508 : free_ent = sp->lzw_free_ent;
1147 35508 : maxcode = sp->lzw_maxcode;
1148 35508 : nbits = sp->lzw_nbits;
1149 35508 : op = tif->tif_rawcp;
1150 35508 : limit = sp->enc_rawlimit;
1151 35508 : ent = (hcode_t)sp->enc_oldcode;
1152 :
1153 35508 : if (ent == (hcode_t)-1 && cc > 0)
1154 : {
1155 : /*
1156 : * NB: This is safe because it can only happen
1157 : * at the start of a strip where we know there
1158 : * is space in the data buffer.
1159 : */
1160 13512 : PutNextCode(op, CODE_CLEAR);
1161 13512 : ent = *bp++;
1162 13512 : cc--;
1163 13512 : incount++;
1164 : }
1165 35508 : while (cc > 0)
1166 : {
1167 589851000 : c = *bp++;
1168 589851000 : cc--;
1169 589851000 : incount++;
1170 589851000 : fcode = ((long)c << BITS_MAX) + ent;
1171 589851000 : h = (c << HSHIFT) ^ ent; /* xor hashing */
1172 : #ifdef _WINDOWS
1173 : /*
1174 : * Check hash index for an overflow.
1175 : */
1176 : if (h >= HSIZE)
1177 : h -= HSIZE;
1178 : #endif
1179 589851000 : hp = &sp->enc_hashtab[h];
1180 589851000 : if (hp->hash == fcode)
1181 : {
1182 470836000 : ent = hp->code;
1183 470836000 : continue;
1184 : }
1185 119015000 : if (hp->hash >= 0)
1186 : {
1187 : /*
1188 : * Primary hash failed, check secondary hash.
1189 : */
1190 101974000 : disp = HSIZE - h;
1191 101974000 : if (h == 0)
1192 14966 : disp = 1;
1193 : do
1194 : {
1195 : /*
1196 : * Avoid pointer arithmetic because of
1197 : * wraparound problems with segments.
1198 : */
1199 130909000 : if ((h -= disp) < 0)
1200 121659000 : h += HSIZE;
1201 130909000 : hp = &sp->enc_hashtab[h];
1202 130909000 : if (hp->hash == fcode)
1203 : {
1204 96324100 : ent = hp->code;
1205 96324100 : goto hit;
1206 : }
1207 34585000 : } while (hp->hash >= 0);
1208 : }
1209 : /*
1210 : * New entry, emit code and add to table.
1211 : */
1212 : /*
1213 : * Verify there is space in the buffer for the code
1214 : * and any potential Clear code that might be emitted
1215 : * below. The value of limit is setup so that there
1216 : * are at least 4 bytes free--room for 2 codes.
1217 : */
1218 22691200 : if (op > limit)
1219 : {
1220 1 : tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata);
1221 1 : if (!TIFFFlushData1(tif))
1222 0 : return 0;
1223 1 : op = tif->tif_rawdata;
1224 : }
1225 22691200 : PutNextCode(op, ent);
1226 22691200 : ent = (hcode_t)c;
1227 22691200 : hp->code = (hcode_t)(free_ent++);
1228 22691200 : hp->hash = fcode;
1229 22691200 : if (free_ent == CODE_MAX - 1)
1230 : {
1231 : /* table is full, emit clear code and reset */
1232 5827 : cl_hash(sp);
1233 5827 : sp->enc_ratio = 0;
1234 5827 : incount = 0;
1235 5827 : outcount = 0;
1236 5827 : free_ent = CODE_FIRST;
1237 5827 : PutNextCode(op, CODE_CLEAR);
1238 5827 : nbits = BITS_MIN;
1239 5827 : maxcode = MAXCODE(BITS_MIN);
1240 : }
1241 : else
1242 : {
1243 : /*
1244 : * If the next entry is going to be too big for
1245 : * the code size, then increase it, if possible.
1246 : */
1247 22685300 : if (free_ent > maxcode)
1248 : {
1249 24471 : nbits++;
1250 24471 : assert(nbits <= BITS_MAX);
1251 24471 : maxcode = (int)MAXCODE(nbits);
1252 : }
1253 22660900 : else if (incount >= checkpoint)
1254 : {
1255 : tmsize_t rat;
1256 : /*
1257 : * Check compression ratio and, if things seem
1258 : * to be slipping, clear the hash table and
1259 : * reset state. The compression ratio is a
1260 : * 24+8-bit fractional number.
1261 : */
1262 40116 : checkpoint = incount + CHECK_GAP;
1263 40116 : CALCRATIO(sp, rat);
1264 40116 : if (rat <= sp->enc_ratio)
1265 : {
1266 174 : cl_hash(sp);
1267 174 : sp->enc_ratio = 0;
1268 174 : incount = 0;
1269 174 : outcount = 0;
1270 174 : free_ent = CODE_FIRST;
1271 174 : PutNextCode(op, CODE_CLEAR);
1272 174 : nbits = BITS_MIN;
1273 174 : maxcode = MAXCODE(BITS_MIN);
1274 : }
1275 : else
1276 39942 : sp->enc_ratio = rat;
1277 : }
1278 : }
1279 589887000 : hit:;
1280 : }
1281 :
1282 : /*
1283 : * Restore global state.
1284 : */
1285 35508 : sp->enc_incount = incount;
1286 35508 : sp->enc_outcount = outcount;
1287 35508 : sp->enc_checkpoint = checkpoint;
1288 35508 : sp->enc_oldcode = ent;
1289 35508 : sp->lzw_nextdata = nextdata;
1290 35508 : sp->lzw_nextbits = nextbits;
1291 35508 : sp->lzw_free_ent = (unsigned short)free_ent;
1292 35508 : sp->lzw_maxcode = (unsigned short)maxcode;
1293 35508 : sp->lzw_nbits = (unsigned short)nbits;
1294 35508 : tif->tif_rawcp = op;
1295 35508 : return (1);
1296 : }
1297 :
1298 : /*
1299 : * Finish off an encoded strip by flushing the last
1300 : * string and tacking on an End Of Information code.
1301 : */
1302 13512 : static int LZWPostEncode(TIFF *tif)
1303 : {
1304 13512 : register LZWCodecState *sp = LZWEncoderState(tif);
1305 13512 : uint8_t *op = tif->tif_rawcp;
1306 13512 : long nextbits = sp->lzw_nextbits;
1307 13512 : WordType nextdata = sp->lzw_nextdata;
1308 13512 : tmsize_t outcount = sp->enc_outcount;
1309 13512 : int nbits = sp->lzw_nbits;
1310 :
1311 13512 : if (op > sp->enc_rawlimit)
1312 : {
1313 0 : tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata);
1314 0 : if (!TIFFFlushData1(tif))
1315 0 : return 0;
1316 0 : op = tif->tif_rawdata;
1317 : }
1318 13512 : if (sp->enc_oldcode != (hcode_t)-1)
1319 : {
1320 13512 : int free_ent = sp->lzw_free_ent;
1321 :
1322 13512 : PutNextCode(op, sp->enc_oldcode);
1323 13512 : sp->enc_oldcode = (hcode_t)-1;
1324 13512 : free_ent++;
1325 :
1326 13512 : if (free_ent == CODE_MAX - 1)
1327 : {
1328 : /* table is full, emit clear code and reset */
1329 0 : outcount = 0;
1330 0 : PutNextCode(op, CODE_CLEAR);
1331 0 : nbits = BITS_MIN;
1332 : }
1333 : else
1334 : {
1335 : /*
1336 : * If the next entry is going to be too big for
1337 : * the code size, then increase it, if possible.
1338 : */
1339 13512 : if (free_ent > sp->lzw_maxcode)
1340 : {
1341 2 : nbits++;
1342 2 : assert(nbits <= BITS_MAX);
1343 : }
1344 : }
1345 : }
1346 13512 : PutNextCode(op, CODE_EOI);
1347 : /* Explicit 0xff masking to make icc -check=conversions happy */
1348 13512 : if (nextbits > 0)
1349 13392 : *op++ = (unsigned char)((nextdata << (8 - nextbits)) & 0xff);
1350 13512 : tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata);
1351 : (void)outcount;
1352 13512 : return (1);
1353 : }
1354 :
1355 : /*
1356 : * Reset encoding hash table.
1357 : */
1358 19513 : static void cl_hash(LZWCodecState *sp)
1359 : {
1360 19513 : register hash_t *hp = &sp->enc_hashtab[HSIZE - 1];
1361 19513 : register long i = HSIZE - 8;
1362 :
1363 : do
1364 : {
1365 21950400 : i -= 8;
1366 21950400 : hp[-7].hash = -1;
1367 21950400 : hp[-6].hash = -1;
1368 21950400 : hp[-5].hash = -1;
1369 21950400 : hp[-4].hash = -1;
1370 21950400 : hp[-3].hash = -1;
1371 21950400 : hp[-2].hash = -1;
1372 21950400 : hp[-1].hash = -1;
1373 21950400 : hp[0].hash = -1;
1374 21950400 : hp -= 8;
1375 21950400 : } while (i >= 0);
1376 39026 : for (i += 8; i > 0; i--, hp--)
1377 19513 : hp->hash = -1;
1378 19513 : }
1379 :
1380 : #endif
1381 :
1382 18998 : static void LZWCleanup(TIFF *tif)
1383 : {
1384 18998 : (void)TIFFPredictorCleanup(tif);
1385 :
1386 18997 : assert(tif->tif_data != NULL);
1387 :
1388 18997 : if (LZWDecoderState(tif)->dec_codetab)
1389 2626 : _TIFFfreeExt(tif, LZWDecoderState(tif)->dec_codetab);
1390 :
1391 18998 : if (LZWEncoderState(tif)->enc_hashtab)
1392 11744 : _TIFFfreeExt(tif, LZWEncoderState(tif)->enc_hashtab);
1393 :
1394 18998 : _TIFFfreeExt(tif, tif->tif_data);
1395 18997 : tif->tif_data = NULL;
1396 :
1397 18997 : _TIFFSetDefaultCompressionState(tif);
1398 18998 : }
1399 :
1400 18999 : int TIFFInitLZW(TIFF *tif, int scheme)
1401 : {
1402 : static const char module[] = "TIFFInitLZW";
1403 : (void)scheme;
1404 18999 : assert(scheme == COMPRESSION_LZW);
1405 : /*
1406 : * Allocate state block so tag methods have storage to record values.
1407 : */
1408 18999 : tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(LZWCodecState));
1409 18998 : if (tif->tif_data == NULL)
1410 0 : goto bad;
1411 18998 : LZWDecoderState(tif)->dec_codetab = NULL;
1412 18998 : LZWDecoderState(tif)->dec_decode = NULL;
1413 18998 : LZWEncoderState(tif)->enc_hashtab = NULL;
1414 18998 : LZWState(tif)->rw_mode = tif->tif_mode;
1415 :
1416 : /*
1417 : * Install codec methods.
1418 : */
1419 18998 : tif->tif_fixuptags = LZWFixupTags;
1420 18998 : tif->tif_setupdecode = LZWSetupDecode;
1421 18998 : tif->tif_predecode = LZWPreDecode;
1422 18998 : tif->tif_decoderow = LZWDecode;
1423 18998 : tif->tif_decodestrip = LZWDecode;
1424 18998 : tif->tif_decodetile = LZWDecode;
1425 : #ifndef LZW_READ_ONLY
1426 18923 : tif->tif_setupencode = LZWSetupEncode;
1427 18923 : tif->tif_preencode = LZWPreEncode;
1428 18923 : tif->tif_postencode = LZWPostEncode;
1429 18923 : tif->tif_encoderow = LZWEncode;
1430 18923 : tif->tif_encodestrip = LZWEncode;
1431 18923 : tif->tif_encodetile = LZWEncode;
1432 : #endif
1433 18998 : tif->tif_cleanup = LZWCleanup;
1434 : /*
1435 : * Setup predictor setup.
1436 : */
1437 18998 : (void)TIFFPredictorInit(tif);
1438 18999 : return (1);
1439 0 : bad:
1440 0 : TIFFErrorExtR(tif, module, "No space for LZW state block");
1441 0 : return (0);
1442 : }
1443 :
1444 : /*
1445 : * Copyright (c) 1985, 1986 The Regents of the University of California.
1446 : * All rights reserved.
1447 : *
1448 : * This code is derived from software contributed to Berkeley by
1449 : * James A. Woods, derived from original work by Spencer Thomas
1450 : * and Joseph Orost.
1451 : *
1452 : * Redistribution and use in source and binary forms are permitted
1453 : * provided that the above copyright notice and this paragraph are
1454 : * duplicated in all such forms and that any documentation,
1455 : * advertising materials, and other materials related to such
1456 : * distribution and use acknowledge that the software was developed
1457 : * by the University of California, Berkeley. The name of the
1458 : * University may not be used to endorse or promote products derived
1459 : * from this software without specific prior written permission.
1460 : * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1461 : * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1462 : * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1463 : */
1464 : #endif /* LZW_SUPPORT */
|