Coverage Report

Created: 2023-06-07 08:11

/src/opencv/3rdparty/libtiff/tif_zip.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 1995-1997 Sam Leffler
3
 * Copyright (c) 1995-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 ZIP_SUPPORT
27
/*
28
 * TIFF Library.
29
 *
30
 * ZIP (aka Deflate) Compression Support
31
 *
32
 * This file is an interface to the zlib library written by
33
 * Jean-loup Gailly and Mark Adler.  You must use version 1.0 or later
34
 * of the library.
35
 *
36
 * Optionally, libdeflate (https://github.com/ebiggers/libdeflate) may be used
37
 * to do the compression and decompression, but only for whole strips and tiles.
38
 * For scanline access, zlib will be sued as a fallback.
39
 */
40
#include "tif_predict.h"
41
#include "zlib.h"
42
43
#if LIBDEFLATE_SUPPORT
44
#include "libdeflate.h"
45
#endif
46
0
#define LIBDEFLATE_MAX_COMPRESSION_LEVEL 12
47
48
#include <stdio.h>
49
50
/*
51
 * Sigh, ZLIB_VERSION is defined as a string so there's no
52
 * way to do a proper check here.  Instead we guess based
53
 * on the presence of #defines that were added between the
54
 * 0.95 and 1.0 distributions.
55
 */
56
#if !defined(Z_NO_COMPRESSION) || !defined(Z_DEFLATED)
57
#error "Antiquated ZLIB software; you must use version 1.0 or later"
58
#endif
59
60
657k
#define SAFE_MSG(sp)   ((sp)->stream.msg == NULL ? "" : (sp)->stream.msg)
61
62
/*
63
 * State block for each open TIFF
64
 * file using ZIP compression/decompression.
65
 */
66
typedef struct {
67
  TIFFPredictorState predict;
68
        z_stream        stream;
69
  int             zipquality;            /* compression level */
70
  int             state;                 /* state flags */
71
  int             subcodec;              /* DEFLATE_SUBCODEC_ZLIB or DEFLATE_SUBCODEC_LIBDEFLATE */
72
#if LIBDEFLATE_SUPPORT
73
  int             libdeflate_state;       /* -1 = until first time ZIPEncode() / ZIPDecode() is called, 0 = use zlib, 1 = use libdeflate */
74
  struct libdeflate_decompressor* libdeflate_dec;
75
  struct libdeflate_compressor*   libdeflate_enc;
76
#endif
77
698k
#define ZSTATE_INIT_DECODE 0x01
78
546
#define ZSTATE_INIT_ENCODE 0x02
79
80
  TIFFVGetMethod  vgetparent;            /* super-class method */
81
  TIFFVSetMethod  vsetparent;            /* super-class method */
82
} ZIPState;
83
84
5.98M
#define ZState(tif)             ((ZIPState*) (tif)->tif_data)
85
1.39M
#define DecoderState(tif)       ZState(tif)
86
0
#define EncoderState(tif)       ZState(tif)
87
88
static int ZIPEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s);
89
static int ZIPDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s);
90
91
static int
92
ZIPFixupTags(TIFF* tif)
93
258
{
94
258
  (void) tif;
95
258
  return (1);
96
258
}
97
98
static int
99
ZIPSetupDecode(TIFF* tif)
100
239
{
101
239
  static const char module[] = "ZIPSetupDecode";
102
239
  ZIPState* sp = DecoderState(tif);
103
104
239
  assert(sp != NULL);
105
        
106
        /* if we were last encoding, terminate this mode */
107
239
  if (sp->state & ZSTATE_INIT_ENCODE) {
108
0
      deflateEnd(&sp->stream);
109
0
      sp->state = 0;
110
0
  }
111
112
  /* This function can possibly be called several times by */
113
  /* PredictorSetupDecode() if this function succeeds but */
114
  /* PredictorSetup() fails */
115
239
  if ((sp->state & ZSTATE_INIT_DECODE) == 0 &&
116
239
      inflateInit(&sp->stream) != Z_OK) {
117
0
    TIFFErrorExt(tif->tif_clientdata, module, "%s", SAFE_MSG(sp));
118
0
    return (0);
119
239
  } else {
120
239
    sp->state |= ZSTATE_INIT_DECODE;
121
239
    return (1);
122
239
  }
123
239
}
124
125
/*
126
 * Setup state for decoding a strip.
127
 */
