Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libfwevt/libfwnt/libfwnt_security_identifier.c
Line
Count
Source
1
/*
2
 * Windows NT Security Identifier (SID) functions
3
 *
4
 * Copyright (C) 2009-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 <types.h>
26
27
#include "libfwnt_definitions.h"
28
#include "libfwnt_libcerror.h"
29
#include "libfwnt_security_identifier.h"
30
#include "libfwnt_types.h"
31
32
/* Creates a security identifier
33
 * Make sure the value security_identifier is referencing, is set to NULL
34
 * Returns 1 if successful or -1 on error
35
 */
36
int libfwnt_security_identifier_initialize(
37
     libfwnt_security_identifier_t **security_identifier,
38
     libcerror_error_t **error )
39
37.9k
{
40
37.9k
  libfwnt_internal_security_identifier_t *internal_security_identifier = NULL;
41
37.9k
  static char *function                                                = "libfwnt_security_identifier_initialize";
42
43
37.9k
  if( security_identifier == 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 security identifier.",
50
0
     function );
51
52
0
    return( -1 );
53
0
  }
54
37.9k
  if( *security_identifier != NULL )
55
26
  {
56
26
    libcerror_error_set(
57
26
     error,
58
26
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
59
26
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
60
26
     "%s: invalid security identifier value already set.",
61
26
     function );
62
63
26
    return( -1 );
64
26
  }
65
37.9k
  internal_security_identifier = memory_allocate_structure(
66
37.9k
                                  libfwnt_internal_security_identifier_t );
67
68
37.9k
  if( internal_security_identifier == 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 security identifier.",
75
0
     function );
76
77
0
    goto on_error;
78
0
  }
79
37.9k
  if( memory_set(
80
37.9k
       internal_security_identifier,
81
37.9k
       0,
82
37.9k
       sizeof( libfwnt_internal_security_identifier_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 security identifier.",
89
0
     function );
90
91
0
    goto on_error;
92
0
  }
93
37.9k
  *security_identifier = (libfwnt_security_identifier_t *) internal_security_identifier;
94
95
37.9k
  return( 1 );
96
97
0
on_error:
98
0
  if( internal_security_identifier != NULL )
99
0
  {
100
0
    memory_free(
101
0
     internal_security_identifier );
102
0
  }
103
0
  return( -1 );
104
37.9k
}
105
106
/* Frees a security identifier
107
 * Returns 1 if successful or -1 on error
108
 */
109
int libfwnt_security_identifier_free(
110
     libfwnt_security_identifier_t **security_identifier,
111
     libcerror_error_t **error )
112
32.4k
{
113
32.4k
  libfwnt_internal_security_identifier_t *internal_security_identifier = NULL;
114
32.4k
  static char *function                                                = "libfwnt_security_identifier_free";
115
32.4k
  int result                                                           = 1;
116
117
32.4k
  if( security_identifier == NULL )
118
0
  {
119
0
    libcerror_error_set(
120
0
     error,
121
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
122
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
123
0
     "%s: invalid security identifier.",
124
0
     function );
125
126
0
    return( -1 );
127
0
  }
128
32.4k
  if( *security_identifier != NULL )
129
32.4k
  {
130
32.4k
    internal_security_identifier = (libfwnt_internal_security_identifier_t *) *security_identifier;
131
132
32.4k
    if( internal_security_identifier->is_managed == 0 )
133
32.4k
    {
134
32.4k
      if( libfwnt_internal_security_identifier_free(
135
32.4k
           &internal_security_identifier,
136
32.4k
           error ) != 1 )
137
0
      {
138
0
        libcerror_error_set(
139
0
         error,
140
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
141
0
         LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
142
0
         "%s: unable to free security identifier.",
143
0
         function );
144
145
0
        result = -1;
146
0
      }
147
32.4k
    }
148
32.4k
    *security_identifier = NULL;
149
32.4k
  }
150
32.4k
  return( result );
151
32.4k
}
152
153
/* Frees a security identifier
154
 * Returns 1 if successful or -1 on error
155
 */
156
int libfwnt_internal_security_identifier_free(
157
     libfwnt_internal_security_identifier_t **internal_security_identifier,
158
     libcerror_error_t **error )
159
37.8k
{
160
37.8k
  static char *function = "libfwnt_internal_security_identifier_free";
161
162
37.8k
  if( internal_security_identifier == NULL )
163
0
  {
164
0
    libcerror_error_set(
165
0
     error,
166
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
167
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
168
0
     "%s: invalid security identifier.",
169
0
     function );
170
171
0
    return( -1 );
172
0
  }
173
37.8k
  if( *internal_security_identifier != NULL )
174
37.8k
  {
175
37.8k
    memory_free(
176
37.8k
     *internal_security_identifier );
177
178
37.8k
    *internal_security_identifier = NULL;
179
37.8k
  }
180
37.8k
  return( 1 );
181
37.8k
}
182
183
/* Converts a security identifier stored in a byte stream into a runtime version
184
 * Returns 1 if successful or -1 on error
185
 */
