Coverage Report

Created: 2025-06-10 07:27

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