Coverage Report

Created: 2026-08-31 07:43

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