BigTIFF upgrade in progress - widespread temp mess - does not compile now

This commit is contained in:
Joris Van Damme 2007-04-10 17:36:19 +00:00
parent 8872f01002
commit 53b3dd936d

View File

@ -1,4 +1,4 @@
/* $Id: tif_zip.c,v 1.14 2007-04-07 15:14:31 dron Exp $ */ /* $Id: tif_zip.c,v 1.15 2007-04-10 17:36:19 joris Exp $ */
/* /*
* Copyright (c) 1995-1997 Sam Leffler * Copyright (c) 1995-1997 Sam Leffler
@ -65,24 +65,24 @@
* State block for each open TIFF * State block for each open TIFF
* file using ZIP compression/decompression. * file using ZIP compression/decompression.
*/ */
typedef struct { typedef struct {
TIFFPredictorState predict; TIFFPredictorState predict;
z_stream stream; z_stream stream;
int zipquality; /* compression level */ int zipquality; /* compression level */
int state; /* state flags */ int state; /* state flags */
#define ZSTATE_INIT_DECODE 0x01 #define ZSTATE_INIT_DECODE 0x01
#define ZSTATE_INIT_ENCODE 0x02 #define ZSTATE_INIT_ENCODE 0x02
TIFFVGetMethod vgetparent; /* super-class method */ TIFFVGetMethod vgetparent; /* super-class method */
TIFFVSetMethod vsetparent; /* super-class method */ TIFFVSetMethod vsetparent; /* super-class method */
} ZIPState; } ZIPState;
#define ZState(tif) ((ZIPState*) (tif)->tif_data) #define ZState(tif) ((ZIPState*) (tif)->tif_data)
#define DecoderState(tif) ZState(tif) #define DecoderState(tif) ZState(tif)
#define EncoderState(tif) ZState(tif) #define EncoderState(tif) ZState(tif)
static int ZIPEncode(TIFF*, tidata_t, tsize_t, uint16); static int ZIPEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s);
static int ZIPDecode(TIFF*, tidata_t, tsize_t, uint16); static int ZIPDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s);
static int static int
ZIPSetupDecode(TIFF* tif) ZIPSetupDecode(TIFF* tif)
@ -94,12 +94,12 @@ ZIPSetupDecode(TIFF* tif)
/* if we were last encoding, terminate this mode */ /* if we were last encoding, terminate this mode */
if (sp->state & ZSTATE_INIT_ENCODE) { if (sp->state & ZSTATE_INIT_ENCODE) {
deflateEnd(&sp->stream); deflateEnd(&sp->stream);
sp->state = 0; sp->state = 0;
} }
if (inflateInit(&sp->stream) != Z_OK) { if (inflateInit(&sp->stream) != Z_OK) {
TIFFErrorExt(tif->tif_clientdata, module, "%s: %s", tif->tif_name, sp->stream.msg); TIFFErrorExt(tif->tif_clientdata, module, "%s", sp->stream.msg);
return (0); return (0);
} else { } else {
sp->state |= ZSTATE_INIT_DECODE; sp->state |= ZSTATE_INIT_DECODE;
@ -113,53 +113,72 @@ ZIPSetupDecode(TIFF* tif)
static int static int
ZIPPreDecode(TIFF* tif, uint16 s) ZIPPreDecode(TIFF* tif, uint16 s)
{ {
static const char module[] = "ZIPPreDecode";
ZIPState* sp = DecoderState(tif); ZIPState* sp = DecoderState(tif);
(void) s; (void) s;
assert(sp != NULL); assert(sp != NULL);
if( (sp->state & ZSTATE_INIT_DECODE) == 0 ) if( (sp->state & ZSTATE_INIT_DECODE) == 0 )
ZIPSetupDecode(tif); ZIPSetupDecode(tif);
sp->stream.next_in = tif->tif_rawdata; sp->stream.next_in = tif->tif_rawdata;
sp->stream.avail_in = tif->tif_rawcc; ddd assert(sizeof(sp->stream.avail_in)==4); /* if this assert gets raised,
we need to simplify this code to reflect a ZLib that is likely updated
to deal with 8byte memory sizes, though this code will respond
apropriately even before we simplify it */
sp->stream.avail_in = (uInt) tif->tif_rawcc;
if ((tmsize_t)sp->stream.avail_in != tif->tif_rawcc)
{
TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
return (0);
}
return (inflateReset(&sp->stream) == Z_OK); return (inflateReset(&sp->stream) == Z_OK);
} }
static int static int
ZIPDecode(TIFF* tif, tidata_t op, tsize_t occ, uint16 s) ZIPDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
{ {
ZIPState* sp = DecoderState(tif);
static const char module[] = "ZIPDecode"; static const char module[] = "ZIPDecode";
ZIPState* sp = DecoderState(tif);
(void) s; (void) s;
assert(sp != NULL); assert(sp != NULL);
assert(sp->state == ZSTATE_INIT_DECODE); assert(sp->state == ZSTATE_INIT_DECODE);
sp->stream.next_out = op; sp->stream.next_out = op;
sp->stream.avail_out = occ; assert(sizeof(sp->stream.avail_out)==4); /* if this assert gets raised,
we need to simplify this code to reflect a ZLib that is likely updated
to deal with 8byte memory sizes, though this code will respond
apropriately even before we simplify it */
sp->stream.avail_out = (uInt) occ;
if ((tmsize_t)sp->stream.avail_out != occ)
{
TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
return (0);
}
do { do {
int state = inflate(&sp->stream, Z_PARTIAL_FLUSH); int state = inflate(&sp->stream, Z_PARTIAL_FLUSH);
if (state == Z_STREAM_END) if (state == Z_STREAM_END)
break; break;
if (state == Z_DATA_ERROR) { if (state == Z_DATA_ERROR) {
TIFFErrorExt(tif->tif_clientdata, module, TIFFErrorExt(tif->tif_clientdata, module,
"%s: Decoding error at scanline %d, %s", "Decoding error at scanline %lud, %s",
tif->tif_name, tif->tif_row, sp->stream.msg); (unsigned long) tif->tif_row, sp->stream.msg);
if (inflateSync(&sp->stream) != Z_OK) if (inflateSync(&sp->stream) != Z_OK)
return (0); return (0);
continue; continue;
} }
if (state != Z_OK) { if (state != Z_OK) {
TIFFErrorExt(tif->tif_clientdata, module, "%s: zlib error: %s", TIFFErrorExt(tif->tif_clientdata, module, "ZLib error: %s",
tif->tif_name, sp->stream.msg); sp->stream.msg);
return (0); return (0);
} }
} while (sp->stream.avail_out > 0); } while (sp->stream.avail_out > 0);
if (sp->stream.avail_out != 0) { if (sp->stream.avail_out != 0) {
TIFFErrorExt(tif->tif_clientdata, module, TIFFErrorExt(tif->tif_clientdata, module,
"%s: Not enough data at scanline %d (short %d bytes)", "Not enough data at scanline %lud (short %llud bytes)",
tif->tif_name, tif->tif_row, sp->stream.avail_out); (unsigned long) tif->tif_row, (unsigned long long) sp->stream.avail_out);
return (0); return (0);
} }
return (1); return (1);
@ -168,17 +187,17 @@ ZIPDecode(TIFF* tif, tidata_t op, tsize_t occ, uint16 s)
static int static int
ZIPSetupEncode(TIFF* tif) ZIPSetupEncode(TIFF* tif)
{ {
ZIPState* sp = EncoderState(tif);
static const char module[] = "ZIPSetupEncode"; static const char module[] = "ZIPSetupEncode";
ZIPState* sp = EncoderState(tif);
assert(sp != NULL); assert(sp != NULL);
if (sp->state & ZSTATE_INIT_DECODE) { if (sp->state & ZSTATE_INIT_DECODE) {
inflateEnd(&sp->stream); inflateEnd(&sp->stream);
sp->state = 0; sp->state = 0;
} }
if (deflateInit(&sp->stream, sp->zipquality) != Z_OK) { if (deflateInit(&sp->stream, sp->zipquality) != Z_OK) {
TIFFErrorExt(tif->tif_clientdata, module, "%s: %s", tif->tif_name, sp->stream.msg); TIFFErrorExt(tif->tif_clientdata, module, "%s", sp->stream.msg);
return (0); return (0);
} else { } else {
sp->state |= ZSTATE_INIT_ENCODE; sp->state |= ZSTATE_INIT_ENCODE;
@ -192,15 +211,25 @@ ZIPSetupEncode(TIFF* tif)
static int static int
ZIPPreEncode(TIFF* tif, uint16 s) ZIPPreEncode(TIFF* tif, uint16 s)
{ {
static const char module[] = "ZIPPreEncode";
ZIPState *sp = EncoderState(tif); ZIPState *sp = EncoderState(tif);
(void) s; (void) s;
assert(sp != NULL); assert(sp != NULL);
if( sp->state != ZSTATE_INIT_ENCODE ) if( sp->state != ZSTATE_INIT_ENCODE )
ZIPSetupEncode(tif); ZIPSetupEncode(tif);
sp->stream.next_out = tif->tif_rawdata; sp->stream.next_out = tif->tif_rawdata;
sp->stream.avail_out = tif->tif_rawdatasize; ddd assert(sizeof(sp->stream.avail_out)==4); /* if this assert gets raised,
we need to simplify this code to reflect a ZLib that is likely updated
to deal with 8byte memory sizes, though this code will respond
apropriately even before we simplify it */
sp->stream.avail_out = tif->tif_rawdatasize;
if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize)
{
TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
return (0);
}
return (deflateReset(&sp->stream) == Z_OK); return (deflateReset(&sp->stream) == Z_OK);
} }
@ -208,28 +237,37 @@ ZIPPreEncode(TIFF* tif, uint16 s)
* Encode a chunk of pixels. * Encode a chunk of pixels.
*/ */
static int static int
ZIPEncode(TIFF* tif, tidata_t bp, tsize_t cc, uint16 s) ZIPEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
{ {
ZIPState *sp = EncoderState(tif);
static const char module[] = "ZIPEncode"; static const char module[] = "ZIPEncode";
ZIPState *sp = EncoderState(tif);
assert(sp != NULL); assert(sp != NULL);
assert(sp->state == ZSTATE_INIT_ENCODE); assert(sp->state == ZSTATE_INIT_ENCODE);
(void) s; (void) s;
sp->stream.next_in = bp; sp->stream.next_in = bp;
sp->stream.avail_in = cc; assert(sizeof(sp->stream.avail_in)==4); /* if this assert gets raised,
we need to simplify this code to reflect a ZLib that is likely updated
to deal with 8byte memory sizes, though this code will respond
apropriately even before we simplify it */
sp->stream.avail_in = (uInt) cc;
if ((tmsize_t)sp->stream.avail_in != cc)
{
TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
return (0);
}
do { do {
if (deflate(&sp->stream, Z_NO_FLUSH) != Z_OK) { if (deflate(&sp->stream, Z_NO_FLUSH) != Z_OK) {
TIFFErrorExt(tif->tif_clientdata, module, "%s: Encoder error: %s", TIFFErrorExt(tif->tif_clientdata, module, "Encoder error: %s",
tif->tif_name, sp->stream.msg); sp->stream.msg);
return (0); return (0);
} }
if (sp->stream.avail_out == 0) { if (sp->stream.avail_out == 0) {
tif->tif_rawcc = tif->tif_rawdatasize; ddd tif->tif_rawcc = tif->tif_rawdatasize;
TIFFFlushData1(tif); TIFFFlushData1(tif);
sp->stream.next_out = tif->tif_rawdata; sp->stream.next_out = tif->tif_rawdata;
sp->stream.avail_out = tif->tif_rawdatasize; ddd sp->stream.avail_out = (uInt) tif->tif_rawdatasize;
} }
} while (sp->stream.avail_in > 0); } while (sp->stream.avail_in > 0);
return (1); return (1);
@ -242,8 +280,8 @@ ZIPEncode(TIFF* tif, tidata_t bp, tsize_t cc, uint16 s)
static int static int
ZIPPostEncode(TIFF* tif) ZIPPostEncode(TIFF* tif)
{ {
ZIPState *sp = EncoderState(tif);
static const char module[] = "ZIPPostEncode"; static const char module[] = "ZIPPostEncode";
ZIPState *sp = EncoderState(tif);
int state; int state;
sp->stream.avail_in = 0; sp->stream.avail_in = 0;
@ -252,19 +290,18 @@ ZIPPostEncode(TIFF* tif)
switch (state) { switch (state) {
case Z_STREAM_END: case Z_STREAM_END:
case Z_OK: case Z_OK:
if ((int)sp->stream.avail_out != (int)tif->tif_rawdatasize) ddd if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize)
{ {
tif->tif_rawcc = ddd tif->tif_rawcc = tif->tif_rawdatasize - sp->stream.avail_out;
tif->tif_rawdatasize - sp->stream.avail_out; ddd TIFFFlushData1(tif);
TIFFFlushData1(tif); sp->stream.next_out = tif->tif_rawdata;
sp->stream.next_out = tif->tif_rawdata; sp->stream.avail_out = (uInt) tif->tif_rawdatasize;
sp->stream.avail_out = tif->tif_rawdatasize; ddd }
} break;
break;
default: default:
TIFFErrorExt(tif->tif_clientdata, module, "%s: zlib error: %s", TIFFErrorExt(tif->tif_clientdata, module, "ZLib error: %s",
tif->tif_name, sp->stream.msg); sp->stream.msg);
return (0); return (0);
} }
} while (state != Z_STREAM_END); } while (state != Z_STREAM_END);
return (1); return (1);
@ -283,11 +320,11 @@ ZIPCleanup(TIFF* tif)
tif->tif_tagmethods.vsetfield = sp->vsetparent; tif->tif_tagmethods.vsetfield = sp->vsetparent;
if (sp->state & ZSTATE_INIT_ENCODE) { if (sp->state & ZSTATE_INIT_ENCODE) {
deflateEnd(&sp->stream); deflateEnd(&sp->stream);
sp->state = 0; sp->state = 0;
} else if( sp->state & ZSTATE_INIT_DECODE) { } else if( sp->state & ZSTATE_INIT_DECODE) {
inflateEnd(&sp->stream); inflateEnd(&sp->stream);
sp->state = 0; sp->state = 0;
} }
_TIFFfree(sp); _TIFFfree(sp);
tif->tif_data = NULL; tif->tif_data = NULL;
@ -296,10 +333,10 @@ ZIPCleanup(TIFF* tif)
} }
static int static int
ZIPVSetField(TIFF* tif, ttag_t tag, va_list ap) ZIPVSetField(TIFF* tif, uint32 tag, va_list ap)
{ {
ZIPState* sp = ZState(tif);
static const char module[] = "ZIPVSetField"; static const char module[] = "ZIPVSetField";
ZIPState* sp = ZState(tif);
switch (tag) { switch (tag) {
case TIFFTAG_ZIPQUALITY: case TIFFTAG_ZIPQUALITY:
@ -307,8 +344,8 @@ ZIPVSetField(TIFF* tif, ttag_t tag, va_list ap)
if ( sp->state&ZSTATE_INIT_ENCODE ) { if ( sp->state&ZSTATE_INIT_ENCODE ) {
if (deflateParams(&sp->stream, if (deflateParams(&sp->stream,
sp->zipquality, Z_DEFAULT_STRATEGY) != Z_OK) { sp->zipquality, Z_DEFAULT_STRATEGY) != Z_OK) {
TIFFErrorExt(tif->tif_clientdata, module, "%s: zlib error: %s", TIFFErrorExt(tif->tif_clientdata, module, "ZLib error: %s",
tif->tif_name, sp->stream.msg); sp->stream.msg);
return (0); return (0);
} }
} }
@ -320,7 +357,7 @@ ZIPVSetField(TIFF* tif, ttag_t tag, va_list ap)
} }
static int static int
ZIPVGetField(TIFF* tif, ttag_t tag, va_list ap) ZIPVGetField(TIFF* tif, uint32 tag, va_list ap)
{ {
ZIPState* sp = ZState(tif); ZIPState* sp = ZState(tif);
@ -361,7 +398,7 @@ TIFFInitZIP(TIFF* tif, int scheme)
/* /*
* Allocate state block so tag methods have storage to record values. * Allocate state block so tag methods have storage to record values.
*/ */
tif->tif_data = (tidata_t) _TIFFmalloc(sizeof (ZIPState)); tif->tif_data = (uint8*) _TIFFmalloc(sizeof (ZIPState));
if (tif->tif_data == NULL) if (tif->tif_data == NULL)
goto bad; goto bad;
sp = ZState(tif); sp = ZState(tif);
@ -387,15 +424,15 @@ TIFFInitZIP(TIFF* tif, int scheme)
*/ */
tif->tif_setupdecode = ZIPSetupDecode; tif->tif_setupdecode = ZIPSetupDecode;
tif->tif_predecode = ZIPPreDecode; tif->tif_predecode = ZIPPreDecode;
tif->tif_decoderow = ZIPDecode; ddd tif->tif_decoderow = ZIPDecode;
tif->tif_decodestrip = ZIPDecode; ddd tif->tif_decodestrip = ZIPDecode;
tif->tif_decodetile = ZIPDecode; ddd tif->tif_decodetile = ZIPDecode;
tif->tif_setupencode = ZIPSetupEncode; tif->tif_setupencode = ZIPSetupEncode;
tif->tif_preencode = ZIPPreEncode; tif->tif_preencode = ZIPPreEncode;
tif->tif_postencode = ZIPPostEncode; tif->tif_postencode = ZIPPostEncode;
tif->tif_encoderow = ZIPEncode; ddd tif->tif_encoderow = ZIPEncode;
tif->tif_encodestrip = ZIPEncode; ddd tif->tif_encodestrip = ZIPEncode;
tif->tif_encodetile = ZIPEncode; ddd tif->tif_encodetile = ZIPEncode;
tif->tif_cleanup = ZIPCleanup; tif->tif_cleanup = ZIPCleanup;
/* /*
* Setup predictor setup. * Setup predictor setup.