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

This commit is contained in:
Joris Van Damme 2007-04-10 02:41:15 +00:00
parent 05046aae9e
commit a9e2df3933

View File

@ -1,4 +1,4 @@
/* $Id: tif_lzw.c,v 1.31 2007-03-31 01:41:11 joris Exp $ */ /* $Id: tif_lzw.c,v 1.32 2007-04-10 02:41:15 joris Exp $ */
/* /*
* Copyright (c) 1988-1997 Sam Leffler * Copyright (c) 1988-1997 Sam Leffler
@ -53,34 +53,34 @@
* *
* Future revisions to the TIFF spec are expected to "clarify this issue". * Future revisions to the TIFF spec are expected to "clarify this issue".
*/ */
#define LZW_COMPAT /* include backwards compatibility code */ #define LZW_COMPAT /* include backwards compatibility code */
/* /*
* Each strip of data is supposed to be terminated by a CODE_EOI. * Each strip of data is supposed to be terminated by a CODE_EOI.
* If the following #define is included, the decoder will also * If the following #define is included, the decoder will also
* check for end-of-strip w/o seeing this code. This makes the * check for end-of-strip w/o seeing this code. This makes the
* library more robust, but also slower. * library more robust, but also slower.
*/ */
#define LZW_CHECKEOS /* include checks for strips w/o EOI code */ #define LZW_CHECKEOS /* include checks for strips w/o EOI code */
#define MAXCODE(n) ((1L<<(n))-1) #define MAXCODE(n) ((1L<<(n))-1)
/* /*
* The TIFF spec specifies that encoded bit * The TIFF spec specifies that encoded bit
* strings range from 9 to 12 bits. * strings range from 9 to 12 bits.
*/ */
#define BITS_MIN 9 /* start with 9 bits */ #define BITS_MIN 9 /* start with 9 bits */
#define BITS_MAX 12 /* max of 12 bit strings */ #define BITS_MAX 12 /* max of 12 bit strings */
/* predefined codes */ /* predefined codes */
#define CODE_CLEAR 256 /* code to clear string table */ #define CODE_CLEAR 256 /* code to clear string table */
#define CODE_EOI 257 /* end-of-information code */ #define CODE_EOI 257 /* end-of-information code */
#define CODE_FIRST 258 /* first free code entry */ #define CODE_FIRST 258 /* first free code entry */
#define CODE_MAX MAXCODE(BITS_MAX) #define CODE_MAX MAXCODE(BITS_MAX)
#define HSIZE 9001L /* 91% occupancy */ #define HSIZE 9001L /* 91% occupancy */
#define HSHIFT (13-8) #define HSHIFT (13-8)
#ifdef LZW_COMPAT #ifdef LZW_COMPAT
/* NB: +1024 is for compatibility with old files */ /* NB: +1024 is for compatibility with old files */
#define CSIZE (MAXCODE(BITS_MAX)+1024L) #define CSIZE (MAXCODE(BITS_MAX)+1024L)
#else #else
#define CSIZE (MAXCODE(BITS_MAX)+1L) #define CSIZE (MAXCODE(BITS_MAX)+1L)
#endif #endif
/* /*
@ -88,23 +88,23 @@
* compression/decompression. Note that the predictor * compression/decompression. Note that the predictor
* state block must be first in this data structure. * state block must be first in this data structure.
*/ */
typedef struct { typedef struct {
TIFFPredictorState predict; /* predictor super class */ TIFFPredictorState predict; /* predictor super class */
unsigned short nbits; /* # of bits/code */ unsigned short nbits; /* # of bits/code */
unsigned short maxcode; /* maximum code for lzw_nbits */ unsigned short maxcode; /* maximum code for lzw_nbits */
unsigned short free_ent; /* next free entry in hash table */ unsigned short free_ent; /* next free entry in hash table */
long nextdata; /* next bits of i/o */ long nextdata; /* next bits of i/o */
long nextbits; /* # of valid bits in lzw_nextdata */ long nextbits; /* # of valid bits in lzw_nextdata */
int rw_mode; /* preserve rw_mode from init */ int rw_mode; /* preserve rw_mode from init */
} LZWBaseState; } LZWBaseState;
#define lzw_nbits base.nbits #define lzw_nbits base.nbits
#define lzw_maxcode base.maxcode #define lzw_maxcode base.maxcode
#define lzw_free_ent base.free_ent #define lzw_free_ent base.free_ent
#define lzw_nextdata base.nextdata #define lzw_nextdata base.nextdata
#define lzw_nextbits base.nextbits #define lzw_nextbits base.nextbits
/* /*
* Encoding-specific state. * Encoding-specific state.
@ -125,44 +125,44 @@ typedef struct code_ent {
unsigned char firstchar; /* first token of string */ unsigned char firstchar; /* first token of string */
} code_t; } code_t;
typedef int (*decodeFunc)(TIFF*, tidata_t, tsize_t, uint16); typedef int (*decodeFunc)(TIFF*, uint8*, tmsize_t, uint16);
typedef struct { typedef struct {
LZWBaseState base; LZWBaseState base;
/* Decoding specific data */ /* Decoding specific data */
long dec_nbitsmask; /* lzw_nbits 1 bits, right adjusted */ long dec_nbitsmask; /* lzw_nbits 1 bits, right adjusted */
long dec_restart; /* restart count */ long dec_restart; /* restart count */
#ifdef LZW_CHECKEOS #ifdef LZW_CHECKEOS
long dec_bitsleft; /* available bits in raw data */ tmsize_t dec_bitsleft; /* available bits in raw data */
#endif #endif
decodeFunc dec_decode; /* regular or backwards compatible */ decodeFunc dec_decode; /* regular or backwards compatible */
code_t* dec_codep; /* current recognized code */ code_t* dec_codep; /* current recognized code */
code_t* dec_oldcodep; /* previously recognized code */ code_t* dec_oldcodep; /* previously recognized code */
code_t* dec_free_entp; /* next free entry */ code_t* dec_free_entp; /* next free entry */
code_t* dec_maxcodep; /* max available entry */ code_t* dec_maxcodep; /* max available entry */
code_t* dec_codetab; /* kept separate for small machines */ code_t* dec_codetab; /* kept separate for small machines */
/* Encoding specific data */ /* Encoding specific data */
int enc_oldcode; /* last code encountered */ int enc_oldcode; /* last code encountered */
long enc_checkpoint; /* point at which to clear table */ long enc_checkpoint; /* point at which to clear table */
#define CHECK_GAP 10000 /* enc_ratio check interval */ #define CHECK_GAP 10000 /* enc_ratio check interval */
long enc_ratio; /* current compression ratio */ long enc_ratio; /* current compression ratio */
long enc_incount; /* (input) data bytes encoded */ long enc_incount; /* (input) data bytes encoded */
long enc_outcount; /* encoded (output) bytes */ long enc_outcount; /* encoded (output) bytes */
tidata_t enc_rawlimit; /* bound on tif_rawdata buffer */ uint8* enc_rawlimit; /* bound on tif_rawdata buffer */
hash_t* enc_hashtab; /* kept separate for small machines */ hash_t* enc_hashtab; /* kept separate for small machines */
} LZWCodecState; } LZWCodecState;
#define LZWState(tif) ((LZWBaseState*) (tif)->tif_data) #define LZWState(tif) ((LZWBaseState*) (tif)->tif_data)
#define DecoderState(tif) ((LZWCodecState*) LZWState(tif)) #define DecoderState(tif) ((LZWCodecState*) LZWState(tif))
#define EncoderState(tif) ((LZWCodecState*) LZWState(tif)) #define EncoderState(tif) ((LZWCodecState*) LZWState(tif))
static int LZWDecode(TIFF*, tidata_t, tsize_t, uint16); static int LZWDecode(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s);
#ifdef LZW_COMPAT #ifdef LZW_COMPAT
static int LZWDecodeCompat(TIFF*, tidata_t, tsize_t, uint16); static int LZWDecodeCompat(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s);
#endif #endif
static void cl_hash(LZWCodecState*); static void cl_hash(LZWCodecState*);
/* /*
* LZW Decoder. * LZW Decoder.
@ -174,8 +174,8 @@ static void cl_hash(LZWCodecState*);
* strip is suppose to be terminated with CODE_EOI. * strip is suppose to be terminated with CODE_EOI.
*/ */
#define NextCode(_tif, _sp, _bp, _code, _get) { \ #define NextCode(_tif, _sp, _bp, _code, _get) { \
if ((_sp)->dec_bitsleft < nbits) { \ if ((_sp)->dec_bitsleft < (tmsize_t)nbits) { \
TIFFWarningExt(_tif->tif_clientdata, _tif->tif_name, \ TIFFWarningExt(_tif->tif_clientdata, module, \
"LZWDecode: Strip %d not terminated with EOI code", \ "LZWDecode: Strip %d not terminated with EOI code", \
_tif->tif_curstrip); \ _tif->tif_curstrip); \
_code = CODE_EOI; \ _code = CODE_EOI; \
@ -191,34 +191,34 @@ static void cl_hash(LZWCodecState*);
static int static int
LZWSetupDecode(TIFF* tif) LZWSetupDecode(TIFF* tif)
{ {
static const char module[] = "LZWSetupDecode";
LZWCodecState* sp = DecoderState(tif); LZWCodecState* sp = DecoderState(tif);
static const char module[] = " LZWSetupDecode";
int code; int code;
if( sp == NULL ) if( sp == NULL )
{ {
/* /*
* Allocate state block so tag methods have storage to record * Allocate state block so tag methods have storage to record
* values. * values.
*/ */
tif->tif_data = (tidata_t) _TIFFmalloc(sizeof(LZWCodecState)); tif->tif_data = (uint8*) _TIFFmalloc(sizeof(LZWCodecState));
if (tif->tif_data == NULL) if (tif->tif_data == NULL)
{ {
TIFFErrorExt(tif->tif_clientdata, "LZWPreDecode", "No space for LZW state block"); TIFFErrorExt(tif->tif_clientdata, module, "No space for LZW state block");
return (0); return (0);
} }
DecoderState(tif)->dec_codetab = NULL; DecoderState(tif)->dec_codetab = NULL;
DecoderState(tif)->dec_decode = NULL; DecoderState(tif)->dec_decode = NULL;
/* /*
* Setup predictor setup. * Setup predictor setup.
*/ */
(void) TIFFPredictorInit(tif); (void) TIFFPredictorInit(tif);
sp = DecoderState(tif);
}
sp = DecoderState(tif);
}
assert(sp != NULL); assert(sp != NULL);
if (sp->dec_codetab == NULL) { if (sp->dec_codetab == NULL) {
@ -230,13 +230,13 @@ LZWSetupDecode(TIFF* tif)
/* /*
* Pre-load the table. * Pre-load the table.
*/ */
code = 255; code = 255;
do { do {
sp->dec_codetab[code].value = code; sp->dec_codetab[code].value = code;
sp->dec_codetab[code].firstchar = code; sp->dec_codetab[code].firstchar = code;
sp->dec_codetab[code].length = 1; sp->dec_codetab[code].length = 1;
sp->dec_codetab[code].next = NULL; sp->dec_codetab[code].next = NULL;
} while (code--); } while (code--);
} }
return (1); return (1);
} }
@ -247,12 +247,13 @@ LZWSetupDecode(TIFF* tif)
static int static int
LZWPreDecode(TIFF* tif, uint16 s) LZWPreDecode(TIFF* tif, uint16 s)
{ {
static const char module[] = "LZWPreDecode";
LZWCodecState *sp = DecoderState(tif); LZWCodecState *sp = DecoderState(tif);
(void) s; (void) s;
assert(sp != NULL); assert(sp != NULL);
if( sp->dec_codetab == NULL ) if( sp->dec_codetab == NULL )
LZWSetupDecode( tif ); LZWSetupDecode( tif );
/* /*
* Check for old bit-reversed codes. * Check for old bit-reversed codes.
@ -260,7 +261,7 @@ LZWPreDecode(TIFF* tif, uint16 s)
if (tif->tif_rawdata[0] == 0 && (tif->tif_rawdata[1] & 0x1)) { if (tif->tif_rawdata[0] == 0 && (tif->tif_rawdata[1] & 0x1)) {
#ifdef LZW_COMPAT #ifdef LZW_COMPAT
if (!sp->dec_decode) { if (!sp->dec_decode) {
TIFFWarningExt(tif->tif_clientdata, tif->tif_name, TIFFWarningExt(tif->tif_clientdata, module,
"Old-style LZW codes, convert file"); "Old-style LZW codes, convert file");
/* /*
* Override default decoding methods with * Override default decoding methods with
@ -269,9 +270,9 @@ LZWPreDecode(TIFF* tif, uint16 s)
* above will call the compatibility routines * above will call the compatibility routines
* through the dec_decode method. * through the dec_decode method.
*/ */
tif->tif_decoderow = LZWDecodeCompat; ddd tif->tif_decoderow = LZWDecodeCompat;
tif->tif_decodestrip = LZWDecodeCompat; ddd tif->tif_decodestrip = LZWDecodeCompat;
tif->tif_decodetile = LZWDecodeCompat; ddd tif->tif_decodetile = LZWDecodeCompat;
/* /*
* If doing horizontal differencing, must * If doing horizontal differencing, must
* re-setup the predictor logic since we * re-setup the predictor logic since we
@ -283,7 +284,7 @@ LZWPreDecode(TIFF* tif, uint16 s)
sp->lzw_maxcode = MAXCODE(BITS_MIN); sp->lzw_maxcode = MAXCODE(BITS_MIN);
#else /* !LZW_COMPAT */ #else /* !LZW_COMPAT */
if (!sp->dec_decode) { if (!sp->dec_decode) {
TIFFErrorExt(tif->tif_clientdata, tif->tif_name, TIFFErrorExt(tif->tif_clientdata, module,
"Old-style LZW codes not supported"); "Old-style LZW codes not supported");
sp->dec_decode = LZWDecode; sp->dec_decode = LZWDecode;
} }
@ -300,7 +301,7 @@ LZWPreDecode(TIFF* tif, uint16 s)
sp->dec_restart = 0; sp->dec_restart = 0;
sp->dec_nbitsmask = MAXCODE(BITS_MIN); sp->dec_nbitsmask = MAXCODE(BITS_MIN);
#ifdef LZW_CHECKEOS #ifdef LZW_CHECKEOS
sp->dec_bitsleft = tif->tif_rawcc << 3; ddd sp->dec_bitsleft = tif->tif_rawcc << 3;
#endif #endif
sp->dec_free_entp = sp->dec_codetab + CODE_FIRST; sp->dec_free_entp = sp->dec_codetab + CODE_FIRST;
/* /*
@ -331,19 +332,20 @@ LZWPreDecode(TIFF* tif, uint16 s)
} }
static void static void
codeLoop(TIFF* tif) codeLoop(TIFF* tif, const char* module)
{ {
TIFFErrorExt(tif->tif_clientdata, tif->tif_name, TIFFErrorExt(tif->tif_clientdata, module,
"LZWDecode: Bogus encoding, loop in the code table; scanline %d", "Bogus encoding, loop in the code table; scanline %d",
tif->tif_row); tif->tif_row);
} }
static int static int
LZWDecode(TIFF* tif, tidata_t op0, tsize_t occ0, uint16 s) LZWDecode(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s)
{ {
static const char module[] = "LZWDecode";
LZWCodecState *sp = DecoderState(tif); LZWCodecState *sp = DecoderState(tif);
char *op = (char*) op0; char *op = (char*) op0;
long occ = (long) occ0; tmsize_t occ = occ0;
char *tp; char *tp;
unsigned char *bp; unsigned char *bp;
hcode_t code; hcode_t code;
@ -362,7 +364,7 @@ LZWDecode(TIFF* tif, tidata_t op0, tsize_t occ0, uint16 s)
codep = sp->dec_codep; codep = sp->dec_codep;
residue = codep->length - sp->dec_restart; residue = codep->length - sp->dec_restart;
if (residue > occ) { if ((tmsize_t)residue > occ) {
/* /*
* Residue from previous decode is sufficient * Residue from previous decode is sufficient
* to satisfy decode request. Skip to the * to satisfy decode request. Skip to the
@ -372,7 +374,7 @@ LZWDecode(TIFF* tif, tidata_t op0, tsize_t occ0, uint16 s)
sp->dec_restart += occ; sp->dec_restart += occ;
do { do {
codep = codep->next; codep = codep->next;
} while (--residue > occ && codep); } while ((tmsize_t)(--residue) > occ && codep);
if (codep) { if (codep) {
tp = op + occ; tp = op + occ;
do { do {
@ -425,22 +427,22 @@ LZWDecode(TIFF* tif, tidata_t op0, tsize_t occ0, uint16 s)
codep = sp->dec_codetab + code; codep = sp->dec_codetab + code;
/* /*
* Add the new entry to the code table. * Add the new entry to the code table.
*/ */
if (free_entp < &sp->dec_codetab[0] || if (free_entp < &sp->dec_codetab[0] ||
free_entp >= &sp->dec_codetab[CSIZE]) { free_entp >= &sp->dec_codetab[CSIZE]) {
TIFFErrorExt(tif->tif_clientdata, tif->tif_name, TIFFErrorExt(tif->tif_clientdata, module,
"LZWDecode: Corrupted LZW table at scanline %d", "Corrupted LZW table at scanline %d",
tif->tif_row); tif->tif_row);
return (0); return (0);
} }
free_entp->next = oldcodep; free_entp->next = oldcodep;
if (free_entp->next < &sp->dec_codetab[0] || if (free_entp->next < &sp->dec_codetab[0] ||
free_entp->next >= &sp->dec_codetab[CSIZE]) { free_entp->next >= &sp->dec_codetab[CSIZE]) {
TIFFErrorExt(tif->tif_clientdata, tif->tif_name, TIFFErrorExt(tif->tif_clientdata, module,
"LZWDecode: Corrupted LZW table at scanline %d", "Corrupted LZW table at scanline %d",
tif->tif_row); tif->tif_row);
return (0); return (0);
} }
free_entp->firstchar = free_entp->next->firstchar; free_entp->firstchar = free_entp->next->firstchar;
@ -456,17 +458,17 @@ LZWDecode(TIFF* tif, tidata_t op0, tsize_t occ0, uint16 s)
oldcodep = codep; oldcodep = codep;
if (code >= 256) { if (code >= 256) {
/* /*
* Code maps to a string, copy string * Code maps to a string, copy string
* value to output (written in reverse). * value to output (written in reverse).
*/ */
if(codep->length == 0) { if(codep->length == 0) {
TIFFErrorExt(tif->tif_clientdata, tif->tif_name, TIFFErrorExt(tif->tif_clientdata, module,
"LZWDecode: Wrong length of decoded string: " "Wrong length of decoded string: "
"data probably corrupted at scanline %d", "data probably corrupted at scanline %d",
tif->tif_row); tif->tif_row);
return (0); return (0);
} }
if (codep->length > occ) { if ((tmsize_t)(codep->length) > occ) {
/* /*
* String is too long for decode buffer, * String is too long for decode buffer,
* locate portion that will fit, copy to * locate portion that will fit, copy to
@ -476,16 +478,16 @@ LZWDecode(TIFF* tif, tidata_t op0, tsize_t occ0, uint16 s)
sp->dec_codep = codep; sp->dec_codep = codep;
do { do {
codep = codep->next; codep = codep->next;
} while (codep && codep->length > occ); } while (codep && (tmsize_t)(codep->length) > occ);
if (codep) { if (codep) {
sp->dec_restart = occ; sp->dec_restart = (long)occ;
tp = op + occ; tp = op + occ;
do { do {
*--tp = codep->value; *--tp = codep->value;
codep = codep->next; codep = codep->next;
} while (--occ && codep); } while (--occ && codep);
if (codep) if (codep)
codeLoop(tif); codeLoop(tif, module);
} }
break; break;
} }
@ -499,15 +501,16 @@ LZWDecode(TIFF* tif, tidata_t op0, tsize_t occ0, uint16 s)
*tp = t; *tp = t;
} while (codep && tp > op); } while (codep && tp > op);
if (codep) { if (codep) {
codeLoop(tif); codeLoop(tif, module);
break; break;
} }
assert(occ>=(tmsize_t)len);
op += len, occ -= len; op += len, occ -= len;
} else } else
*op++ = (char)code, occ--; *op++ = (char)code, occ--;
} }
tif->tif_rawcp = (tidata_t) bp; tif->tif_rawcp = (uint8*) bp;
sp->lzw_nbits = (unsigned short) nbits; sp->lzw_nbits = (unsigned short) nbits;
sp->lzw_nextdata = nextdata; sp->lzw_nextdata = nextdata;
sp->lzw_nextbits = nextbits; sp->lzw_nextbits = nextbits;
@ -517,9 +520,9 @@ LZWDecode(TIFF* tif, tidata_t op0, tsize_t occ0, uint16 s)
sp->dec_maxcodep = maxcodep; sp->dec_maxcodep = maxcodep;
if (occ > 0) { if (occ > 0) {
TIFFErrorExt(tif->tif_clientdata, tif->tif_name, TIFFErrorExt(tif->tif_clientdata, module,
"LZWDecode: Not enough data at scanline %d (short %d bytes)", "Not enough data at scanline %d (short %llud bytes)",
tif->tif_row, occ); tif->tif_row, (unsigned long long) occ);
return (0); return (0);
} }
return (1); return (1);
@ -542,11 +545,12 @@ LZWDecode(TIFF* tif, tidata_t op0, tsize_t occ0, uint16 s)
} }
static int static int
LZWDecodeCompat(TIFF* tif, tidata_t op0, tsize_t occ0, uint16 s) LZWDecodeCompat(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s)
{ {
static const char module[] = "LZWDecodeCompat";
LZWCodecState *sp = DecoderState(tif); LZWCodecState *sp = DecoderState(tif);
char *op = (char*) op0; char *op = (char*) op0;
long occ = (long) occ0; tmsize_t occ = occ0;
char *tp; char *tp;
unsigned char *bp; unsigned char *bp;
int code, nbits; int code, nbits;
@ -563,7 +567,7 @@ LZWDecodeCompat(TIFF* tif, tidata_t op0, tsize_t occ0, uint16 s)
codep = sp->dec_codep; codep = sp->dec_codep;
residue = codep->length - sp->dec_restart; residue = codep->length - sp->dec_restart;
if (residue > occ) { if ((tmsize_t)residue > occ) {
/* /*
* Residue from previous decode is sufficient * Residue from previous decode is sufficient
* to satisfy decode request. Skip to the * to satisfy decode request. Skip to the
@ -573,7 +577,7 @@ LZWDecodeCompat(TIFF* tif, tidata_t op0, tsize_t occ0, uint16 s)
sp->dec_restart += occ; sp->dec_restart += occ;
do { do {
codep = codep->next; codep = codep->next;
} while (--residue > occ); } while ((tmsize_t)(--residue) > occ);
tp = op + occ; tp = op + occ;
do { do {
*--tp = codep->value; *--tp = codep->value;
@ -624,19 +628,17 @@ LZWDecodeCompat(TIFF* tif, tidata_t op0, tsize_t occ0, uint16 s)
* Add the new entry to the code table. * Add the new entry to the code table.
*/ */
if (free_entp < &sp->dec_codetab[0] || if (free_entp < &sp->dec_codetab[0] ||
free_entp >= &sp->dec_codetab[CSIZE]) { free_entp >= &sp->dec_codetab[CSIZE]) {
TIFFErrorExt(tif->tif_clientdata, tif->tif_name, TIFFErrorExt(tif->tif_clientdata, module,
"LZWDecodeCompat: Corrupted LZW table at scanline %d", "Corrupted LZW table at scanline %d", tif->tif_row);
tif->tif_row);
return (0); return (0);
} }
free_entp->next = oldcodep; free_entp->next = oldcodep;
if (free_entp->next < &sp->dec_codetab[0] || if (free_entp->next < &sp->dec_codetab[0] ||
free_entp->next >= &sp->dec_codetab[CSIZE]) { free_entp->next >= &sp->dec_codetab[CSIZE]) {
TIFFErrorExt(tif->tif_clientdata, tif->tif_name, TIFFErrorExt(tif->tif_clientdata, module,
"LZWDecodeCompat: Corrupted LZW table at scanline %d", "Corrupted LZW table at scanline %d", tif->tif_row);
tif->tif_row);
return (0); return (0);
} }
free_entp->firstchar = free_entp->next->firstchar; free_entp->firstchar = free_entp->next->firstchar;
@ -652,17 +654,17 @@ LZWDecodeCompat(TIFF* tif, tidata_t op0, tsize_t occ0, uint16 s)
oldcodep = codep; oldcodep = codep;
if (code >= 256) { if (code >= 256) {
/* /*
* Code maps to a string, copy string * Code maps to a string, copy string
* value to output (written in reverse). * value to output (written in reverse).
*/ */
if(codep->length == 0) { if(codep->length == 0) {
TIFFErrorExt(tif->tif_clientdata, tif->tif_name, TIFFErrorExt(tif->tif_clientdata, module,
"LZWDecodeCompat: Wrong length of decoded " "Wrong length of decoded "
"string: data probably corrupted at scanline %d", "string: data probably corrupted at scanline %d",
tif->tif_row); tif->tif_row);
return (0); return (0);
} }
if (codep->length > occ) { if ((tmsize_t)(codep->length) > occ) {
/* /*
* String is too long for decode buffer, * String is too long for decode buffer,
* locate portion that will fit, copy to * locate portion that will fit, copy to
@ -672,7 +674,7 @@ LZWDecodeCompat(TIFF* tif, tidata_t op0, tsize_t occ0, uint16 s)
sp->dec_codep = codep; sp->dec_codep = codep;
do { do {
codep = codep->next; codep = codep->next;
} while (codep->length > occ); } while ((tmsize_t)(codep->length) > occ);
sp->dec_restart = occ; sp->dec_restart = occ;
tp = op + occ; tp = op + occ;
do { do {
@ -681,6 +683,7 @@ LZWDecodeCompat(TIFF* tif, tidata_t op0, tsize_t occ0, uint16 s)
} while (--occ); } while (--occ);
break; break;
} }
assert(occ>=(tmsize_t)(codep->length));
op += codep->length, occ -= codep->length; op += codep->length, occ -= codep->length;
tp = op; tp = op;
do { do {
@ -690,7 +693,7 @@ LZWDecodeCompat(TIFF* tif, tidata_t op0, tsize_t occ0, uint16 s)
*op++ = code, occ--; *op++ = code, occ--;
} }
tif->tif_rawcp = (tidata_t) bp; tif->tif_rawcp = (uint8*) bp;
sp->lzw_nbits = nbits; sp->lzw_nbits = nbits;
sp->lzw_nextdata = nextdata; sp->lzw_nextdata = nextdata;
sp->lzw_nextbits = nextbits; sp->lzw_nextbits = nextbits;
@ -700,9 +703,9 @@ LZWDecodeCompat(TIFF* tif, tidata_t op0, tsize_t occ0, uint16 s)
sp->dec_maxcodep = maxcodep; sp->dec_maxcodep = maxcodep;
if (occ > 0) { if (occ > 0) {
TIFFErrorExt(tif->tif_clientdata, tif->tif_name, TIFFErrorExt(tif->tif_clientdata, module,
"LZWDecodeCompat: Not enough data at scanline %d (short %d bytes)", "Not enough data at scanline %d (short %llud bytes)",
tif->tif_row, occ); tif->tif_row, (unsigned long long) occ);
return (0); return (0);
} }
return (1); return (1);
@ -716,8 +719,8 @@ LZWDecodeCompat(TIFF* tif, tidata_t op0, tsize_t occ0, uint16 s)
static int static int
LZWSetupEncode(TIFF* tif) LZWSetupEncode(TIFF* tif)
{ {
LZWCodecState* sp = EncoderState(tif);
static const char module[] = "LZWSetupEncode"; static const char module[] = "LZWSetupEncode";
LZWCodecState* sp = EncoderState(tif);
assert(sp != NULL); assert(sp != NULL);
sp->enc_hashtab = (hash_t*) _TIFFmalloc(HSIZE*sizeof (hash_t)); sp->enc_hashtab = (hash_t*) _TIFFmalloc(HSIZE*sizeof (hash_t));
@ -738,9 +741,9 @@ LZWPreEncode(TIFF* tif, uint16 s)
(void) s; (void) s;
assert(sp != NULL); assert(sp != NULL);
if( sp->enc_hashtab == NULL ) if( sp->enc_hashtab == NULL )
LZWSetupEncode( tif ); LZWSetupEncode( tif );
sp->lzw_nbits = BITS_MIN; sp->lzw_nbits = BITS_MIN;
sp->lzw_maxcode = MAXCODE(BITS_MIN); sp->lzw_maxcode = MAXCODE(BITS_MIN);
@ -755,7 +758,7 @@ LZWPreEncode(TIFF* tif, uint16 s)
* The 4 here insures there is space for 2 max-sized * The 4 here insures there is space for 2 max-sized
* codes in LZWEncode and LZWPostDecode. * codes in LZWEncode and LZWPostDecode.
*/ */
sp->enc_rawlimit = tif->tif_rawdata + tif->tif_rawdatasize-1 - 4; ddd sp->enc_rawlimit = tif->tif_rawdata + tif->tif_rawdatasize-1 - 4;
cl_hash(sp); /* clear hash table */ cl_hash(sp); /* clear hash table */
sp->enc_oldcode = (hcode_t) -1; /* generates CODE_CLEAR in LZWEncode */ sp->enc_oldcode = (hcode_t) -1; /* generates CODE_CLEAR in LZWEncode */
return (1); return (1);
@ -795,7 +798,7 @@ LZWPreEncode(TIFF* tif, uint16 s)
* for the decoder. * for the decoder.
*/ */
static int static int
LZWEncode(TIFF* tif, tidata_t bp, tsize_t cc, uint16 s) LZWEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
{ {
register LZWCodecState *sp = EncoderState(tif); register LZWCodecState *sp = EncoderState(tif);
register long fcode; register long fcode;
@ -806,7 +809,8 @@ LZWEncode(TIFF* tif, tidata_t bp, tsize_t cc, uint16 s)
long incount, outcount, checkpoint; long incount, outcount, checkpoint;
long nextdata, nextbits; long nextdata, nextbits;
int free_ent, maxcode, nbits; int free_ent, maxcode, nbits;
tidata_t op, limit; uint8* op;
uint8* limit;
(void) s; (void) s;
if (sp == NULL) if (sp == NULL)
@ -885,7 +889,7 @@ LZWEncode(TIFF* tif, tidata_t bp, tsize_t cc, uint16 s)
* are at least 4 bytes free--room for 2 codes. * are at least 4 bytes free--room for 2 codes.
*/ */
if (op > limit) { if (op > limit) {
tif->tif_rawcc = (tsize_t)(op - tif->tif_rawdata); ddd tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata);
TIFFFlushData1(tif); TIFFFlushData1(tif);
op = tif->tif_rawdata; op = tif->tif_rawdata;
} }
@ -963,14 +967,14 @@ static int
LZWPostEncode(TIFF* tif) LZWPostEncode(TIFF* tif)
{ {
register LZWCodecState *sp = EncoderState(tif); register LZWCodecState *sp = EncoderState(tif);
tidata_t op = tif->tif_rawcp; uint8* op = tif->tif_rawcp;
long nextbits = sp->lzw_nextbits; long nextbits = sp->lzw_nextbits;
long nextdata = sp->lzw_nextdata; long nextdata = sp->lzw_nextdata;
long outcount = sp->enc_outcount; long outcount = sp->enc_outcount;
int nbits = sp->lzw_nbits; int nbits = sp->lzw_nbits;
if (op > sp->enc_rawlimit) { if (op > sp->enc_rawlimit) {
tif->tif_rawcc = (tsize_t)(op - tif->tif_rawdata); ddd tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata);
TIFFFlushData1(tif); TIFFFlushData1(tif);
op = tif->tif_rawdata; op = tif->tif_rawdata;
} }
@ -981,7 +985,7 @@ LZWPostEncode(TIFF* tif)
PutNextCode(op, CODE_EOI); PutNextCode(op, CODE_EOI);
if (nextbits > 0) if (nextbits > 0)
*op++ = (unsigned char)(nextdata << (8-nextbits)); *op++ = (unsigned char)(nextdata << (8-nextbits));
tif->tif_rawcc = (tsize_t)(op - tif->tif_rawdata); ddd tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata);
return (1); return (1);
} }
@ -994,7 +998,7 @@ cl_hash(LZWCodecState* sp)
register hash_t *hp = &sp->enc_hashtab[HSIZE-1]; register hash_t *hp = &sp->enc_hashtab[HSIZE-1];
register long i = HSIZE-8; register long i = HSIZE-8;
do { do {
i -= 8; i -= 8;
hp[-7].hash = -1; hp[-7].hash = -1;
hp[-6].hash = -1; hp[-6].hash = -1;
@ -1006,7 +1010,7 @@ cl_hash(LZWCodecState* sp)
hp[ 0].hash = -1; hp[ 0].hash = -1;
hp -= 8; hp -= 8;
} while (i >= 0); } while (i >= 0);
for (i += 8; i > 0; i--, hp--) for (i += 8; i > 0; i--, hp--)
hp->hash = -1; hp->hash = -1;
} }
@ -1032,11 +1036,12 @@ LZWCleanup(TIFF* tif)
int int
TIFFInitLZW(TIFF* tif, int scheme) TIFFInitLZW(TIFF* tif, int scheme)
{ {
static const char module[] = "TIFFInitLZW";
assert(scheme == COMPRESSION_LZW); assert(scheme == COMPRESSION_LZW);
/* /*
* 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 (LZWCodecState)); tif->tif_data = (uint8*) _TIFFmalloc(sizeof (LZWCodecState));
if (tif->tif_data == NULL) if (tif->tif_data == NULL)
goto bad; goto bad;
DecoderState(tif)->dec_codetab = NULL; DecoderState(tif)->dec_codetab = NULL;
@ -1049,15 +1054,15 @@ TIFFInitLZW(TIFF* tif, int scheme)
*/ */
tif->tif_setupdecode = LZWSetupDecode; tif->tif_setupdecode = LZWSetupDecode;
tif->tif_predecode = LZWPreDecode; tif->tif_predecode = LZWPreDecode;
tif->tif_decoderow = LZWDecode; ddd tif->tif_decoderow = LZWDecode;
tif->tif_decodestrip = LZWDecode; ddd tif->tif_decodestrip = LZWDecode;
tif->tif_decodetile = LZWDecode; ddd tif->tif_decodetile = LZWDecode;
tif->tif_setupencode = LZWSetupEncode; tif->tif_setupencode = LZWSetupEncode;
tif->tif_preencode = LZWPreEncode; tif->tif_preencode = LZWPreEncode;
tif->tif_postencode = LZWPostEncode; tif->tif_postencode = LZWPostEncode;
tif->tif_encoderow = LZWEncode; ddd tif->tif_encoderow = LZWEncode;
tif->tif_encodestrip = LZWEncode; ddd tif->tif_encodestrip = LZWEncode;
tif->tif_encodetile = LZWEncode; ddd tif->tif_encodetile = LZWEncode;
tif->tif_cleanup = LZWCleanup; tif->tif_cleanup = LZWCleanup;
/* /*
* Setup predictor setup. * Setup predictor setup.
@ -1065,7 +1070,7 @@ TIFFInitLZW(TIFF* tif, int scheme)
(void) TIFFPredictorInit(tif); (void) TIFFPredictorInit(tif);
return (1); return (1);
bad: bad:
TIFFErrorExt(tif->tif_clientdata, "TIFFInitLZW", TIFFErrorExt(tif->tif_clientdata, module,
"No space for LZW state block"); "No space for LZW state block");
return (0); return (0);
} }