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" */
28 /*---------------------------------------------------*/
29 #define WEIGHTOF(zz0) ((zz0) & 0xffffff00)
30 #define DEPTHOF(zz1) ((zz1) & 0x000000ff)
31 #define MYMAX(zz2,zz3) ((zz2) > (zz3) ? (zz2) : (zz3))
33 #define ADDWEIGHTS(zw1,zw2) \
34 (WEIGHTOF(zw1)+WEIGHTOF(zw2)) | \
35 (1 + MYMAX(DEPTHOF(zw1),DEPTHOF(zw2)))
42 while (weight[tmp] < weight[heap[zz >> 1]]) { \
43 heap[zz] = heap[zz >> 1]; \
50 /* 90 bytes, 0.3% of overall compress speed */
51 #if CONFIG_BZIP2_FEATURE_SPEED >= 1
53 /* macro works better than inline (gcc 4.2.1) */
54 #define DOWNHEAP1(heap, weight, Heap) \
56 int32_t zz, yy, tmp; \
64 && weight[heap[yy+1]] < weight[heap[yy]]) \
66 if (weight[tmp] < weight[heap[yy]]) \
68 heap[zz] = heap[yy]; \
77 void DOWNHEAP1(int32_t *heap
, int32_t *weight
, int32_t nHeap
)
87 && weight
[heap
[yy
+ 1]] < weight
[heap
[yy
]])
89 if (weight
[tmp
] < weight
[heap
[yy
]])
99 /*---------------------------------------------------*/
101 void BZ2_hbMakeCodeLengths(EState
*s
,
108 * Nodes and heap entries run from 1. Entry 0
109 * for both the heap and nodes is a sentinel.
111 int32_t nNodes
, nHeap
, n1
, n2
, i
, j
, k
;
114 /* bbox: moved to EState to save stack
115 int32_t heap [BZ_MAX_ALPHA_SIZE + 2];
116 int32_t weight[BZ_MAX_ALPHA_SIZE * 2];
117 int32_t parent[BZ_MAX_ALPHA_SIZE * 2];
119 #define heap (s->BZ2_hbMakeCodeLengths__heap)
120 #define weight (s->BZ2_hbMakeCodeLengths__weight)
121 #define parent (s->BZ2_hbMakeCodeLengths__parent)
123 for (i
= 0; i
< alphaSize
; i
++)
124 weight
[i
+1] = (freq
[i
] == 0 ? 1 : freq
[i
]) << 8;
134 for (i
= 1; i
<= alphaSize
; i
++) {
141 AssertH(nHeap
< (BZ_MAX_ALPHA_SIZE
+2), 2001);
144 n1
= heap
[1]; heap
[1] = heap
[nHeap
]; nHeap
--; DOWNHEAP1(heap
, weight
, nHeap
);
145 n2
= heap
[1]; heap
[1] = heap
[nHeap
]; nHeap
--; DOWNHEAP1(heap
, weight
, nHeap
);
147 parent
[n1
] = parent
[n2
] = nNodes
;
148 weight
[nNodes
] = ADDWEIGHTS(weight
[n1
], weight
[n2
]);
151 heap
[nHeap
] = nNodes
;
155 AssertH(nNodes
< (BZ_MAX_ALPHA_SIZE
* 2), 2002);
158 for (i
= 1; i
<= alphaSize
; i
++) {
161 while (parent
[k
] >= 0) {
173 /* 17 Oct 04: keep-going condition for the following loop used
174 to be 'i < alphaSize', which missed the last element,
175 theoretically leading to the possibility of the compressor
176 looping. However, this count-scaling step is only needed if
177 one of the generated Huffman code words is longer than
178 maxLen, which up to and including version 1.0.2 was 20 bits,
179 which is extremely unlikely. In version 1.0.3 maxLen was
180 changed to 17 bits, which has minimal effect on compression
181 ratio, but does mean this scaling step is used from time to
182 time, enough to verify that it works.
184 This means that bzip2-1.0.3 and later will only produce
185 Huffman codes with a maximum length of 17 bits. However, in
186 order to preserve backwards compatibility with bitstreams
187 produced by versions pre-1.0.3, the decompressor must still
188 handle lengths of up to 20. */
190 for (i
= 1; i
<= alphaSize
; i
++) {
192 /* bbox: yes, it is a signed division.
193 * don't replace with shift! */
204 /*---------------------------------------------------*/
206 void BZ2_hbAssignCodes(int32_t *code
,
215 for (n
= minLen
; n
<= maxLen
; n
++) {
216 for (i
= 0; i
< alphaSize
; i
++) {
217 if (length
[i
] == n
) {
227 /*-------------------------------------------------------------*/
228 /*--- end huffman.c ---*/
229 /*-------------------------------------------------------------*/