Coverage Report

Created: 2026-05-24 07:09

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