Coverage Report

Created: 2023-06-07 06:53

/src/libmodi/libfmos/libfmos_lzfse.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * LZFSE (un)compression functions
3
 *
4
 * Copyright (C) 2019-2022, 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 "libfmos_definitions.h"
28
#include "libfmos_libcerror.h"
29
#include "libfmos_libcnotify.h"
30
#include "libfmos_lzfse.h"
31
#include "libfmos_lzfse_bit_stream.h"
32
#include "libfmos_lzfse_decoder.h"
33
#include "libfmos_lzvn.h"
34
35
const uint8_t libfmos_lzfse_frequency_number_of_bits_table[ 32 ] = {
36
      2, 3, 2, 5, 2, 3, 2, 8, 2, 3, 2, 5, 2, 3, 2, 14,
37
      2, 3, 2, 5, 2, 3, 2, 8, 2, 3, 2, 5, 2, 3, 2, 14 };
38
39
const uint16_t libfmos_lzfse_frequency_value_table[ 32 ] = {
40
      0, 2, 1, 4, 0, 3, 1, 0xffff, 0, 2, 1, 5, 0, 3, 1, 0xffff,
41
      0, 2, 1, 6, 0, 3, 1, 0xffff, 0, 2, 1, 7, 0, 3, 1, 0xffff };
42
43
const uint8_t libfmos_lzfse_d_value_bits_table[ LIBFMOS_LZFSE_NUMBER_OF_D_VALUE_SYMBOLS ] = {
44
  0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3,
45
  4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7,
46
  8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11,
47
  12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15 };
48
49
const int32_t libfmos_lzfse_d_value_base_table[ LIBFMOS_LZFSE_NUMBER_OF_D_VALUE_SYMBOLS ] = {
50
  0, 1, 2, 3, 4, 6, 8, 10, 12, 16, 20, 24, 28, 36, 44, 52,
51
        60, 76, 92, 108, 124, 156, 188, 220, 252, 316, 380, 444, 508, 636, 764, 892,
52
        1020, 1276, 1532, 1788, 2044, 2556, 3068, 3580, 4092, 5116, 6140, 7164, 8188, 10236, 12284, 14332,
53
        16380, 20476, 24572, 28668, 32764, 40956, 49148, 57340, 65532, 81916, 98300, 114684, 131068, 163836, 196604, 229372 };
54
55
const uint8_t libfmos_lzfse_l_value_bits_table[ LIBFMOS_LZFSE_NUMBER_OF_L_VALUE_SYMBOLS ] = {
56
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
57
  2, 3, 5, 8 };
58
59
const int32_t libfmos_lzfse_l_value_base_table[ LIBFMOS_LZFSE_NUMBER_OF_L_VALUE_SYMBOLS ] = {
60
  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
61
        16, 20, 28, 60 };
62
63
const uint8_t libfmos_lzfse_m_value_bits_table[ LIBFMOS_LZFSE_NUMBER_OF_M_VALUE_SYMBOLS ] = {
64
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
65
        3, 5, 8, 11 };
66
67
const int32_t libfmos_lzfse_m_value_base_table[ LIBFMOS_LZFSE_NUMBER_OF_M_VALUE_SYMBOLS ] = {
68
  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
69
        16, 24, 56, 312 };
70
71
#if defined( _MSC_VER )
72
#define libfmos_lzfse_count_leading_zeros( value ) \
73
  __lzcnt( (unsigned int) value )
74
75
#else
76
#define libfmos_lzfse_count_leading_zeros( value ) \
77
166k
  __builtin_clz( (unsigned int) value )
78
#endif
79
80
/* Builds a decoder table
81
 * Returns 1 on success or -1 on error
82
 */
83
int libfmos_lzfse_build_decoder_table(
84
     int number_of_states,
85
     uint16_t number_of_symbols,
86
     const uint16_t *frequency_table,
87
     libfmos_lzfse_decoder_entry_t *decoder_table,
88
     libcerror_error_t **error )
89
13.1k
{
90
13.1k
  libfmos_lzfse_decoder_entry_t *decoder_entry = NULL;
91
13.1k
  static char *function                        = "libfmos_lzfse_build_decoder_table";
92
13.1k
  uint16_t symbol                              = 0;
93
13.1k
  int16_t delta                                = 0;
94
13.1k
  int base_decoder_weight                      = 0;
95
13.1k
  int decoder_weight                           = 0;
96
13.1k
  int decoder_table_index                      = 0;
97
13.1k
  int frequency                                = 0;
98
13.1k
  int number_of_bits                           = 0;
99
13.1k
  int number_of_leading_zeros                  = 0;
100
13.1k
  int sum_of_frequencies                       = 0;
101
102
13.1k
  if( number_of_symbols > 256 )
103
0
  {
104
0
    libcerror_error_set(
105
0
     error,
106
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
107
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
108
0
     "%s: invalid number of symbols value out of bounds.",
109
0
     function );
110
111
0
    return( -1 );
112
0
  }
113
13.1k
  if( frequency_table == NULL )
114
0
  {
115
0
    libcerror_error_set(
116
0
     error,
117
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
118
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
119
0
     "%s: invalid frequency table.",
120
0
     function );
121
122
0
    return( -1 );
123
0
  }
124
13.1k
  if( decoder_table == NULL )
125
0
  {
126
0
    libcerror_error_set(
127
0
     error,
128
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
129
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
130
0
     "%s: invalid decoder table.",
131
0
     function );
132
133
0
    return( -1 );
134
0
  }
135
13.1k
  number_of_leading_zeros = (int) libfmos_lzfse_count_leading_zeros( number_of_states );
136
137
13.1k
  for( symbol = 0;
138
3.35M
       symbol < number_of_symbols;
139
3.34M
       symbol++ )
140
3.34M
  {
141
3.34M
    frequency = frequency_table[ symbol ];
142
143
    /* 0 occurrences of the symbol
144
     */
145
3.34M
    if( frequency == 0 )
146
3.33M
    {
147
3.33M
      continue;
148
3.33M
    }
149
11.5k
    sum_of_frequencies += frequency;
150
151
11.5k
    if( sum_of_frequencies > number_of_states )
152
57
    {
153
57
      libcerror_error_set(
154
57
       error,
155
57
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
156
57
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
157
57
       "%s: invalid sum of frequencies value out of bounds.",
158
57
       function );
159
160
57
      return( -1 );
161
57
    }
162
11.4k
    number_of_bits = (int) libfmos_lzfse_count_leading_zeros( frequency ) - number_of_leading_zeros;
163
164
11.4k
    base_decoder_weight = ( ( 2 * number_of_states ) >> number_of_bits ) - frequency;
165
166
11.4k
    for( decoder_weight = 0;
167
1.00M
         decoder_weight < frequency;
168
993k
         decoder_weight++ )
169
993k
    {
170
993k
      decoder_entry = &( decoder_table[ decoder_table_index++ ] );
171
172
993k
      decoder_entry->number_of_bits = (int8_t) number_of_bits;
173
993k
      decoder_entry->symbol         = (uint8_t) symbol;
174
175
993k
      if( decoder_weight < base_decoder_weight )
176
823k
      {
177
823k
        delta = (int16_t) ( ( ( frequency + decoder_weight ) << number_of_bits ) - number_of_states );
178
823k
      }
179
170k
      else
180
170k
      {
181
170k
        decoder_entry->number_of_bits -= 1;
182
183
170k
        delta = (int16_t) ( ( decoder_weight - base_decoder_weight ) << ( number_of_bits - 1 ) );
184
170k
      }
185
993k
      decoder_entry->delta = delta;
186
993k
    }
187
11.4k
  }
188
13.0k
  return( 1 );
189
13.1k
}
190
191
/* Builds a value decoder table
192
 * Returns 1 on success or -1 on error
193
 */
194
int libfmos_lzfse_build_value_decoder_table(
195
     int number_of_states,
196
     uint16_t number_of_symbols,
197
     const uint16_t *frequency_table,
198
     const uint8_t *value_bits_table,
199
     const int32_t *value_base_table,
200
     libfmos_lzfse_value_decoder_entry_t *value_decoder_table,
201
     libcerror_error_t **error )
202
39.0k
{
203
39.0k
  libfmos_lzfse_value_decoder_entry_t *value_decoder_entry = NULL;
204
39.0k
  static char *function                                    = "libfmos_lzfse_build_value_decoder_table";
205
39.0k
  int32_t value_base                                       = 0;
206
39.0k
  uint16_t symbol                                          = 0;
207
39.0k
  int16_t delta                                            = 0;
208
39.0k
  uint8_t value_bits                                       = 0;
209
39.0k
  int base_decoder_weight                                  = 0;
210
39.0k
  int decoder_weight                                       = 0;
211
39.0k
  int decoder_table_index                                  = 0;
212
39.0k
  int frequency                                            = 0;
213
39.0k
  int number_of_bits                                       = 0;
214
39.0k
  int number_of_leading_zeros                              = 0;
215
39.0k
  int sum_of_frequencies                                   = 0;
216
217
39.0k
  if( number_of_symbols > 256 )
218
0
  {
219
0
    libcerror_error_set(
220
0
     error,
221
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
222
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
223
0
     "%s: invalid number of symbols value out of bounds.",
224
0
     function );
225
226
0
    return( -1 );
227
0
  }
228
39.0k
  if( frequency_table == NULL )
229
0
  {
230
0
    libcerror_error_set(
231
0
     error,
232
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
233
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
234
0
     "%s: invalid frequency table.",
235
0
     function );
236
237
0
    return( -1 );
238
0
  }
239
39.0k
  if( value_bits_table == NULL )
240
0
  {
241
0
    libcerror_error_set(
242
0
     error,
243
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
244
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
245
0
     "%s: invalid value bits table.",
246
0
     function );
247
248
0
    return( -1 );
249
0
  }
250
39.0k
  if( value_base_table == NULL )
251
0
  {
252
0
    libcerror_error_set(
253
0
     error,
254
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
255
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
256
0
     "%s: invalid value base table.",
257
0
     function );
258
259
0
    return( -1 );
260
0
  }
261
39.0k
  if( value_decoder_table == NULL )
262
0
  {
263
0
    libcerror_error_set(
264
0
     error,
265
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
266
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
267
0
     "%s: invalid value decoder table.",
268
0
     function );
269
270
0
    return( -1 );
271
0
  }
272
39.0k
  number_of_leading_zeros = (int) libfmos_lzfse_count_leading_zeros( number_of_states );
273
274
39.0k
  for( symbol = 0;
275
1.39M
       symbol < number_of_symbols;
276
1.35M
       symbol++ )
277
1.35M
  {
278
1.35M
    frequency = frequency_table[ symbol ];
279
280
    /* 0 occurrences of the symbol
281
     */
282
1.35M
    if( frequency == 0 )
283
1.24M
    {
284
1.24M
      continue;
285
1.24M
    }
286
102k
    sum_of_frequencies += frequency;
287
288
102k
    if( sum_of_frequencies > number_of_states )
289
74
    {
290
74
      libcerror_error_set(
291
74
       error,
292
74
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
293
74
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
294
74
       "%s: invalid sum of frequencies value out of bounds.",
295
74
       function );
296
297
74
      return( -1 );
298
74
    }
299
102k
    number_of_bits = (int) libfmos_lzfse_count_leading_zeros( frequency ) - number_of_leading_zeros;
300
301
102k
    base_decoder_weight = ( ( 2 * number_of_states ) >> number_of_bits ) - frequency;
302
303
102k
    value_bits = value_bits_table[ symbol ];
304
102k
    value_base = value_base_table[ symbol ];
305
306
102k
    for( decoder_weight = 0;
307
706k
         decoder_weight < frequency;
308
603k
         decoder_weight++ )
309
603k
    {
310
603k
      value_decoder_entry = &( value_decoder_table[ decoder_table_index++ ] );
311
312
603k
      value_decoder_entry->value_bits     = value_bits;
313
603k
      value_decoder_entry->value_base     = value_base;
314
603k
      value_decoder_entry->value_bitmask  = ( (uint32_t) 1UL << value_bits ) - 1;
315
603k
      value_decoder_entry->number_of_bits = number_of_bits + value_bits;
316
317
603k
      if( decoder_weight < base_decoder_weight )
318
336k
      {
319
336k
        delta = (int16_t) ( ( ( frequency + decoder_weight ) << number_of_bits ) - number_of_states );
320
336k
      }
321
267k
      else
322
267k
      {
323
267k
        value_decoder_entry->number_of_bits -= 1;
324
325
267k
        delta = (int16_t) ( ( decoder_weight - base_decoder_weight ) << ( number_of_bits - 1 ) );
326
267k
      }
327
603k
      value_decoder_entry->delta = delta;
328
603k
    }
329
102k
  }
330
38.9k
  return( 1 );
331
39.0k
}
332
333
/* Reads a LZFSE compressed block header with uncompressed tables (version 1)
334
 * Returns 1 on success or -1 on error
335
 */
