Coverage Report

Created: 2026-01-20 07:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libewf/libewf/libewf_huffman_tree.c
Line
Count
Source
1
/*
2
 * Huffman tree functions
3
 *
4
 * Copyright (C) 2006-2025, Joachim Metz <joachim.metz@gmail.com>
5
 *
6
 * Refer to AUTHORS for acknowledgements.
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
#include <common.h>
23
#include <memory.h>
24
#include <types.h>
25
26
#include "libewf_bit_stream.h"
27
#include "libewf_huffman_tree.h"
28
#include "libewf_libcerror.h"
29
#include "libewf_libcnotify.h"
30
31
/* Creates a Huffman tree
32
 * Make sure the value huffman_tree is referencing, is set to NULL
33
 * Returns 1 if successful or -1 on error
34
 */
35
int libewf_huffman_tree_initialize(
36
     libewf_huffman_tree_t **huffman_tree,
37
     int number_of_symbols,
38
     uint8_t maximum_code_size,
39
     libcerror_error_t **error )
40
14.2k
{
41
14.2k
  static char *function = "libewf_huffman_tree_initialize";
42
14.2k
  size_t array_size     = 0;
43
44
14.2k
  if( huffman_tree == NULL )
45
0
  {
46
0
    libcerror_error_set(
47
0
     error,
48
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
49
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
50
0
     "%s: invalid Huffman tree.",
51
0
     function );
52
53
0
    return( -1 );
54
0
  }
55
14.2k
  if( *huffman_tree != NULL )
56
0
  {
57
0
    libcerror_error_set(
58
0
     error,
59
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
60
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
61
0
     "%s: invalid Huffman tree value already set.",
62
0
     function );
63
64
0
    return( -1 );
65
0
  }
66
14.2k
  if( ( number_of_symbols < 0 )
67
14.2k
   || ( number_of_symbols > 1024 ) )
68
0
  {
69
0
    libcerror_error_set(
70
0
     error,
71
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
72
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
73
0
     "%s: invalid number of symbols value out of bounds.",
74
0
     function );
75
76
0
    return( -1 );
77
0
  }
78
14.2k
  if( maximum_code_size > 32 )
79
0
  {
80
0
    libcerror_error_set(
81
0
     error,
82
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
83
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
84
0
     "%s: invalid maximum code size value out of bounds.",
85
0
     function );
86
87
0
    return( -1 );
88
0
  }
89
14.2k
  *huffman_tree = memory_allocate_structure(
90
14.2k
                   libewf_huffman_tree_t );
91
92
14.2k
  if( *huffman_tree == NULL )
93
0
  {
94
0
    libcerror_error_set(
95
0
     error,
96
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
97
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
98
0
     "%s: unable to create Huffman tree.",
99
0
     function );
100
101
0
    goto on_error;
102
0
  }
103
14.2k
  if( memory_set(
104
14.2k
       *huffman_tree,
105
14.2k
       0,
106
14.2k
       sizeof( libewf_huffman_tree_t ) ) == NULL )
107
0
  {
108
0
    libcerror_error_set(
109
0
     error,
110
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
111
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
112
0
     "%s: unable to clear Huffman tree.",
113
0
     function );
114
115
0
    memory_free(
116
0
     *huffman_tree );
117
118
0
    *huffman_tree = NULL;
119
120
0
    return( -1 );
121
0
  }
122
14.2k
  array_size = sizeof( uint16_t ) * number_of_symbols;
123
124
14.2k
  ( *huffman_tree )->symbols = (uint16_t *) memory_allocate(
125
14.2k
                                             array_size );
126
127
14.2k
  if( ( *huffman_tree )->symbols == NULL )
128
0
  {
129
0
    libcerror_error_set(
130
0
     error,
131
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
132
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
133
0
     "%s: unable to create symbols.",
134
0
     function );
135
136
0
    goto on_error;
137
0
  }
138
14.2k
  if( memory_set(
139
14.2k
       ( *huffman_tree )->symbols,
140
14.2k
       0,
141
14.2k
       array_size ) == NULL )
142
0
  {
143
0
    libcerror_error_set(
144
0
     error,
145
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
146
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
147
0
     "%s: unable to clear symbols.",
148
0
     function );
149
150
0
    goto on_error;
151
0
  }
152
14.2k
  array_size = sizeof( int ) * ( maximum_code_size + 1 );
153
154
14.2k
  ( *huffman_tree )->code_size_counts = (int *) memory_allocate(
155
14.2k
                                                 array_size );
156
157
14.2k
  if( ( *huffman_tree )->code_size_counts == NULL )
158
0
  {
159
0
    libcerror_error_set(
160
0
     error,
161
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
162
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
163
0
     "%s: unable to create code size counts.",
164
0
     function );
165
166
0
    goto on_error;
167
0
  }
168
14.2k
  if( memory_set(
169
14.2k
       ( *huffman_tree )->code_size_counts,
170
14.2k
       0,
171
14.2k
       array_size ) == NULL )
172
0
  {
173
0
    libcerror_error_set(
174
0
     error,
175
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
176
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
177
0
     "%s: unable to clear code size counts.",
178
0
     function );
179
180
0
    goto on_error;
181
0
  }
182
14.2k
  ( *huffman_tree )->maximum_code_size = maximum_code_size;
183
184
14.2k
  return( 1 );
185
186
0
on_error:
187
0
  if( *huffman_tree != NULL )
188
0
  {
189
0
    if( ( *huffman_tree )->code_size_counts != NULL )
190
0
    {
191
0
      memory_free(
192
0
       ( *huffman_tree )->code_size_counts );
193
0
    }
194
0
    if( ( *huffman_tree )->symbols != NULL )
195
0
    {
196
0
      memory_free(
197
0
       ( *huffman_tree )->symbols );
198
0
    }
199
0
    memory_free(
200
0
     *huffman_tree );
201
202
0
    *huffman_tree = NULL;
203
0
  }
204
0
  return( -1 );
205
14.2k
}
206
207
/* Frees a Huffman tree
208
 * Returns 1 if successful or -1 on error
209
 */
