Coverage Report

Created: 2025-06-13 07:22

/src/libewf/libewf/libewf_deflate.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Deflate (zlib) (un)compression functions
3
 *
4
 * Copyright (C) 2006-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 "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.54k
{
63
4.54k
  uint8_t code_size_array[ 316 ];
64
65
4.54k
  libewf_huffman_tree_t *codes_tree = NULL;
66
4.54k
  static char *function             = "libewf_deflate_build_dynamic_huffman_trees";
67
4.54k
  uint32_t code_size                = 0;
68
4.54k
  uint32_t code_size_index          = 0;
69
4.54k
  uint32_t code_size_sequence       = 0;
70
4.54k
  uint32_t number_of_code_sizes     = 0;
71
4.54k
  uint32_t number_of_distance_codes = 0;
72
4.54k
  uint32_t number_of_literal_codes  = 0;
73
4.54k
  uint32_t times_to_repeat          = 0;
74
4.54k
  uint16_t symbol                   = 0;
75
76
4.54k
  if( libewf_bit_stream_get_value(
77
4.54k
       bit_stream,
78
4.54k
       14,
79
4.54k
       &number_of_code_sizes,
80
4.54k
       error ) != 1 )
81
3
  {
82
3
    libcerror_error_set(
83
3
     error,
84
3
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
85
3
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
86
3
     "%s: unable to retrieve value from bit stream.",
87
3
     function );
88
89
3
    goto on_error;
90
3
  }
91
4.54k
  number_of_literal_codes  = number_of_code_sizes & 0x0000001fUL;
92
4.54k
  number_of_code_sizes   >>= 5;
93
4.54k
  number_of_distance_codes = number_of_code_sizes & 0x0000001fUL;
94
4.54k
  number_of_code_sizes   >>= 5;
95
96
4.54k
  number_of_literal_codes += 257;
97
98
4.54k
  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.54k
  number_of_distance_codes += 1;
110
111
4.54k
  if( number_of_distance_codes > 30 )
112
2
  {
113
2
    libcerror_error_set(
114
2
     error,
115
2
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
116
2
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
117
2
     "%s: invalid number of distance codes value out of bounds.",
118
2
     function );
119
120
2
    goto on_error;
121
2
  }
122
4.54k
  number_of_code_sizes += 4;
123
124
4.54k
  for( code_size_index = 0;
125
78.5k
       code_size_index < number_of_code_sizes;
126
73.9k
       code_size_index++ )
127
74.0k
  {
128
74.0k
    if( libewf_bit_stream_get_value(
129
74.0k
         bit_stream,
130
74.0k
         3,
131
74.0k
         &code_size,
132
74.0k
         error ) != 1 )
133
6
    {
134
6
      libcerror_error_set(
135
6
       error,
136
6
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
137
6
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
138
6
       "%s: unable to retrieve value from bit stream.",
139
6
       function );
140
141
6
      goto on_error;
142
6
    }
143
73.9k
    code_size_sequence = libewf_deflate_code_sizes_sequence[ code_size_index ];
144
145
73.9k
    code_size_array[ code_size_sequence ] = (uint8_t) code_size;
146
73.9k
  }
147
16.7k
  while( code_size_index < 19 )
148
12.1k
  {
149
12.1k
    code_size_sequence = libewf_deflate_code_sizes_sequence[ code_size_index++ ];
150
151
12.1k
    code_size_array[ code_size_sequence ] = 0;
152
12.1k
  }
153
4.53k
  if( libewf_huffman_tree_initialize(
154
4.53k
       &codes_tree,
155
4.53k
       19,
156
4.53k
       15,
157
4.53k
       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.53k
  if( libewf_huffman_tree_build(
169
4.53k
       codes_tree,
170
4.53k
       code_size_array,
171
4.53k
       19,
172
4.53k
       error ) != 1 )
173
15
  {
174
15
    libcerror_error_set(
175
15
     error,
176
15
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
177
15
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
178
15
     "%s: unable to build codes tree.",
179
15
     function );
180
181
15
    goto on_error;
182
15
  }
183
4.52k
  number_of_code_sizes = number_of_literal_codes + number_of_distance_codes;
184
185
4.52k
  code_size_index = 0;
186
187
306k
  while( code_size_index < number_of_code_sizes )
188
301k
  {
189
301k
    if( libewf_huffman_tree_get_symbol_from_bit_stream(
190
301k
         codes_tree,
191
301k
         bit_stream,
192
301k
         &symbol,
193
301k
         error ) != 1 )
194
26
    {
195
26
      libcerror_error_set(
196
26
       error,
197
26
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
198
26
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
199
26
       "%s: unable to retrieve literal value from bit stream.",
200
26
       function );
201
202
26
      goto on_error;
203
26
    }
204
301k
    if( symbol < 16 )
205
263k
    {
206
263k
      code_size_array[ code_size_index++ ] = (uint8_t) symbol;
207
208
263k
      continue;
209
263k
    }
210
38.1k
    code_size = 0;
211
212
38.1k
    if( symbol == 16 )
213
6.94k
    {
214
6.94k
      if( code_size_index == 0 )
215
2
      {
216
2
        libcerror_error_set(
217
2
         error,
218
2
         LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
219
2
         LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
220
2
         "%s: invalid code size index value out of bounds.",
221
2
         function );
222
223
2
        goto on_error;
224
2
      }
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
2
      {
233
2
        libcerror_error_set(
234
2
         error,
235
2
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
236
2
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
237
2
         "%s: unable to retrieve value from bit stream.",
238
2
         function );
239
240
2
        goto on_error;
241
2
      }
242
6.93k
      times_to_repeat += 3;
243
6.93k
    }
244
31.1k
    else if( symbol == 17 )
245
12.1k
    {
246
12.1k
      if( libewf_bit_stream_get_value(
247
12.1k
           bit_stream,
248
12.1k
           3,
249
12.1k
           &times_to_repeat,
250
12.1k
           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
12.1k
      times_to_repeat += 3;
262
12.1k
    }
263
19.0k
    else if( symbol == 18 )
264
19.0k
    {
265
19.0k
      if( libewf_bit_stream_get_value(
266
19.0k
           bit_stream,
267
19.0k
           7,
268
19.0k
           &times_to_repeat,
269
19.0k
           error ) != 1 )
270
2
      {
271
2
        libcerror_error_set(
272
2
         error,
273
2
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
274
2
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
275
2
         "%s: unable to retrieve value from bit stream.",
276
2
         function );
277
278
2
        goto on_error;
279
2
      }
280
19.0k
      times_to_repeat += 11;
281
19.0k
    }
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
38.1k
    if( ( code_size_index + times_to_repeat ) > number_of_code_sizes )
294
8
    {
295
8
      libcerror_error_set(
296
8
       error,
297
8
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
298
8
       LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
299
8
       "%s: invalid times to repeat value out of bounds.",
300
8
       function );
301
302
8
      goto on_error;
303
8
    }
304
1.05M
    while( times_to_repeat > 0 )
305
1.01M
    {
306
1.01M
      code_size_array[ code_size_index++ ] = (uint8_t) code_size;
307
308
1.01M
      times_to_repeat--;
309
1.01M
    }
310
38.1k
  }
311
4.47k
  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.47k
  if( libewf_huffman_tree_free(
323
4.47k
       &codes_tree,
324
4.47k
       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.47k
  if( libewf_huffman_tree_build(
336
4.47k
       literals_tree,
337
4.47k
       code_size_array,
338
4.47k
       number_of_literal_codes,
339
4.47k
       error ) != 1 )
340
11
  {
341
11
    libcerror_error_set(
342
11
     error,
343
11
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
344
11
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
345
11
     "%s: unable to build literals tree.",
346
11
     function );
347
348
11
    goto on_error;
349
11
  }
350
4.46k
  if( libewf_huffman_tree_build(
351
4.46k
       distances_tree,
352
4.46k
       &( code_size_array[ number_of_literal_codes ] ),
353
4.46k
       number_of_distance_codes,
354
4.46k
       error ) != 1 )
355
5
  {
356
5
    libcerror_error_set(
357
5
     error,
358
5
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
359
5
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
360
5
     "%s: unable to build distances tree.",
361
5
     function );
362
363
5
    goto on_error;
364
5
  }
365
4.46k
  return( 1 );
366
367
88
on_error:
368
88
  if( codes_tree != NULL )
369
59
  {
370
59
    libewf_huffman_tree_free(
371
59
     &codes_tree,
372
59
     NULL );
373
59
  }
374
88
  return( -1 );
375
4.46k
}
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
479
{
385
479
  uint8_t code_size_array[ 318 ];
386
387
479
  static char *function = "libewf_deflate_build_fixed_huffman_trees";
388
479
  uint16_t symbol       = 0;
389
390
479
  for( symbol = 0;
391
152k
       symbol < 318;
392
152k
       symbol++ )
393
152k
  {
394
152k
    if( symbol < 144 )
395
68.9k
    {
396
68.9k
      code_size_array[ symbol ] = 8;
397
68.9k
    }
398
83.3k
    else if( symbol < 256 )
399
53.6k
    {
400
53.6k
      code_size_array[ symbol ] = 9;
401
53.6k
    }
402
29.6k
    else if( symbol < 280 )
403
11.4k
    {
404
11.4k
      code_size_array[ symbol ] = 7;
405
11.4k
    }
406
18.2k
    else if( symbol < 288 )
407
3.83k
    {
408
3.83k
      code_size_array[ symbol ] = 8;
409
3.83k
    }
410
14.3k
    else
411
14.3k
    {
412
14.3k
      code_size_array[ symbol ] = 5;
413
14.3k
    }
414
152k
  }
415
479
  if( libewf_huffman_tree_build(
416
479
       literals_tree,
417
479
       code_size_array,
418
479
       288,
419
479
       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
479
  if( libewf_huffman_tree_build(
431
479
       distances_tree,
432
479
       &( code_size_array[ 288 ] ),
433
479
       30,
434
479
       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
479
  return( 1 );
446
479
}
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
11.7k
{
460
11.7k
  static char *function         = "libewf_deflate_decode_huffman";
461
11.7k
  size_t data_offset            = 0;
462
11.7k
  uint32_t extra_bits           = 0;
463
11.7k
  uint16_t compression_offset   = 0;
464
11.7k
  uint16_t compression_size     = 0;
465
11.7k
  uint16_t number_of_extra_bits = 0;
466
11.7k
  uint16_t symbol               = 0;
467
468
11.7k
  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
11.7k
  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
11.7k
  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
11.7k
  data_offset = *uncompressed_data_offset;
502
503
11.7k
  do
504
8.58M
  {
505
8.58M
    if( libewf_huffman_tree_get_symbol_from_bit_stream(
506
8.58M
         literals_tree,
507
8.58M
         bit_stream,
508
8.58M
         &symbol,
509
8.58M
         error ) != 1 )
510
14
    {
511
14
      libcerror_error_set(
512
14
       error,
513
14
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
514
14
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
515
14
       "%s: unable to retrieve literal value from bit stream.",
516
14
       function );
517
518
14
      return( -1 );
519
14
    }
520
8.58M
    if( symbol < 256 )
521
546k
    {
522
546k
      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
546k
      uncompressed_data[ data_offset++ ] = (uint8_t) symbol;
534
546k
    }
535
8.03M
    else if( ( symbol > 256 )
536
8.03M
          && ( symbol < 286 ) )
537
8.02M
    {
538
8.02M
      symbol -= 257;
539
540
8.02M
      number_of_extra_bits = libewf_deflate_literal_codes_number_of_extra_bits[ symbol ];
541
542
8.02M
      if( libewf_bit_stream_get_value(
543
8.02M
           bit_stream,
544
8.02M
           (uint8_t) number_of_extra_bits,
545
8.02M
           &extra_bits,
546
8.02M
           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
8.02M
      compression_size = libewf_deflate_literal_codes_base[ symbol ] + (uint16_t) extra_bits;
558
559
8.02M
      if( libewf_huffman_tree_get_symbol_from_bit_stream(
560
8.02M
           distances_tree,
561
8.02M
           bit_stream,
562
8.02M
           &symbol,
563
8.02M
           error ) != 1 )
564
8
      {
565
8
        libcerror_error_set(
566
8
         error,
567
8
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
568
8
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
569
8
         "%s: unable to retrieve distance value from bit stream.",
570
8
         function );
571
572
8
        return( -1 );
573
8
      }
574
8.02M
      number_of_extra_bits = libewf_deflate_distance_codes_number_of_extra_bits[ symbol ];
575
576
8.02M
      if( libewf_bit_stream_get_value(
577
8.02M
           bit_stream,
578
8.02M
           (uint8_t) number_of_extra_bits,
579
8.02M
           &extra_bits,
580
8.02M
           error ) != 1 )
581
3
      {
582
3
        libcerror_error_set(
583
3
         error,
584
3
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
585
3
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
586
3
         "%s: unable to retrieve distance extra value from bit stream.",
587
3
         function );
588
589
3
        return( -1 );
590
3
      }
591
8.02M
      compression_offset = libewf_deflate_distance_codes_base[ symbol ] + (uint16_t) extra_bits;
592
593
8.02M
      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
8.02M
      if( ( data_offset + compression_size ) > uncompressed_data_size )
605
9
      {
606
9
        libcerror_error_set(
607
9
         error,
608
9
         LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
609
9
         LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
610
9
         "%s: invalid uncompressed data value too small.",
611
9
         function );
612
613
9
        return( -1 );
614
9
      }
615
33.1M
      while( compression_size > 0 )
616
25.1M
      {
617
25.1M
        uncompressed_data[ data_offset ] = uncompressed_data[ data_offset - compression_offset ];
618
619
25.1M
        data_offset++;
620
25.1M
        compression_size--;
621
25.1M
      }
622
8.02M
    }
623
11.6k
    else if( symbol != 256 )
624
2
    {
625
2
      libcerror_error_set(
626
2
       error,
627
2
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
628
2
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
629
2
       "%s: invalid code value: %" PRIu16 ".",
630
2
       function,
631
2
       symbol );
632
633
2
      return( -1 );
634
2
    }
635
8.58M
  }
636
8.58M
  while( symbol != 256 );
637
638
11.6k
  *uncompressed_data_offset = data_offset;
639
640
11.6k
  return( 1 );
641
11.7k
}
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
24.2k
{
654
24.2k
  static char *function = "libewf_deflate_calculate_adler32";
655
24.2k
  size_t data_offset    = 0;
656
24.2k
  uint32_t lower_word   = 0;
657
24.2k
  uint32_t upper_word   = 0;
658
24.2k
  uint32_t value_32bit  = 0;
659
24.2k
  int block_index       = 0;
660
661
24.2k
  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
24.2k
  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
24.2k
  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
24.2k
  lower_word = initial_value & 0xffff;
695
24.2k
  upper_word = ( initial_value >> 16 ) & 0xffff;
696
697
28.3k
  while( data_size >= 0x15b0 )
698
4.10k
  {
699
    /* The modulo calculation is needed per 5552 (0x15b0) bytes
700
     * 5552 / 16 = 347
701
     */
702
4.10k
    for( block_index = 0;
703
1.42M
         block_index < 347;
704
1.42M
         block_index++ )
705
1.42M
    {
706
1.42M
      lower_word += data[ data_offset++ ];
707
1.42M
      upper_word += lower_word;
708
709
1.42M
      lower_word += data[ data_offset++ ];
710
1.42M
      upper_word += lower_word;
711
712
1.42M
      lower_word += data[ data_offset++ ];
713
1.42M
      upper_word += lower_word;
714
715
1.42M
      lower_word += data[ data_offset++ ];
716
1.42M
      upper_word += lower_word;
717
718
1.42M
      lower_word += data[ data_offset++ ];
719
1.42M
      upper_word += lower_word;
720
721
1.42M
      lower_word += data[ data_offset++ ];
722
1.42M
      upper_word += lower_word;
723
724
1.42M
      lower_word += data[ data_offset++ ];
725
1.42M
      upper_word += lower_word;
726
727
1.42M
      lower_word += data[ data_offset++ ];
728
1.42M
      upper_word += lower_word;
729
730
1.42M
      lower_word += data[ data_offset++ ];
731
1.42M
      upper_word += lower_word;
732
733
1.42M
      lower_word += data[ data_offset++ ];
734
1.42M
      upper_word += lower_word;
735
736
1.42M
      lower_word += data[ data_offset++ ];
737
1.42M
      upper_word += lower_word;
738
739
1.42M
      lower_word += data[ data_offset++ ];
740
1.42M
      upper_word += lower_word;
741
742
1.42M
      lower_word += data[ data_offset++ ];
743
1.42M
      upper_word += lower_word;
744
745
1.42M
      lower_word += data[ data_offset++ ];
746
1.42M
      upper_word += lower_word;
747
748
1.42M
      lower_word += data[ data_offset++ ];
749
1.42M
      upper_word += lower_word;
750
751
1.42M
      lower_word += data[ data_offset++ ];
752
1.42M
      upper_word += lower_word;
753
1.42M
    }
754
    /* Optimized equivalent of:
755
     * lower_word %= 0xfff1
756
     */
757
4.10k
    value_32bit = lower_word >> 16;
758
4.10k
    lower_word &= 0x0000ffffUL;
759
4.10k
    lower_word += ( value_32bit << 4 ) - value_32bit;
760
761
4.10k
    if( lower_word > 65521 )
762
45
    {
763
45
      value_32bit = lower_word >> 16;
764
45
      lower_word &= 0x0000ffffUL;
765
45
      lower_word += ( value_32bit << 4 ) - value_32bit;
766
45
    }
767
4.10k
    if( lower_word >= 65521 )
768
16
    {
769
16
      lower_word -= 65521;
770
16
    }
771
    /* Optimized equivalent of:
772
     * upper_word %= 0xfff1
773
     */
774
4.10k
    value_32bit = upper_word >> 16;
775
4.10k
    upper_word &= 0x0000ffffUL;
776
4.10k
    upper_word += ( value_32bit << 4 ) - value_32bit;
777
778
4.10k
    if( upper_word > 65521 )
779
3.76k
    {
780
3.76k
      value_32bit = upper_word >> 16;
781
3.76k
      upper_word &= 0x0000ffffUL;
782
3.76k
      upper_word += ( value_32bit << 4 ) - value_32bit;
783
3.76k
    }
784
4.10k
    if( upper_word >= 65521 )
785
24
    {
786
24
      upper_word -= 65521;
787
24
    }
788
4.10k
    data_size -= 0x15b0;
789
4.10k
  }
790
24.2k
  if( data_size > 0 )
791
24.2k
  {
792
268k
    while( data_size > 16 )
793
244k
    {
794
244k
      lower_word += data[ data_offset++ ];
795
244k
      upper_word += lower_word;
796
797
244k
      lower_word += data[ data_offset++ ];
798
244k
      upper_word += lower_word;
799
800
244k
      lower_word += data[ data_offset++ ];
801
244k
      upper_word += lower_word;
802
803
244k
      lower_word += data[ data_offset++ ];
804
244k
      upper_word += lower_word;
805
806
244k
      lower_word += data[ data_offset++ ];
807
244k
      upper_word += lower_word;
808
809
244k
      lower_word += data[ data_offset++ ];
810
244k
      upper_word += lower_word;
811
812
244k
      lower_word += data[ data_offset++ ];
813
244k
      upper_word += lower_word;
814
815
244k
      lower_word += data[ data_offset++ ];
816
244k
      upper_word += lower_word;
817
818
244k
      lower_word += data[ data_offset++ ];
819
244k
      upper_word += lower_word;
820
821
244k
      lower_word += data[ data_offset++ ];
822
244k
      upper_word += lower_word;
823
824
244k
      lower_word += data[ data_offset++ ];
825
244k
      upper_word += lower_word;
826
827
244k
      lower_word += data[ data_offset++ ];
828
244k
      upper_word += lower_word;
829
830
244k
      lower_word += data[ data_offset++ ];
831
244k
      upper_word += lower_word;
832
833
244k
      lower_word += data[ data_offset++ ];
834
244k
      upper_word += lower_word;
835
836
244k
      lower_word += data[ data_offset++ ];
837
244k
      upper_word += lower_word;
838
839
244k
      lower_word += data[ data_offset++ ];
840
244k
      upper_word += lower_word;
841
842
244k
      data_size -= 16;
843
244k
    }
844
240k
    while( data_size > 0 )
845
216k
    {
846
216k
      lower_word += data[ data_offset++ ];
847
216k
      upper_word += lower_word;
848
849
216k
      data_size--;
850
216k
    }
851
    /* Optimized equivalent of:
852
     * lower_word %= 0xfff1
853
     */
854
24.2k
    value_32bit = lower_word >> 16;
855
24.2k
    lower_word &= 0x0000ffffUL;
856
24.2k
    lower_word += ( value_32bit << 4 ) - value_32bit;
857
858
24.2k
    if( lower_word > 65521 )
859
2
    {
860
2
      value_32bit = lower_word >> 16;
861
2
      lower_word &= 0x0000ffffUL;
862
2
      lower_word += ( value_32bit << 4 ) - value_32bit;
863
2
    }
864
24.2k
    if( lower_word >= 65521 )
865
2
    {
866
2
      lower_word -= 65521;
867
2
    }
868
    /* Optimized equivalent of:
869
     * upper_word %= 0xfff1
870
     */
871
24.2k
    value_32bit = upper_word >> 16;
872
24.2k
    upper_word &= 0x0000ffffUL;
873
24.2k
    upper_word += ( value_32bit << 4 ) - value_32bit;
874
875
24.2k
    if( upper_word > 65521 )
876
262
    {
877
262
      value_32bit = upper_word >> 16;
878
262
      upper_word &= 0x0000ffffUL;
879
262
      upper_word += ( value_32bit << 4 ) - value_32bit;
880
262
    }
881
24.2k
    if( upper_word >= 65521 )
882
16
    {
883
16
      upper_word -= 65521;
884
16
    }
885
24.2k
  }
886
24.2k
  *checksum_value = ( upper_word << 16 ) | lower_word;
887
888
24.2k
  return( 1 );
889
24.2k
}
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.69k
{
900
3.69k
  static char *function                 = "libewf_deflate_read_data_header";
901
3.69k
  size_t safe_offset                    = 0;
902
3.69k
  uint32_t compression_window_size      = 0;
903
3.69k
  uint32_t preset_dictionary_identifier = 0;
904
3.69k
  uint8_t flags                         = 0;
905
3.69k
  uint8_t compression_data              = 0;
906
3.69k
  uint8_t compression_information       = 0;
907
3.69k
  uint8_t compression_method            = 0;
908
3.69k
  uint8_t compression_window_bits       = 0;
909
910
3.69k
  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
3.69k
  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
3.69k
  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
3.69k
  safe_offset = *compressed_data_offset;
944
945
3.69k
  if( ( compressed_data_size < 2 )
946
3.69k
   || ( 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
3.69k
  compression_data = compressed_data[ safe_offset++ ];
958
3.69k
  flags            = compressed_data[ safe_offset++ ];
959
960
3.69k
  compression_method      = compression_data & 0x0f;
961
3.69k
  compression_information = compression_data >> 4;
962
963
/* TODO validate check bits */
964
3.69k
  if( ( flags & 0x20 ) != 0 )
965
213
  {
966
213
    if( ( compressed_data_size < 6 )
967
213
     || ( safe_offset > ( compressed_data_size - 6 ) ) )
968
3
    {
969
3
      libcerror_error_set(
970
3
       error,
971
3
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
972
3
       LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
973
3
       "%s: invalid compressed data value too small.",
974
3
       function );
975
976
3
      return( -1 );
977
3
    }
978
210
    byte_stream_copy_to_uint32_big_endian(
979
210
     &( compressed_data[ 2 ] ),
980
210
     preset_dictionary_identifier );
981
982
210
    safe_offset += 4;
983
210
  }
984
3.69k
  if( compression_method != 8 )
985
17
  {
986
17
    libcerror_error_set(
987
17
     error,
988
17
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
989
17
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
990
17
     "%s: unsupported compression method: %" PRIu8 ".",
991
17
     function,
992
17
     compression_method );
993
994
17
    return( -1 );
995
17
  }
996
3.67k
  compression_window_bits = (uint8_t) compression_information + 8;
997
3.67k
  compression_window_size = (uint32_t) 1UL << compression_window_bits;
998
999
3.67k
  if( compression_window_size > 32768 )
1000
1
  {
1001
1
    libcerror_error_set(
1002
1
     error,
1003
1
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1004
1
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
1005
1
     "%s: unsupported compression window size: %" PRIu32 ".",
1006
1
     function,
1007
1
     compression_window_size );
1008
1009
1
    return( -1 );
1010
1
  }
1011
3.67k
  *compressed_data_offset += safe_offset;
1012
1013
3.67k
  return( 1 );
1014
3.67k
}
1015
1016
/* Reads the header of a block of compressed data
1017
 * Returns 1 on success or -1 on error
1018
 */
1019
int libewf_deflate_read_block_header(
1020
     libewf_bit_stream_t *bit_stream,
1021
     uint8_t *block_type,
1022
     uint8_t *last_block_flag,
1023
     libcerror_error_t **error )
1024
18.3k
{
1025
18.3k
  static char *function = "libewf_deflate_read_block_header";
1026
18.3k
  uint32_t value_32bit  = 0;
1027
1028
18.3k
  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
18.3k
  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
18.3k
  if( libewf_bit_stream_get_value(
1051
18.3k
       bit_stream,
1052
18.3k
       3,
1053
18.3k
       &value_32bit,
1054
18.3k
       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
18.3k
  *last_block_flag = (uint8_t) ( value_32bit & 0x00000001UL );
1066
18.3k
  value_32bit    >>= 1;
1067
18.3k
  *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 LIBEWF_DEFLATE_BLOCK_TYPE_UNCOMPRESSED:
1085
        libcnotify_printf(
1086
         "Uncompressed" );
1087
        break;
1088
1089
      case LIBEWF_DEFLATE_BLOCK_TYPE_HUFFMAN_FIXED:
1090
        libcnotify_printf(
1091
         "Fixed Huffman" );
1092
        break;
1093
1094
      case LIBEWF_DEFLATE_BLOCK_TYPE_HUFFMAN_DYNAMIC:
1095
        libcnotify_printf(
1096
         "Dynamic Huffman" );
1097
        break;
1098
1099
      case LIBEWF_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
18.3k
  return( 1 );
1114
18.3k
}
1115
1116
/* Reads a block of compressed data
1117
 * Returns 1 on success or -1 on error
1118
 */
1119
int libewf_deflate_read_block(
1120
     libewf_bit_stream_t *bit_stream,
1121
     uint8_t block_type,
1122
     libewf_huffman_tree_t *fixed_huffman_literals_tree,
1123
     libewf_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
18.3k
{
1129
18.3k
  libewf_huffman_tree_t *dynamic_huffman_distances_tree = NULL;
1130
18.3k
  libewf_huffman_tree_t *dynamic_huffman_literals_tree  = NULL;
1131
18.3k
  static char *function                                 = "libewf_deflate_read_block";
1132
18.3k
  size_t safe_uncompressed_data_offset                  = 0;
1133
18.3k
  uint32_t block_size                                   = 0;
1134
18.3k
  uint32_t block_size_copy                              = 0;
1135
18.3k
  uint32_t value_32bit                                  = 0;
1136
18.3k
  uint8_t skip_bits                                     = 0;
1137
1138
18.3k
  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
18.3k
  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
18.3k
  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
18.3k
  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
18.3k
  safe_uncompressed_data_offset = *uncompressed_data_offset;
1183
1184
18.3k
  switch( block_type )
1185
18.3k
  {
1186
6.48k
    case LIBEWF_DEFLATE_BLOCK_TYPE_UNCOMPRESSED:
1187
      /* Ignore the bits in the buffer upto the next byte
1188
       */
1189
6.48k
      skip_bits = bit_stream->bit_buffer_size & 0x07;
1190
1191
6.48k
      if( skip_bits > 0 )
1192
2.69k
      {
1193
2.69k
        if( libewf_bit_stream_get_value(
1194
2.69k
             bit_stream,
1195
2.69k
             skip_bits,
1196
2.69k
             &value_32bit,
1197
2.69k
             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
2.69k
      }
1209
6.48k
      if( libewf_bit_stream_get_value(
1210
6.48k
           bit_stream,
1211
6.48k
           32,
1212
6.48k
           &block_size,
1213
6.48k
           error ) != 1 )
1214
6
      {
1215
6
        libcerror_error_set(
1216
6
         error,
1217
6
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1218
6
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1219
6
         "%s: unable to retrieve value from bit stream.",
1220
6
         function );
1221
1222
6
        goto on_error;
1223
6
      }
1224
6.47k
      block_size_copy = ( block_size >> 16 ) ^ 0x0000ffffUL;
1225
6.47k
      block_size     &= 0x0000ffffUL;
1226
1227
6.47k
      if( block_size != block_size_copy )
1228
47
      {
1229
47
        libcerror_error_set(
1230
47
         error,
1231
47
         LIBCERROR_ERROR_DOMAIN_INPUT,
1232
47
         LIBCERROR_INPUT_ERROR_VALUE_MISMATCH,
1233
47
         "%s: mismatch in block size ( %" PRIu32 " != %" PRIu32 " ).",
1234
47
         function,
1235
47
         block_size,
1236
47
         block_size_copy );
1237
1238
47
        goto on_error;
1239
47
      }
1240
6.42k
      if( block_size == 0 )
1241
982
      {
1242
982
        break;
1243
982
      }
1244
5.44k
      if( (size_t) block_size > ( bit_stream->byte_stream_size - bit_stream->byte_stream_offset ) )
1245
16
      {
1246
16
        libcerror_error_set(
1247
16
         error,
1248
16
         LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1249
16
         LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
1250
16
         "%s: invalid compressed data value too small.",
1251
16
         function );
1252
1253
16
        goto on_error;
1254
16
      }
1255
5.42k
      if( (size_t) block_size > ( uncompressed_data_size - safe_uncompressed_data_offset ) )
1256
1
      {
1257
1
        libcerror_error_set(
1258
1
         error,
1259
1
         LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1260
1
         LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
1261
1
         "%s: invalid uncompressed data value too small.",
1262
1
         function );
1263
1264
1
        goto on_error;
1265
1
      }
1266
5.42k
      if( memory_copy(
1267
5.42k
           &( uncompressed_data[ safe_uncompressed_data_offset ] ),
1268
5.42k
           &( bit_stream->byte_stream[ bit_stream->byte_stream_offset ] ),
1269
5.42k
           (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
5.42k
      bit_stream->byte_stream_offset += block_size;
1281
5.42k
      safe_uncompressed_data_offset  += block_size;
1282
1283
      /* Flush the bit stream buffer
1284
       */
1285
5.42k
      bit_stream->bit_buffer      = 0;
1286
5.42k
      bit_stream->bit_buffer_size = 0;
1287
1288
5.42k
      break;
1289
1290
7.27k
    case LIBEWF_DEFLATE_BLOCK_TYPE_HUFFMAN_FIXED:
1291
7.27k
      if( libewf_deflate_decode_huffman(
1292
7.27k
           bit_stream,
1293
7.27k
           fixed_huffman_literals_tree,
1294
7.27k
           fixed_huffman_distances_tree,
1295
7.27k
           uncompressed_data,
1296
7.27k
           uncompressed_data_size,
1297
7.27k
           &safe_uncompressed_data_offset,
1298
7.27k
           error ) != 1 )
1299
40
      {
1300
40
        libcerror_error_set(
1301
40
         error,
1302
40
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1303
40
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1304
40
         "%s: unable to decode fixed Huffman encoded bit stream.",
1305
40
         function );
1306
1307
40
        goto on_error;
1308
40
      }
1309
7.23k
      break;
1310
1311
7.23k
    case LIBEWF_DEFLATE_BLOCK_TYPE_HUFFMAN_DYNAMIC:
1312
4.54k
      if( libewf_huffman_tree_initialize(
1313
4.54k
           &dynamic_huffman_literals_tree,
1314
4.54k
           288,
1315
4.54k
           15,
1316
4.54k
           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
4.54k
      if( libewf_huffman_tree_initialize(
1328
4.54k
           &dynamic_huffman_distances_tree,
1329
4.54k
           30,
1330
4.54k
           15,
1331
4.54k
           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
4.54k
      if( libewf_deflate_build_dynamic_huffman_trees(
1343
4.54k
           bit_stream,
1344
4.54k
           dynamic_huffman_literals_tree,
1345
4.54k
           dynamic_huffman_distances_tree,
1346
4.54k
           error ) != 1 )
1347
88
      {
1348
88
        libcerror_error_set(
1349
88
         error,
1350
88
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1351
88
         LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1352
88
         "%s: unable to build dynamic Huffman trees.",
1353
88
         function );
1354
1355
88
        goto on_error;
1356
88
      }
1357
4.46k
      if( libewf_deflate_decode_huffman(
1358
4.46k
           bit_stream,
1359
4.46k
           dynamic_huffman_literals_tree,
1360
4.46k
           dynamic_huffman_distances_tree,
1361
4.46k
           uncompressed_data,
1362
4.46k
           uncompressed_data_size,
1363
4.46k
           &safe_uncompressed_data_offset,
1364
4.46k
           error ) != 1 )
1365
18
      {
1366
18
        libcerror_error_set(
1367
18
         error,
1368
18
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1369
18
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1370
18
         "%s: unable to decode dynamic Huffman encoded bit stream.",
1371
18
         function );
1372
1373
18
        goto on_error;
1374
18
      }
1375
4.44k
      if( libewf_huffman_tree_free(
1376
4.44k
           &dynamic_huffman_distances_tree,
1377
4.44k
           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
4.44k
      if( libewf_huffman_tree_free(
1389
4.44k
           &dynamic_huffman_literals_tree,
1390
4.44k
           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
4.44k
      break;
1402
1403
4.44k
    case LIBEWF_DEFLATE_BLOCK_TYPE_RESERVED:
1404
7
    default:
1405
7
      libcerror_error_set(
1406
7
       error,
1407
7
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1408
7
       LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
1409
7
       "%s: unsupported block type.",
1410
7
       function );
1411
1412
7
      goto on_error;
1413
18.3k
  }
1414
18.0k
  *uncompressed_data_offset = safe_uncompressed_data_offset;
1415
1416
18.0k
  return( 1 );
1417
1418
223
on_error:
1419
223
  if( dynamic_huffman_distances_tree != NULL )
1420
106
  {
1421
106
    libewf_huffman_tree_free(
1422
106
     &dynamic_huffman_distances_tree,
1423
106
     NULL );
1424
106
  }
1425
223
  if( dynamic_huffman_literals_tree != NULL )
1426
106
  {
1427
106
    libewf_huffman_tree_free(
1428
106
     &dynamic_huffman_literals_tree,
1429
106
     NULL );
1430
106
  }
1431
223
  return( -1 );
1432
18.3k
}
1433
1434
/* Decompresses data using deflate compression
1435
 * Returns 1 on success or -1 on error
1436
 */
1437
int libewf_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
  libewf_bit_stream_t *bit_stream                     = NULL;
1445
0
  libewf_huffman_tree_t *fixed_huffman_distances_tree = NULL;
1446
0
  libewf_huffman_tree_t *fixed_huffman_literals_tree  = NULL;
1447
0
  static char *function                               = "libewf_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( libewf_bit_stream_initialize(
1523
0
       &bit_stream,
1524
0
       compressed_data,
1525
0
       compressed_data_size,
1526
0
       compressed_data_offset,
1527
0
       LIBEWF_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( libewf_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 == LIBEWF_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( libewf_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( libewf_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( libewf_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( libewf_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( libewf_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( libewf_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( libewf_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
    libewf_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
    libewf_huffman_tree_free(
1690
0
     &fixed_huffman_literals_tree,
1691
0
     NULL );
1692
0
  }
1693
0
  if( bit_stream != NULL )
1694
0
  {
1695
0
    libewf_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 libewf_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
3.69k
{
1712
3.69k
  libewf_bit_stream_t *bit_stream                     = NULL;
1713
3.69k
  libewf_huffman_tree_t *fixed_huffman_distances_tree = NULL;
1714
3.69k
  libewf_huffman_tree_t *fixed_huffman_literals_tree  = NULL;
1715
3.69k
  static char *function                               = "libewf_deflate_decompress_zlib";
1716
3.69k
  size_t compressed_data_offset                       = 0;
1717
3.69k
  size_t safe_uncompressed_data_size                  = 0;
1718
3.69k
  size_t uncompressed_data_offset                     = 0;
1719
3.69k
  uint32_t calculated_checksum                        = 0;
1720
3.69k
  uint32_t stored_checksum                            = 0;
1721
3.69k
  uint8_t block_type                                  = 0;
1722
3.69k
  uint8_t last_block_flag                             = 0;
1723
1724
3.69k
  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
3.69k
  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
3.69k
  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
3.69k
  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
3.69k
  safe_uncompressed_data_size = *uncompressed_data_size;
1769
1770
3.69k
  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
3.69k
  if( libewf_deflate_read_data_header(
1782
3.69k
       compressed_data,
1783
3.69k
       compressed_data_size,
1784
3.69k
       &compressed_data_offset,
1785
3.69k
       error ) != 1 )
1786
21
  {
1787
21
    libcerror_error_set(
1788
21
     error,
1789
21
     LIBCERROR_ERROR_DOMAIN_IO,
1790
21
     LIBCERROR_IO_ERROR_READ_FAILED,
1791
21
     "%s: unable to read data header.",
1792
21
     function );
1793
1794
21
    goto on_error;
1795
21
  }
1796
3.67k
  if( compressed_data_offset >= compressed_data_size )
1797
3
  {
1798
3
    libcerror_error_set(
1799
3
     error,
1800
3
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1801
3
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
1802
3
     "%s: invalid compressed data value too small.",
1803
3
     function );
1804
1805
3
    goto on_error;
1806
3
  }
1807
3.67k
  if( libewf_bit_stream_initialize(
1808
3.67k
       &bit_stream,
1809
3.67k
       compressed_data,
1810
3.67k
       compressed_data_size,
1811
3.67k
       compressed_data_offset,
1812
3.67k
       LIBEWF_BIT_STREAM_STORAGE_TYPE_BYTE_BACK_TO_FRONT,
1813
3.67k
       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
18.4k
  while( bit_stream->byte_stream_offset < bit_stream->byte_stream_size )
1825
18.3k
  {
1826
18.3k
    if( libewf_deflate_read_block_header(
1827
18.3k
         bit_stream,
1828
18.3k
         &block_type,
1829
18.3k
         &last_block_flag,
1830
18.3k
         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
18.3k
    if( block_type == LIBEWF_DEFLATE_BLOCK_TYPE_HUFFMAN_FIXED )
1842
7.27k
    {
1843
7.27k
      if( ( fixed_huffman_literals_tree == NULL )
1844
7.27k
       && ( fixed_huffman_distances_tree == NULL ) )
1845
479
      {
1846
479
        if( libewf_huffman_tree_initialize(
1847
479
             &fixed_huffman_literals_tree,
1848
479
             288,
1849
479
             15,
1850
479
             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
479
        if( libewf_huffman_tree_initialize(
1862
479
             &fixed_huffman_distances_tree,
1863
479
             30,
1864
479
             15,
1865
479
             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
479
        if( libewf_deflate_build_fixed_huffman_trees(
1877
479
             fixed_huffman_literals_tree,
1878
479
             fixed_huffman_distances_tree,
1879
479
             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
479
      }
1891
7.27k
    }
1892
18.3k
    if( libewf_deflate_read_block(
1893
18.3k
         bit_stream,
1894
18.3k
         block_type,
1895
18.3k
         fixed_huffman_literals_tree,
1896
18.3k
         fixed_huffman_distances_tree,
1897
18.3k
         uncompressed_data,
1898
18.3k
         safe_uncompressed_data_size,
1899
18.3k
         &uncompressed_data_offset,
1900
18.3k
         error ) != 1 )
1901
223
    {
1902
223
      libcerror_error_set(
1903
223
       error,
1904
223
       LIBCERROR_ERROR_DOMAIN_IO,
1905
223
       LIBCERROR_IO_ERROR_READ_FAILED,
1906
223
       "%s: unable to read block of compressed data.",
1907
223
       function );
1908
1909
223
      goto on_error;
1910
223
    }
1911
18.0k
    if( last_block_flag != 0 )
1912
3.32k
    {
1913
3.32k
      break;
1914
3.32k
    }
1915
18.0k
  }
1916
3.44k
  if( ( bit_stream->byte_stream_size - bit_stream->byte_stream_offset ) >= 4 )
1917
2.73k
  {
1918
2.73k
    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
2.73k
    byte_stream_copy_to_uint32_big_endian(
1924
2.73k
     &( bit_stream->byte_stream[ bit_stream->byte_stream_offset ] ),
1925
2.73k
     stored_checksum );
1926
1927
2.73k
    if( libewf_deflate_calculate_adler32(
1928
2.73k
         &calculated_checksum,
1929
2.73k
         uncompressed_data,
1930
2.73k
         uncompressed_data_offset,
1931
2.73k
         1,
1932
2.73k
         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
2.73k
    if( stored_checksum != calculated_checksum )
1944
178
    {
1945
178
      libcerror_error_set(
1946
178
       error,
1947
178
       LIBCERROR_ERROR_DOMAIN_INPUT,
1948
178
       LIBCERROR_INPUT_ERROR_CHECKSUM_MISMATCH,
1949
178
       "%s: checksum does not match (stored: 0x%08" PRIx32 ", calculated: 0x%08" PRIx32 ").",
1950
178
       function,
1951
178
       stored_checksum,
1952
178
       calculated_checksum );
1953
1954
178
      goto on_error;
1955
178
    }
1956
2.73k
  }
1957
3.26k
  if( fixed_huffman_distances_tree != NULL )
1958
249
  {
1959
249
    if( libewf_huffman_tree_free(
1960
249
         &fixed_huffman_distances_tree,
1961
249
         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
249
  }
1973
3.26k
  if( fixed_huffman_literals_tree != NULL )
1974
249
  {
1975
249
    if( libewf_huffman_tree_free(
1976
249
         &fixed_huffman_literals_tree,
1977
249
         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
249
  }
1989
3.26k
  if( libewf_bit_stream_free(
1990
3.26k
       &bit_stream,
1991
3.26k
       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
3.26k
  *uncompressed_data_size = uncompressed_data_offset;
2003
2004
3.26k
  return( 1 );
2005
2006
425
on_error:
2007
425
  if( fixed_huffman_distances_tree != NULL )
2008
230
  {
2009
230
    libewf_huffman_tree_free(
2010
230
     &fixed_huffman_distances_tree,
2011
230
     NULL );
2012
230
  }
2013
425
  if( fixed_huffman_literals_tree != NULL )
2014
230
  {
2015
230
    libewf_huffman_tree_free(
2016
230
     &fixed_huffman_literals_tree,
2017
230
     NULL );
2018
230
  }
2019
425
  if( bit_stream != NULL )
2020
401
  {
2021
401
    libewf_bit_stream_free(
2022
401
     &bit_stream,
2023
401
     NULL );
2024
401
  }
2025
425
  return( -1 );
2026
3.26k
}
2027