186
int libfwnt_security_identifier_copy_from_byte_stream(
187
     libfwnt_security_identifier_t *security_identifier,
188
     const uint8_t *byte_stream,
189
     size_t byte_stream_size,
190
     int byte_order,
191
     libcerror_error_t **error )
192
38.0k
{
193
38.0k
  libfwnt_internal_security_identifier_t *internal_security_identifier = NULL;
194
38.0k
  static char *function                                                = "libfwnt_security_identifier_copy_from_byte_stream";
195
38.0k
  size_t security_identifier_size                                      = 0;
196
38.0k
  uint8_t sub_authority_index                                          = 0;
197
198
38.0k
  if( security_identifier == 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 security identifier.",
205
0
     function );
206
207
0
    return( -1 );
208
0
  }
209
38.0k
  internal_security_identifier = (libfwnt_internal_security_identifier_t *) security_identifier;
210
211
38.0k
  if( byte_stream == NULL )
212
136
  {
213
136
    libcerror_error_set(
214
136
     error,
215
136
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
216
136
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
217
136
     "%s: invalid byte stream.",
218
136
     function );
219
220
136
    return( -1 );
221
136
  }
222
37.8k
  if( byte_stream_size < 8 )
223
1.54k
  {
224
1.54k
    libcerror_error_set(
225
1.54k
     error,
226
1.54k
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
227
1.54k
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
228
1.54k
     "%s: byte stream too small.",
229
1.54k
     function );
230
231
1.54k
    return( -1 );
232
1.54k
  }
233
36.3k
  if( byte_stream_size > (size_t) SSIZE_MAX )
234
0
  {
235
0
    libcerror_error_set(
236
0
     error,
237
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
238
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
239
0
     "%s: byte stream size exceeds maximum.",
240
0
     function );
241
242
0
    return( -1 );
243
0
  }
244
36.3k
  if( byte_order != LIBFWNT_ENDIAN_LITTLE )
245
113
  {
246
113
    libcerror_error_set(
247
113
     error,
248
113
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
249
113
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
250
113
     "%s: unsupported byte order.",
251
113
     function );
252
253
113
    return( -1 );
254
113
  }
255
36.2k
  internal_security_identifier->revision_number           = byte_stream[ 0 ];
256
36.2k
  internal_security_identifier->number_of_sub_authorities = byte_stream[ 1 ];
257
258
36.2k
  if( internal_security_identifier->number_of_sub_authorities > 15 )
259
2.87k
  {
260
2.87k
    libcerror_error_set(
261
2.87k
     error,
262
2.87k
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
263
2.87k
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
264
2.87k
     "%s: unsupported security identifier contains more than 15 sub authoritites.",
265
2.87k
     function );
266
267
2.87k
    return( -1 );
268
2.87k
  }
269
33.3k
  security_identifier_size = 8 + ( (size_t) internal_security_identifier->number_of_sub_authorities * 4 );
270
271
33.3k
  if( (size_t) security_identifier_size > byte_stream_size )
272
1.63k
  {
273
1.63k
    libcerror_error_set(
274
1.63k
     error,
275
1.63k
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
276
1.63k
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
277
1.63k
     "%s: byte stream too small.",
278
1.63k
     function );
279
280
1.63k
    return( -1 );
281
1.63k
  }
282
31.7k
  byte_stream += 2;
283
284
  /* The authority is stored as a 48-bit value
285
   */
286
31.7k
  byte_stream_copy_to_uint48_big_endian(
287
31.7k
   byte_stream,
288
31.7k
   internal_security_identifier->authority );
289
290
31.7k
  byte_stream += 6;
291
292
31.7k
  for( sub_authority_index = 0;
293
90.8k
       sub_authority_index < internal_security_identifier->number_of_sub_authorities;
294
59.0k
       sub_authority_index++ )
295
59.0k
  {
296
59.0k
    byte_stream_copy_to_uint32_little_endian(
297
59.0k
     byte_stream,
298
59.0k
     internal_security_identifier->sub_authority[ sub_authority_index ] );
299
300
59.0k
    byte_stream += 4;
301
59.0k
  }
302
31.7k
  return( 1 );
303
33.3k
}
304
305
/* Deterimes the size of the string for the security identifier
306
 * The string size includes the end of string character
307
 * Returns 1 if successful or -1 on error
308
 */
309
int libfwnt_security_identifier_get_string_size(
310
     libfwnt_security_identifier_t *security_identifier,
311
     size_t *string_size,
312
     uint32_t string_format_flags,
313
     libcerror_error_t **error )