336
int libfmos_lzfse_read_block_v1_header(
337
     libfmos_lzfse_decoder_t *decoder,
338
     const uint8_t *compressed_data,
339
     size_t compressed_data_size,
340
     size_t *compressed_data_offset,
341
     uint16_t *frequency_table,
342
     libcerror_error_t **error )
343
2.20k
{
344
2.20k
  static char *function              = "libfmos_lzfse_read_block_v1_header";
345
2.20k
  size_t safe_compressed_data_offset = 0;
346
2.20k
  uint32_t compressed_block_size     = 0;
347
2.20k
  uint32_t literal_bits              = 0;
348
2.20k
  uint32_t lmd_values_bits           = 0;
349
2.20k
  uint16_t table_index               = 0;
350
2.20k
  uint8_t literal_decoder_index      = 0;
351
352
2.20k
  if( decoder == NULL )
353
0
  {
354
0
    libcerror_error_set(
355
0
     error,
356
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
357
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
358
0
     "%s: invalid decoder.",
359
0
     function );
360
361
0
    return( -1 );
362
0
  }
363
2.20k
  if( compressed_data == NULL )
364
0
  {
365
0
    libcerror_error_set(
366
0
     error,
367
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
368
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
369
0
     "%s: invalid compressed data.",
370
0
     function );
371
372
0
    return( -1 );
373
0
  }
374
2.20k
  if( ( compressed_data_size < 762 )
375
2.20k
   || ( compressed_data_size > (size_t) SSIZE_MAX ) )
376
19
  {
377
19
    libcerror_error_set(
378
19
     error,
379
19
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
380
19
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
381
19
     "%s: invalid compressed data size value out of bounds.",
382
19
     function );
383
384
19
    return( -1 );
385
19
  }
386
2.18k
  if( compressed_data_offset == NULL )
387
0
  {
388
0
    libcerror_error_set(
389
0
     error,
390
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
391
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
392
0
     "%s: invalid compressed data offset.",
393
0
     function );
394
395
0
    return( -1 );
396
0
  }
397
2.18k
  safe_compressed_data_offset = *compressed_data_offset;
398
399
2.18k
  if( safe_compressed_data_offset > ( compressed_data_size - 762 ) )
400
24
  {
401
24
    libcerror_error_set(
402
24
     error,
403
24
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
404
24
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
405
24
     "%s: invalid compressed data offset value out of bounds.",
406
24
     function );
407
408
24
    return( -1 );
409
24
  }
410
2.16k
  if( frequency_table == NULL )
411
0
  {
412
0
    libcerror_error_set(
413
0
     error,
414
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
415
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
416
0
     "%s: invalid frequency table.",
417
0
     function );
418
419
0
    return( -1 );
420
0
  }
421
#if defined( HAVE_DEBUG_OUTPUT )
422
  if( libcnotify_verbose != 0 )
423
  {
424
    libcnotify_printf(
425
     "%s: v1 block header data:\n",
426
     function );
427
    libcnotify_print_data(
428
     &( compressed_data[ safe_compressed_data_offset ] ),
429
     762,
430
     0 );
431
  }
432
#endif
433
2.16k
  byte_stream_copy_to_uint32_little_endian(
434
2.16k
   &( compressed_data[ safe_compressed_data_offset ] ),
435
2.16k
   compressed_block_size );
436
437
2.16k
  safe_compressed_data_offset += 4;
438
439
2.16k
  byte_stream_copy_to_uint32_little_endian(
440
2.16k
   &( compressed_data[ safe_compressed_data_offset ] ),
441
2.16k
   decoder->number_of_literals );
442
443
2.16k
  safe_compressed_data_offset += 4;
444
445
2.16k
  byte_stream_copy_to_uint32_little_endian(
446
2.16k
   &( compressed_data[ safe_compressed_data_offset ] ),
447
2.16k
   decoder->number_of_lmd_values );
448
449
2.16k
  safe_compressed_data_offset += 4;
450
451
2.16k
  byte_stream_copy_to_uint32_little_endian(
452
2.16k
   &( compressed_data[ safe_compressed_data_offset ] ),
453
2.16k
   decoder->literals_data_size );
454
455
2.16k
  safe_compressed_data_offset += 4;
456
457
2.16k
  byte_stream_copy_to_uint32_little_endian(
458
2.16k
   &( compressed_data[ safe_compressed_data_offset ] ),
459
2.16k
   decoder->lmd_values_data_size );
460
461
2.16k
  safe_compressed_data_offset += 4;
462
463
2.16k
  byte_stream_copy_to_uint32_little_endian(
464
2.16k
   &( compressed_data[ safe_compressed_data_offset ] ),
465
2.16k
   literal_bits );
466
467
2.16k
  safe_compressed_data_offset += 4;
468
469
2.16k
  for( literal_decoder_index = 0;
470
10.8k
       literal_decoder_index < 4;
471
8.64k
       literal_decoder_index++ )
472
8.64k
  {
473
8.64k
    byte_stream_copy_to_uint16_little_endian(
474
8.64k
     &( compressed_data[ safe_compressed_data_offset ] ),
475
8.64k
     decoder->literal_states[ literal_decoder_index ] );
476
477
8.64k
    safe_compressed_data_offset += 2;
478
8.64k
  }
479
2.16k
  byte_stream_copy_to_uint32_little_endian(
480
2.16k
   &( compressed_data[ safe_compressed_data_offset ] ),
481
2.16k
   lmd_values_bits );
482
483
2.16k
  safe_compressed_data_offset += 4;
484
485
2.16k
  byte_stream_copy_to_uint16_little_endian(
486
2.16k
   &( compressed_data[ safe_compressed_data_offset ] ),
487
2.16k
   decoder->l_value_state );
488
489
2.16k
  safe_compressed_data_offset += 2;
490
491
2.16k
  byte_stream_copy_to_uint16_little_endian(
492
2.16k
   &( compressed_data[ safe_compressed_data_offset ] ),
493
2.16k
   decoder->m_value_state );
494
495
2.16k
  safe_compressed_data_offset += 2;
496
497
2.16k
  byte_stream_copy_to_uint16_little_endian(
498
2.16k
   &( compressed_data[ safe_compressed_data_offset ] ),
499
2.16k
   decoder->d_value_state );
500
501
2.16k
  safe_compressed_data_offset += 2;
502
503
2.16k
  for( table_index = 0;
504
779k
       table_index < 360;
505
777k
       table_index++ )
506
777k
  {
507
777k
    byte_stream_copy_to_uint16_little_endian(
508
777k
     &( compressed_data[ safe_compressed_data_offset ] ),
509
777k
     frequency_table[ table_index ] );
510
511
777k
    safe_compressed_data_offset += 2;
512
777k
  }
513
#if defined( HAVE_DEBUG_OUTPUT )
514
  if( libcnotify_verbose != 0 )
515
  {
516
    libcnotify_printf(
517
     "%s: compressed block size\t\t: %" PRIu32 "\n",
518
     function,
519
     compressed_block_size );
520
521
    libcnotify_printf(
522
     "%s: number of literals\t\t\t: %" PRIu32 "\n",
523
     function,
524
     decoder->number_of_literals );
525
526
    libcnotify_printf(
527
     "%s: number of L, M, D values\t\t: %" PRIu32 "\n",
528
     function,
529
     decoder->number_of_lmd_values );
530
531
    libcnotify_printf(
532
     "%s: literals data size\t\t\t: %" PRIu32 "\n",
533
     function,
534
     decoder->literals_data_size );
535
536
    libcnotify_printf(
537
     "%s: L, M, D values data size\t\t: %" PRIu32 "\n",
538
     function,
539
     decoder->lmd_values_data_size );
540
541
    libcnotify_printf(
542
     "%s: literal_bits\t\t\t: %" PRIi32 "\n",
543
     function,
544
     (int32_t) literal_bits );
545
546
    for( literal_decoder_index = 0;
547
         literal_decoder_index < 4;
548
         literal_decoder_index++ )
549
    {
550
      libcnotify_printf(
551
       "%s: literal_state[ %" PRIu8 " ]\t\t\t: %" PRIu16 "\n",
552
       function,
553
       literal_decoder_index,
554
       decoder->literal_states[ literal_decoder_index ] );
555
    }
556
    libcnotify_printf(
557
     "%s: lmd_values_bits\t\t\t: %" PRIi32 "\n",
558
     function,
559
     (int32_t) lmd_values_bits );
560
561
    libcnotify_printf(
562
     "%s: L value state\t\t\t: %" PRIu16 "\n",
563
     function,
564
     decoder->l_value_state );
565
566
    libcnotify_printf(
567
     "%s: M value state\t\t\t: %" PRIu16 "\n",
568
     function,
569
     decoder->m_value_state );
570
571
    libcnotify_printf(
572
     "%s: D value state\t\t\t: %" PRIu16 "\n",
573
     function,
574
     decoder->d_value_state );
575
576
    for( table_index = 0;
577
         table_index < 360;
578
         table_index++ )
579
    {
580
      if( frequency_table[ table_index ] != 0 )
581
      {
582
        libcnotify_printf(
583
         "%s: frequency table: %d value\t\t: %" PRIu16 "\n",
584
         function,
585
         table_index,
586
         frequency_table[ table_index ] );
587
      }
588
    }
589
    libcnotify_printf(
590
     "\n" );
591
  }
592
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
593
594
2.16k
  decoder->literal_bits    = (int32_t) literal_bits;
595
2.16k
  decoder->lmd_values_bits = (int32_t) lmd_values_bits;
596
597
2.16k
  *compressed_data_offset = safe_compressed_data_offset;
598
599
2.16k
  return( 1 );
600
2.16k
}
601
602
/* Reads a LZFSE compressed block header with compressed tables (version 2)
603
 * Returns 1 on success or -1 on error
604
 */
