/src/libfshfs/libfshfs/libfshfs_extent.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Extent 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 <memory.h> |
24 | | #include <types.h> |
25 | | |
26 | | #include "libfshfs_extent.h" |
27 | | #include "libfshfs_io_handle.h" |
28 | | #include "libfshfs_libcerror.h" |
29 | | |
30 | | /* Creates an extent |
31 | | * Make sure the value extent is referencing, is set to NULL |
32 | | * Returns 1 if successful or -1 on error |
33 | | */ |
34 | | int libfshfs_extent_initialize( |
35 | | libfshfs_extent_t **extent, |
36 | | libcerror_error_t **error ) |
37 | 58.1k | { |
38 | 58.1k | static char *function = "libfshfs_extent_initialize"; |
39 | | |
40 | 58.1k | if( extent == NULL ) |
41 | 0 | { |
42 | 0 | libcerror_error_set( |
43 | 0 | error, |
44 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
45 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
46 | 0 | "%s: invalid extent.", |
47 | 0 | function ); |
48 | |
|
49 | 0 | return( -1 ); |
50 | 0 | } |
51 | 58.1k | if( *extent != NULL ) |
52 | 0 | { |
53 | 0 | libcerror_error_set( |
54 | 0 | error, |
55 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
56 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
57 | 0 | "%s: invalid extent value already set.", |
58 | 0 | function ); |
59 | |
|
60 | 0 | return( -1 ); |
61 | 0 | } |
62 | 58.1k | *extent = memory_allocate_structure( |
63 | 58.1k | libfshfs_extent_t ); |
64 | | |
65 | 58.1k | if( *extent == NULL ) |
66 | 0 | { |
67 | 0 | libcerror_error_set( |
68 | 0 | error, |
69 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
70 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
71 | 0 | "%s: unable to create extent.", |
72 | 0 | function ); |
73 | |
|
74 | 0 | goto on_error; |
75 | 0 | } |
76 | 58.1k | if( memory_set( |
77 | 58.1k | *extent, |
78 | 58.1k | 0, |
79 | 58.1k | sizeof( libfshfs_extent_t ) ) == NULL ) |
80 | 0 | { |
81 | 0 | libcerror_error_set( |
82 | 0 | error, |
83 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
84 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
85 | 0 | "%s: unable to clear extent.", |
86 | 0 | function ); |
87 | |
|
88 | 0 | goto on_error; |
89 | 0 | } |
90 | 58.1k | return( 1 ); |
91 | | |
92 | 0 | on_error: |
93 | 0 | if( *extent != NULL ) |
94 | 0 | { |
95 | 0 | memory_free( |
96 | 0 | *extent ); |
97 | |
|
98 | 0 | *extent = NULL; |
99 | 0 | } |
100 | 0 | return( -1 ); |
101 | 58.1k | } |
102 | | |
103 | | /* Frees an extent |
104 | | * Returns 1 if successful or -1 on error |
105 | | */ |
106 | | int libfshfs_extent_free( |
107 | | libfshfs_extent_t **extent, |
108 | | libcerror_error_t **error ) |
109 | 58.1k | { |
110 | 58.1k | static char *function = "libfshfs_extent_free"; |
111 | | |
112 | 58.1k | if( extent == NULL ) |
113 | 0 | { |
114 | 0 | libcerror_error_set( |
115 | 0 | error, |
116 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
117 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
118 | 0 | "%s: invalid extent.", |
119 | 0 | function ); |
120 | |
|
121 | 0 | return( -1 ); |
122 | 0 | } |
123 | 58.1k | if( *extent != NULL ) |
124 | 58.1k | { |
125 | 58.1k | memory_free( |
126 | 58.1k | *extent ); |
127 | | |
128 | 58.1k | *extent = NULL; |
129 | 58.1k | } |
130 | 58.1k | return( 1 ); |
131 | 58.1k | } |
132 | | |
133 | | /* Retrieves the extents values |
134 | | * Returns 1 if successful or -1 on error |
135 | | */ |
136 | | int libfshfs_extent_get_values( |
137 | | libfshfs_extent_t *extent, |
138 | | libfshfs_io_handle_t *io_handle, |
139 | | off64_t *extent_offset, |
140 | | size64_t *extent_size, |
141 | | uint32_t *extent_flags, |
142 | | libcerror_error_t **error ) |
143 | 0 | { |
144 | 0 | static char *function = "libfshfs_extent_get_values"; |
145 | |
|
146 | 0 | if( extent == NULL ) |
147 | 0 | { |
148 | 0 | libcerror_error_set( |
149 | 0 | error, |
150 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
151 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
152 | 0 | "%s: invalid extent.", |
153 | 0 | function ); |
154 | |
|
155 | 0 | return( -1 ); |
156 | 0 | } |
157 | 0 | if( io_handle == NULL ) |
158 | 0 | { |
159 | 0 | libcerror_error_set( |
160 | 0 | error, |
161 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
162 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
163 | 0 | "%s: invalid IO handle.", |
164 | 0 | function ); |
165 | |
|
166 | 0 | return( -1 ); |
167 | 0 | } |
168 | 0 | if( io_handle->block_size == 0 ) |
169 | 0 | { |
170 | 0 | libcerror_error_set( |
171 | 0 | error, |
172 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
173 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
174 | 0 | "%s: invalid IO handle - block size value out of bounds.", |
175 | 0 | function ); |
176 | |
|
177 | 0 | return( -1 ); |
178 | 0 | } |
179 | 0 | if( extent_offset == NULL ) |
180 | 0 | { |
181 | 0 | libcerror_error_set( |
182 | 0 | error, |
183 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
184 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
185 | 0 | "%s: invalid extent offset.", |
186 | 0 | function ); |
187 | |
|
188 | 0 | return( -1 ); |
189 | 0 | } |
190 | 0 | if( extent_size == NULL ) |
191 | 0 | { |
192 | 0 | libcerror_error_set( |
193 | 0 | error, |
194 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
195 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
196 | 0 | "%s: invalid extent size.", |
197 | 0 | function ); |
198 | |
|
199 | 0 | return( -1 ); |
200 | 0 | } |
201 | 0 | if( extent_flags == 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 extent flags.", |
208 | 0 | function ); |
209 | |
|
210 | 0 | return( -1 ); |
211 | 0 | } |
212 | 0 | if( extent->block_number > ( (uint64_t) INT64_MAX / io_handle->block_size ) ) |
213 | 0 | { |
214 | 0 | libcerror_error_set( |
215 | 0 | error, |
216 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
217 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
218 | 0 | "%s: invalid extent - invalid block number value out of bounds.", |
219 | 0 | function ); |
220 | |
|
221 | 0 | return( -1 ); |
222 | 0 | } |
223 | 0 | if( extent->number_of_blocks > ( (uint64_t) UINT64_MAX / io_handle->block_size ) ) |
224 | 0 | { |
225 | 0 | libcerror_error_set( |
226 | 0 | error, |
227 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
228 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
229 | 0 | "%s: invalid extent - invalid number of blocks value out of bounds.", |
230 | 0 | function ); |
231 | |
|
232 | 0 | return( -1 ); |
233 | 0 | } |
234 | 0 | *extent_offset = (off64_t) extent->block_number * (off64_t) io_handle->block_size; |
235 | 0 | *extent_size = (size64_t) extent->number_of_blocks * (size64_t) io_handle->block_size; |
236 | 0 | *extent_flags = 0; |
237 | |
|
238 | 0 | return( 1 ); |
239 | 0 | } |
240 | | |