Coverage Report

Created: 2025-07-23 08:18

/src/bzip2/huffman.c
Line
Count
Source (jump to first uncovered line)
1
2
/*-------------------------------------------------------------*/
3
/*--- Huffman coding low-level stuff                        ---*/
4
/*---                                             huffman.c ---*/
5
/*-------------------------------------------------------------*/
6
7
/* ------------------------------------------------------------------
8
   This file is part of bzip2/libbzip2, a program and library for
9
   lossless, block-sorting data compression.
10
11
   bzip2/libbzip2 version 1.0.6 of 6 September 2010
12
   Copyright (C) 1996-2010 Julian Seward <jseward@acm.org>
13
14
   Please read the WARNING, DISCLAIMER and PATENTS sections in the 
15
   README file.
16
17
   This program is released under the terms of the license contained
18
   in the file LICENSE.
19
   ------------------------------------------------------------------ */
20
21
22
#include "bzlib_private.h"
23
24
/*---------------------------------------------------*/
25
96.8M
#define WEIGHTOF(zz0)  ((zz0) & 0xffffff00)
26
#define DEPTHOF(zz1)   ((zz1) & 0x000000ff)
27
48.4M
#define MYMAX(zz2,zz3) ((zz2) > (zz3) ? (zz2) : (zz3))
28
29
#define ADDWEIGHTS(zw1,zw2)                           \
30
48.4M
   (WEIGHTOF(zw1)+WEIGHTOF(zw2)) |                    \
31
48.4M
   (1 + MYMAX(DEPTHOF(zw1),DEPTHOF(zw2)))
32
33
97.8M
#define UPHEAP(z)                                     \
34
97.8M
{                                                     \
35
97.8M
   Int32 zz, tmp;                                     \
36
97.8M
   zz = z; tmp = heap[zz];                            \
37
121M
   while (weight[tmp] < weight[heap[zz >> 1]]) {      \
38
23.6M
      heap[zz] = heap[zz >> 1];                       \
39
23.6M
      zz >>= 1;                                       \
40
23.6M
   }                                                  \
41
97.8M
   heap[zz] = tmp;                                    \
42
97.8M
}
43
44
96.8M
#define DOWNHEAP(z)                                   \
45
96.8M
{                                                     \
46
96.8M
   Int32 zz, yy, tmp;                                 \
47
96.8M
   zz = z; tmp = heap[zz];                            \
48
560M
   while (True) {                                     \
49
560M
      yy = zz << 1;                                   \
50
560M
      if (yy > nHeap) break;                          \
51
560M
      if (yy < nHeap &&                               \
52
477M
          weight[heap[yy+1]] < weight[heap[yy]])      \
53
477M
         yy++;                                        \
54
477M
      if (weight[tmp] < weight[heap[yy]]) break;      \
55
477M
      heap[zz] = heap[yy];                            \
56
463M
      zz = yy;                                        \
57
463M
   }                                                  \
58
96.8M
   heap[zz] = tmp;                                    \
59
96.8M
}
60
61
62
/*---------------------------------------------------*/
63
void BZ2_hbMakeCodeLengths ( UChar *len, 
64
                             Int32 *freq,
65
                             Int32 alphaSize,
66
                             Int32 maxLen )
67
942k
{
68
   /*--
69
      Nodes and heap entries run from 1.  Entry 0
70
      for both the heap and nodes is a sentinel.
71
   --*/
72
942k
   Int32 nNodes, nHeap, n1, n2, i, j, k;
73
942k
   Bool  tooLong;
74
75
942k
   Int32 heap   [ BZ_MAX_ALPHA_SIZE + 2 ];
76
942k
   Int32 weight [ BZ_MAX_ALPHA_SIZE * 2 ];
77
942k
   Int32 parent [ BZ_MAX_ALPHA_SIZE * 2 ]; 
78
79
50.3M
   for (i = 0; i < alphaSize; i++)
80
49.3M
      weight[i+1] = (freq[i] == 0 ? 1 : freq[i]) << 8;
81
82
942k
   while (True) {
83
84
942k
      nNodes = alphaSize;
85
942k
      nHeap = 0;
86
87
942k
      heap[0] = 0;
88
942k
      weight[0] = 0;
89
942k
      parent[0] = -2;
90
91
50.3M
      for (i = 1; i <= alphaSize; i++) {
92
49.3M
         parent[i] = -1;
93
49.3M
         nHeap++;
94
49.3M
         heap[nHeap] = i;
95
49.3M
         UPHEAP(nHeap);
96
49.3M
      }
97
98
942k
      AssertH( nHeap < (BZ_MAX_ALPHA_SIZE+2), 2001 );
99
   
100
49.3M
      while (nHeap > 1) {
101
48.4M
         n1 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1);
102
48.4M
         n2 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1);
103
48.4M
         nNodes++;
104
48.4M
         parent[n1] = parent[n2] = nNodes;
105
48.4M
         weight[nNodes] = ADDWEIGHTS(weight[n1], weight[n2]);
106
48.4M
         parent[nNodes] = -1;
107
48.4M
         nHeap++;
108
48.4M
         heap[nHeap] = nNodes;
109
48.4M
         UPHEAP(nHeap);
110
48.4M
      }
