PageRenderTime 46ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/src/3rdparty/tiff-3.9.4/tif_lzw.c

https://bitbucket.org/melbyruarus/pbrt
C | 1129 lines | 768 code | 79 blank | 282 comment | 135 complexity | b38724217cb371356f854076d28a4778 MD5 | raw file
  1. /* $Id: tif_lzw.c,v 1.29.2.6 2010-06-08 18:50:42 bfriesen 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. 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*, tidata_t, tsize_t, tsample_t);
  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. long 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. tidata_t 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*, tidata_t, tsize_t, tsample_t);
  145. #ifdef LZW_COMPAT
  146. static int LZWDecodeCompat(TIFF*, tidata_t, tsize_t, tsample_t);
  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 < nbits) { \
  159. TIFFWarningExt(_tif->tif_clientdata, _tif->tif_name, \
  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. LZWSetupDecode(TIFF* tif)
  173. {
  174. LZWCodecState* sp = DecoderState(tif);
  175. static const char module[] = " LZWSetupDecode";
  176. int code;
  177. if( sp == NULL )
  178. {
  179. /*
  180. * Allocate state block so tag methods have storage to record
  181. * values.
  182. */
  183. tif->tif_data = (tidata_t) _TIFFmalloc(sizeof(LZWCodecState));
  184. if (tif->tif_data == NULL)
  185. {
  186. TIFFErrorExt(tif->tif_clientdata, "LZWPreDecode", "No space for LZW state block");
  187. return (0);
  188. }
  189. DecoderState(tif)->dec_codetab = NULL;
  190. DecoderState(tif)->dec_decode = NULL;
  191. /*
  192. * Setup predictor setup.
  193. */
  194. (void) TIFFPredictorInit(tif);
  195. sp = DecoderState(tif);
  196. }
  197. assert(sp != NULL);
  198. if (sp->dec_codetab == NULL) {
  199. sp->dec_codetab = (code_t*)_TIFFmalloc(CSIZE*sizeof (code_t));
  200. if (sp->dec_codetab == NULL) {
  201. TIFFErrorExt(tif->tif_clientdata, module,
  202. "No space for LZW code table");
  203. return (0);
  204. }
  205. /*
  206. * Pre-load the table.
  207. */
  208. code = 255;
  209. do {
  210. sp->dec_codetab[code].value = code;
  211. sp->dec_codetab[code].firstchar = code;
  212. sp->dec_codetab[code].length = 1;
  213. sp->dec_codetab[code].next = NULL;
  214. } while (code--);
  215. /*
  216. * Zero-out the unused entries
  217. */
  218. _TIFFmemset(&sp->dec_codetab[CODE_CLEAR], 0,
  219. (CODE_FIRST - CODE_CLEAR) * sizeof (code_t));
  220. }
  221. return (1);
  222. }
  223. /*
  224. * Setup state for decoding a strip.
  225. */
  226. static int
  227. LZWPreDecode(TIFF* tif, tsample_t s)
  228. {
  229. LZWCodecState *sp = DecoderState(tif);
  230. (void) s;
  231. assert(sp != NULL);
  232. if( sp->dec_codetab == NULL )
  233. {
  234. tif->tif_setupdecode( tif );
  235. }
  236. /*
  237. * Check for old bit-reversed codes.
  238. */
  239. if (tif->tif_rawdata[0] == 0 && (tif->tif_rawdata[1] & 0x1)) {
  240. #ifdef LZW_COMPAT
  241. if (!sp->dec_decode) {
  242. TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
  243. "Old-style LZW codes, convert file");
  244. /*
  245. * Override default decoding methods with
  246. * ones that deal with the old coding.
  247. * Otherwise the predictor versions set
  248. * above will call the compatibility routines
  249. * through the dec_decode method.
  250. */
  251. tif->tif_decoderow = LZWDecodeCompat;
  252. tif->tif_decodestrip = LZWDecodeCompat;
  253. tif->tif_decodetile = LZWDecodeCompat;
  254. /*
  255. * If doing horizontal differencing, must
  256. * re-setup the predictor logic since we
  257. * switched the basic decoder methods...
  258. */
  259. (*tif->tif_setupdecode)(tif);
  260. sp->dec_decode = LZWDecodeCompat;
  261. }
  262. sp->lzw_maxcode = MAXCODE(BITS_MIN);
  263. #else /* !LZW_COMPAT */
  264. if (!sp->dec_decode) {
  265. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  266. "Old-style LZW codes not supported");
  267. sp->dec_decode = LZWDecode;
  268. }
  269. return (0);
  270. #endif/* !LZW_COMPAT */
  271. } else {
  272. sp->lzw_maxcode = MAXCODE(BITS_MIN)-1;
  273. sp->dec_decode = LZWDecode;
  274. }
  275. sp->lzw_nbits = BITS_MIN;
  276. sp->lzw_nextbits = 0;
  277. sp->lzw_nextdata = 0;
  278. sp->dec_restart = 0;
  279. sp->dec_nbitsmask = MAXCODE(BITS_MIN);
  280. #ifdef LZW_CHECKEOS
  281. sp->dec_bitsleft = tif->tif_rawcc << 3;
  282. #endif
  283. sp->dec_free_entp = sp->dec_codetab + CODE_FIRST;
  284. /*
  285. * Zero entries that are not yet filled in. We do
  286. * this to guard against bogus input data that causes
  287. * us to index into undefined entries. If you can
  288. * come up with a way to safely bounds-check input codes
  289. * while decoding then you can remove this operation.
  290. */
  291. _TIFFmemset(sp->dec_free_entp, 0, (CSIZE-CODE_FIRST)*sizeof (code_t));
  292. sp->dec_oldcodep = &sp->dec_codetab[-1];
  293. sp->dec_maxcodep = &sp->dec_codetab[sp->dec_nbitsmask-1];
  294. return (1);
  295. }
  296. /*
  297. * Decode a "hunk of data".
  298. */
  299. #define GetNextCode(sp, bp, code) { \
  300. nextdata = (nextdata<<8) | *(bp)++; \
  301. nextbits += 8; \
  302. if (nextbits < nbits) { \
  303. nextdata = (nextdata<<8) | *(bp)++; \
  304. nextbits += 8; \
  305. } \
  306. code = (hcode_t)((nextdata >> (nextbits-nbits)) & nbitsmask); \
  307. nextbits -= nbits; \
  308. }
  309. static void
  310. codeLoop(TIFF* tif)
  311. {
  312. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  313. "LZWDecode: Bogus encoding, loop in the code table; scanline %d",
  314. tif->tif_row);
  315. }
  316. static int
  317. LZWDecode(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
  318. {
  319. LZWCodecState *sp = DecoderState(tif);
  320. char *op = (char*) op0;
  321. long occ = (long) occ0;
  322. char *tp;
  323. unsigned char *bp;
  324. hcode_t code;
  325. int len;
  326. long nbits, nextbits, nextdata, nbitsmask;
  327. code_t *codep, *free_entp, *maxcodep, *oldcodep;
  328. (void) s;
  329. assert(sp != NULL);
  330. assert(sp->dec_codetab != NULL);
  331. /*
  332. * Restart interrupted output operation.
  333. */
  334. if (sp->dec_restart) {
  335. long residue;
  336. codep = sp->dec_codep;
  337. residue = codep->length - sp->dec_restart;
  338. if (residue > occ) {
  339. /*
  340. * Residue from previous decode is sufficient
  341. * to satisfy decode request. Skip to the
  342. * start of the decoded string, place decoded
  343. * values in the output buffer, and return.
  344. */
  345. sp->dec_restart += occ;
  346. do {
  347. codep = codep->next;
  348. } while (--residue > occ && codep);
  349. if (codep) {
  350. tp = op + occ;
  351. do {
  352. *--tp = codep->value;
  353. codep = codep->next;
  354. } while (--occ && codep);
  355. }
  356. return (1);
  357. }
  358. /*
  359. * Residue satisfies only part of the decode request.
  360. */
  361. op += residue, occ -= residue;
  362. tp = op;
  363. do {
  364. int t;
  365. --tp;
  366. t = codep->value;
  367. codep = codep->next;
  368. *tp = t;
  369. } while (--residue && codep);
  370. sp->dec_restart = 0;
  371. }
  372. bp = (unsigned char *)tif->tif_rawcp;
  373. nbits = sp->lzw_nbits;
  374. nextdata = sp->lzw_nextdata;
  375. nextbits = sp->lzw_nextbits;
  376. nbitsmask = sp->dec_nbitsmask;
  377. oldcodep = sp->dec_oldcodep;
  378. free_entp = sp->dec_free_entp;
  379. maxcodep = sp->dec_maxcodep;
  380. while (occ > 0) {
  381. NextCode(tif, sp, bp, code, GetNextCode);
  382. if (code == CODE_EOI)
  383. break;
  384. if (code == CODE_CLEAR) {
  385. free_entp = sp->dec_codetab + CODE_FIRST;
  386. _TIFFmemset(free_entp, 0,
  387. (CSIZE - CODE_FIRST) * sizeof (code_t));
  388. nbits = BITS_MIN;
  389. nbitsmask = MAXCODE(BITS_MIN);
  390. maxcodep = sp->dec_codetab + nbitsmask-1;
  391. NextCode(tif, sp, bp, code, GetNextCode);
  392. if (code == CODE_EOI)
  393. break;
  394. if (code == CODE_CLEAR) {
  395. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  396. "LZWDecode: Corrupted LZW table at scanline %d",
  397. tif->tif_row);
  398. return (0);
  399. }
  400. *op++ = (char)code, occ--;
  401. oldcodep = sp->dec_codetab + code;
  402. continue;
  403. }
  404. codep = sp->dec_codetab + code;
  405. /*
  406. * Add the new entry to the code table.
  407. */
  408. if (free_entp < &sp->dec_codetab[0] ||
  409. free_entp >= &sp->dec_codetab[CSIZE]) {
  410. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  411. "LZWDecode: Corrupted LZW table at scanline %d",
  412. tif->tif_row);
  413. return (0);
  414. }
  415. free_entp->next = oldcodep;
  416. if (free_entp->next < &sp->dec_codetab[0] ||
  417. free_entp->next >= &sp->dec_codetab[CSIZE]) {
  418. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  419. "LZWDecode: Corrupted LZW table at scanline %d",
  420. tif->tif_row);
  421. return (0);
  422. }
  423. free_entp->firstchar = free_entp->next->firstchar;
  424. free_entp->length = free_entp->next->length+1;
  425. free_entp->value = (codep < free_entp) ?
  426. codep->firstchar : free_entp->firstchar;
  427. if (++free_entp > maxcodep) {
  428. if (++nbits > BITS_MAX) /* should not happen */
  429. nbits = BITS_MAX;
  430. nbitsmask = MAXCODE(nbits);
  431. maxcodep = sp->dec_codetab + nbitsmask-1;
  432. }
  433. oldcodep = codep;
  434. if (code >= 256) {
  435. /*
  436. * Code maps to a string, copy string
  437. * value to output (written in reverse).
  438. */
  439. if(codep->length == 0) {
  440. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  441. "LZWDecode: Wrong length of decoded string: "
  442. "data probably corrupted at scanline %d",
  443. tif->tif_row);
  444. return (0);
  445. }
  446. if (codep->length > occ) {
  447. /*
  448. * String is too long for decode buffer,
  449. * locate portion that will fit, copy to
  450. * the decode buffer, and setup restart
  451. * logic for the next decoding call.
  452. */
  453. sp->dec_codep = codep;
  454. do {
  455. codep = codep->next;
  456. } while (codep && codep->length > occ);
  457. if (codep) {
  458. sp->dec_restart = occ;
  459. tp = op + occ;
  460. do {
  461. *--tp = codep->value;
  462. codep = codep->next;
  463. } while (--occ && codep);
  464. if (codep)
  465. codeLoop(tif);
  466. }
  467. break;
  468. }
  469. len = codep->length;
  470. tp = op + len;
  471. do {
  472. int t;
  473. --tp;
  474. t = codep->value;
  475. codep = codep->next;
  476. *tp = t;
  477. } while (codep && tp > op);
  478. if (codep) {
  479. codeLoop(tif);
  480. break;
  481. }
  482. op += len, occ -= len;
  483. } else
  484. *op++ = (char)code, occ--;
  485. }
  486. tif->tif_rawcp = (tidata_t) bp;
  487. sp->lzw_nbits = (unsigned short) nbits;
  488. sp->lzw_nextdata = nextdata;
  489. sp->lzw_nextbits = nextbits;
  490. sp->dec_nbitsmask = nbitsmask;
  491. sp->dec_oldcodep = oldcodep;
  492. sp->dec_free_entp = free_entp;
  493. sp->dec_maxcodep = maxcodep;
  494. if (occ > 0) {
  495. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  496. "LZWDecode: Not enough data at scanline %d (short %ld bytes)",
  497. tif->tif_row, occ);
  498. return (0);
  499. }
  500. return (1);
  501. }
  502. #ifdef LZW_COMPAT
  503. /*
  504. * Decode a "hunk of data" for old images.
  505. */
  506. #define GetNextCodeCompat(sp, bp, code) { \
  507. nextdata |= (unsigned long) *(bp)++ << nextbits; \
  508. nextbits += 8; \
  509. if (nextbits < nbits) { \
  510. nextdata |= (unsigned long) *(bp)++ << nextbits;\
  511. nextbits += 8; \
  512. } \
  513. code = (hcode_t)(nextdata & nbitsmask); \
  514. nextdata >>= nbits; \
  515. nextbits -= nbits; \
  516. }
  517. static int
  518. LZWDecodeCompat(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
  519. {
  520. LZWCodecState *sp = DecoderState(tif);
  521. char *op = (char*) op0;
  522. long occ = (long) occ0;
  523. char *tp;
  524. unsigned char *bp;
  525. int code, nbits;
  526. long nextbits, nextdata, nbitsmask;
  527. code_t *codep, *free_entp, *maxcodep, *oldcodep;
  528. (void) s;
  529. assert(sp != NULL);
  530. /*
  531. * Restart interrupted output operation.
  532. */
  533. if (sp->dec_restart) {
  534. long residue;
  535. codep = sp->dec_codep;
  536. residue = codep->length - sp->dec_restart;
  537. if (residue > occ) {
  538. /*
  539. * Residue from previous decode is sufficient
  540. * to satisfy decode request. Skip to the
  541. * start of the decoded string, place decoded
  542. * values in the output buffer, and return.
  543. */
  544. sp->dec_restart += occ;
  545. do {
  546. codep = codep->next;
  547. } while (--residue > occ);
  548. tp = op + occ;
  549. do {
  550. *--tp = codep->value;
  551. codep = codep->next;
  552. } while (--occ);
  553. return (1);
  554. }
  555. /*
  556. * Residue satisfies only part of the decode request.
  557. */
  558. op += residue, occ -= residue;
  559. tp = op;
  560. do {
  561. *--tp = codep->value;
  562. codep = codep->next;
  563. } while (--residue);
  564. sp->dec_restart = 0;
  565. }
  566. bp = (unsigned char *)tif->tif_rawcp;
  567. nbits = sp->lzw_nbits;
  568. nextdata = sp->lzw_nextdata;
  569. nextbits = sp->lzw_nextbits;
  570. nbitsmask = sp->dec_nbitsmask;
  571. oldcodep = sp->dec_oldcodep;
  572. free_entp = sp->dec_free_entp;
  573. maxcodep = sp->dec_maxcodep;
  574. while (occ > 0) {
  575. NextCode(tif, sp, bp, code, GetNextCodeCompat);
  576. if (code == CODE_EOI)
  577. break;
  578. if (code == CODE_CLEAR) {
  579. free_entp = sp->dec_codetab + CODE_FIRST;
  580. _TIFFmemset(free_entp, 0,
  581. (CSIZE - CODE_FIRST) * sizeof (code_t));
  582. nbits = BITS_MIN;
  583. nbitsmask = MAXCODE(BITS_MIN);
  584. maxcodep = sp->dec_codetab + nbitsmask;
  585. NextCode(tif, sp, bp, code, GetNextCodeCompat);
  586. if (code == CODE_EOI)
  587. break;
  588. if (code == CODE_CLEAR) {
  589. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  590. "LZWDecode: Corrupted LZW table at scanline %d",
  591. tif->tif_row);
  592. return (0);
  593. }
  594. *op++ = code, occ--;
  595. oldcodep = sp->dec_codetab + code;
  596. continue;
  597. }
  598. codep = sp->dec_codetab + code;
  599. /*
  600. * Add the new entry to the code table.
  601. */
  602. if (free_entp < &sp->dec_codetab[0] ||
  603. free_entp >= &sp->dec_codetab[CSIZE]) {
  604. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  605. "LZWDecodeCompat: Corrupted LZW table at scanline %d",
  606. tif->tif_row);
  607. return (0);
  608. }
  609. free_entp->next = oldcodep;
  610. if (free_entp->next < &sp->dec_codetab[0] ||
  611. free_entp->next >= &sp->dec_codetab[CSIZE]) {
  612. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  613. "LZWDecodeCompat: Corrupted LZW table at scanline %d",
  614. tif->tif_row);
  615. return (0);
  616. }
  617. free_entp->firstchar = free_entp->next->firstchar;
  618. free_entp->length = free_entp->next->length+1;
  619. free_entp->value = (codep < free_entp) ?
  620. codep->firstchar : free_entp->firstchar;
  621. if (++free_entp > maxcodep) {
  622. if (++nbits > BITS_MAX) /* should not happen */
  623. nbits = BITS_MAX;
  624. nbitsmask = MAXCODE(nbits);
  625. maxcodep = sp->dec_codetab + nbitsmask;
  626. }
  627. oldcodep = codep;
  628. if (code >= 256) {
  629. char *op_orig = op;
  630. /*
  631. * Code maps to a string, copy string
  632. * value to output (written in reverse).
  633. */
  634. if(codep->length == 0) {
  635. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  636. "LZWDecodeCompat: Wrong length of decoded "
  637. "string: data probably corrupted at scanline %d",
  638. tif->tif_row);
  639. return (0);
  640. }
  641. if (codep->length > occ) {
  642. /*
  643. * String is too long for decode buffer,
  644. * locate portion that will fit, copy to
  645. * the decode buffer, and setup restart
  646. * logic for the next decoding call.
  647. */
  648. sp->dec_codep = codep;
  649. do {
  650. codep = codep->next;
  651. } while (codep->length > occ);
  652. sp->dec_restart = occ;
  653. tp = op + occ;
  654. do {
  655. *--tp = codep->value;
  656. codep = codep->next;
  657. } while (--occ);
  658. break;
  659. }
  660. op += codep->length, occ -= codep->length;
  661. tp = op;
  662. do {
  663. *--tp = codep->value;
  664. } while( (codep = codep->next) != NULL && tp > op_orig);
  665. } else
  666. *op++ = code, occ--;
  667. }
  668. tif->tif_rawcp = (tidata_t) bp;
  669. sp->lzw_nbits = nbits;
  670. sp->lzw_nextdata = nextdata;
  671. sp->lzw_nextbits = nextbits;
  672. sp->dec_nbitsmask = nbitsmask;
  673. sp->dec_oldcodep = oldcodep;
  674. sp->dec_free_entp = free_entp;
  675. sp->dec_maxcodep = maxcodep;
  676. if (occ > 0) {
  677. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  678. "LZWDecodeCompat: Not enough data at scanline %d (short %ld bytes)",
  679. tif->tif_row, occ);
  680. return (0);
  681. }
  682. return (1);
  683. }
  684. #endif /* LZW_COMPAT */
  685. /*
  686. * LZW Encoding.
  687. */
  688. static int
  689. LZWSetupEncode(TIFF* tif)
  690. {
  691. LZWCodecState* sp = EncoderState(tif);
  692. static const char module[] = "LZWSetupEncode";
  693. assert(sp != NULL);
  694. sp->enc_hashtab = (hash_t*) _TIFFmalloc(HSIZE*sizeof (hash_t));
  695. if (sp->enc_hashtab == NULL) {
  696. TIFFErrorExt(tif->tif_clientdata, module, "No space for LZW hash table");
  697. return (0);
  698. }
  699. return (1);
  700. }
  701. /*
  702. * Reset encoding state at the start of a strip.
  703. */
  704. static int
  705. LZWPreEncode(TIFF* tif, tsample_t s)
  706. {
  707. LZWCodecState *sp = EncoderState(tif);
  708. (void) s;
  709. assert(sp != NULL);
  710. if( sp->enc_hashtab == NULL )
  711. {
  712. tif->tif_setupencode( tif );
  713. }
  714. sp->lzw_nbits = BITS_MIN;
  715. sp->lzw_maxcode = MAXCODE(BITS_MIN);
  716. sp->lzw_free_ent = CODE_FIRST;
  717. sp->lzw_nextbits = 0;
  718. sp->lzw_nextdata = 0;
  719. sp->enc_checkpoint = CHECK_GAP;
  720. sp->enc_ratio = 0;
  721. sp->enc_incount = 0;
  722. sp->enc_outcount = 0;
  723. /*
  724. * The 4 here insures there is space for 2 max-sized
  725. * codes in LZWEncode and LZWPostDecode.
  726. */
  727. sp->enc_rawlimit = tif->tif_rawdata + tif->tif_rawdatasize-1 - 4;
  728. cl_hash(sp); /* clear hash table */
  729. sp->enc_oldcode = (hcode_t) -1; /* generates CODE_CLEAR in LZWEncode */
  730. return (1);
  731. }
  732. #define CALCRATIO(sp, rat) { \
  733. if (incount > 0x007fffff) { /* NB: shift will overflow */\
  734. rat = outcount >> 8; \
  735. rat = (rat == 0 ? 0x7fffffff : incount/rat); \
  736. } else \
  737. rat = (incount<<8) / outcount; \
  738. }
  739. #define PutNextCode(op, c) { \
  740. nextdata = (nextdata << nbits) | c; \
  741. nextbits += nbits; \
  742. *op++ = (unsigned char)(nextdata >> (nextbits-8)); \
  743. nextbits -= 8; \
  744. if (nextbits >= 8) { \
  745. *op++ = (unsigned char)(nextdata >> (nextbits-8)); \
  746. nextbits -= 8; \
  747. } \
  748. outcount += nbits; \
  749. }
  750. /*
  751. * Encode a chunk of pixels.
  752. *
  753. * Uses an open addressing double hashing (no chaining) on the
  754. * prefix code/next character combination. We do a variant of
  755. * Knuth's algorithm D (vol. 3, sec. 6.4) along with G. Knott's
  756. * relatively-prime secondary probe. Here, the modular division
  757. * first probe is gives way to a faster exclusive-or manipulation.
  758. * Also do block compression with an adaptive reset, whereby the
  759. * code table is cleared when the compression ratio decreases,
  760. * but after the table fills. The variable-length output codes
  761. * are re-sized at this point, and a CODE_CLEAR is generated
  762. * for the decoder.
  763. */
  764. static int
  765. LZWEncode(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
  766. {
  767. register LZWCodecState *sp = EncoderState(tif);
  768. register long fcode;
  769. register hash_t *hp;
  770. register int h, c;
  771. hcode_t ent;
  772. long disp;
  773. long incount, outcount, checkpoint;
  774. long nextdata, nextbits;
  775. int free_ent, maxcode, nbits;
  776. tidata_t op, limit;
  777. (void) s;
  778. if (sp == NULL)
  779. return (0);
  780. assert(sp->enc_hashtab != NULL);
  781. /*
  782. * Load local state.
  783. */
  784. incount = sp->enc_incount;
  785. outcount = sp->enc_outcount;
  786. checkpoint = sp->enc_checkpoint;
  787. nextdata = sp->lzw_nextdata;
  788. nextbits = sp->lzw_nextbits;
  789. free_ent = sp->lzw_free_ent;
  790. maxcode = sp->lzw_maxcode;
  791. nbits = sp->lzw_nbits;
  792. op = tif->tif_rawcp;
  793. limit = sp->enc_rawlimit;
  794. ent = sp->enc_oldcode;
  795. if (ent == (hcode_t) -1 && cc > 0) {
  796. /*
  797. * NB: This is safe because it can only happen
  798. * at the start of a strip where we know there
  799. * is space in the data buffer.
  800. */
  801. PutNextCode(op, CODE_CLEAR);
  802. ent = *bp++; cc--; incount++;
  803. }
  804. while (cc > 0) {
  805. c = *bp++; cc--; incount++;
  806. fcode = ((long)c << BITS_MAX) + ent;
  807. h = (c << HSHIFT) ^ ent; /* xor hashing */
  808. #ifdef _WINDOWS
  809. /*
  810. * Check hash index for an overflow.
  811. */
  812. if (h >= HSIZE)
  813. h -= HSIZE;
  814. #endif
  815. hp = &sp->enc_hashtab[h];
  816. if (hp->hash == fcode) {
  817. ent = hp->code;
  818. continue;
  819. }
  820. if (hp->hash >= 0) {
  821. /*
  822. * Primary hash failed, check secondary hash.
  823. */
  824. disp = HSIZE - h;
  825. if (h == 0)
  826. disp = 1;
  827. do {
  828. /*
  829. * Avoid pointer arithmetic 'cuz of
  830. * wraparound problems with segments.
  831. */
  832. if ((h -= disp) < 0)
  833. h += HSIZE;
  834. hp = &sp->enc_hashtab[h];
  835. if (hp->hash == fcode) {
  836. ent = hp->code;
  837. goto hit;
  838. }
  839. } while (hp->hash >= 0);
  840. }
  841. /*
  842. * New entry, emit code and add to table.
  843. */
  844. /*
  845. * Verify there is space in the buffer for the code
  846. * and any potential Clear code that might be emitted
  847. * below. The value of limit is setup so that there
  848. * are at least 4 bytes free--room for 2 codes.
  849. */
  850. if (op > limit) {
  851. tif->tif_rawcc = (tsize_t)(op - tif->tif_rawdata);
  852. TIFFFlushData1(tif);
  853. op = tif->tif_rawdata;
  854. }
  855. PutNextCode(op, ent);
  856. ent = c;
  857. hp->code = free_ent++;
  858. hp->hash = fcode;
  859. if (free_ent == CODE_MAX-1) {
  860. /* table is full, emit clear code and reset */
  861. cl_hash(sp);
  862. sp->enc_ratio = 0;
  863. incount = 0;
  864. outcount = 0;
  865. free_ent = CODE_FIRST;
  866. PutNextCode(op, CODE_CLEAR);
  867. nbits = BITS_MIN;
  868. maxcode = MAXCODE(BITS_MIN);
  869. } else {
  870. /*
  871. * If the next entry is going to be too big for
  872. * the code size, then increase it, if possible.
  873. */
  874. if (free_ent > maxcode) {
  875. nbits++;
  876. assert(nbits <= BITS_MAX);
  877. maxcode = (int) MAXCODE(nbits);
  878. } else if (incount >= checkpoint) {
  879. long rat;
  880. /*
  881. * Check compression ratio and, if things seem
  882. * to be slipping, clear the hash table and
  883. * reset state. The compression ratio is a
  884. * 24+8-bit fractional number.
  885. */
  886. checkpoint = incount+CHECK_GAP;
  887. CALCRATIO(sp, rat);
  888. if (rat <= sp->enc_ratio) {
  889. cl_hash(sp);
  890. sp->enc_ratio = 0;
  891. incount = 0;
  892. outcount = 0;
  893. free_ent = CODE_FIRST;
  894. PutNextCode(op, CODE_CLEAR);
  895. nbits = BITS_MIN;
  896. maxcode = MAXCODE(BITS_MIN);
  897. } else
  898. sp->enc_ratio = rat;
  899. }
  900. }
  901. hit:
  902. ;
  903. }
  904. /*
  905. * Restore global state.
  906. */
  907. sp->enc_incount = incount;
  908. sp->enc_outcount = outcount;
  909. sp->enc_checkpoint = checkpoint;
  910. sp->enc_oldcode = ent;
  911. sp->lzw_nextdata = nextdata;
  912. sp->lzw_nextbits = nextbits;
  913. sp->lzw_free_ent = free_ent;
  914. sp->lzw_maxcode = maxcode;
  915. sp->lzw_nbits = nbits;
  916. tif->tif_rawcp = op;
  917. return (1);
  918. }
  919. /*
  920. * Finish off an encoded strip by flushing the last
  921. * string and tacking on an End Of Information code.
  922. */
  923. static int
  924. LZWPostEncode(TIFF* tif)
  925. {
  926. register LZWCodecState *sp = EncoderState(tif);
  927. tidata_t op = tif->tif_rawcp;
  928. long nextbits = sp->lzw_nextbits;
  929. long nextdata = sp->lzw_nextdata;
  930. long outcount = sp->enc_outcount;
  931. int nbits = sp->lzw_nbits;
  932. if (op > sp->enc_rawlimit) {
  933. tif->tif_rawcc = (tsize_t)(op - tif->tif_rawdata);
  934. TIFFFlushData1(tif);
  935. op = tif->tif_rawdata;
  936. }
  937. if (sp->enc_oldcode != (hcode_t) -1) {
  938. PutNextCode(op, sp->enc_oldcode);
  939. sp->enc_oldcode = (hcode_t) -1;
  940. }
  941. PutNextCode(op, CODE_EOI);
  942. if (nextbits > 0)
  943. *op++ = (unsigned char)(nextdata << (8-nextbits));
  944. tif->tif_rawcc = (tsize_t)(op - tif->tif_rawdata);
  945. return (1);
  946. }
  947. /*
  948. * Reset encoding hash table.
  949. */
  950. static void
  951. cl_hash(LZWCodecState* sp)
  952. {
  953. register hash_t *hp = &sp->enc_hashtab[HSIZE-1];
  954. register long i = HSIZE-8;
  955. do {
  956. i -= 8;
  957. hp[-7].hash = -1;
  958. hp[-6].hash = -1;
  959. hp[-5].hash = -1;
  960. hp[-4].hash = -1;
  961. hp[-3].hash = -1;
  962. hp[-2].hash = -1;
  963. hp[-1].hash = -1;
  964. hp[ 0].hash = -1;
  965. hp -= 8;
  966. } while (i >= 0);
  967. for (i += 8; i > 0; i--, hp--)
  968. hp->hash = -1;
  969. }
  970. static void
  971. LZWCleanup(TIFF* tif)
  972. {
  973. (void)TIFFPredictorCleanup(tif);
  974. assert(tif->tif_data != 0);
  975. if (DecoderState(tif)->dec_codetab)
  976. _TIFFfree(DecoderState(tif)->dec_codetab);
  977. if (EncoderState(tif)->enc_hashtab)
  978. _TIFFfree(EncoderState(tif)->enc_hashtab);
  979. _TIFFfree(tif->tif_data);
  980. tif->tif_data = NULL;
  981. _TIFFSetDefaultCompressionState(tif);
  982. }
  983. int
  984. TIFFInitLZW(TIFF* tif, int scheme)
  985. {
  986. assert(scheme == COMPRESSION_LZW);
  987. /*
  988. * Allocate state block so tag methods have storage to record values.
  989. */
  990. tif->tif_data = (tidata_t) _TIFFmalloc(sizeof (LZWCodecState));
  991. if (tif->tif_data == NULL)
  992. goto bad;
  993. DecoderState(tif)->dec_codetab = NULL;
  994. DecoderState(tif)->dec_decode = NULL;
  995. EncoderState(tif)->enc_hashtab = NULL;
  996. LZWState(tif)->rw_mode = tif->tif_mode;
  997. /*
  998. * Install codec methods.
  999. */
  1000. tif->tif_setupdecode = LZWSetupDecode;
  1001. tif->tif_predecode = LZWPreDecode;
  1002. tif->tif_decoderow = LZWDecode;
  1003. tif->tif_decodestrip = LZWDecode;
  1004. tif->tif_decodetile = LZWDecode;
  1005. tif->tif_setupencode = LZWSetupEncode;
  1006. tif->tif_preencode = LZWPreEncode;
  1007. tif->tif_postencode = LZWPostEncode;
  1008. tif->tif_encoderow = LZWEncode;
  1009. tif->tif_encodestrip = LZWEncode;
  1010. tif->tif_encodetile = LZWEncode;
  1011. tif->tif_cleanup = LZWCleanup;
  1012. /*
  1013. * Setup predictor setup.
  1014. */
  1015. (void) TIFFPredictorInit(tif);
  1016. return (1);
  1017. bad:
  1018. TIFFErrorExt(tif->tif_clientdata, "TIFFInitLZW",
  1019. "No space for LZW state block");
  1020. return (0);
  1021. }
  1022. /*
  1023. * Copyright (c) 1985, 1986 The Regents of the University of California.
  1024. * All rights reserved.
  1025. *
  1026. * This code is derived from software contributed to Berkeley by
  1027. * James A. Woods, derived from original work by Spencer Thomas
  1028. * and Joseph Orost.
  1029. *
  1030. * Redistribution and use in source and binary forms are permitted
  1031. * provided that the above copyright notice and this paragraph are
  1032. * duplicated in all such forms and that any documentation,
  1033. * advertising materials, and other materials related to such
  1034. * distribution and use acknowledge that the software was developed
  1035. * by the University of California, Berkeley. The name of the
  1036. * University may not be used to endorse or promote products derived
  1037. * from this software without specific prior written permission.
  1038. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  1039. * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  1040. * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  1041. */
  1042. #endif /* LZW_SUPPORT */
  1043. /* vim: set ts=8 sts=8 sw=8 noet: */
  1044. /*
  1045. * Local Variables:
  1046. * mode: c
  1047. * c-basic-offset: 8
  1048. * fill-column: 78
  1049. * End:
  1050. */