Coverage Report

Created: 2025-10-14 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libmodi/libfmos/libfmos_adc.c
Line
Count
Source
1
/*
2
 * ADC (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 <memory.h>
24
#include <types.h>
25
26
#include "libfmos_adc.h"
27
#include "libfmos_libcerror.h"
28
#include "libfmos_libcnotify.h"
29
30
/* Decompresses ADC compressed data
31
 * Returns 1 on success or -1 on error
32
 */
33
int libfmos_adc_decompress(
34
     const uint8_t *compressed_data,
35
     size_t compressed_data_size,
36
     uint8_t *uncompressed_data,
37
     size_t *uncompressed_data_size,
38
     libcerror_error_t **error )
39
248
{
40
248
  static char *function              = "libfmos_adc_decompress";
41
248
  size_t compressed_data_offset      = 0;
42
248
  size_t match_offset                = 0;
43
248
  size_t safe_uncompressed_data_size = 0;
44
248
  size_t uncompressed_data_offset    = 0;
45
248
  uint16_t distance                  = 0;
46
248
  uint8_t oppcode                    = 0;
47
248
  uint8_t size                       = 0;
48
49
#if defined( HAVE_DEBUG_OUTPUT )
50
  size_t debug_match_offset          = 0;
51
  uint16_t debug_match_size          = 0;
52
#endif
53
54
248
  if( compressed_data == NULL )
55
0
  {
56
0
    libcerror_error_set(
57
0
     error,
58
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
59
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
60
0
     "%s: invalid compressed data.",
61
0
     function );
62
63
0
    return( -1 );
64
0
  }
65
248
  if( compressed_data_size > (size_t) SSIZE_MAX )
66
0
  {
67
0
    libcerror_error_set(
68
0
     error,
69
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
70
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
71
0
     "%s: invalid compressed data size value exceeds maximum.",
72
0
     function );
73
74
0
    return( -1 );
75
0
  }
76
248
  if( uncompressed_data == NULL )
77
0
  {
78
0
    libcerror_error_set(
79
0
     error,
80
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
81
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
82
0
     "%s: invalid uncompressed data.",
83
0
     function );
84
85
0
    return( -1 );
86
0
  }
87
248
  if( uncompressed_data_size == NULL )
88
0
  {
89
0
    libcerror_error_set(
90
0
     error,
91
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
92
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
93
0
     "%s: invalid uncompressed data size.",
94
0
     function );
95
96
0
    return( -1 );
97
0
  }
98
248
  safe_uncompressed_data_size = *uncompressed_data_size;
99
100
248
  if( safe_uncompressed_data_size > (size_t) SSIZE_MAX )
101
0
  {
102
0
    libcerror_error_set(
103
0
     error,
104
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
105
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
106
0
     "%s: invalid uncompressed data size value exceeds maximum.",
107
0
     function );
108
109
0
    return( -1 );
110
0
  }
111
876k
  while( compressed_data_offset < compressed_data_size )
112
875k
  {
113
875k
    if( uncompressed_data_offset >= safe_uncompressed_data_size )
114
5
    {
115
5
      break;
116
5
    }
117
875k
    if( compressed_data_offset >= compressed_data_size )
118
0
    {
119
0
      libcerror_error_set(
120
0
       error,
121
0
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
122
0
       LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
123
0
       "%s: compressed data size value too small.",
124
0
       function );
125
126
0
      return( -1 );
127
0
    }
128
875k
    oppcode = compressed_data[ compressed_data_offset++ ];
129
130
875k
    if( ( oppcode & 0x80 ) != 0 )
131
731k
    {
132
731k
      size = ( oppcode & 0x7f ) + 1;
133
134
731k
      if( ( (size_t) size > compressed_data_size )
135
731k
       || ( compressed_data_offset > ( compressed_data_size - size ) ) )
136
30
      {
137
30
        libcerror_error_set(
138
30
         error,
139
30
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
140
30
         LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
141
30
         "%s: literal size value exceeds compressed data size.",
142
30
         function );
143
144
30
        return( -1 );
145
30
      }
146
731k
      if( ( (size_t) size > safe_uncompressed_data_size )
147
731k
       || ( uncompressed_data_offset > ( safe_uncompressed_data_size - size ) ) )
148
8
      {
149
8
        libcerror_error_set(
150
8
         error,
151
8
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
152
8
         LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
153
8
         "%s: literal size value exceeds uncompressed data size.",
154
8
         function );
155
156
8
        return( -1 );
157
8
      }
158
#if defined( HAVE_DEBUG_OUTPUT )
159
      if( libcnotify_verbose != 0 )
160
      {
161
        libcnotify_printf(
162
         "%s: literal:\n",
163
         function );
164
        libcnotify_print_data(
165
         &( compressed_data[ compressed_data_offset ] ),
166
         size,
167
         LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
168
      }
169
#endif
170
731k
      if( memory_copy(
171
731k
           &( uncompressed_data[ uncompressed_data_offset ] ),
172
731k
           &( compressed_data[ compressed_data_offset ] ),
173
731k
           (size_t) size ) == NULL )
174
0
      {
175
0
        libcerror_error_set(
176
0
         error,
177
0
         LIBCERROR_ERROR_DOMAIN_MEMORY,
178
0
         LIBCERROR_MEMORY_ERROR_COPY_FAILED,
179
0
         "%s: unable to copy literal to uncompressed data.",
180
0
         function );
181
182
0
        return( -1 );
183
0
      }
184
731k
      compressed_data_offset   += (size_t) size;
185
731k
      uncompressed_data_offset += (size_t) size;
186
731k
    }
187
144k
    else
188
144k
    {
189
144k
      if( ( oppcode & 0x40 ) != 0 )
190
31.6k
      {
191
31.6k
        if( ( compressed_data_size < 2 )
192
31.6k
         || ( compressed_data_offset > ( compressed_data_size - 2 ) ) )
193
22
        {
194
22
          libcerror_error_set(
195
22
           error,
196
22
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
197
22
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
198
22
           "%s: compressed data size value too small.",
199
22
           function );
200
201
22
          return( -1 );
202
22
        }
203
31.6k
        size       = ( oppcode & 0x3f ) + 4;
204
31.6k
        distance   = compressed_data[ compressed_data_offset++ ];
205
31.6k
        distance <<= 8;
206
31.6k
        distance  |= compressed_data[ compressed_data_offset++ ];
207
31.6k
      }
208
112k
      else
209
112k
      {
210
112k
        if( compressed_data_offset >= compressed_data_size )
211
11
        {
212
11
          libcerror_error_set(
213
11
           error,
214
11
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
215
11
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
216
11
           "%s: compressed data size value too small.",
217
11
           function );
218
219
11
          return( -1 );
220
11
        }
221
112k
        size       = ( ( oppcode & 0x3f ) >> 2 ) + 3;
222
112k
        distance   = oppcode & 0x03;
223
112k
        distance <<= 8;
224
112k
        distance  |= compressed_data[ compressed_data_offset++ ];
225
112k
      }
226
144k
      if( (size_t) distance >= uncompressed_data_offset )
227
62
      {
228
62
        libcerror_error_set(
229
62
         error,
230
62
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
231
62
         LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
232
62
         "%s: invalid distance value out of bounds.",
233
62
         function );
234
235
62
        return( -1 );
236
62
      }
237
144k
      if( uncompressed_data_offset < 1 )
238
0
      {
239
0
        libcerror_error_set(
240
0
         error,
241
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
242
0
         LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
243
0
         "%s: invalid uncompressed data offset value out of bounds.",
244
0
         function );
245
246
0
        return( -1 );
247
0
      }
248
144k
      match_offset = uncompressed_data_offset - distance - 1;
249
250
144k
      if( ( (size_t) size > safe_uncompressed_data_size )
251
144k
       || ( uncompressed_data_offset > ( safe_uncompressed_data_size - size ) ) )
252
15
      {
253
15
        libcerror_error_set(
254
15
         error,
255
15
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
256
15
         LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
257
15
         "%s: invalid match size value out of bounds.",
258
15
         function );
259
260
15
        return( -1 );
261
15
      }
262
#if defined( HAVE_DEBUG_OUTPUT )
263
      if( libcnotify_verbose != 0 )
264
      {
265
        debug_match_offset = match_offset;
266
        debug_match_size   = size;
267
268
        libcnotify_printf(
269
         "%s: match offset\t\t\t\t\t\t: 0x%" PRIzx "\n",
270
         function,
271
         debug_match_offset );
272
      }
273
#endif
274
2.64M
      while( size > 0 )
275
2.49M
      {
276
2.49M
        uncompressed_data[ uncompressed_data_offset++ ] = uncompressed_data[ match_offset++ ];
277
278
2.49M
        size--;
279
2.49M
      }
280
#if defined( HAVE_DEBUG_OUTPUT )
281
      if( libcnotify_verbose != 0 )
282
      {
283
        libcnotify_printf(
284
         "%s: match:\n",
285
         function );
286
        libcnotify_print_data(
287
         &( uncompressed_data[ debug_match_offset ] ),
288
         debug_match_size,
289
         LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
290
      }
291
#endif
292
144k
    }
293
875k
  }
294
100
  *uncompressed_data_size = uncompressed_data_offset;
295
296
100
  return( 1 );
297
248
}
298