/src/libfsapfs/libfsapfs/libfsapfs_data_stream.c
Line | Count | Source |
1 | | /* |
2 | | * Data stream functions |
3 | | * |
4 | | * Copyright (C) 2018-2025, 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 <types.h> |
24 | | |
25 | | #include "libfsapfs_buffer_data_handle.h" |
26 | | #include "libfsapfs_compressed_data_handle.h" |
27 | | #include "libfsapfs_data_block_data_handle.h" |
28 | | #include "libfsapfs_data_stream.h" |
29 | | #include "libfsapfs_encryption_context.h" |
30 | | #include "libfsapfs_file_extent.h" |
31 | | #include "libfsapfs_io_handle.h" |
32 | | #include "libfsapfs_libcdata.h" |
33 | | #include "libfsapfs_libfdata.h" |
34 | | |
35 | | /* Creates data stream from a buffer of data |
36 | | * Make sure the value data_stream is referencing, is set to NULL |
37 | | * Returns 1 if successful or -1 on error |
38 | | */ |
39 | | int libfsapfs_data_stream_initialize_from_data( |
40 | | libfdata_stream_t **data_stream, |
41 | | const uint8_t *data, |
42 | | size_t data_size, |
43 | | libcerror_error_t **error ) |
44 | 5 | { |
45 | 5 | libfdata_stream_t *safe_data_stream = NULL; |
46 | 5 | libfsapfs_buffer_data_handle_t *data_handle = NULL; |
47 | 5 | static char *function = "libfsapfs_data_stream_initialize_from_data"; |
48 | 5 | int segment_index = 0; |
49 | | |
50 | 5 | if( data_stream == NULL ) |
51 | 0 | { |
52 | 0 | libcerror_error_set( |
53 | 0 | error, |
54 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
55 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
56 | 0 | "%s: invalid data stream.", |
57 | 0 | function ); |
58 | |
|
59 | 0 | return( -1 ); |
60 | 0 | } |
61 | 5 | if( libfsapfs_buffer_data_handle_initialize( |
62 | 5 | &data_handle, |
63 | 5 | data, |
64 | 5 | data_size, |
65 | 5 | error ) != 1 ) |
66 | 0 | { |
67 | 0 | libcerror_error_set( |
68 | 0 | error, |
69 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
70 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
71 | 0 | "%s: unable to create buffer data handle.", |
72 | 0 | function ); |
73 | |
|
74 | 0 | goto on_error; |
75 | 0 | } |
76 | 5 | if( libfdata_stream_initialize( |
77 | 5 | &safe_data_stream, |
78 | 5 | (intptr_t *) data_handle, |
79 | 5 | (int (*)(intptr_t **, libcerror_error_t **)) &libfsapfs_buffer_data_handle_free, |
80 | 5 | NULL, |
81 | 5 | NULL, |
82 | 5 | (ssize_t (*)(intptr_t *, intptr_t *, int, int, uint8_t *, size_t, uint32_t, uint8_t, libcerror_error_t **)) &libfsapfs_buffer_data_handle_read_segment_data, |
83 | 5 | NULL, |
84 | 5 | (off64_t (*)(intptr_t *, intptr_t *, int, int, off64_t, libcerror_error_t **)) &libfsapfs_buffer_data_handle_seek_segment_offset, |
85 | 5 | LIBFDATA_DATA_HANDLE_FLAG_MANAGED, |
86 | 5 | error ) != 1 ) |
87 | 0 | { |
88 | 0 | libcerror_error_set( |
89 | 0 | error, |
90 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
91 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
92 | 0 | "%s: unable to create data stream.", |
93 | 0 | function ); |
94 | |
|
95 | 0 | goto on_error; |
96 | 0 | } |
97 | 5 | data_handle = NULL; |
98 | | |
99 | 5 | if( libfdata_stream_append_segment( |
100 | 5 | safe_data_stream, |
101 | 5 | &segment_index, |
102 | 5 | 0, |
103 | 5 | 0, |
104 | 5 | (size64_t) data_size, |
105 | 5 | 0, |
106 | 5 | error ) != 1 ) |
107 | 0 | { |
108 | 0 | libcerror_error_set( |
109 | 0 | error, |
110 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
111 | 0 | LIBCERROR_RUNTIME_ERROR_APPEND_FAILED, |
112 | 0 | "%s: unable to append data stream segment.", |
113 | 0 | function ); |
114 | |
|
115 | 0 | goto on_error; |
116 | 0 | } |
117 | 5 | *data_stream = safe_data_stream; |
118 | | |
119 | 5 | return( 1 ); |
120 | | |
121 | 0 | on_error: |
122 | 0 | if( safe_data_stream != NULL ) |
123 | 0 | { |
124 | 0 | libfdata_stream_free( |
125 | 0 | &safe_data_stream, |
126 | 0 | NULL ); |
127 | 0 | } |
128 | 0 | if( data_handle != NULL ) |
129 | 0 | { |
130 | 0 | libfsapfs_buffer_data_handle_free( |
131 | 0 | &data_handle, |
132 | 0 | NULL ); |
133 | 0 | } |
134 | 0 | return( -1 ); |
135 | 5 | } |
136 | | |
137 | | /* Creates data stream from file extents |
138 | | * Make sure the value data_stream is referencing, is set to NULL |
139 | | * Returns 1 if successful or -1 on error |
140 | | */ |
141 | | int libfsapfs_data_stream_initialize_from_file_extents( |
142 | | libfdata_stream_t **data_stream, |
143 | | libfsapfs_io_handle_t *io_handle, |
144 | | libfsapfs_encryption_context_t *encryption_context, |
145 | | libcdata_array_t *file_extents, |
146 | | size64_t data_stream_size, |
147 | | uint8_t is_sparse, |
148 | | libcerror_error_t **error ) |
149 | 452 | { |
150 | 452 | libfdata_stream_t *safe_data_stream = NULL; |
151 | 452 | libfsapfs_data_block_data_handle_t *data_handle = NULL; |
152 | 452 | static char *function = "libfsapfs_data_stream_initialize_from_file_extents"; |
153 | 452 | int segment_index = 0; |
154 | | |
155 | 452 | if( data_stream == NULL ) |
156 | 0 | { |
157 | 0 | libcerror_error_set( |
158 | 0 | error, |
159 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
160 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
161 | 0 | "%s: invalid data stream.", |
162 | 0 | function ); |
163 | |
|
164 | 0 | return( -1 ); |
165 | 0 | } |
166 | 452 | if( io_handle == NULL ) |
167 | 0 | { |
168 | 0 | libcerror_error_set( |
169 | 0 | error, |
170 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
171 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
172 | 0 | "%s: invalid IO handle.", |
173 | 0 | function ); |
174 | |
|
175 | 0 | return( -1 ); |
176 | 0 | } |
177 | 452 | if( libfsapfs_data_block_data_handle_initialize( |
178 | 452 | &data_handle, |
179 | 452 | io_handle, |
180 | 452 | encryption_context, |
181 | 452 | file_extents, |
182 | 452 | is_sparse, |
183 | 452 | error ) != 1 ) |
184 | 184 | { |
185 | 184 | libcerror_error_set( |
186 | 184 | error, |
187 | 184 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
188 | 184 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
189 | 184 | "%s: unable to create data handle.", |
190 | 184 | function ); |
191 | | |
192 | 184 | goto on_error; |
193 | 184 | } |
194 | 268 | if( libfdata_stream_initialize( |
195 | 268 | &safe_data_stream, |
196 | 268 | (intptr_t *) data_handle, |
197 | 268 | (int (*)(intptr_t **, libcerror_error_t **)) &libfsapfs_data_block_data_handle_free, |
198 | 268 | NULL, |
199 | 268 | NULL, |
200 | 268 | (ssize_t (*)(intptr_t *, intptr_t *, int, int, uint8_t *, size_t, uint32_t, uint8_t, libcerror_error_t **)) &libfsapfs_data_block_data_handle_read_segment_data, |
201 | 268 | NULL, |
202 | 268 | (off64_t (*)(intptr_t *, intptr_t *, int, int, off64_t, libcerror_error_t **)) &libfsapfs_data_block_data_handle_seek_segment_offset, |
203 | 268 | LIBFDATA_DATA_HANDLE_FLAG_MANAGED, |
204 | 268 | error ) != 1 ) |
205 | 0 | { |
206 | 0 | libcerror_error_set( |
207 | 0 | error, |
208 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
209 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
210 | 0 | "%s: unable to create data stream.", |
211 | 0 | function ); |
212 | |
|
213 | 0 | goto on_error; |
214 | 0 | } |
215 | 268 | data_handle = NULL; |
216 | | |
217 | 268 | if( libfdata_stream_append_segment( |
218 | 268 | safe_data_stream, |
219 | 268 | &segment_index, |
220 | 268 | 0, |
221 | 268 | 0, |
222 | 268 | data_stream_size, |
223 | 268 | 0, |
224 | 268 | error ) != 1 ) |
225 | 63 | { |
226 | 63 | libcerror_error_set( |
227 | 63 | error, |
228 | 63 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
229 | 63 | LIBCERROR_RUNTIME_ERROR_APPEND_FAILED, |
230 | 63 | "%s: unable to append data stream segment.", |
231 | 63 | function ); |
232 | | |
233 | 63 | goto on_error; |
234 | 63 | } |
235 | 205 | *data_stream = safe_data_stream; |
236 | | |
237 | 205 | return( 1 ); |
238 | | |
239 | 247 | on_error: |
240 | 247 | if( safe_data_stream != NULL ) |
241 | 63 | { |
242 | 63 | libfdata_stream_free( |
243 | 63 | &safe_data_stream, |
244 | 63 | NULL ); |
245 | 63 | } |
246 | 247 | if( data_handle != NULL ) |
247 | 0 | { |
248 | 0 | libfsapfs_data_block_data_handle_free( |
249 | 0 | &data_handle, |
250 | 0 | NULL ); |
251 | 0 | } |
252 | 247 | return( -1 ); |
253 | 268 | } |
254 | | |
255 | | /* Creates data stream from a compressed data stream |
256 | | * Make sure the value data_stream is referencing, is set to NULL |
257 | | * Returns 1 if successful or -1 on error |
258 | | */ |
259 | | int libfsapfs_data_stream_initialize_from_compressed_data_stream( |
260 | | libfdata_stream_t **data_stream, |
261 | | libfdata_stream_t *compressed_data_stream, |
262 | | size64_t uncompressed_data_size, |
263 | | int compression_method, |
264 | | libcerror_error_t **error ) |
265 | 0 | { |
266 | 0 | libfdata_stream_t *safe_data_stream = NULL; |
267 | 0 | libfsapfs_compressed_data_handle_t *data_handle = NULL; |
268 | 0 | static char *function = "libfsapfs_data_stream_initialize_from_compressed_data_stream"; |
269 | 0 | int segment_index = 0; |
270 | |
|
271 | 0 | if( data_stream == NULL ) |
272 | 0 | { |
273 | 0 | libcerror_error_set( |
274 | 0 | error, |
275 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
276 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
277 | 0 | "%s: invalid data stream.", |
278 | 0 | function ); |
279 | |
|
280 | 0 | return( -1 ); |
281 | 0 | } |
282 | 0 | if( libfsapfs_compressed_data_handle_initialize( |
283 | 0 | &data_handle, |
284 | 0 | compressed_data_stream, |
285 | 0 | uncompressed_data_size, |
286 | 0 | compression_method, |
287 | 0 | error ) != 1 ) |
288 | 0 | { |
289 | 0 | libcerror_error_set( |
290 | 0 | error, |
291 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
292 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
293 | 0 | "%s: unable to create compressed data handle.", |
294 | 0 | function ); |
295 | |
|
296 | 0 | goto on_error; |
297 | 0 | } |
298 | 0 | if( libfdata_stream_initialize( |
299 | 0 | &safe_data_stream, |
300 | 0 | (intptr_t *) data_handle, |
301 | 0 | (int (*)(intptr_t **, libcerror_error_t **)) &libfsapfs_compressed_data_handle_free, |
302 | 0 | NULL, |
303 | 0 | NULL, |
304 | 0 | (ssize_t (*)(intptr_t *, intptr_t *, int, int, uint8_t *, size_t, uint32_t, uint8_t, libcerror_error_t **)) &libfsapfs_compressed_data_handle_read_segment_data, |
305 | 0 | NULL, |
306 | 0 | (off64_t (*)(intptr_t *, intptr_t *, int, int, off64_t, libcerror_error_t **)) &libfsapfs_compressed_data_handle_seek_segment_offset, |
307 | 0 | LIBFDATA_DATA_HANDLE_FLAG_MANAGED, |
308 | 0 | error ) != 1 ) |
309 | 0 | { |
310 | 0 | libcerror_error_set( |
311 | 0 | error, |
312 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
313 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
314 | 0 | "%s: unable to create data stream.", |
315 | 0 | function ); |
316 | |
|
317 | 0 | goto on_error; |
318 | 0 | } |
319 | 0 | data_handle = NULL; |
320 | |
|
321 | 0 | if( libfdata_stream_append_segment( |
322 | 0 | safe_data_stream, |
323 | 0 | &segment_index, |
324 | 0 | 0, |
325 | 0 | 0, |
326 | 0 | uncompressed_data_size, |
327 | 0 | LIBFDATA_RANGE_FLAG_IS_COMPRESSED, |
328 | 0 | error ) != 1 ) |
329 | 0 | { |
330 | 0 | libcerror_error_set( |
331 | 0 | error, |
332 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
333 | 0 | LIBCERROR_RUNTIME_ERROR_APPEND_FAILED, |
334 | 0 | "%s: unable to append data as data stream segment.", |
335 | 0 | function ); |
336 | |
|
337 | 0 | goto on_error; |
338 | 0 | } |
339 | 0 | *data_stream = safe_data_stream; |
340 | |
|
341 | 0 | return( 1 ); |
342 | | |
343 | 0 | on_error: |
344 | 0 | if( safe_data_stream != NULL ) |
345 | 0 | { |
346 | 0 | libfdata_stream_free( |
347 | 0 | &safe_data_stream, |
348 | 0 | NULL ); |
349 | 0 | } |
350 | 0 | if( data_handle != NULL ) |
351 | 0 | { |
352 | 0 | libfsapfs_compressed_data_handle_free( |
353 | 0 | &data_handle, |
354 | | NULL ); |
355 | 0 | } |
356 | 0 | return( -1 ); |
357 | 0 | } |
358 | | |