Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libevtx/libfwevt/libfwevt_xml_string.c
Line
Count
Source
1
/*
2
 * XML string functions
3
 *
4
 * Copyright (C) 2011-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 <types.h>
24
25
#include "libfwevt_libcerror.h"
26
#include "libfwevt_libuna.h"
27
#include "libfwevt_xml_string.h"
28
29
/* Determines the size of an UTF-8 XML string from an UTF-16 stream
30
 * Returns 1 if successful or -1 on error
31
 */
32
int libfwevt_utf8_xml_string_size_from_utf16_stream(
33
     const uint8_t *utf16_stream,
34
     size_t utf16_stream_size,
35
     int byte_order,
36
     size_t *utf8_string_size,
37
     libcerror_error_t **error )
38
315k
{
39
315k
  static char *function                        = "libfwevt_utf8_xml_string_size_from_utf16_stream";
40
315k
  libuna_unicode_character_t unicode_character = 0;
41
315k
  size_t safe_utf8_string_size                 = 0;
42
315k
  size_t utf16_stream_index                    = 0;
43
315k
  int read_byte_order                          = 0;
44
315k
  int result                                   = 0;
45
46
315k
  if( utf16_stream == 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 UTF-16 stream.",
53
0
     function );
54
55
0
    return( -1 );
56
0
  }
57
315k
  if( utf16_stream_size > (size_t) SSIZE_MAX )
58
0
  {
59
0
    libcerror_error_set(
60
0
     error,
61
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
62
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
63
0
     "%s: invalid UTF-16 stream size value exceeds maximum.",
64
0
     function );
65
66
0
    return( -1 );
67
0
  }
68
315k
  if( ( utf16_stream_size % 2 ) != 0 )
69
0
  {
70
0
    libcerror_error_set(
71
0
     error,
72
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
73
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
74
0
     "%s: missing UTF-16 stream value.",
75
0
     function );
76
77
0
    return( -1 );
78
0
  }
79
315k
  if( utf8_string_size == NULL )
80
0
  {
81
0
    libcerror_error_set(
82
0
     error,
83
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
84
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
85
0
     "%s: invalid UTF-8 string size.",
86
0
     function );
87
88
0
    return( -1 );
89
0
  }
90
315k
  if( utf16_stream_size == 0 )
91
0
  {
92
0
    return( 1 );
93
0
  }
94
  /* Check if UTF-16 stream is in big or little endian
95
   */
96
315k
  if( utf16_stream_size >= 2 )
97
315k
  {
98
315k
    if( ( utf16_stream[ 0 ] == 0xfe )
99
10.4k
     && ( utf16_stream[ 1 ] == 0xff ) )
100
4.50k
    {
101
4.50k
      read_byte_order    = LIBUNA_ENDIAN_BIG;
102
4.50k
      utf16_stream_index = 2;
103
4.50k
    }
104
310k
    else if( ( utf16_stream[ 0 ] == 0xff )
105
22.6k
          && ( utf16_stream[ 1 ] == 0xfe ) )
106
4.42k
    {
107
4.42k
      read_byte_order    = LIBUNA_ENDIAN_LITTLE;
108
4.42k
      utf16_stream_index = 2;
109
4.42k
    }
110
315k
    if( byte_order == 0 )
111
0
    {
112
0
      byte_order = read_byte_order;
113
0
    }
114
315k
  }
115
4.31M
  while( ( utf16_stream_index + 1 ) < utf16_stream_size )