128
static int
129
ZIPPreDecode(TIFF* tif, uint16 s)
130
697k
{
131
697k
  ZIPState* sp = DecoderState(tif);
132
133
697k
  (void) s;
134
697k
  assert(sp != NULL);
135
136
697k
  if( (sp->state & ZSTATE_INIT_DECODE) == 0 )
137
0
            tif->tif_setupdecode( tif );
138
139
#if LIBDEFLATE_SUPPORT
140
        sp->libdeflate_state = -1;
141
#endif
142
697k
  sp->stream.next_in = tif->tif_rawdata;
143
697k
  assert(sizeof(sp->stream.avail_in)==4);  /* if this assert gets raised,
144
      we need to simplify this code to reflect a ZLib that is likely updated
145
      to deal with 8byte memory sizes, though this code will respond
146
      appropriately even before we simplify it */
147
697k
  sp->stream.avail_in = (uint64)tif->tif_rawcc < 0xFFFFFFFFU ? (uInt) tif->tif_rawcc : 0xFFFFFFFFU;
148
697k
  return (inflateReset(&sp->stream) == Z_OK);
149
697k
}
150
151
static int
152
ZIPDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
153
697k
{
154
697k
  static const char module[] = "ZIPDecode";
155
697k
  ZIPState* sp = DecoderState(tif);
156
157
697k
  (void) s;
158
697k
  assert(sp != NULL);
159
697k
  assert(sp->state == ZSTATE_INIT_DECODE);
160
161
#if LIBDEFLATE_SUPPORT
162
        if( sp->libdeflate_state == 1 )
163
            return 0;
164
165
        /* If we have libdeflate support and we are asked to read a whole */
166
        /* strip/tile, then go for using it */
167
        do {
168
            TIFFDirectory *td = &tif->tif_dir;
169
170
            if( sp->libdeflate_state == 0 )
171
                break;
172
            if( sp->subcodec == DEFLATE_SUBCODEC_ZLIB )
173
                break;
174
175
            /* Check if we are in the situation where we can use libdeflate */
176
            if (isTiled(tif)) {
177
                if( TIFFTileSize64(tif) != (uint64)occ )
178
                    break;
179
            } else {
180
                uint32 strip_height = td->td_imagelength - tif->tif_row;
181
                if (strip_height > td->td_rowsperstrip)
182
                    strip_height = td->td_rowsperstrip;
183
                if( TIFFVStripSize64(tif, strip_height) != (uint64)occ )
184
                    break;
185
            }
186
187
            /* Check for overflow */
188
            if( (size_t)tif->tif_rawcc != (uint64)tif->tif_rawcc )
189
                break;
190
            if( (size_t)occ != (uint64)occ )
191
                break;
192
193
            /* Go for decompression using libdeflate */
194
            {
195
                enum libdeflate_result res;
196
                if( sp->libdeflate_dec == NULL )
197
                {
198
                    sp->libdeflate_dec = libdeflate_alloc_decompressor();
199
                    if( sp->libdeflate_dec == NULL )
200
                    {
201
                        break;
202
                    }
203
                }
204
205
                sp->libdeflate_state = 1;
206
207
                res = libdeflate_zlib_decompress(
208
                    sp->libdeflate_dec, tif->tif_rawcp, (size_t)tif->tif_rawcc, op, (size_t)occ, NULL);
209
210
                tif->tif_rawcp += tif->tif_rawcc;
211
                tif->tif_rawcc = 0;
212
213
                /* We accept LIBDEFLATE_INSUFFICIENT_SPACE has a return */
214
                /* There are odd files in the wild where the last strip, when */
215
                /* it is smaller in height than td_rowsperstrip, actually contains */
216
                /* data for td_rowsperstrip lines. Just ignore that silently. */
217
                if( res != LIBDEFLATE_SUCCESS &&
218
                    res != LIBDEFLATE_INSUFFICIENT_SPACE )
219
                {
220
                    TIFFErrorExt(tif->tif_clientdata, module,
221
                                 "Decoding error at scanline %lu",
222
                                 (unsigned long) tif->tif_row);
223
                    return 0;
224
                }
225
226
                return 1;
227
            }
228
        } while(0);
229
        sp->libdeflate_state = 0;
230
#endif /* LIBDEFLATE_SUPPORT */
231
232
697k
        sp->stream.next_in = tif->tif_rawcp;
233
        
234
697k
  sp->stream.next_out = op;
235
697k
  assert(sizeof(sp->stream.avail_out)==4);  /* if this assert gets raised,
236
      we need to simplify this code to reflect a ZLib that is likely updated
237
      to deal with 8byte memory sizes, though this code will respond
238
      appropriately even before we simplify it */
239
708k
  do {
240
708k
                int state;
241
708k
                uInt avail_in_before = (uint64)tif->tif_rawcc <= 0xFFFFFFFFU ? (uInt)tif->tif_rawcc : 0xFFFFFFFFU;
242
708k
                uInt avail_out_before = (uint64)occ < 0xFFFFFFFFU ? (uInt) occ : 0xFFFFFFFFU;
243
708k
                sp->stream.avail_in = avail_in_before;
244
708k
                sp->stream.avail_out = avail_out_before;
245
708k
    state = inflate(&sp->stream, Z_PARTIAL_FLUSH);
246
708k
    tif->tif_rawcc -= (avail_in_before - sp->stream.avail_in);
247
708k
                occ -= (avail_out_before - sp->stream.avail_out);
248
708k
    if (state == Z_STREAM_END)
249
28.6k
      break;
250
680k
    if (state == Z_DATA_ERROR) {
251
641k
      TIFFErrorExt(tif->tif_clientdata, module,
252
641k
          "Decoding error at scanline %lu, %s",
253
641k
           (unsigned long) tif->tif_row, SAFE_MSG(sp));
254
641k
      return (0);
255
641k
    }
256
39.2k
    if (state != Z_OK) {
257
16.3k
      TIFFErrorExt(tif->tif_clientdata, module, 
258
16.3k
             "ZLib error: %s", SAFE_MSG(sp));
259
16.3k
      return (0);
260
16.3k
    }
261
39.2k
  } while (occ > 0);
262
40.0k
  if (occ != 0) {
263
28.6k
    TIFFErrorExt(tif->tif_clientdata, module,
264
28.6k
        "Not enough data at scanline %lu (short " TIFF_UINT64_FORMAT " bytes)",
265
28.6k
        (unsigned long) tif->tif_row, (TIFF_UINT64_T) occ);
266
28.6k
    return (0);
267
28.6k
  }
268
269
11.4k
        tif->tif_rawcp = sp->stream.next_in;
270
271
11.4k
  return (1);
272
40.0k
}
273
274
static int
275
ZIPSetupEncode(TIFF* tif)
276
0
{
277
0
  static const char module[] = "ZIPSetupEncode";
278
0
  ZIPState* sp = EncoderState(tif);
279
0
        int cappedQuality;
280
281
0
  assert(sp != NULL);
282
0
  if (sp->state & ZSTATE_INIT_DECODE) {
283
0
    inflateEnd(&sp->stream);
284
0
    sp->state = 0;
285
0
  }
286
287
0
        cappedQuality = sp->zipquality;
288
0
        if( cappedQuality > Z_BEST_COMPRESSION )
289
0
            cappedQuality = Z_BEST_COMPRESSION;
290
291
0
  if (deflateInit(&sp->stream, cappedQuality) != Z_OK) {
292
0
    TIFFErrorExt(tif->tif_clientdata, module, "%s", SAFE_MSG(sp));
293
0
    return (0);
294
0
  } else {
295
0
    sp->state |= ZSTATE_INIT_ENCODE;
296
0
    return (1);
297
0
  }
298
0
}
299
300
/*
301
 * Reset encoding state at the start of a strip.
302
 */
