Coverage Report

Created: 2026-06-07 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libtiff/libtiff/tif_lzw.c
Line
Count
Source
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
126k
#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
156k
#define MAXCODE(n) ((1 << (n)) - 1)
64
/*
65
 * The TIFF spec specifies that encoded bit
66
 * strings range from 9 to 12 bits.
67
 */
68
4.78k
#define BITS_MIN 9  /* start with 9 bits */
69
5.42k
#define BITS_MAX 12 /* max of 12 bit strings */
70
/* predefined codes */
71
79.1k
#define CODE_CLEAR 256 /* code to clear string table */
72
77.0k
#define CODE_EOI 257   /* end-of-information code */
73
199k
#define CODE_FIRST 258 /* first free code entry */
74
0
#define CODE_MAX MAXCODE(BITS_MAX)
75
0
#define HSIZE 9001 /* 91% occupancy */
76
0
#define HSHIFT (13 - 8)
77
#ifdef LZW_COMPAT
78
/* NB: +1024 is for compatibility with old files */
79
146k
#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
5.66k
#define lzw_nbits base.nbits
103
2.37k
#define lzw_maxcode base.maxcode
104
0
#define lzw_free_ent base.free_ent
105
5.66k
#define lzw_nextdata base.nextdata
106
5.66k
#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
0
#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
26.9k
#define LZWState(tif) ((LZWBaseState *)(tif)->tif_data)
164
17.8k
#define LZWDecoderState(tif) ((LZWCodecState *)LZWState(tif))
165
6.06k
#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
static int LZWFixupTags(TIFF *tif)
177
2.89k
{
178
2.89k
    (void)tif;
179
2.89k
    return (1);
180
2.89k
}
181
182
static int LZWSetupDecode(TIFF *tif)
183
2.19k
{
184
2.19k
    static const char module[] = "LZWSetupDecode";
185
2.19k
    LZWCodecState *sp = LZWDecoderState(tif);
186
2.19k
    int code;
187
188
2.19k
    if (sp == NULL)
189
0
    {
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
0
        {
197
0
            TIFFErrorExtR(tif, module, "No space for LZW state block");
198
0
            return (0);
199
0
        }
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
0
    }
210
211
2.19k
    if (sp->dec_codetab == NULL)
212
1.84k
    {
213
1.84k
        sp->dec_codetab = (code_t *)_TIFFmallocExt(tif, CSIZE * sizeof(code_t));
214
1.84k
        if (sp->dec_codetab == NULL)
215
0
        {
216
0
            TIFFErrorExtR(tif, module, "No space for LZW code table");
217
0
            return (0);
218
0
        }
219
        /*
220
         * Pre-load the table.
221
         */
222
1.84k
        code = 255;
223
1.84k
        do
224
472k
        {
225
472k
            sp->dec_codetab[code].firstchar = (unsigned char)code;
226
472k
            sp->dec_codetab[code].value = (unsigned char)code;
227
472k
            sp->dec_codetab[code].repeated = true;
228
472k
            sp->dec_codetab[code].length = 1;
229
472k
            sp->dec_codetab[code].next = NULL;
230
472k
        } while (code--);
231
        /*
232
         * Zero-out the unused entries  */
233
        /* Silence false positive */
234
        /* coverity[overrun-buffer-arg] */
235
1.84k
        memset(&sp->dec_codetab[CODE_CLEAR], 0,
236
1.84k
               (CODE_FIRST - CODE_CLEAR) * sizeof(code_t));
237
1.84k
    }
238
2.19k
    return (1);
239
2.19k
}
240
241
/*
242
 * Setup state for decoding a strip.
243
 */
244
static int LZWPreDecode(TIFF *tif, uint16_t s)
245
2.37k
{
246
2.37k
    static const char module[] = "LZWPreDecode";
247
2.37k
    LZWCodecState *sp = LZWDecoderState(tif);
248
249
2.37k
    (void)s;
250
2.37k
    assert(sp != NULL);
251
2.37k
    if (sp->dec_codetab == NULL)
252
0
    {
253
0
        tif->tif_setupdecode(tif);
254
0
        if (sp->dec_codetab == NULL)
255
0
            return (0);
256
0
    }
257
258
    /*
259
     * Check for old bit-reversed codes.
260
     */
261
2.37k
    if (tif->tif_rawcc >= 2 && tif->tif_rawdata[0] == 0 &&
262
443
        (tif->tif_rawdata[1] & 0x1))
263
402
    {
264
402
#ifdef LZW_COMPAT
265
402
        if (!sp->dec_decode)
266
346
        {
267
346
            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
346
            tif->tif_decoderow = LZWDecodeCompat;
276
346
            tif->tif_decodestrip = LZWDecodeCompat;
277
346
            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
346
            (*tif->tif_setupdecode)(tif);
284
346
            sp->dec_decode = LZWDecodeCompat;
285
346
        }
286
402
        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
402
    }
296
1.96k
    else
297
1.96k
    {
298
1.96k
        sp->lzw_maxcode = MAXCODE(BITS_MIN) - 1;
299
1.96k
        sp->dec_decode = LZWDecode;
300
1.96k
    }
301
2.37k
    sp->lzw_nbits = BITS_MIN;
302
2.37k
    sp->lzw_nextbits = 0;
303
2.37k
    sp->lzw_nextdata = 0;
304
305
2.37k
    sp->dec_restart = 0;
306
2.37k
    sp->dec_nbitsmask = MAXCODE(BITS_MIN);
307
2.37k
    sp->dec_bitsleft = 0;
308
2.37k
    sp->old_tif_rawcc = 0;
309
2.37k
    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
2.37k
    sp->dec_oldcodep = &sp->dec_codetab[0];
318
2.37k
    sp->dec_maxcodep = &sp->dec_codetab[sp->dec_nbitsmask - 1];
319
2.37k
    sp->read_error = 0;
320
2.37k
    return (1);
321
2.37k
}
322
323
/*
324
 * Decode a "hunk of data".
325
 */
326
327
/* Get the next 32 or 64-bit from the input data */
328
#if 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
31.3k
    memcpy(&nextdata, bp, sizeof(nextdata));                                   \
336
31.3k
    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
195k
    do                                                                         \
