Coverage Report

Created: 2024-02-25 07:19

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