303
static int
304
ZIPPreEncode(TIFF* tif, uint16 s)
305
0
{
306
0
  ZIPState *sp = EncoderState(tif);
307
308
0
  (void) s;
309
0
  assert(sp != NULL);
310
0
  if( sp->state != ZSTATE_INIT_ENCODE )
311
0
            tif->tif_setupencode( tif );
312
313
#if LIBDEFLATE_SUPPORT
314
        sp->libdeflate_state = -1;
315
#endif
316
0
  sp->stream.next_out = tif->tif_rawdata;
317
0
  assert(sizeof(sp->stream.avail_out)==4);  /* if this assert gets raised,
318
      we need to simplify this code to reflect a ZLib that is likely updated
319
      to deal with 8byte memory sizes, though this code will respond
320
      appropriately even before we simplify it */
321
0
  sp->stream.avail_out = (uint64)tif->tif_rawdatasize <= 0xFFFFFFFFU ? (uInt)tif->tif_rawdatasize : 0xFFFFFFFFU;
322
0
  return (deflateReset(&sp->stream) == Z_OK);
323
0
}
324
325
/*
326
 * Encode a chunk of pixels.
327
 */
328
static int
329
ZIPEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
330
0
{
331
0
  static const char module[] = "ZIPEncode";
332
0
  ZIPState *sp = EncoderState(tif);
333
334
0
  assert(sp != NULL);
335
0
  assert(sp->state == ZSTATE_INIT_ENCODE);
336
337
0
  (void) s;
338
339
#if LIBDEFLATE_SUPPORT
340
        if( sp->libdeflate_state == 1 )
341
            return 0;
342
343
        /* If we have libdeflate support and we are asked to write a whole */
344
        /* strip/tile, then go for using it */
345
        do {
346
            TIFFDirectory *td = &tif->tif_dir;
347
348
            if( sp->libdeflate_state == 0 )
349
                break;
350
            if( sp->subcodec == DEFLATE_SUBCODEC_ZLIB )
351
                break;
352
353
            /* Libdeflate does not support the 0-compression level */
354
            if( sp->zipquality == Z_NO_COMPRESSION )
355
                break;
356
357
            /* Check if we are in the situation where we can use libdeflate */
358
            if (isTiled(tif)) {
359
                if( TIFFTileSize64(tif) != (uint64)cc )
360
                    break;
361
            } else {
362
                uint32 strip_height = td->td_imagelength - tif->tif_row;
363
                if (strip_height > td->td_rowsperstrip)
364
                    strip_height = td->td_rowsperstrip;
365
                if( TIFFVStripSize64(tif, strip_height) != (uint64)cc )
366
                    break;
367
            }
368
369
            /* Check for overflow */
370
            if( (size_t)tif->tif_rawdatasize != (uint64)tif->tif_rawdatasize )
371
                break;
372
            if( (size_t)cc != (uint64)cc )
373
                break;
374
375
            /* Go for compression using libdeflate */
376
            {
377
                size_t nCompressedBytes;
378
                if( sp->libdeflate_enc == NULL )
379
                {
380
                    /* To get results as good as zlib, we asked for an extra */
381
                    /* level of compression */
382
                    sp->libdeflate_enc = libdeflate_alloc_compressor(
383
                        sp->zipquality == Z_DEFAULT_COMPRESSION ? 7 :
384
                        sp->zipquality >= 6 && sp->zipquality <= 9 ? sp->zipquality + 1 :
385
                        sp->zipquality);
386
                    if( sp->libdeflate_enc == NULL )
387
                    {
388
                        TIFFErrorExt(tif->tif_clientdata, module,
389
                                    "Cannot allocate compressor");
390
                        break;
391
                    }
392
                }
393
394
                /* Make sure the output buffer is large enough for the worse case. */
395
                /* In TIFFWriteBufferSetup(), when libtiff allocates the buffer */
396
                /* we've taken a 10% margin over the uncompressed size, which should */
397
                /* be large enough even for the the worse case scenario. */
398
                if( libdeflate_zlib_compress_bound(sp->libdeflate_enc, (size_t)cc) >
399
                        (size_t)tif->tif_rawdatasize)
400
                {
401
                    break;
402
                }
403
404
                sp->libdeflate_state = 1;
405
                nCompressedBytes = libdeflate_zlib_compress(
406
                    sp->libdeflate_enc, bp, (size_t)cc, tif->tif_rawdata, (size_t)tif->tif_rawdatasize);
407
408
                if( nCompressedBytes == 0 )
409
                {
410
                    TIFFErrorExt(tif->tif_clientdata, module,
411
                                 "Encoder error at scanline %lu",
412
                                 (unsigned long) tif->tif_row);
413
                    return 0;
414
                }
415
416
                tif->tif_rawcc = nCompressedBytes;
417
418
                if( !TIFFFlushData1(tif) )
419
                    return 0;
420
421
                return 1;
422
            }
423
        } while(0);
424
        sp->libdeflate_state = 0;
425
#endif /* LIBDEFLATE_SUPPORT */
426
427
0
  sp->stream.next_in = bp;
428
0
  assert(sizeof(sp->stream.avail_in)==4);  /* if this assert gets raised,
429
      we need to simplify this code to reflect a ZLib that is likely updated
430
      to deal with 8byte memory sizes, though this code will respond
431
      appropriately even before we simplify it */
432
0
  do {
433
0
                uInt avail_in_before = (uint64)cc <= 0xFFFFFFFFU ? (uInt)cc : 0xFFFFFFFFU;
434
0
                sp->stream.avail_in = avail_in_before;
435
0
    if (deflate(&sp->stream, Z_NO_FLUSH) != Z_OK) {
436
0
      TIFFErrorExt(tif->tif_clientdata, module, 
437
0
             "Encoder error: %s",
438
0
             SAFE_MSG(sp));
439
0
      return (0);
440
0
    }
441
0
    if (sp->stream.avail_out == 0) {
442
0
      tif->tif_rawcc = tif->tif_rawdatasize;
443
0
      if (!TIFFFlushData1(tif))
444
0
        return 0;
445
0
      sp->stream.next_out = tif->tif_rawdata;
446
0
      sp->stream.avail_out = (uint64)tif->tif_rawdatasize <= 0xFFFFFFFFU ? (uInt)tif->tif_rawdatasize : 0xFFFFFFFFU;
447
0
    }
448
0
    cc -= (avail_in_before - sp->stream.avail_in);
449
0
  } while (cc > 0);
450
0
  return (1);
451
0
}
452
453
/*
454
 * Finish off an encoded strip by flushing the last
455
 * string and tacking on an End Of Information code.
456
 */
