Coverage Report

Created: 2026-08-31 07:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libpff/libpff/libpff_deflate.c
Line
Count
Source
1
/*
2
 * Deflate (zlib) (un)compression functions
3
 *
4
 * Copyright (C) 2008-2026, 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 <byte_stream.h>
24
#include <memory.h>
25
#include <types.h>
26
27
#include "libpff_bit_stream.h"
28
#include "libpff_deflate.h"
29
#include "libpff_huffman_tree.h"
30
#include "libpff_libcerror.h"
31
#include "libpff_libcnotify.h"
32
33
const uint8_t libpff_deflate_code_sizes_sequence[ 19 ]  = {
34
  16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2,
35
        14, 1, 15 };
36
37
const uint16_t libpff_deflate_literal_codes_base[ 29 ] = {
38
  3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
39
  35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258 };
40
41
const uint16_t libpff_deflate_literal_codes_number_of_extra_bits[ 29 ] = {
42
  0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
43
  3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0 };
44
45
const uint16_t libpff_deflate_distance_codes_base[ 30 ] = {
46
  1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
47
  257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193,
48
  12289, 16385, 24577 };
49
50
const uint16_t libpff_deflate_distance_codes_number_of_extra_bits[ 30 ] = {
51
  0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
52
  7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13 };
53
54
/* Initializes the dynamic Huffman trees
55
 * Returns 1 on success or -1 on error
56
 */
57
int libpff_deflate_build_dynamic_huffman_trees(
58
     libpff_bit_stream_t *bit_stream,
59
     libpff_huffman_tree_t *literals_tree,
60
     libpff_huffman_tree_t *distances_tree,
61
     libcerror_error_t **error )
62
0
{
63
0
  uint8_t code_size_array[ 316 ];
64
65
0
  libpff_huffman_tree_t *codes_tree = NULL;
66
0
  static char *function             = "libpff_deflate_build_dynamic_huffman_trees";
67
0
  uint32_t code_size                = 0;
68
0
  uint32_t code_size_index          = 0;
69
0
  uint32_t code_size_sequence       = 0;
70
0
  uint32_t number_of_code_sizes     = 0;
71
0
  uint32_t number_of_distance_codes = 0;
72
0
  uint32_t number_of_literal_codes  = 0;
73
0
  uint32_t times_to_repeat          = 0;
74
0
  uint16_t symbol                   = 0;
75
76
0
  if( libpff_bit_stream_get_value(
77
0
       bit_stream,
78
0
       14,
79
0
       &number_of_code_sizes,
80
0
       error ) != 1 )
81
0
  {
82
0
    libcerror_error_set(
83
0
     error,
84
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
85
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
86
0
     "%s: unable to retrieve value from bit stream.",
87
0
     function );
88
89
0
    goto on_error;
90
0
  }
91
0
  number_of_literal_codes  = number_of_code_sizes & 0x0000001fUL;
92
0
  number_of_code_sizes   >>= 5;
93
0
  number_of_distance_codes = number_of_code_sizes & 0x0000001fUL;
94
0
  number_of_code_sizes   >>= 5;
95
96
0
  number_of_literal_codes += 257;
97
98
0
  if( number_of_literal_codes > 286 )
99
0
  {
100
0
    libcerror_error_set(
101
0
     error,
102
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
103
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
104
0
     "%s: invalid number of literal codes value out of bounds.",
105
0
     function );
106
107
0
    goto on_error;
108
0
  }
109
0
  number_of_distance_codes += 1;
110
111
0
  if( number_of_distance_codes > 30 )
112
0
  {
113
0
    libcerror_error_set(
114
0
     error,
115
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
116
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
117
0
     "%s: invalid number of distance codes value out of bounds.",
118
0
     function );
119
120
0
    goto on_error;
121
0
  }
122
0
  number_of_code_sizes += 4;
123
124
0
  for( code_size_index = 0;
125
0
       code_size_index < number_of_code_sizes;
126
0
       code_size_index++ )
127
0
  {
128
0
    if( libpff_bit_stream_get_value(
129
0
         bit_stream,
130
0
         3,
131
0
         &code_size,
132
0
         error ) != 1 )
133
0
    {
134
0
      libcerror_error_set(
135
0
       error,
136
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
137
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
138
0
       "%s: unable to retrieve value from bit stream.",
139
0
       function );
140
141
0
      goto on_error;
142
0
    }
143
0
    code_size_sequence = libpff_deflate_code_sizes_sequence[ code_size_index ];
144
145
0
    code_size_array[ code_size_sequence ] = (uint8_t) code_size;
146
0
  }
147
0
  while( code_size_index < 19 )
148
0
  {
149
0
    code_size_sequence = libpff_deflate_code_sizes_sequence[ code_size_index++ ];
150
151
0
    code_size_array[ code_size_sequence ] = 0;
152
0
  }
153
0
  if( libpff_huffman_tree_initialize(
154
0
       &codes_tree,
155
0
       19,
156
0
       15,
157
0
       error ) != 1 )
158
0
  {
159
0
    libcerror_error_set(
160
0
     error,
161
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
162
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
163
0
     "%s: unable to build codes tree.",
164
0
     function );
165
166
0
    goto on_error;
167
0
  }
168
0
  if( libpff_huffman_tree_build(
169
0
       codes_tree,
170
0
       code_size_array,
171
0
       19,
172
0
       error ) != 1 )
173
0
  {
174
0
    libcerror_error_set(
175
0
     error,
176
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
177
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
178
0
     "%s: unable to build codes tree.",
179
0
     function );
180
181
0
    goto on_error;
182
0
  }
183
0
  number_of_code_sizes = number_of_literal_codes + number_of_distance_codes;
184
185
0
  code_size_index = 0;
186
187
0
  while( code_size_index < number_of_code_sizes )
188
0
  {
189
0
    if( libpff_huffman_tree_get_symbol_from_bit_stream(
190
0
         codes_tree,
191
0
         bit_stream,
192
0
         &symbol,
193
0
         error ) != 1 )
194
0
    {
195
0
      libcerror_error_set(
196
0
       error,
197
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
198
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
199
0
       "%s: unable to retrieve literal value from bit stream.",
200
0
       function );
201
202
0
      goto on_error;
203
0
    }
204
0
    if( symbol < 16 )
205
0
    {
206
0
      code_size_array[ code_size_index++ ] = (uint8_t) symbol;
207
208
0
      continue;
209
0
    }
210
0
    code_size = 0;
211
212
0
    if( symbol == 16 )
213
0
    {
214
0
      if( code_size_index == 0 )
215
0
      {
216
0
        libcerror_error_set(
217
0
         error,
218
0
         LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
219
0
         LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
220
0
         "%s: invalid code size index value out of bounds.",
221
0
         function );
222
223
0
        goto on_error;
224
0
      }
225
0
      code_size = (uint32_t) code_size_array[ code_size_index - 1 ];
226
227
0
      if( libpff_bit_stream_get_value(
228
0
           bit_stream,
229
0
           2,
230
0
           &times_to_repeat,
231
0
           error ) != 1 )
232
0
      {
233
0
        libcerror_error_set(
234
0
         error,
235
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
236
0
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
237
0
         "%s: unable to retrieve value from bit stream.",
238
0
         function );
239
240
0
        goto on_error;
241
0
      }
242
0
      times_to_repeat += 3;
243
0
    }
244
0
    else if( symbol == 17 )
245
0
    {
246
0
      if( libpff_bit_stream_get_value(
247
0
           bit_stream,
248
0
           3,
249
0
           &times_to_repeat,
250
0
           error ) != 1 )
251
0
      {
252
0
        libcerror_error_set(
253
0
         error,
254
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
255
0
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
256
0
         "%s: unable to retrieve value from bit stream.",
257
0
         function );
258
259
0
        goto on_error;
260
0
      }
261
0
      times_to_repeat += 3;
262
0
    }
263
0
    else if( symbol == 18 )
264
0
    {
265
0
      if( libpff_bit_stream_get_value(
266
0
           bit_stream,
267
0
           7,
268
0
           &times_to_repeat,
269
0
           error ) != 1 )
270
0
      {
271
0
        libcerror_error_set(
272
0
         error,
273
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
274
0
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
275
0
         "%s: unable to retrieve value from bit stream.",
276
0
         function );
277
278
0
        goto on_error;
279
0
      }
280
0
      times_to_repeat += 11;
281
0
    }
282
0
    else
283
0
    {
284
0
      libcerror_error_set(
285
0
       error,
286
0
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
287
0
       LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
288
0
       "%s: invalid code value value out of bounds.",
289
0
       function );
290
291
0
      goto on_error;
292
0
    }
293
0
    if( ( code_size_index + times_to_repeat ) > number_of_code_sizes )
294
0
    {
295
0
      libcerror_error_set(
296
0
       error,
297
0
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
298
0
       LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
299
0
       "%s: invalid times to repeat value out of bounds.",
300
0
       function );
301
302
0
      goto on_error;
303
0
    }
304
0
    while( times_to_repeat > 0 )
305
0
    {
306
0
      code_size_array[ code_size_index++ ] = (uint8_t) code_size;
307
308
0
      times_to_repeat--;
309
0
    }
310
0
  }
311
0
  if( code_size_array[ 256 ] == 0 )
312
0
  {
313
0
    libcerror_error_set(
314
0
     error,
315
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
316
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
317
0
     "%s: end-of-block code value missing in literal codes array.",
318
0
     function );
319
320
0
    goto on_error;
321
0
  }
322
0
  if( libpff_huffman_tree_free(
323
0
       &codes_tree,
324
0
       error ) != 1 )
325
0
  {
326
0
    libcerror_error_set(
327
0
     error,
328
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
329
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
330
0
     "%s: unable to free codes tree.",
331
0
     function );
332
333
0
    goto on_error;
334
0
  }
335
0
  if( libpff_huffman_tree_build(
336
0
       literals_tree,
337
0
       code_size_array,
338
0
       number_of_literal_codes,
339
0
       error ) != 1 )
340
0
  {
341
0
    libcerror_error_set(
342
0
     error,
343
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
344
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
345
0
     "%s: unable to build literals tree.",
346
0
     function );
347
348
0
    goto on_error;
349
0
  }
350
0
  if( libpff_huffman_tree_build(
351
0
       distances_tree,
352
0
       &( code_size_array[ number_of_literal_codes ] ),
353
0
       number_of_distance_codes,
354
0
       error ) != 1 )
355
0
  {
356
0
    libcerror_error_set(
357
0
     error,
358
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
359
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
360
0
     "%s: unable to build distances tree.",
361
0
     function );
362
363
0
    goto on_error;
364
0
  }
365
0
  return( 1 );
366
367
0
on_error:
368
0
  if( codes_tree != NULL )
369
0
  {
370
0
    libpff_huffman_tree_free(
371
0
     &codes_tree,
372
0
     NULL );
373
0
  }
374
0
  return( -1 );
375
0
}
376
377
/* Initializes the fixed Huffman trees
378
 * Returns 1 on success or -1 on error
379
 */
