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