Coverage Report

Created: 2026-08-31 07:43

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.39k
{
63
4.39k
  uint8_t code_size_array[ 316 ];
64
65
4.39k
  libewf_huffman_tree_t *codes_tree = NULL;
66
4.39k
  static char *function             = "libewf_deflate_build_dynamic_huffman_trees";
67
4.39k
  uint32_t code_size                = 0;
68
4.39k
  uint32_t code_size_index          = 0;
69
4.39k
  uint32_t code_size_sequence       = 0;
70
4.39k
  uint32_t number_of_code_sizes     = 0;
71
4.39k
  uint32_t number_of_distance_codes = 0;
72
4.39k
  uint32_t number_of_literal_codes  = 0;
73
4.39k
  uint32_t times_to_repeat          = 0;
74
4.39k
  uint16_t symbol                   = 0;
75
76
4.39k
  if( libewf_bit_stream_get_value(
77
4.39k
       bit_stream,
78
4.39k
       14,
79
4.39k
       &number_of_code_sizes,
80
4.39k
       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.39k
  number_of_literal_codes  = number_of_code_sizes & 0x0000001fUL;
92
4.39k
  number_of_code_sizes   >>= 5;
93
4.39k
  number_of_distance_codes = number_of_code_sizes & 0x0000001fUL;
94
4.39k
  number_of_code_sizes   >>= 5;
95
96
4.39k
  number_of_literal_codes += 257;
97
98
4.39k
  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.39k
  number_of_distance_codes += 1;
110
111
4.39k
  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.39k
  number_of_code_sizes += 4;
123
124
4.39k
  for( code_size_index = 0;
125
75.9k
       code_size_index < number_of_code_sizes;
126
71.5k
       code_size_index++ )
127
71.5k
  {
128
71.5k
    if( libewf_bit_stream_get_value(
129
71.5k
         bit_stream,
130
71.5k
         3,
131
71.5k
         &code_size,
132
71.5k
         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.5k
    code_size_sequence = libewf_deflate_code_sizes_sequence[ code_size_index ];
144
145
71.5k
    code_size_array[ code_size_sequence ] = (uint8_t) code_size;
146
71.5k
  }
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.38k
  if( libewf_huffman_tree_initialize(
154
4.38k
       &codes_tree,
155
4.38k
       19,
156
4.38k
       15,
157
4.38k
       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.38k
  if( libewf_huffman_tree_build(
169
4.38k
       codes_tree,
170
4.38k
       code_size_array,
171
4.38k
       19,
172
4.38k
       error ) != 1 )
173
11
  {
174
11
    libcerror_error_set(
175
11
     error,
176
11
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
177
11
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
178
11
     "%s: unable to build codes tree.",
179
11
     function );
180
181
11
    goto on_error;
182
11
  }
183
4.37k
  number_of_code_sizes = number_of_literal_codes + number_of_distance_codes;
184
185
4.37k
  code_size_index = 0;
186
187
296k
  while( code_size_index < number_of_code_sizes )
188
292k
  {
189
292k
    if( libewf_huffman_tree_get_symbol_from_bit_stream(
190
292k
         codes_tree,
191
292k
         bit_stream,
192
292k
         &symbol,
193
292k
         error ) != 1 )
194
20
    {
195
20
      libcerror_error_set(
196
20
       error,
197
20
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
198
20
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
199
20
       "%s: unable to retrieve literal value from bit stream.",
200
20
       function );
201
202
20
      goto on_error;
203
20
    }
204
292k
    if( symbol < 16 )
205
255k
    {
206
255k
      code_size_array[ code_size_index++ ] = (uint8_t) symbol;
207
208
255k
      continue;
209
255k
    }
210
36.9k
    code_size = 0;
211
212
36.9k
    if( symbol == 16 )
213
6.89k
    {
214
6.89k
      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.88k
      code_size = (uint32_t) code_size_array[ code_size_index - 1 ];
226
227
6.88k
      if( libewf_bit_stream_get_value(
228
6.88k
           bit_stream,
229
6.88k
           2,
230
6.88k
           &times_to_repeat,
231
6.88k
           error ) != 1 )
232
4
      {
233
4
        libcerror_error_set(
234
4
         error,
235
4
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
236
4
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
237
4
         "%s: unable to retrieve value from bit stream.",
238
4
         function );
239
240
4
        goto on_error;
241
4
      }
242
6.88k
      times_to_repeat += 3;
243
6.88k
    }
244
30.0k
    else if( symbol == 17 )
245
11.7k
    {
246
11.7k
      if( libewf_bit_stream_get_value(
247
11.7k
           bit_stream,
248
11.7k
           3,
249
11.7k
           &times_to_repeat,
250
11.7k
           error ) != 1 )
251
3
      {
252
3
        libcerror_error_set(
253
3
         error,
254
3
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
255
3
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
256
3
         "%s: unable to retrieve value from bit stream.",
257
3
         function );
258
259
3
        goto on_error;
260
3
      }
261
11.7k
      times_to_repeat += 3;
262
11.7k
    }
263
18.3k
    else if( symbol == 18 )
264
18.3k
    {
265
18.3k
      if( libewf_bit_stream_get_value(
266
18.3k
           bit_stream,
267
18.3k
           7,
268
18.3k
           &times_to_repeat,
269
18.3k
           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.3k
      times_to_repeat += 11;
281
18.3k
    }
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
36.9k
    if( ( code_size_index + times_to_repeat ) > number_of_code_sizes )
294
13
    {
295
13
      libcerror_error_set(
296
13
       error,
297
13
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
298
13
       LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
299
13
       "%s: invalid times to repeat value out of bounds.",
300
13
       function );
301
302
13
      goto on_error;
303
13
    }
304
1.02M
    while( times_to_repeat > 0 )
305
983k
    {
306
983k
      code_size_array[ code_size_index++ ] = (uint8_t) code_size;
307
308
983k
      times_to_repeat--;
309
983k
    }
310
36.9k
  }
311
4.33k
  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.32k
  if( libewf_huffman_tree_free(
323
4.32k
       &codes_tree,
324
4.32k
       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.32k
  if( libewf_huffman_tree_build(
336
4.32k
       literals_tree,
337
4.32k
       code_size_array,
338
4.32k
       number_of_literal_codes,
339
4.32k
       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.31k
  if( libewf_huffman_tree_build(
351
4.31k
       distances_tree,
352
4.31k
       &( code_size_array[ number_of_literal_codes ] ),
353
4.31k
       number_of_distance_codes,
354
4.31k
       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.31k
  return( 1 );
366
367
87
on_error:
368
87
  if( codes_tree != NULL )
369
60
  {
370
60
    libewf_huffman_tree_free(
371
60
     &codes_tree,
372
60
     NULL );
373
60
  }
374
87
  return( -1 );
375
4.31k
}
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
484
{
385
484
  uint8_t code_size_array[ 318 ];
386
387
484
  static char *function = "libewf_deflate_build_fixed_huffman_trees";
388
484
  uint16_t symbol       = 0;
389
390
484
  for( symbol = 0;
391
154k
       symbol < 318;
392
153k
       symbol++ )
393
153k
  {
394
153k
    if( symbol < 144 )
395
69.6k
    {
396
69.6k
      code_size_array[ symbol ] = 8;
397
69.6k
    }
398
84.2k
    else if( symbol < 256 )
399
54.2k
    {
400
54.2k
      code_size_array[ symbol ] = 9;
401
54.2k
    }
402
30.0k
    else if( symbol < 280 )
403
11.6k
    {
404
11.6k
      code_size_array[ symbol ] = 7;
405
11.6k
    }
406
18.3k
    else if( symbol < 288 )
407
3.87k
    {
408
3.87k
      code_size_array[ symbol ] = 8;
409
3.87k
    }
410
14.5k
    else
411
14.5k
    {
412
14.5k
      code_size_array[ symbol ] = 5;
413
14.5k
    }
414
153k
  }
415
484
  if( libewf_huffman_tree_build(
416
484
       literals_tree,
417
484
       code_size_array,
418
484
       288,
419
484
       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
484
  if( libewf_huffman_tree_build(
431
484
       distances_tree,
432
484
       &( code_size_array[ 288 ] ),
433
484
       30,
434
484
       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
484
  return( 1 );
446
484
}
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.82k
{
460
9.82k
  static char *function         = "libewf_deflate_decode_huffman";
461
9.82k
  size_t data_offset            = 0;
462
9.82k
  uint32_t extra_bits           = 0;
463
9.82k
  uint16_t compression_offset   = 0;
464
9.82k
  uint16_t compression_size     = 0;
465
9.82k
  uint16_t number_of_extra_bits = 0;
466
9.82k
  uint16_t symbol               = 0;
467
468
9.82k
  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.82k
  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.82k
  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.82k
  data_offset = *uncompressed_data_offset;
502
503
9.82k
  do
504
7.64M
  {
505
7.64M
    if( libewf_huffman_tree_get_symbol_from_bit_stream(
506
7.64M
         literals_tree,
507
7.64M
         bit_stream,
508
7.64M
         &symbol,
509
7.64M
         error ) != 1 )
510
11
    {
511
11
      libcerror_error_set(
512
11
       error,
513
11
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
514
11
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
515
11
       "%s: unable to retrieve literal value from bit stream.",
516
11
       function );
517
518
11
      return( -1 );
519
11
    }
520
7.64M
    if( symbol < 256 )
521
462k
    {
522
462k
      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
462k
      uncompressed_data[ data_offset++ ] = (uint8_t) symbol;
534
462k
    }
535
7.18M
    else if( ( symbol > 256 )
536
7.17M
          && ( symbol < 286 ) )
537
7.17M
    {
538
7.17M
      symbol -= 257;
539
540
7.17M
      number_of_extra_bits = libewf_deflate_literal_codes_number_of_extra_bits[ symbol ];
541
542
7.17M
      if( libewf_bit_stream_get_value(
543
7.17M
           bit_stream,
544
7.17M
           (uint8_t) number_of_extra_bits,
545
7.17M
           &extra_bits,
546
7.17M
           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.17M
      compression_size = libewf_deflate_literal_codes_base[ symbol ] + (uint16_t) extra_bits;
558
559
7.17M
      if( libewf_huffman_tree_get_symbol_from_bit_stream(
560
7.17M
           distances_tree,
561
7.17M
           bit_stream,
562
7.17M
           &symbol,
563
7.17M
           error ) != 1 )
564
5
      {
565
5
        libcerror_error_set(
566
5
         error,
567
5
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
568
5
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
569
5
         "%s: unable to retrieve distance value from bit stream.",
570
5
         function );
571
572
5
        return( -1 );
573
5
      }
574
7.17M
      number_of_extra_bits = libewf_deflate_distance_codes_number_of_extra_bits[ symbol ];
575
576
7.17M
      if( libewf_bit_stream_get_value(
577
7.17M
           bit_stream,
578
7.17M
           (uint8_t) number_of_extra_bits,
579
7.17M
           &extra_bits,
580
7.17M
           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
7.17M
      compression_offset = libewf_deflate_distance_codes_base[ symbol ] + (uint16_t) extra_bits;
592
593
7.17M
      if( compression_offset > data_offset )
594
21
      {
595
21
        libcerror_error_set(
596
21
         error,
597
21
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
598
21
         LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
599
21
         "%s: invalid compression offset value out of bounds.",
600
21
         function );
601
602
21
        return( -1 );
603
21
      }
604
7.17M
      if( ( data_offset + compression_size ) > uncompressed_data_size )
605
8
      {
606
8
        libcerror_error_set(
607
8
         error,
608
8
         LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
609
8
         LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
610
8
         "%s: invalid uncompressed data value too small.",
611
8
         function );
612
613
8
        return( -1 );
614
8
      }
615
29.6M
      while( compression_size > 0 )
616
22.5M
      {
617
22.5M
        uncompressed_data[ data_offset ] = uncompressed_data[ data_offset - compression_offset ];
618
619
22.5M
        data_offset++;
620
22.5M
        compression_size--;
621
22.5M
      }
622
7.17M
    }
623
9.77k
    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
7.64M
  }
636
7.64M
  while( symbol != 256 );
637
638
9.76k
  *uncompressed_data_offset = data_offset;
639
640
9.76k
  return( 1 );
641
9.82k
}
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
23.3k
{
654
23.3k
  static char *function = "libewf_deflate_calculate_adler32";
655
23.3k
  size_t data_offset    = 0;
656
23.3k
  uint32_t lower_word   = 0;
657
23.3k
  uint32_t upper_word   = 0;
658
23.3k
  uint32_t value_32bit  = 0;
659
23.3k
  int block_index       = 0;
660
661
23.3k
  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
23.3k
  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
23.3k
  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
23.3k
  lower_word = initial_value & 0xffff;
695
23.3k
  upper_word = ( initial_value >> 16 ) & 0xffff;
696
697
26.9k
  while( data_size >= 0x15b0 )
698
3.62k
  {
699
    /* The modulo calculation is needed per 5552 (0x15b0) bytes
700
     * 5552 / 16 = 347
701
     */
702
3.62k
    for( block_index = 0;
703
1.26M
         block_index < 347;
704
1.25M
         block_index++ )
705
1.25M
    {
706
1.25M
      lower_word += data[ data_offset++ ];
707
1.25M
      upper_word += lower_word;
708
709
1.25M
      lower_word += data[ data_offset++ ];
710
1.25M
      upper_word += lower_word;
711
712
1.25M
      lower_word += data[ data_offset++ ];
713
1.25M
      upper_word += lower_word;
714
715
1.25M
      lower_word += data[ data_offset++ ];
716
1.25M
      upper_word += lower_word;
717
718
1.25M
      lower_word += data[ data_offset++ ];
719
1.25M
      upper_word += lower_word;
720
721
1.25M
      lower_word += data[ data_offset++ ];
722
1.25M
      upper_word += lower_word;
723
724
1.25M
      lower_word += data[ data_offset++ ];
725
1.25M
      upper_word += lower_word;
726
727
1.25M
      lower_word += data[ data_offset++ ];
728
1.25M
      upper_word += lower_word;
729
730
1.25M
      lower_word += data[ data_offset++ ];
731
1.25M
      upper_word += lower_word;
732
733
1.25M
      lower_word += data[ data_offset++ ];
734
1.25M
      upper_word += lower_word;
735
736
1.25M
      lower_word += data[ data_offset++ ];
737
1.25M
      upper_word += lower_word;
738
739
1.25M
      lower_word += data[ data_offset++ ];
740
1.25M
      upper_word += lower_word;
741
742
1.25M
      lower_word += data[ data_offset++ ];
743
1.25M
      upper_word += lower_word;
744
745
1.25M
      lower_word += data[ data_offset++ ];
746
1.25M
      upper_word += lower_word;
747
748
1.25M
      lower_word += data[ data_offset++ ];
749
1.25M
      upper_word += lower_word;
750
751
1.25M
      lower_word += data[ data_offset++ ];
752
1.25M
      upper_word += lower_word;
753
1.25M
    }
754
    /* Optimized equivalent of:
755
     * lower_word %= 0xfff1
756
     */
757
3.62k
    value_32bit = lower_word >> 16;
758
3.62k
    lower_word &= 0x0000ffffUL;
759
3.62k
    lower_word += ( value_32bit << 4 ) - value_32bit;
760
761
3.62k
    if( lower_word > 65521 )
762
47
    {
763
47
      value_32bit = lower_word >> 16;
764
47
      lower_word &= 0x0000ffffUL;
765
47
      lower_word += ( value_32bit << 4 ) - value_32bit;
766
47
    }
767
3.62k
    if( lower_word >= 65521 )
768
18
    {
769
18
      lower_word -= 65521;
770
18
    }
771
    /* Optimized equivalent of:
772
     * upper_word %= 0xfff1
773
     */
774
3.62k
    value_32bit = upper_word >> 16;
775
3.62k
    upper_word &= 0x0000ffffUL;
776
3.62k
    upper_word += ( value_32bit << 4 ) - value_32bit;
777
778
3.62k
    if( upper_word > 65521 )
779
3.26k
    {
780
3.26k
      value_32bit = upper_word >> 16;
781
3.26k
      upper_word &= 0x0000ffffUL;
782
3.26k
      upper_word += ( value_32bit << 4 ) - value_32bit;
783
3.26k
    }
784
3.62k
    if( upper_word >= 65521 )
785
26
    {
786
26
      upper_word -= 65521;
787
26
    }
788
3.62k
    data_size -= 0x15b0;
789
3.62k
  }
790
23.3k
  if( data_size > 0 )
791
23.3k
  {
792
251k
    while( data_size > 16 )
793
228k
    {
794
228k
      lower_word += data[ data_offset++ ];
795
228k
      upper_word += lower_word;
796
797
228k
      lower_word += data[ data_offset++ ];
798
228k
      upper_word += lower_word;
799
800
228k
      lower_word += data[ data_offset++ ];
801
228k
      upper_word += lower_word;
802
803
228k
      lower_word += data[ data_offset++ ];
804
228k
      upper_word += lower_word;
805
806
228k
      lower_word += data[ data_offset++ ];
807
228k
      upper_word += lower_word;
808
809
228k
      lower_word += data[ data_offset++ ];
810
228k
      upper_word += lower_word;
811
812
228k
      lower_word += data[ data_offset++ ];
813
228k
      upper_word += lower_word;
814
815
228k
      lower_word += data[ data_offset++ ];
816
228k
      upper_word += lower_word;
817
818
228k
      lower_word += data[ data_offset++ ];
819
228k
      upper_word += lower_word;
820
821
228k
      lower_word += data[ data_offset++ ];
822
228k
      upper_word += lower_word;
823
824
228k
      lower_word += data[ data_offset++ ];
825
228k
      upper_word += lower_word;
826
827
228k
      lower_word += data[ data_offset++ ];
828
228k
      upper_word += lower_word;
829
830
228k
      lower_word += data[ data_offset++ ];
831
228k
      upper_word += lower_word;
832
833
228k
      lower_word += data[ data_offset++ ];
834
228k
      upper_word += lower_word;
835
836
228k
      lower_word += data[ data_offset++ ];
837
228k
      upper_word += lower_word;
838
839
228k
      lower_word += data[ data_offset++ ];
840
228k
      upper_word += lower_word;
841
842
228k
      data_size -= 16;
843
228k
    }
844
233k
    while( data_size > 0 )
845
210k
    {
846
210k
      lower_word += data[ data_offset++ ];
847
210k
      upper_word += lower_word;
848
849
210k
      data_size--;
850
210k
    }
851
    /* Optimized equivalent of:
852
     * lower_word %= 0xfff1
853
     */
854
23.3k
    value_32bit = lower_word >> 16;
855
23.3k
    lower_word &= 0x0000ffffUL;
856
23.3k
    lower_word += ( value_32bit << 4 ) - value_32bit;
857
858
23.3k
    if( lower_word > 65521 )
859
6
    {
860
6
      value_32bit = lower_word >> 16;
861
6
      lower_word &= 0x0000ffffUL;
862
6
      lower_word += ( value_32bit << 4 ) - value_32bit;
863
6
    }
864
23.3k
    if( lower_word >= 65521 )
865
6
    {
866
6
      lower_word -= 65521;
867
6
    }
868
    /* Optimized equivalent of:
869
     * upper_word %= 0xfff1
870
     */
871
23.3k
    value_32bit = upper_word >> 16;
872
23.3k
    upper_word &= 0x0000ffffUL;
873
23.3k
    upper_word += ( value_32bit << 4 ) - value_32bit;
874
875
23.3k
    if( upper_word > 65521 )
876
258
    {
877
258
      value_32bit = upper_word >> 16;
878
258
      upper_word &= 0x0000ffffUL;
879
258
      upper_word += ( value_32bit << 4 ) - value_32bit;
880
258
    }
881
23.3k
    if( upper_word >= 65521 )
882
16
    {
883
16
      upper_word -= 65521;
884
16
    }
885
23.3k
  }
886
23.3k
  *checksum_value = ( upper_word << 16 ) | lower_word;
887
888
23.3k
  return( 1 );
889
23.3k
}
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.54k
{
900
3.54k
  static char *function                 = "libewf_deflate_read_data_header";
901
3.54k
  size_t safe_offset                    = 0;
902
3.54k
  uint32_t compression_window_size      = 0;
903
3.54k
  uint8_t flags                         = 0;
904
3.54k
  uint8_t compression_data              = 0;
905
3.54k
  uint8_t compression_information       = 0;
906
3.54k
  uint8_t compression_method            = 0;
907
3.54k
  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.54k
  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.54k
  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.54k
  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.54k
  safe_offset = *compressed_data_offset;
947
948
3.54k
  if( ( compressed_data_size < 2 )
949
3.54k
   || ( 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.54k
  compression_data = compressed_data[ safe_offset++ ];
961
3.54k
  flags            = compressed_data[ safe_offset++ ];
962
963
3.54k
  compression_method      = compression_data & 0x0f;
964
3.54k
  compression_information = compression_data >> 4;
965
966
/* TODO validate check bits */
967
3.54k
  if( ( flags & 0x20 ) != 0 )
968
192
  {
969
192
    if( ( compressed_data_size < 6 )
970
191
     || ( 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
188
    safe_offset += 4;
987
188
  }
988
3.53k
  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.51k
  compression_window_bits = (uint8_t) compression_information + 8;
1001
3.51k
  compression_window_size = (uint32_t) 1UL << compression_window_bits;
1002
1003
3.51k
  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.51k
  *compressed_data_offset += safe_offset;
1016
1017
3.51k
  return( 1 );
1018
3.51k
}
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.7k
{
1029
14.7k
  static char *function = "libewf_deflate_read_block_header";
1030
14.7k
  uint32_t value_32bit  = 0;
1031
1032
14.7k
  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.7k
  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.7k
  if( libewf_bit_stream_get_value(
1055
14.7k
       bit_stream,
1056
14.7k
       3,
1057
14.7k
       &value_32bit,
1058
14.7k
       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.7k
  *last_block_flag = (uint8_t) ( value_32bit & 0x00000001UL );
1070
14.7k
  value_32bit    >>= 1;
1071
14.7k
  *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.7k
  return( 1 );
1118
14.7k
}
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.7k
{
1133
14.7k
  libewf_huffman_tree_t *dynamic_huffman_distances_tree = NULL;
1134
14.7k
  libewf_huffman_tree_t *dynamic_huffman_literals_tree  = NULL;
1135
14.7k
  static char *function                                 = "libewf_deflate_read_block";
1136
14.7k
  size_t safe_uncompressed_data_offset                  = 0;
1137
14.7k
  uint32_t block_size                                   = 0;
1138
14.7k
  uint32_t block_size_copy                              = 0;
1139
14.7k
  uint32_t value_32bit                                  = 0;
1140
14.7k
  uint8_t skip_bits                                     = 0;
1141
1142
14.7k
  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.7k
  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.7k
  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.7k
  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.7k
  safe_uncompressed_data_offset = *uncompressed_data_offset;
1187
1188
14.7k
  switch( block_type )
1189
14.7k
  {
1190
4.82k
    case LIBEWF_DEFLATE_BLOCK_TYPE_UNCOMPRESSED:
1191
      /* Ignore the bits in the buffer upto the next byte
1192
       */
1193
4.82k
      skip_bits = bit_stream->bit_buffer_size & 0x07;
1194
1195
4.82k
      if( skip_bits > 0 )
1196
2.04k
      {
1197
2.04k
        if( libewf_bit_stream_get_value(
1198
2.04k
             bit_stream,
1199
2.04k
             skip_bits,
1200
2.04k
             &value_32bit,
1201
2.04k
             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
2.04k
      }
1213
4.82k
      if( libewf_bit_stream_get_value(
1214
4.82k
           bit_stream,
1215
4.82k
           32,
1216
4.82k
           &block_size,
1217
4.82k
           error ) != 1 )
1218
6
      {
1219
6
        libcerror_error_set(
1220
6
         error,
1221
6
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1222
6
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1223
6
         "%s: unable to retrieve value from bit stream.",
1224
6
         function );
1225
1226
6
        goto on_error;
1227
6
      }
1228
4.81k
      block_size_copy = ( block_size >> 16 ) ^ 0x0000ffffUL;
1229
4.81k
      block_size     &= 0x0000ffffUL;
1230
1231
4.81k
      if( block_size != block_size_copy )
1232
44
      {
1233
44
        libcerror_error_set(
1234
44
         error,
1235
44
         LIBCERROR_ERROR_DOMAIN_INPUT,
1236
44
         LIBCERROR_INPUT_ERROR_VALUE_MISMATCH,
1237
44
         "%s: mismatch in block size ( %" PRIu32 " != %" PRIu32 " ).",
1238
44
         function,
1239
44
         block_size,
1240
44
         block_size_copy );
1241
1242
44
        goto on_error;
1243
44
      }
1244
4.77k
      if( block_size == 0 )
1245
778
      {
1246
778
        break;
1247
778
      }
1248
3.99k
      if( (size_t) block_size > ( bit_stream->byte_stream_size - bit_stream->byte_stream_offset ) )
1249
19
      {
1250
19
        libcerror_error_set(
1251
19
         error,
1252
19
         LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1253
19
         LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
1254
19
         "%s: invalid compressed data value too small.",
1255
19
         function );
1256
1257
19
        goto on_error;
1258
19
      }
1259
3.97k
      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.97k
      if( memory_copy(
1271
3.97k
           &( uncompressed_data[ safe_uncompressed_data_offset ] ),
1272
3.97k
           &( bit_stream->byte_stream[ bit_stream->byte_stream_offset ] ),
1273
3.97k
           (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.97k
      bit_stream->byte_stream_offset += block_size;
1285
3.97k
      safe_uncompressed_data_offset  += block_size;
1286
1287
      /* Flush the bit stream buffer
1288
       */
1289
3.97k
      bit_stream->bit_buffer      = 0;
1290
3.97k
      bit_stream->bit_buffer_size = 0;
1291
1292
3.97k
      break;
1293
1294
5.51k
    case LIBEWF_DEFLATE_BLOCK_TYPE_HUFFMAN_FIXED:
1295
5.51k
      if( libewf_deflate_decode_huffman(
1296
5.51k
           bit_stream,
1297
5.51k
           fixed_huffman_literals_tree,
1298
5.51k
           fixed_huffman_distances_tree,
1299
5.51k
           uncompressed_data,
1300
5.51k
           uncompressed_data_size,
1301
5.51k
           &safe_uncompressed_data_offset,
1302
5.51k
           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.46k
      break;
1314
1315
5.46k
    case LIBEWF_DEFLATE_BLOCK_TYPE_HUFFMAN_DYNAMIC:
1316
4.39k
      if( libewf_huffman_tree_initialize(
1317
4.39k
           &dynamic_huffman_literals_tree,
1318
4.39k
           288,
1319
4.39k
           15,
1320
4.39k
           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.39k
      if( libewf_huffman_tree_initialize(
1332
4.39k
           &dynamic_huffman_distances_tree,
1333
4.39k
           30,
1334
4.39k
           15,
1335
4.39k
           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.39k
      if( libewf_deflate_build_dynamic_huffman_trees(
1347
4.39k
           bit_stream,
1348
4.39k
           dynamic_huffman_literals_tree,
1349
4.39k
           dynamic_huffman_distances_tree,
1350
4.39k
           error ) != 1 )
1351
87
      {
1352
87
        libcerror_error_set(
1353
87
         error,
1354
87
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1355
87
         LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1356
87
         "%s: unable to build dynamic Huffman trees.",
1357
87
         function );
1358
1359
87
        goto on_error;
1360
87
      }
1361
4.31k
      if( libewf_deflate_decode_huffman(
1362
4.31k
           bit_stream,
1363
4.31k
           dynamic_huffman_literals_tree,
1364
4.31k
           dynamic_huffman_distances_tree,
1365
4.31k
           uncompressed_data,
1366
4.31k
           uncompressed_data_size,
1367
4.31k
           &safe_uncompressed_data_offset,
1368
4.31k
           error ) != 1 )
1369
11
      {
1370
11
        libcerror_error_set(
1371
11
         error,
1372
11
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1373
11
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1374
11
         "%s: unable to decode dynamic Huffman encoded bit stream.",
1375
11
         function );
1376
1377
11
        goto on_error;
1378
11
      }
1379
4.29k
      if( libewf_huffman_tree_free(
1380
4.29k
           &dynamic_huffman_distances_tree,
1381
4.29k
           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.29k
      if( libewf_huffman_tree_free(
1393
4.29k
           &dynamic_huffman_literals_tree,
1394
4.29k
           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.29k
      break;
1406
1407
4.29k
    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.7k
  }
1418
14.5k
  *uncompressed_data_offset = safe_uncompressed_data_offset;
1419
1420
14.5k
  return( 1 );
1421
1422
216
on_error:
1423
216
  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
216
  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
216
  return( -1 );
1436
14.7k
}
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.54k
{
1716
3.54k
  libewf_bit_stream_t *bit_stream                     = NULL;
1717
3.54k
  libewf_huffman_tree_t *fixed_huffman_distances_tree = NULL;
1718
3.54k
  libewf_huffman_tree_t *fixed_huffman_literals_tree  = NULL;
1719
3.54k
  static char *function                               = "libewf_deflate_decompress_zlib";
1720
3.54k
  size_t compressed_data_offset                       = 0;
1721
3.54k
  size_t safe_uncompressed_data_size                  = 0;
1722
3.54k
  size_t uncompressed_data_offset                     = 0;
1723
3.54k
  uint32_t calculated_checksum                        = 0;
1724
3.54k
  uint32_t stored_checksum                            = 0;
1725
3.54k
  uint8_t block_type                                  = 0;
1726
3.54k
  uint8_t last_block_flag                             = 0;
1727
1728
3.54k
  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.54k
  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.54k
  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.54k
  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.54k
  safe_uncompressed_data_size = *uncompressed_data_size;
1773
1774
3.54k
  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.54k
  if( libewf_deflate_read_data_header(
1786
3.54k
       compressed_data,
1787
3.54k
       compressed_data_size,
1788
3.54k
       &compressed_data_offset,
1789
3.54k
       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.51k
  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.51k
  if( libewf_bit_stream_initialize(
1812
3.51k
       &bit_stream,
1813
3.51k
       compressed_data,
1814
3.51k
       compressed_data_size,
1815
3.51k
       compressed_data_offset,
1816
3.51k
       LIBEWF_BIT_STREAM_STORAGE_TYPE_BYTE_BACK_TO_FRONT,
1817
3.51k
       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.8k
  while( bit_stream->byte_stream_offset < bit_stream->byte_stream_size )
1829
14.7k
  {
1830
14.7k
    if( libewf_deflate_read_block_header(
1831
14.7k
         bit_stream,
1832
14.7k
         &block_type,
1833
14.7k
         &last_block_flag,
1834
14.7k
         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.7k
    if( block_type == LIBEWF_DEFLATE_BLOCK_TYPE_HUFFMAN_FIXED )
1846
5.51k
    {
1847
5.51k
      if( ( fixed_huffman_literals_tree == NULL )
1848
484
       && ( fixed_huffman_distances_tree == NULL ) )
1849
484
      {
1850
484
        if( libewf_huffman_tree_initialize(
1851
484
             &fixed_huffman_literals_tree,
1852
484
             288,
1853
484
             15,
1854
484
             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
484
        if( libewf_huffman_tree_initialize(
1866
484
             &fixed_huffman_distances_tree,
1867
484
             30,
1868
484
             15,
1869
484
             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
484
        if( libewf_deflate_build_fixed_huffman_trees(
1881
484
             fixed_huffman_literals_tree,
1882
484
             fixed_huffman_distances_tree,
1883
484
             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
484
      }
1895
5.51k
    }
1896
14.7k
    if( libewf_deflate_read_block(
1897
14.7k
         bit_stream,
1898
14.7k
         block_type,
1899
14.7k
         fixed_huffman_literals_tree,
1900
14.7k
         fixed_huffman_distances_tree,
1901
14.7k
         uncompressed_data,
1902
14.7k
         safe_uncompressed_data_size,
1903
14.7k
         &uncompressed_data_offset,
1904
14.7k
         error ) != 1 )
1905
216
    {
1906
216
      libcerror_error_set(
1907
216
       error,
1908
216
       LIBCERROR_ERROR_DOMAIN_IO,
1909
216
       LIBCERROR_IO_ERROR_READ_FAILED,
1910
216
       "%s: unable to read block of compressed data.",
1911
216
       function );
1912
1913
216
      goto on_error;
1914
216
    }
1915
14.5k
    if( last_block_flag != 0 )
1916
3.17k
    {
1917
3.17k
      break;
1918
3.17k
    }
1919
14.5k
  }
1920
3.29k
  if( ( bit_stream->byte_stream_size - bit_stream->byte_stream_offset ) >= 4 )
1921
2.57k
  {
1922
2.57k
    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.57k
    byte_stream_copy_to_uint32_big_endian(
1928
2.57k
     &( bit_stream->byte_stream[ bit_stream->byte_stream_offset ] ),
1929
2.57k
     stored_checksum );
1930
1931
2.57k
    if( libewf_deflate_calculate_adler32(
1932
2.57k
         &calculated_checksum,
1933
2.57k
         uncompressed_data,
1934
2.57k
         uncompressed_data_offset,
1935
2.57k
         1,
1936
2.57k
         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.57k
    if( stored_checksum != calculated_checksum )
1948
180
    {
1949
180
      libcerror_error_set(
1950
180
       error,
1951
180
       LIBCERROR_ERROR_DOMAIN_INPUT,
1952
180
       LIBCERROR_INPUT_ERROR_CHECKSUM_MISMATCH,
1953
180
       "%s: checksum does not match (stored: 0x%08" PRIx32 ", calculated: 0x%08" PRIx32 ").",
1954
180
       function,
1955
180
       stored_checksum,
1956
180
       calculated_checksum );
1957
1958
180
      goto on_error;
1959
180
    }
1960
2.57k
  }
1961
3.11k
  if( fixed_huffman_distances_tree != NULL )
1962
254
  {
1963
254
    if( libewf_huffman_tree_free(
1964
254
         &fixed_huffman_distances_tree,
1965
254
         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
254
  }
1977
3.11k
  if( fixed_huffman_literals_tree != NULL )
1978
254
  {
1979
254
    if( libewf_huffman_tree_free(
1980
254
         &fixed_huffman_literals_tree,
1981
254
         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
254
  }
1993
3.11k
  if( libewf_bit_stream_free(
1994
3.11k
       &bit_stream,
1995
3.11k
       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.11k
  *uncompressed_data_size = uncompressed_data_offset;
2007
2008
3.11k
  return( 1 );
2009
2010
421
on_error:
2011
421
  if( fixed_huffman_distances_tree != NULL )
2012
230
  {
2013
230
    libewf_huffman_tree_free(
2014
230
     &fixed_huffman_distances_tree,
2015
230
     NULL );
2016
230
  }
2017
421
  if( fixed_huffman_literals_tree != NULL )
2018
230
  {
2019
230
    libewf_huffman_tree_free(
2020
230
     &fixed_huffman_literals_tree,
2021
230
     NULL );
2022
230
  }
2023
421
  if( bit_stream != NULL )
2024
396
  {
2025
396
    libewf_bit_stream_free(
2026
396
     &bit_stream,
2027
     NULL );
2028
396
  }
2029
421
  return( -1 );
2030
3.11k
}
2031