380
int libpff_deflate_build_fixed_huffman_trees(
381
     libpff_huffman_tree_t *literals_tree,
382
     libpff_huffman_tree_t *distances_tree,
383
     libcerror_error_t **error )
384
0
{
385
0
  uint8_t code_size_array[ 318 ];
386
387
0
  static char *function = "libpff_deflate_build_fixed_huffman_trees";
388
0
  uint16_t symbol       = 0;
389
390
0
  for( symbol = 0;
391
0
       symbol < 318;
392
0
       symbol++ )
393
0
  {
394
0
    if( symbol < 144 )
395
0
    {
396
0
      code_size_array[ symbol ] = 8;
397
0
    }
398
0
    else if( symbol < 256 )
399
0
    {
400
0
      code_size_array[ symbol ] = 9;
401
0
    }
402
0
    else if( symbol < 280 )
403
0
    {
404
0
      code_size_array[ symbol ] = 7;
405
0
    }
406
0
    else if( symbol < 288 )
407
0
    {
408
0
      code_size_array[ symbol ] = 8;
409
0
    }
410
0
    else
411
0
    {
412
0
      code_size_array[ symbol ] = 5;
413
0
    }
414
0
  }
415
0
  if( libpff_huffman_tree_build(
416
0
       literals_tree,
417
0
       code_size_array,
418
0
       288,
419
0
       error ) != 1 )
420
0
  {
421
0
    libcerror_error_set(
422
0
     error,
423
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
424
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
425
0
     "%s: unable to build literals tree.",
426
0
     function );
427
428
0
    return( -1 );
429
0
  }
430
0
  if( libpff_huffman_tree_build(
431
0
       distances_tree,
432
0
       &( code_size_array[ 288 ] ),
433
0
       30,
434
0
       error ) != 1 )
435
0
  {
436
0
    libcerror_error_set(
437
0
     error,
438
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
439
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
440
0
     "%s: unable to build distances tree.",
441
0
     function );
442
443
0
    return( -1 );
444
0
  }
445
0
  return( 1 );
446
0
}
447
448
/* Decodes a Huffman compressed block
449
 * Returns 1 on success or -1 on error
450
 */
451
int libpff_deflate_decode_huffman(
452
     libpff_bit_stream_t *bit_stream,
453
     libpff_huffman_tree_t *literals_tree,
454
     libpff_huffman_tree_t *distances_tree,
455
     uint8_t *uncompressed_data,
456
     size_t uncompressed_data_size,
457
     size_t *uncompressed_data_offset,
458
     libcerror_error_t **error )
459
0
{
460
0
  static char *function         = "libpff_deflate_decode_huffman";
461
0
  size_t data_offset            = 0;
462
0
  uint32_t extra_bits           = 0;
463
0
  uint16_t compression_offset   = 0;
464
0
  uint16_t compression_size     = 0;
465
0
  uint16_t number_of_extra_bits = 0;
466
0
  uint16_t symbol               = 0;
467
468
0
  if( uncompressed_data == NULL )
469
0
  {
470
0
    libcerror_error_set(
471
0
     error,
472
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
473
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
474
0
     "%s: invalid uncompressed data.",
475
0
     function );
476
477
0
    return( -1 );
478
0
  }
479
0
  if( uncompressed_data_size > (size_t) SSIZE_MAX )
480
0
  {
481
0
    libcerror_error_set(
482
0
     error,
483
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
484
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
485
0
     "%s: invalid uncompressed data size value exceeds maximum.",
486
0
     function );
487
488
0
    return( -1 );
489
0
  }
490
0
  if( uncompressed_data_offset == NULL )
491
0
  {
492
0
    libcerror_error_set(
493
0
     error,
494
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
495
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
496
0
     "%s: invalid uncompressed data offset.",
497
0
     function );
498
499
0
    return( -1 );
500
0
  }
501
0
  data_offset = *uncompressed_data_offset;
502
503
0
  do
504
0
  {
505
0
    if( libpff_huffman_tree_get_symbol_from_bit_stream(
506
0
         literals_tree,
507
0
         bit_stream,
508
0
         &symbol,
509
0
         error ) != 1 )
510
0
    {
511
0
      libcerror_error_set(
512
0
       error,
513
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
514
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
515
0
       "%s: unable to retrieve literal value from bit stream.",
516
0
       function );
517
518
0
      return( -1 );
519
0
    }
520
0
    if( symbol < 256 )
521
0
    {
522
0
      if( data_offset >= uncompressed_data_size )
523
0
      {
524
0
        libcerror_error_set(
525
0
         error,
526
0
         LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
527
0
         LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
528
0
         "%s: invalid uncompressed data value too small.",
529
0
         function );
530
531
0
        return( -1 );
532
0
      }
533
0
      uncompressed_data[ data_offset++ ] = (uint8_t) symbol;
534
0
    }
535
0
    else if( ( symbol > 256 )
536
0
          && ( symbol < 286 ) )
537
0
    {
538
0
      symbol -= 257;
539
540
0
      number_of_extra_bits = libpff_deflate_literal_codes_number_of_extra_bits[ symbol ];
541
542
0
      if( libpff_bit_stream_get_value(
543
0
           bit_stream,
544
0
           (uint8_t) number_of_extra_bits,
545
0
           &extra_bits,
546
0
           error ) != 1 )
547
0
      {
548
0
        libcerror_error_set(
549
0
         error,
550
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
551
0
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
552
0
         "%s: unable to retrieve literal extra value from bit stream.",
553
0
         function );
554
555
0
        return( -1 );
556
0
      }
557
0
      compression_size = libpff_deflate_literal_codes_base[ symbol ] + (uint16_t) extra_bits;
558
559
0
      if( libpff_huffman_tree_get_symbol_from_bit_stream(
560
0
           distances_tree,
561
0
           bit_stream,
562
0
           &symbol,
563
0
           error ) != 1 )
564
0
      {
565
0
        libcerror_error_set(
566
0
         error,
567
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
568
0
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
569
0
         "%s: unable to retrieve distance value from bit stream.",
570
0
         function );
571
572
0
        return( -1 );
573
0
      }
574
0
      number_of_extra_bits = libpff_deflate_distance_codes_number_of_extra_bits[ symbol ];
575
576
0
      if( libpff_bit_stream_get_value(
577
0
           bit_stream,
578
0
           (uint8_t) number_of_extra_bits,
579
0
           &extra_bits,
580
0
           error ) != 1 )
581
0
      {
582
0
        libcerror_error_set(
583
0
         error,
584
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
585
0
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
586
0
         "%s: unable to retrieve distance extra value from bit stream.",
587
0
         function );
588
589
0
        return( -1 );
590
0
      }
591
0
      compression_offset = libpff_deflate_distance_codes_base[ symbol ] + (uint16_t) extra_bits;
592
593
0
      if( compression_offset > data_offset )
594
0
      {
595
0
        libcerror_error_set(
596
0
         error,
597
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
598
0
         LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
599
0
         "%s: invalid compression offset value out of bounds.",
600
0
         function );
601
602
0
        return( -1 );
603
0
      }
604
0
      if( ( data_offset + compression_size ) > uncompressed_data_size )
605
0
      {
606
0
        libcerror_error_set(
607
0
         error,
608
0
         LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
609
0
         LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
610
0
         "%s: invalid uncompressed data value too small.",
611
0
         function );
612
613
0
        return( -1 );
614
0
      }
615
0
      while( compression_size > 0 )
616
0
      {
617
0
        uncompressed_data[ data_offset ] = uncompressed_data[ data_offset - compression_offset ];
618
619
0
        data_offset++;
620
0
        compression_size--;
621
0
      }
622
0
    }
623
0
    else if( symbol != 256 )
624
0
    {
625
0
      libcerror_error_set(
626
0
       error,
627
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
628
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
629
0
       "%s: invalid code value: %" PRIu16 ".",
630
0
       function,
631
0
       symbol );
632
633
0
      return( -1 );
634
0
    }
635
0
  }
636
0
  while( symbol != 256 );
637
638
0
  *uncompressed_data_offset = data_offset;
639
640
0
  return( 1 );
641
0
}
642
643
/* Calculates the little-endian Adler-32 of a buffer
644
 * It uses the initial value to calculate a new Adler-32
645
 * Returns 1 if successful or -1 on error
646
 */
