45#define SIZEOF_WORDTYPE SIZEOF_SIZE_T
46typedef size_t WordType;
63#define MAXCODE(n) ((1L << (n)) - 1)
74#define CODE_MAX MAXCODE(BITS_MAX)
76#define HSHIFT (13 - 8)
79#define CSIZE (MAXCODE(BITS_MAX) + 1024L)
81#define CSIZE (MAXCODE(BITS_MAX) + 1L)
94 unsigned short maxcode;
95 unsigned short free_ent;
102#define lzw_nbits base.nbits
103#define lzw_maxcode base.maxcode
104#define lzw_free_ent base.free_ent
105#define lzw_nextdata base.nextdata
106#define lzw_nextbits base.nextbits
121typedef struct code_ent
123 struct code_ent *
next;
126 unsigned char firstchar;
143 decodeFunc dec_decode;
145 code_t *dec_oldcodep;
146 code_t *dec_free_entp;
147 code_t *dec_maxcodep;
155#define CHECK_GAP 10000
163#define LZWState(tif) ((LZWBaseState *)(tif)->tif_data)
164#define LZWDecoderState(tif) ((LZWCodecState *)LZWState(tif))
165#define LZWEncoderState(tif) ((LZWCodecState *)LZWState(tif))
176static int LZWFixupTags(
TIFF *tif)
182static int LZWSetupDecode(
TIFF *tif)
184 static const char module[] =
"LZWSetupDecode";
185 LZWCodecState *
sp = LZWDecoderState(tif);
201 sp = LZWDecoderState(tif);
211 if (
sp->dec_codetab ==
NULL)
214 if (
sp->dec_codetab ==
NULL)
227 sp->dec_codetab[
code].repeated =
true;
228 sp->dec_codetab[
code].length = 1;
235 memset(&
sp->dec_codetab[CODE_CLEAR], 0,
236 (CODE_FIRST - CODE_CLEAR) *
sizeof(code_t));
246 static const char module[] =
"LZWPreDecode";
247 LZWCodecState *
sp = LZWDecoderState(tif);
251 if (
sp->dec_codetab ==
NULL)
254 if (
sp->dec_codetab ==
NULL)
284 sp->dec_decode = LZWDecodeCompat;
286 sp->lzw_maxcode = MAXCODE(BITS_MIN);
291 sp->dec_decode = LZWDecode;
298 sp->lzw_maxcode = MAXCODE(BITS_MIN) - 1;
299 sp->dec_decode = LZWDecode;
301 sp->lzw_nbits = BITS_MIN;
302 sp->lzw_nextbits = 0;
303 sp->lzw_nextdata = 0;
306 sp->dec_nbitsmask = MAXCODE(BITS_MIN);
307 sp->dec_bitsleft = 0;
308 sp->old_tif_rawcc = 0;
309 sp->dec_free_entp =
sp->dec_codetab - 1;
317 sp->dec_oldcodep = &
sp->dec_codetab[0];
318 sp->dec_maxcodep = &
sp->dec_codetab[
sp->dec_nbitsmask - 1];
328#ifdef WORDS_BIGENDIAN
329#define GetNextData(nextdata, bp) memcpy(&nextdata, bp, sizeof(nextdata))
330#elif SIZEOF_WORDTYPE == 8
332#define GetNextData(nextdata, bp) nextdata = _byteswap_uint64(*(uint64_t *)(bp))
333#elif defined(__GNUC__)
334#define GetNextData(nextdata, bp) \
335 memcpy(&nextdata, bp, sizeof(nextdata)); \
336 nextdata = __builtin_bswap64(nextdata)
338#define GetNextData(nextdata, bp) \
339 nextdata = (((uint64_t)bp[0]) << 56) | (((uint64_t)bp[1]) << 48) | \
340 (((uint64_t)bp[2]) << 40) | (((uint64_t)bp[3]) << 32) | \
341 (((uint64_t)bp[4]) << 24) | (((uint64_t)bp[5]) << 16) | \
342 (((uint64_t)bp[6]) << 8) | (((uint64_t)bp[7]))
344#elif SIZEOF_WORDTYPE == 4
346#define GetNextData(nextdata, bp) \
347 nextdata = _byteswap_ulong(*(unsigned long *)(bp))
348#elif defined(__GNUC__)
349#define GetNextData(nextdata, bp) \
350 memcpy(&nextdata, bp, sizeof(nextdata)); \
351 nextdata = __builtin_bswap32(nextdata)
353#define GetNextData(nextdata, bp) \
354 nextdata = (((uint32_t)bp[0]) << 24) | (((uint32_t)bp[1]) << 16) | \
355 (((uint32_t)bp[2]) << 8) | (((uint32_t)bp[3]))
358#error "Unhandled SIZEOF_WORDTYPE"
361#define GetNextCodeLZW() \
367 if (dec_bitsleft >= 8 * SIZEOF_WORDTYPE) \
369 unsigned codetmp = (unsigned)(nextdata << (-nextbits)); \
370 GetNextData(nextdata, bp); \
371 bp += SIZEOF_WORDTYPE; \
372 nextbits += 8 * SIZEOF_WORDTYPE; \
373 dec_bitsleft -= 8 * SIZEOF_WORDTYPE; \
374 code = (WordType)((codetmp | (nextdata >> nextbits)) & \
380 if (dec_bitsleft < 8) \
384 nextdata = (nextdata << 8) | *(bp)++; \
389 if (dec_bitsleft < 8) \
393 nextdata = (nextdata << 8) | *(bp)++; \
399 code = (WordType)((nextdata >> nextbits) & nbitsmask); \
404 static const char module[] =
"LZWDecode";
405 LZWCodecState *
sp = LZWDecoderState(tif);
409 long nbits, nextbits, nbitsmask;
411 code_t *free_entp, *maxcodep, *oldcodep;
421 "LZWDecode: Scanline %" PRIu32 " cannot be read due to "
435 residue =
codep->length -
sp->dec_restart;
444 sp->dec_restart += occ;
448 }
while (--residue > occ &&
codep);
456 }
while (--occ &&
codep);
470 }
while (--residue &&
codep);
477 nbits =
sp->lzw_nbits;
478 nextdata =
sp->lzw_nextdata;
479 nextbits =
sp->lzw_nextbits;
480 nbitsmask =
sp->dec_nbitsmask;
481 oldcodep =
sp->dec_oldcodep;
482 free_entp =
sp->dec_free_entp;
483 maxcodep =
sp->dec_maxcodep;
484 code_t *
const dec_codetab =
sp->dec_codetab;
497 if (
code >= CODE_FIRST)
498 goto code_above_or_equal_to_258;
501 if (
code == CODE_EOI)
507 if (
codep > free_entp)
509 free_entp->next = oldcodep;
510 free_entp->firstchar = oldcodep->firstchar;
511 free_entp->length = oldcodep->length + 1;
513 free_entp->repeated =
514 (
bool)(oldcodep->repeated & (oldcodep->value ==
code));
515 if (++free_entp > maxcodep)
517 if (++nbits > BITS_MAX)
519 nbitsmask = MAXCODE(nbits);
520 maxcodep = dec_codetab + nbitsmask - 1;
521 if (free_entp >= &dec_codetab[CSIZE])
527 free_entp = dec_codetab - 1;
538code_above_or_equal_to_258:
544 if (
codep >= free_entp)
546 if (
codep != free_entp)
548 free_entp->value = oldcodep->firstchar;
552 free_entp->value =
codep->firstchar;
554 free_entp->repeated =
555 (
bool)(oldcodep->repeated & (oldcodep->value == free_entp->value));
556 free_entp->next = oldcodep;
558 free_entp->firstchar = oldcodep->firstchar;
559 free_entp->length = oldcodep->length + 1;
560 if (++free_entp > maxcodep)
562 if (++nbits > BITS_MAX)
564 nbitsmask = MAXCODE(nbits);
565 maxcodep = dec_codetab + nbitsmask - 1;
566 if (free_entp >= &dec_codetab[CSIZE])
572 free_entp = dec_codetab - 1;
595 goto too_short_buffer;
617 goto too_short_buffer;
630 goto too_short_buffer;
673 free_entp = dec_codetab + CODE_FIRST;
675 nbitsmask = MAXCODE(BITS_MIN);
676 maxcodep = dec_codetab + nbitsmask - 1;
680 }
while (
code == CODE_CLEAR);
681 if (
code == CODE_EOI)
689 oldcodep = dec_codetab +
code;
708 }
while (
codep->length > occ);
710 sp->dec_restart = occ;
723 sp->dec_bitsleft = dec_bitsleft;
724 sp->lzw_nbits = (
unsigned short)nbits;
725 sp->lzw_nextdata = nextdata;
726 sp->lzw_nextbits = nextbits;
727 sp->dec_nbitsmask = nbitsmask;
728 sp->dec_oldcodep = oldcodep;
729 sp->dec_free_entp = free_entp;
730 sp->dec_maxcodep = maxcodep;
737 "Not enough data at scanline %" PRIu32 " (short %" PRIu64
748 "LZWDecode: Strip %" PRIu32 " not terminated with EOI code",
764#define NextCode(_tif, _sp, _bp, _code, _get, dec_bitsleft) \
766 if (dec_bitsleft < (uint64_t)nbits) \
768 TIFFWarningExtR(_tif, module, \
769 "LZWDecode: Strip %" PRIu32 \
770 " not terminated with EOI code", \
771 _tif->tif_curstrip); \
776 _get(_sp, _bp, _code); \
777 dec_bitsleft -= nbits; \
784#define GetNextCodeCompat(sp, bp, code) \
786 nextdata |= (unsigned long)*(bp)++ << nextbits; \
788 if (nextbits < nbits) \
790 nextdata |= (unsigned long)*(bp)++ << nextbits; \
793 code = (hcode_t)(nextdata & nbitsmask); \
794 nextdata >>= nbits; \
800 static const char module[] =
"LZWDecodeCompat";
801 LZWCodecState *
sp = LZWDecoderState(tif);
808 long nextbits, nbitsmask;
810 code_t *
codep, *free_entp, *maxcodep, *oldcodep;
823 residue =
codep->length -
sp->dec_restart;
832 sp->dec_restart += occ;
836 }
while (--residue > occ);
864 nbits =
sp->lzw_nbits;
865 nextdata =
sp->lzw_nextdata;
866 nextbits =
sp->lzw_nextbits;
867 nbitsmask =
sp->dec_nbitsmask;
868 oldcodep =
sp->dec_oldcodep;
869 free_entp =
sp->dec_free_entp;
870 maxcodep =
sp->dec_maxcodep;
874 NextCode(tif,
sp, bp,
code, GetNextCodeCompat, dec_bitsleft);
875 if (
code == CODE_EOI)
877 if (
code == CODE_CLEAR)
881 free_entp =
sp->dec_codetab + CODE_FIRST;
883 (CSIZE - CODE_FIRST) *
sizeof(code_t));
885 nbitsmask = MAXCODE(BITS_MIN);
886 maxcodep =
sp->dec_codetab + nbitsmask;
887 NextCode(tif,
sp, bp,
code, GetNextCodeCompat, dec_bitsleft);
888 }
while (
code == CODE_CLEAR);
889 if (
code == CODE_EOI)
891 if (
code > CODE_CLEAR)
895 "LZWDecode: Corrupted LZW table at scanline %" PRIu32,
901 oldcodep =
sp->dec_codetab +
code;
909 if (free_entp < &sp->dec_codetab[0] ||
910 free_entp >= &
sp->dec_codetab[CSIZE])
913 "Corrupted LZW table at scanline %" PRIu32,
918 free_entp->next = oldcodep;
919 if (free_entp->next < &
sp->dec_codetab[0] ||
920 free_entp->next >= &
sp->dec_codetab[CSIZE])
923 "Corrupted LZW table at scanline %" PRIu32,
927 free_entp->firstchar = free_entp->next->firstchar;
928 free_entp->length = free_entp->next->length + 1;
930 (
codep < free_entp) ?
codep->firstchar : free_entp->firstchar;
931 if (++free_entp > maxcodep)
933 if (++nbits > BITS_MAX)
935 nbitsmask = MAXCODE(nbits);
936 maxcodep =
sp->dec_codetab + nbitsmask;
945 if (
codep->length == 0)
949 "Wrong length of decoded "
950 "string: data probably corrupted at scanline %" PRIu32,
954 if (
codep->length > occ)
966 }
while (
codep->length > occ);
967 sp->dec_restart = occ;
998 sp->dec_bitsleft = dec_bitsleft;
1000 sp->lzw_nbits = (
unsigned short)nbits;
1001 sp->lzw_nextdata = nextdata;
1002 sp->lzw_nextbits = nextbits;
1003 sp->dec_nbitsmask = nbitsmask;
1004 sp->dec_oldcodep = oldcodep;
1005 sp->dec_free_entp = free_entp;
1006 sp->dec_maxcodep = maxcodep;
1011 "Not enough data at scanline %" PRIu32 " (short %" PRIu64
1020#ifndef LZW_READ_ONLY
1022static void cl_hash(LZWCodecState *);
1028static int LZWSetupEncode(
TIFF *tif)
1030 static const char module[] =
"LZWSetupEncode";
1031 LZWCodecState *
sp = LZWEncoderState(tif);
1035 if (
sp->enc_hashtab ==
NULL)
1048 LZWCodecState *
sp = LZWEncoderState(tif);
1053 if (
sp->enc_hashtab ==
NULL)
1058 sp->lzw_nbits = BITS_MIN;
1059 sp->lzw_maxcode = MAXCODE(BITS_MIN);
1060 sp->lzw_free_ent = CODE_FIRST;
1061 sp->lzw_nextbits = 0;
1062 sp->lzw_nextdata = 0;
1063 sp->enc_checkpoint = CHECK_GAP;
1065 sp->enc_incount = 0;
1066 sp->enc_outcount = 0;
1073 sp->enc_oldcode = (hcode_t)-1;
1077#define CALCRATIO(sp, rat) \
1079 if (incount > 0x007fffff) \
1081 rat = outcount >> 8; \
1082 rat = (rat == 0 ? 0x7fffffff : incount / rat); \
1085 rat = (incount << 8) / outcount; \
1089#define PutNextCode(op, c) \
1091 nextdata = (nextdata << nbits) | c; \
1092 nextbits += nbits; \
1093 *op++ = (unsigned char)((nextdata >> (nextbits - 8)) & 0xff); \
1095 if (nextbits >= 8) \
1097 *op++ = (unsigned char)((nextdata >> (nextbits - 8)) & 0xff); \
1100 outcount += nbits; \
1119 register LZWCodecState *
sp = LZWEncoderState(tif);
1120 register long fcode;
1121 register hash_t *hp;
1125 tmsize_t incount, outcount, checkpoint;
1128 int free_ent, maxcode, nbits;
1141 incount =
sp->enc_incount;
1142 outcount =
sp->enc_outcount;
1143 checkpoint =
sp->enc_checkpoint;
1144 nextdata =
sp->lzw_nextdata;
1145 nextbits =
sp->lzw_nextbits;
1146 free_ent =
sp->lzw_free_ent;
1147 maxcode =
sp->lzw_maxcode;
1148 nbits =
sp->lzw_nbits;
1151 ent = (hcode_t)
sp->enc_oldcode;
1153 if (ent == (hcode_t)-1 &&
cc > 0)
1160 PutNextCode(
op, CODE_CLEAR);
1170 fcode = ((
long)
c << BITS_MAX) + ent;
1171 h = (
c << HSHIFT) ^ ent;
1179 hp = &
sp->enc_hashtab[
h];
1180 if (hp->hash == fcode)
1199 if ((
h -= disp) < 0)
1201 hp = &
sp->enc_hashtab[
h];
1202 if (hp->hash == fcode)
1207 }
while (hp->hash >= 0);
1225 PutNextCode(
op, ent);
1227 hp->code = (hcode_t)(free_ent++);
1229 if (free_ent == CODE_MAX - 1)
1236 free_ent = CODE_FIRST;
1237 PutNextCode(
op, CODE_CLEAR);
1239 maxcode = MAXCODE(BITS_MIN);
1247 if (free_ent > maxcode)
1250 assert(nbits <= BITS_MAX);
1251 maxcode = (
int)MAXCODE(nbits);
1253 else if (incount >= checkpoint)
1262 checkpoint = incount + CHECK_GAP;
1264 if (rat <= sp->enc_ratio)
1270 free_ent = CODE_FIRST;
1271 PutNextCode(
op, CODE_CLEAR);
1273 maxcode = MAXCODE(BITS_MIN);
1276 sp->enc_ratio = rat;
1285 sp->enc_incount = incount;
1286 sp->enc_outcount = outcount;
1287 sp->enc_checkpoint = checkpoint;
1288 sp->enc_oldcode = ent;
1289 sp->lzw_nextdata = nextdata;
1290 sp->lzw_nextbits = nextbits;
1291 sp->lzw_free_ent = (
unsigned short)free_ent;
1292 sp->lzw_maxcode = (
unsigned short)maxcode;
1293 sp->lzw_nbits = (
unsigned short)nbits;
1302static int LZWPostEncode(
TIFF *tif)
1304 register LZWCodecState *
sp = LZWEncoderState(tif);
1306 long nextbits =
sp->lzw_nextbits;
1307 WordType nextdata =
sp->lzw_nextdata;
1309 int nbits =
sp->lzw_nbits;
1311 if (
op >
sp->enc_rawlimit)
1318 if (
sp->enc_oldcode != (hcode_t)-1)
1320 int free_ent =
sp->lzw_free_ent;
1322 PutNextCode(
op,
sp->enc_oldcode);
1323 sp->enc_oldcode = (hcode_t)-1;
1326 if (free_ent == CODE_MAX - 1)
1330 PutNextCode(
op, CODE_CLEAR);
1339 if (free_ent >
sp->lzw_maxcode)
1342 assert(nbits <= BITS_MAX);
1346 PutNextCode(
op, CODE_EOI);
1349 *
op++ = (
unsigned char)((nextdata << (8 - nextbits)) & 0xff);
1358static void cl_hash(LZWCodecState *
sp)
1360 register hash_t *hp = &
sp->enc_hashtab[HSIZE - 1];
1361 register long i = HSIZE - 8;
1376 for (
i += 8;
i > 0;
i--, hp--)
1382static void LZWCleanup(
TIFF *tif)
1388 if (LZWDecoderState(tif)->dec_codetab)
1391 if (LZWEncoderState(tif)->enc_hashtab)
1402 static const char module[] =
"TIFFInitLZW";
1411 LZWDecoderState(tif)->dec_codetab =
NULL;
1412 LZWDecoderState(tif)->dec_decode =
NULL;
1413 LZWEncoderState(tif)->enc_hashtab =
NULL;
1414 LZWState(tif)->rw_mode = tif->
tif_mode;
1425#ifndef LZW_READ_ONLY
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
unsigned short(__cdecl typeof(TIFFCurrentDirectory))(struct tiff *)
_In_ uint64_t _In_ uint64_t _In_ uint64_t _In_opt_ traverse_ptr * tp
GLuint GLsizei GLsizei * length
GLfloat GLfloat GLfloat GLfloat h
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
static unsigned char * codep
#define memcpy(s1, s2, n)
static unsigned __int64 next
TIFFCodeMethod tif_encodestrip
TIFFCodeMethod tif_encodetile
TIFFPreMethod tif_preencode
TIFFBoolMethod tif_fixuptags
TIFFPreMethod tif_predecode
TIFFCodeMethod tif_decodestrip
TIFFCodeMethod tif_decoderow
TIFFBoolMethod tif_setupencode
TIFFBoolMethod tif_postencode
TIFFCodeMethod tif_encoderow
TIFFVoidMethod tif_cleanup
TIFFBoolMethod tif_setupdecode
TIFFCodeMethod tif_decodetile
void _TIFFSetDefaultCompressionState(TIFF *tif)
void TIFFErrorExtR(TIFF *tif, const char *module, const char *fmt,...)
void _TIFFfreeExt(TIFF *tif, void *p)
void * _TIFFmallocExt(TIFF *tif, tmsize_t s)
int TIFFPredictorCleanup(TIFF *tif)
int TIFFPredictorInit(TIFF *tif)
void _TIFFmemset(void *p, int v, tmsize_t c)
void TIFFWarningExtR(TIFF *tif, const char *module, const char *fmt,...)
int TIFFFlushData1(TIFF *tif)