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.

1297 lines
29 KiB

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