116
4.13M
  {
117
    /* Convert the UTF-16 stream bytes into an Unicode character
118
     */
119
4.13M
    if( libuna_unicode_character_copy_from_utf16_stream(
120
4.13M
         &unicode_character,
121
4.13M
         utf16_stream,
122
4.13M
         utf16_stream_size,
123
4.13M
         &utf16_stream_index,
124
4.13M
         byte_order,
125
4.13M
         error ) != 1 )
126
502
    {
127
502
      libcerror_error_set(
128
502
       error,
129
502
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
130
502
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
131
502
       "%s: unable to copy Unicode character from UTF-16 stream.",
132
502
       function );
133
134
502
      return( -1 );
135
502
    }
136
4.13M
    switch( unicode_character )
137
4.13M
    {
138
      /* Replace & by &amp; */
139
12.8k
      case (libuna_unicode_character_t) '&':
140
12.8k
        safe_utf8_string_size += 5;
141
12.8k
        break;
142
143
      /* Replace < by &lt; and > by &gt; */
144
8.60k
      case (libuna_unicode_character_t) '<':
145
12.4k
      case (libuna_unicode_character_t) '>':
146
12.4k
        safe_utf8_string_size += 4;
147
12.4k
        break;
148
149
      /* Replace ' by &apos; and " by &quot; */
150
/* TODO disabled for now since Event Viewer does not uses it
151
      case (libuna_unicode_character_t) '\'':
152
      case (libuna_unicode_character_t) '"':
153
        safe_utf8_string_size += 6;
154
        break;
155
*/
156
157
4.11M
      default:
158
        /* Determine how many UTF-8 character bytes are required
159
         */
160
4.11M
        if( ( byte_order & LIBUNA_UTF16_STREAM_ALLOW_UNPAIRED_SURROGATE ) == 0 )
161
4.11M
        {
162
4.11M
          result = libuna_unicode_character_size_to_utf8(
163
4.11M
              unicode_character,
164
4.11M
              &safe_utf8_string_size,
165
4.11M
              error );
166
4.11M
        }
167
0
        else
168
0
        {
169
0
          result = libuna_unicode_character_size_to_utf8_rfc2279(
170
0
              unicode_character,
171
0
              &safe_utf8_string_size,
172
0
              error );
173
0
        }
174
4.11M
        if( result != 1 )
175
0
        {
176
0
          libcerror_error_set(
177
0
           error,
178
0
           LIBCERROR_ERROR_DOMAIN_CONVERSION,
179
0
           LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
180
0
           "%s: unable to unable to determine size of Unicode character in UTF-8.",
181
0
           function );
182
183
0
          return( -1 );
184
0
        }
185
4.11M
        break;
186
4.13M
    }
187
4.13M
    if( unicode_character == 0 )
188
137k
    {
189
137k
      break;
190
137k
    }
191
4.13M
  }
192
  /* Check if the string is terminated with an end-of-string character
193
   */
194
314k
  if( unicode_character != 0 )
195
177k
  {
196
177k
    safe_utf8_string_size++;
197
177k
  }
198
314k
  *utf8_string_size = safe_utf8_string_size;
199
200
314k
  return( 1 );
201
315k
}
202
203
/* Copies an UTF-8 XML string from an UTF-16 stream
204
 * Returns 1 if successful or -1 on error
205
 */
206
int libfwevt_utf8_xml_string_with_index_copy_from_utf16_stream(
207
     libuna_utf8_character_t *utf8_string,
208
     size_t utf8_string_size,
209
     size_t *utf8_string_index,
210
     const uint8_t *utf16_stream,
211
     size_t utf16_stream_size,
212
     int byte_order,
213
     libcerror_error_t **error )
214
237k
{
215
237k
  static char *function                        = "libfwevt_utf8_xml_string_with_index_copy_from_utf16_stream";
216
237k
  libuna_unicode_character_t unicode_character = 0;
217
237k
  size_t safe_utf8_string_index                = 0;
218
237k
  size_t utf16_stream_index                    = 0;
219
237k
  int read_byte_order                          = 0;
220
237k
  int result                                   = 0;
221
222
237k
  if( utf8_string == NULL )
223
0
  {
224
0
    libcerror_error_set(
225
0
     error,
226
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
227
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
228
0
     "%s: invalid UTF-8 string.",
229
0
     function );
230
231
0
    return( -1 );
232
0
  }
233
237k
  if( utf8_string_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: invalid UTF-8 string size value exceeds maximum.",
240
0
     function );
241
242
0
    return( -1 );
243
0
  }
244
237k
  if( utf8_string_index == NULL )
245
0
  {
246
0
    libcerror_error_set(
247
0
     error,
248
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
249
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
250
0
     "%s: invalid UTF-8 string index.",
251
0
     function );
252
253
0
    return( -1 );
254
0
  }
255
237k
  if( utf16_stream == NULL )
256
0
  {
257
0
    libcerror_error_set(
258
0
     error,
259
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
260
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
261
0
     "%s: invalid UTF-16 stream.",
262
0
     function );
263
264
0
    return( -1 );
265
0
  }
266
237k
  if( utf16_stream_size > (size_t) SSIZE_MAX )
267
0
  {
268
0
    libcerror_error_set(
269
0
     error,
270
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
271
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
272
0
     "%s: invalid UTF-16 stream size value exceeds maximum.",
273
0
     function );
274
275
0
    return( -1 );
276
0
  }
277
237k
  if( ( utf16_stream_size == 0 )
278
237k
   || ( ( utf16_stream_size % 2 ) != 0 ) )