363
195k
    {                                                                          \
364
195k
        nextbits -= nbits;                                                     \
365
195k
        if (nextbits < 0)                                                      \
366
195k
        {                                                                      \
367
32.1k
            if (dec_bitsleft >= 8 * SIZEOF_WORDTYPE)                           \
368
32.1k
            {                                                                  \
369
31.3k
                unsigned codetmp = (unsigned)(nextdata << (-nextbits));        \
370
31.3k
                GetNextData(nextdata, bp);                                     \
371
31.3k
                bp += SIZEOF_WORDTYPE;                                         \
372
31.3k
                nextbits += 8 * SIZEOF_WORDTYPE;                               \
373
31.3k
                dec_bitsleft -= 8 * SIZEOF_WORDTYPE;                           \
374
31.3k
                code = (WordType)((codetmp | (nextdata >> nextbits)) &         \
375
31.3k
                                  (WordType)nbitsmask);                        \
376
31.3k
                break;                                                         \
377
31.3k
            }                                                                  \
378
32.1k
            else                                                               \
379
32.1k
            {                                                                  \
380
846
                if (dec_bitsleft < 8)                                          \
381
846
                {                                                              \
382
64
                    goto no_eoi;                                               \
383
64
                }                                                              \
384
846
                nextdata = (nextdata << 8) | *(bp)++;                          \
385
782
                nextbits += 8;                                                 \
386
782
                dec_bitsleft -= 8;                                             \
387
782
                if (nextbits < 0)                                              \
388
782
                {                                                              \
389
299
                    if (dec_bitsleft < 8)                                      \
390
299
                    {                                                          \
391
44
                        goto no_eoi;                                           \
392
44
                    }                                                          \
393
299
                    nextdata = (nextdata << 8) | *(bp)++;                      \
394
255
                    nextbits += 8;                                             \
395
255
                    dec_bitsleft -= 8;                                         \
396
255
                }                                                              \
397
782
            }                                                                  \
398
32.1k
        }                                                                      \
399
195k
        code = (WordType)((nextdata >> nextbits) & (WordType)nbitsmask);       \
400
164k
    } while (0)
