Coverage Report

Created: 2025-10-14 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libfsxfs/libfsxfs/libfsxfs_attribute_values.c
Line
Count
Source
1
/*
2
 * Extended attribute values functions
3
 *
4
 * Copyright (C) 2020-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 <byte_stream.h>
24
#include <memory.h>
25
#include <narrow_string.h>
26
#include <types.h>
27
28
#include "libfsxfs_attribute_values.h"
29
#include "libfsxfs_debug.h"
30
#include "libfsxfs_libcerror.h"
31
#include "libfsxfs_libcnotify.h"
32
#include "libfsxfs_libuna.h"
33
34
#include "fsxfs_attributes.h"
35
36
/* Creates a attribute values
37
 * Make sure the value attribute_values is referencing, is set to NULL
38
 * Returns 1 if successful or -1 on error
39
 */
40
int libfsxfs_attribute_values_initialize(
41
     libfsxfs_attribute_values_t **attribute_values,
42
     libcerror_error_t **error )
43
0
{
44
0
  static char *function = "libfsxfs_attribute_values_initialize";
45
46
0
  if( attribute_values == 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 attribute values.",
53
0
     function );
54
55
0
    return( -1 );
56
0
  }
57
0
  if( *attribute_values != 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 attribute values value already set.",
64
0
     function );
65
66
0
    return( -1 );
67
0
  }
68
0
  *attribute_values = memory_allocate_structure(
69
0
                       libfsxfs_attribute_values_t );
70
71
0
  if( *attribute_values == NULL )
72
0
  {
73
0
    libcerror_error_set(
74
0
     error,
75
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
76
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
77
0
     "%s: unable to create attribute values.",
78
0
     function );
79
80
0
    goto on_error;
81
0
  }
82
0
  if( memory_set(
83
0
       *attribute_values,
84
0
       0,
85
0
       sizeof( libfsxfs_attribute_values_t ) ) == NULL )
86
0
  {
87
0
    libcerror_error_set(
88
0
     error,
89
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
90
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
91
0
     "%s: unable to clear attribute values.",
92
0
     function );
93
94
0
    goto on_error;
95
0
  }
96
0
  return( 1 );
97
98
0
on_error:
99
0
  if( *attribute_values != NULL )
100
0
  {
101
0
    memory_free(
102
0
     *attribute_values );
103
104
0
    *attribute_values = NULL;
105
0
  }
106
0
  return( -1 );
107
0
}
108
109
/* Frees a attribute values
110
 * Returns 1 if successful or -1 on error
111
 */
112
int libfsxfs_attribute_values_free(
113
     libfsxfs_attribute_values_t **attribute_values,
114
     libcerror_error_t **error )
115
0
{
116
0
  static char *function = "libfsxfs_attribute_values_free";
117
118
0
  if( attribute_values == NULL )
119
0
  {
120
0
    libcerror_error_set(
121
0
     error,
122
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
123
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
124
0
     "%s: invalid attribute values.",
125
0
     function );
126
127
0
    return( -1 );
128
0
  }
129
0
  if( *attribute_values != NULL )
130
0
  {
131
0
    if( ( *attribute_values )->name != NULL )
132
0
    {
133
0
      memory_free(
134
0
       ( *attribute_values )->name );
135
0
    }
136
0
    if( ( *attribute_values )->value_data != NULL )
137
0
    {
138
0
      memory_free(
139
0
       ( *attribute_values )->value_data );
140
0
    }
141
0
    memory_free(
142
0
     *attribute_values );
143
144
0
    *attribute_values = NULL;
145
0
  }
146
0
  return( 1 );
147
0
}
148
149
/* Sets the name including prefix
150
 * Returns 1 if successful or -1 on error
151
 */
152
int libfsxfs_attribute_values_set_name(
153
     libfsxfs_attribute_values_t *attribute_values,
154
     const uint8_t *name,
155
     size_t name_size,
156
     uint8_t flags,
157
     libcerror_error_t **error )
