Coverage Report

Created: 2026-07-25 07:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libewf/libewf/libewf_deflate.c
Line
Count
Source
1
/*
2
 * Deflate (zlib) (un)compression functions
3
 *
4
 * Copyright (C) 2006-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 "libewf_bit_stream.h"
28
#include "libewf_deflate.h"
29
#include "libewf_huffman_tree.h"
30
#include "libewf_libcerror.h"
31
#include "libewf_libcnotify.h"
32
33
const uint8_t libewf_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 libewf_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 libewf_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 libewf_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 libewf_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 libewf_deflate_build_dynamic_huffman_trees(
58
     libewf_bit_stream_t *bit_stream,
59
     libewf_huffman_tree_t *literals_tree,
60
     libewf_huffman_tree_t *distances_tree,
61
     libcerror_error_t **error )
62
4.41k
{
63
4.41k
  uint8_t code_size_array[ 316 ];
64
65
4.41k
  libewf_huffman_tree_t *codes_tree = NULL;
66
4.41k
  static char *function             = "libewf_deflate_build_dynamic_huffman_trees";
67
4.41k
  uint32_t code_size                = 0;
68
4.41k
  uint32_t code_size_index          = 0;
69
4.41k
  uint32_t code_size_sequence       = 0;
70
4.41k
  uint32_t number_of_code_sizes     = 0;
71
4.41k
  uint32_t number_of_distance_codes = 0;
72
4.41k
  uint32_t number_of_literal_codes  = 0;
73
4.41k
  uint32_t times_to_repeat          = 0;
74
4.41k
  uint16_t symbol                   = 0;
75
76
4.41k
  if( libewf_bit_stream_get_value(
77
4.41k
       bit_stream,
78
4.41k
       14,
79
4.41k
       &number_of_code_sizes,
80
4.41k
       error ) != 1 )
81
2
  {
82
2
    libcerror_error_set(
83
2
     error,
84
2
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
85
2
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
86
2
     "%s: unable to retrieve value from bit stream.",
87
2
     function );
88
89
2
    goto on_error;
90
2
  }
91
4.41k
  number_of_literal_codes  = number_of_code_sizes & 0x0000001fUL;
92
4.41k
  number_of_code_sizes   >>= 5;
93
4.41k
  number_of_distance_codes = number_of_code_sizes & 0x0000001fUL;
94
4.41k
  number_of_code_sizes   >>= 5;
95
96
4.41k
  number_of_literal_codes += 257;
97
98
4.41k
  if( number_of_literal_codes > 286 )
99
2
  {
100
2
    libcerror_error_set(
101
2
     error,
102
2
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
103
2
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
104
2
     "%s: invalid number of literal codes value out of bounds.",
105
2
     function );
106
107
2
    goto on_error;
108
2
  }
109
4.40k
  number_of_distance_codes += 1;
110
111
4.40k
  if( number_of_distance_codes > 30 )
112
1
  {
113
1
    libcerror_error_set(
114
1
     error,
115
1
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
116
1
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
117
1
     "%s: invalid number of distance codes value out of bounds.",
118
1
     function );
119
120
1
    goto on_error;
121
1
  }
122
4.40k
  number_of_code_sizes += 4;
123
124
4.40k
  for( code_size_index = 0;
125
76.1k
       code_size_index < number_of_code_sizes;
126
71.7k
       code_size_index++ )
127
71.7k
  {
128
71.7k
    if( libewf_bit_stream_get_value(
129
71.7k
         bit_stream,
130
71.7k
         3,
131
71.7k
         &code_size,
132
71.7k
         error ) != 1 )
133
3
    {
134
3
      libcerror_error_set(
135
3
       error,
136
3
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
137
3
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
138
3
       "%s: unable to retrieve value from bit stream.",
139
3
       function );
140
141
3
      goto on_error;
142
3
    }
143
71.7k
    code_size_sequence = libewf_deflate_code_sizes_sequence[ code_size_index ];
144
145
71.7k
    code_size_array[ code_size_sequence ] = (uint8_t) code_size;
146
71.7k
  }
147
16.2k
  while( code_size_index < 19 )
148
11.8k
  {
149
11.8k
    code_size_sequence = libewf_deflate_code_sizes_sequence[ code_size_index++ ];
150
151
11.8k
    code_size_array[ code_size_sequence ] = 0;
152
11.8k
  }
153
4.40k
  if( libewf_huffman_tree_initialize(
154
4.40k
       &codes_tree,
155
4.40k
       19,
156
4.40k
       15,
157
4.40k
       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
4.40k
  if( libewf_huffman_tree_build(
169
4.40k
       codes_tree,
170
4.40k
       code_size_array,
171
4.40k
       19,
172
4.40k
       error ) != 1 )
173
13
  {
174
13
    libcerror_error_set(
175
13
     error,
176
13
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
177
13
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
178
13
     "%s: unable to build codes tree.",
179
13
     function );
180
181
13
    goto on_error;
182
13
  }
183
4.39k
  number_of_code_sizes = number_of_literal_codes + number_of_distance_codes;
184
185
4.39k
  code_size_index = 0;
186
187
298k
  while( code_size_index < number_of_code_sizes )
188
293k
  {
189
293k
    if( libewf_huffman_tree_get_symbol_from_bit_stream(
190
293k
         codes_tree,
191
293k
         bit_stream,
192
293k
         &symbol,
193
293k
         error ) != 1 )
194
19
    {
195
19
      libcerror_error_set(
196
19
       error,
197
19
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
198
19
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
199
19
       "%s: unable to retrieve literal value from bit stream.",
200
19
       function );
201
202
19
      goto on_error;
203
19
    }
204
293k
    if( symbol < 16 )
205
256k
    {
206
256k
      code_size_array[ code_size_index++ ] = (uint8_t) symbol;
207
208
256k
      continue;
209
256k
    }
210
37.0k
    code_size = 0;
211
212
37.0k
    if( symbol == 16 )
213
6.93k
    {
214
6.93k
      if( code_size_index == 0 )
215
3
      {
216
3
        libcerror_error_set(
217
3
         error,
218
3
         LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
219
3
         LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
220
3
         "%s: invalid code size index value out of bounds.",
221
3
         function );
222
223
3
        goto on_error;
224
3
      }
225
6.93k
      code_size = (uint32_t) code_size_array[ code_size_index - 1 ];
226
227
6.93k
      if( libewf_bit_stream_get_value(
228
6.93k
           bit_stream,
229
6.93k
           2,
230
6.93k
           &times_to_repeat,
231
6.93k
           error ) != 1 )
232
5
      {
233
5
        libcerror_error_set(
234
5
         error,
235
5
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
236
5
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
237
5
         "%s: unable to retrieve value from bit stream.",
238
5
         function );
239
240
5
        goto on_error;
241
5
      }
242
6.93k
      times_to_repeat += 3;
243
6.93k
    }
244
30.1k
    else if( symbol == 17 )
245
11.6k
    {
246
11.6k
      if( libewf_bit_stream_get_value(
247
11.6k
           bit_stream,
248
11.6k
           3,
249
11.6k
           &times_to_repeat,
250
11.6k
           error ) != 1 )
251
2
      {
252
2
        libcerror_error_set(
253
2
         error,
254
2
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
255
2
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
256
2
         "%s: unable to retrieve value from bit stream.",
257
2
         function );
258
259
2
        goto on_error;
260
2
      }
261
11.6k
      times_to_repeat += 3;
262
11.6k
    }
263
18.4k
    else if( symbol == 18 )
264
18.4k
    {
265
18.4k
      if( libewf_bit_stream_get_value(
266
18.4k
           bit_stream,
267
18.4k
           7,
268
18.4k
           &times_to_repeat,
269
18.4k
           error ) != 1 )
270
4
      {
271
4
        libcerror_error_set(
272
4
         error,
273
4
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
274
4
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
275
4
         "%s: unable to retrieve value from bit stream.",
276
4
         function );
277
278
4
        goto on_error;
279
4
      }
280
18.4k
      times_to_repeat += 11;
281
18.4k
    }
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
37.0k
    if( ( code_size_index + times_to_repeat ) > number_of_code_sizes )
294
14
    {
295
14
      libcerror_error_set(
296
14
       error,
297
14
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
298
14
       LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
299
14
       "%s: invalid times to repeat value out of bounds.",
300
14
       function );
301
302
14
      goto on_error;
303
14
    }
304
1.02M
    while( times_to_repeat > 0 )
305
986k
    {
306
986k
      code_size_array[ code_size_index++ ] = (uint8_t) code_size;
307
308
986k
      times_to_repeat--;
309
986k
    }
310
37.0k
  }
311
4.34k
  if( code_size_array[ 256 ] == 0 )
312
2
  {
313
2
    libcerror_error_set(
314
2
     error,
315
2
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
316
2
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
317
2
     "%s: end-of-block code value missing in literal codes array.",
318
2
     function );
319
320
2
    goto on_error;
321
2
  }
322
4.34k
  if( libewf_huffman_tree_free(
323
4.34k
       &codes_tree,
324
4.34k
       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
4.34k
  if( libewf_huffman_tree_build(
336
4.34k
       literals_tree,
337
4.34k
       code_size_array,
338
4.34k
       number_of_literal_codes,
339
4.34k
       error ) != 1 )
340
12
  {
341
12
    libcerror_error_set(
342
12
     error,
343
12
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
344
12
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
345
12
     "%s: unable to build literals tree.",
346
12
     function );
347
348
12
    goto on_error;
349
12
  }
350
4.33k
  if( libewf_huffman_tree_build(
351
4.33k
       distances_tree,
352
4.33k
       &( code_size_array[ number_of_literal_codes ] ),
353
4.33k
       number_of_distance_codes,
354
4.33k
       error ) != 1 )
355
6
  {
356
6
    libcerror_error_set(
357
6
     error,
358
6
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
359
6
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
360
6
     "%s: unable to build distances tree.",
361
6
     function );
362
363
6
    goto on_error;
364
6
  }
365
4.32k
  return( 1 );
366
367
88
on_error:
368
88
  if( codes_tree != NULL )
369
62
  {
370
62
    libewf_huffman_tree_free(
371
62
     &codes_tree,
372
62
     NULL );
373
62
  }
374
88
  return( -1 );
375
4.33k
}
376
377
/* Initializes the fixed Huffman trees
378
 * Returns 1 on success or -1 on error
379
 */