401
402
static int LZWDecode(TIFF *tif, uint8_t *op0, tmsize_t occ0, uint16_t s)
403
1.95k
{
404
1.95k
    static const char module[] = "LZWDecode";
405
1.95k
    LZWCodecState *sp = LZWDecoderState(tif);
406
1.95k
    uint8_t *op = (uint8_t *)op0;
407
1.95k
    tmsize_t occ = occ0;
408
1.95k
    uint8_t *bp;
409
1.95k
    long nbits, nextbits, nbitsmask;
410
1.95k
    WordType nextdata;
411
1.95k
    code_t *free_entp, *maxcodep, *oldcodep;
412
413
1.95k
    (void)s;
414
1.95k
    assert(sp != NULL);
415
1.95k
    assert(sp->dec_codetab != NULL);
416
417
1.95k
    if (sp->read_error)
418
0
    {
419
0
        memset(op, 0, (size_t)occ);
420
0
        TIFFErrorExtR(tif, module,
421
0
                      "LZWDecode: Scanline %" PRIu32 " cannot be read due to "
422
0
                      "previous error",
423
0
                      tif->tif_dir.td_row);
424
0
        return 0;
425
0
    }
426
427
    /*
428
     * Restart interrupted output operation.
429
     */
430
1.95k
    if (sp->dec_restart)
431
0
    {
432
0
        tmsize_t residue;
433
434
0
        code_t *codep = sp->dec_codep;
435
0
        residue = codep->length - sp->dec_restart;
436
0
        if (residue > occ)
437
0
        {
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
0
            sp->dec_restart += occ;
445
0
            do
446
0
            {
447
0
                codep = codep->next;
448
0
            } while (--residue > occ && codep);
449
0
            if (codep)
450
0
            {
451
0
                uint8_t *tp = op + occ;
452
0
                do
453
0
                {
454
0
                    *--tp = codep->value;
455
0
                    codep = codep->next;
456
0
                } while (--occ && codep);
457
0
            }
458
0
            return (1);
459
0
        }
460
        /*
461
         * Residue satisfies only part of the decode request.
462
         */
463
0
        op += residue;
464
0
        occ -= residue;
465
0
        uint8_t *tp = op;
466
0
        do
467
0
        {
468
0
            *--tp = codep->value;
469
0
            codep = codep->next;
470
0
        } while (--residue && codep);
471
0
        sp->dec_restart = 0;
472
0
    }
473
474
1.95k
    bp = (uint8_t *)tif->tif_rawcp;
475
1.95k
    sp->dec_bitsleft +=
476
1.95k
        (((uint64_t)tif->tif_rawcc - (uint64_t)sp->old_tif_rawcc) << 3);
477
1.95k
    uint64_t dec_bitsleft = sp->dec_bitsleft;
478
1.95k
    nbits = sp->lzw_nbits;
479
1.95k
    nextdata = sp->lzw_nextdata;
480
1.95k
    nextbits = sp->lzw_nextbits;
481
1.95k
    nbitsmask = sp->dec_nbitsmask;
482
1.95k
    oldcodep = sp->dec_oldcodep;
483
1.95k
    free_entp = sp->dec_free_entp;
484
1.95k
    maxcodep = sp->dec_maxcodep;
485
1.95k
    code_t *const dec_codetab = sp->dec_codetab;
486
1.95k
    code_t *codep;
487
488
1.95k
    if (occ == 0)
489
0
    {
490
0
        goto after_loop;
491
0
    }
492
493
194k
begin:
494
194k
{
495
194k
    WordType code;
496
194k
    GetNextCodeLZW();
497
194k
    codep = dec_codetab + (unsigned long)code;
498
194k
    if (code >= CODE_FIRST)
499
111k
        goto code_above_or_equal_to_258;
500
82.8k
    if (code < 256)
501
81.4k
        goto code_below_256;
502
1.34k
    if (code == CODE_EOI)
503
42
        goto after_loop;
504
1.30k
    goto code_clear;
505
506
81.4k
code_below_256:
507
81.4k
{
508
81.4k
    if (codep > free_entp)
509
840
        goto error_code;
510
80.6k
    free_entp->next = oldcodep;
511
80.6k
    free_entp->firstchar = oldcodep->firstchar;
512
80.6k
    free_entp->length = (uint16_t)(oldcodep->length + 1);
513
80.6k
    free_entp->value = (uint8_t)code;
514
80.6k
    free_entp->repeated =
515
80.6k
        (bool)(oldcodep->repeated & (oldcodep->value == code));
516
80.6k
    if (++free_entp > maxcodep)
517
296
    {
518
296
        if (++nbits > BITS_MAX) /* should not happen for a conformant encoder */
519
224
            nbits = BITS_MAX;
520
296
        nbitsmask = MAXCODE(nbits);
521
296
        maxcodep = dec_codetab + nbitsmask - 1;
522
296
        if (free_entp >= &dec_codetab[CSIZE])
523
0
        {
524
            /* At that point, the next valid states are either EOI or a */
525
            /* CODE_CLEAR. If a regular code is read, at the next */
526
            /* attempt at registering a new entry, we will error out */
527
            /* due to setting free_entp before any valid code */
528
0
            free_entp = dec_codetab - 1;
529
0
        }
530
296
    }
531
80.6k
    oldcodep = codep;
532
80.6k
    *op++ = (uint8_t)code;
533
80.6k
    occ--;
534
80.6k
    if (occ == 0)
535
115
        goto after_loop;
536
80.5k
    goto begin;
537
80.6k
}
538
539
111k
code_above_or_equal_to_258:
540
111k
{
541
    /*
542
     * Add the new entry to the code table.
543
     */
544
545
111k
    if (codep >= free_entp)
546
31.4k
    {
547
31.4k
        if (codep != free_entp)
548
316
            goto error_code;
549
31.1k
        free_entp->value = oldcodep->firstchar;
550
31.1k
    }
551
79.8k
    else
552
79.8k
    {
553
79.8k
        free_entp->value = codep->firstchar;
554
79.8k
    }
555
110k
    free_entp->repeated =
556
110k
        (bool)(oldcodep->repeated & (oldcodep->value == free_entp->value));
557
110k
    free_entp->next = oldcodep;
558
559
110k
    free_entp->firstchar = oldcodep->firstchar;
560
110k
    free_entp->length = (uint16_t)(oldcodep->length + 1);
561
110k
    if (++free_entp > maxcodep)
562
1.03k
    {
563
1.03k
        if (++nbits > BITS_MAX) /* should not happen for a conformant encoder */
564
833
            nbits = BITS_MAX;
565
1.03k
        nbitsmask = MAXCODE(nbits);
566
1.03k
        maxcodep = dec_codetab + nbitsmask - 1;
567
1.03k
        if (free_entp >= &dec_codetab[CSIZE])
568
0
        {
569
            /* At that point, the next valid states are either EOI or a */
570
            /* CODE_CLEAR. If a regular code is read, at the next */
571
            /* attempt at registering a new entry, we will error out */
572
            /* due to setting free_entp before any valid code */
573
0
            free_entp = dec_codetab - 1;
574
0
        }
575
1.03k
    }
576
110k
    oldcodep = codep;
577
578
    /*
579
     * Code maps to a string, copy string
580
     * value to output (written in reverse).
581
     */
582
    /* tiny bit faster on x86_64 to store in unsigned short than int */
583
110k
    unsigned short len = codep->length;
584
585
110k
    if (len < 3) /* equivalent to len == 2 given all other conditions */
586
37.4k
    {
587
37.4k
        if (occ <= 2)
588
88
        {
589
88
            if (occ == 2)
590
52
            {
591
52
                memcpy(op, &(codep->firstchar), 2);
592
52
                op += 2;
593
52
                occ -= 2;
594
52
                goto after_loop;
595
52
            }
596
36
            goto too_short_buffer;
597
88
        }
598
599
37.3k
        memcpy(op, &(codep->firstchar), 2);
600
37.3k
        op += 2;
601
37.3k
        occ -= 2;
602
37.3k
        goto begin; /* we can save the comparison occ > 0 */
603
37.4k
    }
604
605
73.4k
    if (len == 3)
606
16.8k
    {
607
16.8k
        if (occ <= 3)
608
65
        {
609
65
            if (occ == 3)
610
24
            {
611
24
                op[0] = codep->firstchar;
612
24
                op[1] = codep->next->value;
613
24
                op[2] = codep->value;
614
24
                op += 3;
615
24
                occ -= 3;
616
24
                goto after_loop;
617
24
            }
618
41
            goto too_short_buffer;
619
65
        }
620
621
16.7k
        op[0] = codep->firstchar;
622
16.7k
        op[1] = codep->next->value;
623
16.7k
        op[2] = codep->value;
624
16.7k
        op += 3;
625
16.7k
        occ -= 3;
626
16.7k
        goto begin; /* we can save the comparison occ > 0 */
627
16.8k
    }
628
629
56.6k
    if (len > occ)
630
219
    {
631
219
        goto too_short_buffer;
632
219
    }
633
634
56.4k
    if (codep->repeated)
635
33.9k
    {
636
33.9k
        memset(op, codep->value, len);
637
33.9k
        op += len;
638
33.9k
        occ -= len;
639
33.9k
        if (occ == 0)
640
69
            goto after_loop;
641
33.8k
        goto begin;
642
33.9k
    }
643
644
22.4k
    uint8_t *tp = op + len;
645
646
22.4k
    assert(len >= 4);
647
648
22.4k
    *--tp = codep->value;
649
22.4k
    codep = codep->next;
650
22.4k
    *--tp = codep->value;
651
22.4k
    codep = codep->next;
652
22.4k
    *--tp = codep->value;
653
22.4k
    codep = codep->next;
654
22.4k
    *--tp = codep->value;
655
22.4k
    if (tp > op)
656
16.9k
    {
657
16.9k
        do
658
435k
        {
659
435k
            codep = codep->next;
660
435k
            *--tp = codep->value;
661
435k
        } while (tp > op);
662
16.9k
    }
663
664
22.4k
    assert(occ >= len);
665
22.4k
    op += len;
666
22.4k
    occ -= len;
667
22.4k
    if (occ == 0)
668
29
        goto after_loop;
669
22.4k
    goto begin;
670
22.4k
}
671
672
22.4k
code_clear:
673
1.30k
{
674
1.30k
    free_entp = dec_codetab + CODE_FIRST;
675
1.30k
    nbits = BITS_MIN;
676
1.30k
    nbitsmask = MAXCODE(BITS_MIN);
677
1.30k
    maxcodep = dec_codetab + (unsigned long)nbitsmask - 1;
678
1.30k
    do
679
1.41k
    {
680
1.41k
        GetNextCodeLZW();
681
1.41k
    } while (code == CODE_CLEAR); /* consecutive CODE_CLEAR codes */
682
1.29k
    if (code == CODE_EOI)
683
13
        goto after_loop;
684
1.27k
    if (code > CODE_EOI)
685
19
    {
686
19
        goto error_code;
687
19
    }
688
1.25k
    *op++ = (uint8_t)code;
689
1.25k
    occ--;
690
1.25k
    oldcodep = dec_codetab + code;
691
1.25k
    if (occ == 0)
692
28
        goto after_loop;
693
1.23k
    goto begin;
694
1.25k
}
695
1.25k
}
696
697
1.23k
too_short_buffer:
698
296
{
699
    /*
700
     * String is too long for decode buffer,
701
     * locate portion that will fit, copy to
702
     * the decode buffer, and setup restart
703
     * logic for the next decoding call.
704
     */
705
296
    sp->dec_codep = codep;
706
296
    do
707
3.70k
    {
708
3.70k
        codep = codep->next;
709
3.70k
    } while (codep->length > occ);
710
711
296
    sp->dec_restart = occ;
712
296
    uint8_t *tp = op + occ;
713
296
    do
714
4.11k
    {
715
4.11k
        *--tp = codep->value;
716
4.11k
        codep = codep->next;
717
4.11k
    } while (--occ);
718
296
}
719
720
668
after_loop:
721
668
    tif->tif_rawcc -= (tmsize_t)((uint8_t *)bp - tif->tif_rawcp);
722
668
    tif->tif_rawcp = (uint8_t *)bp;
723
668
    sp->old_tif_rawcc = tif->tif_rawcc;
724
668
    sp->dec_bitsleft = dec_bitsleft;
725
668
    sp->lzw_nbits = (unsigned short)nbits;
726
668
    sp->lzw_nextdata = nextdata;
727
668
    sp->lzw_nextbits = nextbits;
728
668
    sp->dec_nbitsmask = nbitsmask;
729
668
    sp->dec_oldcodep = oldcodep;
730
668
    sp->dec_free_entp = free_entp;
731
668
    sp->dec_maxcodep = maxcodep;
732
733
668
    if (occ > 0)
734
55
    {
735
55
        memset(op, 0, (size_t)occ);
736
55
        sp->read_error = 1;
737
55
        TIFFErrorExtR(tif, module,
738
55
                      "Not enough data at scanline %" PRIu32 " (short %" PRIu64
739
55
                      " bytes)",
740
55
                      tif->tif_dir.td_row, (uint64_t)occ);
741
55
        return (0);
742
55
    }
743
613
    return (1);
744
745
108
no_eoi:
746
108
    memset(op, 0, (size_t)occ);
747
108
    sp->read_error = 1;
748
108
    TIFFErrorExtR(tif, module,
749
108
                  "LZWDecode: Strip %" PRIu32 " not terminated with EOI code",
750
108
                  tif->tif_dir.td_curstrip);
751
108
    return 0;
752
1.17k
error_code:
753
1.17k
    memset(op, 0, (size_t)occ);
754
1.17k
    sp->read_error = 1;
755
1.17k
    TIFFErrorExtR(tif, tif->tif_name, "Using code not yet in table");
756
1.17k
    return 0;
757
668
}
758
759
#ifdef LZW_COMPAT
760
761
/*
762
 * This check shouldn't be necessary because each
763
 * strip is suppose to be terminated with CODE_EOI.
764
 */