457
static int
458
ZIPPostEncode(TIFF* tif)
459
0
{
460
0
  static const char module[] = "ZIPPostEncode";
461
0
  ZIPState *sp = EncoderState(tif);
462
0
  int state;
463
464
#if LIBDEFLATE_SUPPORT
465
        if( sp->libdeflate_state == 1 )
466
            return 1;
467
#endif
468
469
0
  sp->stream.avail_in = 0;
470
0
  do {
471
0
    state = deflate(&sp->stream, Z_FINISH);
472
0
    switch (state) {
473
0
    case Z_STREAM_END:
474
0
    case Z_OK:
475
0
      if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize)
476
0
      {
477
0
        tif->tif_rawcc =  tif->tif_rawdatasize - sp->stream.avail_out;
478
0
        if (!TIFFFlushData1(tif))
479
0
          return 0;
480
0
        sp->stream.next_out = tif->tif_rawdata;
481
0
        sp->stream.avail_out = (uint64)tif->tif_rawdatasize <= 0xFFFFFFFFU ? (uInt)tif->tif_rawdatasize : 0xFFFFFFFFU;
482
0
      }
483
0
      break;
484
0
    default:
485
0
      TIFFErrorExt(tif->tif_clientdata, module, 
486
0
             "ZLib error: %s", SAFE_MSG(sp));
487
0
      return (0);
488
0
    }
489
0
  } while (state != Z_STREAM_END);