210
int libewf_huffman_tree_free(
211
     libewf_huffman_tree_t **huffman_tree,
212
     libcerror_error_t **error )
213
14.2k
{
214
14.2k
  static char *function = "libewf_huffman_tree_free";
215
216
14.2k
  if( huffman_tree == NULL )
217
0
  {
218
0
    libcerror_error_set(
219
0
     error,
220
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
221
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
222
0
     "%s: invalid Huffman tree.",
223
0
     function );
224
225
0
    return( -1 );
226
0
  }
227
14.2k
  if( *huffman_tree != NULL )
228
14.2k
  {
229
14.2k
    if( ( *huffman_tree )->code_size_counts != NULL )
230
14.2k
    {
231
14.2k
      memory_free(
232
14.2k
       ( *huffman_tree )->code_size_counts );
233
14.2k
    }
234
14.2k
    if( ( *huffman_tree )->symbols != NULL )
235
14.2k
    {
236
14.2k
      memory_free(
237
14.2k
       ( *huffman_tree )->symbols );
238
14.2k
    }
239
14.2k
    memory_free(
240
14.2k
     *huffman_tree );
241
242
14.2k
    *huffman_tree = NULL;
243
14.2k
  }
244
14.2k
  return( 1 );
245
14.2k
}
246
247
/* Builds the Huffman tree
248
 * Returns 1 on success, 0 if the tree is empty or -1 on error
249
 */
250
int libewf_huffman_tree_build(
251
     libewf_huffman_tree_t *huffman_tree,
252
     const uint8_t *code_sizes_array,
253
     int number_of_code_sizes,
254
     libcerror_error_t **error )