765
#define NextCode(_tif, _sp, _bp, _code, _get, dec_bitsleft)                    \
766
73.2k
    {                                                                          \
767
73.2k
        if (dec_bitsleft < (uint64_t)nbits)                                    \
768
73.2k
        {                                                                      \
769
69
            TIFFWarningExtR(_tif, module,                                      \
770
69
                            "LZWDecode: Strip %" PRIu32                        \
771
69
                            " not terminated with EOI code",                   \
772
69
                            _tif->tif_dir.td_curstrip);                        \
773
69
            _code = CODE_EOI;                                                  \
774
69
        }                                                                      \
775
73.2k
        else                                                                   \
776
73.2k
        {                                                                      \
777
73.1k
            _get(_sp, _bp, _code);                                             \
778
73.1k
            dec_bitsleft -= (uint64_t)nbits;                                   \
779
73.1k
        }                                                                      \
780
73.2k
    }
781
782
/*
783
 * Decode a "hunk of data" for old images.
784
 */
785
#define GetNextCodeCompat(sp, bp, code)                                        \
786
73.1k
    {                                                                          \
787
73.1k
        nextdata |= (unsigned long)*(bp)++ << nextbits;                        \
788
73.1k
        nextbits += 8;                                                         \
789
73.1k
        if (nextbits < nbits)                                                  \
790
73.1k
        {                                                                      \
791
21.4k
            nextdata |= (unsigned long)*(bp)++ << nextbits;                    \
792
21.4k
            nextbits += 8;                                                     \
793
21.4k
        }                                                                      \
794
73.1k
        code = (hcode_t)(nextdata & (unsigned long)nbitsmask);                 \
795
73.1k
        nextdata >>= nbits;                                                    \
796
73.1k
        nextbits -= nbits;                                                     \
797
73.1k
    }