314
26.6k
{
315
26.6k
  libfwnt_internal_security_identifier_t *internal_security_identifier = NULL;
316
26.6k
  static char *function                                                = "libfwnt_security_identifier_get_string_size";
317
26.6k
  size_t value_string_index                                            = 0;
318
26.6k
  uint64_t value_64bit                                                 = 0;
319
26.6k
  uint8_t sub_authority_index                                          = 0;
320
321
26.6k
  if( security_identifier == NULL )
322
0
  {
323
0
    libcerror_error_set(
324
0
     error,
325
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
326
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
327
0
     "%s: invalid security identifier.",
328
0
     function );
329
330
0
    return( -1 );
331
0
  }
332
26.6k
  internal_security_identifier = (libfwnt_internal_security_identifier_t *) security_identifier;
333
334
26.6k
  if( string_size == NULL )
335
4.18k
  {
336
4.18k
    libcerror_error_set(
337
4.18k
     error,
338
4.18k
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
339
4.18k
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
340
4.18k
     "%s: invalid string size.",
341
4.18k
     function );
342
343
4.18k
    return( -1 );
344
4.18k
  }
345
22.4k
  if( string_format_flags != 0 )
346
0
  {
347
0
    libcerror_error_set(
348
0
     error,
349
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
350
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
351
0
     "%s: unsupported string format flags.",
352
0
     function );
353
354
0
    return( -1 );
355
0
  }
356
22.4k
  value_string_index = 2;
357
358
22.4k
  value_64bit = internal_security_identifier->revision_number;
359
360
22.4k
  do
361
36.0k
  {
362
36.0k
    value_string_index++;
363
364
36.0k
    value_64bit /= 10;
365
36.0k
  }
366
36.0k
  while( value_64bit > 0 );
367
368
22.4k
  value_string_index++;
369
370
22.4k
  value_64bit = internal_security_identifier->authority;
371
372
22.4k
  do
373
203k
  {
374
203k
    value_string_index++;
375
376
203k
    value_64bit /= 10;
377
203k
  }
378
203k
  while( value_64bit > 0 );
379
380
22.4k
  for( sub_authority_index = 0;
381
54.9k
       sub_authority_index < internal_security_identifier->number_of_sub_authorities;
382
32.5k
       sub_authority_index++ )
383
32.5k
  {
384
32.5k
    value_string_index++;
385
386
32.5k
    value_64bit = internal_security_identifier->sub_authority[ sub_authority_index ];
387
388
32.5k
    do
389
231k
    {
390
231k
      value_string_index++;
391
392
231k
      value_64bit /= 10;
393
231k
    }
394
231k
    while( value_64bit > 0 );
395
32.5k
  }
396
22.4k
  value_string_index++;
397
398
22.4k
  *string_size = value_string_index;
399
400
22.4k
  return( 1 );
401
22.4k
}
402
403
/* Converts the security identifier into an UTF-8 string
404
 * The string size should include the end of string character
405
 * Returns 1 if successful or -1 on error
406
 */
407
int libfwnt_security_identifier_copy_to_utf8_string(
408
     libfwnt_security_identifier_t *security_identifier,
409
     uint8_t *utf8_string,
410
     size_t utf8_string_size,
411
     uint32_t string_format_flags,
412
     libcerror_error_t **error )
413
2.07k
{
414
2.07k
  static char *function    = "libfwnt_security_identifier_copy_to_utf8_string";
415
2.07k
  size_t utf8_string_index = 0;
416
417
2.07k
  if( libfwnt_security_identifier_copy_to_utf8_string_with_index(
418
2.07k
       security_identifier,
419
2.07k
       utf8_string,
420
2.07k
       utf8_string_size,
421
2.07k
       &utf8_string_index,
422
2.07k
       string_format_flags,
423
2.07k
       error ) != 1 )
424
9
  {
425
9
    libcerror_error_set(
426
9
     error,
427
9
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
428
9
     LIBCERROR_RUNTIME_ERROR_COPY_FAILED,
429
9
     "%s: unable to copy security identifier to UTF-8 string.",
430
9
     function );
431
432
9
    return( -1 );
433
9
  }
434
2.06k
  return( 1 );
435
2.07k
}
436
437
/* Converts the security identifier into an UTF-8 string
438
 * The string size should include the end of string character
439
 * Returns 1 if successful or -1 on error
440
 */
441
int libfwnt_security_identifier_copy_to_utf8_string_with_index(
442
     libfwnt_security_identifier_t *security_identifier,
443
     uint8_t *utf8_string,
444
     size_t utf8_string_size,
445
     size_t *utf8_string_index,
446
     uint32_t string_format_flags,
447
     libcerror_error_t **error )