279
0
  {
280
0
    libcerror_error_set(
281
0
     error,
282
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
283
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
284
0
     "%s: missing UTF-16 stream value.",
285
0
     function );
286
287
0
    return( -1 );
288
0
  }
289
  /* Check if UTF-16 stream is in big or little endian
290
   */
291
237k
  if( utf16_stream_size >= 2 )
292
237k
  {
293
237k
    if( ( utf16_stream[ 0 ] == 0xfe )
294
9.49k
     && ( utf16_stream[ 1 ] == 0xff ) )
295
4.02k
    {
296
4.02k
      read_byte_order    = LIBUNA_ENDIAN_BIG;
297
4.02k
      utf16_stream_index = 2;
298
4.02k
    }
299
233k
    else if( ( utf16_stream[ 0 ] == 0xff )
300
16.3k
          && ( utf16_stream[ 1 ] == 0xfe ) )
301
4.10k
    {
302
4.10k
      read_byte_order    = LIBUNA_ENDIAN_LITTLE;
303
4.10k
      utf16_stream_index = 2;
304
4.10k
    }
305
237k
    if( byte_order == 0 )
306
0
    {
307
0
      byte_order = read_byte_order;
308
0
    }
309
237k
  }
310
237k
  safe_utf8_string_index = *utf8_string_index;
311
312
2.81M
  while( ( utf16_stream_index + 1 ) < utf16_stream_size )