647
int libpff_deflate_calculate_adler32(
648
     uint32_t *checksum_value,
649
     const uint8_t *data,
650
     size_t data_size,
651
     uint32_t initial_value,
652
     libcerror_error_t **error )
653
0
{
654
0
  static char *function = "libpff_deflate_calculate_adler32";
655
0
  size_t data_offset    = 0;
656
0
  uint32_t lower_word   = 0;
657
0
  uint32_t upper_word   = 0;
658
0
  uint32_t value_32bit  = 0;
659
0
  int block_index       = 0;
660
661
0
  if( checksum_value == NULL )
662
0
  {
663
0
    libcerror_error_set(
664
0
     error,
665
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
666
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
667
0
     "%s: invalid checksum value.",
668
0
     function );
669
670
0
    return( -1 );
671
0
  }
672
0
  if( data == NULL )
673
0
  {
674
0
    libcerror_error_set(
675
0
     error,
676
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
677
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
678
0
     "%s: invalid data.",
679
0
     function );
680
681
0
    return( -1 );
682
0
  }
683
0
  if( data_size > (size_t) SSIZE_MAX )
684
0
  {
685
0
    libcerror_error_set(
686
0
     error,
687
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
688
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
689
0
     "%s: invalid data size value exceeds maximum.",
690
0
     function );
691
692
0
    return( -1 );
693
0
  }
694
0
  lower_word = initial_value & 0xffff;
695
0
  upper_word = ( initial_value >> 16 ) & 0xffff;
696
697
0
  while( data_size >= 0x15b0 )
698
0
  {
699
    /* The modulo calculation is needed per 5552 (0x15b0) bytes
700
     * 5552 / 16 = 347
701
     */
702
0
    for( block_index = 0;
703
0
         block_index < 347;
704
0
         block_index++ )
705
0
    {
706
0
      lower_word += data[ data_offset++ ];
707
0
      upper_word += lower_word;
708
709
0
      lower_word += data[ data_offset++ ];
710
0
      upper_word += lower_word;
711
712
0
      lower_word += data[ data_offset++ ];
713
0
      upper_word += lower_word;
714
715
0
      lower_word += data[ data_offset++ ];
716
0
      upper_word += lower_word;
717
718
0
      lower_word += data[ data_offset++ ];
719
0
      upper_word += lower_word;
720
721
0
      lower_word += data[ data_offset++ ];
722
0
      upper_word += lower_word;
723
724
0
      lower_word += data[ data_offset++ ];
725
0
      upper_word += lower_word;
726
727
0
      lower_word += data[ data_offset++ ];
728
0
      upper_word += lower_word;
729
730
0
      lower_word += data[ data_offset++ ];
731
0
      upper_word += lower_word;
732
733
0
      lower_word += data[ data_offset++ ];
734
0
      upper_word += lower_word;
735
736
0
      lower_word += data[ data_offset++ ];
737
0
      upper_word += lower_word;
738
739
0
      lower_word += data[ data_offset++ ];
740
0
      upper_word += lower_word;
741
742
0
      lower_word += data[ data_offset++ ];
743
0
      upper_word += lower_word;
744
745
0
      lower_word += data[ data_offset++ ];
746
0
      upper_word += lower_word;
747
748
0
      lower_word += data[ data_offset++ ];
749
0
      upper_word += lower_word;
750
751
0
      lower_word += data[ data_offset++ ];
752
0
      upper_word += lower_word;
753
0
    }
754
    /* Optimized equivalent of:
755
     * lower_word %= 0xfff1
756
     */
757
0
    value_32bit = lower_word >> 16;
758
0
    lower_word &= 0x0000ffffUL;
759
0
    lower_word += ( value_32bit << 4 ) - value_32bit;
760
761
0
    if( lower_word > 65521 )
762
0
    {
763
0
      value_32bit = lower_word >> 16;
764
0
      lower_word &= 0x0000ffffUL;
765
0
      lower_word += ( value_32bit << 4 ) - value_32bit;
766
0
    }
767
0
    if( lower_word >= 65521 )
768
0
    {
769
0
      lower_word -= 65521;
770
0
    }
771
    /* Optimized equivalent of:
772
     * upper_word %= 0xfff1
773
     */
774
0
    value_32bit = upper_word >> 16;
775
0
    upper_word &= 0x0000ffffUL;
776
0
    upper_word += ( value_32bit << 4 ) - value_32bit;
777
778
0
    if( upper_word > 65521 )
779
0
    {
780
0
      value_32bit = upper_word >> 16;
781
0
      upper_word &= 0x0000ffffUL;
782
0
      upper_word += ( value_32bit << 4 ) - value_32bit;
783
0
    }
784
0
    if( upper_word >= 65521 )
785
0
    {
786
0
      upper_word -= 65521;
787
0
    }
788
0
    data_size -= 0x15b0;
789
0
  }
790
0
  if( data_size > 0 )
791
0
  {
792
0
    while( data_size > 16 )
793
0
    {
794
0
      lower_word += data[ data_offset++ ];
795
0
      upper_word += lower_word;
796
797
0
      lower_word += data[ data_offset++ ];
798
0
      upper_word += lower_word;
799
800
0
      lower_word += data[ data_offset++ ];
801
0
      upper_word += lower_word;
802
803
0
      lower_word += data[ data_offset++ ];
804
0
      upper_word += lower_word;
805
806
0
      lower_word += data[ data_offset++ ];
807
0
      upper_word += lower_word;
808
809
0
      lower_word += data[ data_offset++ ];
810
0
      upper_word += lower_word;
811
812
0
      lower_word += data[ data_offset++ ];
813
0
      upper_word += lower_word;
814
815
0
      lower_word += data[ data_offset++ ];
816
0
      upper_word += lower_word;
817
818
0
      lower_word += data[ data_offset++ ];
819
0
      upper_word += lower_word;
820
821
0
      lower_word += data[ data_offset++ ];
822
0
      upper_word += lower_word;
823
824
0
      lower_word += data[ data_offset++ ];
825
0
      upper_word += lower_word;
826
827
0
      lower_word += data[ data_offset++ ];
828
0
      upper_word += lower_word;
829
830
0
      lower_word += data[ data_offset++ ];
831
0
      upper_word += lower_word;
832
833
0
      lower_word += data[ data_offset++ ];
834
0
      upper_word += lower_word;
835
836
0
      lower_word += data[ data_offset++ ];
837
0
      upper_word += lower_word;
838
839
0
      lower_word += data[ data_offset++ ];
840
0
      upper_word += lower_word;
841
842
0
      data_size -= 16;
843
0
    }
844
0
    while( data_size > 0 )
845
0
    {
846
0
      lower_word += data[ data_offset++ ];
847
0
      upper_word += lower_word;
848
849
0
      data_size--;
850
0
    }
851
    /* Optimized equivalent of:
852
     * lower_word %= 0xfff1
853
     */
854
0
    value_32bit = lower_word >> 16;
855
0
    lower_word &= 0x0000ffffUL;
856
0
    lower_word += ( value_32bit << 4 ) - value_32bit;
857
858
0
    if( lower_word > 65521 )
859
0
    {
860
0
      value_32bit = lower_word >> 16;
861
0
      lower_word &= 0x0000ffffUL;
862
0
      lower_word += ( value_32bit << 4 ) - value_32bit;
863
0
    }
864
0
    if( lower_word >= 65521 )
865
0
    {
866
0
      lower_word -= 65521;
867
0
    }
868
    /* Optimized equivalent of:
869
     * upper_word %= 0xfff1
870
     */
871
0
    value_32bit = upper_word >> 16;
872
0
    upper_word &= 0x0000ffffUL;
873
0
    upper_word += ( value_32bit << 4 ) - value_32bit;
874
875
0
    if( upper_word > 65521 )
876
0
    {
877
0
      value_32bit = upper_word >> 16;
878
0
      upper_word &= 0x0000ffffUL;
879
0
      upper_word += ( value_32bit << 4 ) - value_32bit;
880
0
    }
881
0
    if( upper_word >= 65521 )
882
0
    {
883
0
      upper_word -= 65521;
884
0
    }
885
0
  }
886
0
  *checksum_value = ( upper_word << 16 ) | lower_word;
887
888
0
  return( 1 );
889
0
}
890
891
/* Reads the compressed data header
892
 * Returns 1 on success or -1 on error
893
 */