448
7.44k
{
449
7.44k
  libfwnt_internal_security_identifier_t *internal_security_identifier = NULL;
450
7.44k
  static char *function                                                = "libfwnt_security_identifier_copy_to_utf8_string";
451
7.44k
  size_t string_index                                                  = 0;
452
7.44k
  size_t value_string_index                                            = 0;
453
7.44k
  size_t value_string_length                                           = 0;
454
7.44k
  uint64_t value_64bit                                                 = 0;
455
7.44k
  uint8_t sub_authority_index                                          = 0;
456
457
7.44k
  if( security_identifier == NULL )
458
0
  {
459
0
    libcerror_error_set(
460
0
     error,
461
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
462
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
463
0
     "%s: invalid security identifier.",
464
0
     function );
465
466
0
    return( -1 );
467
0
  }
468
7.44k
  internal_security_identifier = (libfwnt_internal_security_identifier_t *) security_identifier;
469
470
7.44k
  if( utf8_string == NULL )
471
0
  {
472
0
    libcerror_error_set(
473
0
     error,
474
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
475
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
476
0
     "%s: invalid UTF-8 size.",
477
0
     function );
478
479
0
    return( -1 );
480
0
  }
481
7.44k
  if( utf8_string_size > (size_t) SSIZE_MAX )
482
0
  {
483
0
    libcerror_error_set(
484
0
     error,
485
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
486
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
487
0
     "%s: UTF-8 string size exceeds maximum.",
488
0
     function );
489
490
0
    return( -1 );
491
0
  }
492
7.44k
  if( utf8_string_index == NULL )
493
0
  {
494
0
    libcerror_error_set(
495
0
     error,
496
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
497
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
498
0
     "%s: invalid UTF-8 string index.",
499
0
     function );
500
501
0
    return( -1 );
502
0
  }
503
7.44k
  if( string_format_flags != 0 )
504
0
  {
505
0
    libcerror_error_set(
506
0
     error,
507
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
508
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
509
0
     "%s: unsupported string format flags.",
510
0
     function );
511
512
0
    return( -1 );
513
0
  }
514
7.44k
  string_index = *utf8_string_index;
515
516
7.44k
  if( ( string_index + 2 ) >= utf8_string_size )
517
0
  {
518
0
    libcerror_error_set(
519
0
     error,
520
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
521
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
522
0
     "%s: UTF-8 string is too small.",
523
0
     function );
524
525
0
    return( -1 );
526
0
  }
527
7.44k
  utf8_string[ string_index++ ] = (uint8_t) 'S';
528
7.44k
  utf8_string[ string_index++ ] = (uint8_t) '-';
529
530
7.44k
  value_64bit         = internal_security_identifier->revision_number;
531
7.44k
  value_string_length = 0;
532
533
7.44k
  do
534
11.5k
  {
535
11.5k
    value_string_length++;
536
537
11.5k
    value_64bit /= 10;
538
11.5k
  }
539
11.5k
  while( value_64bit > 0 );
540
541
7.44k
  if( ( string_index + value_string_length + 1 ) >= utf8_string_size )
542
0
  {
543
0
    libcerror_error_set(
544
0
     error,
545
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
546
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
547
0
     "%s: UTF-8 string is too small.",
548
0
     function );
549
550
0
    return( -1 );
551
0
  }
552
7.44k
  value_64bit        = internal_security_identifier->revision_number;
553
7.44k
  value_string_index = value_string_length;
554
555
19.0k
  while( value_string_index > 0 )
556
11.5k
  {
557
11.5k
    value_string_index--;
558
559
11.5k
    utf8_string[ string_index + value_string_index ] = (uint8_t) '0' + (uint8_t) ( value_64bit % 10 );
560
561
11.5k
    value_64bit /= 10;
562
11.5k
  }
563
7.44k
  string_index += value_string_length;
564
565
7.44k
  utf8_string[ string_index++ ] = (uint8_t) '-';
566
567
7.44k
  value_64bit         = internal_security_identifier->authority;
568
7.44k
  value_string_length = 0;
569
570
7.44k
  do
571
58.6k
  {
572
58.6k
    value_string_length++;
573
574
58.6k
    value_64bit /= 10;
575
58.6k
  }
576
58.6k
  while( value_64bit > 0 );
577
578
7.44k
  if( ( string_index + value_string_length ) >= utf8_string_size )
579
0
  {
580
0
    libcerror_error_set(
581
0
     error,
582
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
583
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
584
0
     "%s: UTF-8 string is too small.",
585
0
     function );
586
587
0
    return( -1 );
588
0
  }
589
7.44k
  value_64bit        = internal_security_identifier->authority;
590
7.44k
  value_string_index = value_string_length;
591
592
66.0k
  while( value_string_index > 0 )
593
58.6k
  {
594
58.6k
    value_string_index--;
595
596
58.6k
    utf8_string[ string_index + value_string_index ] = (uint8_t) '0' + (uint8_t) ( value_64bit % 10 );
597
598
58.6k
    value_64bit /= 10;
599
58.6k
  }
600
7.44k
  string_index += value_string_length;
601
602
7.44k
  for( sub_authority_index = 0;
603
21.1k
       sub_authority_index < internal_security_identifier->number_of_sub_authorities;
604
13.7k
       sub_authority_index++ )
605
13.7k
  {
606
13.7k
    value_64bit         = internal_security_identifier->sub_authority[ sub_authority_index ];
607
13.7k
    value_string_length = 0;
608
609
13.7k
    do
610
93.0k
    {
611
93.0k
      value_string_length++;
612
613
93.0k
      value_64bit /= 10;
614
93.0k
    }
615
93.0k
    while( value_64bit > 0 );
616
617
13.7k
    if( ( string_index + value_string_length + 1 ) >= utf8_string_size )
618
18
    {
619
18
      libcerror_error_set(
620
18
       error,
621
18
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
622
18
       LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
623
18
       "%s: UTF-8 string is too small.",
624
18
       function );
625
626
18
      return( -1 );
627
18
    }
628
13.7k
    utf8_string[ string_index++ ] = (uint8_t) '-';
629
630
13.7k
    value_64bit        = internal_security_identifier->sub_authority[ sub_authority_index ];
631
13.7k
    value_string_index = value_string_length;
632
633
106k
    while( value_string_index > 0 )
634
92.8k
    {
635
92.8k
      value_string_index--;
636
637
92.8k
      utf8_string[ string_index + value_string_index ] = (uint8_t) '0' + (uint8_t) ( value_64bit % 10 );
638
639
92.8k
      value_64bit /= 10;
640
92.8k
    }
641
13.7k
    string_index += value_string_length;
642
13.7k
  }
643
7.42k
  if( string_index >= utf8_string_size )
644
0
  {
645
0
    libcerror_error_set(
646
0
     error,
647
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
648
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
649
0
     "%s: UTF-8 string is too small.",
650
0
     function );
651
652
0
    return( -1 );
653
0
  }
654
7.42k
  utf8_string[ string_index++ ] = 0;
655
656
7.42k
  *utf8_string_index = string_index;
657
658
7.42k
  return( 1 );
659
7.42k
}
660
661
/* Converts the security identifier into an UTF-16 string
662
 * The string size should include the end of string character
663
 * Returns 1 if successful or -1 on error
664
 */
