File: libfwnt_huffman_tree.c

package info (click to toggle)
libevt 20200926-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 17,024 kB
  • sloc: ansic: 278,926; sh: 6,138; makefile: 1,728; python: 390; cpp: 88; sed: 16
file content (589 lines) | stat: -rw-r--r-- 13,113 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/*
 * Huffman tree functions
 *
 * Copyright (C) 2009-2020, Joachim Metz <joachim.metz@gmail.com>
 *
 * Refer to AUTHORS for acknowledgements.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <common.h>
#include <memory.h>
#include <types.h>

#include "libfwnt_bit_stream.h"
#include "libfwnt_huffman_tree.h"
#include "libfwnt_libcerror.h"
#include "libfwnt_libcnotify.h"

/* Creates a Huffman tree
 * Make sure the value huffman_tree is referencing, is set to NULL
 * Returns 1 if successful or -1 on error
 */
int libfwnt_huffman_tree_initialize(
     libfwnt_huffman_tree_t **huffman_tree,
     int number_of_symbols,
     uint8_t maximum_code_size,
     libcerror_error_t **error )
{
	static char *function = "libfwnt_huffman_tree_initialize";
	size_t array_size     = 0;

	if( huffman_tree == NULL )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
		 "%s: invalid Huffman tree.",
		 function );

		return( -1 );
	}
	if( *huffman_tree != NULL )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_RUNTIME,
		 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
		 "%s: invalid Huffman tree value already set.",
		 function );

		return( -1 );
	}
	if( ( number_of_symbols < 0 )
	 || ( number_of_symbols > 1024 ) )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
		 "%s: invalid number of symbols value out of bounds.",
		 function );

		return( -1 );
	}
	if( maximum_code_size > 32 )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
		 "%s: invalid maximum code size value out of bounds.",
		 function );

		return( -1 );
	}
	*huffman_tree = memory_allocate_structure(
	                 libfwnt_huffman_tree_t );

	if( *huffman_tree == NULL )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_MEMORY,
		 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
		 "%s: unable to create Huffman tree.",
		 function );

		goto on_error;
	}
	if( memory_set(
	     *huffman_tree,
	     0,
	     sizeof( libfwnt_huffman_tree_t ) ) == NULL )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_MEMORY,
		 LIBCERROR_MEMORY_ERROR_SET_FAILED,
		 "%s: unable to clear Huffman tree.",
		 function );

		memory_free(
		 *huffman_tree );

		*huffman_tree = NULL;

		return( -1 );
	}
	array_size = sizeof( int ) * number_of_symbols;

	( *huffman_tree )->symbols = (int *) memory_allocate(
	                                      array_size );

	if( ( *huffman_tree )->symbols == NULL )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_MEMORY,
		 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
		 "%s: unable to create symbols.",
		 function );

		goto on_error;
	}
	if( memory_set(
	     ( *huffman_tree )->symbols,
	     0,
	     array_size ) == NULL )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_MEMORY,
		 LIBCERROR_MEMORY_ERROR_SET_FAILED,
		 "%s: unable to clear symbols.",
		 function );

		goto on_error;
	}
	array_size = sizeof( int ) * ( maximum_code_size + 1 );

	( *huffman_tree )->code_size_counts = (int *) memory_allocate(
	                                               array_size );

	if( ( *huffman_tree )->code_size_counts == NULL )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_MEMORY,
		 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
		 "%s: unable to create code size counts.",
		 function );

		goto on_error;
	}
	if( memory_set(
	     ( *huffman_tree )->code_size_counts,
	     0,
	     array_size ) == NULL )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_MEMORY,
		 LIBCERROR_MEMORY_ERROR_SET_FAILED,
		 "%s: unable to clear code size counts.",
		 function );

		goto on_error;
	}
	( *huffman_tree )->maximum_code_size = maximum_code_size;

	return( 1 );

on_error:
	if( *huffman_tree != NULL )
	{
		if( ( *huffman_tree )->code_size_counts != NULL )
		{
			memory_free(
			 ( *huffman_tree )->code_size_counts );
		}
		if( ( *huffman_tree )->symbols != NULL )
		{
			memory_free(
			 ( *huffman_tree )->symbols );
		}
		memory_free(
		 *huffman_tree );

		*huffman_tree = NULL;
	}
	return( -1 );
}

