Coverage Report

Created: 2022-10-31 07:00

/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
 *
5
 * Permission to use, copy, modify, distribute, and sell this software and 
6
 * its documentation for any purpose is hereby granted without fee, provided
7
 * that (i) the above copyright notices and this permission notice appear in
8
 * all copies of the software and related documentation, and (ii) the names of
9
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
10
 * publicity relating to the software without the specific, prior written
11
 * permission of Sam Leffler and Silicon Graphics.
12
 * 
13
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
14
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
15
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
16
 * 
17
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
21
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
22
 * OF THIS SOFTWARE.
23
 */
24
25
#include "tiffiop.h"
26
#ifdef LZW_SUPPORT
27
/*
28
 * TIFF Library.  
29
 * Rev 5.0 Lempel-Ziv & Welch Compression Support
30
 *
31
 * This code is derived from the compress program whose code is
32
 * derived from software contributed to Berkeley by James A. Woods,
33
 * derived from original work by Spencer Thomas and Joseph Orost.
34
 *
35
 * The original Berkeley copyright notice appears below in its entirety.
36
 */
37
#include "tif_predict.h"
38
39
#include <stdio.h>
40
41
/*
42
 * NB: The 5.0 spec describes a different algorithm than Aldus
43
 *     implements.  Specifically, Aldus does code length transitions
44
 *     one code earlier than should be done (for real LZW).
45
 *     Earlier versions of this library implemented the correct
46
 *     LZW algorithm, but emitted codes in a bit order opposite
47
 *     to the TIFF spec.  Thus, to maintain compatibility w/ Aldus
48
 *     we interpret MSB-LSB ordered codes to be images written w/
49
 *     old versions of this library, but otherwise adhere to the
50
 *     Aldus "off by one" algorithm.
51
 *
52
 * Future revisions to the TIFF spec are expected to "clarify this issue".
53
 */
54
#define LZW_COMPAT              /* include backwards compatibility code */
55
/*
56
 * Each strip of data is supposed to be terminated by a CODE_EOI.
57
 * If the following #define is included, the decoder will also
58
 * check for end-of-strip w/o seeing this code.  This makes the
59
 * library more robust, but also slower.
60
 */
61
#define LZW_CHECKEOS            /* include checks for strips w/o EOI code */
62
63
129M
#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
34.7k
#define BITS_MIN        9               /* start with 9 bits */
69
3.88G
#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
34.7k
#define CODE_FIRST      258             /* first free code entry */
74
129M
#define CODE_MAX        MAXCODE(BITS_MAX)
75
213M
#define HSIZE           9001L           /* 91% occupancy */
76
3.88G
#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
  TIFFPredictorState predict;     /* predictor super class */
91
92
  unsigned short  nbits;          /* # of bits/code */
93
  unsigned short  maxcode;        /* maximum code for lzw_nbits */
94
  unsigned short  free_ent;       /* next free entry in hash table */
95
  unsigned long   nextdata;       /* next bits of i/o */
96
  long            nextbits;       /* # of valid bits in lzw_nextdata */
97
98
  int             rw_mode;        /* preserve rw_mode from init */
99
} LZWBaseState;
100
101
1.75M
#define lzw_nbits       base.nbits
102
1.75M
#define lzw_maxcode     base.maxcode
103
1.75M
#define lzw_free_ent    base.free_ent
104
1.75M
#define lzw_nextdata    base.nextdata
105
1.75M
#define lzw_nextbits    base.nextbits
106
107
/*
108
 * Encoding-specific state.
109
 */
110
typedef uint16_t hcode_t;     /* codes fit in 16 bits */
111
typedef struct {
112
  long  hash;
113
  hcode_t code;
114
} hash_t;
115
116
/*
117
 * Decoding-specific state.
118
 */
119
typedef struct code_ent {
120
  struct code_ent *next;
121
  unsigned short  length;   /* string len, including this token */
122
  unsigned char value;    /* data value */
123
  unsigned char firstchar;  /* first token of string */
124
} code_t;
125
126
typedef int (*decodeFunc)(TIFF*, uint8_t*, tmsize_t, uint16_t);
127
128
typedef struct {
129
  LZWBaseState base;
130
131
  /* Decoding specific data */
132
  long    dec_nbitsmask;    /* lzw_nbits 1 bits, right adjusted */
133
  long    dec_restart;    /* restart count */
134
#ifdef LZW_CHECKEOS
135
  uint64_t  dec_bitsleft;   /* available bits in raw data */
136
  tmsize_t old_tif_rawcc;         /* value of tif_rawcc at the end of the previous TIFLZWDecode() call */
137
#endif
138
  decodeFunc dec_decode;    /* regular or backwards compatible */
139
  code_t* dec_codep;    /* current recognized code */
140
  code_t* dec_oldcodep;   /* previously recognized code */
141
  code_t* dec_free_entp;    /* next free entry */
142
  code_t* dec_maxcodep;   /* max available entry */
143
  code_t* dec_codetab;    /* kept separate for small machines */
144
145
  /* Encoding specific data */
146
  int     enc_oldcode;    /* last code encountered */
147
  long    enc_checkpoint;   /* point at which to clear table */
148
108k
#define CHECK_GAP 10000    /* enc_ratio check interval */
149
  long    enc_ratio;    /* current compression ratio */
150
  long    enc_incount;    /* (input) data bytes encoded */
151
  long    enc_outcount;   /* encoded (output) bytes */
152
  uint8_t*  enc_rawlimit;   /* bound on tif_rawdata buffer */
153
  hash_t* enc_hashtab;    /* kept separate for small machines */
154
} LZWCodecState;
155
156
889k
#define LZWState(tif)   ((LZWBaseState*) (tif)->tif_data)
157
3.83k
#define DecoderState(tif) ((LZWCodecState*) LZWState(tif))
158
883k
#define EncoderState(tif) ((LZWCodecState*) LZWState(tif))
159
160
static int LZWDecode(TIFF* tif, uint8_t* op0, tmsize_t occ0, uint16_t s);
161
#ifdef LZW_COMPAT
162
static int LZWDecodeCompat(TIFF* tif, uint8_t* op0, tmsize_t occ0, uint16_t s);
163
#endif
164
static void cl_hash(LZWCodecState*);
165
166
/*
167
 * LZW Decoder.
168
 */
