1 /* $Id: tif_zip.c,v 1.33 2014-12-25 18:29:11 erouault Exp $ */
4 * Copyright (c) 1995-1997 Sam Leffler
5 * Copyright (c) 1995-1997 Silicon Graphics, Inc.
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
32 * ZIP (aka Deflate) Compression Support
34 * This file is simply an interface to the zlib library written by
35 * Jean-loup Gailly and Mark Adler. You must use version 1.0 or later
36 * of the library: this code assumes the 1.0 API and also depends on
37 * the ability to write the zlib header multiple times (one per strip)
38 * which was not possible with versions prior to 0.95. Note also that
39 * older versions of this codec avoided this bug by suppressing the header
40 * entirely. This means that files written with the old library cannot
41 * be read; they should be converted to a different compression scheme
42 * and then reconverted.
44 * The data format used by the zlib library is described in the files
45 * zlib-3.1.doc, deflate-1.1.doc and gzip-4.1.doc, available in the
46 * directory ftp://ftp.uu.net/pub/archiving/zip/doc. The library was
47 * last found at ftp://ftp.uu.net/pub/archiving/zip/zlib/zlib-0.99.tar.gz.
49 #include "tif_predict.h"
55 * Sigh, ZLIB_VERSION is defined as a string so there's no
56 * way to do a proper check here. Instead we guess based
57 * on the presence of #defines that were added between the
58 * 0.95 and 1.0 distributions.
60 #if !defined(Z_NO_COMPRESSION) || !defined(Z_DEFLATED)
61 #error "Antiquated ZLIB software; you must use version 1.0 or later"
64 #define SAFE_MSG(sp) ((sp)->stream.msg == NULL ? "" : (sp)->stream.msg)
67 * State block for each open TIFF
68 * file using ZIP compression/decompression.
71 TIFFPredictorState predict;
73 int zipquality; /* compression level */
74 int state; /* state flags */
75 #define ZSTATE_INIT_DECODE 0x01
76 #define ZSTATE_INIT_ENCODE 0x02
78 TIFFVGetMethod vgetparent; /* super-class method */
79 TIFFVSetMethod vsetparent; /* super-class method */
82 #define ZState(tif) ((ZIPState*) (tif)->tif_data)
83 #define DecoderState(tif) ZState(tif)
84 #define EncoderState(tif) ZState(tif)
86 static int ZIPEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s);
87 static int ZIPDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s);
90 ZIPFixupTags(TIFF* tif)
97 ZIPSetupDecode(TIFF* tif)
99 static const char module[] = "ZIPSetupDecode";
100 ZIPState* sp = DecoderState(tif);
104 /* if we were last encoding, terminate this mode */
105 if (sp->state & ZSTATE_INIT_ENCODE) {
106 deflateEnd(&sp->stream);
110 if (inflateInit(&sp->stream) != Z_OK) {
111 TIFFErrorExt(tif->tif_clientdata, module, "%s", SAFE_MSG(sp));
114 sp->state |= ZSTATE_INIT_DECODE;
120 * Setup state for decoding a strip.
123 ZIPPreDecode(TIFF* tif, uint16 s)
125 static const char module[] = "ZIPPreDecode";
126 ZIPState* sp = DecoderState(tif);
131 if( (sp->state & ZSTATE_INIT_DECODE) == 0 )
132 tif->tif_setupdecode( tif );
134 sp->stream.next_in = tif->tif_rawdata;
135 assert(sizeof(sp->stream.avail_in)==4); /* if this assert gets raised,
136 we need to simplify this code to reflect a ZLib that is likely updated
137 to deal with 8byte memory sizes, though this code will respond
138 apropriately even before we simplify it */
139 sp->stream.avail_in = (uInt) tif->tif_rawcc;
140 if ((tmsize_t)sp->stream.avail_in != tif->tif_rawcc)
142 TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
145 return (inflateReset(&sp->stream) == Z_OK);
149 ZIPDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
151 static const char module[] = "ZIPDecode";
152 ZIPState* sp = DecoderState(tif);
156 assert(sp->state == ZSTATE_INIT_DECODE);
158 sp->stream.next_in = tif->tif_rawcp;
159 sp->stream.avail_in = (uInt) tif->tif_rawcc;
161 sp->stream.next_out = op;
162 assert(sizeof(sp->stream.avail_out)==4); /* if this assert gets raised,
163 we need to simplify this code to reflect a ZLib that is likely updated
164 to deal with 8byte memory sizes, though this code will respond
165 apropriately even before we simplify it */
166 sp->stream.avail_out = (uInt) occ;
167 if ((tmsize_t)sp->stream.avail_out != occ)
169 TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
173 int state = inflate(&sp->stream, Z_PARTIAL_FLUSH);
174 if (state == Z_STREAM_END)
176 if (state == Z_DATA_ERROR) {
177 TIFFErrorExt(tif->tif_clientdata, module,
178 "Decoding error at scanline %lu, %s",
179 (unsigned long) tif->tif_row, SAFE_MSG(sp));
180 if (inflateSync(&sp->stream) != Z_OK)
185 TIFFErrorExt(tif->tif_clientdata, module,
186 "ZLib error: %s", SAFE_MSG(sp));
189 } while (sp->stream.avail_out > 0);
190 if (sp->stream.avail_out != 0) {
191 TIFFErrorExt(tif->tif_clientdata, module,
192 "Not enough data at scanline %lu (short " TIFF_UINT64_FORMAT " bytes)",
193 (unsigned long) tif->tif_row, (TIFF_UINT64_T) sp->stream.avail_out);
197 tif->tif_rawcp = sp->stream.next_in;
198 tif->tif_rawcc = sp->stream.avail_in;
204 ZIPSetupEncode(TIFF* tif)
206 static const char module[] = "ZIPSetupEncode";
207 ZIPState* sp = EncoderState(tif);
210 if (sp->state & ZSTATE_INIT_DECODE) {
211 inflateEnd(&sp->stream);
215 if (deflateInit(&sp->stream, sp->zipquality) != Z_OK) {
216 TIFFErrorExt(tif->tif_clientdata, module, "%s", SAFE_MSG(sp));
219 sp->state |= ZSTATE_INIT_ENCODE;
225 * Reset encoding state at the start of a strip.
228 ZIPPreEncode(TIFF* tif, uint16 s)
230 static const char module[] = "ZIPPreEncode";
231 ZIPState *sp = EncoderState(tif);
235 if( sp->state != ZSTATE_INIT_ENCODE )
236 tif->tif_setupencode( tif );
238 sp->stream.next_out = tif->tif_rawdata;
239 assert(sizeof(sp->stream.avail_out)==4); /* if this assert gets raised,
240 we need to simplify this code to reflect a ZLib that is likely updated
241 to deal with 8byte memory sizes, though this code will respond
242 apropriately even before we simplify it */
243 sp->stream.avail_out = tif->tif_rawdatasize;
244 if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize)
246 TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
249 return (deflateReset(&sp->stream) == Z_OK);
253 * Encode a chunk of pixels.
256 ZIPEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
258 static const char module[] = "ZIPEncode";
259 ZIPState *sp = EncoderState(tif);
262 assert(sp->state == ZSTATE_INIT_ENCODE);
265 sp->stream.next_in = bp;
266 assert(sizeof(sp->stream.avail_in)==4); /* if this assert gets raised,
267 we need to simplify this code to reflect a ZLib that is likely updated
268 to deal with 8byte memory sizes, though this code will respond
269 apropriately even before we simplify it */
270 sp->stream.avail_in = (uInt) cc;
271 if ((tmsize_t)sp->stream.avail_in != cc)
273 TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
277 if (deflate(&sp->stream, Z_NO_FLUSH) != Z_OK) {
278 TIFFErrorExt(tif->tif_clientdata, module,
283 if (sp->stream.avail_out == 0) {
284 tif->tif_rawcc = tif->tif_rawdatasize;
286 sp->stream.next_out = tif->tif_rawdata;
287 sp->stream.avail_out = (uInt) tif->tif_rawdatasize; /* this is a safe typecast, as check is made already in ZIPPreEncode */
289 } while (sp->stream.avail_in > 0);
294 * Finish off an encoded strip by flushing the last
295 * string and tacking on an End Of Information code.
298 ZIPPostEncode(TIFF* tif)
300 static const char module[] = "ZIPPostEncode";
301 ZIPState *sp = EncoderState(tif);
304 sp->stream.avail_in = 0;
306 state = deflate(&sp->stream, Z_FINISH);
310 if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize)
312 tif->tif_rawcc = tif->tif_rawdatasize - sp->stream.avail_out;
314 sp->stream.next_out = tif->tif_rawdata;
315 sp->stream.avail_out = (uInt) tif->tif_rawdatasize; /* this is a safe typecast, as check is made already in ZIPPreEncode */
319 TIFFErrorExt(tif->tif_clientdata, module,
320 "ZLib error: %s", SAFE_MSG(sp));
323 } while (state != Z_STREAM_END);
328 ZIPCleanup(TIFF* tif)
330 ZIPState* sp = ZState(tif);
334 (void)TIFFPredictorCleanup(tif);
336 tif->tif_tagmethods.vgetfield = sp->vgetparent;
337 tif->tif_tagmethods.vsetfield = sp->vsetparent;
339 if (sp->state & ZSTATE_INIT_ENCODE) {
340 deflateEnd(&sp->stream);
342 } else if( sp->state & ZSTATE_INIT_DECODE) {
343 inflateEnd(&sp->stream);
347 tif->tif_data = NULL;
349 _TIFFSetDefaultCompressionState(tif);
353 ZIPVSetField(TIFF* tif, uint32 tag, va_list ap)
355 static const char module[] = "ZIPVSetField";
356 ZIPState* sp = ZState(tif);
359 case TIFFTAG_ZIPQUALITY:
360 sp->zipquality = (int) va_arg(ap, int);
361 if ( sp->state&ZSTATE_INIT_ENCODE ) {
362 if (deflateParams(&sp->stream,
363 sp->zipquality, Z_DEFAULT_STRATEGY) != Z_OK) {
364 TIFFErrorExt(tif->tif_clientdata, module, "ZLib error: %s",
371 return (*sp->vsetparent)(tif, tag, ap);
377 ZIPVGetField(TIFF* tif, uint32 tag, va_list ap)
379 ZIPState* sp = ZState(tif);
382 case TIFFTAG_ZIPQUALITY:
383 *va_arg(ap, int*) = sp->zipquality;
386 return (*sp->vgetparent)(tif, tag, ap);
391 static const TIFFField zipFields[] = {
392 { TIFFTAG_ZIPQUALITY, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE, "", NULL },
396 TIFFInitZIP(TIFF* tif, int scheme)
398 static const char module[] = "TIFFInitZIP";
401 assert( (scheme == COMPRESSION_DEFLATE)
402 || (scheme == COMPRESSION_ADOBE_DEFLATE));
405 * Merge codec-specific tag information.
407 if (!_TIFFMergeFields(tif, zipFields, TIFFArrayCount(zipFields))) {
408 TIFFErrorExt(tif->tif_clientdata, module,
409 "Merging Deflate codec-specific tags failed");
414 * Allocate state block so tag methods have storage to record values.
416 tif->tif_data = (uint8*) _TIFFmalloc(sizeof (ZIPState));
417 if (tif->tif_data == NULL)
420 sp->stream.zalloc = NULL;
421 sp->stream.zfree = NULL;
422 sp->stream.opaque = NULL;
423 sp->stream.data_type = Z_BINARY;
426 * Override parent get/set field methods.
428 sp->vgetparent = tif->tif_tagmethods.vgetfield;
429 tif->tif_tagmethods.vgetfield = ZIPVGetField; /* hook for codec tags */
430 sp->vsetparent = tif->tif_tagmethods.vsetfield;
431 tif->tif_tagmethods.vsetfield = ZIPVSetField; /* hook for codec tags */
433 /* Default values for codec-specific fields */
434 sp->zipquality = Z_DEFAULT_COMPRESSION; /* default comp. level */
438 * Install codec methods.
440 tif->tif_fixuptags = ZIPFixupTags;
441 tif->tif_setupdecode = ZIPSetupDecode;
442 tif->tif_predecode = ZIPPreDecode;
443 tif->tif_decoderow = ZIPDecode;
444 tif->tif_decodestrip = ZIPDecode;
445 tif->tif_decodetile = ZIPDecode;
446 tif->tif_setupencode = ZIPSetupEncode;
447 tif->tif_preencode = ZIPPreEncode;
448 tif->tif_postencode = ZIPPostEncode;
449 tif->tif_encoderow = ZIPEncode;
450 tif->tif_encodestrip = ZIPEncode;
451 tif->tif_encodetile = ZIPEncode;
452 tif->tif_cleanup = ZIPCleanup;
454 * Setup predictor setup.
456 (void) TIFFPredictorInit(tif);
459 TIFFErrorExt(tif->tif_clientdata, module,
460 "No space for ZIP state block");
463 #endif /* ZIP_SUPORT */
465 /* vim: set ts=8 sts=8 sw=8 noet: */