894
int libpff_deflate_read_data_header(
895
     const uint8_t *compressed_data,
896
     size_t compressed_data_size,
897
     size_t *compressed_data_offset,
898
     libcerror_error_t **error )
899
0
{
900
0
  static char *function                 = "libpff_deflate_read_data_header";
901
0
  size_t safe_offset                    = 0;
902
0
  uint32_t compression_window_size      = 0;
903
0
  uint8_t flags                         = 0;
904
0
  uint8_t compression_data              = 0;
905
0
  uint8_t compression_information       = 0;
906
0
  uint8_t compression_method            = 0;
907
0
  uint8_t compression_window_bits       = 0;
908
909
#if defined( HAVE_DEBUG_OUTPUT )
910
  uint32_t preset_dictionary_identifier = 0;
911
#endif
912
913
0
  if( compressed_data == NULL )
914
0
  {
915
0
    libcerror_error_set(
916
0
     error,
917
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
918
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
919
0
     "%s: invalid compressed data.",
920
0
     function );
921
922
0
    return( -1 );
923
0
  }
924
0
  if( compressed_data_size > (size_t) SSIZE_MAX )
925
0
  {
926
0
    libcerror_error_set(
927
0
     error,
928
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
929
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
930
0
     "%s: invalid compressed data size value exceeds maximum.",
931
0
     function );
932
933
0
    return( -1 );
934
0
  }
935
0
  if( compressed_data_offset == NULL )
936
0
  {
937
0
    libcerror_error_set(
938
0
     error,
939
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
940
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
941
0
     "%s: invalid compressed data offset.",
942
0
     function );
943
944
0
    return( -1 );
945
0
  }
946
0
  safe_offset = *compressed_data_offset;
947
948
0
  if( ( compressed_data_size < 2 )
949
0
   || ( safe_offset > ( compressed_data_size - 2 ) ) )
950
0
  {
951
0
    libcerror_error_set(
952
0
     error,
953
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
954
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
955
0
     "%s: invalid compressed data value too small.",
956
0
     function );
957
958
0
    return( -1 );
959
0
  }
960
0
  compression_data = compressed_data[ safe_offset++ ];
961
0
  flags            = compressed_data[ safe_offset++ ];
962
963
0
  compression_method      = compression_data & 0x0f;
964
0
  compression_information = compression_data >> 4;
965
966
/* TODO validate check bits */
967
0
  if( ( flags & 0x20 ) != 0 )
968
0
  {
969
0
    if( ( compressed_data_size < 6 )
970
0
     || ( safe_offset > ( compressed_data_size - 6 ) ) )
971
0
    {
972
0
      libcerror_error_set(
973
0
       error,
974
0
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
975
0
       LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
976
0
       "%s: invalid compressed data value too small.",
977
0
       function );
978
979
0
      return( -1 );
980
0
    }
981
#if defined( HAVE_DEBUG_OUTPUT )
982
    byte_stream_copy_to_uint32_big_endian(
983
     &( compressed_data[ 2 ] ),
984
     preset_dictionary_identifier );
985
#endif
986
0
    safe_offset += 4;
987
0
  }
988
0
  if( compression_method != 8 )
989
0
  {
990
0
    libcerror_error_set(
991
0
     error,
992
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
993
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
994
0
     "%s: unsupported compression method: %" PRIu8 ".",
995
0
     function,
996
0
     compression_method );
997
998
0
    return( -1 );
999
0
  }
1000
0
  compression_window_bits = (uint8_t) compression_information + 8;
1001
0
  compression_window_size = (uint32_t) 1UL << compression_window_bits;
1002
1003
0
  if( compression_window_size > 32768 )
1004
0
  {
1005
0
    libcerror_error_set(
1006
0
     error,
1007
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1008
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
1009
0
     "%s: unsupported compression window size: %" PRIu32 ".",
1010
0
     function,
1011
0
     compression_window_size );
1012
1013
0
    return( -1 );
1014
0
  }
1015
0
  *compressed_data_offset += safe_offset;
1016
1017
0
  return( 1 );
1018
0
}
1019
1020
/* Reads the header of a block of compressed data
1021
 * Returns 1 on success or -1 on error
1022
 */
1023
int libpff_deflate_read_block_header(
1024
     libpff_bit_stream_t *bit_stream,
1025
     uint8_t *block_type,
1026
     uint8_t *last_block_flag,
1027
     libcerror_error_t **error )
