Coverage Report

Created: 2026-10-22 07:28

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.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.53k
  number_of_distance_codes += 1;
110
111
4.53k
  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.53k
  number_of_code_sizes += 4;
123
124
4.53k
  for( code_size_index = 0;
125
78.4k
       code_size_index < number_of_code_sizes;
126
73.9k
       code_size_index++ )
127
73.9k
  {
128
73.9k
    if( libewf_bit_stream_get_value(
129
73.9k
         bit_stream,
130
73.9k
         3,
131
73.9k
         &code_size,
132
73.9k
         error ) != 1 )
133
5
    {
134
5
      libcerror_error_set(
135
5
       error,
136
5
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
137
5
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
138
5
       "%s: unable to retrieve value from bit stream.",
139
5
       function );
140
141
5
      goto on_error;
142
5
    }
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.2k
  {
149
12.2k
    code_size_sequence = libewf_deflate_code_sizes_sequence[ code_size_index++ ];
150
151
12.2k
    code_size_array[ code_size_sequence ] = 0;
152
12.2k
  }
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
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.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
302k
  {
189
302k
    if( libewf_huffman_tree_get_symbol_from_bit_stream(
190
302k
         codes_tree,
191
302k
         bit_stream,
192
302k
         &symbol,
193
302k
         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
302k
    if( symbol < 16 )
205
264k
    {
206
264k
      code_size_array[ code_size_index++ ] = (uint8_t) symbol;
207
208
264k
      continue;
209
264k
    }
210
38.1k
    code_size = 0;
211
212
38.1k
    if( symbol == 16 )
213
7.07k
    {
214
7.07k
      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
7.07k
      code_size = (uint32_t) code_size_array[ code_size_index - 1 ];
226
227
7.07k
      if( libewf_bit_stream_get_value(
228
7.07k
           bit_stream,
229
7.07k
           2,
230
7.07k
           &times_to_repeat,
231
7.07k
           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
7.06k
      times_to_repeat += 3;
243
7.06k
    }
244
31.0k
    else if( symbol == 17 )
245
12.0k
    {
246
12.0k
      if( libewf_bit_stream_get_value(
247
12.0k
           bit_stream,
248
12.0k
           3,
249
12.0k
           &times_to_repeat,
250
12.0k
           error ) != 1 )
251
4
      {
252
4
        libcerror_error_set(
253
4
         error,
254
4
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
255
4
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
256
4
         "%s: unable to retrieve value from bit stream.",
257
4
         function );
258
259
4
        goto on_error;
260
4
      }
261
12.0k
      times_to_repeat += 3;
262
12.0k
    }
263
18.9k
    else if( symbol == 18 )
264
18.9k
    {
265
18.9k
      if( libewf_bit_stream_get_value(
266
18.9k
           bit_stream,
267
18.9k
           7,
268
18.9k
           &times_to_repeat,
269
18.9k
           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
18.9k
      times_to_repeat += 11;
281
18.9k
    }
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
11
    {
295
11
      libcerror_error_set(
296
11
       error,
297
11
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
298
11
       LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
299
11
       "%s: invalid times to repeat value out of bounds.",
300
11
       function );
301
302
11
      goto on_error;
303
11
    }
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.0k
  }
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
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.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.45k
  return( 1 );
366
367
85
on_error:
368
85
  if( codes_tree != NULL )
369
57
  {
370
57
    libewf_huffman_tree_free(
371
57
     &codes_tree,
372
57
     NULL );
373
57
  }
374
85
  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
482
{
385
482
  uint8_t code_size_array[ 318 ];
386
387
482
  static char *function = "libewf_deflate_build_fixed_huffman_trees";
388
482
  uint16_t symbol       = 0;
389
390
482
  for( symbol = 0;
391
153k
       symbol < 318;
392
153k
       symbol++ )
393
153k
  {
394
153k
    if( symbol < 144 )
395
69.4k
    {
396
69.4k
      code_size_array[ symbol ] = 8;
397
69.4k
    }
398
83.8k
    else if( symbol < 256 )
399
53.9k
    {
400
53.9k
      code_size_array[ symbol ] = 9;
401
53.9k
    }
402
29.8k
    else if( symbol < 280 )
403
11.5k
    {
404
11.5k
      code_size_array[ symbol ] = 7;
405
11.5k
    }
406
18.3k
    else if( symbol < 288 )
407
3.85k
    {
408
3.85k
      code_size_array[ symbol ] = 8;
409
3.85k
    }
410
14.4k
    else
411
14.4k
    {
412
14.4k
      code_size_array[ symbol ] = 5;
413
14.4k
    }
414
153k
  }
415
482
  if( libewf_huffman_tree_build(
416
482
       literals_tree,
417
482
       code_size_array,
418
482
       288,
419
482
       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
482
  if( libewf_huffman_tree_build(
431
482
       distances_tree,
432
482
       &( code_size_array[ 288 ] ),
433
482
       30,
434
482
       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
482
  return( 1 );
446
482
}
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.73k
{
460
9.73k
  static char *function         = "libewf_deflate_decode_huffman";
461
9.73k
  size_t data_offset            = 0;
462
9.73k
  uint32_t extra_bits           = 0;
463
9.73k
  uint16_t compression_offset   = 0;
464
9.73k
  uint16_t compression_size     = 0;
465
9.73k
  uint16_t number_of_extra_bits = 0;
466
9.73k
  uint16_t symbol               = 0;
467
468
9.73k
  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.73k
  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.73k
  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.73k
  data_offset = *uncompressed_data_offset;
502
503
9.73k
  do
504
8.40M
  {
505
8.40M
    if( libewf_huffman_tree_get_symbol_from_bit_stream(
506
8.40M
         literals_tree,
507
8.40M
         bit_stream,
508
8.40M
         &symbol,
509
8.40M
         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.40M
    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.94M
    else if( ( symbol > 256 )
536
7.93M
          && ( symbol < 286 ) )
537
7.93M
    {
538
7.93M
      symbol -= 257;
539
540
7.93M
      number_of_extra_bits = libewf_deflate_literal_codes_number_of_extra_bits[ symbol ];
541
542
7.93M
      if( libewf_bit_stream_get_value(
543
7.93M
           bit_stream,
544
7.93M
           (uint8_t) number_of_extra_bits,
545
7.93M
           &extra_bits,
546
7.93M
           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.93M
      compression_size = libewf_deflate_literal_codes_base[ symbol ] + (uint16_t) extra_bits;
558
559
7.93M
      if( libewf_huffman_tree_get_symbol_from_bit_stream(
560
7.93M
           distances_tree,
561
7.93M
           bit_stream,
562
7.93M
           &symbol,
563
7.93M
           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.93M
      number_of_extra_bits = libewf_deflate_distance_codes_number_of_extra_bits[ symbol ];
575
576
7.93M
      if( libewf_bit_stream_get_value(
577
7.93M
           bit_stream,
578
7.93M
           (uint8_t) number_of_extra_bits,
579
7.93M
           &extra_bits,
580
7.93M
           error ) != 1 )
581
2
      {
582
2
        libcerror_error_set(
583
2
         error,
584
2
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
585
2
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
586
2
         "%s: unable to retrieve distance extra value from bit stream.",
587
2
         function );
588
589
2
        return( -1 );
590
2
      }
591
7.93M
      compression_offset = libewf_deflate_distance_codes_base[ symbol ] + (uint16_t) extra_bits;
592
593
7.93M
      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.93M
      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
32.6M
      while( compression_size > 0 )
616
24.7M
      {
617
24.7M
        uncompressed_data[ data_offset ] = uncompressed_data[ data_offset - compression_offset ];
618
619
24.7M
        data_offset++;
620
24.7M
        compression_size--;
621
24.7M
      }
622
7.93M
    }
623
9.68k
    else if( symbol != 256 )
624
5
    {
625
5
      libcerror_error_set(
626
5
       error,
627
5
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
628
5
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
629
5
       "%s: invalid code value: %" PRIu16 ".",
630
5
       function,
631
5
       symbol );
632
633
5
      return( -1 );
634
5
    }
635
8.40M
  }
636
8.40M
  while( symbol != 256 );
637
638
9.67k
  *uncompressed_data_offset = data_offset;
639
640
9.67k
  return( 1 );
641
9.73k
}
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.0k
{
654
24.0k
  static char *function = "libewf_deflate_calculate_adler32";
655
24.0k
  size_t data_offset    = 0;
656
24.0k
  uint32_t lower_word   = 0;
657
24.0k
  uint32_t upper_word   = 0;
658
24.0k
  uint32_t value_32bit  = 0;
659
24.0k
  int block_index       = 0;
660
661
24.0k
  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.0k
  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.0k
  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.0k
  lower_word = initial_value & 0xffff;
695
24.0k
  upper_word = ( initial_value >> 16 ) & 0xffff;
696
697
28.0k
  while( data_size >= 0x15b0 )
698
4.00k
  {
699
    /* The modulo calculation is needed per 5552 (0x15b0) bytes
700
     * 5552 / 16 = 347
701
     */
702
4.00k
    for( block_index = 0;
703
1.39M
         block_index < 347;
704
1.39M
         block_index++ )
705
1.39M
    {
706
1.39M
      lower_word += data[ data_offset++ ];
707
1.39M
      upper_word += lower_word;
708
709
1.39M
      lower_word += data[ data_offset++ ];
710
1.39M
      upper_word += lower_word;
711
712
1.39M
      lower_word += data[ data_offset++ ];
713
1.39M
      upper_word += lower_word;
714
715
1.39M
      lower_word += data[ data_offset++ ];
716
1.39M
      upper_word += lower_word;
717
718
1.39M
      lower_word += data[ data_offset++ ];
719
1.39M
      upper_word += lower_word;
720
721
1.39M
      lower_word += data[ data_offset++ ];
722
1.39M
      upper_word += lower_word;
723
724
1.39M
      lower_word += data[ data_offset++ ];
725
1.39M
      upper_word += lower_word;
726
727
1.39M
      lower_word += data[ data_offset++ ];
728
1.39M
      upper_word += lower_word;
729
730
1.39M
      lower_word += data[ data_offset++ ];
731
1.39M
      upper_word += lower_word;
732
733
1.39M
      lower_word += data[ data_offset++ ];
734
1.39M
      upper_word += lower_word;
735
736
1.39M
      lower_word += data[ data_offset++ ];
737
1.39M
      upper_word += lower_word;
738
739
1.39M
      lower_word += data[ data_offset++ ];
740
1.39M
      upper_word += lower_word;
741
742
1.39M
      lower_word += data[ data_offset++ ];
743
1.39M
      upper_word += lower_word;
744
745
1.39M
      lower_word += data[ data_offset++ ];
746
1.39M
      upper_word += lower_word;
747
748
1.39M
      lower_word += data[ data_offset++ ];
749
1.39M
      upper_word += lower_word;
750
751
1.39M
      lower_word += data[ data_offset++ ];
752
1.39M
      upper_word += lower_word;
753
1.39M
    }
754
    /* Optimized equivalent of:
755
     * lower_word %= 0xfff1
756
     */
757
4.00k
    value_32bit = lower_word >> 16;
758
4.00k
    lower_word &= 0x0000ffffUL;
759
4.00k
    lower_word += ( value_32bit << 4 ) - value_32bit;
760
761
4.00k
    if( lower_word > 65521 )
762
48
    {
763
48
      value_32bit = lower_word >> 16;
764
48
      lower_word &= 0x0000ffffUL;
765
48
      lower_word += ( value_32bit << 4 ) - value_32bit;
766
48
    }
767
4.00k
    if( lower_word >= 65521 )
768
18
    {
769
18
      lower_word -= 65521;
770
18
    }
771
    /* Optimized equivalent of:
772
     * upper_word %= 0xfff1
773
     */
774
4.00k
    value_32bit = upper_word >> 16;
775
4.00k
    upper_word &= 0x0000ffffUL;
776
4.00k
    upper_word += ( value_32bit << 4 ) - value_32bit;
777
778
4.00k
    if( upper_word > 65521 )
779
3.63k
    {
780
3.63k
      value_32bit = upper_word >> 16;
781
3.63k
      upper_word &= 0x0000ffffUL;
782
3.63k
      upper_word += ( value_32bit << 4 ) - value_32bit;
783
3.63k
    }
784
4.00k
    if( upper_word >= 65521 )
785
27
    {
786
27
      upper_word -= 65521;
787
27
    }
788
4.00k
    data_size -= 0x15b0;
789
4.00k
  }
790
24.0k
  if( data_size > 0 )
791
24.0k
  {
792
257k
    while( data_size > 16 )
793
233k
    {
794
233k
      lower_word += data[ data_offset++ ];
795
233k
      upper_word += lower_word;
796
797
233k
      lower_word += data[ data_offset++ ];
798
233k
      upper_word += lower_word;
799
800
233k
      lower_word += data[ data_offset++ ];
801
233k
      upper_word += lower_word;
802
803
233k
      lower_word += data[ data_offset++ ];
804
233k
      upper_word += lower_word;
805
806
233k
      lower_word += data[ data_offset++ ];
807
233k
      upper_word += lower_word;
808
809
233k
      lower_word += data[ data_offset++ ];
810
233k
      upper_word += lower_word;
811
812
233k
      lower_word += data[ data_offset++ ];
813
233k
      upper_word += lower_word;
814
815
233k
      lower_word += data[ data_offset++ ];
816
233k
      upper_word += lower_word;
817
818
233k
      lower_word += data[ data_offset++ ];
819
233k
      upper_word += lower_word;
820
821
233k
      lower_word += data[ data_offset++ ];
822
233k
      upper_word += lower_word;
823
824
233k
      lower_word += data[ data_offset++ ];
825
233k
      upper_word += lower_word;
826
827
233k
      lower_word += data[ data_offset++ ];
828
233k
      upper_word += lower_word;
829
830
233k
      lower_word += data[ data_offset++ ];
831
233k
      upper_word += lower_word;
832
833
233k
      lower_word += data[ data_offset++ ];
834
233k
      upper_word += lower_word;
835
836
233k
      lower_word += data[ data_offset++ ];
837
233k
      upper_word += lower_word;
838
839
233k
      lower_word += data[ data_offset++ ];
840
233k
      upper_word += lower_word;
841
842
233k
      data_size -= 16;
843
233k
    }
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.0k
    value_32bit = lower_word >> 16;
855
24.0k
    lower_word &= 0x0000ffffUL;
856
24.0k
    lower_word += ( value_32bit << 4 ) - value_32bit;
857
858
24.0k
    if( lower_word > 65521 )
859
5
    {
860
5
      value_32bit = lower_word >> 16;
861
5
      lower_word &= 0x0000ffffUL;
862
5
      lower_word += ( value_32bit << 4 ) - value_32bit;
863
5
    }
864
24.0k
    if( lower_word >= 65521 )
865
7
    {
866
7
      lower_word -= 65521;
867
7
    }
868
    /* Optimized equivalent of:
869
     * upper_word %= 0xfff1
870
     */
871
24.0k
    value_32bit = upper_word >> 16;
872
24.0k
    upper_word &= 0x0000ffffUL;
873
24.0k
    upper_word += ( value_32bit << 4 ) - value_32bit;
874
875
24.0k
    if( upper_word > 65521 )
876
267
    {
877
267
      value_32bit = upper_word >> 16;
878
267
      upper_word &= 0x0000ffffUL;
879
267
      upper_word += ( value_32bit << 4 ) - value_32bit;
880
267
    }
881
24.0k
    if( upper_word >= 65521 )
882
15
    {
883
15
      upper_word -= 65521;
884
15
    }
885
24.0k
  }
886
24.0k
  *checksum_value = ( upper_word << 16 ) | lower_word;
887
888
24.0k
  return( 1 );
889
24.0k
}
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.63k
{
900
3.63k
  static char *function                 = "libewf_deflate_read_data_header";
901
3.63k
  size_t safe_offset                    = 0;
902
3.63k
  uint32_t compression_window_size      = 0;
903
3.63k
  uint8_t flags                         = 0;
904
3.63k
  uint8_t compression_data              = 0;
905
3.63k
  uint8_t compression_information       = 0;
906
3.63k
  uint8_t compression_method            = 0;
907
3.63k
  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.63k
  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.63k
  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.63k
  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.63k
  safe_offset = *compressed_data_offset;
947
948
3.63k
  if( ( compressed_data_size < 2 )
949
3.63k
   || ( safe_offset > ( compressed_data_size - 2 ) ) )
950
1
  {
951
1
    libcerror_error_set(
952
1
     error,
953
1
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
954
1
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
955
1
     "%s: invalid compressed data value too small.",
956
1
     function );
957
958
1
    return( -1 );
959
1
  }
960
3.63k
  compression_data = compressed_data[ safe_offset++ ];
961
3.63k
  flags            = compressed_data[ safe_offset++ ];
962
963
3.63k
  compression_method      = compression_data & 0x0f;
964
3.63k
  compression_information = compression_data >> 4;
965
966
/* TODO validate check bits */
967
3.63k
  if( ( flags & 0x20 ) != 0 )
968
192
  {
969
192
    if( ( compressed_data_size < 6 )
970
191
     || ( safe_offset > ( compressed_data_size - 6 ) ) )
971
3
    {
972
3
      libcerror_error_set(
973
3
       error,
974
3
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
975
3
       LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
976
3
       "%s: invalid compressed data value too small.",
977
3
       function );
978
979
3
      return( -1 );
980
3
    }
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
189
    safe_offset += 4;
987
189
  }
988
3.63k
  if( compression_method != 8 )
989
16
  {
990
16
    libcerror_error_set(
991
16
     error,
992
16
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
993
16
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
994
16
     "%s: unsupported compression method: %" PRIu8 ".",
995
16
     function,
996
16
     compression_method );
997
998
16
    return( -1 );
999
16
  }
1000
3.61k
  compression_window_bits = (uint8_t) compression_information + 8;
1001
3.61k
  compression_window_size = (uint32_t) 1UL << compression_window_bits;
1002
1003
3.61k
  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.61k
  *compressed_data_offset += safe_offset;
1016
1017
3.61k
  return( 1 );
1018
3.61k
}
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.3k
{
1029
14.3k
  static char *function = "libewf_deflate_read_block_header";
1030
14.3k
  uint32_t value_32bit  = 0;
1031
1032
14.3k
  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.3k
  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.3k
  if( libewf_bit_stream_get_value(
1055
14.3k
       bit_stream,
1056
14.3k
       3,
1057
14.3k
       &value_32bit,
1058
14.3k
       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.3k
  *last_block_flag = (uint8_t) ( value_32bit & 0x00000001UL );
1070
14.3k
  value_32bit    >>= 1;
1071
14.3k
  *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.3k
  return( 1 );
1118
14.3k
}
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.3k
{
1133
14.3k
  libewf_huffman_tree_t *dynamic_huffman_distances_tree = NULL;
1134
14.3k
  libewf_huffman_tree_t *dynamic_huffman_literals_tree  = NULL;
1135
14.3k
  static char *function                                 = "libewf_deflate_read_block";
1136
14.3k
  size_t safe_uncompressed_data_offset                  = 0;
1137
14.3k
  uint32_t block_size                                   = 0;
1138
14.3k
  uint32_t block_size_copy                              = 0;
1139
14.3k
  uint32_t value_32bit                                  = 0;
1140
14.3k
  uint8_t skip_bits                                     = 0;
1141
1142
14.3k
  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.3k
  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.3k
  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.3k
  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.3k
  safe_uncompressed_data_offset = *uncompressed_data_offset;
1187
1188
14.3k
  switch( block_type )
1189
14.3k
  {
1190
4.57k
    case LIBEWF_DEFLATE_BLOCK_TYPE_UNCOMPRESSED:
1191
      /* Ignore the bits in the buffer upto the next byte
1192
       */
1193
4.57k
      skip_bits = bit_stream->bit_buffer_size & 0x07;
1194
1195
4.57k
      if( skip_bits > 0 )
1196
1.94k
      {
1197
1.94k
        if( libewf_bit_stream_get_value(
1198
1.94k
             bit_stream,
1199
1.94k
             skip_bits,
1200
1.94k
             &value_32bit,
1201
1.94k
             error ) != 1 )
1202
0
        {
1203
0
          libcerror_error_set(
1204
0
           error,
1205
0
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
1206
0
           LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1207
0
           "%s: unable to retrieve value from bit stream.",
1208
0
           function );
1209
1210
0
          goto on_error;
1211
0
        }
1212
1.94k
      }
1213
4.57k
      if( libewf_bit_stream_get_value(
1214
4.57k
           bit_stream,
1215
4.57k
           32,
1216
4.57k
           &block_size,
1217
4.57k
           error ) != 1 )
1218
7
      {
1219
7
        libcerror_error_set(
1220
7
         error,
1221
7
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1222
7
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1223
7
         "%s: unable to retrieve value from bit stream.",
1224
7
         function );
1225
1226
7
        goto on_error;
1227
7
      }
1228
4.56k
      block_size_copy = ( block_size >> 16 ) ^ 0x0000ffffUL;
1229
4.56k
      block_size     &= 0x0000ffffUL;
1230
1231
4.56k
      if( block_size != block_size_copy )
1232
42
      {
1233
42
        libcerror_error_set(
1234
42
         error,
1235
42
         LIBCERROR_ERROR_DOMAIN_INPUT,
1236
42
         LIBCERROR_INPUT_ERROR_VALUE_MISMATCH,
1237
42
         "%s: mismatch in block size ( %" PRIu32 " != %" PRIu32 " ).",
1238
42
         function,
1239
42
         block_size,
1240
42
         block_size_copy );
1241
1242
42
        goto on_error;
1243
42
      }
1244
4.52k
      if( block_size == 0 )
1245
741
      {
1246
741
        break;
1247
741
      }
1248
3.78k
      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.76k
      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.76k
      if( memory_copy(
1271
3.76k
           &( uncompressed_data[ safe_uncompressed_data_offset ] ),
1272
3.76k
           &( bit_stream->byte_stream[ bit_stream->byte_stream_offset ] ),
1273
3.76k
           (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.76k
      bit_stream->byte_stream_offset += block_size;
1285
3.76k
      safe_uncompressed_data_offset  += block_size;
1286
1287
      /* Flush the bit stream buffer
1288
       */
1289
3.76k
      bit_stream->bit_buffer      = 0;
1290
3.76k
      bit_stream->bit_buffer_size = 0;
1291
1292
3.76k
      break;
1293
1294
5.27k
    case LIBEWF_DEFLATE_BLOCK_TYPE_HUFFMAN_FIXED:
1295
5.27k
      if( libewf_deflate_decode_huffman(
1296
5.27k
           bit_stream,
1297
5.27k
           fixed_huffman_literals_tree,
1298
5.27k
           fixed_huffman_distances_tree,
1299
5.27k
           uncompressed_data,
1300
5.27k
           uncompressed_data_size,
1301
5.27k
           &safe_uncompressed_data_offset,
1302
5.27k
           error ) != 1 )
1303
44
      {
1304
44
        libcerror_error_set(
1305
44
         error,
1306
44
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1307
44
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1308
44
         "%s: unable to decode fixed Huffman encoded bit stream.",
1309
44
         function );
1310
1311
44
        goto on_error;
1312
44
      }
1313
5.23k
      break;
1314
1315
5.23k
    case LIBEWF_DEFLATE_BLOCK_TYPE_HUFFMAN_DYNAMIC:
1316
4.54k
      if( libewf_huffman_tree_initialize(
1317
4.54k
           &dynamic_huffman_literals_tree,
1318
4.54k
           288,
1319
4.54k
           15,
1320
4.54k
           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.54k
      if( libewf_huffman_tree_initialize(
1332
4.54k
           &dynamic_huffman_distances_tree,
1333
4.54k
           30,
1334
4.54k
           15,
1335
4.54k
           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.54k
      if( libewf_deflate_build_dynamic_huffman_trees(
1347
4.54k
           bit_stream,
1348
4.54k
           dynamic_huffman_literals_tree,
1349
4.54k
           dynamic_huffman_distances_tree,
1350
4.54k
           error ) != 1 )
1351
85
      {
1352
85
        libcerror_error_set(
1353
85
         error,
1354
85
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1355
85
         LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1356
85
         "%s: unable to build dynamic Huffman trees.",
1357
85
         function );
1358
1359
85
        goto on_error;
1360
85
      }
1361
4.45k
      if( libewf_deflate_decode_huffman(
1362
4.45k
           bit_stream,
1363
4.45k
           dynamic_huffman_literals_tree,
1364
4.45k
           dynamic_huffman_distances_tree,
1365
4.45k
           uncompressed_data,
1366
4.45k
           uncompressed_data_size,
1367
4.45k
           &safe_uncompressed_data_offset,
1368
4.45k
           error ) != 1 )
1369
14
      {
1370
14
        libcerror_error_set(
1371
14
         error,
1372
14
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1373
14
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1374
14
         "%s: unable to decode dynamic Huffman encoded bit stream.",
1375
14
         function );
1376
1377
14
        goto on_error;
1378
14
      }
1379
4.44k
      if( libewf_huffman_tree_free(
1380
4.44k
           &dynamic_huffman_distances_tree,
1381
4.44k
           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.44k
      if( libewf_huffman_tree_free(
1393
4.44k
           &dynamic_huffman_literals_tree,
1394
4.44k
           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.44k
      break;
1406
1407
4.44k
    case LIBEWF_DEFLATE_BLOCK_TYPE_RESERVED:
1408
4
    default:
1409
4
      libcerror_error_set(
1410
4
       error,
1411
4
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1412
4
       LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
1413
4
       "%s: unsupported block type.",
1414
4
       function );
1415
1416
4
      goto on_error;
1417
14.3k
  }
1418
14.1k
  *uncompressed_data_offset = safe_uncompressed_data_offset;
1419
1420
14.1k
  return( 1 );
1421
1422
216
on_error:
1423
216
  if( dynamic_huffman_distances_tree != NULL )
1424
99
  {
1425
99
    libewf_huffman_tree_free(
1426
99
     &dynamic_huffman_distances_tree,
1427
99
     NULL );
1428
99
  }
1429
216
  if( dynamic_huffman_literals_tree != NULL )
1430
99
  {
1431
99
    libewf_huffman_tree_free(
1432
99
     &dynamic_huffman_literals_tree,
1433
99
     NULL );
1434
99
  }
1435
216
  return( -1 );
1436
14.3k
}
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.63k
{
1716
3.63k
  libewf_bit_stream_t *bit_stream                     = NULL;
1717
3.63k
  libewf_huffman_tree_t *fixed_huffman_distances_tree = NULL;
1718
3.63k
  libewf_huffman_tree_t *fixed_huffman_literals_tree  = NULL;
1719
3.63k
  static char *function                               = "libewf_deflate_decompress_zlib";
1720
3.63k
  size_t compressed_data_offset                       = 0;
1721
3.63k
  size_t safe_uncompressed_data_size                  = 0;
1722
3.63k
  size_t uncompressed_data_offset                     = 0;
1723
3.63k
  uint32_t calculated_checksum                        = 0;
1724
3.63k
  uint32_t stored_checksum                            = 0;
1725
3.63k
  uint8_t block_type                                  = 0;
1726
3.63k
  uint8_t last_block_flag                             = 0;
1727
1728
3.63k
  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.63k
  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.63k
  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.63k
  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.63k
  safe_uncompressed_data_size = *uncompressed_data_size;
1773
1774
3.63k
  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.63k
  if( libewf_deflate_read_data_header(
1786
3.63k
       compressed_data,
1787
3.63k
       compressed_data_size,
1788
3.63k
       &compressed_data_offset,
1789
3.63k
       error ) != 1 )
1790
21
  {
1791
21
    libcerror_error_set(
1792
21
     error,
1793
21
     LIBCERROR_ERROR_DOMAIN_IO,
1794
21
     LIBCERROR_IO_ERROR_READ_FAILED,
1795
21
     "%s: unable to read data header.",
1796
21
     function );
1797
1798
21
    goto on_error;
1799
21
  }
1800
3.61k
  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.61k
  if( libewf_bit_stream_initialize(
1812
3.61k
       &bit_stream,
1813
3.61k
       compressed_data,
1814
3.61k
       compressed_data_size,
1815
3.61k
       compressed_data_offset,
1816
3.61k
       LIBEWF_BIT_STREAM_STORAGE_TYPE_BYTE_BACK_TO_FRONT,
1817
3.61k
       error ) != 1 )
1818
0
  {
1819
0
    libcerror_error_set(
1820
0
     error,
1821
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1822
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1823
0
     "%s: unable to create bit stream.",
1824
0
     function );
1825
1826
0
    goto on_error;
1827
0
  }
1828
14.5k
  while( bit_stream->byte_stream_offset < bit_stream->byte_stream_size )
1829
14.3k
  {
1830
14.3k
    if( libewf_deflate_read_block_header(
1831
14.3k
         bit_stream,
1832
14.3k
         &block_type,
1833
14.3k
         &last_block_flag,
1834
14.3k
         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.3k
    if( block_type == LIBEWF_DEFLATE_BLOCK_TYPE_HUFFMAN_FIXED )
1846
5.27k
    {
1847
5.27k
      if( ( fixed_huffman_literals_tree == NULL )
1848
482
       && ( fixed_huffman_distances_tree == NULL ) )
1849
482
      {
1850
482
        if( libewf_huffman_tree_initialize(
1851
482
             &fixed_huffman_literals_tree,
1852
482
             288,
1853
482
             15,
1854
482
             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
482
        if( libewf_huffman_tree_initialize(
1866
482
             &fixed_huffman_distances_tree,
1867
482
             30,
1868
482
             15,
1869
482
             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
482
        if( libewf_deflate_build_fixed_huffman_trees(
1881
482
             fixed_huffman_literals_tree,
1882
482
             fixed_huffman_distances_tree,
1883
482
             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
482
      }
1895
5.27k
    }
1896
14.3k
    if( libewf_deflate_read_block(
1897
14.3k
         bit_stream,
1898
14.3k
         block_type,
1899
14.3k
         fixed_huffman_literals_tree,
1900
14.3k
         fixed_huffman_distances_tree,
1901
14.3k
         uncompressed_data,
1902
14.3k
         safe_uncompressed_data_size,
1903
14.3k
         &uncompressed_data_offset,
1904
14.3k
         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.1k
    if( last_block_flag != 0 )
1916
3.27k
    {
1917
3.27k
      break;
1918
3.27k
    }
1919
14.1k
  }
1920
3.39k
  if( ( bit_stream->byte_stream_size - bit_stream->byte_stream_offset ) >= 4 )
1921
2.63k
  {
1922
2.63k
    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.63k
    byte_stream_copy_to_uint32_big_endian(
1928
2.63k
     &( bit_stream->byte_stream[ bit_stream->byte_stream_offset ] ),
1929
2.63k
     stored_checksum );
1930
1931
2.63k
    if( libewf_deflate_calculate_adler32(
1932
2.63k
         &calculated_checksum,
1933
2.63k
         uncompressed_data,
1934
2.63k
         uncompressed_data_offset,
1935
2.63k
         1,
1936
2.63k
         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.63k
    if( stored_checksum != calculated_checksum )
1948
187
    {
1949
187
      libcerror_error_set(
1950
187
       error,
1951
187
       LIBCERROR_ERROR_DOMAIN_INPUT,
1952
187
       LIBCERROR_INPUT_ERROR_CHECKSUM_MISMATCH,
1953
187
       "%s: checksum does not match (stored: 0x%08" PRIx32 ", calculated: 0x%08" PRIx32 ").",
1954
187
       function,
1955
187
       stored_checksum,
1956
187
       calculated_checksum );
1957
1958
187
      goto on_error;
1959
187
    }
1960
2.63k
  }
1961
3.21k
  if( fixed_huffman_distances_tree != NULL )
1962
255
  {
1963
255
    if( libewf_huffman_tree_free(
1964
255
         &fixed_huffman_distances_tree,
1965
255
         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
255
  }
1977
3.21k
  if( fixed_huffman_literals_tree != NULL )
1978
255
  {
1979
255
    if( libewf_huffman_tree_free(
1980
255
         &fixed_huffman_literals_tree,
1981
255
         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
255
  }
1993
3.21k
  if( libewf_bit_stream_free(
1994
3.21k
       &bit_stream,
1995
3.21k
       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.21k
  *uncompressed_data_size = uncompressed_data_offset;
2007
2008
3.21k
  return( 1 );
2009
2010
427
on_error:
2011
427
  if( fixed_huffman_distances_tree != NULL )
2012
227
  {
2013
227
    libewf_huffman_tree_free(
2014
227
     &fixed_huffman_distances_tree,
2015
227
     NULL );
2016
227
  }
2017
427
  if( fixed_huffman_literals_tree != NULL )
2018
227
  {
2019
227
    libewf_huffman_tree_free(
2020
227
     &fixed_huffman_literals_tree,
2021
227
     NULL );
2022
227
  }
2023
427
  if( bit_stream != NULL )
2024
403
  {
2025
403
    libewf_bit_stream_free(
2026
403
     &bit_stream,
2027
     NULL );
2028
403
  }
2029
427
  return( -1 );
2030
3.21k
}
2031