tif_fax3.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598
  1. /*
  2. * Copyright (c) 1990-1997 Sam Leffler
  3. * Copyright (c) 1991-1997 Silicon Graphics, Inc.
  4. *
  5. * Permission to use, copy, modify, distribute, and sell this software and
  6. * its documentation for any purpose is hereby granted without fee, provided
  7. * that (i) the above copyright notices and this permission notice appear in
  8. * all copies of the software and related documentation, and (ii) the names of
  9. * Sam Leffler and Silicon Graphics may not be used in any advertising or
  10. * publicity relating to the software without the specific, prior written
  11. * permission of Sam Leffler and Silicon Graphics.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  14. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  18. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22. * OF THIS SOFTWARE.
  23. */
  24. #include "tiffiop.h"
  25. #ifdef CCITT_SUPPORT
  26. /*
  27. * TIFF Library.
  28. *
  29. * CCITT Group 3 (T.4) and Group 4 (T.6) Compression Support.
  30. *
  31. * This file contains support for decoding and encoding TIFF
  32. * compression algorithms 2, 3, 4, and 32771.
  33. *
  34. * Decoder support is derived, with permission, from the code
  35. * in Frank Cringle's viewfax program;
  36. * Copyright (C) 1990, 1995 Frank D. Cringle.
  37. */
  38. #include "tif_fax3.h"
  39. #define G3CODES
  40. #include "t4.h"
  41. #include <stdio.h>
  42. /*
  43. * Compression+decompression state blocks are
  44. * derived from this ``base state'' block.
  45. */
  46. typedef struct {
  47. int rw_mode; /* O_RDONLY for decode, else encode */
  48. int mode; /* operating mode */
  49. tmsize_t rowbytes; /* bytes in a decoded scanline */
  50. uint32_t rowpixels; /* pixels in a scanline */
  51. uint16_t cleanfaxdata; /* CleanFaxData tag */
  52. uint32_t badfaxrun; /* BadFaxRun tag */
  53. uint32_t badfaxlines; /* BadFaxLines tag */
  54. uint32_t groupoptions; /* Group 3/4 options tag */
  55. TIFFVGetMethod vgetparent; /* super-class method */
  56. TIFFVSetMethod vsetparent; /* super-class method */
  57. TIFFPrintMethod printdir; /* super-class method */
  58. } Fax3BaseState;
  59. #define Fax3State(tif) ((Fax3BaseState*) (tif)->tif_data)
  60. typedef enum { G3_1D, G3_2D } Ttag;
  61. typedef struct {
  62. Fax3BaseState b;
  63. /* Decoder state info */
  64. const unsigned char* bitmap; /* bit reversal table */
  65. uint32_t data; /* current i/o byte/word */
  66. int bit; /* current i/o bit in byte */
  67. int EOLcnt; /* count of EOL codes recognized */
  68. TIFFFaxFillFunc fill; /* fill routine */
  69. uint32_t* runs; /* b&w runs for current/previous row */
  70. uint32_t nruns; /* size of the refruns / curruns arrays */
  71. uint32_t* refruns; /* runs for reference line */
  72. uint32_t* curruns; /* runs for current line */
  73. /* Encoder state info */
  74. Ttag tag; /* encoding state */
  75. unsigned char* refline; /* reference line for 2d decoding */
  76. int k; /* #rows left that can be 2d encoded */
  77. int maxk; /* max #rows that can be 2d encoded */
  78. int line;
  79. } Fax3CodecState;
  80. #define DecoderState(tif) ((Fax3CodecState*) Fax3State(tif))
  81. #define EncoderState(tif) ((Fax3CodecState*) Fax3State(tif))
  82. #define is2DEncoding(sp) (sp->b.groupoptions & GROUP3OPT_2DENCODING)
  83. #define isAligned(p,t) ((((size_t)(p)) & (sizeof (t)-1)) == 0)
  84. /*
  85. * Group 3 and Group 4 Decoding.
  86. */
  87. /*
  88. * These macros glue the TIFF library state to
  89. * the state expected by Frank's decoder.
  90. */
  91. #define DECLARE_STATE(tif, sp, mod) \
  92. static const char module[] = mod; \
  93. Fax3CodecState* sp = DecoderState(tif); \
  94. int a0; /* reference element */ \
  95. int lastx = sp->b.rowpixels; /* last element in row */ \
  96. uint32_t BitAcc; /* bit accumulator */ \
  97. int BitsAvail; /* # valid bits in BitAcc */ \
  98. int RunLength; /* length of current run */ \
  99. unsigned char* cp; /* next byte of input data */ \
  100. unsigned char* ep; /* end of input data */ \
  101. uint32_t* pa; /* place to stuff next run */ \
  102. uint32_t* thisrun; /* current row's run array */ \
  103. int EOLcnt; /* # EOL codes recognized */ \
  104. const unsigned char* bitmap = sp->bitmap; /* input data bit reverser */ \
  105. const TIFFFaxTabEnt* TabEnt
  106. #define DECLARE_STATE_2D(tif, sp, mod) \
  107. DECLARE_STATE(tif, sp, mod); \
  108. int b1; /* next change on prev line */ \
  109. uint32_t* pb /* next run in reference line */\
  110. /*
  111. * Load any state that may be changed during decoding.
  112. */
  113. #define CACHE_STATE(tif, sp) do { \
  114. BitAcc = sp->data; \
  115. BitsAvail = sp->bit; \
  116. EOLcnt = sp->EOLcnt; \
  117. cp = (unsigned char*) tif->tif_rawcp; \
  118. ep = cp + tif->tif_rawcc; \
  119. } while (0)
  120. /*
  121. * Save state possibly changed during decoding.
  122. */
  123. #define UNCACHE_STATE(tif, sp) do { \
  124. sp->bit = BitsAvail; \
  125. sp->data = BitAcc; \
  126. sp->EOLcnt = EOLcnt; \
  127. tif->tif_rawcc -= (tmsize_t)((uint8_t*) cp - tif->tif_rawcp); \
  128. tif->tif_rawcp = (uint8_t*) cp; \
  129. } while (0)
  130. /*
  131. * Setup state for decoding a strip.
  132. */
  133. static int
  134. Fax3PreDecode(TIFF* tif, uint16_t s)
  135. {
  136. Fax3CodecState* sp = DecoderState(tif);
  137. (void) s;
  138. assert(sp != NULL);
  139. sp->bit = 0; /* force initial read */
  140. sp->data = 0;
  141. sp->EOLcnt = 0; /* force initial scan for EOL */
  142. /*
  143. * Decoder assumes lsb-to-msb bit order. Note that we select
  144. * this here rather than in Fax3SetupState so that viewers can
  145. * hold the image open, fiddle with the FillOrder tag value,
  146. * and then re-decode the image. Otherwise they'd need to close
  147. * and open the image to get the state reset.
  148. */
  149. sp->bitmap =
  150. TIFFGetBitRevTable(tif->tif_dir.td_fillorder != FILLORDER_LSB2MSB);
  151. sp->curruns = sp->runs;
  152. if (sp->refruns) { /* init reference line to white */
  153. sp->refruns = sp->runs + sp->nruns;
  154. sp->refruns[0] = (uint32_t) sp->b.rowpixels;
  155. sp->refruns[1] = 0;
  156. }
  157. sp->line = 0;
  158. return (1);
  159. }
  160. /*
  161. * Routine for handling various errors/conditions.
  162. * Note how they are "glued into the decoder" by
  163. * overriding the definitions used by the decoder.
  164. */
  165. static void
  166. Fax3Unexpected(const char* module, TIFF* tif, uint32_t line, uint32_t a0)
  167. {
  168. TIFFErrorExt(tif->tif_clientdata, module, "Bad code word at line %"PRIu32" of %s %"PRIu32" (x %"PRIu32")",
  169. line, isTiled(tif) ? "tile" : "strip",
  170. (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip),
  171. a0);
  172. }
  173. #define unexpected(table, a0) Fax3Unexpected(module, tif, sp->line, a0)
  174. static void
  175. Fax3Extension(const char* module, TIFF* tif, uint32_t line, uint32_t a0)
  176. {
  177. TIFFErrorExt(tif->tif_clientdata, module,
  178. "Uncompressed data (not supported) at line %"PRIu32" of %s %"PRIu32" (x %"PRIu32")",
  179. line, isTiled(tif) ? "tile" : "strip",
  180. (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip),
  181. a0);
  182. }
  183. #define extension(a0) Fax3Extension(module, tif, sp->line, a0)
  184. static void
  185. Fax3BadLength(const char* module, TIFF* tif, uint32_t line, uint32_t a0, uint32_t lastx)
  186. {
  187. TIFFWarningExt(tif->tif_clientdata, module, "%s at line %"PRIu32" of %s %"PRIu32" (got %"PRIu32", expected %"PRIu32")",
  188. a0 < lastx ? "Premature EOL" : "Line length mismatch",
  189. line, isTiled(tif) ? "tile" : "strip",
  190. (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip),
  191. a0, lastx);
  192. }
  193. #define badlength(a0,lastx) Fax3BadLength(module, tif, sp->line, a0, lastx)
  194. static void
  195. Fax3PrematureEOF(const char* module, TIFF* tif, uint32_t line, uint32_t a0)
  196. {
  197. TIFFWarningExt(tif->tif_clientdata, module, "Premature EOF at line %"PRIu32" of %s %"PRIu32" (x %"PRIu32")",
  198. line, isTiled(tif) ? "tile" : "strip",
  199. (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip),
  200. a0);
  201. }
  202. #define prematureEOF(a0) Fax3PrematureEOF(module, tif, sp->line, a0)
  203. #define Nop
  204. /**
  205. * Decode the requested amount of G3 1D-encoded data.
  206. * @param buf destination buffer
  207. * @param occ available bytes in destination buffer
  208. * @param s number of planes (ignored)
  209. * @returns 1 for success, -1 in case of error
  210. */
  211. static int
  212. Fax3Decode1D(TIFF* tif, uint8_t* buf, tmsize_t occ, uint16_t s)
  213. {
  214. DECLARE_STATE(tif, sp, "Fax3Decode1D");
  215. (void) s;
  216. if (occ % sp->b.rowbytes)
  217. {
  218. TIFFErrorExt(tif->tif_clientdata, module, "Fractional scanlines cannot be read");
  219. return (-1);
  220. }
  221. CACHE_STATE(tif, sp);
  222. thisrun = sp->curruns;
  223. while (occ > 0) {
  224. a0 = 0;
  225. RunLength = 0;
  226. pa = thisrun;
  227. #ifdef FAX3_DEBUG
  228. printf("\nBitAcc=%08"PRIX32", BitsAvail = %d\n", BitAcc, BitsAvail);
  229. printf("-------------------- %"PRIu32"\n", tif->tif_row);
  230. fflush(stdout);
  231. #endif
  232. SYNC_EOL(EOF1D);
  233. EXPAND1D(EOF1Da);
  234. (*sp->fill)(buf, thisrun, pa, lastx);
  235. buf += sp->b.rowbytes;
  236. occ -= sp->b.rowbytes;
  237. sp->line++;
  238. continue;
  239. EOF1D: /* premature EOF */
  240. CLEANUP_RUNS();
  241. EOF1Da: /* premature EOF */
  242. (*sp->fill)(buf, thisrun, pa, lastx);
  243. UNCACHE_STATE(tif, sp);
  244. return (-1);
  245. }
  246. UNCACHE_STATE(tif, sp);
  247. return (1);
  248. }
  249. #define SWAP(t,a,b) { t x; x = (a); (a) = (b); (b) = x; }
  250. /*
  251. * Decode the requested amount of G3 2D-encoded data.
  252. */
  253. static int
  254. Fax3Decode2D(TIFF* tif, uint8_t* buf, tmsize_t occ, uint16_t s)
  255. {
  256. DECLARE_STATE_2D(tif, sp, "Fax3Decode2D");
  257. int is1D; /* current line is 1d/2d-encoded */
  258. (void) s;
  259. if (occ % sp->b.rowbytes)
  260. {
  261. TIFFErrorExt(tif->tif_clientdata, module, "Fractional scanlines cannot be read");
  262. return (-1);
  263. }
  264. CACHE_STATE(tif, sp);
  265. while (occ > 0) {
  266. a0 = 0;
  267. RunLength = 0;
  268. pa = thisrun = sp->curruns;
  269. #ifdef FAX3_DEBUG
  270. printf("\nBitAcc=%08"PRIX32", BitsAvail = %d EOLcnt = %d",
  271. BitAcc, BitsAvail, EOLcnt);
  272. #endif
  273. SYNC_EOL(EOF2D);
  274. NeedBits8(1, EOF2D);
  275. is1D = GetBits(1); /* 1D/2D-encoding tag bit */
  276. ClrBits(1);
  277. #ifdef FAX3_DEBUG
  278. printf(" %s\n-------------------- %"PRIu32"\n",
  279. is1D ? "1D" : "2D", tif->tif_row);
  280. fflush(stdout);
  281. #endif
  282. pb = sp->refruns;
  283. b1 = *pb++;
  284. if (is1D)
  285. EXPAND1D(EOF2Da);
  286. else
  287. EXPAND2D(EOF2Da);
  288. (*sp->fill)(buf, thisrun, pa, lastx);
  289. if (pa < thisrun + sp->nruns) {
  290. SETVALUE(0); /* imaginary change for reference */
  291. }
  292. SWAP(uint32_t*, sp->curruns, sp->refruns);
  293. buf += sp->b.rowbytes;
  294. occ -= sp->b.rowbytes;
  295. sp->line++;
  296. continue;
  297. EOF2D: /* premature EOF */
  298. CLEANUP_RUNS();
  299. EOF2Da: /* premature EOF */
  300. (*sp->fill)(buf, thisrun, pa, lastx);
  301. UNCACHE_STATE(tif, sp);
  302. return (-1);
  303. }
  304. UNCACHE_STATE(tif, sp);
  305. return (1);
  306. }
  307. #undef SWAP
  308. /*
  309. * Bit-fill a row according to the white/black
  310. * runs generated during G3/G4 decoding.
  311. */
  312. void
  313. _TIFFFax3fillruns(unsigned char* buf, uint32_t* runs, uint32_t* erun, uint32_t lastx)
  314. {
  315. static const unsigned char _fillmasks[] =
  316. { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff };
  317. unsigned char* cp;
  318. uint32_t x, bx, run;
  319. int32_t n;
  320. if ((erun-runs)&1)
  321. *erun++ = 0;
  322. x = 0;
  323. for (; runs < erun; runs += 2) {
  324. run = runs[0];
  325. if (x+run > lastx || run > lastx )
  326. run = runs[0] = (uint32_t) (lastx - x);
  327. if (run) {
  328. cp = buf + (x>>3);
  329. bx = x&7;
  330. if (run > 8-bx) {
  331. if (bx) { /* align to byte boundary */
  332. *cp++ &= 0xff << (8-bx);
  333. run -= 8-bx;
  334. }
  335. if( (n = run >> 3) != 0 ) { /* multiple bytes to fill */
  336. memset( cp, 0, n );
  337. cp += n;
  338. run &= 7;
  339. }
  340. if (run)
  341. cp[0] &= 0xff >> run;
  342. } else
  343. cp[0] &= ~(_fillmasks[run]>>bx);
  344. x += runs[0];
  345. }
  346. run = runs[1];
  347. if (x+run > lastx || run > lastx )
  348. run = runs[1] = lastx - x;
  349. if (run) {
  350. cp = buf + (x>>3);
  351. bx = x&7;
  352. if (run > 8-bx) {
  353. if (bx) { /* align to byte boundary */
  354. *cp++ |= 0xff >> bx;
  355. run -= 8-bx;
  356. }
  357. if( (n = run>>3) != 0 ) { /* multiple bytes to fill */
  358. memset( cp, 0xff, n );
  359. cp += n;
  360. run &= 7;
  361. }
  362. /* Explicit 0xff masking to make icc -check=conversions happy */
  363. if (run)
  364. cp[0] = (unsigned char)((cp[0] | (0xff00 >> run))&0xff);
  365. } else
  366. cp[0] |= _fillmasks[run]>>bx;
  367. x += runs[1];
  368. }
  369. }
  370. assert(x == lastx);
  371. }
  372. #undef ZERO
  373. #undef FILL
  374. static int
  375. Fax3FixupTags(TIFF* tif)
  376. {
  377. (void) tif;
  378. return (1);
  379. }
  380. /*
  381. * Setup G3/G4-related compression/decompression state
  382. * before data is processed. This routine is called once
  383. * per image -- it sets up different state based on whether
  384. * or not decoding or encoding is being done and whether
  385. * 1D- or 2D-encoded data is involved.
  386. */
  387. static int
  388. Fax3SetupState(TIFF* tif)
  389. {
  390. static const char module[] = "Fax3SetupState";
  391. TIFFDirectory* td = &tif->tif_dir;
  392. Fax3BaseState* sp = Fax3State(tif);
  393. int needsRefLine;
  394. Fax3CodecState* dsp = (Fax3CodecState*) Fax3State(tif);
  395. tmsize_t rowbytes;
  396. uint32_t rowpixels;
  397. if (td->td_bitspersample != 1) {
  398. TIFFErrorExt(tif->tif_clientdata, module,
  399. "Bits/sample must be 1 for Group 3/4 encoding/decoding");
  400. return (0);
  401. }
  402. /*
  403. * Calculate the scanline/tile widths.
  404. */
  405. if (isTiled(tif)) {
  406. rowbytes = TIFFTileRowSize(tif);
  407. rowpixels = td->td_tilewidth;
  408. } else {
  409. rowbytes = TIFFScanlineSize(tif);
  410. rowpixels = td->td_imagewidth;
  411. }
  412. if ((int64_t)rowbytes < ((int64_t)rowpixels + 7) / 8)
  413. {
  414. TIFFErrorExt(tif->tif_clientdata, module,
  415. "Inconsistent number of bytes per row : rowbytes=%" PRId64 " rowpixels=%" PRIu32,
  416. (int64_t) rowbytes, rowpixels);
  417. return (0);
  418. }
  419. sp->rowbytes = rowbytes;
  420. sp->rowpixels = rowpixels;
  421. /*
  422. * Allocate any additional space required for decoding/encoding.
  423. */
  424. needsRefLine = (
  425. (sp->groupoptions & GROUP3OPT_2DENCODING) ||
  426. td->td_compression == COMPRESSION_CCITTFAX4
  427. );
  428. /*
  429. Assure that allocation computations do not overflow.
  430. TIFFroundup and TIFFSafeMultiply return zero on integer overflow
  431. */
  432. dsp->runs=(uint32_t*) NULL;
  433. dsp->nruns = TIFFroundup_32(rowpixels,32);
  434. if (needsRefLine) {
  435. dsp->nruns = TIFFSafeMultiply(uint32_t, dsp->nruns, 2);
  436. }
  437. if ((dsp->nruns == 0) || (TIFFSafeMultiply(uint32_t, dsp->nruns, 2) == 0)) {
  438. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  439. "Row pixels integer overflow (rowpixels %"PRIu32")",
  440. rowpixels);
  441. return (0);
  442. }
  443. dsp->runs = (uint32_t*) _TIFFCheckMalloc(tif,
  444. TIFFSafeMultiply(uint32_t, dsp->nruns, 2),
  445. sizeof (uint32_t),
  446. "for Group 3/4 run arrays");
  447. if (dsp->runs == NULL)
  448. return (0);
  449. memset( dsp->runs, 0, TIFFSafeMultiply(uint32_t,dsp->nruns,2)*sizeof(uint32_t));
  450. dsp->curruns = dsp->runs;
  451. if (needsRefLine)
  452. dsp->refruns = dsp->runs + dsp->nruns;
  453. else
  454. dsp->refruns = NULL;
  455. if (td->td_compression == COMPRESSION_CCITTFAX3
  456. && is2DEncoding(dsp)) { /* NB: default is 1D routine */
  457. tif->tif_decoderow = Fax3Decode2D;
  458. tif->tif_decodestrip = Fax3Decode2D;
  459. tif->tif_decodetile = Fax3Decode2D;
  460. }
  461. if (needsRefLine) { /* 2d encoding */
  462. Fax3CodecState* esp = EncoderState(tif);
  463. /*
  464. * 2d encoding requires a scanline
  465. * buffer for the ``reference line''; the
  466. * scanline against which delta encoding
  467. * is referenced. The reference line must
  468. * be initialized to be ``white'' (done elsewhere).
  469. */
  470. esp->refline = (unsigned char*) _TIFFmalloc(rowbytes);
  471. if (esp->refline == NULL) {
  472. TIFFErrorExt(tif->tif_clientdata, module,
  473. "No space for Group 3/4 reference line");
  474. return (0);
  475. }
  476. } else /* 1d encoding */
  477. EncoderState(tif)->refline = NULL;
  478. return (1);
  479. }
  480. /*
  481. * CCITT Group 3 FAX Encoding.
  482. */
  483. #define Fax3FlushBits(tif, sp) { \
  484. if ((tif)->tif_rawcc >= (tif)->tif_rawdatasize) { \
  485. if( !TIFFFlushData1(tif) ) \
  486. return 0; \
  487. } \
  488. *(tif)->tif_rawcp++ = (uint8_t) (sp)->data; \
  489. (tif)->tif_rawcc++; \
  490. (sp)->data = 0, (sp)->bit = 8; \
  491. }
  492. #define _FlushBits(tif) { \
  493. if ((tif)->tif_rawcc >= (tif)->tif_rawdatasize) { \
  494. if( !TIFFFlushData1(tif) ) \
  495. return 0; \
  496. } \
  497. *(tif)->tif_rawcp++ = (uint8_t) data; \
  498. (tif)->tif_rawcc++; \
  499. data = 0, bit = 8; \
  500. }
  501. static const int _msbmask[9] =
  502. { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
  503. #define _PutBits(tif, bits, length) { \
  504. while (length > bit) { \
  505. data |= bits >> (length - bit); \
  506. length -= bit; \
  507. _FlushBits(tif); \
  508. } \
  509. assert( length < 9 ); \
  510. data |= (bits & _msbmask[length]) << (bit - length); \
  511. bit -= length; \
  512. if (bit == 0) \
  513. _FlushBits(tif); \
  514. }
  515. /*
  516. * Write a variable-length bit-value to
  517. * the output stream. Values are
  518. * assumed to be at most 16 bits.
  519. */
  520. static int
  521. Fax3PutBits(TIFF* tif, unsigned int bits, unsigned int length)
  522. {
  523. Fax3CodecState* sp = EncoderState(tif);
  524. unsigned int bit = sp->bit;
  525. int data = sp->data;
  526. _PutBits(tif, bits, length);
  527. sp->data = data;
  528. sp->bit = bit;
  529. return 1;
  530. }
  531. /*
  532. * Write a code to the output stream.
  533. */
  534. #define putcode(tif, te) Fax3PutBits(tif, (te)->code, (te)->length)
  535. #ifdef FAX3_DEBUG
  536. #define DEBUG_COLOR(w) (tab == TIFFFaxWhiteCodes ? w "W" : w "B")
  537. #define DEBUG_PRINT(what,len) { \
  538. int t; \
  539. printf("%08"PRIX32"/%-2d: %s%5d\t", data, bit, DEBUG_COLOR(what), len); \
  540. for (t = length-1; t >= 0; t--) \
  541. putchar(code & (1<<t) ? '1' : '0'); \
  542. putchar('\n'); \
  543. }
  544. #endif
  545. /*
  546. * Write the sequence of codes that describes
  547. * the specified span of zero's or one's. The
  548. * appropriate table that holds the make-up and
  549. * terminating codes is supplied.
  550. */
  551. static int
  552. putspan(TIFF* tif, int32_t span, const tableentry* tab)
  553. {
  554. Fax3CodecState* sp = EncoderState(tif);
  555. unsigned int bit = sp->bit;
  556. int data = sp->data;
  557. unsigned int code, length;
  558. while (span >= 2624) {
  559. const tableentry* te = &tab[63 + (2560>>6)];
  560. code = te->code;
  561. length = te->length;
  562. #ifdef FAX3_DEBUG
  563. DEBUG_PRINT("MakeUp", te->runlen);
  564. #endif
  565. _PutBits(tif, code, length);
  566. span -= te->runlen;
  567. }
  568. if (span >= 64) {
  569. const tableentry* te = &tab[63 + (span>>6)];
  570. assert(te->runlen == 64*(span>>6));
  571. code = te->code;
  572. length = te->length;
  573. #ifdef FAX3_DEBUG
  574. DEBUG_PRINT("MakeUp", te->runlen);
  575. #endif
  576. _PutBits(tif, code, length);
  577. span -= te->runlen;
  578. }
  579. code = tab[span].code;
  580. length = tab[span].length;
  581. #ifdef FAX3_DEBUG
  582. DEBUG_PRINT(" Term", tab[span].runlen);
  583. #endif
  584. _PutBits(tif, code, length);
  585. sp->data = data;
  586. sp->bit = bit;
  587. return 1;
  588. }
  589. /*
  590. * Write an EOL code to the output stream. The zero-fill
  591. * logic for byte-aligning encoded scanlines is handled
  592. * here. We also handle writing the tag bit for the next
  593. * scanline when doing 2d encoding.
  594. */
  595. static int
  596. Fax3PutEOL(TIFF* tif)
  597. {
  598. Fax3CodecState* sp = EncoderState(tif);
  599. unsigned int bit = sp->bit;
  600. int data = sp->data;
  601. unsigned int code, length, tparm;
  602. if (sp->b.groupoptions & GROUP3OPT_FILLBITS) {
  603. /*
  604. * Force bit alignment so EOL will terminate on
  605. * a byte boundary. That is, force the bit alignment
  606. * to 16-12 = 4 before putting out the EOL code.
  607. */
  608. int align = 8 - 4;
  609. if (align != sp->bit) {
  610. if (align > sp->bit)
  611. align = sp->bit + (8 - align);
  612. else
  613. align = sp->bit - align;
  614. tparm=align;
  615. _PutBits(tif, 0, tparm);
  616. }
  617. }
  618. code = EOL;
  619. length = 12;
  620. if (is2DEncoding(sp)) {
  621. code = (code<<1) | (sp->tag == G3_1D);
  622. length++;
  623. }
  624. _PutBits(tif, code, length);
  625. sp->data = data;
  626. sp->bit = bit;
  627. return 1;
  628. }
  629. /*
  630. * Reset encoding state at the start of a strip.
  631. */
  632. static int
  633. Fax3PreEncode(TIFF* tif, uint16_t s)
  634. {
  635. Fax3CodecState* sp = EncoderState(tif);
  636. (void) s;
  637. assert(sp != NULL);
  638. sp->bit = 8;
  639. sp->data = 0;
  640. sp->tag = G3_1D;
  641. /*
  642. * This is necessary for Group 4; otherwise it isn't
  643. * needed because the first scanline of each strip ends
  644. * up being copied into the refline.
  645. */
  646. if (sp->refline)
  647. _TIFFmemset(sp->refline, 0x00, sp->b.rowbytes);
  648. if (is2DEncoding(sp)) {
  649. float res = tif->tif_dir.td_yresolution;
  650. /*
  651. * The CCITT spec says that when doing 2d encoding, you
  652. * should only do it on K consecutive scanlines, where K
  653. * depends on the resolution of the image being encoded
  654. * (2 for <= 200 lpi, 4 for > 200 lpi). Since the directory
  655. * code initializes td_yresolution to 0, this code will
  656. * select a K of 2 unless the YResolution tag is set
  657. * appropriately. (Note also that we fudge a little here
  658. * and use 150 lpi to avoid problems with units conversion.)
  659. */
  660. if (tif->tif_dir.td_resolutionunit == RESUNIT_CENTIMETER)
  661. res *= 2.54f; /* convert to inches */
  662. sp->maxk = (res > 150 ? 4 : 2);
  663. sp->k = sp->maxk-1;
  664. } else
  665. sp->k = sp->maxk = 0;
  666. sp->line = 0;
  667. return (1);
  668. }
  669. static const unsigned char zeroruns[256] = {
  670. 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, /* 0x00 - 0x0f */
  671. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0x10 - 0x1f */
  672. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0x20 - 0x2f */
  673. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0x30 - 0x3f */
  674. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40 - 0x4f */
  675. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x50 - 0x5f */
  676. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60 - 0x6f */
  677. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x70 - 0x7f */
  678. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 - 0x8f */
  679. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 - 0x9f */
  680. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 - 0xaf */
  681. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 - 0xbf */
  682. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 - 0xcf */
  683. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 - 0xdf */
  684. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 - 0xef */
  685. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xf0 - 0xff */
  686. };
  687. static const unsigned char oneruns[256] = {
  688. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 - 0x0f */
  689. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 - 0x1f */
  690. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 - 0x2f */
  691. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x30 - 0x3f */
  692. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x40 - 0x4f */
  693. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 - 0x5f */
  694. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x60 - 0x6f */
  695. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 - 0x7f */
  696. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x80 - 0x8f */
  697. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x90 - 0x9f */
  698. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0xa0 - 0xaf */
  699. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0xb0 - 0xbf */
  700. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0xc0 - 0xcf */
  701. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0xd0 - 0xdf */
  702. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0xe0 - 0xef */
  703. 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 8, /* 0xf0 - 0xff */
  704. };
  705. /*
  706. * Find a span of ones or zeros using the supplied
  707. * table. The ``base'' of the bit string is supplied
  708. * along with the start+end bit indices.
  709. */
  710. static inline int32_t
  711. find0span(unsigned char* bp, int32_t bs, int32_t be)
  712. {
  713. int32_t bits = be - bs;
  714. int32_t n, span;
  715. bp += bs>>3;
  716. /*
  717. * Check partial byte on lhs.
  718. */
  719. if (bits > 0 && (n = (bs & 7)) != 0) {
  720. span = zeroruns[(*bp << n) & 0xff];
  721. if (span > 8-n) /* table value too generous */
  722. span = 8-n;
  723. if (span > bits) /* constrain span to bit range */
  724. span = bits;
  725. if (n+span < 8) /* doesn't extend to edge of byte */
  726. return (span);
  727. bits -= span;
  728. bp++;
  729. } else
  730. span = 0;
  731. if (bits >= (int32_t)(2 * 8 * sizeof(int64_t))) {
  732. int64_t* lp;
  733. /*
  734. * Align to int64_t boundary and check int64_t words.
  735. */
  736. while (!isAligned(bp, int64_t)) {
  737. if (*bp != 0x00)
  738. return (span + zeroruns[*bp]);
  739. span += 8;
  740. bits -= 8;
  741. bp++;
  742. }
  743. lp = (int64_t*) bp;
  744. while ((bits >= (int32_t)(8 * sizeof(int64_t))) && (0 == *lp)) {
  745. span += 8*sizeof (int64_t);
  746. bits -= 8*sizeof (int64_t);
  747. lp++;
  748. }
  749. bp = (unsigned char*) lp;
  750. }
  751. /*
  752. * Scan full bytes for all 0's.
  753. */
  754. while (bits >= 8) {
  755. if (*bp != 0x00) /* end of run */
  756. return (span + zeroruns[*bp]);
  757. span += 8;
  758. bits -= 8;
  759. bp++;
  760. }
  761. /*
  762. * Check partial byte on rhs.
  763. */
  764. if (bits > 0) {
  765. n = zeroruns[*bp];
  766. span += (n > bits ? bits : n);
  767. }
  768. return (span);
  769. }
  770. static inline int32_t
  771. find1span(unsigned char* bp, int32_t bs, int32_t be)
  772. {
  773. int32_t bits = be - bs;
  774. int32_t n, span;
  775. bp += bs>>3;
  776. /*
  777. * Check partial byte on lhs.
  778. */
  779. if (bits > 0 && (n = (bs & 7)) != 0) {
  780. span = oneruns[(*bp << n) & 0xff];
  781. if (span > 8-n) /* table value too generous */
  782. span = 8-n;
  783. if (span > bits) /* constrain span to bit range */
  784. span = bits;
  785. if (n+span < 8) /* doesn't extend to edge of byte */
  786. return (span);
  787. bits -= span;
  788. bp++;
  789. } else
  790. span = 0;
  791. if (bits >= (int32_t)(2 * 8 * sizeof(int64_t))) {
  792. int64_t* lp;
  793. /*
  794. * Align to int64_t boundary and check int64_t words.
  795. */
  796. while (!isAligned(bp, int64_t)) {
  797. if (*bp != 0xff)
  798. return (span + oneruns[*bp]);
  799. span += 8;
  800. bits -= 8;
  801. bp++;
  802. }
  803. lp = (int64_t*) bp;
  804. while ((bits >= (int32_t)(8 * sizeof(int64_t))) && (~((uint64_t)0) == (uint64_t)*lp)) {
  805. span += 8*sizeof (int64_t);
  806. bits -= 8*sizeof (int64_t);
  807. lp++;
  808. }
  809. bp = (unsigned char*) lp;
  810. }
  811. /*
  812. * Scan full bytes for all 1's.
  813. */
  814. while (bits >= 8) {
  815. if (*bp != 0xff) /* end of run */
  816. return (span + oneruns[*bp]);
  817. span += 8;
  818. bits -= 8;
  819. bp++;
  820. }
  821. /*
  822. * Check partial byte on rhs.
  823. */
  824. if (bits > 0) {
  825. n = oneruns[*bp];
  826. span += (n > bits ? bits : n);
  827. }
  828. return (span);
  829. }
  830. /*
  831. * Return the offset of the next bit in the range
  832. * [bs..be] that is different from the specified
  833. * color. The end, be, is returned if no such bit
  834. * exists.
  835. */
  836. #define finddiff(_cp, _bs, _be, _color) \
  837. (_bs + (_color ? find1span(_cp,_bs,_be) : find0span(_cp,_bs,_be)))
  838. /*
  839. * Like finddiff, but also check the starting bit
  840. * against the end in case start > end.
  841. */
  842. #define finddiff2(_cp, _bs, _be, _color) \
  843. (_bs < _be ? finddiff(_cp,_bs,_be,_color) : _be)
  844. /*
  845. * 1d-encode a row of pixels. The encoding is
  846. * a sequence of all-white or all-black spans
  847. * of pixels encoded with Huffman codes.
  848. */
  849. static int
  850. Fax3Encode1DRow(TIFF* tif, unsigned char* bp, uint32_t bits)
  851. {
  852. Fax3CodecState* sp = EncoderState(tif);
  853. int32_t span;
  854. uint32_t bs = 0;
  855. for (;;) {
  856. span = find0span(bp, bs, bits); /* white span */
  857. if( !putspan(tif, span, TIFFFaxWhiteCodes) )
  858. return 0;
  859. bs += span;
  860. if (bs >= bits)
  861. break;
  862. span = find1span(bp, bs, bits); /* black span */
  863. if( !putspan(tif, span, TIFFFaxBlackCodes) )
  864. return 0;
  865. bs += span;
  866. if (bs >= bits)
  867. break;
  868. }
  869. if (sp->b.mode & (FAXMODE_BYTEALIGN|FAXMODE_WORDALIGN)) {
  870. if (sp->bit != 8) /* byte-align */
  871. Fax3FlushBits(tif, sp);
  872. if ((sp->b.mode&FAXMODE_WORDALIGN) &&
  873. !isAligned(tif->tif_rawcp, uint16_t))
  874. Fax3FlushBits(tif, sp);
  875. }
  876. return (1);
  877. }
  878. static const tableentry horizcode =
  879. { 3, 0x1, 0 }; /* 001 */
  880. static const tableentry passcode =
  881. { 4, 0x1, 0 }; /* 0001 */
  882. static const tableentry vcodes[7] = {
  883. { 7, 0x03, 0 }, /* 0000 011 */
  884. { 6, 0x03, 0 }, /* 0000 11 */
  885. { 3, 0x03, 0 }, /* 011 */
  886. { 1, 0x1, 0 }, /* 1 */
  887. { 3, 0x2, 0 }, /* 010 */
  888. { 6, 0x02, 0 }, /* 0000 10 */
  889. { 7, 0x02, 0 } /* 0000 010 */
  890. };
  891. /*
  892. * 2d-encode a row of pixels. Consult the CCITT
  893. * documentation for the algorithm.
  894. */
  895. static int
  896. Fax3Encode2DRow(TIFF* tif, unsigned char* bp, unsigned char* rp, uint32_t bits)
  897. {
  898. #define PIXEL(buf,ix) ((((buf)[(ix)>>3]) >> (7-((ix)&7))) & 1)
  899. uint32_t a0 = 0;
  900. uint32_t a1 = (PIXEL(bp, 0) != 0 ? 0 : finddiff(bp, 0, bits, 0));
  901. uint32_t b1 = (PIXEL(rp, 0) != 0 ? 0 : finddiff(rp, 0, bits, 0));
  902. uint32_t a2, b2;
  903. for (;;) {
  904. b2 = finddiff2(rp, b1, bits, PIXEL(rp,b1));
  905. if (b2 >= a1) {
  906. /* Naive computation triggers -fsanitize=undefined,unsigned-integer-overflow */
  907. /* although it is correct unless the difference between both is < 31 bit */
  908. /* int32_t d = b1 - a1; */
  909. int32_t d = (b1 >= a1 && b1 - a1 <= 3U) ? (int32_t)(b1 - a1) :
  910. (b1 < a1 && a1 - b1 <= 3U) ? -(int32_t)(a1 - b1) : 0x7FFFFFFF;
  911. if (!(-3 <= d && d <= 3)) { /* horizontal mode */
  912. a2 = finddiff2(bp, a1, bits, PIXEL(bp,a1));
  913. if( !putcode(tif, &horizcode) )
  914. return 0;
  915. if (a0+a1 == 0 || PIXEL(bp, a0) == 0) {
  916. if( !putspan(tif, a1-a0, TIFFFaxWhiteCodes) )
  917. return 0;
  918. if( !putspan(tif, a2-a1, TIFFFaxBlackCodes) )
  919. return 0;
  920. } else {
  921. if( !putspan(tif, a1-a0, TIFFFaxBlackCodes) )
  922. return 0;
  923. if( !putspan(tif, a2-a1, TIFFFaxWhiteCodes) )
  924. return 0;
  925. }
  926. a0 = a2;
  927. } else { /* vertical mode */
  928. if( !putcode(tif, &vcodes[d+3]) )
  929. return 0;
  930. a0 = a1;
  931. }
  932. } else { /* pass mode */
  933. if( !putcode(tif, &passcode) )
  934. return 0;
  935. a0 = b2;
  936. }
  937. if (a0 >= bits)
  938. break;
  939. a1 = finddiff(bp, a0, bits, PIXEL(bp,a0));
  940. b1 = finddiff(rp, a0, bits, !PIXEL(bp,a0));
  941. b1 = finddiff(rp, b1, bits, PIXEL(bp,a0));
  942. }
  943. return (1);
  944. #undef PIXEL
  945. }
  946. /*
  947. * Encode a buffer of pixels.
  948. */
  949. static int
  950. Fax3Encode(TIFF* tif, uint8_t* bp, tmsize_t cc, uint16_t s)
  951. {
  952. static const char module[] = "Fax3Encode";
  953. Fax3CodecState* sp = EncoderState(tif);
  954. (void) s;
  955. if (cc % sp->b.rowbytes)
  956. {
  957. TIFFErrorExt(tif->tif_clientdata, module, "Fractional scanlines cannot be written");
  958. return (0);
  959. }
  960. while (cc > 0) {
  961. if ((sp->b.mode & FAXMODE_NOEOL) == 0)
  962. {
  963. if( !Fax3PutEOL(tif) )
  964. return 0;
  965. }
  966. if (is2DEncoding(sp)) {
  967. if (sp->tag == G3_1D) {
  968. if (!Fax3Encode1DRow(tif, bp, sp->b.rowpixels))
  969. return (0);
  970. sp->tag = G3_2D;
  971. } else {
  972. if (!Fax3Encode2DRow(tif, bp, sp->refline,
  973. sp->b.rowpixels))
  974. return (0);
  975. sp->k--;
  976. }
  977. if (sp->k == 0) {
  978. sp->tag = G3_1D;
  979. sp->k = sp->maxk-1;
  980. } else
  981. _TIFFmemcpy(sp->refline, bp, sp->b.rowbytes);
  982. } else {
  983. if (!Fax3Encode1DRow(tif, bp, sp->b.rowpixels))
  984. return (0);
  985. }
  986. bp += sp->b.rowbytes;
  987. cc -= sp->b.rowbytes;
  988. }
  989. return (1);
  990. }
  991. static int
  992. Fax3PostEncode(TIFF* tif)
  993. {
  994. Fax3CodecState* sp = EncoderState(tif);
  995. if (sp->bit != 8)
  996. Fax3FlushBits(tif, sp);
  997. return (1);
  998. }
  999. static int
  1000. _Fax3Close(TIFF* tif)
  1001. {
  1002. if ((Fax3State(tif)->mode & FAXMODE_NORTC) == 0 && tif->tif_rawcp) {
  1003. Fax3CodecState* sp = EncoderState(tif);
  1004. unsigned int code = EOL;
  1005. unsigned int length = 12;
  1006. int i;
  1007. if (is2DEncoding(sp)) {
  1008. code = (code<<1) | (sp->tag == G3_1D);
  1009. length++;
  1010. }
  1011. for (i = 0; i < 6; i++)
  1012. Fax3PutBits(tif, code, length);
  1013. Fax3FlushBits(tif, sp);
  1014. }
  1015. return 1;
  1016. }
  1017. static void
  1018. Fax3Close(TIFF* tif)
  1019. {
  1020. _Fax3Close(tif);
  1021. }
  1022. static void
  1023. Fax3Cleanup(TIFF* tif)
  1024. {
  1025. Fax3CodecState* sp = DecoderState(tif);
  1026. assert(sp != 0);
  1027. tif->tif_tagmethods.vgetfield = sp->b.vgetparent;
  1028. tif->tif_tagmethods.vsetfield = sp->b.vsetparent;
  1029. tif->tif_tagmethods.printdir = sp->b.printdir;
  1030. if (sp->runs)
  1031. _TIFFfree(sp->runs);
  1032. if (sp->refline)
  1033. _TIFFfree(sp->refline);
  1034. _TIFFfree(tif->tif_data);
  1035. tif->tif_data = NULL;
  1036. _TIFFSetDefaultCompressionState(tif);
  1037. }
  1038. #define FIELD_BADFAXLINES (FIELD_CODEC+0)
  1039. #define FIELD_CLEANFAXDATA (FIELD_CODEC+1)
  1040. #define FIELD_BADFAXRUN (FIELD_CODEC+2)
  1041. #define FIELD_OPTIONS (FIELD_CODEC+7)
  1042. static const TIFFField faxFields[] = {
  1043. { TIFFTAG_FAXMODE, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE, "FaxMode", NULL },
  1044. { TIFFTAG_FAXFILLFUNC, 0, 0, TIFF_ANY, 0, TIFF_SETGET_OTHER, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE, "FaxFillFunc", NULL },
  1045. { TIFFTAG_BADFAXLINES, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UINT32, FIELD_BADFAXLINES, TRUE, FALSE, "BadFaxLines", NULL },
  1046. { TIFFTAG_CLEANFAXDATA, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UINT16, FIELD_CLEANFAXDATA, TRUE, FALSE, "CleanFaxData", NULL },
  1047. { TIFFTAG_CONSECUTIVEBADFAXLINES, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UINT32, FIELD_BADFAXRUN, TRUE, FALSE, "ConsecutiveBadFaxLines", NULL }};
  1048. static const TIFFField fax3Fields[] = {
  1049. { TIFFTAG_GROUP3OPTIONS, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UINT32, FIELD_OPTIONS, FALSE, FALSE, "Group3Options", NULL },
  1050. };
  1051. static const TIFFField fax4Fields[] = {
  1052. { TIFFTAG_GROUP4OPTIONS, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UINT32, FIELD_OPTIONS, FALSE, FALSE, "Group4Options", NULL },
  1053. };
  1054. static int
  1055. Fax3VSetField(TIFF* tif, uint32_t tag, va_list ap)
  1056. {
  1057. Fax3BaseState* sp = Fax3State(tif);
  1058. const TIFFField* fip;
  1059. assert(sp != 0);
  1060. assert(sp->vsetparent != 0);
  1061. switch (tag) {
  1062. case TIFFTAG_FAXMODE:
  1063. sp->mode = (int) va_arg(ap, int);
  1064. return 1; /* NB: pseudo tag */
  1065. case TIFFTAG_FAXFILLFUNC:
  1066. DecoderState(tif)->fill = va_arg(ap, TIFFFaxFillFunc);
  1067. return 1; /* NB: pseudo tag */
  1068. case TIFFTAG_GROUP3OPTIONS:
  1069. /* XXX: avoid reading options if compression mismatches. */
  1070. if (tif->tif_dir.td_compression == COMPRESSION_CCITTFAX3)
  1071. sp->groupoptions = (uint32_t) va_arg(ap, uint32_t);
  1072. break;
  1073. case TIFFTAG_GROUP4OPTIONS:
  1074. /* XXX: avoid reading options if compression mismatches. */
  1075. if (tif->tif_dir.td_compression == COMPRESSION_CCITTFAX4)
  1076. sp->groupoptions = (uint32_t) va_arg(ap, uint32_t);
  1077. break;
  1078. case TIFFTAG_BADFAXLINES:
  1079. sp->badfaxlines = (uint32_t) va_arg(ap, uint32_t);
  1080. break;
  1081. case TIFFTAG_CLEANFAXDATA:
  1082. sp->cleanfaxdata = (uint16_t) va_arg(ap, uint16_vap);
  1083. break;
  1084. case TIFFTAG_CONSECUTIVEBADFAXLINES:
  1085. sp->badfaxrun = (uint32_t) va_arg(ap, uint32_t);
  1086. break;
  1087. default:
  1088. return (*sp->vsetparent)(tif, tag, ap);
  1089. }
  1090. if ((fip = TIFFFieldWithTag(tif, tag)) != NULL)
  1091. TIFFSetFieldBit(tif, fip->field_bit);
  1092. else
  1093. return 0;
  1094. tif->tif_flags |= TIFF_DIRTYDIRECT;
  1095. return 1;
  1096. }
  1097. static int
  1098. Fax3VGetField(TIFF* tif, uint32_t tag, va_list ap)
  1099. {
  1100. Fax3BaseState* sp = Fax3State(tif);
  1101. assert(sp != 0);
  1102. switch (tag) {
  1103. case TIFFTAG_FAXMODE:
  1104. *va_arg(ap, int*) = sp->mode;
  1105. break;
  1106. case TIFFTAG_FAXFILLFUNC:
  1107. *va_arg(ap, TIFFFaxFillFunc*) = DecoderState(tif)->fill;
  1108. break;
  1109. case TIFFTAG_GROUP3OPTIONS:
  1110. case TIFFTAG_GROUP4OPTIONS:
  1111. *va_arg(ap, uint32_t*) = sp->groupoptions;
  1112. break;
  1113. case TIFFTAG_BADFAXLINES:
  1114. *va_arg(ap, uint32_t*) = sp->badfaxlines;
  1115. break;
  1116. case TIFFTAG_CLEANFAXDATA:
  1117. *va_arg(ap, uint16_t*) = sp->cleanfaxdata;
  1118. break;
  1119. case TIFFTAG_CONSECUTIVEBADFAXLINES:
  1120. *va_arg(ap, uint32_t*) = sp->badfaxrun;
  1121. break;
  1122. default:
  1123. return (*sp->vgetparent)(tif, tag, ap);
  1124. }
  1125. return (1);
  1126. }
  1127. static void
  1128. Fax3PrintDir(TIFF* tif, FILE* fd, long flags)
  1129. {
  1130. Fax3BaseState* sp = Fax3State(tif);
  1131. assert(sp != 0);
  1132. (void) flags;
  1133. if (TIFFFieldSet(tif,FIELD_OPTIONS)) {
  1134. const char* sep = " ";
  1135. if (tif->tif_dir.td_compression == COMPRESSION_CCITTFAX4) {
  1136. fprintf(fd, " Group 4 Options:");
  1137. if (sp->groupoptions & GROUP4OPT_UNCOMPRESSED)
  1138. fprintf(fd, "%suncompressed data", sep);
  1139. } else {
  1140. fprintf(fd, " Group 3 Options:");
  1141. if (sp->groupoptions & GROUP3OPT_2DENCODING) {
  1142. fprintf(fd, "%s2-d encoding", sep);
  1143. sep = "+";
  1144. }
  1145. if (sp->groupoptions & GROUP3OPT_FILLBITS) {
  1146. fprintf(fd, "%sEOL padding", sep);
  1147. sep = "+";
  1148. }
  1149. if (sp->groupoptions & GROUP3OPT_UNCOMPRESSED)
  1150. fprintf(fd, "%suncompressed data", sep);
  1151. }
  1152. fprintf(fd, " (%" PRIu32 " = 0x%" PRIx32 ")\n",
  1153. sp->groupoptions,
  1154. sp->groupoptions);
  1155. }
  1156. if (TIFFFieldSet(tif,FIELD_CLEANFAXDATA)) {
  1157. fprintf(fd, " Fax Data:");
  1158. switch (sp->cleanfaxdata) {
  1159. case CLEANFAXDATA_CLEAN:
  1160. fprintf(fd, " clean");
  1161. break;
  1162. case CLEANFAXDATA_REGENERATED:
  1163. fprintf(fd, " receiver regenerated");
  1164. break;
  1165. case CLEANFAXDATA_UNCLEAN:
  1166. fprintf(fd, " uncorrected errors");
  1167. break;
  1168. }
  1169. fprintf(fd, " (%"PRIu16" = 0x%"PRIx16")\n",
  1170. sp->cleanfaxdata, sp->cleanfaxdata);
  1171. }
  1172. if (TIFFFieldSet(tif,FIELD_BADFAXLINES))
  1173. fprintf(fd, " Bad Fax Lines: %" PRIu32 "\n",
  1174. sp->badfaxlines);
  1175. if (TIFFFieldSet(tif,FIELD_BADFAXRUN))
  1176. fprintf(fd, " Consecutive Bad Fax Lines: %" PRIu32 "\n",
  1177. sp->badfaxrun);
  1178. if (sp->printdir)
  1179. (*sp->printdir)(tif, fd, flags);
  1180. }
  1181. static int
  1182. InitCCITTFax3(TIFF* tif)
  1183. {
  1184. static const char module[] = "InitCCITTFax3";
  1185. Fax3BaseState* sp;
  1186. /*
  1187. * Merge codec-specific tag information.
  1188. */
  1189. if (!_TIFFMergeFields(tif, faxFields, TIFFArrayCount(faxFields))) {
  1190. TIFFErrorExt(tif->tif_clientdata, "InitCCITTFax3",
  1191. "Merging common CCITT Fax codec-specific tags failed");
  1192. return 0;
  1193. }
  1194. /*
  1195. * Allocate state block so tag methods have storage to record values.
  1196. */
  1197. tif->tif_data = (uint8_t*)
  1198. _TIFFmalloc(sizeof (Fax3CodecState));
  1199. if (tif->tif_data == NULL) {
  1200. TIFFErrorExt(tif->tif_clientdata, module,
  1201. "No space for state block");
  1202. return (0);
  1203. }
  1204. _TIFFmemset(tif->tif_data, 0, sizeof (Fax3CodecState));
  1205. sp = Fax3State(tif);
  1206. sp->rw_mode = tif->tif_mode;
  1207. /*
  1208. * Override parent get/set field methods.
  1209. */
  1210. sp->vgetparent = tif->tif_tagmethods.vgetfield;
  1211. tif->tif_tagmethods.vgetfield = Fax3VGetField; /* hook for codec tags */
  1212. sp->vsetparent = tif->tif_tagmethods.vsetfield;
  1213. tif->tif_tagmethods.vsetfield = Fax3VSetField; /* hook for codec tags */
  1214. sp->printdir = tif->tif_tagmethods.printdir;
  1215. tif->tif_tagmethods.printdir = Fax3PrintDir; /* hook for codec tags */
  1216. sp->groupoptions = 0;
  1217. if (sp->rw_mode == O_RDONLY) /* FIXME: improve for in place update */
  1218. tif->tif_flags |= TIFF_NOBITREV; /* decoder does bit reversal */
  1219. DecoderState(tif)->runs = NULL;
  1220. TIFFSetField(tif, TIFFTAG_FAXFILLFUNC, _TIFFFax3fillruns);
  1221. EncoderState(tif)->refline = NULL;
  1222. /*
  1223. * Install codec methods.
  1224. */
  1225. tif->tif_fixuptags = Fax3FixupTags;
  1226. tif->tif_setupdecode = Fax3SetupState;
  1227. tif->tif_predecode = Fax3PreDecode;
  1228. tif->tif_decoderow = Fax3Decode1D;
  1229. tif->tif_decodestrip = Fax3Decode1D;
  1230. tif->tif_decodetile = Fax3Decode1D;
  1231. tif->tif_setupencode = Fax3SetupState;
  1232. tif->tif_preencode = Fax3PreEncode;
  1233. tif->tif_postencode = Fax3PostEncode;
  1234. tif->tif_encoderow = Fax3Encode;
  1235. tif->tif_encodestrip = Fax3Encode;
  1236. tif->tif_encodetile = Fax3Encode;
  1237. tif->tif_close = Fax3Close;
  1238. tif->tif_cleanup = Fax3Cleanup;
  1239. return (1);
  1240. }
  1241. int
  1242. TIFFInitCCITTFax3(TIFF* tif, int scheme)
  1243. {
  1244. (void) scheme;
  1245. if (InitCCITTFax3(tif)) {
  1246. /*
  1247. * Merge codec-specific tag information.
  1248. */
  1249. if (!_TIFFMergeFields(tif, fax3Fields,
  1250. TIFFArrayCount(fax3Fields))) {
  1251. TIFFErrorExt(tif->tif_clientdata, "TIFFInitCCITTFax3",
  1252. "Merging CCITT Fax 3 codec-specific tags failed");
  1253. return 0;
  1254. }
  1255. /*
  1256. * The default format is Class/F-style w/o RTC.
  1257. */
  1258. return TIFFSetField(tif, TIFFTAG_FAXMODE, FAXMODE_CLASSF);
  1259. } else
  1260. return 01;
  1261. }
  1262. /*
  1263. * CCITT Group 4 (T.6) Facsimile-compatible
  1264. * Compression Scheme Support.
  1265. */
  1266. #define SWAP(t,a,b) { t x; x = (a); (a) = (b); (b) = x; }
  1267. /*
  1268. * Decode the requested amount of G4-encoded data.
  1269. */
  1270. static int
  1271. Fax4Decode(TIFF* tif, uint8_t* buf, tmsize_t occ, uint16_t s)
  1272. {
  1273. DECLARE_STATE_2D(tif, sp, "Fax4Decode");
  1274. (void) s;
  1275. if (occ % sp->b.rowbytes)
  1276. {
  1277. TIFFErrorExt(tif->tif_clientdata, module, "Fractional scanlines cannot be read");
  1278. return (-1);
  1279. }
  1280. CACHE_STATE(tif, sp);
  1281. while (occ > 0) {
  1282. a0 = 0;
  1283. RunLength = 0;
  1284. pa = thisrun = sp->curruns;
  1285. pb = sp->refruns;
  1286. b1 = *pb++;
  1287. #ifdef FAX3_DEBUG
  1288. printf("\nBitAcc=%08"PRIX32", BitsAvail = %d\n", BitAcc, BitsAvail);
  1289. printf("-------------------- %d\n", tif->tif_row);
  1290. fflush(stdout);
  1291. #endif
  1292. EXPAND2D(EOFG4);
  1293. if (EOLcnt)
  1294. goto EOFG4;
  1295. if (((lastx + 7) >> 3) > (int)occ) /* check for buffer overrun */
  1296. {
  1297. TIFFErrorExt(tif->tif_clientdata, module,
  1298. "Buffer overrun detected : %"TIFF_SSIZE_FORMAT" bytes available, %d bits needed",
  1299. occ, lastx);
  1300. return -1;
  1301. }
  1302. (*sp->fill)(buf, thisrun, pa, lastx);
  1303. SETVALUE(0); /* imaginary change for reference */
  1304. SWAP(uint32_t*, sp->curruns, sp->refruns);
  1305. buf += sp->b.rowbytes;
  1306. occ -= sp->b.rowbytes;
  1307. sp->line++;
  1308. continue;
  1309. EOFG4:
  1310. NeedBits16( 13, BADG4 );
  1311. BADG4:
  1312. #ifdef FAX3_DEBUG
  1313. if( GetBits(13) != 0x1001 )
  1314. fputs( "Bad EOFB\n", stderr );
  1315. #endif
  1316. ClrBits( 13 );
  1317. if (((lastx + 7) >> 3) > (int)occ) /* check for buffer overrun */
  1318. {
  1319. TIFFErrorExt(tif->tif_clientdata, module,
  1320. "Buffer overrun detected : %"TIFF_SSIZE_FORMAT" bytes available, %d bits needed",
  1321. occ, lastx);
  1322. return -1;
  1323. }
  1324. (*sp->fill)(buf, thisrun, pa, lastx);
  1325. UNCACHE_STATE(tif, sp);
  1326. return ( sp->line ? 1 : -1); /* don't error on badly-terminated strips */
  1327. }
  1328. UNCACHE_STATE(tif, sp);
  1329. return (1);
  1330. }
  1331. #undef SWAP
  1332. /*
  1333. * Encode the requested amount of data.
  1334. */
  1335. static int
  1336. Fax4Encode(TIFF* tif, uint8_t* bp, tmsize_t cc, uint16_t s)
  1337. {
  1338. static const char module[] = "Fax4Encode";
  1339. Fax3CodecState *sp = EncoderState(tif);
  1340. (void) s;
  1341. if (cc % sp->b.rowbytes)
  1342. {
  1343. TIFFErrorExt(tif->tif_clientdata, module, "Fractional scanlines cannot be written");
  1344. return (0);
  1345. }
  1346. while (cc > 0) {
  1347. if (!Fax3Encode2DRow(tif, bp, sp->refline, sp->b.rowpixels))
  1348. return (0);
  1349. _TIFFmemcpy(sp->refline, bp, sp->b.rowbytes);
  1350. bp += sp->b.rowbytes;
  1351. cc -= sp->b.rowbytes;
  1352. }
  1353. return (1);
  1354. }
  1355. static int
  1356. Fax4PostEncode(TIFF* tif)
  1357. {
  1358. Fax3CodecState *sp = EncoderState(tif);
  1359. /* terminate strip w/ EOFB */
  1360. Fax3PutBits(tif, EOL, 12);
  1361. Fax3PutBits(tif, EOL, 12);
  1362. if (sp->bit != 8)
  1363. Fax3FlushBits(tif, sp);
  1364. return (1);
  1365. }
  1366. int
  1367. TIFFInitCCITTFax4(TIFF* tif, int scheme)
  1368. {
  1369. (void) scheme;
  1370. if (InitCCITTFax3(tif)) { /* reuse G3 support */
  1371. /*
  1372. * Merge codec-specific tag information.
  1373. */
  1374. if (!_TIFFMergeFields(tif, fax4Fields,
  1375. TIFFArrayCount(fax4Fields))) {
  1376. TIFFErrorExt(tif->tif_clientdata, "TIFFInitCCITTFax4",
  1377. "Merging CCITT Fax 4 codec-specific tags failed");
  1378. return 0;
  1379. }
  1380. tif->tif_decoderow = Fax4Decode;
  1381. tif->tif_decodestrip = Fax4Decode;
  1382. tif->tif_decodetile = Fax4Decode;
  1383. tif->tif_encoderow = Fax4Encode;
  1384. tif->tif_encodestrip = Fax4Encode;
  1385. tif->tif_encodetile = Fax4Encode;
  1386. tif->tif_postencode = Fax4PostEncode;
  1387. /*
  1388. * Suppress RTC at the end of each strip.
  1389. */
  1390. return TIFFSetField(tif, TIFFTAG_FAXMODE, FAXMODE_NORTC);
  1391. } else
  1392. return (0);
  1393. }
  1394. /*
  1395. * CCITT Group 3 1-D Modified Huffman RLE Compression Support.
  1396. * (Compression algorithms 2 and 32771)
  1397. */
  1398. /*
  1399. * Decode the requested amount of RLE-encoded data.
  1400. */
  1401. static int
  1402. Fax3DecodeRLE(TIFF* tif, uint8_t* buf, tmsize_t occ, uint16_t s)
  1403. {
  1404. DECLARE_STATE(tif, sp, "Fax3DecodeRLE");
  1405. int mode = sp->b.mode;
  1406. (void) s;
  1407. if (occ % sp->b.rowbytes)
  1408. {
  1409. TIFFErrorExt(tif->tif_clientdata, module, "Fractional scanlines cannot be read");
  1410. return (-1);
  1411. }
  1412. CACHE_STATE(tif, sp);
  1413. thisrun = sp->curruns;
  1414. while (occ > 0) {
  1415. a0 = 0;
  1416. RunLength = 0;
  1417. pa = thisrun;
  1418. #ifdef FAX3_DEBUG
  1419. printf("\nBitAcc=%08"PRIX32", BitsAvail = %d\n", BitAcc, BitsAvail);
  1420. printf("-------------------- %"PRIu32"\n", tif->tif_row);
  1421. fflush(stdout);
  1422. #endif
  1423. EXPAND1D(EOFRLE);
  1424. (*sp->fill)(buf, thisrun, pa, lastx);
  1425. /*
  1426. * Cleanup at the end of the row.
  1427. */
  1428. if (mode & FAXMODE_BYTEALIGN) {
  1429. int n = BitsAvail - (BitsAvail &~ 7);
  1430. ClrBits(n);
  1431. } else if (mode & FAXMODE_WORDALIGN) {
  1432. int n = BitsAvail - (BitsAvail &~ 15);
  1433. ClrBits(n);
  1434. if (BitsAvail == 0 && !isAligned(cp, uint16_t))
  1435. cp++;
  1436. }
  1437. buf += sp->b.rowbytes;
  1438. occ -= sp->b.rowbytes;
  1439. sp->line++;
  1440. continue;
  1441. EOFRLE: /* premature EOF */
  1442. (*sp->fill)(buf, thisrun, pa, lastx);
  1443. UNCACHE_STATE(tif, sp);
  1444. return (-1);
  1445. }
  1446. UNCACHE_STATE(tif, sp);
  1447. return (1);
  1448. }
  1449. int
  1450. TIFFInitCCITTRLE(TIFF* tif, int scheme)
  1451. {
  1452. (void) scheme;
  1453. if (InitCCITTFax3(tif)) { /* reuse G3 support */
  1454. tif->tif_decoderow = Fax3DecodeRLE;
  1455. tif->tif_decodestrip = Fax3DecodeRLE;
  1456. tif->tif_decodetile = Fax3DecodeRLE;
  1457. /*
  1458. * Suppress RTC+EOLs when encoding and byte-align data.
  1459. */
  1460. return TIFFSetField(tif, TIFFTAG_FAXMODE,
  1461. FAXMODE_NORTC|FAXMODE_NOEOL|FAXMODE_BYTEALIGN);
  1462. } else
  1463. return (0);
  1464. }
  1465. int
  1466. TIFFInitCCITTRLEW(TIFF* tif, int scheme)
  1467. {
  1468. (void) scheme;
  1469. if (InitCCITTFax3(tif)) { /* reuse G3 support */
  1470. tif->tif_decoderow = Fax3DecodeRLE;
  1471. tif->tif_decodestrip = Fax3DecodeRLE;
  1472. tif->tif_decodetile = Fax3DecodeRLE;
  1473. /*
  1474. * Suppress RTC+EOLs when encoding and word-align data.
  1475. */
  1476. return TIFFSetField(tif, TIFFTAG_FAXMODE,
  1477. FAXMODE_NORTC|FAXMODE_NOEOL|FAXMODE_WORDALIGN);
  1478. } else
  1479. return (0);
  1480. }
  1481. #endif /* CCITT_SUPPORT */
  1482. /* vim: set ts=8 sts=8 sw=8 noet: */
  1483. /*
  1484. * Local Variables:
  1485. * mode: c
  1486. * c-basic-offset: 8
  1487. * fill-column: 78
  1488. * End:
  1489. */