]> git.saurik.com Git - wxWidgets.git/blame - src/tiff/libtiff/tif_zip.c
Reflect changes in stc.cpp in stc.cpp.in from which it's generated.
[wxWidgets.git] / src / tiff / libtiff / tif_zip.c
CommitLineData
8414a40c
VZ
1
2/*
3 * Copyright (c) 1995-1997 Sam Leffler
4 * Copyright (c) 1995-1997 Silicon Graphics, Inc.
5 *
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that (i) the above copyright notices and this permission notice appear in
9 * all copies of the software and related documentation, and (ii) the names of
10 * Sam Leffler and Silicon Graphics may not be used in any advertising or
11 * publicity relating to the software without the specific, prior written
12 * permission of Sam Leffler and Silicon Graphics.
13 *
14 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
16 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
17 *
18 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
19 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
20 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
21 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
22 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
23 * OF THIS SOFTWARE.
24 */
25
26#include "tiffiop.h"
27#ifdef ZIP_SUPPORT
28/*
29 * TIFF Library.
30 *
31 * ZIP (aka Deflate) Compression Support
32 *
33 * This file is simply an interface to the zlib library written by
34 * Jean-loup Gailly and Mark Adler. You must use version 1.0 or later
35 * of the library: this code assumes the 1.0 API and also depends on
36 * the ability to write the zlib header multiple times (one per strip)
37 * which was not possible with versions prior to 0.95. Note also that
38 * older versions of this codec avoided this bug by supressing the header
39 * entirely. This means that files written with the old library cannot
40 * be read; they should be converted to a different compression scheme
41 * and then reconverted.
42 *
43 * The data format used by the zlib library is described in the files
44 * zlib-3.1.doc, deflate-1.1.doc and gzip-4.1.doc, available in the
45 * directory ftp://ftp.uu.net/pub/archiving/zip/doc. The library was
46 * last found at ftp://ftp.uu.net/pub/archiving/zip/zlib/zlib-0.99.tar.gz.
47 */
48#include "tif_predict.h"
49#include "zlib.h"
50
51#include <stdio.h>
52
53/*
54 * Sigh, ZLIB_VERSION is defined as a string so there's no
55 * way to do a proper check here. Instead we guess based
56 * on the presence of #defines that were added between the
57 * 0.95 and 1.0 distributions.
58 */
59#if !defined(Z_NO_COMPRESSION) || !defined(Z_DEFLATED)
60#error "Antiquated ZLIB software; you must use version 1.0 or later"
61#endif
62
63/*
64 * State block for each open TIFF
65 * file using ZIP compression/decompression.
66 */
80ed523f 67typedef struct {
8414a40c 68 TIFFPredictorState predict;
80ed523f
VZ
69 z_stream stream;
70 int zipquality; /* compression level */
71 int state; /* state flags */
72#define ZSTATE_INIT_DECODE 0x01
73#define ZSTATE_INIT_ENCODE 0x02
74
75 TIFFVGetMethod vgetparent; /* super-class method */
76 TIFFVSetMethod vsetparent; /* super-class method */
8414a40c
VZ
77} ZIPState;
78
80ed523f
VZ
79#define ZState(tif) ((ZIPState*) (tif)->tif_data)
80#define DecoderState(tif) ZState(tif)
81#define EncoderState(tif) ZState(tif)
82
83static int ZIPEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s);
84static int ZIPDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s);
8414a40c 85
80ed523f
VZ
86static int
87ZIPFixupTags(TIFF* tif)
88{
89 (void) tif;
90 return (1);
91}
8414a40c
VZ
92
93static int
94ZIPSetupDecode(TIFF* tif)
95{
8414a40c 96 static const char module[] = "ZIPSetupDecode";
80ed523f 97 ZIPState* sp = DecoderState(tif);
8414a40c
VZ
98
99 assert(sp != NULL);
80ed523f
VZ
100
101 /* if we were last encoding, terminate this mode */
102 if (sp->state & ZSTATE_INIT_ENCODE) {
103 deflateEnd(&sp->stream);
104 sp->state = 0;
105 }
106
8414a40c 107 if (inflateInit(&sp->stream) != Z_OK) {
80ed523f 108 TIFFErrorExt(tif->tif_clientdata, module, "%s", sp->stream.msg);
8414a40c
VZ
109 return (0);
110 } else {
80ed523f 111 sp->state |= ZSTATE_INIT_DECODE;
8414a40c
VZ
112 return (1);
113 }
114}
115
116/*
117 * Setup state for decoding a strip.
118 */
119static int
80ed523f 120ZIPPreDecode(TIFF* tif, uint16 s)
8414a40c 121{
80ed523f 122 static const char module[] = "ZIPPreDecode";
8414a40c
VZ
123 ZIPState* sp = DecoderState(tif);
124
125 (void) s;
126 assert(sp != NULL);
80ed523f
VZ
127
128 if( (sp->state & ZSTATE_INIT_DECODE) == 0 )
129 tif->tif_setupdecode( tif );
130
8414a40c 131 sp->stream.next_in = tif->tif_rawdata;
80ed523f
VZ
132 assert(sizeof(sp->stream.avail_in)==4); /* if this assert gets raised,
133 we need to simplify this code to reflect a ZLib that is likely updated
134 to deal with 8byte memory sizes, though this code will respond
135 apropriately even before we simplify it */
136 sp->stream.avail_in = (uInt) tif->tif_rawcc;
137 if ((tmsize_t)sp->stream.avail_in != tif->tif_rawcc)
138 {
139 TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
140 return (0);
141 }
8414a40c
VZ
142 return (inflateReset(&sp->stream) == Z_OK);
143}
144
145static int
80ed523f 146ZIPDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
8414a40c 147{
8414a40c 148 static const char module[] = "ZIPDecode";
80ed523f 149 ZIPState* sp = DecoderState(tif);
8414a40c
VZ
150
151 (void) s;
152 assert(sp != NULL);
80ed523f
VZ
153 assert(sp->state == ZSTATE_INIT_DECODE);
154
155 sp->stream.next_in = tif->tif_rawcp;
156 sp->stream.avail_in = (uInt) tif->tif_rawcc;
157
8414a40c 158 sp->stream.next_out = op;
80ed523f
VZ
159 assert(sizeof(sp->stream.avail_out)==4); /* if this assert gets raised,
160 we need to simplify this code to reflect a ZLib that is likely updated
161 to deal with 8byte memory sizes, though this code will respond
162 apropriately even before we simplify it */
163 sp->stream.avail_out = (uInt) occ;
164 if ((tmsize_t)sp->stream.avail_out != occ)
165 {
166 TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
167 return (0);
168 }
8414a40c
VZ
169 do {
170 int state = inflate(&sp->stream, Z_PARTIAL_FLUSH);
171 if (state == Z_STREAM_END)
172 break;
173 if (state == Z_DATA_ERROR) {
174 TIFFErrorExt(tif->tif_clientdata, module,
80ed523f
VZ
175 "Decoding error at scanline %lu, %s",
176 (unsigned long) tif->tif_row, sp->stream.msg);
8414a40c
VZ
177 if (inflateSync(&sp->stream) != Z_OK)
178 return (0);
179 continue;
180 }
181 if (state != Z_OK) {
80ed523f
VZ
182 TIFFErrorExt(tif->tif_clientdata, module, "ZLib error: %s",
183 sp->stream.msg);
8414a40c
VZ
184 return (0);
185 }
186 } while (sp->stream.avail_out > 0);
187 if (sp->stream.avail_out != 0) {
188 TIFFErrorExt(tif->tif_clientdata, module,
80ed523f
VZ
189 "Not enough data at scanline %lu (short " TIFF_UINT64_FORMAT " bytes)",
190 (unsigned long) tif->tif_row, (TIFF_UINT64_T) sp->stream.avail_out);
8414a40c
VZ
191 return (0);
192 }
80ed523f
VZ
193
194 tif->tif_rawcp = sp->stream.next_in;
195 tif->tif_rawcc = sp->stream.avail_in;
196
8414a40c
VZ
197 return (1);
198}
199
200static int
201ZIPSetupEncode(TIFF* tif)
202{
8414a40c 203 static const char module[] = "ZIPSetupEncode";
80ed523f 204 ZIPState* sp = EncoderState(tif);
8414a40c
VZ
205
206 assert(sp != NULL);
80ed523f
VZ
207 if (sp->state & ZSTATE_INIT_DECODE) {
208 inflateEnd(&sp->stream);
209 sp->state = 0;
210 }
211
8414a40c 212 if (deflateInit(&sp->stream, sp->zipquality) != Z_OK) {
80ed523f 213 TIFFErrorExt(tif->tif_clientdata, module, "%s", sp->stream.msg);
8414a40c
VZ
214 return (0);
215 } else {
80ed523f 216 sp->state |= ZSTATE_INIT_ENCODE;
8414a40c
VZ
217 return (1);
218 }
219}
220
221/*
222 * Reset encoding state at the start of a strip.
223 */
224static int
80ed523f 225ZIPPreEncode(TIFF* tif, uint16 s)
8414a40c 226{
80ed523f 227 static const char module[] = "ZIPPreEncode";
8414a40c
VZ
228 ZIPState *sp = EncoderState(tif);
229
230 (void) s;
231 assert(sp != NULL);
80ed523f
VZ
232 if( sp->state != ZSTATE_INIT_ENCODE )
233 tif->tif_setupencode( tif );
234
8414a40c 235 sp->stream.next_out = tif->tif_rawdata;
80ed523f
VZ
236 assert(sizeof(sp->stream.avail_out)==4); /* if this assert gets raised,
237 we need to simplify this code to reflect a ZLib that is likely updated
238 to deal with 8byte memory sizes, though this code will respond
239 apropriately even before we simplify it */
8414a40c 240 sp->stream.avail_out = tif->tif_rawdatasize;
80ed523f
VZ
241 if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize)
242 {
243 TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
244 return (0);
245 }
8414a40c
VZ
246 return (deflateReset(&sp->stream) == Z_OK);
247}
248
249/*
250 * Encode a chunk of pixels.
251 */
252static int
80ed523f 253ZIPEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
8414a40c 254{
8414a40c 255 static const char module[] = "ZIPEncode";
80ed523f
VZ
256 ZIPState *sp = EncoderState(tif);
257
258 assert(sp != NULL);
259 assert(sp->state == ZSTATE_INIT_ENCODE);
8414a40c
VZ
260
261 (void) s;
262 sp->stream.next_in = bp;
80ed523f
VZ
263 assert(sizeof(sp->stream.avail_in)==4); /* if this assert gets raised,
264 we need to simplify this code to reflect a ZLib that is likely updated
265 to deal with 8byte memory sizes, though this code will respond
266 apropriately even before we simplify it */
267 sp->stream.avail_in = (uInt) cc;
268 if ((tmsize_t)sp->stream.avail_in != cc)
269 {
270 TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
271 return (0);
272 }
8414a40c
VZ
273 do {
274 if (deflate(&sp->stream, Z_NO_FLUSH) != Z_OK) {
80ed523f
VZ
275 TIFFErrorExt(tif->tif_clientdata, module, "Encoder error: %s",
276 sp->stream.msg);
8414a40c
VZ
277 return (0);
278 }
279 if (sp->stream.avail_out == 0) {
280 tif->tif_rawcc = tif->tif_rawdatasize;
281 TIFFFlushData1(tif);
282 sp->stream.next_out = tif->tif_rawdata;
80ed523f 283 sp->stream.avail_out = (uInt) tif->tif_rawdatasize; /* this is a safe typecast, as check is made already in ZIPPreEncode */
8414a40c
VZ
284 }
285 } while (sp->stream.avail_in > 0);
286 return (1);
287}
288
289/*
290 * Finish off an encoded strip by flushing the last
291 * string and tacking on an End Of Information code.
292 */
293static int
294ZIPPostEncode(TIFF* tif)
295{
8414a40c 296 static const char module[] = "ZIPPostEncode";
80ed523f 297 ZIPState *sp = EncoderState(tif);
8414a40c
VZ
298 int state;
299
300 sp->stream.avail_in = 0;
301 do {
302 state = deflate(&sp->stream, Z_FINISH);
303 switch (state) {
304 case Z_STREAM_END:
305 case Z_OK:
80ed523f
VZ
306 if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize)
307 {
308 tif->tif_rawcc = tif->tif_rawdatasize - sp->stream.avail_out;
309 TIFFFlushData1(tif);
310 sp->stream.next_out = tif->tif_rawdata;
311 sp->stream.avail_out = (uInt) tif->tif_rawdatasize; /* this is a safe typecast, as check is made already in ZIPPreEncode */
312 }
313 break;
8414a40c 314 default:
80ed523f
VZ
315 TIFFErrorExt(tif->tif_clientdata, module, "ZLib error: %s",
316 sp->stream.msg);
317 return (0);
8414a40c
VZ
318 }
319 } while (state != Z_STREAM_END);
320 return (1);
321}
322
323static void
324ZIPCleanup(TIFF* tif)
325{
326 ZIPState* sp = ZState(tif);
327
328 assert(sp != 0);
329
330 (void)TIFFPredictorCleanup(tif);
331
332 tif->tif_tagmethods.vgetfield = sp->vgetparent;
333 tif->tif_tagmethods.vsetfield = sp->vsetparent;
334
80ed523f
VZ
335 if (sp->state & ZSTATE_INIT_ENCODE) {
336 deflateEnd(&sp->stream);
337 sp->state = 0;
338 } else if( sp->state & ZSTATE_INIT_DECODE) {
339 inflateEnd(&sp->stream);
340 sp->state = 0;
8414a40c
VZ
341 }
342 _TIFFfree(sp);
343 tif->tif_data = NULL;
344
345 _TIFFSetDefaultCompressionState(tif);
346}
347
348static int
80ed523f 349ZIPVSetField(TIFF* tif, uint32 tag, va_list ap)
8414a40c 350{
8414a40c 351 static const char module[] = "ZIPVSetField";
80ed523f 352 ZIPState* sp = ZState(tif);
8414a40c
VZ
353
354 switch (tag) {
355 case TIFFTAG_ZIPQUALITY:
80ed523f
VZ
356 sp->zipquality = (int) va_arg(ap, int);
357 if ( sp->state&ZSTATE_INIT_ENCODE ) {
8414a40c
VZ
358 if (deflateParams(&sp->stream,
359 sp->zipquality, Z_DEFAULT_STRATEGY) != Z_OK) {
80ed523f
VZ
360 TIFFErrorExt(tif->tif_clientdata, module, "ZLib error: %s",
361 sp->stream.msg);
8414a40c
VZ
362 return (0);
363 }
364 }
365 return (1);
366 default:
367 return (*sp->vsetparent)(tif, tag, ap);
368 }
369 /*NOTREACHED*/
370}
371
372static int
80ed523f 373ZIPVGetField(TIFF* tif, uint32 tag, va_list ap)
8414a40c
VZ
374{
375 ZIPState* sp = ZState(tif);
376
377 switch (tag) {
378 case TIFFTAG_ZIPQUALITY:
379 *va_arg(ap, int*) = sp->zipquality;
380 break;
381 default:
382 return (*sp->vgetparent)(tif, tag, ap);
383 }
384 return (1);
385}
386
80ed523f
VZ
387static const TIFFField zipFields[] = {
388 { TIFFTAG_ZIPQUALITY, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE, "", NULL },
8414a40c
VZ
389};
390
391int
392TIFFInitZIP(TIFF* tif, int scheme)
393{
80ed523f 394 static const char module[] = "TIFFInitZIP";
8414a40c
VZ
395 ZIPState* sp;
396
397 assert( (scheme == COMPRESSION_DEFLATE)
398 || (scheme == COMPRESSION_ADOBE_DEFLATE));
399
80ed523f
VZ
400 /*
401 * Merge codec-specific tag information.
402 */
403 if (!_TIFFMergeFields(tif, zipFields, TIFFArrayCount(zipFields))) {
404 TIFFErrorExt(tif->tif_clientdata, module,
405 "Merging Deflate codec-specific tags failed");
406 return 0;
407 }
408
8414a40c
VZ
409 /*
410 * Allocate state block so tag methods have storage to record values.
411 */
80ed523f 412 tif->tif_data = (uint8*) _TIFFmalloc(sizeof (ZIPState));
8414a40c
VZ
413 if (tif->tif_data == NULL)
414 goto bad;
415 sp = ZState(tif);
416 sp->stream.zalloc = NULL;
417 sp->stream.zfree = NULL;
418 sp->stream.opaque = NULL;
419 sp->stream.data_type = Z_BINARY;
420
421 /*
80ed523f 422 * Override parent get/set field methods.
8414a40c 423 */
8414a40c
VZ
424 sp->vgetparent = tif->tif_tagmethods.vgetfield;
425 tif->tif_tagmethods.vgetfield = ZIPVGetField; /* hook for codec tags */
426 sp->vsetparent = tif->tif_tagmethods.vsetfield;
427 tif->tif_tagmethods.vsetfield = ZIPVSetField; /* hook for codec tags */
428
429 /* Default values for codec-specific fields */
430 sp->zipquality = Z_DEFAULT_COMPRESSION; /* default comp. level */
431 sp->state = 0;
432
433 /*
434 * Install codec methods.
435 */
80ed523f 436 tif->tif_fixuptags = ZIPFixupTags;
8414a40c
VZ
437 tif->tif_setupdecode = ZIPSetupDecode;
438 tif->tif_predecode = ZIPPreDecode;
439 tif->tif_decoderow = ZIPDecode;
440 tif->tif_decodestrip = ZIPDecode;
80ed523f 441 tif->tif_decodetile = ZIPDecode;
8414a40c
VZ
442 tif->tif_setupencode = ZIPSetupEncode;
443 tif->tif_preencode = ZIPPreEncode;
444 tif->tif_postencode = ZIPPostEncode;
445 tif->tif_encoderow = ZIPEncode;
446 tif->tif_encodestrip = ZIPEncode;
447 tif->tif_encodetile = ZIPEncode;
448 tif->tif_cleanup = ZIPCleanup;
449 /*
450 * Setup predictor setup.
451 */
452 (void) TIFFPredictorInit(tif);
453 return (1);
454bad:
80ed523f 455 TIFFErrorExt(tif->tif_clientdata, module,
8414a40c
VZ
456 "No space for ZIP state block");
457 return (0);
458}
459#endif /* ZIP_SUPORT */
460
461/* vim: set ts=8 sts=8 sw=8 noet: */
80ed523f
VZ
462/*
463 * Local Variables:
464 * mode: c
465 * c-basic-offset: 8
466 * fill-column: 78
467 * End:
468 */