665
int libfwnt_security_identifier_copy_to_utf16_string(
666
     libfwnt_security_identifier_t *security_identifier,
667
     uint16_t *utf16_string,
668
     size_t utf16_string_size,
669
     uint32_t string_format_flags,
670
     libcerror_error_t **error )
671
2.07k
{
672
2.07k
  static char *function    = "libfwnt_security_identifier_copy_to_utf16_string";
673
2.07k
  size_t utf16_string_index = 0;
674
675
2.07k
  if( libfwnt_security_identifier_copy_to_utf16_string_with_index(
676
2.07k
       security_identifier,
677
2.07k
       utf16_string,
678
2.07k
       utf16_string_size,
679
2.07k
       &utf16_string_index,
680
2.07k
       string_format_flags,
681
2.07k
       error ) != 1 )
682
9
  {
683
9
    libcerror_error_set(
684
9
     error,
685
9
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
686
9
     LIBCERROR_RUNTIME_ERROR_COPY_FAILED,
687
9
     "%s: unable to copy security identifier to UTF-16 string.",
688
9
     function );
689
690
9
    return( -1 );
691
9
  }
692
2.06k
  return( 1 );
693
2.07k
}
694
695
/* Converts the security identifier into an UTF-16 string
696
 * The string size should include the end of string character
697
 * Returns 1 if successful or -1 on error
698
 */
699
int libfwnt_security_identifier_copy_to_utf16_string_with_index(
700
     libfwnt_security_identifier_t *security_identifier,
701
     uint16_t *utf16_string,
702
     size_t utf16_string_size,
703
     size_t *utf16_string_index,
704
     uint32_t string_format_flags,
705
     libcerror_error_t **error )