313
2.65M
  {
314
    /* Convert the UTF-16 stream bytes into an Unicode character
315
     */
316
2.65M
    if( libuna_unicode_character_copy_from_utf16_stream(
317
2.65M
         &unicode_character,
318
2.65M
         utf16_stream,
319
2.65M
         utf16_stream_size,
320
2.65M
         &utf16_stream_index,
321
2.65M
         byte_order,
322
2.65M
         error ) != 1 )
323
0
    {
324
0
      libcerror_error_set(
325
0
       error,
326
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
327
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
328
0
       "%s: unable to copy Unicode character from UTF-16 stream.",
329
0
       function );
330
331
0
      return( -1 );
332
0
    }
333
2.65M
    switch( unicode_character )
334
2.65M
    {
335
      /* Replace & by &amp; */
336
11.7k
      case (libuna_unicode_character_t) '&':
337
11.7k
        if( ( safe_utf8_string_index + 5 ) > utf8_string_size )
338
3
        {
339
3
          libcerror_error_set(
340
3
           error,
341
3
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
342
3
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
343
3
           "%s: UTF-8 string size too small.",
344
3
           function );
345
346
3
          return( -1 );
347
3
        }
348
11.7k
        utf8_string[ safe_utf8_string_index++ ] = (uint8_t) '&';
349
11.7k
        utf8_string[ safe_utf8_string_index++ ] = (uint8_t) 'a';
350
11.7k
        utf8_string[ safe_utf8_string_index++ ] = (uint8_t) 'm';
351
11.7k
        utf8_string[ safe_utf8_string_index++ ] = (uint8_t) 'p';
352
11.7k
        utf8_string[ safe_utf8_string_index++ ] = (uint8_t) ';';
353
354
11.7k
        break;
355
356
      /* Replace < by &lt; */
357
7.16k
      case (libuna_unicode_character_t) '<':
358
7.16k
        if( ( safe_utf8_string_index + 4 ) > utf8_string_size )
359
3
        {
360
3
          libcerror_error_set(
361
3
           error,
362
3
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
363
3
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
364
3
           "%s: UTF-8 string size too small.",
365
3
           function );
366
367
3
          return( -1 );
368
3
        }
369
7.15k
        utf8_string[ safe_utf8_string_index++ ] = (uint8_t) '&';
370
7.15k
        utf8_string[ safe_utf8_string_index++ ] = (uint8_t) 'l';
371
7.15k
        utf8_string[ safe_utf8_string_index++ ] = (uint8_t) 't';
372
7.15k
        utf8_string[ safe_utf8_string_index++ ] = (uint8_t) ';';
373
374
7.15k
        break;
375
376
      /* Replace > by &gt; */
377
3.26k
      case (libuna_unicode_character_t) '>':
378
3.26k
        if( ( safe_utf8_string_index + 4 ) > utf8_string_size )
379
3
        {
380
3
          libcerror_error_set(
381
3
           error,
382
3
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
383
3
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
384
3
           "%s: UTF-8 string size too small.",
385
3
           function );
386
387
3
          return( -1 );
388
3
        }
389
3.26k
        utf8_string[ safe_utf8_string_index++ ] = (uint8_t) '&';
390
3.26k
        utf8_string[ safe_utf8_string_index++ ] = (uint8_t) 'g';
391
3.26k
        utf8_string[ safe_utf8_string_index++ ] = (uint8_t) 't';
392
3.26k
        utf8_string[ safe_utf8_string_index++ ] = (uint8_t) ';';
393
394
3.26k
        break;
395
396
      /* Replace ' by &apos; */
397
/* TODO disabled for now since Event Viewer does not uses it
398
      case (libuna_unicode_character_t) '\'':
399
        if( ( safe_utf8_string_index + 6 ) > utf8_string_size )
400
        {
401
          libcerror_error_set(
402
           error,
403
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
404
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
405
           "%s: UTF-8 string size too small.",
406
           function );
407
408
          return( -1 );
409
        }
410
        utf8_string[ safe_utf8_string_index++ ] = (uint8_t) '&';
411
        utf8_string[ safe_utf8_string_index++ ] = (uint8_t) 'a';
412
        utf8_string[ safe_utf8_string_index++ ] = (uint8_t) 'p';
413
        utf8_string[ safe_utf8_string_index++ ] = (uint8_t) 'o';
414
        utf8_string[ safe_utf8_string_index++ ] = (uint8_t) 's';
415
        utf8_string[ safe_utf8_string_index++ ] = (uint8_t) ';';
416
417
        break;
418
*/
419
420
      /* Replace " by &quot; */
421
/* TODO disabled for now since Event Viewer does not uses it
422
      case (libuna_unicode_character_t) '"':
423
        if( ( safe_utf8_string_index + 6 ) > utf8_string_size )
424
        {
425
          libcerror_error_set(
426
           error,
427
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
428
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
429
           "%s: UTF-8 string size too small.",
430
           function );
431
432
          return( -1 );
433
        }
434
        utf8_string[ safe_utf8_string_index++ ] = (uint8_t) '&';
435
        utf8_string[ safe_utf8_string_index++ ] = (uint8_t) 'q';
436
        utf8_string[ safe_utf8_string_index++ ] = (uint8_t) 'u';
437
        utf8_string[ safe_utf8_string_index++ ] = (uint8_t) 'o';
438
        utf8_string[ safe_utf8_string_index++ ] = (uint8_t) 't';
439
        utf8_string[ safe_utf8_string_index++ ] = (uint8_t) ';';
440
441
        break;
442
*/
443
444
2.63M
      default:
445
        /* Convert the Unicode character into UTF-8 character bytes
446
         */
447
2.63M
        if( ( byte_order & LIBUNA_UTF16_STREAM_ALLOW_UNPAIRED_SURROGATE ) == 0 )
448
2.63M
        {
449
2.63M
          result = libuna_unicode_character_copy_to_utf8(
450
2.63M
              unicode_character,
451
2.63M
              utf8_string,
452
2.63M
              utf8_string_size,
453
2.63M
              &safe_utf8_string_index,
454
2.63M
              error );
455
2.63M
        }
456
0
        else
457
0
        {
458
0
          result = libuna_unicode_character_copy_to_utf8_rfc2279(
459
0
              unicode_character,
460
0
              utf8_string,
461
0
              utf8_string_size,
462
0
              &safe_utf8_string_index,
463
0
              error );
464
0
        }
465
2.63M
        if( result != 1 )
466
3
        {
467
3
          libcerror_error_set(
468
3
           error,
469
3
           LIBCERROR_ERROR_DOMAIN_CONVERSION,
470
3
           LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
471
3
           "%s: unable to copy Unicode character to UTF-8.",
472
3
           function );
473
474
3
          return( -1 );
475
3
        }
476
2.63M
        break;
477
2.65M
    }
478
2.65M
    if( unicode_character == 0 )
479
81.0k
    {
480
81.0k
      break;
481
81.0k
    }
482
2.65M
  }
483
  /* Check if the string is terminated with an end-of-string character
484
   */
485
237k
  if( unicode_character != 0 )
486
156k
  {
487
156k
    if( safe_utf8_string_index >= utf8_string_size )
488
3
    {
489
3
      libcerror_error_set(
490
3
       error,
491
3
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
492
3
       LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
493
3
       "%s: UTF-8 string too small.",
494
3
       function );
495
496
3
      return( -1 );
497
3
    }
498
156k
    utf8_string[ safe_utf8_string_index ] = 0;
499
500
156k
    safe_utf8_string_index++;
501
156k
  }