1028
0
{
1029
0
  static char *function = "libpff_deflate_read_block_header";
1030
0
  uint32_t value_32bit  = 0;
1031
1032
0
  if( block_type == NULL )
1033
0
  {
1034
0
    libcerror_error_set(
1035
0
     error,
1036
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1037
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1038
0
     "%s: invalid block type.",
1039
0
     function );
1040
1041
0
    return( -1 );
1042
0
  }
1043
0
  if( last_block_flag == NULL )
1044
0
  {
1045
0
    libcerror_error_set(
1046
0
     error,
1047
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1048
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1049
0
     "%s: invalid last block flag.",
1050
0
     function );
1051
1052
0
    return( -1 );
1053
0
  }
1054
0
  if( libpff_bit_stream_get_value(
1055
0
       bit_stream,
1056
0
       3,
1057
0
       &value_32bit,
1058
0
       error ) != 1 )
1059
0
  {
1060
0
    libcerror_error_set(
1061
0
     error,
1062
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1063
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1064
0
     "%s: unable to retrieve value from bit stream.",
1065
0
     function );
1066
1067
0
    return( -1 );
1068
0
  }
1069
0
  *last_block_flag = (uint8_t) ( value_32bit & 0x00000001UL );
1070
0
  value_32bit    >>= 1;
1071
0
  *block_type      = (uint8_t) value_32bit;
1072
1073
#if defined( HAVE_DEBUG_OUTPUT )
1074
  if( libcnotify_verbose != 0 )
1075
  {
1076
    libcnotify_printf(
1077
     "%s: block header last block flag\t\t\t: %" PRIu8 "\n",
1078
     function,
1079
     *last_block_flag );
1080
1081
    libcnotify_printf(
1082
     "%s: block header block type\t\t\t: %" PRIu8 " (",
1083
     function,
1084
     *block_type );
1085
1086
    switch( *block_type )
1087
    {
1088
      case LIBPFF_DEFLATE_BLOCK_TYPE_UNCOMPRESSED:
1089
        libcnotify_printf(
1090
         "Uncompressed" );
1091
        break;
1092
1093
      case LIBPFF_DEFLATE_BLOCK_TYPE_HUFFMAN_FIXED:
1094
        libcnotify_printf(
1095
         "Fixed Huffman" );
1096
        break;
1097
1098
      case LIBPFF_DEFLATE_BLOCK_TYPE_HUFFMAN_DYNAMIC:
1099
        libcnotify_printf(
1100
         "Dynamic Huffman" );
1101
        break;
1102
1103
      case LIBPFF_DEFLATE_BLOCK_TYPE_RESERVED:
1104
      default:
1105
        libcnotify_printf(
1106
         "Reserved" );
1107
        break;
1108
    }
1109
    libcnotify_printf(
1110
     ")\n" );
1111
1112
    libcnotify_printf(
1113
     "\n" );
1114
  }
1115
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
1116
1117
0
  return( 1 );
1118
0
}
1119
1120
/* Reads a block of compressed data
1121
 * Returns 1 on success or -1 on error
1122
 */
1123
int libpff_deflate_read_block(
1124
     libpff_bit_stream_t *bit_stream,
1125
     uint8_t block_type,
1126
     libpff_huffman_tree_t *fixed_huffman_literals_tree,
1127
     libpff_huffman_tree_t *fixed_huffman_distances_tree,
1128
     uint8_t *uncompressed_data,
1129
     size_t uncompressed_data_size,
1130
     size_t *uncompressed_data_offset,
1131
     libcerror_error_t **error )
1132
0
{
1133
0
  libpff_huffman_tree_t *dynamic_huffman_distances_tree = NULL;
1134
0
  libpff_huffman_tree_t *dynamic_huffman_literals_tree  = NULL;
1135
0
  static char *function                                 = "libpff_deflate_read_block";
1136
0
  size_t safe_uncompressed_data_offset                  = 0;
1137
0
  uint32_t block_size                                   = 0;
1138
0
  uint32_t block_size_copy                              = 0;
1139
0
  uint32_t value_32bit                                  = 0;
1140
0
  uint8_t skip_bits                                     = 0;
1141
1142
0
  if( bit_stream == NULL )
1143
0
  {
1144
0
    libcerror_error_set(
1145
0
     error,
1146
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1147
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1148
0
     "%s: invalid bit stream.",
1149
0
     function );
1150
1151
0
    return( -1 );
1152
0
  }
1153
0
  if( uncompressed_data == NULL )
1154
0
  {
1155
0
    libcerror_error_set(
1156
0
     error,
1157
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1158
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1159
0
     "%s: invalid uncompressed data.",
1160
0
     function );
1161
1162
0
    return( -1 );
1163
0
  }
1164
0
  if( uncompressed_data_size > (size_t) SSIZE_MAX )
1165
0
  {
1166
0
    libcerror_error_set(
1167
0
     error,
1168
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1169
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1170
0
     "%s: invalid uncompressed data size value exceeds maximum.",
1171
0
     function );
1172
1173
0
    return( -1 );
1174
0
  }
1175
0
  if( uncompressed_data_offset == NULL )
1176
0
  {
1177
0
    libcerror_error_set(
1178
0
     error,
1179
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1180
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1181
0
     "%s: invalid uncompressed data.",
1182
0
     function );
1183
1184
0
    return( -1 );
1185
0
  }
1186
0
  safe_uncompressed_data_offset = *uncompressed_data_offset;
1187
1188
0
  switch( block_type )
1189
0
  {
1190
0
    case LIBPFF_DEFLATE_BLOCK_TYPE_UNCOMPRESSED:
1191
      /* Ignore the bits in the buffer upto the next byte
1192
       */
1193
0
      skip_bits = bit_stream->bit_buffer_size & 0x07;
1194
1195
0
      if( skip_bits > 0 )
1196
0
      {
1197
0
        if( libpff_bit_stream_get_value(
1198
0
             bit_stream,
1199
0
             skip_bits,
1200
0
             &value_32bit,
1201
0
             error ) != 1 )
1202
0
        {
1203
0
          libcerror_error_set(
1204
0
           error,
1205
0
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
1206
0
           LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1207
0
           "%s: unable to retrieve value from bit stream.",
1208
0
           function );
1209
1210
0
          goto on_error;
1211
0
        }
1212
0
      }
1213
0
      if( libpff_bit_stream_get_value(
1214
0
           bit_stream,
1215
0
           32,
1216
0
           &block_size,
1217
0
           error ) != 1 )
1218
0
      {
1219
0
        libcerror_error_set(
1220
0
         error,
1221
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1222
0
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1223
0
         "%s: unable to retrieve value from bit stream.",
1224
0
         function );
1225
1226
0
        goto on_error;
1227
0
      }
1228
0
      block_size_copy = ( block_size >> 16 ) ^ 0x0000ffffUL;
1229
0
      block_size     &= 0x0000ffffUL;
1230
1231
0
      if( block_size != block_size_copy )
1232
0
      {
1233
0
        libcerror_error_set(
1234
0
         error,
1235
0
         LIBCERROR_ERROR_DOMAIN_INPUT,
1236
0
         LIBCERROR_INPUT_ERROR_VALUE_MISMATCH,
1237
0
         "%s: mismatch in block size ( %" PRIu32 " != %" PRIu32 " ).",
1238
0
         function,
1239
0
         block_size,
1240
0
         block_size_copy );
1241
1242
0
        goto on_error;
1243
0
      }
1244
0
      if( block_size == 0 )
1245
0
      {
1246
0
        break;
1247
0
      }
1248
0
      if( (size_t) block_size > ( bit_stream->byte_stream_size - bit_stream->byte_stream_offset ) )
1249
0
      {
1250
0
        libcerror_error_set(
1251
0
         error,
1252
0
         LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1253
0
         LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
1254
0
         "%s: invalid compressed data value too small.",
1255
0
         function );
1256
1257
0
        goto on_error;
1258
0
      }
1259
0
      if( (size_t) block_size > ( uncompressed_data_size - safe_uncompressed_data_offset ) )
1260
0
      {
1261
0
        libcerror_error_set(
1262
0
         error,
1263
0
         LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1264
0
         LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
1265
0
         "%s: invalid uncompressed data value too small.",
1266
0
         function );
1267
1268
0
        goto on_error;
1269
0
      }
1270
0
      if( memory_copy(
1271
0
           &( uncompressed_data[ safe_uncompressed_data_offset ] ),
1272
0
           &( bit_stream->byte_stream[ bit_stream->byte_stream_offset ] ),
1273
0
           (size_t) block_size ) == NULL )
1274
0
      {
1275
0
        libcerror_error_set(
1276
0
         error,
1277
0
         LIBCERROR_ERROR_DOMAIN_MEMORY,
1278
0
         LIBCERROR_MEMORY_ERROR_COPY_FAILED,
1279
0
         "%s: unable to initialize lz buffer.",
1280
0
         function );
1281
1282
0
        goto on_error;
1283
0
      }
1284
0
      bit_stream->byte_stream_offset += block_size;
1285
0
      safe_uncompressed_data_offset  += block_size;
1286
1287
      /* Flush the bit stream buffer
1288
       */
1289
0
      bit_stream->bit_buffer      = 0;
1290
0
      bit_stream->bit_buffer_size = 0;
1291
1292
0
      break;
1293
1294
0
    case LIBPFF_DEFLATE_BLOCK_TYPE_HUFFMAN_FIXED:
1295
0
      if( libpff_deflate_decode_huffman(
1296
0
           bit_stream,
1297
0
           fixed_huffman_literals_tree,
1298
0
           fixed_huffman_distances_tree,
1299
0
           uncompressed_data,
1300
0
           uncompressed_data_size,
1301
0
           &safe_uncompressed_data_offset,
1302
0
           error ) != 1 )
1303
0
      {
1304
0
        libcerror_error_set(
1305
0
         error,
1306
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1307
0
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1308
0
         "%s: unable to decode fixed Huffman encoded bit stream.",
1309
0
         function );
1310
1311
0
        goto on_error;
1312
0
      }
1313
0
      break;
1314
1315
0
    case LIBPFF_DEFLATE_BLOCK_TYPE_HUFFMAN_DYNAMIC:
1316
0
      if( libpff_huffman_tree_initialize(
1317
0
           &dynamic_huffman_literals_tree,
1318
0
           288,
1319
0
           15,
1320
0
           error ) != 1 )
1321
0
      {
1322
0
        libcerror_error_set(
1323
0
         error,
1324
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1325
0
         LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1326
0
         "%s: unable to build dynamic literals Huffman tree.",
1327
0
         function );
1328
1329
0
        goto on_error;
1330
0
      }
1331
0
      if( libpff_huffman_tree_initialize(
1332
0
           &dynamic_huffman_distances_tree,
1333
0
           30,
1334
0
           15,
1335
0
           error ) != 1 )
1336
0
      {
1337
0
        libcerror_error_set(
1338
0
         error,
1339
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1340
0
         LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1341
0
         "%s: unable to build dynamic distances Huffman tree.",
1342
0
         function );
1343
1344
0
        goto on_error;
1345
0
      }
1346
0
      if( libpff_deflate_build_dynamic_huffman_trees(
1347
0
           bit_stream,
1348
0
           dynamic_huffman_literals_tree,
1349
0
           dynamic_huffman_distances_tree,
1350
0
           error ) != 1 )
1351
0
      {
1352
0
        libcerror_error_set(
1353
0
         error,
1354
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1355
0
         LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1356
0
         "%s: unable to build dynamic Huffman trees.",
1357
0
         function );
1358
1359
0
        goto on_error;
1360
0
      }
1361
0
      if( libpff_deflate_decode_huffman(
1362
0
           bit_stream,
1363
0
           dynamic_huffman_literals_tree,
1364
0
           dynamic_huffman_distances_tree,
1365
0
           uncompressed_data,
1366
0
           uncompressed_data_size,
1367
0
           &safe_uncompressed_data_offset,
1368
0
           error ) != 1 )
1369
0
      {
1370
0
        libcerror_error_set(
1371
0
         error,
1372
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1373
0
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1374
0
         "%s: unable to decode dynamic Huffman encoded bit stream.",
1375
0
         function );
1376
1377
0
        goto on_error;
1378
0
      }
1379
0
      if( libpff_huffman_tree_free(
1380
0
           &dynamic_huffman_distances_tree,
1381
0
           error ) != 1 )
1382
0
      {
1383
0
        libcerror_error_set(
1384
0
         error,
1385
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1386
0
         LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1387
0
         "%s: unable to free dynamic distances Huffman tree.",
1388
0
         function );
1389
1390
0
        goto on_error;
1391
0
      }
1392
0
      if( libpff_huffman_tree_free(
1393
0
           &dynamic_huffman_literals_tree,
1394
0
           error ) != 1 )
1395
0
      {
1396
0
        libcerror_error_set(
1397
0
         error,
1398
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1399
0
         LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1400
0
         "%s: unable to free dynamic literals Huffman tree.",
1401
0
         function );
1402
1403
0
        goto on_error;
1404
0
      }
1405
0
      break;
1406
1407
0
    case LIBPFF_DEFLATE_BLOCK_TYPE_RESERVED:
1408
0
    default:
1409
0
      libcerror_error_set(
1410
0
       error,
1411
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1412
0
       LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
1413
0
       "%s: unsupported block type.",
1414
0
       function );
1415
1416
0
      goto on_error;
1417
0
  }
1418
0
  *uncompressed_data_offset = safe_uncompressed_data_offset;
1419
1420
0
  return( 1 );
1421
1422
0
on_error:
1423
0
  if( dynamic_huffman_distances_tree != NULL )
1424
0
  {
1425
0
    libpff_huffman_tree_free(
1426
0
     &dynamic_huffman_distances_tree,
1427
0
     NULL );
1428
0
  }
1429
0
  if( dynamic_huffman_literals_tree != NULL )
1430
0
  {
1431
0
    libpff_huffman_tree_free(
1432
0
     &dynamic_huffman_literals_tree,
1433
0
     NULL );
1434
0
  }
1435
0
  return( -1 );
1436
0
}
1437
1438
/* Decompresses data using deflate compression
1439
 * Returns 1 on success or -1 on error
1440
 */