706
6.49k
{
707
6.49k
  libfwnt_internal_security_identifier_t *internal_security_identifier = NULL;
708
6.49k
  static char *function                                                = "libfwnt_security_identifier_copy_to_utf16_string";
709
6.49k
  size_t string_index                                                  = 0;
710
6.49k
  size_t value_string_index                                            = 0;
711
6.49k
  size_t value_string_length                                           = 0;
712
6.49k
  uint64_t value_64bit                                                 = 0;
713
6.49k
  uint8_t sub_authority_index                                          = 0;
714
715
6.49k
  if( security_identifier == NULL )
716
0
  {
717
0
    libcerror_error_set(
718
0
     error,
719
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
720
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
721
0
     "%s: invalid security identifier.",
722
0
     function );
723
724
0
    return( -1 );
725
0
  }
726
6.49k
  internal_security_identifier = (libfwnt_internal_security_identifier_t *) security_identifier;
727
728
6.49k
  if( utf16_string == NULL )
729
0
  {
730
0
    libcerror_error_set(
731
0
     error,
732
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
733
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
734
0
     "%s: invalid UTF-16 string.",
735
0
     function );
736
737
0
    return( -1 );
738
0
  }
739
6.49k
  if( utf16_string_size > (size_t) SSIZE_MAX )
740
0
  {
741
0
    libcerror_error_set(
742
0
     error,
743
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
744
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
745
0
     "%s: UTF-16 string size exceeds maximum.",
746
0
     function );
747
748
0
    return( -1 );
749
0
  }
750
6.49k
  if( utf16_string_index == NULL )
751
0
  {
752
0
    libcerror_error_set(
753
0
     error,
754
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
755
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
756
0
     "%s: invalid UTF-16 string index.",
757
0
     function );
758
759
0
    return( -1 );
760
0
  }
761
6.49k
  if( string_format_flags != 0 )
762
0
  {
763
0
    libcerror_error_set(
764
0
     error,
765
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
766
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
767
0
     "%s: unsupported string format flags.",
768
0
     function );
769
770
0
    return( -1 );
771
0
  }
772
6.49k
  string_index = *utf16_string_index;
773
774
6.49k
  if( ( string_index + 2 ) >= utf16_string_size )
775
0
  {
776
0
    libcerror_error_set(
777
0
     error,
778
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
779
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
780
0
     "%s: UTF-16 string is too small.",
781
0
     function );
782
783
0
    return( -1 );
784
0
  }
785
6.49k
  utf16_string[ string_index++ ] = (uint16_t) 'S';
786
6.49k
  utf16_string[ string_index++ ] = (uint16_t) '-';
787
788
6.49k
  value_64bit         = internal_security_identifier->revision_number;
789
6.49k
  value_string_length = 0;
790
791
6.49k
  do
792
9.65k
  {
793
9.65k
    value_string_length++;
794
795
9.65k
    value_64bit /= 10;
796
9.65k
  }
797
9.65k
  while( value_64bit > 0 );
798
799
6.49k
  if( ( string_index + value_string_length + 1 ) >= utf16_string_size )
800
0
  {
801
0
    libcerror_error_set(
802
0
     error,
803
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
804
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
805
0
     "%s: UTF-16 string is too small.",
806
0
     function );
807
808
0
    return( -1 );
809
0
  }
810
6.49k
  value_64bit        = internal_security_identifier->revision_number;
811
6.49k
  value_string_index = value_string_length;
812
813
16.1k
  while( value_string_index > 0 )
814
9.65k
  {
815
9.65k
    value_string_index--;
816
817
9.65k
    utf16_string[ string_index + value_string_index ] = (uint16_t) '0' + (uint16_t) ( value_64bit % 10 );
818
819
9.65k
    value_64bit /= 10;
820
9.65k
  }
821
6.49k
  string_index += value_string_length;
822
823
6.49k
  utf16_string[ string_index++ ] = (uint16_t) '-';
824
825
6.49k
  value_64bit         = internal_security_identifier->authority;
826
6.49k
  value_string_length = 0;
827
828
6.49k
  do
829
50.1k
  {
830
50.1k
    value_string_length++;
831
832
50.1k
    value_64bit /= 10;
833
50.1k
  }
834
50.1k
  while( value_64bit > 0 );
835
836
6.49k
  if( ( string_index + value_string_length ) >= utf16_string_size )
837
0
  {
838
0
    libcerror_error_set(
839
0
     error,
840
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
841
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
842
0
     "%s: UTF-16 string is too small.",
843
0
     function );
844
845
0
    return( -1 );
846
0
  }
847
6.49k
  value_64bit        = internal_security_identifier->authority;
848
6.49k
  value_string_index = value_string_length;
849
850
56.5k
  while( value_string_index > 0 )
851
50.1k
  {
852
50.1k
    value_string_index--;
853
854
50.1k
    utf16_string[ string_index + value_string_index ] = (uint16_t) '0' + (uint16_t) ( value_64bit % 10 );
855
856
50.1k
    value_64bit /= 10;
857
50.1k
  }
858
6.49k
  string_index += value_string_length;
859
860
6.49k
  for( sub_authority_index = 0;
861
17.0k
       sub_authority_index < internal_security_identifier->number_of_sub_authorities;
862
10.5k
       sub_authority_index++ )
863
10.5k
  {
864
10.5k
    value_64bit         = internal_security_identifier->sub_authority[ sub_authority_index ];
865
10.5k
    value_string_length = 0;
866
867
10.5k
    do
868
69.3k
    {
869
69.3k
      value_64bit /= 10;
870
871
69.3k
      value_string_length++;
872
69.3k
    }
873
69.3k
    while( value_64bit > 0 );
874
875
10.5k
    if( ( string_index + value_string_length + 1 ) >= utf16_string_size )
876
18
    {
877
18
      libcerror_error_set(
878
18
       error,
879
18
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
880
18
       LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
881
18
       "%s: UTF-16 string is too small.",
882
18
       function );
883
884
18
      return( -1 );
885
18
    }
886
10.5k
    utf16_string[ string_index++ ] = (uint16_t) '-';
887
888
10.5k
    value_64bit        = internal_security_identifier->sub_authority[ sub_authority_index ];
889
10.5k
    value_string_index = value_string_length;
890
891
79.7k
    while( value_string_index > 0 )
892
69.2k
    {
893
69.2k
      value_string_index--;
894
895
69.2k
      utf16_string[ string_index + value_string_index ] = (uint16_t) '0' + (uint16_t) ( value_64bit % 10 );
896
897
69.2k
      value_64bit /= 10;
898
69.2k
    }
899
10.5k
    string_index += value_string_length;
900
10.5k
  }
901
6.47k
  if( string_index >= utf16_string_size )
902
0
  {
903
0
    libcerror_error_set(
904
0
     error,
905
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
906
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
907
0
     "%s: UTF-16 string is too small.",
908
0
     function );
909
910
0
    return( -1 );
911
0
  }
912
6.47k
  utf16_string[ string_index++ ] = 0;
913
914
6.47k
  *utf16_string_index = string_index;
915
916
6.47k
  return( 1 );
917
6.47k
}
918
919
/* Converts the security identifier into an UTF-32 string
920
 * The string size should include the end of string character
921
 * Returns 1 if successful or -1 on error
922
 */