/* Frees a Huffman tree
 * Returns 1 if successful or -1 on error
 */
int libfwnt_huffman_tree_free(
     libfwnt_huffman_tree_t **huffman_tree,
     libcerror_error_t **error )
{
	static char *function = "libfwnt_huffman_tree_free";

	if( huffman_tree == NULL )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
		 "%s: invalid Huffman tree.",
		 function );

		return( -1 );
	}
	if( *huffman_tree != NULL )
	{
		if( ( *huffman_tree )->code_size_counts != NULL )
		{
			memory_free(
			 ( *huffman_tree )->code_size_counts );
		}
		if( ( *huffman_tree )->symbols != NULL )
		{
			memory_free(
			 ( *huffman_tree )->symbols );
		}
		memory_free(
		 *huffman_tree );

		*huffman_tree = NULL;
	}
	return( 1 );
}

/* Builds the Huffman tree
 * Returns 1 on success, 0 if the tree is empty or -1 on error
 */
int libfwnt_huffman_tree_build(
     libfwnt_huffman_tree_t *huffman_tree,
     const uint8_t *code_sizes_array,
     int number_of_code_sizes,
     libcerror_error_t **error )
{
	int *symbol_offsets   = NULL;
	static char *function = "libfwnt_huffman_tree_build";
	size_t array_size     = 0;
	uint8_t bit_index     = 0;
	uint8_t code_size     = 0;
	int code_offset       = 0;
	int left_value        = 0;
	int symbol            = 0;

	if( huffman_tree == NULL )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
		 "%s: invalid Huffman tree.",
		 function );

		return( -1 );
	}
	if( code_sizes_array == NULL )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
		 "%s: invalid code sizes array.",
		 function );

		return( -1 );
	}
	if( number_of_code_sizes < 0 )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
		 "%s: invalid number of code sizes value out of bounds.",
		 function );

		return( -1 );
	}
	/* Determine the code size frequencies
	 */
	array_size = sizeof( int ) * ( huffman_tree->maximum_code_size + 1 );

	if( memory_set(
	     huffman_tree->code_size_counts,
	     0,
	     array_size ) == NULL )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_MEMORY,
		 LIBCERROR_MEMORY_ERROR_SET_FAILED,
		 "%s: unable to clear code size counts.",
		 function );

		goto on_error;
	}
	for( symbol = 0;
	     symbol < number_of_code_sizes;
	     symbol++ )
	{
		code_size = code_sizes_array[ symbol ];

		if( code_size > huffman_tree->maximum_code_size )
		{
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_RUNTIME,
			 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
			 "%s: invalid symbol: %d code size: %" PRIu8 " value out of bounds.",
			 function,
			 symbol,
			 code_size );

			goto on_error;
		}
		huffman_tree->code_size_counts[ code_size ] += 1;
	}
	/* The tree has no codes
	 */
	if( huffman_tree->code_size_counts[ 0 ] == number_of_code_sizes )
	{
		return( 0 );
	}
	/* Check if the set of code sizes is incomplete or over-subscribed
	 */
	left_value = 1;

	for( bit_index = 1;
	     bit_index <= huffman_tree->maximum_code_size;
	     bit_index++ )
	{
		left_value <<= 1;
		left_value  -= huffman_tree->code_size_counts[ bit_index ];

		if( left_value < 0 )
		{
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_RUNTIME,
			 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
			 "%s: code sizes are over-subscribed.",
			 function );

			goto on_error;
		}
	}