605
int libfmos_lzfse_read_block_v2_header(
606
     libfmos_lzfse_decoder_t *decoder,
607
     const uint8_t *compressed_data,
608
     size_t compressed_data_size,
609
     size_t *compressed_data_offset,
610
     uint16_t *frequency_table,
611
     libcerror_error_t **error )
612
11.0k
{
613
11.0k
  static char *function              = "libfmos_lzfse_read_block_v2_header";
614
11.0k
  size_t safe_compressed_data_offset = 0;
615
11.0k
  uint64_t packed_fields1            = 0;
616
11.0k
  uint64_t packed_fields2            = 0;
617
11.0k
  uint64_t packed_fields3            = 0;
618
11.0k
  uint32_t header_size               = 0;
619
620
#if defined( HAVE_DEBUG_OUTPUT )
621
  uint16_t table_index               = 0;
622
  uint8_t literal_decoder_index      = 0;
623
#endif
624
625
11.0k
  if( decoder == NULL )
626
0
  {
627
0
    libcerror_error_set(
628
0
     error,
629
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
630
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
631
0
     "%s: invalid decoder.",
632
0
     function );
633
634
0
    return( -1 );
635
0
  }
636
11.0k
  if( compressed_data == NULL )
637
0
  {
638
0
    libcerror_error_set(
639
0
     error,
640
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
641
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
642
0
     "%s: invalid compressed data.",
643
0
     function );
644
645
0
    return( -1 );
646
0
  }
647
11.0k
  if( ( compressed_data_size < 24 )
648
11.0k
   || ( compressed_data_size > (size_t) SSIZE_MAX ) )
649
7
  {
650
7
    libcerror_error_set(
651
7
     error,
652
7
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
653
7
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
654
7
     "%s: invalid compressed data size value out of bounds.",
655
7
     function );
656
657
7
    return( -1 );
658
7
  }
659
11.0k
  if( compressed_data_offset == NULL )
660
0
  {
661
0
    libcerror_error_set(
662
0
     error,
663
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
664
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
665
0
     "%s: invalid compressed data offset.",
666
0
     function );
667
668
0
    return( -1 );
669
0
  }
670
11.0k
  safe_compressed_data_offset = *compressed_data_offset;
671
672
11.0k
  if( safe_compressed_data_offset > ( compressed_data_size - 24 ) )
673
23
  {
674
23
    libcerror_error_set(
675
23
     error,
676
23
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
677
23
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
678
23
     "%s: invalid compressed data offset value out of bounds.",
679
23
     function );
680
681
23
    return( -1 );
682
23
  }
683
11.0k
  if( frequency_table == NULL )
684
0
  {
685
0
    libcerror_error_set(
686
0
     error,
687
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
688
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
689
0
     "%s: invalid frequency table.",
690
0
     function );
691
692
0
    return( -1 );
693
0
  }
694
#if defined( HAVE_DEBUG_OUTPUT )
695
  if( libcnotify_verbose != 0 )
696
  {
697
    libcnotify_printf(
698
     "%s: v2 block header data:\n",
699
     function );
700
    libcnotify_print_data(
701
     &( compressed_data[ safe_compressed_data_offset ] ),
702
     24,
703
     0 );
704
  }
705
#endif
706
11.0k
  byte_stream_copy_to_uint64_little_endian(
707
11.0k
   &( compressed_data[ safe_compressed_data_offset ] ),
708
11.0k
   packed_fields1 );
709
710
11.0k
  safe_compressed_data_offset += 8;
711
712
11.0k
  byte_stream_copy_to_uint64_little_endian(
713
11.0k
   &( compressed_data[ safe_compressed_data_offset ] ),
714
11.0k
   packed_fields2 );
715
716
11.0k
  safe_compressed_data_offset += 8;
717
718
11.0k
  byte_stream_copy_to_uint64_little_endian(
719
11.0k
   &( compressed_data[ safe_compressed_data_offset ] ),
720
11.0k
   packed_fields3 );
721
722
11.0k
  safe_compressed_data_offset += 8;
723
724
#if defined( HAVE_DEBUG_OUTPUT )
725
  if( libcnotify_verbose != 0 )
726
  {
727
    libcnotify_printf(
728
     "%s: packed fields 1\t\t\t: 0x%08" PRIx64 "\n",
729
     function,
730
     packed_fields1 );
731
732
    libcnotify_printf(
733
     "%s: packed fields 2\t\t\t: 0x%08" PRIx64 "\n",
734
     function,
735
     packed_fields2 );
736
737
    libcnotify_printf(
738
     "%s: packed fields 3\t\t\t: 0x%08" PRIx64 "\n",
739
     function,
740
     packed_fields3 );
741
  }
742
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
743
744
11.0k
  decoder->number_of_literals   = (uint32_t) ( packed_fields1 & 0x000fffffUL );
745
11.0k
  decoder->literals_data_size   = (uint32_t) ( ( packed_fields1 >> 20 ) & 0x000fffffUL );
746
11.0k
  decoder->number_of_lmd_values = (uint32_t) ( ( packed_fields1 >> 40 ) & 0x000fffffUL );
747
11.0k
  decoder->literal_bits         = (int32_t) ( ( packed_fields1 >> 60 ) & 0x00000007UL ) - 7;
748
749
11.0k
  decoder->literal_states[ 0 ]  = (uint16_t) ( packed_fields2 & 0x000003ffUL );
750
11.0k
  decoder->literal_states[ 1 ]  = (uint16_t) ( ( packed_fields2 >> 10 ) & 0x000003ffUL );
751
11.0k
  decoder->literal_states[ 2 ]  = (uint16_t) ( ( packed_fields2 >> 20 ) & 0x000003ffUL );
752
11.0k
  decoder->literal_states[ 3 ]  = (uint16_t) ( ( packed_fields2 >> 30 ) & 0x000003ffUL );
753
11.0k
  decoder->lmd_values_data_size = (uint32_t) ( ( packed_fields2 >> 40 ) & 0x000fffffUL );
754
11.0k
  decoder->lmd_values_bits      = (int32_t) ( ( packed_fields2 >> 60 ) & 0x00000007UL ) - 7;
755
756
11.0k
  header_size                   = (uint32_t) ( packed_fields3 & 0xffffffffUL );
757
11.0k
  decoder->l_value_state        = (uint16_t) ( ( packed_fields3 >> 32 ) & 0x000003ffUL );
758
11.0k
  decoder->m_value_state        = (uint16_t) ( ( packed_fields3 >> 42 ) & 0x000003ffUL );
759
11.0k
  decoder->d_value_state        = (uint16_t) ( ( packed_fields3 >> 52 ) & 0x000003ffUL );
760
761
11.0k
  if( ( header_size < 32 )
762
11.0k
   || ( header_size > 720 ) )
763
58
  {
764
58
    libcerror_error_set(
765
58
     error,
766
58
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
767
58
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
768
58
     "%s: invalid header size value out of bounds.",
769
58
     function );
770
771
58
    return( -1 );
772
58
  }
773
11.0k
  if( header_size > 32 )
774
8.92k
  {
775
#if defined( HAVE_DEBUG_OUTPUT )
776
    if( libcnotify_verbose != 0 )
777
    {
778
      libcnotify_printf(
779
       "%s: compressed frequency table data:\n",
780
       function );
781
      libcnotify_print_data(
782
       &( compressed_data[ safe_compressed_data_offset ] ),
783
       header_size - 32,
784
       0 );
785
    }
786
#endif
787
8.92k
    if( ( header_size > compressed_data_size )
788
8.92k
     || ( safe_compressed_data_offset > ( compressed_data_size - header_size ) ) )
789
39
    {
790
39
      libcerror_error_set(
791
39
       error,
792
39
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
793
39
       LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
794
39
       "%s: compressed data size value too small.",
795
39
       function );
796
797
39
      return( -1 );
798
39
    }
799
8.88k
    if( libfmos_lzfse_read_compressed_frequency_table(
800
8.88k
         &( compressed_data[ safe_compressed_data_offset ] ),
801
8.88k
         header_size - 32,
802
8.88k
         frequency_table,
803
8.88k
         error ) != 1 )
804
5
    {
805
5
      libcerror_error_set(
806
5
       error,
807
5
       LIBCERROR_ERROR_DOMAIN_IO,
808
5
       LIBCERROR_IO_ERROR_READ_FAILED,
809
5
       "%s: unable to read compressed frequency table.",
810
5
       function );
811
812
5
      return( -1 );
813
5
    }
814
8.88k
    safe_compressed_data_offset += (size_t) header_size - 32;
815
8.88k
  }
816
#if defined( HAVE_DEBUG_OUTPUT )
817
  if( libcnotify_verbose != 0 )
818
  {
819
    libcnotify_printf(
820
     "%s: number of literals\t\t\t: %" PRIu32 "\n",
821
     function,
822
     decoder->number_of_literals );
823
824
    libcnotify_printf(
825
     "%s: number of L, M, D values\t\t: %" PRIu32 "\n",
826
     function,
827
     decoder->number_of_lmd_values );
828
829
    libcnotify_printf(
830
     "%s: literals data size\t\t\t: %" PRIu32 "\n",
831
     function,
832
     decoder->literals_data_size );
833
834
    libcnotify_printf(
835
     "%s: L, M, D values data size\t\t: %" PRIu32 "\n",
836
     function,
837
     decoder->lmd_values_data_size );
838
839
    libcnotify_printf(
840
     "%s: literal_bits\t\t\t: %" PRIi32 "\n",
841
     function,
842
     decoder->literal_bits );
843
844
    for( literal_decoder_index = 0;
845
         literal_decoder_index < 4;
846
         literal_decoder_index++ )
847
    {
848
      libcnotify_printf(
849
       "%s: literal_states[ %" PRIu8 " ]\t\t\t: %" PRIu16 "\n",
850
       function,
851
       literal_decoder_index,
852
       decoder->literal_states[ literal_decoder_index ] );
853
    }
854
    libcnotify_printf(
855
     "%s: lmd_bits\t\t\t\t: %" PRIi32 "\n",
856
     function,
857
     decoder->lmd_values_bits );
858
859
    libcnotify_printf(
860
     "%s: header size\t\t\t\t: %" PRIu32 "\n",
861
     function,
862
     header_size );
863
864
    libcnotify_printf(
865
     "%s: L value state\t\t\t: %" PRIu16 "\n",
866
     function,
867
     decoder->l_value_state );
868
869
    libcnotify_printf(
870
     "%s: M value state\t\t\t: %" PRIu16 "\n",
871
     function,
872
     decoder->m_value_state );
873
874
    libcnotify_printf(
875
     "%s: D value state\t\t\t: %" PRIu16 "\n",
876
     function,
877
     decoder->d_value_state );
878
879
    for( table_index = 0;
880
         table_index < 360;
881
         table_index++ )
882
    {
883
      if( frequency_table[ table_index ] != 0 )
884
      {
885
        libcnotify_printf(
886
         "%s: frequency table: %d value\t\t: %" PRIu16 "\n",
887
         function,
888
         table_index,
889
         frequency_table[ table_index ] );
890
      }
891
    }
892
    libcnotify_printf(
893
     "\n" );
894
  }
895
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
896
897
10.9k
  *compressed_data_offset = safe_compressed_data_offset;
898
899
10.9k
  return( 1 );
900
11.0k
}
901
902
/* Reads a compressed frequency table bit stream
903
 * Returns 1 on success or -1 on error
904
 */
