Coverage Report

Created: 2026-06-30 07:12

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
158k
#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
5.59k
#define BITS_MIN 9  /* start with 9 bits */
69
5.67k
#define BITS_MAX 12 /* max of 12 bit strings */
70
/* predefined codes */
71
80.6k
#define CODE_CLEAR 256 /* code to clear string table */
72
77.9k
#define CODE_EOI 257   /* end-of-information code */
73
200k
#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
147k
#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
6.26k
#define lzw_nbits base.nbits
103
2.57k
#define lzw_maxcode base.maxcode
104
0
#define lzw_free_ent base.free_ent
105
6.26k
#define lzw_nextdata base.nextdata
106
6.26k
#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
34.2k
#define LZWState(tif) ((LZWBaseState *)(tif)->tif_data)
164
21.7k
#define LZWDecoderState(tif) ((LZWCodecState *)LZWState(tif))
165
8.32k
#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
4.04k
{
178
4.04k
    (void)tif;
179
4.04k
    return (1);
180
4.04k
}
181
182
static int LZWSetupDecode(TIFF *tif)
183
2.22k
{
184
2.22k
    static const char module[] = "LZWSetupDecode";
185
2.22k
    LZWCodecState *sp = LZWDecoderState(tif);
186
2.22k
    int code;
187
188
2.22k
    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.22k
    if (sp->dec_codetab == NULL)
212
1.87k
    {
213
1.87k
        sp->dec_codetab = (code_t *)_TIFFmallocExt(tif, CSIZE * sizeof(code_t));
214
1.87k
        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.87k
        code = 255;
223
1.87k
        do
224
478k
        {
225
478k
            sp->dec_codetab[code].firstchar = (unsigned char)code;
226
478k
            sp->dec_codetab[code].value = (unsigned char)code;
227
478k
            sp->dec_codetab[code].repeated = true;
228
478k
            sp->dec_codetab[code].length = 1;
229
478k
            sp->dec_codetab[code].next = NULL;
230
478k
        } while (code--);
231
        /*
232
         * Zero-out the unused entries  */
233
        /* Silence false positive */
234
        /* coverity[overrun-buffer-arg] */
235
1.87k
        memset(&sp->dec_codetab[CODE_CLEAR], 0,
236
1.87k
               (CODE_FIRST - CODE_CLEAR) * sizeof(code_t));
237
1.87k
    }
238
2.22k
    return (1);
239
2.22k
}
240
241
/*
242
 * Setup state for decoding a strip.
243
 */
244
static int LZWPreDecode(TIFF *tif, uint16_t s)
245
2.57k
{
246
2.57k
    static const char module[] = "LZWPreDecode";
247
2.57k
    LZWCodecState *sp = LZWDecoderState(tif);
248
249
2.57k
    (void)s;
250
2.57k
    assert(sp != NULL);
251
2.57k
    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.57k
    if (tif->tif_rawcc >= 2 && tif->tif_rawdata[0] == 0 &&
262
622
        (tif->tif_rawdata[1] & 0x1))
263
578
    {
264
578
#ifdef LZW_COMPAT
265
578
        if (!sp->dec_decode)
266
350
        {
267
350
            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
350
            tif->tif_decoderow = LZWDecodeCompat;
276
350
            tif->tif_decodestrip = LZWDecodeCompat;
277
350
            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
350
            (*tif->tif_setupdecode)(tif);
284
350
            sp->dec_decode = LZWDecodeCompat;
285
350
        }
286
578
        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
578
    }
296
2.00k
    else
297
2.00k
    {
298
2.00k
        sp->lzw_maxcode = MAXCODE(BITS_MIN) - 1;
299
2.00k
        sp->dec_decode = LZWDecode;
300
2.00k
    }
301
2.57k
    sp->lzw_nbits = BITS_MIN;
302
2.57k
    sp->lzw_nextbits = 0;
303
2.57k
    sp->lzw_nextdata = 0;
304
305
2.57k
    sp->dec_restart = 0;
306
2.57k
    sp->dec_nbitsmask = MAXCODE(BITS_MIN);
307
2.57k
    sp->dec_bitsleft = 0;
308
2.57k
    sp->old_tif_rawcc = 0;
309
2.57k
    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.57k
    sp->dec_oldcodep = &sp->dec_codetab[0];
318
2.57k
    sp->dec_maxcodep = &sp->dec_codetab[sp->dec_nbitsmask - 1];
319
2.57k
    sp->read_error = 0;
320
2.57k
    return (1);
321
2.57k
}
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.2k
            if (dec_bitsleft >= 8 * SIZEOF_WORDTYPE)                           \