158
0
{
159
0
  static char *function     = "libfsxfs_attribute_values_set_name";
160
0
  const char *name_prefix   = NULL;
161
0
  size_t name_prefix_length = 0;
162
163
0
  if( attribute_values == NULL )
164
0
  {
165
0
    libcerror_error_set(
166
0
     error,
167
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
168
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
169
0
     "%s: invalid attribute values.",
170
0
     function );
171
172
0
    return( -1 );
173
0
  }
174
0
  if( attribute_values->name != NULL )
175
0
  {
176
0
    libcerror_error_set(
177
0
     error,
178
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
179
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
180
0
     "%s: invalid attribute values - name value already set.",
181
0
     function );
182
183
0
    return( -1 );
184
0
  }
185
0
  if( name == NULL )
186
0
  {
187
0
    libcerror_error_set(
188
0
     error,
189
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
190
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
191
0
     "%s: invalid name.",
192
0
     function );
193
194
0
    return( -1 );
195
0
  }
196
0
  if( ( name_size == 0 )
197
0
   || ( name_size > (size_t) 255 ) )
198
0
  {
199
0
    libcerror_error_set(
200
0
     error,
201
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
202
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
203
0
     "%s: invalid name size value out of bounds.",
204
0
     function );
205
206
0
    return( -1 );
207
0
  }
208
0
  switch( flags & 0x7e )
209
0
  {
210
0
    case 0:
211
0
      name_prefix = "user.";
212
0
      break;
213
214
0
    case 2:
215
0
      name_prefix = "trusted.";
216
0
      break;
217
218
0
    case 4:
219
0
      name_prefix = "secure.";
220
0
      break;
221
222
0
    default:
223
0
      libcerror_error_set(
224
0
       error,
225
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
226
0
       LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
227
0
       "%s: unsupported flags: 0x%02" PRIx8 ".",
228
0
       function,
229
0
       flags );
230
231
0
      return( -1 );
232
0
  }
233
0
  name_prefix_length = narrow_string_length(
234
0
                        name_prefix );
235
236
0
  attribute_values->name_size = name_prefix_length + name_size + 1;
237
238
0
  if( attribute_values->name_size > (size_t) MEMORY_MAXIMUM_ALLOCATION_SIZE )
239
0
  {
240
0
    libcerror_error_set(
241
0
     error,
242
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
243
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
244
0
     "%s: invalid name size value out of bounds.",
245
0
     function );
246
247
0
    return( -1 );
248
0
  }
249
0
  attribute_values->name = (uint8_t *) memory_allocate(
250
0
                                        sizeof( uint8_t ) * attribute_values->name_size );
251
252
0
  if( attribute_values->name == NULL )
253
0
  {
254
0
    libcerror_error_set(
255
0
     error,
256
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
257
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
258
0
     "%s: unable to create name.",
259
0
     function );
260
261
0
    goto on_error;
262
0
  }
263
0
  if( name_prefix_length > 0 )
264
0
  {
265
0
    if( memory_copy(
266
0
         attribute_values->name,
267
0
         name_prefix,
268
0
         name_prefix_length ) == NULL )
269
0
    {
270
0
      libcerror_error_set(
271
0
       error,
272
0
       LIBCERROR_ERROR_DOMAIN_MEMORY,
273
0
       LIBCERROR_MEMORY_ERROR_COPY_FAILED,
274
0
       "%s: unable to copy name prefix to extended attribute name.",
275
0
       function );
276
277
0
      goto on_error;
278
0
    }
279
0
  }
280
0
  if( memory_copy(
281
0
       &( ( attribute_values->name )[ name_prefix_length ] ),
282
0
       name,
283
0
       name_size ) == NULL )
284
0
  {
285
0
    libcerror_error_set(
286
0
     error,
287
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
288
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
289
0
     "%s: unable to copy name to extended attribute name.",
290
0
     function );
291
292
0
    goto on_error;
293
0
  }
294
0
  ( attribute_values->name )[ attribute_values->name_size - 1 ] = 0;
295
296
0
  return( 1 );
297
298
0
on_error:
299
0
  if( attribute_values->name != NULL )
300
0
  {
301
0
    memory_free(
302
0
     attribute_values->name );
303
304
0
    attribute_values->name = NULL;
305
0
  }
306
0
  attribute_values->name_size = 0;
307
308
0
  return( -1 );
309
0
}
310
311
/* Sets the value data
312
 * Returns 1 if successful or -1 on error
313
 */
314
int libfsxfs_attribute_values_set_value_data(
315
     libfsxfs_attribute_values_t *attribute_values,
316
     const uint8_t *value_data,
317
     size_t value_data_size,
318
     libcerror_error_t **error )
