/src/libpff/libpff/libpff_table_block_index.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Table block index functions |
3 | | * |
4 | | * Copyright (C) 2008-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 "libpff_libcdata.h" |
27 | | #include "libpff_libcerror.h" |
28 | | #include "libpff_table_block_index.h" |
29 | | #include "libpff_table_index_value.h" |
30 | | |
31 | | /* Creates table block index |
32 | | * Make sure the value table_block_index is referencing, is set to NULL |
33 | | * Returns 1 if successful or -1 on error |
34 | | */ |
35 | | int libpff_table_block_index_initialize( |
36 | | libpff_table_block_index_t **table_block_index, |
37 | | libcerror_error_t **error ) |
38 | 4.65k | { |
39 | 4.65k | static char *function = "libpff_table_block_index_initialize"; |
40 | | |
41 | 4.65k | if( table_block_index == NULL ) |
42 | 0 | { |
43 | 0 | libcerror_error_set( |
44 | 0 | error, |
45 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
46 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
47 | 0 | "%s: invalid table block index.", |
48 | 0 | function ); |
49 | |
|
50 | 0 | return( -1 ); |
51 | 0 | } |
52 | 4.65k | if( *table_block_index != NULL ) |
53 | 0 | { |
54 | 0 | libcerror_error_set( |
55 | 0 | error, |
56 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
57 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
58 | 0 | "%s: invalid table block index value already set.", |
59 | 0 | function ); |
60 | |
|
61 | 0 | return( -1 ); |
62 | 0 | } |
63 | 4.65k | *table_block_index = memory_allocate_structure( |
64 | 4.65k | libpff_table_block_index_t ); |
65 | | |
66 | 4.65k | if( *table_block_index == NULL ) |
67 | 0 | { |
68 | 0 | libcerror_error_set( |
69 | 0 | error, |
70 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
71 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
72 | 0 | "%s: unable to create table block index.", |
73 | 0 | function ); |
74 | |
|
75 | 0 | goto on_error; |
76 | 0 | } |
77 | 4.65k | if( memory_set( |
78 | 4.65k | *table_block_index, |
79 | 4.65k | 0, |
80 | 4.65k | sizeof( libpff_table_block_index_t ) ) == NULL ) |
81 | 0 | { |
82 | 0 | libcerror_error_set( |
83 | 0 | error, |
84 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
85 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
86 | 0 | "%s: unable to clear table block index.", |
87 | 0 | function ); |
88 | |
|
89 | 0 | memory_free( |
90 | 0 | *table_block_index ); |
91 | |
|
92 | 0 | *table_block_index = NULL; |
93 | |
|
94 | 0 | return( -1 ); |
95 | 0 | } |
96 | 4.65k | if( libcdata_array_initialize( |
97 | 4.65k | &( ( *table_block_index )->values_array ), |
98 | 4.65k | 0, |
99 | 4.65k | error ) != 1 ) |
100 | 0 | { |
101 | 0 | libcerror_error_set( |
102 | 0 | error, |
103 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
104 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
105 | 0 | "%s: unable to create values array.", |
106 | 0 | function ); |
107 | |
|
108 | 0 | goto on_error; |
109 | 0 | } |
110 | 4.65k | return( 1 ); |
111 | | |
112 | 0 | on_error: |
113 | 0 | if( *table_block_index != NULL ) |
114 | 0 | { |
115 | 0 | memory_free( |
116 | 0 | *table_block_index ); |
117 | |
|
118 | 0 | *table_block_index = NULL; |
119 | 0 | } |
120 | 0 | return( -1 ); |
121 | 4.65k | } |
122 | | |
123 | | /* Frees table block index |
124 | | * Returns 1 if successful or -1 on error |
125 | | */ |
126 | | int libpff_table_block_index_free( |
127 | | libpff_table_block_index_t **table_block_index, |
128 | | libcerror_error_t **error ) |
129 | 4.65k | { |
130 | 4.65k | static char *function = "libpff_table_block_index_free"; |
131 | 4.65k | int result = 1; |
132 | | |
133 | 4.65k | if( table_block_index == NULL ) |
134 | 0 | { |
135 | 0 | libcerror_error_set( |
136 | 0 | error, |
137 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
138 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
139 | 0 | "%s: invalid table block index.", |
140 | 0 | function ); |
141 | |
|
142 | 0 | return( -1 ); |
143 | 0 | } |
144 | 4.65k | if( *table_block_index != NULL ) |
145 | 4.65k | { |
146 | 4.65k | if( libcdata_array_free( |
147 | 4.65k | &( ( *table_block_index )->values_array ), |
148 | 4.65k | (int (*)(intptr_t **, libcerror_error_t **)) &libpff_table_index_value_free, |
149 | 4.65k | error ) != 1 ) |
150 | 0 | { |
151 | 0 | libcerror_error_set( |
152 | 0 | error, |
153 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
154 | 0 | LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, |
155 | 0 | "%s: unable to free values array.", |
156 | 0 | function ); |
157 | |
|
158 | 0 | result = -1; |
159 | 0 | } |
160 | 4.65k | memory_free( |
161 | 4.65k | *table_block_index ); |
162 | | |
163 | 4.65k | *table_block_index = NULL; |
164 | 4.65k | } |
165 | 4.65k | return( result ); |
166 | 4.65k | } |
167 | | |
168 | | /* Retrieves the number of values |
169 | | * Returns 1 if successful or -1 on error |
170 | | */ |
171 | | int libpff_table_block_index_get_number_of_values( |
172 | | libpff_table_block_index_t *table_block_index, |
173 | | uint16_t *number_of_values, |
174 | | libcerror_error_t **error ) |
175 | 162 | { |
176 | 162 | static char *function = "libpff_table_block_index_get_number_of_values"; |
177 | 162 | int number_of_entries = 0; |
178 | | |
179 | 162 | if( table_block_index == 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 table block index.", |
186 | 0 | function ); |
187 | |
|
188 | 0 | return( -1 ); |
189 | 0 | } |
190 | 162 | if( number_of_values == 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 number of values.", |
197 | 0 | function ); |
198 | |
|
199 | 0 | return( -1 ); |
200 | 0 | } |
201 | 162 | if( libcdata_array_get_number_of_entries( |
202 | 162 | table_block_index->values_array, |
203 | 162 | &number_of_entries, |
204 | 162 | error ) != 1 ) |
205 | 0 | { |
206 | 0 | libcerror_error_set( |
207 | 0 | error, |
208 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
209 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
210 | 0 | "%s: unable to retrieve number of entries.", |
211 | 0 | function ); |
212 | |
|
213 | 0 | return( -1 ); |
214 | 0 | } |
215 | 162 | if( ( number_of_entries < 0 ) |
216 | 162 | || ( number_of_entries > (int) UINT16_MAX ) ) |
217 | 0 | { |
218 | 0 | libcerror_error_set( |
219 | 0 | error, |
220 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
221 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS, |
222 | 0 | "%s: invalid number of entries value out of bounds.", |
223 | 0 | function ); |
224 | |
|
225 | 0 | return( -1 ); |
226 | 0 | } |
227 | 162 | *number_of_values = (uint16_t) number_of_entries; |
228 | | |
229 | 162 | return( 1 ); |
230 | 162 | } |
231 | | |
232 | | /* Retrieves a specific table index value |
233 | | * Returns 1 if successful or -1 on error |
234 | | */ |
235 | | int libpff_table_block_index_get_value_by_index( |
236 | | libpff_table_block_index_t *table_block_index, |
237 | | uint16_t value_index, |
238 | | libpff_table_index_value_t **table_index_value, |
239 | | libcerror_error_t **error ) |
240 | 238k | { |
241 | 238k | static char *function = "libpff_table_block_index_get_value_by_index"; |
242 | | |
243 | 238k | if( table_block_index == NULL ) |
244 | 0 | { |
245 | 0 | libcerror_error_set( |
246 | 0 | error, |
247 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
248 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
249 | 0 | "%s: invalid table block index.", |
250 | 0 | function ); |
251 | |
|
252 | 0 | return( -1 ); |
253 | 0 | } |
254 | 238k | if( libcdata_array_get_entry_by_index( |
255 | 238k | table_block_index->values_array, |
256 | 238k | (int) value_index, |
257 | 238k | (intptr_t **) table_index_value, |
258 | 238k | error ) != 1 ) |
259 | 6.19k | { |
260 | 6.19k | libcerror_error_set( |
261 | 6.19k | error, |
262 | 6.19k | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
263 | 6.19k | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
264 | 6.19k | "%s: unable to retrieve entry: %" PRIu16 ".", |
265 | 6.19k | function, |
266 | 6.19k | value_index ); |
267 | | |
268 | 6.19k | return( -1 ); |
269 | 6.19k | } |
270 | 232k | return( 1 ); |
271 | 238k | } |
272 | | |
273 | | /* Appends a table index value |
274 | | * Returns 1 if successful or -1 on error |
275 | | */ |
276 | | int libpff_table_block_index_append_value( |
277 | | libpff_table_block_index_t *table_block_index, |
278 | | uint16_t *value_index, |
279 | | libpff_table_index_value_t *table_index_value, |
280 | | libcerror_error_t **error ) |
281 | 104k | { |
282 | 104k | static char *function = "libpff_table_block_index_append_value"; |
283 | 104k | int entry_index = 0; |
284 | | |
285 | 104k | if( table_block_index == NULL ) |
286 | 0 | { |
287 | 0 | libcerror_error_set( |
288 | 0 | error, |
289 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
290 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
291 | 0 | "%s: invalid table block index.", |
292 | 0 | function ); |
293 | |
|
294 | 0 | return( -1 ); |
295 | 0 | } |
296 | 104k | if( value_index == NULL ) |
297 | 0 | { |
298 | 0 | libcerror_error_set( |
299 | 0 | error, |
300 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
301 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
302 | 0 | "%s: invalid value index.", |
303 | 0 | function ); |
304 | |
|
305 | 0 | return( -1 ); |
306 | 0 | } |
307 | 104k | if( libcdata_array_append_entry( |
308 | 104k | table_block_index->values_array, |
309 | 104k | &entry_index, |
310 | 104k | (intptr_t *) table_index_value, |
311 | 104k | error ) != 1 ) |
312 | 0 | { |
313 | 0 | libcerror_error_set( |
314 | 0 | error, |
315 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
316 | 0 | LIBCERROR_RUNTIME_ERROR_APPEND_FAILED, |
317 | 0 | "%s: unable to append table index value to array.", |
318 | 0 | function ); |
319 | |
|
320 | 0 | return( -1 ); |
321 | 0 | } |
322 | 104k | if( ( entry_index < 0 ) |
323 | 104k | || ( entry_index > (int) UINT16_MAX ) ) |
324 | 0 | { |
325 | 0 | libcerror_error_set( |
326 | 0 | error, |
327 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
328 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS, |
329 | 0 | "%s: invalid entry index value out of bounds.", |
330 | 0 | function ); |
331 | |
|
332 | 0 | return( -1 ); |
333 | 0 | } |
334 | 104k | *value_index = (uint16_t) entry_index; |
335 | | |
336 | 104k | return( 1 ); |
337 | 104k | } |
338 | | |