Coverage Report

Created: 2025-06-13 07:22

/src/libfshfs/libfshfs/libfshfs_deflate.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Deflate (zlib) (un)compression functions
3
 *
4
 * Copyright (C) 2009-2024, 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 "libfshfs_bit_stream.h"
28
#include "libfshfs_deflate.h"
29
#include "libfshfs_huffman_tree.h"
30
#include "libfshfs_libcerror.h"
31
#include "libfshfs_libcnotify.h"
32
33
const uint8_t libfshfs_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 libfshfs_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 libfshfs_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 libfshfs_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 libfshfs_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 libfshfs_deflate_build_dynamic_huffman_trees(
58
     libfshfs_bit_stream_t *bit_stream,
59
     libfshfs_huffman_tree_t *literals_tree,
60
     libfshfs_huffman_tree_t *distances_tree,
61
     libcerror_error_t **error )
62
0
{
63
0
  uint8_t code_size_array[ 316 ];
64
65
0
  libfshfs_huffman_tree_t *codes_tree = NULL;
66
0
  static char *function              = "libfshfs_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( libfshfs_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( libfshfs_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 = libfshfs_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 = libfshfs_deflate_code_sizes_sequence[ code_size_index++ ];
150
151
0
    code_size_array[ code_size_sequence ] = 0;
152
0
  }
