/src/libfsapfs/libfsapfs/libfsapfs_compressed_data_header.c
Line | Count | Source |
1 | | /* |
2 | | * The compressed data header functions |
3 | | * |
4 | | * Copyright (C) 2018-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 <byte_stream.h> |
24 | | #include <memory.h> |
25 | | #include <types.h> |
26 | | |
27 | | #include "libfsapfs_compressed_data_header.h" |
28 | | #include "libfsapfs_libcerror.h" |
29 | | #include "libfsapfs_libcnotify.h" |
30 | | |
31 | | #include "fsapfs_compressed_data.h" |
32 | | |
33 | | /* Creates compressed data header |
34 | | * Make sure the value compressed_data_header is referencing, is set to NULL |
35 | | * Returns 1 if successful or -1 on error |
36 | | */ |
37 | | int libfsapfs_compressed_data_header_initialize( |
38 | | libfsapfs_compressed_data_header_t **compressed_data_header, |
39 | | libcerror_error_t **error ) |
40 | 0 | { |
41 | 0 | static char *function = "libfsapfs_compressed_data_header_initialize"; |
42 | |
|
43 | 0 | if( compressed_data_header == NULL ) |
44 | 0 | { |
45 | 0 | libcerror_error_set( |
46 | 0 | error, |
47 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
48 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
49 | 0 | "%s: invalid compressed data header.", |
50 | 0 | function ); |
51 | |
|
52 | 0 | return( -1 ); |
53 | 0 | } |
54 | 0 | if( *compressed_data_header != NULL ) |
55 | 0 | { |
56 | 0 | libcerror_error_set( |
57 | 0 | error, |
58 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
59 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
60 | 0 | "%s: invalid compressed data header value already set.", |
61 | 0 | function ); |
62 | |
|
63 | 0 | return( -1 ); |
64 | 0 | } |
65 | 0 | *compressed_data_header = memory_allocate_structure( |
66 | 0 | libfsapfs_compressed_data_header_t ); |
67 | |
|
68 | 0 | if( *compressed_data_header == NULL ) |
69 | 0 | { |
70 | 0 | libcerror_error_set( |
71 | 0 | error, |
72 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
73 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
74 | 0 | "%s: unable to create compressed data header.", |
75 | 0 | function ); |
76 | |
|
77 | 0 | goto on_error; |
78 | 0 | } |
79 | 0 | if( memory_set( |
80 | 0 | *compressed_data_header, |
81 | 0 | 0, |
82 | 0 | sizeof( libfsapfs_compressed_data_header_t ) ) == NULL ) |
83 | 0 | { |
84 | 0 | libcerror_error_set( |
85 | 0 | error, |
86 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
87 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
88 | 0 | "%s: unable to clear compressed data header.", |
89 | 0 | function ); |
90 | |
|
91 | 0 | goto on_error; |
92 | 0 | } |
93 | 0 | return( 1 ); |
94 | | |
95 | 0 | on_error: |
96 | 0 | if( *compressed_data_header != NULL ) |
97 | 0 | { |
98 | 0 | memory_free( |
99 | 0 | *compressed_data_header ); |
100 | |
|
101 | 0 | *compressed_data_header = NULL; |
102 | 0 | } |
103 | 0 | return( -1 ); |
104 | 0 | } |
105 | | |
106 | | /* Frees compressed data header |
107 | | * Returns 1 if successful or -1 on error |
108 | | */ |
109 | | int libfsapfs_compressed_data_header_free( |
110 | | libfsapfs_compressed_data_header_t **compressed_data_header, |
111 | | libcerror_error_t **error ) |
112 | 0 | { |
113 | 0 | static char *function = "libfsapfs_compressed_data_header_free"; |
114 | |
|
115 | 0 | if( compressed_data_header == NULL ) |
116 | 0 | { |
117 | 0 | libcerror_error_set( |
118 | 0 | error, |
119 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
120 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
121 | 0 | "%s: invalid compressed data header.", |
122 | 0 | function ); |
123 | |
|
124 | 0 | return( -1 ); |
125 | 0 | } |
126 | 0 | if( *compressed_data_header != NULL ) |
127 | 0 | { |
128 | 0 | memory_free( |
129 | 0 | *compressed_data_header ); |
130 | |
|
131 | 0 | *compressed_data_header = NULL; |
132 | 0 | } |
133 | 0 | return( 1 ); |
134 | 0 | } |
135 | | |
136 | | /* Reads the compressed data header |
137 | | * Returns 1 if successful, 0 if the signature does not match or -1 on error |
138 | | */ |
139 | | int libfsapfs_compressed_data_header_read_data( |
140 | | libfsapfs_compressed_data_header_t *compressed_data_header, |
141 | | const uint8_t *data, |
142 | | size_t data_size, |
143 | | libcerror_error_t **error ) |
144 | 0 | { |
145 | 0 | static char *function = "libfsapfs_compressed_data_header_read_data"; |
146 | |
|
147 | 0 | if( compressed_data_header == NULL ) |
148 | 0 | { |
149 | 0 | libcerror_error_set( |
150 | 0 | error, |
151 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
152 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
153 | 0 | "%s: invalid compressed data header.", |
154 | 0 | function ); |
155 | |
|
156 | 0 | return( -1 ); |
157 | 0 | } |
158 | 0 | if( data == NULL ) |
159 | 0 | { |
160 | 0 | libcerror_error_set( |
161 | 0 | error, |
162 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
163 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, |
164 | 0 | "%s: invalid data.", |
165 | 0 | function ); |
166 | |
|
167 | 0 | return( -1 ); |
168 | 0 | } |
169 | 0 | if( ( data_size < sizeof( fsapfs_compressed_data_header_t ) ) |
170 | 0 | || ( data_size > (size_t) SSIZE_MAX ) ) |
171 | 0 | { |
172 | 0 | libcerror_error_set( |
173 | 0 | error, |
174 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
175 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
176 | 0 | "%s: invalid data size value out of bounds.", |
177 | 0 | function ); |
178 | |
|
179 | 0 | return( -1 ); |
180 | 0 | } |
181 | | #if defined( HAVE_DEBUG_OUTPUT ) |
182 | | if( libcnotify_verbose != 0 ) |
183 | | { |
184 | | libcnotify_printf( |
185 | | "%s: compressed data header data:\n", |
186 | | function ); |
187 | | libcnotify_print_data( |
188 | | data, |
189 | | sizeof( fsapfs_compressed_data_header_t ), |
190 | | LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA ); |
191 | | } |
192 | | #endif |
193 | 0 | if( memory_compare( |
194 | 0 | ( (fsapfs_compressed_data_header_t *) data )->signature, |
195 | 0 | "fpmc", |
196 | 0 | 4 ) != 0 ) |
197 | 0 | { |
198 | 0 | return( 0 ); |
199 | 0 | } |
200 | 0 | byte_stream_copy_to_uint32_little_endian( |
201 | 0 | ( (fsapfs_compressed_data_header_t *) data )->compression_method, |
202 | 0 | compressed_data_header->compression_method ); |
203 | |
|
204 | 0 | byte_stream_copy_to_uint64_little_endian( |
205 | 0 | ( (fsapfs_compressed_data_header_t *) data )->uncompressed_data_size, |
206 | 0 | compressed_data_header->uncompressed_data_size ); |
207 | |
|
208 | | #if defined( HAVE_DEBUG_OUTPUT ) |
209 | | if( libcnotify_verbose != 0 ) |
210 | | { |
211 | | libcnotify_printf( |
212 | | "%s: signature\t\t\t: %c%c%c%c\n", |
213 | | function, |
214 | | ( (fsapfs_compressed_data_header_t *) data )->signature[ 0 ], |
215 | | ( (fsapfs_compressed_data_header_t *) data )->signature[ 1 ], |
216 | | ( (fsapfs_compressed_data_header_t *) data )->signature[ 2 ], |
217 | | ( (fsapfs_compressed_data_header_t *) data )->signature[ 3 ] ); |
218 | | |
219 | | libcnotify_printf( |
220 | | "%s: compression method\t\t: %" PRIu32 "\n", |
221 | | function, |
222 | | compressed_data_header->compression_method ); |
223 | | |
224 | | libcnotify_printf( |
225 | | "%s: uncompressed data size\t: %" PRIu64 "\n", |
226 | | function, |
227 | | compressed_data_header->uncompressed_data_size ); |
228 | | |
229 | | libcnotify_printf( |
230 | | "\n" ); |
231 | | } |
232 | | #endif /* defined( HAVE_DEBUG_OUTPUT ) */ |
233 | |
|
234 | 0 | return( 1 ); |
235 | 0 | } |
236 | | |