Coverage Report

Created: 2026-12-01 06:24

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
46.0M
#define WEIGHTOF(zz0)  ((zz0) & 0xffffff00)
26
#define DEPTHOF(zz1)   ((zz1) & 0x000000ff)
27
23.0M
#define MYMAX(zz2,zz3) ((zz2) > (zz3) ? (zz2) : (zz3))
28
29
#define ADDWEIGHTS(zw1,zw2)                           \
30
23.0M
   (WEIGHTOF(zw1)+WEIGHTOF(zw2)) |                    \
31
23.0M
   (1 + MYMAX(DEPTHOF(zw1),DEPTHOF(zw2)))
32
33
46.1M
#define UPHEAP(z)                                     \
34
46.1M
{                                                     \
35
46.1M
   Int32 zz, tmp;                                     \
36
46.1M
   zz = z; tmp = heap[zz];                            \
37
66.1M
   while (weight[tmp] < weight[heap[zz >> 1]]) {      \
38
19.9M
      heap[zz] = heap[zz >> 1];                       \
39
19.9M
      zz >>= 1;                                       \
40
19.9M
   }                                                  \
41
46.1M
   heap[zz] = tmp;                                    \
42
46.1M
}
43
44
46.0M
#define DOWNHEAP(z)                                   \
45
46.0M
{                                                     \
46
46.0M
   Int32 zz, yy, tmp;                                 \
47
46.0M
   zz = z; tmp = heap[zz];                            \
48
288M
   while (True) {                                     \
49
288M
      yy = zz << 1;                                   \
50
288M
      if (yy > nHeap) break;                          \
51
288M
      if (yy < nHeap &&                               \
52
249M
          weight[heap[yy+1]] < weight[heap[yy]])      \
53
249M
         yy++;                                        \
54
249M
      if (weight[tmp] < weight[heap[yy]]) break;      \
55
249M
      heap[zz] = heap[yy];                            \
56
242M
      zz = yy;                                        \
57
242M
   }                                                  \
58
46.0M
   heap[zz] = tmp;                                    \
59
46.0M
}
60
61
62
/*---------------------------------------------------*/
63
void BZ2_hbMakeCodeLengths ( UChar *len, 
64
                             Int32 *freq,
65
                             Int32 alphaSize,
66
                             Int32 maxLen )
67
137k
{
68
   /*--
69
      Nodes and heap entries run from 1.  Entry 0
70
      for both the heap and nodes is a sentinel.
71
   --*/
72
137k
   Int32 nNodes, nHeap, n1, n2, i, j, k;
73
137k
   Bool  tooLong;
74
75
137k
   Int32 heap   [ BZ_MAX_ALPHA_SIZE + 2 ];
76
137k
   Int32 weight [ BZ_MAX_ALPHA_SIZE * 2 ];
77
137k
   Int32 parent [ BZ_MAX_ALPHA_SIZE * 2 ]; 
78
79
23.2M
   for (i = 0; i < alphaSize; i++)
80
23.0M
      weight[i+1] = (freq[i] == 0 ? 1 : freq[i]) << 8;
81
82
138k
   while (True) {
83
84
138k
      nNodes = alphaSize;
85
138k
      nHeap = 0;
86
87
138k
      heap[0] = 0;
88
138k
      weight[0] = 0;
89
138k
      parent[0] = -2;
90
91
23.3M
      for (i = 1; i <= alphaSize; i++) {
92
23.1M
         parent[i] = -1;
93
23.1M
         nHeap++;
94
23.1M
         heap[nHeap] = i;
95
23.1M
         UPHEAP(nHeap);
96
23.1M
      }
97
98
138k
      AssertH( nHeap < (BZ_MAX_ALPHA_SIZE+2), 2001 );
99
   
100
23.1M
      while (nHeap > 1) {
101
23.0M
         n1 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1);
102
23.0M
         n2 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1);
103
23.0M
         nNodes++;
104
23.0M
         parent[n1] = parent[n2] = nNodes;
105
23.0M
         weight[nNodes] = ADDWEIGHTS(weight[n1], weight[n2]);
106
23.0M
         parent[nNodes] = -1;
107
23.0M
         nHeap++;
108
23.0M
         heap[nHeap] = nNodes;
109
23.0M
         UPHEAP(nHeap);
110
23.0M
      }
111
112
138k
      AssertH( nNodes < (BZ_MAX_ALPHA_SIZE * 2), 2002 );
113
114
138k
      tooLong = False;
115
23.3M
      for (i = 1; i <= alphaSize; i++) {
116
23.1M
         j = 0;
117
23.1M
         k = i;
118
219M
         while (parent[k] >= 0) { k = parent[k]; j++; }
119
23.1M
         len[i-1] = j;
120
23.1M
         if (j > maxLen) tooLong = True;
121
23.1M
      }
122
      
123
138k
      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
85.2k
      for (i = 1; i <= alphaSize; i++) {
143
84.8k
         j = weight[i] >> 8;
144
84.8k
         j = 1 + (j / 2);
145
84.8k
         weight[i] = j << 8;
146
84.8k
      }
147
348
   }
148
137k
}
149
150
151
/*---------------------------------------------------*/
152
void BZ2_hbAssignCodes ( Int32 *code,
153
                         UChar *length,
154
                         Int32 minLen,
155
                         Int32 maxLen,
156
                         Int32 alphaSize )
157
34.4k
{
158
34.4k
   Int32 n, vec, i;
159
160
34.4k
   vec = 0;
161
227k
   for (n = minLen; n <= maxLen; n++) {
162
36.1M
      for (i = 0; i < alphaSize; i++)
163
35.9M
         if (length[i] == n) { code[i] = vec; vec++; };
164
192k
      vec <<= 1;
165
192k
   }
166
34.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
465k
{
178
465k
   Int32 pp, i, j, vec;
179
180
465k
   pp = 0;
181
1.12M
   for (i = minLen; i <= maxLen; i++)
182
25.6M
      for (j = 0; j < alphaSize; j++)
183
24.9M
         if (length[j] == i) { perm[pp] = j; pp++; };
184
185
11.1M
   for (i = 0; i < BZ_MAX_CODE_LEN; i++) base[i] = 0;
186
5.89M
   for (i = 0; i < alphaSize; i++) base[length[i]+1]++;
187
188
10.7M
   for (i = 1; i < BZ_MAX_CODE_LEN; i++) base[i] += base[i-1];
189
190
11.1M
   for (i = 0; i < BZ_MAX_CODE_LEN; i++) limit[i] = 0;
191
465k
   vec = 0;
192
193
1.12M
   for (i = minLen; i <= maxLen; i++) {
194
659k
      vec += (base[i+1] - base[i]);
195
659k
      limit[i] = vec-1;
196
659k
      vec <<= 1;
197
659k
   }
198
659k
   for (i = minLen + 1; i <= maxLen; i++)
199
194k
      base[i] = ((limit[i-1] + 1) << 1) - base[i];
200
465k
}
201
202
203
/*-------------------------------------------------------------*/
204
/*--- end                                         huffman.c ---*/
205
/*-------------------------------------------------------------*/