Coverage Report

Created: 2025-10-14 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libwrc/libwrc/libwrc_language_table.c
Line
Count
Source
1
/*
2
 * Language table 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_language_entry.h"
27
#include "libwrc_language_table.h"
28
#include "libwrc_libcdata.h"
29
#include "libwrc_libcerror.h"
30
31
/* Creates a language table
32
 * Make sure the value language_table is referencing, is set to NULL
33
 * Returns 1 if successful or -1 on error
34
 */
35
int libwrc_language_table_initialize(
36
     libwrc_language_table_t **language_table,
37
     libcerror_error_t **error )
38
0
{
39
0
  static char *function = "libwrc_language_table_initialize";
40
41
0
  if( language_table == 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 language table.",
48
0
     function );
49
50
0
    return( -1 );
51
0
  }
52
0
  if( *language_table != 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 language table value already set.",
59
0
     function );
60
61
0
    return( -1 );
62
0
  }
63
0
  *language_table = memory_allocate_structure(
64
0
                     libwrc_language_table_t );
65
66
0
  if( *language_table == 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 language table.",
73
0
     function );
74
75
0
    goto on_error;
76
0
  }
77
0
  if( memory_set(
78
0
       *language_table,
79
0
       0,
80
0
       sizeof( libwrc_language_table_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 language table.",
87
0
     function );
88
89
0
    memory_free(
90
0
     *language_table );
91
92
0
    *language_table = NULL;
93
94
0
    return( -1 );
95
0
  }
96
0
  if( libcdata_array_initialize(
97
0
       &( ( *language_table )->entries_array ),
98
0
       0,
99
0
       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 entries array.",
106
0
     function );
107
108
0
    goto on_error;
109
0
  }
110
0
  return( 1 );
111
112
0
on_error:
113
0
  if( *language_table != NULL )
114
0
  {
115
0
    memory_free(
116
0
     *language_table );
117
118
0
    *language_table = NULL;
119
0
  }
120
0
  return( -1 );
121
0
}
122
123
/* Frees a language table
124
 * Returns 1 if successful or -1 on error
125
 */
126
int libwrc_language_table_free(
127
     libwrc_language_table_t **language_table,
128
     libcerror_error_t **error )
129
0
{
130
0
  static char *function = "libwrc_language_table_free";
131
0
  int result            = 1;
132
133
0
  if( language_table == 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 language table.",
140
0
     function );
141
142
0
    return( -1 );
143
0
  }
144
0
  if( *language_table != NULL )
