Coverage Report

Created: 2026-10-22 07:28

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-2026, 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
    case 8:
223
0
      name_prefix = "";
224
0
      break;
225
226
0
    default:
227
0
      libcerror_error_set(
228
0
       error,
229
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
230
0
       LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
231
0
       "%s: unsupported flags: 0x%02" PRIx8 ".",
232
0
       function,
233
0
       flags );
234
235
0
      return( -1 );
236
0
  }
237
0
  name_prefix_length = narrow_string_length(
238
0
                        name_prefix );
239
240
0
  attribute_values->name_size = name_prefix_length + name_size + 1;
241
242
0
  if( attribute_values->name_size > (size_t) MEMORY_MAXIMUM_ALLOCATION_SIZE )
243
0
  {
244
0
    libcerror_error_set(
245
0
     error,
246
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
247
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
248
0
     "%s: invalid name size value out of bounds.",
249
0
     function );
250
251
0
    return( -1 );
252
0
  }
253
0
  attribute_values->name = (uint8_t *) memory_allocate(
254
0
                                        sizeof( uint8_t ) * attribute_values->name_size );
255
256
0
  if( attribute_values->name == NULL )
257
0
  {
258
0
    libcerror_error_set(
259
0
     error,
260
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
261
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
262
0
     "%s: unable to create name.",
263
0
     function );
264
265
0
    goto on_error;
266
0
  }
267
0
  if( name_prefix_length > 0 )
268
0
  {
269
0
    if( memory_copy(
270
0
         attribute_values->name,
271
0
         name_prefix,
272
0
         name_prefix_length ) == NULL )
273
0
    {
274
0
      libcerror_error_set(
275
0
       error,
276
0
       LIBCERROR_ERROR_DOMAIN_MEMORY,
277
0
       LIBCERROR_MEMORY_ERROR_COPY_FAILED,
278
0
       "%s: unable to copy name prefix to extended attribute name.",
279
0
       function );
280
281
0
      goto on_error;
282
0
    }
283
0
  }
284
0
  if( memory_copy(
285
0
       &( ( attribute_values->name )[ name_prefix_length ] ),
286
0
       name,
287
0
       name_size ) == NULL )
288
0
  {
289
0
    libcerror_error_set(
290
0
     error,
291
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
292
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
293
0
     "%s: unable to copy name to extended attribute name.",
294
0
     function );
295
296
0
    goto on_error;
297
0
  }
298
0
  ( attribute_values->name )[ attribute_values->name_size - 1 ] = 0;
299
300
0
  return( 1 );
301
302
0
on_error:
303
0
  if( attribute_values->name != NULL )
304
0
  {
305
0
    memory_free(
306
0
     attribute_values->name );
307
308
0
    attribute_values->name = NULL;
309
0
  }
310
0
  attribute_values->name_size = 0;
311
312
0
  return( -1 );
313
0
}
314
315
/* Sets the value data
316
 * Returns 1 if successful or -1 on error
317
 */
318
int libfsxfs_attribute_values_set_value_data(
319
     libfsxfs_attribute_values_t *attribute_values,
320
     const uint8_t *value_data,
321
     size_t value_data_size,
322
     libcerror_error_t **error )
323
0
{
324
0
  static char *function = "libfsxfs_attribute_values_set_value_data";
325
326
0
  if( attribute_values == NULL )
327
0
  {
328
0
    libcerror_error_set(
329
0
     error,
330
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
331
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
332
0
     "%s: invalid attribute values.",
333
0
     function );
334
335
0
    return( -1 );
336
0
  }
337
0
  if( attribute_values->value_data != NULL )
338
0
  {
339
0
    libcerror_error_set(
340
0
     error,
341
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
342
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
343
0
     "%s: invalid attribute values - value data value already set.",
344
0
     function );
345
346
0
    return( -1 );
347
0
  }
348
0
  if( value_data == NULL )
349
0
  {
350
0
    libcerror_error_set(
351
0
     error,
352
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
353
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
354
0
     "%s: invalid value data.",
355
0
     function );
356
357
0
    return( -1 );
358
0
  }
359
0
  if( ( value_data_size == 0 )
360
0
   || ( value_data_size > (size_t) MEMORY_MAXIMUM_ALLOCATION_SIZE ) )
361
0
  {
362
0
    libcerror_error_set(
363
0
     error,
364
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
365
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
366
0
     "%s: invalid value data size value out of bounds.",
367
0
     function );
368
369
0
    return( -1 );
370
0
  }
371
0
  attribute_values->value_data = (uint8_t *) memory_allocate(
372
0
                                              sizeof( uint8_t ) * value_data_size );
373
374
0
  if( attribute_values->value_data == NULL )
375
0
  {
376
0
    libcerror_error_set(
377
0
     error,
378
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
379
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
380
0
     "%s: unable to create value data.",
381
0
     function );
382
383
0
    goto on_error;
384
0
  }
385
0
  if( memory_copy(
386
0
       attribute_values->value_data,
387
0
       value_data,
388
0
       value_data_size ) == NULL )
389
0
  {
390
0
    libcerror_error_set(
391
0
     error,
392
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
393
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
394
0
     "%s: unable to copy value data.",
395
0
     function );
396
397
0
    goto on_error;
398
0
  }
399
0
  attribute_values->value_data_size = value_data_size;
400
401
0
  return( 1 );
402
403
0
on_error:
404
0
  if( attribute_values->value_data != NULL )
405
0
  {
406
0
    memory_free(
407
0
     attribute_values->value_data );
408
409
0
    attribute_values->value_data = NULL;
410
0
  }
411
0
  return( -1 );
412
0
}
413
414
/* Compares an UTF-8 string with an attribute name
415
 * Returns LIBUNA_COMPARE_LESS, LIBUNA_COMPARE_EQUAL, LIBUNA_COMPARE_GREATER if successful or -1 on error
416
 */