905
int libfmos_lzfse_read_compressed_frequency_table(
906
     const uint8_t *compressed_data,
907
     size_t compressed_data_size,
908
     uint16_t *frequency_table,
909
     libcerror_error_t **error )
910
8.88k
{
911
8.88k
  static char *function         = "libfmos_lzfse_read_compressed_frequency_table";
912
8.88k
  size_t compressed_data_offset = 0;
913
8.88k
  uint32_t value_32bit          = 0;
914
8.88k
  uint16_t frequency_value      = 0;
915
8.88k
  int16_t table_index           = 0;
916
8.88k
  uint8_t frequency_value_size  = 0;
917
8.88k
  uint8_t lookup_index          = 0;
918
8.88k
  uint8_t number_of_bits        = 0;
919
920
8.88k
  if( compressed_data == NULL )
921
0
  {
922
0
    libcerror_error_set(
923
0
     error,
924
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
925
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
926
0
     "%s: invalid compressed data.",
927
0
     function );
928
929
0
    return( -1 );
930
0
  }
931
8.88k
  if( ( compressed_data_size < 4 )
932
8.88k
   || ( compressed_data_size > (size_t) SSIZE_MAX ) )
933
5
  {
934
5
    libcerror_error_set(
935
5
     error,
936
5
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
937
5
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
938
5
     "%s: invalid compressed data size value out of bounds.",
939
5
     function );
940
941
5
    return( -1 );
942
5
  }
943
8.88k
  if( frequency_table == NULL )
944
0
  {
945
0
    libcerror_error_set(
946
0
     error,
947
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
948
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
949
0
     "%s: invalid frequency table.",
950
0
     function );
951
952
0
    return( -1 );
953
0
  }
954
8.88k
  for( table_index = 0;
955
3.20M
       table_index < 360;
956
3.19M
       table_index++ )
957
3.19M
  {
958
3.31M
    while( ( number_of_bits <= 24 )
959
3.31M
        && ( compressed_data_offset < compressed_data_size ) )
960
118k
    {
961
118k
      value_32bit    |= (uint32_t) compressed_data[ compressed_data_offset++ ] << number_of_bits;
962
118k
      number_of_bits += 8;
963
118k
    }
964
3.19M
    lookup_index         = (uint8_t) ( value_32bit & 0x0000001fUL );
965
3.19M
    frequency_value_size = libfmos_lzfse_frequency_number_of_bits_table[ lookup_index ];
966
967
3.19M
    if( frequency_value_size == 8 )
968
9.24k
    {
969
9.24k
      frequency_value = (uint16_t) ( ( value_32bit >> 4 ) & 0x0000000fUL ) + 8;
970
9.24k
    }
971
3.18M
    else if( frequency_value_size == 14 )
972
7.82k
    {
973
7.82k
      frequency_value = (uint16_t) ( ( value_32bit >> 4 ) & 0x000003ffUL ) + 24;
974
7.82k
    }
975
3.18M
    else
976
3.18M
    {
977
3.18M
      frequency_value = libfmos_lzfse_frequency_value_table[ lookup_index ];
978
3.18M
    }
979
3.19M
    frequency_table[ table_index ] = frequency_value;
980
981
3.19M
    value_32bit   >>= frequency_value_size;
982
3.19M
    number_of_bits -= frequency_value_size;
983
3.19M
  }
984
8.88k
  return( 1 );
985
8.88k
}
986
987
/* Reads a LZFSE compressed block
988
 * Returns 1 on success or -1 on error
989
 */
990
int libfmos_lzfse_read_block(
991
     libfmos_lzfse_decoder_t *decoder,
992
     const uint8_t *compressed_data,
993
     size_t compressed_data_size,
994
     size_t *compressed_data_offset,
995
     uint8_t *uncompressed_data,
996
     size_t uncompressed_data_size,
997
     size_t *uncompressed_data_offset,
998
     libcerror_error_t **error )
999
12.9k
{
1000
12.9k
  uint8_t literal_values[ LIBFMOS_LZFSE_LITERALS_PER_BLOCK + 64 ];
1001
1002
12.9k
  libfmos_lzfse_bit_stream_t *bit_stream = NULL;
1003
12.9k
  static char *function                   = "libfmos_lzfse_read_block";
1004
12.9k
  size_t safe_compressed_data_offset      = 0;
1005
1006
12.9k
  if( decoder == NULL )
1007
0
  {
1008
0
    libcerror_error_set(
1009
0
     error,
1010
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1011
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1012
0
     "%s: invalid decoder.",
1013
0
     function );
1014
1015
0
    return( -1 );
1016
0
  }
1017
12.9k
  if( compressed_data == NULL )
1018
0
  {
1019
0
    libcerror_error_set(
1020
0
     error,
1021
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1022
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1023
0
     "%s: invalid compressed data.",
1024
0
     function );
1025
1026
0
    return( -1 );
1027
0
  }
1028
12.9k
  if( compressed_data_size > (size_t) SSIZE_MAX )
1029
0
  {
1030
0
    libcerror_error_set(
1031
0
     error,
1032
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1033
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1034
0
     "%s: invalid compressed data size value exceeds maximum.",
1035
0
     function );
1036
1037
0
    return( -1 );
1038
0
  }
1039
12.9k
  if( compressed_data_offset == NULL )
1040
0
  {
1041
0
    libcerror_error_set(
1042
0
     error,
1043
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1044
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1045
0
     "%s: invalid compressed offset.",
1046
0
     function );
1047
1048
0
    return( -1 );
1049
0
  }
1050
12.9k
  safe_compressed_data_offset = *compressed_data_offset;
1051
1052
12.9k
  if( ( decoder->literals_data_size > compressed_data_size )
1053
12.9k
   || ( safe_compressed_data_offset > ( compressed_data_size - decoder->literals_data_size ) ) )
1054
104
  {
1055
104
    libcerror_error_set(
1056
104
     error,
1057
104
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1058
104
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1059
104
     "%s: invalid compressed data size value out of bounds.",
1060
104
     function );
1061
1062
104
    return( -1 );
1063
104
  }
1064
12.8k
  if( libfmos_lzfse_bit_stream_initialize(
1065
12.8k
       &bit_stream,
1066
12.8k
       &( compressed_data[ safe_compressed_data_offset ] ),
1067
12.8k
       decoder->literals_data_size,
1068
12.8k
       error ) != 1 )
1069
0
  {
1070
0
    libcerror_error_set(
1071
0
     error,
1072
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1073
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1074
0
     "%s: unable to create literals bit stream.",
1075
0
     function );
1076
1077
0
    goto on_error;
1078
0
  }
1079
12.8k
  if( libfmos_lzfse_read_literal_values(
1080
12.8k
       decoder,
1081
12.8k
       bit_stream,
1082
12.8k
       literal_values,
1083
12.8k
       error ) != 1 )
1084
131
  {
1085
131
    libcerror_error_set(
1086
131
     error,
1087
131
     LIBCERROR_ERROR_DOMAIN_IO,
1088
131
     LIBCERROR_IO_ERROR_READ_FAILED,
1089
131
     "%s: unable to read literal values.",
1090
131
     function );
1091
1092
131
    goto on_error;
1093
131
  }
1094
12.7k
  if( libfmos_lzfse_bit_stream_free(
1095
12.7k
       &bit_stream,
1096
12.7k
       error ) != 1 )
1097
0
  {
1098
0
    libcerror_error_set(
1099
0
     error,
1100
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1101
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1102
0
     "%s: unable to free literals bit stream.",
1103
0
     function );
1104
1105
0
    goto on_error;
1106
0
  }
1107
12.7k
  safe_compressed_data_offset += decoder->literals_data_size;
1108
1109
12.7k
  if( ( decoder->lmd_values_data_size > compressed_data_size )
1110
12.7k
   || ( safe_compressed_data_offset > ( compressed_data_size - decoder->lmd_values_data_size ) ) )
1111
93
  {
1112
93
    libcerror_error_set(
1113
93
     error,
1114
93
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1115
93
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1116
93
     "%s: invalid compressed data size value out of bounds.",
1117
93
     function );
1118
1119
93
    return( -1 );
1120
93
  }
1121
12.6k
  if( libfmos_lzfse_bit_stream_initialize(
1122
12.6k
       &bit_stream,
1123
12.6k
       &( compressed_data[ safe_compressed_data_offset ] ),
1124
12.6k
       decoder->lmd_values_data_size,
1125
12.6k
       error ) != 1 )
1126
0
  {
1127
0
    libcerror_error_set(
1128
0
     error,
1129
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1130
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1131
0
     "%s: unable to create L, M, D values bit stream.",
1132
0
     function );
1133
1134
0
    goto on_error;
1135
0
  }
1136
12.6k
  if( libfmos_lzfse_read_lmd_values(
1137
12.6k
       decoder,
1138
12.6k
       bit_stream,
1139
12.6k
       literal_values,
1140
12.6k
       uncompressed_data,
1141
12.6k
       uncompressed_data_size,
1142
12.6k
       uncompressed_data_offset,
1143
12.6k
       error ) != 1 )
1144
343
  {
1145
343
    libcerror_error_set(
1146
343
     error,
1147
343
     LIBCERROR_ERROR_DOMAIN_IO,
1148
343
     LIBCERROR_IO_ERROR_READ_FAILED,
1149
343
     "%s: unable to read L, M, D values.",
1150
343
     function );
1151
1152
343
    goto on_error;
1153
343
  }
1154
12.3k
  if( libfmos_lzfse_bit_stream_free(
1155
12.3k
       &bit_stream,
1156
12.3k
       error ) != 1 )
1157
0
  {
1158
0
    libcerror_error_set(
1159
0
     error,
1160
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1161
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1162
0
     "%s: unable to free L, M, D values bit stream.",
1163
0
     function );
1164
1165
0
    goto on_error;
1166
0
  }
1167
12.3k
  safe_compressed_data_offset += decoder->lmd_values_data_size;
1168
1169
12.3k
  *compressed_data_offset = safe_compressed_data_offset;
1170
1171
12.3k
  return( 1 );
1172
1173
474
on_error:
1174
474
  if( bit_stream != NULL )
1175
474
  {
1176
474
    libfmos_lzfse_bit_stream_free(
1177
474
     &bit_stream,
1178
474
     NULL );
1179
474
  }
1180
474
  return( -1 );
1181
12.3k
}
1182
1183
/* Reads literal values
1184
 * Returns 1 on success or -1 on error
1185
 */
