4
4
/*--- huffman.c ---*/
5
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-2000 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.
7
/* ------------------------------------------------------------------
8
This file is part of bzip2/libbzip2, a program and library for
9
lossless, block-sorting data compression.
11
bzip2/libbzip2 version 1.0.5 of 10 December 2007
12
Copyright (C) 1996-2007 Julian Seward <jseward@bzip.org>
14
Please read the WARNING, DISCLAIMER and PATENTS sections in the
17
This program is released under the terms of the license contained
19
------------------------------------------------------------------ */
62
22
#include "bzlib_private.h"
163
123
if (! tooLong) break;
165
for (i = 1; i < alphaSize; i++) {
125
/* 17 Oct 04: keep-going condition for the following loop used
126
to be 'i < alphaSize', which missed the last element,
127
theoretically leading to the possibility of the compressor
128
looping. However, this count-scaling step is only needed if
129
one of the generated Huffman code words is longer than
130
maxLen, which up to and including version 1.0.2 was 20 bits,
131
which is extremely unlikely. In version 1.0.3 maxLen was
132
changed to 17 bits, which has minimal effect on compression
133
ratio, but does mean this scaling step is used from time to
134
time, enough to verify that it works.
136
This means that bzip2-1.0.3 and later will only produce
137
Huffman codes with a maximum length of 17 bits. However, in
138
order to preserve backwards compatibility with bitstreams
139
produced by versions pre-1.0.3, the decompressor must still
140
handle lengths of up to 20. */
142
for (i = 1; i <= alphaSize; i++) {
166
143
j = weight[i] >> 8;
168
145
weight[i] = j << 8;