tif_lzw.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458
  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 DecoderState(tif) ((LZWCodecState *)LZWState(tif))
  149. #define EncoderState(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 = DecoderState(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 = DecoderState(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 = DecoderState(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(__GNUC__) && defined(__x86_64__)
  303. #define GetNextData(nextdata, bp) \
  304. nextdata = __builtin_bswap64(*(uint64_t *)(bp))
  305. #elif defined(_M_X64)
  306. #define GetNextData(nextdata, bp) nextdata = _byteswap_uint64(*(uint64_t *)(bp))
  307. #elif defined(__GNUC__)
  308. #define GetNextData(nextdata, bp) \
  309. memcpy(&nextdata, bp, sizeof(nextdata)); \
  310. nextdata = __builtin_bswap64(nextdata)
  311. #else
  312. #define GetNextData(nextdata, bp) \
  313. nextdata = (((uint64_t)bp[0]) << 56) | (((uint64_t)bp[1]) << 48) | \
  314. (((uint64_t)bp[2]) << 40) | (((uint64_t)bp[3]) << 32) | \
  315. (((uint64_t)bp[4]) << 24) | (((uint64_t)bp[5]) << 16) | \
  316. (((uint64_t)bp[6]) << 8) | (((uint64_t)bp[7]))
  317. #endif
  318. #elif SIZEOF_WORDTYPE == 4
  319. #if defined(__GNUC__) && defined(__i386__)
  320. #define GetNextData(nextdata, bp) \
  321. nextdata = __builtin_bswap32(*(uint32_t *)(bp))
  322. #elif defined(_M_X86)
  323. #define GetNextData(nextdata, bp) \
  324. nextdata = _byteswap_ulong(*(unsigned long *)(bp))
  325. #elif defined(__GNUC__)
  326. #define GetNextData(nextdata, bp) \
  327. memcpy(&nextdata, bp, sizeof(nextdata)); \
  328. nextdata = __builtin_bswap32(nextdata)
  329. #else
  330. #define GetNextData(nextdata, bp) \
  331. nextdata = (((uint32_t)bp[0]) << 24) | (((uint32_t)bp[1]) << 16) | \
  332. (((uint32_t)bp[2]) << 8) | (((uint32_t)bp[3]))
  333. #endif
  334. #else
  335. #error "Unhandled SIZEOF_WORDTYPE"
  336. #endif
  337. #define GetNextCodeLZW() \
  338. do \
  339. { \
  340. nextbits -= nbits; \
  341. if (nextbits < 0) \
  342. { \
  343. if (dec_bitsleft >= 8 * SIZEOF_WORDTYPE) \
  344. { \
  345. unsigned codetmp = (unsigned)(nextdata << (-nextbits)); \
  346. GetNextData(nextdata, bp); \
  347. bp += SIZEOF_WORDTYPE; \
  348. nextbits += 8 * SIZEOF_WORDTYPE; \
  349. dec_bitsleft -= 8 * SIZEOF_WORDTYPE; \
  350. code = (WordType)((codetmp | (nextdata >> nextbits)) & \
  351. nbitsmask); \
  352. break; \
  353. } \
  354. else \
  355. { \
  356. if (dec_bitsleft < 8) \
  357. { \
  358. goto no_eoi; \
  359. } \
  360. nextdata = (nextdata << 8) | *(bp)++; \
  361. nextbits += 8; \
  362. dec_bitsleft -= 8; \
  363. if (nextbits < 0) \
  364. { \
  365. if (dec_bitsleft < 8) \
  366. { \
  367. goto no_eoi; \
  368. } \
  369. nextdata = (nextdata << 8) | *(bp)++; \
  370. nextbits += 8; \
  371. dec_bitsleft -= 8; \
  372. } \
  373. } \
  374. } \
  375. code = (WordType)((nextdata >> nextbits) & nbitsmask); \
  376. } while (0)
  377. static int LZWDecode(TIFF *tif, uint8_t *op0, tmsize_t occ0, uint16_t s)
  378. {
  379. static const char module[] = "LZWDecode";
  380. LZWCodecState *sp = DecoderState(tif);
  381. uint8_t *op = (uint8_t *)op0;
  382. tmsize_t occ = occ0;
  383. uint8_t *bp;
  384. long nbits, nextbits, nbitsmask;
  385. WordType nextdata;
  386. code_t *free_entp, *maxcodep, *oldcodep;
  387. (void)s;
  388. assert(sp != NULL);
  389. assert(sp->dec_codetab != NULL);
  390. if (sp->read_error)
  391. {
  392. TIFFErrorExtR(tif, module,
  393. "LZWDecode: Scanline %" PRIu32 " cannot be read due to "
  394. "previous error",
  395. tif->tif_row);
  396. return 0;
  397. }
  398. /*
  399. * Restart interrupted output operation.
  400. */
  401. if (sp->dec_restart)
  402. {
  403. tmsize_t residue;
  404. code_t *codep = sp->dec_codep;
  405. residue = codep->length - sp->dec_restart;
  406. if (residue > occ)
  407. {
  408. /*
  409. * Residue from previous decode is sufficient
  410. * to satisfy decode request. Skip to the
  411. * start of the decoded string, place decoded
  412. * values in the output buffer, and return.
  413. */
  414. sp->dec_restart += occ;
  415. do
  416. {
  417. codep = codep->next;
  418. } while (--residue > occ && codep);
  419. if (codep)
  420. {
  421. uint8_t *tp = op + occ;
  422. do
  423. {
  424. *--tp = codep->value;
  425. codep = codep->next;
  426. } while (--occ && codep);
  427. }
  428. return (1);
  429. }
  430. /*
  431. * Residue satisfies only part of the decode request.
  432. */
  433. op += residue;
  434. occ -= residue;
  435. uint8_t *tp = op;
  436. do
  437. {
  438. *--tp = codep->value;
  439. codep = codep->next;
  440. } while (--residue && codep);
  441. sp->dec_restart = 0;
  442. }
  443. bp = (uint8_t *)tif->tif_rawcp;
  444. sp->dec_bitsleft += (((uint64_t)tif->tif_rawcc - sp->old_tif_rawcc) << 3);
  445. uint64_t dec_bitsleft = sp->dec_bitsleft;
  446. nbits = sp->lzw_nbits;
  447. nextdata = sp->lzw_nextdata;
  448. nextbits = sp->lzw_nextbits;
  449. nbitsmask = sp->dec_nbitsmask;
  450. oldcodep = sp->dec_oldcodep;
  451. free_entp = sp->dec_free_entp;
  452. maxcodep = sp->dec_maxcodep;
  453. code_t *const dec_codetab = sp->dec_codetab;
  454. code_t *codep;
  455. if (occ == 0)
  456. {
  457. goto after_loop;
  458. }
  459. begin:
  460. {
  461. WordType code;
  462. GetNextCodeLZW();
  463. codep = dec_codetab + code;
  464. if (code >= CODE_FIRST)
  465. goto code_above_or_equal_to_258;
  466. if (code < 256)
  467. goto code_below_256;
  468. if (code == CODE_EOI)
  469. goto after_loop;
  470. goto code_clear;
  471. code_below_256:
  472. {
  473. if (codep > free_entp)
  474. goto error_code;
  475. free_entp->next = oldcodep;
  476. free_entp->firstchar = oldcodep->firstchar;
  477. free_entp->length = oldcodep->length + 1;
  478. free_entp->value = (uint8_t)code;
  479. free_entp->repeated =
  480. (bool)(oldcodep->repeated & (oldcodep->value == code));
  481. if (++free_entp > maxcodep)
  482. {
  483. if (++nbits > BITS_MAX) /* should not happen for a conformant encoder */
  484. nbits = BITS_MAX;
  485. nbitsmask = MAXCODE(nbits);
  486. maxcodep = dec_codetab + nbitsmask - 1;
  487. if (free_entp >= &dec_codetab[CSIZE])
  488. {
  489. /* At that point, the next valid states are either EOI or a */
  490. /* CODE_CLEAR. If a regular code is read, at the next */
  491. /* attempt at registering a new entry, we will error out */
  492. /* due to setting free_entp before any valid code */
  493. free_entp = dec_codetab - 1;
  494. }
  495. }
  496. oldcodep = codep;
  497. *op++ = (uint8_t)code;
  498. occ--;
  499. if (occ == 0)
  500. goto after_loop;
  501. goto begin;
  502. }
  503. code_above_or_equal_to_258:
  504. {
  505. /*
  506. * Add the new entry to the code table.
  507. */
  508. if (codep >= free_entp)
  509. {
  510. if (codep != free_entp)
  511. goto error_code;
  512. free_entp->value = oldcodep->firstchar;
  513. }
  514. else
  515. {
  516. free_entp->value = codep->firstchar;
  517. }
  518. free_entp->repeated =
  519. (bool)(oldcodep->repeated & (oldcodep->value == free_entp->value));
  520. free_entp->next = oldcodep;
  521. free_entp->firstchar = oldcodep->firstchar;
  522. free_entp->length = oldcodep->length + 1;
  523. if (++free_entp > maxcodep)
  524. {
  525. if (++nbits > BITS_MAX) /* should not happen for a conformant encoder */
  526. nbits = BITS_MAX;
  527. nbitsmask = MAXCODE(nbits);
  528. maxcodep = dec_codetab + nbitsmask - 1;
  529. if (free_entp >= &dec_codetab[CSIZE])
  530. {
  531. /* At that point, the next valid states are either EOI or a */
  532. /* CODE_CLEAR. If a regular code is read, at the next */
  533. /* attempt at registering a new entry, we will error out */
  534. /* due to setting free_entp before any valid code */
  535. free_entp = dec_codetab - 1;
  536. }
  537. }
  538. oldcodep = codep;
  539. /*
  540. * Code maps to a string, copy string
  541. * value to output (written in reverse).
  542. */
  543. /* tiny bit faster on x86_64 to store in unsigned short than int */
  544. unsigned short len = codep->length;
  545. if (len < 3) /* equivalent to len == 2 given all other conditions */
  546. {
  547. if (occ <= 2)
  548. {
  549. if (occ == 2)
  550. {
  551. memcpy(op, &(codep->firstchar), 2);
  552. op += 2;
  553. occ -= 2;
  554. goto after_loop;
  555. }
  556. goto too_short_buffer;
  557. }
  558. memcpy(op, &(codep->firstchar), 2);
  559. op += 2;
  560. occ -= 2;
  561. goto begin; /* we can save the comparison occ > 0 */
  562. }
  563. if (len == 3)
  564. {
  565. if (occ <= 3)
  566. {
  567. if (occ == 3)
  568. {
  569. op[0] = codep->firstchar;
  570. op[1] = codep->next->value;
  571. op[2] = codep->value;
  572. op += 3;
  573. occ -= 3;
  574. goto after_loop;
  575. }
  576. goto too_short_buffer;
  577. }
  578. op[0] = codep->firstchar;
  579. op[1] = codep->next->value;
  580. op[2] = codep->value;
  581. op += 3;
  582. occ -= 3;
  583. goto begin; /* we can save the comparison occ > 0 */
  584. }
  585. if (len > occ)
  586. {
  587. goto too_short_buffer;
  588. }
  589. if (codep->repeated)
  590. {
  591. memset(op, codep->value, len);
  592. op += len;
  593. occ -= len;
  594. if (occ == 0)
  595. goto after_loop;
  596. goto begin;
  597. }
  598. uint8_t *tp = op + len;
  599. assert(len >= 4);
  600. *--tp = codep->value;
  601. codep = codep->next;
  602. *--tp = codep->value;
  603. codep = codep->next;
  604. *--tp = codep->value;
  605. codep = codep->next;
  606. *--tp = codep->value;
  607. if (tp > op)
  608. {
  609. do
  610. {
  611. codep = codep->next;
  612. *--tp = codep->value;
  613. } while (tp > op);
  614. }
  615. assert(occ >= len);
  616. op += len;
  617. occ -= len;
  618. if (occ == 0)
  619. goto after_loop;
  620. goto begin;
  621. }
  622. code_clear:
  623. {
  624. free_entp = dec_codetab + CODE_FIRST;
  625. nbits = BITS_MIN;
  626. nbitsmask = MAXCODE(BITS_MIN);
  627. maxcodep = dec_codetab + nbitsmask - 1;
  628. do
  629. {
  630. GetNextCodeLZW();
  631. } while (code == CODE_CLEAR); /* consecutive CODE_CLEAR codes */
  632. if (code == CODE_EOI)
  633. goto after_loop;
  634. if (code > CODE_EOI)
  635. {
  636. goto error_code;
  637. }
  638. *op++ = (uint8_t)code;
  639. occ--;
  640. oldcodep = dec_codetab + code;
  641. if (occ == 0)
  642. goto after_loop;
  643. goto begin;
  644. }
  645. }
  646. too_short_buffer:
  647. {
  648. /*
  649. * String is too long for decode buffer,
  650. * locate portion that will fit, copy to
  651. * the decode buffer, and setup restart
  652. * logic for the next decoding call.
  653. */
  654. sp->dec_codep = codep;
  655. do
  656. {
  657. codep = codep->next;
  658. } while (codep->length > occ);
  659. sp->dec_restart = occ;
  660. uint8_t *tp = op + occ;
  661. do
  662. {
  663. *--tp = codep->value;
  664. codep = codep->next;
  665. } while (--occ);
  666. }
  667. after_loop:
  668. tif->tif_rawcc -= (tmsize_t)((uint8_t *)bp - tif->tif_rawcp);
  669. tif->tif_rawcp = (uint8_t *)bp;
  670. sp->old_tif_rawcc = tif->tif_rawcc;
  671. sp->dec_bitsleft = dec_bitsleft;
  672. sp->lzw_nbits = (unsigned short)nbits;
  673. sp->lzw_nextdata = nextdata;
  674. sp->lzw_nextbits = nextbits;
  675. sp->dec_nbitsmask = nbitsmask;
  676. sp->dec_oldcodep = oldcodep;
  677. sp->dec_free_entp = free_entp;
  678. sp->dec_maxcodep = maxcodep;
  679. if (occ > 0)
  680. {
  681. TIFFErrorExtR(tif, module,
  682. "Not enough data at scanline %" PRIu32 " (short %" PRIu64
  683. " bytes)",
  684. tif->tif_row, (uint64_t)occ);
  685. return (0);
  686. }
  687. return (1);
  688. no_eoi:
  689. sp->read_error = 1;
  690. TIFFErrorExtR(tif, module,
  691. "LZWDecode: Strip %" PRIu32 " not terminated with EOI code",
  692. tif->tif_curstrip);
  693. return 0;
  694. error_code:
  695. sp->read_error = 1;
  696. TIFFErrorExtR(tif, tif->tif_name, "Using code not yet in table");
  697. return 0;
  698. }
  699. #ifdef LZW_COMPAT
  700. /*
  701. * This check shouldn't be necessary because each
  702. * strip is suppose to be terminated with CODE_EOI.
  703. */
  704. #define NextCode(_tif, _sp, _bp, _code, _get, dec_bitsleft) \
  705. { \
  706. if (dec_bitsleft < (uint64_t)nbits) \
  707. { \
  708. TIFFWarningExtR(_tif, module, \
  709. "LZWDecode: Strip %" PRIu32 \
  710. " not terminated with EOI code", \
  711. _tif->tif_curstrip); \
  712. _code = CODE_EOI; \
  713. } \
  714. else \
  715. { \
  716. _get(_sp, _bp, _code); \
  717. dec_bitsleft -= nbits; \
  718. } \
  719. }
  720. /*
  721. * Decode a "hunk of data" for old images.
  722. */
  723. #define GetNextCodeCompat(sp, bp, code) \
  724. { \
  725. nextdata |= (unsigned long)*(bp)++ << nextbits; \
  726. nextbits += 8; \
  727. if (nextbits < nbits) \
  728. { \
  729. nextdata |= (unsigned long)*(bp)++ << nextbits; \
  730. nextbits += 8; \
  731. } \
  732. code = (hcode_t)(nextdata & nbitsmask); \
  733. nextdata >>= nbits; \
  734. nextbits -= nbits; \
  735. }
  736. static int LZWDecodeCompat(TIFF *tif, uint8_t *op0, tmsize_t occ0, uint16_t s)
  737. {
  738. static const char module[] = "LZWDecodeCompat";
  739. LZWCodecState *sp = DecoderState(tif);
  740. uint8_t *op = (uint8_t *)op0;
  741. tmsize_t occ = occ0;
  742. uint8_t *tp;
  743. uint8_t *bp;
  744. int code, nbits;
  745. int len;
  746. long nextbits, nbitsmask;
  747. WordType nextdata;
  748. code_t *codep, *free_entp, *maxcodep, *oldcodep;
  749. (void)s;
  750. assert(sp != NULL);
  751. /*
  752. * Restart interrupted output operation.
  753. */
  754. if (sp->dec_restart)
  755. {
  756. tmsize_t residue;
  757. codep = sp->dec_codep;
  758. residue = codep->length - sp->dec_restart;
  759. if (residue > occ)
  760. {
  761. /*
  762. * Residue from previous decode is sufficient
  763. * to satisfy decode request. Skip to the
  764. * start of the decoded string, place decoded
  765. * values in the output buffer, and return.
  766. */
  767. sp->dec_restart += occ;
  768. do
  769. {
  770. codep = codep->next;
  771. } while (--residue > occ);
  772. tp = op + occ;
  773. do
  774. {
  775. *--tp = codep->value;
  776. codep = codep->next;
  777. } while (--occ);
  778. return (1);
  779. }
  780. /*
  781. * Residue satisfies only part of the decode request.
  782. */
  783. op += residue;
  784. occ -= residue;
  785. tp = op;
  786. do
  787. {
  788. *--tp = codep->value;
  789. codep = codep->next;
  790. } while (--residue);
  791. sp->dec_restart = 0;
  792. }
  793. bp = (uint8_t *)tif->tif_rawcp;
  794. sp->dec_bitsleft += (((uint64_t)tif->tif_rawcc - sp->old_tif_rawcc) << 3);
  795. uint64_t dec_bitsleft = sp->dec_bitsleft;
  796. nbits = sp->lzw_nbits;
  797. nextdata = sp->lzw_nextdata;
  798. nextbits = sp->lzw_nextbits;
  799. nbitsmask = sp->dec_nbitsmask;
  800. oldcodep = sp->dec_oldcodep;
  801. free_entp = sp->dec_free_entp;
  802. maxcodep = sp->dec_maxcodep;
  803. while (occ > 0)
  804. {
  805. NextCode(tif, sp, bp, code, GetNextCodeCompat, dec_bitsleft);
  806. if (code == CODE_EOI)
  807. break;
  808. if (code == CODE_CLEAR)
  809. {
  810. do
  811. {
  812. free_entp = sp->dec_codetab + CODE_FIRST;
  813. _TIFFmemset(free_entp, 0,
  814. (CSIZE - CODE_FIRST) * sizeof(code_t));
  815. nbits = BITS_MIN;
  816. nbitsmask = MAXCODE(BITS_MIN);
  817. maxcodep = sp->dec_codetab + nbitsmask;
  818. NextCode(tif, sp, bp, code, GetNextCodeCompat, dec_bitsleft);
  819. } while (code == CODE_CLEAR); /* consecutive CODE_CLEAR codes */
  820. if (code == CODE_EOI)
  821. break;
  822. if (code > CODE_CLEAR)
  823. {
  824. TIFFErrorExtR(
  825. tif, tif->tif_name,
  826. "LZWDecode: Corrupted LZW table at scanline %" PRIu32,
  827. tif->tif_row);
  828. return (0);
  829. }
  830. *op++ = (uint8_t)code;
  831. occ--;
  832. oldcodep = sp->dec_codetab + code;
  833. continue;
  834. }
  835. codep = sp->dec_codetab + code;
  836. /*
  837. * Add the new entry to the code table.
  838. */
  839. if (free_entp < &sp->dec_codetab[0] ||
  840. free_entp >= &sp->dec_codetab[CSIZE])
  841. {
  842. TIFFErrorExtR(tif, module,
  843. "Corrupted LZW table at scanline %" PRIu32,
  844. tif->tif_row);
  845. return (0);
  846. }
  847. free_entp->next = oldcodep;
  848. if (free_entp->next < &sp->dec_codetab[0] ||
  849. free_entp->next >= &sp->dec_codetab[CSIZE])
  850. {
  851. TIFFErrorExtR(tif, module,
  852. "Corrupted LZW table at scanline %" PRIu32,
  853. tif->tif_row);
  854. return (0);
  855. }
  856. free_entp->firstchar = free_entp->next->firstchar;
  857. free_entp->length = free_entp->next->length + 1;
  858. free_entp->value =
  859. (codep < free_entp) ? codep->firstchar : free_entp->firstchar;
  860. if (++free_entp > maxcodep)
  861. {
  862. if (++nbits > BITS_MAX) /* should not happen */
  863. nbits = BITS_MAX;
  864. nbitsmask = MAXCODE(nbits);
  865. maxcodep = sp->dec_codetab + nbitsmask;
  866. }
  867. oldcodep = codep;
  868. if (code >= 256)
  869. {
  870. /*
  871. * Code maps to a string, copy string
  872. * value to output (written in reverse).
  873. */
  874. if (codep->length == 0)
  875. {
  876. TIFFErrorExtR(
  877. tif, module,
  878. "Wrong length of decoded "
  879. "string: data probably corrupted at scanline %" PRIu32,
  880. tif->tif_row);
  881. return (0);
  882. }
  883. if (codep->length > occ)
  884. {
  885. /*
  886. * String is too long for decode buffer,
  887. * locate portion that will fit, copy to
  888. * the decode buffer, and setup restart
  889. * logic for the next decoding call.
  890. */
  891. sp->dec_codep = codep;
  892. do
  893. {
  894. codep = codep->next;
  895. } while (codep->length > occ);
  896. sp->dec_restart = occ;
  897. tp = op + occ;
  898. do
  899. {
  900. *--tp = codep->value;
  901. codep = codep->next;
  902. } while (--occ);
  903. break;
  904. }
  905. len = codep->length;
  906. tp = op + len;
  907. do
  908. {
  909. *--tp = codep->value;
  910. codep = codep->next;
  911. } while (codep && tp > op);
  912. assert(occ >= len);
  913. op += len;
  914. occ -= len;
  915. }
  916. else
  917. {
  918. *op++ = (uint8_t)code;
  919. occ--;
  920. }
  921. }
  922. tif->tif_rawcc -= (tmsize_t)((uint8_t *)bp - tif->tif_rawcp);
  923. tif->tif_rawcp = (uint8_t *)bp;
  924. sp->old_tif_rawcc = tif->tif_rawcc;
  925. sp->dec_bitsleft = dec_bitsleft;
  926. sp->lzw_nbits = (unsigned short)nbits;
  927. sp->lzw_nextdata = nextdata;
  928. sp->lzw_nextbits = nextbits;
  929. sp->dec_nbitsmask = nbitsmask;
  930. sp->dec_oldcodep = oldcodep;
  931. sp->dec_free_entp = free_entp;
  932. sp->dec_maxcodep = maxcodep;
  933. if (occ > 0)
  934. {
  935. TIFFErrorExtR(tif, module,
  936. "Not enough data at scanline %" PRIu32 " (short %" PRIu64
  937. " bytes)",
  938. tif->tif_row, (uint64_t)occ);
  939. return (0);
  940. }
  941. return (1);
  942. }
  943. #endif /* LZW_COMPAT */
  944. /*
  945. * LZW Encoding.
  946. */
  947. static int LZWSetupEncode(TIFF *tif)
  948. {
  949. static const char module[] = "LZWSetupEncode";
  950. LZWCodecState *sp = EncoderState(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 = EncoderState(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 = EncoderState(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 = EncoderState(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. static void LZWCleanup(TIFF *tif)
  1280. {
  1281. (void)TIFFPredictorCleanup(tif);
  1282. assert(tif->tif_data != 0);
  1283. if (DecoderState(tif)->dec_codetab)
  1284. _TIFFfreeExt(tif, DecoderState(tif)->dec_codetab);
  1285. if (EncoderState(tif)->enc_hashtab)
  1286. _TIFFfreeExt(tif, EncoderState(tif)->enc_hashtab);
  1287. _TIFFfreeExt(tif, tif->tif_data);
  1288. tif->tif_data = NULL;
  1289. _TIFFSetDefaultCompressionState(tif);
  1290. }
  1291. int TIFFInitLZW(TIFF *tif, int scheme)
  1292. {
  1293. static const char module[] = "TIFFInitLZW";
  1294. (void)scheme;
  1295. assert(scheme == COMPRESSION_LZW);
  1296. /*
  1297. * Allocate state block so tag methods have storage to record values.
  1298. */
  1299. tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(LZWCodecState));
  1300. if (tif->tif_data == NULL)
  1301. goto bad;
  1302. DecoderState(tif)->dec_codetab = NULL;
  1303. DecoderState(tif)->dec_decode = NULL;
  1304. EncoderState(tif)->enc_hashtab = NULL;
  1305. LZWState(tif)->rw_mode = tif->tif_mode;
  1306. /*
  1307. * Install codec methods.
  1308. */
  1309. tif->tif_fixuptags = LZWFixupTags;
  1310. tif->tif_setupdecode = LZWSetupDecode;
  1311. tif->tif_predecode = LZWPreDecode;
  1312. tif->tif_decoderow = LZWDecode;
  1313. tif->tif_decodestrip = LZWDecode;
  1314. tif->tif_decodetile = LZWDecode;
  1315. tif->tif_setupencode = LZWSetupEncode;
  1316. tif->tif_preencode = LZWPreEncode;
  1317. tif->tif_postencode = LZWPostEncode;
  1318. tif->tif_encoderow = LZWEncode;
  1319. tif->tif_encodestrip = LZWEncode;
  1320. tif->tif_encodetile = LZWEncode;
  1321. tif->tif_cleanup = LZWCleanup;
  1322. /*
  1323. * Setup predictor setup.
  1324. */
  1325. (void)TIFFPredictorInit(tif);
  1326. return (1);
  1327. bad:
  1328. TIFFErrorExtR(tif, module, "No space for LZW state block");
  1329. return (0);
  1330. }
  1331. /*
  1332. * Copyright (c) 1985, 1986 The Regents of the University of California.
  1333. * All rights reserved.
  1334. *
  1335. * This code is derived from software contributed to Berkeley by
  1336. * James A. Woods, derived from original work by Spencer Thomas
  1337. * and Joseph Orost.
  1338. *
  1339. * Redistribution and use in source and binary forms are permitted
  1340. * provided that the above copyright notice and this paragraph are
  1341. * duplicated in all such forms and that any documentation,
  1342. * advertising materials, and other materials related to such
  1343. * distribution and use acknowledge that the software was developed
  1344. * by the University of California, Berkeley. The name of the
  1345. * University may not be used to endorse or promote products derived
  1346. * from this software without specific prior written permission.
  1347. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  1348. * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  1349. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  1350. */
  1351. #endif /* LZW_SUPPORT */