1186
int libfmos_lzfse_read_literal_values(
1187
     libfmos_lzfse_decoder_t *decoder,
1188
     libfmos_lzfse_bit_stream_t *bit_stream,
1189
     uint8_t *literal_values,
1190
     libcerror_error_t **error )
1191
12.8k
{
1192
12.8k
  uint16_t literal_states[ 4 ];
1193
1194
12.8k
  libfmos_lzfse_decoder_entry_t *decoder_entry = NULL;
1195
12.8k
  static char *function                        = "libfmos_lzfse_read_literal_values";
1196
12.8k
  uint32_t value_32bit                         = 0;
1197
12.8k
  uint32_t literal_value_index                 = 0;
1198
12.8k
  int32_t literal_state                        = 0;
1199
12.8k
  uint8_t literal_decoder_index                = 0;
1200
1201
12.8k
  if( decoder == NULL )
1202
0
  {
1203
0
    libcerror_error_set(
1204
0
     error,
1205
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1206
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1207
0
     "%s: invalid decoder.",
1208
0
     function );
1209
1210
0
    return( -1 );
1211
0
  }
1212
12.8k
  if( decoder->number_of_literals > (uint32_t) ( LIBFMOS_LZFSE_LITERALS_PER_BLOCK + 64 ) )
1213
35
  {
1214
35
    libcerror_error_set(
1215
35
     error,
1216
35
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1217
35
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1218
35
     "%s: invalid decoder - number of literals value out of bounds.",
1219
35
     function );
1220
1221
35
    return( -1 );
1222
35
  }
1223
12.8k
  if( literal_values == NULL )
1224
0
  {
1225
0
    libcerror_error_set(
1226
0
     error,
1227
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1228
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1229
0
     "%s: invalid literal values.",
1230
0
     function );
1231
1232
0
    return( -1 );
1233
0
  }
1234
12.8k
  literal_states[ 0 ] = decoder->literal_states[ 0 ];
1235
12.8k
  literal_states[ 1 ] = decoder->literal_states[ 1 ];
1236
12.8k
  literal_states[ 2 ] = decoder->literal_states[ 2 ];
1237
12.8k
  literal_states[ 3 ] = decoder->literal_states[ 3 ];
1238
1239
12.8k
  if( ( decoder->literal_bits < (int32_t) -32 )
1240
12.8k
   || ( decoder->literal_bits > 0 ) )
1241
64
  {
1242
64
    libcerror_error_set(
1243
64
     error,
1244
64
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1245
64
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1246
64
     "%s: invalid decoder - literal bits value out of bounds.",
1247
64
     function );
1248
1249
64
    return( -1 );
1250
64
  }
1251
12.7k
  if( libfmos_lzfse_bit_stream_get_value(
1252
12.7k
       bit_stream,
1253
12.7k
       -1 * decoder->literal_bits,
1254
12.7k
       &value_32bit,
1255
12.7k
       error ) != 1 )
1256
15
  {
1257
15
    libcerror_error_set(
1258
15
     error,
1259
15
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1260
15
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1261
15
     "%s: unable to retrieve value from bit stream.",
1262
15
     function );
1263
1264
15
    return( -1 );
1265
15
  }
1266
12.7k
  for( literal_value_index = 0;
1267
2.94M
       literal_value_index < decoder->number_of_literals;
1268
2.93M
       literal_value_index += 4 )
1269
2.93M
  {
1270
2.93M
    for( literal_decoder_index = 0;
1271
14.6M
         literal_decoder_index < 4;
1272
11.7M
         literal_decoder_index++ )
1273
11.7M
    {
1274
11.7M
      literal_state = literal_states[ literal_decoder_index ];
1275
1276
11.7M
      if( ( literal_state < 0 )
1277
11.7M
       || ( literal_state >= LIBFMOS_LZFSE_NUMBER_OF_LITERAL_STATES ) )
1278
7
      {
1279
7
        libcerror_error_set(
1280
7
         error,
1281
7
         LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1282
7
         LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1283
7
         "%s: invalid literal state value out of bounds.",
1284
7
         function );
1285
1286
7
        return( -1 );
1287
7
      }
1288
11.7M
      decoder_entry = &( decoder->literal_decoder_table[ literal_state ] );
1289
1290
11.7M
      if( libfmos_lzfse_bit_stream_get_value(
1291
11.7M
           bit_stream,
1292
11.7M
           decoder_entry->number_of_bits,
1293
11.7M
           &value_32bit,
1294
11.7M
           error ) != 1 )
1295
10
      {
1296
10
        libcerror_error_set(
1297
10
         error,
1298
10
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1299
10
         LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1300
10
         "%s: unable to retrieve value from bit stream.",
1301
10
         function );
1302
1303
10
        return( -1 );
1304
10
      }
1305
11.7M
      literal_state = (int32_t) decoder_entry->delta + (int32_t) value_32bit;
1306
1307
11.7M
      literal_values[ literal_value_index + literal_decoder_index ] = decoder_entry->symbol;
1308
1309
11.7M
      literal_states[ literal_decoder_index ] = (uint16_t) literal_state;
1310
1311
#if defined( HAVE_DEBUG_OUTPUT )
1312
      if( libcnotify_verbose != 0 )
1313
      {
1314
        libcnotify_printf(
1315
         "%s: value\t\t\t\t: 0x%" PRIx32 " (%" PRIu8 ")\n",
1316
         function,
1317
         value_32bit,
1318
         decoder_entry->number_of_bits );
1319
1320
        libcnotify_printf(
1321
         "%s: literal values[ %" PRIu32 " ]\t\t\t: 0x%02" PRIx8 "\n",
1322
         function,
1323
         literal_value_index + literal_decoder_index,
1324
         decoder_entry->symbol );
1325
1326
        libcnotify_printf(
1327
         "%s: literal states[ %" PRIu8 " ]\t\t\t: %" PRIi32 "\n",
1328
         function,
1329
         literal_decoder_index,
1330
         literal_state );
1331
      }
1332
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
1333
11.7M
    }
1334
2.93M
  }
1335
#if defined( HAVE_DEBUG_OUTPUT )
1336
  if( libcnotify_verbose != 0 )
1337
  {
1338
    libcnotify_printf(
1339
     "\n" );
1340
  }
1341
#endif
1342
12.7k
  return( 1 );
1343
12.7k
}
1344
1345
/* Reads L, M, D values
1346
 * Returns 1 on success or -1 on error
1347
 */
1348
int libfmos_lzfse_read_lmd_values(
1349
     libfmos_lzfse_decoder_t *decoder,
1350
     libfmos_lzfse_bit_stream_t *bit_stream,
1351
     uint8_t *literal_values,
1352
     uint8_t *uncompressed_data,
1353
     size_t uncompressed_data_size,
1354
     size_t *uncompressed_data_offset,
1355
     libcerror_error_t **error )
