Debian Codegraph @

libfdata/libfdata_range.c

raw symbols SCIP
1 /*
2 * The range functions
3 *
4 * Copyright (C) 2010-2024, Joachim Metz <[email protected]>
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 "libfdata_libcerror.h"
27 #include "libfdata_range.h"
28
29 /* Creates a range
30 * Make sure the value range is referencing, is set to NULL
31 * Returns 1 if successful or -1 on error
32 */
33 int libfdata_range_initialize(
34 libfdata_range_t **range,
35 libcerror_error_t **error )
36 {
37 static char *function = "libfdata_range_initialize";
38
39 if( range == NULL )
40 {
41 libcerror_error_set(
42 error,
43 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
44 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
45 "%s: invalid range.",
46 function );
47
48 return( -1 );
49 }
50 if( *range != NULL )
51 {
52 libcerror_error_set(
53 error,
54 LIBCERROR_ERROR_DOMAIN_RUNTIME,
55 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
56 "%s: invalid range value already set.",
57 function );
58
59 return( -1 );
60 }
61 *range = memory_allocate_structure(
62 libfdata_range_t );
63
64 if( *range == NULL )
65 {
66 libcerror_error_set(
67 error,
68 LIBCERROR_ERROR_DOMAIN_MEMORY,
69 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
70 "%s: unable to create range.",
71 function );
72
73 goto on_error;
74 }
75 if( memory_set(
76 *range,
77 0,
78 sizeof( libfdata_range_t ) ) == NULL )
79 {
80 libcerror_error_set(
81 error,
82 LIBCERROR_ERROR_DOMAIN_MEMORY,
83 LIBCERROR_MEMORY_ERROR_SET_FAILED,
84 "%s: unable to clear range.",
85 function );
86
87 goto on_error;
88 }
89 ( *range )->file_index = -1;
90 ( *range )->offset = (off64_t) -1;
91
92 return( 1 );
93
94 on_error:
95 if( *range != NULL )
96 {
97 memory_free(
98 *range );
99
100 *range = NULL;
101 }
102 return( -1 );
103 }
104
105 /* Frees a range
106 * Returns 1 if successful or -1 on error
107 */
108 int libfdata_range_free(
109 libfdata_range_t **range,
110 libcerror_error_t **error )
111 {
112 static char *function = "libfdata_range_free";
113
114 if( range == NULL )
115 {
116 libcerror_error_set(
117 error,
118 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
119 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
120 "%s: invalid range.",
121 function );
122
123 return( -1 );
124 }
125 if( *range != NULL )
126 {
127 memory_free(
128 *range );
129
130 *range = NULL;
131 }
132 return( 1 );
133 }
134
135 /* Clones (duplicates) the range
136 * Returns 1 if successful or -1 on error
137 */
138 int libfdata_range_clone(
139 libfdata_range_t **destination_range,
140 libfdata_range_t *source_range,
141 libcerror_error_t **error )
142 {
143 static char *function = "libfdata_range_clone";
144
145 if( destination_range == NULL )
146 {
147 libcerror_error_set(
148 error,
149 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
150 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
151 "%s: invalid destination range.",
152 function );
153
154 return( -1 );
155 }
156 if( *destination_range != NULL )
157 {
158 libcerror_error_set(
159 error,
160 LIBCERROR_ERROR_DOMAIN_RUNTIME,
161 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
162 "%s: invalid destination range value already set.",
163 function );
164
165 return( -1 );
166 }
167 if( source_range == NULL )
168 {
169 *destination_range = NULL;
170
171 return( 1 );
172 }
173 *destination_range = memory_allocate_structure(
174 libfdata_range_t );
175
176 if( *destination_range == NULL )
177 {
178 libcerror_error_set(
179 error,
180 LIBCERROR_ERROR_DOMAIN_MEMORY,
181 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
182 "%s: unable to create destination range.",
183 function );
184
185 goto on_error;
186 }
187 if( memory_copy(
188 *destination_range,
189 source_range,
190 sizeof( libfdata_range_t ) ) == NULL )
191 {
192 libcerror_error_set(
193 error,
194 LIBCERROR_ERROR_DOMAIN_MEMORY,
195 LIBCERROR_MEMORY_ERROR_COPY_FAILED,
196 "%s: unable to copy source to destination range.",
197 function );
198
199 goto on_error;
200 }
201 return( 1 );
202
203 on_error:
204 if( *destination_range != NULL )
205 {
206 memory_free(
207 *destination_range );
208
209 *destination_range = NULL;
210 }
211 return( -1 );
212 }
213
214 /* Retrieves the range values
215 * Returns 1 if successful or -1 on error
216 */
217 int libfdata_range_get(
218 libfdata_range_t *range,
219 int *file_index,
220 off64_t *offset,
221 size64_t *size,
222 uint32_t *flags,
223 libcerror_error_t **error )
224 {
225 static char *function = "libfdata_range_get";
226
227 if( range == NULL )
228 {
229 libcerror_error_set(
230 error,
231 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
232 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
233 "%s: invalid range.",
234 function );
235
236 return( -1 );
237 }
238 if( file_index == NULL )
239 {
240 libcerror_error_set(
241 error,
242 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
243 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
244 "%s: invalid file index.",
245 function );
246
247 return( -1 );
248 }
249 if( offset == NULL )
250 {
251 libcerror_error_set(
252 error,
253 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
254 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
255 "%s: invalid offset.",
256 function );
257
258 return( -1 );
259 }
260 if( size == NULL )
261 {
262 libcerror_error_set(
263 error,
264 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
265 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
266 "%s: invalid size.",
267 function );
268
269 return( -1 );
270 }
271 if( flags == NULL )
272 {
273 libcerror_error_set(
274 error,
275 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
276 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
277 "%s: invalid flags.",
278 function );
279
280 return( -1 );
281 }
282 *file_index = range->file_index;
283 *offset = range->offset;
284 *size = range->size;
285 *flags = range->flags;
286
287 return( 1 );
288 }
289
290 /* Retrieves the size
291 * Returns 1 if successful or -1 on error
292 */
293 int libfdata_range_get_size(
294 libfdata_range_t *range,
295 size64_t *size,
296 libcerror_error_t **error )
297 {
298 static char *function = "libfdata_range_get_size";
299
300 if( range == NULL )
301 {
302 libcerror_error_set(
303 error,
304 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
305 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
306 "%s: invalid range.",
307 function );
308
309 return( -1 );
310 }
311 if( size == NULL )
312 {
313 libcerror_error_set(
314 error,
315 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
316 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
317 "%s: invalid size.",
318 function );
319
320 return( -1 );
321 }
322 *size = range->size;
323
324 return( 1 );
325 }
326
327 /* Sets the range values
328 * Returns 1 if successful or -1 on error
329 */
330 int libfdata_range_set(
331 libfdata_range_t *range,
332 int file_index,
333 off64_t offset,
334 size64_t size,
335 uint32_t flags,
336 libcerror_error_t **error )
337 {
338 static char *function = "libfdata_range_set";
339
340 if( range == NULL )
341 {
342 libcerror_error_set(
343 error,
344 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
345 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
346 "%s: invalid range.",
347 function );
348
349 return( -1 );
350 }
351 range->file_index = file_index;
352 range->offset = offset;
353 range->size = size;
354 range->flags = flags;
355
356 return( 1 );
357 }
358