Coverage Report

Created: 2026-08-13 06:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/bzip2/huffman.c
Line
Count
Source
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.8 of 13 July 2019
12
   Copyright (C) 1996-2019 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
16.6M
#define WEIGHTOF(zz0)  ((zz0) & 0xffffff00)
26
#define DEPTHOF(zz1)   ((zz1) & 0x000000ff)
27
8.30M
#define MYMAX(zz2,zz3) ((zz2) > (zz3) ? (zz2) : (zz3))
28
29
#define ADDWEIGHTS(zw1,zw2)                           \
30
8.30M
   (WEIGHTOF(zw1)+WEIGHTOF(zw2)) |                    \
31
8.30M
   (1 + MYMAX(DEPTHOF(zw1),DEPTHOF(zw2)))
32
33
16.6M
#define UPHEAP(z)                                     \
34
16.6M
{                                                     \
35
16.6M
   Int32 zz, tmp;                                     \
36
16.6M
   zz = z; tmp = heap[zz];                            \
37
22.9M
   while (weight[tmp] < weight[heap[zz >> 1]]) {      \
38
6.30M
      heap[zz] = heap[zz >> 1];                       \
39
6.30M
      zz >>= 1;                                       \
40
6.30M
   }                                                  \
41
16.6M
   heap[zz] = tmp;                                    \
42
16.6M
}
43
44
16.6M
#define DOWNHEAP(z)                                   \
45
16.6M
{                                                     \
46
16.6M
   Int32 zz, yy, tmp;                                 \
47
16.6M
   zz = z; tmp = heap[zz];                            \
48
104M
   while (True) {                                     \
49
104M
      yy = zz << 1;                                   \
50
104M
      if (yy > nHeap) break;                          \
51
104M
      if (yy < nHeap &&                               \
52
90.0M
          weight[heap[yy+1]] < weight[heap[yy]])      \
53
90.0M
         yy++;                                        \
54
90.0M
      if (weight[tmp] < weight[heap[yy]]) break;      \
55
90.0M
      heap[zz] = heap[yy];                            \
56
87.6M
      zz = yy;                                        \
57
87.6M
   }                                                  \
58
16.6M
   heap[zz] = tmp;                                    \
59
16.6M
}
60
61
62
/*---------------------------------------------------*/
63
void BZ2_hbMakeCodeLengths ( UChar *len, 
64
                             Int32 *freq,
65
                             Int32 alphaSize,
66
                             Int32 maxLen )
67
49.8k
{
68
   /*--
69
      Nodes and heap entries run from 1.  Entry 0
70
      for both the heap and nodes is a sentinel.
71
   --*/
72
49.8k
   Int32 nNodes, nHeap, n1, n2, i, j, k;
73
49.8k
   Bool  tooLong;
74
75
49.8k
   Int32 heap   [ BZ_MAX_ALPHA_SIZE + 2 ];
76
49.8k
   Int32 weight [ BZ_MAX_ALPHA_SIZE * 2 ];
77
49.8k
   Int32 parent [ BZ_MAX_ALPHA_SIZE * 2 ]; 
78
79
8.36M
   for (i = 0; i < alphaSize; i++)
80
8.31M
      weight[i+1] = (freq[i] == 0 ? 1 : freq[i]) << 8;
81
82
49.9k
   while (True) {
83
84
49.9k
      nNodes = alphaSize;
85
49.9k
      nHeap = 0;
86
87
49.9k
      heap[0] = 0;
88
49.9k
      weight[0] = 0;
89
49.9k
      parent[0] = -2;
90
91
8.40M
      for (i = 1; i <= alphaSize; i++) {
92
8.35M
         parent[i] = -1;
93
8.35M
         nHeap++;
94
8.35M
         heap[nHeap] = i;
95
8.35M
         UPHEAP(nHeap);
96
8.35M
      }
97
98
49.9k
      AssertH( nHeap < (BZ_MAX_ALPHA_SIZE+2), 2001 );
99
   
100
8.35M
      while (nHeap > 1) {
101
8.30M
         n1 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1);
102
8.30M
         n2 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1);
103
8.30M
         nNodes++;
104
8.30M
         parent[n1] = parent[n2] = nNodes;
105
8.30M
         weight[nNodes] = ADDWEIGHTS(weight[n1], weight[n2]);
106
8.30M
         parent[nNodes] = -1;
107
8.30M
         nHeap++;
108
8.30M
         heap[nHeap] = nNodes;
109
8.30M
         UPHEAP(nHeap);
110
8.30M
      }
111
112
49.9k
      AssertH( nNodes < (BZ_MAX_ALPHA_SIZE * 2), 2002 );
113
114
49.9k
      tooLong = False;
115
8.40M
      for (i = 1; i <= alphaSize; i++) {
116
8.35M
         j = 0;
117
8.35M
         k = i;
118
78.7M
         while (parent[k] >= 0) { k = parent[k]; j++; }
119
8.35M
         len[i-1] = j;
120
8.35M
         if (j > maxLen) tooLong = True;
121
8.35M
      }
122
      
123
49.9k
      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
36.5k
      for (i = 1; i <= alphaSize; i++) {
143
36.4k
         j = weight[i] >> 8;
144
36.4k
         j = 1 + (j / 2);
145
36.4k
         weight[i] = j << 8;
146
36.4k
      }
147
149
   }
148
49.8k
}
149
150
151
/*---------------------------------------------------*/
152
void BZ2_hbAssignCodes ( Int32 *code,
153
                         UChar *length,
154
                         Int32 minLen,
155
                         Int32 maxLen,
156
                         Int32 alphaSize )
157
12.4k
{
158
12.4k
   Int32 n, vec, i;
159
160
12.4k
   vec = 0;
161
80.7k
   for (n = minLen; n <= maxLen; n++) {
162
12.7M
      for (i = 0; i < alphaSize; i++)
163
12.7M
         if (length[i] == n) { code[i] = vec; vec++; };
164
68.2k
      vec <<= 1;
165
68.2k
   }
166
12.4k
}
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
2.35k
{
178
2.35k
   Int32 pp, i, j, vec;
179
180
2.35k
   pp = 0;
181
10.1k
   for (i = minLen; i <= maxLen; i++)
182
760k
      for (j = 0; j < alphaSize; j++)
183
752k
         if (length[j] == i) { perm[pp] = j; pp++; };
184
185
56.4k
   for (i = 0; i < BZ_MAX_CODE_LEN; i++) base[i] = 0;
186
171k
   for (i = 0; i < alphaSize; i++) base[length[i]+1]++;
187
188
54.0k
   for (i = 1; i < BZ_MAX_CODE_LEN; i++) base[i] += base[i-1];
189
190
56.4k
   for (i = 0; i < BZ_MAX_CODE_LEN; i++) limit[i] = 0;
191
2.35k
   vec = 0;
192
193
10.1k
   for (i = minLen; i <= maxLen; i++) {
194
7.80k
      vec += (base[i+1] - base[i]);
195
7.80k
      limit[i] = vec-1;
196
7.80k
      vec <<= 1;
197
7.80k
   }
198
7.80k
   for (i = minLen + 1; i <= maxLen; i++)
199
5.44k
      base[i] = ((limit[i-1] + 1) << 1) - base[i];
200
2.35k
}
201
202
203
/*-------------------------------------------------------------*/
204
/*--- end                                         huffman.c ---*/
205
/*-------------------------------------------------------------*/