490
0
  return (1);
491
0
}
492
493
static void
494
ZIPCleanup(TIFF* tif)
495
307
{
496
307
  ZIPState* sp = ZState(tif);
497
498
307
  assert(sp != 0);
499
500
307
  (void)TIFFPredictorCleanup(tif);
501
502
307
  tif->tif_tagmethods.vgetfield = sp->vgetparent;
503
307
  tif->tif_tagmethods.vsetfield = sp->vsetparent;
504
505
307
  if (sp->state & ZSTATE_INIT_ENCODE) {
506
0
    deflateEnd(&sp->stream);
507
0
    sp->state = 0;
508
307
  } else if( sp->state & ZSTATE_INIT_DECODE) {
509
239
    inflateEnd(&sp->stream);
510
239
    sp->state = 0;
511
239
  }
512
513
#if LIBDEFLATE_SUPPORT
514
        if( sp->libdeflate_dec )
515
            libdeflate_free_decompressor(sp->libdeflate_dec);
516
        if( sp->libdeflate_enc )
517
            libdeflate_free_compressor(sp->libdeflate_enc);
518
#endif
519
520
307
  _TIFFfree(sp);
521
307
  tif->tif_data = NULL;
522
523
307
  _TIFFSetDefaultCompressionState(tif);
524
307
}
525
526
static int
527
ZIPVSetField(TIFF* tif, uint32 tag, va_list ap)
528
4.07k
{
529
4.07k
  static const char module[] = "ZIPVSetField";
530
4.07k
  ZIPState* sp = ZState(tif);
531
532
4.07k
  switch (tag) {
533
0
  case TIFFTAG_ZIPQUALITY:
534
0
    sp->zipquality = (int) va_arg(ap, int);
535
0
                if( sp->zipquality < Z_DEFAULT_COMPRESSION ||
536
0
                    sp->zipquality > LIBDEFLATE_MAX_COMPRESSION_LEVEL ) {
537
0
                    TIFFErrorExt(tif->tif_clientdata, module,
538
0
                                 "Invalid ZipQuality value. Should be in [-1,%d] range",
539
0
                                 LIBDEFLATE_MAX_COMPRESSION_LEVEL);
540
0
                    return 0;
541
0
                }
542
543
0
                if ( sp->state&ZSTATE_INIT_ENCODE ) {
544
0
                        int cappedQuality = sp->zipquality;
545
0
                        if( cappedQuality > Z_BEST_COMPRESSION )
546
0
                            cappedQuality = Z_BEST_COMPRESSION;
547
0
      if (deflateParams(&sp->stream,
548
0
          cappedQuality, Z_DEFAULT_STRATEGY) != Z_OK) {
549
0
        TIFFErrorExt(tif->tif_clientdata, module, "ZLib error: %s",
550
0
               SAFE_MSG(sp));
551
0
        return (0);
552
0
      }
553
0
    }
554
555
#if LIBDEFLATE_SUPPORT
556
                if( sp->libdeflate_enc )
557
                {
558
                    libdeflate_free_compressor(sp->libdeflate_enc);
559
                    sp->libdeflate_enc = NULL;
560
                }
561
#endif
562
563
0
    return (1);
564
565
0
        case TIFFTAG_DEFLATE_SUBCODEC:
566
0
                sp->subcodec = (int) va_arg(ap, int);
567
0
                if( sp->subcodec != DEFLATE_SUBCODEC_ZLIB &&
568
0
                    sp->subcodec != DEFLATE_SUBCODEC_LIBDEFLATE )
569
0
                {
570
0
                    TIFFErrorExt(tif->tif_clientdata, module,
571
0
                                 "Invalid DeflateCodec value.");
572
0
                    return 0;
573
0
                }
574
0
#if !LIBDEFLATE_SUPPORT
575
0
                if( sp->subcodec == DEFLATE_SUBCODEC_LIBDEFLATE )
576
0
                {
577
0
                    TIFFErrorExt(tif->tif_clientdata, module,
578
0
                                 "DeflateCodec = DEFLATE_SUBCODEC_LIBDEFLATE unsupported in this build");
579
0
                    return 0;
580
0
                }
581
0
#endif
582
0
                return 1;
583
584
4.07k
  default:
585
4.07k
    return (*sp->vsetparent)(tif, tag, ap);
586
4.07k
  }
587
  /*NOTREACHED*/
588
4.07k
}
589
590
static int
591
ZIPVGetField(TIFF* tif, uint32 tag, va_list ap)
592
4.58M
{
593
4.58M
  ZIPState* sp = ZState(tif);
594
595
4.58M
  switch (tag) {
596
0
  case TIFFTAG_ZIPQUALITY:
597
0
    *va_arg(ap, int*) = sp->zipquality;
598
0
    break;
599
600
0
        case TIFFTAG_DEFLATE_SUBCODEC:
601
0
    *va_arg(ap, int*) = sp->subcodec;
602
0
    break;
603
604
4.58M
  default:
605
4.58M
    return (*sp->vgetparent)(tif, tag, ap);
606
4.58M
  }
607
0
  return (1);
608
4.58M
}
609
610
static const TIFFField zipFields[] = {
611
    { TIFFTAG_ZIPQUALITY, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE, "", NULL },
612
    { TIFFTAG_DEFLATE_SUBCODEC, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE, "", NULL },
613
};
614
615
int
616
TIFFInitZIP(TIFF* tif, int scheme)
617
307
{
618
307
  static const char module[] = "TIFFInitZIP";
619
307
  ZIPState* sp;
620
621
307
  assert( (scheme == COMPRESSION_DEFLATE)
622
307
    || (scheme == COMPRESSION_ADOBE_DEFLATE));
623
307
#ifdef NDEBUG
624
307
  (void)scheme;
625
307
#endif
626
627
  /*
628
   * Merge codec-specific tag information.
629
   */
630
307
  if (!_TIFFMergeFields(tif, zipFields, TIFFArrayCount(zipFields))) {
631
0
    TIFFErrorExt(tif->tif_clientdata, module,
632
0
           "Merging Deflate codec-specific tags failed");
633
0
    return 0;
634
0
  }
635
636
  /*
637
   * Allocate state block so tag methods have storage to record values.
638
   */
639
307
  tif->tif_data = (uint8*) _TIFFcalloc(sizeof (ZIPState), 1);
640
307
  if (tif->tif_data == NULL)
641
0
    goto bad;
642
307
  sp = ZState(tif);
643
307
  sp->stream.zalloc = NULL;
644
307
  sp->stream.zfree = NULL;
645
307
  sp->stream.opaque = NULL;
646
307
  sp->stream.data_type = Z_BINARY;
647
648
  /*
649
   * Override parent get/set field methods.
650
   */
651
307
  sp->vgetparent = tif->tif_tagmethods.vgetfield;
652
307
  tif->tif_tagmethods.vgetfield = ZIPVGetField; /* hook for codec tags */
653
307
  sp->vsetparent = tif->tif_tagmethods.vsetfield;
654
307
  tif->tif_tagmethods.vsetfield = ZIPVSetField; /* hook for codec tags */
655
656
  /* Default values for codec-specific fields */
657
307
  sp->zipquality = Z_DEFAULT_COMPRESSION; /* default comp. level */
658
307
  sp->state = 0;
659
#if LIBDEFLATE_SUPPORT
660
        sp->subcodec = DEFLATE_SUBCODEC_LIBDEFLATE;
661
#else
662
307
        sp->subcodec = DEFLATE_SUBCODEC_ZLIB;
663
307
#endif
664
665
  /*
666
   * Install codec methods.
667
   */
668
307
  tif->tif_fixuptags = ZIPFixupTags; 
669
307
  tif->tif_setupdecode = ZIPSetupDecode;
670
307
  tif->tif_predecode = ZIPPreDecode;
671
307
  tif->tif_decoderow = ZIPDecode;
672
307
  tif->tif_decodestrip = ZIPDecode;
673
307
  tif->tif_decodetile = ZIPDecode;  
674
307
  tif->tif_setupencode = ZIPSetupEncode;
675
307
  tif->tif_preencode = ZIPPreEncode;
676
307
  tif->tif_postencode = ZIPPostEncode;
677
307
  tif->tif_encoderow = ZIPEncode;
678
307
  tif->tif_encodestrip = ZIPEncode;
679
307
  tif->tif_encodetile = ZIPEncode;
680
307
  tif->tif_cleanup = ZIPCleanup;
681
  /*
682
   * Setup predictor setup.
683
   */
684
307
  (void) TIFFPredictorInit(tif);
685
307
  return (1);
686
0
bad:
687
0
  TIFFErrorExt(tif->tif_clientdata, module,
688
0
         "No space for ZIP state block");
689
0
  return (0);
690
307
}
691
#endif /* ZIP_SUPPORT */
692
693
/* vim: set ts=8 sts=8 sw=8 noet: */
694
/*
695
 * Local Variables:
696
 * mode: c
697
 * c-basic-offset: 8
698
 * fill-column: 78
699
 * End:
700
 */