Coverage Report

Created: 2026-01-20 07:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libwrc/libwrc/libwrc_manifest_resource.c
Line
Count
Source
1
/*
2
 * Manifest resource 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_libcerror.h"
28
#include "libwrc_libcnotify.h"
29
#include "libwrc_libuna.h"
30
#include "libwrc_manifest_resource.h"
31
32
/* Creates a manifest resource
33
 * Make sure the value manifest_resource is referencing, is set to NULL
34
 * Returns 1 if successful or -1 on error
35
 */
36
int libwrc_manifest_resource_initialize(
37
     libwrc_manifest_resource_t **manifest_resource,
38
     libcerror_error_t **error )
39
22
{
40
22
  libwrc_internal_manifest_resource_t *internal_manifest_resource = NULL;
41
22
  static char *function                                           = "libwrc_manifest_resource_initialize";
42
43
22
  if( manifest_resource == NULL )
44
0
  {
45
0
    libcerror_error_set(
46
0
     error,
47
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
48
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
49
0
     "%s: invalid manifest resource.",
50
0
     function );
51
52
0
    return( -1 );
53
0
  }
54
22
  if( *manifest_resource != NULL )
55
0
  {
56
0
    libcerror_error_set(
57
0
     error,
58
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
59
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
60
0
     "%s: invalid manifest resource value already set.",
61
0
     function );
62
63
0
    return( -1 );
64
0
  }
65
22
  internal_manifest_resource = memory_allocate_structure(
66
22
                                libwrc_internal_manifest_resource_t );
67
68
22
  if( internal_manifest_resource == NULL )
69
0
  {
70
0
    libcerror_error_set(
71
0
     error,
72
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
73
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
74
0
     "%s: unable to create manifest resource.",
75
0
     function );
76
77
0
    goto on_error;
78
0
  }
79
22
  if( memory_set(
80
22
       internal_manifest_resource,
81
22
       0,
82
22
       sizeof( libwrc_internal_manifest_resource_t ) ) == NULL )
83
0
  {
84
0
    libcerror_error_set(
85
0
     error,
86
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
87
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
88
0
     "%s: unable to clear manifest resource.",
89
0
     function );
90
91
0
    goto on_error;
92
0
  }
93
22
  *manifest_resource = (libwrc_manifest_resource_t *) internal_manifest_resource;
94
95
22
  return( 1 );
96
97
0
on_error:
98
0
  if( internal_manifest_resource != NULL )
99
0
  {
100
0
    memory_free(
101
0
     internal_manifest_resource );
102
0
  }
103
0
  return( -1 );
104
22
}
105
106
/* Frees a manifest resource
107
 * Returns 1 if successful or -1 on error
108
 */
109
int libwrc_manifest_resource_free(
110
     libwrc_manifest_resource_t **manifest_resource,
111
     libcerror_error_t **error )
112
22
{
113
22
  libwrc_internal_manifest_resource_t *internal_manifest_resource = NULL;
114
22
  static char *function                                           = "libwrc_manifest_resource_free";
115
116
22
  if( manifest_resource == NULL )
117
0
  {
118
0
    libcerror_error_set(
119
0
     error,
120
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
121
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
122
0
     "%s: invalid manifest resource.",
123
0
     function );
124
125
0
    return( -1 );
126
0
  }
127
22
  if( *manifest_resource != NULL )
128
22
  {
129
22
    internal_manifest_resource = (libwrc_internal_manifest_resource_t *) *manifest_resource;
130
22
    *manifest_resource         = NULL;
131
132
22
    if( internal_manifest_resource->string != NULL )
133
22
    {
134
22
      memory_free(
135
22
       internal_manifest_resource->string );
136
22
    }
137
22
    memory_free(
138
22
     internal_manifest_resource );
139
22
  }
140
22
  return( 1 );
141
22
}
142
143
/* Reads the manifest resource
144
 * Returns 1 if successful or -1 on error
145
 */
146
int libwrc_manifest_resource_read(
147
     libwrc_manifest_resource_t *manifest_resource,
148
     const uint8_t *data,
149
     size_t data_size,
150
     libcerror_error_t **error )
