/src/libwrc/libwrc/libwrc_table_entry.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Message or string table entry functions |
3 | | * |
4 | | * Copyright (C) 2011-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 "libwrc_libcerror.h" |
27 | | #include "libwrc_libuna.h" |
28 | | #include "libwrc_table_entry.h" |
29 | | |
30 | | /* Creates a table entry |
31 | | * Make sure the value table_entry is referencing, is set to NULL |
32 | | * Returns 1 if successful or -1 on error |
33 | | */ |
34 | | int libwrc_table_entry_initialize( |
35 | | libwrc_table_entry_t **table_entry, |
36 | | libcerror_error_t **error ) |
37 | 2.10M | { |
38 | 2.10M | static char *function = "libwrc_table_entry_initialize"; |
39 | | |
40 | 2.10M | if( table_entry == 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 table entry.", |
47 | 0 | function ); |
48 | |
|
49 | 0 | return( -1 ); |
50 | 0 | } |
51 | 2.10M | if( *table_entry != 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 table entry value already set.", |
58 | 0 | function ); |
59 | |
|
60 | 0 | return( -1 ); |
61 | 0 | } |
62 | 2.10M | *table_entry = memory_allocate_structure( |
63 | 2.10M | libwrc_table_entry_t ); |
64 | | |
65 | 2.10M | if( *table_entry == 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 table entry.", |
72 | 0 | function ); |
73 | |
|
74 | 0 | goto on_error; |
75 | 0 | } |
76 | 2.10M | if( memory_set( |
77 | 2.10M | *table_entry, |
78 | 2.10M | 0, |
79 | 2.10M | sizeof( libwrc_table_entry_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 table entry.", |
86 | 0 | function ); |
87 | |
|
88 | 0 | goto on_error; |
89 | 0 | } |
90 | 2.10M | return( 1 ); |
91 | | |
92 | 0 | on_error: |
93 | 0 | if( *table_entry != NULL ) |
94 | 0 | { |
95 | 0 | memory_free( |
96 | 0 | *table_entry ); |
97 | |
|
98 | 0 | *table_entry = NULL; |
99 | 0 | } |
100 | 0 | return( -1 ); |
101 | 2.10M | } |
102 | | |
103 | | /* Frees a table entry |
104 | | * Returns 1 if successful or -1 on error |
105 | | */ |
106 | | int libwrc_table_entry_free( |
107 | | libwrc_table_entry_t **table_entry, |
108 | | libcerror_error_t **error ) |
109 | 2.10M | { |
110 | 2.10M | static char *function = "libwrc_table_entry_free"; |
111 | | |
112 | 2.10M | if( table_entry == 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 table entry.", |
119 | 0 | function ); |
120 | |
|
121 | 0 | return( -1 ); |
122 | 0 | } |
123 | 2.10M | if( *table_entry != NULL ) |
124 | 2.10M | { |
125 | 2.10M | if( ( *table_entry )->string != NULL ) |
126 | 2.10M | { |
127 | 2.10M | memory_free( |
128 | 2.10M | ( *table_entry )->string ); |
129 | 2.10M | } |
130 | 2.10M | memory_free( |
131 | 2.10M | *table_entry ); |
132 | | |
133 | 2.10M | *table_entry = NULL; |
134 | 2.10M | } |
135 | 2.10M | return( 1 ); |
136 | 2.10M | } |
137 | | |
138 | | /* Sets the string |
139 | | * Returns 1 if successful or -1 on error |
140 | | */ |
141 | | int libwrc_table_entry_set_string( |
142 | | libwrc_table_entry_t *table_entry, |
143 | | const uint8_t *string, |
144 | | size_t string_size, |
145 | | int codepage, |
146 | | libcerror_error_t **error ) |
147 | 2.10M | { |
148 | 2.10M | static char *function = "libwrc_table_entry_set_string"; |
149 | | |
150 | 2.10M | if( table_entry == NULL ) |
151 | 0 | { |
152 | 0 | libcerror_error_set( |
153 | 0 | error, |
154 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
155 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
156 | 0 | "%s: invalid table entry.", |
157 | 0 | function ); |
158 | |
|
159 | 0 | return( -1 ); |
160 | 0 | } |
161 | 2.10M | if( table_entry->string != NULL ) |
162 | 0 | { |
163 | 0 | libcerror_error_set( |
164 | 0 | error, |
165 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
166 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
167 | 0 | "%s: invalid table entry - string value already set.", |
168 | 0 | function ); |
169 | |
|
170 | 0 | return( -1 ); |
171 | 0 | } |
172 | 2.10M | if( string == NULL ) |
173 | 0 | { |
174 | 0 | libcerror_error_set( |
175 | 0 | error, |
176 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
177 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
178 | 0 | "%s: invalid string.", |
179 | 0 | function ); |
180 | |
|
181 | 0 | return( -1 ); |
182 | 0 | } |
183 | 2.10M | if( ( string_size == 0 ) |
184 | 2.10M | || ( string_size > (size_t) MEMORY_MAXIMUM_ALLOCATION_SIZE ) ) |
185 | 0 | { |
186 | 0 | libcerror_error_set( |
187 | 0 | error, |
188 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
189 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS, |
190 | 0 | "%s: invalid string size value out of bounds.", |
191 | 0 | function ); |
192 | |
|
193 | 0 | return( -1 ); |
194 | 0 | } |
195 | 2.10M | table_entry->string = (uint8_t *) memory_allocate( |
196 | 2.10M | sizeof( uint8_t ) * string_size ); |
197 | | |
198 | 2.10M | if( table_entry->string == NULL ) |
199 | 0 | { |
200 | 0 | libcerror_error_set( |
201 | 0 | error, |
202 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
203 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
204 | 0 | "%s: unable to create string.", |
205 | 0 | function ); |
206 | |
|
207 | 0 | goto on_error; |
208 | 0 | } |
209 | 2.10M | table_entry->string_size = string_size; |
210 | | |
211 | 2.10M | if( memory_copy( |
212 | 2.10M | table_entry->string, |
213 | 2.10M | string, |
214 | 2.10M | string_size ) == NULL ) |
215 | 0 | { |
216 | 0 | libcerror_error_set( |
217 | 0 | error, |
218 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
219 | 0 | LIBCERROR_MEMORY_ERROR_COPY_FAILED, |
220 | 0 | "%s: unable to copy data to string.", |
221 | 0 | function ); |
222 | |
|
223 | 0 | goto on_error; |
224 | 0 | } |
225 | 2.10M | table_entry->codepage = codepage; |
226 | | |
227 | 2.10M | return( 1 ); |
228 | | |
229 | 0 | on_error: |
230 | 0 | if( table_entry->string != NULL ) |
231 | 0 | { |
232 | 0 | memory_free( |
233 | 0 | table_entry->string ); |
234 | |
|
235 | 0 | table_entry->string = NULL; |
236 | 0 | } |
237 | 0 | table_entry->string_size = 0; |
238 | |
|
239 | 0 | return( -1 ); |
240 | 2.10M | } |
241 | | |
242 | | /* Retrieves the size of a specific UTF-8 formatted string |
243 | | * Returns 1 if successful or -1 on error |
244 | | */ |
245 | | int libwrc_table_entry_get_utf8_string_size( |
246 | | libwrc_table_entry_t *table_entry, |
247 | | size_t *utf8_string_size, |
248 | | libcerror_error_t **error ) |
249 | 0 | { |
250 | 0 | static char *function = "libwrc_table_entry_get_utf8_string_size"; |
251 | 0 | int result = 0; |
252 | |
|
253 | 0 | if( table_entry == NULL ) |
254 | 0 | { |
255 | 0 | libcerror_error_set( |
256 | 0 | error, |
257 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
258 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
259 | 0 | "%s: invalid manifest resource.", |
260 | 0 | function ); |
261 | |
|
262 | 0 | return( -1 ); |
263 | 0 | } |
264 | 0 | if( table_entry->codepage == LIBUNA_CODEPAGE_UTF16_LITTLE_ENDIAN ) |
265 | 0 | { |
266 | 0 | result = libuna_utf8_string_size_from_utf16_stream( |
267 | 0 | table_entry->string, |
268 | 0 | table_entry->string_size, |
269 | 0 | LIBUNA_ENDIAN_LITTLE, |
270 | 0 | utf8_string_size, |
271 | 0 | error ); |
272 | 0 | } |
273 | 0 | else |
274 | 0 | { |
275 | 0 | result = libuna_utf8_string_size_from_byte_stream( |
276 | 0 | table_entry->string, |
277 | 0 | table_entry->string_size, |
278 | 0 | table_entry->codepage, |
279 | 0 | utf8_string_size, |
280 | 0 | error ); |
281 | 0 | } |
282 | 0 | if( result != 1 ) |
283 | 0 | { |
284 | 0 | libcerror_error_set( |
285 | 0 | error, |
286 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
287 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
288 | 0 | "%s: unable to retrieve UTF-8 string size.", |
289 | 0 | function ); |
290 | |
|
291 | 0 | return( -1 ); |
292 | 0 | } |
293 | 0 | return( 1 ); |
294 | 0 | } |
295 | | |
296 | | /* Retrieves a specific UTF-8 formatted string |
297 | | * Returns 1 if successful or -1 on error |
298 | | */ |
299 | | int libwrc_table_entry_get_utf8_string( |
300 | | libwrc_table_entry_t *table_entry, |
301 | | uint8_t *utf8_string, |
302 | | size_t utf8_string_size, |
303 | | libcerror_error_t **error ) |
304 | 0 | { |
305 | 0 | static char *function = "libwrc_table_entry_get_utf8_string"; |
306 | 0 | int result = 0; |
307 | |
|
308 | 0 | if( table_entry == NULL ) |
309 | 0 | { |
310 | 0 | libcerror_error_set( |
311 | 0 | error, |
312 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
313 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
314 | 0 | "%s: invalid manifest resource.", |
315 | 0 | function ); |
316 | |
|
317 | 0 | return( -1 ); |
318 | 0 | } |
319 | 0 | if( table_entry->codepage == LIBUNA_CODEPAGE_UTF16_LITTLE_ENDIAN ) |
320 | 0 | { |
321 | 0 | result = libuna_utf8_string_copy_from_utf16_stream( |
322 | 0 | utf8_string, |
323 | 0 | utf8_string_size, |
324 | 0 | table_entry->string, |
325 | 0 | table_entry->string_size, |
326 | 0 | LIBUNA_ENDIAN_LITTLE, |
327 | 0 | error ); |
328 | 0 | } |
329 | 0 | else |
330 | 0 | { |
331 | 0 | result = libuna_utf8_string_copy_from_byte_stream( |
332 | 0 | utf8_string, |
333 | 0 | utf8_string_size, |
334 | 0 | table_entry->string, |
335 | 0 | table_entry->string_size, |
336 | 0 | table_entry->codepage, |
337 | 0 | error ); |
338 | 0 | } |
339 | 0 | if( result != 1 ) |
340 | 0 | { |
341 | 0 | libcerror_error_set( |
342 | 0 | error, |
343 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
344 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
345 | 0 | "%s: unable to retrieve UTF-8 string.", |
346 | 0 | function ); |
347 | |
|
348 | 0 | return( -1 ); |
349 | 0 | } |
350 | 0 | return( 1 ); |
351 | 0 | } |
352 | | |
353 | | /* Retrieves the size of a specific UTF-16 formatted string |
354 | | * Returns 1 if successful or -1 on error |
355 | | */ |
356 | | int libwrc_table_entry_get_utf16_string_size( |
357 | | libwrc_table_entry_t *table_entry, |
358 | | size_t *utf16_string_size, |
359 | | libcerror_error_t **error ) |
360 | 0 | { |
361 | 0 | static char *function = "libwrc_table_entry_get_utf16_string_size"; |
362 | 0 | int result = 0; |
363 | |
|
364 | 0 | if( table_entry == NULL ) |
365 | 0 | { |
366 | 0 | libcerror_error_set( |
367 | 0 | error, |
368 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
369 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
370 | 0 | "%s: invalid manifest resource.", |
371 | 0 | function ); |
372 | |
|
373 | 0 | return( -1 ); |
374 | 0 | } |
375 | 0 | if( table_entry->codepage == LIBUNA_CODEPAGE_UTF16_LITTLE_ENDIAN ) |
376 | 0 | { |
377 | 0 | result = libuna_utf16_string_size_from_utf16_stream( |
378 | 0 | table_entry->string, |
379 | 0 | table_entry->string_size, |
380 | 0 | LIBUNA_ENDIAN_LITTLE, |
381 | 0 | utf16_string_size, |
382 | 0 | error ); |
383 | 0 | } |
384 | 0 | else |
385 | 0 | { |
386 | 0 | result = libuna_utf16_string_size_from_byte_stream( |
387 | 0 | table_entry->string, |
388 | 0 | table_entry->string_size, |
389 | 0 | table_entry->codepage, |
390 | 0 | utf16_string_size, |
391 | 0 | error ); |
392 | 0 | } |
393 | 0 | if( result != 1 ) |
394 | 0 | { |
395 | 0 | libcerror_error_set( |
396 | 0 | error, |
397 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
398 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
399 | 0 | "%s: unable to retrieve UTF-16 string size.", |
400 | 0 | function ); |
401 | |
|
402 | 0 | return( -1 ); |
403 | 0 | } |
404 | 0 | return( 1 ); |
405 | 0 | } |
406 | | |
407 | | /* Retrieves a specific UTF-16 formatted string |
408 | | * Returns 1 if successful or -1 on error |
409 | | */ |
410 | | int libwrc_table_entry_get_utf16_string( |
411 | | libwrc_table_entry_t *table_entry, |
412 | | uint16_t *utf16_string, |
413 | | size_t utf16_string_size, |
414 | | libcerror_error_t **error ) |
415 | 0 | { |
416 | 0 | static char *function = "libwrc_table_entry_get_utf16_string"; |
417 | 0 | int result = 0; |
418 | |
|
419 | 0 | if( table_entry == NULL ) |
420 | 0 | { |
421 | 0 | libcerror_error_set( |
422 | 0 | error, |
423 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
424 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
425 | 0 | "%s: invalid manifest resource.", |
426 | 0 | function ); |
427 | |
|
428 | 0 | return( -1 ); |
429 | 0 | } |
430 | 0 | if( table_entry->codepage == LIBUNA_CODEPAGE_UTF16_LITTLE_ENDIAN ) |
431 | 0 | { |
432 | 0 | result = libuna_utf16_string_copy_from_utf16_stream( |
433 | 0 | utf16_string, |
434 | 0 | utf16_string_size, |
435 | 0 | table_entry->string, |
436 | 0 | table_entry->string_size, |
437 | 0 | LIBUNA_ENDIAN_LITTLE, |
438 | 0 | error ); |
439 | 0 | } |
440 | 0 | else |
441 | 0 | { |
442 | 0 | result = libuna_utf16_string_copy_from_byte_stream( |
443 | 0 | utf16_string, |
444 | 0 | utf16_string_size, |
445 | 0 | table_entry->string, |
446 | 0 | table_entry->string_size, |
447 | 0 | table_entry->codepage, |
448 | 0 | error ); |
449 | 0 | } |
450 | 0 | if( result != 1 ) |
451 | 0 | { |
452 | 0 | libcerror_error_set( |
453 | 0 | error, |
454 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
455 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
456 | 0 | "%s: unable to retrieve UTF-16 string.", |
457 | 0 | function ); |
458 | |
|
459 | 0 | return( -1 ); |
460 | 0 | } |
461 | 0 | return( 1 ); |
462 | 0 | } |
463 | | |