111
112
942k
      AssertH( nNodes < (BZ_MAX_ALPHA_SIZE * 2), 2002 );
113
114
942k
      tooLong = False;
115
50.3M
      for (i = 1; i <= alphaSize; i++) {
116
49.3M
         j = 0;
117
49.3M
         k = i;
118
429M
         while (parent[k] >= 0) { k = parent[k]; j++; }
119
49.3M
         len[i-1] = j;
120
49.3M
         if (j > maxLen) tooLong = True;
121
49.3M
      }
122
      
123
942k
      if (! tooLong) break;
124
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.
135
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. */
141
142
0
      for (i = 1; i <= alphaSize; i++) {
143
0
         j = weight[i] >> 8;
144
0
         j = 1 + (j / 2);
145
0
         weight[i] = j << 8;
146
0
      }
147
0
   }
148
942k
}
149
150
151
/*---------------------------------------------------*/
152
void BZ2_hbAssignCodes ( Int32 *code,
153
                         UChar *length,
154
                         Int32 minLen,
155
                         Int32 maxLen,
156
                         Int32 alphaSize )
157
235k
{
158
235k
   Int32 n, vec, i;
159
160
235k
   vec = 0;
161
1.01M
   for (n = minLen; n <= maxLen; n++) {
162
63.9M
      for (i = 0; i < alphaSize; i++)
163
63.2M
         if (length[i] == n) { code[i] = vec; vec++; };
164
779k
      vec <<= 1;
165
779k
   }
166
235k
}
167
168
169
/*---------------------------------------------------*/
170
void BZ2_hbCreateDecodeTables ( Int32 *limit,
171
                                Int32 *base,
172
                                Int32 *perm,
173
                                UChar *length,
174
                                Int32 minLen,
175
                                Int32 maxLen,
176
                                Int32 alphaSize )
177
3.15k
{
178
3.15k
   Int32 pp, i, j, vec;
179
180
3.15k
   pp = 0;
181
11.5k
   for (i = minLen; i <= maxLen; i++)
182
241k
      for (j = 0; j < alphaSize; j++)
183
232k
         if (length[j] == i) { perm[pp] = j; pp++; };
184
185
75.7k
   for (i = 0; i < BZ_MAX_CODE_LEN; i++) base[i] = 0;
186
68.9k
   for (i = 0; i < alphaSize; i++) base[length[i]+1]++;
187
188
72.6k
   for (i = 1; i < BZ_MAX_CODE_LEN; i++) base[i] += base[i-1];
189
190
75.7k
   for (i = 0; i < BZ_MAX_CODE_LEN; i++) limit[i] = 0;
191
3.15k
   vec = 0;
192
193
11.5k
   for (i = minLen; i <= maxLen; i++) {
194
8.37k
      vec += (base[i+1] - base[i]);
195
8.37k
      limit[i] = vec-1;
196
8.37k
      vec <<= 1;
197
8.37k
   }
198
8.37k
   for (i = minLen + 1; i <= maxLen; i++)
199
5.21k
      base[i] = ((limit[i-1] + 1) << 1) - base[i];
200
3.15k
}
201
202
203
/*-------------------------------------------------------------*/
204
/*--- end                                         huffman.c ---*/
205
/*-------------------------------------------------------------*/