blob: 904783d7cb1887e843074859cfce4a892699be55 [file] [log] [blame]
wdenk710e3502003-08-29 20:57:53 +00001#include <config.h>
wdenk710e3502003-08-29 20:57:53 +00002
3/*-------------------------------------------------------------*/
4/*--- Huffman coding low-level stuff ---*/
5/*--- huffman.c ---*/
6/*-------------------------------------------------------------*/
7
8/*--
9 This file is a part of bzip2 and/or libbzip2, a program and
10 library for lossless, block-sorting data compression.
11
12 Copyright (C) 1996-2002 Julian R Seward. All rights reserved.
13
14 Redistribution and use in source and binary forms, with or without
15 modification, are permitted provided that the following conditions
16 are met:
17
18 1. Redistributions of source code must retain the above copyright
19 notice, this list of conditions and the following disclaimer.
20
21 2. The origin of this software must not be misrepresented; you must
22 not claim that you wrote the original software. If you use this
23 software in a product, an acknowledgment in the product
24 documentation would be appreciated but is not required.
25
26 3. Altered source versions must be plainly marked as such, and must
27 not be misrepresented as being the original software.
28
29 4. The name of the author may not be used to endorse or promote
30 products derived from this software without specific prior written
31 permission.
32
33 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
34 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
37 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
39 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
40 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
41 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
42 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
43 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44
45 Julian Seward, Cambridge, UK.
46 jseward@acm.org
47 bzip2/libbzip2 version 1.0 of 21 March 2000
48
49 This program is based on (at least) the work of:
50 Mike Burrows
51 David Wheeler
52 Peter Fenwick
53 Alistair Moffat
54 Radford Neal
55 Ian H. Witten
56 Robert Sedgewick
57 Jon L. Bentley
58
59 For more information on these sources, see the manual.
60--*/
61
wdenk710e3502003-08-29 20:57:53 +000062#include "bzlib_private.h"
63
64/*---------------------------------------------------*/
65#define WEIGHTOF(zz0) ((zz0) & 0xffffff00)
66#define DEPTHOF(zz1) ((zz1) & 0x000000ff)
67#define MYMAX(zz2,zz3) ((zz2) > (zz3) ? (zz2) : (zz3))
68
69#define ADDWEIGHTS(zw1,zw2) \
70 (WEIGHTOF(zw1)+WEIGHTOF(zw2)) | \
71 (1 + MYMAX(DEPTHOF(zw1),DEPTHOF(zw2)))
72
73#define UPHEAP(z) \
74{ \
75 Int32 zz, tmp; \
76 zz = z; tmp = heap[zz]; \
77 while (weight[tmp] < weight[heap[zz >> 1]]) { \
78 heap[zz] = heap[zz >> 1]; \
79 zz >>= 1; \
80 } \
81 heap[zz] = tmp; \
82}
83
84#define DOWNHEAP(z) \
85{ \
86 Int32 zz, yy, tmp; \
87 zz = z; tmp = heap[zz]; \
88 while (True) { \
89 yy = zz << 1; \
90 if (yy > nHeap) break; \
91 if (yy < nHeap && \
wdenk9c53f402003-10-15 23:53:47 +000092 weight[heap[yy+1]] < weight[heap[yy]]) \
93 yy++; \
wdenk710e3502003-08-29 20:57:53 +000094 if (weight[tmp] < weight[heap[yy]]) break; \
95 heap[zz] = heap[yy]; \
96 zz = yy; \
97 } \
98 heap[zz] = tmp; \
99}
100
wdenk710e3502003-08-29 20:57:53 +0000101/*---------------------------------------------------*/
102void BZ2_hbMakeCodeLengths ( UChar *len,
wdenk9c53f402003-10-15 23:53:47 +0000103 Int32 *freq,
104 Int32 alphaSize,
105 Int32 maxLen )
wdenk710e3502003-08-29 20:57:53 +0000106{
107 /*--
108 Nodes and heap entries run from 1. Entry 0
109 for both the heap and nodes is a sentinel.
110 --*/
111 Int32 nNodes, nHeap, n1, n2, i, j, k;
112 Bool tooLong;
113
114 Int32 heap [ BZ_MAX_ALPHA_SIZE + 2 ];
115 Int32 weight [ BZ_MAX_ALPHA_SIZE * 2 ];
116 Int32 parent [ BZ_MAX_ALPHA_SIZE * 2 ];
117
118 for (i = 0; i < alphaSize; i++)
119 weight[i+1] = (freq[i] == 0 ? 1 : freq[i]) << 8;
120
121 while (True) {
122
123 nNodes = alphaSize;
124 nHeap = 0;
125
126 heap[0] = 0;
127 weight[0] = 0;
128 parent[0] = -2;
129
130 for (i = 1; i <= alphaSize; i++) {
wdenk9c53f402003-10-15 23:53:47 +0000131 parent[i] = -1;
132 nHeap++;
133 heap[nHeap] = i;
134 UPHEAP(nHeap);
wdenk710e3502003-08-29 20:57:53 +0000135 }
136
137 AssertH( nHeap < (BZ_MAX_ALPHA_SIZE+2), 2001 );
138
139 while (nHeap > 1) {
wdenk9c53f402003-10-15 23:53:47 +0000140 n1 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1);
141 n2 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1);
142 nNodes++;
143 parent[n1] = parent[n2] = nNodes;
144 weight[nNodes] = ADDWEIGHTS(weight[n1], weight[n2]);
145 parent[nNodes] = -1;
146 nHeap++;
147 heap[nHeap] = nNodes;
148 UPHEAP(nHeap);
wdenk710e3502003-08-29 20:57:53 +0000149 }
150
151 AssertH( nNodes < (BZ_MAX_ALPHA_SIZE * 2), 2002 );
152
153 tooLong = False;
154 for (i = 1; i <= alphaSize; i++) {
wdenk9c53f402003-10-15 23:53:47 +0000155 j = 0;
156 k = i;
157 while (parent[k] >= 0) { k = parent[k]; j++; }
158 len[i-1] = j;
159 if (j > maxLen) tooLong = True;
wdenk710e3502003-08-29 20:57:53 +0000160 }
161
162 if (! tooLong) break;
163
164 for (i = 1; i < alphaSize; i++) {
wdenk9c53f402003-10-15 23:53:47 +0000165 j = weight[i] >> 8;
166 j = 1 + (j / 2);
167 weight[i] = j << 8;
wdenk710e3502003-08-29 20:57:53 +0000168 }
169 }
170}
171
wdenk710e3502003-08-29 20:57:53 +0000172/*---------------------------------------------------*/
173void BZ2_hbAssignCodes ( Int32 *code,
wdenk9c53f402003-10-15 23:53:47 +0000174 UChar *length,
175 Int32 minLen,
176 Int32 maxLen,
177 Int32 alphaSize )
wdenk710e3502003-08-29 20:57:53 +0000178{
179 Int32 n, vec, i;
180
181 vec = 0;
182 for (n = minLen; n <= maxLen; n++) {
183 for (i = 0; i < alphaSize; i++)
wdenk9c53f402003-10-15 23:53:47 +0000184 if (length[i] == n) { code[i] = vec; vec++; };
wdenk710e3502003-08-29 20:57:53 +0000185 vec <<= 1;
186 }
187}
188
wdenk710e3502003-08-29 20:57:53 +0000189/*---------------------------------------------------*/
190void BZ2_hbCreateDecodeTables ( Int32 *limit,
wdenk9c53f402003-10-15 23:53:47 +0000191 Int32 *base,
192 Int32 *perm,
193 UChar *length,
194 Int32 minLen,
195 Int32 maxLen,
196 Int32 alphaSize )
wdenk710e3502003-08-29 20:57:53 +0000197{
198 Int32 pp, i, j, vec;
199
200 pp = 0;
201 for (i = minLen; i <= maxLen; i++)
202 for (j = 0; j < alphaSize; j++)
wdenk9c53f402003-10-15 23:53:47 +0000203 if (length[j] == i) { perm[pp] = j; pp++; };
wdenk710e3502003-08-29 20:57:53 +0000204
205 for (i = 0; i < BZ_MAX_CODE_LEN; i++) base[i] = 0;
206 for (i = 0; i < alphaSize; i++) base[length[i]+1]++;
207
208 for (i = 1; i < BZ_MAX_CODE_LEN; i++) base[i] += base[i-1];
209
210 for (i = 0; i < BZ_MAX_CODE_LEN; i++) limit[i] = 0;
211 vec = 0;
212
213 for (i = minLen; i <= maxLen; i++) {
214 vec += (base[i+1] - base[i]);
215 limit[i] = vec-1;
216 vec <<= 1;
217 }
218 for (i = minLen + 1; i <= maxLen; i++)
219 base[i] = ((limit[i-1] + 1) << 1) - base[i];
220}
221
wdenk710e3502003-08-29 20:57:53 +0000222/*-------------------------------------------------------------*/
223/*--- end huffman.c ---*/
224/*-------------------------------------------------------------*/