255
14.0k
{
256
14.0k
  int *symbol_offsets   = NULL;
257
14.0k
  static char *function = "libewf_huffman_tree_build";
258
14.0k
  size_t array_size     = 0;
259
14.0k
  uint16_t symbol       = 0;
260
14.0k
  uint8_t bit_index     = 0;
261
14.0k
  uint8_t code_size     = 0;
262
14.0k
  int code_offset       = 0;
263
14.0k
  int left_value        = 0;
264
265
14.0k
  if( huffman_tree == NULL )
266
0
  {
267
0
    libcerror_error_set(
268
0
     error,
269
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
270
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
271
0
     "%s: invalid Huffman tree.",
272
0
     function );
273
274
0
    return( -1 );
275
0
  }
276
14.0k
  if( code_sizes_array == NULL )
277
0
  {
278
0
    libcerror_error_set(
279
0
     error,
280
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
281
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
282
0
     "%s: invalid code sizes array.",
283
0
     function );
284
285
0
    return( -1 );
286
0
  }
287
14.0k
  if( ( number_of_code_sizes < 0 )
288
14.0k
   || ( number_of_code_sizes > (int) INT16_MAX ) )
289
0
  {
290
0
    libcerror_error_set(
291
0
     error,
292
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
293
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
294
0
     "%s: invalid number of code sizes value out of bounds.",
295
0
     function );
296
297
0
    return( -1 );
298
0
  }
299
  /* Determine the code size frequencies
300
   */
301
14.0k
  array_size = sizeof( int ) * ( huffman_tree->maximum_code_size + 1 );
302
303
14.0k
  if( memory_set(
304
14.0k
       huffman_tree->code_size_counts,
305
14.0k
       0,
306
14.0k
       array_size ) == NULL )
307
0
  {
308
0
    libcerror_error_set(
309
0
     error,
310
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
311
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
312
0
     "%s: unable to clear code size counts.",
313
0
     function );
314
315
0
    goto on_error;
316
0
  }
317
14.0k
  for( symbol = 0;
318
1.49M
       symbol < (uint16_t) number_of_code_sizes;
319
1.47M
       symbol++ )
320
1.47M
  {
321
1.47M
    code_size = code_sizes_array[ symbol ];
322
323
1.47M
    if( code_size > huffman_tree->maximum_code_size )
324
0
    {
325
0
      libcerror_error_set(
326
0
       error,
327
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
328
0
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
329
0
       "%s: invalid symbol: %d code size: %" PRIu8 " value out of bounds.",
330
0
       function,
331
0
       symbol,
332
0
       code_size );
333
334
0
      goto on_error;
335
0
    }
336
1.47M
    huffman_tree->code_size_counts[ code_size ] += 1;
337
1.47M
  }
338
  /* The tree has no codes
339
   */
340
14.0k
  if( huffman_tree->code_size_counts[ 0 ] == number_of_code_sizes )
341
5
  {
342
5
    return( 0 );
343
5
  }
344
  /* Check if the set of code sizes is incomplete or over-subscribed
345
   */
346
14.0k
  left_value = 1;
347
348
14.0k
  for( bit_index = 1;
349
224k
       bit_index <= huffman_tree->maximum_code_size;
350
210k
       bit_index++ )
351
210k
  {
352
210k
    left_value <<= 1;
353
210k
    left_value  -= huffman_tree->code_size_counts[ bit_index ];
354
355
210k
    if( left_value < 0 )
356
26
    {
357
26
      libcerror_error_set(
358
26
       error,
359
26
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
360
26
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
361
26
       "%s: code sizes are over-subscribed.",
362
26
       function );
363
364
26
      goto on_error;
365
26
    }
366
210k
  }
367
/* TODO
368
  if( left_value > 0 )
369
  {
370
    libcerror_error_set(
371
     error,
372
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
373
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
374
     "%s: code sizes are incomplete.",
375
     function );
376
377
    goto on_error;
378
  }
379
*/
380
14.0k
  symbol_offsets = (int *) memory_allocate(
381
14.0k
                            array_size );
382
383
14.0k
  if( symbol_offsets == NULL )
384
0
  {
385
0
    libcerror_error_set(
386
0
     error,
387
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
388
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
389
0
     "%s: unable to create symbol offsets.",
390
0
     function );
391
392
0
    goto on_error;
393
0
  }
394
  /* Calculate the offsets to sort the symbols per code size
395
   */
396
14.0k
  symbol_offsets[ 0 ] = 0;
397
14.0k
  symbol_offsets[ 1 ] = 0;
398
399
14.0k
  for( bit_index = 1;
400
210k
       bit_index < huffman_tree->maximum_code_size;
401
196k
       bit_index++ )
402
196k
  {
403
196k
    symbol_offsets[ bit_index + 1 ] = symbol_offsets[ bit_index ] + huffman_tree->code_size_counts[ bit_index ];
404
196k
  }
405
  /* Fill the symbols sorted by code size
406
   */
407
14.0k
  for( symbol = 0;
408
1.49M
       symbol < (uint16_t) number_of_code_sizes;
409
1.47M
       symbol++ )
410
1.47M
  {
411
1.47M
    code_size = code_sizes_array[ symbol ];
412
413
1.47M
    if( code_size == 0 )
414
1.04M
    {
415
1.04M
      continue;
416
1.04M
    }
417
431k
    code_offset = symbol_offsets[ code_size ];
418
419
431k
    if( ( code_offset < 0 )
420
431k
     || ( code_offset > number_of_code_sizes ) )
421
0
    {
422
0
      libcerror_error_set(
423
0
       error,
424
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
425
0
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
426
0
       "%s: invalid symbol: %" PRIu16 " code offset: %d value out of bounds.",
427
0
       function,
428
0
       symbol,
429
0
       code_offset );
430
431
0
      goto on_error;
432
0
    }
433
431k
    symbol_offsets[ code_size ] += 1;
434
435
431k
    huffman_tree->symbols[ code_offset ] = symbol;
436
431k
  }
437
14.0k
  memory_free(
438
14.0k
   symbol_offsets );
439
440
14.0k
  return( 1 );
441
442
26
on_error:
443
26
  if( symbol_offsets != NULL )
444
0
  {
445
0
    memory_free(
446
0
     symbol_offsets );
447
0
  }
448
26
  return( -1 );
449
14.0k
}
450
451
/* Retrieves a symbol based on the Huffman code read from the bit-stream
452
 * Returns 1 on success or -1 on error
453
 */