798
799
static int LZWDecodeCompat(TIFF *tif, uint8_t *op0, tmsize_t occ0, uint16_t s)
800
419
{
801
419
    static const char module[] = "LZWDecodeCompat";
802
419
    LZWCodecState *sp = LZWDecoderState(tif);
803
419
    uint8_t *op = (uint8_t *)op0;
804
419
    tmsize_t occ = occ0;
805
419
    uint8_t *tp;
806
419
    uint8_t *bp;
807
419
    int code, nbits;
808
419
    int len;
809
419
    long nextbits, nbitsmask;
810
419
    WordType nextdata;
811
419
    code_t *codep, *free_entp, *maxcodep, *oldcodep;
812
813
419
    (void)s;
814
419
    assert(sp != NULL);
815
816
    /*
817
     * Restart interrupted output operation.
818
     */
819
419
    if (sp->dec_restart)
820
0
    {
821
0
        tmsize_t residue;
822
823
0
        codep = sp->dec_codep;
824
0
        residue = codep->length - sp->dec_restart;
825
0
        if (residue > occ)
826
0
        {
827
            /*
828
             * Residue from previous decode is sufficient
829
             * to satisfy decode request.  Skip to the
830
             * start of the decoded string, place decoded
831
             * values in the output buffer, and return.
832
             */
833
0
            sp->dec_restart += occ;
834
0
            do
835
0
            {
836
0
                codep = codep->next;
837
0
            } while (--residue > occ);
838
0
            tp = op + occ;
839
0
            do
840
0
            {
841
0
                *--tp = codep->value;
842
0
                codep = codep->next;
843
0
            } while (--occ);
844
0
            return (1);
845
0
        }
846
        /*
847
         * Residue satisfies only part of the decode request.
848
         */
849
0
        op += residue;
850
0
        occ -= residue;
851
0
        tp = op;
852
0
        do
853
0
        {
854
0
            *--tp = codep->value;
855
0
            codep = codep->next;
856
0
        } while (--residue);
857
0
        sp->dec_restart = 0;
858
0
    }
859
860
419
    bp = (uint8_t *)tif->tif_rawcp;
861
862
419
    sp->dec_bitsleft +=
863
419
        (((uint64_t)tif->tif_rawcc - (uint64_t)sp->old_tif_rawcc) << 3);
864
419
    uint64_t dec_bitsleft = sp->dec_bitsleft;
865
866
419
    nbits = sp->lzw_nbits;
867
419
    nextdata = sp->lzw_nextdata;
868
419
    nextbits = sp->lzw_nextbits;
869
419
    nbitsmask = sp->dec_nbitsmask;
870
419
    oldcodep = sp->dec_oldcodep;
871
419
    free_entp = sp->dec_free_entp;
872
419
    maxcodep = sp->dec_maxcodep;
873
874
72.2k
    while (occ > 0)
875
72.1k
    {
876
72.1k
        NextCode(tif, sp, bp, code, GetNextCodeCompat, dec_bitsleft);
877
72.1k
        if (code == CODE_EOI)
878
75
            break;
879
72.0k
        if (code == CODE_CLEAR)
880
874
        {
881
874
            do
882
1.10k
            {
883
1.10k
                free_entp = sp->dec_codetab + CODE_FIRST;
884
1.10k
                _TIFFmemset(free_entp, 0,
885
1.10k
                            (CSIZE - CODE_FIRST) * sizeof(code_t));
886
1.10k
                nbits = BITS_MIN;
887
1.10k
                nbitsmask = MAXCODE(BITS_MIN);
888
1.10k
                maxcodep = sp->dec_codetab + nbitsmask;
889
1.10k
                NextCode(tif, sp, bp, code, GetNextCodeCompat, dec_bitsleft);
890
1.10k
            } while (code == CODE_CLEAR); /* consecutive CODE_CLEAR codes */
891
874
            if (code == CODE_EOI)
892
19
                break;
893
855
            if (code > CODE_CLEAR)
894
32
            {
895
32
                TIFFErrorExtR(
896
32
                    tif, tif->tif_name,
897
32
                    "LZWDecode: Corrupted LZW table at scanline %" PRIu32,
898
32
                    tif->tif_dir.td_row);
899
32
                return (0);
900
32
            }
901
823
            *op++ = (uint8_t)code;
902
823
            occ--;
903
823
            oldcodep = sp->dec_codetab + code;
904
823
            continue;
905
855
        }
906
71.1k
        codep = sp->dec_codetab + code;
907
908
        /*
909
         * Add the new entry to the code table.
910
         */
911
71.1k
        if (free_entp < &sp->dec_codetab[0] ||
912
71.1k
            free_entp >= &sp->dec_codetab[CSIZE])
913
16
        {
914
16
            TIFFErrorExtR(tif, module,
915
16
                          "Corrupted LZW table at scanline %" PRIu32,
916
16
                          tif->tif_dir.td_row);
917
16
            return (0);
918
16
        }
919
920
71.1k
        free_entp->next = oldcodep;
921
71.1k
        if (free_entp->next < &sp->dec_codetab[0] ||
922
71.1k
            free_entp->next >= &sp->dec_codetab[CSIZE])
923
0
        {
924
0
            TIFFErrorExtR(tif, module,
925
0
                          "Corrupted LZW table at scanline %" PRIu32,
926
0
                          tif->tif_dir.td_row);
927
0
            return (0);
928
0
        }
929
71.1k
        free_entp->firstchar = free_entp->next->firstchar;
930
71.1k
        free_entp->length = (uint16_t)(free_entp->next->length + 1);
931
71.1k
        free_entp->value =
932
71.1k
            (codep < free_entp) ? codep->firstchar : free_entp->firstchar;
933
71.1k
        if (++free_entp > maxcodep)
934
1.55k
        {
935
1.55k
            if (++nbits > BITS_MAX) /* should not happen */
936
1.48k
                nbits = BITS_MAX;
937
1.55k
            nbitsmask = MAXCODE(nbits);
938
1.55k
            maxcodep = sp->dec_codetab + nbitsmask;
939
1.55k
        }
940
71.1k
        oldcodep = codep;
941
71.1k
        if (code >= 256)
942
3.14k
        {
943
            /*
944
             * Code maps to a string, copy string
945
             * value to output (written in reverse).
946
             */
947
3.14k
            if (codep->length == 0)
948
113
            {
949
113
                TIFFErrorExtR(
950
113
                    tif, module,
951
113
                    "Wrong length of decoded "
952
113
                    "string: data probably corrupted at scanline %" PRIu32,
953
113
                    tif->tif_dir.td_row);
954
113
                return (0);
955
113
            }
956
3.02k
            if (codep->length > occ)
957
29
            {
958
                /*
959
                 * String is too long for decode buffer,
960
                 * locate portion that will fit, copy to
961
                 * the decode buffer, and setup restart
962
                 * logic for the next decoding call.
963
                 */
964
29
                sp->dec_codep = codep;
965
29
                do
966
45
                {
967
45
                    codep = codep->next;
968
45
                } while (codep->length > occ);
969
29
                sp->dec_restart = occ;
970
29
                tp = op + occ;
971
29
                do
972
47
                {
973
47
                    *--tp = codep->value;
974
47
                    codep = codep->next;
975
47
                } while (--occ);
976
29
                break;
977
29
            }
978
3.00k
            len = codep->length;
979
3.00k
            tp = op + len;
980
3.00k
            do
981
6.49k
            {
982
6.49k
                *--tp = codep->value;
983
6.49k
                codep = codep->next;
984
6.49k
            } while (codep && tp > op);
985
3.00k
            assert(occ >= len);
986
3.00k
            op += len;
987
3.00k
            occ -= len;
988
3.00k
        }
989
68.0k
        else
990
68.0k
        {
991
68.0k
            *op++ = (uint8_t)code;
992
68.0k
            occ--;
993
68.0k
        }
994
71.1k
    }
995
996
258
    tif->tif_rawcc -= (tmsize_t)((uint8_t *)bp - tif->tif_rawcp);
997
258
    tif->tif_rawcp = (uint8_t *)bp;
998
999
258
    sp->old_tif_rawcc = tif->tif_rawcc;
1000
258
    sp->dec_bitsleft = dec_bitsleft;
1001
1002
258
    sp->lzw_nbits = (unsigned short)nbits;
1003
258
    sp->lzw_nextdata = nextdata;
1004
258
    sp->lzw_nextbits = nextbits;
1005
258
    sp->dec_nbitsmask = nbitsmask;
1006
258
    sp->dec_oldcodep = oldcodep;
1007
258
    sp->dec_free_entp = free_entp;
1008
258
    sp->dec_maxcodep = maxcodep;
1009
1010
258
    if (occ > 0)
1011
94
    {
1012
94
        TIFFErrorExtR(tif, module,
1013
94
                      "Not enough data at scanline %" PRIu32 " (short %" PRIu64
1014
94
                      " bytes)",
1015
94
                      tif->tif_dir.td_row, (uint64_t)occ);
1016
94
        return (0);
1017
94
    }
1018
164
    return (1);
1019
258
}
1020
#endif /* LZW_COMPAT */
1021
1022
#ifndef LZW_READ_ONLY
1023
1024
static void cl_hash(LZWCodecState *);
1025
1026
/*
1027
 * LZW Encoding.
1028
 */