923
int libfwnt_security_identifier_copy_to_utf32_string(
924
     libfwnt_security_identifier_t *security_identifier,
925
     uint32_t *utf32_string,
926
     size_t utf32_string_size,
927
     uint32_t string_format_flags,
928
     libcerror_error_t **error )
929
117
{
930
117
  static char *function    = "libfwnt_security_identifier_copy_to_utf32_string";
931
117
  size_t utf32_string_index = 0;
932
933
117
  if( libfwnt_security_identifier_copy_to_utf32_string_with_index(
934
117
       security_identifier,
935
117
       utf32_string,
936
117
       utf32_string_size,
937
117
       &utf32_string_index,
938
117
       string_format_flags,
939
117
       error ) != 1 )
940
9
  {
941
9
    libcerror_error_set(
942
9
     error,
943
9
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
944
9
     LIBCERROR_RUNTIME_ERROR_COPY_FAILED,
945
9
     "%s: unable to copy security identifier to UTF-32 string.",
946
9
     function );
947
948
9
    return( -1 );
949
9
  }
950
108
  return( 1 );
951
117
}
952
953
/* Converts the security identifier into an UTF-32 string
954
 * The string size should include the end of string character
955
 * Returns 1 if successful or -1 on error
956
 */
957
int libfwnt_security_identifier_copy_to_utf32_string_with_index(
958
     libfwnt_security_identifier_t *security_identifier,
959
     uint32_t *utf32_string,
960
     size_t utf32_string_size,
961
     size_t *utf32_string_index,
962
     uint32_t string_format_flags,
963
     libcerror_error_t **error )