169
170
#ifdef LZW_CHECKEOS
171
/*
172
 * This check shouldn't be necessary because each
173
 * strip is suppose to be terminated with CODE_EOI.
174
 */
175
0
#define NextCode(_tif, _sp, _bp, _code, _get) {       \
176
0
  if ((_sp)->dec_bitsleft < (uint64_t)nbits) {     \
177
0
    TIFFWarningExt(_tif->tif_clientdata, module,    \
178
0
        "LZWDecode: Strip %"PRIu32" not terminated with EOI code", \
179
0
        _tif->tif_curstrip);        \
180
0
    _code = CODE_EOI;         \
181
0
  } else {             \
182
0
    _get(_sp,_bp,_code);          \
183
0
    (_sp)->dec_bitsleft -= nbits;       \
184
0
  }               \
185
0
}
186
#else
187
#define NextCode(tif, sp, bp, code, get) get(sp, bp, code)
188
#endif
189
190
static int
191
LZWFixupTags(TIFF* tif)
192
0
{
193
0
  (void) tif;
194
0
  return (1);
195
0
}
196
197
static int
198
LZWSetupDecode(TIFF* tif)
199
0
{
200
0
  static const char module[] = "LZWSetupDecode";
201
0
  LZWCodecState* sp = DecoderState(tif);
202
0
  int code;
203
204
0
  if( sp == NULL )
205
0
  {
206
    /*
207
     * Allocate state block so tag methods have storage to record
208
     * values.
209
    */
210
0
    tif->tif_data = (uint8_t*) _TIFFmalloc(sizeof(LZWCodecState));
211
0
    if (tif->tif_data == NULL)
212
0
    {
213
0
      TIFFErrorExt(tif->tif_clientdata, module, "No space for LZW state block");
214
0
      return (0);
215
0
    }
216
217
0
    sp = DecoderState(tif);
218
0
    sp->dec_codetab = NULL;
219
0
    sp->dec_decode = NULL;
220
221
    /*
222
     * Setup predictor setup.
223
     */
224
0
    (void) TIFFPredictorInit(tif);
225
0
  }
226
227
0
  if (sp->dec_codetab == NULL) {
228
0
    sp->dec_codetab = (code_t*)_TIFFmalloc(CSIZE*sizeof (code_t));
229
0
    if (sp->dec_codetab == NULL) {
230
0
      TIFFErrorExt(tif->tif_clientdata, module,
231
0
             "No space for LZW code table");
232
0
      return (0);
233
0
    }
234
    /*
235
     * Pre-load the table.
236
     */
237
0
    code = 255;
238
0
    do {
239
0
      sp->dec_codetab[code].value = (unsigned char)code;
240
0
      sp->dec_codetab[code].firstchar = (unsigned char)code;
241
0
      sp->dec_codetab[code].length = 1;
242
0
      sp->dec_codetab[code].next = NULL;
243
0
    } while (code--);
244
    /*
245
     * Zero-out the unused entries
246
                 */
247
                /* Silence false positive */
248
                /* coverity[overrun-buffer-arg] */
249
0
                 _TIFFmemset(&sp->dec_codetab[CODE_CLEAR], 0,
250
0
           (CODE_FIRST - CODE_CLEAR) * sizeof (code_t));
251
0
  }
252
0
  return (1);
253
0
}
254
255
/*
256
 * Setup state for decoding a strip.
257
 */
258
static int
259
LZWPreDecode(TIFF* tif, uint16_t s)
260
0
{
261
0
  static const char module[] = "LZWPreDecode";
262
0
  LZWCodecState *sp = DecoderState(tif);
263
264
0
  (void) s;
265
0
  assert(sp != NULL);
266
0
  if( sp->dec_codetab == NULL )
267
0
        {
268
0
            tif->tif_setupdecode( tif );
269
0
      if( sp->dec_codetab == NULL )
270
0
    return (0);
271
0
        }
272
273
  /*
274
   * Check for old bit-reversed codes.
275
   */
276
0
  if (tif->tif_rawcc >= 2 &&
277
0
      tif->tif_rawdata[0] == 0 && (tif->tif_rawdata[1] & 0x1)) {
278
0
#ifdef LZW_COMPAT
279
0
    if (!sp->dec_decode) {
280
0
      TIFFWarningExt(tif->tif_clientdata, module,
281
0
          "Old-style LZW codes, convert file");
282
      /*
283
       * Override default decoding methods with
284
       * ones that deal with the old coding.
285
       * Otherwise the predictor versions set
286
       * above will call the compatibility routines
287
       * through the dec_decode method.
288
       */
289
0
      tif->tif_decoderow = LZWDecodeCompat;
290
0
      tif->tif_decodestrip = LZWDecodeCompat;
291
0
      tif->tif_decodetile = LZWDecodeCompat;
292
      /*
293
       * If doing horizontal differencing, must
294
       * re-setup the predictor logic since we
295
       * switched the basic decoder methods...
296
       */
297
0
      (*tif->tif_setupdecode)(tif);
298
0
      sp->dec_decode = LZWDecodeCompat;
299
0
    }
300
0
    sp->lzw_maxcode = MAXCODE(BITS_MIN);
301
#else /* !LZW_COMPAT */
302
    if (!sp->dec_decode) {
303
      TIFFErrorExt(tif->tif_clientdata, module,
304
          "Old-style LZW codes not supported");
305
      sp->dec_decode = LZWDecode;
306
    }
307
    return (0);
308
#endif/* !LZW_COMPAT */
309
0
  } else {
310
0
    sp->lzw_maxcode = MAXCODE(BITS_MIN)-1;
311
0
    sp->dec_decode = LZWDecode;
312
0
  }
313
0
  sp->lzw_nbits = BITS_MIN;
314
0
  sp->lzw_nextbits = 0;
315
0
  sp->lzw_nextdata = 0;
316
317
0
  sp->dec_restart = 0;
318
0
  sp->dec_nbitsmask = MAXCODE(BITS_MIN);
319
0
#ifdef LZW_CHECKEOS
320
0
  sp->dec_bitsleft = 0;
321
0
        sp->old_tif_rawcc = 0;
322
0
#endif
323
0
  sp->dec_free_entp = sp->dec_codetab + CODE_FIRST;
324
  /*
325
   * Zero entries that are not yet filled in.  We do
326
   * this to guard against bogus input data that causes
327
   * us to index into undefined entries.  If you can
328
   * come up with a way to safely bounds-check input codes
329
   * while decoding then you can remove this operation.
330
   */
331
0
  _TIFFmemset(sp->dec_free_entp, 0, (CSIZE-CODE_FIRST)*sizeof (code_t));
332
0
  sp->dec_oldcodep = &sp->dec_codetab[-1];
333
0
  sp->dec_maxcodep = &sp->dec_codetab[sp->dec_nbitsmask-1];
334
0
  return (1);
335
0
}
336
337
/*
338
 * Decode a "hunk of data".
339
 */