319
0
{
320
0
  static char *function = "libfsxfs_attribute_values_set_value_data";
321
322
0
  if( attribute_values == NULL )
323
0
  {
324
0
    libcerror_error_set(
325
0
     error,
326
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
327
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
328
0
     "%s: invalid attribute values.",
329
0
     function );
330
331
0
    return( -1 );
332
0
  }
333
0
  if( attribute_values->value_data != NULL )
334
0
  {
335
0
    libcerror_error_set(
336
0
     error,
337
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
338
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
339
0
     "%s: invalid attribute values - value data value already set.",
340
0
     function );
341
342
0
    return( -1 );
343
0
  }
344
0
  if( value_data == NULL )
345
0
  {
346
0
    libcerror_error_set(
347
0
     error,
348
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
349
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
350
0
     "%s: invalid value data.",
351
0
     function );
352
353
0
    return( -1 );
354
0
  }
355
0
  if( ( value_data_size == 0 )
356
0
   || ( value_data_size > (size_t) MEMORY_MAXIMUM_ALLOCATION_SIZE ) )
357
0
  {
358
0
    libcerror_error_set(
359
0
     error,
360
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
361
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
362
0
     "%s: invalid value data size value out of bounds.",
363
0
     function );
364
365
0
    return( -1 );
366
0
  }
367
0
  attribute_values->value_data = (uint8_t *) memory_allocate(
368
0
                                              sizeof( uint8_t ) * value_data_size );
369
370
0
  if( attribute_values->value_data == NULL )
371
0
  {
372
0
    libcerror_error_set(
373
0
     error,
374
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
375
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
376
0
     "%s: unable to create value data.",
377
0
     function );
378
379
0
    goto on_error;
380
0
  }
381
0
  if( memory_copy(
382
0
       attribute_values->value_data,
383
0
       value_data,
384
0
       value_data_size ) == NULL )
385
0
  {
386
0
    libcerror_error_set(
387
0
     error,
388
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
389
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
390
0
     "%s: unable to copy value data.",
391
0
     function );
392
393
0
    goto on_error;
394
0
  }
395
0
  attribute_values->value_data_size = value_data_size;
396
397
0
  return( 1 );
398
399
0
on_error:
400
0
  if( attribute_values->value_data != NULL )
401
0
  {
402
0
    memory_free(
403
0
     attribute_values->value_data );
404
405
0
    attribute_values->value_data = NULL;
406
0
  }
407
0
  return( -1 );
408
0
}
409
410
/* Compares an UTF-8 string with an attribute name
411
 * Returns LIBUNA_COMPARE_LESS, LIBUNA_COMPARE_EQUAL, LIBUNA_COMPARE_GREATER if successful or -1 on error
412
 */
413
int libfsxfs_attribute_values_compare_name_with_utf8_string(
414
     libfsxfs_attribute_values_t *attribute_values,
415
     const uint8_t *utf8_string,
416
     size_t utf8_string_length,
417
     libcerror_error_t **error )
418
0
{
419
0
  static char *function = "libfsxfs_attribute_values_compare_name_with_utf8_string";
420
0
  int result            = 0;
421
422
0
  if( attribute_values == NULL )
423
0
  {
424
0
    libcerror_error_set(
425
0
     error,
426
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
427
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
428
0
     "%s: invalid attribute values.",
429
0
     function );
430
431
0
    return( -1 );
432
0
  }
433
0
  result = libuna_utf8_string_compare_with_utf8_stream(
434
0
            utf8_string,
435
0
            utf8_string_length,
436
0
            attribute_values->name,
437
0
            attribute_values->name_size,
438
0
            error );
439
440
0
  if( result == -1 )
441
0
  {
442
0
    libcerror_error_set(
443
0
     error,
444
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
445
0
     LIBCERROR_RUNTIME_ERROR_GENERIC,
446
0
     "%s: unable to compare UTF-8 string with name.",
447
0
     function );
448
449
0
    return( -1 );
450
0
  }
451
0
  return( result );
452
0
}
453
454
/* Retrieves the size of the UTF-8 encoded name
455
 * The returned size includes the end of string character
456
 * Returns 1 if successful or -1 on error
457
 */
458
int libfsxfs_attribute_values_get_utf8_name_size(
459
     libfsxfs_attribute_values_t *attribute_values,
460
     size_t *utf8_string_size,
461
     libcerror_error_t **error )
462
0
{
463
0
  static char *function = "libfsxfs_attribute_values_get_utf8_name_size";
464
465
0
  if( attribute_values == NULL )
466
0
  {
467
0
    libcerror_error_set(
468
0
     error,
469
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
470
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
471
0
     "%s: invalid attribute values.",
472
0
     function );
473
474
0
    return( -1 );
475
0
  }
476
0
  if( libuna_utf8_string_size_from_utf8_stream(
477
0
       attribute_values->name,
478
0
       (size_t) attribute_values->name_size,
479
0
       utf8_string_size,
480
0
       error ) != 1 )
481
0
  {
482
0
    libcerror_error_set(
483
0
     error,
484
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
485
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
486
0
     "%s: unable to retrieve UTF-8 string size.",
487
0
     function );
488
489
0
    return( -1 );
490
0
  }
491
0
  return( 1 );
492
0
}
493
494
/* Retrieves the UTF-8 encoded name
495
 * The size should include the end of string character
496
 * Returns 1 if successful or -1 on error
497
 */
498
int libfsxfs_attribute_values_get_utf8_name(
499
     libfsxfs_attribute_values_t *attribute_values,
500
     uint8_t *utf8_string,
501
     size_t utf8_string_size,
502
     libcerror_error_t **error )