964
234
{
965
234
  libfwnt_internal_security_identifier_t *internal_security_identifier = NULL;
966
234
  static char *function                                                = "libfwnt_security_identifier_copy_to_utf32_string_with_index";
967
234
  size_t string_index                                                  = 0;
968
234
  size_t value_string_index                                            = 0;
969
234
  size_t value_string_length                                           = 0;
970
234
  uint64_t value_64bit                                                 = 0;
971
234
  uint8_t sub_authority_index                                          = 0;
972
973
234
  if( security_identifier == NULL )
974
0
  {
975
0
    libcerror_error_set(
976
0
     error,
977
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
978
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
979
0
     "%s: invalid security identifier.",
980
0
     function );
981
982
0
    return( -1 );
983
0
  }
984
234
  internal_security_identifier = (libfwnt_internal_security_identifier_t *) security_identifier;
985
986
234
  if( utf32_string == NULL )
987
0
  {
988
0
    libcerror_error_set(
989
0
     error,
990
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
991
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
992
0
     "%s: invalid UTF-32 string.",
993
0
     function );
994
995
0
    return( -1 );
996
0
  }
997
234
  if( utf32_string_size > (size_t) SSIZE_MAX )
998
0
  {
999
0
    libcerror_error_set(
1000
0
     error,
1001
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1002
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1003
0
     "%s: UTF-32 string size exceeds maximum.",
1004
0
     function );
1005
1006
0
    return( -1 );
1007
0
  }
1008
234
  if( utf32_string_index == NULL )
1009
0
  {
1010
0
    libcerror_error_set(
1011
0
     error,
1012
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1013
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1014
0
     "%s: invalid UTF-32 string index.",
1015
0
     function );
1016
1017
0
    return( -1 );
1018
0
  }
1019
234
  if( string_format_flags != 0 )
1020
0
  {
1021
0
    libcerror_error_set(
1022
0
     error,
1023
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1024
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
1025
0
     "%s: unsupported string format flags.",
1026
0
     function );
1027
1028
0
    return( -1 );
1029
0
  }
1030
234
  string_index = *utf32_string_index;
1031
1032
234
  if( ( string_index + 2 ) > utf32_string_size )
1033
0
  {
1034
0
    libcerror_error_set(
1035
0
     error,
1036
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1037
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
1038
0
     "%s: UTF-32 string is too small.",
1039
0
     function );
1040
1041
0
    return( -1 );
1042
0
  }
1043
234
  utf32_string[ string_index++ ] = (uint32_t) 'S';
1044
234
  utf32_string[ string_index++ ] = (uint32_t) '-';
1045
1046
234
  value_64bit         = internal_security_identifier->revision_number;
1047
234
  value_string_length = 0;
1048
1049
234
  do
1050
462
  {
1051
462
    value_64bit /= 10;
1052
1053
462
    value_string_length++;
1054
462
  }
1055
462
  while( value_64bit > 0 );
1056
1057
234
  if( ( string_index + value_string_length + 1 ) >= utf32_string_size )
1058
0
  {
1059
0
    libcerror_error_set(
1060
0
     error,
1061
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1062
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
1063
0
     "%s: UTF-32 string is too small.",
1064
0
     function );
1065
1066
0
    return( -1 );
1067
0
  }
1068
234
  value_64bit        = internal_security_identifier->revision_number;
1069
234
  value_string_index = value_string_length;
1070
1071
696
  while( value_string_index > 0 )
1072
462
  {
1073
462
    value_string_index--;
1074
1075
462
    utf32_string[ string_index + value_string_index ] = (uint32_t) '0' + (uint32_t) ( value_64bit % 10 );
1076
1077
462
    value_64bit /= 10;
1078
462
  }
1079
234
  string_index += value_string_length;
1080
1081
234
  utf32_string[ string_index++ ] = (uint32_t) '-';
1082
1083
234
  value_64bit         = internal_security_identifier->authority;
1084
234
  value_string_length = 0;
1085
1086
234
  do
1087
1.75k
  {
1088
1.75k
    value_string_length++;
1089
1090
1.75k
    value_64bit /= 10;
1091
1.75k
  }
1092
1.75k
  while( value_64bit > 0 );
1093
1094
234
  if( ( string_index + value_string_length ) >= utf32_string_size )
1095
0
  {
1096
0
    libcerror_error_set(
1097
0
     error,
1098
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1099
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
1100
0
     "%s: UTF-32 string is too small.",
1101
0
     function );
1102
1103
0
    return( -1 );
1104
0
  }
1105
234
  value_64bit        = internal_security_identifier->authority;
1106
234
  value_string_index = value_string_length;
1107
1108
1.98k
  while( value_string_index > 0 )
1109
1.75k
  {
1110
1.75k
    value_string_index--;
1111
1112
1.75k
    utf32_string[ string_index + value_string_index ] = (uint32_t) '0' + (uint32_t) ( value_64bit % 10 );
1113
1114
1.75k
    value_64bit /= 10;
1115
1.75k
  }
1116
234
  string_index += value_string_length;
1117
1118
234
  for( sub_authority_index = 0;
1119
1.42k
       sub_authority_index < internal_security_identifier->number_of_sub_authorities;
1120
1.18k
       sub_authority_index++ )
1121
1.20k
  {
1122
1.20k
    value_64bit         = internal_security_identifier->sub_authority[ sub_authority_index ];
1123
1.20k
    value_string_length = 0;
1124
1125
1.20k
    do
1126
5.70k
    {
1127
5.70k
      value_string_length++;
1128
1129
5.70k
      value_64bit /= 10;
1130
5.70k
    }
1131
5.70k
    while( value_64bit > 0 );
1132
1133
1.20k
    if( ( string_index + value_string_length + 1 ) >= utf32_string_size )
1134
18
    {
1135
18
      libcerror_error_set(
1136
18
       error,
1137
18
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1138
18
       LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
1139
18
       "%s: UTF-32 string is too small.",
1140
18
       function );
1141
1142
18
      return( -1 );
1143
18
    }
1144
1.18k
    utf32_string[ string_index++ ] = (uint32_t) '-';
1145
1146
1.18k
    value_64bit        = internal_security_identifier->sub_authority[ sub_authority_index ];
1147
1.18k
    value_string_index = value_string_length;
1148
1149
6.71k
    while( value_string_index > 0 )
1150
5.53k
    {
1151
5.53k
      value_string_index--;
1152
1153
5.53k
      utf32_string[ string_index + value_string_index ] = (uint32_t) '0' + (uint32_t) ( value_64bit % 10 );
1154
1155
5.53k
      value_64bit /= 10;
1156
5.53k
    }
1157
1.18k
    string_index += value_string_length;
1158
1.18k
  }
1159
216
  if( string_index >= utf32_string_size )
1160
0
  {
1161
0
    libcerror_error_set(
1162
0
     error,
1163
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1164
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
1165
0
     "%s: UTF-32 string is too small.",
1166
0
     function );
1167
1168
0
    return( -1 );
1169
0
  }
1170
216
  utf32_string[ string_index++ ] = 0;
1171
1172
216
  *utf32_string_index = string_index;
1173
1174
216
  return( 1 );
1175
216
}
1176