151
22
{
152
22
  libwrc_internal_manifest_resource_t *internal_manifest_resource = NULL;
153
22
  static char *function                                           = "libwrc_manifest_resource_read";
154
155
22
  if( manifest_resource == NULL )
156
0
  {
157
0
    libcerror_error_set(
158
0
     error,
159
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
160
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
161
0
     "%s: invalid manifest resource.",
162
0
     function );
163
164
0
    return( -1 );
165
0
  }
166
22
  internal_manifest_resource = (libwrc_internal_manifest_resource_t *) manifest_resource;
167
168
22
  if( internal_manifest_resource->string != NULL )
169
0
  {
170
0
    libcerror_error_set(
171
0
     error,
172
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
173
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
174
0
     "%s: invalid manifest resource - string value already set.",
175
0
     function );
176
177
0
    return( -1 );
178
0
  }
179
22
  if( data == 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 data.",
186
0
     function );
187
188
0
    return( -1 );
189
0
  }
190
22
  if( ( data_size == 0 )
191
22
   || ( data_size > (size_t) MEMORY_MAXIMUM_ALLOCATION_SIZE ) )
192
0
  {
193
0
    libcerror_error_set(
194
0
     error,
195
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
196
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
197
0
     "%s: invalid data size value out of bounds.",
198
0
     function );
199
200
0
    return( -1 );
201
0
  }
202
#if defined( HAVE_DEBUG_OUTPUT )
203
  if( libcnotify_verbose != 0 )
204
  {
205
    libcnotify_printf(
206
     "%s: data:\n",
207
     function );
208
    libcnotify_print_data(
209
     data,
210
     data_size,
211
     0 );
212
  }
213
#endif
214
22
  internal_manifest_resource->string = (uint8_t *) memory_allocate(
215
22
                                                    sizeof( uint8_t ) * data_size );
216
217
22
  if( internal_manifest_resource->string == NULL )
218
0
  {
219
0
    libcerror_error_set(
220
0
     error,
221
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
222
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
223
0
     "%s: unable to create string.",
224
0
     function );
225
226
0
    goto on_error;
227
0
  }
228
22
  internal_manifest_resource->string_size = data_size;
229
230
22
  if( memory_copy(
231
22
       internal_manifest_resource->string,
232
22
       data,
233
22
       data_size ) == NULL )
234
0
  {
235
0
    libcerror_error_set(
236
0
     error,
237
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
238
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
239
0
     "%s: unable to copy data to string.",
240
0
     function );
241
242
0
    goto on_error;
243
0
  }
244
22
  return( 1 );
245
246
0
on_error:
247
0
  if( internal_manifest_resource->string != NULL )
248
0
  {
249
0
    memory_free(
250
0
     internal_manifest_resource->string );
251
252
0
    internal_manifest_resource->string = NULL;
253
0
  }
254
0
  internal_manifest_resource->string_size = 0;
255
256
0
  return( -1 );
257
22
}
258
259
/* Retrieves the size of a specific UTF-8 formatted string
260
 * Returns 1 if successful or -1 on error
261
 */
262
int libwrc_manifest_resource_get_utf8_string_size(
263
     libwrc_manifest_resource_t *manifest_resource,
264
     size_t *utf8_string_size,
265
     libcerror_error_t **error )
266
0
{
267
0
  libwrc_internal_manifest_resource_t *internal_manifest_resource = NULL;
268
0
  static char *function                                           = "libwrc_manifest_resource_get_utf8_string_size";
269
270
0
  if( manifest_resource == NULL )
271
0
  {
272
0
    libcerror_error_set(
273
0
     error,
274
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
275
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
276
0
     "%s: invalid manifest resource.",
277
0
     function );
278
279
0
    return( -1 );
280
0
  }
281
0
  internal_manifest_resource = (libwrc_internal_manifest_resource_t *) manifest_resource;
282
283
0
  if( libuna_utf8_string_size_from_utf8_stream(
284
0
       internal_manifest_resource->string,
285
0
       internal_manifest_resource->string_size,
286
0
       utf8_string_size,
287
0
       error ) != 1 )
288
0
  {
289
0
    libcerror_error_set(
290
0
     error,
291
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
292
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
293
0
     "%s: unable to retrieve UTF-8 string size.",
294
0
     function );
295
296
0
    return( -1 );
297
0
  }
298
0
  return( 1 );
299
0
}
300
301
/* Retrieves a specific UTF-8 formatted string
302
 * Returns 1 if successful or -1 on error
303
 */
