/src/libfsapfs/libfsapfs/libfsapfs_data_block_vector.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * The data block vector functions |
3 | | * |
4 | | * Copyright (C) 2018-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 <types.h> |
24 | | |
25 | | #include "libfsapfs_file_extent.h" |
26 | | #include "libfsapfs_file_system_data_handle.h" |
27 | | #include "libfsapfs_io_handle.h" |
28 | | #include "libfsapfs_libcdata.h" |
29 | | #include "libfsapfs_libcerror.h" |
30 | | #include "libfsapfs_libfdata.h" |
31 | | |
32 | | /* Creates a data block vector |
33 | | * Make sure the value vector is referencing, is set to NULL |
34 | | * Returns 1 if successful or -1 on error |
35 | | */ |
36 | | int libfsapfs_data_block_vector_initialize( |
37 | | libfdata_vector_t **vector, |
38 | | libfsapfs_io_handle_t *io_handle, |
39 | | libfsapfs_file_system_data_handle_t *data_handle, |
40 | | libcdata_array_t *file_extents, |
41 | | uint8_t is_sparse, |
42 | | libcerror_error_t **error ) |
43 | 455 | { |
44 | 455 | libfdata_vector_t *safe_vector = NULL; |
45 | 455 | libfsapfs_file_extent_t *file_extent = NULL; |
46 | 455 | static char *function = "libfsapfs_data_block_vector_initialize"; |
47 | 455 | uint64_t logical_offset = 0; |
48 | 455 | uint32_t segment_flags = 0; |
49 | 455 | int extent_index = 0; |
50 | 455 | int number_of_extents = 0; |
51 | 455 | int segment_index = 0; |
52 | | |
53 | 455 | if( vector == NULL ) |
54 | 0 | { |
55 | 0 | libcerror_error_set( |
56 | 0 | error, |
57 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
58 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
59 | 0 | "%s: invalid vector.", |
60 | 0 | function ); |
61 | |
|
62 | 0 | return( -1 ); |
63 | 0 | } |
64 | 455 | if( *vector != NULL ) |
65 | 0 | { |
66 | 0 | libcerror_error_set( |
67 | 0 | error, |
68 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
69 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
70 | 0 | "%s: invalid vector value already set.", |
71 | 0 | function ); |
72 | |
|
73 | 0 | return( -1 ); |
74 | 0 | } |
75 | 455 | if( io_handle == NULL ) |
76 | 0 | { |
77 | 0 | libcerror_error_set( |
78 | 0 | error, |
79 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
80 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
81 | 0 | "%s: invalid IO handle.", |
82 | 0 | function ); |
83 | |
|
84 | 0 | return( -1 ); |
85 | 0 | } |
86 | 455 | if( data_handle == NULL ) |
87 | 0 | { |
88 | 0 | libcerror_error_set( |
89 | 0 | error, |
90 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
91 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
92 | 0 | "%s: invalid data handle.", |
93 | 0 | function ); |
94 | |
|
95 | 0 | return( -1 ); |
96 | 0 | } |
97 | 455 | if( libfdata_vector_initialize( |
98 | 455 | &safe_vector, |
99 | 455 | (size64_t) io_handle->block_size, |
100 | 455 | (intptr_t *) data_handle, |
101 | 455 | (int (*)(intptr_t **, libcerror_error_t **)) &libfsapfs_file_system_data_handle_free, |
102 | 455 | NULL, |
103 | 455 | (int (*)(intptr_t *, intptr_t *, libfdata_vector_t *, libfdata_cache_t *, int, int, off64_t, size64_t, uint32_t, uint8_t, libcerror_error_t **)) &libfsapfs_file_system_data_handle_read_data_block, |
104 | 455 | NULL, |
105 | 455 | LIBFDATA_DATA_HANDLE_FLAG_NON_MANAGED, |
106 | 455 | error ) != 1 ) |
107 | 0 | { |
108 | 0 | libcerror_error_set( |
109 | 0 | error, |
110 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
111 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
112 | 0 | "%s: unable to create data block vector.", |
113 | 0 | function ); |
114 | |
|
115 | 0 | goto on_error; |
116 | 0 | } |
117 | 455 | if( libcdata_array_get_number_of_entries( |
118 | 455 | file_extents, |
119 | 455 | &number_of_extents, |
120 | 455 | error ) != 1 ) |
121 | 0 | { |
122 | 0 | libcerror_error_set( |
123 | 0 | error, |
124 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
125 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
126 | 0 | "%s: unable to retrieve number of entries from array.", |
127 | 0 | function ); |
128 | |
|
129 | 0 | goto on_error; |
130 | 0 | } |
131 | 455 | for( extent_index = 0; |
132 | 725 | extent_index < number_of_extents; |
133 | 455 | extent_index++ ) |
134 | 455 | { |
135 | 455 | if( libcdata_array_get_entry_by_index( |
136 | 455 | file_extents, |
137 | 455 | extent_index, |
138 | 455 | (intptr_t **) &file_extent, |
139 | 455 | error ) != 1 ) |
140 | 0 | { |
141 | 0 | libcerror_error_set( |
142 | 0 | error, |
143 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
144 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
145 | 0 | "%s: unable to retrieve file extent: %d.", |
146 | 0 | function, |
147 | 0 | extent_index ); |
148 | |
|
149 | 0 | goto on_error; |
150 | 0 | } |
151 | 455 | if( file_extent == NULL ) |
152 | 0 | { |
153 | 0 | libcerror_error_set( |
154 | 0 | error, |
155 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
156 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, |
157 | 0 | "%s: missing file extent: %d.", |
158 | 0 | function, |
159 | 0 | extent_index ); |
160 | |
|
161 | 0 | goto on_error; |
162 | 0 | } |
163 | 455 | if( ( is_sparse == 0 ) |
164 | 455 | || ( file_extent->physical_block_number != 0 ) ) |
165 | 334 | { |
166 | 334 | if( file_extent->logical_offset != logical_offset ) |
167 | 133 | { |
168 | 133 | libcerror_error_set( |
169 | 133 | error, |
170 | 133 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
171 | 133 | LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS, |
172 | 133 | "%s: invalid file extent: %d - logical offset value out of bounds.", |
173 | 133 | function, |
174 | 133 | extent_index ); |
175 | | |
176 | 133 | goto on_error; |
177 | 133 | } |
178 | 334 | } |
179 | 322 | segment_flags = 0; |
180 | | |
181 | 322 | if( ( is_sparse != 0 ) |
182 | 322 | && ( file_extent->physical_block_number == 0 ) ) |
183 | 121 | { |
184 | 121 | segment_flags = LIBFDATA_RANGE_FLAG_IS_SPARSE; |
185 | 121 | } |
186 | 322 | if( libfdata_vector_append_segment( |
187 | 322 | safe_vector, |
188 | 322 | &segment_index, |
189 | 322 | extent_index, |
190 | 322 | file_extent->physical_block_number * io_handle->block_size, |
191 | 322 | file_extent->data_size, |
192 | 322 | segment_flags, |
193 | 322 | error ) != 1 ) |
194 | 52 | { |
195 | 52 | libcerror_error_set( |
196 | 52 | error, |
197 | 52 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
198 | 52 | LIBCERROR_RUNTIME_ERROR_APPEND_FAILED, |
199 | 52 | "%s: unable to append extent: %d as data block vector segment.", |
200 | 52 | function, |
201 | 52 | extent_index ); |
202 | | |
203 | 52 | goto on_error; |
204 | 52 | } |
205 | 270 | logical_offset += file_extent->data_size; |
206 | 270 | } |
207 | 270 | *vector = safe_vector; |
208 | | |
209 | 270 | return( 1 ); |
210 | | |
211 | 185 | on_error: |
212 | 185 | if( safe_vector != NULL ) |
213 | 185 | { |
214 | 185 | libfdata_vector_free( |
215 | 185 | &safe_vector, |
216 | 185 | NULL ); |
217 | 185 | } |
218 | 185 | return( -1 ); |
219 | 455 | } |
220 | | |