1029
1030
static int LZWSetupEncode(TIFF *tif)
1031
0
{
1032
0
    static const char module[] = "LZWSetupEncode";
1033
0
    LZWCodecState *sp = LZWEncoderState(tif);
1034
1035
0
    assert(sp != NULL);
1036
0
    sp->enc_hashtab = (hash_t *)_TIFFmallocExt(tif, HSIZE * sizeof(hash_t));
1037
0
    if (sp->enc_hashtab == NULL)
1038
0
    {
1039
0
        TIFFErrorExtR(tif, module, "No space for LZW hash table");
1040
0
        return (0);
1041
0
    }
1042
0
    return (1);
1043
0
}
1044
1045
/*
1046
 * Reset encoding state at the start of a strip.
1047
 */
1048
static int LZWPreEncode(TIFF *tif, uint16_t s)
1049
0
{
1050
0
    LZWCodecState *sp = LZWEncoderState(tif);
1051
1052
0
    (void)s;
1053
0
    assert(sp != NULL);
1054
1055
0
    if (sp->enc_hashtab == NULL)
1056
0
    {
1057
0
        tif->tif_setupencode(tif);
1058
0
    }
1059
1060
0
    sp->lzw_nbits = BITS_MIN;
1061
0
    sp->lzw_maxcode = MAXCODE(BITS_MIN);
1062
0
    sp->lzw_free_ent = CODE_FIRST;
1063
0
    sp->lzw_nextbits = 0;
1064
0
    sp->lzw_nextdata = 0;
1065
0
    sp->enc_checkpoint = CHECK_GAP;
1066
0
    sp->enc_ratio = 0;
1067
0
    sp->enc_incount = 0;
1068
0
    sp->enc_outcount = 0;
1069
    /*
1070
     * The 4 here insures there is space for 2 max-sized
1071
     * codes in LZWEncode and LZWPostDecode.
1072
     */
1073
0
    sp->enc_rawlimit = tif->tif_rawdata + tif->tif_rawdatasize - 1 - 4;
1074
0
    cl_hash(sp);                   /* clear hash table */
1075
0
    sp->enc_oldcode = (hcode_t)-1; /* generates CODE_CLEAR in LZWEncode */
1076
0
    return (1);
1077
0
}
1078
1079
#define CALCRATIO(sp, rat)                                                     \
1080
0
    {                                                                          \
1081
0
        if (incount > 0x007fffff)                                              \
1082
0
        { /* NB: shift will overflow */                                        \
1083
0
            rat = outcount >> 8;                                               \
1084
0
            rat = (rat == 0 ? 0x7fffffff : incount / rat);                     \
1085
0
        }                                                                      \
1086
0
        else                                                                   \
1087
0
            rat = (incount << 8) / outcount;                                   \
1088
0
    }
1089
1090
/* Explicit 0xff masking to make icc -check=conversions happy */
1091
#define PutNextCode(op, c)                                                     \
1092
0
    {                                                                          \
1093
0
        nextdata = (nextdata << nbits) | c;                                    \
1094
0
        nextbits += nbits;                                                     \
1095
0
        *op++ = (unsigned char)((nextdata >> (nextbits - 8)) & 0xff);          \
1096
0
        nextbits -= 8;                                                         \
1097
0
        if (nextbits >= 8)                                                     \
1098
0
        {                                                                      \
1099
0
            *op++ = (unsigned char)((nextdata >> (nextbits - 8)) & 0xff);      \
1100
0
            nextbits -= 8;                                                     \
1101
0
        }                                                                      \
1102
0
        outcount += nbits;                                                     \
1103
0
    }
1104
1105
/*
1106
 * Encode a chunk of pixels.
1107
 *
1108
 * Uses an open addressing double hashing (no chaining) on the
1109
 * prefix code/next character combination.  We do a variant of
1110
 * Knuth's algorithm D (vol. 3, sec. 6.4) along with G. Knott's
1111
 * relatively-prime secondary probe.  Here, the modular division
1112
 * first probe is gives way to a faster exclusive-or manipulation.
1113
 * Also do block compression with an adaptive reset, whereby the
1114
 * code table is cleared when the compression ratio decreases,
1115
 * but after the table fills.  The variable-length output codes
1116
 * are re-sized at this point, and a CODE_CLEAR is generated
1117
 * for the decoder.
1118
 */
