tif_lzw.c 46 KB

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