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.
7 /*-------------------------------------------------------------*/
8 /*--- Huffman coding low-level stuff ---*/
10 /*-------------------------------------------------------------*/
12 /* ------------------------------------------------------------------
13 This file is part of bzip2/libbzip2, a program and library for
14 lossless, block-sorting data compression.
16 bzip2/libbzip2 version 1.0.4 of 20 December 2006
17 Copyright (C) 1996-2006 Julian Seward <jseward@bzip.org>
19 Please read the WARNING, DISCLAIMER and PATENTS sections in the
22 This program is released under the terms of the license contained
24 ------------------------------------------------------------------ */
26 /* #include "bzlib_private.h" */
29 /*---------------------------------------------------*/
30 #define WEIGHTOF(zz0) ((zz0) & 0xffffff00)
31 #define DEPTHOF(zz1) ((zz1) & 0x000000ff)
32 #define MYMAX(zz2,zz3) ((zz2) > (zz3) ? (zz2) : (zz3))
34 #define ADDWEIGHTS(zw1,zw2) \
35 (WEIGHTOF(zw1)+WEIGHTOF(zw2)) | \
36 (1 + MYMAX(DEPTHOF(zw1),DEPTHOF(zw2)))
43 while (weight[tmp] < weight[heap[zz >> 1]]) { \
44 heap[zz] = heap[zz >> 1]; \
51 /* 90 bytes, 0.3% of overall compress speed */
54 /* macro works better than inline (gcc 4.2.1) */
55 #define BZ_DOWNHEAP1(heap, weight, Heap) \
57 int32_t zz, yy, tmp; \
65 && weight[heap[yy+1]] < weight[heap[yy]]) \
67 if (weight[tmp] < weight[heap[yy]]) \
69 heap[zz] = heap[yy]; \
78 void BB_DOWNHEAP1(int32_t *heap
, int32_t *weight
, int32_t nHeap
)
88 && weight
[heap
[yy
+ 1]] < weight
[heap
[yy
]])
90 if (weight
[tmp
] < weight
[heap
[yy
]])
100 /*---------------------------------------------------*/
102 void BZ2_hbMakeCodeLengths(struct bz_EState
*s
,
109 * Nodes and heap entries run from 1. Entry 0
110 * for both the heap and nodes is a sentinel.
112 int32_t nNodes
, nHeap
, n1
, n2
, i
, j
, k
;
115 /* bbox: moved to EState to save stack
116 int32_t heap [BZ_MAX_ALPHA_SIZE + 2];
117 int32_t weight[BZ_MAX_ALPHA_SIZE * 2];
118 int32_t parent[BZ_MAX_ALPHA_SIZE * 2];
120 #define heap (s->BZ2_hbMakeCodeLengths__heap)
121 #define weight (s->BZ2_hbMakeCodeLengths__weight)
122 #define parent (s->BZ2_hbMakeCodeLengths__parent)
124 for (i
= 0; i
< alphaSize
; i
++)
125 weight
[i
+1] = (freq
[i
] == 0 ? 1 : freq
[i
]) << 8;
135 for (i
= 1; i
<= alphaSize
; i
++) {
142 bz_AssertH(nHeap
< (BZ_MAX_ALPHA_SIZE
+2), 2001);
145 n1
= heap
[1]; heap
[1] = heap
[nHeap
]; nHeap
--; BZ_DOWNHEAP1(heap
, weight
, nHeap
);
146 n2
= heap
[1]; heap
[1] = heap
[nHeap
]; nHeap
--; BZ_DOWNHEAP1(heap
, weight
, nHeap
);
148 parent
[n1
] = parent
[n2
] = nNodes
;
149 weight
[nNodes
] = ADDWEIGHTS(weight
[n1
], weight
[n2
]);
152 heap
[nHeap
] = nNodes
;
156 bz_AssertH(nNodes
< (BZ_MAX_ALPHA_SIZE
* 2), 2002);
159 for (i
= 1; i
<= alphaSize
; i
++) {
162 while (parent
[k
] >= 0) {
174 /* 17 Oct 04: keep-going condition for the following loop used
175 to be 'i < alphaSize', which missed the last element,
176 theoretically leading to the possibility of the compressor
177 looping. However, this count-scaling step is only needed if
178 one of the generated Huffman code words is longer than
179 maxLen, which up to and including version 1.0.2 was 20 bits,
180 which is extremely unlikely. In version 1.0.3 maxLen was
181 changed to 17 bits, which has minimal effect on compression
182 ratio, but does mean this scaling step is used from time to
183 time, enough to verify that it works.
185 This means that bzip2-1.0.3 and later will only produce
186 Huffman codes with a maximum length of 17 bits. However, in
187 order to preserve backwards compatibility with bitstreams
188 produced by versions pre-1.0.3, the decompressor must still
189 handle lengths of up to 20. */
191 for (i
= 1; i
<= alphaSize
; i
++) {
193 /* bbox: yes, it is a signed division.
194 * don't replace with shift! */
205 /*---------------------------------------------------*/
207 void BZ2_hbAssignCodes(int32_t *code
,
216 for (n
= minLen
; n
<= maxLen
; n
++) {
217 for (i
= 0; i
< alphaSize
; i
++) {
218 if (length
[i
] == n
) {
238 /*-------------------------------------------------------------*/
239 /*--- end huffman.c ---*/
240 /*-------------------------------------------------------------*/