tif_lzw.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  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. /*
  155. * LZW Decoder.
  156. */
  157. static int LZWFixupTags(TIFF *tif)
  158. {
  159. (void)tif;
  160. return (1);
  161. }
  162. static int LZWSetupDecode(TIFF *tif)
  163. {
  164. static const char module[] = "LZWSetupDecode";
  165. LZWCodecState *sp = LZWDecoderState(tif);
  166. int code;
  167. if (sp == NULL)
  168. {
  169. /*
  170. * Allocate state block so tag methods have storage to record
  171. * values.
  172. */
  173. tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(LZWCodecState));
  174. if (tif->tif_data == NULL)
  175. {
  176. TIFFErrorExtR(tif, module, "No space for LZW state block");
  177. return (0);
  178. }
  179. sp = LZWDecoderState(tif);
  180. sp->dec_codetab = NULL;
  181. sp->dec_decode = NULL;
  182. /*
  183. * Setup predictor setup.
  184. */
  185. (void)TIFFPredictorInit(tif);
  186. }
  187. if (sp->dec_codetab == NULL)
  188. {
  189. sp->dec_codetab = (code_t *)_TIFFmallocExt(tif, CSIZE * sizeof(code_t));
  190. if (sp->dec_codetab == NULL)
  191. {
  192. TIFFErrorExtR(tif, module, "No space for LZW code table");
  193. return (0);
  194. }
  195. /*
  196. * Pre-load the table.
  197. */
  198. code = 255;
  199. do
  200. {
  201. sp->dec_codetab[code].firstchar = (unsigned char)code;
  202. sp->dec_codetab[code].value = (unsigned char)code;
  203. sp->dec_codetab[code].repeated = true;
  204. sp->dec_codetab[code].length = 1;
  205. sp->dec_codetab[code].next = NULL;
  206. } while (code--);
  207. /*
  208. * Zero-out the unused entries */
  209. /* Silence false positive */
  210. /* coverity[overrun-buffer-arg] */
  211. memset(&sp->dec_codetab[CODE_CLEAR], 0,
  212. (CODE_FIRST - CODE_CLEAR) * sizeof(code_t));
  213. }
  214. return (1);
  215. }
  216. /*
  217. * Setup state for decoding a strip.
  218. */
  219. static int LZWPreDecode(TIFF *tif, uint16_t s)
  220. {
  221. static const char module[] = "LZWPreDecode";
  222. LZWCodecState *sp = LZWDecoderState(tif);
  223. (void)s;
  224. assert(sp != NULL);
  225. if (sp->dec_codetab == NULL)
  226. {
  227. tif->tif_setupdecode(tif);
  228. if (sp->dec_codetab == NULL)
  229. return (0);
  230. }
  231. /*
  232. * Check for old bit-reversed codes.
  233. */
  234. if (tif->tif_rawcc >= 2 && tif->tif_rawdata[0] == 0 &&
  235. (tif->tif_rawdata[1] & 0x1))
  236. {
  237. #ifdef LZW_COMPAT
  238. if (!sp->dec_decode)
  239. {
  240. TIFFWarningExtR(tif, module, "Old-style LZW codes, convert file");
  241. /*
  242. * Override default decoding methods with
  243. * ones that deal with the old coding.
  244. * Otherwise the predictor versions set
  245. * above will call the compatibility routines
  246. * through the dec_decode method.
  247. */
  248. tif->tif_decoderow = LZWDecodeCompat;
  249. tif->tif_decodestrip = LZWDecodeCompat;
  250. tif->tif_decodetile = LZWDecodeCompat;
  251. /*
  252. * If doing horizontal differencing, must
  253. * re-setup the predictor logic since we
  254. * switched the basic decoder methods...
  255. */
  256. (*tif->tif_setupdecode)(tif);
  257. sp->dec_decode = LZWDecodeCompat;
  258. }
  259. sp->lzw_maxcode = MAXCODE(BITS_MIN);
  260. #else /* !LZW_COMPAT */
  261. if (!sp->dec_decode)
  262. {
  263. TIFFErrorExtR(tif, module, "Old-style LZW codes not supported");
  264. sp->dec_decode = LZWDecode;
  265. }
  266. return (0);
  267. #endif /* !LZW_COMPAT */
  268. }
  269. else
  270. {
  271. sp->lzw_maxcode = MAXCODE(BITS_MIN) - 1;
  272. sp->dec_decode = LZWDecode;
  273. }
  274. sp->lzw_nbits = BITS_MIN;
  275. sp->lzw_nextbits = 0;
  276. sp->lzw_nextdata = 0;
  277. sp->dec_restart = 0;
  278. sp->dec_nbitsmask = MAXCODE(BITS_MIN);
  279. sp->dec_bitsleft = 0;
  280. sp->old_tif_rawcc = 0;
  281. sp->dec_free_entp = sp->dec_codetab - 1; // + CODE_FIRST;
  282. /*
  283. * Zero entries that are not yet filled in. We do
  284. * this to guard against bogus input data that causes
  285. * us to index into undefined entries. If you can
  286. * come up with a way to safely bounds-check input codes
  287. * while decoding then you can remove this operation.
  288. */
  289. sp->dec_oldcodep = &sp->dec_codetab[0];
  290. sp->dec_maxcodep = &sp->dec_codetab[sp->dec_nbitsmask - 1];
  291. sp->read_error = 0;
  292. return (1);
  293. }
  294. /*
  295. * Decode a "hunk of data".
  296. */
  297. /* Get the next 32 or 64-bit from the input data */
  298. #ifdef WORDS_BIGENDIAN
  299. #define GetNextData(nextdata, bp) memcpy(&nextdata, bp, sizeof(nextdata))
  300. #elif SIZEOF_WORDTYPE == 8
  301. #if defined(_M_X64)
  302. #define GetNextData(nextdata, bp) nextdata = _byteswap_uint64(*(uint64_t *)(bp))
  303. #elif defined(__GNUC__)
  304. #define GetNextData(nextdata, bp) \
  305. memcpy(&nextdata, bp, sizeof(nextdata)); \
  306. nextdata = __builtin_bswap64(nextdata)
  307. #else
  308. #define GetNextData(nextdata, bp) \
  309. nextdata = (((uint64_t)bp[0]) << 56) | (((uint64_t)bp[1]) << 48) | \
  310. (((uint64_t)bp[2]) << 40) | (((uint64_t)bp[3]) << 32) | \
  311. (((uint64_t)bp[4]) << 24) | (((uint64_t)bp[5]) << 16) | \
  312. (((uint64_t)bp[6]) << 8) | (((uint64_t)bp[7]))
  313. #endif
  314. #elif SIZEOF_WORDTYPE == 4
  315. #if defined(_M_X86)
  316. #define GetNextData(nextdata, bp) \
  317. nextdata = _byteswap_ulong(*(unsigned long *)(bp))
  318. #elif defined(__GNUC__)
  319. #define GetNextData(nextdata, bp) \
  320. memcpy(&nextdata, bp, sizeof(nextdata)); \
  321. nextdata = __builtin_bswap32(nextdata)
  322. #else
  323. #define GetNextData(nextdata, bp) \
  324. nextdata = (((uint32_t)bp[0]) << 24) | (((uint32_t)bp[1]) << 16) | \
  325. (((uint32_t)bp[2]) << 8) | (((uint32_t)bp[3]))
  326. #endif
  327. #else
  328. #error "Unhandled SIZEOF_WORDTYPE"
  329. #endif
  330. #define GetNextCodeLZW() \
  331. do \
  332. { \
  333. nextbits -= nbits; \
  334. if (nextbits < 0) \
  335. { \
  336. if (dec_bitsleft >= 8 * SIZEOF_WORDTYPE) \
  337. { \
  338. unsigned codetmp = (unsigned)(nextdata << (-nextbits)); \
  339. GetNextData(nextdata, bp); \
  340. bp += SIZEOF_WORDTYPE; \
  341. nextbits += 8 * SIZEOF_WORDTYPE; \
  342. dec_bitsleft -= 8 * SIZEOF_WORDTYPE; \
  343. code = (WordType)((codetmp | (nextdata >> nextbits)) & \
  344. nbitsmask); \
  345. break; \
  346. } \
  347. else \
  348. { \
  349. if (dec_bitsleft < 8) \
  350. { \
  351. goto no_eoi; \
  352. } \
  353. nextdata = (nextdata << 8) | *(bp)++; \
  354. nextbits += 8; \
  355. dec_bitsleft -= 8; \
  356. if (nextbits < 0) \
  357. { \
  358. if (dec_bitsleft < 8) \
  359. { \
  360. goto no_eoi; \
  361. } \
  362. nextdata = (nextdata << 8) | *(bp)++; \
  363. nextbits += 8; \
  364. dec_bitsleft -= 8; \
  365. } \
  366. } \
  367. } \
  368. code = (WordType)((nextdata >> nextbits) & nbitsmask); \
  369. } while (0)
  370. static int LZWDecode(TIFF *tif, uint8_t *op0, tmsize_t occ0, uint16_t s)
  371. {
  372. static const char module[] = "LZWDecode";
  373. LZWCodecState *sp = LZWDecoderState(tif);
  374. uint8_t *op = (uint8_t *)op0;
  375. tmsize_t occ = occ0;
  376. uint8_t *bp;
  377. long nbits, nextbits, nbitsmask;
  378. WordType nextdata;
  379. code_t *free_entp, *maxcodep, *oldcodep;
  380. (void)s;
  381. assert(sp != NULL);
  382. assert(sp->dec_codetab != NULL);
  383. if (sp->read_error)
  384. {
  385. memset(op, 0, (size_t)occ);
  386. TIFFErrorExtR(tif, module,
  387. "LZWDecode: Scanline %" PRIu32 " cannot be read due to "
  388. "previous error",
  389. tif->tif_row);
  390. return 0;
  391. }
  392. /*
  393. * Restart interrupted output operation.
  394. */
  395. if (sp->dec_restart)
  396. {
  397. tmsize_t residue;
  398. code_t *codep = sp->dec_codep;
  399. residue = codep->length - sp->dec_restart;
  400. if (residue > occ)
  401. {
  402. /*
  403. * Residue from previous decode is sufficient
  404. * to satisfy decode request. Skip to the
  405. * start of the decoded string, place decoded
  406. * values in the output buffer, and return.
  407. */
  408. sp->dec_restart += occ;
  409. do
  410. {
  411. codep = codep->next;
  412. } while (--residue > occ && codep);
  413. if (codep)
  414. {
  415. uint8_t *tp = op + occ;
  416. do
  417. {
  418. *--tp = codep->value;
  419. codep = codep->next;
  420. } while (--occ && codep);
  421. }
  422. return (1);
  423. }
  424. /*
  425. * Residue satisfies only part of the decode request.
  426. */
  427. op += residue;
  428. occ -= residue;
  429. uint8_t *tp = op;
  430. do
  431. {
  432. *--tp = codep->value;
  433. codep = codep->next;
  434. } while (--residue && codep);
  435. sp->dec_restart = 0;
  436. }
  437. bp = (uint8_t *)tif->tif_rawcp;
  438. sp->dec_bitsleft += (((uint64_t)tif->tif_rawcc - sp->old_tif_rawcc) << 3);
  439. uint64_t dec_bitsleft = sp->dec_bitsleft;
  440. nbits = sp->lzw_nbits;
  441. nextdata = sp->lzw_nextdata;
  442. nextbits = sp->lzw_nextbits;
  443. nbitsmask = sp->dec_nbitsmask;
  444. oldcodep = sp->dec_oldcodep;
  445. free_entp = sp->dec_free_entp;
  446. maxcodep = sp->dec_maxcodep;
  447. code_t *const dec_codetab = sp->dec_codetab;
  448. code_t *codep;
  449. if (occ == 0)
  450. {
  451. goto after_loop;
  452. }
  453. begin:
  454. {
  455. WordType code;
  456. GetNextCodeLZW();
  457. codep = dec_codetab + code;
  458. if (code >= CODE_FIRST)
  459. goto code_above_or_equal_to_258;
  460. if (code < 256)
  461. goto code_below_256;
  462. if (code == CODE_EOI)
  463. goto after_loop;
  464. goto code_clear;
  465. code_below_256:
  466. {
  467. if (codep > free_entp)
  468. goto error_code;
  469. free_entp->next = oldcodep;
  470. free_entp->firstchar = oldcodep->firstchar;
  471. free_entp->length = oldcodep->length + 1;
  472. free_entp->value = (uint8_t)code;
  473. free_entp->repeated =
  474. (bool)(oldcodep->repeated & (oldcodep->value == code));
  475. if (++free_entp > maxcodep)
  476. {
  477. if (++nbits > BITS_MAX) /* should not happen for a conformant encoder */
  478. nbits = BITS_MAX;
  479. nbitsmask = MAXCODE(nbits);
  480. maxcodep = dec_codetab + nbitsmask - 1;
  481. if (free_entp >= &dec_codetab[CSIZE])
  482. {
  483. /* At that point, the next valid states are either EOI or a */
  484. /* CODE_CLEAR. If a regular code is read, at the next */
  485. /* attempt at registering a new entry, we will error out */
  486. /* due to setting free_entp before any valid code */
  487. free_entp = dec_codetab - 1;
  488. }
  489. }
  490. oldcodep = codep;
  491. *op++ = (uint8_t)code;
  492. occ--;
  493. if (occ == 0)
  494. goto after_loop;
  495. goto begin;
  496. }
  497. code_above_or_equal_to_258:
  498. {
  499. /*
  500. * Add the new entry to the code table.
  501. */
  502. if (codep >= free_entp)
  503. {
  504. if (codep != free_entp)
  505. goto error_code;
  506. free_entp->value = oldcodep->firstchar;
  507. }
  508. else
  509. {
  510. free_entp->value = codep->firstchar;
  511. }
  512. free_entp->repeated =
  513. (bool)(oldcodep->repeated & (oldcodep->value == free_entp->value));
  514. free_entp->next = oldcodep;
  515. free_entp->firstchar = oldcodep->firstchar;
  516. free_entp->length = oldcodep->length + 1;
  517. if (++free_entp > maxcodep)
  518. {
  519. if (++nbits > BITS_MAX) /* should not happen for a conformant encoder */
  520. nbits = BITS_MAX;
  521. nbitsmask = MAXCODE(nbits);
  522. maxcodep = dec_codetab + nbitsmask - 1;
  523. if (free_entp >= &dec_codetab[CSIZE])
  524. {
  525. /* At that point, the next valid states are either EOI or a */
  526. /* CODE_CLEAR. If a regular code is read, at the next */
  527. /* attempt at registering a new entry, we will error out */
  528. /* due to setting free_entp before any valid code */
  529. free_entp = dec_codetab - 1;
  530. }
  531. }
  532. oldcodep = codep;
  533. /*
  534. * Code maps to a string, copy string
  535. * value to output (written in reverse).
  536. */
  537. /* tiny bit faster on x86_64 to store in unsigned short than int */
  538. unsigned short len = codep->length;
  539. if (len < 3) /* equivalent to len == 2 given all other conditions */
  540. {
  541. if (occ <= 2)
  542. {
  543. if (occ == 2)
  544. {
  545. memcpy(op, &(codep->firstchar), 2);
  546. op += 2;
  547. occ -= 2;
  548. goto after_loop;
  549. }
  550. goto too_short_buffer;
  551. }
  552. memcpy(op, &(codep->firstchar), 2);
  553. op += 2;
  554. occ -= 2;
  555. goto begin; /* we can save the comparison occ > 0 */
  556. }
  557. if (len == 3)
  558. {
  559. if (occ <= 3)
  560. {
  561. if (occ == 3)
  562. {
  563. op[0] = codep->firstchar;
  564. op[1] = codep->next->value;
  565. op[2] = codep->value;
  566. op += 3;
  567. occ -= 3;
  568. goto after_loop;
  569. }
  570. goto too_short_buffer;
  571. }
  572. op[0] = codep->firstchar;
  573. op[1] = codep->next->value;
  574. op[2] = codep->value;
  575. op += 3;
  576. occ -= 3;
  577. goto begin; /* we can save the comparison occ > 0 */
  578. }
  579. if (len > occ)
  580. {
  581. goto too_short_buffer;
  582. }
  583. if (codep->repeated)
  584. {
  585. memset(op, codep->value, len);
  586. op += len;
  587. occ -= len;
  588. if (occ == 0)
  589. goto after_loop;
  590. goto begin;
  591. }
  592. uint8_t *tp = op + len;
  593. assert(len >= 4);
  594. *--tp = codep->value;
  595. codep = codep->next;
  596. *--tp = codep->value;
  597. codep = codep->next;
  598. *--tp = codep->value;
  599. codep = codep->next;
  600. *--tp = codep->value;
  601. if (tp > op)
  602. {
  603. do
  604. {
  605. codep = codep->next;
  606. *--tp = codep->value;
  607. } while (tp > op);
  608. }
  609. assert(occ >= len);
  610. op += len;
  611. occ -= len;
  612. if (occ == 0)
  613. goto after_loop;
  614. goto begin;
  615. }
  616. code_clear:
  617. {
  618. free_entp = dec_codetab + CODE_FIRST;
  619. nbits = BITS_MIN;
  620. nbitsmask = MAXCODE(BITS_MIN);
  621. maxcodep = dec_codetab + nbitsmask - 1;
  622. do
  623. {
  624. GetNextCodeLZW();
  625. } while (code == CODE_CLEAR); /* consecutive CODE_CLEAR codes */
  626. if (code == CODE_EOI)
  627. goto after_loop;
  628. if (code > CODE_EOI)
  629. {
  630. goto error_code;
  631. }
  632. *op++ = (uint8_t)code;
  633. occ--;
  634. oldcodep = dec_codetab + code;
  635. if (occ == 0)
  636. goto after_loop;
  637. goto begin;
  638. }
  639. }
  640. too_short_buffer:
  641. {
  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. {
  651. codep = codep->next;
  652. } while (codep->length > occ);
  653. sp->dec_restart = occ;
  654. uint8_t *tp = op + occ;
  655. do
  656. {
  657. *--tp = codep->value;
  658. codep = codep->next;
  659. } while (--occ);
  660. }
  661. after_loop:
  662. tif->tif_rawcc -= (tmsize_t)((uint8_t *)bp - tif->tif_rawcp);
  663. tif->tif_rawcp = (uint8_t *)bp;
  664. sp->old_tif_rawcc = tif->tif_rawcc;
  665. sp->dec_bitsleft = dec_bitsleft;
  666. sp->lzw_nbits = (unsigned short)nbits;
  667. sp->lzw_nextdata = nextdata;
  668. sp->lzw_nextbits = nextbits;
  669. sp->dec_nbitsmask = nbitsmask;
  670. sp->dec_oldcodep = oldcodep;
  671. sp->dec_free_entp = free_entp;
  672. sp->dec_maxcodep = maxcodep;
  673. if (occ > 0)
  674. {
  675. memset(op, 0, (size_t)occ);
  676. sp->read_error = 1;
  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. #ifndef LZW_READ_ONLY
  943. static void cl_hash(LZWCodecState *);
  944. /*
  945. * LZW Encoding.
  946. */
  947. static int LZWSetupEncode(TIFF *tif)
  948. {
  949. static const char module[] = "LZWSetupEncode";
  950. LZWCodecState *sp = LZWEncoderState(tif);
  951. assert(sp != NULL);
  952. sp->enc_hashtab = (hash_t *)_TIFFmallocExt(tif, HSIZE * sizeof(hash_t));
  953. if (sp->enc_hashtab == NULL)
  954. {
  955. TIFFErrorExtR(tif, module, "No space for LZW hash table");
  956. return (0);
  957. }
  958. return (1);
  959. }
  960. /*
  961. * Reset encoding state at the start of a strip.
  962. */
  963. static int LZWPreEncode(TIFF *tif, uint16_t s)
  964. {
  965. LZWCodecState *sp = LZWEncoderState(tif);
  966. (void)s;
  967. assert(sp != NULL);
  968. if (sp->enc_hashtab == NULL)
  969. {
  970. tif->tif_setupencode(tif);
  971. }
  972. sp->lzw_nbits = BITS_MIN;
  973. sp->lzw_maxcode = MAXCODE(BITS_MIN);
  974. sp->lzw_free_ent = CODE_FIRST;
  975. sp->lzw_nextbits = 0;
  976. sp->lzw_nextdata = 0;
  977. sp->enc_checkpoint = CHECK_GAP;
  978. sp->enc_ratio = 0;
  979. sp->enc_incount = 0;
  980. sp->enc_outcount = 0;
  981. /*
  982. * The 4 here insures there is space for 2 max-sized
  983. * codes in LZWEncode and LZWPostDecode.
  984. */
  985. sp->enc_rawlimit = tif->tif_rawdata + tif->tif_rawdatasize - 1 - 4;
  986. cl_hash(sp); /* clear hash table */
  987. sp->enc_oldcode = (hcode_t)-1; /* generates CODE_CLEAR in LZWEncode */
  988. return (1);
  989. }
  990. #define CALCRATIO(sp, rat) \
  991. { \
  992. if (incount > 0x007fffff) \
  993. { /* NB: shift will overflow */ \
  994. rat = outcount >> 8; \
  995. rat = (rat == 0 ? 0x7fffffff : incount / rat); \
  996. } \
  997. else \
  998. rat = (incount << 8) / outcount; \
  999. }
  1000. /* Explicit 0xff masking to make icc -check=conversions happy */
  1001. #define PutNextCode(op, c) \
  1002. { \
  1003. nextdata = (nextdata << nbits) | c; \
  1004. nextbits += nbits; \
  1005. *op++ = (unsigned char)((nextdata >> (nextbits - 8)) & 0xff); \
  1006. nextbits -= 8; \
  1007. if (nextbits >= 8) \
  1008. { \
  1009. *op++ = (unsigned char)((nextdata >> (nextbits - 8)) & 0xff); \
  1010. nextbits -= 8; \
  1011. } \
  1012. outcount += nbits; \
  1013. }
  1014. /*
  1015. * Encode a chunk of pixels.
  1016. *
  1017. * Uses an open addressing double hashing (no chaining) on the
  1018. * prefix code/next character combination. We do a variant of
  1019. * Knuth's algorithm D (vol. 3, sec. 6.4) along with G. Knott's
  1020. * relatively-prime secondary probe. Here, the modular division
  1021. * first probe is gives way to a faster exclusive-or manipulation.
  1022. * Also do block compression with an adaptive reset, whereby the
  1023. * code table is cleared when the compression ratio decreases,
  1024. * but after the table fills. The variable-length output codes
  1025. * are re-sized at this point, and a CODE_CLEAR is generated
  1026. * for the decoder.
  1027. */
  1028. static int LZWEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
  1029. {
  1030. register LZWCodecState *sp = LZWEncoderState(tif);
  1031. register long fcode;
  1032. register hash_t *hp;
  1033. register int h, c;
  1034. hcode_t ent;
  1035. long disp;
  1036. tmsize_t incount, outcount, checkpoint;
  1037. WordType nextdata;
  1038. long nextbits;
  1039. int free_ent, maxcode, nbits;
  1040. uint8_t *op;
  1041. uint8_t *limit;
  1042. (void)s;
  1043. if (sp == NULL)
  1044. return (0);
  1045. assert(sp->enc_hashtab != NULL);
  1046. /*
  1047. * Load local state.
  1048. */
  1049. incount = sp->enc_incount;
  1050. outcount = sp->enc_outcount;
  1051. checkpoint = sp->enc_checkpoint;
  1052. nextdata = sp->lzw_nextdata;
  1053. nextbits = sp->lzw_nextbits;
  1054. free_ent = sp->lzw_free_ent;
  1055. maxcode = sp->lzw_maxcode;
  1056. nbits = sp->lzw_nbits;
  1057. op = tif->tif_rawcp;
  1058. limit = sp->enc_rawlimit;
  1059. ent = (hcode_t)sp->enc_oldcode;
  1060. if (ent == (hcode_t)-1 && cc > 0)
  1061. {
  1062. /*
  1063. * NB: This is safe because it can only happen
  1064. * at the start of a strip where we know there
  1065. * is space in the data buffer.
  1066. */
  1067. PutNextCode(op, CODE_CLEAR);
  1068. ent = *bp++;
  1069. cc--;
  1070. incount++;
  1071. }
  1072. while (cc > 0)
  1073. {
  1074. c = *bp++;
  1075. cc--;
  1076. incount++;
  1077. fcode = ((long)c << BITS_MAX) + ent;
  1078. h = (c << HSHIFT) ^ ent; /* xor hashing */
  1079. #ifdef _WINDOWS
  1080. /*
  1081. * Check hash index for an overflow.
  1082. */
  1083. if (h >= HSIZE)
  1084. h -= HSIZE;
  1085. #endif
  1086. hp = &sp->enc_hashtab[h];
  1087. if (hp->hash == fcode)
  1088. {
  1089. ent = hp->code;
  1090. continue;
  1091. }
  1092. if (hp->hash >= 0)
  1093. {
  1094. /*
  1095. * Primary hash failed, check secondary hash.
  1096. */
  1097. disp = HSIZE - h;
  1098. if (h == 0)
  1099. disp = 1;
  1100. do
  1101. {
  1102. /*
  1103. * Avoid pointer arithmetic because of
  1104. * wraparound problems with segments.
  1105. */
  1106. if ((h -= disp) < 0)
  1107. h += HSIZE;
  1108. hp = &sp->enc_hashtab[h];
  1109. if (hp->hash == fcode)
  1110. {
  1111. ent = hp->code;
  1112. goto hit;
  1113. }
  1114. } while (hp->hash >= 0);
  1115. }
  1116. /*
  1117. * New entry, emit code and add to table.
  1118. */
  1119. /*
  1120. * Verify there is space in the buffer for the code
  1121. * and any potential Clear code that might be emitted
  1122. * below. The value of limit is setup so that there
  1123. * are at least 4 bytes free--room for 2 codes.
  1124. */
  1125. if (op > limit)
  1126. {
  1127. tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata);
  1128. if (!TIFFFlushData1(tif))
  1129. return 0;
  1130. op = tif->tif_rawdata;
  1131. }
  1132. PutNextCode(op, ent);
  1133. ent = (hcode_t)c;
  1134. hp->code = (hcode_t)(free_ent++);
  1135. hp->hash = fcode;
  1136. if (free_ent == CODE_MAX - 1)
  1137. {
  1138. /* table is full, emit clear code and reset */
  1139. cl_hash(sp);
  1140. sp->enc_ratio = 0;
  1141. incount = 0;
  1142. outcount = 0;
  1143. free_ent = CODE_FIRST;
  1144. PutNextCode(op, CODE_CLEAR);
  1145. nbits = BITS_MIN;
  1146. maxcode = MAXCODE(BITS_MIN);
  1147. }
  1148. else
  1149. {
  1150. /*
  1151. * If the next entry is going to be too big for
  1152. * the code size, then increase it, if possible.
  1153. */
  1154. if (free_ent > maxcode)
  1155. {
  1156. nbits++;
  1157. assert(nbits <= BITS_MAX);
  1158. maxcode = (int)MAXCODE(nbits);
  1159. }
  1160. else if (incount >= checkpoint)
  1161. {
  1162. tmsize_t rat;
  1163. /*
  1164. * Check compression ratio and, if things seem
  1165. * to be slipping, clear the hash table and
  1166. * reset state. The compression ratio is a
  1167. * 24+8-bit fractional number.
  1168. */
  1169. checkpoint = incount + CHECK_GAP;
  1170. CALCRATIO(sp, rat);
  1171. if (rat <= sp->enc_ratio)
  1172. {
  1173. cl_hash(sp);
  1174. sp->enc_ratio = 0;
  1175. incount = 0;
  1176. outcount = 0;
  1177. free_ent = CODE_FIRST;
  1178. PutNextCode(op, CODE_CLEAR);
  1179. nbits = BITS_MIN;
  1180. maxcode = MAXCODE(BITS_MIN);
  1181. }
  1182. else
  1183. sp->enc_ratio = rat;
  1184. }
  1185. }
  1186. hit:;
  1187. }
  1188. /*
  1189. * Restore global state.
  1190. */
  1191. sp->enc_incount = incount;
  1192. sp->enc_outcount = outcount;
  1193. sp->enc_checkpoint = checkpoint;
  1194. sp->enc_oldcode = ent;
  1195. sp->lzw_nextdata = nextdata;
  1196. sp->lzw_nextbits = nextbits;
  1197. sp->lzw_free_ent = (unsigned short)free_ent;
  1198. sp->lzw_maxcode = (unsigned short)maxcode;
  1199. sp->lzw_nbits = (unsigned short)nbits;
  1200. tif->tif_rawcp = op;
  1201. return (1);
  1202. }
  1203. /*
  1204. * Finish off an encoded strip by flushing the last
  1205. * string and tacking on an End Of Information code.
  1206. */
  1207. static int LZWPostEncode(TIFF *tif)
  1208. {
  1209. register LZWCodecState *sp = LZWEncoderState(tif);
  1210. uint8_t *op = tif->tif_rawcp;
  1211. long nextbits = sp->lzw_nextbits;
  1212. WordType nextdata = sp->lzw_nextdata;
  1213. tmsize_t outcount = sp->enc_outcount;
  1214. int nbits = sp->lzw_nbits;
  1215. if (op > sp->enc_rawlimit)
  1216. {
  1217. tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata);
  1218. if (!TIFFFlushData1(tif))
  1219. return 0;
  1220. op = tif->tif_rawdata;
  1221. }
  1222. if (sp->enc_oldcode != (hcode_t)-1)
  1223. {
  1224. int free_ent = sp->lzw_free_ent;
  1225. PutNextCode(op, sp->enc_oldcode);
  1226. sp->enc_oldcode = (hcode_t)-1;
  1227. free_ent++;
  1228. if (free_ent == CODE_MAX - 1)
  1229. {
  1230. /* table is full, emit clear code and reset */
  1231. outcount = 0;
  1232. PutNextCode(op, CODE_CLEAR);
  1233. nbits = BITS_MIN;
  1234. }
  1235. else
  1236. {
  1237. /*
  1238. * If the next entry is going to be too big for
  1239. * the code size, then increase it, if possible.
  1240. */
  1241. if (free_ent > sp->lzw_maxcode)
  1242. {
  1243. nbits++;
  1244. assert(nbits <= BITS_MAX);
  1245. }
  1246. }
  1247. }
  1248. PutNextCode(op, CODE_EOI);
  1249. /* Explicit 0xff masking to make icc -check=conversions happy */
  1250. if (nextbits > 0)
  1251. *op++ = (unsigned char)((nextdata << (8 - nextbits)) & 0xff);
  1252. tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata);
  1253. (void)outcount;
  1254. return (1);
  1255. }
  1256. /*
  1257. * Reset encoding hash table.
  1258. */
  1259. static void cl_hash(LZWCodecState *sp)
  1260. {
  1261. register hash_t *hp = &sp->enc_hashtab[HSIZE - 1];
  1262. register long i = HSIZE - 8;
  1263. do
  1264. {
  1265. i -= 8;
  1266. hp[-7].hash = -1;
  1267. hp[-6].hash = -1;
  1268. hp[-5].hash = -1;
  1269. hp[-4].hash = -1;
  1270. hp[-3].hash = -1;
  1271. hp[-2].hash = -1;
  1272. hp[-1].hash = -1;
  1273. hp[0].hash = -1;
  1274. hp -= 8;
  1275. } while (i >= 0);
  1276. for (i += 8; i > 0; i--, hp--)
  1277. hp->hash = -1;
  1278. }
  1279. #endif
  1280. static void LZWCleanup(TIFF *tif)
  1281. {
  1282. (void)TIFFPredictorCleanup(tif);
  1283. assert(tif->tif_data != NULL);
  1284. if (LZWDecoderState(tif)->dec_codetab)
  1285. _TIFFfreeExt(tif, LZWDecoderState(tif)->dec_codetab);
  1286. if (LZWEncoderState(tif)->enc_hashtab)
  1287. _TIFFfreeExt(tif, LZWEncoderState(tif)->enc_hashtab);
  1288. _TIFFfreeExt(tif, tif->tif_data);
  1289. tif->tif_data = NULL;
  1290. _TIFFSetDefaultCompressionState(tif);
  1291. }
  1292. int TIFFInitLZW(TIFF *tif, int scheme)
  1293. {
  1294. static const char module[] = "TIFFInitLZW";
  1295. (void)scheme;
  1296. assert(scheme == COMPRESSION_LZW);
  1297. /*
  1298. * Allocate state block so tag methods have storage to record values.
  1299. */
  1300. tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(LZWCodecState));
  1301. if (tif->tif_data == NULL)
  1302. goto bad;
  1303. LZWDecoderState(tif)->dec_codetab = NULL;
  1304. LZWDecoderState(tif)->dec_decode = NULL;
  1305. LZWEncoderState(tif)->enc_hashtab = NULL;
  1306. LZWState(tif)->rw_mode = tif->tif_mode;
  1307. /*
  1308. * Install codec methods.
  1309. */
  1310. tif->tif_fixuptags = LZWFixupTags;
  1311. tif->tif_setupdecode = LZWSetupDecode;
  1312. tif->tif_predecode = LZWPreDecode;
  1313. tif->tif_decoderow = LZWDecode;
  1314. tif->tif_decodestrip = LZWDecode;
  1315. tif->tif_decodetile = LZWDecode;
  1316. #ifndef LZW_READ_ONLY
  1317. tif->tif_setupencode = LZWSetupEncode;
  1318. tif->tif_preencode = LZWPreEncode;
  1319. tif->tif_postencode = LZWPostEncode;
  1320. tif->tif_encoderow = LZWEncode;
  1321. tif->tif_encodestrip = LZWEncode;
  1322. tif->tif_encodetile = LZWEncode;
  1323. #endif
  1324. tif->tif_cleanup = LZWCleanup;
  1325. /*
  1326. * Setup predictor setup.
  1327. */
  1328. (void)TIFFPredictorInit(tif);
  1329. return (1);
  1330. bad:
  1331. TIFFErrorExtR(tif, module, "No space for LZW state block");
  1332. return (0);
  1333. }
  1334. /*
  1335. * Copyright (c) 1985, 1986 The Regents of the University of California.
  1336. * All rights reserved.
  1337. *
  1338. * This code is derived from software contributed to Berkeley by
  1339. * James A. Woods, derived from original work by Spencer Thomas
  1340. * and Joseph Orost.
  1341. *
  1342. * Redistribution and use in source and binary forms are permitted
  1343. * provided that the above copyright notice and this paragraph are
  1344. * duplicated in all such forms and that any documentation,
  1345. * advertising materials, and other materials related to such
  1346. * distribution and use acknowledge that the software was developed
  1347. * by the University of California, Berkeley. The name of the
  1348. * University may not be used to endorse or promote products derived
  1349. * from this software without specific prior written permission.
  1350. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  1351. * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  1352. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  1353. */
  1354. #endif /* LZW_SUPPORT */