1119
static int LZWEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
1120
0
{
1121
0
    LZWCodecState *sp = LZWEncoderState(tif);
1122
0
    long fcode;
1123
0
    hash_t *hp;
1124
0
    int h, c;
1125
0
    hcode_t ent;
1126
0
    long disp;
1127
0
    tmsize_t incount, outcount, checkpoint;
1128
0
    WordType nextdata;
1129
0
    long nextbits;
1130
0
    int free_ent, maxcode, nbits;
1131
0
    uint8_t *op;
1132
0
    uint8_t *limit;
1133
1134
0
    (void)s;
1135
0
    if (sp == NULL)
1136
0
        return (0);
1137
1138
0
    assert(sp->enc_hashtab != NULL);
1139
1140
    /*
1141
     * Load local state.
1142
     */
1143
0
    incount = sp->enc_incount;
1144
0
    outcount = sp->enc_outcount;
1145
0
    checkpoint = sp->enc_checkpoint;
1146
0
    nextdata = sp->lzw_nextdata;
1147
0
    nextbits = sp->lzw_nextbits;
1148
0
    free_ent = sp->lzw_free_ent;
1149
0
    maxcode = sp->lzw_maxcode;
1150
0
    nbits = sp->lzw_nbits;
1151
0
    op = tif->tif_rawcp;
1152
0
    limit = sp->enc_rawlimit;
1153
0
    ent = (hcode_t)sp->enc_oldcode;
1154
1155
0
    if (ent == (hcode_t)-1 && cc > 0)
1156
0
    {
1157
        /*
1158
         * NB: This is safe because it can only happen
1159
         *     at the start of a strip where we know there
1160
         *     is space in the data buffer.
1161
         */
1162
0
        PutNextCode(op, CODE_CLEAR);
1163
0
        ent = *bp++;
1164
0
        cc--;
1165
0
        incount++;
1166
0
    }
1167
0
    while (cc > 0)
1168
0
    {
1169
0
        c = *bp++;
1170
0
        cc--;
1171
0
        incount++;
1172
0
        fcode = ((long)c << BITS_MAX) + ent;
1173
0
        h = (c << HSHIFT) ^ ent; /* xor hashing */
1174
#ifdef _WINDOWS
1175
        /*
1176
         * Check hash index for an overflow.
1177
         */
1178
        if (h >= HSIZE)
1179
            h -= HSIZE;
1180
#endif
1181
0
        hp = &sp->enc_hashtab[h];
1182
0
        if (hp->hash == fcode)
1183
0
        {
1184
0
            ent = hp->code;
1185
0
            continue;
1186
0
        }
1187
0
        if (hp->hash >= 0)
1188
0
        {
1189
            /*
1190
             * Primary hash failed, check secondary hash.
1191
             */
1192
0
            disp = HSIZE - h;
1193
0
            if (h == 0)
1194
0
                disp = 1;
1195
0
            do
1196
0
            {
1197
                /*
1198
                 * Avoid pointer arithmetic because of
1199
                 * wraparound problems with segments.
1200
                 */
1201
0
                if ((h -= (int)disp) < 0)
1202
0
                    h += HSIZE;
1203
0
                hp = &sp->enc_hashtab[h];
1204
0
                if (hp->hash == fcode)
1205
0
                {
1206
0
                    ent = hp->code;
1207
0
                    goto hit;
1208
0
                }
1209
0
            } while (hp->hash >= 0);
1210
0
        }
1211
        /*
1212
         * New entry, emit code and add to table.
1213
         */
1214
        /*
1215
         * Verify there is space in the buffer for the code
1216
         * and any potential Clear code that might be emitted
1217
         * below.  The value of limit is setup so that there
1218
         * are at least 4 bytes free--room for 2 codes.
1219
         */
1220
0
        if (op > limit)
1221
0
        {
1222
0
            tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata);
1223
0
            if (!TIFFFlushData1(tif))
1224
0
                return 0;
1225
0
            op = tif->tif_rawdata;
1226
0
        }
1227
0
        PutNextCode(op, ent);
1228
0
        ent = (hcode_t)c;
1229
0
        hp->code = (hcode_t)(free_ent++);
1230
0
        hp->hash = fcode;
1231
0
        if (free_ent == CODE_MAX - 1)
1232
0
        {
1233
            /* table is full, emit clear code and reset */
1234
0
            cl_hash(sp);
1235
0
            sp->enc_ratio = 0;
1236
0
            incount = 0;
1237
0
            outcount = 0;
1238
0
            free_ent = CODE_FIRST;
1239
0
            PutNextCode(op, CODE_CLEAR);
1240
0
            nbits = BITS_MIN;
1241
0
            maxcode = MAXCODE(BITS_MIN);
1242
0
        }
1243
0
        else
1244
0
        {
1245
            /*
1246
             * If the next entry is going to be too big for
1247
             * the code size, then increase it, if possible.
1248
             */
1249
0
            if (free_ent > maxcode)
1250
0
            {
1251
0
                nbits++;
1252
0
                assert(nbits <= BITS_MAX);
1253
0
                maxcode = (int)MAXCODE(nbits);
1254
0
            }
1255
0
            else if (incount >= checkpoint)
1256
0
            {
1257
0
                tmsize_t rat;
1258
                /*
1259
                 * Check compression ratio and, if things seem
1260
                 * to be slipping, clear the hash table and
1261
                 * reset state.  The compression ratio is a
1262
                 * 24+8-bit fractional number.
1263
                 */
1264
0
                checkpoint = incount + CHECK_GAP;
1265
0
                CALCRATIO(sp, rat);
1266
0
                if (rat <= sp->enc_ratio)
1267
0
                {
1268
0
                    cl_hash(sp);
1269
0
                    sp->enc_ratio = 0;
1270
0
                    incount = 0;
1271
0
                    outcount = 0;
1272
0
                    free_ent = CODE_FIRST;
1273
0
                    PutNextCode(op, CODE_CLEAR);
1274
0
                    nbits = BITS_MIN;
1275
0
                    maxcode = MAXCODE(BITS_MIN);
1276
0
                }
1277
0
                else
1278
0
                    sp->enc_ratio = rat;
1279
0
            }
1280
0
        }
1281
0
    hit:;
1282
0
    }
1283
1284
    /*
1285
     * Restore global state.
1286
     */
1287
0
    sp->enc_incount = incount;
1288
0
    sp->enc_outcount = outcount;
1289
0
    sp->enc_checkpoint = checkpoint;
1290
0
    sp->enc_oldcode = ent;
1291
0
    sp->lzw_nextdata = nextdata;
1292
0
    sp->lzw_nextbits = nextbits;
1293
0
    sp->lzw_free_ent = (unsigned short)free_ent;
1294
0
    sp->lzw_maxcode = (unsigned short)maxcode;
1295
0
    sp->lzw_nbits = (unsigned short)nbits;
1296
0
    tif->tif_rawcp = op;
1297
0
    return (1);
1298
0
}
1299
1300
/*
1301
 * Finish off an encoded strip by flushing the last
1302
 * string and tacking on an End Of Information code.
1303
 */
1304
static int LZWPostEncode(TIFF *tif)
1305
0
{
1306
0
    LZWCodecState *sp = LZWEncoderState(tif);
1307
0
    uint8_t *op = tif->tif_rawcp;
1308
0
    long nextbits = sp->lzw_nextbits;
1309
0
    WordType nextdata = sp->lzw_nextdata;
1310
0
    tmsize_t outcount = sp->enc_outcount;
1311
0
    int nbits = sp->lzw_nbits;
1312
1313
0
    if (op > sp->enc_rawlimit)
1314
0
    {
1315
0
        tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata);
1316
0
        if (!TIFFFlushData1(tif))
1317
0
            return 0;
1318
0
        op = tif->tif_rawdata;
1319
0
    }
1320
0
    if (sp->enc_oldcode != (hcode_t)-1)
