Coverage Report

Created: 2025-06-13 06:18

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