huffman.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * bzip2 is written by Julian Seward <jseward@bzip.org>.
  3. * Adapted for busybox by Denys Vlasenko <vda.linux@googlemail.com>.
  4. * See README and LICENSE files in this directory for more information.
  5. */
  6. /*-------------------------------------------------------------*/
  7. /*--- Huffman coding low-level stuff ---*/
  8. /*--- huffman.c ---*/
  9. /*-------------------------------------------------------------*/
  10. /* ------------------------------------------------------------------
  11. This file is part of bzip2/libbzip2, a program and library for
  12. lossless, block-sorting data compression.
  13. bzip2/libbzip2 version 1.0.4 of 20 December 2006
  14. Copyright (C) 1996-2006 Julian Seward <jseward@bzip.org>
  15. Please read the WARNING, DISCLAIMER and PATENTS sections in the
  16. README file.
  17. This program is released under the terms of the license contained
  18. in the file LICENSE.
  19. ------------------------------------------------------------------ */
  20. /* #include "bzlib_private.h" */
  21. /*---------------------------------------------------*/
  22. #define WEIGHTOF(zz0) ((zz0) & 0xffffff00)
  23. #define DEPTHOF(zz1) ((zz1) & 0x000000ff)
  24. #define MYMAX(zz2,zz3) ((zz2) > (zz3) ? (zz2) : (zz3))
  25. #define ADDWEIGHTS(zw1,zw2) \
  26. (WEIGHTOF(zw1)+WEIGHTOF(zw2)) | \
  27. (1 + MYMAX(DEPTHOF(zw1),DEPTHOF(zw2)))
  28. #define UPHEAP(z) \
  29. { \
  30. int32_t zz, tmp; \
  31. zz = z; \
  32. tmp = heap[zz]; \
  33. while (weight[tmp] < weight[heap[zz >> 1]]) { \
  34. heap[zz] = heap[zz >> 1]; \
  35. zz >>= 1; \
  36. } \
  37. heap[zz] = tmp; \
  38. }
  39. /* 90 bytes, 0.3% of overall compress speed */
  40. #if CONFIG_BZIP2_FEATURE_SPEED >= 1
  41. /* macro works better than inline (gcc 4.2.1) */
  42. #define DOWNHEAP1(heap, weight, Heap) \
  43. { \
  44. int32_t zz, yy, tmp; \
  45. zz = 1; \
  46. tmp = heap[zz]; \
  47. while (1) { \
  48. yy = zz << 1; \
  49. if (yy > nHeap) \
  50. break; \
  51. if (yy < nHeap \
  52. && weight[heap[yy+1]] < weight[heap[yy]]) \
  53. yy++; \
  54. if (weight[tmp] < weight[heap[yy]]) \
  55. break; \
  56. heap[zz] = heap[yy]; \
  57. zz = yy; \
  58. } \
  59. heap[zz] = tmp; \
  60. }
  61. #else
  62. static
  63. void DOWNHEAP1(int32_t *heap, int32_t *weight, int32_t nHeap)
  64. {
  65. int32_t zz, yy, tmp;
  66. zz = 1;
  67. tmp = heap[zz];
  68. while (1) {
  69. yy = zz << 1;
  70. if (yy > nHeap)
  71. break;
  72. if (yy < nHeap
  73. && weight[heap[yy + 1]] < weight[heap[yy]])
  74. yy++;
  75. if (weight[tmp] < weight[heap[yy]])
  76. break;
  77. heap[zz] = heap[yy];
  78. zz = yy;
  79. }
  80. heap[zz] = tmp;
  81. }
  82. #endif
  83. /*---------------------------------------------------*/
  84. static
  85. void BZ2_hbMakeCodeLengths(EState *s,
  86. uint8_t *len,
  87. int32_t *freq,
  88. int32_t alphaSize,
  89. int32_t maxLen)
  90. {
  91. /*
  92. * Nodes and heap entries run from 1. Entry 0
  93. * for both the heap and nodes is a sentinel.
  94. */
  95. int32_t nNodes, nHeap, n1, n2, i, j, k;
  96. Bool tooLong;
  97. /* bbox: moved to EState to save stack
  98. int32_t heap [BZ_MAX_ALPHA_SIZE + 2];
  99. int32_t weight[BZ_MAX_ALPHA_SIZE * 2];
  100. int32_t parent[BZ_MAX_ALPHA_SIZE * 2];
  101. */
  102. #define heap (s->BZ2_hbMakeCodeLengths__heap)
  103. #define weight (s->BZ2_hbMakeCodeLengths__weight)
  104. #define parent (s->BZ2_hbMakeCodeLengths__parent)
  105. for (i = 0; i < alphaSize; i++)
  106. weight[i+1] = (freq[i] == 0 ? 1 : freq[i]) << 8;
  107. while (1) {
  108. nNodes = alphaSize;
  109. nHeap = 0;
  110. heap[0] = 0;
  111. weight[0] = 0;
  112. parent[0] = -2;
  113. for (i = 1; i <= alphaSize; i++) {
  114. parent[i] = -1;
  115. nHeap++;
  116. heap[nHeap] = i;
  117. UPHEAP(nHeap);
  118. }
  119. AssertH(nHeap < (BZ_MAX_ALPHA_SIZE+2), 2001);
  120. while (nHeap > 1) {
  121. n1 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP1(heap, weight, nHeap);
  122. n2 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP1(heap, weight, nHeap);
  123. nNodes++;
  124. parent[n1] = parent[n2] = nNodes;
  125. weight[nNodes] = ADDWEIGHTS(weight[n1], weight[n2]);
  126. parent[nNodes] = -1;
  127. nHeap++;
  128. heap[nHeap] = nNodes;
  129. UPHEAP(nHeap);
  130. }
  131. AssertH(nNodes < (BZ_MAX_ALPHA_SIZE * 2), 2002);
  132. tooLong = False;
  133. for (i = 1; i <= alphaSize; i++) {
  134. j = 0;
  135. k = i;
  136. while (parent[k] >= 0) {
  137. k = parent[k];
  138. j++;
  139. }
  140. len[i-1] = j;
  141. if (j > maxLen)
  142. tooLong = True;
  143. }
  144. if (!tooLong)
  145. break;
  146. /* 17 Oct 04: keep-going condition for the following loop used
  147. to be 'i < alphaSize', which missed the last element,
  148. theoretically leading to the possibility of the compressor
  149. looping. However, this count-scaling step is only needed if
  150. one of the generated Huffman code words is longer than
  151. maxLen, which up to and including version 1.0.2 was 20 bits,
  152. which is extremely unlikely. In version 1.0.3 maxLen was
  153. changed to 17 bits, which has minimal effect on compression
  154. ratio, but does mean this scaling step is used from time to
  155. time, enough to verify that it works.
  156. This means that bzip2-1.0.3 and later will only produce
  157. Huffman codes with a maximum length of 17 bits. However, in
  158. order to preserve backwards compatibility with bitstreams
  159. produced by versions pre-1.0.3, the decompressor must still
  160. handle lengths of up to 20. */
  161. for (i = 1; i <= alphaSize; i++) {
  162. j = weight[i] >> 8;
  163. /* bbox: yes, it is a signed division.
  164. * don't replace with shift! */
  165. j = 1 + (j / 2);
  166. weight[i] = j << 8;
  167. }
  168. }
  169. #undef heap
  170. #undef weight
  171. #undef parent
  172. }
  173. /*---------------------------------------------------*/
  174. static
  175. void BZ2_hbAssignCodes(int32_t *code,
  176. uint8_t *length,
  177. int32_t minLen,
  178. int32_t maxLen,
  179. int32_t alphaSize)
  180. {
  181. int32_t n, vec, i;
  182. vec = 0;
  183. for (n = minLen; n <= maxLen; n++) {
  184. for (i = 0; i < alphaSize; i++) {
  185. if (length[i] == n) {
  186. code[i] = vec;
  187. vec++;
  188. };
  189. }
  190. vec <<= 1;
  191. }
  192. }
  193. /*-------------------------------------------------------------*/
  194. /*--- end huffman.c ---*/
  195. /*-------------------------------------------------------------*/