417
int libfsxfs_attribute_values_compare_name_with_utf8_string(
418
     libfsxfs_attribute_values_t *attribute_values,
419
     const uint8_t *utf8_string,
420
     size_t utf8_string_length,
421
     libcerror_error_t **error )
422
0
{
423
0
  static char *function = "libfsxfs_attribute_values_compare_name_with_utf8_string";
424
0
  int result            = 0;
425
426
0
  if( attribute_values == NULL )
427
0
  {
428
0
    libcerror_error_set(
429
0
     error,
430
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
431
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
432
0
     "%s: invalid attribute values.",
433
0
     function );
434
435
0
    return( -1 );
436
0
  }
437
0
  result = libuna_utf8_string_compare_with_utf8_stream(
438
0
            utf8_string,
439
0
            utf8_string_length,
440
0
            attribute_values->name,
441
0
            attribute_values->name_size,
442
0
            error );
443
444
0
  if( result == -1 )
445
0
  {
446
0
    libcerror_error_set(
447
0
     error,
448
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
449
0
     LIBCERROR_RUNTIME_ERROR_GENERIC,
450
0
     "%s: unable to compare UTF-8 string with name.",
451
0
     function );
452
453
0
    return( -1 );
454
0
  }
455
0
  return( result );
456
0
}
457
458
/* Retrieves the size of the UTF-8 encoded name
459
 * The returned size includes the end of string character
460
 * Returns 1 if successful or -1 on error
461
 */
462
int libfsxfs_attribute_values_get_utf8_name_size(
463
     libfsxfs_attribute_values_t *attribute_values,
464
     size_t *utf8_string_size,
465
     libcerror_error_t **error )
466
0
{
467
0
  static char *function = "libfsxfs_attribute_values_get_utf8_name_size";
468
469
0
  if( attribute_values == NULL )
470
0
  {
471
0
    libcerror_error_set(
472
0
     error,
473
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
474
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
475
0
     "%s: invalid attribute values.",
476
0
     function );
477
478
0
    return( -1 );
479
0
  }
480
0
  if( libuna_utf8_string_size_from_utf8_stream(
481
0
       attribute_values->name,
482
0
       (size_t) attribute_values->name_size,
483
0
       utf8_string_size,
484
0
       error ) != 1 )
485
0
  {
486
0
    libcerror_error_set(
487
0
     error,
488
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
489
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
490
0
     "%s: unable to retrieve UTF-8 string size.",
491
0
     function );
492
493
0
    return( -1 );
494
0
  }
495
0
  return( 1 );
496
0
}
497
498
/* Retrieves the UTF-8 encoded name
499
 * The size should include the end of string character
500
 * Returns 1 if successful or -1 on error
501
 */
502
int libfsxfs_attribute_values_get_utf8_name(
503
     libfsxfs_attribute_values_t *attribute_values,
504
     uint8_t *utf8_string,
505
     size_t utf8_string_size,
506
     libcerror_error_t **error )