502
237k
  *utf8_string_index = safe_utf8_string_index;
503
504
237k
  return( 1 );
505
237k
}
506
507
/* Determines the size of an UTF-16 XML string from an UTF-16 stream
508
 * Returns 1 if successful or -1 on error
509
 */
510
int libfwevt_utf16_xml_string_size_from_utf16_stream(
511
     const uint8_t *utf16_stream,
512
     size_t utf16_stream_size,
513
     int byte_order,
514
     size_t *utf16_string_size,
515
     libcerror_error_t **error )
516
135k
{
517
135k
  static char *function                        = "libfwevt_utf16_xml_string_size_from_utf16_stream";
518
135k
  libuna_unicode_character_t unicode_character = 0;
519
135k
  size_t safe_utf16_string_size                = 0;
520
135k
  size_t utf16_stream_index                    = 0;
521
135k
  int read_byte_order                          = 0;
522
135k
  int result                                   = 0;
523
524
135k
  if( utf16_stream == NULL )
525
0
  {
526
0
    libcerror_error_set(
527
0
     error,
528
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
529
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
530
0
     "%s: invalid UTF-16 stream.",
531
0
     function );
532
533
0
    return( -1 );
534
0
  }
535
135k
  if( utf16_stream_size > (size_t) SSIZE_MAX )
536
0
  {
537
0
    libcerror_error_set(
538
0
     error,
539
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
540
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
541
0
     "%s: invalid UTF-16 stream size value exceeds maximum.",
542
0
     function );
543
544
0
    return( -1 );
545
0
  }
546
135k
  if( ( utf16_stream_size % 2 ) != 0 )
547
0
  {
548
0
    libcerror_error_set(
549
0
     error,
550
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
551
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
552
0
     "%s: missing UTF-16 stream value.",
553
0
     function );
554
555
0
    return( -1 );
556
0
  }
557
135k
  if( utf16_string_size == NULL )
558
0
  {
559
0
    libcerror_error_set(
560
0
     error,
561
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
562
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
563
0
     "%s: invalid UTF-16 string size.",
564
0
     function );
565
566
0
    return( -1 );
567
0
  }
568
135k
  if( utf16_stream_size == 0 )
569
0
  {
570
0
    return( 1 );
571
0
  }
572
  /* Check if UTF-16 stream is in big or little endian
573
   */
574
135k
  if( utf16_stream_size >= 2 )
575
135k
  {
576
135k
    if( ( utf16_stream[ 0 ] == 0x0ff )
577
13.6k
     && ( utf16_stream[ 1 ] == 0x0fe ) )
578
1.12k
    {
579
1.12k
      read_byte_order    = LIBUNA_ENDIAN_LITTLE;
580
1.12k
      utf16_stream_index = 2;
581
1.12k
    }
582
134k
    else if( ( utf16_stream[ 0 ] == 0x0fe )
583
3.90k
          && ( utf16_stream[ 1 ] == 0x0ff ) )
584
2.13k
    {
585
2.13k
      read_byte_order    = LIBUNA_ENDIAN_BIG;
586
2.13k
      utf16_stream_index = 2;
587
2.13k
    }
588
135k
    if( byte_order == 0 )
589
0
    {
590
0
      byte_order = read_byte_order;
591
0
    }
592
135k
  }
593
3.38M
  while( ( utf16_stream_index + 1 ) < utf16_stream_size )
594
3.34M
  {
595
    /* Convert the UTF-16 stream bytes into an Unicode character
596
     */
597
3.34M
    if( libuna_unicode_character_copy_from_utf16_stream(
598
3.34M
         &unicode_character,
599
3.34M
         utf16_stream,
600
3.34M
         utf16_stream_size,
601
3.34M
         &utf16_stream_index,
602
3.34M
         byte_order,
603
3.34M
         error ) != 1 )
604
403
    {
605
403
      libcerror_error_set(
606
403
       error,
607
403
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
608
403
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
609
403
       "%s: unable to copy Unicode character from UTF-16 stream.",
610
403
       function );
611
612
403
      return( -1 );
613
403
    }
614
3.33M
    switch( unicode_character )
615
3.33M
    {
616
      /* Replace & by &amp; */
617
3.63k
      case (libuna_unicode_character_t) '&':
618
3.63k
        safe_utf16_string_size += 5;
619
3.63k
        break;
620
621
      /* Replace < by &lt; and > by &gt; */
622
3.56k
      case (libuna_unicode_character_t) '<':
623
5.70k
      case (libuna_unicode_character_t) '>':
624
5.70k
        safe_utf16_string_size += 4;
625
5.70k
        break;
626
627
      /* Replace ' by &apos; and " by &quot; */
628
/* TODO disabled for now since Event Viewer does not uses it
629
      case (libuna_unicode_character_t) '\'':
630
      case (libuna_unicode_character_t) '"':
631
        safe_utf16_string_size += 6;
632
        break;
633
*/
634
635
3.33M
      default:
636
        /* Determine how many UTF-16 character bytes are required
637
         */
638
3.33M
        if( ( byte_order & LIBUNA_UTF16_STREAM_ALLOW_UNPAIRED_SURROGATE ) == 0 )
639
3.33M
        {
640
3.33M
          result = libuna_unicode_character_size_to_utf16(
641
3.33M
              unicode_character,
642
3.33M
              &safe_utf16_string_size,
643
3.33M
              error );
644
3.33M
        }
645
0
        else
646
0
        {
647
0
          result = libuna_unicode_character_size_to_ucs2(
648
0
              unicode_character,
649
0
              &safe_utf16_string_size,
650
0
              error );
651
0
        }
652
3.33M
        if( result != 1 )
653
0
        {
654
0
          libcerror_error_set(
655
0
           error,
656
0
           LIBCERROR_ERROR_DOMAIN_CONVERSION,
657
0
           LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
658
0
           "%s: unable to unable to determine size of Unicode character in UTF-16.",
659
0
           function );
660
661
0
          return( -1 );
662
0
        }
663
3.33M
        break;
664
3.33M
    }
665
3.33M
    if( unicode_character == 0 )
666
85.0k
    {
667
85.0k
      break;
668
85.0k
    }
669
3.33M
  }
670
  /* Check if the string is terminated with an end-of-string character
671
   */
672
134k
  if( unicode_character != 0 )
673
49.7k
  {
674
49.7k
    safe_utf16_string_size++;
675
49.7k
  }
676
134k
  *utf16_string_size = safe_utf16_string_size;
677
678
134k
  return( 1 );
679
135k
}
680
681
/* Copies an UTF-16 XML string from an UTF-16 stream
682
 * Returns 1 if successful or -1 on error
683
 */