340
0
#define GetNextCode(sp, bp, code) {       \
341
0
  nextdata = (nextdata<<8) | *(bp)++;     \
342
0
  nextbits += 8;            \
343
0
  if (nextbits < nbits) {         \
344
0
    nextdata = (nextdata<<8) | *(bp)++;   \
345
0
    nextbits += 8;          \
346
0
  }              \
347
0
  code = (hcode_t)((nextdata >> (nextbits-nbits)) & nbitsmask); \
348
0
  nextbits -= nbits;          \
349
0
}
350
351
static void
352
codeLoop(TIFF* tif, const char* module)
353
0
{
354
0
  TIFFErrorExt(tif->tif_clientdata, module,
355
0
      "Bogus encoding, loop in the code table; scanline %"PRIu32,
356
0
      tif->tif_row);
357
0
}
358
359
static int
360
LZWDecode(TIFF* tif, uint8_t* op0, tmsize_t occ0, uint16_t s)
361
0
{
362
0
  static const char module[] = "LZWDecode";
363
0
  LZWCodecState *sp = DecoderState(tif);
364
0
  char *op = (char*) op0;
365
0
  long occ = (long) occ0;
366
0
  char *tp;
367
0
  unsigned char *bp;
368
0
  hcode_t code;
369
0
  int len;
370
0
  long nbits, nextbits, nbitsmask;
371
0
        unsigned long nextdata;
372
0
  code_t *codep, *free_entp, *maxcodep, *oldcodep;
373
374
0
  (void) s;
375
0
  assert(sp != NULL);
376
0
        assert(sp->dec_codetab != NULL);
377
378
  /*
379
    Fail if value does not fit in long.
380
  */
381
0
  if ((tmsize_t) occ != occ0)
382
0
          return (0);
383
  /*
384
   * Restart interrupted output operation.
385
   */
386
0
  if (sp->dec_restart) {
387
0
    long residue;
388
389
0
    codep = sp->dec_codep;
390
0
    residue = codep->length - sp->dec_restart;
391
0
    if (residue > occ) {
392
      /*
393
       * Residue from previous decode is sufficient
394
       * to satisfy decode request.  Skip to the
395
       * start of the decoded string, place decoded
396
       * values in the output buffer, and return.
397
       */
398
0
      sp->dec_restart += occ;
399
0
      do {
400
0
        codep = codep->next;
401
0
      } while (--residue > occ && codep);
402
0
      if (codep) {
403
0
        tp = op + occ;
404
0
        do {
405
0
          *--tp = codep->value;
406
0
          codep = codep->next;
407
0
        } while (--occ && codep);
408
0
      }
409
0
      return (1);
410
0
    }
411
    /*
412
     * Residue satisfies only part of the decode request.
413
     */
414
0
    op += residue;
415
0
    occ -= residue;
416
0
    tp = op;
417
0
    do {
418
0
      int t;
419
0
      --tp;
420
0
      t = codep->value;
421
0
      codep = codep->next;
422
0
      *tp = (char)t;
423
0
    } while (--residue && codep);
424
0
    sp->dec_restart = 0;
425
0
  }
426
427
0
  bp = (unsigned char *)tif->tif_rawcp;
428
0
#ifdef LZW_CHECKEOS
429
0
  sp->dec_bitsleft += (((uint64_t)tif->tif_rawcc - sp->old_tif_rawcc) << 3);
430
0
#endif
431
0
  nbits = sp->lzw_nbits;
432
0
  nextdata = sp->lzw_nextdata;
433
0
  nextbits = sp->lzw_nextbits;
434
0
  nbitsmask = sp->dec_nbitsmask;
435
0
  oldcodep = sp->dec_oldcodep;
436
0
  free_entp = sp->dec_free_entp;
437
0
  maxcodep = sp->dec_maxcodep;
438
439
0
  while (occ > 0) {
440
0
    NextCode(tif, sp, bp, code, GetNextCode);
441
0
    if (code == CODE_EOI)
442
0
      break;
443
0
    if (code == CODE_CLEAR) {
444
0
      do {
445
0
        free_entp = sp->dec_codetab + CODE_FIRST;
446
0
        _TIFFmemset(free_entp, 0,
447
0
              (CSIZE - CODE_FIRST) * sizeof (code_t));
448
0
        nbits = BITS_MIN;
449
0
        nbitsmask = MAXCODE(BITS_MIN);
450
0
        maxcodep = sp->dec_codetab + nbitsmask-1;
451
0
        NextCode(tif, sp, bp, code, GetNextCode);
452
0
      } while (code == CODE_CLEAR);  /* consecutive CODE_CLEAR codes */
453
0
      if (code == CODE_EOI)
454
0
        break;
455
0
      if (code > CODE_CLEAR) {
456
0
        TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
457
0
        "LZWDecode: Corrupted LZW table at scanline %"PRIu32,
458
0
               tif->tif_row);
459
0
        return (0);
460
0
      }
461
0
      *op++ = (char)code;
462
0
      occ--;
463
0
      oldcodep = sp->dec_codetab + code;
464
0
      continue;
465
0
    }
466
0
    codep = sp->dec_codetab + code;
467
468
    /*
469
     * Add the new entry to the code table.
470
     */
471
0
    if (free_entp < &sp->dec_codetab[0] ||
472
0
        free_entp >= &sp->dec_codetab[CSIZE]) {
473
0
      TIFFErrorExt(tif->tif_clientdata, module,
474
0
          "Corrupted LZW table at scanline %"PRIu32,
475
0
          tif->tif_row);
476
0
      return (0);
477
0
    }
478
479
0
    free_entp->next = oldcodep;
480
0
    if (free_entp->next < &sp->dec_codetab[0] ||
481
0
        free_entp->next >= &sp->dec_codetab[CSIZE]) {
482
0
      TIFFErrorExt(tif->tif_clientdata, module,
483
0
          "Corrupted LZW table at scanline %"PRIu32,
484
0
          tif->tif_row);
485
0
      return (0);
486
0
    }
487
0
    free_entp->firstchar = free_entp->next->firstchar;
488
0
    free_entp->length = free_entp->next->length+1;
489
0
    free_entp->value = (codep < free_entp) ?
490
0
        codep->firstchar : free_entp->firstchar;
491
0
    if (++free_entp > maxcodep) {
492
0
      if (++nbits > BITS_MAX)   /* should not happen */
493
0
        nbits = BITS_MAX;
494
0
      nbitsmask = MAXCODE(nbits);
495
0
      maxcodep = sp->dec_codetab + nbitsmask-1;
496
0
    }
497
0
    oldcodep = codep;
498
0
    if (code >= 256) {
499
      /*
500
       * Code maps to a string, copy string
501
       * value to output (written in reverse).
502
       */
503
0
      if(codep->length == 0) {
504
0
        TIFFErrorExt(tif->tif_clientdata, module,
505
0
            "Wrong length of decoded string: "
506
0
            "data probably corrupted at scanline %"PRIu32,
507
0
            tif->tif_row);
508
0
        return (0);
509
0
      }
510
0
      if (codep->length > occ) {
511
        /*
512
         * String is too long for decode buffer,
513
         * locate portion that will fit, copy to
514
         * the decode buffer, and setup restart
515
         * logic for the next decoding call.
516
         */
517
0
        sp->dec_codep = codep;
518
0
        do {
519
0
          codep = codep->next;
520
0
        } while (codep && codep->length > occ);
521
0
        if (codep) {
522
0
          sp->dec_restart = (long)occ;
523
0
          tp = op + occ;
524
0
          do  {
525
0
            *--tp = codep->value;
526
0
            codep = codep->next;
527
0
          }  while (--occ && codep);
528
0
          if (codep)
529
0
            codeLoop(tif, module);
530
0
        }
531
0
        break;
532
0
      }
533
0
      len = codep->length;
534
0
      tp = op + len;
535
0
      do {
536
0
        int t;
537
0
        --tp;
538
0
        t = codep->value;
539
0
        codep = codep->next;
540
0
        *tp = (char)t;
541
0
      } while (codep && tp > op);
542
0
      if (codep) {
543
0
          codeLoop(tif, module);
544
0
          break;
545
0
      }
546
0
      assert(occ >= len);
547
0
      op += len;
548
0
      occ -= len;
549
0
    } else {
550
0
      *op++ = (char)code;
551
0
      occ--;
552
0
    }
553
0
  }