507
0
{
508
0
  static char *function = "libfsxfs_attribute_values_get_utf8_name";
509
510
0
  if( attribute_values == NULL )
511
0
  {
512
0
    libcerror_error_set(
513
0
     error,
514
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
515
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
516
0
     "%s: invalid attribute values.",
517
0
     function );
518
519
0
    return( -1 );
520
0
  }
521
0
  if( libuna_utf8_string_copy_from_utf8_stream(
522
0
       utf8_string,
523
0
       utf8_string_size,
524
0
       attribute_values->name,
525
0
       (size_t) attribute_values->name_size,
526
0
       error ) != 1 )
527
0
  {
528
0
    libcerror_error_set(
529
0
     error,
530
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
531
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
532
0
     "%s: unable to retrieve UTF-8 string.",
533
0
     function );
534
535
0
    return( -1 );
536
0
  }
537
0
  return( 1 );
538
0
}
539
540
/* Compares an UTF-16 string with an attribute name
541
 * Returns LIBUNA_COMPARE_LESS, LIBUNA_COMPARE_EQUAL, LIBUNA_COMPARE_GREATER if successful or -1 on error
542
 */
543
int libfsxfs_attribute_values_compare_name_with_utf16_string(
544
     libfsxfs_attribute_values_t *attribute_values,
545
     const uint16_t *utf16_string,
546
     size_t utf16_string_length,
547
     libcerror_error_t **error )
548
0
{
549
0
  static char *function = "libfsxfs_attribute_values_compare_name_with_utf16_string";
550
0
  int result            = 0;
551
552
0
  if( attribute_values == NULL )
553
0
  {
554
0
    libcerror_error_set(
555
0
     error,
556
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
557
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
558
0
     "%s: invalid attribute values.",
559
0
     function );
560
561
0
    return( -1 );
562
0
  }
563
0
  result = libuna_utf16_string_compare_with_utf8_stream(
564
0
            utf16_string,
565
0
            utf16_string_length,
566
0
            attribute_values->name,
567
0
            attribute_values->name_size,
568
0
            error );
569
570
0
  if( result == -1 )
571
0
  {
572
0
    libcerror_error_set(
573
0
     error,
574
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
575
0
     LIBCERROR_RUNTIME_ERROR_GENERIC,
576
0
     "%s: unable to compare UTF-16 string with name.",
577
0
     function );
578
579
0
    return( -1 );
580
0
  }
581
0
  return( result );
582
0
}
583
584
/* Retrieves the size of the UTF-16 encoded name
585
 * The returned size includes the end of string character
586
 * Returns 1 if successful or -1 on error
587
 */
588
int libfsxfs_attribute_values_get_utf16_name_size(
589
     libfsxfs_attribute_values_t *attribute_values,
590
     size_t *utf16_string_size,
591
     libcerror_error_t **error )
592
0
{
593
0
  static char *function = "libfsxfs_attribute_values_get_utf16_name_size";
594
595
0
  if( attribute_values == NULL )
596
0
  {
597
0
    libcerror_error_set(
598
0
     error,
599
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
600
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
601
0
     "%s: invalid attribute values.",
602
0
     function );
603
604
0
    return( -1 );
605
0
  }
606
0
  if( libuna_utf16_string_size_from_utf8_stream(
607
0
       attribute_values->name,
608
0
       (size_t) attribute_values->name_size,
609
0
       utf16_string_size,
610
0
       error ) != 1 )
611
0
  {
612
0
    libcerror_error_set(
613
0
     error,
614
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
615
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
616
0
     "%s: unable to retrieve UTF-16 string size.",
617
0
     function );
618
619
0
    return( -1 );
620
0
  }
621
0
  return( 1 );
622
0
}
623
624
/* Retrieves the UTF-16 encoded name
625
 * The size should include the end of string character
626
 * Returns 1 if successful or -1 on error
627
 */
628
int libfsxfs_attribute_values_get_utf16_name(
629
     libfsxfs_attribute_values_t *attribute_values,
630
     uint16_t *utf16_string,
631
     size_t utf16_string_size,
632
     libcerror_error_t **error )
633
0
{
634
0
  static char *function = "libfsxfs_attribute_values_get_utf16_name";
635
636
0
  if( attribute_values == NULL )
637
0
  {
638
0
    libcerror_error_set(
639
0
     error,
640
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
641
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
642
0
     "%s: invalid attribute values.",
643
0
     function );
644
645
0
    return( -1 );
646
0
  }
647
0
  if( libuna_utf16_string_copy_from_utf8_stream(
648
0
       utf16_string,
649
0
       utf16_string_size,
650
0
       attribute_values->name,
651
0
       (size_t) attribute_values->name_size,
652
0
       error ) != 1 )
653
0
  {
654
0
    libcerror_error_set(
655
0
     error,
656
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
657
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
658
0
     "%s: unable to retrieve UTF-16 string.",
659
0
     function );
660
661
0
    return( -1 );
662
0
  }
663
0
  return( 1 );
664
0
}
665