454
int libewf_huffman_tree_get_symbol_from_bit_stream(
455
     libewf_huffman_tree_t *huffman_tree,
456
     libewf_bit_stream_t *bit_stream,
457
     uint16_t *symbol,
458
     libcerror_error_t **error )
459
16.6M
{
460
16.6M
  static char *function  = "libewf_huffman_tree_get_symbol_from_bit_stream";
461
16.6M
  uint32_t value_32bit   = 0;
462
16.6M
  uint16_t safe_symbol   = 0;
463
16.6M
  uint8_t bit_index      = 0;
464
16.6M
  int code_size_count    = 0;
465
16.6M
  int first_huffman_code = 0;
466
16.6M
  int first_index        = 0;
467
16.6M
  int huffman_code       = 0;
468
16.6M
  int result             = 0;
469
470
16.6M
  if( huffman_tree == NULL )
471
0
  {
472
0
    libcerror_error_set(
473
0
     error,
474
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
475
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
476
0
     "%s: invalid Huffman tree.",
477
0
     function );
478
479
0
    return( -1 );
480
0
  }
481
16.6M
  if( bit_stream == NULL )
482
0
  {
483
0
    libcerror_error_set(
484
0
     error,
485
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
486
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
487
0
     "%s: invalid bit stream.",
488
0
     function );
489
490
0
    return( -1 );
491
0
  }
492
16.6M
  if( symbol == NULL )
493
0
  {
494
0
    libcerror_error_set(
495
0
     error,
496
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
497
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
498
0
     "%s: invalid symbol.",
499
0
     function );
500
501
0
    return( -1 );
502
0
  }
503
/* TODO get maximum_code_size of bits into local bit buffer */
504
505
16.6M
  for( bit_index = 1;
506
43.9M
       bit_index <= huffman_tree->maximum_code_size;
507
27.2M
       bit_index++ )
508
43.9M
  {
509
43.9M
    if( libewf_bit_stream_get_value(
510
43.9M
         bit_stream,
511
43.9M
         1,
512
43.9M
         &value_32bit,
513
43.9M
         error ) != 1 )
514
22
    {
515
22
      libcerror_error_set(
516
22
       error,
517
22
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
518
22
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
519
22
       "%s: unable to retrieve value from bit stream.",
520
22
       function );
521
522
22
      return( -1 );
523
22
    }
524
43.9M
    huffman_code <<= 1;
525
43.9M
    huffman_code  |= (int) value_32bit;
526
527
43.9M
    code_size_count = huffman_tree->code_size_counts[ bit_index ];
528
529
43.9M
    if( ( huffman_code - code_size_count ) < first_huffman_code )
530
16.6M
    {
531
16.6M
      safe_symbol = huffman_tree->symbols[ first_index + ( huffman_code - first_huffman_code ) ];
532
533
16.6M
      result = 1;
534
535
16.6M
      break;
536
16.6M
    }
537
27.2M
    first_huffman_code  += code_size_count;
538
27.2M
    first_huffman_code <<= 1;
539
27.2M
    first_index         += code_size_count;
540
27.2M
  }
541
16.6M
  if( result != 1 )
542
22
  {
543
22
    libcerror_error_set(
544
22
     error,
545
22
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
546
22
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
547
22
     "%s: invalid Huffman code: 0x%08" PRIx32 ".",
548
22
     function,
549
22
     huffman_code );
550
551
22
    return( -1 );
552
22
  }
553
16.6M
  *symbol = safe_symbol;
554
555
16.6M
  return( 1 );
556
16.6M
}
557