Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/tiff/libtiff/tif_zip.c
8823 views
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
#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
#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
{
68
TIFFPredictorState predict;
69
z_stream stream;
70
int read_error; /* whether a read error has occurred, and which should cause
71
further reads in the same strip/tile to be aborted */
72
int zipquality; /* compression level */
73
int state; /* state flags */
74
int subcodec; /* DEFLATE_SUBCODEC_ZLIB or DEFLATE_SUBCODEC_LIBDEFLATE */
75
#if LIBDEFLATE_SUPPORT
76
int libdeflate_state; /* -1 = until first time ZIPEncode() / ZIPDecode() is
77
called, 0 = use zlib, 1 = use libdeflate */
78
struct libdeflate_decompressor *libdeflate_dec;
79
struct libdeflate_compressor *libdeflate_enc;
80
#endif
81
#define ZSTATE_INIT_DECODE 0x01
82
#define ZSTATE_INIT_ENCODE 0x02
83
84
TIFFVGetMethod vgetparent; /* super-class method */
85
TIFFVSetMethod vsetparent; /* super-class method */
86
} ZIPState;
87
88
#define GetZIPState(tif) ((ZIPState *)(tif)->tif_data)
89
#define ZIPDecoderState(tif) GetZIPState(tif)
90
#define ZIPEncoderState(tif) GetZIPState(tif)
91
92
static int ZIPEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s);
93
static int ZIPDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s);
94
95
static int ZIPFixupTags(TIFF *tif)
96
{
97
(void)tif;
98
return (1);
99
}
100
101
static int ZIPSetupDecode(TIFF *tif)
102
{
103
static const char module[] = "ZIPSetupDecode";
104
ZIPState *sp = ZIPDecoderState(tif);
105
106
assert(sp != NULL);
107
108
/* if we were last encoding, terminate this mode */
109
if (sp->state & ZSTATE_INIT_ENCODE)
110
{
111
deflateEnd(&sp->stream);
112
sp->state = 0;
113
}
114
115
/* This function can possibly be called several times by */
116
/* PredictorSetupDecode() if this function succeeds but */
117
/* PredictorSetup() fails */
118
if ((sp->state & ZSTATE_INIT_DECODE) == 0 &&
119
inflateInit(&sp->stream) != Z_OK)
120
{
121
TIFFErrorExtR(tif, module, "%s", SAFE_MSG(sp));
122
return (0);
123
}
124
else
125
{
126
sp->state |= ZSTATE_INIT_DECODE;
127
return (1);
128
}
129
}
130
131
static inline uint64_t TIFF_MIN_UINT64(uint64_t a, uint64_t b)
132
{
133
return a < b ? a : b;
134
}
135
136
static inline uInt TIFF_CLAMP_UINT64_TO_INT32_MAX(uint64_t v)
137
{
138
return (uInt)TIFF_MIN_UINT64(v, INT32_MAX);
139
}
140
141
/*
142
* Setup state for decoding a strip.
143
*/
144
static int ZIPPreDecode(TIFF *tif, uint16_t s)
145
{
146
ZIPState *sp = ZIPDecoderState(tif);
147
148
(void)s;
149
assert(sp != NULL);
150
151
if ((sp->state & ZSTATE_INIT_DECODE) == 0)
152
tif->tif_setupdecode(tif);
153
154
#if LIBDEFLATE_SUPPORT
155
sp->libdeflate_state = -1;
156
#endif
157
sp->stream.next_in = tif->tif_rawdata;
158
assert(sizeof(sp->stream.avail_in) == 4); /* if this assert gets raised,
159
we need to simplify this code to reflect a ZLib that is likely updated
160
to deal with 8byte memory sizes, though this code will respond
161
appropriately even before we simplify it */
162
sp->stream.avail_in = TIFF_CLAMP_UINT64_TO_INT32_MAX(tif->tif_rawcc);
163
if (inflateReset(&sp->stream) == Z_OK)
164
{
165
sp->read_error = 0;
166
return 1;
167
}
168
return 0;
169
}
170
171
static int ZIPDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s)
172
{
173
static const char module[] = "ZIPDecode";
174
ZIPState *sp = ZIPDecoderState(tif);
175
176
(void)s;
177
assert(sp != NULL);
178
assert(sp->state == ZSTATE_INIT_DECODE);
179
180
if (sp->read_error)
181
{
182
memset(op, 0, (size_t)occ);
183
TIFFErrorExtR(tif, module,
184
"ZIPDecode: Scanline %" PRIu32 " cannot be read due to "
185
"previous error",
186
tif->tif_row);
187
return 0;
188
}
189
190
#if LIBDEFLATE_SUPPORT
191
if (sp->libdeflate_state == 1)
192
return 0;
193
194
/* If we have libdeflate support and we are asked to read a whole */
195
/* strip/tile, then go for using it */
196
do
197
{
198
TIFFDirectory *td = &tif->tif_dir;
199
200
if (sp->libdeflate_state == 0)
201
break;
202
if (sp->subcodec == DEFLATE_SUBCODEC_ZLIB)
203
break;
204
205
/* Check if we are in the situation where we can use libdeflate */
206
if (isTiled(tif))
207
{
208
if (TIFFTileSize64(tif) != (uint64_t)occ)
209
break;
210
}
211
else
212
{
213
uint32_t strip_height = td->td_imagelength - tif->tif_row;
214
if (strip_height > td->td_rowsperstrip)
215
strip_height = td->td_rowsperstrip;
216
if (TIFFVStripSize64(tif, strip_height) != (uint64_t)occ)
217
break;
218
}
219
220
/* Check for overflow */
221
if ((size_t)tif->tif_rawcc != (uint64_t)tif->tif_rawcc)
222
break;
223
if ((size_t)occ != (uint64_t)occ)
224
break;
225
226
/* Go for decompression using libdeflate */
227
{
228
enum libdeflate_result res;
229
if (sp->libdeflate_dec == NULL)
230
{
231
sp->libdeflate_dec = libdeflate_alloc_decompressor();
232
if (sp->libdeflate_dec == NULL)
233
{
234
break;
235
}
236
}
237
238
sp->libdeflate_state = 1;
239
240
res = libdeflate_zlib_decompress(sp->libdeflate_dec, tif->tif_rawcp,
241
(size_t)tif->tif_rawcc, op,
242
(size_t)occ, NULL);
243
244
tif->tif_rawcp += tif->tif_rawcc;
245
tif->tif_rawcc = 0;
246
247
/* We accept LIBDEFLATE_INSUFFICIENT_SPACE has a return */
248
/* There are odd files in the wild where the last strip, when */
249
/* it is smaller in height than td_rowsperstrip, actually contains
250
*/
251
/* data for td_rowsperstrip lines. Just ignore that silently. */
252
if (res != LIBDEFLATE_SUCCESS &&
253
res != LIBDEFLATE_INSUFFICIENT_SPACE)
254
{
255
memset(op, 0, (size_t)occ);
256
TIFFErrorExtR(tif, module, "Decoding error at scanline %lu",
257
(unsigned long)tif->tif_row);
258
sp->read_error = 1;
259
return 0;
260
}
261
262
return 1;
263
}
264
} while (0);
265
sp->libdeflate_state = 0;
266
#endif /* LIBDEFLATE_SUPPORT */
267
268
sp->stream.next_in = tif->tif_rawcp;
269
270
sp->stream.next_out = op;
271
assert(sizeof(sp->stream.avail_out) == 4); /* if this assert gets raised,
272
we need to simplify this code to reflect a ZLib that is likely updated
273
to deal with 8byte memory sizes, though this code will respond
274
appropriately even before we simplify it */
275
do
276
{
277
int state;
278
uInt avail_in_before = TIFF_CLAMP_UINT64_TO_INT32_MAX(tif->tif_rawcc);
279
uInt avail_out_before = TIFF_CLAMP_UINT64_TO_INT32_MAX(occ);
280
sp->stream.avail_in = avail_in_before;
281
sp->stream.avail_out = avail_out_before;
282
state = inflate(&sp->stream, Z_PARTIAL_FLUSH);
283
tif->tif_rawcc -= (avail_in_before - sp->stream.avail_in);
284
occ -= (avail_out_before - sp->stream.avail_out);
285
if (state == Z_STREAM_END)
286
break;
287
if (state == Z_DATA_ERROR)
288
{
289
memset(sp->stream.next_out, 0, (size_t)occ);
290
TIFFErrorExtR(tif, module, "Decoding error at scanline %lu, %s",
291
(unsigned long)tif->tif_row, SAFE_MSG(sp));
292
sp->read_error = 1;
293
return (0);
294
}
295
if (state != Z_OK)
296
{
297
memset(sp->stream.next_out, 0, (size_t)occ);
298
TIFFErrorExtR(tif, module, "ZLib error: %s", SAFE_MSG(sp));
299
sp->read_error = 1;
300
return (0);
301
}
302
} while (occ > 0);
303
if (occ != 0)
304
{
305
TIFFErrorExtR(tif, module,
306
"Not enough data at scanline %lu (short %" PRIu64
307
" bytes)",
308
(unsigned long)tif->tif_row, (uint64_t)occ);
309
memset(sp->stream.next_out, 0, (size_t)occ);
310
sp->read_error = 1;
311
return (0);
312
}
313
314
tif->tif_rawcp = sp->stream.next_in;
315
316
return (1);
317
}
318
319
static int ZIPSetupEncode(TIFF *tif)
320
{
321
static const char module[] = "ZIPSetupEncode";
322
ZIPState *sp = ZIPEncoderState(tif);
323
int cappedQuality;
324
325
assert(sp != NULL);
326
if (sp->state & ZSTATE_INIT_DECODE)
327
{
328
inflateEnd(&sp->stream);
329
sp->state = 0;
330
}
331
332
cappedQuality = sp->zipquality;
333
if (cappedQuality > Z_BEST_COMPRESSION)
334
cappedQuality = Z_BEST_COMPRESSION;
335
336
if (deflateInit(&sp->stream, cappedQuality) != Z_OK)
337
{
338
TIFFErrorExtR(tif, module, "%s", SAFE_MSG(sp));
339
return (0);
340
}
341
else
342
{
343
sp->state |= ZSTATE_INIT_ENCODE;
344
return (1);
345
}
346
}
347
348
/*
349
* Reset encoding state at the start of a strip.
350
*/
351
static int ZIPPreEncode(TIFF *tif, uint16_t s)
352
{
353
ZIPState *sp = ZIPEncoderState(tif);
354
355
(void)s;
356
assert(sp != NULL);
357
if (sp->state != ZSTATE_INIT_ENCODE)
358
tif->tif_setupencode(tif);
359
360
#if LIBDEFLATE_SUPPORT
361
sp->libdeflate_state = -1;
362
#endif
363
sp->stream.next_out = tif->tif_rawdata;
364
assert(sizeof(sp->stream.avail_out) == 4); /* if this assert gets raised,
365
we need to simplify this code to reflect a ZLib that is likely updated
366
to deal with 8byte memory sizes, though this code will respond
367
appropriately even before we simplify it */
368
sp->stream.avail_out = (uint64_t)tif->tif_rawdatasize <= 0xFFFFFFFFU
369
? (uInt)tif->tif_rawdatasize
370
: 0xFFFFFFFFU;
371
return (deflateReset(&sp->stream) == Z_OK);
372
}
373
374
/*
375
* Encode a chunk of pixels.
376
*/
377
static int ZIPEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
378
{
379
static const char module[] = "ZIPEncode";
380
ZIPState *sp = ZIPEncoderState(tif);
381
382
assert(sp != NULL);
383
assert(sp->state == ZSTATE_INIT_ENCODE);
384
385
(void)s;
386
387
#if LIBDEFLATE_SUPPORT
388
if (sp->libdeflate_state == 1)
389
return 0;
390
391
/* If we have libdeflate support and we are asked to write a whole */
392
/* strip/tile, then go for using it */
393
do
394
{
395
TIFFDirectory *td = &tif->tif_dir;
396
397
if (sp->libdeflate_state == 0)
398
break;
399
if (sp->subcodec == DEFLATE_SUBCODEC_ZLIB)
400
break;
401
402
/* Libdeflate does not support the 0-compression level */
403
if (sp->zipquality == Z_NO_COMPRESSION)
404
break;
405
406
/* Check if we are in the situation where we can use libdeflate */
407
if (isTiled(tif))
408
{
409
if (TIFFTileSize64(tif) != (uint64_t)cc)
410
break;
411
}
412
else
413
{
414
uint32_t strip_height = td->td_imagelength - tif->tif_row;
415
if (strip_height > td->td_rowsperstrip)
416
strip_height = td->td_rowsperstrip;
417
if (TIFFVStripSize64(tif, strip_height) != (uint64_t)cc)
418
break;
419
}
420
421
/* Check for overflow */
422
if ((size_t)tif->tif_rawdatasize != (uint64_t)tif->tif_rawdatasize)
423
break;
424
if ((size_t)cc != (uint64_t)cc)
425
break;
426
427
/* Go for compression using libdeflate */
428
{
429
size_t nCompressedBytes;
430
if (sp->libdeflate_enc == NULL)
431
{
432
/* To get results as good as zlib, we asked for an extra */
433
/* level of compression */
434
sp->libdeflate_enc = libdeflate_alloc_compressor(
435
sp->zipquality == Z_DEFAULT_COMPRESSION ? 7
436
: sp->zipquality >= 6 && sp->zipquality <= 9
437
? sp->zipquality + 1
438
: sp->zipquality);
439
if (sp->libdeflate_enc == NULL)
440
{
441
TIFFErrorExtR(tif, module, "Cannot allocate compressor");
442
break;
443
}
444
}
445
446
/* Make sure the output buffer is large enough for the worse case.
447
*/
448
/* In TIFFWriteBufferSetup(), when libtiff allocates the buffer */
449
/* we've taken a 10% margin over the uncompressed size, which should
450
*/
451
/* be large enough even for the the worse case scenario. */
452
if (libdeflate_zlib_compress_bound(sp->libdeflate_enc, (size_t)cc) >
453
(size_t)tif->tif_rawdatasize)
454
{
455
break;
456
}
457
458
sp->libdeflate_state = 1;
459
nCompressedBytes = libdeflate_zlib_compress(
460
sp->libdeflate_enc, bp, (size_t)cc, tif->tif_rawdata,
461
(size_t)tif->tif_rawdatasize);
462
463
if (nCompressedBytes == 0)
464
{
465
TIFFErrorExtR(tif, module, "Encoder error at scanline %lu",
466
(unsigned long)tif->tif_row);
467
return 0;
468
}
469
470
tif->tif_rawcc = nCompressedBytes;
471
472
if (!TIFFFlushData1(tif))
473
return 0;
474
475
return 1;
476
}
477
} while (0);
478
sp->libdeflate_state = 0;
479
#endif /* LIBDEFLATE_SUPPORT */
480
481
sp->stream.next_in = bp;
482
assert(sizeof(sp->stream.avail_in) == 4); /* if this assert gets raised,
483
we need to simplify this code to reflect a ZLib that is likely updated
484
to deal with 8byte memory sizes, though this code will respond
485
appropriately even before we simplify it */
486
do
487
{
488
uInt avail_in_before = TIFF_CLAMP_UINT64_TO_INT32_MAX(cc);
489
sp->stream.avail_in = avail_in_before;
490
if (deflate(&sp->stream, Z_NO_FLUSH) != Z_OK)
491
{
492
TIFFErrorExtR(tif, module, "Encoder error: %s", SAFE_MSG(sp));
493
return (0);
494
}
495
if (sp->stream.avail_out == 0)
496
{
497
tif->tif_rawcc = tif->tif_rawdatasize;
498
if (!TIFFFlushData1(tif))
499
return 0;
500
sp->stream.next_out = tif->tif_rawdata;
501
sp->stream.avail_out =
502
TIFF_CLAMP_UINT64_TO_INT32_MAX(tif->tif_rawdatasize);
503
}
504
cc -= (avail_in_before - sp->stream.avail_in);
505
} while (cc > 0);
506
return (1);
507
}
508
509
/*
510
* Finish off an encoded strip by flushing the last
511
* string and tacking on an End Of Information code.
512
*/
513
static int ZIPPostEncode(TIFF *tif)
514
{
515
static const char module[] = "ZIPPostEncode";
516
ZIPState *sp = ZIPEncoderState(tif);
517
int state;
518
519
#if LIBDEFLATE_SUPPORT
520
if (sp->libdeflate_state == 1)
521
return 1;
522
#endif
523
524
sp->stream.avail_in = 0;
525
do
526
{
527
state = deflate(&sp->stream, Z_FINISH);
528
switch (state)
529
{
530
case Z_STREAM_END:
531
case Z_OK:
532
if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize)
533
{
534
tif->tif_rawcc =
535
tif->tif_rawdatasize - sp->stream.avail_out;
536
if (!TIFFFlushData1(tif))
537
return 0;
538
sp->stream.next_out = tif->tif_rawdata;
539
sp->stream.avail_out =
540
(uint64_t)tif->tif_rawdatasize <= 0xFFFFFFFFU
541
? (uInt)tif->tif_rawdatasize
542
: 0xFFFFFFFFU;
543
}
544
break;
545
default:
546
TIFFErrorExtR(tif, module, "ZLib error: %s", SAFE_MSG(sp));
547
return (0);
548
}
549
} while (state != Z_STREAM_END);
550
return (1);
551
}
552
553
static void ZIPCleanup(TIFF *tif)
554
{
555
ZIPState *sp = GetZIPState(tif);
556
557
assert(sp != 0);
558
559
(void)TIFFPredictorCleanup(tif);
560
561
tif->tif_tagmethods.vgetfield = sp->vgetparent;
562
tif->tif_tagmethods.vsetfield = sp->vsetparent;
563
564
if (sp->state & ZSTATE_INIT_ENCODE)
565
{
566
deflateEnd(&sp->stream);
567
sp->state = 0;
568
}
569
else if (sp->state & ZSTATE_INIT_DECODE)
570
{
571
inflateEnd(&sp->stream);
572
sp->state = 0;
573
}
574
575
#if LIBDEFLATE_SUPPORT
576
if (sp->libdeflate_dec)
577
libdeflate_free_decompressor(sp->libdeflate_dec);
578
if (sp->libdeflate_enc)
579
libdeflate_free_compressor(sp->libdeflate_enc);
580
#endif
581
582
_TIFFfreeExt(tif, sp);
583
tif->tif_data = NULL;
584
585
_TIFFSetDefaultCompressionState(tif);
586
}
587
588
static int ZIPVSetField(TIFF *tif, uint32_t tag, va_list ap)
589
{
590
static const char module[] = "ZIPVSetField";
591
ZIPState *sp = GetZIPState(tif);
592
593
switch (tag)
594
{
595
case TIFFTAG_ZIPQUALITY:
596
sp->zipquality = (int)va_arg(ap, int);
597
if (sp->zipquality < Z_DEFAULT_COMPRESSION ||
598
sp->zipquality > LIBDEFLATE_MAX_COMPRESSION_LEVEL)
599
{
600
TIFFErrorExtR(
601
tif, module,
602
"Invalid ZipQuality value. Should be in [-1,%d] range",
603
LIBDEFLATE_MAX_COMPRESSION_LEVEL);
604
return 0;
605
}
606
607
if (sp->state & ZSTATE_INIT_ENCODE)
608
{
609
int cappedQuality = sp->zipquality;
610
if (cappedQuality > Z_BEST_COMPRESSION)
611
cappedQuality = Z_BEST_COMPRESSION;
612
if (deflateParams(&sp->stream, cappedQuality,
613
Z_DEFAULT_STRATEGY) != Z_OK)
614
{
615
TIFFErrorExtR(tif, module, "ZLib error: %s", SAFE_MSG(sp));
616
return (0);
617
}
618
}
619
620
#if LIBDEFLATE_SUPPORT
621
if (sp->libdeflate_enc)
622
{
623
libdeflate_free_compressor(sp->libdeflate_enc);
624
sp->libdeflate_enc = NULL;
625
}
626
#endif
627
628
return (1);
629
630
case TIFFTAG_DEFLATE_SUBCODEC:
631
sp->subcodec = (int)va_arg(ap, int);
632
if (sp->subcodec != DEFLATE_SUBCODEC_ZLIB &&
633
sp->subcodec != DEFLATE_SUBCODEC_LIBDEFLATE)
634
{
635
TIFFErrorExtR(tif, module, "Invalid DeflateCodec value.");
636
return 0;
637
}
638
#if !LIBDEFLATE_SUPPORT
639
if (sp->subcodec == DEFLATE_SUBCODEC_LIBDEFLATE)
640
{
641
TIFFErrorExtR(tif, module,
642
"DeflateCodec = DEFLATE_SUBCODEC_LIBDEFLATE "
643
"unsupported in this build");
644
return 0;
645
}
646
#endif
647
return 1;
648
649
default:
650
return (*sp->vsetparent)(tif, tag, ap);
651
}
652
/*NOTREACHED*/
653
}
654
655
static int ZIPVGetField(TIFF *tif, uint32_t tag, va_list ap)
656
{
657
ZIPState *sp = GetZIPState(tif);
658
659
switch (tag)
660
{
661
case TIFFTAG_ZIPQUALITY:
662
*va_arg(ap, int *) = sp->zipquality;
663
break;
664
665
case TIFFTAG_DEFLATE_SUBCODEC:
666
*va_arg(ap, int *) = sp->subcodec;
667
break;
668
669
default:
670
return (*sp->vgetparent)(tif, tag, ap);
671
}
672
return (1);
673
}
674
675
static const TIFFField zipFields[] = {
676
{TIFFTAG_ZIPQUALITY, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, FIELD_PSEUDO, TRUE,
677
FALSE, "", NULL},
678
{TIFFTAG_DEFLATE_SUBCODEC, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, FIELD_PSEUDO,
679
TRUE, FALSE, "", NULL},
680
};
681
682
static void *TIFF_zalloc(void *opaque, unsigned int items, unsigned int size)
683
{
684
static const char module[] = "TIFF_zalloc";
685
TIFF *tif = opaque;
686
687
if (items > ~(size_t)0 / size)
688
{
689
TIFFErrorExtR(tif, module, "Overflow");
690
return NULL;
691
}
692
693
return _TIFFmallocExt(tif, items * size);
694
}
695
696
static void TIFF_zfree(void *opaque, void *ptr)
697
{
698
_TIFFfreeExt((TIFF *)opaque, ptr);
699
}
700
701
int TIFFInitZIP(TIFF *tif, int scheme)
702
{
703
static const char module[] = "TIFFInitZIP";
704
ZIPState *sp;
705
706
assert((scheme == COMPRESSION_DEFLATE) ||
707
(scheme == COMPRESSION_ADOBE_DEFLATE));
708
#ifdef NDEBUG
709
(void)scheme;
710
#endif
711
712
/*
713
* Merge codec-specific tag information.
714
*/
715
if (!_TIFFMergeFields(tif, zipFields, TIFFArrayCount(zipFields)))
716
{
717
TIFFErrorExtR(tif, module,
718
"Merging Deflate codec-specific tags failed");
719
return 0;
720
}
721
722
/*
723
* Allocate state block so tag methods have storage to record values.
724
*/
725
tif->tif_data = (uint8_t *)_TIFFcallocExt(tif, sizeof(ZIPState), 1);
726
if (tif->tif_data == NULL)
727
goto bad;
728
sp = GetZIPState(tif);
729
sp->stream.zalloc = TIFF_zalloc;
730
sp->stream.zfree = TIFF_zfree;
731
sp->stream.opaque = tif;
732
sp->stream.data_type = Z_BINARY;
733
734
/*
735
* Override parent get/set field methods.
736
*/
737
sp->vgetparent = tif->tif_tagmethods.vgetfield;
738
tif->tif_tagmethods.vgetfield = ZIPVGetField; /* hook for codec tags */
739
sp->vsetparent = tif->tif_tagmethods.vsetfield;
740
tif->tif_tagmethods.vsetfield = ZIPVSetField; /* hook for codec tags */
741
742
/* Default values for codec-specific fields */
743
sp->zipquality = Z_DEFAULT_COMPRESSION; /* default comp. level */
744
sp->state = 0;
745
#if LIBDEFLATE_SUPPORT
746
sp->subcodec = DEFLATE_SUBCODEC_LIBDEFLATE;
747
#else
748
sp->subcodec = DEFLATE_SUBCODEC_ZLIB;
749
#endif
750
751
/*
752
* Install codec methods.
753
*/
754
tif->tif_fixuptags = ZIPFixupTags;
755
tif->tif_setupdecode = ZIPSetupDecode;
756
tif->tif_predecode = ZIPPreDecode;
757
tif->tif_decoderow = ZIPDecode;
758
tif->tif_decodestrip = ZIPDecode;
759
tif->tif_decodetile = ZIPDecode;
760
tif->tif_setupencode = ZIPSetupEncode;
761
tif->tif_preencode = ZIPPreEncode;
762
tif->tif_postencode = ZIPPostEncode;
763
tif->tif_encoderow = ZIPEncode;
764
tif->tif_encodestrip = ZIPEncode;
765
tif->tif_encodetile = ZIPEncode;
766
tif->tif_cleanup = ZIPCleanup;
767
/*
768
* Setup predictor setup.
769
*/
770
(void)TIFFPredictorInit(tif);
771
return (1);
772
bad:
773
TIFFErrorExtR(tif, module, "No space for ZIP state block");
774
return (0);
775
}
776
#endif /* ZIP_SUPPORT */
777
778