554
555
0
  tif->tif_rawcc -= (tmsize_t)((uint8_t*) bp - tif->tif_rawcp );
556
0
  tif->tif_rawcp = (uint8_t*) bp;
557
0
#ifdef LZW_CHECKEOS
558
0
  sp->old_tif_rawcc = tif->tif_rawcc;
559
0
#endif
560
0
  sp->lzw_nbits = (unsigned short) nbits;
561
0
  sp->lzw_nextdata = nextdata;
562
0
  sp->lzw_nextbits = nextbits;
563
0
  sp->dec_nbitsmask = nbitsmask;
564
0
  sp->dec_oldcodep = oldcodep;
565
0
  sp->dec_free_entp = free_entp;
566
0
  sp->dec_maxcodep = maxcodep;
567
568
0
  if (occ > 0) {
569
0
    TIFFErrorExt(tif->tif_clientdata, module,
570
0
      "Not enough data at scanline %"PRIu32" (short %ld bytes)",
571
0
           tif->tif_row, occ);
572
0
    return (0);
573
0
  }
574
0
  return (1);
575
0
}
576
577
#ifdef LZW_COMPAT
578
/*
579
 * Decode a "hunk of data" for old images.
580
 */
581
0
#define GetNextCodeCompat(sp, bp, code) {     \
582
0
  nextdata |= (unsigned long) *(bp)++ << nextbits;  \
583
0
  nextbits += 8;            \
584
0
  if (nextbits < nbits) {         \
585
0
    nextdata |= (unsigned long) *(bp)++ << nextbits;\
586
0
    nextbits += 8;          \
587
0
  }              \
588
0
  code = (hcode_t)(nextdata & nbitsmask);     \
589
0
  nextdata >>= nbits;         \
590
0
  nextbits -= nbits;          \
