/src/libewf/libewf/libewf_sector_range.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Sector range functions |
3 | | * |
4 | | * Copyright (C) 2006-2023, 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 "libewf_libcerror.h" |
27 | | #include "libewf_sector_range.h" |
28 | | |
29 | | /* Creates a sector range |
30 | | * Make sure the value sector_range is referencing, is set to NULL |
31 | | * Returns 1 if successful or -1 on error |
32 | | */ |
33 | | int libewf_sector_range_initialize( |
34 | | libewf_sector_range_t **sector_range, |
35 | | libcerror_error_t **error ) |
36 | 190 | { |
37 | 190 | static char *function = "libewf_sector_range_initialize"; |
38 | | |
39 | 190 | if( sector_range == NULL ) |
40 | 0 | { |
41 | 0 | libcerror_error_set( |
42 | 0 | error, |
43 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
44 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
45 | 0 | "%s: invalid sector range.", |
46 | 0 | function ); |
47 | |
|
48 | 0 | return( -1 ); |
49 | 0 | } |
50 | 190 | if( *sector_range != NULL ) |
51 | 0 | { |
52 | 0 | libcerror_error_set( |
53 | 0 | error, |
54 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
55 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
56 | 0 | "%s: invalid sector range value already set.", |
57 | 0 | function ); |
58 | |
|
59 | 0 | return( -1 ); |
60 | 0 | } |
61 | 190 | *sector_range = memory_allocate_structure( |
62 | 190 | libewf_sector_range_t ); |
63 | | |
64 | 190 | if( *sector_range == NULL ) |
65 | 0 | { |
66 | 0 | libcerror_error_set( |
67 | 0 | error, |
68 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
69 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
70 | 0 | "%s: unable to create sector range.", |
71 | 0 | function ); |
72 | |
|
73 | 0 | goto on_error; |
74 | 0 | } |
75 | 190 | if( memory_set( |
76 | 190 | *sector_range, |
77 | 190 | 0, |
78 | 190 | sizeof( libewf_sector_range_t ) ) == NULL ) |
79 | 0 | { |
80 | 0 | libcerror_error_set( |
81 | 0 | error, |
82 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
83 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
84 | 0 | "%s: unable to clear sector range.", |
85 | 0 | function ); |
86 | |
|
87 | 0 | goto on_error; |
88 | 0 | } |
89 | 190 | return( 1 ); |
90 | | |
91 | 0 | on_error: |
92 | 0 | if( *sector_range != NULL ) |
93 | 0 | { |
94 | 0 | memory_free( |
95 | 0 | *sector_range ); |
96 | |
|
97 | 0 | *sector_range = NULL; |
98 | 0 | } |
99 | 0 | return( -1 ); |
100 | 190 | } |
101 | | |
102 | | /* Frees a sector range |
103 | | * Returns 1 if successful or -1 on error |
104 | | */ |
105 | | int libewf_sector_range_free( |
106 | | libewf_sector_range_t **sector_range, |
107 | | libcerror_error_t **error ) |
108 | 190 | { |
109 | 190 | static char *function = "libewf_sector_range_free"; |
110 | | |
111 | 190 | if( sector_range == NULL ) |
112 | 0 | { |
113 | 0 | libcerror_error_set( |
114 | 0 | error, |
115 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
116 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
117 | 0 | "%s: invalid sector range.", |
118 | 0 | function ); |
119 | |
|
120 | 0 | return( -1 ); |
121 | 0 | } |
122 | 190 | if( *sector_range != NULL ) |
123 | 190 | { |
124 | 190 | memory_free( |
125 | 190 | *sector_range ); |
126 | | |
127 | 190 | *sector_range = NULL; |
128 | 190 | } |
129 | 190 | return( 1 ); |
130 | 190 | } |
131 | | |
132 | | /* Clones the sector range |
133 | | * Returns 1 if successful or -1 on error |
134 | | */ |
135 | | int libewf_sector_range_clone( |
136 | | libewf_sector_range_t **destination_sector_range, |
137 | | libewf_sector_range_t *source_sector_range, |
138 | | libcerror_error_t **error ) |
139 | 0 | { |
140 | 0 | static char *function = "libewf_sector_range_clone"; |
141 | |
|
142 | 0 | if( destination_sector_range == NULL ) |
143 | 0 | { |
144 | 0 | libcerror_error_set( |
145 | 0 | error, |
146 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
147 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
148 | 0 | "%s: invalid destination sector range.", |
149 | 0 | function ); |
150 | |
|
151 | 0 | return( -1 ); |
152 | 0 | } |
153 | 0 | if( *destination_sector_range != NULL ) |
154 | 0 | { |
155 | 0 | libcerror_error_set( |
156 | 0 | error, |
157 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
158 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
159 | 0 | "%s: invalid destination sector range already set.", |
160 | 0 | function ); |
161 | |
|
162 | 0 | return( -1 ); |
163 | 0 | } |
164 | 0 | if( source_sector_range == NULL ) |
165 | 0 | { |
166 | 0 | *destination_sector_range = NULL; |
167 | |
|
168 | 0 | return( 1 ); |
169 | 0 | } |
170 | 0 | *destination_sector_range = memory_allocate_structure( |
171 | 0 | libewf_sector_range_t ); |
172 | |
|
173 | 0 | if( *destination_sector_range == NULL ) |
174 | 0 | { |
175 | 0 | libcerror_error_set( |
176 | 0 | error, |
177 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
178 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
179 | 0 | "%s: unable to create destination sector range.", |
180 | 0 | function ); |
181 | |
|
182 | 0 | goto on_error; |
183 | 0 | } |
184 | 0 | if( memory_copy( |
185 | 0 | *destination_sector_range, |
186 | 0 | source_sector_range, |
187 | 0 | sizeof( libewf_sector_range_t ) ) == NULL ) |
188 | 0 | { |
189 | 0 | libcerror_error_set( |
190 | 0 | error, |
191 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
192 | 0 | LIBCERROR_MEMORY_ERROR_COPY_FAILED, |
193 | 0 | "%s: unable to copy source to destination sector range.", |
194 | 0 | function ); |
195 | |
|
196 | 0 | goto on_error; |
197 | 0 | } |
198 | 0 | return( 1 ); |
199 | | |
200 | 0 | on_error: |
201 | 0 | if( *destination_sector_range != NULL ) |
202 | 0 | { |
203 | 0 | memory_free( |
204 | 0 | *destination_sector_range ); |
205 | |
|
206 | 0 | *destination_sector_range = NULL; |
207 | 0 | } |
208 | 0 | return( -1 ); |
209 | 0 | } |
210 | | |
211 | | /* Retrieves a sector range |
212 | | * Returns 1 if successful or -1 on error |
213 | | */ |
214 | | int libewf_sector_range_get( |
215 | | libewf_sector_range_t *sector_range, |
216 | | uint64_t *start_sector, |
217 | | uint64_t *number_of_sectors, |
218 | | libcerror_error_t **error ) |
219 | 0 | { |
220 | 0 | static char *function = "libewf_sector_range_get"; |
221 | |
|
222 | 0 | if( sector_range == NULL ) |
223 | 0 | { |
224 | 0 | libcerror_error_set( |
225 | 0 | error, |
226 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
227 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
228 | 0 | "%s: invalid sector range.", |
229 | 0 | function ); |
230 | |
|
231 | 0 | return( -1 ); |
232 | 0 | } |
233 | 0 | if( start_sector == NULL ) |
234 | 0 | { |
235 | 0 | libcerror_error_set( |
236 | 0 | error, |
237 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
238 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
239 | 0 | "%s: invalid start sector.", |
240 | 0 | function ); |
241 | |
|
242 | 0 | return( -1 ); |
243 | 0 | } |
244 | 0 | if( number_of_sectors == NULL ) |
245 | 0 | { |
246 | 0 | libcerror_error_set( |
247 | 0 | error, |
248 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
249 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
250 | 0 | "%s: invalid number of sectors.", |
251 | 0 | function ); |
252 | |
|
253 | 0 | return( -1 ); |
254 | 0 | } |
255 | 0 | *start_sector = sector_range->start_sector; |
256 | 0 | *number_of_sectors = sector_range->number_of_sectors; |
257 | |
|
258 | 0 | return( 1 ); |
259 | 0 | } |
260 | | |
261 | | /* Sets a sector range |
262 | | * Returns 1 if successful or -1 on error |
263 | | */ |
264 | | int libewf_sector_range_set( |
265 | | libewf_sector_range_t *sector_range, |
266 | | uint64_t start_sector, |
267 | | uint64_t number_of_sectors, |
268 | | libcerror_error_t **error ) |
269 | 190 | { |
270 | 190 | static char *function = "libewf_sector_range_set"; |
271 | | |
272 | 190 | if( sector_range == NULL ) |
273 | 0 | { |
274 | 0 | libcerror_error_set( |
275 | 0 | error, |
276 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
277 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
278 | 0 | "%s: invalid sector range.", |
279 | 0 | function ); |
280 | |
|
281 | 0 | return( -1 ); |
282 | 0 | } |
283 | 190 | if( start_sector > (uint64_t) INT64_MAX ) |
284 | 0 | { |
285 | 0 | libcerror_error_set( |
286 | 0 | error, |
287 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
288 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
289 | 0 | "%s: invalid start sector value exceeds maximum.", |
290 | 0 | function ); |
291 | |
|
292 | 0 | return( -1 ); |
293 | 0 | } |
294 | 190 | if( number_of_sectors > (uint64_t) INT64_MAX ) |
295 | 0 | { |
296 | 0 | libcerror_error_set( |
297 | 0 | error, |
298 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
299 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
300 | 0 | "%s: invalid number of sectors value exceeds maximum.", |
301 | 0 | function ); |
302 | |
|
303 | 0 | return( -1 ); |
304 | 0 | } |
305 | 190 | sector_range->start_sector = start_sector; |
306 | 190 | sector_range->end_sector = start_sector + number_of_sectors; |
307 | 190 | sector_range->number_of_sectors = number_of_sectors; |
308 | | |
309 | 190 | return( 1 ); |
310 | 190 | } |
311 | | |