/* TODO
	if( left_value > 0 )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_RUNTIME,
		 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
		 "%s: code sizes are incomplete.",
		 function );

		goto on_error;
	}
*/
	symbol_offsets = (int *) memory_allocate(
	                          array_size );

	if( symbol_offsets == NULL )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_MEMORY,
		 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
		 "%s: unable to create symbol offsets.",
		 function );

		goto on_error;
	}
	/* Calculate the offsets to sort the symbols per code size
	 */
	symbol_offsets[ 0 ] = 0;
	symbol_offsets[ 1 ] = 0;

	for( bit_index = 1;
	     bit_index < huffman_tree->maximum_code_size;
	     bit_index++ )
	{
		symbol_offsets[ bit_index + 1 ] = symbol_offsets[ bit_index ] + huffman_tree->code_size_counts[ bit_index ];
	}
	/* Fill the symbols sorted by code size
	 */
	for( symbol = 0;
	     symbol < number_of_code_sizes;
	     symbol++ )
	{
		code_size = code_sizes_array[ symbol ];

		if( code_size == 0 )
		{
			continue;
		}
		code_offset = symbol_offsets[ code_size ];

		if( ( code_offset < 0 )
		 || ( code_offset > number_of_code_sizes ) )
		{
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_RUNTIME,
			 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
			 "%s: invalid symbol: %d code offset: %d value out of bounds.",
			 function,
			 symbol,
			 code_offset );

			goto on_error;
		}
		symbol_offsets[ code_size ] += 1;

		huffman_tree->symbols[ code_offset ] = symbol;
	}
	memory_free(
	 symbol_offsets );

	return( 1 );

on_error:
	if( symbol_offsets != NULL )
	{
		memory_free(
		 symbol_offsets );
	}
	return( -1 );
}

/* Retrieves a symbol based on the Huffman code read from the bit-stream
 * Returns 1 on success or -1 on error
 */
int libfwnt_huffman_tree_get_symbol_from_bit_stream(
     libfwnt_huffman_tree_t *huffman_tree,
     libfwnt_bit_stream_t *bit_stream,
     uint32_t *symbol,
     libcerror_error_t **error )
{
	static char *function  = "libfwnt_huffman_tree_get_symbol_from_bit_stream";
	uint32_t safe_symbol   = 0;
	uint32_t value_32bit   = 0;
	uint8_t bit_index      = 0;
	uint8_t number_of_bits = 0;
	int code_size_count    = 0;
	int first_huffman_code = 0;
	int first_index        = 0;
	int huffman_code       = 0;
	int result             = 0;

	if( huffman_tree == NULL )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
		 "%s: invalid Huffman tree.",
		 function );

		return( -1 );
	}
	if( bit_stream == NULL )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
		 "%s: invalid bit stream.",
		 function );

		return( -1 );
	}
	if( symbol == NULL )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
		 "%s: invalid symbol.",
		 function );

		return( -1 );
	}
	/* Try to fill the bit buffer with the maximum number of bits
	 */
	while( bit_stream->bit_buffer_size < huffman_tree->maximum_code_size )
	{
		result = libfwnt_bit_stream_read(
		          bit_stream,
		          huffman_tree->maximum_code_size,
		          error );

		if( result == -1 )
		{
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_IO,
			 LIBCERROR_IO_ERROR_READ_FAILED,
			 "%s: unable to read bits.",
			 function );

			return( -1 );
		}
		else if( result == 0 )
		{
			break;
		}
	}
	if( huffman_tree->maximum_code_size < bit_stream->bit_buffer_size )
	{
		number_of_bits = huffman_tree->maximum_code_size;
	}
	else
	{
		number_of_bits = bit_stream->bit_buffer_size;
	}
	for( bit_index = 1;
	     bit_index <= number_of_bits;
	     bit_index++ )
	{
/* TODO get maximum_code_size of bits into local bit buffer */
		if( libfwnt_bit_stream_get_value(
		     bit_stream,
		     1,
		     &value_32bit,
		     error ) != 1 )
		{
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_RUNTIME,
			 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
			 "%s: unable to retrieve value from bit stream.",
			 function );

			return( -1 );
		}
		huffman_code <<= 1;
		huffman_code  |= (int) value_32bit;

		code_size_count = huffman_tree->code_size_counts[ bit_index ];

		if( ( huffman_code - code_size_count ) < first_huffman_code )
		{
			safe_symbol = huffman_tree->symbols[ first_index + ( huffman_code - first_huffman_code ) ];

			result = 1;

			break;
		}
		first_huffman_code  += code_size_count;
		first_huffman_code <<= 1;
		first_index         += code_size_count;
	}
	if( result != 1 )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_RUNTIME,
		 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
		 "%s: invalid Huffman code: 0x%08" PRIx32 ".",
		 function,
		 huffman_code );

		return( -1 );
	}
	*symbol = safe_symbol;

	return( 1 );
}