153
0
  if( libfshfs_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( libfshfs_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( libfshfs_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( libfshfs_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( libfshfs_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( libfshfs_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( libfshfs_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( libfshfs_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( libfshfs_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
    libfshfs_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 libfshfs_deflate_build_fixed_huffman_trees(
381
     libfshfs_huffman_tree_t *literals_tree,
382
     libfshfs_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 = "libfshfs_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( libfshfs_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( libfshfs_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 libfshfs_deflate_decode_huffman(
452
     libfshfs_bit_stream_t *bit_stream,
453
     libfshfs_huffman_tree_t *literals_tree,
454
     libfshfs_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         = "libfshfs_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( libfshfs_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 = libfshfs_deflate_literal_codes_number_of_extra_bits[ symbol ];
541
542
0
      if( libfshfs_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 = libfshfs_deflate_literal_codes_base[ symbol ] + (uint16_t) extra_bits;
558
559
0
      if( libfshfs_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 = libfshfs_deflate_distance_codes_number_of_extra_bits[ symbol ];
575
576
0
      if( libfshfs_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 = libfshfs_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 libfshfs_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 = "libfshfs_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 libfshfs_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                 = "libfshfs_deflate_read_data_header";
901
0
  size_t safe_offset                    = 0;
902
0
  uint32_t compression_window_size      = 0;
903
0
  uint32_t preset_dictionary_identifier = 0;
904
0
  uint8_t flags                         = 0;
905
0
  uint8_t compression_data              = 0;
906
0
  uint8_t compression_information       = 0;
907
0
  uint8_t compression_method            = 0;
908
0
  uint8_t compression_window_bits       = 0;
909
910
0
  if( compressed_data == NULL )
911
0
  {
912
0
    libcerror_error_set(
913
0
     error,
914
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
915
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
916
0
     "%s: invalid compressed data.",
917
0
     function );
918
919
0
    return( -1 );
920
0
  }
921
0
  if( compressed_data_size > (size_t) SSIZE_MAX )
922
0
  {
923
0
    libcerror_error_set(
924
0
     error,
925
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
926
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
927
0
     "%s: invalid compressed data size value exceeds maximum.",
928
0
     function );
929
930
0
    return( -1 );
931
0
  }
932
0
  if( compressed_data_offset == NULL )
933
0
  {
934
0
    libcerror_error_set(
935
0
     error,
936
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
937
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
938
0
     "%s: invalid compressed data offset.",
939
0
     function );
940
941
0
    return( -1 );
942
0
  }
943
0
  safe_offset = *compressed_data_offset;
944
945
0
  if( ( compressed_data_size < 2 )
946
0
   || ( safe_offset > ( compressed_data_size - 2 ) ) )
947
0
  {
948
0
    libcerror_error_set(
949
0
     error,
950
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
951
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
952
0
     "%s: invalid compressed data value too small.",
953
0
     function );
954
955
0
    return( -1 );
956
0
  }
957
0
  compression_data = compressed_data[ safe_offset++ ];
958
0
  flags            = compressed_data[ safe_offset++ ];
959
960
0
  compression_method      = compression_data & 0x0f;
961
0
  compression_information = compression_data >> 4;
962
963
/* TODO validate check bits */
964
0
  if( ( flags & 0x20 ) != 0 )
965
0
  {
966
0
    if( ( compressed_data_size < 6 )
967
0
     || ( safe_offset > ( compressed_data_size - 6 ) ) )
968
0
    {
969
0
      libcerror_error_set(
970
0
       error,
971
0
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
972
0
       LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
973
0
       "%s: invalid compressed data value too small.",
974
0
       function );
975
976
0
      return( -1 );
977
0
    }
978
0
    byte_stream_copy_to_uint32_big_endian(
979
0
     &( compressed_data[ 2 ] ),
980
0
     preset_dictionary_identifier );
981
982
0
    safe_offset += 4;
983
0
  }
984
0
  if( compression_method != 8 )
985
0
  {
986
0
    libcerror_error_set(
987
0
     error,
988
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
989
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
990
0
     "%s: unsupported compression method: %" PRIu8 ".",
991
0
     function,
992
0
     compression_method );
993
994
0
    return( -1 );
995
0
  }
996
0
  compression_window_bits = (uint8_t) compression_information + 8;
997
0
  compression_window_size = (uint32_t) 1UL << compression_window_bits;
998
999
0
  if( compression_window_size > 32768 )
1000
0
  {
1001
0
    libcerror_error_set(
1002
0
     error,
1003
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1004
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
1005
0
     "%s: unsupported compression window size: %" PRIu32 ".",
1006
0
     function,
1007
0
     compression_window_size );
1008
1009
0
    return( -1 );
1010
0
  }
1011
0
  *compressed_data_offset += safe_offset;
1012
1013
0
  return( 1 );
1014
0
}
1015
1016
/* Reads the header of a block of compressed data
1017
 * Returns 1 on success or -1 on error
1018
 */
1019
int libfshfs_deflate_read_block_header(
1020
     libfshfs_bit_stream_t *bit_stream,
1021
     uint8_t *block_type,
1022
     uint8_t *last_block_flag,
1023
     libcerror_error_t **error )
1024
0
{
1025
0
  static char *function = "libfshfs_deflate_read_block_header";
1026
0
  uint32_t value_32bit  = 0;
1027
1028
0
  if( block_type == NULL )
1029
0
  {
1030
0
    libcerror_error_set(
1031
0
     error,
1032
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1033
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1034
0
     "%s: invalid block type.",
1035
0
     function );
1036
1037
0
    return( -1 );
1038
0
  }
1039
0
  if( last_block_flag == NULL )
1040
0
  {
1041
0
    libcerror_error_set(
1042
0
     error,
1043
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1044
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1045
0
     "%s: invalid last block flag.",
1046
0
     function );
1047
1048
0
    return( -1 );
1049
0
  }
1050
0
  if( libfshfs_bit_stream_get_value(
1051
0
       bit_stream,
1052
0
       3,
1053
0
       &value_32bit,
1054
0
       error ) != 1 )
1055
0
  {
1056
0
    libcerror_error_set(
1057
0
     error,
1058
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1059
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1060
0
     "%s: unable to retrieve value from bit stream.",
1061
0
     function );
1062
1063
0
    return( -1 );
1064
0
  }
1065
0
  *last_block_flag = (uint8_t) ( value_32bit & 0x00000001UL );
1066
0
  value_32bit    >>= 1;
1067
0
  *block_type      = (uint8_t) value_32bit;
1068
1069
#if defined( HAVE_DEBUG_OUTPUT )
1070
  if( libcnotify_verbose != 0 )
1071
  {
1072
    libcnotify_printf(
1073
     "%s: block header last block flag\t\t\t: %" PRIu8 "\n",
1074
     function,
1075
     *last_block_flag );
1076
1077
    libcnotify_printf(
1078
     "%s: block header block type\t\t\t: %" PRIu8 " (",
1079
     function,
1080
     *block_type );
1081
1082
    switch( *block_type )
1083
    {
1084
      case LIBFSHFS_DEFLATE_BLOCK_TYPE_UNCOMPRESSED:
1085
        libcnotify_printf(
1086
         "Uncompressed" );
1087
        break;
1088
1089
      case LIBFSHFS_DEFLATE_BLOCK_TYPE_HUFFMAN_FIXED:
1090
        libcnotify_printf(
1091
         "Fixed Huffman" );
1092
        break;
1093
1094
      case LIBFSHFS_DEFLATE_BLOCK_TYPE_HUFFMAN_DYNAMIC:
1095
        libcnotify_printf(
1096
         "Dynamic Huffman" );
1097
        break;
1098
1099
      case LIBFSHFS_DEFLATE_BLOCK_TYPE_RESERVED:
1100
      default:
1101
        libcnotify_printf(
1102
         "Reserved" );
1103
        break;
1104
    }
1105
    libcnotify_printf(
1106
     ")\n" );
1107
1108
    libcnotify_printf(
1109
     "\n" );
1110
  }
1111
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
1112
1113
0
  return( 1 );
1114
0
}
1115
1116
/* Reads a block of compressed data
1117
 * Returns 1 on success or -1 on error
1118
 */
1119
int libfshfs_deflate_read_block(
1120
     libfshfs_bit_stream_t *bit_stream,
1121
     uint8_t block_type,
1122
     libfshfs_huffman_tree_t *fixed_huffman_literals_tree,
1123
     libfshfs_huffman_tree_t *fixed_huffman_distances_tree,
1124
     uint8_t *uncompressed_data,
1125
     size_t uncompressed_data_size,
1126
     size_t *uncompressed_data_offset,
1127
     libcerror_error_t **error )
1128
0
{
1129
0
  libfshfs_huffman_tree_t *dynamic_huffman_distances_tree = NULL;
1130
0
  libfshfs_huffman_tree_t *dynamic_huffman_literals_tree  = NULL;
1131
0
  static char *function                                  = "libfshfs_deflate_read_block";
1132
0
  size_t safe_uncompressed_data_offset                   = 0;
1133
0
  uint32_t block_size                                    = 0;
1134
0
  uint32_t block_size_copy                               = 0;
1135
0
  uint32_t value_32bit                                   = 0;
1136
0
  uint8_t skip_bits                                      = 0;
1137
1138
0
  if( bit_stream == NULL )
1139
0
  {
1140
0
    libcerror_error_set(
1141
0
     error,
1142
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1143
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1144
0
     "%s: invalid bit stream.",
1145
0
     function );
1146
1147
0
    return( -1 );
1148
0
  }
1149
0
  if( uncompressed_data == NULL )
1150
0
  {
1151
0
    libcerror_error_set(
1152
0
     error,
1153
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1154
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1155
0
     "%s: invalid uncompressed data.",
1156
0
     function );
1157
1158
0
    return( -1 );
1159
0
  }
1160
0
  if( uncompressed_data_size > (size_t) SSIZE_MAX )
1161
0
  {
1162
0
    libcerror_error_set(
1163
0
     error,
1164
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1165
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1166
0
     "%s: invalid uncompressed data size value exceeds maximum.",
1167
0
     function );
1168
1169
0
    return( -1 );
1170
0
  }
1171
0
  if( uncompressed_data_offset == NULL )
1172
0
  {
1173
0
    libcerror_error_set(
1174
0
     error,
1175
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1176
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1177
0
     "%s: invalid uncompressed data.",
1178
0
     function );
1179
1180
0
    return( -1 );
1181
0
  }
1182
0
  safe_uncompressed_data_offset = *uncompressed_data_offset;
1183
1184
0
  switch( block_type )
1185
0
  {
1186
0
    case LIBFSHFS_DEFLATE_BLOCK_TYPE_UNCOMPRESSED:
1187
      /* Ignore the bits in the buffer upto the next byte
1188
       */
1189
0
      skip_bits = bit_stream->bit_buffer_size & 0x07;
1190
1191
0
      if( skip_bits > 0 )
1192
0
      {
1193
0
        if( libfshfs_bit_stream_get_value(
1194
0
             bit_stream,
1195
0
             skip_bits,
1196
0
             &value_32bit,
1197
0
             error ) != 1 )
1198
0
        {
1199
0
          libcerror_error_set(
1200
0
           error,
1201
0
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
1202
0
           LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1203
0
           "%s: unable to retrieve value from bit stream.",
1204
0
           function );
1205
1206
0
          goto on_error;
1207
0
        }
1208
0
      }
1209
0
      if( libfshfs_bit_stream_get_value(
1210
0
           bit_stream,
1211
0
           32,
1212
0
           &block_size,
1213
0
           error ) != 1 )
1214
0
      {
1215
0
        libcerror_error_set(
1216
0
         error,
1217
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1218
0
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1219
0
         "%s: unable to retrieve value from bit stream.",
1220
0
         function );
1221
1222
0
        goto on_error;
1223
0
      }
1224
0
      block_size_copy = ( block_size >> 16 ) ^ 0x0000ffffUL;
1225
0
      block_size     &= 0x0000ffffUL;
1226
1227
0
      if( block_size != block_size_copy )
1228
0
      {
1229
0
        libcerror_error_set(
1230
0
         error,
1231
0
         LIBCERROR_ERROR_DOMAIN_INPUT,
1232
0
         LIBCERROR_INPUT_ERROR_VALUE_MISMATCH,
1233
0
         "%s: mismatch in block size ( %" PRIu32 " != %" PRIu32 " ).",
1234
0
         function,
1235
0
         block_size,
1236
0
         block_size_copy );
1237
1238
0
        goto on_error;
1239
0
      }
1240
0
      if( block_size == 0 )
1241
0
      {
1242
0
        break;
1243
0
      }
1244
0
      if( (size_t) block_size > ( bit_stream->byte_stream_size - bit_stream->byte_stream_offset ) )
1245
0
      {
1246
0
        libcerror_error_set(
1247
0
         error,
1248
0
         LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1249
0
         LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
1250
0
         "%s: invalid compressed data value too small.",
1251
0
         function );
1252
1253
0
        goto on_error;
1254
0
      }
1255
0
      if( (size_t) block_size > ( uncompressed_data_size - safe_uncompressed_data_offset ) )
1256
0
      {
1257
0
        libcerror_error_set(
1258
0
         error,
1259
0
         LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1260
0
         LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
1261
0
         "%s: invalid uncompressed data value too small.",
1262
0
         function );
1263
1264
0
        goto on_error;
1265
0
      }
1266
0
      if( memory_copy(
1267
0
           &( uncompressed_data[ safe_uncompressed_data_offset ] ),
1268
0
           &( bit_stream->byte_stream[ bit_stream->byte_stream_offset ] ),
1269
0
           (size_t) block_size ) == NULL )
1270
0
      {
1271
0
        libcerror_error_set(
1272
0
         error,
1273
0
         LIBCERROR_ERROR_DOMAIN_MEMORY,
1274
0
         LIBCERROR_MEMORY_ERROR_COPY_FAILED,
1275
0
         "%s: unable to initialize lz buffer.",
1276
0
         function );
1277
1278
0
        goto on_error;
1279
0
      }
1280
0
      bit_stream->byte_stream_offset += block_size;
1281
0
      safe_uncompressed_data_offset  += block_size;
1282
1283
      /* Flush the bit stream buffer
1284
       */
1285
0
      bit_stream->bit_buffer      = 0;
1286
0
      bit_stream->bit_buffer_size = 0;
1287
1288
0
      break;
1289
1290
0
    case LIBFSHFS_DEFLATE_BLOCK_TYPE_HUFFMAN_FIXED:
1291
0
      if( libfshfs_deflate_decode_huffman(
1292
0
           bit_stream,
1293
0
           fixed_huffman_literals_tree,
1294
0
           fixed_huffman_distances_tree,
1295
0
           uncompressed_data,
1296
0
           uncompressed_data_size,
1297
0
           &safe_uncompressed_data_offset,
1298
0
           error ) != 1 )
1299
0
      {
1300
0
        libcerror_error_set(
1301
0
         error,
1302
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1303
0
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1304
0
         "%s: unable to decode fixed Huffman encoded bit stream.",
1305
0
         function );
1306
1307
0
        goto on_error;
1308
0
      }
1309
0
      break;
1310
1311
0
    case LIBFSHFS_DEFLATE_BLOCK_TYPE_HUFFMAN_DYNAMIC:
1312
0
      if( libfshfs_huffman_tree_initialize(
1313
0
           &dynamic_huffman_literals_tree,
1314
0
           288,
1315
0
           15,
1316
0
           error ) != 1 )
1317
0
      {
1318
0
        libcerror_error_set(
1319
0
         error,
1320
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1321
0
         LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1322
0
         "%s: unable to build dynamic literals Huffman tree.",
1323
0
         function );
1324
1325
0
        goto on_error;
1326
0
      }
1327
0
      if( libfshfs_huffman_tree_initialize(
1328
0
           &dynamic_huffman_distances_tree,
1329
0
           30,
1330
0
           15,
1331
0
           error ) != 1 )
1332
0
      {
1333
0
        libcerror_error_set(
1334
0
         error,
1335
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1336
0
         LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1337
0
         "%s: unable to build dynamic distances Huffman tree.",
1338
0
         function );
1339
1340
0
        goto on_error;
1341
0
      }
1342
0
      if( libfshfs_deflate_build_dynamic_huffman_trees(
1343
0
           bit_stream,
1344
0
           dynamic_huffman_literals_tree,
1345
0
           dynamic_huffman_distances_tree,
1346
0
           error ) != 1 )
1347
0
      {
1348
0
        libcerror_error_set(
1349
0
         error,
1350
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1351
0
         LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1352
0
         "%s: unable to build dynamic Huffman trees.",
1353
0
         function );
1354
1355
0
        goto on_error;
1356
0
      }
1357
0
      if( libfshfs_deflate_decode_huffman(
1358
0
           bit_stream,
1359
0
           dynamic_huffman_literals_tree,
1360
0
           dynamic_huffman_distances_tree,
1361
0
           uncompressed_data,
1362
0
           uncompressed_data_size,
1363
0
           &safe_uncompressed_data_offset,
1364
0
           error ) != 1 )
1365
0
      {
1366
0
        libcerror_error_set(
1367
0
         error,
1368
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1369
0
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1370
0
         "%s: unable to decode dynamic Huffman encoded bit stream.",
1371
0
         function );
1372
1373
0
        goto on_error;
1374
0
      }
1375
0
      if( libfshfs_huffman_tree_free(
1376
0
           &dynamic_huffman_distances_tree,
1377
0
           error ) != 1 )
1378
0
      {
1379
0
        libcerror_error_set(
1380
0
         error,
1381
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1382
0
         LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1383
0
         "%s: unable to free dynamic distances Huffman tree.",
1384
0
         function );
1385
1386
0
        goto on_error;
1387
0
      }
1388
0
      if( libfshfs_huffman_tree_free(
1389
0
           &dynamic_huffman_literals_tree,
1390
0
           error ) != 1 )
1391
0
      {
1392
0
        libcerror_error_set(
1393
0
         error,
1394
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1395
0
         LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1396
0
         "%s: unable to free dynamic literals Huffman tree.",
1397
0
         function );
1398
1399
0
        goto on_error;
1400
0
      }
1401
0
      break;
1402
1403
0
    case LIBFSHFS_DEFLATE_BLOCK_TYPE_RESERVED:
1404
0
    default:
1405
0
      libcerror_error_set(
1406
0
       error,
1407
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1408
0
       LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
1409
0
       "%s: unsupported block type.",
1410
0
       function );
1411
1412
0
      goto on_error;
1413
0
  }
1414
0
  *uncompressed_data_offset = safe_uncompressed_data_offset;
1415
1416
0
  return( 1 );
1417
1418
0
on_error:
1419
0
  if( dynamic_huffman_distances_tree != NULL )
1420
0
  {
1421
0
    libfshfs_huffman_tree_free(
1422
0
     &dynamic_huffman_distances_tree,
1423
0
     NULL );
1424
0
  }
1425
0
  if( dynamic_huffman_literals_tree != NULL )
1426
0
  {
1427
0
    libfshfs_huffman_tree_free(
1428
0
     &dynamic_huffman_literals_tree,
1429
0
     NULL );
1430
0
  }
1431
0
  return( -1 );
1432
0
}
1433
1434
/* Decompresses data using deflate compression
1435
 * Returns 1 on success or -1 on error
1436
 */
1437
int libfshfs_deflate_decompress(
1438
     const uint8_t *compressed_data,
1439
     size_t compressed_data_size,
1440
     uint8_t *uncompressed_data,
1441
     size_t *uncompressed_data_size,
1442
     libcerror_error_t **error )
1443
0
{
1444
0
  libfshfs_bit_stream_t *bit_stream                     = NULL;
1445
0
  libfshfs_huffman_tree_t *fixed_huffman_distances_tree = NULL;
1446
0
  libfshfs_huffman_tree_t *fixed_huffman_literals_tree  = NULL;
1447
0
  static char *function                                = "libfshfs_deflate_decompress";
1448
0
  size_t compressed_data_offset                        = 0;
1449
0
  size_t safe_uncompressed_data_size                   = 0;
1450
0
  size_t uncompressed_data_offset                      = 0;
1451
0
  uint8_t block_type                                   = 0;
1452
0
  uint8_t last_block_flag                              = 0;
1453
1454
0
  if( compressed_data == NULL )
1455
0
  {
1456
0
    libcerror_error_set(
1457
0
     error,
1458
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1459
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1460
0
     "%s: invalid compressed data.",
1461
0
     function );
1462
1463
0
    return( -1 );
1464
0
  }
1465
0
  if( compressed_data_size > (size_t) SSIZE_MAX )
1466
0
  {
1467
0
    libcerror_error_set(
1468
0
     error,
1469
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1470
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1471
0
     "%s: invalid compressed data size value exceeds maximum.",
1472
0
     function );
1473
1474
0
    return( -1 );
1475
0
  }
1476
0
  if( uncompressed_data == NULL )
1477
0
  {
1478
0
    libcerror_error_set(
1479
0
     error,
1480
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1481
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1482
0
     "%s: invalid uncompressed data.",
1483
0
     function );
1484
1485
0
    return( -1 );
1486
0
  }
1487
0
  if( uncompressed_data_size == NULL )
1488
0
  {
1489
0
    libcerror_error_set(
1490
0
     error,
1491
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1492
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1493
0
     "%s: invalid uncompressed data size.",
1494
0
     function );
1495
1496
0
    return( -1 );
1497
0
  }
1498
0
  safe_uncompressed_data_size = *uncompressed_data_size;
1499
1500
0
  if( safe_uncompressed_data_size > (size_t) SSIZE_MAX )
1501
0
  {
1502
0
    libcerror_error_set(
1503
0
     error,
1504
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1505
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1506
0
     "%s: invalid uncompressed data size value exceeds maximum.",
1507
0
     function );
1508
1509
0
    return( -1 );
1510
0
  }
1511
0
  if( compressed_data_offset >= compressed_data_size )
1512
0
  {
1513
0
    libcerror_error_set(
1514
0
     error,
1515
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1516
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
1517
0
     "%s: invalid compressed data value too small.",
1518
0
     function );
1519
1520
0
    return( -1 );
1521
0
  }
1522
0
  if( libfshfs_bit_stream_initialize(
1523
0
       &bit_stream,
1524
0
       compressed_data,
1525
0
       compressed_data_size,
1526
0
       compressed_data_offset,
1527
0
       LIBFSHFS_BIT_STREAM_STORAGE_TYPE_BYTE_BACK_TO_FRONT,
1528
0
       error ) != 1 )
1529
0
  {
1530
0
    libcerror_error_set(
1531
0
     error,
1532
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1533
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1534
0
     "%s: unable to create bit stream.",
1535
0
     function );
1536
1537
0
    goto on_error;
1538
0
  }
1539
0
  while( bit_stream->byte_stream_offset < bit_stream->byte_stream_size )
1540
0
  {
1541
0
    if( libfshfs_deflate_read_block_header(
1542
0
         bit_stream,
1543
0
         &block_type,
1544
0
         &last_block_flag,
1545
0
         error ) != 1 )
1546
0
    {
1547
0
      libcerror_error_set(
1548
0
       error,
1549
0
       LIBCERROR_ERROR_DOMAIN_IO,
1550
0
       LIBCERROR_IO_ERROR_READ_FAILED,
1551
0
       "%s: unable to read compressed data block header.",
1552
0
       function );
1553
1554
0
      goto on_error;
1555
0
    }
1556
0
    if( block_type == LIBFSHFS_DEFLATE_BLOCK_TYPE_HUFFMAN_FIXED )
1557
0
    {
1558
0
      if( ( fixed_huffman_literals_tree == NULL )
1559
0
       && ( fixed_huffman_distances_tree == NULL ) )
1560
0
      {
1561
0
        if( libfshfs_huffman_tree_initialize(
1562
0
             &fixed_huffman_literals_tree,
1563
0
             288,
1564
0
             15,
1565
0
             error ) != 1 )
1566
0
        {
1567
0
          libcerror_error_set(
1568
0
           error,
1569
0
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
1570
0
           LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1571
0
           "%s: unable to build fixed literals Huffman tree.",
1572
0
           function );
1573
1574
0
          goto on_error;
1575
0
        }
1576
0
        if( libfshfs_huffman_tree_initialize(
1577
0
             &fixed_huffman_distances_tree,
1578
0
             30,
1579
0
             15,
1580
0
             error ) != 1 )
1581
0
        {
1582
0
          libcerror_error_set(
1583
0
           error,
1584
0
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
1585
0
           LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1586
0
           "%s: unable to build fixed distances Huffman tree.",
1587
0
           function );
1588
1589
0
          goto on_error;
1590
0
        }
1591
0
        if( libfshfs_deflate_build_fixed_huffman_trees(
1592
0
             fixed_huffman_literals_tree,
1593
0
             fixed_huffman_distances_tree,
1594
0
             error ) != 1 )
1595
0
        {
1596
0
          libcerror_error_set(
1597
0
           error,
1598
0
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
1599
0
           LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1600
0
           "%s: unable to build fixed Huffman trees.",
1601
0
           function );
1602
1603
0
          goto on_error;
1604
0
        }
1605
0
      }
1606
0
    }
1607
0
    if( libfshfs_deflate_read_block(
1608
0
         bit_stream,
1609
0
         block_type,
1610
0
         fixed_huffman_literals_tree,
1611
0
         fixed_huffman_distances_tree,
1612
0
         uncompressed_data,
1613
0
         safe_uncompressed_data_size,
1614
0
         &uncompressed_data_offset,
1615
0
         error ) != 1 )
1616
0
    {
1617
0
      libcerror_error_set(
1618
0
       error,
1619
0
       LIBCERROR_ERROR_DOMAIN_IO,
1620
0
       LIBCERROR_IO_ERROR_READ_FAILED,
1621
0
       "%s: unable to read block of compressed data.",
1622
0
       function );
1623
1624
0
      goto on_error;
1625
0
    }
1626
0
    if( last_block_flag != 0 )
1627
0
    {
1628
0
      break;
1629
0
    }
1630
0
  }
1631
0
  if( fixed_huffman_distances_tree != NULL )
1632
0
  {
1633
0
    if( libfshfs_huffman_tree_free(
1634
0
         &fixed_huffman_distances_tree,
1635
0
         error ) != 1 )
1636
0
    {
1637
0
      libcerror_error_set(
1638
0
       error,
1639
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1640
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1641
0
       "%s: unable to free fixed distances Huffman tree.",
1642
0
       function );
1643
1644
0
      goto on_error;
1645
0
    }
1646
0
  }
1647
0
  if( fixed_huffman_literals_tree != NULL )
1648
0
  {
1649
0
    if( libfshfs_huffman_tree_free(
1650
0
         &fixed_huffman_literals_tree,
1651
0
         error ) != 1 )
1652
0
    {
1653
0
      libcerror_error_set(
1654
0
       error,
1655
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1656
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1657
0
       "%s: unable to free fixed literals Huffman tree.",
1658
0
       function );
1659
1660
0
      goto on_error;
1661
0
    }
1662
0
  }
1663
0
  if( libfshfs_bit_stream_free(
1664
0
       &bit_stream,
1665
0
       error ) != 1 )
1666
0
  {
1667
0
    libcerror_error_set(
1668
0
     error,
1669
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1670
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1671
0
     "%s: unable to free bit stream.",
1672
0
     function );
1673
1674
0
    goto on_error;
1675
0
  }
1676
0
  *uncompressed_data_size = uncompressed_data_offset;
1677
1678
0
  return( 1 );
1679
1680
0
on_error:
1681
0
  if( fixed_huffman_distances_tree != NULL )
1682
0
  {
1683
0
    libfshfs_huffman_tree_free(
1684
0
     &fixed_huffman_distances_tree,
1685
0
     NULL );
1686
0
  }
1687
0
  if( fixed_huffman_literals_tree != NULL )
1688
0
  {
1689
0
    libfshfs_huffman_tree_free(
1690
0
     &fixed_huffman_literals_tree,
1691
0
     NULL );
1692
0
  }
1693
0
  if( bit_stream != NULL )
1694
0
  {
1695
0
    libfshfs_bit_stream_free(
1696
0
     &bit_stream,
1697
0
     NULL );
1698
0
  }
1699
0
  return( -1 );
1700
0
}
1701
1702
/* Decompresses data using zlib compression
1703
 * Returns 1 on success or -1 on error
1704
 */
1705
int libfshfs_deflate_decompress_zlib(
1706
     const uint8_t *compressed_data,
1707
     size_t compressed_data_size,
1708
     uint8_t *uncompressed_data,
1709
     size_t *uncompressed_data_size,
1710
     libcerror_error_t **error )
1711
0
{
1712
0
  libfshfs_bit_stream_t *bit_stream                     = NULL;
1713
0
  libfshfs_huffman_tree_t *fixed_huffman_distances_tree = NULL;
1714
0
  libfshfs_huffman_tree_t *fixed_huffman_literals_tree  = NULL;
1715
0
  static char *function                                = "libfshfs_deflate_decompress_zlib";
1716
0
  size_t compressed_data_offset                        = 0;
1717
0
  size_t safe_uncompressed_data_size                   = 0;
1718
0
  size_t uncompressed_data_offset                      = 0;
1719
0
  uint32_t calculated_checksum                         = 0;
1720
0
  uint32_t stored_checksum                             = 0;
1721
0
  uint8_t block_type                                   = 0;
1722
0
  uint8_t last_block_flag                              = 0;
1723
1724
0
  if( compressed_data == NULL )
1725
0
  {
1726
0
    libcerror_error_set(
1727
0
     error,
1728
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1729
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1730
0
     "%s: invalid compressed data.",
1731
0
     function );
1732
1733
0
    return( -1 );
1734
0
  }
1735
0
  if( compressed_data_size > (size_t) SSIZE_MAX )
1736
0
  {
1737
0
    libcerror_error_set(
1738
0
     error,
1739
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1740
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1741
0
     "%s: invalid compressed data size value exceeds maximum.",
1742
0
     function );
1743
1744
0
    return( -1 );
1745
0
  }
1746
0
  if( uncompressed_data == NULL )
1747
0
  {
1748
0
    libcerror_error_set(
1749
0
     error,
1750
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1751
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1752
0
     "%s: invalid uncompressed data.",
1753
0
     function );
1754
1755
0
    return( -1 );
1756
0
  }
1757
0
  if( uncompressed_data_size == NULL )
1758
0
  {
1759
0
    libcerror_error_set(
1760
0
     error,
1761
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1762
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1763
0
     "%s: invalid uncompressed data size.",
1764
0
     function );
1765
1766
0
    return( -1 );
1767
0
  }
1768
0
  safe_uncompressed_data_size = *uncompressed_data_size;
1769
1770
0
  if( safe_uncompressed_data_size > (size_t) SSIZE_MAX )
1771
0
  {
1772
0
    libcerror_error_set(
1773
0
     error,
1774
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1775
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1776
0
     "%s: invalid uncompressed data size value exceeds maximum.",
1777
0
     function );
1778
1779
0
    return( -1 );
1780
0
  }
1781
0
  if( libfshfs_deflate_read_data_header(
1782
0
       compressed_data,
1783
0
       compressed_data_size,
1784
0
       &compressed_data_offset,
1785
0
       error ) != 1 )
1786
0
  {
1787
0
    libcerror_error_set(
1788
0
     error,
1789
0
     LIBCERROR_ERROR_DOMAIN_IO,
1790
0
     LIBCERROR_IO_ERROR_READ_FAILED,
1791
0
     "%s: unable to read data header.",
1792
0
     function );
1793
1794
0
    goto on_error;
1795
0
  }
1796
0
  if( compressed_data_offset >= compressed_data_size )
1797
0
  {
1798
0
    libcerror_error_set(
1799
0
     error,
1800
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1801
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
1802
0
     "%s: invalid compressed data value too small.",
1803
0
     function );
1804
1805
0
    goto on_error;
1806
0
  }
1807
0
  if( libfshfs_bit_stream_initialize(
1808
0
       &bit_stream,
1809
0
       compressed_data,
1810
0
       compressed_data_size,
1811
0
       compressed_data_offset,
1812
0
       LIBFSHFS_BIT_STREAM_STORAGE_TYPE_BYTE_BACK_TO_FRONT,
1813
0
       error ) != 1 )
1814
0
  {
1815
0
    libcerror_error_set(
1816
0
     error,
1817
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1818
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1819
0
     "%s: unable to create bit stream.",
1820
0
     function );
1821
1822
0
    goto on_error;
1823
0
  }
1824
0
  while( bit_stream->byte_stream_offset < bit_stream->byte_stream_size )
1825
0
  {
1826
0
    if( libfshfs_deflate_read_block_header(
1827
0
         bit_stream,
1828
0
         &block_type,
1829
0
         &last_block_flag,
1830
0
         error ) != 1 )
1831
0
    {
1832
0
      libcerror_error_set(
1833
0
       error,
1834
0
       LIBCERROR_ERROR_DOMAIN_IO,
1835
0
       LIBCERROR_IO_ERROR_READ_FAILED,
1836
0
       "%s: unable to read compressed data block header.",
1837
0
       function );
1838
1839
0
      goto on_error;
1840
0
    }
1841
0
    if( block_type == LIBFSHFS_DEFLATE_BLOCK_TYPE_HUFFMAN_FIXED )
1842
0
    {
1843
0
      if( ( fixed_huffman_literals_tree == NULL )
1844
0
       && ( fixed_huffman_distances_tree == NULL ) )
1845
0
      {
1846
0
        if( libfshfs_huffman_tree_initialize(
1847
0
             &fixed_huffman_literals_tree,
1848
0
             288,
1849
0
             15,
1850
0
             error ) != 1 )
1851
0
        {
1852
0
          libcerror_error_set(
1853
0
           error,
1854
0
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
1855
0
           LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1856
0
           "%s: unable to build fixed literals Huffman tree.",
1857
0
           function );
1858
1859
0
          goto on_error;
1860
0
        }
1861
0
        if( libfshfs_huffman_tree_initialize(
1862
0
             &fixed_huffman_distances_tree,
1863
0
             30,
1864
0
             15,
1865
0
             error ) != 1 )
1866
0
        {
1867
0
          libcerror_error_set(
1868
0
           error,
1869
0
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
1870
0
           LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1871
0
           "%s: unable to build fixed distances Huffman tree.",
1872
0
           function );
1873
1874
0
          goto on_error;
1875
0
        }
1876
0
        if( libfshfs_deflate_build_fixed_huffman_trees(
1877
0
             fixed_huffman_literals_tree,
1878
0
             fixed_huffman_distances_tree,
1879
0
             error ) != 1 )
1880
0
        {
1881
0
          libcerror_error_set(
1882
0
           error,
1883
0
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
1884
0
           LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1885
0
           "%s: unable to build fixed Huffman trees.",
1886
0
           function );
1887
1888
0
          goto on_error;
1889
0
        }
1890
0
      }
1891
0
    }
1892
0
    if( libfshfs_deflate_read_block(
1893
0
         bit_stream,
1894
0
         block_type,
1895
0
         fixed_huffman_literals_tree,
1896
0
         fixed_huffman_distances_tree,
1897
0
         uncompressed_data,
1898
0
         safe_uncompressed_data_size,
1899
0
         &uncompressed_data_offset,
1900
0
         error ) != 1 )
1901
0
    {
1902
0
      libcerror_error_set(
1903
0
       error,
1904
0
       LIBCERROR_ERROR_DOMAIN_IO,
1905
0
       LIBCERROR_IO_ERROR_READ_FAILED,
1906
0
       "%s: unable to read block of compressed data.",
1907
0
       function );
1908
1909
0
      goto on_error;
1910
0
    }
1911
0
    if( last_block_flag != 0 )
1912
0
    {
1913
0
      break;
1914
0
    }
1915
0
  }
1916
0
  if( ( bit_stream->byte_stream_size - bit_stream->byte_stream_offset ) >= 4 )
1917
0
  {
1918
0
    while( bit_stream->bit_buffer_size >= 8 )
1919
0
    {
1920
0
      bit_stream->byte_stream_offset -= 1;
1921
0
      bit_stream->bit_buffer_size    -= 8;
1922
0
    }
1923
0
    byte_stream_copy_to_uint32_big_endian(
1924
0
     &( bit_stream->byte_stream[ bit_stream->byte_stream_offset ] ),
1925
0
     stored_checksum );
1926
1927
0
    if( libfshfs_deflate_calculate_adler32(
1928
0
         &calculated_checksum,
1929
0
         uncompressed_data,
1930
0
         uncompressed_data_offset,
1931
0
         1,
1932
0
         error ) != 1 )
1933
0
    {
1934
0
      libcerror_error_set(
1935
0
       error,
1936
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1937
0
       LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1938
0
       "%s: unable to calculate checksum.",
1939
0
       function );
1940
1941
0
      goto on_error;
1942
0
    }
1943
0
    if( stored_checksum != calculated_checksum )
1944
0
    {
1945
0
      libcerror_error_set(
1946
0
       error,
1947
0
       LIBCERROR_ERROR_DOMAIN_INPUT,
1948
0
       LIBCERROR_INPUT_ERROR_CHECKSUM_MISMATCH,
1949
0
       "%s: checksum does not match (stored: 0x%08" PRIx32 ", calculated: 0x%08" PRIx32 ").",
1950
0
       function,
1951
0
       stored_checksum,
1952
0
       calculated_checksum );
1953
1954
0
      goto on_error;
1955
0
    }
1956
0
  }
1957
0
  if( fixed_huffman_distances_tree != NULL )
1958
0
  {
1959
0
    if( libfshfs_huffman_tree_free(
1960
0
         &fixed_huffman_distances_tree,
1961
0
         error ) != 1 )
1962
0
    {
1963
0
      libcerror_error_set(
1964
0
       error,
1965
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1966
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1967
0
       "%s: unable to free fixed distances Huffman tree.",
1968
0
       function );
1969
1970
0
      goto on_error;
1971
0
    }
1972
0
  }
1973
0
  if( fixed_huffman_literals_tree != NULL )
1974
0
  {
1975
0
    if( libfshfs_huffman_tree_free(
1976
0
         &fixed_huffman_literals_tree,
1977
0
         error ) != 1 )
1978
0
    {
1979
0
      libcerror_error_set(
1980
0
       error,
1981
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1982
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1983
0
       "%s: unable to free fixed literals Huffman tree.",
1984
0
       function );
1985
1986
0
      goto on_error;
1987
0
    }
1988
0
  }
1989
0
  if( libfshfs_bit_stream_free(
1990
0
       &bit_stream,
1991
0
       error ) != 1 )
1992
0
  {
1993
0
    libcerror_error_set(
1994
0
     error,
1995
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1996
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1997
0
     "%s: unable to free bit stream.",
1998
0
     function );
1999
2000
0
    goto on_error;
2001
0
  }
2002
0
  *uncompressed_data_size = uncompressed_data_offset;
2003
2004
0
  return( 1 );
2005
2006
0
on_error:
2007
0
  if( fixed_huffman_distances_tree != NULL )
2008
0
  {
2009
0
    libfshfs_huffman_tree_free(
2010
0
     &fixed_huffman_distances_tree,
2011
0
     NULL );
2012
0
  }
2013
0
  if( fixed_huffman_literals_tree != NULL )
2014
0
  {
2015
0
    libfshfs_huffman_tree_free(
2016
0
     &fixed_huffman_literals_tree,
2017
0
     NULL );
2018
0
  }
2019
0
  if( bit_stream != NULL )
2020
0
  {
2021
0
    libfshfs_bit_stream_free(
2022
0
     &bit_stream,
2023
0
     NULL );
2024
0
  }
2025
0
  return( -1 );
2026
0
}
2027