591
0
}
592
593
static int
594
LZWDecodeCompat(TIFF* tif, uint8_t* op0, tmsize_t occ0, uint16_t s)
595
0
{
596
0
  static const char module[] = "LZWDecodeCompat";
597
0
  LZWCodecState *sp = DecoderState(tif);
598
0
  char *op = (char*) op0;
599
0
  long occ = (long) occ0;
600
0
  char *tp;
601
0
  unsigned char *bp;
602
0
  int code, nbits;
603
0
  int len;
604
0
  long nextbits, nextdata, nbitsmask;
605
0
  code_t *codep, *free_entp, *maxcodep, *oldcodep;
606
607
0
  (void) s;
608
0
  assert(sp != NULL);
609
610
  /*
611
    Fail if value does not fit in long.
612
  */
613
0
  if ((tmsize_t) occ != occ0)
614
0
          return (0);
615
616
  /*
617
   * Restart interrupted output operation.
618
   */
619
0
  if (sp->dec_restart) {
620
0
    long residue;
621
622
0
    codep = sp->dec_codep;
623
0
    residue = codep->length - sp->dec_restart;
624
0
    if (residue > occ) {
625
      /*
626
       * Residue from previous decode is sufficient
627
       * to satisfy decode request.  Skip to the
628
       * start of the decoded string, place decoded
629
       * values in the output buffer, and return.
630
       */
631
0
      sp->dec_restart += occ;
632
0
      do {
633
0
        codep = codep->next;
634
0
      } while (--residue > occ);
635
0
      tp = op + occ;
636
0
      do {
637
0
        *--tp = codep->value;
638
0
        codep = codep->next;
639
0
      } while (--occ);
640
0
      return (1);
641
0
    }
642
    /*
643
     * Residue satisfies only part of the decode request.
644
     */
645
0
    op += residue;
646
0
    occ -= residue;
647
0
    tp = op;
648
0
    do {
649
0
      *--tp = codep->value;
650
0
      codep = codep->next;
651
0
    } while (--residue);
652
0
    sp->dec_restart = 0;
653
0
  }
654
655
0
  bp = (unsigned char *)tif->tif_rawcp;
656
0
#ifdef LZW_CHECKEOS
657
0
  sp->dec_bitsleft += (((uint64_t)tif->tif_rawcc - sp->old_tif_rawcc) << 3);
658
0
#endif
659
0
  nbits = sp->lzw_nbits;
660
0
  nextdata = sp->lzw_nextdata;
661
0
  nextbits = sp->lzw_nextbits;
662
0
  nbitsmask = sp->dec_nbitsmask;
663
0
  oldcodep = sp->dec_oldcodep;
664
0
  free_entp = sp->dec_free_entp;
665
0
  maxcodep = sp->dec_maxcodep;
666
667
0
  while (occ > 0) {
668
0
    NextCode(tif, sp, bp, code, GetNextCodeCompat);
669
0
    if (code == CODE_EOI)
670
0
      break;
671
0
    if (code == CODE_CLEAR) {
672
0
      do {
673
0
        free_entp = sp->dec_codetab + CODE_FIRST;
674
0
        _TIFFmemset(free_entp, 0,
675
0
              (CSIZE - CODE_FIRST) * sizeof (code_t));
676
0
        nbits = BITS_MIN;
677
0
        nbitsmask = MAXCODE(BITS_MIN);
678
0
        maxcodep = sp->dec_codetab + nbitsmask;
679
0
        NextCode(tif, sp, bp, code, GetNextCodeCompat);
680
0
      } while (code == CODE_CLEAR);  /* consecutive CODE_CLEAR codes */
681
0
      if (code == CODE_EOI)
682
0
        break;
683
0
      if (code > CODE_CLEAR) {
684
0
        TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
685
0
        "LZWDecode: Corrupted LZW table at scanline %"PRIu32,
686
0
               tif->tif_row);
687
0
        return (0);
688
0
      }
689
0
      *op++ = (char)code;
690
0
      occ--;
691
0
      oldcodep = sp->dec_codetab + code;
692
0
      continue;
693
0
    }
694
0
    codep = sp->dec_codetab + code;
695
696
    /*
697
     * Add the new entry to the code table.
698
     */
699
0
    if (free_entp < &sp->dec_codetab[0] ||
700
0
        free_entp >= &sp->dec_codetab[CSIZE]) {
701
0
      TIFFErrorExt(tif->tif_clientdata, module,
702
0
          "Corrupted LZW table at scanline %"PRIu32, tif->tif_row);
703
0
      return (0);
704
0
    }
705
706
0
    free_entp->next = oldcodep;
707
0
    if (free_entp->next < &sp->dec_codetab[0] ||
708
0
        free_entp->next >= &sp->dec_codetab[CSIZE]) {
709
0
      TIFFErrorExt(tif->tif_clientdata, module,
710
0
          "Corrupted LZW table at scanline %"PRIu32, tif->tif_row);
711
0
      return (0);
712
0
    }
713
0
    free_entp->firstchar = free_entp->next->firstchar;
714
0
    free_entp->length = free_entp->next->length+1;
715
0
    free_entp->value = (codep < free_entp) ?
716
0
        codep->firstchar : free_entp->firstchar;
717
0
    if (++free_entp > maxcodep) {
718
0
      if (++nbits > BITS_MAX)   /* should not happen */
719
0
        nbits = BITS_MAX;
720
0
      nbitsmask = MAXCODE(nbits);
721
0
      maxcodep = sp->dec_codetab + nbitsmask;
722
0
    }
723
0
    oldcodep = codep;
724
0
    if (code >= 256) {
725
      /*
726
       * Code maps to a string, copy string
727
       * value to output (written in reverse).
728
       */
729
0
      if(codep->length == 0) {
730
0
        TIFFErrorExt(tif->tif_clientdata, module,
731
0
            "Wrong length of decoded "
732
0
            "string: data probably corrupted at scanline %"PRIu32,
733
0
            tif->tif_row);
734
0
        return (0);
735
0
      }
736
0
      if (codep->length > occ) {
737
        /*
738
         * String is too long for decode buffer,
739
         * locate portion that will fit, copy to
740
         * the decode buffer, and setup restart
741
         * logic for the next decoding call.
742
         */
743
0
        sp->dec_codep = codep;
744
0
        do {
745
0
          codep = codep->next;
746
0
        } while (codep->length > occ);
747
0
        sp->dec_restart = occ;
748
0
        tp = op + occ;
749
0
        do  {
750
0
          *--tp = codep->value;
751
0
          codep = codep->next;
752
0
        }  while (--occ);
753
0
        break;
754
0
      }
755
0
      len = codep->length;
756
0
      tp = op + len;
757
0
      do {
758
0
        int t;
759
0
        --tp;
760
0
        t = codep->value;
761
0
        codep = codep->next;
762
0
        *tp = (char)t;
763
0
      } while (codep && tp > op);
764
0
      assert(occ >= len);
765
0
      op += len;
766
0
      occ -= len;
767
0
    } else {
768
0
      *op++ = (char)code;
769
0
      occ--;
770
0
    }
771
0
  }
772
773
0
  tif->tif_rawcc -= (tmsize_t)((uint8_t*) bp - tif->tif_rawcp );
774
0
  tif->tif_rawcp = (uint8_t*) bp;
775
0
#ifdef LZW_CHECKEOS
776
0
  sp->old_tif_rawcc = tif->tif_rawcc;
777
0
#endif
778
0
  sp->lzw_nbits = (unsigned short)nbits;
779
0
  sp->lzw_nextdata = nextdata;
780
0
  sp->lzw_nextbits = nextbits;
781
0
  sp->dec_nbitsmask = nbitsmask;
782
0
  sp->dec_oldcodep = oldcodep;
783
0
  sp->dec_free_entp = free_entp;
784
0
  sp->dec_maxcodep = maxcodep;