1321
0
    {
1322
0
        int free_ent = sp->lzw_free_ent;
1323
1324
0
        PutNextCode(op, (WordType)sp->enc_oldcode);
1325
0
        sp->enc_oldcode = (hcode_t)-1;
1326
0
        free_ent++;
1327
1328
0
        if (free_ent == CODE_MAX - 1)
1329
0
        {
1330
            /* table is full, emit clear code and reset */
1331
0
            outcount = 0;
1332
0
            PutNextCode(op, CODE_CLEAR);
1333
0
            nbits = BITS_MIN;
1334
0
        }
1335
0
        else
1336
0
        {
1337
            /*
1338
             * If the next entry is going to be too big for
1339
             * the code size, then increase it, if possible.
1340
             */
1341
0
            if (free_ent > sp->lzw_maxcode)
1342
0
            {
1343
0
                nbits++;
1344
0
                assert(nbits <= BITS_MAX);
1345
0
            }
1346
0
        }
1347
0
    }
1348
0
    PutNextCode(op, CODE_EOI);
1349
    /* Explicit 0xff masking to make icc -check=conversions happy */
1350
0
    if (nextbits > 0)
1351
0
        *op++ = (unsigned char)((nextdata << (8 - nextbits)) & 0xff);
1352
0
    tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata);
1353
0
    (void)outcount;
1354
0
    return (1);
1355
0
}
1356
1357
/*
1358
 * Reset encoding hash table.
1359
 */
1360
static void cl_hash(LZWCodecState *sp)
1361
0
{
1362
0
    hash_t *hp = &sp->enc_hashtab[HSIZE - 1];
1363
0
    long i = HSIZE - 8;
1364
1365
0
    do
1366
0
    {
1367
0
        i -= 8;
1368
0
        hp[-7].hash = -1;
1369
0
        hp[-6].hash = -1;
1370
0
        hp[-5].hash = -1;
1371
0
        hp[-4].hash = -1;
1372
0
        hp[-3].hash = -1;
1373
0
        hp[-2].hash = -1;
1374
0
        hp[-1].hash = -1;
1375
0
        hp[0].hash = -1;
1376
0
        hp -= 8;
1377
0
    } while (i >= 0);
1378
0
    for (i += 8; i > 0; i--, hp--)
1379
0
        hp->hash = -1;
1380
0
}
1381
1382
#endif
1383
1384
static void LZWCleanup(TIFF *tif)
1385
3.03k
{
1386
3.03k
    (void)TIFFPredictorCleanup(tif);
1387
1388
3.03k
    assert(tif->tif_data != NULL);
1389
1390
3.03k
    if (LZWDecoderState(tif)->dec_codetab)
1391
1.84k
        _TIFFfreeExt(tif, LZWDecoderState(tif)->dec_codetab);
1392
1393
3.03k
    if (LZWEncoderState(tif)->enc_hashtab)
1394
0
        _TIFFfreeExt(tif, LZWEncoderState(tif)->enc_hashtab);
1395
1396
3.03k
    _TIFFfreeExt(tif, tif->tif_data);
1397
3.03k
    tif->tif_data = NULL;
1398
1399
3.03k
    _TIFFSetDefaultCompressionState(tif);
1400
3.03k
}
1401
1402
static uint64_t LZWGetMaxCompressionRatio(TIFF *tif)
1403
3
{
1404
3
    (void)tif;
1405
1406
    /* See README_for_libtiff_developpers.md for raw data used to estimate
1407
     * the maximum compression rate. */
1408
1409
    /* 1024x1024: 562 */
1410
    /* 4096x4096: 1243 */
1411
    /* 16383x16383: 1353 */
1412
    /* 65536x65536: 1362 */
1413
1414
3
    return 1400;
1415
3
}
1416
1417
int TIFFInitLZW(TIFF *tif, int scheme)
1418
3.03k
{
1419
3.03k
    static const char module[] = "TIFFInitLZW";
1420
3.03k
    (void)scheme;
1421
3.03k
    assert(scheme == COMPRESSION_LZW);
1422
    /*
1423
     * Allocate state block so tag methods have storage to record values.
1424
     */
1425
3.03k
    tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(LZWCodecState));
1426
3.03k
    if (tif->tif_data == NULL)
1427
0
        goto bad;
1428
3.03k
    LZWDecoderState(tif)->dec_codetab = NULL;
1429
3.03k
    LZWDecoderState(tif)->dec_decode = NULL;
1430
3.03k
    LZWEncoderState(tif)->enc_hashtab = NULL;
1431
3.03k
    LZWState(tif)->rw_mode = tif->tif_mode;
1432
1433
    /*
1434
     * Install codec methods.
1435
     */
1436
3.03k
    tif->tif_fixuptags = LZWFixupTags;
1437
3.03k
    tif->tif_setupdecode = LZWSetupDecode;
1438
3.03k
    tif->tif_predecode = LZWPreDecode;
1439
3.03k
    tif->tif_decoderow = LZWDecode;
1440
3.03k
    tif->tif_decodestrip = LZWDecode;
1441
3.03k
    tif->tif_decodetile = LZWDecode;
1442
3.03k
#ifndef LZW_READ_ONLY
1443
3.03k
    tif->tif_setupencode = LZWSetupEncode;
1444
3.03k
    tif->tif_preencode = LZWPreEncode;
1445
3.03k
    tif->tif_postencode = LZWPostEncode;
1446
3.03k
    tif->tif_encoderow = LZWEncode;
1447
3.03k
    tif->tif_encodestrip = LZWEncode;
1448
3.03k
    tif->tif_encodetile = LZWEncode;
1449
3.03k
#endif
1450
3.03k
    tif->tif_getmaxcompressionratio = LZWGetMaxCompressionRatio;
1451
3.03k
    tif->tif_cleanup = LZWCleanup;
1452
    /*
1453
     * Setup predictor setup.
1454
     */
1455
3.03k
    (void)TIFFPredictorInit(tif);
1456
3.03k
    return (1);
1457
0
bad:
1458
0
    TIFFErrorExtR(tif, module, "No space for LZW state block");
1459
0
    return (0);
1460
3.03k
}
1461
1462
/*
1463
 * Copyright (c) 1985, 1986 The Regents of the University of California.
1464
 * All rights reserved.
1465
 *
1466
 * This code is derived from software contributed to Berkeley by
1467
 * James A. Woods, derived from original work by Spencer Thomas
1468
 * and Joseph Orost.
1469
 *
1470
 * Redistribution and use in source and binary forms are permitted
1471
 * provided that the above copyright notice and this paragraph are
1472
 * duplicated in all such forms and that any documentation,
1473
 * advertising materials, and other materials related to such
1474
 * distribution and use acknowledge that the software was developed
1475
 * by the University of California, Berkeley.  The name of the
1476
 * University may not be used to endorse or promote products derived
1477
 * from this software without specific prior written permission.
1478
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1479
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1480
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1481
 */
1482
#endif /* LZW_SUPPORT */