/src/libwrc/libwrc/libwrc_language_entry.c
Line | Count | Source |
1 | | /* |
2 | | * Language (table) entry 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_definitions.h" |
27 | | #include "libwrc_language_entry.h" |
28 | | #include "libwrc_libcdata.h" |
29 | | #include "libwrc_libcerror.h" |
30 | | #include "libwrc_libcnotify.h" |
31 | | |
32 | | /* Creates a language entry |
33 | | * Make sure the value language_entry is referencing, is set to NULL |
34 | | * Returns 1 if successful or -1 on error |
35 | | */ |
36 | | int libwrc_language_entry_initialize( |
37 | | libwrc_language_entry_t **language_entry, |
38 | | uint32_t language_identifier, |
39 | | int (*value_free_function)( |
40 | | intptr_t **value, |
41 | | libcerror_error_t **error ), |
42 | | libcerror_error_t **error ) |
43 | 0 | { |
44 | 0 | static char *function = "libwrc_language_entry_initialize"; |
45 | |
|
46 | 0 | if( language_entry == NULL ) |
47 | 0 | { |
48 | 0 | libcerror_error_set( |
49 | 0 | error, |
50 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
51 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
52 | 0 | "%s: invalid language entry.", |
53 | 0 | function ); |
54 | |
|
55 | 0 | return( -1 ); |
56 | 0 | } |
57 | 0 | if( *language_entry != NULL ) |
58 | 0 | { |
59 | 0 | libcerror_error_set( |
60 | 0 | error, |
61 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
62 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
63 | 0 | "%s: invalid language entry value already set.", |
64 | 0 | function ); |
65 | |
|
66 | 0 | return( -1 ); |
67 | 0 | } |
68 | 0 | if( value_free_function == NULL ) |
69 | 0 | { |
70 | 0 | libcerror_error_set( |
71 | 0 | error, |
72 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
73 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
74 | 0 | "%s: invalid value free function.", |
75 | 0 | function ); |
76 | |
|
77 | 0 | return( -1 ); |
78 | 0 | } |
79 | 0 | *language_entry = memory_allocate_structure( |
80 | 0 | libwrc_language_entry_t ); |
81 | |
|
82 | 0 | if( *language_entry == NULL ) |
83 | 0 | { |
84 | 0 | libcerror_error_set( |
85 | 0 | error, |
86 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
87 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
88 | 0 | "%s: unable to create language entry.", |
89 | 0 | function ); |
90 | |
|
91 | 0 | goto on_error; |
92 | 0 | } |
93 | 0 | if( memory_set( |
94 | 0 | *language_entry, |
95 | 0 | 0, |
96 | 0 | sizeof( libwrc_language_entry_t ) ) == NULL ) |
97 | 0 | { |
98 | 0 | libcerror_error_set( |
99 | 0 | error, |
100 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
101 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
102 | 0 | "%s: unable to clear language entry.", |
103 | 0 | function ); |
104 | |
|
105 | 0 | memory_free( |
106 | 0 | *language_entry ); |
107 | |
|
108 | 0 | *language_entry = NULL; |
109 | |
|
110 | 0 | return( -1 ); |
111 | 0 | } |
112 | 0 | if( libcdata_array_initialize( |
113 | 0 | &( ( *language_entry )->values_array ), |
114 | 0 | 0, |
115 | 0 | error ) != 1 ) |
116 | 0 | { |
117 | 0 | libcerror_error_set( |
118 | 0 | error, |
119 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
120 | 0 | LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, |
121 | 0 | "%s: unable to create values array.", |
122 | 0 | function ); |
123 | |
|
124 | 0 | goto on_error; |
125 | 0 | } |
126 | 0 | ( *language_entry )->language_identifier = language_identifier; |
127 | 0 | ( *language_entry )->value_free_function = value_free_function; |
128 | | |
129 | |
|
130 | 0 | return( 1 ); |
131 | | |
132 | 0 | on_error: |
133 | 0 | if( *language_entry != NULL ) |
134 | 0 | { |
135 | 0 | memory_free( |
136 | 0 | *language_entry ); |
137 | |
|
138 | 0 | *language_entry = NULL; |
139 | 0 | } |
140 | 0 | return( -1 ); |
141 | 0 | } |
142 | | |
143 | | /* Frees a language entry |
144 | | * Returns 1 if successful or -1 on error |
145 | | */ |
146 | | int libwrc_language_entry_free( |
147 | | libwrc_language_entry_t **language_entry, |
148 | | libcerror_error_t **error ) |
149 | 0 | { |
150 | 0 | static char *function = "libwrc_language_entry_free"; |
151 | 0 | int result = 1; |
152 | |
|
153 | 0 | if( language_entry == NULL ) |
154 | 0 | { |
155 | 0 | libcerror_error_set( |
156 | 0 | error, |
157 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
158 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
159 | 0 | "%s: invalid language entry.", |
160 | 0 | function ); |
161 | |
|
162 | 0 | return( -1 ); |
163 | 0 | } |
164 | 0 | if( *language_entry != NULL ) |
165 | 0 | { |
166 | 0 | if( libcdata_array_free( |
167 | 0 | &( ( *language_entry )->values_array ), |
168 | 0 | ( *language_entry )->value_free_function, |
169 | 0 | error ) != 1 ) |
170 | 0 | { |
171 | 0 | libcerror_error_set( |
172 | 0 | error, |
173 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
174 | 0 | LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, |
175 | 0 | "%s: unable to free values array.", |
176 | 0 | function ); |
177 | |
|
178 | 0 | result = -1; |
179 | 0 | } |
180 | 0 | memory_free( |
181 | 0 | *language_entry ); |
182 | |
|
183 | 0 | *language_entry = NULL; |
184 | 0 | } |
185 | 0 | return( result ); |
186 | 0 | } |
187 | | |
188 | | /* Retrieves the number of values |
189 | | * Returns 1 if successful or -1 on error |
190 | | */ |
191 | | int libwrc_language_entry_get_number_of_values( |
192 | | libwrc_language_entry_t *language_entry, |
193 | | int *number_of_values, |
194 | | libcerror_error_t **error ) |
195 | 0 | { |
196 | 0 | static char *function = "libwrc_language_entry_get_number_of_values"; |
197 | |
|
198 | 0 | if( language_entry == NULL ) |
199 | 0 | { |
200 | 0 | libcerror_error_set( |
201 | 0 | error, |
202 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
203 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
204 | 0 | "%s: invalid language entry.", |
205 | 0 | function ); |
206 | |
|
207 | 0 | return( -1 ); |
208 | 0 | } |
209 | 0 | if( libcdata_array_get_number_of_entries( |
210 | 0 | language_entry->values_array, |
211 | 0 | number_of_values, |
212 | 0 | error ) != 1 ) |
213 | 0 | { |
214 | 0 | libcerror_error_set( |
215 | 0 | error, |
216 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
217 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
218 | 0 | "%s: unable to retrieve number of entries.", |
219 | 0 | function ); |
220 | |
|
221 | 0 | return( -1 ); |
222 | 0 | } |
223 | 0 | return( 1 ); |
224 | 0 | } |
225 | | |
226 | | /* Retrieves a specific value |
227 | | * Returns 1 if successful or -1 on error |
228 | | */ |
229 | | int libwrc_language_entry_get_value_by_index( |
230 | | libwrc_language_entry_t *language_entry, |
231 | | int value_index, |
232 | | intptr_t **value, |
233 | | libcerror_error_t **error ) |
234 | 0 | { |
235 | 0 | static char *function = "libwrc_language_entry_get_value_by_index"; |
236 | |
|
237 | 0 | if( language_entry == NULL ) |
238 | 0 | { |
239 | 0 | libcerror_error_set( |
240 | 0 | error, |
241 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
242 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
243 | 0 | "%s: invalid language entry.", |
244 | 0 | function ); |
245 | |
|
246 | 0 | return( -1 ); |
247 | 0 | } |
248 | 0 | if( libcdata_array_get_entry_by_index( |
249 | 0 | language_entry->values_array, |
250 | 0 | value_index, |
251 | 0 | value, |
252 | 0 | error ) != 1 ) |
253 | 0 | { |
254 | 0 | libcerror_error_set( |
255 | 0 | error, |
256 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
257 | 0 | LIBCERROR_RUNTIME_ERROR_GET_FAILED, |
258 | 0 | "%s: unable to retrieve entry: %d.", |
259 | 0 | function, |
260 | 0 | value_index ); |
261 | |
|
262 | 0 | return( -1 ); |
263 | 0 | } |
264 | 0 | return( 1 ); |
265 | 0 | } |
266 | | |
267 | | /* Appends a value |
268 | | * Returns 1 if successful or -1 on error |
269 | | */ |
270 | | int libwrc_language_entry_append_value( |
271 | | libwrc_language_entry_t *language_entry, |
272 | | int *value_index, |
273 | | intptr_t *value, |
274 | | libcerror_error_t **error ) |
275 | 0 | { |
276 | 0 | static char *function = "libwrc_language_entry_append_value"; |
277 | |
|
278 | 0 | if( language_entry == NULL ) |
279 | 0 | { |
280 | 0 | libcerror_error_set( |
281 | 0 | error, |
282 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
283 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
284 | 0 | "%s: invalid language entry.", |
285 | 0 | function ); |
286 | |
|
287 | 0 | return( -1 ); |
288 | 0 | } |
289 | 0 | if( libcdata_array_append_entry( |
290 | 0 | language_entry->values_array, |
291 | 0 | value_index, |
292 | 0 | value, |
293 | 0 | error ) != 1 ) |
294 | 0 | { |
295 | 0 | libcerror_error_set( |
296 | 0 | error, |
297 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
298 | 0 | LIBCERROR_RUNTIME_ERROR_APPEND_FAILED, |
299 | 0 | "%s: unable to append entry.", |
300 | 0 | function, |
301 | 0 | value_index ); |
302 | |
|
303 | 0 | return( -1 ); |
304 | 0 | } |
305 | 0 | return( 1 ); |
306 | 0 | } |
307 | | |