785
786
0
  if (occ > 0) {
787
0
    TIFFErrorExt(tif->tif_clientdata, module,
788
0
      "Not enough data at scanline %"PRIu32" (short %ld bytes)",
789
0
           tif->tif_row, occ);
790
0
    return (0);
791
0
  }
792
0
  return (1);
793
0
}
794
#endif /* LZW_COMPAT */
795
796
/*
797
 * LZW Encoding.
798
 */
799
800
static int
801
LZWSetupEncode(TIFF* tif)
802
1.08k
{
803
1.08k
  static const char module[] = "LZWSetupEncode";
804
1.08k
  LZWCodecState* sp = EncoderState(tif);
805
806
1.08k
  assert(sp != NULL);
807
1.08k
  sp->enc_hashtab = (hash_t*) _TIFFmalloc(HSIZE*sizeof (hash_t));
808
1.08k
  if (sp->enc_hashtab == NULL) {
809
0
    TIFFErrorExt(tif->tif_clientdata, module,
810
0
           "No space for LZW hash table");
811
0
    return (0);
812
0
  }
813
1.08k
  return (1);
814
1.08k
}
815
816
/*
817
 * Reset encoding state at the start of a strip.
818
 */
819
static int
820
LZWPreEncode(TIFF* tif, uint16_t s)
821
1.08k
{
822
1.08k
  LZWCodecState *sp = EncoderState(tif);
823
824
1.08k
  (void) s;
825
1.08k
  assert(sp != NULL);
826
827
1.08k
  if( sp->enc_hashtab == NULL )
828
0
        {
829
0
            tif->tif_setupencode( tif );
830
0
        }
831
832
1.08k
  sp->lzw_nbits = BITS_MIN;
833
1.08k
  sp->lzw_maxcode = MAXCODE(BITS_MIN);
834
1.08k
  sp->lzw_free_ent = CODE_FIRST;
835
1.08k
  sp->lzw_nextbits = 0;
836
1.08k
  sp->lzw_nextdata = 0;
837
1.08k
  sp->enc_checkpoint = CHECK_GAP;
838
1.08k
  sp->enc_ratio = 0;
839
1.08k
  sp->enc_incount = 0;
840
1.08k
  sp->enc_outcount = 0;
841
  /*
842
   * The 4 here insures there is space for 2 max-sized
843
   * codes in LZWEncode and LZWPostDecode.
844
   */
845
1.08k
  sp->enc_rawlimit = tif->tif_rawdata + tif->tif_rawdatasize-1 - 4;
846
1.08k
  cl_hash(sp);    /* clear hash table */
847
1.08k
  sp->enc_oldcode = (hcode_t) -1; /* generates CODE_CLEAR in LZWEncode */
848
1.08k
  return (1);
849
1.08k
}
850
851
107k
#define CALCRATIO(sp, rat) {         \
852
107k
  if (incount > 0x007fffff) { /* NB: shift will overflow */\
853
0
    rat = outcount >> 8;        \
854
0
    rat = (rat == 0 ? 0x7fffffff : incount/rat);  \
855
0
  } else             \
856
107k
    rat = (incount<<8) / outcount;     \
857
107k
}
858
859
/* Explicit 0xff masking to make icc -check=conversions happy */
860
129M
#define PutNextCode(op, c) {         \
861
129M
  nextdata = (nextdata << nbits) | c;     \
862
129M
  nextbits += nbits;          \
863
129M
  *op++ = (unsigned char)((nextdata >> (nextbits-8))&0xff);   \
864
129M
  nextbits -= 8;            \
865
129M
  if (nextbits >= 8) {         \
866
52.7M
    *op++ = (unsigned char)((nextdata >> (nextbits-8))&0xff); \
867
52.7M
    nextbits -= 8;          \
868
52.7M
  }              \
869
129M
  outcount += nbits;          \
870
129M
}
871
872
/*
873
 * Encode a chunk of pixels.
874
 *
875
 * Uses an open addressing double hashing (no chaining) on the 
876
 * prefix code/next character combination.  We do a variant of
877
 * Knuth's algorithm D (vol. 3, sec. 6.4) along with G. Knott's
878
 * relatively-prime secondary probe.  Here, the modular division
879
 * first probe is gives way to a faster exclusive-or manipulation. 
880
 * Also do block compression with an adaptive reset, whereby the
881
 * code table is cleared when the compression ratio decreases,
882
 * but after the table fills.  The variable-length output codes
883
 * are re-sized at this point, and a CODE_CLEAR is generated
884
 * for the decoder. 
885
 */
