2 /*-------------------------------------------------------------*/
3 /*--- Huffman coding low-level stuff ---*/
5 /*-------------------------------------------------------------*/
8 This file is a part of bzip2 and/or libbzip2, a program and
9 library for lossless, block-sorting data compression.
11 Copyright (C) 1996-2002 Julian R Seward. All rights reserved.
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions
17 1. Redistributions of source code must retain the above copyright
18 notice, this list of conditions and the following disclaimer.
20 2. The origin of this software must not be misrepresented; you must
21 not claim that you wrote the original software. If you use this
22 software in a product, an acknowledgment in the product
23 documentation would be appreciated but is not required.
25 3. Altered source versions must be plainly marked as such, and must
26 not be misrepresented as being the original software.
28 4. The name of the author may not be used to endorse or promote
29 products derived from this software without specific prior written
32 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
33 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
34 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
36 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
38 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
40 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
41 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
42 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 Julian Seward, Cambridge, UK.
46 bzip2/libbzip2 version 1.0 of 21 March 2000
48 This program is based on (at least) the work of:
58 For more information on these sources, see the manual.
62 #include "bzlib_private.h"
64 /*---------------------------------------------------*/
65 #define WEIGHTOF(zz0) ((zz0) & 0xffffff00)
66 #define DEPTHOF(zz1) ((zz1) & 0x000000ff)
67 #define MYMAX(zz2,zz3) ((zz2) > (zz3) ? (zz2) : (zz3))
69 #define ADDWEIGHTS(zw1,zw2) \
70 (WEIGHTOF(zw1)+WEIGHTOF(zw2)) | \
71 (1 + MYMAX(DEPTHOF(zw1),DEPTHOF(zw2)))
76 zz = z; tmp = heap[zz]; \
77 while (weight[tmp] < weight[heap[zz >> 1]]) { \
78 heap[zz] = heap[zz >> 1]; \
87 zz = z; tmp = heap[zz]; \
90 if (yy > nHeap) break; \
92 weight[heap[yy+1]] < weight[heap[yy]]) \
94 if (weight[tmp] < weight[heap[yy]]) break; \
95 heap[zz] = heap[yy]; \
102 /*---------------------------------------------------*/
103 void BZ2_hbMakeCodeLengths ( UChar
*len
,
109 Nodes and heap entries run from 1. Entry 0
110 for both the heap and nodes is a sentinel.
112 Int32 nNodes
, nHeap
, n1
, n2
, i
, j
, k
;
115 Int32 heap
[ BZ_MAX_ALPHA_SIZE
+ 2 ];
116 Int32 weight
[ BZ_MAX_ALPHA_SIZE
* 2 ];
117 Int32 parent
[ BZ_MAX_ALPHA_SIZE
* 2 ];
119 for (i
= 0; i
< alphaSize
; i
++)
120 weight
[i
+1] = (freq
[i
] == 0 ? 1 : freq
[i
]) << 8;
131 for (i
= 1; i
<= alphaSize
; i
++) {
138 AssertH( nHeap
< (BZ_MAX_ALPHA_SIZE
+2), 2001 );
141 n1
= heap
[1]; heap
[1] = heap
[nHeap
]; nHeap
--; DOWNHEAP(1);
142 n2
= heap
[1]; heap
[1] = heap
[nHeap
]; nHeap
--; DOWNHEAP(1);
144 parent
[n1
] = parent
[n2
] = nNodes
;
145 weight
[nNodes
] = ADDWEIGHTS(weight
[n1
], weight
[n2
]);
148 heap
[nHeap
] = nNodes
;
152 AssertH( nNodes
< (BZ_MAX_ALPHA_SIZE
* 2), 2002 );
155 for (i
= 1; i
<= alphaSize
; i
++) {
158 while (parent
[k
] >= 0) { k
= parent
[k
]; j
++; }
160 if (j
> maxLen
) tooLong
= True
;
163 if (! tooLong
) break;
165 for (i
= 1; i
< alphaSize
; i
++) {
174 /*---------------------------------------------------*/
175 void BZ2_hbAssignCodes ( Int32
*code
,
184 for (n
= minLen
; n
<= maxLen
; n
++) {
185 for (i
= 0; i
< alphaSize
; i
++)
186 if (length
[i
] == n
) { code
[i
] = vec
; vec
++; };
192 /*---------------------------------------------------*/
193 void BZ2_hbCreateDecodeTables ( Int32
*limit
,
204 for (i
= minLen
; i
<= maxLen
; i
++)
205 for (j
= 0; j
< alphaSize
; j
++)
206 if (length
[j
] == i
) { perm
[pp
] = j
; pp
++; };
208 for (i
= 0; i
< BZ_MAX_CODE_LEN
; i
++) base
[i
] = 0;
209 for (i
= 0; i
< alphaSize
; i
++) base
[length
[i
]+1]++;
211 for (i
= 1; i
< BZ_MAX_CODE_LEN
; i
++) base
[i
] += base
[i
-1];
213 for (i
= 0; i
< BZ_MAX_CODE_LEN
; i
++) limit
[i
] = 0;
216 for (i
= minLen
; i
<= maxLen
; i
++) {
217 vec
+= (base
[i
+1] - base
[i
]);
221 for (i
= minLen
+ 1; i
<= maxLen
; i
++)
222 base
[i
] = ((limit
[i
-1] + 1) << 1) - base
[i
];
226 /*-------------------------------------------------------------*/
227 /*--- end huffman.c ---*/
228 /*-------------------------------------------------------------*/