Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1293 lines
28 KiB

  1. /* -------------------------------------------------------------------- */
  2. /* */
  3. /* Copyright (c) 1991-1999 by Andrew Kadatch */
  4. /* */
  5. /* -------------------------------------------------------------------- */
  6. #include "xprs.h"
  7. #define MAX_CHAIN 9
  8. // Zobrist hashing
  9. #define Z_HASH_SIZE_LOG (BUFF_SIZE_LOG - 1)
  10. #define Z_HASH_SIZE (1 << Z_HASH_SIZE_LOG)
  11. #define Z_HASH_SUM(b) (z_hash_map[0][b[0]] ^ z_hash_map[1][b[1]] ^ z_hash_map[2][b[2]])
  12. // quick hashing
  13. #define Q_HASH_SH1 3
  14. #define Q_HASH_SH2 (Q_HASH_SH1 >> 1)
  15. #define Q_HASH_SUM3(c1,c2,c3) (((c1) << Q_HASH_SH1) + ((c2) << Q_HASH_SH2) + (c3))
  16. #define Q_HASH_SUM(b) Q_HASH_SUM3 (b[0], b[1], b[2])
  17. #define Q_HASH_SIZE (Q_HASH_SUM3 (255, 255, 255) + 1)
  18. #define z_hash_t uint16
  19. #define z_index_t uint16
  20. #if CODING & (CODING_HUFF_LEN | CODING_HUFF_PTR | CODING_HUFF_ALL)
  21. typedef struct huff_node_t huff_node;
  22. struct huff_node_t
  23. {
  24. huff_node *son[2];
  25. uxint freq;
  26. uint16 ch;
  27. uint16 bits;
  28. };
  29. typedef struct
  30. {
  31. huff_node buff[2 * HUFF_SIZE], *head[256], **link[256];
  32. } huff_info;
  33. #endif
  34. typedef struct
  35. {
  36. struct
  37. {
  38. #if CODING == CODING_BY_BIT
  39. xint bits;
  40. uchar *ptr;
  41. #elif CODING & (CODING_DIRECT | CODING_DIRECT2)
  42. uchar *ptr;
  43. #elif CODING & (CODING_HUFF_LEN | CODING_HUFF_PTR | CODING_HUFF_ALL)
  44. uxint freq[HUFF_SIZE];
  45. uxint mask[HUFF_SIZE];
  46. uchar bits[HUFF_SIZE];
  47. huff_info info;
  48. uxint pointers;
  49. uxint extra;
  50. uxint masks;
  51. #endif /* CODING */
  52. } stat;
  53. xint chain;
  54. xint max_size;
  55. struct
  56. {
  57. uchar *beg;
  58. uchar *ptr;
  59. } comp;
  60. struct
  61. {
  62. z_index_t *hash;
  63. uchar *beg;
  64. uchar *ptr;
  65. uchar *tag_ptr;
  66. tag_t tag_mask;
  67. } temp;
  68. struct
  69. {
  70. xint len;
  71. xint pos;
  72. } match;
  73. struct
  74. {
  75. xint pos;
  76. xint size;
  77. xint stop;
  78. xint progress;
  79. const uchar *ptr;
  80. const uchar *end;
  81. const uchar *end_16;
  82. const uchar *end_3;
  83. const uchar *ptr_stop;
  84. } orig;
  85. } context;
  86. #define v p[-1].c
  87. typedef struct
  88. {
  89. union
  90. {
  91. z_index_t z_next[16];
  92. const uchar *q_last[16];
  93. } x;
  94. context c;
  95. } prs;
  96. #define MAGIC_ENCODE 0x53E7C0DE
  97. typedef struct
  98. {
  99. int magic;
  100. void *memory;
  101. prs *p;
  102. uchar *temp;
  103. unsigned max_size;
  104. int chain;
  105. } xpress_info;
  106. #if MAX_CHAIN >= 1
  107. static z_hash_t z_hash_map[MIN_MATCH][256];
  108. static int z_hash_map_initialized = 0;
  109. static void z_hash_map_init (void)
  110. {
  111. long v1, v2;
  112. z_hash_t *m;
  113. if (z_hash_map_initialized)
  114. return;
  115. v1 = 0x13579bdfL; v2 = 0x87654321L;
  116. for (m = z_hash_map[0]; m < z_hash_map[0] + sizeof (z_hash_map) / sizeof (z_hash_map[0][0]); ++m)
  117. {
  118. long vv1 = v2, vv2 = v1, d = 0;
  119. xint i = 32;
  120. do
  121. {
  122. d += 0x9e3779b9L; vv1 += d; vv2 += d;
  123. v1 += ((v2<<3) + vv1) ^ (v2 + d) ^ ((v2>>5) + vv2);
  124. v2 += ((v1<<3) + vv2) ^ (v1 + d) ^ ((v1>>5) + vv1);
  125. --i;
  126. }
  127. while (i);
  128. *m = (z_hash_t) ((v1 += v2) & (Z_HASH_SIZE - 1));
  129. }
  130. z_hash_map_initialized = 1;
  131. }
  132. static void z_hash_insert (prs *p)
  133. {
  134. const uchar *b, *e;
  135. xint n, h;
  136. z_index_t *hash = v.temp.hash;
  137. e = v.orig.end - (MIN_MATCH-1);
  138. b = v.orig.ptr;
  139. n = 0;
  140. for (; b < e; ++b, ++n)
  141. {
  142. h = Z_HASH_SUM (b);
  143. p->x.z_next[n] = hash[h];
  144. hash[h] = (z_index_t) n;
  145. }
  146. e += MIN_MATCH-1;
  147. for (; b < e; ++b, ++n)
  148. p->x.z_next[n] = 0;
  149. }
  150. #endif
  151. #if CODING != CODING_BY_BIT
  152. static void tag_write_start (prs *p)
  153. {
  154. v.temp.tag_ptr = v.temp.ptr;
  155. v.temp.ptr += sizeof (v.temp.tag_mask);
  156. v.temp.tag_mask = 1;
  157. }
  158. #if CODING == CODING_HUFF_ALL
  159. #define INC_MASKS ++v.stat.masks
  160. #else
  161. #define INC_MASKS
  162. #endif
  163. #define tag_write(p,ptr,n) \
  164. { \
  165. tag_t __n = n | (v.temp.tag_mask << 1); \
  166. if (v.temp.tag_mask < 0) \
  167. { \
  168. *(__unaligned tag_t *) v.temp.tag_ptr = __n; \
  169. v.temp.tag_ptr = ptr; \
  170. ptr += sizeof (v.temp.tag_mask); \
  171. INC_MASKS; \
  172. __n = 1; \
  173. } \
  174. v.temp.tag_mask = __n; \
  175. }
  176. static void tag_write_finish (prs *p)
  177. {
  178. uchar *ptr = v.temp.ptr;
  179. do
  180. {
  181. tag_write (p, ptr, 1);
  182. }
  183. while (ptr == v.temp.ptr);
  184. }
  185. #elif CODING == CODING_BY_BIT
  186. static void tag_write_start (prs *p)
  187. {
  188. v.temp.tag_ptr = (uchar *) &v.temp.tag_mask;
  189. v.temp.tag_mask = 0;
  190. v.stat.bits = 0;
  191. }
  192. #define tag_write(p,ptr,n) do { \
  193. if (--v.stat.bits < 0) \
  194. { \
  195. *(__unaligned tag_t *)v.temp.tag_ptr = v.temp.tag_mask; \
  196. v.temp.tag_mask = n; \
  197. v.stat.bits = 8 * sizeof (v.temp.tag_mask) - 1; \
  198. v.temp.tag_ptr = ptr; \
  199. ptr += sizeof (v.temp.tag_mask); \
  200. } \
  201. v.temp.tag_mask = (v.temp.tag_mask << 1) + (n); \
  202. } while (0)
  203. #define tag_write_mask(p,ptr,n,b) do { \
  204. if ((v.stat.bits -= (b)) < 0) \
  205. { \
  206. *(__unaligned tag_t *)v.temp.tag_ptr = (v.temp.tag_mask << ((b) + v.stat.bits)) \
  207. + ((n) >> (-v.stat.bits)); \
  208. v.stat.bits += 8 * sizeof (v.temp.tag_mask); \
  209. v.temp.tag_mask = (n); \
  210. v.temp.tag_ptr = ptr; \
  211. ptr += sizeof (v.temp.tag_mask); \
  212. } \
  213. else \
  214. v.temp.tag_mask = (v.temp.tag_mask << (b)) + (n); \
  215. } while (0);
  216. static void tag_write_finish (prs *p)
  217. {
  218. do
  219. tag_write (p, v.temp.ptr, 1);
  220. while (v.stat.bits != 0);
  221. *(__unaligned tag_t *)v.temp.tag_ptr = v.temp.tag_mask;
  222. }
  223. #define write_lit(p,ptr,ch) do { \
  224. tag_write (p, ptr, 0); \
  225. *ptr++ = (ch); \
  226. } while (0)
  227. INLINE uchar *write_ptr (prs *p, uchar *ptr, xint offset, xint length)
  228. {
  229. uxint k;
  230. --offset;
  231. k = 2;
  232. if (offset > 255) k = 3;
  233. tag_write_mask (p, ptr, k, 2);
  234. *ptr++ = (uchar) offset;
  235. if (offset > 255) *ptr++ = (uchar) (offset >>= 8);
  236. if (length <= 8)
  237. {
  238. length -= MIN_MATCH - 1;
  239. tag_write_mask (p, ptr, 1, length);
  240. }
  241. else
  242. {
  243. tag_write_mask (p, ptr, 0, (9 - MIN_MATCH));
  244. if ((length -= 9) < 15)
  245. {
  246. if (v.stat.ptr == 0)
  247. {
  248. v.stat.ptr = ptr;
  249. *ptr++ = (uchar) length;
  250. }
  251. else
  252. {
  253. v.stat.ptr[0] |= length << 4;
  254. v.stat.ptr = 0;
  255. }
  256. }
  257. else
  258. {
  259. length -= 15;
  260. if (v.stat.ptr == 0)
  261. {
  262. v.stat.ptr = ptr;
  263. *ptr++ = 15;
  264. }
  265. else
  266. {
  267. v.stat.ptr[0] += 0xf0;
  268. v.stat.ptr = 0;
  269. }
  270. *ptr++ = (uchar) length;
  271. if (length >= 255)
  272. {
  273. ptr[-1] = 255;
  274. ptr[0] = (uchar) length;
  275. ptr[1] = (uchar) (length >>= 8);
  276. ptr += 2;
  277. }
  278. }
  279. }
  280. return (ptr);
  281. }
  282. #endif
  283. #if CODING & (CODING_DIRECT | CODING_DIRECT2)
  284. #define write_lit(p,ptr,ch) do { \
  285. *ptr++ = (ch); \
  286. tag_write (p, ptr, 0); \
  287. } while (0)
  288. INLINE uchar *write_ptr (prs *p, uchar *ptr, int offset, int length)
  289. {
  290. length -= MIN_MATCH;
  291. --offset;
  292. #if CODING == CODING_DIRECT2
  293. offset <<= DIRECT2_LEN_LOG;
  294. if (length < DIRECT2_MAX_LEN)
  295. {
  296. offset |= length;
  297. ptr[0] = (uchar) offset;
  298. ptr[1] = (uchar) (offset >>= 8);
  299. ptr += 2;
  300. }
  301. else
  302. {
  303. offset |= DIRECT2_MAX_LEN;
  304. length -= DIRECT2_MAX_LEN;
  305. ptr[0] = (uchar) offset;
  306. ptr[1] = (uchar) (offset >>= 8);
  307. ptr += 2;
  308. if (v.stat.ptr == 0)
  309. {
  310. v.stat.ptr = ptr;
  311. *ptr++ = (uchar) (length < 15 ? length : 15);
  312. }
  313. else
  314. {
  315. v.stat.ptr[0] |= (uchar) ((length < 15 ? length : 15) << 4);
  316. v.stat.ptr = 0;
  317. }
  318. if ((length -= 15) >= 0)
  319. {
  320. *ptr++ = (uchar) length;
  321. if (length >= 255)
  322. {
  323. ptr[-1] = 255;
  324. length += DIRECT2_MAX_LEN + 15;
  325. ptr[0] = (uchar) length;
  326. ptr[1] = (uchar) (length >>= 8);
  327. ptr += 2;
  328. }
  329. }
  330. }
  331. #elif CODING == CODING_DIRECT
  332. if (v.stat.ptr == 0)
  333. {
  334. if (length < 7)
  335. {
  336. length <<= 5;
  337. v.stat.ptr = ptr;
  338. short_len:
  339. ptr[0] = (uchar) length;
  340. ptr[1] = (uchar) offset;
  341. ptr += 2;
  342. if (offset > 255)
  343. {
  344. ptr[0] = (uchar) (offset >>= 8);
  345. ptr[-2] = (uchar) (length += 16);
  346. ptr += 1;
  347. }
  348. }
  349. else if (length < 15 + 7)
  350. {
  351. length += (14 << 4) - 7;
  352. goto short_len;
  353. }
  354. else
  355. {
  356. if (offset > 255)
  357. ptr[0] = 0xff;
  358. else
  359. ptr[0] = 0xef;
  360. long_len:
  361. ptr[1] = (uchar) (length -= (7 + 15));
  362. ptr += 2;
  363. if (length >= 255)
  364. {
  365. length += 7 + 15;
  366. ptr[-1] = 255;
  367. ptr[0] = (uchar) (length);
  368. ptr[1] = (uchar) (length >>= 8);
  369. ptr += 2;
  370. }
  371. *ptr++ = (uchar) offset;
  372. if (offset > 255)
  373. *ptr++ = (uchar) (offset >>= 8);
  374. }
  375. }
  376. else
  377. {
  378. if (length < 7)
  379. {
  380. length |= v.stat.ptr[0];
  381. *ptr++ = (uchar) offset;
  382. if (offset > 255)
  383. {
  384. *ptr++ = (uchar) (offset >>= 8);
  385. length |= 8;
  386. }
  387. v.stat.ptr[0] = (uchar) length;
  388. v.stat.ptr = 0;
  389. }
  390. else if (length < 15 + 7)
  391. {
  392. length -= 7;
  393. ptr[1] = (uchar) offset;
  394. ptr[0] = (uchar) (length <<= 4);
  395. if (offset > 255)
  396. {
  397. v.stat.ptr[0] |= 15;
  398. v.stat.ptr = ptr;
  399. ptr[2] = (uchar) (offset >>= 8);
  400. ptr += 3;
  401. }
  402. else
  403. {
  404. v.stat.ptr[0] |= 7;
  405. v.stat.ptr = ptr;
  406. ptr += 2;
  407. }
  408. }
  409. else
  410. {
  411. if (offset > 255)
  412. v.stat.ptr[0] |= 15;
  413. else
  414. v.stat.ptr[0] |= 7;
  415. v.stat.ptr = ptr;
  416. ptr[0] = 15 << 4;
  417. goto long_len;
  418. }
  419. }
  420. #endif /* CODING */
  421. tag_write (p, ptr, 1);
  422. return (ptr);
  423. }
  424. #elif CODING & (CODING_HUFF_LEN | CODING_HUFF_PTR | CODING_HUFF_ALL)
  425. #if CODING == CODING_HUFF_ALL
  426. #define write_lit(p,ptr,ch) do { \
  427. ++v.stat.freq[*ptr++ = (ch)]; \
  428. tag_write (p, ptr, 0); \
  429. } while (0)
  430. #else
  431. #define write_lit(p,ptr,ch) do { \
  432. *ptr++ = (ch); \
  433. tag_write (p, ptr, 0); \
  434. } while (0)
  435. #endif
  436. #define BIOWR(mask,bits) { \
  437. assert (((mask) >> (bits)) == 0); \
  438. if ((Bits -= (bits)) < 0) \
  439. { \
  440. *(__unaligned bitmask2 *)Ptr1 = (bitmask2) ((Mask << (Bits + (bits))) \
  441. + ((mask) >> (-Bits))); \
  442. Mask = (mask); \
  443. Bits += sizeof (ubitmask2) * 8; \
  444. Ptr1 = Ptr2; \
  445. Ptr2 = (ubitmask2 *) ptr; \
  446. ptr += sizeof (ubitmask2); \
  447. } \
  448. else \
  449. Mask = (Mask << (bits)) + (mask); \
  450. }
  451. #if CODING == CODING_HUFF_LEN
  452. INLINE uchar *write_ptr (prs *p, uchar *ptr, int offset, int length)
  453. {
  454. xint k;
  455. ++v.stat.pointers;
  456. length -= MIN_MATCH;
  457. --offset;
  458. k = 0; if (offset > 255) k = 1;
  459. if (length < MAX_LENGTH - 1)
  460. ++v.stat.freq[*ptr++ = (uchar) (k |= length << 1)];
  461. else
  462. {
  463. length -= MAX_LENGTH - 1;
  464. ++v.stat.freq[ptr[0] = (uchar) (k |= (MAX_LENGTH - 1) << 1)];
  465. ptr[1] = (uchar) length;
  466. ptr += 2;
  467. if (length >= 255)
  468. {
  469. ptr[-1] = 255;
  470. length += MAX_LENGTH - 1;
  471. ptr[0] = (uchar) length;
  472. ptr[1] = (uchar) (length >>= 8);
  473. ptr += 2;
  474. }
  475. }
  476. *ptr++ = (uchar) offset;
  477. if (offset > 255)
  478. *ptr++ = (uchar) (offset >>= 8);
  479. tag_write (p, ptr, 1);
  480. return (ptr);
  481. }
  482. static void encode_pass2 (prs *p)
  483. {
  484. xint Bits;
  485. ubitmask4 Mask;
  486. ubitmask2 *Ptr1, *Ptr2;
  487. tag_t bmask;
  488. uchar *src = v.temp.beg;
  489. uchar *ptr = v.comp.ptr;
  490. uxint k;
  491. Ptr1 = (ubitmask2 *) ptr; ptr += sizeof (ubitmask2);
  492. Ptr2 = (ubitmask2 *) ptr; ptr += sizeof (ubitmask2);
  493. Mask = 0;
  494. Bits = 8 * sizeof (ubitmask2);
  495. bmask = 0;
  496. goto start;
  497. next:
  498. if (bmask >= 0)
  499. {
  500. bmask <<= 1;
  501. copy_byte:
  502. *ptr++ = *src++;
  503. goto next;
  504. }
  505. if ((bmask <<= 1) == 0)
  506. {
  507. start:
  508. *(__unaligned tag_t *)ptr = bmask = *(__unaligned tag_t *)src;
  509. src += sizeof (tag_t);
  510. ptr += sizeof (tag_t);
  511. if (bmask >= 0)
  512. {
  513. bmask = (bmask << 1) + 1;
  514. goto copy_byte;
  515. }
  516. bmask = (bmask << 1) + 1;
  517. }
  518. if (src >= v.temp.ptr)
  519. goto done;
  520. k = *src++;
  521. assert (k < HUFF_SIZE);
  522. BIOWR (v.stat.mask[k], v.stat.bits[k]);
  523. if (k >= ((MAX_LENGTH - 1) << 1))
  524. {
  525. if ((*ptr++ = *src++) == 255)
  526. {
  527. ptr[0] = src[0];
  528. ptr[1] = src[1];
  529. src += 2;
  530. ptr += 2;
  531. }
  532. }
  533. *ptr++ = *src++;
  534. if (k & 1) *ptr++ = *src++;
  535. goto next;
  536. done:
  537. *Ptr1 = (ubitmask2) (Mask <<= Bits);
  538. *Ptr2 = 0;
  539. v.comp.ptr = ptr;
  540. assert (src == v.temp.ptr);
  541. }
  542. #elif CODING & (CODING_HUFF_PTR | CODING_HUFF_ALL)
  543. #define MAX_BITNO_LOG 8
  544. #define MAX_BITNO (1 << MAX_BITNO_LOG)
  545. static uchar bitno_table[MAX_BITNO];
  546. static int bitno_table_initialized = 0;
  547. static void bitno_init (void)
  548. {
  549. int i, k, n;
  550. if (bitno_table_initialized)
  551. return;
  552. bitno_table[0] = 255;
  553. for (i = 0; i < MAX_BITNO_LOG; ++i)
  554. {
  555. for (n = (k = 1<<i) << 1; k < n; ++k)
  556. bitno_table[k] = (uchar) i;
  557. }
  558. bitno_table_initialized = 1;
  559. }
  560. static int bitno (uxint n)
  561. {
  562. assert (n != 0 && (n >> (MAX_BITNO_LOG*2)) == 0);
  563. if (n >= MAX_BITNO)
  564. return (bitno_table[n >> MAX_BITNO_LOG] + MAX_BITNO_LOG);
  565. return (bitno_table[n]);
  566. }
  567. #if CODING == CODING_HUFF_ALL
  568. #define CODING_ADJUST(n) (256 + (n))
  569. #else
  570. #define CODING_ADJUST(n) (n)
  571. #endif
  572. INLINE uchar *write_ptr (prs *p, uchar *ptr, int offset, int length)
  573. {
  574. xint k;
  575. k = bitno (offset);
  576. length -= MIN_MATCH;
  577. offset ^= 1 << k;
  578. v.stat.pointers += 2;
  579. v.stat.extra += k;
  580. k <<= MAX_LENGTH_LOG;
  581. if (length < MAX_LENGTH - 1)
  582. {
  583. k |= length;
  584. *ptr++ = (uchar) k;
  585. ++v.stat.freq[CODING_ADJUST (k)];
  586. }
  587. else
  588. {
  589. k |= MAX_LENGTH - 1;
  590. length -= MAX_LENGTH - 1;
  591. ptr[0] = (uchar) k;
  592. ++v.stat.freq[CODING_ADJUST (k)];
  593. ptr[1] = (uchar) length;
  594. ptr += 2;
  595. if (length >= 255)
  596. {
  597. length += MAX_LENGTH - 1;
  598. ptr[-1] = 255;
  599. ptr[0] = (uchar) length;
  600. ptr[1] = (uchar) (length >>= 8);
  601. ptr += 2;
  602. }
  603. }
  604. *ptr++ = (uchar) offset;
  605. if (k >= (9 << MAX_LENGTH_LOG))
  606. {
  607. v.stat.pointers += 1;
  608. *ptr++ = (uchar) (offset >>= 8);
  609. }
  610. tag_write (p, ptr, 1);
  611. return (ptr);
  612. }
  613. static void encode_pass2 (prs *p)
  614. {
  615. xint Bits;
  616. uxint Mask;
  617. ubitmask2 *Ptr1, *Ptr2;
  618. tag_t bmask;
  619. uchar *src = v.temp.beg;
  620. uchar *ptr = v.comp.ptr;
  621. uxint k;
  622. Ptr1 = (ubitmask2 *) ptr; ptr += sizeof (ubitmask2);
  623. Ptr2 = (ubitmask2 *) ptr; ptr += sizeof (ubitmask2);
  624. Mask = 0;
  625. Bits = 8 * sizeof (ubitmask2);
  626. bmask = 0;
  627. goto start;
  628. next:
  629. if (bmask >= 0)
  630. {
  631. bmask <<= 1;
  632. copy_byte:
  633. #if CODING == CODING_HUFF_ALL
  634. k = *src++;
  635. BIOWR (v.stat.mask[k], v.stat.bits[k]);
  636. #elif CODING == CODING_HUFF_PTR
  637. *ptr++ = *src++;
  638. #endif
  639. goto next;
  640. }
  641. if ((bmask <<= 1) == 0)
  642. {
  643. start:
  644. bmask = *(__unaligned tag_t *)src;
  645. src += sizeof (tag_t);
  646. #if CODING == CODING_HUFF_PTR
  647. *(__unaligned tag_t *)ptr = bmask;
  648. ptr += sizeof (tag_t);
  649. #endif
  650. if (bmask >= 0)
  651. {
  652. bmask = (bmask << 1) + 1;
  653. goto copy_byte;
  654. }
  655. bmask = (bmask << 1) + 1;
  656. }
  657. if (src >= v.temp.ptr)
  658. goto done;
  659. k = *src++;
  660. assert (k < HUFF_SIZE);
  661. BIOWR (v.stat.mask[CODING_ADJUST (k)], v.stat.bits[CODING_ADJUST (k)]);
  662. if ((k & (MAX_LENGTH - 1)) == MAX_LENGTH - 1)
  663. {
  664. if ((*ptr++ = *src++) == 255)
  665. {
  666. ptr[0] = src[0];
  667. ptr[1] = src[1];
  668. src += 2;
  669. ptr += 2;
  670. }
  671. }
  672. k >>= MAX_LENGTH_LOG;
  673. {
  674. uxint m = *src++;
  675. if (k > 8)
  676. m += *src++ << 8;
  677. BIOWR (m, k);
  678. }
  679. goto next;
  680. done:
  681. #if CODING == CODING_HUFF_ALL
  682. BIOWR (v.stat.mask[CODING_ADJUST(0)], v.stat.bits[CODING_ADJUST(0)]);
  683. #endif
  684. *Ptr1 = (ubitmask2) (Mask <<= Bits);
  685. *Ptr2 = 0;
  686. v.comp.ptr = ptr;
  687. assert (src == v.temp.ptr);
  688. }
  689. #endif
  690. /* ------------------ Create canonical Huffman code ------------------- */
  691. /* ----------------------------- */
  692. #define MAX_ALPHABET HUFF_SIZE
  693. static void huffman_create_codes (huff_info *info, uxint *freq, xint n, uxint *mask, uchar *length, uxint maxbits, uchar *encoded, uxint *total)
  694. {
  695. huff_node
  696. *p, *q, *r,
  697. *first_sorted, *first_free;
  698. xint i, k;
  699. assert ((uxint) (n-1) <= (MAX_ALPHABET-1));
  700. /* honestly it is easy enough to create Huffman code in-place */
  701. /* but the use of explicit data structures makes code simpler */
  702. /* clean everything up */
  703. memset (length, 0, sizeof (length[0]) * n);
  704. memset (encoded, 0, (n + 1) >> 1);
  705. if (mask != 0 && mask != freq)
  706. memset (mask, 0, sizeof (mask[0]) * n);
  707. /* store frequencies */
  708. p = info->buff;
  709. for (i = 0; i < n; ++i)
  710. {
  711. if ((p->freq = freq[i]) != 0)
  712. {
  713. p->son[0] = p+1; p->son[1] = 0;
  714. p->ch = (uint16) i;
  715. ++p;
  716. }
  717. }
  718. /* handle simple case */
  719. *total = 0;
  720. if (p <= info->buff + 1)
  721. {
  722. if (p == info->buff) /* if no symbols do nothing */
  723. return;
  724. i = p[-1].ch; /* single symbol code */
  725. mask[i] = 0;
  726. encoded[i >> 1] = 0x11; /* two symbols has 1-bit length */
  727. return;
  728. }
  729. first_free = p; /* store location of first unused node */
  730. p[-1].son[0] = 0; /* terminate the list */
  731. /* radix sort the list by frequency */
  732. p = info->buff; /* head of the list */
  733. /* initialize */
  734. for (n = 0; n < 256; ++n)
  735. *(info->link[n] = info->head + n) = 0;
  736. for (i = 0; i < (BUFF_SIZE_LOG <= 16 ? 16 : 32); i += 8)
  737. {
  738. /* link node to the end of respective bucket */
  739. do
  740. {
  741. n = (p->freq >> i) & 0xff;
  742. info->link[n][0] = p; info->link[n] = p->son;
  743. }
  744. while ((p = p->son[0]) != 0);
  745. /* merge buckets into single list */
  746. n = 0;
  747. while (info->head[n] == 0) ++n;
  748. p = info->head[n]; info->head[k = n] = 0;
  749. while (++n < 256)
  750. {
  751. if (info->head[n] == 0) continue;
  752. info->link[k][0] = info->head[n]; info->link[k] = info->head + k; info->head[n] = 0;
  753. k = n;
  754. }
  755. info->link[k][0] = 0; info->link[k] = info->head + k;
  756. }
  757. first_sorted = p; /* store head of sorted symbol's list */
  758. restart:
  759. assert (p == first_sorted);
  760. q = first_free;
  761. r = q - 1;
  762. while (p != 0 || q != r)
  763. {
  764. ++r;
  765. /* select left subtree */
  766. assert (q <= r && (p != 0 || q != r));
  767. if (p == 0 || (q != r && p->freq > q->freq))
  768. {
  769. r->son[0] = q; r->freq = q->freq; ++q;
  770. }
  771. else
  772. {
  773. r->son[0] = p; r->freq = p->freq; p = p->son[0];
  774. }
  775. /* select right subtree */
  776. assert (q <= r && (p != 0 || q != r));
  777. if (p == 0 || (q != r && p->freq > q->freq))
  778. {
  779. r->son[1] = q; r->freq += q->freq; ++q;
  780. }
  781. else
  782. {
  783. r->son[1] = p; r->freq += p->freq; p = p->son[0];
  784. }
  785. }
  786. /* evaluate codewords' length */
  787. i = -1; /* stack pointer */
  788. n = 0; /* current tree depth */
  789. p = r; /* current subtree root */
  790. for (;;)
  791. {
  792. while (p->son[1] != 0)
  793. {
  794. /* put right son into stack and set up its depth */
  795. (info->head[++i] = p->son[1])->bits = (uint16) (++n);
  796. (p = p->son[0])->bits = (uint16) n;
  797. }
  798. length[p->ch] = (uchar) n;
  799. if (i < 0) break; /* nothing's in stack */
  800. n = (p = info->head[i--])->bits;
  801. }
  802. p = first_sorted;
  803. #if DEBUG
  804. for (q = p; (r = q->son[0]) != 0; q = r)
  805. assert (q->bits >= r->bits);
  806. #endif
  807. if (p->bits > maxbits)
  808. {
  809. assert (p == first_sorted);
  810. q = p;
  811. do
  812. q->freq = (q->freq + 1) >> 1;
  813. while ((q = q->son[0]) != 0);
  814. goto restart;
  815. }
  816. /* now sort symbols in a stable way by increasing codeword length */
  817. /* initialize */
  818. memset (info->head, 0, sizeof (info->head[0]) * 32);
  819. for (n = 0; n < 32; ++n)
  820. info->link[n] = info->head + n;
  821. /* link node to the end of respective bucket */
  822. p = info->buff;
  823. do
  824. {
  825. n = p->bits;
  826. info->link[n][0] = p; info->link[n] = p->son;
  827. }
  828. while (++p != first_free);
  829. /* merge buckets into single list */
  830. n = 0;
  831. while (info->head[n] == 0) ++n;
  832. p = info->head[n]; k = n;
  833. while (++n < 32)
  834. {
  835. if (info->head[n] == 0) continue;
  836. info->link[k][0] = info->head[n];
  837. k = n;
  838. }
  839. info->link[k][0] = 0;
  840. #if DEBUG
  841. for (q = p; (r = q->son[0]) != 0; q = r)
  842. assert (r->bits > q->bits || (r->bits == q->bits && r->ch > q->ch));
  843. #endif
  844. /* set up code masks */
  845. if (mask == freq)
  846. memset (mask, 0, sizeof (mask[0]) * n);
  847. n = 0; /* mask */
  848. i = 1; /* bit length */
  849. k = 1; /* first index */
  850. do
  851. {
  852. /* sum a[i] * b[i] may be evaluated without multiplications */
  853. /* using O(B) memory and O(N+B) time if 0 <= b[i] < B */
  854. *total += freq[p->ch] * p->bits;
  855. encoded[p->ch >> 1] |= p->bits << (p->ch & 1 ? 4 : 0);
  856. mask[p->ch] = (n <<= p->bits - i);
  857. i = p->bits;
  858. ++n;
  859. }
  860. while ((p = p->son[0]) != 0);
  861. }
  862. #endif /* CODING */
  863. #define CHAIN 0
  864. #define encode_pass1 encode0_pass1
  865. #include "xencode.i"
  866. #if MAX_CHAIN >= 1
  867. #define CHAIN 1
  868. #define encode_pass1 encode1_pass1
  869. #define find_match find_match1
  870. #include "xencode.i"
  871. #endif
  872. #if MAX_CHAIN >= 2
  873. #define CHAIN 2
  874. #define encode_pass1 encode2_pass1
  875. #define find_match find_match2
  876. #include "xencode.i"
  877. #endif
  878. #if MAX_CHAIN >= 3
  879. #define CHAIN 3
  880. #define encode_pass1 encodeN_pass1
  881. #define find_match find_matchN
  882. #include "xencode.i"
  883. #endif
  884. typedef void encode_pass1_proc (prs *p);
  885. static void encode_pass1_progress (
  886. prs *p,
  887. encode_pass1_proc *encode_pass1,
  888. XpressProgressFn *ProgressFn, // NULL or progress callback
  889. void *ProgressContext, // user-defined context that will be passed to ProgressFn
  890. int ProgressSize // call ProgressFn each time ProgressSize bytes processed
  891. )
  892. {
  893. xint stop;
  894. if (ProgressFn == 0)
  895. {
  896. encode_pass1 (p);
  897. return;
  898. }
  899. stop = v.orig.stop;
  900. for (;;)
  901. {
  902. if (v.orig.pos - v.orig.progress >= ProgressSize)
  903. {
  904. #if CODING & (CODING_HUFF_LEN | CODING_HUFF_PTR | CODING_HUFF_ALL)
  905. ProgressFn (ProgressContext, v.orig.ptr);
  906. #else
  907. ProgressFn (ProgressContext, (v.orig.pos * 15) >> 4);
  908. #endif
  909. v.orig.progress = v.orig.pos;
  910. }
  911. v.orig.stop = stop;
  912. if (v.orig.pos >= stop)
  913. break;
  914. if (stop - v.orig.progress > ProgressSize)
  915. v.orig.stop = v.orig.progress + ProgressSize;
  916. assert (v.orig.stop > v.orig.pos);
  917. encode_pass1 (p);
  918. }
  919. }
  920. int XPRESS_CALL XpressEncode
  921. (
  922. XpressEncodeStream stream,
  923. void *comp,
  924. int comp_size,
  925. const void *orig,
  926. int orig_size,
  927. XpressProgressFn *ProgressFn, // NULL or progress callback
  928. void *ProgressContext, // user-defined context that will be passed to ProgressFn
  929. int ProgressSize // call ProgressFn each time ProgressSize bytes processed
  930. )
  931. {
  932. #if CODING & (CODING_HUFF_LEN | CODING_HUFF_PTR | CODING_HUFF_ALL)
  933. uchar huff_buff [HUFF_SIZE >> 1];
  934. uxint huff_total;
  935. #endif
  936. uxint c_size;
  937. prs *p;
  938. xpress_info *info = (xpress_info *) stream;
  939. encode_pass1_proc *encode_pass1;
  940. if (info == 0 || info->magic != MAGIC_ENCODE)
  941. return (0);
  942. if ((unsigned) (orig_size-1) > info->max_size
  943. || orig_size <= MIN_SIZE
  944. || comp_size < MIN_SIZE
  945. || comp == 0
  946. || orig == 0)
  947. {
  948. return (orig_size);
  949. }
  950. p = info->p;
  951. memset (&v, 0, sizeof (v));
  952. v.temp.hash = (z_index_t *) (v.temp.ptr = info->temp);
  953. v.chain = info->chain;
  954. v.orig.end = (v.orig.ptr = orig) + (v.orig.size = v.orig.stop = orig_size);
  955. v.orig.end_16 = v.orig.end - 16;
  956. v.orig.end_3 = v.orig.end - MIN_MATCH;
  957. v.comp.ptr = v.comp.beg = comp;
  958. v.orig.pos = 0;
  959. v.temp.beg = v.temp.ptr;
  960. encode_pass1 = encode0_pass1;
  961. if (v.chain <= 0)
  962. memset ((void *) (&p->x.q_last[0]), 0, Q_HASH_SIZE * sizeof (p->x.q_last[0]));
  963. #if MAX_CHAIN >= 1
  964. else if (v.chain == 1)
  965. {
  966. encode_pass1 = encode1_pass1;
  967. memset ((void *) (&p->x.z_next[0]), 0, Z_HASH_SIZE * sizeof (p->x.z_next[0]));
  968. }
  969. #if MAX_CHAIN >= 2
  970. else
  971. {
  972. encode_pass1 = encode2_pass1;
  973. #if MAX_CHAIN >= 3
  974. if (v.chain >= 3)
  975. encode_pass1 = encodeN_pass1;
  976. #endif
  977. memset (v.temp.hash, 0, sizeof (v.temp.hash[0]) * Z_HASH_SIZE);
  978. z_hash_insert (p);
  979. }
  980. #endif
  981. #endif
  982. if (ProgressSize <= 0 || ProgressSize > orig_size)
  983. ProgressSize = orig_size;
  984. if (ProgressFn != 0)
  985. ProgressFn (ProgressContext, v.orig.progress = 0);
  986. #if CODING & (CODING_DIRECT | CODING_DIRECT2 | CODING_BY_BIT)
  987. v.temp.beg = v.temp.ptr = v.comp.ptr;
  988. tag_write_start (p);
  989. for (;;)
  990. {
  991. xint rest = comp_size - (xint) (v.temp.ptr - v.comp.beg) - 2 * sizeof (tag_t);
  992. rest -= (rest + 7) >> 3;
  993. if (rest <= (xint) (2 * sizeof (tag_t) + 8))
  994. goto no_compression;
  995. if (v.orig.pos >= v.orig.size)
  996. break;
  997. v.orig.stop = v.orig.pos + rest;
  998. if (v.orig.stop > v.orig.size - 8)
  999. {
  1000. v.orig.stop = v.orig.size - 8;
  1001. if (v.orig.pos >= v.orig.stop)
  1002. break;
  1003. }
  1004. encode_pass1_progress (p, encode_pass1, ProgressFn, ProgressContext, ProgressSize);
  1005. }
  1006. #else
  1007. v.orig.stop -= 7;
  1008. tag_write_start (p);
  1009. encode_pass1_progress (p, encode_pass1, ProgressFn, ProgressContext, ProgressSize);
  1010. #endif
  1011. while (v.orig.pos < v.orig.size)
  1012. {
  1013. write_lit (p, v.temp.ptr, v.orig.ptr[v.orig.pos]);
  1014. ++v.orig.pos;
  1015. }
  1016. tag_write_finish (p);
  1017. #if CODING & (CODING_DIRECT | CODING_DIRECT2 | CODING_BY_BIT)
  1018. c_size = (xint) (v.temp.ptr - v.temp.beg);
  1019. #elif CODING & (CODING_HUFF_LEN | CODING_HUFF_PTR | CODING_HUFF_ALL)
  1020. if (v.stat.pointers == 0)
  1021. goto no_compression;
  1022. #if CODING == CODING_HUFF_ALL
  1023. ++v.stat.freq[CODING_ADJUST(0)];
  1024. #endif
  1025. huffman_create_codes (
  1026. &v.stat.info,
  1027. v.stat.freq, HUFF_SIZE,
  1028. v.stat.mask, v.stat.bits, 15, huff_buff, &huff_total);
  1029. c_size = huff_total;
  1030. #if CODING & (CODING_HUFF_PTR | CODING_HUFF_ALL)
  1031. c_size += v.stat.extra;
  1032. #endif
  1033. if (c_size == 0) c_size = 1;
  1034. c_size = (((c_size - 1) & ~(sizeof (bitmask2) * 8 - 1)) >> 3);
  1035. c_size += (int) (v.temp.ptr - v.temp.beg) - v.stat.pointers + 4 + sizeof (huff_buff);
  1036. #if CODING == CODING_HUFF_ALL
  1037. for (huff_total = 0; huff_total < 256; ++huff_total)
  1038. c_size -= v.stat.freq[huff_total];
  1039. c_size -= v.stat.masks * sizeof (tag_t);
  1040. #endif
  1041. #endif /* CODING */
  1042. if (c_size >= (uxint) comp_size)
  1043. {
  1044. no_compression:
  1045. comp_size = orig_size;
  1046. }
  1047. else
  1048. {
  1049. #if CODING & (CODING_HUFF_LEN | CODING_HUFF_PTR | CODING_HUFF_ALL)
  1050. memcpy (v.comp.ptr, huff_buff, sizeof (huff_buff));
  1051. v.comp.ptr += sizeof (huff_buff);
  1052. encode_pass2 (p);
  1053. #elif CODING & (CODING_BY_BIT | CODING_DIRECT | CODING_DIRECT2)
  1054. v.comp.ptr += c_size;
  1055. #else
  1056. #error Unknown CODING
  1057. #endif
  1058. comp_size = (int) (v.comp.ptr - v.comp.beg);
  1059. #if DEBUG
  1060. if (c_size != (uxint) comp_size)
  1061. printf ("error: c_size = %d, comp_size = %d\n", c_size, comp_size);
  1062. #endif
  1063. }
  1064. if (ProgressFn != 0)
  1065. ProgressFn (ProgressContext, orig_size);
  1066. return (comp_size);
  1067. }
  1068. #define ALIGN 32
  1069. XpressEncodeStream XPRESS_CALL XpressEncodeCreate
  1070. (int orig_size, void *context, XpressAllocFn *AllocFn, int chain)
  1071. {
  1072. xpress_info *info;
  1073. prs *p;
  1074. uchar *b;
  1075. int temp_size;
  1076. int alloc_size;
  1077. if (AllocFn == 0 || (unsigned) (orig_size-1) > (BUFF_SIZE-1))
  1078. return (0);
  1079. #if CODING & (CODING_DIRECT | CODING_DIRECT2 | CODING_BY_BIT)
  1080. temp_size = 0;
  1081. #else
  1082. temp_size = orig_size + ((orig_size + 7) >> 3);
  1083. #endif
  1084. alloc_size = sizeof (p->x.q_last[0]) * Q_HASH_SIZE;
  1085. #if MAX_CHAIN <= 0
  1086. chain = 0;
  1087. #else
  1088. if (chain > MAX_CHAIN)
  1089. chain = MAX_CHAIN;
  1090. if (chain >= 1)
  1091. {
  1092. alloc_size = sizeof (p->x.z_next[0]) * Z_HASH_SIZE;
  1093. #if MAX_CHAIN >= 2
  1094. if (chain >= 2)
  1095. {
  1096. alloc_size = sizeof (p->x.z_next[0]) * orig_size;
  1097. if (temp_size < sizeof (v.temp.hash[0]) * Z_HASH_SIZE)
  1098. temp_size = sizeof (v.temp.hash[0]) * Z_HASH_SIZE;
  1099. }
  1100. #endif
  1101. }
  1102. #endif
  1103. temp_size = (temp_size + 256 + sizeof (*p)) & ~(ALIGN-1);
  1104. alloc_size += temp_size + sizeof (*info) + ALIGN;
  1105. b = AllocFn (context, alloc_size);
  1106. if (b == 0)
  1107. return (0);
  1108. info = (xpress_info *) b;
  1109. info->max_size = orig_size - 1;
  1110. info->chain = chain;
  1111. info->magic = MAGIC_ENCODE;
  1112. info->memory = b;
  1113. b = (uchar *) (info + 1);
  1114. b += ALIGN - (((__int64) b) & (ALIGN-1));
  1115. info->p = p = ((prs *) (b + temp_size));
  1116. info->temp = b;
  1117. #if MAX_CHAIN >= 1
  1118. z_hash_map_init ();
  1119. #endif
  1120. #if CODING & (CODING_HUFF_PTR | CODING_HUFF_ALL)
  1121. bitno_init ();
  1122. #endif
  1123. return ((XpressEncodeStream) info);
  1124. }
  1125. void XPRESS_CALL XpressEncodeClose
  1126. (XpressEncodeStream stream, void *context, XpressFreeFn *FreeFn)
  1127. {
  1128. xpress_info *info = (xpress_info *) stream;
  1129. if (info != 0 && FreeFn != 0 && info->magic == MAGIC_ENCODE)
  1130. {
  1131. info->magic = 0;
  1132. FreeFn (context, info->memory);
  1133. }
  1134. }