Debian Codegraph @

Source/bzip2/huffman.c

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