/src/libvslvm/libvslvm/libvslvm_chunk_data.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Chunk data functions |
3 | | * |
4 | | * Copyright (C) 2014-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 <byte_stream.h> |
24 | | #include <memory.h> |
25 | | #include <types.h> |
26 | | |
27 | | #include "libvslvm_chunk_data.h" |
28 | | #include "libvslvm_definitions.h" |
29 | | #include "libvslvm_libbfio.h" |
30 | | #include "libvslvm_libcerror.h" |
31 | | #include "libvslvm_libcnotify.h" |
32 | | |
33 | | /* Creates a chunk data |
34 | | * Make sure the value chunk_data is referencing, is set to NULL |
35 | | * Returns 1 if successful or -1 on error |
36 | | */ |
37 | | int libvslvm_chunk_data_initialize( |
38 | | libvslvm_chunk_data_t **chunk_data, |
39 | | size_t data_size, |
40 | | libcerror_error_t **error ) |
41 | 0 | { |
42 | 0 | static char *function = "libvslvm_chunk_data_initialize"; |
43 | |
|
44 | 0 | if( chunk_data == NULL ) |
45 | 0 | { |
46 | 0 | libcerror_error_set( |
47 | 0 | error, |
48 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
49 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
50 | 0 | "%s: invalid chunk data.", |
51 | 0 | function ); |
52 | |
|
53 | 0 | return( -1 ); |
54 | 0 | } |
55 | 0 | if( *chunk_data != NULL ) |
56 | 0 | { |
57 | 0 | libcerror_error_set( |
58 | 0 | error, |
59 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
60 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
61 | 0 | "%s: invalid chunk data value already set.", |
62 | 0 | function ); |
63 | |
|
64 | 0 | return( -1 ); |
65 | 0 | } |
66 | 0 | if( ( data_size == 0 ) |
67 | 0 | || ( data_size > (size_t) MEMORY_MAXIMUM_ALLOCATION_SIZE ) ) |
68 | 0 | { |
69 | 0 | libcerror_error_set( |
70 | 0 | error, |
71 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
72 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS, |
73 | 0 | "%s: invalid data size value out of bounds.", |
74 | 0 | function ); |
75 | |
|
76 | 0 | return( -1 ); |
77 | 0 | } |
78 | 0 | *chunk_data = memory_allocate_structure( |
79 | 0 | libvslvm_chunk_data_t ); |
80 | |
|
81 | 0 | if( *chunk_data == NULL ) |
82 | 0 | { |
83 | 0 | libcerror_error_set( |
84 | 0 | error, |
85 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
86 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
87 | 0 | "%s: unable to create chunk data.", |
88 | 0 | function ); |
89 | |
|
90 | 0 | goto on_error; |
91 | 0 | } |
92 | 0 | if( memory_set( |
93 | 0 | *chunk_data, |
94 | 0 | 0, |
95 | 0 | sizeof( libvslvm_chunk_data_t ) ) == NULL ) |
96 | 0 | { |
97 | 0 | libcerror_error_set( |
98 | 0 | error, |
99 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
100 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
101 | 0 | "%s: unable to clear chunk data.", |
102 | 0 | function ); |
103 | |
|
104 | 0 | memory_free( |
105 | 0 | *chunk_data ); |
106 | |
|
107 | 0 | *chunk_data = NULL; |
108 | |
|
109 | 0 | return( -1 ); |
110 | 0 | } |
111 | 0 | ( *chunk_data )->data = (uint8_t *) memory_allocate( |
112 | 0 | sizeof( uint8_t ) * data_size ); |
113 | |
|
114 | 0 | if( ( *chunk_data )->data == NULL ) |
115 | 0 | { |
116 | 0 | libcerror_error_set( |
117 | 0 | error, |
118 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
119 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
120 | 0 | "%s: unable to create data.", |
121 | 0 | function ); |
122 | |
|
123 | 0 | goto on_error; |
124 | 0 | } |
125 | 0 | ( *chunk_data )->data_size = data_size; |
126 | |
|
127 | 0 | return( 1 ); |
128 | | |
129 | 0 | on_error: |
130 | 0 | if( *chunk_data != NULL ) |
131 | 0 | { |
132 | 0 | memory_free( |
133 | 0 | *chunk_data ); |
134 | |
|
135 | 0 | *chunk_data = NULL; |
136 | 0 | } |
137 | 0 | return( -1 ); |
138 | 0 | } |
139 | | |
140 | | /* Frees a chunk data |
141 | | * Returns 1 if successful or -1 on error |
142 | | */ |
143 | | int libvslvm_chunk_data_free( |
144 | | libvslvm_chunk_data_t **chunk_data, |
145 | | libcerror_error_t **error ) |
146 | 0 | { |
147 | 0 | static char *function = "libvslvm_chunk_data_free"; |
148 | 0 | int result = 1; |
149 | |
|
150 | 0 | if( chunk_data == NULL ) |
151 | 0 | { |
152 | 0 | libcerror_error_set( |
153 | 0 | error, |
154 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
155 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
156 | 0 | "%s: invalid chunk data.", |
157 | 0 | function ); |
158 | |
|
159 | 0 | return( -1 ); |
160 | 0 | } |
161 | 0 | if( *chunk_data != NULL ) |
162 | 0 | { |
163 | 0 | if( memory_set( |
164 | 0 | ( *chunk_data )->data, |
165 | 0 | 0, |
166 | 0 | ( *chunk_data )->data_size ) == NULL ) |
167 | 0 | { |
168 | 0 | libcerror_error_set( |
169 | 0 | error, |
170 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
171 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
172 | 0 | "%s: unable to clear data.", |
173 | 0 | function ); |
174 | |
|
175 | 0 | result = -1; |
176 | 0 | } |
177 | 0 | memory_free( |
178 | 0 | ( *chunk_data )->data ); |
179 | |
|
180 | 0 | memory_free( |
181 | 0 | *chunk_data ); |
182 | |
|
183 | 0 | *chunk_data = NULL; |
184 | 0 | } |
185 | 0 | return( result ); |
186 | 0 | } |
187 | | |
188 | | /* Reads chunk data |
189 | | * Returns 1 if successful or -1 on error |
190 | | */ |
191 | | int libvslvm_chunk_data_read_file_io_pool( |
192 | | libvslvm_chunk_data_t *chunk_data, |
193 | | libbfio_pool_t *file_io_pool, |
194 | | int file_io_pool_entry, |
195 | | off64_t chunk_offset, |
196 | | libcerror_error_t **error ) |
197 | 0 | { |
198 | 0 | static char *function = "libvslvm_chunk_data_read_file_io_pool"; |
199 | 0 | ssize_t read_count = 0; |
200 | |
|
201 | 0 | if( chunk_data == NULL ) |
202 | 0 | { |
203 | 0 | libcerror_error_set( |
204 | 0 | error, |
205 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
206 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
207 | 0 | "%s: invalid chunk data.", |
208 | 0 | function ); |
209 | |
|
210 | 0 | return( -1 ); |
211 | 0 | } |
212 | 0 | if( chunk_data->data == NULL ) |
213 | 0 | { |
214 | 0 | libcerror_error_set( |
215 | 0 | error, |
216 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
217 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, |
218 | 0 | "%s: invalid chunk data - missing data.", |
219 | 0 | function ); |
220 | |
|
221 | 0 | return( -1 ); |
222 | 0 | } |
223 | | #if defined( HAVE_DEBUG_OUTPUT ) |
224 | | if( libcnotify_verbose != 0 ) |
225 | | { |
226 | | libcnotify_printf( |
227 | | "%s: reading chunk data at offset: %" PRIi64 " (0x%08" PRIx64 ")\n", |
228 | | function, |
229 | | chunk_offset, |
230 | | chunk_offset ); |
231 | | } |
232 | | #endif |
233 | 0 | if( libbfio_pool_seek_offset( |
234 | 0 | file_io_pool, |
235 | 0 | file_io_pool_entry, |
236 | 0 | chunk_offset, |
237 | 0 | SEEK_SET, |
238 | 0 | error ) == -1 ) |
239 | 0 | { |
240 | 0 | libcerror_error_set( |
241 | 0 | error, |
242 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
243 | 0 | LIBCERROR_IO_ERROR_SEEK_FAILED, |
244 | 0 | "%s: unable to seek chunk data offset: %" PRIi64 " (0x%08" PRIx64 ").", |
245 | 0 | function, |
246 | 0 | chunk_offset, |
247 | 0 | chunk_offset ); |
248 | |
|
249 | 0 | return( -1 ); |
250 | 0 | } |
251 | 0 | read_count = libbfio_pool_read_buffer( |
252 | 0 | file_io_pool, |
253 | 0 | file_io_pool_entry, |
254 | 0 | chunk_data->data, |
255 | 0 | chunk_data->data_size, |
256 | 0 | error ); |
257 | |
|
258 | 0 | if( read_count != (ssize_t) chunk_data->data_size ) |
259 | 0 | { |
260 | 0 | libcerror_error_set( |
261 | 0 | error, |
262 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
263 | 0 | LIBCERROR_IO_ERROR_READ_FAILED, |
264 | 0 | "%s: unable to read chunk data.", |
265 | 0 | function ); |
266 | |
|
267 | 0 | return( -1 ); |
268 | 0 | } |
269 | | #if defined( HAVE_DEBUG_OUTPUT ) |
270 | | if( libcnotify_verbose != 0 ) |
271 | | { |
272 | | libcnotify_printf( |
273 | | "%s: chunk data:\n", |
274 | | function ); |
275 | | libcnotify_print_data( |
276 | | chunk_data->data, |
277 | | chunk_data->data_size, |
278 | | LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA ); |
279 | | } |
280 | | #endif |
281 | 0 | return( 1 ); |
282 | 0 | } |
283 | | |