Coverage Report

Created: 2026-03-05 07:45

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