380
int libewf_deflate_build_fixed_huffman_trees(
381
     libewf_huffman_tree_t *literals_tree,
382
     libewf_huffman_tree_t *distances_tree,
383
     libcerror_error_t **error )
384
491
{
385
491
  uint8_t code_size_array[ 318 ];
386
387
491
  static char *function = "libewf_deflate_build_fixed_huffman_trees";
388
491
  uint16_t symbol       = 0;
389
390
491
  for( symbol = 0;
391
156k
       symbol < 318;
392
156k
       symbol++ )
393
156k
  {
394
156k
    if( symbol < 144 )
395
70.7k
    {
396
70.7k
      code_size_array[ symbol ] = 8;
397
70.7k
    }
398
85.4k
    else if( symbol < 256 )
399
54.9k
    {
400
54.9k
      code_size_array[ symbol ] = 9;
401
54.9k
    }
402
30.4k
    else if( symbol < 280 )
403
11.7k
    {
404
11.7k
      code_size_array[ symbol ] = 7;
405
11.7k
    }
406
18.6k
    else if( symbol < 288 )
407
3.92k
    {
408
3.92k
      code_size_array[ symbol ] = 8;
409
3.92k
    }
410
14.7k
    else
411
14.7k
    {
412
14.7k
      code_size_array[ symbol ] = 5;
413
14.7k
    }
414
156k
  }
415
491
  if( libewf_huffman_tree_build(
416
491
       literals_tree,
417
491
       code_size_array,
418
491
       288,
419
491
       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
491
  if( libewf_huffman_tree_build(
431
491
       distances_tree,
432
491
       &( code_size_array[ 288 ] ),
433
491
       30,
434
491
       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
491
  return( 1 );
446
491
}
447
448
/* Decodes a Huffman compressed block
449
 * Returns 1 on success or -1 on error
450
 */
451
int libewf_deflate_decode_huffman(
452
     libewf_bit_stream_t *bit_stream,
453
     libewf_huffman_tree_t *literals_tree,
454
     libewf_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
9.69k
{
460
9.69k
  static char *function         = "libewf_deflate_decode_huffman";
461
9.69k
  size_t data_offset            = 0;
462
9.69k
  uint32_t extra_bits           = 0;
463
9.69k
  uint16_t compression_offset   = 0;
464
9.69k
  uint16_t compression_size     = 0;
465
9.69k
  uint16_t number_of_extra_bits = 0;
466
9.69k
  uint16_t symbol               = 0;
467
468
9.69k
  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
9.69k
  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
9.69k
  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
9.69k
  data_offset = *uncompressed_data_offset;
502
503
9.69k
  do
504
8.12M
  {
505
8.12M
    if( libewf_huffman_tree_get_symbol_from_bit_stream(
506
8.12M
         literals_tree,
507
8.12M
         bit_stream,
508
8.12M
         &symbol,
509
8.12M
         error ) != 1 )
510
12
    {
511
12
      libcerror_error_set(
512
12
       error,
513
12
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
514
12
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
515
12
       "%s: unable to retrieve literal value from bit stream.",
516
12
       function );
517
518
12
      return( -1 );
519
12
    }
520
8.12M
    if( symbol < 256 )
521
484k
    {
522
484k
      if( data_offset >= uncompressed_data_size )
523
1
      {
524
1
        libcerror_error_set(
525
1
         error,
526
1
         LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
527
1
         LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
528
1
         "%s: invalid uncompressed data value too small.",
529
1
         function );
530
531
1
        return( -1 );
532
1
      }
533
484k
      uncompressed_data[ data_offset++ ] = (uint8_t) symbol;
534
484k
    }
535
7.63M
    else if( ( symbol > 256 )
536
7.62M
          && ( symbol < 286 ) )
537
7.62M
    {
538
7.62M
      symbol -= 257;
539
540
7.62M
      number_of_extra_bits = libewf_deflate_literal_codes_number_of_extra_bits[ symbol ];
541
542
7.62M
      if( libewf_bit_stream_get_value(
543
7.62M
           bit_stream,
544
7.62M
           (uint8_t) number_of_extra_bits,
545
7.62M
           &extra_bits,
546
7.62M
           error ) != 1 )
547
1
      {
548
1
        libcerror_error_set(
549
1
         error,
550
1
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
551
1
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
552
1
         "%s: unable to retrieve literal extra value from bit stream.",
553
1
         function );
554
555
1
        return( -1 );
556
1
      }
557
7.62M
      compression_size = libewf_deflate_literal_codes_base[ symbol ] + (uint16_t) extra_bits;
558
559
7.62M
      if( libewf_huffman_tree_get_symbol_from_bit_stream(
560
7.62M
           distances_tree,
561
7.62M
           bit_stream,
562
7.62M
           &symbol,
563
7.62M
           error ) != 1 )
564
6
      {
565
6
        libcerror_error_set(
566
6
         error,
567
6
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
568
6
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
569
6
         "%s: unable to retrieve distance value from bit stream.",
570
6
         function );
571
572
6
        return( -1 );
573
6
      }
574
7.62M
      number_of_extra_bits = libewf_deflate_distance_codes_number_of_extra_bits[ symbol ];
575
576
7.62M
      if( libewf_bit_stream_get_value(
577
7.62M
           bit_stream,
578
7.62M
           (uint8_t) number_of_extra_bits,
579
7.62M
           &extra_bits,
580
7.62M
           error ) != 1 )
581
2
      {
582
2
        libcerror_error_set(
583
2
         error,
584
2
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
585
2
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
586
2
         "%s: unable to retrieve distance extra value from bit stream.",
587
2
         function );
588
589
2
        return( -1 );
590
2
      }
591
7.62M
      compression_offset = libewf_deflate_distance_codes_base[ symbol ] + (uint16_t) extra_bits;
592
593
7.62M
      if( compression_offset > data_offset )
594
20
      {
595
20
        libcerror_error_set(
596
20
         error,
597
20
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
598
20
         LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
599
20
         "%s: invalid compression offset value out of bounds.",
600
20
         function );
601
602
20
        return( -1 );
603
20
      }
604
7.62M
      if( ( data_offset + compression_size ) > uncompressed_data_size )
605
7
      {
606
7
        libcerror_error_set(
607
7
         error,
608
7
         LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
609
7
         LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
610
7
         "%s: invalid uncompressed data value too small.",
611
7
         function );
612
613
7
        return( -1 );
614
7
      }
615
31.5M
      while( compression_size > 0 )
616
23.8M
      {
617
23.8M
        uncompressed_data[ data_offset ] = uncompressed_data[ data_offset - compression_offset ];
618
619
23.8M
        data_offset++;
620
23.8M
        compression_size--;
621
23.8M
      }
622
7.62M
    }
623
9.64k
    else if( symbol != 256 )
624
4
    {
625
4
      libcerror_error_set(
626
4
       error,
627
4
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
628
4
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
629
4
       "%s: invalid code value: %" PRIu16 ".",
630
4
       function,
631
4
       symbol );
632
633
4
      return( -1 );
634
4
    }
635
8.12M
  }
636
8.12M
  while( symbol != 256 );
637
638
9.63k
  *uncompressed_data_offset = data_offset;
639
640
9.63k
  return( 1 );
641
9.69k
}
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 libewf_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
22.9k
{
654
22.9k
  static char *function = "libewf_deflate_calculate_adler32";
655
22.9k
  size_t data_offset    = 0;
656
22.9k
  uint32_t lower_word   = 0;
657
22.9k
  uint32_t upper_word   = 0;
658
22.9k
  uint32_t value_32bit  = 0;
659
22.9k
  int block_index       = 0;
660
661
22.9k
  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
22.9k
  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
22.9k
  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
22.9k
  lower_word = initial_value & 0xffff;
695
22.9k
  upper_word = ( initial_value >> 16 ) & 0xffff;
696
697
26.8k
  while( data_size >= 0x15b0 )
698
3.88k
  {
699
    /* The modulo calculation is needed per 5552 (0x15b0) bytes
700
     * 5552 / 16 = 347
701
     */
702
3.88k
    for( block_index = 0;
703
1.35M
         block_index < 347;
704
1.34M
         block_index++ )
705
1.34M
    {
706
1.34M
      lower_word += data[ data_offset++ ];
707
1.34M
      upper_word += lower_word;
708
709
1.34M
      lower_word += data[ data_offset++ ];
710
1.34M
      upper_word += lower_word;
711
712
1.34M
      lower_word += data[ data_offset++ ];
713
1.34M
      upper_word += lower_word;
714
715
1.34M
      lower_word += data[ data_offset++ ];
716
1.34M
      upper_word += lower_word;
717
718
1.34M
      lower_word += data[ data_offset++ ];
719
1.34M
      upper_word += lower_word;
720
721
1.34M
      lower_word += data[ data_offset++ ];
722
1.34M
      upper_word += lower_word;
723
724
1.34M
      lower_word += data[ data_offset++ ];
725
1.34M
      upper_word += lower_word;
726
727
1.34M
      lower_word += data[ data_offset++ ];
728
1.34M
      upper_word += lower_word;
729
730
1.34M
      lower_word += data[ data_offset++ ];
731
1.34M
      upper_word += lower_word;
732
733
1.34M
      lower_word += data[ data_offset++ ];
734
1.34M
      upper_word += lower_word;
735
736
1.34M
      lower_word += data[ data_offset++ ];
737
1.34M
      upper_word += lower_word;
738
739
1.34M
      lower_word += data[ data_offset++ ];
740
1.34M
      upper_word += lower_word;
741
742
1.34M
      lower_word += data[ data_offset++ ];
743
1.34M
      upper_word += lower_word;
744
745
1.34M
      lower_word += data[ data_offset++ ];
746
1.34M
      upper_word += lower_word;
747
748
1.34M
      lower_word += data[ data_offset++ ];
749
1.34M
      upper_word += lower_word;
750
751
1.34M
      lower_word += data[ data_offset++ ];
752
1.34M
      upper_word += lower_word;
753
1.34M
    }
754
    /* Optimized equivalent of:
755
     * lower_word %= 0xfff1
756
     */
757
3.88k
    value_32bit = lower_word >> 16;
758
3.88k
    lower_word &= 0x0000ffffUL;
759
3.88k
    lower_word += ( value_32bit << 4 ) - value_32bit;
760
761
3.88k
    if( lower_word > 65521 )
762
51
    {
763
51
      value_32bit = lower_word >> 16;
764
51
      lower_word &= 0x0000ffffUL;
765
51
      lower_word += ( value_32bit << 4 ) - value_32bit;
766
51
    }
767
3.88k
    if( lower_word >= 65521 )
768
19
    {
769
19
      lower_word -= 65521;
770
19
    }
771
    /* Optimized equivalent of:
772
     * upper_word %= 0xfff1
773
     */
774
3.88k
    value_32bit = upper_word >> 16;
775
3.88k
    upper_word &= 0x0000ffffUL;
776
3.88k
    upper_word += ( value_32bit << 4 ) - value_32bit;
777
778
3.88k
    if( upper_word > 65521 )
779
3.43k
    {
780
3.43k
      value_32bit = upper_word >> 16;
781
3.43k
      upper_word &= 0x0000ffffUL;
782
3.43k
      upper_word += ( value_32bit << 4 ) - value_32bit;
783
3.43k
    }
784
3.88k
    if( upper_word >= 65521 )
785
25
    {
786
25
      upper_word -= 65521;
787
25
    }
788
3.88k
    data_size -= 0x15b0;
789
3.88k
  }
790
22.9k
  if( data_size > 0 )
791
22.8k
  {
792
248k
    while( data_size > 16 )
793
225k
    {
794
225k
      lower_word += data[ data_offset++ ];
795
225k
      upper_word += lower_word;
796
797
225k
      lower_word += data[ data_offset++ ];
798
225k
      upper_word += lower_word;
799
800
225k
      lower_word += data[ data_offset++ ];
801
225k
      upper_word += lower_word;
802
803
225k
      lower_word += data[ data_offset++ ];
804
225k
      upper_word += lower_word;
805
806
225k
      lower_word += data[ data_offset++ ];
807
225k
      upper_word += lower_word;
808
809
225k
      lower_word += data[ data_offset++ ];
810
225k
      upper_word += lower_word;
811
812
225k
      lower_word += data[ data_offset++ ];
813
225k
      upper_word += lower_word;
814
815
225k
      lower_word += data[ data_offset++ ];
816
225k
      upper_word += lower_word;
817
818
225k
      lower_word += data[ data_offset++ ];
819
225k
      upper_word += lower_word;
820
821
225k
      lower_word += data[ data_offset++ ];
822
225k
      upper_word += lower_word;
823
824
225k
      lower_word += data[ data_offset++ ];
825
225k
      upper_word += lower_word;
826
827
225k
      lower_word += data[ data_offset++ ];
828
225k
      upper_word += lower_word;
829
830
225k
      lower_word += data[ data_offset++ ];
831
225k
      upper_word += lower_word;
832
833
225k
      lower_word += data[ data_offset++ ];
834
225k
      upper_word += lower_word;
835
836
225k
      lower_word += data[ data_offset++ ];
837
225k
      upper_word += lower_word;
838
839
225k
      lower_word += data[ data_offset++ ];
840
225k
      upper_word += lower_word;
841
842
225k
      data_size -= 16;
843
225k
    }
844
228k
    while( data_size > 0 )
845
205k
    {
846
205k
      lower_word += data[ data_offset++ ];
847
205k
      upper_word += lower_word;
848
849
205k
      data_size--;
850
205k
    }
851
    /* Optimized equivalent of:
852
     * lower_word %= 0xfff1
853
     */
854
22.8k
    value_32bit = lower_word >> 16;
855
22.8k
    lower_word &= 0x0000ffffUL;
856
22.8k
    lower_word += ( value_32bit << 4 ) - value_32bit;
857
858
22.8k
    if( lower_word > 65521 )
859
5
    {
860
5
      value_32bit = lower_word >> 16;
861
5
      lower_word &= 0x0000ffffUL;
862
5
      lower_word += ( value_32bit << 4 ) - value_32bit;
863
5
    }
864
22.8k
    if( lower_word >= 65521 )
865
6
    {
866
6
      lower_word -= 65521;
867
6
    }
868
    /* Optimized equivalent of:
869
     * upper_word %= 0xfff1
870
     */
871
22.8k
    value_32bit = upper_word >> 16;
872
22.8k
    upper_word &= 0x0000ffffUL;
873
22.8k
    upper_word += ( value_32bit << 4 ) - value_32bit;
874
875
22.8k
    if( upper_word > 65521 )
876
264
    {
877
264
      value_32bit = upper_word >> 16;
878
264
      upper_word &= 0x0000ffffUL;
879
264
      upper_word += ( value_32bit << 4 ) - value_32bit;
880
264
    }
881
22.8k
    if( upper_word >= 65521 )
882
14
    {
883
14
      upper_word -= 65521;
884
14
    }
885
22.8k
  }
886
22.9k
  *checksum_value = ( upper_word << 16 ) | lower_word;
887
888
22.9k
  return( 1 );
889
22.9k
}
890
891
/* Reads the compressed data header
892
 * Returns 1 on success or -1 on error
893
 */
894
int libewf_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
3.51k
{
900
3.51k
  static char *function                 = "libewf_deflate_read_data_header";
901
3.51k
  size_t safe_offset                    = 0;
902
3.51k
  uint32_t compression_window_size      = 0;
903
3.51k
  uint8_t flags                         = 0;
904
3.51k
  uint8_t compression_data              = 0;
905
3.51k
  uint8_t compression_information       = 0;
906
3.51k
  uint8_t compression_method            = 0;
907
3.51k
  uint8_t compression_window_bits       = 0;
908
909
#if defined( HAVE_DEBUG_OUTPUT )
910
  uint32_t preset_dictionary_identifier = 0;
911
#endif
912
913
3.51k
  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
3.51k
  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
3.51k
  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
3.51k
  safe_offset = *compressed_data_offset;
947
948
3.51k
  if( ( compressed_data_size < 2 )
949
3.51k
   || ( 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
3.51k
  compression_data = compressed_data[ safe_offset++ ];
961
3.51k
  flags            = compressed_data[ safe_offset++ ];
962
963
3.51k
  compression_method      = compression_data & 0x0f;
964
3.51k
  compression_information = compression_data >> 4;
965
966
/* TODO validate check bits */
967
3.51k
  if( ( flags & 0x20 ) != 0 )
968
188
  {
969
188
    if( ( compressed_data_size < 6 )
970
187
     || ( safe_offset > ( compressed_data_size - 6 ) ) )
971
4
    {
972
4
      libcerror_error_set(
973
4
       error,
974
4
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
975
4
       LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
976
4
       "%s: invalid compressed data value too small.",
977
4
       function );
978
979
4
      return( -1 );
980
4
    }
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
184
    safe_offset += 4;
987
184
  }
988
3.50k
  if( compression_method != 8 )
989
17
  {
990
17
    libcerror_error_set(
991
17
     error,
992
17
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
993
17
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
994
17
     "%s: unsupported compression method: %" PRIu8 ".",
995
17
     function,
996
17
     compression_method );
997
998
17
    return( -1 );
999
17
  }
1000
3.49k
  compression_window_bits = (uint8_t) compression_information + 8;
1001
3.49k
  compression_window_size = (uint32_t) 1UL << compression_window_bits;
1002
1003
3.49k
  if( compression_window_size > 32768 )
1004
1
  {
1005
1
    libcerror_error_set(
1006
1
     error,
1007
1
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1008
1
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
1009
1
     "%s: unsupported compression window size: %" PRIu32 ".",
1010
1
     function,
1011
1
     compression_window_size );
1012
1013
1
    return( -1 );
1014
1
  }
1015
3.49k
  *compressed_data_offset += safe_offset;
1016
1017
3.49k
  return( 1 );
1018
3.49k
}
1019
1020
/* Reads the header of a block of compressed data
1021
 * Returns 1 on success or -1 on error
1022
 */
1023
int libewf_deflate_read_block_header(
1024
     libewf_bit_stream_t *bit_stream,
1025
     uint8_t *block_type,
1026
     uint8_t *last_block_flag,
1027
     libcerror_error_t **error )
1028
14.4k
{
1029
14.4k
  static char *function = "libewf_deflate_read_block_header";
1030
14.4k
  uint32_t value_32bit  = 0;
1031
1032
14.4k
  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
14.4k
  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
14.4k
  if( libewf_bit_stream_get_value(
1055
14.4k
       bit_stream,
1056
14.4k
       3,
1057
14.4k
       &value_32bit,
1058
14.4k
       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
14.4k
  *last_block_flag = (uint8_t) ( value_32bit & 0x00000001UL );
1070
14.4k
  value_32bit    >>= 1;
1071
14.4k
  *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 LIBEWF_DEFLATE_BLOCK_TYPE_UNCOMPRESSED:
1089
        libcnotify_printf(
1090
         "Uncompressed" );
1091
        break;
1092
1093
      case LIBEWF_DEFLATE_BLOCK_TYPE_HUFFMAN_FIXED:
1094
        libcnotify_printf(
1095
         "Fixed Huffman" );
1096
        break;
1097
1098
      case LIBEWF_DEFLATE_BLOCK_TYPE_HUFFMAN_DYNAMIC:
1099
        libcnotify_printf(
1100
         "Dynamic Huffman" );
1101
        break;
1102
1103
      case LIBEWF_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
14.4k
  return( 1 );
1118
14.4k
}
1119
1120
/* Reads a block of compressed data
1121
 * Returns 1 on success or -1 on error
1122
 */
1123
int libewf_deflate_read_block(
1124
     libewf_bit_stream_t *bit_stream,
1125
     uint8_t block_type,
1126
     libewf_huffman_tree_t *fixed_huffman_literals_tree,
1127
     libewf_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
14.4k
{
1133
14.4k
  libewf_huffman_tree_t *dynamic_huffman_distances_tree = NULL;
1134
14.4k
  libewf_huffman_tree_t *dynamic_huffman_literals_tree  = NULL;
1135
14.4k
  static char *function                                 = "libewf_deflate_read_block";
1136
14.4k
  size_t safe_uncompressed_data_offset                  = 0;
1137
14.4k
  uint32_t block_size                                   = 0;
1138
14.4k
  uint32_t block_size_copy                              = 0;
1139
14.4k
  uint32_t value_32bit                                  = 0;
1140
14.4k
  uint8_t skip_bits                                     = 0;
1141
1142
14.4k
  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
14.4k
  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
14.4k
  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
14.4k
  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
14.4k
  safe_uncompressed_data_offset = *uncompressed_data_offset;
1187
1188
14.4k
  switch( block_type )
1189
14.4k
  {
1190
4.67k
    case LIBEWF_DEFLATE_BLOCK_TYPE_UNCOMPRESSED:
1191
      /* Ignore the bits in the buffer upto the next byte
1192
       */
1193
4.67k
      skip_bits = bit_stream->bit_buffer_size & 0x07;
1194
1195
4.67k
      if( skip_bits > 0 )
1196
1.98k
      {
1197
1.98k
        if( libewf_bit_stream_get_value(
1198
1.98k
             bit_stream,
1199
1.98k
             skip_bits,
1200
1.98k
             &value_32bit,
1201
1.98k
             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
1.98k
      }
1213
4.67k
      if( libewf_bit_stream_get_value(
1214
4.67k
           bit_stream,
1215
4.67k
           32,
1216
4.67k
           &block_size,
1217
4.67k
           error ) != 1 )
1218
7
      {
1219
7
        libcerror_error_set(
1220
7
         error,
1221
7
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1222
7
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1223
7
         "%s: unable to retrieve value from bit stream.",
1224
7
         function );
1225
1226
7
        goto on_error;
1227
7
      }
1228
4.66k
      block_size_copy = ( block_size >> 16 ) ^ 0x0000ffffUL;
1229
4.66k
      block_size     &= 0x0000ffffUL;
1230
1231
4.66k
      if( block_size != block_size_copy )
1232
40
      {
1233
40
        libcerror_error_set(
1234
40
         error,
1235
40
         LIBCERROR_ERROR_DOMAIN_INPUT,
1236
40
         LIBCERROR_INPUT_ERROR_VALUE_MISMATCH,
1237
40
         "%s: mismatch in block size ( %" PRIu32 " != %" PRIu32 " ).",
1238
40
         function,
1239
40
         block_size,
1240
40
         block_size_copy );
1241
1242
40
        goto on_error;
1243
40
      }
1244
4.62k
      if( block_size == 0 )
1245
757
      {
1246
757
        break;
1247
757
      }
1248
3.86k
      if( (size_t) block_size > ( bit_stream->byte_stream_size - bit_stream->byte_stream_offset ) )
1249
20
      {
1250
20
        libcerror_error_set(
1251
20
         error,
1252
20
         LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1253
20
         LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
1254
20
         "%s: invalid compressed data value too small.",
1255
20
         function );
1256
1257
20
        goto on_error;
1258
20
      }
1259
3.84k
      if( (size_t) block_size > ( uncompressed_data_size - safe_uncompressed_data_offset ) )
1260
1
      {
1261
1
        libcerror_error_set(
1262
1
         error,
1263
1
         LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1264
1
         LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
1265
1
         "%s: invalid uncompressed data value too small.",
1266
1
         function );
1267
1268
1
        goto on_error;
1269
1
      }
1270
3.84k
      if( memory_copy(
1271
3.84k
           &( uncompressed_data[ safe_uncompressed_data_offset ] ),
1272
3.84k
           &( bit_stream->byte_stream[ bit_stream->byte_stream_offset ] ),
1273
3.84k
           (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
3.84k
      bit_stream->byte_stream_offset += block_size;
1285
3.84k
      safe_uncompressed_data_offset  += block_size;
1286
1287
      /* Flush the bit stream buffer
1288
       */
1289
3.84k
      bit_stream->bit_buffer      = 0;
1290
3.84k
      bit_stream->bit_buffer_size = 0;
1291
1292
3.84k
      break;
1293
1294
5.36k
    case LIBEWF_DEFLATE_BLOCK_TYPE_HUFFMAN_FIXED:
1295
5.36k
      if( libewf_deflate_decode_huffman(
1296
5.36k
           bit_stream,
1297
5.36k
           fixed_huffman_literals_tree,
1298
5.36k
           fixed_huffman_distances_tree,
1299
5.36k
           uncompressed_data,
1300
5.36k
           uncompressed_data_size,
1301
5.36k
           &safe_uncompressed_data_offset,
1302
5.36k
           error ) != 1 )
1303
43
      {
1304
43
        libcerror_error_set(
1305
43
         error,
1306
43
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1307
43
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1308
43
         "%s: unable to decode fixed Huffman encoded bit stream.",
1309
43
         function );
1310
1311
43
        goto on_error;
1312
43
      }
1313
5.32k
      break;
1314
1315
5.32k
    case LIBEWF_DEFLATE_BLOCK_TYPE_HUFFMAN_DYNAMIC:
1316
4.41k
      if( libewf_huffman_tree_initialize(
1317
4.41k
           &dynamic_huffman_literals_tree,
1318
4.41k
           288,
1319
4.41k
           15,
1320
4.41k
           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
4.41k
      if( libewf_huffman_tree_initialize(
1332
4.41k
           &dynamic_huffman_distances_tree,
1333
4.41k
           30,
1334
4.41k
           15,
1335
4.41k
           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
4.41k
      if( libewf_deflate_build_dynamic_huffman_trees(
1347
4.41k
           bit_stream,
1348
4.41k
           dynamic_huffman_literals_tree,
1349
4.41k
           dynamic_huffman_distances_tree,
1350
4.41k
           error ) != 1 )
1351
88
      {
1352
88
        libcerror_error_set(
1353
88
         error,
1354
88
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1355
88
         LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1356
88
         "%s: unable to build dynamic Huffman trees.",
1357
88
         function );
1358
1359
88
        goto on_error;
1360
88
      }
1361
4.32k
      if( libewf_deflate_decode_huffman(
1362
4.32k
           bit_stream,
1363
4.32k
           dynamic_huffman_literals_tree,
1364
4.32k
           dynamic_huffman_distances_tree,
1365
4.32k
           uncompressed_data,
1366
4.32k
           uncompressed_data_size,
1367
4.32k
           &safe_uncompressed_data_offset,
1368
4.32k
           error ) != 1 )
1369
10
      {
1370
10
        libcerror_error_set(
1371
10
         error,
1372
10
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1373
10
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1374
10
         "%s: unable to decode dynamic Huffman encoded bit stream.",
1375
10
         function );
1376
1377
10
        goto on_error;
1378
10
      }
1379
4.31k
      if( libewf_huffman_tree_free(
1380
4.31k
           &dynamic_huffman_distances_tree,
1381
4.31k
           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
4.31k
      if( libewf_huffman_tree_free(
1393
4.31k
           &dynamic_huffman_literals_tree,
1394
4.31k
           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
4.31k
      break;
1406
1407
4.31k
    case LIBEWF_DEFLATE_BLOCK_TYPE_RESERVED:
1408
5
    default:
1409
5
      libcerror_error_set(
1410
5
       error,
1411
5
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1412
5
       LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
1413
5
       "%s: unsupported block type.",
1414
5
       function );
1415
1416
5
      goto on_error;
1417
14.4k
  }
1418
14.2k
  *uncompressed_data_offset = safe_uncompressed_data_offset;
1419
1420
14.2k
  return( 1 );
1421
1422
214
on_error:
1423
214
  if( dynamic_huffman_distances_tree != NULL )
1424
98
  {
1425
98
    libewf_huffman_tree_free(
1426
98
     &dynamic_huffman_distances_tree,
1427
98
     NULL );
1428
98
  }
1429
214
  if( dynamic_huffman_literals_tree != NULL )
1430
98
  {
1431
98
    libewf_huffman_tree_free(
1432
98
     &dynamic_huffman_literals_tree,
1433
98
     NULL );
1434
98
  }
1435
214
  return( -1 );
1436
14.4k
}
1437
1438
/* Decompresses data using deflate compression
1439
 * Returns 1 on success or -1 on error
1440
 */
1441
int libewf_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
  libewf_bit_stream_t *bit_stream                     = NULL;
1449
0
  libewf_huffman_tree_t *fixed_huffman_distances_tree = NULL;
1450
0
  libewf_huffman_tree_t *fixed_huffman_literals_tree  = NULL;
1451
0
  static char *function                               = "libewf_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( libewf_bit_stream_initialize(
1527
0
       &bit_stream,
1528
0
       compressed_data,
1529
0
       compressed_data_size,
1530
0
       compressed_data_offset,
1531
0
       LIBEWF_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( libewf_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 == LIBEWF_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( libewf_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( libewf_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( libewf_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( libewf_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( libewf_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( libewf_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( libewf_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
    libewf_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
    libewf_huffman_tree_free(
1694
0
     &fixed_huffman_literals_tree,
1695
0
     NULL );
1696
0
  }
1697
0
  if( bit_stream != NULL )
1698
0
  {
1699
0
    libewf_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 libewf_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
3.51k
{
1716
3.51k
  libewf_bit_stream_t *bit_stream                     = NULL;
1717
3.51k
  libewf_huffman_tree_t *fixed_huffman_distances_tree = NULL;
1718
3.51k
  libewf_huffman_tree_t *fixed_huffman_literals_tree  = NULL;
1719
3.51k
  static char *function                               = "libewf_deflate_decompress_zlib";
1720
3.51k
  size_t compressed_data_offset                       = 0;
1721
3.51k
  size_t safe_uncompressed_data_size                  = 0;
1722
3.51k
  size_t uncompressed_data_offset                     = 0;
1723
3.51k
  uint32_t calculated_checksum                        = 0;
1724
3.51k
  uint32_t stored_checksum                            = 0;
1725
3.51k
  uint8_t block_type                                  = 0;
1726
3.51k
  uint8_t last_block_flag                             = 0;
1727
1728
3.51k
  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
3.51k
  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
3.51k
  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
3.51k
  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
3.51k
  safe_uncompressed_data_size = *uncompressed_data_size;
1773
1774
3.51k
  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
3.51k
  if( libewf_deflate_read_data_header(
1786
3.51k
       compressed_data,
1787
3.51k
       compressed_data_size,
1788
3.51k
       &compressed_data_offset,
1789
3.51k
       error ) != 1 )
1790
22
  {
1791
22
    libcerror_error_set(
1792
22
     error,
1793
22
     LIBCERROR_ERROR_DOMAIN_IO,
1794
22
     LIBCERROR_IO_ERROR_READ_FAILED,
1795
22
     "%s: unable to read data header.",
1796
22
     function );
1797
1798
22
    goto on_error;
1799
22
  }
1800
3.49k
  if( compressed_data_offset >= compressed_data_size )
1801
3
  {
1802
3
    libcerror_error_set(
1803
3
     error,
1804
3
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1805
3
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
1806
3
     "%s: invalid compressed data value too small.",
1807
3
     function );
1808
1809
3
    goto on_error;
1810
3
  }
1811
3.48k
  if( libewf_bit_stream_initialize(
1812
3.48k
       &bit_stream,
1813
3.48k
       compressed_data,
1814
3.48k
       compressed_data_size,
1815
3.48k
       compressed_data_offset,
1816
3.48k
       LIBEWF_BIT_STREAM_STORAGE_TYPE_BYTE_BACK_TO_FRONT,
1817
3.48k
       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
14.5k
  while( bit_stream->byte_stream_offset < bit_stream->byte_stream_size )
1829
14.4k
  {
1830
14.4k
    if( libewf_deflate_read_block_header(
1831
14.4k
         bit_stream,
1832
14.4k
         &block_type,
1833
14.4k
         &last_block_flag,
1834
14.4k
         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
14.4k
    if( block_type == LIBEWF_DEFLATE_BLOCK_TYPE_HUFFMAN_FIXED )
1846
5.36k
    {
1847
5.36k
      if( ( fixed_huffman_literals_tree == NULL )
1848
491
       && ( fixed_huffman_distances_tree == NULL ) )
1849
491
      {
1850
491
        if( libewf_huffman_tree_initialize(
1851
491
             &fixed_huffman_literals_tree,
1852
491
             288,
1853
491
             15,
1854
491
             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
491
        if( libewf_huffman_tree_initialize(
1866
491
             &fixed_huffman_distances_tree,
1867
491
             30,
1868
491
             15,
1869
491
             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
491
        if( libewf_deflate_build_fixed_huffman_trees(
1881
491
             fixed_huffman_literals_tree,
1882
491
             fixed_huffman_distances_tree,
1883
491
             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
491
      }
1895
5.36k
    }
1896
14.4k
    if( libewf_deflate_read_block(
1897
14.4k
         bit_stream,
1898
14.4k
         block_type,
1899
14.4k
         fixed_huffman_literals_tree,
1900
14.4k
         fixed_huffman_distances_tree,
1901
14.4k
         uncompressed_data,
1902
14.4k
         safe_uncompressed_data_size,
1903
14.4k
         &uncompressed_data_offset,
1904
14.4k
         error ) != 1 )
1905
214
    {
1906
214
      libcerror_error_set(
1907
214
       error,
1908
214
       LIBCERROR_ERROR_DOMAIN_IO,
1909
214
       LIBCERROR_IO_ERROR_READ_FAILED,
1910
214
       "%s: unable to read block of compressed data.",
1911
214
       function );
1912
1913
214
      goto on_error;
1914
214
    }
1915
14.2k
    if( last_block_flag != 0 )
1916
3.14k
    {
1917
3.14k
      break;
1918
3.14k
    }
1919
14.2k
  }
1920
3.27k
  if( ( bit_stream->byte_stream_size - bit_stream->byte_stream_offset ) >= 4 )
1921
2.53k
  {
1922
2.53k
    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
2.53k
    byte_stream_copy_to_uint32_big_endian(
1928
2.53k
     &( bit_stream->byte_stream[ bit_stream->byte_stream_offset ] ),
1929
2.53k
     stored_checksum );
1930
1931
2.53k
    if( libewf_deflate_calculate_adler32(
1932
2.53k
         &calculated_checksum,
1933
2.53k
         uncompressed_data,
1934
2.53k
         uncompressed_data_offset,
1935
2.53k
         1,
1936
2.53k
         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
2.53k
    if( stored_checksum != calculated_checksum )
1948
178
    {
1949
178
      libcerror_error_set(
1950
178
       error,
1951
178
       LIBCERROR_ERROR_DOMAIN_INPUT,
1952
178
       LIBCERROR_INPUT_ERROR_CHECKSUM_MISMATCH,
1953
178
       "%s: checksum does not match (stored: 0x%08" PRIx32 ", calculated: 0x%08" PRIx32 ").",
1954
178
       function,
1955
178
       stored_checksum,
1956
178
       calculated_checksum );
1957
1958
178
      goto on_error;
1959
178
    }
1960
2.53k
  }
1961
3.09k
  if( fixed_huffman_distances_tree != NULL )
1962
263
  {
1963
263
    if( libewf_huffman_tree_free(
1964
263
         &fixed_huffman_distances_tree,
1965
263
         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
263
  }
1977
3.09k
  if( fixed_huffman_literals_tree != NULL )
1978
263
  {
1979
263
    if( libewf_huffman_tree_free(
1980
263
         &fixed_huffman_literals_tree,
1981
263
         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
263
  }
1993
3.09k
  if( libewf_bit_stream_free(
1994
3.09k
       &bit_stream,
1995
3.09k
       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
3.09k
  *uncompressed_data_size = uncompressed_data_offset;
2007
2008
3.09k
  return( 1 );
2009
2010
417
on_error:
2011
417
  if( fixed_huffman_distances_tree != NULL )
2012
228
  {
2013
228
    libewf_huffman_tree_free(
2014
228
     &fixed_huffman_distances_tree,
2015
228
     NULL );
2016
228
  }
2017
417
  if( fixed_huffman_literals_tree != NULL )
2018
228
  {
2019
228
    libewf_huffman_tree_free(
2020
228
     &fixed_huffman_literals_tree,
2021
228
     NULL );
2022
228
  }
2023
417
  if( bit_stream != NULL )
2024
392
  {
2025
392
    libewf_bit_stream_free(
2026
392
     &bit_stream,
2027
     NULL );
2028
392
  }
2029
417
  return( -1 );
2030
3.09k
}
2031