145
0
  {
146
0
    if( libcdata_array_free(
147
0
         &( ( *language_table )->entries_array ),
148
0
         (int (*)(intptr_t **, libcerror_error_t **)) &libwrc_language_entry_free,
149
0
         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
0
    memory_free(
161
0
     *language_table );
162
163
0
    *language_table = NULL;
164
0
  }
165
0
  return( result );
166
0
}
167
168
/* Retrieves the number of entries
169
 * Returns 1 if successful or -1 on error
170
 */
171
int libwrc_language_table_get_number_of_entries(
172
     libwrc_language_table_t *language_table,
173
     int *number_of_entries,
174
     libcerror_error_t **error )
175
0
{
176
0
  static char *function = "libwrc_language_table_get_number_of_entries";
177
178
0
  if( language_table == NULL )
179
0
  {
180
0
    libcerror_error_set(
181
0
     error,
182
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
183
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
184
0
     "%s: invalid language table.",
185
0
     function );
186
187
0
    return( -1 );
188
0
  }
189
0
  if( libcdata_array_get_number_of_entries(
190
0
       language_table->entries_array,
191
0
       number_of_entries,
192
0
       error ) != 1 )
193
0
  {
194
0
    libcerror_error_set(
195
0
     error,
196
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
197
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
198
0
     "%s: unable to retrieve number of entries.",
199
0
     function );
200
201
0
    return( -1 );
202
0
  }
203
0
  return( 1 );
204
0
}
205
206
/* Retrieves a specific language identifier
207
 * Returns 1 if successful or -1 on error
208
 */
209
int libwrc_language_table_get_identifier(
210
     libwrc_language_table_t *language_table,
211
     int entry_index,
212
     uint32_t *language_identifier,
213
     libcerror_error_t **error )
214
0
{
215
0
  libwrc_language_entry_t *language_entry = NULL;
216
0
  static char *function                   = "libwrc_language_table_get_identifier";
217
218
0
  if( language_table == NULL )
219
0
  {
220
0
    libcerror_error_set(
221
0
     error,
222
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
223
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
224
0
     "%s: invalid language table.",
225
0
     function );
226
227
0
    return( -1 );
228
0
  }
229
0
  if( language_identifier == NULL )
230
0
  {
231
0
    libcerror_error_set(
232
0
     error,
233
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
234
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
235
0
     "%s: invalid language identifier.",
236
0
     function );
237
238
0
    return( -1 );
239
0
  }
240
0
  if( libcdata_array_get_entry_by_index(
241
0
       language_table->entries_array,
242
0
       entry_index,
243
0
       (intptr_t **) &language_entry,
244
0
       error ) != 1 )
245
0
  {
246
0
    libcerror_error_set(
247
0
     error,
248
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
249
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
250
0
     "%s: unable to retrieve language entry: %d.",
251
0
     function,
252
0
     entry_index );
253
254
0
    return( -1 );
255
0
  }
256
0
  if( language_entry == NULL )
257
0
  {
258
0
    libcerror_error_set(
259
0
     error,
260
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
261
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
262
0
     "%s: missing language entry: %d.",
263
0
     function,
264
0
     entry_index );
265
266
0
    return( -1 );
267
0
  }
268
0
  *language_identifier = language_entry->language_identifier;
269
270
0
  return( 1 );
271
0
}
272
273
/* Retrieves the entry for a specific language identifier
274
 * Returns 1 if successful, 0 if no such language or -1 on error
275
 */
276
int libwrc_language_table_get_entry_by_identifier(
277
     libwrc_language_table_t *language_table,
278
     uint32_t language_identifier,
279
     libwrc_language_entry_t **language_entry,
280
     libcerror_error_t **error )
281
0
{
282
0
  static char *function = "libwrc_language_table_get_entry_by_identifier";
283
0
  int entry_index       = 0;
284
0
  int number_of_entries = 0;
285
286
0
  if( language_table == NULL )
287
0
  {
288
0
    libcerror_error_set(
289
0
     error,
290
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
291
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
292
0
     "%s: invalid language table.",
293
0
     function );
294
295
0
    return( -1 );
296
0
  }
297
0
  if( language_entry == NULL )
298
0
  {
299
0
    libcerror_error_set(
300
0
     error,
301
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
302
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
303
0
     "%s: invalid language entry.",
304
0
     function );
305
306
0
    return( -1 );
307
0
  }
308
0
  if( libcdata_array_get_number_of_entries(
309
0
       language_table->entries_array,
310
0
       &number_of_entries,
311
0
       error ) != 1 )
312
0
  {
313
0
    libcerror_error_set(
314
0
     error,
315
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
316
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
317
0
     "%s: unable to retrieve number of entries.",
318
0
     function );
319
320
0
    return( -1 );
321
0
  }
322
0
  for( entry_index = 0;
323
0
       entry_index < number_of_entries;
324
0
       entry_index++ )
325
0
  {
326
0
    if( libcdata_array_get_entry_by_index(
327
0
         language_table->entries_array,
328
0
         entry_index,
329
0
         (intptr_t **) language_entry,
330
0
         error ) != 1 )
331
0
    {
332
0
      libcerror_error_set(
333
0
       error,
334
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
335
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
336
0
       "%s: unable to retrieve language entry: %d.",
337
0
       function,
338
0
       entry_index );
339
340
0
      return( -1 );
341
0
    }
342
0
    if( *language_entry == NULL )
343
0
    {
344
0
      libcerror_error_set(
345
0
       error,
346
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
347
0
       LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
348
0
       "%s: missing language entry: %d.",
349
0
       function,
350
0
       entry_index );
351
352
0
      return( -1 );
353
0
    }
354
0
    if( ( *language_entry )->language_identifier == language_identifier )
355
0
    {
356
0
      return( 1 );
357
0
    }
358
0
  }
359
0
  *language_entry = NULL;
360
361
0
  return( 0 );
362
0
}
363
364
/* Appends a language entry
365
 * Returns 1 if successful or -1 on error
366
 */
367
int libwrc_language_table_append_entry(
368
     libwrc_language_table_t *language_table,
369
     int *entry_index,
370
     libwrc_language_entry_t *language_entry,
371
     libcerror_error_t **error )
372
0
{
373
0
  static char *function = "libwrc_language_table_append_entry";
374
375
0
  if( language_table == NULL )
376
0
  {
377
0
    libcerror_error_set(
378
0
     error,
379
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
380
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
381
0
     "%s: invalid language table.",
382
0
     function );
383
384
0
    return( -1 );
385
0
  }
386
0
  if( libcdata_array_append_entry(
387
0
       language_table->entries_array,
388
0
       entry_index,
389
0
       (intptr_t *) language_entry,
390
0
       error ) != 1 )
391
0
  {
392
0
    libcerror_error_set(
393
0
     error,
394
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
395
0
     LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
396
0
     "%s: unable to append entry.",
397
0
     function );
398
399
0
    return( -1 );
400
0
  }
401
0
  return( 1 );
402
0
}
403
404
/* Retrieves the number of values for a specific language identifier
405
 * Returns 1 if successful or -1 on error
406
 */
407
int libwrc_language_table_get_number_of_values(
408
     libwrc_language_table_t *language_table,
409
     uint32_t language_identifier,
410
     int *number_of_values,
411
     libcerror_error_t **error )
412
0
{
413
0
  libwrc_language_entry_t *language_entry = NULL;
414
0
  static char *function                   = "libwrc_language_table_get_number_of_values";
415
416
0
  if( language_table == NULL )
417
0
  {
418
0
    libcerror_error_set(
419
0
     error,
420
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
421
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
422
0
     "%s: invalid language table.",
423
0
     function );
424
425
0
    return( -1 );
426
0
  }
427
0
  if( libwrc_language_table_get_entry_by_identifier(
428
0
       language_table,
429
0
       language_identifier,
430
0
       &language_entry,
431
0
       error ) != 1 )
432
0
  {
433
0
    libcerror_error_set(
434
0
     error,
435
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
436
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
437
0
     "%s: unable to retrieve language entry for identifier: 0x%08" PRIx32 ".",
438
0
     function,
439
0
     language_identifier );
440
441
0
    return( -1 );
442
0
  }
443
0
  if( libwrc_language_entry_get_number_of_values(
444
0
       language_entry,
445
0
       number_of_values,
446
0
       error ) != 1 )
447
0
  {
448
0
    libcerror_error_set(
449
0
     error,
450
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
451
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
452
0
     "%s: unable to retrieve number of values.",
453
0
     function );
454
455
0
    return( -1 );
456
0
  }
457
0
  return( 1 );
458
0
}
459