1 : /* $Id: tif_zip.c,v 1.29 2010-03-10 18:56:49 bfriesen Exp $ */
2 :
3 : /*
4 : * Copyright (c) 1995-1997 Sam Leffler
5 : * Copyright (c) 1995-1997 Silicon Graphics, Inc.
6 : *
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.
14 : *
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.
18 : *
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
24 : * OF THIS SOFTWARE.
25 : */
26 :
27 : #include "tiffiop.h"
28 : #ifdef ZIP_SUPPORT
29 : /*
30 : * TIFF Library.
31 : *
32 : * ZIP (aka Deflate) Compression Support
33 : *
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 supressing 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.
43 : *
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.
48 : */
49 : #include "tif_predict.h"
50 : #include "zlib.h"
51 :
52 : #include <stdio.h>
53 :
54 : /*
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.
59 : */
60 : #if !defined(Z_NO_COMPRESSION) || !defined(Z_DEFLATED)
61 : #error "Antiquated ZLIB software; you must use version 1.0 or later"
62 : #endif
63 :
64 : /*
65 : * State block for each open TIFF
66 : * file using ZIP compression/decompression.
67 : */
68 : typedef struct {
69 : TIFFPredictorState predict;
70 : z_stream stream;
71 : int zipquality; /* compression level */
72 : int state; /* state flags */
73 : #define ZSTATE_INIT_DECODE 0x01
74 : #define ZSTATE_INIT_ENCODE 0x02
75 :
76 : TIFFVGetMethod vgetparent; /* super-class method */
77 : TIFFVSetMethod vsetparent; /* super-class method */
78 : } ZIPState;
79 :
80 : #define ZState(tif) ((ZIPState*) (tif)->tif_data)
81 : #define DecoderState(tif) ZState(tif)
82 : #define EncoderState(tif) ZState(tif)
83 :
84 : static int ZIPEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s);
85 : static int ZIPDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s);
86 :
87 : static int
88 : ZIPFixupTags(TIFF* tif)
89 171 : {
90 : (void) tif;
91 171 : return (1);
92 : }
93 :
94 : static int
95 : ZIPSetupDecode(TIFF* tif)
96 60 : {
97 : static const char module[] = "ZIPSetupDecode";
98 60 : ZIPState* sp = DecoderState(tif);
99 :
100 60 : assert(sp != NULL);
101 :
102 : /* if we were last encoding, terminate this mode */
103 60 : if (sp->state & ZSTATE_INIT_ENCODE) {
104 1 : deflateEnd(&sp->stream);
105 1 : sp->state = 0;
106 : }
107 :
108 60 : if (inflateInit(&sp->stream) != Z_OK) {
109 0 : TIFFErrorExt(tif->tif_clientdata, module, "%s", sp->stream.msg);
110 0 : return (0);
111 : } else {
112 60 : sp->state |= ZSTATE_INIT_DECODE;
113 60 : return (1);
114 : }
115 : }
116 :
117 : /*
118 : * Setup state for decoding a strip.
119 : */
120 : static int
121 : ZIPPreDecode(TIFF* tif, uint16 s)
122 237 : {
123 : static const char module[] = "ZIPPreDecode";
124 237 : ZIPState* sp = DecoderState(tif);
125 :
126 : (void) s;
127 237 : assert(sp != NULL);
128 :
129 237 : if( (sp->state & ZSTATE_INIT_DECODE) == 0 )
130 1 : tif->tif_setupdecode( tif );
131 :
132 237 : sp->stream.next_in = tif->tif_rawdata;
133 : assert(sizeof(sp->stream.avail_in)==4); /* if this assert gets raised,
134 : we need to simplify this code to reflect a ZLib that is likely updated
135 : to deal with 8byte memory sizes, though this code will respond
136 : apropriately even before we simplify it */
137 237 : sp->stream.avail_in = (uInt) tif->tif_rawcc;
138 237 : if ((tmsize_t)sp->stream.avail_in != tif->tif_rawcc)
139 : {
140 0 : TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
141 0 : return (0);
142 : }
143 237 : return (inflateReset(&sp->stream) == Z_OK);
144 : }
145 :
146 : static int
147 : ZIPDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
148 237 : {
149 : static const char module[] = "ZIPDecode";
150 237 : ZIPState* sp = DecoderState(tif);
151 :
152 : (void) s;
153 237 : assert(sp != NULL);
154 237 : assert(sp->state == ZSTATE_INIT_DECODE);
155 :
156 237 : sp->stream.next_out = op;
157 : assert(sizeof(sp->stream.avail_out)==4); /* if this assert gets raised,
158 : we need to simplify this code to reflect a ZLib that is likely updated
159 : to deal with 8byte memory sizes, though this code will respond
160 : apropriately even before we simplify it */
161 237 : sp->stream.avail_out = (uInt) occ;
162 237 : if ((tmsize_t)sp->stream.avail_out != occ)
163 : {
164 0 : TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
165 0 : return (0);
166 : }
167 : do {
168 237 : int state = inflate(&sp->stream, Z_PARTIAL_FLUSH);
169 237 : if (state == Z_STREAM_END)
170 230 : break;
171 7 : if (state == Z_DATA_ERROR) {
172 0 : TIFFErrorExt(tif->tif_clientdata, module,
173 : "Decoding error at scanline %lu, %s",
174 : (unsigned long) tif->tif_row, sp->stream.msg);
175 0 : if (inflateSync(&sp->stream) != Z_OK)
176 0 : return (0);
177 0 : continue;
178 : }
179 7 : if (state != Z_OK) {
180 0 : TIFFErrorExt(tif->tif_clientdata, module, "ZLib error: %s",
181 : sp->stream.msg);
182 0 : return (0);
183 : }
184 7 : } while (sp->stream.avail_out > 0);
185 237 : if (sp->stream.avail_out != 0) {
186 0 : TIFFErrorExt(tif->tif_clientdata, module,
187 : "Not enough data at scanline %lu (short %llu bytes)",
188 : (unsigned long) tif->tif_row, (unsigned long long) sp->stream.avail_out);
189 0 : return (0);
190 : }
191 237 : return (1);
192 : }
193 :
194 : static int
195 : ZIPSetupEncode(TIFF* tif)
196 16 : {
197 : static const char module[] = "ZIPSetupEncode";
198 16 : ZIPState* sp = EncoderState(tif);
199 :
200 16 : assert(sp != NULL);
201 16 : if (sp->state & ZSTATE_INIT_DECODE) {
202 1 : inflateEnd(&sp->stream);
203 1 : sp->state = 0;
204 : }
205 :
206 16 : if (deflateInit(&sp->stream, sp->zipquality) != Z_OK) {
207 0 : TIFFErrorExt(tif->tif_clientdata, module, "%s", sp->stream.msg);
208 0 : return (0);
209 : } else {
210 16 : sp->state |= ZSTATE_INIT_ENCODE;
211 16 : return (1);
212 : }
213 : }
214 :
215 : /*
216 : * Reset encoding state at the start of a strip.
217 : */
218 : static int
219 : ZIPPreEncode(TIFF* tif, uint16 s)
220 98 : {
221 : static const char module[] = "ZIPPreEncode";
222 98 : ZIPState *sp = EncoderState(tif);
223 :
224 : (void) s;
225 98 : assert(sp != NULL);
226 98 : if( sp->state != ZSTATE_INIT_ENCODE )
227 1 : tif->tif_setupencode( tif );
228 :
229 98 : sp->stream.next_out = tif->tif_rawdata;
230 : assert(sizeof(sp->stream.avail_out)==4); /* if this assert gets raised,
231 : we need to simplify this code to reflect a ZLib that is likely updated
232 : to deal with 8byte memory sizes, though this code will respond
233 : apropriately even before we simplify it */
234 98 : sp->stream.avail_out = tif->tif_rawdatasize;
235 98 : if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize)
236 : {
237 0 : TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
238 0 : return (0);
239 : }
240 98 : return (deflateReset(&sp->stream) == Z_OK);
241 : }
242 :
243 : /*
244 : * Encode a chunk of pixels.
245 : */
246 : static int
247 : ZIPEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
248 98 : {
249 : static const char module[] = "ZIPEncode";
250 98 : ZIPState *sp = EncoderState(tif);
251 :
252 98 : assert(sp != NULL);
253 98 : assert(sp->state == ZSTATE_INIT_ENCODE);
254 :
255 : (void) s;
256 98 : sp->stream.next_in = bp;
257 : assert(sizeof(sp->stream.avail_in)==4); /* if this assert gets raised,
258 : we need to simplify this code to reflect a ZLib that is likely updated
259 : to deal with 8byte memory sizes, though this code will respond
260 : apropriately even before we simplify it */
261 98 : sp->stream.avail_in = (uInt) cc;
262 98 : if ((tmsize_t)sp->stream.avail_in != cc)
263 : {
264 0 : TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
265 0 : return (0);
266 : }
267 : do {
268 98 : if (deflate(&sp->stream, Z_NO_FLUSH) != Z_OK) {
269 0 : TIFFErrorExt(tif->tif_clientdata, module, "Encoder error: %s",
270 : sp->stream.msg);
271 0 : return (0);
272 : }
273 98 : if (sp->stream.avail_out == 0) {
274 0 : tif->tif_rawcc = tif->tif_rawdatasize;
275 0 : TIFFFlushData1(tif);
276 0 : sp->stream.next_out = tif->tif_rawdata;
277 0 : sp->stream.avail_out = (uInt) tif->tif_rawdatasize; /* this is a safe typecast, as check is made already in ZIPPreEncode */
278 : }
279 98 : } while (sp->stream.avail_in > 0);
280 98 : return (1);
281 : }
282 :
283 : /*
284 : * Finish off an encoded strip by flushing the last
285 : * string and tacking on an End Of Information code.
286 : */
287 : static int
288 : ZIPPostEncode(TIFF* tif)
289 98 : {
290 : static const char module[] = "ZIPPostEncode";
291 98 : ZIPState *sp = EncoderState(tif);
292 : int state;
293 :
294 98 : sp->stream.avail_in = 0;
295 : do {
296 98 : state = deflate(&sp->stream, Z_FINISH);
297 98 : switch (state) {
298 : case Z_STREAM_END:
299 : case Z_OK:
300 98 : if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize)
301 : {
302 98 : tif->tif_rawcc = tif->tif_rawdatasize - sp->stream.avail_out;
303 98 : TIFFFlushData1(tif);
304 98 : sp->stream.next_out = tif->tif_rawdata;
305 98 : sp->stream.avail_out = (uInt) tif->tif_rawdatasize; /* this is a safe typecast, as check is made already in ZIPPreEncode */
306 : }
307 : break;
308 : default:
309 0 : TIFFErrorExt(tif->tif_clientdata, module, "ZLib error: %s",
310 : sp->stream.msg);
311 0 : return (0);
312 : }
313 98 : } while (state != Z_STREAM_END);
314 98 : return (1);
315 : }
316 :
317 : static void
318 : ZIPCleanup(TIFF* tif)
319 182 : {
320 182 : ZIPState* sp = ZState(tif);
321 :
322 182 : assert(sp != 0);
323 :
324 182 : (void)TIFFPredictorCleanup(tif);
325 :
326 182 : tif->tif_tagmethods.vgetfield = sp->vgetparent;
327 182 : tif->tif_tagmethods.vsetfield = sp->vsetparent;
328 :
329 182 : if (sp->state & ZSTATE_INIT_ENCODE) {
330 15 : deflateEnd(&sp->stream);
331 15 : sp->state = 0;
332 167 : } else if( sp->state & ZSTATE_INIT_DECODE) {
333 59 : inflateEnd(&sp->stream);
334 59 : sp->state = 0;
335 : }
336 182 : _TIFFfree(sp);
337 182 : tif->tif_data = NULL;
338 :
339 182 : _TIFFSetDefaultCompressionState(tif);
340 182 : }
341 :
342 : static int
343 : ZIPVSetField(TIFF* tif, uint32 tag, va_list ap)
344 1759 : {
345 : static const char module[] = "ZIPVSetField";
346 1759 : ZIPState* sp = ZState(tif);
347 :
348 1759 : switch (tag) {
349 : case TIFFTAG_ZIPQUALITY:
350 0 : sp->zipquality = (int) va_arg(ap, int);
351 0 : if ( sp->state&ZSTATE_INIT_ENCODE ) {
352 0 : if (deflateParams(&sp->stream,
353 : sp->zipquality, Z_DEFAULT_STRATEGY) != Z_OK) {
354 0 : TIFFErrorExt(tif->tif_clientdata, module, "ZLib error: %s",
355 : sp->stream.msg);
356 0 : return (0);
357 : }
358 : }
359 0 : return (1);
360 : default:
361 1759 : return (*sp->vsetparent)(tif, tag, ap);
362 : }
363 : /*NOTREACHED*/
364 : }
365 :
366 : static int
367 : ZIPVGetField(TIFF* tif, uint32 tag, va_list ap)
368 2779 : {
369 2779 : ZIPState* sp = ZState(tif);
370 :
371 2779 : switch (tag) {
372 : case TIFFTAG_ZIPQUALITY:
373 33 : *va_arg(ap, int*) = sp->zipquality;
374 : break;
375 : default:
376 2746 : return (*sp->vgetparent)(tif, tag, ap);
377 : }
378 33 : return (1);
379 : }
380 :
381 : static const TIFFField zipFields[] = {
382 : { TIFFTAG_ZIPQUALITY, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE, "", NULL },
383 : };
384 :
385 : int
386 : TIFFInitZIP(TIFF* tif, int scheme)
387 188 : {
388 : static const char module[] = "TIFFInitZIP";
389 : ZIPState* sp;
390 :
391 188 : assert( (scheme == COMPRESSION_DEFLATE)
392 : || (scheme == COMPRESSION_ADOBE_DEFLATE));
393 :
394 : /*
395 : * Merge codec-specific tag information.
396 : */
397 188 : if (!_TIFFMergeFields(tif, zipFields, TIFFArrayCount(zipFields))) {
398 0 : TIFFErrorExt(tif->tif_clientdata, module,
399 : "Merging Deflate codec-specific tags failed");
400 0 : return 0;
401 : }
402 :
403 : /*
404 : * Allocate state block so tag methods have storage to record values.
405 : */
406 188 : tif->tif_data = (uint8*) _TIFFmalloc(sizeof (ZIPState));
407 188 : if (tif->tif_data == NULL)
408 0 : goto bad;
409 188 : sp = ZState(tif);
410 188 : sp->stream.zalloc = NULL;
411 188 : sp->stream.zfree = NULL;
412 188 : sp->stream.opaque = NULL;
413 188 : sp->stream.data_type = Z_BINARY;
414 :
415 : /*
416 : * Override parent get/set field methods.
417 : */
418 188 : sp->vgetparent = tif->tif_tagmethods.vgetfield;
419 188 : tif->tif_tagmethods.vgetfield = ZIPVGetField; /* hook for codec tags */
420 188 : sp->vsetparent = tif->tif_tagmethods.vsetfield;
421 188 : tif->tif_tagmethods.vsetfield = ZIPVSetField; /* hook for codec tags */
422 :
423 : /* Default values for codec-specific fields */
424 188 : sp->zipquality = Z_DEFAULT_COMPRESSION; /* default comp. level */
425 188 : sp->state = 0;
426 :
427 : /*
428 : * Install codec methods.
429 : */
430 188 : tif->tif_fixuptags = ZIPFixupTags;
431 188 : tif->tif_setupdecode = ZIPSetupDecode;
432 188 : tif->tif_predecode = ZIPPreDecode;
433 188 : tif->tif_decoderow = ZIPDecode;
434 188 : tif->tif_decodestrip = ZIPDecode;
435 188 : tif->tif_decodetile = ZIPDecode;
436 188 : tif->tif_setupencode = ZIPSetupEncode;
437 188 : tif->tif_preencode = ZIPPreEncode;
438 188 : tif->tif_postencode = ZIPPostEncode;
439 188 : tif->tif_encoderow = ZIPEncode;
440 188 : tif->tif_encodestrip = ZIPEncode;
441 188 : tif->tif_encodetile = ZIPEncode;
442 188 : tif->tif_cleanup = ZIPCleanup;
443 : /*
444 : * Setup predictor setup.
445 : */
446 188 : (void) TIFFPredictorInit(tif);
447 188 : return (1);
448 0 : bad:
449 0 : TIFFErrorExt(tif->tif_clientdata, module,
450 : "No space for ZIP state block");
451 0 : return (0);
452 : }
453 : #endif /* ZIP_SUPORT */
454 :
455 : /* vim: set ts=8 sts=8 sw=8 noet: */
456 : /*
457 : * Local Variables:
458 : * mode: c
459 : * c-basic-offset: 8
460 : * fill-column: 78
461 : * End:
462 : */
|