/src/libwrc/libwrc/libwrc_string_values.c
Line | Count | Source |
1 | | /* |
2 | | * String values functions |
3 | | * |
4 | | * Copyright (C) 2011-2025, 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_data_descriptor.h" |
27 | | #include "libwrc_definitions.h" |
28 | | #include "libwrc_io_handle.h" |
29 | | #include "libwrc_language_entry.h" |
30 | | #include "libwrc_libbfio.h" |
31 | | #include "libwrc_libcerror.h" |
32 | | #include "libwrc_libcnotify.h" |
33 | | #include "libwrc_libfvalue.h" |
34 | | #include "libwrc_string_values.h" |
35 | | |
36 | | /* Reads string values |
37 | | * Returns 1 if successful or -1 on error |
38 | | */ |
39 | | int libwrc_string_values_read( |
40 | | libwrc_language_entry_t *language_entry, |
41 | | libwrc_io_handle_t *io_handle, |
42 | | libbfio_handle_t *file_io_handle, |
43 | | uint32_t identifier, |
44 | | libwrc_data_descriptor_t *data_descriptor, |
45 | | libcerror_error_t **error ) |
46 | 0 | { |
47 | 0 | libfvalue_value_t *string_value = NULL; |
48 | 0 | uint8_t *resource_data = NULL; |
49 | 0 | uint8_t *string_resource_data = NULL; |
50 | 0 | static char *function = "libwrc_string_values_read"; |
51 | 0 | off64_t file_offset = 0; |
52 | 0 | size_t resource_data_size = 0; |
53 | 0 | ssize_t read_count = 0; |
54 | 0 | uint32_t string_identifier = 0; |
55 | 0 | uint32_t string_index = 0; |
56 | 0 | uint32_t string_size = 0; |
57 | 0 | int value_index = 0; |
58 | |
|
59 | 0 | if( language_entry == NULL ) |
60 | 0 | { |
61 | 0 | libcerror_error_set( |
62 | 0 | error, |
63 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
64 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
65 | 0 | "%s: invalid language entry.", |
66 | 0 | function ); |
67 | |
|
68 | 0 | return( -1 ); |
69 | 0 | } |
70 | 0 | if( io_handle == NULL ) |
71 | 0 | { |
72 | 0 | libcerror_error_set( |
73 | 0 | error, |
74 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
75 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
76 | 0 | "%s: invalid IO handle.", |
77 | 0 | function ); |
78 | |
|
79 | 0 | return( -1 ); |
80 | 0 | } |
81 | 0 | if( data_descriptor == NULL ) |
82 | 0 | { |
83 | 0 | libcerror_error_set( |
84 | 0 | error, |
85 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
86 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
87 | 0 | "%s: invalid data descriptor.", |
88 | 0 | function ); |
89 | |
|
90 | 0 | return( -1 ); |
91 | 0 | } |
92 | 0 | resource_data_size = (size_t) data_descriptor->size; |
93 | |
|
94 | 0 | if( ( resource_data_size == 0 ) |
95 | 0 | || ( resource_data_size > (size_t) MEMORY_MAXIMUM_ALLOCATION_SIZE ) ) |
96 | 0 | { |
97 | 0 | libcerror_error_set( |
98 | 0 | error, |
99 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
100 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
101 | 0 | "%s: invalid resource data size value out of bounds.", |
102 | 0 | function ); |
103 | |
|
104 | 0 | goto on_error; |
105 | 0 | } |
106 | 0 | resource_data = (uint8_t *) memory_allocate( |
107 | 0 | sizeof( uint8_t ) * resource_data_size ); |
108 | |
|
109 | 0 | if( resource_data == NULL ) |
110 | 0 | { |
111 | 0 | libcerror_error_set( |
112 | 0 | error, |
113 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
114 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
115 | 0 | "%s: unable to create resource data.", |
116 | 0 | function ); |
117 | |
|
118 | 0 | goto on_error; |
119 | 0 | } |
120 | 0 | file_offset = data_descriptor->virtual_address - io_handle->virtual_address; |
121 | |
|
122 | 0 | read_count = libbfio_handle_read_buffer_at_offset( |
123 | 0 | file_io_handle, |
124 | 0 | resource_data, |
125 | 0 | resource_data_size, |
126 | 0 | file_offset, |
127 | 0 | error ); |
128 | |
|
129 | 0 | if( read_count != (ssize_t) resource_data_size ) |
130 | 0 | { |
131 | 0 | libcerror_error_set( |
132 | 0 | error, |
133 | 0 | LIBCERROR_ERROR_DOMAIN_IO, |
134 | 0 | LIBCERROR_IO_ERROR_READ_FAILED, |
135 | 0 | "%s: unable to read resource data at offset: %" PRIi64 " (0x%08" PRIx64 ").", |
136 | 0 | function, |
137 | 0 | file_offset, |
138 | 0 | file_offset ); |
139 | |
|
140 | 0 | goto on_error; |
141 | 0 | } |
142 | 0 | string_resource_data = resource_data; |
143 | |
|
144 | | #if defined( HAVE_DEBUG_OUTPUT ) |
145 | | if( libcnotify_verbose != 0 ) |
146 | | { |
147 | | libcnotify_printf( |
148 | | "%s: resource data:\n", |
149 | | function ); |
150 | | libcnotify_print_data( |
151 | | string_resource_data, |
152 | | resource_data_size, |
153 | | 0 ); |
154 | | } |
155 | | #endif |
156 | 0 | while( resource_data_size > 0 ) |
157 | 0 | { |
158 | 0 | byte_stream_copy_to_uint16_little_endian( |
159 | 0 | string_resource_data, |
160 | 0 | string_size ); |
161 | |
|
162 | 0 | string_resource_data += sizeof( uint16_t ); |
163 | 0 | resource_data_size -= sizeof( uint16_t ); |
164 | |
|
165 | | #if defined( HAVE_DEBUG_OUTPUT ) |
166 | | if( libcnotify_verbose != 0 ) |
167 | | { |
168 | | libcnotify_printf( |
169 | | "%s: string: %02" PRIu32 " length\t\t\t\t: %" PRIu32 "\n", |
170 | | function, |
171 | | string_index, |
172 | | string_size ); |
173 | | } |
174 | | #endif |
175 | 0 | if( string_size > 0 ) |
176 | 0 | { |
177 | 0 | string_identifier = ( identifier << 4 ) | string_index; |
178 | |
|
179 | 0 | string_size *= 2; |
180 | |
|
181 | 0 | if( string_size > resource_data_size ) |
182 | 0 | { |
183 | 0 | libcerror_error_set( |
184 | 0 | error, |
185 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
186 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, |
187 | 0 | "%s: string size value out of bound.", |
188 | 0 | function ); |
189 | |
|
190 | 0 | goto on_error; |
191 | 0 | } |
192 | 0 | if( libfvalue_value_type_initialize( |
193 | 0 | &string_value, |
194 | 0 | LIBFVALUE_VALUE_TYPE_STRING_UTF16, |
195 | 0 | error ) != 1 ) |
196 | 0 | { |
197 | 0 | libcerror_error_set( |
198 | 0 | error, |
199 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
200 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
201 | 0 | "%s: unable to create string value.", |
202 | 0 | function ); |
203 | |
|
204 | 0 | goto on_error; |
205 | 0 | } |
206 | 0 | if( libfvalue_value_set_identifier( |
207 | 0 | string_value, |
208 | 0 | (uint8_t *) &string_identifier, |
209 | 0 | 4, |
210 | 0 | LIBFVALUE_VALUE_IDENTIFIER_FLAG_MANAGED, |
211 | 0 | error ) != 1 ) |
212 | 0 | { |
213 | 0 | libcerror_error_set( |
214 | 0 | error, |
215 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
216 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
217 | 0 | "%s: unable to set identifier of string value.", |
218 | 0 | function ); |
219 | |
|
220 | 0 | goto on_error; |
221 | 0 | } |
222 | | #if defined( HAVE_DEBUG_OUTPUT ) |
223 | | if( libcnotify_verbose != 0 ) |
224 | | { |
225 | | libcnotify_printf( |
226 | | "%s: string: %02" PRIu32 " data:\n", |
227 | | function, |
228 | | string_index ); |
229 | | libcnotify_print_data( |
230 | | string_resource_data, |
231 | | (size_t) string_size, |
232 | | 0 ); |
233 | | } |
234 | | #endif |
235 | 0 | if( libfvalue_value_set_data( |
236 | 0 | string_value, |
237 | 0 | string_resource_data, |
238 | 0 | (size_t) string_size, |
239 | 0 | LIBFVALUE_CODEPAGE_UTF16_LITTLE_ENDIAN, |
240 | 0 | LIBFVALUE_VALUE_DATA_FLAG_MANAGED, |
241 | 0 | error ) != 1 ) |
242 | 0 | { |
243 | 0 | libcerror_error_set( |
244 | 0 | error, |
245 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
246 | 0 | LIBCERROR_RUNTIME_ERROR_SET_FAILED, |
247 | 0 | "%s: unable to set data of string value.", |
248 | 0 | function ); |
249 | |
|
250 | 0 | goto on_error; |
251 | 0 | } |
252 | | #if defined( HAVE_DEBUG_OUTPUT ) |
253 | | if( libcnotify_verbose != 0 ) |
254 | | { |
255 | | libcnotify_printf( |
256 | | "%s: string: %02" PRIu32 " value\t\t\t\t: ", |
257 | | function, |
258 | | string_index ); |
259 | | |
260 | | if( libfvalue_value_print( |
261 | | string_value, |
262 | | 0, |
263 | | 0, |
264 | | error ) != 1 ) |
265 | | { |
266 | | libcerror_error_set( |
267 | | error, |
268 | | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
269 | | LIBCERROR_RUNTIME_ERROR_PRINT_FAILED, |
270 | | "%s: unable to print string value.", |
271 | | function ); |
272 | | |
273 | | goto on_error; |
274 | | } |
275 | | libcnotify_printf( |
276 | | "\n" ); |
277 | | } |
278 | | #endif |
279 | 0 | string_resource_data += (size_t) string_size; |
280 | 0 | resource_data_size -= (size_t) string_size; |
281 | |
|
282 | 0 | if( libwrc_language_entry_append_value( |
283 | 0 | language_entry, |
284 | 0 | &value_index, |
285 | 0 | (intptr_t *) string_value, |
286 | 0 | error ) != 1 ) |
287 | 0 | { |
288 | 0 | libcerror_error_set( |
289 | 0 | error, |
290 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
291 | 0 | LIBCERROR_RUNTIME_ERROR_APPEND_FAILED, |
292 | 0 | "%s: unable to append string value.", |
293 | 0 | function ); |
294 | |
|
295 | 0 | goto on_error; |
296 | 0 | } |
297 | 0 | string_value = NULL; |
298 | 0 | } |
299 | | #if defined( HAVE_DEBUG_OUTPUT ) |
300 | | else if( libcnotify_verbose != 0 ) |
301 | | { |
302 | | libcnotify_printf( |
303 | | "\n" ); |
304 | | } |
305 | | #endif |
306 | 0 | string_index++; |
307 | 0 | } |
308 | 0 | memory_free( |
309 | 0 | resource_data ); |
310 | | |
311 | | /* TODO validate if number of strings is 16 ? */ |
312 | |
|
313 | 0 | return( 1 ); |
314 | | |
315 | 0 | on_error: |
316 | 0 | if( string_value != NULL ) |
317 | 0 | { |
318 | 0 | libfvalue_value_free( |
319 | 0 | &string_value, |
320 | 0 | NULL ); |
321 | 0 | } |
322 | 0 | if( resource_data != NULL ) |
323 | 0 | { |
324 | 0 | memory_free( |
325 | 0 | resource_data ); |
326 | 0 | } |
327 | 0 | return( -1 ); |
328 | 0 | } |
329 | | |