503
0
{
504
0
  static char *function = "libfsxfs_attribute_values_get_utf8_name";
505
506
0
  if( attribute_values == NULL )
507
0
  {
508
0
    libcerror_error_set(
509
0
     error,
510
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
511
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
512
0
     "%s: invalid attribute values.",
513
0
     function );
514
515
0
    return( -1 );
516
0
  }
517
0
  if( libuna_utf8_string_copy_from_utf8_stream(
518
0
       utf8_string,
519
0
       utf8_string_size,
520
0
       attribute_values->name,
521
0
       (size_t) attribute_values->name_size,
522
0
       error ) != 1 )
523
0
  {
524
0
    libcerror_error_set(
525
0
     error,
526
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
527
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
528
0
     "%s: unable to retrieve UTF-8 string.",
529
0
     function );
530
531
0
    return( -1 );
532
0
  }
533
0
  return( 1 );
534
0
}
535
536
/* Compares an UTF-16 string with an attribute name
537
 * Returns LIBUNA_COMPARE_LESS, LIBUNA_COMPARE_EQUAL, LIBUNA_COMPARE_GREATER if successful or -1 on error
538
 */
539
int libfsxfs_attribute_values_compare_name_with_utf16_string(
540
     libfsxfs_attribute_values_t *attribute_values,
541
     const uint16_t *utf16_string,
542
     size_t utf16_string_length,
543
     libcerror_error_t **error )
544
0
{
545
0
  static char *function = "libfsxfs_attribute_values_compare_name_with_utf16_string";
546
0
  int result            = 0;
547
548
0
  if( attribute_values == NULL )
549
0
  {
550
0
    libcerror_error_set(
551
0
     error,
552
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
553
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
554
0
     "%s: invalid attribute values.",
555
0
     function );
556
557
0
    return( -1 );
558
0
  }
559
0
  result = libuna_utf16_string_compare_with_utf8_stream(
560
0
            utf16_string,
561
0
            utf16_string_length,
562
0
            attribute_values->name,
563
0
            attribute_values->name_size,
564
0
            error );
565
566
0
  if( result == -1 )
567
0
  {
568
0
    libcerror_error_set(
569
0
     error,
570
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
571
0
     LIBCERROR_RUNTIME_ERROR_GENERIC,
572
0
     "%s: unable to compare UTF-16 string with name.",
573
0
     function );
574
575
0
    return( -1 );
576
0
  }
577
0
  return( result );
578
0
}
579
580
/* Retrieves the size of the UTF-16 encoded name
581
 * The returned size includes the end of string character
582
 * Returns 1 if successful or -1 on error
583
 */
584
int libfsxfs_attribute_values_get_utf16_name_size(
585
     libfsxfs_attribute_values_t *attribute_values,
586
     size_t *utf16_string_size,
587
     libcerror_error_t **error )
588
0
{
589
0
  static char *function = "libfsxfs_attribute_values_get_utf16_name_size";
590
591
0
  if( attribute_values == NULL )
592
0
  {
593
0
    libcerror_error_set(
594
0
     error,
595
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
596
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
597
0
     "%s: invalid attribute values.",
598
0
     function );
599
600
0
    return( -1 );
601
0
  }
602
0
  if( libuna_utf16_string_size_from_utf8_stream(
603
0
       attribute_values->name,
604
0
       (size_t) attribute_values->name_size,
605
0
       utf16_string_size,
606
0
       error ) != 1 )
607
0
  {
608
0
    libcerror_error_set(
609
0
     error,
610
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
611
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
612
0
     "%s: unable to retrieve UTF-16 string size.",
613
0
     function );
614
615
0
    return( -1 );
616
0
  }
617
0
  return( 1 );
618
0
}
619
620
/* Retrieves the UTF-16 encoded name
621
 * The size should include the end of string character
622
 * Returns 1 if successful or -1 on error
623
 */
624
int libfsxfs_attribute_values_get_utf16_name(
625
     libfsxfs_attribute_values_t *attribute_values,
626
     uint16_t *utf16_string,
627
     size_t utf16_string_size,
628
     libcerror_error_t **error )
629
0
{
630
0
  static char *function = "libfsxfs_attribute_values_get_utf16_name";
631
632
0
  if( attribute_values == NULL )
633
0
  {
634
0
    libcerror_error_set(
635
0
     error,
636
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
637
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
638
0
     "%s: invalid attribute values.",
639
0
     function );
640
641
0
    return( -1 );
642
0
  }
643
0
  if( libuna_utf16_string_copy_from_utf8_stream(
644
0
       utf16_string,
645
0
       utf16_string_size,
646
0
       attribute_values->name,
647
0
       (size_t) attribute_values->name_size,
648
0
       error ) != 1 )
649
0
  {
650
0
    libcerror_error_set(
651
0
     error,
652
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
653
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
654
0
     "%s: unable to retrieve UTF-16 string.",
655
0
     function );
656
657
0
    return( -1 );
658
0
  }
659
0
  return( 1 );
660
0
}
661