/src/libewf/libewf/libewf_single_file_tree.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Single file tree functions |
3 | | * |
4 | | * Copyright (C) 2006-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 "libewf_lef_file_entry.h" |
26 | | #include "libewf_libcdata.h" |
27 | | #include "libewf_libcerror.h" |
28 | | #include "libewf_libuna.h" |
29 | | #include "libewf_single_file_tree.h" |
30 | | |
31 | | /* Retrieves the file entry sub node for the specific UTF-8 formatted name |
32 | | * Returns 1 if successful, 0 in no such value or -1 on error |
33 | | */ |
34 | | int libewf_single_file_tree_get_sub_node_by_utf8_name( |
35 | | libcdata_tree_node_t *node, |
36 | | const uint8_t *utf8_string, |
37 | | size_t utf8_string_length, |
38 | | libcdata_tree_node_t **sub_node, |
39 | | libewf_lef_file_entry_t **sub_lef_file_entry, |
40 | | libcerror_error_t **error ) |
41 | 0 | { |
42 | 0 | libcdata_tree_node_t *safe_sub_node = NULL; |
43 | 0 | libewf_lef_file_entry_t *safe_sub_lef_file_entry = NULL; |
44 | 0 | static char *function = "libewf_single_file_tree_get_sub_node_by_utf8_name"; |
45 | 0 | int compare_result = LIBUNA_COMPARE_GREATER; |
46 | 0 | int number_of_sub_nodes = 0; |
47 | 0 | int result = 0; |
48 | 0 | int sub_node_index = 0; |
49 | |
|
50 | 0 | if( node == NULL ) |
51 | 0 | { |
52 | 0 | libcerror_error_set( |
53 | 0 | error, |
54 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
55 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
56 | 0 | "%s: invalid node.", |
57 | 0 | function ); |
58 | |
|
59 | 0 | return( -1 ); |
60 | 0 | } |
61 | 0 | if( sub_node == NULL ) |
62 | 0 | { |
63 | 0 | libcerror_error_set( |
64 | 0 | error, |
65 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
66 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
67 | 0 | "%s: invalid sub node.", |
68 | 0 | function ); |
69 | |
|
70 | 0 | return( -1 ); |
71 | 0 | } |
72 | 0 | if( sub_lef_file_entry == NULL ) |
73 | 0 | { |
74 | 0 | libcerror_error_set( |
75 | 0 | error, |
76 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
77 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
78 | 0 | "%s: invalid sub file entry.", |
79 | 0 | function ); |
80 | |
|
81 | 0 | return( -1 ); |
82 | 0 | } |
83 | 0 | *sub_node = NULL; |
84 | 0 | *sub_lef_file_entry = NULL; |
85 | |
|
86 | 0 | if( libcdata_tree_node_get_number_of_sub_nodes( |
87 | 0 | node, |
88 | 0 | &number_of_sub_nodes, |
89 | 0 | error ) != 1 ) |
90 | 0 | { |
91 | 0 | libcerror_error_set( |
92 | 0 | error, |
93 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
94 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
95 | 0 | "%s: unable to retrieve number of sub nodes.", |
96 | 0 | function ); |
97 | |
|
98 | 0 | return( -1 ); |
99 | 0 | } |
100 | 0 | if( libcdata_tree_node_get_sub_node_by_index( |
101 | 0 | node, |
102 | 0 | 0, |
103 | 0 | &safe_sub_node, |
104 | 0 | error ) != 1 ) |
105 | 0 | { |
106 | 0 | libcerror_error_set( |
107 | 0 | error, |
108 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
109 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
110 | 0 | "%s: unable to retrieve first sub node.", |
111 | 0 | function ); |
112 | |
|
113 | 0 | return( -1 ); |
114 | 0 | } |
115 | 0 | for( sub_node_index = 0; |
116 | 0 | sub_node_index < number_of_sub_nodes; |
117 | 0 | sub_node_index++ ) |
118 | 0 | { |
119 | 0 | if( libcdata_tree_node_get_value( |
120 | 0 | safe_sub_node, |
121 | 0 | (intptr_t **) &safe_sub_lef_file_entry, |
122 | 0 | error ) != 1 ) |
123 | 0 | { |
124 | 0 | libcerror_error_set( |
125 | 0 | error, |
126 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
127 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
128 | 0 | "%s: unable to retrieve value from sub node: %d.", |
129 | 0 | function, |
130 | 0 | sub_node_index ); |
131 | |
|
132 | 0 | return( -1 ); |
133 | 0 | } |
134 | 0 | compare_result = libewf_lef_file_entry_compare_name_with_utf8_string( |
135 | 0 | safe_sub_lef_file_entry, |
136 | 0 | utf8_string, |
137 | 0 | utf8_string_length, |
138 | 0 | error ); |
139 | |
|
140 | 0 | if( compare_result == -1 ) |
141 | 0 | { |
142 | 0 | libcerror_error_set( |
143 | 0 | error, |
144 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
145 | 0 | LIBCERROR_RUNTIME_ERROR_GENERIC, |
146 | 0 | "%s: unable to compare sub file entry: %d name with UTF-8 string.", |
147 | 0 | function, |
148 | 0 | sub_node_index ); |
149 | |
|
150 | 0 | return( -1 ); |
151 | 0 | } |
152 | 0 | else if( compare_result == LIBUNA_COMPARE_EQUAL ) |
153 | 0 | { |
154 | 0 | *sub_node = safe_sub_node; |
155 | 0 | *sub_lef_file_entry = safe_sub_lef_file_entry; |
156 | |
|
157 | 0 | result = 1; |
158 | |
|
159 | 0 | break; |
160 | 0 | } |
161 | 0 | if( libcdata_tree_node_get_next_node( |
162 | 0 | safe_sub_node, |
163 | 0 | &safe_sub_node, |
164 | 0 | error ) != 1 ) |
165 | 0 | { |
166 | 0 | libcerror_error_set( |
167 | 0 | error, |
168 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
169 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
170 | 0 | "%s: unable to retrieve next node from sub node: %d.", |
171 | 0 | function, |
172 | 0 | sub_node_index ); |
173 | |
|
174 | 0 | return( -1 ); |
175 | 0 | } |
176 | 0 | } |
177 | 0 | return( result ); |
178 | 0 | } |
179 | | |
180 | | /* Retrieves the file entry sub node for the specific UTF-16 formatted name |
181 | | * Returns 1 if successful, 0 in no such value or -1 on error |
182 | | */ |
183 | | int libewf_single_file_tree_get_sub_node_by_utf16_name( |
184 | | libcdata_tree_node_t *node, |
185 | | const uint16_t *utf16_string, |
186 | | size_t utf16_string_length, |
187 | | libcdata_tree_node_t **sub_node, |
188 | | libewf_lef_file_entry_t **sub_lef_file_entry, |
189 | | libcerror_error_t **error ) |
190 | 0 | { |
191 | 0 | libcdata_tree_node_t *safe_sub_node = NULL; |
192 | 0 | libewf_lef_file_entry_t *safe_sub_lef_file_entry = NULL; |
193 | 0 | static char *function = "libewf_single_file_tree_get_sub_node_by_utf16_name"; |
194 | 0 | int compare_result = LIBUNA_COMPARE_GREATER; |
195 | 0 | int number_of_sub_nodes = 0; |
196 | 0 | int result = 0; |
197 | 0 | int sub_node_index = 0; |
198 | |
|
199 | 0 | if( node == NULL ) |
200 | 0 | { |
201 | 0 | libcerror_error_set( |
202 | 0 | error, |
203 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
204 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
205 | 0 | "%s: invalid node.", |
206 | 0 | function ); |
207 | |
|
208 | 0 | return( -1 ); |
209 | 0 | } |
210 | 0 | if( sub_node == NULL ) |
211 | 0 | { |
212 | 0 | libcerror_error_set( |
213 | 0 | error, |
214 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
215 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
216 | 0 | "%s: invalid sub node.", |
217 | 0 | function ); |
218 | |
|
219 | 0 | return( -1 ); |
220 | 0 | } |
221 | 0 | if( sub_lef_file_entry == NULL ) |
222 | 0 | { |
223 | 0 | libcerror_error_set( |
224 | 0 | error, |
225 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
226 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
227 | 0 | "%s: invalid sub file entry.", |
228 | 0 | function ); |
229 | |
|
230 | 0 | return( -1 ); |
231 | 0 | } |
232 | 0 | *sub_node = NULL; |
233 | 0 | *sub_lef_file_entry = NULL; |
234 | |
|
235 | 0 | if( libcdata_tree_node_get_number_of_sub_nodes( |
236 | 0 | node, |
237 | 0 | &number_of_sub_nodes, |
238 | 0 | error ) != 1 ) |
239 | 0 | { |
240 | 0 | libcerror_error_set( |
241 | 0 | error, |
242 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
243 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
244 | 0 | "%s: unable to retrieve number of sub nodes.", |
245 | 0 | function ); |
246 | |
|
247 | 0 | return( -1 ); |
248 | 0 | } |
249 | 0 | if( libcdata_tree_node_get_sub_node_by_index( |
250 | 0 | node, |
251 | 0 | 0, |
252 | 0 | &safe_sub_node, |
253 | 0 | error ) != 1 ) |
254 | 0 | { |
255 | 0 | libcerror_error_set( |
256 | 0 | error, |
257 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
258 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
259 | 0 | "%s: unable to retrieve first sub node.", |
260 | 0 | function ); |
261 | |
|
262 | 0 | return( -1 ); |
263 | 0 | } |
264 | 0 | for( sub_node_index = 0; |
265 | 0 | sub_node_index < number_of_sub_nodes; |
266 | 0 | sub_node_index++ ) |
267 | 0 | { |
268 | 0 | if( libcdata_tree_node_get_value( |
269 | 0 | safe_sub_node, |
270 | 0 | (intptr_t **) &safe_sub_lef_file_entry, |
271 | 0 | error ) != 1 ) |
272 | 0 | { |
273 | 0 | libcerror_error_set( |
274 | 0 | error, |
275 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
276 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
277 | 0 | "%s: unable to retrieve value from sub node: %d.", |
278 | 0 | function, |
279 | 0 | sub_node_index ); |
280 | |
|
281 | 0 | return( -1 ); |
282 | 0 | } |
283 | 0 | compare_result = libewf_lef_file_entry_compare_name_with_utf16_string( |
284 | 0 | safe_sub_lef_file_entry, |
285 | 0 | utf16_string, |
286 | 0 | utf16_string_length, |
287 | 0 | error ); |
288 | |
|
289 | 0 | if( compare_result == -1 ) |
290 | 0 | { |
291 | 0 | libcerror_error_set( |
292 | 0 | error, |
293 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
294 | 0 | LIBCERROR_RUNTIME_ERROR_GENERIC, |
295 | 0 | "%s: unable to compare sub file entry: %d name with UTF-16 string.", |
296 | 0 | function, |
297 | 0 | sub_node_index ); |
298 | |
|
299 | 0 | return( -1 ); |
300 | 0 | } |
301 | 0 | else if( compare_result == LIBUNA_COMPARE_EQUAL ) |
302 | 0 | { |
303 | 0 | *sub_node = safe_sub_node; |
304 | 0 | *sub_lef_file_entry = safe_sub_lef_file_entry; |
305 | |
|
306 | 0 | result = 1; |
307 | |
|
308 | 0 | break; |
309 | 0 | } |
310 | 0 | if( libcdata_tree_node_get_next_node( |
311 | 0 | safe_sub_node, |
312 | 0 | &safe_sub_node, |
313 | 0 | error ) != 1 ) |
314 | 0 | { |
315 | 0 | libcerror_error_set( |
316 | 0 | error, |
317 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
318 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
319 | 0 | "%s: unable to retrieve next node from sub node: %d.", |
320 | 0 | function, |
321 | 0 | sub_node_index ); |
322 | |
|
323 | 0 | return( -1 ); |
324 | 0 | } |
325 | 0 | } |
326 | 0 | return( result ); |
327 | 0 | } |
328 | | |