Subversion Repositories Games.Chess Giants

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. #ifndef TBDECODE
  2. /* *INDENT-OFF* */
  3. #  define       TBDECODE
  4. #  include <stdio.h>
  5. #  include <stdlib.h>
  6. #  include <string.h>
  7. #  include <time.h>
  8. #  ifndef CLOCKS_PER_SEC
  9. #    define CLOCKS_PER_SEC CLK_TCK
  10. #  endif
  11. /* ---------------------------- Error codes --------------------------- */
  12. /*                              -----------                             */
  13. #  define COMP_ERR_NONE     0   /* everything is OK                     */
  14. #  define COMP_ERR_READ     2   /* input file read error                */
  15. #  define COMP_ERR_NOMEM    5   /* no enough memory                     */
  16. #  define COMP_ERR_BROKEN   6   /* damaged compressed data              */
  17. #  define COMP_ERR_PARAM    7   /* incorrect function parameter         */
  18. #  define COMP_ERR_INTERNAL 9   /* everything else is internal error    */
  19.                                 /* hopefully it should never happen     */
  20. /* Almost all  functions listed further return one as  its result on of */
  21. /* codes given  above: if no  error occured then COMP_ERR_NONE (i.e. 0) */
  22. /* is returned, otherwise functions return  error  code  plus number of */
  23. /* line in "comp.c"  where the error  was detected multiplied  by  256; */
  24. /* line number may  be  used for exact specification  of  a place where */
  25. /* error was detected thus making debugging slightly simpler.           */
  26. /*                                                                      */
  27. /* Thus, "(code &  0xff)"  gives proper error code,  and  "(code >> 8)" */
  28. /* gives number of line where the error was raised.                     */
  29. /* -------------------------------------------------------------------- */
  30. /*                                                                      */
  31. /*                Compress/decompress some chess tables                 */
  32. /*                                                                      */
  33. /*               Copyright (c) 1991--1998 Andrew Kadatch                */
  34. /*                                                                      */
  35. /* The Limited-Reference  variant  of  Lempel-Ziv algorithm implemented */
  36. /* here was first described in  my  B.Sc.  thesis "Efficient algorithms */
  37. /* for image  compression",  Novosibirsk  State  University,  1992, and */
  38. /* cannot be  used in any product distributed in  Russia or CIS without */
  39. /* written permission from the author.                                  */
  40. /*                                                                      */
  41. /* Most of the code listed below is significantly  simplified code from */
  42. /* the PRS data compression library and therefore it should not be used */
  43. /* in any product (software or hardware, commercial or not, and  so on) */
  44. /* without written permission from the author.                          */
  45. /*                                                                      */
  46. /* -------------------------------------------------------------------- */
  47. /* ---------------------------- Debugging ----------------------------- */
  48. /*                              ---------                               */
  49. #  ifndef DEBUG
  50. #    define DEBUG       0
  51. #  endif
  52. #  if DEBUG
  53. #    define assert(cond) ((cond) ? (void) 0 : _local_assert (__LINE__))
  54. static void _local_assert(int lineno)
  55. {
  56.   fprintf(stderr, "assertion at line %u failed\n", lineno);
  57.   exit(33);
  58. }
  59.  
  60. #    define debug(x) x
  61. #    define dprintf(x) printf x
  62. #  else
  63. #    if !defined (assert)
  64. #      define assert(cond) ((void) 0)
  65. #    endif
  66. #    define debug(x)     ((void) 0)
  67. #    define dprintf(x)   ((void) 0)
  68. #  endif
  69. /* mob_pach */
  70. #  ifndef  __cplusplus
  71. int cbEGTBCompBytes = 0;
  72. #  else
  73. extern "C" {
  74.   int cbEGTBCompBytes = 0;
  75. }
  76. #  endif
  77. /* --------------------- Constants, types, etc. ----------------------- */
  78. /*                       ----------------------                         */
  79. #  define MIN_BLOCK_BITS        8
  80. /* LOG2 (min size of block to compress) */
  81. #  define MAX_BLOCK_BITS        16
  82. /* LOG2 (max size of block to compress) */
  83. /* max. integer we can take LOG2 by table       */
  84. #  define MAX_BITS_HALF ((MAX_BLOCK_BITS + 1) >> 1)
  85. #  define MAX_BITS      (MAX_BITS_HALF * 2)
  86. /* assume that integer is at least 32 bits wide */
  87. #  ifndef uchar
  88. #    define uchar unsigned char
  89. #  endif
  90. #  define HEADER_SIZE           80      /* number of reserved bytes     */
  91. #  define STOP_SEARCH_LENGTH    256     /* terminate search if match    */
  92.                                         /* length exceeds that value    */
  93. #  define MAX_LENGTH_BITS               5
  94. #  define MAX_LENGTH              (1 << MAX_LENGTH_BITS)
  95. #  define LONG_BITS               1
  96. #  define LONG_LENGTH           (MAX_BLOCK_BITS - LONG_BITS)
  97. #  define LONG_QUICK            (MAX_LENGTH - LONG_LENGTH)
  98. #  if LONG_LENGTH > (MAX_BLOCK_BITS - LONG_BITS)
  99. #    undef LONG_LENGTH
  100. #    define LONG_LENGTH         (MAX_BLOCK_BITS - LONG_BITS)
  101. #  endif
  102. #  if LONG_LENGTH >= MAX_LENGTH || LONG_LENGTH <= 0
  103. #    error LONG_LENGTH is out of range
  104. #  endif
  105. #  if LONG_BITS <= 0
  106. #    error LONG_BITS must be positive
  107. #  endif
  108. #  define DELTA (LONG_BITS + LONG_QUICK - 1)
  109. #  if (MAX_LENGTH - 1) - (LONG_LENGTH - LONG_BITS) != DELTA
  110. #    error Hmmm
  111. #  endif
  112. #  define MAX_DISTANCES         24
  113. #  define LOG_MAX_DISTANCES     6       /* see check below      */
  114. #  if MAX_DISTANCES > (1 << LOG_MAX_DISTANCES)
  115. #    error MAX_DISTANCES should not exceed (1 << LOG_MAX_DISTANCES)
  116. #  endif
  117. #  define ALPHABET_SIZE         (256 + (MAX_DISTANCES << MAX_LENGTH_BITS))
  118. #  define MAX_ALPHABET  ALPHABET_SIZE   /* max. alphabet handled by     */
  119.                                         /* Huffman coding routines      */
  120. #  define USE_CRC32             1
  121. /* 0 - use Fletcher's checksum, != 0 - use proper CRC32                 */
  122.     static uchar header_title[64] =
  123.     "Compressed by DATACOMP v 1.0 (c) 1991--1998 Andrew Kadatch\r\n\0";
  124.  
  125. #  define RET(n) ((n) + __LINE__ * 256)
  126. /* ------------------------- CRC32 routines --------------------------- */
  127. /*                           --------------                             */
  128. #  if USE_CRC32
  129. static unsigned CRC32_table[256];
  130. static int CRC32_initialized = 0;
  131. static void CRC32_init(void)
  132. {
  133.   int i, j;
  134.   unsigned k, m = (unsigned) 0xedb88320L;
  135.  
  136.   if (CRC32_initialized)
  137.     return;
  138.   for (i = 0; i < 256; ++i) {
  139.     k = i;
  140.     j = 8;
  141.     do {
  142.       if ((k & 1) != 0)
  143.         k >>= 1;
  144.       else {
  145.         k >>= 1;
  146.         k ^= m;
  147.       };
  148.     } while (--j);
  149.     CRC32_table[i] = k;
  150.   }
  151.   CRC32_initialized = 1;
  152. }
  153. static unsigned CRC32(uchar * p, int n, unsigned k)
  154. {
  155.   unsigned *table = CRC32_table;
  156.   uchar *e = p + n;
  157.  
  158.   while (p + 16 < e) {
  159. #    define X(i) k = table[((uchar) k) ^ p[i]] ^ (k >> 8)
  160.     X(0);
  161.     X(1);
  162.     X(2);
  163.     X(3);
  164.     X(4);
  165.     X(5);
  166.     X(6);
  167.     X(7);
  168.     X(8);
  169.     X(9);
  170.     X(10);
  171.     X(11);
  172.     X(12);
  173.     X(13);
  174.     X(14);
  175.     X(15);
  176. #    undef X
  177.     p += 16;
  178.   }
  179.   while (p < e)
  180.     k = table[((uchar) k) ^ *p++] ^ (k >> 8);
  181.   return (k);
  182. }
  183. #  else
  184. #    define CRC32_init()
  185. static unsigned CRC32(uchar * p, int n, unsigned k1)
  186. {
  187.   unsigned k0 = k1 & 0xffff;
  188.   uchar *e = p + n;
  189.  
  190.   k1 = (k1 >> 16) & 0xffff;
  191.   while (p + 16 < e) {
  192. #    define X(i) k0 += p[i]; k1 += k0;
  193.     X(0);
  194.     X(1);
  195.     X(2);
  196.     X(3);
  197.     X(4);
  198.     X(5);
  199.     X(6);
  200.     X(7);
  201.     X(8);
  202.     X(9);
  203.     X(10);
  204.     X(11);
  205.     X(12);
  206.     X(13);
  207.     X(14);
  208.     X(15);
  209. #    undef X
  210.     k0 = (k0 & 0xffff) + (k0 >> 16);
  211.     k1 = (k1 & 0xffff) + (k1 >> 16);
  212.     p += 16;
  213.   }
  214.   while (p < e) {
  215.     k0 += *p++;
  216.     k1 += k0;
  217.   }
  218.   k0 = (k0 & 0xffff) + (k0 >> 16);
  219.   k1 = (k1 & 0xffff) + (k1 >> 16);
  220.   k0 = (k0 & 0xffff) + (k0 >> 16);
  221.   k1 = (k1 & 0xffff) + (k1 >> 16);
  222.   assert(((k0 | k1) >> 16) == 0);
  223.   return (k0 + (k1 << 16));
  224. }
  225. #  endif                        /* USE_CRC32    */
  226. /* ------------------------ Bit IO interface -------------------------- */
  227. /*                          ----------------                            */
  228. #  define BITIO_LOCALS  \
  229.   unsigned int _mask;   \
  230.   int    _bits;         \
  231.   uchar *_ptr
  232. typedef struct {
  233.   BITIO_LOCALS;
  234. } bitio_t;
  235.  
  236. #  define BITIO_ENTER(p) do {   \
  237.   _mask = (p)._mask;            \
  238.   _bits = (p)._bits;            \
  239.   _ptr  = (p)._ptr;             \
  240. } while (0)
  241. #  define BITIO_LEAVE(p) do {   \
  242.   (p)._mask = _mask;            \
  243.   (p)._bits = _bits;            \
  244.   (p)._ptr  = _ptr;             \
  245. } while (0)
  246. #  define BIORD_START(from) do {        \
  247.   _ptr = (uchar *) (from);              \
  248.   _bits = sizeof (_mask);               \
  249.   _mask = 0;                            \
  250.   do                                    \
  251.     _mask = (_mask << 8) | *_ptr++;     \
  252.   while (--_bits != 0);                 \
  253.   _bits = 16;                           \
  254. } while (0)
  255. /* read [1, 17] bits at once */
  256. #  define BIORD(bits)      \
  257.   (_mask >> (8 * sizeof (_mask) - (bits)))
  258. #  define BIORD_MORE(bits) do {         \
  259.   _mask <<= (bits);                     \
  260.   if ((_bits -= (bits)) <= 0)           \
  261.   {                                     \
  262.     _mask |= ((_ptr[0] << 8) + _ptr[1]) << (-_bits);    \
  263.     _ptr += 2; _bits += 16;             \
  264.   }                                     \
  265. } while (0)
  266. /* ------------------------ Huffman coding ---------------------------- */
  267. /*                          --------------                              */
  268. #  if MAX_ALPHABET <= 0xffff
  269. #    if MAX_ALPHABET <= 1024
  270. /* positive value takes 15 bits => symbol number occupies <= 10 bits    */
  271. #      define huffman_decode_t  short
  272. #    else
  273. #      define huffman_decode_t  int
  274. #    endif
  275. #  else
  276. #    define huffman_decode_t    int
  277. #  endif
  278. #  define HUFFMAN_DECODE(ch,table,start_bits) do {      \
  279.   (ch) = table[BIORD (start_bits)];                     \
  280.   if (((int) (ch)) >= 0)                                \
  281.   {                                                     \
  282.     BIORD_MORE ((ch) & 31);                             \
  283.     (ch) >>= 5;                                         \
  284.     break;                                              \
  285.   }                                                     \
  286.   BIORD_MORE (start_bits);                              \
  287.   do                                                    \
  288.   {                                                     \
  289.     (ch) = table[BIORD (1) - (ch)];                     \
  290.     BIORD_MORE (1);                                     \
  291.   }                                                     \
  292.   while (((int) (ch)) < 0);                             \
  293. } while (0)
  294. #  define HUFFMAN_TABLE_SIZE(n,start_bits) \
  295.   ((1 << (start_bits)) + ((n) << 1))
  296. static int huffman_decode_create(huffman_decode_t * table, uchar * length,
  297.     int n, int start_bits)
  298. {
  299.   int i, j, k, last, freq[32], sum[32];
  300.  
  301. /* calculate number of codewords                                      */
  302.   memset(freq, 0, sizeof(freq));
  303.   for (i = 0; i < n; ++i) {
  304.     if ((k = length[i]) > 31)
  305.       return RET(COMP_ERR_BROKEN);
  306.     ++freq[k];
  307.   }
  308. /* handle special case(s) -- 0 and 1 symbols in alphabet              */
  309.   if (freq[0] == n) {
  310.     memset(table, 0, sizeof(table[0]) << start_bits);
  311.     return (0);
  312.   }
  313.   if (freq[0] == n - 1) {
  314.     if (freq[1] != 1)
  315.       return RET(COMP_ERR_BROKEN);
  316.     for (i = 0; length[i] == 0;)
  317.       ++i;
  318.     i <<= 5;
  319.     for (k = 1 << start_bits; --k >= 0;)
  320.       *table++ = (huffman_decode_t) i;
  321.     return (0);
  322.   }
  323. /* save frequences                    */
  324.   memcpy(sum, freq, sizeof(sum));
  325. /* check code correctness             */
  326.   k = 0;
  327.   for (i = 32; --i != 0;) {
  328.     if ((k += freq[i]) & 1)
  329.       return RET(COMP_ERR_BROKEN);
  330.     k >>= 1;
  331.   }
  332.   if (k != 1)
  333.     return RET(COMP_ERR_BROKEN);
  334. /* sort symbols               */
  335.   k = 0;
  336.   for (i = 1; i < 32; ++i)
  337.     freq[i] = (k += freq[i]);
  338.   last = freq[31];      /* preserve number of symbols in alphabet       */
  339.   for (i = n; --i >= 0;) {
  340.     if ((k = length[i]) != 0)
  341.       table[--freq[k]] = (huffman_decode_t) i;
  342.   }
  343. /* now create decoding table  */
  344.   k = i = (1 << start_bits) + (n << 1);
  345.   for (n = 32; --n > start_bits;) {
  346.     j = i;
  347.     while (k > j)
  348.       table[--i] = (huffman_decode_t) - (k -= 2);
  349.     for (k = sum[n]; --k >= 0;)
  350.       table[--i] = table[--last];
  351.     k = j;
  352.   }
  353.   j = i;
  354.   i = 1 << start_bits;
  355.   while (k > j)
  356.     table[--i] = (huffman_decode_t) - (k -= 2);
  357.   for (; n > 0; --n) {
  358.     for (k = sum[n]; --k >= 0;) {
  359.       assert(last <= i && last > 0);
  360.       j = i - (1 << (start_bits - n));
  361.       n |= table[--last] << 5;
  362.       do
  363.         table[--i] = (huffman_decode_t) n;
  364.       while (i != j);
  365.       n &= 31;
  366.     }
  367.   }
  368.   assert((i | last) == 0);
  369.   return (0);
  370. }
  371.  
  372. /* -------------------- Read/write Huffman code ----------------------- */
  373. /*                      -----------------------                         */
  374. #  define MIN_REPT      2
  375. #  if MIN_REPT <= 1
  376. #    error MIN_REPT must exceed 1
  377. #  endif
  378. #  define TEMP_TABLE_BITS 8
  379. static int huffman_read_length(bitio_t * bitio, uchar * length, int n)
  380. {
  381.   BITIO_LOCALS;
  382.   huffman_decode_t table[2][HUFFMAN_TABLE_SIZE(64, TEMP_TABLE_BITS)];
  383.   uchar bits[128];
  384.   int i, j, k;
  385.  
  386.   BITIO_ENTER(*bitio);
  387.   k = BIORD(1);
  388.   BIORD_MORE(1);
  389.   if (k != 0) {
  390.     memset(length, 0, n);
  391.     goto ret;
  392.   }
  393.   if (n <= 128) {
  394.     k = BIORD(5);
  395.     BIORD_MORE(5);
  396.     for (i = 0; i < n;) {
  397.       length[i] = (uchar) BIORD(k);
  398.       BIORD_MORE(k);
  399.       if (length[i++] == 0) {
  400.         j = i + BIORD(4);
  401.         BIORD_MORE(4);
  402.         if (j > n)
  403.           return RET(COMP_ERR_BROKEN);
  404.         while (i != j)
  405.           length[i++] = 0;
  406.       }
  407.     }
  408.     goto ret;
  409.   }
  410.   BITIO_LEAVE(*bitio);
  411.   i = huffman_read_length(bitio, bits, 128);
  412.   if (i != 0)
  413.     return (i);
  414.   i = huffman_decode_create(table[0], bits, 64, TEMP_TABLE_BITS);
  415.   if (i != 0)
  416.     return (i);
  417.   i = huffman_decode_create(table[1], bits + 64, 64, TEMP_TABLE_BITS);
  418.   if (i != 0)
  419.     return (i);
  420.   BITIO_ENTER(*bitio);
  421.   for (i = 0; i < n;) {
  422.     HUFFMAN_DECODE(k, table[0], TEMP_TABLE_BITS);
  423.     if (k <= 31) {
  424.       length[i++] = (uchar) k;
  425.       continue;
  426.     }
  427.     k &= 31;
  428.     HUFFMAN_DECODE(j, table[1], TEMP_TABLE_BITS);
  429.     if (j > 31) {
  430.       int jj = j - 32;
  431.  
  432.       j = 1 << jj;
  433.       if (jj != 0) {
  434.         if (jj > 16) {
  435.           j += BIORD(16) << (jj - 16);
  436.           BIORD_MORE(16);
  437.         }
  438.         j += BIORD(jj);
  439.         BIORD_MORE(jj);
  440.       }
  441.       j += 31;
  442.     }
  443.     j += MIN_REPT + i;
  444.     if (j > n)
  445.       return RET(COMP_ERR_BROKEN);
  446.     do
  447.       length[i] = (uchar) k;
  448.     while (++i != j);
  449.   }
  450. ret:
  451.   BITIO_LEAVE(*bitio);
  452.   return (0);
  453. }
  454.  
  455. /* ----------------------- Proper compression ------------------------- */
  456. /*                         ------------------                           */
  457. #  if MIN_BLOCK_BITS > MAX_BLOCK_BITS || MAX_BLOCK_BITS > MAX_BITS_HALF*2
  458. #    error condition MIN_BLOCK_BITS <= MAX_BLOCK_BITS <= MAX_BITS_HALF*2 failed
  459. #  endif
  460. #  define DECODE_MAGIC    ((int) 0x5abc947fL)
  461. #  define BLOCK_MAGIC     ((int) 0x79a3f29dL)
  462. #  define START_BITS      13
  463. #  define SHORT_INDEX     8u
  464. typedef struct {
  465.   huffman_decode_t table[HUFFMAN_TABLE_SIZE(ALPHABET_SIZE, START_BITS)];
  466.   int distance[MAX_DISTANCES];
  467.   unsigned *crc, *blk_u;
  468.   unsigned short *blk_s;
  469.   int block_size_log,           /* block_size is integral power of 2    */
  470.    block_size,                  /* 1 << block_size_log                  */
  471.    last_block_size,             /* [original] size of last block        */
  472.    n_blk,                       /* total number of blocks               */
  473.    comp_block_size,             /* size of largest compressed block+32  */
  474.    check_crc;                   /* check CRC32?                         */
  475.   uchar *comp;
  476.   int magic;
  477. } decode_info;
  478. typedef struct {
  479.   unsigned char *ptr;           /* pointer to the first decoded byte */
  480.   int decoded;                  /* number of bytes decoded so far    */
  481.   int total;                    /* total number of bytes in block    */
  482.   int number;                   /* number of this block              */
  483. } COMP_BLOCK_T;
  484.  
  485. /* Pointer to compressed data block                                     */
  486. typedef struct {
  487.   COMP_BLOCK_T b;
  488.   struct {
  489.     uchar *first;
  490.     int size;
  491.   } orig, comp;
  492.   struct {
  493.     uchar *ptr, *src;
  494.     int rept;
  495.   } emit;
  496.   bitio_t bitio;
  497.   int n;
  498.   int magic;
  499. } decode_block;
  500. static int calculate_offset(decode_info * info, unsigned n)
  501. {
  502.   unsigned i;
  503.  
  504.   i = n / (2 * SHORT_INDEX);
  505.   if (n & SHORT_INDEX)
  506.     return info->blk_u[i + 1] - info->blk_s[n];
  507.   else
  508.     return info->blk_u[i] + info->blk_s[n];
  509. }
  510. static void do_decode(decode_info * info, decode_block * block, uchar * e)
  511. {
  512.   BITIO_LOCALS;
  513.   uchar *p, *s = 0;
  514.   int ch;
  515.  
  516.   if ((p = block->emit.ptr) >= e)
  517.     return;
  518.   if (p == block->orig.first) {
  519.     BIORD_START(block->comp.first);
  520.     block->emit.rept = 0;
  521.   } else {
  522.     BITIO_ENTER(block->bitio);
  523.     if ((ch = block->emit.rept) != 0) {
  524.       block->emit.rept = 0;
  525.       s = block->emit.src;
  526.       goto copy;
  527.     }
  528.   }
  529. #  define OVER if (p < e) goto over; break
  530.   do {
  531.   over:
  532.     HUFFMAN_DECODE(ch, info->table, START_BITS);
  533.     if ((ch -= 256) < 0) {
  534.       *p++ = (uchar) ch;
  535.       OVER;
  536.     }
  537.     s = p + info->distance[ch >> MAX_LENGTH_BITS];
  538.     ch &= MAX_LENGTH - 1;
  539.     if (ch <= 3) {
  540.       p[0] = s[0];
  541.       p[1] = s[1];
  542.       p[2] = s[2];
  543.       p[3] = s[3];
  544.       p += ch + 1;
  545.       OVER;
  546.     } else if (ch >= LONG_LENGTH) {
  547.       ch -= LONG_LENGTH - LONG_BITS;
  548. #  if (MAX_BLOCK_BITS - 1) + (LONG_LENGTH - LONG_BITS) >= MAX_LENGTH
  549.       if (ch == DELTA) {
  550.         ch = BIORD(5);
  551.         BIORD_MORE(5);
  552.         ch += DELTA;
  553.       }
  554. #  endif
  555.       {
  556.         int n = 1 << ch;
  557.  
  558.         if (ch > 16) {
  559.           n += BIORD(16) << (ch -= 16);
  560.           BIORD_MORE(16);
  561.         }
  562.         n += BIORD(ch);
  563.         BIORD_MORE(ch);
  564.         ch = n;
  565.       }
  566.       ch += LONG_LENGTH - (1 << LONG_BITS);
  567.     }
  568.     ++ch;
  569.   copy:
  570.     if (ch > 16) {
  571.       if (p + ch > e) {
  572.         block->emit.rept = ch - (int) (e - p);
  573.         ch = (int) (e - p);
  574.         goto copy;
  575.       }
  576.       do {
  577. #  define X(i) p[i] = s[i]
  578.         X(0);
  579.         X(1);
  580.         X(2);
  581.         X(3);
  582.         X(4);
  583.         X(5);
  584.         X(6);
  585.         X(7);
  586.         X(8);
  587.         X(9);
  588.         X(10);
  589.         X(11);
  590.         X(12);
  591.         X(13);
  592.         X(14);
  593.         X(15);
  594. #  undef X
  595.         p += 16;
  596.         s += 16;
  597.       } while ((ch -= 16) > 16);
  598.     }
  599.     p += ch;
  600.     s += ch;
  601.     switch (ch) {
  602. #  define X(i) case i: p[-i] = s[-i]
  603.       X(16);
  604.       X(15);
  605.       X(14);
  606.       X(13);
  607.       X(12);
  608.       X(11);
  609.       X(10);
  610.       X(9);
  611.       X(8);
  612.       X(7);
  613.       X(6);
  614.       X(5);
  615.       X(4);
  616.       X(3);
  617.       X(2);
  618. #  undef X
  619.     }
  620.     p[-1] = s[-1];
  621.   } while (p < e);
  622. #  undef OVER
  623.   block->emit.ptr = p;
  624.   block->emit.src = s;
  625.   BITIO_LEAVE(block->bitio);
  626. }
  627.  
  628. /* pretty ugly */
  629. static int comp_open_file(decode_info ** res, FILE * fd, int check_crc)
  630. {
  631.   BITIO_LOCALS;
  632.   bitio_t Bitio;
  633.   uchar temp[ALPHABET_SIZE >= HEADER_SIZE ? ALPHABET_SIZE : HEADER_SIZE];
  634.   uchar *ptr;
  635.   int header_size, block_size, block_size_log, n_blk, i, n, n_s, n_u;
  636.   unsigned *blk_u, *blk;
  637.   unsigned short *blk_s;
  638.   decode_info *info;
  639.  
  640.   if (res == 0)
  641.     return RET(COMP_ERR_PARAM);
  642.   CRC32_init();
  643.   *res = 0;
  644.   if (fread(temp, 1, HEADER_SIZE, fd) != HEADER_SIZE)
  645.     return RET(COMP_ERR_READ);
  646.   if (memcmp(temp, header_title, 64) != 0)
  647.     return RET(COMP_ERR_READ);
  648.   ptr = temp;
  649. #  define R4(i) \
  650.   ((ptr[i] << 24) + (ptr[(i) + 1] << 16) + (ptr[(i) + 2] << 8) + (ptr[(i) + 3]))
  651.   header_size = R4(64);
  652.   block_size_log = ptr[70];
  653.   if (block_size_log > MAX_BITS || header_size < 84)
  654.     return RET(COMP_ERR_BROKEN);
  655.   block_size = 1 << block_size_log;
  656.   if (ptr[71] != MAX_DISTANCES)
  657.     return RET(COMP_ERR_BROKEN);
  658.   n_blk = R4(72);
  659.   if (R4(76) !=
  660.       (ALPHABET_SIZE << 12) + (LONG_BITS << 8) + (LONG_LENGTH << 4) +
  661.       MAX_LENGTH_BITS)
  662.     return RET(COMP_ERR_BROKEN);
  663.   if ((ptr = (uchar *) malloc(header_size)) == 0)
  664.     return RET(COMP_ERR_NOMEM);
  665.   if (fread(ptr + HEADER_SIZE, 1, header_size - HEADER_SIZE,
  666.           fd) != (size_t) (header_size - HEADER_SIZE)) {
  667.     free(ptr);
  668.     return RET(COMP_ERR_NOMEM);
  669.   }
  670.   memcpy(ptr, temp, HEADER_SIZE);
  671.   header_size -= 4;
  672.   if (CRC32(ptr, header_size, 0) != (unsigned) R4(header_size)) {
  673.     free(ptr);
  674.     return RET(COMP_ERR_BROKEN);
  675.   }
  676.   header_size += 4;
  677. /*
  678.    blk = (unsigned *) malloc (sizeof (unsigned) * (1 + n_blk));
  679.  */
  680.   n = sizeof(unsigned) * (1 + n_blk);
  681.   if (n < 4 * 1024 * 1024)
  682.     n = 4 * 1024 * 1024;
  683.   blk = (unsigned *) malloc(n);
  684.   if (blk == 0) {
  685.     free(ptr);
  686.     return RET(COMP_ERR_NOMEM);
  687.   }
  688.   n = sizeof(info->crc[0]) * (1 + (check_crc ? (2 * n_blk) : 0));
  689.   n_u = sizeof(unsigned) * (2 + n_blk / (2 * SHORT_INDEX));
  690.   n_s = sizeof(unsigned short) * (1 + n_blk);
  691.   if ((info = (decode_info *) malloc(sizeof(*info) + n + n_u + n_s)) == 0) {
  692.     free(ptr);
  693.     free(blk);
  694.     return RET(COMP_ERR_NOMEM);
  695.   }
  696.   cbEGTBCompBytes += sizeof(*info) + n + n_s + n_u;
  697.   info->crc = (unsigned *) (info + 1);
  698.   if (check_crc)
  699.     blk_u = info->blk_u = info->crc + 2 * n_blk;
  700.   else
  701.     blk_u = info->blk_u = info->crc;
  702.   blk_s = info->blk_s =
  703.       (unsigned short *) (blk_u + 2 + n_blk / (2 * SHORT_INDEX));
  704.   info->check_crc = check_crc;
  705.   info->block_size_log = block_size_log;
  706.   info->block_size = block_size;
  707.   info->n_blk = n_blk;
  708.   if (check_crc) {
  709.     n_blk <<= 1;
  710.     i = HEADER_SIZE;
  711.     for (n = 0; n < n_blk; ++n) {
  712.       info->crc[n] = R4(i);
  713.       i += 4;
  714.     }
  715.     n_blk >>= 1;
  716.   }
  717.   i = HEADER_SIZE + (n_blk << 3);
  718.   BIORD_START(ptr + i);
  719.   info->comp_block_size = 0;
  720.   for (n = 0; n <= n_blk; ++n) {
  721.     if ((blk[n] = BIORD(block_size_log)) == 0)
  722.       blk[n] = block_size;
  723.     if (info->comp_block_size < (int) (blk[n]))
  724.       info->comp_block_size = (int) (blk[n]);
  725.     BIORD_MORE(block_size_log);
  726.   }
  727.   info->comp_block_size += 32;
  728.   for (n = 0; n < MAX_DISTANCES; ++n) {
  729.     info->distance[n] = -((int) BIORD(block_size_log));
  730.     BIORD_MORE(block_size_log);
  731.   }
  732.   i += ((n_blk + 1 + MAX_DISTANCES) * block_size_log + 7) >> 3;
  733.   BIORD_START(ptr + i);
  734.   BITIO_LEAVE(Bitio);
  735.   if (huffman_read_length(&Bitio, temp, ALPHABET_SIZE) != 0) {
  736.     free(blk);
  737.     free(info);
  738.     free(ptr);
  739.     return RET(COMP_ERR_BROKEN);
  740.   }
  741.   if (huffman_decode_create(info->table, temp, ALPHABET_SIZE, START_BITS) != 0) {
  742.     free(blk);
  743.     free(info);
  744.     free(ptr);
  745.     return RET(COMP_ERR_BROKEN);
  746.   }
  747.   info->last_block_size = blk[n_blk];
  748.   blk[n_blk] = 0;
  749.   for (n = 0; n <= n_blk; ++n) {
  750.     i = blk[n];
  751.     blk[n] = header_size;
  752.     header_size += i;
  753.     if (0 == n % (2 * SHORT_INDEX))
  754.       blk_u[n / (2 * SHORT_INDEX)] = blk[n];
  755.   }
  756.   blk_u[n_blk / (2 * SHORT_INDEX) + 1] = blk[n_blk];
  757.   for (n = 0; n <= n_blk; ++n) {
  758.     i = n / (2 * SHORT_INDEX);
  759.     if (n & SHORT_INDEX)
  760.       blk_s[n] = blk_u[i + 1] - blk[n];
  761.     else
  762.       blk_s[n] = blk[n] - blk_u[i];
  763.   }
  764.   free(blk);
  765.   free(ptr);
  766.   info->comp = 0;
  767.   info->magic = DECODE_MAGIC;
  768.   *res = info;
  769.   return (COMP_ERR_NONE);
  770. }
  771. static int comp_init_block(decode_block * block, int block_size, uchar * orig)
  772. {
  773.   if (block == 0)
  774.     return RET(COMP_ERR_PARAM);
  775.   block->orig.first = orig;
  776.   block->comp.first = (uchar *) (block + 1);
  777.   block->b.ptr = 0;
  778.   block->b.decoded = -1;
  779.   block->b.total = -1;
  780.   block->b.number = -1;
  781.   block->n = -1;
  782.   block->magic = BLOCK_MAGIC;
  783.   return (COMP_ERR_NONE);
  784. }
  785. static int comp_alloc_block(decode_block ** ret_block, int block_size)
  786. {
  787.   decode_block *block;
  788.  
  789.   if (ret_block == 0)
  790.     return RET(COMP_ERR_PARAM);
  791.   *ret_block = 0;
  792.   if ((block = (decode_block *) malloc(sizeof(*block) + block_size)) == 0)
  793.     return RET(COMP_ERR_NOMEM);
  794.   cbEGTBCompBytes += sizeof(*block) + block_size;
  795.   if (0 != comp_init_block(block, block_size, NULL))
  796.     return RET(COMP_ERR_PARAM);
  797.   *ret_block = block;
  798.   return (COMP_ERR_NONE);
  799. }
  800.  
  801. #  define RETURN(n) \
  802.   return ((n) == COMP_ERR_NONE ? COMP_ERR_NONE : RET (n));
  803. static int comp_read_block(decode_block * block, decode_info * info, FILE * fd,
  804.     int n)
  805. {
  806.   int comp_size, orig_size, comp_start;
  807.   uchar *comp, *orig;
  808.  
  809.   if (block == 0 || block->magic != BLOCK_MAGIC)
  810.     return RET(COMP_ERR_PARAM);
  811.   assert(info->magic == DECODE_MAGIC);
  812.   if ((unsigned) n >= (unsigned) info->n_blk)
  813.     RETURN(COMP_ERR_PARAM);
  814.   comp = block->comp.first;
  815.   block->n = n;
  816.   orig = block->orig.first;
  817.   orig_size = info->block_size;
  818.   if (n == info->n_blk - 1)
  819.     orig_size = info->last_block_size;
  820.   block->orig.size = orig_size;
  821.   comp_start = calculate_offset(info, n);
  822.   block->comp.size = comp_size = calculate_offset(info, n + 1) - comp_start;
  823.   if (fseek(fd, comp_start, SEEK_SET) != 0)
  824.     RETURN(COMP_ERR_READ);
  825.   if (fread(comp, 1, comp_size, fd) != (size_t) comp_size)
  826.     RETURN(COMP_ERR_READ);
  827.   if (info->check_crc &&
  828.       info->crc[(n << 1) + 1] != CRC32(block->comp.first, comp_size, 0))
  829.     RETURN(COMP_ERR_BROKEN);
  830.   block->emit.rept = 0;
  831.   if (comp_size == orig_size) {
  832.     memcpy(orig, comp, comp_size);
  833.     block->emit.ptr = orig + comp_size;
  834.     block->b.decoded = comp_size;
  835.   } else {
  836.     block->emit.ptr = orig;
  837.     block->b.decoded = 0;
  838.   }
  839.   block->b.number = n;
  840.   block->b.ptr = orig;
  841.   block->b.total = orig_size;
  842.   RETURN(COMP_ERR_NONE);
  843. }
  844. static int comp_decode_and_check_crc(decode_block * block, decode_info * info,
  845.     int n, int check_crc)
  846. {
  847.   if (block == 0 || block->magic != BLOCK_MAGIC)
  848.     return RET(COMP_ERR_PARAM);
  849.   assert(info->magic == DECODE_MAGIC);
  850.   if ((unsigned) (n - 1) > (unsigned) (block->orig.size - 1))
  851.     RETURN(COMP_ERR_PARAM);
  852.   if (check_crc)
  853.     n = block->orig.size;
  854.   do_decode(info, block, block->orig.first + n);
  855.   block->b.ptr = block->orig.first;
  856.   block->b.total = block->orig.size;
  857.   if (block->b.decoded >= block->b.total) {
  858.     if (block->b.decoded > block->b.total)
  859.       RETURN(COMP_ERR_BROKEN);
  860.     if (block->emit.rept != 0)
  861.       RETURN(COMP_ERR_BROKEN);
  862.   }
  863.   if (check_crc && info->check_crc &&
  864.       info->crc[block->n << 1] != CRC32(block->orig.first, block->orig.size, 0))
  865.     RETURN(COMP_ERR_BROKEN);
  866.   RETURN(COMP_ERR_NONE);
  867. }
  868.  
  869. #  if !defined (COLOR_DECLARED)
  870. /*
  871.    Test driver
  872.  */
  873. #    define     CRC_CHECK       1
  874. int main(int argc, char *argv[])
  875. {
  876.   int i;
  877.   int size;
  878.   int result;
  879.   FILE *fp;
  880.   decode_info *comp_info;
  881.   decode_block *comp_block;
  882.   clock_t tStart, tEnd;
  883.   double dSeconds;
  884.   uchar rgbBuf[8192 + 32];
  885.  
  886.   if (2 != argc) {
  887.     printf("Invalid arguments\n");
  888.     exit(1);
  889.   }
  890.   fp = fopen(argv[1], "rb");
  891.   if (0 == fp) {
  892.     printf("Unable to open file\n");
  893.     exit(1);
  894.   }
  895.   result = comp_open_file(&comp_info, fp, CRC_CHECK);
  896.   if (0 != result) {
  897.     printf("Unable to read file (1): %d\n", result);
  898.     exit(1);
  899.   }
  900.   if (8192 != comp_info->block_size) {
  901.     printf("Invalid block size: %d\n", comp_info->block_size);
  902.     exit(1);
  903.   }
  904.   result = comp_alloc_block(&comp_block, comp_info->block_size);
  905.   if (0 != result) {
  906.     printf("Unable to allocate block: %d\n", result);
  907.     exit(1);
  908.   }
  909.   size = 0;
  910.   tStart = clock();
  911.   for (i = 0; i < comp_info->n_blk; i++) {
  912.     if (0 != (result =
  913.             comp_init_block(comp_block, comp_info->block_size, rgbBuf))) {
  914.       printf("Unable to init block: %d\n", result);
  915.       exit(1);
  916.     }
  917.     if (0 != (result = comp_read_block(comp_block, comp_info, fp, i))) {
  918.       printf("Unable to read block: %d\n", result);
  919.       exit(1);
  920.     }
  921.     size += comp_block->orig.size;
  922.     if (0 != (result =
  923.             comp_decode_and_check_crc(comp_block, comp_info,
  924.                 comp_block->orig.size, CRC_CHECK))) {
  925.       printf("Unable to decode block: %d\n", result);
  926.       exit(1);
  927.     }
  928.   }
  929.   tEnd = clock();
  930.   dSeconds = (double) (tEnd - tStart) / CLOCKS_PER_SEC;
  931.   printf("Total memory allocated: %dKb\n", (cbEGTBCompBytes + 1023) / 1024);
  932.   printf("%g seconds, %dMb, %gMb/sec)\n", dSeconds, size / (1024 * 1024),
  933.       size / (1024 * 1024) / dSeconds);
  934.   return 0;
  935. }
  936. #  endif
  937. /* *INDENT-ON* */
  938. #endif
  939.