304
int libwrc_manifest_resource_get_utf8_string(
305
     libwrc_manifest_resource_t *manifest_resource,
306
     uint8_t *utf8_string,
307
     size_t utf8_string_size,
308
     libcerror_error_t **error )
309
0
{
310
0
  libwrc_internal_manifest_resource_t *internal_manifest_resource = NULL;
311
0
  static char *function                                           = "libwrc_manifest_resource_get_utf8_string";
312
313
0
  if( manifest_resource == NULL )
314
0
  {
315
0
    libcerror_error_set(
316
0
     error,
317
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
318
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
319
0
     "%s: invalid manifest resource.",
320
0
     function );
321
322
0
    return( -1 );
323
0
  }
324
0
  internal_manifest_resource = (libwrc_internal_manifest_resource_t *) manifest_resource;
325
326
0
  if( libuna_utf8_string_copy_from_utf8_stream(
327
0
       utf8_string,
328
0
       utf8_string_size,
329
0
       internal_manifest_resource->string,
330
0
       internal_manifest_resource->string_size,
331
0
       error ) != 1 )
332
0
  {
333
0
    libcerror_error_set(
334
0
     error,
335
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
336
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
337
0
     "%s: unable to retrieve UTF-8 string.",
338
0
     function );
339
340
0
    return( -1 );
341
0
  }
342
0
  return( 1 );
343
0
}
344
345
/* Retrieves the size of a specific UTF-16 formatted string
346
 * Returns 1 if successful or -1 on error
347
 */
348
int libwrc_manifest_resource_get_utf16_string_size(
349
     libwrc_manifest_resource_t *manifest_resource,
350
     size_t *utf16_string_size,
351
     libcerror_error_t **error )
352
0
{
353
0
  libwrc_internal_manifest_resource_t *internal_manifest_resource = NULL;
354
0
  static char *function                                           = "libwrc_manifest_resource_get_utf16_string_size";
355
356
0
  if( manifest_resource == NULL )
357
0
  {
358
0
    libcerror_error_set(
359
0
     error,
360
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
361
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
362
0
     "%s: invalid manifest resource.",
363
0
     function );
364
365
0
    return( -1 );
366
0
  }
367
0
  internal_manifest_resource = (libwrc_internal_manifest_resource_t *) manifest_resource;
368
369
0
  if( libuna_utf16_string_size_from_utf8_stream(
370
0
       internal_manifest_resource->string,
371
0
       internal_manifest_resource->string_size,
372
0
       utf16_string_size,
373
0
       error ) != 1 )
374
0
  {
375
0
    libcerror_error_set(
376
0
     error,
377
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
378
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
379
0
     "%s: unable to retrieve UTF-16 string size.",
380
0
     function );
381
382
0
    return( -1 );
383
0
  }
384
0
  return( 1 );
385
0
}
386
387
/* Retrieves a specific UTF-16 formatted string
388
 * Returns 1 if successful or -1 on error
389
 */
390
int libwrc_manifest_resource_get_utf16_string(
391
     libwrc_manifest_resource_t *manifest_resource,
392
     uint16_t *utf16_string,
393
     size_t utf16_string_size,
394
     libcerror_error_t **error )
395
0
{
396
0
  libwrc_internal_manifest_resource_t *internal_manifest_resource = NULL;
397
0
  static char *function                                           = "libwrc_manifest_resource_get_utf16_string";
398
399
0
  if( manifest_resource == NULL )
400
0
  {
401
0
    libcerror_error_set(
402
0
     error,
403
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
404
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
405
0
     "%s: invalid manifest resource.",
406
0
     function );
407
408
0
    return( -1 );
409
0
  }
410
0
  internal_manifest_resource = (libwrc_internal_manifest_resource_t *) manifest_resource;
411
412
0
  if( libuna_utf16_string_copy_from_utf8_stream(
413
0
       utf16_string,
414
0
       utf16_string_size,
415
0
       internal_manifest_resource->string,
416
0
       internal_manifest_resource->string_size,
417
0
       error ) != 1 )
418
0
  {
419
0
    libcerror_error_set(
420
0
     error,
421
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
422
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
423
0
     "%s: unable to retrieve UTF-16 string.",
424
0
     function );
425
426
0
    return( -1 );
427
0
  }
428
0
  return( 1 );
429
0
}
430