/src/libqcow/libqcow/libqcow_cluster_block.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Cluster block functions |
3 | | * |
4 | | * Copyright (C) 2010-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 "libqcow_cluster_block.h" |
28 | | #include "libqcow_definitions.h" |
29 | | #include "libqcow_libbfio.h" |
30 | | #include "libqcow_libcerror.h" |
31 | | #include "libqcow_libcnotify.h" |
32 | | |
33 | | /* Creates a cluster block |
34 | | * Make sure the value cluster_block is referencing, is set to NULL |
35 | | * Returns 1 if successful or -1 on error |
36 | | */ |
37 | | int libqcow_cluster_block_initialize( |
38 | | libqcow_cluster_block_t **cluster_block, |
39 | | size_t data_size, |
40 | | libcerror_error_t **error ) |
41 | 0 | { |
42 | 0 | static char *function = "libqcow_cluster_block_initialize"; |
43 | |
|
44 | 0 | if( cluster_block == 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 cluster block.", |
51 | 0 | function ); |
52 | |
|
53 | 0 | return( -1 ); |
54 | 0 | } |
55 | 0 | if( *cluster_block != 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 cluster block 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 | *cluster_block = memory_allocate_structure( |
79 | 0 | libqcow_cluster_block_t ); |
80 | |
|
81 | 0 | if( *cluster_block == 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 cluster block.", |
88 | 0 | function ); |
89 | |
|
90 | 0 | goto on_error; |
91 | 0 | } |
92 | 0 | if( memory_set( |
93 | 0 | *cluster_block, |
94 | 0 | 0, |
95 | 0 | sizeof( libqcow_cluster_block_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 cluster block.", |
102 | 0 | function ); |
103 | |
|
104 | 0 | memory_free( |
105 | 0 | *cluster_block ); |
106 | |
|
107 | 0 | *cluster_block = NULL; |
108 | |
|
109 | 0 | return( -1 ); |
110 | 0 | } |
111 | 0 | ( *cluster_block )->data = (uint8_t *) memory_allocate( |
112 | 0 | sizeof( uint8_t ) * data_size ); |
113 | |
|
114 | 0 | if( ( *cluster_block )->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 | ( *cluster_block )->data_size = data_size; |
126 | |
|
127 | 0 | return( 1 ); |
128 | | |
129 | 0 | on_error: |
130 | 0 | if( *cluster_block != NULL ) |
131 | 0 | { |
132 | 0 | memory_free( |
133 | 0 | *cluster_block ); |
134 | |
|
135 | 0 | *cluster_block = NULL; |
136 | 0 | } |
137 | 0 | return( -1 ); |
138 | 0 | } |
139 | | |
140 | | /* Frees a cluster block |
141 | | * Returns 1 if successful or -1 on error |
142 | | */ |
143 | | int libqcow_cluster_block_free( |
144 | | libqcow_cluster_block_t **cluster_block, |
145 | | libcerror_error_t **error ) |
146 | 0 | { |
147 | 0 | static char *function = "libqcow_cluster_block_free"; |
148 | 0 | int result = 1; |
149 | |
|
150 | 0 | if( cluster_block == 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 cluster block.", |
157 | 0 | function ); |
158 | |
|
159 | 0 | return( -1 ); |
160 | 0 | } |
161 | 0 | if( *cluster_block != NULL ) |
162 | 0 | { |
163 | 0 | if( ( *cluster_block )->compressed_data != NULL ) |
164 | 0 | { |
165 | 0 | memory_free( |
166 | 0 | ( *cluster_block )->compressed_data ); |
167 | 0 | } |
168 | 0 | if( ( *cluster_block )->encrypted_data != NULL ) |
169 | 0 | { |
170 | 0 | memory_free( |
171 | 0 | ( *cluster_block )->encrypted_data ); |
172 | 0 | } |
173 | 0 | if( ( *cluster_block )->data != NULL ) |
174 | 0 | { |
175 | 0 | if( memory_set( |
176 | 0 | ( *cluster_block )->data, |
177 | 0 | 0, |
178 | 0 | ( *cluster_block )->data_size ) == NULL ) |
179 | 0 | { |
180 | 0 | libcerror_error_set( |
181 | 0 | error, |
182 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
183 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
184 | 0 | "%s: unable to clear data.", |
185 | 0 | function ); |
186 | |
|
187 | 0 | result = -1; |
188 | 0 | } |
189 | 0 | memory_free( |
190 | 0 | ( *cluster_block )->data ); |
191 | 0 | } |
192 | 0 | memory_free( |
193 | 0 | *cluster_block ); |
194 | |
|
195 | 0 | *cluster_block = NULL; |
196 | 0 | } |
197 | 0 | return( result ); |
198 | 0 | } |
199 | | |
200 | | /* Reads cluster block |
201 | | * Returns 1 if successful or -1 on error |
202 | | */ |
203 | | int libqcow_cluster_block_read( |
204 | | libqcow_cluster_block_t *cluster_block, |
205 | | libbfio_handle_t *file_io_handle, |
206 | | off64_t cluster_block_offset, |
207 | | libcerror_error_t **error ) |
208 | 0 | { |
209 | 0 | static char *function = "libqcow_cluster_block_read"; |
210 | 0 | ssize_t read_count = 0; |
211 | |
|
212 | 0 | if( cluster_block == NULL ) |
213 | 0 | { |
214 | 0 | libcerror_error_set( |
215 | 0 | error, |
216 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
217 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
218 | 0 | "%s: invalid cluster block.", |
219 | 0 | function ); |
220 | |
|
221 | 0 | return( -1 ); |
222 | 0 | } |
223 | 0 | if( cluster_block->data == NULL ) |
224 | 0 | { |
225 | 0 | libcerror_error_set( |
226 | 0 | error, |
227 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
228 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, |
229 | 0 | "%s: invalid cluster block - missing data.", |
230 | 0 | function ); |
231 | |
|
232 | 0 | return( -1 ); |
233 | 0 | } |
234 | | #if defined( HAVE_DEBUG_OUTPUT ) |
235 | | if( libcnotify_verbose != 0 ) |
236 | | { |
237 | | libcnotify_printf( |
238 | | "%s: reading cluster block at offset: %" PRIi64 " (0x%08" PRIx64 ")\n", |
239 | | function, |
240 | | cluster_block_offset, |
241 | | cluster_block_offset ); |
242 | | } |
243 | | #endif |
244 | 0 | read_count = libbfio_handle_read_buffer_at_offset( |
245 | 0 | file_io_handle, |
246 | 0 | cluster_block->data, |
247 | 0 | cluster_block->data_size, |
248 | 0 | cluster_block_offset, |
249 | 0 | error ); |
250 | |
|
251 | 0 | if( read_count != (ssize_t) cluster_block->data_size ) |
252 | 0 | { |
253 | 0 | libcerror_error_set( |
254 | 0 | error, |
255 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
256 | 0 | LIBCERROR_IO_ERROR_READ_FAILED, |
257 | 0 | "%s: unable to read cluster block at offset: %" PRIi64 " (0x%08" PRIx64 ").", |
258 | 0 | function, |
259 | 0 | cluster_block_offset, |
260 | 0 | cluster_block_offset ); |
261 | |
|
262 | 0 | return( -1 ); |
263 | 0 | } |
264 | | #if defined( HAVE_DEBUG_OUTPUT ) |
265 | | if( libcnotify_verbose != 0 ) |
266 | | { |
267 | | libcnotify_printf( |
268 | | "%s: cluster block:\n", |
269 | | function ); |
270 | | libcnotify_print_data( |
271 | | cluster_block->data, |
272 | | cluster_block->data_size, |
273 | | LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA ); |
274 | | } |
275 | | #endif |
276 | 0 | return( 1 ); |
277 | 0 | } |
278 | | |