1441
int libpff_deflate_decompress(
1442
     const uint8_t *compressed_data,
1443
     size_t compressed_data_size,
1444
     uint8_t *uncompressed_data,
1445
     size_t *uncompressed_data_size,
1446
     libcerror_error_t **error )
1447
0
{
1448
0
  libpff_bit_stream_t *bit_stream                     = NULL;
1449
0
  libpff_huffman_tree_t *fixed_huffman_distances_tree = NULL;
1450
0
  libpff_huffman_tree_t *fixed_huffman_literals_tree  = NULL;
1451
0
  static char *function                               = "libpff_deflate_decompress";
1452
0
  size_t compressed_data_offset                       = 0;
1453
0
  size_t safe_uncompressed_data_size                  = 0;
1454
0
  size_t uncompressed_data_offset                     = 0;
1455
0
  uint8_t block_type                                  = 0;
1456
0
  uint8_t last_block_flag                             = 0;
1457
1458
0
  if( compressed_data == NULL )
1459
0
  {
1460
0
    libcerror_error_set(
1461
0
     error,
1462
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1463
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1464
0
     "%s: invalid compressed data.",
1465
0
     function );
1466
1467
0
    return( -1 );
1468
0
  }
1469
0
  if( compressed_data_size > (size_t) SSIZE_MAX )
1470
0
  {
1471
0
    libcerror_error_set(
1472
0
     error,
1473
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1474
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1475
0
     "%s: invalid compressed data size value exceeds maximum.",
1476
0
     function );
1477
1478
0
    return( -1 );
1479
0
  }
1480
0
  if( uncompressed_data == NULL )
1481
0
  {
1482
0
    libcerror_error_set(
1483
0
     error,
1484
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1485
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1486
0
     "%s: invalid uncompressed data.",
1487
0
     function );
1488
1489
0
    return( -1 );
1490
0
  }
1491
0
  if( uncompressed_data_size == NULL )
1492
0
  {
1493
0
    libcerror_error_set(
1494
0
     error,
1495
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1496
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1497
0
     "%s: invalid uncompressed data size.",
1498
0
     function );
1499
1500
0
    return( -1 );
1501
0
  }
1502
0
  safe_uncompressed_data_size = *uncompressed_data_size;
1503
1504
0
  if( safe_uncompressed_data_size > (size_t) SSIZE_MAX )
1505
0
  {
1506
0
    libcerror_error_set(
1507
0
     error,
1508
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1509
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1510
0
     "%s: invalid uncompressed data size value exceeds maximum.",
1511
0
     function );
1512
1513
0
    return( -1 );
1514
0
  }
1515
0
  if( compressed_data_offset >= compressed_data_size )
1516
0
  {
1517
0
    libcerror_error_set(
1518
0
     error,
1519
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1520
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
1521
0
     "%s: invalid compressed data value too small.",
1522
0
     function );
1523
1524
0
    return( -1 );
1525
0
  }
1526
0
  if( libpff_bit_stream_initialize(
1527
0
       &bit_stream,
1528
0
       compressed_data,
1529
0
       compressed_data_size,
1530
0
       compressed_data_offset,
1531
0
       LIBPFF_BIT_STREAM_STORAGE_TYPE_BYTE_BACK_TO_FRONT,
1532
0
       error ) != 1 )
1533
0
  {
1534
0
    libcerror_error_set(
1535
0
     error,
1536
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1537
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1538
0
     "%s: unable to create bit stream.",
1539
0
     function );
1540
1541
0
    goto on_error;
1542
0
  }
1543
0
  while( bit_stream->byte_stream_offset < bit_stream->byte_stream_size )
1544
0
  {
1545
0
    if( libpff_deflate_read_block_header(
1546
0
         bit_stream,
1547
0
         &block_type,
1548
0
         &last_block_flag,
1549
0
         error ) != 1 )
1550
0
    {
1551
0
      libcerror_error_set(
1552
0
       error,
1553
0
       LIBCERROR_ERROR_DOMAIN_IO,
1554
0
       LIBCERROR_IO_ERROR_READ_FAILED,
1555
0
       "%s: unable to read compressed data block header.",
1556
0
       function );
1557
1558
0
      goto on_error;
1559
0
    }
1560
0
    if( block_type == LIBPFF_DEFLATE_BLOCK_TYPE_HUFFMAN_FIXED )
1561
0
    {
1562
0
      if( ( fixed_huffman_literals_tree == NULL )
1563
0
       && ( fixed_huffman_distances_tree == NULL ) )
1564
0
      {
1565
0
        if( libpff_huffman_tree_initialize(
1566
0
             &fixed_huffman_literals_tree,
1567
0
             288,
1568
0
             15,
1569
0
             error ) != 1 )
1570
0
        {
1571
0
          libcerror_error_set(
1572
0
           error,
1573
0
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
1574
0
           LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1575
0
           "%s: unable to build fixed literals Huffman tree.",
1576
0
           function );
1577
1578
0
          goto on_error;
1579
0
        }
1580
0
        if( libpff_huffman_tree_initialize(
1581
0
             &fixed_huffman_distances_tree,
1582
0
             30,
1583
0
             15,
1584
0
             error ) != 1 )
1585
0
        {
1586
0
          libcerror_error_set(
1587
0
           error,
1588
0
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
1589
0
           LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1590
0
           "%s: unable to build fixed distances Huffman tree.",
1591
0
           function );
1592
1593
0
          goto on_error;
1594
0
        }
1595
0
        if( libpff_deflate_build_fixed_huffman_trees(
1596
0
             fixed_huffman_literals_tree,
1597
0
             fixed_huffman_distances_tree,
1598
0
             error ) != 1 )
1599
0
        {
1600
0
          libcerror_error_set(
1601
0
           error,
1602
0
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
1603
0
           LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1604
0
           "%s: unable to build fixed Huffman trees.",
1605
0
           function );
1606
1607
0
          goto on_error;
1608
0
        }
1609
0
      }
1610
0
    }
1611
0
    if( libpff_deflate_read_block(
1612
0
         bit_stream,
1613
0
         block_type,
1614
0
         fixed_huffman_literals_tree,
1615
0
         fixed_huffman_distances_tree,
1616
0
         uncompressed_data,
1617
0
         safe_uncompressed_data_size,
1618
0
         &uncompressed_data_offset,
1619
0
         error ) != 1 )
1620
0
    {
1621
0
      libcerror_error_set(
1622
0
       error,
1623
0
       LIBCERROR_ERROR_DOMAIN_IO,
1624
0
       LIBCERROR_IO_ERROR_READ_FAILED,
1625
0
       "%s: unable to read block of compressed data.",
1626
0
       function );
1627
1628
0
      goto on_error;
1629
0
    }
1630
0
    if( last_block_flag != 0 )
1631
0
    {
1632
0
      break;
1633
0
    }
1634
0
  }
1635
0
  if( fixed_huffman_distances_tree != NULL )
1636
0
  {
1637
0
    if( libpff_huffman_tree_free(
1638
0
         &fixed_huffman_distances_tree,
1639
0
         error ) != 1 )
1640
0
    {
1641
0
      libcerror_error_set(
1642
0
       error,
1643
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1644
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1645
0
       "%s: unable to free fixed distances Huffman tree.",
1646
0
       function );
1647
1648
0
      goto on_error;
1649
0
    }
1650
0
  }
1651
0
  if( fixed_huffman_literals_tree != NULL )
1652
0
  {
1653
0
    if( libpff_huffman_tree_free(
1654
0
         &fixed_huffman_literals_tree,
1655
0
         error ) != 1 )
1656
0
    {
1657
0
      libcerror_error_set(
1658
0
       error,
1659
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1660
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1661
0
       "%s: unable to free fixed literals Huffman tree.",
1662
0
       function );
1663
1664
0
      goto on_error;
1665
0
    }
1666
0
  }
1667
0
  if( libpff_bit_stream_free(
1668
0
       &bit_stream,
1669
0
       error ) != 1 )
1670
0
  {
1671
0
    libcerror_error_set(
1672
0
     error,
1673
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1674
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1675
0
     "%s: unable to free bit stream.",
1676
0
     function );
1677
1678
0
    goto on_error;
1679
0
  }
1680
0
  *uncompressed_data_size = uncompressed_data_offset;
1681
1682
0
  return( 1 );
1683
1684
0
on_error:
1685
0
  if( fixed_huffman_distances_tree != NULL )
1686
0
  {
1687
0
    libpff_huffman_tree_free(
1688
0
     &fixed_huffman_distances_tree,
1689
0
     NULL );
1690
0
  }
1691
0
  if( fixed_huffman_literals_tree != NULL )
1692
0
  {
1693
0
    libpff_huffman_tree_free(
1694
0
     &fixed_huffman_literals_tree,
1695
0
     NULL );
1696
0
  }
1697
0
  if( bit_stream != NULL )
1698
0
  {
1699
0
    libpff_bit_stream_free(
1700
0
     &bit_stream,
1701
0
     NULL );
1702
0
  }
1703
0
  return( -1 );
1704
0
}
1705
1706
/* Decompresses data using zlib compression
1707
 * Returns 1 on success or -1 on error
1708
 */