368
32.2k
            {                                                                  \
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.2k
            else                                                               \
379
32.2k
            {                                                                  \
380
834
                if (dec_bitsleft < 8)                                          \
381
834
                {                                                              \
382
62
                    goto no_eoi;                                               \
383
62
                }                                                              \
384
834
                nextdata = (nextdata << 8) | *(bp)++;                          \
385
772
                nextbits += 8;                                                 \
386
772
                dec_bitsleft -= 8;                                             \
387
772
                if (nextbits < 0)                                              \
388
772
                {                                                              \
389
301
                    if (dec_bitsleft < 8)                                      \
390
301
                    {                                                          \
391
44
                        goto no_eoi;                                           \
392
44
                    }                                                          \
393
301
                    nextdata = (nextdata << 8) | *(bp)++;                      \
394
257
                    nextbits += 8;                                             \
395
257
                    dec_bitsleft -= 8;                                         \
396
257
                }                                                              \
397
772
            }                                                                  \
398
32.2k
        }                                                                      \
399
195k
        code = (WordType)((nextdata >> nextbits) & (WordType)nbitsmask);       \
400
163k
    } while (0)
401
402
static int LZWDecode(TIFF *tif, uint8_t *op0, tmsize_t occ0, uint16_t s)
403
1.97k
{
404
1.97k
    static const char module[] = "LZWDecode";
405
1.97k
    LZWCodecState *sp = LZWDecoderState(tif);
406
1.97k
    uint8_t *op = (uint8_t *)op0;
407
1.97k
    tmsize_t occ = occ0;
408
1.97k
    uint8_t *bp;
409
1.97k
    long nbits, nextbits, nbitsmask;
410
1.97k
    WordType nextdata;
411
1.97k
    code_t *free_entp, *maxcodep, *oldcodep;
412
413
1.97k
    (void)s;
414
1.97k
    assert(sp != NULL);
415
1.97k
    assert(sp->dec_codetab != NULL);
416
417
1.97k
    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.97k
    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.97k
    bp = (uint8_t *)tif->tif_rawcp;
475
1.97k
    sp->dec_bitsleft +=
476
1.97k
        (((uint64_t)tif->tif_rawcc - (uint64_t)sp->old_tif_rawcc) << 3);
477
1.97k
    uint64_t dec_bitsleft = sp->dec_bitsleft;
478
1.97k
    nbits = sp->lzw_nbits;
479
1.97k
    nextdata = sp->lzw_nextdata;
480
1.97k
    nextbits = sp->lzw_nextbits;
481
1.97k
    nbitsmask = sp->dec_nbitsmask;
482
1.97k
    oldcodep = sp->dec_oldcodep;
483
1.97k
    free_entp = sp->dec_free_entp;
484
1.97k
    maxcodep = sp->dec_maxcodep;
485
1.97k
    code_t *const dec_codetab = sp->dec_codetab;
486
1.97k
    code_t *codep;
487
488
1.97k
    if (occ == 0)
489
0
    {
490
0
        goto after_loop;
491
0
    }
492
493
193k
begin:
494
193k
{
495
193k
    WordType code;
496
193k
    GetNextCodeLZW();
497
193k
    codep = dec_codetab + (unsigned long)code;
498
193k
    if (code >= CODE_FIRST)
499
109k
        goto code_above_or_equal_to_258;
500
83.9k
    if (code < 256)
501
82.5k
        goto code_below_256;
502
1.36k
    if (code == CODE_EOI)
503
41
        goto after_loop;
504
1.32k
    goto code_clear;
505
506
82.5k
code_below_256:
507
82.5k
{
508
82.5k
    if (codep > free_entp)
509
844
        goto error_code;
510
81.7k
    free_entp->next = oldcodep;
511
81.7k
    free_entp->firstchar = oldcodep->firstchar;
512
81.7k
    free_entp->length = (uint16_t)(oldcodep->length + 1);
513
81.7k
    free_entp->value = (uint8_t)code;
514
81.7k
    free_entp->repeated =
515
81.7k
        (bool)(oldcodep->repeated & (oldcodep->value == code));
516
81.7k
    if (++free_entp > maxcodep)
517
297
    {
518
297
        if (++nbits > BITS_MAX) /* should not happen for a conformant encoder */
519
232
            nbits = BITS_MAX;
520
297
        nbitsmask = MAXCODE(nbits);
521
297
        maxcodep = dec_codetab + nbitsmask - 1;
522
297
        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
297
    }
531
81.7k
    oldcodep = codep;
532
81.7k
    *op++ = (uint8_t)code;
533
81.7k
    occ--;
534
81.7k
    if (occ == 0)
535
109
        goto after_loop;
536
81.6k
    goto begin;
537
81.7k
}
538
539
109k
code_above_or_equal_to_258:
540
109k
{
541
    /*
542
     * Add the new entry to the code table.
543
     */
544
545
109k
    if (codep >= free_entp)
546
30.1k
    {
547
30.1k
        if (codep != free_entp)
548
341
            goto error_code;
549
29.8k
        free_entp->value = oldcodep->firstchar;
550
29.8k
    }
551
79.3k
    else
552
79.3k
    {
553
79.3k
        free_entp->value = codep->firstchar;
554
79.3k
    }
555
109k
    free_entp->repeated =
556
109k
        (bool)(oldcodep->repeated & (oldcodep->value == free_entp->value));
557
109k
    free_entp->next = oldcodep;
558
559
109k
    free_entp->firstchar = oldcodep->firstchar;
560
109k
    free_entp->length = (uint16_t)(oldcodep->length + 1);
561
109k
    if (++free_entp > maxcodep)
562
1.15k
    {
563
1.15k
        if (++nbits > BITS_MAX) /* should not happen for a conformant encoder */
564
956
            nbits = BITS_MAX;
565
1.15k
        nbitsmask = MAXCODE(nbits);
566
1.15k
        maxcodep = dec_codetab + nbitsmask - 1;
567
1.15k
        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.15k
    }
576
109k
    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
109k
    unsigned short len = codep->length;
584
585
109k
    if (len < 3) /* equivalent to len == 2 given all other conditions */
586
37.0k
    {
587
37.0k
        if (occ <= 2)
588
90
        {
589
90
            if (occ == 2)
590
53
            {
591
53
                memcpy(op, &(codep->firstchar), 2);
592
53
                op += 2;
593
53
                occ -= 2;
594
53
                goto after_loop;
595
53
            }
596
37
            goto too_short_buffer;
597
90
        }
598
599
36.9k
        memcpy(op, &(codep->firstchar), 2);
600
36.9k
        op += 2;
601
36.9k
        occ -= 2;
602
36.9k
        goto begin; /* we can save the comparison occ > 0 */
603
37.0k
    }
604
605
72.1k
    if (len == 3)
606
16.8k
    {
607
16.8k
        if (occ <= 3)
608
66
        {
609
66
            if (occ == 3)
610
26
            {
611
26
                op[0] = codep->firstchar;
612
26
                op[1] = codep->next->value;
613
26
                op[2] = codep->value;
614
26
                op += 3;
615
26
                occ -= 3;
616
26
                goto after_loop;
617
26
            }
618
40
            goto too_short_buffer;
619
66
        }
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
55.3k
    if (len > occ)
630
224
    {
631
224
        goto too_short_buffer;
632
224
    }
633
634
55.0k
    if (codep->repeated)
635
31.9k
    {
636
31.9k
        memset(op, codep->value, len);
637
31.9k
        op += len;
638
31.9k
        occ -= len;
639
31.9k
        if (occ == 0)
640
66
            goto after_loop;
641
31.9k
        goto begin;
642
31.9k
    }
643
644
23.1k
    uint8_t *tp = op + len;
645
646
23.1k
    assert(len >= 4);
647
648
23.1k
    *--tp = codep->value;
649
23.1k
    codep = codep->next;
650
23.1k
    *--tp = codep->value;
651
23.1k
    codep = codep->next;
652
23.1k
    *--tp = codep->value;
653
23.1k
    codep = codep->next;
654
23.1k
    *--tp = codep->value;
655
23.1k
    if (tp > op)
656
17.7k
    {
657
17.7k
        do
658
465k
        {
659
465k
            codep = codep->next;
660
465k
            *--tp = codep->value;
661
465k
        } while (tp > op);
662
17.7k
    }
663
664
23.1k
    assert(occ >= len);
665
23.1k
    op += len;
666
23.1k
    occ -= len;
667
23.1k
    if (occ == 0)
668
29
        goto after_loop;
669
23.0k
    goto begin;
670
23.1k
}
671
672
23.0k
code_clear:
673
1.32k
{
674
1.32k
    free_entp = dec_codetab + CODE_FIRST;
675
1.32k
    nbits = BITS_MIN;
676
1.32k
    nbitsmask = MAXCODE(BITS_MIN);
677
1.32k
    maxcodep = dec_codetab + (unsigned long)nbitsmask - 1;
678
1.32k
    do
679
1.43k
    {
680
1.43k
        GetNextCodeLZW();
681
1.43k
    } while (code == CODE_CLEAR); /* consecutive CODE_CLEAR codes */
682
1.31k
    if (code == CODE_EOI)
683
13
        goto after_loop;
684
1.29k
    if (code > CODE_EOI)
685
19
    {
686
19
        goto error_code;
687
19
    }
688
1.27k
    *op++ = (uint8_t)code;
689
1.27k
    occ--;
690
1.27k
    oldcodep = dec_codetab + code;
691
1.27k
    if (occ == 0)
692
25
        goto after_loop;
693
1.25k
    goto begin;
694
1.27k
}
695
1.27k
}
696
697
1.25k
too_short_buffer:
698
301
{
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
301
    sp->dec_codep = codep;
706
301
    do
707
3.79k
    {
708
3.79k
        codep = codep->next;
709
3.79k
    } while (codep->length > occ);
710
711
301
    sp->dec_restart = occ;
712
301
    uint8_t *tp = op + occ;
713
301
    do
714
4.27k
    {
715
4.27k
        *--tp = codep->value;
716
4.27k
        codep = codep->next;
717
4.27k
    } while (--occ);
718
301
}
719
720
663
after_loop:
721
663
    tif->tif_rawcc -= (tmsize_t)((uint8_t *)bp - tif->tif_rawcp);
722
663
    tif->tif_rawcp = (uint8_t *)bp;
723
663
    sp->old_tif_rawcc = tif->tif_rawcc;
724
663
    sp->dec_bitsleft = dec_bitsleft;
725
663
    sp->lzw_nbits = (unsigned short)nbits;
726
663
    sp->lzw_nextdata = nextdata;
727
663
    sp->lzw_nextbits = nextbits;
728
663
    sp->dec_nbitsmask = nbitsmask;
729
663
    sp->dec_oldcodep = oldcodep;
730
663
    sp->dec_free_entp = free_entp;
731
663
    sp->dec_maxcodep = maxcodep;
732
733
663
    if (occ > 0)
734
54
    {
735
54
        memset(op, 0, (size_t)occ);
736
54
        sp->read_error = 1;
737
54
        TIFFErrorExtR(tif, module,
738
54
                      "Not enough data at scanline %" PRIu32 " (short %" PRIu64
739
54
                      " bytes)",
740
54
                      tif->tif_dir.td_row, (uint64_t)occ);
741
54
        return (0);
742
54
    }
743
609
    return (1);
744
745
106
no_eoi:
746
106
    memset(op, 0, (size_t)occ);
747
106
    sp->read_error = 1;
748
106
    TIFFErrorExtR(tif, module,
749
106
                  "LZWDecode: Strip %" PRIu32 " not terminated with EOI code",
750
106
                  tif->tif_dir.td_curstrip);
751
106
    return 0;
752
1.20k
error_code:
753
1.20k
    memset(op, 0, (size_t)occ);
754
1.20k
    sp->read_error = 1;
755
1.20k
    TIFFErrorExtR(tif, tif->tif_name, "Using code not yet in table");
756
1.20k
    return 0;
757
663
}
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
74.3k
    {                                                                          \
767
74.3k
        if (dec_bitsleft < (uint64_t)nbits)                                    \
768
74.3k
        {                                                                      \
769
64
            TIFFWarningExtR(_tif, module,                                      \
770
64
                            "LZWDecode: Strip %" PRIu32                        \
771
64
                            " not terminated with EOI code",                   \
772
64
                            _tif->tif_dir.td_curstrip);                        \
773
64
            _code = CODE_EOI;                                                  \
774
64
        }                                                                      \
775
74.3k
        else                                                                   \
776
74.3k
        {                                                                      \
777
74.2k
            _get(_sp, _bp, _code);                                             \
778
74.2k
            dec_bitsleft -= (uint64_t)nbits;                                   \
779
74.2k
        }                                                                      \
780
74.3k
    }
781
782
/*
783
 * Decode a "hunk of data" for old images.
784
 */
785
#define GetNextCodeCompat(sp, bp, code)                                        \
786
74.2k
    {                                                                          \
787
74.2k
        nextdata |= (unsigned long)*(bp)++ << nextbits;                        \
788
74.2k
        nextbits += 8;                                                         \
789
74.2k
        if (nextbits < nbits)                                                  \
790
74.2k
        {                                                                      \
791
21.5k
            nextdata |= (unsigned long)*(bp)++ << nextbits;                    \
792
21.5k
            nextbits += 8;                                                     \
793
21.5k
        }                                                                      \
794
74.2k
        code = (hcode_t)(nextdata & (unsigned long)nbitsmask);                 \
795
74.2k
        nextdata >>= nbits;                                                    \
796
74.2k
        nextbits -= nbits;                                                     \
797
74.2k
    }
798
799
static int LZWDecodeCompat(TIFF *tif, uint8_t *op0, tmsize_t occ0, uint16_t s)
800
606
{
801
606
    static const char module[] = "LZWDecodeCompat";
802
606
    LZWCodecState *sp = LZWDecoderState(tif);
803
606
    uint8_t *op = (uint8_t *)op0;
804
606
    tmsize_t occ = occ0;
805
606
    uint8_t *tp;
806
606
    uint8_t *bp;
807
606
    int code, nbits;
808
606
    int len;
809
606
    long nextbits, nbitsmask;
810
606
    WordType nextdata;
811
606
    code_t *codep, *free_entp, *maxcodep, *oldcodep;
812
813
606
    (void)s;
814
606
    assert(sp != NULL);
815
816
    /*
817
     * Restart interrupted output operation.
818
     */
819
606
    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
606
    bp = (uint8_t *)tif->tif_rawcp;
861
862
606
    sp->dec_bitsleft +=
863
606
        (((uint64_t)tif->tif_rawcc - (uint64_t)sp->old_tif_rawcc) << 3);
864
606
    uint64_t dec_bitsleft = sp->dec_bitsleft;
865
866
606
    nbits = sp->lzw_nbits;
867
606
    nextdata = sp->lzw_nextdata;
868
606
    nextbits = sp->lzw_nextbits;
869
606
    nbitsmask = sp->dec_nbitsmask;
870
606
    oldcodep = sp->dec_oldcodep;
871
606
    free_entp = sp->dec_free_entp;
872
606
    maxcodep = sp->dec_maxcodep;
873
874
72.8k
    while (occ > 0)
875
72.6k
    {
876
72.6k
        NextCode(tif, sp, bp, code, GetNextCodeCompat, dec_bitsleft);
877
72.6k
        if (code == CODE_EOI)
878
73
            break;
879
72.5k
        if (code == CODE_CLEAR)
880
1.25k
        {
881
1.25k
            do
882
1.68k
            {
883
1.68k
                free_entp = sp->dec_codetab + CODE_FIRST;
884
1.68k
                _TIFFmemset(free_entp, 0,
885
1.68k
                            (CSIZE - CODE_FIRST) * sizeof(code_t));
886
1.68k
                nbits = BITS_MIN;
887
1.68k
                nbitsmask = MAXCODE(BITS_MIN);
888
1.68k
                maxcodep = sp->dec_codetab + nbitsmask;
889
1.68k
                NextCode(tif, sp, bp, code, GetNextCodeCompat, dec_bitsleft);
890
1.68k
            } while (code == CODE_CLEAR); /* consecutive CODE_CLEAR codes */
891
1.25k
            if (code == CODE_EOI)
892
20
                break;
893
1.23k
            if (code > CODE_CLEAR)
894
29
            {
895
29
                TIFFErrorExtR(
896
29
                    tif, tif->tif_name,
897
29
                    "LZWDecode: Corrupted LZW table at scanline %" PRIu32,
898
29
                    tif->tif_dir.td_row);
899
29
                return (0);
900
29
            }
901
1.20k
            *op++ = (uint8_t)code;
902
1.20k
            occ--;
903
1.20k
            oldcodep = sp->dec_codetab + code;
904
1.20k
            continue;
905
1.23k
        }
906
71.3k
        codep = sp->dec_codetab + code;
907
908
        /*
909
         * Add the new entry to the code table.
910
         */
911
71.3k
        if (free_entp < &sp->dec_codetab[0] ||
912
71.2k
            free_entp >= &sp->dec_codetab[CSIZE])
913
24
        {
914
24
            TIFFErrorExtR(tif, module,
915
24
                          "Corrupted LZW table at scanline %" PRIu32,
916
24
                          tif->tif_dir.td_row);
917
24
            return (0);
918
24
        }
919
920
71.2k
        free_entp->next = oldcodep;
921
71.2k
        if (free_entp->next < &sp->dec_codetab[0] ||
922
71.2k
            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.2k
        free_entp->firstchar = free_entp->next->firstchar;
930
71.2k
        free_entp->length = (uint16_t)(free_entp->next->length + 1);
931
71.2k
        free_entp->value =
932
71.2k
            (codep < free_entp) ? codep->firstchar : free_entp->firstchar;
933
71.2k
        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.2k
        oldcodep = codep;
941
71.2k
        if (code >= 256)
942
3.15k
        {
943
            /*
944
             * Code maps to a string, copy string
945
             * value to output (written in reverse).
946
             */
947
3.15k
            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.04k
            if (codep->length > occ)
957
103
            {
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
103
                sp->dec_codep = codep;
965
103
                do
966
116
                {
967
116
                    codep = codep->next;
968
116
                } while (codep->length > occ);
969
103
                sp->dec_restart = occ;
970
103
                tp = op + occ;
971
103
                do
972
119
                {
973
119
                    *--tp = codep->value;
974
119
                    codep = codep->next;
975
119
                } while (--occ);
976
103
                break;
977
103
            }
978
2.94k
            len = codep->length;
979
2.94k
            tp = op + len;
980
2.94k
            do
981
6.32k
            {
982
6.32k
                *--tp = codep->value;
983
6.32k
                codep = codep->next;
984
6.32k
            } while (codep && tp > op);
985
2.94k
            assert(occ >= len);
986
2.94k
            op += len;
987
2.94k
            occ -= len;
988
2.94k
        }
989
68.1k
        else
990
68.1k
        {
991
68.1k
            *op++ = (uint8_t)code;
992
68.1k
            occ--;
993
68.1k
        }
994
71.2k
    }
995
996
440
    tif->tif_rawcc -= (tmsize_t)((uint8_t *)bp - tif->tif_rawcp);
997
440
    tif->tif_rawcp = (uint8_t *)bp;
998
999
440
    sp->old_tif_rawcc = tif->tif_rawcc;
1000
440
    sp->dec_bitsleft = dec_bitsleft;
1001
1002
440
    sp->lzw_nbits = (unsigned short)nbits;
1003
440
    sp->lzw_nextdata = nextdata;
1004
440
    sp->lzw_nextbits = nextbits;
1005
440
    sp->dec_nbitsmask = nbitsmask;
1006
440
    sp->dec_oldcodep = oldcodep;
1007
440
    sp->dec_free_entp = free_entp;
1008
440
    sp->dec_maxcodep = maxcodep;
1009
1010
440
    if (occ > 0)
1011
93
    {
1012
93
        TIFFErrorExtR(tif, module,
1013
93
                      "Not enough data at scanline %" PRIu32 " (short %" PRIu64
1014
93
                      " bytes)",
1015
93
                      tif->tif_dir.td_row, (uint64_t)occ);
1016
93
        return (0);
1017
93
    }
1018
347
    return (1);
1019
440
}
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
4.16k
{
1386
4.16k
    (void)TIFFPredictorCleanup(tif);
1387
1388
4.16k
    assert(tif->tif_data != NULL);
1389
1390
4.16k
    if (LZWDecoderState(tif)->dec_codetab)
1391
1.87k
        _TIFFfreeExt(tif, LZWDecoderState(tif)->dec_codetab);
1392
1393
4.16k
    if (LZWEncoderState(tif)->enc_hashtab)
1394
0
        _TIFFfreeExt(tif, LZWEncoderState(tif)->enc_hashtab);
1395
1396
4.16k
    _TIFFfreeExt(tif, tif->tif_data);
1397
4.16k
    tif->tif_data = NULL;
1398
1399
4.16k
    _TIFFSetDefaultCompressionState(tif);
1400
4.16k
}
1401
1402
static uint64_t LZWGetMaxCompressionRatio(TIFF *tif)
1403
4
{
1404
4
    (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
4
    return 1400;
1415
4
}
1416
1417
int TIFFInitLZW(TIFF *tif, int scheme)
1418
4.16k
{
1419
4.16k
    static const char module[] = "TIFFInitLZW";
1420
4.16k
    (void)scheme;
1421
4.16k
    assert(scheme == COMPRESSION_LZW);
1422
    /*
1423
     * Allocate state block so tag methods have storage to record values.
1424
     */
1425
4.16k
    tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(LZWCodecState));
1426
4.16k
    if (tif->tif_data == NULL)
1427
0
        goto bad;
1428
4.16k
    LZWDecoderState(tif)->dec_codetab = NULL;
1429
4.16k
    LZWDecoderState(tif)->dec_decode = NULL;
1430
4.16k
    LZWEncoderState(tif)->enc_hashtab = NULL;
1431
4.16k
    LZWState(tif)->rw_mode = tif->tif_mode;
1432
1433
    /*
1434
     * Install codec methods.
1435
     */
1436
4.16k
    tif->tif_fixuptags = LZWFixupTags;
1437
4.16k
    tif->tif_setupdecode = LZWSetupDecode;
1438
4.16k
    tif->tif_predecode = LZWPreDecode;
1439
4.16k
    tif->tif_decoderow = LZWDecode;
1440
4.16k
    tif->tif_decodestrip = LZWDecode;
1441
4.16k
    tif->tif_decodetile = LZWDecode;
1442
4.16k
#ifndef LZW_READ_ONLY
1443
4.16k
    tif->tif_setupencode = LZWSetupEncode;
1444
4.16k
    tif->tif_preencode = LZWPreEncode;
1445
4.16k
    tif->tif_postencode = LZWPostEncode;
1446
4.16k
    tif->tif_encoderow = LZWEncode;
1447
4.16k
    tif->tif_encodestrip = LZWEncode;
1448
4.16k
    tif->tif_encodetile = LZWEncode;
1449
4.16k
#endif
1450
4.16k
    tif->tif_getmaxcompressionratio = LZWGetMaxCompressionRatio;
1451
4.16k
    tif->tif_cleanup = LZWCleanup;
1452
    /*
1453
     * Setup predictor setup.
1454
     */
1455
4.16k
    (void)TIFFPredictorInit(tif);
1456
4.16k
    return (1);
1457
0
bad:
1458
0
    TIFFErrorExtR(tif, module, "No space for LZW state block");
1459
0
    return (0);
1460
4.16k
}
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 */