1356
12.6k
{
1357
12.6k
  libfmos_lzfse_value_decoder_entry_t *value_decoder_entry = NULL;
1358
12.6k
  static char *function                                    = "libfmos_lzfse_read_lmd_values";
1359
12.6k
  size_t safe_uncompressed_data_offset                     = 0;
1360
12.6k
  size_t remaining_uncompressed_data_size                  = 0;
1361
12.6k
  uint32_t lmd_value_index                                 = 0;
1362
12.6k
  uint32_t value_32bit                                     = 0;
1363
12.6k
  int32_t d_value                                          = -1;
1364
12.6k
  int32_t d_value_state                                    = 0;
1365
12.6k
  int32_t l_value                                          = 0;
1366
12.6k
  int32_t l_value_index                                    = 0;
1367
12.6k
  int32_t l_value_state                                    = 0;
1368
12.6k
  int32_t literal_value_index                              = 0;
1369
12.6k
  int32_t m_value                                          = 0;
1370
12.6k
  int32_t m_value_index                                    = 0;
1371
12.6k
  int32_t m_value_state                                    = 0;
1372
12.6k
  int32_t safe_d_value                                     = 0;
1373
1374
12.6k
  if( decoder == NULL )
1375
0
  {
1376
0
    libcerror_error_set(
1377
0
     error,
1378
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1379
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1380
0
     "%s: invalid decoder.",
1381
0
     function );
1382
1383
0
    return( -1 );
1384
0
  }
1385
12.6k
  if( literal_values == NULL )
1386
0
  {
1387
0
    libcerror_error_set(
1388
0
     error,
1389
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1390
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1391
0
     "%s: invalid literal values.",
1392
0
     function );
1393
1394
0
    return( -1 );
1395
0
  }
1396
12.6k
  if( uncompressed_data == NULL )
1397
0
  {
1398
0
    libcerror_error_set(
1399
0
     error,
1400
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1401
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1402
0
     "%s: invalid uncompressed data.",
1403
0
     function );
1404
1405
0
    return( -1 );
1406
0
  }
1407
12.6k
  if( uncompressed_data_size > (size_t) INT32_MAX )
1408
0
  {
1409
0
    libcerror_error_set(
1410
0
     error,
1411
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1412
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1413
0
     "%s: invalid uncompressed data size value exceeds maximum.",
1414
0
     function );
1415
1416
0
    return( -1 );
1417
0
  }
1418
12.6k
  if( uncompressed_data_offset == NULL )
1419
0
  {
1420
0
    libcerror_error_set(
1421
0
     error,
1422
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1423
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1424
0
     "%s: invalid uncompressed offset.",
1425
0
     function );
1426
1427
0
    return( -1 );
1428
0
  }
1429
12.6k
  safe_uncompressed_data_offset = *uncompressed_data_offset;
1430
1431
12.6k
  if( safe_uncompressed_data_offset > uncompressed_data_size )
1432
0
  {
1433
0
    libcerror_error_set(
1434
0
     error,
1435
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1436
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1437
0
     "%s: invalid uncompressed data offset value out of bounds.",
1438
0
     function );
1439
1440
0
    return( -1 );
1441
0
  }
1442
12.6k
  remaining_uncompressed_data_size = uncompressed_data_size - safe_uncompressed_data_offset;
1443
1444
12.6k
  l_value_state = decoder->l_value_state;
1445
12.6k
  m_value_state = decoder->m_value_state;
1446
12.6k
  d_value_state = decoder->d_value_state;
1447
1448
12.6k
  if( ( decoder->lmd_values_bits < (int32_t) -32 )
1449
12.6k
   || ( decoder->lmd_values_bits > 0 ) )
1450
58
  {
1451
58
    libcerror_error_set(
1452
58
     error,
1453
58
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1454
58
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1455
58
     "%s: invalid decoder - L, M, D values bits value out of bounds.",
1456
58
     function );
1457
1458
58
    return( -1 );
1459
58
  }
1460
12.5k
  if( libfmos_lzfse_bit_stream_get_value(
1461
12.5k
       bit_stream,
1462
12.5k
       -1 * decoder->lmd_values_bits,
1463
12.5k
       &value_32bit,
1464
12.5k
       error ) != 1 )
1465
30
  {
1466
30
    libcerror_error_set(
1467
30
     error,
1468
30
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1469
30
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1470
30
     "%s: unable to retrieve value from bit stream.",
1471
30
     function );
1472
1473
30
    return( -1 );
1474
30
  }
1475
12.5k
  for( lmd_value_index = 0; 
1476
43.1k
       lmd_value_index < decoder->number_of_lmd_values;
1477
30.5k
       lmd_value_index++ )
1478
30.8k
  {
1479
30.8k
    if( ( l_value_state < 0 )
1480
30.8k
     || ( l_value_state >= LIBFMOS_LZFSE_NUMBER_OF_L_VALUE_STATES ) )
1481
30
    {
1482
30
      libcerror_error_set(
1483
30
       error,
1484
30
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1485
30
       LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1486
30
       "%s: invalid L value state value out of bounds.",
1487
30
       function );
1488
1489
30
      return( -1 );
1490
30
    }
1491
30.8k
    value_decoder_entry = &( decoder->l_value_decoder_table[ l_value_state ] );
1492
1493
30.8k
    if( libfmos_lzfse_bit_stream_get_value(
1494
30.8k
         bit_stream,
1495
30.8k
         value_decoder_entry->number_of_bits,
1496
30.8k
         &value_32bit,
1497
30.8k
         error ) != 1 )
1498
16
    {
1499
16
      libcerror_error_set(
1500
16
       error,
1501
16
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1502
16
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1503
16
       "%s: unable to retrieve value from bit stream.",
1504
16
       function );
1505
1506
16
      return( -1 );
1507
16
    }
1508
30.7k
    l_value_state = (int32_t) value_decoder_entry->delta + (int32_t) ( value_32bit >> value_decoder_entry->value_bits );
1509
30.7k
    l_value       = value_decoder_entry->value_base + (int32_t) ( value_32bit & value_decoder_entry->value_bitmask );
1510
1511
#if defined( HAVE_DEBUG_OUTPUT )
1512
    if( libcnotify_verbose != 0 )
1513
    {
1514
      libcnotify_printf(
1515
       "%s: l_value\t\t\t\t\t: %" PRIi32 "\n",
1516
       function,
1517
       l_value );
1518
1519
      libcnotify_printf(
1520
       "%s: l_value_state\t\t\t\t: %" PRIi32 "\n",
1521
       function,
1522
       l_value_state );
1523
    }
1524
#endif
1525
30.7k
    if( ( m_value_state < 0 )
1526
30.7k
     || ( m_value_state >= LIBFMOS_LZFSE_NUMBER_OF_M_VALUE_STATES ) )
1527
27
    {
1528
27
      libcerror_error_set(
1529
27
       error,
1530
27
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1531
27
       LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1532
27
       "%s: invalid M value state value out of bounds.",
1533
27
       function );
1534
1535
27
      return( -1 );
1536
27
    }
1537
30.7k
    value_decoder_entry = &( decoder->m_value_decoder_table[ m_value_state ] );
1538
1539
30.7k
    if( libfmos_lzfse_bit_stream_get_value(
1540
30.7k
         bit_stream,
1541
30.7k
         value_decoder_entry->number_of_bits,
1542
30.7k
         &value_32bit,
1543
30.7k
         error ) != 1 )
1544
16
    {
1545
16
      libcerror_error_set(
1546
16
       error,
1547
16
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1548
16
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1549
16
       "%s: unable to retrieve value from bit stream.",
1550
16
       function );
1551
1552
16
      return( -1 );
1553
16
    }
1554
30.7k
    m_value_state = (int32_t) value_decoder_entry->delta + (int32_t) ( value_32bit >> value_decoder_entry->value_bits );
1555
30.7k
    m_value       = value_decoder_entry->value_base + (int32_t) ( value_32bit & value_decoder_entry->value_bitmask );
1556
1557
#if defined( HAVE_DEBUG_OUTPUT )
1558
    if( libcnotify_verbose != 0 )
1559
    {
1560
      libcnotify_printf(
1561
       "%s: m_value\t\t\t\t\t: %" PRIi32 "\n",
1562
       function,
1563
       m_value );
1564
1565
      libcnotify_printf(
1566
       "%s: m_value_state\t\t\t\t: %" PRIi32 "\n",
1567
       function,
1568
       m_value_state );
1569
    }
1570
#endif
1571
30.7k
    if( ( d_value_state < 0 )
1572
30.7k
     || ( d_value_state >= LIBFMOS_LZFSE_NUMBER_OF_D_VALUE_STATES ) )
1573
30
    {
1574
30
      libcerror_error_set(
1575
30
       error,
1576
30
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1577
30
       LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1578
30
       "%s: invalid D value state value out of bounds.",
1579
30
       function );
1580
1581
30
      return( -1 );
1582
30
    }
1583
30.7k
    value_decoder_entry = &( decoder->d_value_decoder_table[ d_value_state ] );
1584
1585
30.7k
    if( libfmos_lzfse_bit_stream_get_value(
1586
30.7k
         bit_stream,
1587
30.7k
         value_decoder_entry->number_of_bits,
1588
30.7k
         &value_32bit,
1589
30.7k
         error ) != 1 )
1590
13
    {
1591
13
      libcerror_error_set(
1592
13
       error,
1593
13
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1594
13
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1595
13
       "%s: unable to retrieve value from bit stream.",
1596
13
       function );
1597
1598
13
      return( -1 );
1599
13
    }
1600
30.7k
    d_value_state = (int32_t) value_decoder_entry->delta + (int32_t) ( value_32bit >> value_decoder_entry->value_bits );
1601
30.7k
    safe_d_value  = value_decoder_entry->value_base + (int32_t) ( value_32bit & value_decoder_entry->value_bitmask );
1602
1603
#if defined( HAVE_DEBUG_OUTPUT )
1604
    if( libcnotify_verbose != 0 )
1605
    {
1606
      libcnotify_printf(
1607
       "%s: d_value\t\t\t\t\t: %" PRIi32 "\n",
1608
       function,
1609
       safe_d_value );
1610
1611
      libcnotify_printf(
1612
       "%s: d_value_state\t\t\t\t: %" PRIi32 "\n",
1613
       function,
1614
       d_value_state );
1615
    }
1616
#endif
1617
30.7k
    if( safe_d_value != 0 )
1618
29.8k
    {
1619
29.8k
      d_value = safe_d_value;
1620
29.8k
    }
1621
30.7k
    if( ( l_value < 0 )
1622
30.7k
     || ( l_value > (int32_t) remaining_uncompressed_data_size )
1623
30.7k
     || ( l_value >= ( LIBFMOS_LZFSE_LITERALS_PER_BLOCK + 64 ) )
1624
30.7k
     || ( literal_value_index > ( ( LIBFMOS_LZFSE_LITERALS_PER_BLOCK + 64 ) - l_value ) ) )
1625
19
    {
1626
19
      libcerror_error_set(
1627
19
       error,
1628
19
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1629
19
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1630
19
       "%s: invalid L value out of bounds.",
1631
19
       function );
1632
1633
19
      return( -1 );
1634
19
    }
1635
30.6k
    for( l_value_index = 0;
1636
419k
         l_value_index < l_value;
1637
388k
         l_value_index++ )
1638
388k
    {
1639
388k
      uncompressed_data[ safe_uncompressed_data_offset++ ] = literal_values[ literal_value_index + l_value_index ];
1640
388k
    }
1641
30.6k
    literal_value_index              += l_value;
1642
30.6k
    remaining_uncompressed_data_size -= l_value;
1643
1644
30.6k
    if( ( m_value < 0 )
1645
30.6k
     || ( m_value > (int32_t) remaining_uncompressed_data_size ) )
1646
11
    {
1647
11
      libcerror_error_set(
1648
11
       error,
1649
11
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1650
11
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1651
11
       "%s: invalid M value out of bounds.",
1652
11
       function );
1653
1654
11
      return( -1 );
1655
11
    }
1656
30.6k
    if( ( d_value < 0 )
1657
30.6k
     || ( d_value > (int32_t) safe_uncompressed_data_offset )
1658
30.6k
     || ( ( safe_uncompressed_data_offset - d_value ) > uncompressed_data_size ) )
1659
93
    {
1660
93
      libcerror_error_set(
1661
93
       error,
1662
93
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1663
93
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1664
93
       "%s: invalid D value out of bounds.",
1665
93
       function );
1666
1667
93
      return( -1 );
1668
93
    }
1669
30.5k
    for( m_value_index = 0;
1670
115k
         m_value_index < m_value;
1671
85.3k
         m_value_index++ )
1672
85.3k
    {
1673
85.3k
      uncompressed_data[ safe_uncompressed_data_offset ] = uncompressed_data[ safe_uncompressed_data_offset - d_value ];
1674
1675
85.3k
      safe_uncompressed_data_offset++;
1676
85.3k
    }
1677
30.5k
    remaining_uncompressed_data_size -= m_value;
1678
30.5k
  }
1679
#if defined( HAVE_DEBUG_OUTPUT )
1680
  if( libcnotify_verbose != 0 )
1681
  {
1682
    libcnotify_printf(
1683
     "\n" );
1684
  }
1685
#endif
1686
#if defined( HAVE_DEBUG_OUTPUT )
1687
  if( libcnotify_verbose != 0 )
1688
  {
1689
    libcnotify_printf(
1690
     "%s: block data:\n",
1691
     function );
1692
    libcnotify_print_data(
1693
     &( uncompressed_data[ *uncompressed_data_offset ] ),
1694
     safe_uncompressed_data_offset - *uncompressed_data_offset,
1695
     0 );
1696
  }
1697
#endif
1698
12.3k
  *uncompressed_data_offset = safe_uncompressed_data_offset;
1699
1700
12.3k
  return( 1 );
1701
12.5k
}
1702
1703
/* Decompresses LZFSE compressed data
1704
 * Returns 1 on success or -1 on error
1705
 */
1706
int libfmos_lzfse_decompress(
1707
     const uint8_t *compressed_data,
1708
     size_t compressed_data_size,
1709
     uint8_t *uncompressed_data,
1710
     size_t *uncompressed_data_size,
1711
     libcerror_error_t **error )
