Coverage Report

Created: 2026-01-20 07:11

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
247
{
40
247
  static char *function              = "libfmos_adc_decompress";
41
247
  size_t compressed_data_offset      = 0;
42
247
  size_t match_offset                = 0;
43
247
  size_t safe_uncompressed_data_size = 0;
44
247
  size_t uncompressed_data_offset    = 0;
45
247
  uint16_t distance                  = 0;
46
247
  uint8_t oppcode                    = 0;
47
247
  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
247
  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
247
  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
247
  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
247
  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
247
  safe_uncompressed_data_size = *uncompressed_data_size;
99
100
247
  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
886k
  while( compressed_data_offset < compressed_data_size )
112
886k
  {
113
886k
    if( uncompressed_data_offset >= safe_uncompressed_data_size )
114
7
    {
115
7
      break;
116
7
    }
117
886k
    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
886k
    oppcode = compressed_data[ compressed_data_offset++ ];
129
130
886k
    if( ( oppcode & 0x80 ) != 0 )
131
726k
    {
132
726k
      size = ( oppcode & 0x7f ) + 1;
133
134
726k
      if( ( (size_t) size > compressed_data_size )
135
726k
       || ( compressed_data_offset > ( compressed_data_size - size ) ) )
136
31
      {
137
31
        libcerror_error_set(
138
31
         error,
139
31
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
140
31
         LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
141
31
         "%s: literal size value exceeds compressed data size.",
142
31
         function );
143
144
31
        return( -1 );
145
31
      }
146
726k
      if( ( (size_t) size > safe_uncompressed_data_size )
147
726k
       || ( uncompressed_data_offset > ( safe_uncompressed_data_size - size ) ) )
148
7
      {
149
7
        libcerror_error_set(
150
7
         error,
151
7
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
152
7
         LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
153
7
         "%s: literal size value exceeds uncompressed data size.",
154
7
         function );
155
156
7
        return( -1 );
157
7
      }
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
726k
      if( memory_copy(
171
726k
           &( uncompressed_data[ uncompressed_data_offset ] ),
172
726k
           &( compressed_data[ compressed_data_offset ] ),
173
726k
           (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
726k
      compressed_data_offset   += (size_t) size;
185
726k
      uncompressed_data_offset += (size_t) size;
186
726k
    }
187
160k
    else
188
160k
    {
189
160k
      if( ( oppcode & 0x40 ) != 0 )
190
34.0k
      {
191
34.0k
        if( ( compressed_data_size < 2 )
192
34.0k
         || ( compressed_data_offset > ( compressed_data_size - 2 ) ) )
193
20
        {
194
20
          libcerror_error_set(
195
20
           error,
196
20
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
197
20
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
198
20
           "%s: compressed data size value too small.",
199
20
           function );
200
201
20
          return( -1 );
202
20
        }
203
34.0k
        size       = ( oppcode & 0x3f ) + 4;
204
34.0k
        distance   = compressed_data[ compressed_data_offset++ ];
205
34.0k
        distance <<= 8;
206
34.0k
        distance  |= compressed_data[ compressed_data_offset++ ];
207
34.0k
      }
208
126k
      else
209
126k
      {
210
126k
        if( compressed_data_offset >= compressed_data_size )
211
12
        {
212
12
          libcerror_error_set(
213
12
           error,
214
12
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
215
12
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
216
12
           "%s: compressed data size value too small.",
217
12
           function );
218
219
12
          return( -1 );
220
12
        }
221
126k
        size       = ( ( oppcode & 0x3f ) >> 2 ) + 3;
222
126k
        distance   = oppcode & 0x03;
223
126k
        distance <<= 8;
224
126k
        distance  |= compressed_data[ compressed_data_offset++ ];
225
126k
      }
226
160k
      if( (size_t) distance >= uncompressed_data_offset )
227
64
      {
228
64
        libcerror_error_set(
229
64
         error,
230
64
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
231
64
         LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
232
64
         "%s: invalid distance value out of bounds.",
233
64
         function );
234
235
64
        return( -1 );
236
64
      }
237
160k
      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
160k
      match_offset = uncompressed_data_offset - distance - 1;
249
250
160k
      if( ( (size_t) size > safe_uncompressed_data_size )
251
160k
       || ( uncompressed_data_offset > ( safe_uncompressed_data_size - size ) ) )
252
17
      {
253
17
        libcerror_error_set(
254
17
         error,
255
17
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
256
17
         LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
257
17
         "%s: invalid match size value out of bounds.",
258
17
         function );
259
260
17
        return( -1 );
261
17
      }
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.84M
      while( size > 0 )
275
2.68M
      {
276
2.68M
        uncompressed_data[ uncompressed_data_offset++ ] = uncompressed_data[ match_offset++ ];
277
278
2.68M
        size--;
279
2.68M
      }
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
160k
    }
293
886k
  }
294
96
  *uncompressed_data_size = uncompressed_data_offset;
295
296
96
  return( 1 );
297
247
}
298