1709
int libpff_deflate_decompress_zlib(
1710
     const uint8_t *compressed_data,
1711
     size_t compressed_data_size,
1712
     uint8_t *uncompressed_data,
1713
     size_t *uncompressed_data_size,
1714
     libcerror_error_t **error )
1715
0
{
1716
0
  libpff_bit_stream_t *bit_stream                     = NULL;
1717
0
  libpff_huffman_tree_t *fixed_huffman_distances_tree = NULL;
1718
0
  libpff_huffman_tree_t *fixed_huffman_literals_tree  = NULL;
1719
0
  static char *function                               = "libpff_deflate_decompress_zlib";
1720
0
  size_t compressed_data_offset                       = 0;
1721
0
  size_t safe_uncompressed_data_size                  = 0;
1722
0
  size_t uncompressed_data_offset                     = 0;
1723
0
  uint32_t calculated_checksum                        = 0;
1724
0
  uint32_t stored_checksum                            = 0;
1725
0
  uint8_t block_type                                  = 0;
1726
0
  uint8_t last_block_flag                             = 0;
1727
1728
0
  if( compressed_data == NULL )
1729
0
  {
1730
0
    libcerror_error_set(
1731
0
     error,
1732
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1733
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1734
0
     "%s: invalid compressed data.",
1735
0
     function );
1736
1737
0
    return( -1 );
1738
0
  }
1739
0
  if( compressed_data_size > (size_t) SSIZE_MAX )
1740
0
  {
1741
0
    libcerror_error_set(
1742
0
     error,
1743
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1744
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1745
0
     "%s: invalid compressed data size value exceeds maximum.",
1746
0
     function );
1747
1748
0
    return( -1 );
1749
0
  }
1750
0
  if( uncompressed_data == NULL )
1751
0
  {
1752
0
    libcerror_error_set(
1753
0
     error,
1754
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1755
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1756
0
     "%s: invalid uncompressed data.",
1757
0
     function );
1758
1759
0
    return( -1 );
1760
0
  }
1761
0
  if( uncompressed_data_size == NULL )
1762
0
  {
1763
0
    libcerror_error_set(
1764
0
     error,
1765
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1766
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1767
0
     "%s: invalid uncompressed data size.",
1768
0
     function );
1769
1770
0
    return( -1 );
1771
0
  }
1772
0
  safe_uncompressed_data_size = *uncompressed_data_size;
1773
1774
0
  if( safe_uncompressed_data_size > (size_t) SSIZE_MAX )
1775
0
  {
1776
0
    libcerror_error_set(
1777
0
     error,
1778
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1779
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1780
0
     "%s: invalid uncompressed data size value exceeds maximum.",
1781
0
     function );
1782
1783
0
    return( -1 );
1784
0
  }
1785
0
  if( libpff_deflate_read_data_header(
1786
0
       compressed_data,
1787
0
       compressed_data_size,
1788
0
       &compressed_data_offset,
1789
0
       error ) != 1 )
1790
0
  {
1791
0
    libcerror_error_set(
1792
0
     error,
1793
0
     LIBCERROR_ERROR_DOMAIN_IO,
1794
0
     LIBCERROR_IO_ERROR_READ_FAILED,
1795
0
     "%s: unable to read data header.",
1796
0
     function );
1797
1798
0
    goto on_error;
1799
0
  }
1800
0
  if( compressed_data_offset >= compressed_data_size )
1801
0
  {
1802
0
    libcerror_error_set(
1803
0
     error,
1804
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1805
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
1806
0
     "%s: invalid compressed data value too small.",
1807
0
     function );
1808
1809
0
    goto on_error;
1810
0
  }
1811
0
  if( libpff_bit_stream_initialize(
1812
0
       &bit_stream,
1813
0
       compressed_data,
1814
0
       compressed_data_size,
1815
0
       compressed_data_offset,
1816
0
       LIBPFF_BIT_STREAM_STORAGE_TYPE_BYTE_BACK_TO_FRONT,
1817
0
       error ) != 1 )
1818
0
  {
1819
0
    libcerror_error_set(
1820
0
     error,
1821
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1822
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1823
0
     "%s: unable to create bit stream.",
1824
0
     function );
1825
1826
0
    goto on_error;
1827
0
  }
1828
0
  while( bit_stream->byte_stream_offset < bit_stream->byte_stream_size )
1829
0
  {
1830
0
    if( libpff_deflate_read_block_header(
1831
0
         bit_stream,
1832
0
         &block_type,
1833
0
         &last_block_flag,
1834
0
         error ) != 1 )
1835
0
    {
1836
0
      libcerror_error_set(
1837
0
       error,
1838
0
       LIBCERROR_ERROR_DOMAIN_IO,
1839
0
       LIBCERROR_IO_ERROR_READ_FAILED,
1840
0
       "%s: unable to read compressed data block header.",
1841
0
       function );
1842
1843
0
      goto on_error;
1844
0
    }
1845
0
    if( block_type == LIBPFF_DEFLATE_BLOCK_TYPE_HUFFMAN_FIXED )
1846
0
    {
1847
0
      if( ( fixed_huffman_literals_tree == NULL )
1848
0
       && ( fixed_huffman_distances_tree == NULL ) )
1849
0
      {
1850
0
        if( libpff_huffman_tree_initialize(
1851
0
             &fixed_huffman_literals_tree,
1852
0
             288,
1853
0
             15,
1854
0
             error ) != 1 )
1855
0
        {
1856
0
          libcerror_error_set(
1857
0
           error,
1858
0
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
1859
0
           LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1860
0
           "%s: unable to build fixed literals Huffman tree.",
1861
0
           function );
1862
1863
0
          goto on_error;
1864
0
        }
1865
0
        if( libpff_huffman_tree_initialize(
1866
0
             &fixed_huffman_distances_tree,
1867
0
             30,
1868
0
             15,
1869
0
             error ) != 1 )
1870
0
        {
1871
0
          libcerror_error_set(
1872
0
           error,
1873
0
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
1874
0
           LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1875
0
           "%s: unable to build fixed distances Huffman tree.",
1876
0
           function );
1877
1878
0
          goto on_error;
1879
0
        }
1880
0
        if( libpff_deflate_build_fixed_huffman_trees(
1881
0
             fixed_huffman_literals_tree,
1882
0
             fixed_huffman_distances_tree,
1883
0
             error ) != 1 )
1884
0
        {
1885
0
          libcerror_error_set(
1886
0
           error,
1887
0
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
1888
0
           LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1889
0
           "%s: unable to build fixed Huffman trees.",
1890
0
           function );
1891
1892
0
          goto on_error;
1893
0
        }
1894
0
      }
1895
0
    }
1896
0
    if( libpff_deflate_read_block(
1897
0
         bit_stream,
1898
0
         block_type,
1899
0
         fixed_huffman_literals_tree,
1900
0
         fixed_huffman_distances_tree,
1901
0
         uncompressed_data,
1902
0
         safe_uncompressed_data_size,
1903
0
         &uncompressed_data_offset,
1904
0
         error ) != 1 )
1905
0
    {
1906
0
      libcerror_error_set(
1907
0
       error,
1908
0
       LIBCERROR_ERROR_DOMAIN_IO,
1909
0
       LIBCERROR_IO_ERROR_READ_FAILED,
1910
0
       "%s: unable to read block of compressed data.",
1911
0
       function );
1912
1913
0
      goto on_error;
1914
0
    }
1915
0
    if( last_block_flag != 0 )
1916
0
    {
1917
0
      break;
1918
0
    }
1919
0
  }
1920
0
  if( ( bit_stream->byte_stream_size - bit_stream->byte_stream_offset ) >= 4 )
1921
0
  {
1922
0
    while( bit_stream->bit_buffer_size >= 8 )
1923
0
    {
1924
0
      bit_stream->byte_stream_offset -= 1;
1925
0
      bit_stream->bit_buffer_size    -= 8;
1926
0
    }
1927
0
    byte_stream_copy_to_uint32_big_endian(
1928
0
     &( bit_stream->byte_stream[ bit_stream->byte_stream_offset ] ),
1929
0
     stored_checksum );
1930
1931
0
    if( libpff_deflate_calculate_adler32(
1932
0
         &calculated_checksum,
1933
0
         uncompressed_data,
1934
0
         uncompressed_data_offset,
1935
0
         1,
1936
0
         error ) != 1 )
1937
0
    {
1938
0
      libcerror_error_set(
1939
0
       error,
1940
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1941
0
       LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1942
0
       "%s: unable to calculate checksum.",
1943
0
       function );
1944
1945
0
      goto on_error;
1946
0
    }
1947
0
    if( stored_checksum != calculated_checksum )
1948
0
    {
1949
0
      libcerror_error_set(
1950
0
       error,
1951
0
       LIBCERROR_ERROR_DOMAIN_INPUT,
1952
0
       LIBCERROR_INPUT_ERROR_CHECKSUM_MISMATCH,
1953
0
       "%s: checksum does not match (stored: 0x%08" PRIx32 ", calculated: 0x%08" PRIx32 ").",
1954
0
       function,
1955
0
       stored_checksum,
1956
0
       calculated_checksum );
1957
1958
0
      goto on_error;
1959
0
    }
1960
0
  }
1961
0
  if( fixed_huffman_distances_tree != NULL )
1962
0
  {
1963
0
    if( libpff_huffman_tree_free(
1964
0
         &fixed_huffman_distances_tree,
1965
0
         error ) != 1 )
1966
0
    {
1967
0
      libcerror_error_set(
1968
0
       error,
1969
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1970
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1971
0
       "%s: unable to free fixed distances Huffman tree.",
1972
0
       function );
1973
1974
0
      goto on_error;
1975
0
    }
1976
0
  }
1977
0
  if( fixed_huffman_literals_tree != NULL )
1978
0
  {
1979
0
    if( libpff_huffman_tree_free(
1980
0
         &fixed_huffman_literals_tree,
1981
0
         error ) != 1 )
1982
0
    {
1983
0
      libcerror_error_set(
1984
0
       error,
1985
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1986
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1987
0
       "%s: unable to free fixed literals Huffman tree.",
1988
0
       function );
1989
1990
0
      goto on_error;
1991
0
    }
1992
0
  }
1993
0
  if( libpff_bit_stream_free(
1994
0
       &bit_stream,
1995
0
       error ) != 1 )
1996
0
  {
1997
0
    libcerror_error_set(
1998
0
     error,
1999
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
2000
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
2001
0
     "%s: unable to free bit stream.",
2002
0
     function );
2003
2004
0
    goto on_error;
2005
0
  }
2006
0
  *uncompressed_data_size = uncompressed_data_offset;
2007
2008
0
  return( 1 );
2009
2010
0
on_error:
2011
0
  if( fixed_huffman_distances_tree != NULL )
2012
0
  {
2013
0
    libpff_huffman_tree_free(
2014
0
     &fixed_huffman_distances_tree,
2015
0
     NULL );
2016
0
  }
2017
0
  if( fixed_huffman_literals_tree != NULL )
2018
0
  {
2019
0
    libpff_huffman_tree_free(
2020
0
     &fixed_huffman_literals_tree,
2021
0
     NULL );
2022
0
  }
2023
0
  if( bit_stream != NULL )
2024
0
  {
2025
0
    libpff_bit_stream_free(
2026
0
     &bit_stream,
2027
     NULL );
2028
0
  }
2029
0
  return( -1 );
2030
0
}
2031