Coverage Report

Created: 2026-05-30 07:13

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-2026, Joachim Metz <joachim.metz@gmail.com>
5
 *
6
 * Refer to AUTHORS for acknowledgements.
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
#include <common.h>
23
#include <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
259
{
40
259
  static char *function              = "libfmos_adc_decompress";
41
259
  size_t compressed_data_offset      = 0;
42
259
  size_t match_offset                = 0;
43
259
  size_t safe_uncompressed_data_size = 0;
44
259
  size_t uncompressed_data_offset    = 0;
45
259
  uint16_t distance                  = 0;
46
259
  uint8_t oppcode                    = 0;
47
259
  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
259
  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
259
  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
259
  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
259
  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
259
  safe_uncompressed_data_size = *uncompressed_data_size;
99
100
259
  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
1.17M
  while( compressed_data_offset < compressed_data_size )
112
1.17M
  {
113
1.17M
    if( uncompressed_data_offset >= safe_uncompressed_data_size )
114
8
    {
115
8
      break;
116
8
    }
117
1.17M
    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
1.17M
    oppcode = compressed_data[ compressed_data_offset++ ];
129
130
1.17M
    if( ( oppcode & 0x80 ) != 0 )
131
1.03M
    {
132
1.03M
      size = ( oppcode & 0x7f ) + 1;
133
134
1.03M
      if( ( (size_t) size > compressed_data_size )
135
1.03M
       || ( compressed_data_offset > ( compressed_data_size - size ) ) )
136
34
      {
137
34
        libcerror_error_set(
138
34
         error,
139
34
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
140
34
         LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
141
34
         "%s: literal size value exceeds compressed data size.",
142
34
         function );
143
144
34
        return( -1 );
145
34
      }
146
1.03M
      if( ( (size_t) size > safe_uncompressed_data_size )
147
1.03M
       || ( uncompressed_data_offset > ( safe_uncompressed_data_size - size ) ) )
148
9
      {
149
9
        libcerror_error_set(
150
9
         error,
151
9
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
152
9
         LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
153
9
         "%s: literal size value exceeds uncompressed data size.",
154
9
         function );
155
156
9
        return( -1 );
157
9
      }
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
1.03M
      if( memory_copy(
171
1.03M
           &( uncompressed_data[ uncompressed_data_offset ] ),
172
1.03M
           &( compressed_data[ compressed_data_offset ] ),
173
1.03M
           (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
1.03M
      compressed_data_offset   += (size_t) size;
185
1.03M
      uncompressed_data_offset += (size_t) size;
186
1.03M
    }
187
139k
    else
188
139k
    {
189
139k
      if( ( oppcode & 0x40 ) != 0 )
190
32.3k
      {
191
32.3k
        if( ( compressed_data_size < 2 )
192
32.3k
         || ( compressed_data_offset > ( compressed_data_size - 2 ) ) )
193
21
        {
194
21
          libcerror_error_set(
195
21
           error,
196
21
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
197
21
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
198
21
           "%s: compressed data size value too small.",
199
21
           function );
200
201
21
          return( -1 );
202
21
        }
203
32.2k
        size       = ( oppcode & 0x3f ) + 4;
204
32.2k
        distance   = compressed_data[ compressed_data_offset++ ];
205
32.2k
        distance <<= 8;
206
32.2k
        distance  |= compressed_data[ compressed_data_offset++ ];
207
32.2k
      }
208
107k
      else
209
107k
      {
210
107k
        if( compressed_data_offset >= compressed_data_size )
211
15
        {
212
15
          libcerror_error_set(
213
15
           error,
214
15
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
215
15
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
216
15
           "%s: compressed data size value too small.",
217
15
           function );
218
219
15
          return( -1 );
220
15
        }
221
107k
        size       = ( ( oppcode & 0x3f ) >> 2 ) + 3;
222
107k
        distance   = oppcode & 0x03;
223
107k
        distance <<= 8;
224
107k
        distance  |= compressed_data[ compressed_data_offset++ ];
225
107k
      }
226
139k
      if( (size_t) distance >= uncompressed_data_offset )
227
61
      {
228
61
        libcerror_error_set(
229
61
         error,
230
61
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
231
61
         LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
232
61
         "%s: invalid distance value out of bounds.",
233
61
         function );
234
235
61
        return( -1 );
236
61
      }
237
139k
      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
139k
      match_offset = uncompressed_data_offset - distance - 1;
249
250
139k
      if( ( (size_t) size > safe_uncompressed_data_size )
251
139k
       || ( uncompressed_data_offset > ( safe_uncompressed_data_size - size ) ) )
252
20
      {
253
20
        libcerror_error_set(
254
20
         error,
255
20
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
256
20
         LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
257
20
         "%s: invalid match size value out of bounds.",
258
20
         function );
259
260
20
        return( -1 );
261
20
      }
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.82M
      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
139k
    }
293
1.17M
  }
294
99
  *uncompressed_data_size = uncompressed_data_offset;
295
296
99
  return( 1 );
297
259
}
298