1712
1.82k
{
1713
1.82k
  uint16_t frequency_table[ 360 ];
1714
1715
1.82k
  libfmos_lzfse_decoder_t *decoder    = NULL;
1716
1.82k
  static char *function               = "libfmos_lzfse_decompress";
1717
1.82k
  size_t compressed_data_offset       = 0;
1718
1.82k
  size_t safe_uncompressed_block_size = 0;
1719
1.82k
  size_t safe_uncompressed_data_size  = 0;
1720
1.82k
  size_t uncompressed_data_offset     = 0;
1721
1.82k
  uint32_t block_marker               = 0;
1722
1.82k
  uint32_t compressed_block_size      = 0;
1723
1.82k
  uint32_t uncompressed_block_size    = 0;
1724
1725
1.82k
  if( compressed_data == NULL )
1726
0
  {
1727
0
    libcerror_error_set(
1728
0
     error,
1729
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1730
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1731
0
     "%s: invalid compressed data.",
1732
0
     function );
1733
1734
0
    return( -1 );
1735
0
  }
1736
1.82k
  if( ( compressed_data_size < 4 )
1737
1.82k
   || ( compressed_data_size > (size_t) SSIZE_MAX ) )
1738
2
  {
1739
2
    libcerror_error_set(
1740
2
     error,
1741
2
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1742
2
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1743
2
     "%s: invalid compressed data size value out of bounds.",
1744
2
     function );
1745
1746
2
    return( -1 );
1747
2
  }
1748
1.82k
  if( uncompressed_data == NULL )
1749
0
  {
1750
0
    libcerror_error_set(
1751
0
     error,
1752
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1753
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1754
0
     "%s: invalid uncompressed data.",
1755
0
     function );
1756
1757
0
    return( -1 );
1758
0
  }
1759
1.82k
  if( uncompressed_data_size == NULL )
1760
0
  {
1761
0
    libcerror_error_set(
1762
0
     error,
1763
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1764
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1765
0
     "%s: invalid uncompressed data size.",
1766
0
     function );
1767
1768
0
    return( -1 );
1769
0
  }
1770
1.82k
  safe_uncompressed_data_size = *uncompressed_data_size;
1771
1772
1.82k
  if( safe_uncompressed_data_size > (size_t) SSIZE_MAX )
1773
0
  {
1774
0
    libcerror_error_set(
1775
0
     error,
1776
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1777
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1778
0
     "%s: invalid uncompressed data size value exceeds maximum.",
1779
0
     function );
1780
1781
0
    return( -1 );
1782
0
  }
1783
1.82k
  if( memory_set(
1784
1.82k
       frequency_table,
1785
1.82k
       0,
1786
1.82k
       sizeof( uint16_t ) * 360 ) == NULL )
1787
0
  {
1788
0
    libcerror_error_set(
1789
0
     error,
1790
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
1791
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
1792
0
     "%s: unable to clear frequency table.",
1793
0
     function );
1794
1795
0
    goto on_error;
1796
0
  }
1797
33.2k
  while( compressed_data_offset < compressed_data_size )
1798
33.1k
  {
1799
33.1k
    if( uncompressed_data_offset >= safe_uncompressed_data_size )
1800
4
    {
1801
4
      break;
1802
4
    }
1803
33.1k
    if( compressed_data_offset > ( compressed_data_size - 4 ) )
1804
40
    {
1805
40
      libcerror_error_set(
1806
40
       error,
1807
40
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1808
40
       LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
1809
40
       "%s: compressed data size value too small.",
1810
40
       function );
1811
1812
40
      goto on_error;
1813
40
    }
1814
33.0k
    byte_stream_copy_to_uint32_little_endian(
1815
33.0k
     &( compressed_data[ compressed_data_offset ] ),
1816
33.0k
     block_marker );
1817
1818
#if defined( HAVE_DEBUG_OUTPUT )
1819
    if( libcnotify_verbose != 0 )
1820
    {
1821
      if( ( block_marker != LIBFMOS_LZFSE_ENDOFSTREAM_BLOCK_MARKER )
1822
       && ( block_marker != LIBFMOS_LZFSE_UNCOMPRESSED_BLOCK_MARKER )
1823
       && ( block_marker != LIBFMOS_LZFSE_COMPRESSED_BLOCK_V1_MARKER )
1824
       && ( block_marker != LIBFMOS_LZFSE_COMPRESSED_BLOCK_V2_MARKER )
1825
       && ( block_marker != LIBFMOS_LZFSE_COMPRESSED_BLOCK_LZVN_MARKER ) )
1826
      {
1827
        libcnotify_printf(
1828
         "%s: block marker\t\t\t\t\t: 0x%08" PRIx32 "\n",
1829
         function,
1830
         block_marker );
1831
      }
1832
      else
1833
      {
1834
        libcnotify_printf(
1835
         "%s: block marker\t\t\t\t\t: %c%c%c%c (",
1836
         function,
1837
         compressed_data[ compressed_data_offset ],
1838
         compressed_data[ compressed_data_offset + 1 ],
1839
         compressed_data[ compressed_data_offset + 2 ],
1840
         compressed_data[ compressed_data_offset + 3 ] );
1841
1842
        switch( block_marker )
1843
        {
1844
          case LIBFMOS_LZFSE_ENDOFSTREAM_BLOCK_MARKER:
1845
            libcnotify_printf(
1846
             "end-of-stream" );
1847
            break;
1848
1849
          case LIBFMOS_LZFSE_UNCOMPRESSED_BLOCK_MARKER:
1850
            libcnotify_printf(
1851
             "uncompressed" );
1852
            break;
1853
1854
          case LIBFMOS_LZFSE_COMPRESSED_BLOCK_V1_MARKER:
1855
            libcnotify_printf(
1856
             "compressed version 1" );
1857
            break;
1858
1859
          case LIBFMOS_LZFSE_COMPRESSED_BLOCK_V2_MARKER:
1860
            libcnotify_printf(
1861
             "compressed version 2" );
1862
            break;
1863
1864
          case LIBFMOS_LZFSE_COMPRESSED_BLOCK_LZVN_MARKER:
1865
            libcnotify_printf(
1866
             "compressed LZVN" );
1867
            break;
1868
1869
          default:
1870
            libcnotify_printf(
1871
             "UNKNOWN" );
1872
            break;
1873
        }
1874
        libcnotify_printf(
1875
         ")\n" );
1876
      }
1877
    }
1878
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
1879
1880
33.0k
    compressed_data_offset += 4;
1881
1882
33.0k
    if( block_marker == LIBFMOS_LZFSE_ENDOFSTREAM_BLOCK_MARKER )
1883
1
    {
1884
1
      break;
1885
1
    }
1886
33.0k
    else if( ( block_marker != LIBFMOS_LZFSE_UNCOMPRESSED_BLOCK_MARKER )
1887
33.0k
          && ( block_marker != LIBFMOS_LZFSE_COMPRESSED_BLOCK_V1_MARKER )
1888
33.0k
          && ( block_marker != LIBFMOS_LZFSE_COMPRESSED_BLOCK_V2_MARKER )
1889
33.0k
          && ( block_marker != LIBFMOS_LZFSE_COMPRESSED_BLOCK_LZVN_MARKER ) )
1890
218
    {
1891
218
      libcerror_error_set(
1892
218
       error,
1893
218
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1894
218
       LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
1895
218
       "%s: unsupported block marker: 0x%08" PRIx32 ".",
1896
218
       function,
1897
218
       block_marker );
1898
1899
218
      goto on_error;
1900
218
    }
1901
32.8k
    if( compressed_data_offset > ( compressed_data_size - 4 ) )
1902
26
    {
1903
26
      libcerror_error_set(
1904
26
       error,
1905
26
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1906
26
       LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
1907
26
       "%s: compressed data size value too small.",
1908
26
       function );
1909
1910
26
      goto on_error;
1911
26
    }
1912
32.8k
    byte_stream_copy_to_uint32_little_endian(
1913
32.8k
     &( compressed_data[ compressed_data_offset ] ),
1914
32.8k
     uncompressed_block_size );
1915
1916
32.8k
    compressed_data_offset += 4;
1917
1918
#if defined( HAVE_DEBUG_OUTPUT )
1919
    if( libcnotify_verbose != 0 )
1920
    {
1921
      libcnotify_printf(
1922
       "%s: uncompressed block size\t\t\t: %" PRIu32 "\n",
1923
       function,
1924
       uncompressed_block_size );
1925
    }
1926
#endif
1927
/* TODO check if uncompressed data is sufficiently large and error if not */
1928
1929
32.8k
    switch( block_marker )
1930
32.8k
    {
1931
2.20k
      case LIBFMOS_LZFSE_COMPRESSED_BLOCK_V1_MARKER:
1932
2.20k
        if( libfmos_lzfse_decoder_initialize(
1933
2.20k
             &decoder,
1934
2.20k
             error ) != 1 )
1935
0
        {
1936
0
          libcerror_error_set(
1937
0
           error,
1938
0
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
1939
0
           LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1940
0
           "%s: unable to create decoder.",
1941
0
           function );
1942
1943
0
          goto on_error;
1944
0
        }
1945
2.20k
        if( libfmos_lzfse_read_block_v1_header(
1946
2.20k
             decoder,
1947
2.20k
             compressed_data,
1948
2.20k
             compressed_data_size,
1949
2.20k
             &compressed_data_offset,
1950
2.20k
             frequency_table,
1951
2.20k
             error ) != 1 )
1952
43
        {
1953
43
          libcerror_error_set(
1954
43
           error,
1955
43
           LIBCERROR_ERROR_DOMAIN_IO,
1956
43
           LIBCERROR_IO_ERROR_READ_FAILED,
1957
43
           "%s: unable to read block v1 header.",
1958
43
           function );
1959
1960
43
          goto on_error;
1961
43
        }
1962
2.16k
        break;
1963
1964
11.0k
      case LIBFMOS_LZFSE_COMPRESSED_BLOCK_V2_MARKER:
1965
11.0k
        if( libfmos_lzfse_decoder_initialize(
1966
11.0k
             &decoder,
1967
11.0k
             error ) != 1 )
1968
0
        {
1969
0
          libcerror_error_set(
1970
0
           error,
1971
0
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
1972
0
           LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1973
0
           "%s: unable to create decoder.",
1974
0
           function );
1975
1976
0
          goto on_error;
1977
0
        }
1978
11.0k
        if( libfmos_lzfse_read_block_v2_header(
1979
11.0k
             decoder,
1980
11.0k
             compressed_data,
1981
11.0k
             compressed_data_size,
1982
11.0k
             &compressed_data_offset,
1983
11.0k
             frequency_table,
1984
11.0k
             error ) != 1 )
1985
132
        {
1986
132
          libcerror_error_set(
1987
132
           error,
1988
132
           LIBCERROR_ERROR_DOMAIN_IO,
1989
132
           LIBCERROR_IO_ERROR_READ_FAILED,
1990
132
           "%s: unable to read block v2 header.",
1991
132
           function );
1992
1993
132
          goto on_error;
1994
132
        }
1995
10.9k
        break;
1996
1997
10.9k
      case LIBFMOS_LZFSE_COMPRESSED_BLOCK_LZVN_MARKER:
1998
4.02k
        if( compressed_data_offset > ( compressed_data_size - 4 ) )
1999
5
        {
2000
5
          libcerror_error_set(
2001
5
           error,
2002
5
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2003
5
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
2004
5
           "%s: compressed data size value too small.",
2005
5
           function );
2006
2007
5
          goto on_error;
2008
5
        }
2009
4.02k
        byte_stream_copy_to_uint32_little_endian(
2010
4.02k
         &( compressed_data[ compressed_data_offset ] ),
2011
4.02k
         compressed_block_size );
2012
2013
4.02k
        compressed_data_offset += 4;
2014
2015
#if defined( HAVE_DEBUG_OUTPUT )
2016
        if( libcnotify_verbose != 0 )
2017
        {
2018
          libcnotify_printf(
2019
           "%s: compressed block size\t\t\t\t: %" PRIu32 "\n",
2020
           function,
2021
           compressed_block_size );
2022
2023
          libcnotify_printf(
2024
           "\n" );
2025
        }
2026
#endif
2027
4.02k
        break;
2028
32.8k
    }
2029
32.6k
    if( ( (size_t) uncompressed_block_size > safe_uncompressed_data_size )
2030
32.6k
     || ( uncompressed_data_offset > ( safe_uncompressed_data_size - uncompressed_block_size ) ) )
2031
55
    {
2032
55
      libcerror_error_set(
2033
55
       error,
2034
55
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
2035
55
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
2036
55
       "%s: uncompressed block size value exceeds uncompressed data size.",
2037
55
       function );
2038
2039
55
      goto on_error;
2040
55
    }
2041
32.6k
    switch( block_marker )
2042
32.6k
    {
2043
15.4k
      case LIBFMOS_LZFSE_UNCOMPRESSED_BLOCK_MARKER:
2044
15.4k
        if( ( (size_t) uncompressed_block_size > compressed_data_size )
2045
15.4k
         || ( compressed_data_offset > ( compressed_data_size - uncompressed_block_size ) ) )
2046
56
        {
2047
56
          libcerror_error_set(
2048
56
           error,
2049
56
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
2050
56
           LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
2051
56
           "%s: uncompressed block size value exceeds compressed data size.",
2052
56
           function );
2053
2054
56
          goto on_error;
2055
56
        }
2056
#if defined( HAVE_DEBUG_OUTPUT )
2057
        if( libcnotify_verbose != 0 )
2058
        {
2059
          libcnotify_printf(
2060
           "%s: uncompressed:\n",
2061
           function );
2062
          libcnotify_print_data(
2063
           &( compressed_data[ compressed_data_offset ] ),
2064
           uncompressed_block_size,
2065
           LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
2066
        }
2067
#endif
2068
15.4k
        if( memory_copy(
2069
15.4k
             &( uncompressed_data[ uncompressed_data_offset ] ),
2070
15.4k
             &( compressed_data[ compressed_data_offset ] ),
2071
15.4k
             (size_t) uncompressed_block_size ) == NULL )
2072
0
        {
2073
0
          libcerror_error_set(
2074
0
           error,
2075
0
           LIBCERROR_ERROR_DOMAIN_MEMORY,
2076
0
           LIBCERROR_MEMORY_ERROR_COPY_FAILED,
2077
0
           "%s: unable to copy literal to uncompressed data.",
2078
0
           function );
2079
2080
0
          goto on_error;
2081
0
        }
2082
15.4k
        compressed_data_offset   += (size_t) uncompressed_block_size;
2083
15.4k
        uncompressed_data_offset += (size_t) uncompressed_block_size;
2084
2085
15.4k
        break;
2086
2087
2.16k
      case LIBFMOS_LZFSE_COMPRESSED_BLOCK_V1_MARKER:
2088
13.1k
      case LIBFMOS_LZFSE_COMPRESSED_BLOCK_V2_MARKER:
2089
13.1k
        if( libfmos_lzfse_build_decoder_table(
2090
13.1k
             LIBFMOS_LZFSE_NUMBER_OF_LITERAL_STATES,
2091
13.1k
             LIBFMOS_LZFSE_NUMBER_OF_LITERAL_SYMBOLS,
2092
13.1k
             &( frequency_table[ 104 ] ),
2093
13.1k
             decoder->literal_decoder_table,
2094
13.1k
             error ) != 1 )
2095
57
        {
2096
57
          libcerror_error_set(
2097
57
           error,
2098
57
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
2099
57
           LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
2100
57
           "%s: unable to build literal decoder table.",
2101
57
           function );
2102
2103
57
          goto on_error;
2104
57
        }
2105
13.0k
        if( libfmos_lzfse_build_value_decoder_table(
2106
13.0k
             LIBFMOS_LZFSE_NUMBER_OF_L_VALUE_STATES,
2107
13.0k
             LIBFMOS_LZFSE_NUMBER_OF_L_VALUE_SYMBOLS,
2108
13.0k
             &( frequency_table[ 0 ] ),
2109
13.0k
             libfmos_lzfse_l_value_bits_table,
2110
13.0k
             libfmos_lzfse_l_value_base_table,
2111
13.0k
             decoder->l_value_decoder_table,
2112
13.0k
             error ) != 1 )
2113
36
        {
2114
36
          libcerror_error_set(
2115
36
           error,
2116
36
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
2117
36
           LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
2118
36
           "%s: unable to build L value decoder table.",
2119
36
           function );
2120
2121
36
          goto on_error;
2122
36
        }
2123
13.0k
        if( libfmos_lzfse_build_value_decoder_table(
2124
13.0k
             LIBFMOS_LZFSE_NUMBER_OF_M_VALUE_STATES,
2125
13.0k
             LIBFMOS_LZFSE_NUMBER_OF_M_VALUE_SYMBOLS,
2126
13.0k
             &( frequency_table[ 20 ] ),
2127
13.0k
             libfmos_lzfse_m_value_bits_table,
2128
13.0k
             libfmos_lzfse_m_value_base_table,
2129
13.0k
             decoder->m_value_decoder_table,
2130
13.0k
             error ) != 1 )
2131
18
        {
2132
18
          libcerror_error_set(
2133
18
           error,
2134
18
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
2135
18
           LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
2136
18
           "%s: unable to build M value decoder table.",
2137
18
           function );
2138
2139
18
          goto on_error;
2140
18
        }
2141
13.0k
        if( libfmos_lzfse_build_value_decoder_table(
2142
13.0k
             LIBFMOS_LZFSE_NUMBER_OF_D_VALUE_STATES,
2143
13.0k
             LIBFMOS_LZFSE_NUMBER_OF_D_VALUE_SYMBOLS,
2144
13.0k
             &( frequency_table[ 40 ] ),
2145
13.0k
             libfmos_lzfse_d_value_bits_table,
2146
13.0k
             libfmos_lzfse_d_value_base_table,
2147
13.0k
             decoder->d_value_decoder_table,
2148
13.0k
             error ) != 1 )
2149
20
        {
2150
20
          libcerror_error_set(
2151
20
           error,
2152
20
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
2153
20
           LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
2154
20
           "%s: unable to build D value decoder table.",
2155
20
           function );
2156
2157
20
          goto on_error;
2158
20
        }
2159
12.9k
        if( libfmos_lzfse_read_block(
2160
12.9k
             decoder,
2161
12.9k
             compressed_data,
2162
12.9k
             compressed_data_size,
2163
12.9k
             &compressed_data_offset,
2164
12.9k
             uncompressed_data,
2165
12.9k
             safe_uncompressed_data_size,
2166
12.9k
             &uncompressed_data_offset,
2167
12.9k
             error ) != 1 )
2168
671
        {
2169
671
          libcerror_error_set(
2170
671
           error,
2171
671
           LIBCERROR_ERROR_DOMAIN_IO,
2172
671
           LIBCERROR_IO_ERROR_READ_FAILED,
2173
671
           "%s: unable to read block.",
2174
671
           function );
2175
2176
671
          goto on_error;
2177
671
        }
2178
12.3k
        if( libfmos_lzfse_decoder_free(
2179
12.3k
             &decoder,
2180
12.3k
             error ) != 1 )
2181
0
        {
2182
0
          libcerror_error_set(
2183
0
           error,
2184
0
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
2185
0
           LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
2186
0
           "%s: unable to free decoder.",
2187
0
           function );
2188
2189
0
          goto on_error;
2190
0
        }
2191
12.3k
        break;
2192
2193
12.3k
      case LIBFMOS_LZFSE_COMPRESSED_BLOCK_LZVN_MARKER:
2194
4.01k
        if( ( (size_t) compressed_block_size > compressed_data_size )
2195
4.01k
         || ( compressed_data_offset > ( compressed_data_size - compressed_block_size ) ) )
2196
79
        {
2197
79
          libcerror_error_set(
2198
79
           error,
2199
79
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
2200
79
           LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
2201
79
           "%s: compressed block size value exceeds compressed data size.",
2202
79
           function );
2203
2204
79
          goto on_error;
2205
79
        }
2206
3.93k
        safe_uncompressed_block_size = (size_t) uncompressed_block_size;
2207
2208
3.93k
        if( libfmos_lzvn_decompress(
2209
3.93k
             &( compressed_data[ compressed_data_offset ] ),
2210
3.93k
             compressed_block_size,
2211
3.93k
             &( uncompressed_data[ uncompressed_data_offset ] ),
2212
3.93k
             &safe_uncompressed_block_size,
2213
3.93k
             error ) != 1 )
2214
198
        {
2215
198
          libcerror_error_set(
2216
198
           error,
2217
198
           LIBCERROR_ERROR_DOMAIN_COMPRESSION,
2218
198
           LIBCERROR_COMPRESSION_ERROR_DECOMPRESS_FAILED,
2219
198
           "%s: unable to decompress LZVN compressed data.",
2220
198
           function );
2221
2222
198
          goto on_error;
2223
198
        }
2224
3.73k
        compressed_data_offset   += (size_t) compressed_block_size;
2225
3.73k
        uncompressed_data_offset += (size_t) uncompressed_block_size;
2226
2227
3.73k
        break;
2228
32.6k
    }
2229
32.6k
  }
2230
168
  *uncompressed_data_size = uncompressed_data_offset;
2231
2232
168
  return( 1 );
2233
2234
1.65k
on_error:
2235
1.65k
  if( decoder != NULL )
2236
986
  {
2237
986
    libfmos_lzfse_decoder_free(
2238
986
     &decoder,
2239
986
     NULL );
2240
986
  }
2241
1.65k
  return( -1 );
2242
1.82k
}
2243