886
static int
887
LZWEncode(TIFF* tif, uint8_t* bp, tmsize_t cc, uint16_t s)
888
877k
{
889
877k
  register LZWCodecState *sp = EncoderState(tif);
890
877k
  register long fcode;
891
877k
  register hash_t *hp;
892
877k
  register int h, c;
893
877k
  hcode_t ent;
894
877k
  long disp;
895
877k
  long incount, outcount, checkpoint;
896
877k
  unsigned long nextdata;
897
877k
        long nextbits;
898
877k
  int free_ent, maxcode, nbits;
899
877k
  uint8_t* op;
900
877k
  uint8_t* limit;
901
902
877k
  (void) s;
903
877k
  if (sp == NULL)
904
0
    return (0);
905
906
877k
        assert(sp->enc_hashtab != NULL);
907
908
  /*
909
   * Load local state.
910
   */
911
877k
  incount = sp->enc_incount;
912
877k
  outcount = sp->enc_outcount;
913
877k
  checkpoint = sp->enc_checkpoint;
914
877k
  nextdata = sp->lzw_nextdata;
915
877k
  nextbits = sp->lzw_nextbits;
916
877k
  free_ent = sp->lzw_free_ent;
917
877k
  maxcode = sp->lzw_maxcode;
918
877k
  nbits = sp->lzw_nbits;
919
877k
  op = tif->tif_rawcp;
920
877k
  limit = sp->enc_rawlimit;
921
877k
  ent = (hcode_t)sp->enc_oldcode;
922
923
877k
  if (ent == (hcode_t) -1 && cc > 0) {
924
    /*
925
     * NB: This is safe because it can only happen
926
     *     at the start of a strip where we know there
927
     *     is space in the data buffer.
928
     */
929
1.08k
    PutNextCode(op, CODE_CLEAR);
930
1.08k
    ent = *bp++; cc--; incount++;
931
1.08k
  }
932
3.88G
  while (cc > 0) {
933
3.88G
    c = *bp++; cc--; incount++;
934
3.88G
    fcode = ((long)c << BITS_MAX) + ent;
935
3.88G
    h = (c << HSHIFT) ^ ent;  /* xor hashing */
936
#ifdef _WINDOWS
937
    /*
938
     * Check hash index for an overflow.
939
     */
940
    if (h >= HSIZE)
941
      h -= HSIZE;
942
#endif
943
3.88G
    hp = &sp->enc_hashtab[h];
944
3.88G
    if (hp->hash == fcode) {
945
3.65G
      ent = hp->code;
946
3.65G
      continue;
947
3.65G
    }
948
228M
    if (hp->hash >= 0) {
949
      /*
950
       * Primary hash failed, check secondary hash.
951
       */
952
127M
      disp = HSIZE - h;
953
127M
      if (h == 0)
954
52.1k
        disp = 1;
955
192M
      do {
956
        /*
957
         * Avoid pointer arithmetic because of
958
         * wraparound problems with segments.
959
         */
960
192M
        if ((h -= disp) < 0)
961
85.3M
          h += HSIZE;
962
192M
        hp = &sp->enc_hashtab[h];
963
192M
        if (hp->hash == fcode) {
964
98.5M
          ent = hp->code;
965
98.5M
          goto hit;
966
98.5M
        }
967
192M
      } while (hp->hash >= 0);
968
127M
    }
969
    /*
970
     * New entry, emit code and add to table.
971
     */
972
    /*
973
     * Verify there is space in the buffer for the code
974
     * and any potential Clear code that might be emitted
975
     * below.  The value of limit is setup so that there
976
     * are at least 4 bytes free--room for 2 codes.
977
     */
978
129M
    if (op > limit) {
979
57
      tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata);
980
57
      if( !TIFFFlushData1(tif) )
981
0
                            return 0;
982
57
      op = tif->tif_rawdata;
983
57
    }
984
129M
    PutNextCode(op, ent);
985
129M
    ent = (hcode_t)c;
986
129M
    hp->code = (hcode_t)(free_ent++);
987
129M
    hp->hash = fcode;
988
129M
    if (free_ent == CODE_MAX-1) {
989
      /* table is full, emit clear code and reset */
990
32.9k
      cl_hash(sp);
991
32.9k
      sp->enc_ratio = 0;
992
32.9k
      incount = 0;
993
32.9k
      outcount = 0;
994
32.9k
      free_ent = CODE_FIRST;
995
32.9k
      PutNextCode(op, CODE_CLEAR);
996
32.9k
      nbits = BITS_MIN;
997
32.9k
      maxcode = MAXCODE(BITS_MIN);
998
129M
    } else {
999
      /*
1000
       * If the next entry is going to be too big for
1001
       * the code size, then increase it, if possible.
1002
       */
1003
129M
      if (free_ent > maxcode) {
1004
102k
        nbits++;
1005
102k
        assert(nbits <= BITS_MAX);
1006
102k
        maxcode = (int) MAXCODE(nbits);
1007
129M
      } else if (incount >= checkpoint) {
1008
107k
        long rat;
1009
        /*
1010
         * Check compression ratio and, if things seem
1011
         * to be slipping, clear the hash table and
1012
         * reset state.  The compression ratio is a
1013
         * 24+8-bit fractional number.
1014
         */
1015
107k
        checkpoint = incount+CHECK_GAP;
1016
107k
        CALCRATIO(sp, rat);
1017
107k
        if (rat <= sp->enc_ratio) {
1018
776
          cl_hash(sp);
1019
776
          sp->enc_ratio = 0;
1020
776
          incount = 0;
1021
776
          outcount = 0;
1022
776
          free_ent = CODE_FIRST;
1023
776
          PutNextCode(op, CODE_CLEAR);
1024
776
          nbits = BITS_MIN;
1025
776
          maxcode = MAXCODE(BITS_MIN);
1026
776
        } else
1027
106k
          sp->enc_ratio = rat;
1028
107k
      }
1029
129M
    }
1030
228M
  hit:
1031
228M
    ;
1032
228M
  }
1033
1034
  /*
1035
   * Restore global state.
1036
   */
1037
877k
  sp->enc_incount = incount;
1038
877k
  sp->enc_outcount = outcount;
1039
877k
  sp->enc_checkpoint = checkpoint;
1040
877k
  sp->enc_oldcode = ent;
1041
877k
  sp->lzw_nextdata = nextdata;
1042
877k
  sp->lzw_nextbits = nextbits;
1043
877k
  sp->lzw_free_ent = (unsigned short)free_ent;
1044
877k
  sp->lzw_maxcode = (unsigned short)maxcode;
1045
877k
  sp->lzw_nbits = (unsigned short)nbits;
1046
877k
  tif->tif_rawcp = op;
1047
877k
  return (1);
1048
877k
}
1049
1050
/*
1051
 * Finish off an encoded strip by flushing the last
1052
 * string and tacking on an End Of Information code.
1053
 */
1054
static int
1055
LZWPostEncode(TIFF* tif)
1056
1.08k
{
1057
1.08k
  register LZWCodecState *sp = EncoderState(tif);
1058
1.08k
  uint8_t* op = tif->tif_rawcp;
1059
1.08k
  long nextbits = sp->lzw_nextbits;
1060
1.08k
  unsigned long nextdata = sp->lzw_nextdata;
1061
1.08k
  long outcount = sp->enc_outcount;
1062
1.08k
  int nbits = sp->lzw_nbits;
1063
1064
1.08k
  if (op > sp->enc_rawlimit) {
1065
0
    tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata);
1066
0
    if( !TIFFFlushData1(tif) )
1067
0
                    return 0;
1068
0
    op = tif->tif_rawdata;
1069
0
  }
