tif_lzw.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  1. /* $Id: tif_lzw.c,v 1.55 2017-05-17 09:38:58 erouault Exp $ */
  2. /*
  3. * Copyright (c) 1988-1997 Sam Leffler
  4. * Copyright (c) 1991-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. #include "tiffiop.h"
  26. #ifdef LZW_SUPPORT
  27. /*
  28. * TIFF Library.
  29. * Rev 5.0 Lempel-Ziv & Welch Compression Support
  30. *
  31. * This code is derived from the compress program whose code is
  32. * derived from software contributed to Berkeley by James A. Woods,
  33. * derived from original work by Spencer Thomas and Joseph Orost.
  34. *
  35. * The original Berkeley copyright notice appears below in its entirety.
  36. */
  37. #include "tif_predict.h"
  38. #include <stdio.h>
  39. /*
  40. * NB: The 5.0 spec describes a different algorithm than Aldus
  41. * implements. Specifically, Aldus does code length transitions
  42. * one code earlier than should be done (for real LZW).
  43. * Earlier versions of this library implemented the correct
  44. * LZW algorithm, but emitted codes in a bit order opposite
  45. * to the TIFF spec. Thus, to maintain compatibility w/ Aldus
  46. * we interpret MSB-LSB ordered codes to be images written w/
  47. * old versions of this library, but otherwise adhere to the
  48. * Aldus "off by one" algorithm.
  49. *
  50. * Future revisions to the TIFF spec are expected to "clarify this issue".
  51. */
  52. #define LZW_COMPAT /* include backwards compatibility code */
  53. /*
  54. * Each strip of data is supposed to be terminated by a CODE_EOI.
  55. * If the following #define is included, the decoder will also
  56. * check for end-of-strip w/o seeing this code. This makes the
  57. * library more robust, but also slower.
  58. */
  59. #define LZW_CHECKEOS /* include checks for strips w/o EOI code */
  60. #define MAXCODE(n) ((1L<<(n))-1)
  61. /*
  62. * The TIFF spec specifies that encoded bit
  63. * strings range from 9 to 12 bits.
  64. */
  65. #define BITS_MIN 9 /* start with 9 bits */
  66. #define BITS_MAX 12 /* max of 12 bit strings */
  67. /* predefined codes */
  68. #define CODE_CLEAR 256 /* code to clear string table */
  69. #define CODE_EOI 257 /* end-of-information code */
  70. #define CODE_FIRST 258 /* first free code entry */
  71. #define CODE_MAX MAXCODE(BITS_MAX)
  72. #define HSIZE 9001L /* 91% occupancy */
  73. #define HSHIFT (13-8)
  74. #ifdef LZW_COMPAT
  75. /* NB: +1024 is for compatibility with old files */
  76. #define CSIZE (MAXCODE(BITS_MAX)+1024L)
  77. #else
  78. #define CSIZE (MAXCODE(BITS_MAX)+1L)
  79. #endif
  80. /*
  81. * State block for each open TIFF file using LZW
  82. * compression/decompression. Note that the predictor
  83. * state block must be first in this data structure.
  84. */
  85. typedef struct {
  86. TIFFPredictorState predict; /* predictor super class */
  87. unsigned short nbits; /* # of bits/code */
  88. unsigned short maxcode; /* maximum code for lzw_nbits */
  89. unsigned short free_ent; /* next free entry in hash table */
  90. unsigned long nextdata; /* next bits of i/o */
  91. long nextbits; /* # of valid bits in lzw_nextdata */
  92. int rw_mode; /* preserve rw_mode from init */
  93. } LZWBaseState;
  94. #define lzw_nbits base.nbits
  95. #define lzw_maxcode base.maxcode
  96. #define lzw_free_ent base.free_ent
  97. #define lzw_nextdata base.nextdata
  98. #define lzw_nextbits base.nextbits
  99. /*
  100. * Encoding-specific state.
  101. */
  102. typedef uint16 hcode_t; /* codes fit in 16 bits */
  103. typedef struct {
  104. long hash;
  105. hcode_t code;
  106. } hash_t;
  107. /*
  108. * Decoding-specific state.
  109. */
  110. typedef struct code_ent {
  111. struct code_ent *next;
  112. unsigned short length; /* string len, including this token */
  113. unsigned char value; /* data value */
  114. unsigned char firstchar; /* first token of string */
  115. } code_t;
  116. typedef int (*decodeFunc)(TIFF*, uint8*, tmsize_t, uint16);
  117. typedef struct {
  118. LZWBaseState base;
  119. /* Decoding specific data */
  120. long dec_nbitsmask; /* lzw_nbits 1 bits, right adjusted */
  121. long dec_restart; /* restart count */
  122. #ifdef LZW_CHECKEOS
  123. uint64 dec_bitsleft; /* available bits in raw data */
  124. #endif
  125. decodeFunc dec_decode; /* regular or backwards compatible */
  126. code_t* dec_codep; /* current recognized code */
  127. code_t* dec_oldcodep; /* previously recognized code */
  128. code_t* dec_free_entp; /* next free entry */
  129. code_t* dec_maxcodep; /* max available entry */
  130. code_t* dec_codetab; /* kept separate for small machines */
  131. /* Encoding specific data */
  132. int enc_oldcode; /* last code encountered */
  133. long enc_checkpoint; /* point at which to clear table */
  134. #define CHECK_GAP 10000 /* enc_ratio check interval */
  135. long enc_ratio; /* current compression ratio */
  136. long enc_incount; /* (input) data bytes encoded */
  137. long enc_outcount; /* encoded (output) bytes */
  138. uint8* enc_rawlimit; /* bound on tif_rawdata buffer */
  139. hash_t* enc_hashtab; /* kept separate for small machines */
  140. } LZWCodecState;
  141. #define LZWState(tif) ((LZWBaseState*) (tif)->tif_data)
  142. #define DecoderState(tif) ((LZWCodecState*) LZWState(tif))
  143. #define EncoderState(tif) ((LZWCodecState*) LZWState(tif))
  144. static int LZWDecode(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s);
  145. #ifdef LZW_COMPAT
  146. static int LZWDecodeCompat(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s);
  147. #endif
  148. static void cl_hash(LZWCodecState*);
  149. /*
  150. * LZW Decoder.
  151. */
  152. #ifdef LZW_CHECKEOS
  153. /*
  154. * This check shouldn't be necessary because each
  155. * strip is suppose to be terminated with CODE_EOI.
  156. */
  157. #define NextCode(_tif, _sp, _bp, _code, _get) { \
  158. if ((_sp)->dec_bitsleft < (uint64)nbits) { \
  159. TIFFWarningExt(_tif->tif_clientdata, module, \
  160. "LZWDecode: Strip %d not terminated with EOI code", \
  161. _tif->tif_curstrip); \
  162. _code = CODE_EOI; \
  163. } else { \
  164. _get(_sp,_bp,_code); \
  165. (_sp)->dec_bitsleft -= nbits; \
  166. } \
  167. }
  168. #else
  169. #define NextCode(tif, sp, bp, code, get) get(sp, bp, code)
  170. #endif
  171. static int
  172. LZWFixupTags(TIFF* tif)
  173. {
  174. (void) tif;
  175. return (1);
  176. }
  177. static int
  178. LZWSetupDecode(TIFF* tif)
  179. {
  180. static const char module[] = "LZWSetupDecode";
  181. LZWCodecState* sp = DecoderState(tif);
  182. int code;
  183. if( sp == NULL )
  184. {
  185. /*
  186. * Allocate state block so tag methods have storage to record
  187. * values.
  188. */
  189. tif->tif_data = (uint8*) _TIFFmalloc(sizeof(LZWCodecState));
  190. if (tif->tif_data == NULL)
  191. {
  192. TIFFErrorExt(tif->tif_clientdata, module, "No space for LZW state block");
  193. return (0);
  194. }
  195. DecoderState(tif)->dec_codetab = NULL;
  196. DecoderState(tif)->dec_decode = NULL;
  197. /*
  198. * Setup predictor setup.
  199. */
  200. (void) TIFFPredictorInit(tif);
  201. sp = DecoderState(tif);
  202. }
  203. assert(sp != NULL);
  204. if (sp->dec_codetab == NULL) {
  205. sp->dec_codetab = (code_t*)_TIFFmalloc(CSIZE*sizeof (code_t));
  206. if (sp->dec_codetab == NULL) {
  207. TIFFErrorExt(tif->tif_clientdata, module,
  208. "No space for LZW code table");
  209. return (0);
  210. }
  211. /*
  212. * Pre-load the table.
  213. */
  214. code = 255;
  215. do {
  216. sp->dec_codetab[code].value = (unsigned char)code;
  217. sp->dec_codetab[code].firstchar = (unsigned char)code;
  218. sp->dec_codetab[code].length = 1;
  219. sp->dec_codetab[code].next = NULL;
  220. } while (code--);
  221. /*
  222. * Zero-out the unused entries
  223. */
  224. _TIFFmemset(&sp->dec_codetab[CODE_CLEAR], 0,
  225. (CODE_FIRST - CODE_CLEAR) * sizeof (code_t));
  226. }
  227. return (1);
  228. }
  229. /*
  230. * Setup state for decoding a strip.
  231. */
  232. static int
  233. LZWPreDecode(TIFF* tif, uint16 s)
  234. {
  235. static const char module[] = "LZWPreDecode";
  236. LZWCodecState *sp = DecoderState(tif);
  237. (void) s;
  238. assert(sp != NULL);
  239. if( sp->dec_codetab == NULL )
  240. {
  241. tif->tif_setupdecode( tif );
  242. if( sp->dec_codetab == NULL )
  243. return (0);
  244. }
  245. /*
  246. * Check for old bit-reversed codes.
  247. */
  248. if (tif->tif_rawdata[0] == 0 && (tif->tif_rawdata[1] & 0x1)) {
  249. #ifdef LZW_COMPAT
  250. if (!sp->dec_decode) {
  251. TIFFWarningExt(tif->tif_clientdata, module,
  252. "Old-style LZW codes, convert file");
  253. /*
  254. * Override default decoding methods with
  255. * ones that deal with the old coding.
  256. * Otherwise the predictor versions set
  257. * above will call the compatibility routines
  258. * through the dec_decode method.
  259. */
  260. tif->tif_decoderow = LZWDecodeCompat;
  261. tif->tif_decodestrip = LZWDecodeCompat;
  262. tif->tif_decodetile = LZWDecodeCompat;
  263. /*
  264. * If doing horizontal differencing, must
  265. * re-setup the predictor logic since we
  266. * switched the basic decoder methods...
  267. */
  268. (*tif->tif_setupdecode)(tif);
  269. sp->dec_decode = LZWDecodeCompat;
  270. }
  271. sp->lzw_maxcode = MAXCODE(BITS_MIN);
  272. #else /* !LZW_COMPAT */
  273. if (!sp->dec_decode) {
  274. TIFFErrorExt(tif->tif_clientdata, module,
  275. "Old-style LZW codes not supported");
  276. sp->dec_decode = LZWDecode;
  277. }
  278. return (0);
  279. #endif/* !LZW_COMPAT */
  280. } else {
  281. sp->lzw_maxcode = MAXCODE(BITS_MIN)-1;
  282. sp->dec_decode = LZWDecode;
  283. }
  284. sp->lzw_nbits = BITS_MIN;
  285. sp->lzw_nextbits = 0;
  286. sp->lzw_nextdata = 0;
  287. sp->dec_restart = 0;
  288. sp->dec_nbitsmask = MAXCODE(BITS_MIN);
  289. #ifdef LZW_CHECKEOS
  290. sp->dec_bitsleft = 0;
  291. #endif
  292. sp->dec_free_entp = sp->dec_codetab + CODE_FIRST;
  293. /*
  294. * Zero entries that are not yet filled in. We do
  295. * this to guard against bogus input data that causes
  296. * us to index into undefined entries. If you can
  297. * come up with a way to safely bounds-check input codes
  298. * while decoding then you can remove this operation.
  299. */
  300. _TIFFmemset(sp->dec_free_entp, 0, (CSIZE-CODE_FIRST)*sizeof (code_t));
  301. sp->dec_oldcodep = &sp->dec_codetab[-1];
  302. sp->dec_maxcodep = &sp->dec_codetab[sp->dec_nbitsmask-1];
  303. return (1);
  304. }
  305. /*
  306. * Decode a "hunk of data".
  307. */
  308. #define GetNextCode(sp, bp, code) { \
  309. nextdata = (nextdata<<8) | *(bp)++; \
  310. nextbits += 8; \
  311. if (nextbits < nbits) { \
  312. nextdata = (nextdata<<8) | *(bp)++; \
  313. nextbits += 8; \
  314. } \
  315. code = (hcode_t)((nextdata >> (nextbits-nbits)) & nbitsmask); \
  316. nextbits -= nbits; \
  317. }
  318. static void
  319. codeLoop(TIFF* tif, const char* module)
  320. {
  321. TIFFErrorExt(tif->tif_clientdata, module,
  322. "Bogus encoding, loop in the code table; scanline %d",
  323. tif->tif_row);
  324. }
  325. static int
  326. LZWDecode(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s)
  327. {
  328. static const char module[] = "LZWDecode";
  329. LZWCodecState *sp = DecoderState(tif);
  330. char *op = (char*) op0;
  331. long occ = (long) occ0;
  332. char *tp;
  333. unsigned char *bp;
  334. hcode_t code;
  335. int len;
  336. long nbits, nextbits, nbitsmask;
  337. unsigned long nextdata;
  338. code_t *codep, *free_entp, *maxcodep, *oldcodep;
  339. (void) s;
  340. assert(sp != NULL);
  341. assert(sp->dec_codetab != NULL);
  342. /*
  343. Fail if value does not fit in long.
  344. */
  345. if ((tmsize_t) occ != occ0)
  346. return (0);
  347. /*
  348. * Restart interrupted output operation.
  349. */
  350. if (sp->dec_restart) {
  351. long residue;
  352. codep = sp->dec_codep;
  353. residue = codep->length - sp->dec_restart;
  354. if (residue > occ) {
  355. /*
  356. * Residue from previous decode is sufficient
  357. * to satisfy decode request. Skip to the
  358. * start of the decoded string, place decoded
  359. * values in the output buffer, and return.
  360. */
  361. sp->dec_restart += occ;
  362. do {
  363. codep = codep->next;
  364. } while (--residue > occ && codep);
  365. if (codep) {
  366. tp = op + occ;
  367. do {
  368. *--tp = codep->value;
  369. codep = codep->next;
  370. } while (--occ && codep);
  371. }
  372. return (1);
  373. }
  374. /*
  375. * Residue satisfies only part of the decode request.
  376. */
  377. op += residue;
  378. occ -= residue;
  379. tp = op;
  380. do {
  381. int t;
  382. --tp;
  383. t = codep->value;
  384. codep = codep->next;
  385. *tp = (char)t;
  386. } while (--residue && codep);
  387. sp->dec_restart = 0;
  388. }
  389. bp = (unsigned char *)tif->tif_rawcp;
  390. #ifdef LZW_CHECKEOS
  391. sp->dec_bitsleft = (((uint64)tif->tif_rawcc) << 3);
  392. #endif
  393. nbits = sp->lzw_nbits;
  394. nextdata = sp->lzw_nextdata;
  395. nextbits = sp->lzw_nextbits;
  396. nbitsmask = sp->dec_nbitsmask;
  397. oldcodep = sp->dec_oldcodep;
  398. free_entp = sp->dec_free_entp;
  399. maxcodep = sp->dec_maxcodep;
  400. while (occ > 0) {
  401. NextCode(tif, sp, bp, code, GetNextCode);
  402. if (code == CODE_EOI)
  403. break;
  404. if (code == CODE_CLEAR) {
  405. do {
  406. free_entp = sp->dec_codetab + CODE_FIRST;
  407. _TIFFmemset(free_entp, 0,
  408. (CSIZE - CODE_FIRST) * sizeof (code_t));
  409. nbits = BITS_MIN;
  410. nbitsmask = MAXCODE(BITS_MIN);
  411. maxcodep = sp->dec_codetab + nbitsmask-1;
  412. NextCode(tif, sp, bp, code, GetNextCode);
  413. } while (code == CODE_CLEAR); /* consecutive CODE_CLEAR codes */
  414. if (code == CODE_EOI)
  415. break;
  416. if (code > CODE_CLEAR) {
  417. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  418. "LZWDecode: Corrupted LZW table at scanline %d",
  419. tif->tif_row);
  420. return (0);
  421. }
  422. *op++ = (char)code;
  423. occ--;
  424. oldcodep = sp->dec_codetab + code;
  425. continue;
  426. }
  427. codep = sp->dec_codetab + code;
  428. /*
  429. * Add the new entry to the code table.
  430. */
  431. if (free_entp < &sp->dec_codetab[0] ||
  432. free_entp >= &sp->dec_codetab[CSIZE]) {
  433. TIFFErrorExt(tif->tif_clientdata, module,
  434. "Corrupted LZW table at scanline %d",
  435. tif->tif_row);
  436. return (0);
  437. }
  438. free_entp->next = oldcodep;
  439. if (free_entp->next < &sp->dec_codetab[0] ||
  440. free_entp->next >= &sp->dec_codetab[CSIZE]) {
  441. TIFFErrorExt(tif->tif_clientdata, module,
  442. "Corrupted LZW table at scanline %d",
  443. tif->tif_row);
  444. return (0);
  445. }
  446. free_entp->firstchar = free_entp->next->firstchar;
  447. free_entp->length = free_entp->next->length+1;
  448. free_entp->value = (codep < free_entp) ?
  449. codep->firstchar : free_entp->firstchar;
  450. if (++free_entp > maxcodep) {
  451. if (++nbits > BITS_MAX) /* should not happen */
  452. nbits = BITS_MAX;
  453. nbitsmask = MAXCODE(nbits);
  454. maxcodep = sp->dec_codetab + nbitsmask-1;
  455. }
  456. oldcodep = codep;
  457. if (code >= 256) {
  458. /*
  459. * Code maps to a string, copy string
  460. * value to output (written in reverse).
  461. */
  462. if(codep->length == 0) {
  463. TIFFErrorExt(tif->tif_clientdata, module,
  464. "Wrong length of decoded string: "
  465. "data probably corrupted at scanline %d",
  466. tif->tif_row);
  467. return (0);
  468. }
  469. if (codep->length > occ) {
  470. /*
  471. * String is too long for decode buffer,
  472. * locate portion that will fit, copy to
  473. * the decode buffer, and setup restart
  474. * logic for the next decoding call.
  475. */
  476. sp->dec_codep = codep;
  477. do {
  478. codep = codep->next;
  479. } while (codep && codep->length > occ);
  480. if (codep) {
  481. sp->dec_restart = (long)occ;
  482. tp = op + occ;
  483. do {
  484. *--tp = codep->value;
  485. codep = codep->next;
  486. } while (--occ && codep);
  487. if (codep)
  488. codeLoop(tif, module);
  489. }
  490. break;
  491. }
  492. len = codep->length;
  493. tp = op + len;
  494. do {
  495. int t;
  496. --tp;
  497. t = codep->value;
  498. codep = codep->next;
  499. *tp = (char)t;
  500. } while (codep && tp > op);
  501. if (codep) {
  502. codeLoop(tif, module);
  503. break;
  504. }
  505. assert(occ >= len);
  506. op += len;
  507. occ -= len;
  508. } else {
  509. *op++ = (char)code;
  510. occ--;
  511. }
  512. }
  513. tif->tif_rawcc -= (tmsize_t)( (uint8*) bp - tif->tif_rawcp );
  514. tif->tif_rawcp = (uint8*) bp;
  515. sp->lzw_nbits = (unsigned short) nbits;
  516. sp->lzw_nextdata = nextdata;
  517. sp->lzw_nextbits = nextbits;
  518. sp->dec_nbitsmask = nbitsmask;
  519. sp->dec_oldcodep = oldcodep;
  520. sp->dec_free_entp = free_entp;
  521. sp->dec_maxcodep = maxcodep;
  522. if (occ > 0) {
  523. #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
  524. TIFFErrorExt(tif->tif_clientdata, module,
  525. "Not enough data at scanline %d (short %I64d bytes)",
  526. tif->tif_row, (unsigned __int64) occ);
  527. #else
  528. TIFFErrorExt(tif->tif_clientdata, module,
  529. "Not enough data at scanline %d (short %llu bytes)",
  530. tif->tif_row, (unsigned long long) occ);
  531. #endif
  532. return (0);
  533. }
  534. return (1);
  535. }
  536. #ifdef LZW_COMPAT
  537. /*
  538. * Decode a "hunk of data" for old images.
  539. */
  540. #define GetNextCodeCompat(sp, bp, code) { \
  541. nextdata |= (unsigned long) *(bp)++ << nextbits; \
  542. nextbits += 8; \
  543. if (nextbits < nbits) { \
  544. nextdata |= (unsigned long) *(bp)++ << nextbits;\
  545. nextbits += 8; \
  546. } \
  547. code = (hcode_t)(nextdata & nbitsmask); \
  548. nextdata >>= nbits; \
  549. nextbits -= nbits; \
  550. }
  551. static int
  552. LZWDecodeCompat(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s)
  553. {
  554. static const char module[] = "LZWDecodeCompat";
  555. LZWCodecState *sp = DecoderState(tif);
  556. char *op = (char*) op0;
  557. long occ = (long) occ0;
  558. char *tp;
  559. unsigned char *bp;
  560. int code, nbits;
  561. long nextbits, nextdata, nbitsmask;
  562. code_t *codep, *free_entp, *maxcodep, *oldcodep;
  563. (void) s;
  564. assert(sp != NULL);
  565. /*
  566. Fail if value does not fit in long.
  567. */
  568. if ((tmsize_t) occ != occ0)
  569. return (0);
  570. /*
  571. * Restart interrupted output operation.
  572. */
  573. if (sp->dec_restart) {
  574. long residue;
  575. codep = sp->dec_codep;
  576. residue = codep->length - sp->dec_restart;
  577. if (residue > occ) {
  578. /*
  579. * Residue from previous decode is sufficient
  580. * to satisfy decode request. Skip to the
  581. * start of the decoded string, place decoded
  582. * values in the output buffer, and return.
  583. */
  584. sp->dec_restart += occ;
  585. do {
  586. codep = codep->next;
  587. } while (--residue > occ);
  588. tp = op + occ;
  589. do {
  590. *--tp = codep->value;
  591. codep = codep->next;
  592. } while (--occ);
  593. return (1);
  594. }
  595. /*
  596. * Residue satisfies only part of the decode request.
  597. */
  598. op += residue;
  599. occ -= residue;
  600. tp = op;
  601. do {
  602. *--tp = codep->value;
  603. codep = codep->next;
  604. } while (--residue);
  605. sp->dec_restart = 0;
  606. }
  607. bp = (unsigned char *)tif->tif_rawcp;
  608. nbits = sp->lzw_nbits;
  609. nextdata = sp->lzw_nextdata;
  610. nextbits = sp->lzw_nextbits;
  611. nbitsmask = sp->dec_nbitsmask;
  612. oldcodep = sp->dec_oldcodep;
  613. free_entp = sp->dec_free_entp;
  614. maxcodep = sp->dec_maxcodep;
  615. while (occ > 0) {
  616. NextCode(tif, sp, bp, code, GetNextCodeCompat);
  617. if (code == CODE_EOI)
  618. break;
  619. if (code == CODE_CLEAR) {
  620. do {
  621. free_entp = sp->dec_codetab + CODE_FIRST;
  622. _TIFFmemset(free_entp, 0,
  623. (CSIZE - CODE_FIRST) * sizeof (code_t));
  624. nbits = BITS_MIN;
  625. nbitsmask = MAXCODE(BITS_MIN);
  626. maxcodep = sp->dec_codetab + nbitsmask;
  627. NextCode(tif, sp, bp, code, GetNextCodeCompat);
  628. } while (code == CODE_CLEAR); /* consecutive CODE_CLEAR codes */
  629. if (code == CODE_EOI)
  630. break;
  631. if (code > CODE_CLEAR) {
  632. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  633. "LZWDecode: Corrupted LZW table at scanline %d",
  634. tif->tif_row);
  635. return (0);
  636. }
  637. *op++ = (char)code;
  638. occ--;
  639. oldcodep = sp->dec_codetab + code;
  640. continue;
  641. }
  642. codep = sp->dec_codetab + code;
  643. /*
  644. * Add the new entry to the code table.
  645. */
  646. if (free_entp < &sp->dec_codetab[0] ||
  647. free_entp >= &sp->dec_codetab[CSIZE]) {
  648. TIFFErrorExt(tif->tif_clientdata, module,
  649. "Corrupted LZW table at scanline %d", tif->tif_row);
  650. return (0);
  651. }
  652. free_entp->next = oldcodep;
  653. if (free_entp->next < &sp->dec_codetab[0] ||
  654. free_entp->next >= &sp->dec_codetab[CSIZE]) {
  655. TIFFErrorExt(tif->tif_clientdata, module,
  656. "Corrupted LZW table at scanline %d", tif->tif_row);
  657. return (0);
  658. }
  659. free_entp->firstchar = free_entp->next->firstchar;
  660. free_entp->length = free_entp->next->length+1;
  661. free_entp->value = (codep < free_entp) ?
  662. codep->firstchar : free_entp->firstchar;
  663. if (++free_entp > maxcodep) {
  664. if (++nbits > BITS_MAX) /* should not happen */
  665. nbits = BITS_MAX;
  666. nbitsmask = MAXCODE(nbits);
  667. maxcodep = sp->dec_codetab + nbitsmask;
  668. }
  669. oldcodep = codep;
  670. if (code >= 256) {
  671. /*
  672. * Code maps to a string, copy string
  673. * value to output (written in reverse).
  674. */
  675. if(codep->length == 0) {
  676. TIFFErrorExt(tif->tif_clientdata, module,
  677. "Wrong length of decoded "
  678. "string: data probably corrupted at scanline %d",
  679. tif->tif_row);
  680. return (0);
  681. }
  682. if (codep->length > occ) {
  683. /*
  684. * String is too long for decode buffer,
  685. * locate portion that will fit, copy to
  686. * the decode buffer, and setup restart
  687. * logic for the next decoding call.
  688. */
  689. sp->dec_codep = codep;
  690. do {
  691. codep = codep->next;
  692. } while (codep->length > occ);
  693. sp->dec_restart = occ;
  694. tp = op + occ;
  695. do {
  696. *--tp = codep->value;
  697. codep = codep->next;
  698. } while (--occ);
  699. break;
  700. }
  701. assert(occ >= codep->length);
  702. op += codep->length;
  703. occ -= codep->length;
  704. tp = op;
  705. do {
  706. *--tp = codep->value;
  707. } while( (codep = codep->next) != NULL );
  708. } else {
  709. *op++ = (char)code;
  710. occ--;
  711. }
  712. }
  713. tif->tif_rawcp = (uint8*) bp;
  714. sp->lzw_nbits = (unsigned short)nbits;
  715. sp->lzw_nextdata = nextdata;
  716. sp->lzw_nextbits = nextbits;
  717. sp->dec_nbitsmask = nbitsmask;
  718. sp->dec_oldcodep = oldcodep;
  719. sp->dec_free_entp = free_entp;
  720. sp->dec_maxcodep = maxcodep;
  721. if (occ > 0) {
  722. #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
  723. TIFFErrorExt(tif->tif_clientdata, module,
  724. "Not enough data at scanline %d (short %I64d bytes)",
  725. tif->tif_row, (unsigned __int64) occ);
  726. #else
  727. TIFFErrorExt(tif->tif_clientdata, module,
  728. "Not enough data at scanline %d (short %llu bytes)",
  729. tif->tif_row, (unsigned long long) occ);
  730. #endif
  731. return (0);
  732. }
  733. return (1);
  734. }
  735. #endif /* LZW_COMPAT */
  736. /*
  737. * LZW Encoding.
  738. */
  739. static int
  740. LZWSetupEncode(TIFF* tif)
  741. {
  742. static const char module[] = "LZWSetupEncode";
  743. LZWCodecState* sp = EncoderState(tif);
  744. assert(sp != NULL);
  745. sp->enc_hashtab = (hash_t*) _TIFFmalloc(HSIZE*sizeof (hash_t));
  746. if (sp->enc_hashtab == NULL) {
  747. TIFFErrorExt(tif->tif_clientdata, module,
  748. "No space for LZW hash table");
  749. return (0);
  750. }
  751. return (1);
  752. }
  753. /*
  754. * Reset encoding state at the start of a strip.
  755. */
  756. static int
  757. LZWPreEncode(TIFF* tif, uint16 s)
  758. {
  759. LZWCodecState *sp = EncoderState(tif);
  760. (void) s;
  761. assert(sp != NULL);
  762. if( sp->enc_hashtab == NULL )
  763. {
  764. tif->tif_setupencode( tif );
  765. }
  766. sp->lzw_nbits = BITS_MIN;
  767. sp->lzw_maxcode = MAXCODE(BITS_MIN);
  768. sp->lzw_free_ent = CODE_FIRST;
  769. sp->lzw_nextbits = 0;
  770. sp->lzw_nextdata = 0;
  771. sp->enc_checkpoint = CHECK_GAP;
  772. sp->enc_ratio = 0;
  773. sp->enc_incount = 0;
  774. sp->enc_outcount = 0;
  775. /*
  776. * The 4 here insures there is space for 2 max-sized
  777. * codes in LZWEncode and LZWPostDecode.
  778. */
  779. sp->enc_rawlimit = tif->tif_rawdata + tif->tif_rawdatasize-1 - 4;
  780. cl_hash(sp); /* clear hash table */
  781. sp->enc_oldcode = (hcode_t) -1; /* generates CODE_CLEAR in LZWEncode */
  782. return (1);
  783. }
  784. #define CALCRATIO(sp, rat) { \
  785. if (incount > 0x007fffff) { /* NB: shift will overflow */\
  786. rat = outcount >> 8; \
  787. rat = (rat == 0 ? 0x7fffffff : incount/rat); \
  788. } else \
  789. rat = (incount<<8) / outcount; \
  790. }
  791. /* Explicit 0xff masking to make icc -check=conversions happy */
  792. #define PutNextCode(op, c) { \
  793. nextdata = (nextdata << nbits) | c; \
  794. nextbits += nbits; \
  795. *op++ = (unsigned char)((nextdata >> (nextbits-8))&0xff); \
  796. nextbits -= 8; \
  797. if (nextbits >= 8) { \
  798. *op++ = (unsigned char)((nextdata >> (nextbits-8))&0xff); \
  799. nextbits -= 8; \
  800. } \
  801. outcount += nbits; \
  802. }
  803. /*
  804. * Encode a chunk of pixels.
  805. *
  806. * Uses an open addressing double hashing (no chaining) on the
  807. * prefix code/next character combination. We do a variant of
  808. * Knuth's algorithm D (vol. 3, sec. 6.4) along with G. Knott's
  809. * relatively-prime secondary probe. Here, the modular division
  810. * first probe is gives way to a faster exclusive-or manipulation.
  811. * Also do block compression with an adaptive reset, whereby the
  812. * code table is cleared when the compression ratio decreases,
  813. * but after the table fills. The variable-length output codes
  814. * are re-sized at this point, and a CODE_CLEAR is generated
  815. * for the decoder.
  816. */
  817. static int
  818. LZWEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
  819. {
  820. register LZWCodecState *sp = EncoderState(tif);
  821. register long fcode;
  822. register hash_t *hp;
  823. register int h, c;
  824. hcode_t ent;
  825. long disp;
  826. long incount, outcount, checkpoint;
  827. unsigned long nextdata;
  828. long nextbits;
  829. int free_ent, maxcode, nbits;
  830. uint8* op;
  831. uint8* limit;
  832. (void) s;
  833. if (sp == NULL)
  834. return (0);
  835. assert(sp->enc_hashtab != NULL);
  836. /*
  837. * Load local state.
  838. */
  839. incount = sp->enc_incount;
  840. outcount = sp->enc_outcount;
  841. checkpoint = sp->enc_checkpoint;
  842. nextdata = sp->lzw_nextdata;
  843. nextbits = sp->lzw_nextbits;
  844. free_ent = sp->lzw_free_ent;
  845. maxcode = sp->lzw_maxcode;
  846. nbits = sp->lzw_nbits;
  847. op = tif->tif_rawcp;
  848. limit = sp->enc_rawlimit;
  849. ent = (hcode_t)sp->enc_oldcode;
  850. if (ent == (hcode_t) -1 && cc > 0) {
  851. /*
  852. * NB: This is safe because it can only happen
  853. * at the start of a strip where we know there
  854. * is space in the data buffer.
  855. */
  856. PutNextCode(op, CODE_CLEAR);
  857. ent = *bp++; cc--; incount++;
  858. }
  859. while (cc > 0) {
  860. c = *bp++; cc--; incount++;
  861. fcode = ((long)c << BITS_MAX) + ent;
  862. h = (c << HSHIFT) ^ ent; /* xor hashing */
  863. #ifdef _WINDOWS
  864. /*
  865. * Check hash index for an overflow.
  866. */
  867. if (h >= HSIZE)
  868. h -= HSIZE;
  869. #endif
  870. hp = &sp->enc_hashtab[h];
  871. if (hp->hash == fcode) {
  872. ent = hp->code;
  873. continue;
  874. }
  875. if (hp->hash >= 0) {
  876. /*
  877. * Primary hash failed, check secondary hash.
  878. */
  879. disp = HSIZE - h;
  880. if (h == 0)
  881. disp = 1;
  882. do {
  883. /*
  884. * Avoid pointer arithmetic because of
  885. * wraparound problems with segments.
  886. */
  887. if ((h -= disp) < 0)
  888. h += HSIZE;
  889. hp = &sp->enc_hashtab[h];
  890. if (hp->hash == fcode) {
  891. ent = hp->code;
  892. goto hit;
  893. }
  894. } while (hp->hash >= 0);
  895. }
  896. /*
  897. * New entry, emit code and add to table.
  898. */
  899. /*
  900. * Verify there is space in the buffer for the code
  901. * and any potential Clear code that might be emitted
  902. * below. The value of limit is setup so that there
  903. * are at least 4 bytes free--room for 2 codes.
  904. */
  905. if (op > limit) {
  906. tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata);
  907. if( !TIFFFlushData1(tif) )
  908. return 0;
  909. op = tif->tif_rawdata;
  910. }
  911. PutNextCode(op, ent);
  912. ent = (hcode_t)c;
  913. hp->code = (hcode_t)(free_ent++);
  914. hp->hash = fcode;
  915. if (free_ent == CODE_MAX-1) {
  916. /* table is full, emit clear code and reset */
  917. cl_hash(sp);
  918. sp->enc_ratio = 0;
  919. incount = 0;
  920. outcount = 0;
  921. free_ent = CODE_FIRST;
  922. PutNextCode(op, CODE_CLEAR);
  923. nbits = BITS_MIN;
  924. maxcode = MAXCODE(BITS_MIN);
  925. } else {
  926. /*
  927. * If the next entry is going to be too big for
  928. * the code size, then increase it, if possible.
  929. */
  930. if (free_ent > maxcode) {
  931. nbits++;
  932. assert(nbits <= BITS_MAX);
  933. maxcode = (int) MAXCODE(nbits);
  934. } else if (incount >= checkpoint) {
  935. long rat;
  936. /*
  937. * Check compression ratio and, if things seem
  938. * to be slipping, clear the hash table and
  939. * reset state. The compression ratio is a
  940. * 24+8-bit fractional number.
  941. */
  942. checkpoint = incount+CHECK_GAP;
  943. CALCRATIO(sp, rat);
  944. if (rat <= sp->enc_ratio) {
  945. cl_hash(sp);
  946. sp->enc_ratio = 0;
  947. incount = 0;
  948. outcount = 0;
  949. free_ent = CODE_FIRST;
  950. PutNextCode(op, CODE_CLEAR);
  951. nbits = BITS_MIN;
  952. maxcode = MAXCODE(BITS_MIN);
  953. } else
  954. sp->enc_ratio = rat;
  955. }
  956. }
  957. hit:
  958. ;
  959. }
  960. /*
  961. * Restore global state.
  962. */
  963. sp->enc_incount = incount;
  964. sp->enc_outcount = outcount;
  965. sp->enc_checkpoint = checkpoint;
  966. sp->enc_oldcode = ent;
  967. sp->lzw_nextdata = nextdata;
  968. sp->lzw_nextbits = nextbits;
  969. sp->lzw_free_ent = (unsigned short)free_ent;
  970. sp->lzw_maxcode = (unsigned short)maxcode;
  971. sp->lzw_nbits = (unsigned short)nbits;
  972. tif->tif_rawcp = op;
  973. return (1);
  974. }
  975. /*
  976. * Finish off an encoded strip by flushing the last
  977. * string and tacking on an End Of Information code.
  978. */
  979. static int
  980. LZWPostEncode(TIFF* tif)
  981. {
  982. register LZWCodecState *sp = EncoderState(tif);
  983. uint8* op = tif->tif_rawcp;
  984. long nextbits = sp->lzw_nextbits;
  985. unsigned long nextdata = sp->lzw_nextdata;
  986. long outcount = sp->enc_outcount;
  987. int nbits = sp->lzw_nbits;
  988. if (op > sp->enc_rawlimit) {
  989. tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata);
  990. if( !TIFFFlushData1(tif) )
  991. return 0;
  992. op = tif->tif_rawdata;
  993. }
  994. if (sp->enc_oldcode != (hcode_t) -1) {
  995. int free_ent = sp->lzw_free_ent;
  996. PutNextCode(op, sp->enc_oldcode);
  997. sp->enc_oldcode = (hcode_t) -1;
  998. free_ent ++;
  999. if (free_ent == CODE_MAX-1) {
  1000. /* table is full, emit clear code and reset */
  1001. outcount = 0;
  1002. PutNextCode(op, CODE_CLEAR);
  1003. nbits = BITS_MIN;
  1004. } else {
  1005. /*
  1006. * If the next entry is going to be too big for
  1007. * the code size, then increase it, if possible.
  1008. */
  1009. if (free_ent > sp->lzw_maxcode) {
  1010. nbits++;
  1011. assert(nbits <= BITS_MAX);
  1012. }
  1013. }
  1014. }
  1015. PutNextCode(op, CODE_EOI);
  1016. /* Explicit 0xff masking to make icc -check=conversions happy */
  1017. if (nextbits > 0)
  1018. *op++ = (unsigned char)((nextdata << (8-nextbits))&0xff);
  1019. tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata);
  1020. return (1);
  1021. }
  1022. /*
  1023. * Reset encoding hash table.
  1024. */
  1025. static void
  1026. cl_hash(LZWCodecState* sp)
  1027. {
  1028. register hash_t *hp = &sp->enc_hashtab[HSIZE-1];
  1029. register long i = HSIZE-8;
  1030. do {
  1031. i -= 8;
  1032. hp[-7].hash = -1;
  1033. hp[-6].hash = -1;
  1034. hp[-5].hash = -1;
  1035. hp[-4].hash = -1;
  1036. hp[-3].hash = -1;
  1037. hp[-2].hash = -1;
  1038. hp[-1].hash = -1;
  1039. hp[ 0].hash = -1;
  1040. hp -= 8;
  1041. } while (i >= 0);
  1042. for (i += 8; i > 0; i--, hp--)
  1043. hp->hash = -1;
  1044. }
  1045. static void
  1046. LZWCleanup(TIFF* tif)
  1047. {
  1048. (void)TIFFPredictorCleanup(tif);
  1049. assert(tif->tif_data != 0);
  1050. if (DecoderState(tif)->dec_codetab)
  1051. _TIFFfree(DecoderState(tif)->dec_codetab);
  1052. if (EncoderState(tif)->enc_hashtab)
  1053. _TIFFfree(EncoderState(tif)->enc_hashtab);
  1054. _TIFFfree(tif->tif_data);
  1055. tif->tif_data = NULL;
  1056. _TIFFSetDefaultCompressionState(tif);
  1057. }
  1058. int
  1059. TIFFInitLZW(TIFF* tif, int scheme)
  1060. {
  1061. static const char module[] = "TIFFInitLZW";
  1062. assert(scheme == COMPRESSION_LZW);
  1063. /*
  1064. * Allocate state block so tag methods have storage to record values.
  1065. */
  1066. tif->tif_data = (uint8*) _TIFFmalloc(sizeof (LZWCodecState));
  1067. if (tif->tif_data == NULL)
  1068. goto bad;
  1069. DecoderState(tif)->dec_codetab = NULL;
  1070. DecoderState(tif)->dec_decode = NULL;
  1071. EncoderState(tif)->enc_hashtab = NULL;
  1072. LZWState(tif)->rw_mode = tif->tif_mode;
  1073. /*
  1074. * Install codec methods.
  1075. */
  1076. tif->tif_fixuptags = LZWFixupTags;
  1077. tif->tif_setupdecode = LZWSetupDecode;
  1078. tif->tif_predecode = LZWPreDecode;
  1079. tif->tif_decoderow = LZWDecode;
  1080. tif->tif_decodestrip = LZWDecode;
  1081. tif->tif_decodetile = LZWDecode;
  1082. tif->tif_setupencode = LZWSetupEncode;
  1083. tif->tif_preencode = LZWPreEncode;
  1084. tif->tif_postencode = LZWPostEncode;
  1085. tif->tif_encoderow = LZWEncode;
  1086. tif->tif_encodestrip = LZWEncode;
  1087. tif->tif_encodetile = LZWEncode;
  1088. tif->tif_cleanup = LZWCleanup;
  1089. /*
  1090. * Setup predictor setup.
  1091. */
  1092. (void) TIFFPredictorInit(tif);
  1093. return (1);
  1094. bad:
  1095. TIFFErrorExt(tif->tif_clientdata, module,
  1096. "No space for LZW state block");
  1097. return (0);
  1098. }
  1099. /*
  1100. * Copyright (c) 1985, 1986 The Regents of the University of California.
  1101. * All rights reserved.
  1102. *
  1103. * This code is derived from software contributed to Berkeley by
  1104. * James A. Woods, derived from original work by Spencer Thomas
  1105. * and Joseph Orost.
  1106. *
  1107. * Redistribution and use in source and binary forms are permitted
  1108. * provided that the above copyright notice and this paragraph are
  1109. * duplicated in all such forms and that any documentation,
  1110. * advertising materials, and other materials related to such
  1111. * distribution and use acknowledge that the software was developed
  1112. * by the University of California, Berkeley. The name of the
  1113. * University may not be used to endorse or promote products derived
  1114. * from this software without specific prior written permission.
  1115. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  1116. * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  1117. * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  1118. */
  1119. #endif /* LZW_SUPPORT */
  1120. /* vim: set ts=8 sts=8 sw=8 noet: */
  1121. /*
  1122. * Local Variables:
  1123. * mode: c
  1124. * c-basic-offset: 8
  1125. * fill-column: 78
  1126. * End:
  1127. */