684
int libfwevt_utf16_xml_string_with_index_copy_from_utf16_stream(
685
     libuna_utf16_character_t *utf16_string,
686
     size_t utf16_string_size,
687
     size_t *utf16_string_index,
688
     const uint8_t *utf16_stream,
689
     size_t utf16_stream_size,
690
     int byte_order,
691
     libcerror_error_t **error )
692
28.9k
{
693
28.9k
  static char *function                        = "libfwevt_utf16_xml_string_with_index_copy_from_utf16_stream";
694
28.9k
  libuna_unicode_character_t unicode_character = 0;
695
28.9k
  size_t safe_utf16_string_index               = 0;
696
28.9k
  size_t utf16_stream_index                    = 0;
697
28.9k
  int read_byte_order                          = 0;
698
28.9k
  int result                                   = 0;
699
700
28.9k
  if( utf16_string == NULL )
701
0
  {
702
0
    libcerror_error_set(
703
0
     error,
704
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
705
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
706
0
     "%s: invalid UTF-16 string.",
707
0
     function );
708
709
0
    return( -1 );
710
0
  }
711
28.9k
  if( utf16_string_size > (size_t) SSIZE_MAX )
712
0
  {
713
0
    libcerror_error_set(
714
0
     error,
715
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
716
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
717
0
     "%s: invalid UTF-16 string size value exceeds maximum.",
718
0
     function );
719
720
0
    return( -1 );
721
0
  }
722
28.9k
  if( utf16_string_index == NULL )
723
0
  {
724
0
    libcerror_error_set(
725
0
     error,
726
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
727
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
728
0
     "%s: invalid UTF-16 string index.",
729
0
     function );
730
731
0
    return( -1 );
732
0
  }
733
28.9k
  if( utf16_stream == NULL )
734
0
  {
735
0
    libcerror_error_set(
736
0
     error,
737
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
738
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
739
0
     "%s: invalid UTF-16 stream.",
740
0
     function );
741
742
0
    return( -1 );
743
0
  }
744
28.9k
  if( utf16_stream_size > (size_t) SSIZE_MAX )
745
0
  {
746
0
    libcerror_error_set(
747
0
     error,
748
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
749
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
750
0
     "%s: invalid UTF-16 stream size value exceeds maximum.",
751
0
     function );
752
753
0
    return( -1 );
754
0
  }
755
28.9k
  if( ( utf16_stream_size == 0 )
756
28.9k
   || ( ( utf16_stream_size % 2 ) != 0 ) )
757
0
  {
758
0
    libcerror_error_set(
759
0
     error,
760
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
761
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
762
0
     "%s: missing UTF-16 stream value.",
763
0
     function );
764
765
0
    return( -1 );
766
0
  }
767
  /* Check if UTF-16 stream is in big or little endian
768
   */
769
28.9k
  if( utf16_stream_size >= 2 )
770
28.9k
  {
771
28.9k
    if( ( utf16_stream[ 0 ] == 0x0ff )
772
1.64k
     && ( utf16_stream[ 1 ] == 0x0fe ) )
773
528
    {
774
528
      read_byte_order    = LIBUNA_ENDIAN_LITTLE;
775
528
      utf16_stream_index = 2;
776
528
    }
777
28.4k
    else if( ( utf16_stream[ 0 ] == 0x0fe )
778
2.12k
          && ( utf16_stream[ 1 ] == 0x0ff ) )
779
1.25k
    {
780
1.25k
      read_byte_order    = LIBUNA_ENDIAN_BIG;
781
1.25k
      utf16_stream_index = 2;
782
1.25k
    }
783
28.9k
    if( byte_order == 0 )
784
0
    {
785
0
      byte_order = read_byte_order;
786
0
    }
787
28.9k
  }
788
28.9k
  safe_utf16_string_index = *utf16_string_index;
789
790
659k
  while( ( utf16_stream_index + 1 ) < utf16_stream_size )
791
637k
  {
792
    /* Convert the UTF-16 stream bytes into an Unicode character
793
     */
794
637k
    if( libuna_unicode_character_copy_from_utf16_stream(
795
637k
         &unicode_character,
796
637k
         utf16_stream,
797
637k
         utf16_stream_size,
798
637k
         &utf16_stream_index,
799
637k
         byte_order,
800
637k
         error ) != 1 )
801
0
    {
802
0
      libcerror_error_set(
803
0
       error,
804
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
805
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
806
0
       "%s: unable to copy Unicode character from UTF-16 stream.",
807
0
       function );
808
809
0
      return( -1 );
810
0
    }
811
637k
    switch( unicode_character )
812
637k
    {
813
      /* Replace & by &amp; */
814
1.96k
      case (libuna_unicode_character_t) '&':
815
1.96k
        if( ( safe_utf16_string_index + 5 ) > utf16_string_size )
816
0
        {
817
0
          libcerror_error_set(
818
0
           error,
819
0
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
820
0
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
821
0
           "%s: UTF-16 string size too small.",
822
0
           function );
823
824
0
          return( -1 );
825
0
        }
826
1.96k
        utf16_string[ safe_utf16_string_index++ ] = (uint16_t) '&';
827
1.96k
        utf16_string[ safe_utf16_string_index++ ] = (uint16_t) 'a';
828
1.96k
        utf16_string[ safe_utf16_string_index++ ] = (uint16_t) 'm';
829
1.96k
        utf16_string[ safe_utf16_string_index++ ] = (uint16_t) 'p';
830
1.96k
        utf16_string[ safe_utf16_string_index++ ] = (uint16_t) ';';
831
832
1.96k
        break;
833
834
      /* Replace < by &lt; */
835
940
      case (libuna_unicode_character_t) '<':
836
940
        if( ( safe_utf16_string_index + 4 ) > 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 size too small.",
843
0
           function );
844
845
0
          return( -1 );
846
0
        }
847
940
        utf16_string[ safe_utf16_string_index++ ] = (uint16_t) '&';
848
940
        utf16_string[ safe_utf16_string_index++ ] = (uint16_t) 'l';
849
940
        utf16_string[ safe_utf16_string_index++ ] = (uint16_t) 't';
850
940
        utf16_string[ safe_utf16_string_index++ ] = (uint16_t) ';';
851
852
940
        break;
853
854
      /* Replace > by &gt; */
855
1.11k
      case (libuna_unicode_character_t) '>':
856
1.11k
        if( ( safe_utf16_string_index + 4 ) > utf16_string_size )
857
0
        {
858
0
          libcerror_error_set(
859
0
           error,
860
0
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
861
0
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
862
0
           "%s: UTF-16 string size too small.",
863
0
           function );
864
865
0
          return( -1 );
866
0
        }
867
1.11k
        utf16_string[ safe_utf16_string_index++ ] = (uint16_t) '&';
868
1.11k
        utf16_string[ safe_utf16_string_index++ ] = (uint16_t) 'g';
869
1.11k
        utf16_string[ safe_utf16_string_index++ ] = (uint16_t) 't';
870
1.11k
        utf16_string[ safe_utf16_string_index++ ] = (uint16_t) ';';
871
872
1.11k
        break;
873
874
      /* Replace ' by &apos; */
875
/* TODO disabled for now since Event Viewer does not uses it
876
      case (libuna_unicode_character_t) '\'':
877
        if( ( safe_utf16_string_index + 6 ) > utf16_string_size )
878
        {
879
          libcerror_error_set(
880
           error,
881
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
882
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
883
           "%s: UTF-16 string size too small.",
884
           function );
885
886
          return( -1 );
887
        }
888
        utf16_string[ safe_utf16_string_index++ ] = (uint16_t) '&';
889
        utf16_string[ safe_utf16_string_index++ ] = (uint16_t) 'a';
890
        utf16_string[ safe_utf16_string_index++ ] = (uint16_t) 'p';
891
        utf16_string[ safe_utf16_string_index++ ] = (uint16_t) 'o';
892
        utf16_string[ safe_utf16_string_index++ ] = (uint16_t) 's';
893
        utf16_string[ safe_utf16_string_index++ ] = (uint16_t) ';';
894
895
        break;
896
*/
897
898
      /* Replace " by &quot; */
899
/* TODO disabled for now since Event Viewer does not uses it
900
      case (libuna_unicode_character_t) '"':
901
        if( ( safe_utf16_string_index + 6 ) > utf16_string_size )
902
        {
903
          libcerror_error_set(
904
           error,
905
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
906
           LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
907
           "%s: UTF-16 string size too small.",
908
           function );
909
910
          return( -1 );
911
        }
912
        utf16_string[ safe_utf16_string_index++ ] = (uint16_t) '&';
913
        utf16_string[ safe_utf16_string_index++ ] = (uint16_t) 'q';
914
        utf16_string[ safe_utf16_string_index++ ] = (uint16_t) 'u';
915
        utf16_string[ safe_utf16_string_index++ ] = (uint16_t) 'o';
916
        utf16_string[ safe_utf16_string_index++ ] = (uint16_t) 't';
917
        utf16_string[ safe_utf16_string_index++ ] = (uint16_t) ';';
918
919
        break;
920
*/
921
922
633k
      default:
923
        /* Convert the Unicode character into UTF-16 character bytes
924
         */
925
633k
        if( ( byte_order & LIBUNA_UTF16_STREAM_ALLOW_UNPAIRED_SURROGATE ) == 0 )
926
633k
        {
927
633k
          result = libuna_unicode_character_copy_to_utf16(
928
633k
              unicode_character,
929
633k
              utf16_string,
930
633k
              utf16_string_size,
931
633k
              &safe_utf16_string_index,
932
633k
              error );
933
633k
        }
934
0
        else
935
0
        {
936
0
          result = libuna_unicode_character_copy_to_ucs2(
937
0
              unicode_character,
938
0
              utf16_string,
939
0
              utf16_string_size,
940
0
              &safe_utf16_string_index,
941
0
              error );
942
0
        }
943
633k
        if( result != 1 )
944
0
        {
945
0
          libcerror_error_set(
946
0
           error,
947
0
           LIBCERROR_ERROR_DOMAIN_CONVERSION,
948
0
           LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
949
0
           "%s: unable to copy Unicode character to UTF-16.",
950
0
           function );
951
952
0
          return( -1 );
953
0
        }
954
633k
        break;
955
637k
    }
956
637k
    if( unicode_character == 0 )
957
7.04k
    {
958
7.04k
      break;
959
7.04k
    }
960
637k
  }
961
  /* Check if the string is terminated with an end-of-string character
962
   */
963
28.9k
  if( unicode_character != 0 )
964
21.9k
  {
965
21.9k
    if( safe_utf16_string_index >= utf16_string_size )
966
0
    {
967
0
      libcerror_error_set(
968
0
       error,
969
0
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
970
0
       LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
971
0
       "%s: UTF-16 string too small.",
972
0
       function );
973
974
0
      return( -1 );
975
0
    }
976
21.9k
    utf16_string[ safe_utf16_string_index ] = 0;
977
978
21.9k
    safe_utf16_string_index++;
979
21.9k
  }
980
28.9k
  *utf16_string_index = safe_utf16_string_index;
981
982
28.9k
  return( 1 );
983
28.9k
}
984