1070
1.08k
  if (sp->enc_oldcode != (hcode_t) -1) {
1071
1.08k
                int free_ent = sp->lzw_free_ent;
1072
1073
1.08k
    PutNextCode(op, sp->enc_oldcode);
1074
1.08k
    sp->enc_oldcode = (hcode_t) -1;
1075
1.08k
                free_ent ++;
1076
1077
1.08k
                if (free_ent == CODE_MAX-1) {
1078
                        /* table is full, emit clear code and reset */
1079
0
                        outcount = 0;
1080
0
                        PutNextCode(op, CODE_CLEAR);
1081
0
                        nbits = BITS_MIN;
1082
1.08k
                } else {
1083
                        /*
1084
                        * If the next entry is going to be too big for
1085
                        * the code size, then increase it, if possible.
1086
                        */
1087
1.08k
                        if (free_ent > sp->lzw_maxcode) {
1088
1
                                nbits++;
1089
1
                                assert(nbits <= BITS_MAX);
1090
1
                        }
1091
1.08k
                }
1092
1.08k
  }
1093
1.08k
  PutNextCode(op, CODE_EOI);
1094
        /* Explicit 0xff masking to make icc -check=conversions happy */
1095
1.08k
  if (nextbits > 0) 
1096
918
    *op++ = (unsigned char)((nextdata << (8-nextbits))&0xff);
1097
1.08k
  tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata);
1098
1.08k
  return (1);
1099
1.08k
}
1100
1101
/*
1102
 * Reset encoding hash table.
1103
 */
1104
static void
1105
cl_hash(LZWCodecState* sp)
1106
34.7k
{
1107
34.7k
  register hash_t *hp = &sp->enc_hashtab[HSIZE-1];
1108
34.7k
  register long i = HSIZE-8;
1109
1110
39.1M
  do {
1111
39.1M
    i -= 8;
1112
39.1M
    hp[-7].hash = -1;
1113
39.1M
    hp[-6].hash = -1;
1114
39.1M
    hp[-5].hash = -1;
1115
39.1M
    hp[-4].hash = -1;
1116
39.1M
    hp[-3].hash = -1;
1117
39.1M
    hp[-2].hash = -1;
1118
39.1M
    hp[-1].hash = -1;
1119
39.1M
    hp[ 0].hash = -1;
1120
39.1M
    hp -= 8;
1121
39.1M
  } while (i >= 0);
1122
69.5k
  for (i += 8; i > 0; i--, hp--)
1123
34.7k
    hp->hash = -1;
1124
34.7k
}
1125
1126
static void
1127
LZWCleanup(TIFF* tif)
1128
1.27k
{
1129
1.27k
  (void)TIFFPredictorCleanup(tif);
1130
1131
1.27k
  assert(tif->tif_data != 0);
1132
1133
1.27k
  if (DecoderState(tif)->dec_codetab)
1134
0
    _TIFFfree(DecoderState(tif)->dec_codetab);
1135
1136
1.27k
  if (EncoderState(tif)->enc_hashtab)
1137
1.08k
    _TIFFfree(EncoderState(tif)->enc_hashtab);
1138
1139
1.27k
  _TIFFfree(tif->tif_data);
1140
1.27k
  tif->tif_data = NULL;
1141
1142
1.27k
  _TIFFSetDefaultCompressionState(tif);
1143
1.27k
}
1144
1145
int
1146
TIFFInitLZW(TIFF* tif, int scheme)
1147
1.27k
{
1148
1.27k
  static const char module[] = "TIFFInitLZW";
1149
1.27k
        (void)scheme;
1150
1.27k
  assert(scheme == COMPRESSION_LZW);
1151
  /*
1152
   * Allocate state block so tag methods have storage to record values.
1153
   */
1154
1.27k
  tif->tif_data = (uint8_t*) _TIFFmalloc(sizeof (LZWCodecState));
1155
1.27k
  if (tif->tif_data == NULL)
1156
0
    goto bad;
1157
1.27k
  DecoderState(tif)->dec_codetab = NULL;
1158
1.27k
  DecoderState(tif)->dec_decode = NULL;
1159
1.27k
  EncoderState(tif)->enc_hashtab = NULL;
1160
1.27k
        LZWState(tif)->rw_mode = tif->tif_mode;
1161
1162
  /*
1163
   * Install codec methods.
1164
   */
1165
1.27k
  tif->tif_fixuptags = LZWFixupTags; 
1166
1.27k
  tif->tif_setupdecode = LZWSetupDecode;
1167
1.27k
  tif->tif_predecode = LZWPreDecode;
1168
1.27k
  tif->tif_decoderow = LZWDecode;
1169
1.27k
  tif->tif_decodestrip = LZWDecode;
1170
1.27k
  tif->tif_decodetile = LZWDecode;
1171
1.27k
  tif->tif_setupencode = LZWSetupEncode;
1172
1.27k
  tif->tif_preencode = LZWPreEncode;
1173
1.27k
  tif->tif_postencode = LZWPostEncode;
1174
1.27k
  tif->tif_encoderow = LZWEncode;
1175
1.27k
  tif->tif_encodestrip = LZWEncode;
1176
1.27k
  tif->tif_encodetile = LZWEncode;
1177
1.27k
  tif->tif_cleanup = LZWCleanup;
1178
  /*
1179
   * Setup predictor setup.
1180
   */
1181
1.27k
  (void) TIFFPredictorInit(tif);
1182
1.27k
  return (1);
1183
0
bad:
1184
0
  TIFFErrorExt(tif->tif_clientdata, module, 
1185
0
         "No space for LZW state block");
1186
0
  return (0);
1187
1.27k
}
1188
1189
/*
1190
 * Copyright (c) 1985, 1986 The Regents of the University of California.
1191
 * All rights reserved.
1192
 *
1193
 * This code is derived from software contributed to Berkeley by
1194
 * James A. Woods, derived from original work by Spencer Thomas
1195
 * and Joseph Orost.
1196
 *
1197
 * Redistribution and use in source and binary forms are permitted
1198
 * provided that the above copyright notice and this paragraph are
1199
 * duplicated in all such forms and that any documentation,
1200
 * advertising materials, and other materials related to such
1201
 * distribution and use acknowledge that the software was developed
1202
 * by the University of California, Berkeley.  The name of the
1203
 * University may not be used to endorse or promote products derived
1204
 * from this software without specific prior written permission.
1205
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1206
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1207
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1208
 */
1209
#endif /* LZW_SUPPORT */
1210
1211
/* vim: set ts=8 sts=8 sw=8 noet: */
1212
/*
1213
 * Local Variables:
1214
 * mode: c
1215
 * c-basic-offset: 8
1216
 * fill-column: 78
1217
 * End:
1218
 */