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_document.c
Line
Count
Source
1
/*
2
 * Windows Event Log binary XML document 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 <byte_stream.h>
24
#include <memory.h>
25
#include <system_string.h>
26
#include <types.h>
27
28
#include "libfwevt_debug.h"
29
#include "libfwevt_definitions.h"
30
#include "libfwevt_integer.h"
31
#include "libfwevt_libcdata.h"
32
#include "libfwevt_libcerror.h"
33
#include "libfwevt_libcnotify.h"
34
#include "libfwevt_libfguid.h"
35
#include "libfwevt_libuna.h"
36
#include "libfwevt_types.h"
37
#include "libfwevt_unused.h"
38
#include "libfwevt_xml_document.h"
39
#include "libfwevt_xml_template_value.h"
40
#include "libfwevt_xml_tag.h"
41
#include "libfwevt_xml_token.h"
42
43
/* Creates a binary XML document
44
 * Make sure the value xml_document is referencing, is set to NULL
45
 * Returns 1 if successful or -1 on error
46
 */
47
int libfwevt_xml_document_initialize(
48
     libfwevt_xml_document_t **xml_document,
49
     libcerror_error_t **error )
50
66.6k
{
51
66.6k
  libfwevt_internal_xml_document_t *internal_xml_document = NULL;
52
66.6k
  static char *function                                   = "libfwevt_xml_document_initialize";
53
54
66.6k
  if( xml_document == NULL )
55
0
  {
56
0
    libcerror_error_set(
57
0
     error,
58
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
59
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
60
0
     "%s: invalid binary XML document.",
61
0
     function );
62
63
0
    return( -1 );
64
0
  }
65
66.6k
  if( *xml_document != NULL )
66
0
  {
67
0
    libcerror_error_set(
68
0
     error,
69
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
70
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
71
0
     "%s: invalid binary XML document value already set.",
72
0
     function );
73
74
0
    return( -1 );
75
0
  }
76
66.6k
  internal_xml_document = memory_allocate_structure(
77
66.6k
                           libfwevt_internal_xml_document_t );
78
79
66.6k
  if( internal_xml_document == NULL )
80
0
  {
81
0
    libcerror_error_set(
82
0
     error,
83
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
84
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
85
0
     "%s: unable to create binary XML document.",
86
0
     function );
87
88
0
    goto on_error;
89
0
  }
90
66.6k
  if( memory_set(
91
66.6k
       internal_xml_document,
92
66.6k
       0,
93
66.6k
       sizeof( libfwevt_internal_xml_document_t ) ) == NULL )
94
0
  {
95
0
    libcerror_error_set(
96
0
     error,
97
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
98
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
99
0
     "%s: unable to clear binary XML document.",
100
0
     function );
101
102
0
    goto on_error;
103
0
  }
104
66.6k
  *xml_document = (libfwevt_xml_document_t *) internal_xml_document;
105
106
66.6k
  return( 1 );
107
108
0
on_error:
109
0
  if( internal_xml_document != NULL )
110
0
  {
111
0
    memory_free(
112
0
     internal_xml_document );
113
0
  }
114
0
  return( -1 );
115
66.6k
}
116
117
/* Frees a binary XML document
118
 * Returns 1 if successful or -1 on error
119
 */
120
int libfwevt_xml_document_free(
121
     libfwevt_xml_document_t **xml_document,
122
     libcerror_error_t **error )
123
66.6k
{
124
66.6k
  libfwevt_internal_xml_document_t *internal_xml_document = NULL;
125
66.6k
  static char *function                                   = "libfwevt_xml_document_free";
126
66.6k
  int result                                              = 1;
127
128
66.6k
  if( xml_document == NULL )
129
0
  {
130
0
    libcerror_error_set(
131
0
     error,
132
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
133
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
134
0
     "%s: invalid binary XML document.",
135
0
     function );
136
137
0
    return( -1 );
138
0
  }
139
66.6k
  if( *xml_document != NULL )
140
66.6k
  {
141
66.6k
    internal_xml_document = (libfwevt_internal_xml_document_t *) *xml_document;
142
66.6k
    *xml_document         = NULL;
143
144
66.6k
    if( internal_xml_document->root_xml_tag != NULL )
145
23.7k
    {
146
23.7k
      if( libfwevt_internal_xml_tag_free(
147
23.7k
           (libfwevt_internal_xml_tag_t **) &( internal_xml_document->root_xml_tag ),
148
23.7k
           error ) != 1 )
149
0
      {
150
0
        libcerror_error_set(
151
0
         error,
152
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
153
0
         LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
154
0
         "%s: unable to free root XML tag.",
155
0
         function );
156
157
0
        result = -1;
158
0
      }
159
23.7k
    }
160
66.6k
    memory_free(
161
66.6k
     internal_xml_document );
162
66.6k
  }
163
66.6k
  return( result );
164
66.6k
}
165
166
/* Retrieves the root XML tag
167
 * Returns 1 if successful or -1 on error
168
 */
169
int libfwevt_xml_document_get_root_xml_tag(
170
     libfwevt_xml_document_t *xml_document,
171
     libfwevt_xml_tag_t **root_xml_tag,
172
     libcerror_error_t **error )
173
71.3k
{
174
71.3k
  libfwevt_internal_xml_document_t *internal_xml_document = NULL;
175
71.3k
  static char *function                                   = "libfwevt_xml_document_get_root_xml_tag";
176
177
71.3k
  if( xml_document == NULL )
178
0
  {
179
0
    libcerror_error_set(
180
0
     error,
181
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
182
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
183
0
     "%s: invalid binary XML document.",
184
0
     function );
185
186
0
    return( -1 );
187
0
  }
188
71.3k
  internal_xml_document = (libfwevt_internal_xml_document_t *) xml_document;
189
190
71.3k
  if( root_xml_tag == NULL )
191
0
  {
192
0
    libcerror_error_set(
193
0
     error,
194
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
195
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
196
0
     "%s: invalid root XML tag.",
197
0
     function );
198
199
0
    return( -1 );
200
0
  }
201
71.3k
  *root_xml_tag = internal_xml_document->root_xml_tag;
202
203
71.3k
  return( 1 );
204
71.3k
}
205
206
/* Reads a binary XML document
207
 * Returns 1 if successful or -1 on error
208
 */
209
int libfwevt_xml_document_read(
210
     libfwevt_xml_document_t *xml_document,
211
     const uint8_t *binary_data,
212
     size_t binary_data_size,
213
     size_t binary_data_offset,
214
     int ascii_codepage,
215
     uint8_t flags,
216
     libcerror_error_t **error )
217
55.8k
{
218
55.8k
  static char *function = "libfwevt_xml_document_read";
219
220
55.8k
  if( libfwevt_xml_document_read_with_template_values(
221
55.8k
       xml_document,
222
55.8k
       binary_data,
223
55.8k
       binary_data_size,
224
55.8k
       binary_data_offset,
225
55.8k
       ascii_codepage,
226
55.8k
       flags,
227
55.8k
       NULL,
228
55.8k
       error ) != 1 )
229
51.7k
  {
230
51.7k
    libcerror_error_set(
231
51.7k
     error,
232
51.7k
     LIBCERROR_ERROR_DOMAIN_IO,
233
51.7k
     LIBCERROR_IO_ERROR_READ_FAILED,
234
51.7k
     "%s: unable to read XML document.",
235
51.7k
     function );
236
237
51.7k
    return( -1 );
238
51.7k
  }
239
4.10k
  return( 1 );
240
55.8k
}
241
242
/* Reads a binary XML document with template values
243
 * Returns 1 if successful or -1 on error
244
 */
245
int libfwevt_xml_document_read_with_template_values(
246
     libfwevt_xml_document_t *xml_document,
247
     const uint8_t *binary_data,
248
     size_t binary_data_size,
249
     size_t binary_data_offset,
250
     int ascii_codepage,
251
     uint8_t flags,
252
     libcdata_array_t *template_values_array,
253
     libcerror_error_t **error )
254
66.5k
{
255
66.5k
  libfwevt_internal_xml_document_t *internal_xml_document = NULL;
256
66.5k
  libfwevt_xml_token_t *xml_token                         = NULL;
257
66.5k
  static char *function                                   = "libfwevt_xml_document_read_with_template_values";
258
66.5k
  uint8_t supported_flags                                 = 0;
259
260
66.5k
  if( xml_document == NULL )
261
0
  {
262
0
    libcerror_error_set(
263
0
     error,
264
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
265
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
266
0
     "%s: invalid binary XML document.",
267
0
     function );
268
269
0
    return( -1 );
270
0
  }
271
66.5k
  internal_xml_document = (libfwevt_internal_xml_document_t *) xml_document;
272
273
66.5k
  if( internal_xml_document->root_xml_tag != NULL )
274
0
  {
275
0
    libcerror_error_set(
276
0
     error,
277
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
278
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
279
0
     "%s: invalid binary XML document - root XML tag already set.",
280
0
     function );
281
282
0
    return( -1 );
283
0
  }
284
66.5k
  if( binary_data == NULL )
285
0
  {
286
0
    libcerror_error_set(
287
0
     error,
288
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
289
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
290
0
     "%s: invalid binary data.",
291
0
     function );
292
293
0
    return( -1 );
294
0
  }
295
66.5k
  if( binary_data_size > (size_t) SSIZE_MAX )
296
0
  {
297
0
    libcerror_error_set(
298
0
     error,
299
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
300
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
301
0
     "%s: invalid binary XML document data size value exceeds maximum.",
302
0
     function );
303
304
0
    return( -1 );
305
0
  }
306
66.5k
  if( binary_data_offset >= binary_data_size )
307
0
  {
308
0
    libcerror_error_set(
309
0
     error,
310
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
311
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
312
0
     "%s: invalid binary data offset value out of bounds.",
313
0
     function );
314
315
0
    return( -1 );
316
0
  }
317
66.5k
  supported_flags = LIBFWEVT_XML_DOCUMENT_READ_FLAG_HAS_DATA_OFFSETS
318
66.5k
                  | LIBFWEVT_XML_DOCUMENT_READ_FLAG_HAS_DEPENDENCY_IDENTIFIERS;
319
320
66.5k
  if( ( flags & ~( supported_flags ) ) != 0 )
321
0
  {
322
0
    libcerror_error_set(
323
0
     error,
324
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
325
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
326
0
     "%s: unsupported flags: 0x%02" PRIx8 ".",
327
0
     function,
328
0
     flags );
329
330
0
    return( -1 );
331
0
  }
332
66.5k
  if( libfwevt_xml_token_initialize(
333
66.5k
       &xml_token,
334
66.5k
       error ) != 1 )
335
0
  {
336
0
    libcerror_error_set(
337
0
     error,
338
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
339
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
340
0
     "%s: unable to create binary XML token.",
341
0
     function );
342
343
0
    goto on_error;
344
0
  }
345
170k
  while( binary_data_offset < binary_data_size )
346
169k
  {
347
169k
    if( libfwevt_xml_token_read_data(
348
169k
         xml_token,
349
169k
         binary_data,
350
169k
         binary_data_size,
351
169k
         binary_data_offset,
352
169k
         error ) != 1 )
353
2.77k
    {
354
2.77k
      libcerror_error_set(
355
2.77k
       error,
356
2.77k
       LIBCERROR_ERROR_DOMAIN_IO,
357
2.77k
       LIBCERROR_IO_ERROR_READ_FAILED,
358
2.77k
       "%s: unable to read binary XML token.",
359
2.77k
       function );
360
361
2.77k
      goto on_error;
362
2.77k
    }
363
/* TODO check for prologue */
364
/* TODO validate the order */
365
/* TODO check for Miscellaneous before end of file token */
366
167k
    switch( xml_token->type & 0xbf )
367
167k
    {
368
9.88k
      case LIBFWEVT_XML_TOKEN_END_OF_FILE:
369
9.88k
        if( ( binary_data_size < 1 )
370
9.88k
         || ( binary_data_offset >= ( binary_data_size - 1 ) ) )
371
295
        {
372
295
          libcerror_error_set(
373
295
           error,
374
295
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
375
295
           LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
376
295
           "%s: invalid binary XML document data size value too small.",
377
295
           function );
378
379
295
          goto on_error;
380
295
        }
381
#if defined( HAVE_DEBUG_OUTPUT )
382
        if( libcnotify_verbose != 0 )
383
        {
384
          libcnotify_printf(
385
           "%s: data offset\t\t: 0x%08" PRIzx "\n",
386
           function,
387
           binary_data_offset );
388
389
          libcnotify_printf(
390
           "%s: end of file data:\n",
391
           function );
392
          libcnotify_print_data(
393
           &( binary_data[ binary_data_offset ] ),
394
           1,
395
           0 );
396
        }
397
#endif
398
#if defined( HAVE_DEBUG_OUTPUT )
399
        if( libcnotify_verbose != 0 )
400
        {
401
          libcnotify_printf(
402
           "%s: type\t\t\t: 0x%02" PRIx8 "\n",
403
           function,
404
           binary_data[ binary_data_offset ] );
405
406
          libcnotify_printf(
407
           "\n" );
408
        }
409
#endif
410
9.58k
        xml_token->size = 1;
411
412
9.58k
        break;
413
414
154k
      case LIBFWEVT_XML_TOKEN_FRAGMENT_HEADER:
415
154k
        if( libfwevt_xml_document_read_fragment(
416
154k
             internal_xml_document,
417
154k
             xml_token,
418
154k
             binary_data,
419
154k
             binary_data_size,
420
154k
             binary_data_offset,
421
154k
             ascii_codepage,
422
154k
             flags,
423
154k
             template_values_array,
424
154k
             internal_xml_document->root_xml_tag,
425
154k
             0,
426
154k
             0,
427
154k
             error ) != 1 )
428
50.8k
        {
429
50.8k
          libcerror_error_set(
430
50.8k
           error,
431
50.8k
           LIBCERROR_ERROR_DOMAIN_IO,
432
50.8k
           LIBCERROR_IO_ERROR_READ_FAILED,
433
50.8k
           "%s: unable to read fragment header.",
434
50.8k
           function );
435
436
50.8k
          goto on_error;
437
50.8k
        }
438
103k
        break;
439
440
103k
      default:
441
2.50k
        libcerror_error_set(
442
2.50k
         error,
443
2.50k
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
444
2.50k
         LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
445
2.50k
         "%s: invalid binary XML token - unsupported type: 0x%02" PRIx8 ".",
446
2.50k
         function,
447
2.50k
         xml_token->type );
448
449
2.50k
        goto on_error;
450
167k
    }
451
113k
    internal_xml_document->size += xml_token->size;
452
113k
    binary_data_offset          += xml_token->size;
453
454
113k
    if( xml_token->type == LIBFWEVT_XML_TOKEN_END_OF_FILE )
455
9.58k
    {
456
9.58k
      break;
457
9.58k
    }
458
113k
  }
459
10.1k
  if( libfwevt_xml_token_free(
460
10.1k
       &xml_token,
461
10.1k
       error ) != 1 )
462
0
  {
463
0
    libcerror_error_set(
464
0
     error,
465
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
466
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
467
0
     "%s: unable to free binary XML token.",
468
0
     function );
469
470
0
    goto on_error;
471
0
  }
472
10.1k
  return( 1 );
473
474
56.4k
on_error:
475
56.4k
  if( xml_token != NULL )
476
56.4k
  {
477
56.4k
    libfwevt_xml_token_free(
478
56.4k
     &xml_token,
479
56.4k
     NULL );
480
56.4k
  }
481
56.4k
  return( -1 );
482
10.1k
}
483
484
/* Reads an attribute from a binary XML document
485
 * Returns 1 if successful or -1 on error
486
 */
487
int libfwevt_xml_document_read_attribute(
488
     libfwevt_internal_xml_document_t *internal_xml_document,
489
     libfwevt_xml_token_t *xml_token,
490
     const uint8_t *binary_data,
491
     size_t binary_data_size,
492
     size_t binary_data_offset,
493
     int ascii_codepage,
494
     uint8_t flags,
495
     libcdata_array_t *template_values_array,
496
     libfwevt_xml_tag_t *xml_tag,
497
     int element_recursion_depth,
498
     int template_instance_recursion_depth,
499
     libcerror_error_t **error )
500
1.23M
{
501
1.23M
  libfwevt_xml_tag_t *attribute_xml_tag    = NULL;
502
1.23M
  libfwevt_xml_token_t *xml_sub_token      = NULL;
503
1.23M
  const uint8_t *xml_document_data         = NULL;
504
1.23M
  static char *function                    = "libfwevt_xml_document_read_attribute";
505
1.23M
  size_t additional_value_size             = 0;
506
1.23M
  size_t template_value_offset             = 0;
507
1.23M
  size_t trailing_data_size                = 0;
508
1.23M
  size_t xml_document_data_offset          = 0;
509
1.23M
  size_t xml_document_data_size            = 0;
510
1.23M
  uint32_t attribute_name_offset           = 0;
511
1.23M
  uint32_t attribute_name_size             = 0;
512
1.23M
  int result                               = 0;
513
1.23M
  int template_value_array_recursion_depth = 0;
514
515
1.23M
  if( internal_xml_document == NULL )
516
0
  {
517
0
    libcerror_error_set(
518
0
     error,
519
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
520
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
521
0
     "%s: invalid binary XML document.",
522
0
     function );
523
524
0
    return( -1 );
525
0
  }
526
1.23M
  if( xml_token == NULL )
527
0
  {
528
0
    libcerror_error_set(
529
0
     error,
530
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
531
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
532
0
     "%s: invalid binary XML token.",
533
0
     function );
534
535
0
    return( -1 );
536
0
  }
537
1.23M
  if( ( xml_token->type & 0xbf ) != LIBFWEVT_XML_TOKEN_ATTRIBUTE )
538
181
  {
539
181
    libcerror_error_set(
540
181
     error,
541
181
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
542
181
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
543
181
     "%s: invalid binary XML token - unsupported type: 0x%02" PRIx8 ".",
544
181
     function,
545
181
     xml_token->type );
546
547
181
    return( -1 );
548
181
  }
549
1.23M
  if( binary_data == NULL )
550
0
  {
551
0
    libcerror_error_set(
552
0
     error,
553
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
554
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
555
0
     "%s: invalid binary data.",
556
0
     function );
557
558
0
    return( -1 );
559
0
  }
560
1.23M
  if( binary_data_size > (size_t) SSIZE_MAX )
561
0
  {
562
0
    libcerror_error_set(
563
0
     error,
564
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
565
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
566
0
     "%s: invalid binary XML document data size value exceeds maximum.",
567
0
     function );
568
569
0
    return( -1 );
570
0
  }
571
1.23M
  if( binary_data_offset >= binary_data_size )
572
0
  {
573
0
    libcerror_error_set(
574
0
     error,
575
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
576
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
577
0
     "%s: invalid binary data offset value out of bounds.",
578
0
     function );
579
580
0
    return( -1 );
581
0
  }
582
1.23M
  if( ( flags & LIBFWEVT_XML_DOCUMENT_READ_FLAG_HAS_DATA_OFFSETS ) != 0 )
583
1.00M
  {
584
1.00M
    additional_value_size = 4;
585
586
1.00M
    if( ( binary_data_size < 4 )
587
1.00M
     || ( binary_data_offset > ( binary_data_size - 4 ) ) )
588
48
    {
589
48
      libcerror_error_set(
590
48
       error,
591
48
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
592
48
       LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
593
48
       "%s: invalid binary data offset value out of bounds.",
594
48
       function );
595
596
48
      return( -1 );
597
48
    }
598
1.00M
  }
599
1.23M
  xml_document_data      = &( binary_data[ binary_data_offset ] );
600
1.23M
  xml_document_data_size = binary_data_size - binary_data_offset;
601
602
1.23M
  if( libfwevt_xml_token_initialize(
603
1.23M
       &xml_sub_token,
604
1.23M
       error ) != 1 )
605
0
  {
606
0
    libcerror_error_set(
607
0
     error,
608
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
609
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
610
0
     "%s: unable to create binary XML sub token.",
611
0
     function );
612
613
0
    goto on_error;
614
0
  }
615
1.23M
  do
616
1.24M
  {
617
1.24M
    if( ( template_value_array_recursion_depth < 0 )
618
1.24M
     || ( template_value_array_recursion_depth > LIBFWEVT_XML_DOCUMENT_TEMPLATE_VALUE_ARRAY_RECURSION_DEPTH ) )
619
6
    {
620
6
      libcerror_error_set(
621
6
       error,
622
6
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
623
6
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
624
6
       "%s: invalid template value array recursion depth value out of bounds.",
625
6
       function );
626
627
6
      goto on_error;
628
6
    }
629
1.24M
    if( ( xml_document_data_size < ( additional_value_size + 1 ) )
630
1.24M
     || ( xml_document_data_offset > ( xml_document_data_size - ( additional_value_size + 1 ) ) ) )
631
77
    {
632
77
      libcerror_error_set(
633
77
       error,
634
77
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
635
77
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
636
77
       "%s: invalid binary XML document data size value too small.",
637
77
       function );
638
639
77
      goto on_error;
640
77
    }
641
#if defined( HAVE_DEBUG_OUTPUT )
642
    if( libcnotify_verbose != 0 )
643
    {
644
      libcnotify_printf(
645
       "%s: data offset\t\t\t: 0x%08" PRIzx "\n",
646
       function,
647
       binary_data_offset + xml_document_data_offset );
648
649
      libcnotify_printf(
650
       "%s: attribute data:\n",
651
       function );
652
      libcnotify_print_data(
653
       &( xml_document_data[ xml_document_data_offset ] ),
654
       additional_value_size + 1,
655
       LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
656
    }
657
#endif
658
#if defined( HAVE_DEBUG_OUTPUT )
659
    if( libcnotify_verbose != 0 )
660
    {
661
      libcnotify_printf(
662
       "%s: type\t\t\t\t: 0x%02" PRIx8 "\n",
663
       function,
664
       xml_document_data[ xml_document_data_offset ] );
665
    }
666
#endif
667
1.24M
    xml_document_data_offset += 1;
668
669
1.24M
    if( ( flags & LIBFWEVT_XML_DOCUMENT_READ_FLAG_HAS_DATA_OFFSETS ) == 0 )
670
232k
    {
671
232k
      attribute_name_offset = (uint32_t) ( binary_data_offset + xml_document_data_offset );
672
232k
    }
673
1.01M
    else
674
1.01M
    {
675
1.01M
      if( ( xml_document_data_size < 4 )
676
1.01M
       || ( xml_document_data_offset >= ( xml_document_data_size - 4 ) ) )
677
36
      {
678
36
        libcerror_error_set(
679
36
         error,
680
36
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
681
36
         LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
682
36
         "%s: invalid binary XML document data size value too small.",
683
36
         function );
684
685
36
        goto on_error;
686
36
      }
687
1.01M
      byte_stream_copy_to_uint32_little_endian(
688
1.01M
       &( xml_document_data[ xml_document_data_offset ] ),
689
1.01M
       attribute_name_offset );
690
691
#if defined( HAVE_DEBUG_OUTPUT )
692
      if( libcnotify_verbose != 0 )
693
      {
694
        libcnotify_printf(
695
         "%s: name offset\t\t\t: 0x%08" PRIx32 "\n",
696
         function,
697
         attribute_name_offset );
698
      }
699
#endif
700
1.01M
      xml_document_data_offset += 4;
701
1.01M
    }
702
#if defined( HAVE_DEBUG_OUTPUT )
703
    if( libcnotify_verbose != 0 )
704
    {
705
      libcnotify_printf(
706
       "\n" );
707
    }
708
#endif
709
1.24M
    if( attribute_name_offset > ( binary_data_offset + xml_document_data_offset ) )
710
787
    {
711
787
      libcerror_error_set(
712
787
       error,
713
787
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
714
787
       LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
715
787
       "%s: invalid attribute data offset value out of bounds.",
716
787
       function );
717
718
787
      goto on_error;
719
787
    }
720
1.24M
    if( ( binary_data_offset + xml_document_data_offset ) < attribute_name_offset )
721
0
    {
722
0
      trailing_data_size = attribute_name_offset - ( binary_data_offset - xml_document_data_offset );
723
724
#if defined( HAVE_DEBUG_OUTPUT )
725
      if( libcnotify_verbose != 0 )
726
      {
727
        libcnotify_printf(
728
         "%s: data offset\t\t: 0x%08" PRIzx "\n",
729
         function,
730
         binary_data_offset + xml_document_data_offset );
731
732
        libcnotify_printf(
733
         "%s: trailing data:\n",
734
         function );
735
        libcnotify_print_data(
736
         &( xml_document_data[ xml_document_data_offset ] ),
737
         trailing_data_size,
738
         LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
739
      }
740
#endif
741
0
      xml_document_data_offset += trailing_data_size;
742
0
    }
743
1.24M
    if( libfwevt_xml_tag_initialize(
744
1.24M
         &attribute_xml_tag,
745
1.24M
         error ) != 1 )
746
0
    {
747
0
      libcerror_error_set(
748
0
       error,
749
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
750
0
       LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
751
0
       "%s: unable to create attribute XML tag.",
752
0
       function );
753
754
0
      goto on_error;
755
0
    }
756
1.24M
    if( libfwevt_xml_document_read_name(
757
1.24M
         internal_xml_document,
758
1.24M
         binary_data,
759
1.24M
         binary_data_size,
760
1.24M
         attribute_name_offset,
761
1.24M
         flags,
762
1.24M
         &attribute_name_size,
763
1.24M
         attribute_xml_tag,
764
1.24M
         error ) != 1 )
765
198
    {
766
198
      libcerror_error_set(
767
198
       error,
768
198
       LIBCERROR_ERROR_DOMAIN_IO,
769
198
       LIBCERROR_IO_ERROR_READ_FAILED,
770
198
       "%s: unable to read attribute name.",
771
198
       function );
772
773
198
      goto on_error;
774
198
    }
775
1.24M
    if( ( binary_data_offset + xml_document_data_offset ) == attribute_name_offset )
776
343k
    {
777
343k
      xml_document_data_offset += attribute_name_size;
778
343k
    }
779
1.24M
    if( libfwevt_xml_token_read_data(
780
1.24M
         xml_sub_token,
781
1.24M
         binary_data,
782
1.24M
         binary_data_size,
783
1.24M
         binary_data_offset + xml_document_data_offset,
784
1.24M
         error ) != 1 )
785
91
    {
786
91
      libcerror_error_set(
787
91
       error,
788
91
       LIBCERROR_ERROR_DOMAIN_IO,
789
91
       LIBCERROR_IO_ERROR_READ_FAILED,
790
91
       "%s: unable to read binary XML sub token.",
791
91
       function );
792
793
91
      goto on_error;
794
91
    }
795
1.24M
    result = 1;
796
797
1.24M
    switch( xml_sub_token->type & 0xbf )
798
1.24M
    {
799
601k
      case LIBFWEVT_XML_TOKEN_VALUE:
800
601k
        if( template_value_offset != 0 )
801
90
        {
802
90
          libcerror_error_set(
803
90
           error,
804
90
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
805
90
           LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
806
90
           "%s: invalid template value offset value out of bounds.",
807
90
           function );
808
809
90
          goto on_error;
810
90
        }
811
601k
        if( libfwevt_xml_document_read_value(
812
601k
             internal_xml_document,
813
601k
             xml_sub_token,
814
601k
             binary_data,
815
601k
             binary_data_size,
816
601k
             binary_data_offset + xml_document_data_offset,
817
601k
             attribute_xml_tag,
818
601k
             ascii_codepage,
819
601k
             error ) != 1 )
820
313
        {
821
313
          libcerror_error_set(
822
313
           error,
823
313
           LIBCERROR_ERROR_DOMAIN_IO,
824
313
           LIBCERROR_IO_ERROR_READ_FAILED,
825
313
           "%s: unable to read value.",
826
313
           function );
827
828
313
          goto on_error;
829
313
        }
830
601k
        break;
831
832
601k
      case LIBFWEVT_XML_TOKEN_NORMAL_SUBSTITUTION:
833
82.8k
        result = libfwevt_xml_document_read_normal_substitution(
834
82.8k
                  internal_xml_document,
835
82.8k
                  xml_sub_token,
836
82.8k
                  binary_data,
837
82.8k
                  binary_data_size,
838
82.8k
                  binary_data_offset + xml_document_data_offset,
839
82.8k
                  ascii_codepage,
840
82.8k
                  flags,
841
82.8k
                  template_values_array,
842
82.8k
                  &template_value_offset,
843
82.8k
                  attribute_xml_tag,
844
82.8k
                  element_recursion_depth,
845
82.8k
                  template_instance_recursion_depth,
846
82.8k
                  error );
847
848
82.8k
        if( result == -1 )
849
8.77k
        {
850
8.77k
          libcerror_error_set(
851
8.77k
           error,
852
8.77k
           LIBCERROR_ERROR_DOMAIN_IO,
853
8.77k
           LIBCERROR_IO_ERROR_READ_FAILED,
854
8.77k
           "%s: unable to read normal substitution.",
855
8.77k
           function );
856
857
8.77k
          goto on_error;
858
8.77k
        }
859
74.0k
        break;
860
861
561k
      case LIBFWEVT_XML_TOKEN_OPTIONAL_SUBSTITUTION:
862
561k
        result = libfwevt_xml_document_read_optional_substitution(
863
561k
            internal_xml_document,
864
561k
            xml_sub_token,
865
561k
            binary_data,
866
561k
            binary_data_size,
867
561k
            binary_data_offset + xml_document_data_offset,
868
561k
            ascii_codepage,
869
561k
            flags,
870
561k
            template_values_array,
871
561k
            &template_value_offset,
872
561k
            attribute_xml_tag,
873
561k
                  element_recursion_depth,
874
561k
                  template_instance_recursion_depth,
875
561k
            error );
876
877
561k
        if( result == -1 )
878
5.34k
        {
879
5.34k
          libcerror_error_set(
880
5.34k
           error,
881
5.34k
           LIBCERROR_ERROR_DOMAIN_IO,
882
5.34k
           LIBCERROR_IO_ERROR_READ_FAILED,
883
5.34k
           "%s: unable to read optional substitution.",
884
5.34k
           function );
885
886
5.34k
          goto on_error;
887
5.34k
        }
888
556k
        break;
889
890
556k
      default:
891
205
        libcerror_error_set(
892
205
         error,
893
205
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
894
205
         LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
895
205
         "%s: invalid binary XML sub token - unsupported type: 0x%02" PRIx8 ".",
896
205
         function,
897
205
         xml_sub_token->type );
898
899
205
        goto on_error;
900
1.24M
    }
901
1.23M
    if( result != 0 )
902
1.06M
    {
903
1.06M
      if( libfwevt_xml_tag_append_attribute(
904
1.06M
           xml_tag,
905
1.06M
           attribute_xml_tag,
906
1.06M
           error ) != 1 )
907
0
      {
908
0
        libcerror_error_set(
909
0
         error,
910
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
911
0
         LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
912
0
         "%s: unable to append attribute to XML tag.",
913
0
         function );
914
915
0
        goto on_error;
916
0
      }
917
1.06M
      attribute_xml_tag = NULL;
918
1.06M
    }
919
1.23M
    xml_document_data_offset += xml_sub_token->size;
920
921
1.23M
    template_value_array_recursion_depth++;
922
1.23M
  }
923
1.23M
  while( template_value_offset > 0 );
924
925
1.22M
  xml_token->size = xml_document_data_offset;
926
927
1.22M
  if( attribute_xml_tag != NULL )
928
166k
  {
929
166k
    if( libfwevt_internal_xml_tag_free(
930
166k
         (libfwevt_internal_xml_tag_t **) &attribute_xml_tag,
931
166k
         error ) != 1 )
932
0
    {
933
0
      libcerror_error_set(
934
0
       error,
935
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
936
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
937
0
       "%s: unable to free attribute XML tag.",
938
0
       function );
939
940
0
      goto on_error;
941
0
    }
942
166k
  }
943
1.22M
  if( libfwevt_xml_token_free(
944
1.22M
       &xml_sub_token,
945
1.22M
       error ) != 1 )
946
0
  {
947
0
    libcerror_error_set(
948
0
     error,
949
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
950
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
951
0
     "%s: unable to free binary XML sub token.",
952
0
     function );
953
954
0
    goto on_error;
955
0
  }
956
1.22M
  return( 1 );
957
958
15.9k
on_error:
959
15.9k
  if( attribute_xml_tag != NULL )
960
15.0k
  {
961
15.0k
    libfwevt_internal_xml_tag_free(
962
15.0k
     (libfwevt_internal_xml_tag_t **) &attribute_xml_tag,
963
15.0k
     NULL );
964
15.0k
  }
965
15.9k
  if( xml_sub_token != NULL )
966
15.9k
  {
967
15.9k
    libfwevt_xml_token_free(
968
15.9k
     &xml_sub_token,
969
15.9k
     NULL );
970
15.9k
  }
971
15.9k
  return( -1 );
972
1.22M
}
973
974
/* Reads a CDATA section from a binary XML document
975
 * Returns 1 if successful or -1 on error
976
 */
977
int libfwevt_xml_document_read_cdata_section(
978
     libfwevt_internal_xml_document_t *internal_xml_document,
979
     libfwevt_xml_token_t *xml_token,
980
     const uint8_t *binary_data,
981
     size_t binary_data_size,
982
     size_t binary_data_offset,
983
     libfwevt_xml_tag_t *xml_tag,
984
     int ascii_codepage LIBFWEVT_ATTRIBUTE_UNUSED,
985
     libcerror_error_t **error )
986
24.1k
{
987
24.1k
  const uint8_t *xml_document_data = NULL;
988
24.1k
  static char *function            = "libfwevt_xml_document_read_cdata_section";
989
24.1k
  size_t value_data_size           = 0;
990
24.1k
  size_t xml_document_data_size    = 0;
991
992
24.1k
  LIBFWEVT_UNREFERENCED_PARAMETER( ascii_codepage )
993
994
24.1k
  if( internal_xml_document == NULL )
995
0
  {
996
0
    libcerror_error_set(
997
0
     error,
998
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
999
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1000
0
     "%s: invalid binary XML document.",
1001
0
     function );
1002
1003
0
    return( -1 );
1004
0
  }
1005
24.1k
  if( xml_token == NULL )
1006
0
  {
1007
0
    libcerror_error_set(
1008
0
     error,
1009
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1010
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1011
0
     "%s: invalid binary XML token.",
1012
0
     function );
1013
1014
0
    return( -1 );
1015
0
  }
1016
24.1k
  if( xml_token->type != LIBFWEVT_XML_TOKEN_CDATA_SECTION )
1017
34
  {
1018
34
    libcerror_error_set(
1019
34
     error,
1020
34
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1021
34
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
1022
34
     "%s: invalid binary XML token - unsupported type: 0x%02" PRIx8 ".",
1023
34
     function,
1024
34
     xml_token->type );
1025
1026
34
    return( -1 );
1027
34
  }
1028
24.1k
  if( binary_data == NULL )
1029
0
  {
1030
0
    libcerror_error_set(
1031
0
     error,
1032
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1033
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1034
0
     "%s: invalid binary data.",
1035
0
     function );
1036
1037
0
    return( -1 );
1038
0
  }
1039
24.1k
  if( binary_data_size > (size_t) SSIZE_MAX )
1040
0
  {
1041
0
    libcerror_error_set(
1042
0
     error,
1043
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1044
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1045
0
     "%s: invalid binary XML document data size value exceeds maximum.",
1046
0
     function );
1047
1048
0
    return( -1 );
1049
0
  }
1050
24.1k
  if( binary_data_offset >= binary_data_size )
1051
0
  {
1052
0
    libcerror_error_set(
1053
0
     error,
1054
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1055
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1056
0
     "%s: invalid binary data offset value out of bounds.",
1057
0
     function );
1058
1059
0
    return( -1 );
1060
0
  }
1061
24.1k
  if( xml_tag == NULL )
1062
0
  {
1063
0
    libcerror_error_set(
1064
0
     error,
1065
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1066
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1067
0
     "%s: invalid XML tag.",
1068
0
     function );
1069
1070
0
    return( -1 );
1071
0
  }
1072
24.1k
  xml_document_data      = &( binary_data[ binary_data_offset ] );
1073
24.1k
  xml_document_data_size = binary_data_size - binary_data_offset;
1074
1075
24.1k
  if( xml_document_data_size < 3 )
1076
88
  {
1077
88
    libcerror_error_set(
1078
88
     error,
1079
88
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1080
88
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1081
88
     "%s: invalid binary XML document data size value too small.",
1082
88
     function );
1083
1084
88
    return( -1 );
1085
88
  }
1086
24.0k
  if( libfwevt_xml_tag_set_type(
1087
24.0k
       xml_tag,
1088
24.0k
       LIBFWEVT_XML_TAG_TYPE_CDATA,
1089
24.0k
       error ) != 1 )
1090
0
  {
1091
0
    libcerror_error_set(
1092
0
     error,
1093
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1094
0
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1095
0
     "%s: unable to set XML tag type.",
1096
0
     function );
1097
1098
0
    return( -1 );
1099
0
  }
1100
#if defined( HAVE_DEBUG_OUTPUT )
1101
  if( libcnotify_verbose != 0 )
1102
  {
1103
    libcnotify_printf(
1104
     "%s: data offset\t\t: 0x%08" PRIzx "\n",
1105
     function,
1106
     binary_data_offset );
1107
1108
    libcnotify_printf(
1109
     "%s: cdata section data:\n",
1110
     function );
1111
    libcnotify_print_data(
1112
     xml_document_data,
1113
     3,
1114
     LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
1115
  }
1116
#endif
1117
24.0k
  byte_stream_copy_to_uint16_little_endian(
1118
24.0k
   &( xml_document_data[ 1 ] ),
1119
24.0k
   value_data_size );
1120
1121
#if defined( HAVE_DEBUG_OUTPUT )
1122
  if( libcnotify_verbose != 0 )
1123
  {
1124
    libcnotify_printf(
1125
     "%s: type\t\t\t: 0x%02" PRIx8 "\n",
1126
     function,
1127
     xml_document_data[ 0 ] );
1128
1129
    libcnotify_printf(
1130
     "%s: number of characters\t: %" PRIzd "\n",
1131
     function,
1132
     value_data_size );
1133
  }
1134
#endif
1135
24.0k
  xml_token->size     = 3;
1136
24.0k
  binary_data_offset += 3;
1137
1138
24.0k
  value_data_size *= 2;
1139
1140
24.0k
  if( ( value_data_size > binary_data_size )
1141
23.7k
   || ( binary_data_offset >= ( binary_data_size - value_data_size ) ) )
1142
530
  {
1143
530
    libcerror_error_set(
1144
530
     error,
1145
530
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1146
530
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1147
530
     "%s: invalid value data size value out of bounds.",
1148
530
     function );
1149
1150
530
    return( -1 );
1151
530
  }
1152
#if defined( HAVE_DEBUG_OUTPUT )
1153
  if( libcnotify_verbose != 0 )
1154
  {
1155
    libcnotify_printf(
1156
     "%s: data offset\t\t: 0x%08" PRIzx "\n",
1157
     function,
1158
     binary_data_offset );
1159
1160
    libcnotify_printf(
1161
     "%s: value data:\n",
1162
     function );
1163
    libcnotify_print_data(
1164
     &( xml_document_data[ 3 ] ),
1165
     value_data_size,
1166
     LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
1167
  }
1168
#endif
1169
23.4k
  if( libfwevt_xml_tag_set_value_type(
1170
23.4k
       xml_tag,
1171
23.4k
       LIBFWEVT_VALUE_TYPE_STRING_UTF16,
1172
23.4k
       error ) != 1 )
1173
31
  {
1174
31
    libcerror_error_set(
1175
31
     error,
1176
31
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1177
31
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1178
31
     "%s: unable to set value type.",
1179
31
     function );
1180
1181
31
    return( -1 );
1182
31
  }
1183
23.4k
  if( libfwevt_xml_tag_set_value_data(
1184
23.4k
       xml_tag,
1185
23.4k
       &( binary_data[ binary_data_offset ] ),
1186
23.4k
       value_data_size,
1187
23.4k
       error ) != 1 )
1188
0
  {
1189
0
    libcerror_error_set(
1190
0
     error,
1191
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1192
0
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1193
0
     "%s: unable to set value data.",
1194
0
     function );
1195
1196
0
    return( -1 );
1197
0
  }
1198
#if defined( HAVE_DEBUG_OUTPUT )
1199
  if( libcnotify_verbose != 0 )
1200
  {
1201
    if( libfwevt_xml_tag_debug_print_value_data_segment(
1202
         xml_tag,
1203
         0,
1204
         0,
1205
         ascii_codepage,
1206
         error ) != 1 )
1207
    {
1208
      libcerror_error_set(
1209
       error,
1210
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1211
       LIBCERROR_RUNTIME_ERROR_PRINT_FAILED,
1212
       "%s: unable to print value data segment: 0.",
1213
       function );
1214
1215
      return( -1 );
1216
    }
1217
  }
1218
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
1219
1220
23.4k
  xml_token->size += value_data_size;
1221
1222
23.4k
  return( 1 );
1223
23.4k
}
1224
1225
/* Reads a character entity reference from a binary XML document
1226
 * Returns 1 if successful or -1 on error
1227
 */
1228
int libfwevt_xml_document_read_character_reference(
1229
     libfwevt_internal_xml_document_t *internal_xml_document,
1230
     libfwevt_xml_token_t *xml_token,
1231
     const uint8_t *binary_data,
1232
     size_t binary_data_size,
1233
     size_t binary_data_offset,
1234
     libfwevt_xml_tag_t *xml_tag,
1235
     int ascii_codepage LIBFWEVT_ATTRIBUTE_UNUSED,
1236
     libcerror_error_t **error )
1237
31
{
1238
31
  libfwevt_xml_tag_t *character_xml_tag     = NULL;
1239
31
  uint16_t *character_value_string          = NULL;
1240
31
  uint8_t *character_value_utf16_stream     = NULL;
1241
31
  const uint8_t *xml_document_data          = NULL;
1242
31
  static char *function                     = "libfwevt_xml_document_read_character_reference";
1243
31
  size_t character_value_string_index       = 0;
1244
31
  size_t character_value_string_size        = 0;
1245
31
  size_t character_value_utf16_stream_index = 0;
1246
31
  size_t character_value_utf16_stream_size  = 0;
1247
31
  size_t xml_document_data_size             = 0;
1248
31
  uint16_t character_value                  = 0;
1249
31
  int data_segment_index                    = 0;
1250
1251
31
  LIBFWEVT_UNREFERENCED_PARAMETER( ascii_codepage )
1252
1253
31
  if( internal_xml_document == NULL )
1254
0
  {
1255
0
    libcerror_error_set(
1256
0
     error,
1257
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1258
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1259
0
     "%s: invalid binary XML document.",
1260
0
     function );
1261
1262
0
    return( -1 );
1263
0
  }
1264
31
  if( xml_token == NULL )
1265
0
  {
1266
0
    libcerror_error_set(
1267
0
     error,
1268
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1269
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1270
0
     "%s: invalid binary XML token.",
1271
0
     function );
1272
1273
0
    return( -1 );
1274
0
  }
1275
31
  if( ( xml_token->type & 0xbf ) != LIBFWEVT_XML_TOKEN_ENTITY_REFERENCE )
1276
31
  {
1277
31
    libcerror_error_set(
1278
31
     error,
1279
31
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1280
31
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
1281
31
     "%s: invalid binary XML token - unsupported type: 0x%02" PRIx8 ".",
1282
31
     function,
1283
31
     xml_token->type );
1284
1285
31
    return( -1 );
1286
31
  }
1287
0
  if( binary_data == NULL )
1288
0
  {
1289
0
    libcerror_error_set(
1290
0
     error,
1291
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1292
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1293
0
     "%s: invalid binary data.",
1294
0
     function );
1295
1296
0
    return( -1 );
1297
0
  }
1298
0
  if( binary_data_size > (size_t) SSIZE_MAX )
1299
0
  {
1300
0
    libcerror_error_set(
1301
0
     error,
1302
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1303
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1304
0
     "%s: invalid binary XML document data size value exceeds maximum.",
1305
0
     function );
1306
1307
0
    return( -1 );
1308
0
  }
1309
0
  if( binary_data_offset >= binary_data_size )
1310
0
  {
1311
0
    libcerror_error_set(
1312
0
     error,
1313
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1314
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1315
0
     "%s: invalid binary data offset value out of bounds.",
1316
0
     function );
1317
1318
0
    return( -1 );
1319
0
  }
1320
0
  if( xml_tag == NULL )
1321
0
  {
1322
0
    libcerror_error_set(
1323
0
     error,
1324
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1325
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1326
0
     "%s: invalid XML tag.",
1327
0
     function );
1328
1329
0
    return( -1 );
1330
0
  }
1331
0
  xml_document_data      = &( binary_data[ binary_data_offset ] );
1332
0
  xml_document_data_size = binary_data_size - binary_data_offset;
1333
1334
0
  if( xml_document_data_size < 3 )
1335
0
  {
1336
0
    libcerror_error_set(
1337
0
     error,
1338
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1339
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1340
0
     "%s: invalid binary XML document data size value too small.",
1341
0
     function );
1342
1343
0
    return( -1 );
1344
0
  }
1345
#if defined( HAVE_DEBUG_OUTPUT )
1346
  if( libcnotify_verbose != 0 )
1347
  {
1348
    libcnotify_printf(
1349
     "%s: data offset\t\t: 0x%08" PRIzx "\n",
1350
     function,
1351
     binary_data_offset );
1352
1353
    libcnotify_printf(
1354
     "%s: character reference data:\n",
1355
     function );
1356
    libcnotify_print_data(
1357
     xml_document_data,
1358
     3,
1359
     0 );
1360
  }
1361
#endif
1362
0
  byte_stream_copy_to_uint16_little_endian(
1363
0
   &( xml_document_data[ 1 ] ),
1364
0
   character_value );
1365
1366
#if defined( HAVE_DEBUG_OUTPUT )
1367
  if( libcnotify_verbose != 0 )
1368
  {
1369
    libcnotify_printf(
1370
     "%s: type\t\t\t: 0x%02" PRIx8 "\n",
1371
     function,
1372
     xml_document_data[ 0 ] );
1373
1374
    libcnotify_printf(
1375
     "%s: character value\t\t: 0x%04" PRIx16 "\n",
1376
     function,
1377
     character_value );
1378
1379
    libcnotify_printf(
1380
     "\n" );
1381
  }
1382
#endif
1383
0
  xml_token->size = 3;
1384
1385
0
  if( libfwevt_integer_as_unsigned_decimal_get_string_size(
1386
0
       (uint64_t) character_value,
1387
0
       &character_value_string_size,
1388
0
       error ) != 1 )
1389
0
  {
1390
0
    libcerror_error_set(
1391
0
     error,
1392
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1393
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1394
0
     "%s: unable to determine size of character value string.",
1395
0
     function );
1396
1397
0
    goto on_error;
1398
0
  }
1399
0
  character_value_string_size += 3;
1400
1401
0
  if( character_value_string_size > ( MEMORY_MAXIMUM_ALLOCATION_SIZE / sizeof( uint16_t ) ) )
1402
0
  {
1403
0
    libcerror_error_set(
1404
0
     error,
1405
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1406
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1407
0
     "%s: invalid character value string size value out of bounds.",
1408
0
     function );
1409
1410
0
    goto on_error;
1411
0
  }
1412
0
  character_value_string = (uint16_t *) memory_allocate(
1413
0
                                         sizeof( uint16_t ) * character_value_string_size );
1414
1415
0
  if( character_value_string == NULL )
1416
0
  {
1417
0
    libcerror_error_set(
1418
0
     error,
1419
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
1420
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
1421
0
     "%s: unable to create character value string.",
1422
0
     function );
1423
1424
0
    goto on_error;
1425
0
  }
1426
0
  character_value_string[ character_value_string_index++ ] = (uint16_t) '&';
1427
0
  character_value_string[ character_value_string_index++ ] = (uint16_t) '#';
1428
1429
0
  if( libfwevt_integer_as_unsigned_decimal_copy_to_utf16_string_with_index(
1430
0
       (uint64_t) character_value,
1431
0
       character_value_string,
1432
0
       character_value_string_size,
1433
0
       &character_value_string_index,
1434
0
       error ) != 1 )
1435
0
  {
1436
0
    libcerror_error_set(
1437
0
     error,
1438
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1439
0
     LIBCERROR_RUNTIME_ERROR_COPY_FAILED,
1440
0
     "%s: unable to copy character value to UTF-16 string.",
1441
0
     function );
1442
1443
0
    goto on_error;
1444
0
  }
1445
0
  character_value_string[ character_value_string_size - 2 ] = (uint16_t) ';';
1446
0
  character_value_string[ character_value_string_size - 1 ] = 0;
1447
1448
0
  if( libfwevt_xml_tag_set_value_type(
1449
0
       xml_tag,
1450
0
       LIBFWEVT_VALUE_TYPE_STRING_UTF16,
1451
0
       error ) != 1 )
1452
0
  {
1453
0
    libcerror_error_set(
1454
0
     error,
1455
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1456
0
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1457
0
     "%s: unable to set value type.",
1458
0
     function );
1459
1460
0
    goto on_error;
1461
0
  }
1462
  /* Make sure the character value data is in UTF-16 litte-endian
1463
   */
1464
0
  if( ( character_value_utf16_stream_size == 0 )
1465
0
   || ( character_value_utf16_stream_size > ( MEMORY_MAXIMUM_ALLOCATION_SIZE / 2 ) ) )
1466
0
  {
1467
0
    libcerror_error_set(
1468
0
     error,
1469
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1470
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1471
0
     "%s: invalid character value UTF-16 stream size value out of bounds.",
1472
0
     function );
1473
1474
0
    goto on_error;
1475
0
  }
1476
0
  character_value_utf16_stream_size = character_value_string_size * 2;
1477
1478
0
  character_value_utf16_stream = (uint8_t *) memory_allocate(
1479
0
                                              sizeof( uint8_t ) * character_value_utf16_stream_size );
1480
1481
0
  if( character_value_utf16_stream == NULL )
1482
0
  {
1483
0
    libcerror_error_set(
1484
0
     error,
1485
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
1486
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
1487
0
     "%s: unable to create character value UTF-16 stream.",
1488
0
     function );
1489
1490
0
    goto on_error;
1491
0
  }
1492
0
  for( character_value_string_index = 0;
1493
0
       character_value_string_index < character_value_string_size;
1494
0
       character_value_string_index++ )
1495
0
  {
1496
0
    byte_stream_copy_from_uint16_little_endian(
1497
0
     &( character_value_utf16_stream[ character_value_utf16_stream_index ] ),
1498
0
     character_value_string[ character_value_string_index ] );
1499
1500
0
    character_value_utf16_stream_index += 2;
1501
0
  }
1502
0
  memory_free(
1503
0
   character_value_string );
1504
1505
0
  character_value_string = NULL;
1506
1507
0
  if( libfwevt_xml_tag_append_value_data(
1508
0
       xml_tag,
1509
0
       character_value_utf16_stream,
1510
0
       character_value_utf16_stream_size,
1511
0
       &data_segment_index,
1512
0
       error ) != 1 )
1513
0
  {
1514
0
    libcerror_error_set(
1515
0
     error,
1516
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1517
0
     LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
1518
0
     "%s: unable to append value data.",
1519
0
     function );
1520
1521
0
    goto on_error;
1522
0
  }
1523
0
  memory_free(
1524
0
   character_value_utf16_stream );
1525
1526
0
  character_value_utf16_stream = NULL;
1527
1528
#if defined( HAVE_DEBUG_OUTPUT )
1529
  if( libcnotify_verbose != 0 )
1530
  {
1531
    if( libfwevt_xml_tag_debug_print_value_data_segment(
1532
         xml_tag,
1533
         data_segment_index,
1534
         0,
1535
         ascii_codepage,
1536
         error ) != 1 )
1537
    {
1538
      libcerror_error_set(
1539
       error,
1540
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1541
       LIBCERROR_RUNTIME_ERROR_PRINT_FAILED,
1542
       "%s: unable to print value data segment: %d.",
1543
       function,
1544
       data_segment_index );
1545
1546
      goto on_error;
1547
    }
1548
  }
1549
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
1550
1551
0
  if( libfwevt_internal_xml_tag_free(
1552
0
       (libfwevt_internal_xml_tag_t **) &character_xml_tag,
1553
0
       error ) != 1 )
1554
0
  {
1555
0
    libcerror_error_set(
1556
0
     error,
1557
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1558
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1559
0
     "%s: unable to free character XML tag.",
1560
0
     function );
1561
1562
0
    goto on_error;
1563
0
  }
1564
0
  return( 1 );
1565
1566
0
on_error:
1567
0
  if( character_value_utf16_stream != NULL )
1568
0
  {
1569
0
    memory_free(
1570
0
     character_value_utf16_stream );
1571
0
  }
1572
0
  if( character_value_string != NULL )
1573
0
  {
1574
0
    memory_free(
1575
0
     character_value_string );
1576
0
  }
1577
0
  if( character_xml_tag != NULL )
1578
0
  {
1579
0
    libfwevt_internal_xml_tag_free(
1580
0
     (libfwevt_internal_xml_tag_t **) &character_xml_tag,
1581
0
     NULL );
1582
0
  }
1583
0
  return( -1 );
1584
0
}
1585
1586
/* Reads an element from a binary XML document
1587
 * Returns 1 if successful or -1 on error
1588
 */
1589
int libfwevt_xml_document_read_element(
1590
     libfwevt_internal_xml_document_t *internal_xml_document,
1591
     libfwevt_xml_token_t *xml_token,
1592
     const uint8_t *binary_data,
1593
     size_t binary_data_size,
1594
     size_t binary_data_offset,
1595
     int ascii_codepage,
1596
     uint8_t flags,
1597
     libcdata_array_t *template_values_array,
1598
     libfwevt_xml_tag_t *xml_tag,
1599
     int element_recursion_depth,
1600
     int template_instance_recursion_depth,
1601
     libcerror_error_t **error )
1602
1.86M
{
1603
1.86M
  libfwevt_xml_tag_t *element_xml_tag      = NULL;
1604
1.86M
  libfwevt_xml_token_t *xml_sub_token      = NULL;
1605
1.86M
  const uint8_t *xml_document_data         = NULL;
1606
1.86M
  static char *function                    = "libfwevt_xml_document_read_element";
1607
1.86M
  size_t additional_value_size             = 0;
1608
1.86M
  size_t element_size_offset               = 0;
1609
1.86M
  size_t template_value_offset             = 0;
1610
1.86M
  size_t trailing_data_size                = 0;
1611
1.86M
  size_t xml_document_data_offset          = 0;
1612
1.86M
  size_t xml_document_data_size            = 0;
1613
1.86M
  uint32_t attribute_list_size             = 0;
1614
1.86M
  uint32_t element_name_offset             = 0;
1615
1.86M
  uint32_t element_name_size               = 0;
1616
1.86M
  uint32_t element_size                    = 0;
1617
1.86M
  int result                               = 0;
1618
1.86M
  int template_value_array_recursion_depth = 0;
1619
1620
#if defined( HAVE_DEBUG_OUTPUT )
1621
  uint16_t value_16bit                     = 0;
1622
#endif
1623
1624
1.86M
  if( internal_xml_document == NULL )
1625
0
  {
1626
0
    libcerror_error_set(
1627
0
     error,
1628
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1629
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1630
0
     "%s: invalid binary XML document.",
1631
0
     function );
1632
1633
0
    return( -1 );
1634
0
  }
1635
1.86M
  if( xml_token == NULL )
1636
0
  {
1637
0
    libcerror_error_set(
1638
0
     error,
1639
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1640
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1641
0
     "%s: invalid binary XML token.",
1642
0
     function );
1643
1644
0
    return( -1 );
1645
0
  }
1646
1.86M
  if( ( xml_token->type & 0xbf ) != LIBFWEVT_XML_TOKEN_OPEN_START_ELEMENT_TAG )
1647
87
  {
1648
87
    libcerror_error_set(
1649
87
     error,
1650
87
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1651
87
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
1652
87
     "%s: invalid binary XML token - unsupported type: 0x%02" PRIx8 ".",
1653
87
     function,
1654
87
     xml_token->type );
1655
1656
87
    return( -1 );
1657
87
  }
1658
1.86M
  if( binary_data == NULL )
1659
0
  {
1660
0
    libcerror_error_set(
1661
0
     error,
1662
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1663
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1664
0
     "%s: invalid binary data.",
1665
0
     function );
1666
1667
0
    return( -1 );
1668
0
  }
1669
1.86M
  if( binary_data_size > (size_t) SSIZE_MAX )
1670
0
  {
1671
0
    libcerror_error_set(
1672
0
     error,
1673
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1674
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1675
0
     "%s: invalid binary XML document data size value exceeds maximum.",
1676
0
     function );
1677
1678
0
    return( -1 );
1679
0
  }
1680
1.86M
  if( binary_data_offset >= binary_data_size )
1681
0
  {
1682
0
    libcerror_error_set(
1683
0
     error,
1684
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1685
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1686
0
     "%s: invalid binary data offset value out of bounds.",
1687
0
     function );
1688
1689
0
    return( -1 );
1690
0
  }
1691
1.86M
  if( ( element_recursion_depth < 0 )
1692
1.86M
   || ( element_recursion_depth > LIBFWEVT_XML_DOCUMENT_ELEMENT_RECURSION_DEPTH ) )
1693
218
  {
1694
218
    libcerror_error_set(
1695
218
     error,
1696
218
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1697
218
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1698
218
     "%s: invalid element recursion depth value out of bounds.",
1699
218
     function );
1700
1701
218
    return( -1 );
1702
218
  }
1703
1.86M
  if( ( flags & LIBFWEVT_XML_DOCUMENT_READ_FLAG_HAS_DATA_OFFSETS ) != 0 )
1704
1.79M
  {
1705
1.79M
    additional_value_size = 4;
1706
1.79M
  }
1707
1.86M
  if( ( binary_data_size < ( 5 + additional_value_size ) )
1708
1.86M
   || ( binary_data_offset > ( binary_data_size - 5 - additional_value_size ) ) )
1709
641
  {
1710
641
    libcerror_error_set(
1711
641
     error,
1712
641
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1713
641
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1714
641
     "%s: invalid binary XML document data size value too small.",
1715
641
     function );
1716
1717
641
    goto on_error;
1718
641
  }
1719
1.86M
  if( libfwevt_xml_token_initialize(
1720
1.86M
       &xml_sub_token,
1721
1.86M
       error ) != 1 )
1722
0
  {
1723
0
    libcerror_error_set(
1724
0
     error,
1725
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1726
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1727
0
     "%s: unable to create binary XML sub token.",
1728
0
     function );
1729
1730
0
    goto on_error;
1731
0
  }
1732
1.86M
  xml_document_data      = &( binary_data[ binary_data_offset ] );
1733
1.86M
  xml_document_data_size = binary_data_size - binary_data_offset;
1734
1735
1.86M
  do
1736
5.74M
  {
1737
5.74M
    if( ( template_value_array_recursion_depth < 0 )
1738
5.74M
     || ( template_value_array_recursion_depth > LIBFWEVT_XML_DOCUMENT_TEMPLATE_VALUE_ARRAY_RECURSION_DEPTH ) )
1739
222
    {
1740
222
      libcerror_error_set(
1741
222
       error,
1742
222
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1743
222
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1744
222
       "%s: invalid template value array recursion depth value out of bounds.",
1745
222
       function );
1746
1747
222
      goto on_error;
1748
222
    }
1749
5.74M
    if( libfwevt_xml_tag_initialize(
1750
5.74M
         &element_xml_tag,
1751
5.74M
         error ) != 1 )
1752
478
    {
1753
478
      libcerror_error_set(
1754
478
       error,
1755
478
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1756
478
       LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1757
478
       "%s: unable to create element XML tag.",
1758
478
       function );
1759
1760
478
      goto on_error;
1761
478
    }
1762
    /* Note that the dependency identifier is an optional value.
1763
     */
1764
5.74M
    element_size_offset = 1;
1765
1766
5.74M
    byte_stream_copy_to_uint32_little_endian(
1767
5.74M
     &( xml_document_data[ element_size_offset ] ),
1768
5.74M
     element_size );
1769
1770
5.74M
    if( ( xml_document_data_size > 7 )
1771
5.74M
     && ( element_size > ( xml_document_data_size - 7 ) ) )
1772
1.72M
    {
1773
1.72M
      element_size_offset = 3;
1774
1.72M
    }
1775
#if defined( HAVE_DEBUG_OUTPUT )
1776
    if( libcnotify_verbose != 0 )
1777
    {
1778
      libcnotify_printf(
1779
       "%s: data offset\t\t\t\t: 0x%08" PRIzx "\n",
1780
       function,
1781
       binary_data_offset );
1782
1783
      libcnotify_printf(
1784
       "%s: element data:\n",
1785
       function );
1786
      libcnotify_print_data(
1787
       xml_document_data,
1788
       element_size_offset + 4 + additional_value_size,
1789
       LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
1790
    }
1791
#endif
1792
5.74M
    byte_stream_copy_to_uint32_little_endian(
1793
5.74M
     &( xml_document_data[ element_size_offset ] ),
1794
5.74M
     element_size );
1795
1796
#if defined( HAVE_DEBUG_OUTPUT )
1797
    if( libcnotify_verbose != 0 )
1798
    {
1799
      libcnotify_printf(
1800
       "%s: type\t\t\t\t: 0x%02" PRIx8 "\n",
1801
       function,
1802
       xml_document_data[ 0 ] );
1803
1804
      if( element_size_offset == 3 )
1805
      {
1806
        byte_stream_copy_to_uint16_little_endian(
1807
         &( xml_document_data[ 1 ] ),
1808
         value_16bit );
1809
        libcnotify_printf(
1810
         "%s: dependency identifier\t\t: %" PRIi16 " (0x%04" PRIx16 ")\n",
1811
         function,
1812
         (int16_t) value_16bit,
1813
         value_16bit );
1814
      }
1815
      libcnotify_printf(
1816
       "%s: size\t\t\t\t: %" PRIu32 "\n",
1817
       function,
1818
       element_size );
1819
    }
1820
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
1821
1822
5.74M
    xml_document_data_offset = element_size_offset + 4;
1823
1824
    /* The first 5 or 7 bytes are not included in the element size
1825
     */
1826
5.74M
    if( ( flags & LIBFWEVT_XML_DOCUMENT_READ_FLAG_HAS_DATA_OFFSETS ) == 0 )
1827
268k
    {
1828
268k
      element_name_offset = (uint32_t) ( binary_data_offset + xml_document_data_offset );
1829
268k
    }
1830
5.47M
    else
1831
5.47M
    {
1832
5.47M
      if( xml_document_data_offset >= ( xml_document_data_size - 4 ) )
1833
181
      {
1834
181
        libcerror_error_set(
1835
181
         error,
1836
181
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1837
181
         LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1838
181
         "%s: invalid binary XML document data size value too small.",
1839
181
         function );
1840
1841
181
        goto on_error;
1842
181
      }
1843
5.47M
      byte_stream_copy_to_uint32_little_endian(
1844
5.47M
       &( xml_document_data[ xml_document_data_offset ] ),
1845
5.47M
       element_name_offset );
1846
1847
#if defined( HAVE_DEBUG_OUTPUT )
1848
      if( libcnotify_verbose != 0 )
1849
      {
1850
        libcnotify_printf(
1851
         "%s: name offset\t\t\t\t: 0x%08" PRIx32 "\n",
1852
         function,
1853
         element_name_offset );
1854
      }
1855
#endif
1856
5.47M
      xml_document_data_offset += 4;
1857
5.47M
      element_size             -= 4;
1858
5.47M
    }
1859
#if defined( HAVE_DEBUG_OUTPUT )
1860
    if( libcnotify_verbose != 0 )
1861
    {
1862
      libcnotify_printf(
1863
       "\n" );
1864
    }
1865
#endif
1866
5.74M
    if( element_name_offset > ( binary_data_offset + xml_document_data_offset ) )
1867
4.72k
    {
1868
4.72k
      libcerror_error_set(
1869
4.72k
       error,
1870
4.72k
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1871
4.72k
       LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1872
4.72k
       "%s: invalid element data offset value out of bounds.",
1873
4.72k
       function );
1874
1875
4.72k
      goto on_error;
1876
4.72k
    }
1877
5.73M
    if( ( binary_data_offset + xml_document_data_offset ) < element_name_offset )
1878
0
    {
1879
0
      trailing_data_size = element_name_offset - ( binary_data_offset + xml_document_data_offset );
1880
1881
#if defined( HAVE_DEBUG_OUTPUT )
1882
      if( libcnotify_verbose != 0 )
1883
      {
1884
        libcnotify_printf(
1885
         "%s: data offset\t\t\t\t: 0x%08" PRIzx "\n",
1886
         function,
1887
         binary_data_offset + xml_document_data_offset );
1888
1889
        libcnotify_printf(
1890
         "%s: trailing data:\n",
1891
         function );
1892
        libcnotify_print_data(
1893
         &( xml_document_data[ xml_document_data_offset ] ),
1894
         trailing_data_size,
1895
         LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
1896
      }
1897
#endif
1898
0
      xml_document_data_offset += trailing_data_size;
1899
0
      element_size             -= (uint32_t) trailing_data_size;
1900
0
    }
1901
5.73M
    if( libfwevt_xml_document_read_name(
1902
5.73M
         internal_xml_document,
1903
5.73M
         binary_data,
1904
5.73M
         binary_data_size,
1905
5.73M
         element_name_offset,
1906
5.73M
         flags,
1907
5.73M
         &element_name_size,
1908
5.73M
         element_xml_tag,
1909
5.73M
         error ) != 1 )
1910
13.4k
    {
1911
13.4k
      libcerror_error_set(
1912
13.4k
       error,
1913
13.4k
       LIBCERROR_ERROR_DOMAIN_IO,
1914
13.4k
       LIBCERROR_IO_ERROR_READ_FAILED,
1915
13.4k
       "%s: unable to read element name.",
1916
13.4k
       function );
1917
1918
13.4k
      goto on_error;
1919
13.4k
    }
1920
5.72M
    if( ( binary_data_offset + xml_document_data_offset ) == element_name_offset )
1921
307k
    {
1922
307k
      xml_document_data_offset += element_name_size;
1923
307k
      element_size             -= element_name_size;
1924
307k
    }
1925
5.72M
    if( ( xml_token->type & LIBFWEVT_XML_TOKEN_FLAG_HAS_MORE_DATA ) != 0 )
1926
898k
    {
1927
898k
      if( xml_document_data_offset >= ( xml_document_data_size - 4 ) )
1928
71
      {
1929
71
        libcerror_error_set(
1930
71
         error,
1931
71
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1932
71
         LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1933
71
         "%s: invalid binary XML document data size value too small.",
1934
71
         function );
1935
1936
71
        goto on_error;
1937
71
      }
1938
#if defined( HAVE_DEBUG_OUTPUT )
1939
      if( libcnotify_verbose != 0 )
1940
      {
1941
        libcnotify_printf(
1942
         "%s: data offset\t\t\t\t: 0x%08" PRIzx "\n",
1943
         function,
1944
         binary_data_offset + xml_document_data_offset );
1945
1946
        libcnotify_printf(
1947
         "%s: attribute list data:\n",
1948
         function );
1949
        libcnotify_print_data(
1950
         &( xml_document_data[ xml_document_data_offset ] ),
1951
         4,
1952
         0 );
1953
      }
1954
#endif
1955
898k
      byte_stream_copy_to_uint32_little_endian(
1956
898k
       &( xml_document_data[ xml_document_data_offset ] ),
1957
898k
       attribute_list_size );
1958
1959
#if defined( HAVE_DEBUG_OUTPUT )
1960
      if( libcnotify_verbose != 0 )
1961
      {
1962
        libcnotify_printf(
1963
         "%s: attribute list size\t\t\t: %" PRIu32 "\n",
1964
         function,
1965
         attribute_list_size );
1966
1967
        libcnotify_printf(
1968
         "\n" );
1969
      }
1970
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
1971
1972
898k
      xml_document_data_offset += 4;
1973
898k
      element_size             -= 4;
1974
1975
898k
      if( attribute_list_size > ( binary_data_size - ( binary_data_offset + xml_document_data_offset ) ) )
1976
510
      {
1977
510
        libcerror_error_set(
1978
510
         error,
1979
510
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
1980
510
         LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
1981
510
         "%s: invalid attribute list size value out of bounds.",
1982
510
         function );
1983
1984
510
        goto on_error;
1985
510
      }
1986
2.11M
      while( attribute_list_size > 0 )
1987
1.23M
      {
1988
1.23M
        if( libfwevt_xml_token_read_data(
1989
1.23M
             xml_sub_token,
1990
1.23M
             binary_data,
1991
1.23M
             binary_data_size,
1992
1.23M
             binary_data_offset + xml_document_data_offset,
1993
1.23M
             error ) != 1 )
1994
110
        {
1995
110
          libcerror_error_set(
1996
110
           error,
1997
110
           LIBCERROR_ERROR_DOMAIN_IO,
1998
110
           LIBCERROR_IO_ERROR_READ_FAILED,
1999
110
           "%s: unable to read binary XML sub token.",
2000
110
           function );
2001
2002
110
          goto on_error;
2003
110
        }
2004
1.23M
        if( libfwevt_xml_document_read_attribute(
2005
1.23M
             internal_xml_document,
2006
1.23M
             xml_sub_token,
2007
1.23M
             binary_data,
2008
1.23M
             binary_data_size,
2009
1.23M
             binary_data_offset + xml_document_data_offset,
2010
1.23M
             ascii_codepage,
2011
1.23M
             flags,
2012
1.23M
             template_values_array,
2013
1.23M
             element_xml_tag,
2014
1.23M
             element_recursion_depth,
2015
1.23M
             template_instance_recursion_depth,
2016
1.23M
             error ) != 1 )
2017
16.1k
        {
2018
16.1k
          libcerror_error_set(
2019
16.1k
           error,
2020
16.1k
           LIBCERROR_ERROR_DOMAIN_IO,
2021
16.1k
           LIBCERROR_IO_ERROR_READ_FAILED,
2022
16.1k
           "%s: unable to read attribute.",
2023
16.1k
           function );
2024
2025
16.1k
          goto on_error;
2026
16.1k
        }
2027
1.22M
        xml_document_data_offset += xml_sub_token->size;
2028
1.22M
        element_size             -= (uint32_t) xml_sub_token->size;
2029
2030
1.22M
        if( attribute_list_size < xml_sub_token->size )
2031
429
        {
2032
429
          libcerror_error_set(
2033
429
           error,
2034
429
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
2035
429
           LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
2036
429
           "%s: invalid attribute list size value too small.",
2037
429
           function );
2038
2039
429
          goto on_error;
2040
429
        }
2041
1.22M
        attribute_list_size -= (uint32_t) xml_sub_token->size;
2042
1.22M
      }
2043
897k
    }
2044
5.70M
    if( libfwevt_xml_token_read_data(
2045
5.70M
         xml_sub_token,
2046
5.70M
         binary_data,
2047
5.70M
         binary_data_size,
2048
5.70M
         binary_data_offset + xml_document_data_offset,
2049
5.70M
         error ) != 1 )
2050
255
    {
2051
255
      libcerror_error_set(
2052
255
       error,
2053
255
       LIBCERROR_ERROR_DOMAIN_IO,
2054
255
       LIBCERROR_IO_ERROR_READ_FAILED,
2055
255
       "%s: unable to read binary XML sub token.",
2056
255
       function );
2057
2058
255
      goto on_error;
2059
255
    }
2060
5.70M
    if( ( xml_sub_token->type != LIBFWEVT_XML_TOKEN_CLOSE_START_ELEMENT_TAG )
2061
480k
     && ( xml_sub_token->type != LIBFWEVT_XML_TOKEN_CLOSE_EMPTY_ELEMENT_TAG ) )
2062
539
    {
2063
539
      libcerror_error_set(
2064
539
       error,
2065
539
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
2066
539
       LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
2067
539
       "%s: invalid binary XML token - unsupported type: 0x%02" PRIx8 ".",
2068
539
       function,
2069
539
       xml_token->type );
2070
2071
539
      goto on_error;
2072
539
    }
2073
5.70M
    if( xml_document_data_offset >= xml_document_data_size )
2074
0
    {
2075
0
      libcerror_error_set(
2076
0
       error,
2077
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
2078
0
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
2079
0
       "%s: invalid binary XML document data size value too small.",
2080
0
       function );
2081
2082
0
      goto on_error;
2083
0
    }
2084
#if defined( HAVE_DEBUG_OUTPUT )
2085
    if( libcnotify_verbose != 0 )
2086
    {
2087
      libcnotify_printf(
2088
       "%s: data offset\t\t\t\t: 0x%08" PRIzx "\n",
2089
       function,
2090
       binary_data_offset + xml_document_data_offset );
2091
2092
      libcnotify_printf(
2093
       "%s: close element tag data:\n",
2094
       function );
2095
      libcnotify_print_data(
2096
       &( xml_document_data[ xml_document_data_offset ] ),
2097
       1,
2098
       0 );
2099
    }
2100
#endif
2101
#if defined( HAVE_DEBUG_OUTPUT )
2102
    if( libcnotify_verbose != 0 )
2103
    {
2104
      libcnotify_printf(
2105
       "%s: type\t\t\t\t: 0x%02" PRIx8 "\n",
2106
       function,
2107
       xml_document_data[ xml_document_data_offset ] );
2108
2109
      libcnotify_printf(
2110
       "\n" );
2111
    }
2112
#endif
2113
5.70M
    xml_document_data_offset += 1;
2114
5.70M
    element_size             -= 1;
2115
2116
5.70M
    if( xml_sub_token->type == LIBFWEVT_XML_TOKEN_CLOSE_START_ELEMENT_TAG )
2117
5.22M
    {
2118
5.22M
      result = 1;
2119
2120
15.8M
      while( element_size > 0 )
2121
12.0M
      {
2122
12.0M
        if( libfwevt_xml_token_read_data(
2123
12.0M
             xml_sub_token,
2124
12.0M
             binary_data,
2125
12.0M
             binary_data_size,
2126
12.0M
             binary_data_offset + xml_document_data_offset,
2127
12.0M
             error ) != 1 )
2128
1.24k
        {
2129
1.24k
          libcerror_error_set(
2130
1.24k
           error,
2131
1.24k
           LIBCERROR_ERROR_DOMAIN_IO,
2132
1.24k
           LIBCERROR_IO_ERROR_READ_FAILED,
2133
1.24k
           "%s: unable to read binary XML sub token.",
2134
1.24k
           function );
2135
2136
1.24k
          goto on_error;
2137
1.24k
        }
2138
12.0M
        switch( xml_sub_token->type & 0xbf )
2139
12.0M
        {
2140
1.41M
          case LIBFWEVT_XML_TOKEN_OPEN_START_ELEMENT_TAG:
2141
1.41M
            if( libfwevt_xml_document_read_element(
2142
1.41M
                 internal_xml_document,
2143
1.41M
                 xml_sub_token,
2144
1.41M
                 binary_data,
2145
1.41M
                 binary_data_size,
2146
1.41M
                 binary_data_offset + xml_document_data_offset,
2147
1.41M
                 ascii_codepage,
2148
1.41M
                 flags,
2149
1.41M
                 template_values_array,
2150
1.41M
                 element_xml_tag,
2151
1.41M
                 element_recursion_depth + 1,
2152
1.41M
                 template_instance_recursion_depth,
2153
1.41M
                 error ) != 1 )
2154
43.3k
            {
2155
43.3k
              libcerror_error_set(
2156
43.3k
               error,
2157
43.3k
               LIBCERROR_ERROR_DOMAIN_IO,
2158
43.3k
               LIBCERROR_IO_ERROR_READ_FAILED,
2159
43.3k
               "%s: unable to read element.",
2160
43.3k
               function );
2161
2162
43.3k
              goto on_error;
2163
43.3k
            }
2164
1.36M
            break;
2165
2166
1.36M
          case LIBFWEVT_XML_TOKEN_CLOSE_EMPTY_ELEMENT_TAG:
2167
1.30M
          case LIBFWEVT_XML_TOKEN_END_ELEMENT_TAG:
2168
1.30M
            if( xml_document_data_offset >= xml_document_data_size )
2169
0
            {
2170
0
              libcerror_error_set(
2171
0
               error,
2172
0
               LIBCERROR_ERROR_DOMAIN_RUNTIME,
2173
0
               LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
2174
0
               "%s: invalid binary XML document data size value too small.",
2175
0
               function );
2176
2177
0
              goto on_error;
2178
0
            }
2179
#if defined( HAVE_DEBUG_OUTPUT )
2180
            if( libcnotify_verbose != 0 )
2181
            {
2182
              libcnotify_printf(
2183
               "%s: data offset\t\t\t\t: 0x%08" PRIzx "\n",
2184
               function,
2185
               binary_data_offset + xml_document_data_offset );
2186
2187
              libcnotify_printf(
2188
               "%s: end element tag data:\n",
2189
               function );
2190
              libcnotify_print_data(
2191
               &( xml_document_data[ xml_document_data_offset ] ),
2192
               1,
2193
               0 );
2194
            }
2195
#endif
2196
#if defined( HAVE_DEBUG_OUTPUT )
2197
            if( libcnotify_verbose != 0 )
2198
            {
2199
              libcnotify_printf(
2200
               "%s: type\t\t\t\t: 0x%02" PRIx8 "\n",
2201
               function,
2202
               xml_document_data[ xml_document_data_offset ] );
2203
2204
              libcnotify_printf(
2205
               "\n" );
2206
            }
2207
#endif
2208
1.30M
            xml_sub_token->size = 1;
2209
2210
1.30M
            break;
2211
2212
24.1k
          case LIBFWEVT_XML_TOKEN_CDATA_SECTION:
2213
24.1k
            if( template_value_offset != 0 )
2214
16
            {
2215
16
              libcerror_error_set(
2216
16
               error,
2217
16
               LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2218
16
               LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
2219
16
               "%s: invalid template value offset value out of bounds.",
2220
16
               function );
2221
2222
16
              goto on_error;
2223
16
            }
2224
24.1k
            if( libfwevt_xml_document_read_cdata_section(
2225
24.1k
                 internal_xml_document,
2226
24.1k
                 xml_sub_token,
2227
24.1k
                 binary_data,
2228
24.1k
                 binary_data_size,
2229
24.1k
                 binary_data_offset + xml_document_data_offset,
2230
24.1k
                 element_xml_tag,
2231
24.1k
                 ascii_codepage,
2232
24.1k
                 error ) != 1 )
2233
683
            {
2234
683
              libcerror_error_set(
2235
683
               error,
2236
683
               LIBCERROR_ERROR_DOMAIN_IO,
2237
683
               LIBCERROR_IO_ERROR_READ_FAILED,
2238
683
               "%s: unable to read CDATA section.",
2239
683
               function );
2240
2241
683
              goto on_error;
2242
683
            }
2243
23.4k
            break;
2244
2245
34.0k
          case LIBFWEVT_XML_TOKEN_PI_TARGET:
2246
34.0k
            if( template_value_offset != 0 )
2247
100
            {
2248
100
              libcerror_error_set(
2249
100
               error,
2250
100
               LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2251
100
               LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
2252
100
               "%s: invalid template value offset value out of bounds.",
2253
100
               function );
2254
2255
100
              goto on_error;
2256
100
            }
2257
33.9k
            if( libfwevt_xml_document_read_pi_target(
2258
33.9k
                 internal_xml_document,
2259
33.9k
                 xml_sub_token,
2260
33.9k
                 binary_data,
2261
33.9k
                 binary_data_size,
2262
33.9k
                 binary_data_offset + xml_document_data_offset,
2263
33.9k
                 flags,
2264
33.9k
                 element_xml_tag,
2265
33.9k
                 ascii_codepage,
2266
33.9k
                 error ) != 1 )
2267
1.47k
            {
2268
1.47k
              libcerror_error_set(
2269
1.47k
               error,
2270
1.47k
               LIBCERROR_ERROR_DOMAIN_IO,
2271
1.47k
               LIBCERROR_IO_ERROR_READ_FAILED,
2272
1.47k
               "%s: unable to read PI target.",
2273
1.47k
               function );
2274
2275
1.47k
              goto on_error;
2276
1.47k
            }
2277
32.4k
            break;
2278
2279
32.4k
          case LIBFWEVT_XML_TOKEN_CHARACTER_REFERENCE:
2280
52
            if( template_value_offset != 0 )
2281
21
            {
2282
21
              libcerror_error_set(
2283
21
               error,
2284
21
               LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2285
21
               LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
2286
21
               "%s: invalid template value offset value out of bounds.",
2287
21
               function );
2288
2289
21
              goto on_error;
2290
21
            }
2291
31
            if( libfwevt_xml_document_read_character_reference(
2292
31
                 internal_xml_document,
2293
31
                 xml_sub_token,
2294
31
                 binary_data,
2295
31
                 binary_data_size,
2296
31
                 binary_data_offset + xml_document_data_offset,
2297
31
                 element_xml_tag,
2298
31
                 ascii_codepage,
2299
31
                 error ) != 1 )
2300
31
            {
2301
31
              libcerror_error_set(
2302
31
               error,
2303
31
               LIBCERROR_ERROR_DOMAIN_IO,
2304
31
               LIBCERROR_IO_ERROR_READ_FAILED,
2305
31
               "%s: unable to read character reference.",
2306
31
               function );
2307
2308
31
              goto on_error;
2309
31
            }
2310
0
            break;
2311
2312
131k
          case LIBFWEVT_XML_TOKEN_ENTITY_REFERENCE:
2313
131k
            if( template_value_offset != 0 )
2314
14
            {
2315
14
              libcerror_error_set(
2316
14
               error,
2317
14
               LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2318
14
               LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
2319
14
               "%s: invalid template value offset value out of bounds.",
2320
14
               function );
2321
2322
14
              goto on_error;
2323
14
            }
2324
131k
            if( libfwevt_xml_document_read_entity_reference(
2325
131k
                 internal_xml_document,
2326
131k
                 xml_sub_token,
2327
131k
                 binary_data,
2328
131k
                 binary_data_size,
2329
131k
                 binary_data_offset + xml_document_data_offset,
2330
131k
                 flags,
2331
131k
                 element_xml_tag,
2332
131k
                 ascii_codepage,
2333
131k
                 error ) != 1 )
2334
3.86k
            {
2335
3.86k
              libcerror_error_set(
2336
3.86k
               error,
2337
3.86k
               LIBCERROR_ERROR_DOMAIN_IO,
2338
3.86k
               LIBCERROR_IO_ERROR_READ_FAILED,
2339
3.86k
               "%s: unable to read entity reference.",
2340
3.86k
               function );
2341
2342
3.86k
              goto on_error;
2343
3.86k
            }
2344
127k
            break;
2345
2346
1.48M
          case LIBFWEVT_XML_TOKEN_VALUE:
2347
1.48M
            if( template_value_offset != 0 )
2348
78
            {
2349
78
              libcerror_error_set(
2350
78
               error,
2351
78
               LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2352
78
               LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
2353
78
               "%s: invalid template value offset value out of bounds.",
2354
78
               function );
2355
2356
78
              goto on_error;
2357
78
            }
2358
1.48M
            if( libfwevt_xml_document_read_value(
2359
1.48M
                 internal_xml_document,
2360
1.48M
                 xml_sub_token,
2361
1.48M
                 binary_data,
2362
1.48M
                 binary_data_size,
2363
1.48M
                 binary_data_offset + xml_document_data_offset,
2364
1.48M
                 element_xml_tag,
2365
1.48M
                 ascii_codepage,
2366
1.48M
                 error ) != 1 )
2367
637
            {
2368
637
              libcerror_error_set(
2369
637
               error,
2370
637
               LIBCERROR_ERROR_DOMAIN_IO,
2371
637
               LIBCERROR_IO_ERROR_READ_FAILED,
2372
637
               "%s: unable to read value.",
2373
637
               function );
2374
2375
637
              goto on_error;
2376
637
            }
2377
1.48M
            break;
2378
2379
1.98M
          case LIBFWEVT_XML_TOKEN_NORMAL_SUBSTITUTION:
2380
1.98M
            result = libfwevt_xml_document_read_normal_substitution(
2381
1.98M
                internal_xml_document,
2382
1.98M
                xml_sub_token,
2383
1.98M
                binary_data,
2384
1.98M
                binary_data_size,
2385
1.98M
                binary_data_offset + xml_document_data_offset,
2386
1.98M
                ascii_codepage,
2387
1.98M
                flags,
2388
1.98M
                template_values_array,
2389
1.98M
                &template_value_offset,
2390
1.98M
                element_xml_tag,
2391
1.98M
                element_recursion_depth,
2392
1.98M
                template_instance_recursion_depth,
2393
1.98M
                error );
2394
2395
1.98M
            if( result == -1 )
2396
15.4k
            {
2397
15.4k
              libcerror_error_set(
2398
15.4k
               error,
2399
15.4k
               LIBCERROR_ERROR_DOMAIN_IO,
2400
15.4k
               LIBCERROR_IO_ERROR_READ_FAILED,
2401
15.4k
               "%s: unable to read normal substitution.",
2402
15.4k
               function );
2403
2404
15.4k
              goto on_error;
2405
15.4k
            }
2406
1.96M
            break;
2407
2408
5.63M
          case LIBFWEVT_XML_TOKEN_OPTIONAL_SUBSTITUTION:
2409
5.63M
            result = libfwevt_xml_document_read_optional_substitution(
2410
5.63M
                internal_xml_document,
2411
5.63M
                xml_sub_token,
2412
5.63M
                binary_data,
2413
5.63M
                binary_data_size,
2414
5.63M
                binary_data_offset + xml_document_data_offset,
2415
5.63M
                ascii_codepage,
2416
5.63M
                flags,
2417
5.63M
                template_values_array,
2418
5.63M
                &template_value_offset,
2419
5.63M
                element_xml_tag,
2420
5.63M
                      element_recursion_depth,
2421
5.63M
                template_instance_recursion_depth,
2422
5.63M
                error );
2423
2424
5.63M
            if( result == -1 )
2425
33.3k
            {
2426
33.3k
              libcerror_error_set(
2427
33.3k
               error,
2428
33.3k
               LIBCERROR_ERROR_DOMAIN_IO,
2429
33.3k
               LIBCERROR_IO_ERROR_READ_FAILED,
2430
33.3k
               "%s: unable to read optional substitution.",
2431
33.3k
               function );
2432
2433
33.3k
              goto on_error;
2434
33.3k
            }
2435
5.59M
            break;
2436
2437
5.59M
          default:
2438
481
            libcerror_error_set(
2439
481
             error,
2440
481
             LIBCERROR_ERROR_DOMAIN_RUNTIME,
2441
481
             LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
2442
481
             "%s: invalid binary XML sub token - unsupported type: 0x%02" PRIx8 ".",
2443
481
             function,
2444
481
             xml_sub_token->type );
2445
2446
481
            goto on_error;
2447
12.0M
        }
2448
11.9M
        xml_document_data_offset += xml_sub_token->size;
2449
2450
11.9M
        if( element_size < xml_sub_token->size )
2451
412
        {
2452
412
          libcerror_error_set(
2453
412
           error,
2454
412
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
2455
412
           LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
2456
412
           "%s: invalid element size value too small.",
2457
412
           function );
2458
2459
412
          goto on_error;
2460
412
        }
2461
11.9M
        element_size -= (uint32_t) xml_sub_token->size;
2462
2463
11.9M
        if( ( xml_sub_token->type == LIBFWEVT_XML_TOKEN_CLOSE_EMPTY_ELEMENT_TAG )
2464
11.7M
         || ( xml_sub_token->type == LIBFWEVT_XML_TOKEN_END_ELEMENT_TAG ) )
2465
1.30M
        {
2466
1.30M
          break;
2467
1.30M
        }
2468
11.9M
      }
2469
5.22M
    }
2470
479k
    else if( xml_sub_token->type == LIBFWEVT_XML_TOKEN_CLOSE_EMPTY_ELEMENT_TAG )
2471
479k
    {
2472
479k
      result = 1;
2473
479k
    }
2474
5.60M
    if( element_size > 0 )
2475
1.03k
    {
2476
1.03k
      libcerror_error_set(
2477
1.03k
       error,
2478
1.03k
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
2479
1.03k
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
2480
1.03k
       "%s: invalid element size value out of bounds.",
2481
1.03k
       function );
2482
2483
1.03k
      goto on_error;
2484
1.03k
    }
2485
5.60M
    if( result != 0 )
2486
5.50M
    {
2487
5.50M
      if( xml_tag != NULL )
2488
5.47M
      {
2489
5.47M
        if( libfwevt_xml_tag_append_element(
2490
5.47M
             xml_tag,
2491
5.47M
             element_xml_tag,
2492
5.47M
             error ) != 1 )
2493
0
        {
2494
0
          libcerror_error_set(
2495
0
           error,
2496
0
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
2497
0
           LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
2498
0
           "%s: unable to append element to XML tag.",
2499
0
           function );
2500
2501
0
          goto on_error;
2502
0
        }
2503
5.47M
        element_xml_tag = NULL;
2504
5.47M
      }
2505
24.7k
      else if( internal_xml_document->root_xml_tag == NULL )
2506
23.7k
      {
2507
23.7k
        internal_xml_document->root_xml_tag = element_xml_tag;
2508
2509
23.7k
        element_xml_tag = NULL;
2510
23.7k
      }
2511
5.50M
    }
2512
5.60M
    template_value_array_recursion_depth++;
2513
5.60M
  }
2514
5.60M
  while( template_value_offset > 0 );
2515
2516
1.72M
  xml_token->size = xml_document_data_offset;
2517
2518
1.72M
  if( element_xml_tag != NULL )
2519
103k
  {
2520
103k
    if( libfwevt_internal_xml_tag_free(
2521
103k
         (libfwevt_internal_xml_tag_t **) &element_xml_tag,
2522
103k
         error ) != 1 )
2523
0
    {
2524
0
      libcerror_error_set(
2525
0
       error,
2526
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
2527
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
2528
0
       "%s: unable to free element XML tag.",
2529
0
       function );
2530
2531
0
      goto on_error;
2532
0
    }
2533
103k
  }
2534
1.72M
  if( libfwevt_xml_token_free(
2535
1.72M
       &xml_sub_token,
2536
1.72M
       error ) != 1 )
2537
0
  {
2538
0
    libcerror_error_set(
2539
0
     error,
2540
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
2541
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
2542
0
     "%s: unable to free binary XML sub token.",
2543
0
     function );
2544
2545
0
    goto on_error;
2546
0
  }
2547
1.72M
  return( 1 );
2548
2549
140k
on_error:
2550
140k
  if( ( element_xml_tag != NULL )
2551
139k
   && ( element_xml_tag != internal_xml_document->root_xml_tag ) )
2552
139k
  {
2553
139k
    libfwevt_internal_xml_tag_free(
2554
139k
     (libfwevt_internal_xml_tag_t **) &element_xml_tag,
2555
139k
     NULL );
2556
139k
  }
2557
140k
  if( xml_sub_token != NULL )
2558
139k
  {
2559
139k
    libfwevt_xml_token_free(
2560
139k
     &xml_sub_token,
2561
139k
     NULL );
2562
139k
  }
2563
140k
  return( -1 );
2564
1.72M
}
2565
2566
/* Reads an entity reference from a binary XML document
2567
 * Returns 1 if successful or -1 on error
2568
 */
2569
int libfwevt_xml_document_read_entity_reference(
2570
     libfwevt_internal_xml_document_t *internal_xml_document,
2571
     libfwevt_xml_token_t *xml_token,
2572
     const uint8_t *binary_data,
2573
     size_t binary_data_size,
2574
     size_t binary_data_offset,
2575
     uint8_t flags,
2576
     libfwevt_xml_tag_t *xml_tag,
2577
     int ascii_codepage LIBFWEVT_ATTRIBUTE_UNUSED,
2578
     libcerror_error_t **error )
2579
131k
{
2580
131k
  libfwevt_xml_tag_t *entity_xml_tag = NULL;
2581
131k
  uint8_t *entity_name               = NULL;
2582
131k
  uint8_t *entity_value_utf16_stream = NULL;
2583
131k
  const uint8_t *xml_document_data   = NULL;
2584
131k
  static char *function              = "libfwevt_xml_document_read_entity_reference";
2585
131k
  size_t additional_value_size       = 0;
2586
131k
  size_t trailing_data_size          = 0;
2587
131k
  size_t utf8_string_size            = 0;
2588
131k
  size_t xml_document_data_offset    = 0;
2589
131k
  size_t xml_document_data_size      = 0;
2590
131k
  uint32_t entity_name_offset        = 0;
2591
131k
  uint32_t entity_name_size          = 0;
2592
131k
  uint8_t entity_name_match          = 0;
2593
131k
  int data_segment_index             = 0;
2594
2595
131k
  LIBFWEVT_UNREFERENCED_PARAMETER( ascii_codepage )
2596
2597
131k
  if( internal_xml_document == NULL )
2598
0
  {
2599
0
    libcerror_error_set(
2600
0
     error,
2601
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2602
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2603
0
     "%s: invalid binary XML document.",
2604
0
     function );
2605
2606
0
    return( -1 );
2607
0
  }
2608
131k
  if( xml_token == NULL )
2609
0
  {
2610
0
    libcerror_error_set(
2611
0
     error,
2612
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2613
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2614
0
     "%s: invalid binary XML token.",
2615
0
     function );
2616
2617
0
    return( -1 );
2618
0
  }
2619
131k
  if( ( xml_token->type & 0xbf ) != LIBFWEVT_XML_TOKEN_ENTITY_REFERENCE )
2620
0
  {
2621
0
    libcerror_error_set(
2622
0
     error,
2623
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
2624
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
2625
0
     "%s: invalid binary XML token - unsupported type: 0x%02" PRIx8 ".",
2626
0
     function,
2627
0
     xml_token->type );
2628
2629
0
    return( -1 );
2630
0
  }
2631
131k
  if( binary_data == NULL )
2632
0
  {
2633
0
    libcerror_error_set(
2634
0
     error,
2635
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2636
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2637
0
     "%s: invalid binary data.",
2638
0
     function );
2639
2640
0
    return( -1 );
2641
0
  }
2642
131k
  if( binary_data_size > (size_t) SSIZE_MAX )
2643
0
  {
2644
0
    libcerror_error_set(
2645
0
     error,
2646
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2647
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
2648
0
     "%s: invalid binary XML document data size value exceeds maximum.",
2649
0
     function );
2650
2651
0
    return( -1 );
2652
0
  }
2653
131k
  if( binary_data_offset >= binary_data_size )
2654
0
  {
2655
0
    libcerror_error_set(
2656
0
     error,
2657
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2658
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
2659
0
     "%s: invalid binary data offset value out of bounds.",
2660
0
     function );
2661
2662
0
    return( -1 );
2663
0
  }
2664
131k
  if( xml_tag == NULL )
2665
0
  {
2666
0
    libcerror_error_set(
2667
0
     error,
2668
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2669
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2670
0
     "%s: invalid XML tag.",
2671
0
     function );
2672
2673
0
    return( -1 );
2674
0
  }
2675
131k
  if( ( flags & LIBFWEVT_XML_DOCUMENT_READ_FLAG_HAS_DATA_OFFSETS ) != 0 )
2676
130k
  {
2677
130k
    additional_value_size = 4;
2678
130k
  }
2679
131k
  xml_document_data      = &( binary_data[ binary_data_offset ] );
2680
131k
  xml_document_data_size = binary_data_size - binary_data_offset;
2681
2682
131k
  if( xml_document_data_size < ( 1 + additional_value_size ) )
2683
181
  {
2684
181
    libcerror_error_set(
2685
181
     error,
2686
181
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
2687
181
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
2688
181
     "%s: invalid binary XML document data size value too small.",
2689
181
     function );
2690
2691
181
    goto on_error;
2692
181
  }
2693
#if defined( HAVE_DEBUG_OUTPUT )
2694
  if( libcnotify_verbose != 0 )
2695
  {
2696
    libcnotify_printf(
2697
     "%s: data offset\t\t: 0x%08" PRIzx "\n",
2698
     function,
2699
     binary_data_offset );
2700
2701
    libcnotify_printf(
2702
     "%s: entity reference data:\n",
2703
     function );
2704
    libcnotify_print_data(
2705
     xml_document_data,
2706
     1 + additional_value_size,
2707
     LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
2708
  }
2709
#endif
2710
#if defined( HAVE_DEBUG_OUTPUT )
2711
  if( libcnotify_verbose != 0 )
2712
  {
2713
    libcnotify_printf(
2714
     "%s: type\t\t\t: 0x%02" PRIx8 "\n",
2715
     function,
2716
     xml_document_data[ 0 ] );
2717
  }
2718
#endif
2719
131k
  xml_token->size          = 1;
2720
131k
  xml_document_data_offset = 1;
2721
2722
131k
  if( ( flags & LIBFWEVT_XML_DOCUMENT_READ_FLAG_HAS_DATA_OFFSETS ) == 0 )
2723
996
  {
2724
996
    entity_name_offset = (uint32_t) ( binary_data_offset + xml_document_data_offset );
2725
996
  }
2726
130k
  else
2727
130k
  {
2728
130k
    if( ( xml_document_data_size < 4 )
2729
130k
     || ( xml_document_data_offset >= ( xml_document_data_size - 4 ) ) )
2730
13
    {
2731
13
      libcerror_error_set(
2732
13
       error,
2733
13
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
2734
13
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
2735
13
       "%s: invalid binary XML document data size value too small.",
2736
13
       function );
2737
2738
13
      goto on_error;
2739
13
    }
2740
130k
    byte_stream_copy_to_uint32_little_endian(
2741
130k
     &( xml_document_data[ xml_document_data_offset ] ),
2742
130k
     entity_name_offset );
2743
2744
#if defined( HAVE_DEBUG_OUTPUT )
2745
    if( libcnotify_verbose != 0 )
2746
    {
2747
      libcnotify_printf(
2748
       "%s: name offset\t\t: 0x%08" PRIx32 "\n",
2749
       function,
2750
       entity_name_offset );
2751
    }
2752
#endif
2753
130k
    xml_token->size          += 4;
2754
130k
    xml_document_data_offset += 4;
2755
130k
  }
2756
#if defined( HAVE_DEBUG_OUTPUT )
2757
  if( libcnotify_verbose != 0 )
2758
  {
2759
    libcnotify_printf(
2760
     "\n" );
2761
  }
2762
#endif
2763
131k
  if( entity_name_offset > ( binary_data_offset + xml_document_data_offset ) )
2764
516
  {
2765
516
    libcerror_error_set(
2766
516
     error,
2767
516
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2768
516
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
2769
516
     "%s: invalid entity name offset value out of bounds.",
2770
516
     function );
2771
2772
516
    goto on_error;
2773
516
  }
2774
130k
  if( ( binary_data_offset + xml_document_data_offset ) < entity_name_offset )
2775
0
  {
2776
0
    trailing_data_size = entity_name_offset - ( binary_data_offset + xml_document_data_offset );
2777
2778
#if defined( HAVE_DEBUG_OUTPUT )
2779
    if( libcnotify_verbose != 0 )
2780
    {
2781
      libcnotify_printf(
2782
       "%s: data offset\t\t: 0x%08" PRIzx "\n",
2783
       function,
2784
       binary_data_offset + xml_document_data_offset );
2785
2786
      libcnotify_printf(
2787
       "%s: trailing data:\n",
2788
       function );
2789
      libcnotify_print_data(
2790
       &( xml_document_data[ xml_document_data_offset ] ),
2791
       trailing_data_size,
2792
       LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
2793
    }
2794
#endif
2795
0
    xml_token->size          += trailing_data_size;
2796
0
    xml_document_data_offset += trailing_data_size;
2797
0
  }
2798
130k
  if( libfwevt_xml_tag_initialize(
2799
130k
       &entity_xml_tag,
2800
130k
       error ) != 1 )
2801
0
  {
2802
0
    libcerror_error_set(
2803
0
     error,
2804
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
2805
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
2806
0
     "%s: unable to create entity XML tag.",
2807
0
     function );
2808
2809
0
    goto on_error;
2810
0
  }
2811
130k
  if( libfwevt_xml_document_read_name(
2812
130k
       internal_xml_document,
2813
130k
       binary_data,
2814
130k
       binary_data_size,
2815
130k
       entity_name_offset,
2816
130k
       flags,
2817
130k
       &entity_name_size,
2818
130k
       entity_xml_tag,
2819
130k
       error ) != 1 )
2820
130
  {
2821
130
    libcerror_error_set(
2822
130
     error,
2823
130
     LIBCERROR_ERROR_DOMAIN_IO,
2824
130
     LIBCERROR_IO_ERROR_READ_FAILED,
2825
130
     "%s: unable to read entity name.",
2826
130
     function );
2827
2828
130
    goto on_error;
2829
130
  }
2830
130k
  if( ( binary_data_offset + xml_document_data_offset ) == entity_name_offset )
2831
5.18k
  {
2832
5.18k
    xml_token->size += entity_name_size;
2833
5.18k
  }
2834
130k
  if( libfwevt_xml_tag_get_utf8_name_size(
2835
130k
       entity_xml_tag,
2836
130k
       &utf8_string_size,
2837
130k
       error ) != 1 )
2838
430
  {
2839
430
    libcerror_error_set(
2840
430
     error,
2841
430
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
2842
430
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2843
430
     "%s: unable to retrieve UTF-8 string size of entity name.",
2844
430
     function );
2845
2846
430
    goto on_error;
2847
430
  }
2848
130k
  if( ( utf8_string_size == 0 )
2849
130k
   || ( utf8_string_size > ( MEMORY_MAXIMUM_ALLOCATION_SIZE / sizeof( uint8_t ) ) ) )
2850
0
  {
2851
0
    libcerror_error_set(
2852
0
     error,
2853
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
2854
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
2855
0
     "%s: invalid UTF-8 string size value out of bounds.",
2856
0
     function );
2857
2858
0
    goto on_error;
2859
0
  }
2860
130k
  entity_name = (uint8_t *) memory_allocate(
2861
130k
                             sizeof( uint8_t ) * utf8_string_size );
2862
2863
130k
  if( entity_name == NULL )
2864
0
  {
2865
0
    libcerror_error_set(
2866
0
     error,
2867
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
2868
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
2869
0
     "%s: unable to create entity name.",
2870
0
     function );
2871
2872
0
    goto on_error;
2873
0
  }
2874
130k
  if( libfwevt_xml_tag_get_utf8_name(
2875
130k
       entity_xml_tag,
2876
130k
       entity_name,
2877
130k
       utf8_string_size,
2878
130k
       error ) != 1 )
2879
0
  {
2880
0
    libcerror_error_set(
2881
0
     error,
2882
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
2883
0
     LIBCERROR_RUNTIME_ERROR_COPY_FAILED,
2884
0
     "%s: unable to copy entity name to UTF-16 string.",
2885
0
     function );
2886
2887
0
    goto on_error;
2888
0
  }
2889
130k
  if( libfwevt_xml_tag_set_value_type(
2890
130k
       xml_tag,
2891
130k
       LIBFWEVT_VALUE_TYPE_STRING_UTF16,
2892
130k
       error ) != 1 )
2893
103
  {
2894
103
    libcerror_error_set(
2895
103
     error,
2896
103
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
2897
103
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2898
103
     "%s: unable to set value type.",
2899
103
     function );
2900
2901
103
    goto on_error;
2902
103
  }
2903
  /* Make sure the character value data is in UTF-16 litte-endian
2904
   */
2905
130k
  entity_value_utf16_stream = (uint8_t *) memory_allocate(
2906
130k
                                           sizeof( uint8_t ) * 4 );
2907
2908
130k
  if( entity_value_utf16_stream == NULL )
2909
0
  {
2910
0
    libcerror_error_set(
2911
0
     error,
2912
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
2913
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
2914
0
     "%s: unable to create entity value UTF-16 stream.",
2915
0
     function );
2916
2917
0
    goto on_error;
2918
0
  }
2919
130k
  if( utf8_string_size == 3 )
2920
126k
  {
2921
126k
    if( ( entity_name[ 0 ] == (uint8_t) 'g' )
2922
1.23k
     && ( entity_name[ 1 ] == (uint8_t) 't' ) )
2923
1.14k
    {
2924
1.14k
      entity_value_utf16_stream[ 0 ] = (uint8_t) '>';
2925
1.14k
      entity_value_utf16_stream[ 1 ] = 0;
2926
2927
1.14k
      entity_name_match = 1;
2928
1.14k
    }
2929
125k
    else if( ( entity_name[ 0 ] == (uint8_t) 'l' )
2930
125k
          && ( entity_name[ 1 ] == (uint8_t) 't' ) )
2931
124k
    {
2932
124k
      entity_value_utf16_stream[ 0 ] = (uint8_t) '<';
2933
124k
      entity_value_utf16_stream[ 1 ] = 0;
2934
2935
124k
      entity_name_match = 1;
2936
124k
    }
2937
126k
  }
2938
3.76k
  else if( utf8_string_size == 4 )
2939
711
  {
2940
711
    if( ( entity_name[ 0 ] == (uint8_t) 'a' )
2941
560
     && ( entity_name[ 1 ] == (uint8_t) 'm' )
2942
442
     && ( entity_name[ 2 ] == (uint8_t) 'p' ) )
2943
372
    {
2944
372
      entity_value_utf16_stream[ 0 ] = (uint8_t) '&';
2945
372
      entity_value_utf16_stream[ 1 ] = 0;
2946
2947
372
      entity_name_match = 1;
2948
372
    }
2949
711
  }
2950
3.05k
  else if( utf8_string_size == 5 )
2951
2.21k
  {
2952
2.21k
    if( ( entity_name[ 0 ] == (uint8_t) 'a' )
2953
765
     && ( entity_name[ 1 ] == (uint8_t) 'p' )
2954
662
     && ( entity_name[ 2 ] == (uint8_t) 'o' )
2955
559
     && ( entity_name[ 3 ] == (uint8_t) 's' ) )
2956
495
    {
2957
495
      entity_value_utf16_stream[ 0 ] = (uint8_t) '\'';
2958
495
      entity_value_utf16_stream[ 1 ] = 0;
2959
2960
495
      entity_name_match = 1;
2961
495
    }
2962
1.71k
    else if( ( entity_name[ 0 ] == (uint8_t) 'q' )
2963
1.01k
          && ( entity_name[ 1 ] == (uint8_t) 'u' )
2964
936
          && ( entity_name[ 2 ] == (uint8_t) 'o' )
2965
837
          && ( entity_name[ 3 ] == (uint8_t) 't' ) )
2966
762
    {
2967
762
      entity_value_utf16_stream[ 0 ] = (uint8_t) '"';
2968
762
      entity_value_utf16_stream[ 1 ] = 0;
2969
2970
762
      entity_name_match = 1;
2971
762
    }
2972
2.21k
  }
2973
130k
  if( entity_name_match == 0 )
2974
2.49k
  {
2975
2.49k
    libcerror_error_set(
2976
2.49k
     error,
2977
2.49k
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
2978
2.49k
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
2979
2.49k
     "%s: unsupported entity name: %s\n",
2980
2.49k
     function,
2981
2.49k
     entity_name );
2982
2983
2.49k
    goto on_error;
2984
2.49k
  }
2985
127k
  entity_value_utf16_stream[ 2 ] = 0;
2986
127k
  entity_value_utf16_stream[ 3 ] = 0;
2987
2988
127k
  memory_free(
2989
127k
   entity_name );
2990
2991
127k
  entity_name = NULL;
2992
2993
127k
  if( libfwevt_xml_tag_append_value_data(
2994
127k
       xml_tag,
2995
127k
       entity_value_utf16_stream,
2996
127k
       4,
2997
127k
       &data_segment_index,
2998
127k
       error ) != 1 )
2999
0
  {
3000
0
    libcerror_error_set(
3001
0
     error,
3002
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
3003
0
     LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
3004
0
     "%s: unable to append value data.",
3005
0
     function );
3006
3007
0
    goto on_error;
3008
0
  }
3009
127k
  memory_free(
3010
127k
   entity_value_utf16_stream );
3011
3012
127k
  entity_value_utf16_stream = NULL;
3013
3014
#if defined( HAVE_DEBUG_OUTPUT )
3015
  if( libcnotify_verbose != 0 )
3016
  {
3017
    if( libfwevt_xml_tag_debug_print_value_data_segment(
3018
         xml_tag,
3019
         data_segment_index,
3020
         0,
3021
         ascii_codepage,
3022
         error ) != 1 )
3023
    {
3024
      libcerror_error_set(
3025
       error,
3026
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
3027
       LIBCERROR_RUNTIME_ERROR_PRINT_FAILED,
3028
       "%s: unable to print value data segment: %d.",
3029
       function,
3030
       data_segment_index );
3031
3032
      goto on_error;
3033
    }
3034
  }
3035
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
3036
3037
127k
  if( libfwevt_internal_xml_tag_free(
3038
127k
       (libfwevt_internal_xml_tag_t **) &entity_xml_tag,
3039
127k
       error ) != 1 )
3040
0
  {
3041
0
    libcerror_error_set(
3042
0
     error,
3043
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
3044
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
3045
0
     "%s: unable to free entity XML tag.",
3046
0
     function );
3047
3048
0
    goto on_error;
3049
0
  }
3050
127k
  return( 1 );
3051
3052
3.86k
on_error:
3053
3.86k
  if( entity_value_utf16_stream != NULL )
3054
2.49k
  {
3055
2.49k
    memory_free(
3056
2.49k
     entity_value_utf16_stream );
3057
2.49k
  }
3058
3.86k
  if( entity_name != NULL )
3059
2.59k
  {
3060
2.59k
    memory_free(
3061
2.59k
     entity_name );
3062
2.59k
  }
3063
3.86k
  if( entity_xml_tag != NULL )
3064
3.15k
  {
3065
3.15k
    libfwevt_internal_xml_tag_free(
3066
3.15k
     (libfwevt_internal_xml_tag_t **) &entity_xml_tag,
3067
3.15k
     NULL );
3068
3.15k
  }
3069
3.86k
  return( -1 );
3070
127k
}
3071
3072
/* Reads a fragment from a binary XML document
3073
 * Returns 1 if successful or -1 on error
3074
 */
3075
int libfwevt_xml_document_read_fragment(
3076
     libfwevt_internal_xml_document_t *internal_xml_document,
3077
     libfwevt_xml_token_t *xml_token,
3078
     const uint8_t *binary_data,
3079
     size_t binary_data_size,
3080
     size_t binary_data_offset,
3081
     int ascii_codepage,
3082
     uint8_t flags,
3083
     libcdata_array_t *template_values_array,
3084
     libfwevt_xml_tag_t *xml_tag,
3085
     int element_recursion_depth,
3086
     int template_instance_recursion_depth,
3087
     libcerror_error_t **error )
3088
216k
{
3089
216k
  libfwevt_xml_token_t *xml_sub_token = NULL;
3090
216k
  static char *function               = "libfwevt_xml_document_read_fragment";
3091
3092
216k
  if( internal_xml_document == NULL )
3093
0
  {
3094
0
    libcerror_error_set(
3095
0
     error,
3096
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3097
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3098
0
     "%s: invalid binary XML document.",
3099
0
     function );
3100
3101
0
    return( -1 );
3102
0
  }
3103
216k
  if( xml_token == NULL )
3104
0
  {
3105
0
    libcerror_error_set(
3106
0
     error,
3107
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3108
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3109
0
     "%s: invalid binary XML token.",
3110
0
     function );
3111
3112
0
    return( -1 );
3113
0
  }
3114
216k
  if( libfwevt_xml_document_read_fragment_header(
3115
216k
       internal_xml_document,
3116
216k
       xml_token,
3117
216k
       binary_data,
3118
216k
       binary_data_size,
3119
216k
       binary_data_offset,
3120
216k
       error ) != 1 )
3121
199
  {
3122
199
    libcerror_error_set(
3123
199
     error,
3124
199
     LIBCERROR_ERROR_DOMAIN_IO,
3125
199
     LIBCERROR_IO_ERROR_READ_FAILED,
3126
199
     "%s: unable to read fragment header.",
3127
199
     function );
3128
3129
199
    goto on_error;
3130
199
  }
3131
216k
  binary_data_offset += xml_token->size;
3132
3133
216k
  if( libfwevt_xml_token_initialize(
3134
216k
       &xml_sub_token,
3135
216k
       error ) != 1 )
3136
0
  {
3137
0
    libcerror_error_set(
3138
0
     error,
3139
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
3140
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
3141
0
     "%s: unable to create binary XML sub token.",
3142
0
     function );
3143
3144
0
    goto on_error;
3145
0
  }
3146
216k
  if( libfwevt_xml_token_read_data(
3147
216k
       xml_sub_token,
3148
216k
       binary_data,
3149
216k
       binary_data_size,
3150
216k
       binary_data_offset,
3151
216k
       error ) != 1 )
3152
157
  {
3153
157
    libcerror_error_set(
3154
157
     error,
3155
157
     LIBCERROR_ERROR_DOMAIN_IO,
3156
157
     LIBCERROR_IO_ERROR_READ_FAILED,
3157
157
     "%s: unable to read binary XML sub token.",
3158
157
     function );
3159
3160
157
    goto on_error;
3161
157
  }
3162
215k
  switch( xml_sub_token->type & 0xbf )
3163
215k
  {
3164
60.8k
    case LIBFWEVT_XML_TOKEN_OPEN_START_ELEMENT_TAG:
3165
60.8k
      if( libfwevt_xml_document_read_element(
3166
60.8k
           internal_xml_document,
3167
60.8k
           xml_sub_token,
3168
60.8k
           binary_data,
3169
60.8k
           binary_data_size,
3170
60.8k
           binary_data_offset,
3171
60.8k
           ascii_codepage,
3172
60.8k
           flags,
3173
60.8k
           template_values_array,
3174
60.8k
           xml_tag,
3175
60.8k
           element_recursion_depth + 1,
3176
60.8k
           template_instance_recursion_depth,
3177
60.8k
           error ) != 1 )
3178
18.2k
      {
3179
18.2k
        libcerror_error_set(
3180
18.2k
         error,
3181
18.2k
         LIBCERROR_ERROR_DOMAIN_IO,
3182
18.2k
         LIBCERROR_IO_ERROR_READ_FAILED,
3183
18.2k
         "%s: unable to read element.",
3184
18.2k
         function );
3185
3186
18.2k
        goto on_error;
3187
18.2k
      }
3188
42.6k
      break;
3189
3190
154k
    case LIBFWEVT_XML_TOKEN_TEMPLATE_INSTANCE:
3191
154k
      if( libfwevt_xml_document_read_template_instance(
3192
154k
           internal_xml_document,
3193
154k
           xml_sub_token,
3194
154k
           binary_data,
3195
154k
           binary_data_size,
3196
154k
           binary_data_offset,
3197
154k
           ascii_codepage,
3198
154k
           flags,
3199
154k
           xml_tag,
3200
154k
           element_recursion_depth,
3201
154k
           template_instance_recursion_depth + 1,
3202
154k
           error ) != 1 )
3203
47.8k
      {
3204
47.8k
        libcerror_error_set(
3205
47.8k
         error,
3206
47.8k
         LIBCERROR_ERROR_DOMAIN_IO,
3207
47.8k
         LIBCERROR_IO_ERROR_READ_FAILED,
3208
47.8k
         "%s: unable to read document template instance.",
3209
47.8k
         function );
3210
3211
47.8k
        goto on_error;
3212
47.8k
      }
3213
106k
      break;
3214
3215
106k
    default:
3216
166
      libcerror_error_set(
3217
166
       error,
3218
166
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
3219
166
       LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
3220
166
       "%s: invalid binary XML token - unsupported type: 0x%02" PRIx8 ".",
3221
166
       function,
3222
166
       xml_sub_token->type );
3223
3224
166
      goto on_error;
3225
215k
  }
3226
149k
  xml_token->size += xml_sub_token->size;
3227
3228
149k
  if( libfwevt_xml_token_free(
3229
149k
       &xml_sub_token,
3230
149k
       error ) != 1 )
3231
0
  {
3232
0
    libcerror_error_set(
3233
0
     error,
3234
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
3235
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
3236
0
     "%s: unable to free binary XML sub token.",
3237
0
     function );
3238
3239
0
    goto on_error;
3240
0
  }
3241
149k
  return( 1 );
3242
3243
66.6k
on_error:
3244
66.6k
  if( xml_sub_token != NULL )
3245
66.4k
  {
3246
66.4k
    libfwevt_xml_token_free(
3247
66.4k
     &xml_sub_token,
3248
66.4k
     NULL );
3249
66.4k
  }
3250
66.6k
  return( -1 );
3251
149k
}
3252
3253
/* Reads a fragment header from a binary XML document
3254
 * Returns 1 if successful or -1 on error
3255
 */
3256
int libfwevt_xml_document_read_fragment_header(
3257
     libfwevt_internal_xml_document_t *internal_xml_document,
3258
     libfwevt_xml_token_t *xml_token,
3259
     const uint8_t *binary_data,
3260
     size_t binary_data_size,
3261
     size_t binary_data_offset,
3262
     libcerror_error_t **error )
3263
565k
{
3264
565k
  static char *function            = "libfwevt_xml_document_read_fragment_header";
3265
565k
  size_t xml_document_data_size    = 0;
3266
3267
#if defined( HAVE_DEBUG_OUTPUT )
3268
  const uint8_t *xml_document_data = NULL;
3269
#endif
3270
3271
565k
  if( internal_xml_document == NULL )
3272
0
  {
3273
0
    libcerror_error_set(
3274
0
     error,
3275
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3276
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3277
0
     "%s: invalid binary XML document.",
3278
0
     function );
3279
3280
0
    return( -1 );
3281
0
  }
3282
565k
  if( xml_token == NULL )
3283
0
  {
3284
0
    libcerror_error_set(
3285
0
     error,
3286
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3287
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3288
0
     "%s: invalid binary XML token.",
3289
0
     function );
3290
3291
0
    return( -1 );
3292
0
  }
3293
565k
  if( xml_token->type != LIBFWEVT_XML_TOKEN_FRAGMENT_HEADER )
3294
607
  {
3295
607
    libcerror_error_set(
3296
607
     error,
3297
607
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
3298
607
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
3299
607
     "%s: invalid binary XML token - unsupported type: 0x%02" PRIx8 ".",
3300
607
     function,
3301
607
     xml_token->type );
3302
3303
607
    return( -1 );
3304
607
  }
3305
565k
  if( binary_data == NULL )
3306
0
  {
3307
0
    libcerror_error_set(
3308
0
     error,
3309
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3310
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3311
0
     "%s: invalid binary data.",
3312
0
     function );
3313
3314
0
    return( -1 );
3315
0
  }
3316
565k
  if( binary_data_size > (size_t) SSIZE_MAX )
3317
0
  {
3318
0
    libcerror_error_set(
3319
0
     error,
3320
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3321
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
3322
0
     "%s: invalid binary XML document data size value exceeds maximum.",
3323
0
     function );
3324
3325
0
    return( -1 );
3326
0
  }
3327
565k
  if( binary_data_offset >= binary_data_size )
3328
0
  {
3329
0
    libcerror_error_set(
3330
0
     error,
3331
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3332
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
3333
0
     "%s: invalid binary data offset value out of bounds.",
3334
0
     function );
3335
3336
0
    return( -1 );
3337
0
  }
3338
#if defined( HAVE_DEBUG_OUTPUT )
3339
  xml_document_data = &( binary_data[ binary_data_offset ] );
3340
#endif
3341
3342
565k
  xml_document_data_size = binary_data_size - binary_data_offset;
3343
3344
565k
  if( xml_document_data_size < 4 )
3345
208
  {
3346
208
    libcerror_error_set(
3347
208
     error,
3348
208
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
3349
208
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
3350
208
     "%s: invalid binary XML document data size value too small.",
3351
208
     function );
3352
3353
208
    return( -1 );
3354
208
  }
3355
#if defined( HAVE_DEBUG_OUTPUT )
3356
  if( libcnotify_verbose != 0 )
3357
  {
3358
    libcnotify_printf(
3359
     "%s: data offset\t\t\t: 0x%08" PRIzx "\n",
3360
     function,
3361
     binary_data_offset );
3362
3363
    libcnotify_printf(
3364
     "%s: fragment header data:\n",
3365
     function );
3366
    libcnotify_print_data(
3367
     xml_document_data,
3368
     4,
3369
     0 );
3370
  }
3371
#endif
3372
#if defined( HAVE_DEBUG_OUTPUT )
3373
  if( libcnotify_verbose != 0 )
3374
  {
3375
    libcnotify_printf(
3376
     "%s: type\t\t\t: 0x%02" PRIx8 "\n",
3377
     function,
3378
     xml_document_data[ 0 ] );
3379
3380
    libcnotify_printf(
3381
     "%s: major version\t\t: %" PRIu8 "\n",
3382
     function,
3383
     xml_document_data[ 1 ] );
3384
3385
    libcnotify_printf(
3386
     "%s: minor version\t\t: %" PRIu8 "\n",
3387
     function,
3388
     xml_document_data[ 2 ] );
3389
3390
    libcnotify_printf(
3391
     "%s: flags\t\t\t: 0x%02" PRIx8 "\n",
3392
     function,
3393
     xml_document_data[ 3 ] );
3394
3395
    libcnotify_printf(
3396
     "\n" );
3397
  }
3398
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
3399
3400
/* TODO check values */
3401
565k
  xml_token->size = 4;
3402
3403
565k
  return( 1 );
3404
565k
}
3405
3406
/* Reads a name from a binary XML document
3407
 * Returns 1 if successful or -1 on error
3408
 */
3409
int libfwevt_xml_document_read_name(
3410
     libfwevt_internal_xml_document_t *internal_xml_document,
3411
     const uint8_t *binary_data,
3412
     size_t binary_data_size,
3413
     size_t binary_data_offset,
3414
     uint8_t flags,
3415
     uint32_t *name_data_size,
3416
     libfwevt_xml_tag_t *xml_tag,
3417
     libcerror_error_t **error )
3418
7.15M
{
3419
7.15M
  const uint8_t *xml_document_data = NULL;
3420
7.15M
  static char *function            = "libfwevt_xml_document_read_name";
3421
7.15M
  size_t additional_value_size     = 0;
3422
7.15M
  size_t xml_document_data_offset  = 0;
3423
7.15M
  size_t xml_document_data_size    = 0;
3424
7.15M
  uint32_t name_size               = 0;
3425
3426
#if defined( HAVE_DEBUG_OUTPUT )
3427
  uint32_t value_32bit             = 0;
3428
  uint16_t value_16bit             = 0;
3429
#endif
3430
3431
7.15M
  if( internal_xml_document == NULL )
3432
0
  {
3433
0
    libcerror_error_set(
3434
0
     error,
3435
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3436
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3437
0
     "%s: invalid binary XML document.",
3438
0
     function );
3439
3440
0
    return( -1 );
3441
0
  }
3442
7.15M
  if( binary_data == NULL )
3443
0
  {
3444
0
    libcerror_error_set(
3445
0
     error,
3446
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3447
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3448
0
     "%s: invalid binary data.",
3449
0
     function );
3450
3451
0
    return( -1 );
3452
0
  }
3453
7.15M
  if( ( binary_data_size < 4 )
3454
7.15M
   || ( binary_data_size > (size_t) SSIZE_MAX ) )
3455
0
  {
3456
0
    libcerror_error_set(
3457
0
     error,
3458
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3459
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
3460
0
     "%s: invalid binary XML document data size value out of bounds.",
3461
0
     function );
3462
3463
0
    return( -1 );
3464
0
  }
3465
7.15M
  if( binary_data_offset >= binary_data_size )
3466
60
  {
3467
60
    libcerror_error_set(
3468
60
     error,
3469
60
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3470
60
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
3471
60
     "%s: invalid binary data offset value out of bounds.",
3472
60
     function );
3473
3474
60
    return( -1 );
3475
60
  }
3476
7.15M
  if( name_data_size == NULL )
3477
0
  {
3478
0
    libcerror_error_set(
3479
0
     error,
3480
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3481
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3482
0
     "%s: invalid name data size.",
3483
0
     function );
3484
3485
0
    return( -1 );
3486
0
  }
3487
7.15M
  if( xml_tag == NULL )
3488
0
  {
3489
0
    libcerror_error_set(
3490
0
     error,
3491
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3492
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3493
0
     "%s: invalid XML tag.",
3494
0
     function );
3495
3496
0
    return( -1 );
3497
0
  }
3498
7.15M
  xml_document_data      = &( binary_data[ binary_data_offset ] );
3499
7.15M
  xml_document_data_size = binary_data_size - binary_data_offset;
3500
3501
7.15M
  if( ( flags & LIBFWEVT_XML_DOCUMENT_READ_FLAG_HAS_DATA_OFFSETS ) != 0 )
3502
6.63M
  {
3503
6.63M
    additional_value_size += 4;
3504
6.63M
  }
3505
7.15M
  if( ( additional_value_size + 4 ) > xml_document_data_size )
3506
268
  {
3507
268
    libcerror_error_set(
3508
268
     error,
3509
268
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
3510
268
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
3511
268
     "%s: invalid binary XML document data size value too small.",
3512
268
     function );
3513
3514
268
    return( -1 );
3515
268
  }
3516
#if defined( HAVE_DEBUG_OUTPUT )
3517
  if( libcnotify_verbose != 0 )
3518
  {
3519
    libcnotify_printf(
3520
     "%s: data offset\t\t\t\t: 0x%08" PRIzx "\n",
3521
     function,
3522
     binary_data_offset );
3523
3524
    libcnotify_printf(
3525
     "%s: name header data:\n",
3526
     function );
3527
    libcnotify_print_data(
3528
     xml_document_data,
3529
     additional_value_size + 4,
3530
     LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
3531
  }
3532
#endif
3533
7.15M
  if( ( flags & LIBFWEVT_XML_DOCUMENT_READ_FLAG_HAS_DATA_OFFSETS ) != 0 )
3534
6.63M
  {
3535
/* TODO determine if this used */
3536
#if defined( HAVE_DEBUG_OUTPUT )
3537
    if( libcnotify_verbose != 0 )
3538
    {
3539
      byte_stream_copy_to_uint32_little_endian(
3540
       xml_document_data,
3541
       value_32bit );
3542
      libcnotify_printf(
3543
       "%s: name unknown1\t\t\t\t: 0x%08" PRIx32 "\n",
3544
       function,
3545
       value_32bit );
3546
    }
3547
#endif
3548
6.63M
    xml_document_data_offset += 4;
3549
6.63M
  }
3550
7.15M
  byte_stream_copy_to_uint16_little_endian(
3551
7.15M
   &( xml_document_data[ xml_document_data_offset + 2 ] ),
3552
7.15M
   name_size );
3553
3554
#if defined( HAVE_DEBUG_OUTPUT )
3555
  if( libcnotify_verbose != 0 )
3556
  {
3557
    byte_stream_copy_to_uint16_little_endian(
3558
     &( xml_document_data[ xml_document_data_offset ] ),
3559
     value_16bit );
3560
    libcnotify_printf(
3561
     "%s: name hash\t\t\t\t: 0x%04" PRIx16 "\n",
3562
     function,
3563
     value_16bit );
3564
3565
    libcnotify_printf(
3566
     "%s: name number of characters\t\t: %" PRIu16 "\n",
3567
     function,
3568
     name_size );
3569
  }
3570
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
3571
3572
7.15M
  xml_document_data_offset += 4;
3573
3574
7.15M
  if( ( name_size == 0 )
3575
7.13M
   || ( (size_t) name_size > ( ( MEMORY_MAXIMUM_ALLOCATION_SIZE - 1 ) / 2 ) ) )
3576
12.9k
  {
3577
12.9k
    libcerror_error_set(
3578
12.9k
     error,
3579
12.9k
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
3580
12.9k
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
3581
12.9k
     "%s: invalid name size value out of bounds.",
3582
12.9k
     function );
3583
3584
12.9k
    return( -1 );
3585
12.9k
  }
3586
7.13M
  name_size = ( name_size + 1 ) * 2;
3587
3588
7.13M
  if( (size_t) name_size > ( xml_document_data_size - xml_document_data_offset ) )
3589
697
  {
3590
697
    libcerror_error_set(
3591
697
     error,
3592
697
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
3593
697
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
3594
697
     "%s: invalid binary XML document data size value too small.",
3595
697
     function );
3596
3597
697
    return( -1 );
3598
697
  }
3599
#if defined( HAVE_DEBUG_OUTPUT )
3600
  if( libcnotify_verbose != 0 )
3601
  {
3602
    libcnotify_printf(
3603
     "%s: name data:\n",
3604
     function );
3605
    libcnotify_print_data(
3606
     &( xml_document_data[ xml_document_data_offset ] ),
3607
     name_size,
3608
     LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
3609
  }
3610
#endif
3611
7.13M
  if( libfwevt_xml_tag_set_name_data(
3612
7.13M
       xml_tag,
3613
7.13M
       &( xml_document_data[ xml_document_data_offset ] ),
3614
7.13M
       name_size,
3615
7.13M
       error ) != 1 )
3616
0
  {
3617
0
    libcerror_error_set(
3618
0
     error,
3619
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
3620
0
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
3621
0
     "%s: unable to set name data.",
3622
0
     function );
3623
3624
0
    return( -1 );
3625
0
  }
3626
#if defined( HAVE_DEBUG_OUTPUT )
3627
  if( libcnotify_verbose != 0 )
3628
  {
3629
    if( libfwevt_xml_tag_name_debug_print(
3630
         xml_tag,
3631
         error ) != 1 )
3632
    {
3633
      libcerror_error_set(
3634
       error,
3635
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
3636
       LIBCERROR_RUNTIME_ERROR_PRINT_FAILED,
3637
       "%s: unable to print name.",
3638
       function );
3639
3640
      return( -1 );
3641
    }
3642
  }
3643
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
3644
3645
7.13M
  *name_data_size = (uint32_t) ( xml_document_data_offset + name_size );
3646
3647
7.13M
  return( 1 );
3648
7.13M
}
3649
3650
/* Reads a normal substitution from a binary XML document
3651
 * Returns 1 if successful or -1 on error
3652
 */
3653
int libfwevt_xml_document_read_normal_substitution(
3654
     libfwevt_internal_xml_document_t *internal_xml_document,
3655
     libfwevt_xml_token_t *xml_token,
3656
     const uint8_t *binary_data,
3657
     size_t binary_data_size,
3658
     size_t binary_data_offset,
3659
     int ascii_codepage,
3660
     uint8_t flags,
3661
     libcdata_array_t *template_values_array,
3662
     size_t *template_value_offset,
3663
     libfwevt_xml_tag_t *xml_tag,
3664
     int element_recursion_depth,
3665
     int template_instance_recursion_depth,
3666
     libcerror_error_t **error )
3667
2.06M
{
3668
2.06M
  const uint8_t *xml_document_data = NULL;
3669
2.06M
  static char *function            = "libfwevt_xml_document_read_normal_substitution";
3670
2.06M
  size_t xml_document_data_size    = 0;
3671
2.06M
  uint16_t template_value_index    = 0;
3672
2.06M
  uint8_t template_value_type      = 0;
3673
2.06M
  int result                       = 0;
3674
3675
2.06M
  if( internal_xml_document == NULL )
3676
0
  {
3677
0
    libcerror_error_set(
3678
0
     error,
3679
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3680
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3681
0
     "%s: invalid binary XML document.",
3682
0
     function );
3683
3684
0
    return( -1 );
3685
0
  }
3686
2.06M
  if( xml_token == NULL )
3687
0
  {
3688
0
    libcerror_error_set(
3689
0
     error,
3690
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3691
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3692
0
     "%s: invalid binary XML token.",
3693
0
     function );
3694
3695
0
    return( -1 );
3696
0
  }
3697
2.06M
  if( xml_token->type != LIBFWEVT_XML_TOKEN_NORMAL_SUBSTITUTION )
3698
0
  {
3699
0
    libcerror_error_set(
3700
0
     error,
3701
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
3702
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
3703
0
     "%s: invalid binary XML token - unsupported type: 0x%02" PRIx8 ".",
3704
0
     function,
3705
0
     xml_token->type );
3706
3707
0
    return( -1 );
3708
0
  }
3709
2.06M
  if( binary_data == NULL )
3710
0
  {
3711
0
    libcerror_error_set(
3712
0
     error,
3713
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3714
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3715
0
     "%s: invalid binary data.",
3716
0
     function );
3717
3718
0
    return( -1 );
3719
0
  }
3720
2.06M
  if( binary_data_size > (size_t) SSIZE_MAX )
3721
0
  {
3722
0
    libcerror_error_set(
3723
0
     error,
3724
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3725
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
3726
0
     "%s: invalid binary XML document data size value exceeds maximum.",
3727
0
     function );
3728
3729
0
    return( -1 );
3730
0
  }
3731
2.06M
  if( binary_data_offset >= binary_data_size )
3732
0
  {
3733
0
    libcerror_error_set(
3734
0
     error,
3735
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3736
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
3737
0
     "%s: invalid binary data offset value out of bounds.",
3738
0
     function );
3739
3740
0
    return( -1 );
3741
0
  }
3742
2.06M
  xml_document_data      = &( binary_data[ binary_data_offset ] );
3743
2.06M
  xml_document_data_size = binary_data_size - binary_data_offset;
3744
3745
2.06M
  if( xml_document_data_size < 4 )
3746
86
  {
3747
86
    libcerror_error_set(
3748
86
     error,
3749
86
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
3750
86
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
3751
86
     "%s: invalid binary XML document data size value too small.",
3752
86
     function );
3753
3754
86
    return( -1 );
3755
86
  }
3756
#if defined( HAVE_DEBUG_OUTPUT )
3757
  if( libcnotify_verbose != 0 )
3758
  {
3759
    libcnotify_printf(
3760
     "%s: data offset\t\t: 0x%08" PRIzx "\n",
3761
     function,
3762
     binary_data_offset );
3763
3764
    libcnotify_printf(
3765
     "%s: normal substitution data:\n",
3766
     function );
3767
    libcnotify_print_data(
3768
     xml_document_data,
3769
     4,
3770
     0 );
3771
  }
3772
#endif
3773
2.06M
  byte_stream_copy_to_uint16_little_endian(
3774
2.06M
   &( xml_document_data[ 1 ] ),
3775
2.06M
   template_value_index );
3776
3777
2.06M
  template_value_type = xml_document_data[ 3 ];
3778
3779
#if defined( HAVE_DEBUG_OUTPUT )
3780
  if( libcnotify_verbose != 0 )
3781
  {
3782
    libcnotify_printf(
3783
     "%s: type\t\t\t: 0x%02" PRIx8 "\n",
3784
     function,
3785
     xml_document_data[ 0 ] );
3786
3787
    libcnotify_printf(
3788
     "%s: identifier\t\t: %" PRIu16 "\n",
3789
     function,
3790
     template_value_index );
3791
3792
    libcnotify_printf(
3793
     "%s: value type\t\t: 0x%02" PRIx8 " (",
3794
     function,
3795
     template_value_type );
3796
    libfwevt_debug_print_value_type(
3797
     template_value_type );
3798
    libcnotify_printf(
3799
     ")\n" );
3800
3801
    libcnotify_printf(
3802
     "\n" );
3803
  }
3804
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
3805
3806
2.06M
  xml_token->size = 4;
3807
3808
2.06M
  result = libfwevt_xml_document_substitute_template_value(
3809
2.06M
            internal_xml_document,
3810
2.06M
            binary_data,
3811
2.06M
            binary_data_size,
3812
2.06M
            ascii_codepage,
3813
2.06M
            flags,
3814
2.06M
            template_values_array,
3815
2.06M
            template_value_index,
3816
2.06M
            template_value_type,
3817
2.06M
            template_value_offset,
3818
2.06M
            xml_tag,
3819
2.06M
            element_recursion_depth,
3820
2.06M
            template_instance_recursion_depth,
3821
2.06M
            error );
3822
3823
2.06M
  if( result != 1 )
3824
24.1k
  {
3825
24.1k
    libcerror_error_set(
3826
24.1k
     error,
3827
24.1k
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
3828
24.1k
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
3829
24.1k
     "%s: unable to substitute template value.",
3830
24.1k
     function );
3831
3832
24.1k
    return( -1 );
3833
24.1k
  }
3834
2.04M
  return( 1 );
3835
2.06M
}
3836
3837
/* Reads an optional substitution from a binary XML document
3838
 * Returns 1 if successful, 0 if no substitution or -1 on error
3839
 */
3840
int libfwevt_xml_document_read_optional_substitution(
3841
     libfwevt_internal_xml_document_t *internal_xml_document,
3842
     libfwevt_xml_token_t *xml_token,
3843
     const uint8_t *binary_data,
3844
     size_t binary_data_size,
3845
     size_t binary_data_offset,
3846
     int ascii_codepage,
3847
     uint8_t flags,
3848
     libcdata_array_t *template_values_array,
3849
     size_t *template_value_offset,
3850
     libfwevt_xml_tag_t *xml_tag,
3851
     int element_recursion_depth,
3852
     int template_instance_recursion_depth,
3853
     libcerror_error_t **error )
3854
6.19M
{
3855
6.19M
  const uint8_t *xml_document_data = NULL;
3856
6.19M
  static char *function            = "libfwevt_xml_document_read_optional_substitution";
3857
6.19M
  size_t xml_document_data_size    = 0;
3858
6.19M
  uint16_t template_value_index    = 0;
3859
6.19M
  uint8_t template_value_type      = 0;
3860
6.19M
  int result                       = 0;
3861
3862
6.19M
  if( internal_xml_document == NULL )
3863
0
  {
3864
0
    libcerror_error_set(
3865
0
     error,
3866
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3867
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3868
0
     "%s: invalid binary XML document.",
3869
0
     function );
3870
3871
0
    return( -1 );
3872
0
  }
3873
6.19M
  if( xml_token == NULL )
3874
0
  {
3875
0
    libcerror_error_set(
3876
0
     error,
3877
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3878
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3879
0
     "%s: invalid binary XML token.",
3880
0
     function );
3881
3882
0
    return( -1 );
3883
0
  }
3884
6.19M
  if( xml_token->type != LIBFWEVT_XML_TOKEN_OPTIONAL_SUBSTITUTION )
3885
0
  {
3886
0
    libcerror_error_set(
3887
0
     error,
3888
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
3889
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
3890
0
     "%s: invalid binary XML token - unsupported type: 0x%02" PRIx8 ".",
3891
0
     function,
3892
0
     xml_token->type );
3893
3894
0
    return( -1 );
3895
0
  }
3896
6.19M
  if( binary_data == NULL )
3897
0
  {
3898
0
    libcerror_error_set(
3899
0
     error,
3900
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3901
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3902
0
     "%s: invalid binary data.",
3903
0
     function );
3904
3905
0
    return( -1 );
3906
0
  }
3907
6.19M
  if( binary_data_size > (size_t) SSIZE_MAX )
3908
0
  {
3909
0
    libcerror_error_set(
3910
0
     error,
3911
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3912
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
3913
0
     "%s: invalid binary XML document data size value exceeds maximum.",
3914
0
     function );
3915
3916
0
    return( -1 );
3917
0
  }
3918
6.19M
  if( binary_data_offset >= binary_data_size )
3919
0
  {
3920
0
    libcerror_error_set(
3921
0
     error,
3922
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3923
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
3924
0
     "%s: invalid binary data offset value out of bounds.",
3925
0
     function );
3926
3927
0
    return( -1 );
3928
0
  }
3929
6.19M
  xml_document_data      = &( binary_data[ binary_data_offset ] );
3930
6.19M
  xml_document_data_size = binary_data_size - binary_data_offset;
3931
3932
6.19M
  if( xml_document_data_size < 4 )
3933
70
  {
3934
70
    libcerror_error_set(
3935
70
     error,
3936
70
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
3937
70
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
3938
70
     "%s: invalid binary XML document data size value too small.",
3939
70
     function );
3940
3941
70
    return( -1 );
3942
70
  }
3943
#if defined( HAVE_DEBUG_OUTPUT )
3944
  if( libcnotify_verbose != 0 )
3945
  {
3946
    libcnotify_printf(
3947
     "%s: data offset\t\t: 0x%08" PRIzx "\n",
3948
     function,
3949
     binary_data_offset );
3950
3951
    libcnotify_printf(
3952
     "%s: optional substitution data:\n",
3953
     function );
3954
    libcnotify_print_data(
3955
     xml_document_data,
3956
     4,
3957
     0 );
3958
  }
3959
#endif
3960
6.19M
  byte_stream_copy_to_uint16_little_endian(
3961
6.19M
   &( xml_document_data[ 1 ] ),
3962
6.19M
   template_value_index );
3963
3964
6.19M
  template_value_type = xml_document_data[ 3 ];
3965
3966
#if defined( HAVE_DEBUG_OUTPUT )
3967
  if( libcnotify_verbose != 0 )
3968
  {
3969
    libcnotify_printf(
3970
     "%s: type\t\t\t: 0x%02" PRIx8 "\n",
3971
     function,
3972
     xml_document_data[ 0 ] );
3973
3974
    libcnotify_printf(
3975
     "%s: identifier\t\t: %" PRIu16 "\n",
3976
     function,
3977
     template_value_index );
3978
3979
    libcnotify_printf(
3980
     "%s: value type\t\t: 0x%02" PRIx8 " (",
3981
     function,
3982
     template_value_type );
3983
    libfwevt_debug_print_value_type(
3984
     template_value_type );
3985
    libcnotify_printf(
3986
     ")\n" );
3987
3988
    libcnotify_printf(
3989
     "\n" );
3990
  }
3991
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
3992
3993
6.19M
  xml_token->size = 4;
3994
3995
6.19M
  result = libfwevt_xml_document_substitute_template_value(
3996
6.19M
            internal_xml_document,
3997
6.19M
            binary_data,
3998
6.19M
            binary_data_size,
3999
6.19M
            ascii_codepage,
4000
6.19M
            flags,
4001
6.19M
            template_values_array,
4002
6.19M
            template_value_index,
4003
6.19M
            template_value_type,
4004
6.19M
            template_value_offset,
4005
6.19M
            xml_tag,
4006
6.19M
            element_recursion_depth,
4007
6.19M
            template_instance_recursion_depth,
4008
6.19M
            error );
4009
4010
6.19M
  if( result == -1 )
4011
38.6k
  {
4012
38.6k
    libcerror_error_set(
4013
38.6k
     error,
4014
38.6k
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
4015
38.6k
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
4016
38.6k
     "%s: unable to substitute template value.",
4017
38.6k
     function );
4018
4019
38.6k
    return( -1 );
4020
38.6k
  }
4021
6.15M
  return( result );
4022
6.19M
}
4023
4024
/* Reads a PI data from a binary XML document
4025
 * Returns 1 if successful or -1 on error
4026
 */
4027
int libfwevt_xml_document_read_pi_data(
4028
     libfwevt_internal_xml_document_t *internal_xml_document,
4029
     libfwevt_xml_token_t *xml_token,
4030
     const uint8_t *binary_data,
4031
     size_t binary_data_size,
4032
     size_t binary_data_offset,
4033
     libfwevt_xml_tag_t *xml_tag,
4034
     int ascii_codepage LIBFWEVT_ATTRIBUTE_UNUSED,
4035
     libcerror_error_t **error )
4036
33.0k
{
4037
33.0k
  const uint8_t *xml_document_data = NULL;
4038
33.0k
  static char *function            = "libfwevt_xml_document_read_pi_data";
4039
33.0k
  size_t value_data_size           = 0;
4040
33.0k
  size_t xml_document_data_size    = 0;
4041
4042
33.0k
  LIBFWEVT_UNREFERENCED_PARAMETER( ascii_codepage )
4043
4044
33.0k
  if( internal_xml_document == NULL )
4045
0
  {
4046
0
    libcerror_error_set(
4047
0
     error,
4048
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4049
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
4050
0
     "%s: invalid binary XML document.",
4051
0
     function );
4052
4053
0
    return( -1 );
4054
0
  }
4055
33.0k
  if( xml_token == NULL )
4056
0
  {
4057
0
    libcerror_error_set(
4058
0
     error,
4059
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4060
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
4061
0
     "%s: invalid binary XML token.",
4062
0
     function );
4063
4064
0
    return( -1 );
4065
0
  }
4066
33.0k
  if( xml_token->type != LIBFWEVT_XML_TOKEN_PI_DATA )
4067
222
  {
4068
222
    libcerror_error_set(
4069
222
     error,
4070
222
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
4071
222
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
4072
222
     "%s: invalid binary XML token - unsupported type: 0x%02" PRIx8 ".",
4073
222
     function,
4074
222
     xml_token->type );
4075
4076
222
    return( -1 );
4077
222
  }
4078
32.8k
  if( binary_data == NULL )
4079
0
  {
4080
0
    libcerror_error_set(
4081
0
     error,
4082
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4083
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
4084
0
     "%s: invalid binary data.",
4085
0
     function );
4086
4087
0
    return( -1 );
4088
0
  }
4089
32.8k
  if( binary_data_size > (size_t) SSIZE_MAX )
4090
0
  {
4091
0
    libcerror_error_set(
4092
0
     error,
4093
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4094
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
4095
0
     "%s: invalid binary XML document data size value exceeds maximum.",
4096
0
     function );
4097
4098
0
    return( -1 );
4099
0
  }
4100
32.8k
  if( binary_data_offset >= binary_data_size )
4101
0
  {
4102
0
    libcerror_error_set(
4103
0
     error,
4104
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4105
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
4106
0
     "%s: invalid binary data offset value out of bounds.",
4107
0
     function );
4108
4109
0
    return( -1 );
4110
0
  }
4111
32.8k
  if( xml_tag == NULL )
4112
0
  {
4113
0
    libcerror_error_set(
4114
0
     error,
4115
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4116
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
4117
0
     "%s: invalid XML tag.",
4118
0
     function );
4119
4120
0
    return( -1 );
4121
0
  }
4122
32.8k
  xml_document_data      = &( binary_data[ binary_data_offset ] );
4123
32.8k
  xml_document_data_size = binary_data_size - binary_data_offset;
4124
4125
32.8k
  if( xml_document_data_size < 3 )
4126
42
  {
4127
42
    libcerror_error_set(
4128
42
     error,
4129
42
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
4130
42
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
4131
42
     "%s: invalid binary XML document data size value too small.",
4132
42
     function );
4133
4134
42
    return( -1 );
4135
42
  }
4136
#if defined( HAVE_DEBUG_OUTPUT )
4137
  if( libcnotify_verbose != 0 )
4138
  {
4139
    libcnotify_printf(
4140
     "%s: data offset\t\t: 0x%08" PRIzx "\n",
4141
     function,
4142
     binary_data_offset );
4143
4144
    libcnotify_printf(
4145
     "%s: PI data:\n",
4146
     function );
4147
    libcnotify_print_data(
4148
     xml_document_data,
4149
     3,
4150
     0 );
4151
  }
4152
#endif
4153
32.8k
  byte_stream_copy_to_uint16_little_endian(
4154
32.8k
   &( xml_document_data[ 1 ] ),
4155
32.8k
   value_data_size );
4156
4157
#if defined( HAVE_DEBUG_OUTPUT )
4158
  if( libcnotify_verbose != 0 )
4159
  {
4160
    libcnotify_printf(
4161
     "%s: type\t\t\t\t: 0x%02" PRIx8 "\n",
4162
     function,
4163
     xml_document_data[ 0 ] );
4164
4165
    libcnotify_printf(
4166
     "%s: number of characters\t\t: %" PRIzd "\n",
4167
     function,
4168
     value_data_size );
4169
  }
4170
#endif
4171
32.8k
  xml_token->size     = 3;
4172
32.8k
  binary_data_offset += 3;
4173
4174
32.8k
  value_data_size *= 2;
4175
4176
32.8k
  if( ( value_data_size > binary_data_size )
4177
32.5k
   || ( binary_data_offset >= ( binary_data_size - value_data_size ) ) )
4178
359
  {
4179
359
    libcerror_error_set(
4180
359
     error,
4181
359
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4182
359
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
4183
359
     "%s: invalid value data size value out of bounds.",
4184
359
     function );
4185
4186
359
    return( -1 );
4187
359
  }
4188
#if defined( HAVE_DEBUG_OUTPUT )
4189
  if( libcnotify_verbose != 0 )
4190
  {
4191
    libcnotify_printf(
4192
     "%s: data offset\t\t: 0x%08" PRIzx "\n",
4193
     function,
4194
     binary_data_offset );
4195
4196
    libcnotify_printf(
4197
     "%s: value data:\n",
4198
     function );
4199
    libcnotify_print_data(
4200
     &( xml_document_data[ 3 ] ),
4201
     value_data_size,
4202
     LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
4203
  }
4204
#endif
4205
32.4k
  if( libfwevt_xml_tag_set_value_type(
4206
32.4k
       xml_tag,
4207
32.4k
       LIBFWEVT_VALUE_TYPE_STRING_UTF16,
4208
32.4k
       error ) != 1 )
4209
0
  {
4210
0
    libcerror_error_set(
4211
0
     error,
4212
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
4213
0
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
4214
0
     "%s: unable to set value type.",
4215
0
     function );
4216
4217
0
    return( -1 );
4218
0
  }
4219
32.4k
  if( libfwevt_xml_tag_set_value_data(
4220
32.4k
       xml_tag,
4221
32.4k
       &( binary_data[ binary_data_offset ] ),
4222
32.4k
       value_data_size,
4223
32.4k
       error ) != 1 )
4224
0
  {
4225
0
    libcerror_error_set(
4226
0
     error,
4227
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
4228
0
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
4229
0
     "%s: unable to set value data.",
4230
0
     function );
4231
4232
0
    return( -1 );
4233
0
  }
4234
#if defined( HAVE_DEBUG_OUTPUT )
4235
  if( libcnotify_verbose != 0 )
4236
  {
4237
    if( libfwevt_xml_tag_debug_print_value_data_segment(
4238
         xml_tag,
4239
         0,
4240
         0,
4241
         ascii_codepage,
4242
         error ) != 1 )
4243
    {
4244
      libcerror_error_set(
4245
       error,
4246
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
4247
       LIBCERROR_RUNTIME_ERROR_PRINT_FAILED,
4248
       "%s: unable to print value data segment: 0.",
4249
       function );
4250
4251
      return( -1 );
4252
    }
4253
  }
4254
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
4255
4256
32.4k
  xml_token->size += value_data_size;
4257
4258
32.4k
  return( 1 );
4259
32.4k
}
4260
4261
/* Reads a PI target from a binary XML document
4262
 * Returns 1 if successful or -1 on error
4263
 */
4264
int libfwevt_xml_document_read_pi_target(
4265
     libfwevt_internal_xml_document_t *internal_xml_document,
4266
     libfwevt_xml_token_t *xml_token,
4267
     const uint8_t *binary_data,
4268
     size_t binary_data_size,
4269
     size_t binary_data_offset,
4270
     uint8_t flags,
4271
     libfwevt_xml_tag_t *xml_tag,
4272
     int ascii_codepage,
4273
     libcerror_error_t **error )
4274
33.9k
{
4275
33.9k
  libfwevt_xml_tag_t *pi_xml_tag      = NULL;
4276
33.9k
  libfwevt_xml_token_t *xml_sub_token = NULL;
4277
33.9k
  const uint8_t *xml_document_data    = NULL;
4278
33.9k
  static char *function               = "libfwevt_xml_document_read_pi_target";
4279
33.9k
  size_t additional_value_size        = 0;
4280
33.9k
  size_t trailing_data_size           = 0;
4281
33.9k
  size_t xml_document_data_offset     = 0;
4282
33.9k
  size_t xml_document_data_size       = 0;
4283
33.9k
  uint32_t pi_name_offset             = 0;
4284
33.9k
  uint32_t pi_name_size               = 0;
4285
4286
33.9k
  if( internal_xml_document == NULL )
4287
0
  {
4288
0
    libcerror_error_set(
4289
0
     error,
4290
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4291
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
4292
0
     "%s: invalid binary XML document.",
4293
0
     function );
4294
4295
0
    return( -1 );
4296
0
  }
4297
33.9k
  if( xml_token == NULL )
4298
0
  {
4299
0
    libcerror_error_set(
4300
0
     error,
4301
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4302
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
4303
0
     "%s: invalid binary XML token.",
4304
0
     function );
4305
4306
0
    return( -1 );
4307
0
  }
4308
33.9k
  if( xml_token->type != LIBFWEVT_XML_TOKEN_PI_TARGET )
4309
0
  {
4310
0
    libcerror_error_set(
4311
0
     error,
4312
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
4313
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
4314
0
     "%s: invalid binary XML token - unsupported type: 0x%02" PRIx8 ".",
4315
0
     function,
4316
0
     xml_token->type );
4317
4318
0
    return( -1 );
4319
0
  }
4320
33.9k
  if( binary_data == NULL )
4321
0
  {
4322
0
    libcerror_error_set(
4323
0
     error,
4324
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4325
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
4326
0
     "%s: invalid binary data.",
4327
0
     function );
4328
4329
0
    return( -1 );
4330
0
  }
4331
33.9k
  if( binary_data_size > (size_t) SSIZE_MAX )
4332
0
  {
4333
0
    libcerror_error_set(
4334
0
     error,
4335
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4336
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
4337
0
     "%s: invalid binary XML document data size value exceeds maximum.",
4338
0
     function );
4339
4340
0
    return( -1 );
4341
0
  }
4342
33.9k
  if( binary_data_offset >= binary_data_size )
4343
0
  {
4344
0
    libcerror_error_set(
4345
0
     error,
4346
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4347
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
4348
0
     "%s: invalid binary data offset value out of bounds.",
4349
0
     function );
4350
4351
0
    return( -1 );
4352
0
  }
4353
33.9k
  if( xml_tag == NULL )
4354
0
  {
4355
0
    libcerror_error_set(
4356
0
     error,
4357
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4358
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
4359
0
     "%s: invalid XML tag.",
4360
0
     function );
4361
4362
0
    return( -1 );
4363
0
  }
4364
33.9k
  if( ( flags & LIBFWEVT_XML_DOCUMENT_READ_FLAG_HAS_DATA_OFFSETS ) != 0 )
4365
19.1k
  {
4366
19.1k
    additional_value_size = 4;
4367
19.1k
  }
4368
33.9k
  xml_document_data      = &( binary_data[ binary_data_offset ] );
4369
33.9k
  xml_document_data_size = binary_data_size - binary_data_offset;
4370
4371
33.9k
  if( xml_document_data_size < ( 1 + additional_value_size ) )
4372
49
  {
4373
49
    libcerror_error_set(
4374
49
     error,
4375
49
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
4376
49
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
4377
49
     "%s: invalid binary XML document data size value too small.",
4378
49
     function );
4379
4380
49
    return( -1 );
4381
49
  }
4382
33.8k
  if( libfwevt_xml_tag_initialize(
4383
33.8k
       &pi_xml_tag,
4384
33.8k
       error ) != 1 )
4385
0
  {
4386
0
    libcerror_error_set(
4387
0
     error,
4388
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
4389
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
4390
0
     "%s: unable to create PI XML tag.",
4391
0
     function );
4392
4393
0
    goto on_error;
4394
0
  }
4395
33.8k
  if( libfwevt_xml_tag_set_type(
4396
33.8k
       pi_xml_tag,
4397
33.8k
       LIBFWEVT_XML_TAG_TYPE_PI,
4398
33.8k
       error ) != 1 )
4399
0
  {
4400
0
    libcerror_error_set(
4401
0
     error,
4402
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
4403
0
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
4404
0
     "%s: unable to set XML tag type.",
4405
0
     function );
4406
4407
0
    goto on_error;
4408
0
  }
4409
#if defined( HAVE_DEBUG_OUTPUT )
4410
  if( libcnotify_verbose != 0 )
4411
  {
4412
    libcnotify_printf(
4413
     "%s: data offset\t\t: 0x%08" PRIzx "\n",
4414
     function,
4415
     binary_data_offset );
4416
4417
    libcnotify_printf(
4418
     "%s: PI target data:\n",
4419
     function );
4420
    libcnotify_print_data(
4421
     xml_document_data,
4422
     1 + additional_value_size,
4423
     LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
4424
  }
4425
#endif
4426
#if defined( HAVE_DEBUG_OUTPUT )
4427
  if( libcnotify_verbose != 0 )
4428
  {
4429
    libcnotify_printf(
4430
     "%s: type\t\t\t: 0x%02" PRIx8 "\n",
4431
     function,
4432
     xml_document_data[ 0 ] );
4433
  }
4434
#endif
4435
33.8k
  xml_token->size          = 1;
4436
33.8k
  xml_document_data_offset = 1;
4437
4438
33.8k
  if( ( flags & LIBFWEVT_XML_DOCUMENT_READ_FLAG_HAS_DATA_OFFSETS ) == 0 )
4439
14.8k
  {
4440
    /* TODO double check if this needs to be binary_data_offset + xml_document_data_offset + 1
4441
     */
4442
14.8k
    pi_name_offset = (uint32_t) ( binary_data_offset + xml_document_data_offset );
4443
14.8k
  }
4444
19.0k
  else
4445
19.0k
  {
4446
19.0k
    if( ( xml_document_data_size < 4 )
4447
19.0k
     || ( xml_document_data_offset >= ( xml_document_data_size - 4 ) ) )
4448
25
    {
4449
25
      libcerror_error_set(
4450
25
       error,
4451
25
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
4452
25
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
4453
25
       "%s: invalid binary XML document data size value too small.",
4454
25
       function );
4455
4456
25
      goto on_error;
4457
25
    }
4458
19.0k
    byte_stream_copy_to_uint32_little_endian(
4459
19.0k
     &( xml_document_data[ xml_document_data_offset ] ),
4460
19.0k
     pi_name_offset );
4461
4462
#if defined( HAVE_DEBUG_OUTPUT )
4463
    if( libcnotify_verbose != 0 )
4464
    {
4465
      libcnotify_printf(
4466
       "%s: name offset\t\t\t: 0x%08" PRIx32 "\n",
4467
       function,
4468
       pi_name_offset );
4469
    }
4470
#endif
4471
19.0k
    xml_token->size           = 4;
4472
/* TODO
4473
    xml_document_data_offset += 4;
4474
*/
4475
19.0k
  }
4476
#if defined( HAVE_DEBUG_OUTPUT )
4477
  if( libcnotify_verbose != 0 )
4478
  {
4479
    libcnotify_printf(
4480
     "\n" );
4481
  }
4482
#endif
4483
4484
  /* TODO double check setting this to 5 but this is currently needed
4485
   * likely because of additional_value_size when LIBFWEVT_XML_DOCUMENT_READ_FLAG_HAS_DATA_OFFSETS is not set
4486
   */
4487
33.8k
  xml_token->size          = 5;
4488
33.8k
  xml_document_data_offset = 5;
4489
4490
33.8k
  if( pi_name_offset > ( binary_data_offset + xml_document_data_offset ) )
4491
540
  {
4492
540
    libcerror_error_set(
4493
540
     error,
4494
540
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4495
540
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
4496
540
     "%s: invalid PI name offset value out of bounds.",
4497
540
     function );
4498
4499
540
    goto on_error;
4500
540
  }
4501
33.3k
  if( ( binary_data_offset + xml_document_data_offset ) < pi_name_offset )
4502
0
  {
4503
0
    trailing_data_size = pi_name_offset - ( binary_data_offset + xml_document_data_offset );
4504
4505
#if defined( HAVE_DEBUG_OUTPUT )
4506
    if( libcnotify_verbose != 0 )
4507
    {
4508
      libcnotify_printf(
4509
       "%s: data offset\t\t: 0x%08" PRIzx "\n",
4510
       function,
4511
       binary_data_offset + 5 );
4512
4513
      libcnotify_printf(
4514
       "%s: trailing data:\n",
4515
       function );
4516
      libcnotify_print_data(
4517
       &( xml_document_data[ 5 ] ),
4518
       trailing_data_size,
4519
       LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
4520
    }
4521
#endif
4522
0
    xml_token->size          += trailing_data_size;
4523
0
    xml_document_data_offset += trailing_data_size;
4524
0
  }
4525
33.3k
  if( libfwevt_xml_document_read_name(
4526
33.3k
       internal_xml_document,
4527
33.3k
       binary_data,
4528
33.3k
       binary_data_size,
4529
33.3k
       pi_name_offset,
4530
33.3k
       flags,
4531
33.3k
       &pi_name_size,
4532
33.3k
       pi_xml_tag,
4533
33.3k
       error ) != 1 )
4534
171
  {
4535
171
    libcerror_error_set(
4536
171
     error,
4537
171
     LIBCERROR_ERROR_DOMAIN_IO,
4538
171
     LIBCERROR_IO_ERROR_READ_FAILED,
4539
171
     "%s: unable to read PI name.",
4540
171
     function );
4541
4542
171
    goto on_error;
4543
171
  }
4544
33.1k
  if( ( binary_data_offset + xml_document_data_offset ) == pi_name_offset )
4545
1.71k
  {
4546
1.71k
    xml_token->size          += pi_name_size;
4547
1.71k
    xml_document_data_offset += pi_name_size;
4548
1.71k
  }
4549
33.1k
  if( libfwevt_xml_token_initialize(
4550
33.1k
       &xml_sub_token,
4551
33.1k
       error ) != 1 )
4552
0
  {
4553
0
    libcerror_error_set(
4554
0
     error,
4555
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
4556
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
4557
0
     "%s: unable to create binary XML sub token.",
4558
0
     function );
4559
4560
0
    goto on_error;
4561
0
  }
4562
33.1k
  if( libfwevt_xml_token_read_data(
4563
33.1k
       xml_sub_token,
4564
33.1k
       binary_data,
4565
33.1k
       binary_data_size,
4566
33.1k
       binary_data_offset + xml_document_data_offset,
4567
33.1k
       error ) != 1 )
4568
69
  {
4569
69
    libcerror_error_set(
4570
69
     error,
4571
69
     LIBCERROR_ERROR_DOMAIN_IO,
4572
69
     LIBCERROR_IO_ERROR_READ_FAILED,
4573
69
     "%s: unable to read binary XML sub token.",
4574
69
     function );
4575
4576
69
    goto on_error;
4577
69
  }
4578
33.0k
  if( libfwevt_xml_document_read_pi_data(
4579
33.0k
       internal_xml_document,
4580
33.0k
       xml_sub_token,
4581
33.0k
       binary_data,
4582
33.0k
       binary_data_size,
4583
33.0k
       binary_data_offset + xml_document_data_offset,
4584
33.0k
       pi_xml_tag,
4585
33.0k
       ascii_codepage,
4586
33.0k
       error ) != 1 )
4587
623
  {
4588
623
    libcerror_error_set(
4589
623
     error,
4590
623
     LIBCERROR_ERROR_DOMAIN_IO,
4591
623
     LIBCERROR_IO_ERROR_READ_FAILED,
4592
623
     "%s: unable to read PI target.",
4593
623
     function );
4594
4595
623
    goto on_error;
4596
623
  }
4597
32.4k
  xml_token->size += xml_sub_token->size;
4598
4599
32.4k
  if( libfwevt_xml_token_free(
4600
32.4k
       &xml_sub_token,
4601
32.4k
       error ) != 1 )
4602
0
  {
4603
0
    libcerror_error_set(
4604
0
     error,
4605
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
4606
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
4607
0
     "%s: unable to free binary XML sub token.",
4608
0
     function );
4609
4610
0
    goto on_error;
4611
0
  }
4612
32.4k
  if( libfwevt_xml_tag_append_element(
4613
32.4k
       xml_tag,
4614
32.4k
       pi_xml_tag,
4615
32.4k
       error ) != 1 )
4616
0
  {
4617
0
    libcerror_error_set(
4618
0
     error,
4619
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
4620
0
     LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
4621
0
     "%s: unable to append PI to XML tag.",
4622
0
     function );
4623
4624
0
    goto on_error;
4625
0
  }
4626
32.4k
  pi_xml_tag = NULL;
4627
4628
32.4k
  return( 1 );
4629
4630
1.42k
on_error:
4631
1.42k
  if( xml_sub_token != NULL )
4632
692
  {
4633
692
    libfwevt_xml_token_free(
4634
692
     &xml_sub_token,
4635
692
     NULL );
4636
692
  }
4637
1.42k
  if( pi_xml_tag != NULL )
4638
1.42k
  {
4639
1.42k
    libfwevt_internal_xml_tag_free(
4640
1.42k
     (libfwevt_internal_xml_tag_t **) &pi_xml_tag,
4641
1.42k
     NULL );
4642
1.42k
  }
4643
1.42k
  return( -1 );
4644
32.4k
}
4645
4646
/* Reads a template instance from a binary XML document
4647
 * Returns 1 if successful or -1 on error
4648
 */
4649
int libfwevt_xml_document_read_template_instance(
4650
     libfwevt_internal_xml_document_t *internal_xml_document,
4651
     libfwevt_xml_token_t *xml_token,
4652
     const uint8_t *binary_data,
4653
     size_t binary_data_size,
4654
     size_t binary_data_offset,
4655
     int ascii_codepage,
4656
     uint8_t flags,
4657
     libfwevt_xml_tag_t *xml_tag,
4658
     int element_recursion_depth,
4659
     int template_instance_recursion_depth,
4660
     libcerror_error_t **error )
4661
78.7k
{
4662
78.7k
  libcdata_array_t *template_values_array  = NULL;
4663
78.7k
  libfwevt_xml_token_t *xml_sub_token      = NULL;
4664
78.7k
  const uint8_t *xml_document_data         = NULL;
4665
78.7k
  static char *function                    = "libfwevt_xml_document_read_template_instance";
4666
78.7k
  size_t template_data_offset              = 0;
4667
78.7k
  size_t template_data_size                = 0;
4668
78.7k
  size_t template_values_data_offset       = 0;
4669
78.7k
  size_t template_values_data_size         = 0;
4670
78.7k
  size_t trailing_data_size                = 0;
4671
78.7k
  size_t xml_document_data_size            = 0;
4672
78.7k
  uint32_t template_definition_data_offset = 0;
4673
78.7k
  uint32_t template_definition_data_size   = 0;
4674
4675
#if defined( HAVE_DEBUG_OUTPUT )
4676
  uint32_t value_32bit                     = 0;
4677
#endif
4678
4679
78.7k
  if( internal_xml_document == NULL )
4680
0
  {
4681
0
    libcerror_error_set(
4682
0
     error,
4683
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4684
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
4685
0
     "%s: invalid binary XML document.",
4686
0
     function );
4687
4688
0
    return( -1 );
4689
0
  }
4690
78.7k
  if( xml_token == NULL )
4691
0
  {
4692
0
    libcerror_error_set(
4693
0
     error,
4694
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4695
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
4696
0
     "%s: invalid binary XML token.",
4697
0
     function );
4698
4699
0
    return( -1 );
4700
0
  }
4701
78.7k
  if( xml_token->type != LIBFWEVT_XML_TOKEN_TEMPLATE_INSTANCE )
4702
0
  {
4703
0
    libcerror_error_set(
4704
0
     error,
4705
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
4706
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
4707
0
     "%s: invalid binary XML token - unsupported type: 0x%02" PRIx8 ".",
4708
0
     function,
4709
0
     xml_token->type );
4710
4711
0
    return( -1 );
4712
0
  }
4713
78.7k
  if( binary_data == NULL )
4714
0
  {
4715
0
    libcerror_error_set(
4716
0
     error,
4717
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4718
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
4719
0
     "%s: invalid binary data.",
4720
0
     function );
4721
4722
0
    return( -1 );
4723
0
  }
4724
78.7k
  if( binary_data_size > (size_t) SSIZE_MAX )
4725
0
  {
4726
0
    libcerror_error_set(
4727
0
     error,
4728
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4729
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
4730
0
     "%s: invalid binary XML document data size value exceeds maximum.",
4731
0
     function );
4732
4733
0
    return( -1 );
4734
0
  }
4735
78.7k
  if( binary_data_offset >= binary_data_size )
4736
0
  {
4737
0
    libcerror_error_set(
4738
0
     error,
4739
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4740
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
4741
0
     "%s: invalid binary data offset value out of bounds.",
4742
0
     function );
4743
4744
0
    return( -1 );
4745
0
  }
4746
78.7k
  if( ( template_instance_recursion_depth < 0 )
4747
78.7k
   || ( template_instance_recursion_depth > LIBFWEVT_XML_DOCUMENT_TEMPLATE_INSTANCE_RECURSION_DEPTH ) )
4748
1
  {
4749
1
    libcerror_error_set(
4750
1
     error,
4751
1
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
4752
1
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
4753
1
     "%s: invalid template instance recursion depth value out of bounds.",
4754
1
     function );
4755
4756
1
    return( -1 );
4757
1
  }
4758
78.7k
  xml_document_data      = &( binary_data[ binary_data_offset ] );
4759
78.7k
  xml_document_data_size = binary_data_size - binary_data_offset;
4760
4761
78.7k
  if( ( binary_data_size < 10 )
4762
78.7k
   || ( binary_data_offset >= ( binary_data_size - 10 ) ) )
4763
3
  {
4764
3
    libcerror_error_set(
4765
3
     error,
4766
3
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
4767
3
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
4768
3
     "%s: invalid binary XML document data size value too small.",
4769
3
     function );
4770
4771
3
    goto on_error;
4772
3
  }
4773
#if defined( HAVE_DEBUG_OUTPUT )
4774
  if( libcnotify_verbose != 0 )
4775
  {
4776
    libcnotify_printf(
4777
     "%s: data offset\t\t: 0x%08" PRIzx "\n",
4778
     function,
4779
     binary_data_offset );
4780
4781
    libcnotify_printf(
4782
     "%s: template instance header data:\n",
4783
     function );
4784
    libcnotify_print_data(
4785
     xml_document_data,
4786
     10,
4787
     0 );
4788
  }
4789
#endif
4790
78.7k
  byte_stream_copy_to_uint32_little_endian(
4791
78.7k
   &( xml_document_data[ 6 ] ),
4792
78.7k
   template_definition_data_offset );
4793
4794
#if defined( HAVE_DEBUG_OUTPUT )
4795
  if( libcnotify_verbose != 0 )
4796
  {
4797
    libcnotify_printf(
4798
     "%s: type\t\t\t: 0x%02" PRIx8 "\n",
4799
     function,
4800
     xml_document_data[ 0 ] );
4801
4802
    libcnotify_printf(
4803
     "%s: unknown1\t\t\t: %" PRIu8 "\n",
4804
     function,
4805
     xml_document_data[ 1 ] );
4806
4807
    byte_stream_copy_to_uint32_little_endian(
4808
     &( xml_document_data[ 2 ] ),
4809
     value_32bit );
4810
    libcnotify_printf(
4811
     "%s: unknown2\t\t\t: 0x%08" PRIx32 "\n",
4812
     function,
4813
     value_32bit );
4814
4815
    libcnotify_printf(
4816
     "%s: data offset\t\t: 0x%08" PRIx32 "\n",
4817
     function,
4818
     template_definition_data_offset );
4819
4820
    libcnotify_printf(
4821
     "\n" );
4822
  }
4823
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
4824
4825
78.7k
  xml_token->size     = 10;
4826
78.7k
  binary_data_offset += 10;
4827
4828
78.7k
  if( template_definition_data_offset >= binary_data_size )
4829
181
  {
4830
181
    libcerror_error_set(
4831
181
     error,
4832
181
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4833
181
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
4834
181
     "%s: invalid template definition data offset value out of bounds.",
4835
181
     function );
4836
4837
181
    goto on_error;
4838
181
  }
4839
78.5k
  if( template_definition_data_offset > binary_data_offset )
4840
1.51k
  {
4841
1.51k
    trailing_data_size = template_definition_data_offset - binary_data_offset;
4842
4843
#if defined( HAVE_DEBUG_OUTPUT )
4844
    if( libcnotify_verbose != 0 )
4845
    {
4846
      libcnotify_printf(
4847
       "%s: data offset\t\t: 0x%08" PRIzx "\n",
4848
       function,
4849
       binary_data_offset );
4850
4851
      libcnotify_printf(
4852
       "%s: trailing data:\n",
4853
       function );
4854
      libcnotify_print_data(
4855
       &( binary_data[ binary_data_offset ] ),
4856
       trailing_data_size,
4857
       LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
4858
    }
4859
#endif
4860
1.51k
    xml_token->size    += trailing_data_size;
4861
1.51k
    binary_data_offset += trailing_data_size;
4862
1.51k
  }
4863
78.5k
  template_data_offset = template_definition_data_offset;
4864
4865
78.5k
  if( ( binary_data_size < 24 )
4866
78.5k
   || ( template_data_offset >= ( binary_data_size - 24 ) ) )
4867
10
  {
4868
10
    libcerror_error_set(
4869
10
     error,
4870
10
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
4871
10
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
4872
10
     "%s: invalid binary XML document data size value too small.",
4873
10
     function );
4874
4875
10
    goto on_error;
4876
10
  }
4877
78.5k
  byte_stream_copy_to_uint32_little_endian(
4878
78.5k
   &( binary_data[ template_data_offset + 20 ] ),
4879
78.5k
   template_definition_data_size );
4880
4881
#if defined( HAVE_DEBUG_OUTPUT )
4882
  if( libcnotify_verbose != 0 )
4883
  {
4884
    byte_stream_copy_to_uint32_little_endian(
4885
     &( binary_data[ template_data_offset ] ),
4886
     value_32bit );
4887
    libcnotify_printf(
4888
     "%s: offset next\t\t: 0x%08" PRIx32 "\n",
4889
     function,
4890
     value_32bit );
4891
4892
    if( libfwevt_debug_print_guid_value(
4893
         function,
4894
         "identifier\t\t",
4895
         &( binary_data[ template_data_offset + 4 ] ),
4896
         16,
4897
         LIBFGUID_ENDIAN_LITTLE,
4898
         LIBFGUID_STRING_FORMAT_FLAG_USE_LOWER_CASE,
4899
         error ) != 1 )
4900
    {
4901
      libcerror_error_set(
4902
       error,
4903
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
4904
       LIBCERROR_RUNTIME_ERROR_PRINT_FAILED,
4905
       "%s: unable to print GUID value.",
4906
       function );
4907
4908
      return( -1 );
4909
    }
4910
    libcnotify_printf(
4911
     "%s: definition size\t\t: %" PRIu32 "\n",
4912
     function,
4913
     template_definition_data_size );
4914
4915
    libcnotify_printf(
4916
     "\n" );
4917
  }
4918
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
4919
4920
78.5k
  if( template_definition_data_size > binary_data_size )
4921
763
  {
4922
763
    libcerror_error_set(
4923
763
     error,
4924
763
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
4925
763
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
4926
763
     "%s: invalid template definition data size value out of bounds.",
4927
763
     function );
4928
4929
763
    goto on_error;
4930
763
  }
4931
77.7k
  if( template_data_offset == binary_data_offset )
4932
5.42k
  {
4933
5.42k
    template_values_data_offset = 24 + template_definition_data_size;
4934
5.42k
  }
4935
72.3k
  else
4936
72.3k
  {
4937
72.3k
    template_values_data_offset = 0;
4938
72.3k
  }
4939
77.7k
  template_data_offset += 24;
4940
4941
77.7k
  if( template_values_data_offset >= xml_document_data_size )
4942
33
  {
4943
33
    libcerror_error_set(
4944
33
     error,
4945
33
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
4946
33
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
4947
33
     "%s: invalid template values data offset value out of bounds.",
4948
33
     function );
4949
4950
33
    goto on_error;
4951
33
  }
4952
77.7k
  if( libfwevt_xml_document_read_template_instance_values(
4953
77.7k
       internal_xml_document,
4954
77.7k
       binary_data,
4955
77.7k
       binary_data_size,
4956
77.7k
       binary_data_offset + template_values_data_offset,
4957
77.7k
       &template_values_array,
4958
77.7k
       &template_values_data_size,
4959
77.7k
       error ) != 1 )
4960
435
  {
4961
435
    libcerror_error_set(
4962
435
     error,
4963
435
     LIBCERROR_ERROR_DOMAIN_IO,
4964
435
     LIBCERROR_IO_ERROR_READ_FAILED,
4965
435
     "%s: unable to read document template instance values.",
4966
435
     function );
4967
4968
435
    goto on_error;
4969
435
  }
4970
77.2k
  xml_token->size += template_values_data_size;
4971
4972
77.2k
  if( libfwevt_xml_token_initialize(
4973
77.2k
       &xml_sub_token,
4974
77.2k
       error ) != 1 )
4975
0
  {
4976
0
    libcerror_error_set(
4977
0
     error,
4978
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
4979
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
4980
0
     "%s: unable to create binary XML sub token.",
4981
0
     function );
4982
4983
0
    goto on_error;
4984
0
  }
4985
77.2k
  if( libfwevt_xml_token_read_data(
4986
77.2k
       xml_sub_token,
4987
77.2k
       binary_data,
4988
77.2k
       binary_data_size,
4989
77.2k
       template_data_offset,
4990
77.2k
       error ) != 1 )
4991
52
  {
4992
52
    libcerror_error_set(
4993
52
     error,
4994
52
     LIBCERROR_ERROR_DOMAIN_IO,
4995
52
     LIBCERROR_IO_ERROR_READ_FAILED,
4996
52
     "%s: unable to read binary XML sub token.",
4997
52
     function );
4998
4999
52
    goto on_error;
5000
52
  }
5001
77.2k
  if( libfwevt_xml_document_read_fragment_header(
5002
77.2k
       internal_xml_document,
5003
77.2k
       xml_sub_token,
5004
77.2k
       binary_data,
5005
77.2k
       binary_data_size,
5006
77.2k
       template_data_offset,
5007
77.2k
       error ) != 1 )
5008
316
  {
5009
316
    libcerror_error_set(
5010
316
     error,
5011
316
     LIBCERROR_ERROR_DOMAIN_IO,
5012
316
     LIBCERROR_IO_ERROR_READ_FAILED,
5013
316
     "%s: unable to read fragment header.",
5014
316
     function );
5015
5016
316
    goto on_error;
5017
316
  }
5018
76.9k
  template_data_offset += xml_sub_token->size;
5019
5020
76.9k
  if( libfwevt_xml_token_read_data(
5021
76.9k
       xml_sub_token,
5022
76.9k
       binary_data,
5023
76.9k
       binary_data_size,
5024
76.9k
       template_data_offset,
5025
76.9k
       error ) != 1 )
5026
16
  {
5027
16
    libcerror_error_set(
5028
16
     error,
5029
16
     LIBCERROR_ERROR_DOMAIN_IO,
5030
16
     LIBCERROR_IO_ERROR_READ_FAILED,
5031
16
     "%s: unable to read binary XML sub token.",
5032
16
     function );
5033
5034
16
    goto on_error;
5035
16
  }
5036
76.9k
  if( libfwevt_xml_document_read_element(
5037
76.9k
       internal_xml_document,
5038
76.9k
       xml_sub_token,
5039
76.9k
       binary_data,
5040
76.9k
       binary_data_size,
5041
76.9k
       template_data_offset,
5042
76.9k
       ascii_codepage,
5043
76.9k
       flags,
5044
76.9k
       template_values_array,
5045
76.9k
       xml_tag,
5046
76.9k
       element_recursion_depth + 1,
5047
76.9k
       template_instance_recursion_depth,
5048
76.9k
       error ) != 1 )
5049
4.26k
  {
5050
4.26k
    libcerror_error_set(
5051
4.26k
     error,
5052
4.26k
     LIBCERROR_ERROR_DOMAIN_IO,
5053
4.26k
     LIBCERROR_IO_ERROR_READ_FAILED,
5054
4.26k
     "%s: unable to read element.",
5055
4.26k
     function );
5056
5057
4.26k
    goto on_error;
5058
4.26k
  }
5059
72.6k
  template_data_offset += xml_sub_token->size;
5060
5061
72.6k
  if( libfwevt_xml_token_read_data(
5062
72.6k
       xml_sub_token,
5063
72.6k
       binary_data,
5064
72.6k
       binary_data_size,
5065
72.6k
       template_data_offset,
5066
72.6k
       error ) != 1 )
5067
26
  {
5068
26
    libcerror_error_set(
5069
26
     error,
5070
26
     LIBCERROR_ERROR_DOMAIN_IO,
5071
26
     LIBCERROR_IO_ERROR_READ_FAILED,
5072
26
     "%s: unable to read binary XML sub token.",
5073
26
     function );
5074
5075
26
    goto on_error;
5076
26
  }
5077
72.6k
  if( xml_sub_token->type != LIBFWEVT_XML_TOKEN_END_OF_FILE )
5078
33
  {
5079
33
    libcerror_error_set(
5080
33
     error,
5081
33
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
5082
33
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
5083
33
     "%s: invalid binary XML token - unsupported type: 0x%02" PRIx8 ".",
5084
33
     function,
5085
33
     xml_token->type );
5086
5087
33
    goto on_error;
5088
33
  }
5089
72.5k
  if( binary_data_offset >= ( binary_data_size - 1 ) )
5090
0
  {
5091
0
    libcerror_error_set(
5092
0
     error,
5093
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
5094
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
5095
0
     "%s: invalid binary XML document data size value too small.",
5096
0
     function );
5097
5098
0
    goto on_error;
5099
0
  }
5100
#if defined( HAVE_DEBUG_OUTPUT )
5101
  if( libcnotify_verbose != 0 )
5102
  {
5103
    libcnotify_printf(
5104
     "%s: data offset\t\t: 0x%08" PRIzx "\n",
5105
     function,
5106
     binary_data_offset );
5107
5108
    libcnotify_printf(
5109
     "%s: end of file data:\n",
5110
     function );
5111
    libcnotify_print_data(
5112
     &( binary_data[ binary_data_offset ] ),
5113
     1,
5114
     0 );
5115
  }
5116
#endif
5117
#if defined( HAVE_DEBUG_OUTPUT )
5118
  if( libcnotify_verbose != 0 )
5119
  {
5120
    libcnotify_printf(
5121
     "%s: type\t\t\t: 0x%02" PRIx8 "\n",
5122
     function,
5123
     binary_data[ binary_data_offset ] );
5124
5125
    libcnotify_printf(
5126
     "\n" );
5127
  }
5128
#endif
5129
72.5k
  template_data_offset += 1;
5130
5131
72.5k
  if( libfwevt_xml_token_free(
5132
72.5k
       &xml_sub_token,
5133
72.5k
       error ) != 1 )
5134
0
  {
5135
0
    libcerror_error_set(
5136
0
     error,
5137
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
5138
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
5139
0
     "%s: unable to free binary XML sub token.",
5140
0
     function );
5141
5142
0
    goto on_error;
5143
0
  }
5144
72.5k
  if( template_definition_data_offset == binary_data_offset )
5145
2.28k
  {
5146
2.28k
    template_data_size = template_data_offset
5147
2.28k
                       - template_definition_data_offset;
5148
5149
2.28k
    xml_token->size += template_data_size;
5150
5151
    /* The template data size does not include the first 33 bytes
5152
     * of the template definition
5153
     * In this case the template data size contains 24 of the 33 bytes
5154
     */
5155
2.28k
    if( template_definition_data_size < ( template_data_size - 24 ) )
5156
1
    {
5157
1
      libcerror_error_set(
5158
1
       error,
5159
1
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
5160
1
       LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
5161
1
       "%s: invalid template definition data size value too small.",
5162
1
       function );
5163
5164
1
      goto on_error;
5165
1
    }
5166
/* TODO
5167
    template_definition_data_size -= (uint32_t) ( template_data_size - 24 );
5168
*/
5169
2.28k
  }
5170
/* TODO check if template_definition_data_size is 0 */
5171
5172
72.5k
  if( libcdata_array_free(
5173
72.5k
       &template_values_array,
5174
72.5k
       (int (*)(intptr_t **, libcerror_error_t **)) &libfwevt_xml_template_value_free,
5175
72.5k
       error ) != 1 )
5176
0
  {
5177
0
    libcerror_error_set(
5178
0
     error,
5179
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
5180
0
     LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
5181
0
     "%s: unable to free template values array.",
5182
0
     function );
5183
5184
0
    goto on_error;
5185
0
  }
5186
72.5k
  return( 1 );
5187
5188
6.13k
on_error:
5189
6.13k
  if( template_values_array != NULL )
5190
4.70k
  {
5191
4.70k
    libcdata_array_free(
5192
4.70k
     &template_values_array,
5193
4.70k
     (int (*)(intptr_t **, libcerror_error_t **)) &libfwevt_xml_template_value_free,
5194
4.70k
     NULL );
5195
4.70k
  }
5196
6.13k
  if( xml_sub_token != NULL )
5197
4.70k
  {
5198
4.70k
    libfwevt_xml_token_free(
5199
4.70k
     &xml_sub_token,
5200
4.70k
     NULL );
5201
4.70k
  }
5202
6.13k
  return( -1 );
5203
72.5k
}
5204
5205
/* Reads the template instance values from a binary XML document
5206
 * Returns 1 if successful or -1 on error
5207
 */
5208
int libfwevt_xml_document_read_template_instance_values(
5209
     libfwevt_internal_xml_document_t *internal_xml_document,
5210
     const uint8_t *binary_data,
5211
     size_t binary_data_size,
5212
     size_t binary_data_offset,
5213
     libcdata_array_t **template_values_array,
5214
     size_t *template_values_size,
5215
     libcerror_error_t **error )
5216
352k
{
5217
352k
  libfwevt_xml_template_value_t *template_value = NULL;
5218
352k
  static char *function                         = "libfwevt_xml_document_read_template_instance_values";
5219
352k
  size_t safe_template_values_size              = 0;
5220
352k
  size_t template_value_definitions_data_size   = 0;
5221
352k
  size_t template_values_data_size              = 0;
5222
352k
  uint32_t number_of_template_values            = 0;
5223
352k
  uint32_t template_value_index                 = 0;
5224
352k
  uint16_t template_value_data_size             = 0;
5225
352k
  uint8_t template_value_type                   = 0;
5226
5227
352k
  if( internal_xml_document == NULL )
5228
0
  {
5229
0
    libcerror_error_set(
5230
0
     error,
5231
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5232
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
5233
0
     "%s: invalid binary XML document.",
5234
0
     function );
5235
5236
0
    return( -1 );
5237
0
  }
5238
352k
  if( ( binary_data_size < 4 )
5239
352k
   || ( binary_data_offset >= ( binary_data_size - 4 ) ) )
5240
515
  {
5241
515
    libcerror_error_set(
5242
515
     error,
5243
515
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5244
515
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
5245
515
     "%s: invalid binary data size value out of bounds.",
5246
515
     function );
5247
5248
515
    return( -1 );
5249
515
  }
5250
352k
  if( template_values_size == NULL )
5251
0
  {
5252
0
    libcerror_error_set(
5253
0
     error,
5254
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5255
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
5256
0
     "%s: invalid template values size.",
5257
0
     function );
5258
5259
0
    return( -1 );
5260
0
  }
5261
#if defined( HAVE_DEBUG_OUTPUT )
5262
  if( libcnotify_verbose != 0 )
5263
  {
5264
    libcnotify_printf(
5265
     "%s: data offset\t: 0x%08" PRIzx "\n",
5266
     function,
5267
     binary_data_offset );
5268
5269
    libcnotify_printf(
5270
     "%s: template instance data:\n",
5271
     function );
5272
    libcnotify_print_data(
5273
     &( binary_data[ binary_data_offset ] ),
5274
     4,
5275
     0 );
5276
  }
5277
#endif
5278
352k
  byte_stream_copy_to_uint32_little_endian(
5279
352k
   &( binary_data[ binary_data_offset ] ),
5280
352k
   number_of_template_values );
5281
5282
#if defined( HAVE_DEBUG_OUTPUT )
5283
  if( libcnotify_verbose != 0 )
5284
  {
5285
    libcnotify_printf(
5286
     "%s: number of values\t: %" PRIu32 "\n",
5287
     function,
5288
     number_of_template_values );
5289
5290
    libcnotify_printf(
5291
     "\n" );
5292
  }
5293
#endif
5294
352k
  safe_template_values_size = 4;
5295
352k
  binary_data_offset       += 4;
5296
5297
352k
  template_value_definitions_data_size = number_of_template_values * 4;
5298
5299
352k
  if( ( template_value_definitions_data_size > binary_data_size )
5300
351k
   || ( binary_data_offset >= ( binary_data_size - template_value_definitions_data_size ) ) )
5301
753
  {
5302
753
    libcerror_error_set(
5303
753
     error,
5304
753
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5305
753
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
5306
753
     "%s: invalid template value definitions data size value out of bounds.",
5307
753
     function );
5308
5309
753
    goto on_error;
5310
753
  }
5311
#if defined( HAVE_DEBUG_OUTPUT )
5312
  if( libcnotify_verbose != 0 )
5313
  {
5314
    libcnotify_printf(
5315
     "%s: data offset\t: 0x%08" PRIzx "\n",
5316
     function,
5317
     binary_data_offset );
5318
5319
    libcnotify_printf(
5320
     "%s: template instance value descriptor data:\n",
5321
     function );
5322
    libcnotify_print_data(
5323
     &( binary_data[ binary_data_offset ] ),
5324
     template_value_definitions_data_size,
5325
     LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
5326
  }
5327
#endif
5328
351k
  if( libcdata_array_initialize(
5329
351k
       template_values_array,
5330
351k
       number_of_template_values,
5331
351k
       error ) != 1 )
5332
167
  {
5333
167
    libcerror_error_set(
5334
167
     error,
5335
167
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
5336
167
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
5337
167
     "%s: unable to create template values array.",
5338
167
     function );
5339
5340
167
    goto on_error;
5341
167
  }
5342
351k
  for( template_value_index = 0;
5343
15.5M
       template_value_index < number_of_template_values;
5344
15.2M
       template_value_index++ )
5345
15.2M
  {
5346
15.2M
    byte_stream_copy_to_uint16_little_endian(
5347
15.2M
     &( binary_data[ binary_data_offset ] ),
5348
15.2M
     template_value_data_size );
5349
5350
15.2M
    template_value_type = binary_data[ binary_data_offset + 2 ];
5351
5352
#if defined( HAVE_DEBUG_OUTPUT )
5353
    if( libcnotify_verbose != 0 )
5354
    {
5355
      libcnotify_printf(
5356
       "%s: value: %02" PRIu32 " size\t: %" PRIu16 "\n",
5357
       function,
5358
       template_value_index,
5359
       template_value_data_size );
5360
5361
      libcnotify_printf(
5362
       "%s: value: %02" PRIu32 " type\t: 0x%02" PRIx8 " (",
5363
       function,
5364
       template_value_index,
5365
       template_value_type );
5366
      libfwevt_debug_print_value_type(
5367
       template_value_type );
5368
      libcnotify_printf(
5369
       ")\n" );
5370
5371
      libcnotify_printf(
5372
       "%s: value: %02" PRIu32 " unknown1\t: 0x%02" PRIx8 "\n",
5373
       function,
5374
       template_value_index,
5375
       binary_data[ binary_data_offset + 3 ] );
5376
5377
      libcnotify_printf(
5378
       "\n" );
5379
    }
5380
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
5381
5382
15.2M
    safe_template_values_size += 4;
5383
15.2M
    binary_data_offset        += 4;
5384
5385
15.2M
    template_values_data_size += template_value_data_size;
5386
5387
15.2M
    if( libfwevt_xml_template_value_initialize(
5388
15.2M
         &template_value,
5389
15.2M
         error ) != 1 )
5390
0
    {
5391
0
      libcerror_error_set(
5392
0
       error,
5393
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
5394
0
       LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
5395
0
       "%s: unable to create template value.",
5396
0
       function );
5397
5398
0
      goto on_error;
5399
0
    }
5400
15.2M
    if( libfwevt_xml_template_value_set_type(
5401
15.2M
         template_value,
5402
15.2M
         template_value_type,
5403
15.2M
         error ) != 1 )
5404
0
    {
5405
0
      libcerror_error_set(
5406
0
       error,
5407
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
5408
0
       LIBCERROR_RUNTIME_ERROR_SET_FAILED,
5409
0
       "%s: unable to set template value type.",
5410
0
       function );
5411
5412
0
      libfwevt_xml_template_value_free(
5413
0
       &template_value,
5414
0
       NULL );
5415
5416
0
      goto on_error;
5417
0
    }
5418
15.2M
    if( libfwevt_xml_template_value_set_size(
5419
15.2M
         template_value,
5420
15.2M
         template_value_data_size,
5421
15.2M
         error ) != 1 )
5422
0
    {
5423
0
      libcerror_error_set(
5424
0
       error,
5425
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
5426
0
       LIBCERROR_RUNTIME_ERROR_SET_FAILED,
5427
0
       "%s: unable to set template value data size.",
5428
0
       function );
5429
5430
0
      libfwevt_xml_template_value_free(
5431
0
       &template_value,
5432
0
       NULL );
5433
5434
0
      goto on_error;
5435
0
    }
5436
15.2M
    if( libcdata_array_set_entry_by_index(
5437
15.2M
         *template_values_array,
5438
15.2M
         (int) template_value_index,
5439
15.2M
         (intptr_t *) template_value,
5440
15.2M
         error ) != 1 )
5441
0
    {
5442
0
      libcerror_error_set(
5443
0
       error,
5444
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
5445
0
       LIBCERROR_RUNTIME_ERROR_SET_FAILED,
5446
0
       "%s: unable to set template value: %" PRIu32 " in array.",
5447
0
       function,
5448
0
       template_value_index );
5449
5450
0
      libfwevt_xml_template_value_free(
5451
0
       &template_value,
5452
0
       NULL );
5453
5454
0
      goto on_error;
5455
0
    }
5456
15.2M
    template_value = NULL;
5457
15.2M
  }
5458
351k
  if( ( template_values_data_size > binary_data_size )
5459
350k
   || ( binary_data_offset >= ( binary_data_size - template_values_data_size ) ) )
5460
1.24k
  {
5461
1.24k
    libcerror_error_set(
5462
1.24k
     error,
5463
1.24k
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5464
1.24k
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
5465
1.24k
     "%s: invalid template values data size value out of bounds.",
5466
1.24k
     function );
5467
5468
1.24k
    goto on_error;
5469
1.24k
  }
5470
#if defined( HAVE_DEBUG_OUTPUT )
5471
  if( libcnotify_verbose != 0 )
5472
  {
5473
    libcnotify_printf(
5474
     "%s: data offset\t: 0x%08" PRIzx "\n",
5475
     function,
5476
     binary_data_offset );
5477
5478
    libcnotify_printf(
5479
     "%s: values data:\n",
5480
     function );
5481
    libcnotify_print_data(
5482
     &( binary_data[ binary_data_offset ] ),
5483
     template_values_data_size,
5484
     LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
5485
  }
5486
#endif
5487
349k
  for( template_value_index = 0;
5488
6.19M
       template_value_index < number_of_template_values;
5489
5.84M
       template_value_index++ )
5490
5.84M
  {
5491
5.84M
    if( libcdata_array_get_entry_by_index(
5492
5.84M
         *template_values_array,
5493
5.84M
         (int) template_value_index,
5494
5.84M
         (intptr_t **) &template_value,
5495
5.84M
         error ) != 1 )
5496
0
    {
5497
0
      libcerror_error_set(
5498
0
       error,
5499
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
5500
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
5501
0
       "%s: unable to retrieve template value: %" PRIu32 " from array.",
5502
0
       function,
5503
0
       template_value_index );
5504
5505
0
      goto on_error;
5506
0
    }
5507
5.84M
    if( libfwevt_xml_template_value_get_size(
5508
5.84M
         template_value,
5509
5.84M
         &template_value_data_size,
5510
5.84M
         error ) != 1 )
5511
0
    {
5512
0
      libcerror_error_set(
5513
0
       error,
5514
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
5515
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
5516
0
       "%s: unable to retrieve template value data size.",
5517
0
       function );
5518
5519
0
      goto on_error;
5520
0
    }
5521
#if defined( HAVE_DEBUG_OUTPUT )
5522
    if( libcnotify_verbose != 0 )
5523
    {
5524
      libcnotify_printf(
5525
       "%s: data offset\t: 0x%08" PRIzx "\n",
5526
       function,
5527
       binary_data_offset );
5528
5529
      libcnotify_printf(
5530
       "%s: value: %02" PRIu32 " data:\n",
5531
       function,
5532
       template_value_index );
5533
      libcnotify_print_data(
5534
       &( binary_data[ binary_data_offset ] ),
5535
       template_value_data_size,
5536
       LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
5537
    }
5538
#endif
5539
    /* Note that template_value_data_size is allowed to be 0.
5540
     * Don't set the template value offset in such a case.
5541
     */
5542
5.84M
    if( template_value_data_size == 0 )
5543
4.76M
    {
5544
4.76M
      continue;
5545
4.76M
    }
5546
1.08M
    if( libfwevt_xml_template_value_set_offset(
5547
1.08M
         template_value,
5548
1.08M
         binary_data_offset,
5549
1.08M
         error ) != 1 )
5550
0
    {
5551
0
      libcerror_error_set(
5552
0
       error,
5553
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
5554
0
       LIBCERROR_RUNTIME_ERROR_SET_FAILED,
5555
0
       "%s: unable to set template value data offset.",
5556
0
       function );
5557
5558
0
      goto on_error;
5559
0
    }
5560
1.08M
    binary_data_offset += template_value_data_size;
5561
1.08M
  }
5562
349k
  *template_values_size = safe_template_values_size + template_values_data_size;
5563
5564
349k
  return( 1 );
5565
5566
2.16k
on_error:
5567
2.16k
  if( template_values_array != NULL )
5568
2.16k
  {
5569
2.16k
    libcdata_array_free(
5570
2.16k
     template_values_array,
5571
2.16k
     (int (*)(intptr_t **, libcerror_error_t **)) &libfwevt_xml_template_value_free,
5572
2.16k
     NULL );
5573
2.16k
  }
5574
2.16k
  return( -1 );
5575
349k
}
5576
5577
/* Reads a value from a binary XML document
5578
 * Returns 1 if successful or -1 on error
5579
 */
5580
int libfwevt_xml_document_read_value(
5581
     libfwevt_internal_xml_document_t *internal_xml_document,
5582
     libfwevt_xml_token_t *xml_token,
5583
     const uint8_t *binary_data,
5584
     size_t binary_data_size,
5585
     size_t binary_data_offset,
5586
     libfwevt_xml_tag_t *xml_tag,
5587
     int ascii_codepage LIBFWEVT_ATTRIBUTE_UNUSED,
5588
     libcerror_error_t **error )
5589
2.08M
{
5590
2.08M
  const uint8_t *xml_document_data = NULL;
5591
2.08M
  static char *function            = "libfwevt_xml_document_read_value";
5592
2.08M
  size_t value_data_size           = 0;
5593
2.08M
  size_t xml_document_data_size    = 0;
5594
2.08M
  uint8_t value_type               = 0;
5595
2.08M
  int data_segment_index           = 0;
5596
5597
2.08M
  LIBFWEVT_UNREFERENCED_PARAMETER( ascii_codepage )
5598
5599
2.08M
  if( internal_xml_document == NULL )
5600
0
  {
5601
0
    libcerror_error_set(
5602
0
     error,
5603
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5604
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
5605
0
     "%s: invalid binary XML document.",
5606
0
     function );
5607
5608
0
    return( -1 );
5609
0
  }
5610
2.08M
  if( xml_token == NULL )
5611
0
  {
5612
0
    libcerror_error_set(
5613
0
     error,
5614
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5615
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
5616
0
     "%s: invalid binary XML token.",
5617
0
     function );
5618
5619
0
    return( -1 );
5620
0
  }
5621
2.08M
  if( ( xml_token->type & 0xbf ) != LIBFWEVT_XML_TOKEN_VALUE )
5622
0
  {
5623
0
    libcerror_error_set(
5624
0
     error,
5625
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
5626
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
5627
0
     "%s: invalid binary XML token - unsupported type: 0x%02" PRIx8 ".",
5628
0
     function,
5629
0
     xml_token->type );
5630
5631
0
    return( -1 );
5632
0
  }
5633
2.08M
  if( binary_data == NULL )
5634
0
  {
5635
0
    libcerror_error_set(
5636
0
     error,
5637
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5638
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
5639
0
     "%s: invalid binary data.",
5640
0
     function );
5641
5642
0
    return( -1 );
5643
0
  }
5644
2.08M
  if( binary_data_size > (size_t) SSIZE_MAX )
5645
0
  {
5646
0
    libcerror_error_set(
5647
0
     error,
5648
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5649
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
5650
0
     "%s: invalid binary XML document data size value exceeds maximum.",
5651
0
     function );
5652
5653
0
    return( -1 );
5654
0
  }
5655
2.08M
  if( binary_data_offset >= binary_data_size )
5656
0
  {
5657
0
    libcerror_error_set(
5658
0
     error,
5659
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5660
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
5661
0
     "%s: invalid binary data offset value out of bounds.",
5662
0
     function );
5663
5664
0
    return( -1 );
5665
0
  }
5666
2.08M
  if( xml_tag == NULL )
5667
0
  {
5668
0
    libcerror_error_set(
5669
0
     error,
5670
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5671
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
5672
0
     "%s: invalid XML tag.",
5673
0
     function );
5674
5675
0
    return( -1 );
5676
0
  }
5677
2.08M
  xml_document_data      = &( binary_data[ binary_data_offset ] );
5678
2.08M
  xml_document_data_size = binary_data_size - binary_data_offset;
5679
5680
2.08M
  if( xml_document_data_size < 4 )
5681
156
  {
5682
156
    libcerror_error_set(
5683
156
     error,
5684
156
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
5685
156
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
5686
156
     "%s: invalid binary XML document data size value too small.",
5687
156
     function );
5688
5689
156
    return( -1 );
5690
156
  }
5691
#if defined( HAVE_DEBUG_OUTPUT )
5692
  if( libcnotify_verbose != 0 )
5693
  {
5694
    libcnotify_printf(
5695
     "%s: data offset\t\t\t\t: 0x%08" PRIzx "\n",
5696
     function,
5697
     binary_data_offset );
5698
5699
    libcnotify_printf(
5700
     "%s: value data:\n",
5701
     function );
5702
    libcnotify_print_data(
5703
     xml_document_data,
5704
     4,
5705
     0 );
5706
  }
5707
#endif
5708
2.08M
  value_type = xml_document_data[ 1 ];
5709
5710
#if defined( HAVE_DEBUG_OUTPUT )
5711
  if( libcnotify_verbose != 0 )
5712
  {
5713
    libcnotify_printf(
5714
     "%s: type\t\t\t\t\t: 0x%02" PRIx8 "\n",
5715
     function,
5716
     xml_document_data[ 0 ] );
5717
5718
    libcnotify_printf(
5719
     "%s: value type\t\t\t\t: 0x%02" PRIx8 " (",
5720
     function,
5721
     value_type );
5722
    libfwevt_debug_print_value_type(
5723
     value_type );
5724
    libcnotify_printf(
5725
     ")\n" );
5726
  }
5727
#endif
5728
2.08M
  xml_token->size     = 4;
5729
2.08M
  binary_data_offset += 4;
5730
5731
2.08M
  switch( value_type )
5732
2.08M
  {
5733
2.08M
    case LIBFWEVT_VALUE_TYPE_STRING_UTF16:
5734
2.08M
      byte_stream_copy_to_uint16_little_endian(
5735
2.08M
       &( xml_document_data[ 2 ] ),
5736
2.08M
       value_data_size );
5737
5738
#if defined( HAVE_DEBUG_OUTPUT )
5739
      if( libcnotify_verbose != 0 )
5740
      {
5741
        libcnotify_printf(
5742
         "%s: number of characters\t\t\t: %" PRIzd "\n",
5743
         function,
5744
         value_data_size );
5745
      }
5746
#endif
5747
2.08M
      value_data_size *= 2;
5748
5749
2.08M
      break;
5750
5751
177
    default:
5752
177
      libcerror_error_set(
5753
177
       error,
5754
177
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
5755
177
       LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
5756
177
       "%s: unsupported value type: 0x%02" PRIx8 ".",
5757
177
       function,
5758
177
       value_type );
5759
5760
177
      return( -1 );
5761
2.08M
  }
5762
2.08M
  if( ( value_data_size > binary_data_size )
5763
2.08M
   || ( binary_data_offset >= ( binary_data_size - value_data_size ) ) )
5764
591
  {
5765
591
    libcerror_error_set(
5766
591
     error,
5767
591
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5768
591
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
5769
591
     "%s: invalid value data size value out of bounds.",
5770
591
     function );
5771
5772
591
    return( -1 );
5773
591
  }
5774
#if defined( HAVE_DEBUG_OUTPUT )
5775
  if( libcnotify_verbose != 0 )
5776
  {
5777
    libcnotify_printf(
5778
     "%s: data offset\t\t\t\t: 0x%08" PRIzx "\n",
5779
     function,
5780
     binary_data_offset );
5781
5782
    libcnotify_printf(
5783
     "%s: value data:\n",
5784
     function );
5785
    libcnotify_print_data(
5786
     &( binary_data[ binary_data_offset ] ),
5787
     value_data_size,
5788
     0 );
5789
  }
5790
#endif
5791
2.08M
  if( libfwevt_xml_tag_set_value_type(
5792
2.08M
       xml_tag,
5793
2.08M
       value_type,
5794
2.08M
       error ) != 1 )
5795
26
  {
5796
26
    libcerror_error_set(
5797
26
     error,
5798
26
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
5799
26
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
5800
26
     "%s: unable to set value type.",
5801
26
     function );
5802
5803
26
    return( -1 );
5804
26
  }
5805
2.08M
  if( libfwevt_xml_tag_append_value_data(
5806
2.08M
       xml_tag,
5807
2.08M
       &( binary_data[ binary_data_offset ] ),
5808
2.08M
       value_data_size,
5809
2.08M
       &data_segment_index,
5810
2.08M
       error ) != 1 )
5811
0
  {
5812
0
    libcerror_error_set(
5813
0
     error,
5814
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
5815
0
     LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
5816
0
     "%s: unable to append value data.",
5817
0
     function );
5818
5819
0
    return( -1 );
5820
0
  }
5821
#if defined( HAVE_DEBUG_OUTPUT )
5822
  if( libcnotify_verbose != 0 )
5823
  {
5824
    if( libfwevt_xml_tag_debug_print_value_data_segment(
5825
         xml_tag,
5826
         data_segment_index,
5827
         0,
5828
         ascii_codepage,
5829
         error ) != 1 )
5830
    {
5831
      libcerror_error_set(
5832
       error,
5833
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
5834
       LIBCERROR_RUNTIME_ERROR_PRINT_FAILED,
5835
       "%s: unable to print value data segment: %d.",
5836
       function,
5837
       data_segment_index );
5838
5839
      return( -1 );
5840
    }
5841
  }
5842
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
5843
5844
2.08M
  xml_token->size += value_data_size;
5845
5846
2.08M
  return( 1 );
5847
2.08M
}
5848
5849
/* Substitutes a substitution placeholder with a template value
5850
 * Returns 1 if successful, 0 if no substitution or -1 on error
5851
 */
5852
int libfwevt_xml_document_substitute_template_value(
5853
     libfwevt_internal_xml_document_t *internal_xml_document,
5854
     const uint8_t *binary_data,
5855
     size_t binary_data_size,
5856
     int ascii_codepage,
5857
     uint8_t flags,
5858
     libcdata_array_t *template_values_array,
5859
     uint16_t template_value_index,
5860
     uint8_t template_value_type,
5861
     size_t *template_value_offset,
5862
     libfwevt_xml_tag_t *xml_tag,
5863
     int element_recursion_depth,
5864
     int template_instance_recursion_depth,
5865
     libcerror_error_t **error )
5866
8.25M
{
5867
8.25M
  libfwevt_xml_template_value_t *template_value = NULL;
5868
8.25M
  libfwevt_xml_token_t *xml_sub_token           = NULL;
5869
8.25M
  const uint8_t *template_value_data            = NULL;
5870
8.25M
  static char *function                         = "libfwevt_xml_document_substitute_template_value";
5871
8.25M
  size_t binary_data_offset                     = 0;
5872
8.25M
  size_t safe_template_value_offset             = 0;
5873
8.25M
  size_t template_value_data_offset             = 0;
5874
8.25M
  size_t template_value_data_size               = 0;
5875
8.25M
  size_t template_value_size                    = 0;
5876
8.25M
  uint16_t substitution_value_data_size         = 0;
5877
8.25M
  uint8_t substitution_value_type               = 0;
5878
8.25M
  uint8_t template_value_flags                  = 0;
5879
5880
8.25M
  if( internal_xml_document == NULL )
5881
0
  {
5882
0
    libcerror_error_set(
5883
0
     error,
5884
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5885
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
5886
0
     "%s: invalid binary XML document.",
5887
0
     function );
5888
5889
0
    return( -1 );
5890
0
  }
5891
8.25M
  if( template_value_offset == NULL )
5892
0
  {
5893
0
    libcerror_error_set(
5894
0
     error,
5895
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5896
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
5897
0
     "%s: invalid template value offset.",
5898
0
     function );
5899
5900
0
    return( -1 );
5901
0
  }
5902
8.25M
  if( xml_tag == NULL )
5903
0
  {
5904
0
    libcerror_error_set(
5905
0
     error,
5906
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5907
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
5908
0
     "%s: invalid XML tag.",
5909
0
     function );
5910
5911
0
    return( -1 );
5912
0
  }
5913
8.25M
  if( libcdata_array_get_entry_by_index(
5914
8.25M
       template_values_array,
5915
8.25M
       (int) template_value_index,
5916
8.25M
       (intptr_t **) &template_value,
5917
8.25M
       error ) != 1 )
5918
969
  {
5919
969
    libcerror_error_set(
5920
969
     error,
5921
969
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
5922
969
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
5923
969
     "%s: unable to retrieve template value: %" PRIu16 " from array.",
5924
969
     function,
5925
969
     template_value_index );
5926
5927
969
    goto on_error;
5928
969
  }
5929
8.25M
  if( libfwevt_xml_template_value_get_flags(
5930
8.25M
       template_value,
5931
8.25M
       &template_value_flags,
5932
8.25M
       error ) != 1 )
5933
0
  {
5934
0
    libcerror_error_set(
5935
0
     error,
5936
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
5937
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
5938
0
     "%s: unable to retrieve template value flags.",
5939
0
     function );
5940
5941
0
    goto on_error;
5942
0
  }
5943
8.25M
  if( ( template_value_flags & LIBFWEVT_XML_TEMPLATE_VALUE_FLAG_IS_DEFINITION ) != 0 )
5944
3.97k
  {
5945
3.97k
    substitution_value_type = LIBFWEVT_VALUE_TYPE_STRING_UTF16;
5946
3.97k
  }
5947
8.25M
  else
5948
8.25M
  {
5949
8.25M
    if( libfwevt_xml_template_value_get_type(
5950
8.25M
         template_value,
5951
8.25M
         &substitution_value_type,
5952
8.25M
         error ) != 1 )
5953
0
    {
5954
0
      libcerror_error_set(
5955
0
       error,
5956
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
5957
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
5958
0
       "%s: unable to retrieve template value type.",
5959
0
       function );
5960
5961
0
      goto on_error;
5962
0
    }
5963
8.25M
  }
5964
8.25M
  if( libfwevt_xml_template_value_get_offset(
5965
8.25M
       template_value,
5966
8.25M
       &binary_data_offset,
5967
8.25M
       error ) != 1 )
5968
0
  {
5969
0
    libcerror_error_set(
5970
0
     error,
5971
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
5972
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
5973
0
     "%s: unable to retrieve template value data offset.",
5974
0
     function );
5975
5976
0
    goto on_error;
5977
0
  }
5978
8.25M
  if( libfwevt_xml_template_value_get_size(
5979
8.25M
       template_value,
5980
8.25M
       &substitution_value_data_size,
5981
8.25M
       error ) != 1 )
5982
0
  {
5983
0
    libcerror_error_set(
5984
0
     error,
5985
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
5986
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
5987
0
     "%s: unable to retrieve template value data size.",
5988
0
     function );
5989
5990
0
    goto on_error;
5991
0
  }
5992
#if defined( HAVE_DEBUG_OUTPUT )
5993
  if( libcnotify_verbose != 0 )
5994
  {
5995
    libcnotify_printf(
5996
     "%s: value: %02" PRIu32 " offset\t: 0x%08" PRIzx "\n",
5997
     function,
5998
     template_value_index,
5999
     binary_data_offset );
6000
6001
    libcnotify_printf(
6002
     "%s: value: %02" PRIu32 " size\t\t: %" PRIu16 "\n",
6003
     function,
6004
     template_value_index,
6005
     substitution_value_data_size );
6006
6007
    libcnotify_printf(
6008
     "%s: value: %02" PRIu32 " type\t\t: 0x%02" PRIx8 " (",
6009
     function,
6010
     template_value_index,
6011
     substitution_value_type );
6012
    libfwevt_debug_print_value_type(
6013
     substitution_value_type );
6014
    libcnotify_printf(
6015
     ")\n" );
6016
6017
    libcnotify_printf(
6018
     "%s: value: %02" PRIu32 " data:\n",
6019
     function,
6020
     template_value_index );
6021
    libcnotify_print_data(
6022
     &( binary_data[ binary_data_offset ] ),
6023
     substitution_value_data_size,
6024
     LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
6025
6026
    libcnotify_printf(
6027
     "\n" );
6028
  }
6029
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
6030
6031
  /* No substitution
6032
   */
6033
8.25M
  if( substitution_value_type == LIBFWEVT_VALUE_TYPE_NULL )
6034
282k
  {
6035
282k
    *template_value_offset = 0;
6036
6037
282k
    return( 0 );
6038
282k
  }
6039
#if defined( HAVE_VERBOSE_OUTPUT )
6040
  if( template_value_type != substitution_value_type )
6041
  {
6042
    /* The size type can be substituded by a 32-bit or 64-bit hexadecimal integer
6043
     * The same applies to it's array type
6044
     */
6045
    if( ( ( template_value_type & 0x7f ) != LIBFWEVT_VALUE_TYPE_SIZE )
6046
     || ( ( ( substitution_value_type & 0x7f ) != LIBFWEVT_VALUE_TYPE_HEXADECIMAL_INTEGER_32BIT )
6047
      &&  ( ( substitution_value_type & 0x7f ) != LIBFWEVT_VALUE_TYPE_HEXADECIMAL_INTEGER_64BIT ) ) )
6048
    {
6049
      if( libcnotify_verbose != 0 )
6050
      {
6051
        libcnotify_printf(
6052
         "%s: mismatch in value type ( 0x%02" PRIx8 " != 0x%02" PRIx8 " ).\n",
6053
         function,
6054
         template_value_type,
6055
         substitution_value_type );
6056
      }
6057
    }
6058
  }
6059
#endif /* defined( HAVE_VERBOSE_OUTPUT ) */
6060
6061
7.97M
  if( substitution_value_type == LIBFWEVT_VALUE_TYPE_BINARY_XML )
6062
307k
  {
6063
307k
    if( libfwevt_xml_token_initialize(
6064
307k
         &xml_sub_token,
6065
307k
         error ) != 1 )
6066
0
    {
6067
0
      libcerror_error_set(
6068
0
       error,
6069
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
6070
0
       LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
6071
0
       "%s: unable to create binary XML sub token.",
6072
0
       function );
6073
6074
0
      goto on_error;
6075
0
    }
6076
307k
    if( libfwevt_xml_token_read_data(
6077
307k
         xml_sub_token,
6078
307k
         binary_data,
6079
307k
         binary_data_size,
6080
307k
         binary_data_offset,
6081
307k
         error ) != 1 )
6082
81
    {
6083
81
      libcerror_error_set(
6084
81
       error,
6085
81
       LIBCERROR_ERROR_DOMAIN_IO,
6086
81
       LIBCERROR_IO_ERROR_READ_FAILED,
6087
81
       "%s: unable to read binary XML sub token.",
6088
81
       function );
6089
6090
81
      goto on_error;
6091
81
    }
6092
307k
    switch( xml_sub_token->type & 0xbf )
6093
307k
    {
6094
44.0k
      case LIBFWEVT_XML_TOKEN_OPEN_START_ELEMENT_TAG:
6095
44.0k
        if( libfwevt_xml_document_read_element(
6096
44.0k
             internal_xml_document,
6097
44.0k
             xml_sub_token,
6098
44.0k
             binary_data,
6099
44.0k
             binary_data_size,
6100
44.0k
             binary_data_offset,
6101
44.0k
             ascii_codepage,
6102
44.0k
             flags,
6103
44.0k
             template_values_array,
6104
44.0k
             xml_tag,
6105
44.0k
             element_recursion_depth + 1,
6106
44.0k
             template_instance_recursion_depth,
6107
44.0k
             error ) != 1 )
6108
43.3k
        {
6109
43.3k
          libcerror_error_set(
6110
43.3k
           error,
6111
43.3k
           LIBCERROR_ERROR_DOMAIN_IO,
6112
43.3k
           LIBCERROR_IO_ERROR_READ_FAILED,
6113
43.3k
           "%s: unable to read element.",
6114
43.3k
           function );
6115
6116
43.3k
          goto on_error;
6117
43.3k
        }
6118
670
        break;
6119
6120
61.4k
      case LIBFWEVT_XML_TOKEN_FRAGMENT_HEADER:
6121
61.4k
        if( libfwevt_xml_document_read_fragment(
6122
61.4k
             internal_xml_document,
6123
61.4k
             xml_sub_token,
6124
61.4k
             binary_data,
6125
61.4k
             binary_data_size,
6126
61.4k
             binary_data_offset,
6127
61.4k
             ascii_codepage,
6128
61.4k
             flags,
6129
61.4k
             NULL,
6130
61.4k
             xml_tag,
6131
61.4k
             element_recursion_depth,
6132
61.4k
             template_instance_recursion_depth,
6133
61.4k
             error ) != 1 )
6134
15.7k
        {
6135
15.7k
          libcerror_error_set(
6136
15.7k
           error,
6137
15.7k
           LIBCERROR_ERROR_DOMAIN_IO,
6138
15.7k
           LIBCERROR_IO_ERROR_READ_FAILED,
6139
15.7k
           "%s: unable to read fragment header.",
6140
15.7k
           function );
6141
6142
15.7k
          goto on_error;
6143
15.7k
        }
6144
45.7k
        break;
6145
6146
201k
      case LIBFWEVT_XML_TOKEN_TEMPLATE_INSTANCE:
6147
201k
        if( libfwevt_xml_document_read_template_instance(
6148
201k
             internal_xml_document,
6149
201k
             xml_sub_token,
6150
201k
             binary_data,
6151
201k
             binary_data_size,
6152
201k
             binary_data_offset,
6153
201k
             ascii_codepage,
6154
201k
             flags,
6155
201k
             xml_tag,
6156
201k
             element_recursion_depth,
6157
201k
             template_instance_recursion_depth + 1,
6158
201k
             error ) != 1 )
6159
1.14k
        {
6160
1.14k
          libcerror_error_set(
6161
1.14k
           error,
6162
1.14k
           LIBCERROR_ERROR_DOMAIN_IO,
6163
1.14k
           LIBCERROR_IO_ERROR_READ_FAILED,
6164
1.14k
           "%s: unable to read document template instance.",
6165
1.14k
           function );
6166
6167
1.14k
          goto on_error;
6168
1.14k
        }
6169
200k
        break;
6170
6171
200k
      default:
6172
157
        libcerror_error_set(
6173
157
         error,
6174
157
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
6175
157
         LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
6176
157
         "%s: invalid binary XML token - unsupported type: 0x%02" PRIx8 ".",
6177
157
         function,
6178
157
         xml_sub_token->type );
6179
6180
157
        goto on_error;
6181
307k
    }
6182
246k
    if( libfwevt_xml_token_free(
6183
246k
         &xml_sub_token,
6184
246k
         error ) != 1 )
6185
0
    {
6186
0
      libcerror_error_set(
6187
0
       error,
6188
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
6189
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
6190
0
       "%s: unable to free binary XML sub token.",
6191
0
       function );
6192
6193
0
      goto on_error;
6194
0
    }
6195
246k
  }
6196
7.66M
  else
6197
7.66M
  {
6198
/* TODO for now the array types are kept separate to find undocumented values
6199
 * when done apply ( substitution_value_type & 0x7f ) and remove the array types
6200
 */
6201
7.66M
    switch( substitution_value_type )
6202
7.66M
    {
6203
92.9k
      case LIBFWEVT_VALUE_TYPE_STRING_UTF16:
6204
191k
      case LIBFWEVT_VALUE_TYPE_ARRAY_OF_STRING_UTF16:
6205
191k
        break;
6206
6207
20.1k
      case LIBFWEVT_VALUE_TYPE_STRING_BYTE_STREAM:
6208
1.94M
      case LIBFWEVT_VALUE_TYPE_ARRAY_OF_STRING_BYTE_STREAM:
6209
1.94M
        break;
6210
6211
20.4k
      case LIBFWEVT_VALUE_TYPE_INTEGER_8BIT:
6212
106k
      case LIBFWEVT_VALUE_TYPE_ARRAY_OF_INTEGER_8BIT:
6213
106k
        template_value_size = 1;
6214
106k
        break;
6215
6216
271k
      case LIBFWEVT_VALUE_TYPE_UNSIGNED_INTEGER_8BIT:
6217
319k
      case LIBFWEVT_VALUE_TYPE_ARRAY_OF_UNSIGNED_INTEGER_8BIT:
6218
319k
        template_value_size = 1;
6219
319k
        break;
6220
6221
10.0k
      case LIBFWEVT_VALUE_TYPE_INTEGER_16BIT:
6222
98.4k
      case LIBFWEVT_VALUE_TYPE_ARRAY_OF_INTEGER_16BIT:
6223
98.4k
        template_value_size = 2;
6224
98.4k
        break;
6225
6226
214k
      case LIBFWEVT_VALUE_TYPE_UNSIGNED_INTEGER_16BIT:
6227
245k
      case LIBFWEVT_VALUE_TYPE_ARRAY_OF_UNSIGNED_INTEGER_16BIT:
6228
245k
        template_value_size = 2;
6229
245k
        break;
6230
6231
948
      case LIBFWEVT_VALUE_TYPE_INTEGER_32BIT:
6232
6.15k
      case LIBFWEVT_VALUE_TYPE_ARRAY_OF_INTEGER_32BIT:
6233
6.15k
        template_value_size = 4;
6234
6.15k
        break;
6235
6236
133k
      case LIBFWEVT_VALUE_TYPE_UNSIGNED_INTEGER_32BIT:
6237
529k
      case LIBFWEVT_VALUE_TYPE_ARRAY_OF_UNSIGNED_INTEGER_32BIT:
6238
529k
        template_value_size = 4;
6239
529k
        break;
6240
6241
983
      case LIBFWEVT_VALUE_TYPE_HEXADECIMAL_INTEGER_32BIT:
6242
6.19k
      case LIBFWEVT_VALUE_TYPE_ARRAY_OF_HEXADECIMAL_INTEGER_32BIT:
6243
6.19k
        template_value_size = 4;
6244
6.19k
        break;
6245
6246
11.8k
      case LIBFWEVT_VALUE_TYPE_INTEGER_64BIT:
6247
22.4k
      case LIBFWEVT_VALUE_TYPE_ARRAY_OF_INTEGER_64BIT:
6248
22.4k
        template_value_size = 8;
6249
22.4k
        break;
6250
6251
40.8k
      case LIBFWEVT_VALUE_TYPE_UNSIGNED_INTEGER_64BIT:
6252
52.4k
      case LIBFWEVT_VALUE_TYPE_ARRAY_OF_UNSIGNED_INTEGER_64BIT:
6253
52.4k
        template_value_size = 8;
6254
52.4k
        break;
6255
6256
74.1k
      case LIBFWEVT_VALUE_TYPE_HEXADECIMAL_INTEGER_64BIT:
6257
172k
      case LIBFWEVT_VALUE_TYPE_ARRAY_OF_HEXADECIMAL_INTEGER_64BIT:
6258
172k
        template_value_size = 8;
6259
172k
        break;
6260
6261
2.02k
      case LIBFWEVT_VALUE_TYPE_FLOATING_POINT_32BIT:
6262
284k
      case LIBFWEVT_VALUE_TYPE_ARRAY_OF_FLOATING_POINT_32BIT:
6263
284k
        template_value_size = 4;
6264
284k
        break;
6265
6266
2.62k
      case LIBFWEVT_VALUE_TYPE_FLOATING_POINT_64BIT:
6267
189k
      case LIBFWEVT_VALUE_TYPE_ARRAY_OF_FLOATING_POINT_64BIT:
6268
189k
        template_value_size = 8;
6269
189k
        break;
6270
6271
3.56k
      case LIBFWEVT_VALUE_TYPE_BOOLEAN:
6272
3.56k
        template_value_size = 4;
6273
3.56k
        break;
6274
6275
26.2k
      case LIBFWEVT_VALUE_TYPE_BINARY_DATA:
6276
26.2k
        break;
6277
6278
1.47k
      case LIBFWEVT_VALUE_TYPE_GUID:
6279
9.90k
      case LIBFWEVT_VALUE_TYPE_ARRAY_OF_GUID:
6280
9.90k
        template_value_size = 16;
6281
9.90k
        break;
6282
6283
/* TODO how to deal with array types ? */
6284
4.20k
      case LIBFWEVT_VALUE_TYPE_SIZE:
6285
4.20k
        if( ( substitution_value_data_size != 4 )
6286
1.91k
         && ( substitution_value_data_size != 8 ) )
6287
73
        {
6288
73
          libcerror_error_set(
6289
73
           error,
6290
73
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
6291
73
           LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
6292
73
           "%s: unsupported value data size: %" PRIu16 ".",
6293
73
           function,
6294
73
           substitution_value_data_size );
6295
6296
73
          goto on_error;
6297
73
        }
6298
4.13k
        break;
6299
6300
83.9k
      case LIBFWEVT_VALUE_TYPE_FILETIME:
6301
3.37M
      case LIBFWEVT_VALUE_TYPE_ARRAY_OF_FILETIME:
6302
3.37M
        template_value_size = 8;
6303
3.37M
        break;
6304
6305
4.10k
      case LIBFWEVT_VALUE_TYPE_SYSTEMTIME:
6306
76.7k
      case LIBFWEVT_VALUE_TYPE_ARRAY_OF_SYSTEMTIME:
6307
76.7k
        template_value_size = 16;
6308
76.7k
        break;
6309
6310
10.7k
      case LIBFWEVT_VALUE_TYPE_NT_SECURITY_IDENTIFIER:
6311
10.7k
        break;
6312
6313
123
      default:
6314
123
        libcerror_error_set(
6315
123
         error,
6316
123
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
6317
123
         LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
6318
123
         "%s: unsupported value type: 0x%02" PRIx8 ".",
6319
123
         function,
6320
123
         substitution_value_type );
6321
6322
123
        goto on_error;
6323
7.66M
    }
6324
7.66M
    if( libfwevt_xml_tag_set_value_type(
6325
7.66M
         xml_tag,
6326
7.66M
         substitution_value_type,
6327
7.66M
         error ) != 1 )
6328
51
    {
6329
51
      libcerror_error_set(
6330
51
       error,
6331
51
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
6332
51
       LIBCERROR_RUNTIME_ERROR_SET_FAILED,
6333
51
       "%s: unable to set value type.",
6334
51
       function );
6335
6336
51
      goto on_error;
6337
51
    }
6338
7.66M
    if( ( substitution_value_type & LIBFWEVT_VALUE_TYPE_ARRAY ) != 0 )
6339
6.63M
    {
6340
6.63M
      safe_template_value_offset = *template_value_offset;
6341
6342
6.63M
      if( substitution_value_data_size > 0 )
6343
6.62M
      {
6344
6.62M
        if( safe_template_value_offset >= (size_t) substitution_value_data_size )
6345
135
        {
6346
135
          libcerror_error_set(
6347
135
           error,
6348
135
           LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
6349
135
           LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
6350
135
           "%s: invalid template value offset value out of bounds.",
6351
135
           function );
6352
6353
135
          goto on_error;
6354
135
        }
6355
6.62M
        template_value_data      = &( binary_data[ binary_data_offset + safe_template_value_offset ] );
6356
6.62M
        template_value_data_size = substitution_value_data_size - (uint16_t) safe_template_value_offset;
6357
6.62M
      }
6358
6.63M
      if( template_value_data_size > 0 )
6359
6.62M
      {
6360
#if defined( HAVE_DEBUG_OUTPUT )
6361
        if( libcnotify_verbose != 0 )
6362
        {
6363
          libcnotify_printf(
6364
           "%s: template value data:\n",
6365
           function );
6366
          libcnotify_print_data(
6367
           template_value_data,
6368
           template_value_data_size,
6369
           LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
6370
        }
6371
#endif
6372
6.62M
        if( substitution_value_type == LIBFWEVT_VALUE_TYPE_ARRAY_OF_STRING_BYTE_STREAM )
6373
1.91M
        {
6374
1.91M
          if( template_value_data_size < 1 )
6375
0
          {
6376
0
            libcerror_error_set(
6377
0
             error,
6378
0
             LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
6379
0
             LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
6380
0
             "%s: invalid byte string template value data size value out of bounds.",
6381
0
             function );
6382
6383
0
            goto on_error;
6384
0
          }
6385
4.87M
          while( template_value_data_offset < template_value_data_size )
6386
4.84M
          {
6387
4.84M
            if( template_value_data[ template_value_data_offset ] == 0 )
6388
1.88M
            {
6389
1.88M
              template_value_data_offset += 1;
6390
6391
1.88M
              break;
6392
1.88M
            }
6393
2.95M
            template_value_data_offset += 1;
6394
2.95M
          }
6395
1.91M
          template_value_data_size = template_value_data_offset;
6396
1.91M
        }
6397
4.70M
        else if( substitution_value_type == LIBFWEVT_VALUE_TYPE_ARRAY_OF_STRING_UTF16 )
6398
94.4k
        {
6399
94.4k
          if( ( template_value_data_size < 2 )
6400
40
           && ( ( template_value_data_size % 2 ) != 0 ) )
6401
40
          {
6402
40
            libcerror_error_set(
6403
40
             error,
6404
40
             LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
6405
40
             LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
6406
40
             "%s: invalid UTF-16 template value data size value out of bounds.",
6407
40
             function );
6408
6409
40
            goto on_error;
6410
40
          }
6411
16.4M
          while( template_value_data_offset < template_value_data_size )
6412
16.4M
          {
6413
16.4M
            if( ( template_value_data[ template_value_data_offset ] == 0 )
6414
204k
             && ( template_value_data[ template_value_data_offset + 1 ] == 0 ) )
6415
90.5k
            {
6416
90.5k
              template_value_data_offset += 2;
6417
6418
90.5k
              break;
6419
90.5k
            }
6420
16.3M
            template_value_data_offset += 2;
6421
16.3M
          }
6422
94.4k
          template_value_data_size = template_value_data_offset;
6423
94.4k
        }
6424
4.61M
        else
6425
4.61M
        {
6426
4.61M
          if( template_value_size > template_value_data_size )
6427
558
          {
6428
558
            libcerror_error_set(
6429
558
             error,
6430
558
             LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
6431
558
             LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
6432
558
             "%s: invalid template value size value out of bounds.",
6433
558
             function );
6434
6435
558
            goto on_error;
6436
558
          }
6437
4.61M
          template_value_data_size = template_value_size;
6438
4.61M
        }
6439
6.62M
      }
6440
      /* An empty XML tag should be created if template_value_data_size == 0
6441
       */
6442
/* TODO create empty XML tag if template value data is an empty string */
6443
6.63M
      if( template_value_data_size > 0 )
6444
6.62M
      {
6445
6.62M
        if( libfwevt_xml_tag_set_value_data(
6446
6.62M
             xml_tag,
6447
6.62M
             template_value_data,
6448
6.62M
             template_value_data_size,
6449
6.62M
             error ) != 1 )
6450
0
        {
6451
0
          libcerror_error_set(
6452
0
           error,
6453
0
           LIBCERROR_ERROR_DOMAIN_RUNTIME,
6454
0
           LIBCERROR_RUNTIME_ERROR_SET_FAILED,
6455
0
           "%s: unable to set value data.",
6456
0
           function );
6457
6458
0
          goto on_error;
6459
0
        }
6460
6.62M
        safe_template_value_offset += template_value_data_size;
6461
6.62M
      }
6462
6.63M
      if( safe_template_value_offset == substitution_value_data_size )
6463
173k
      {
6464
173k
        safe_template_value_offset = 0;
6465
173k
      }
6466
6.63M
    }
6467
1.03M
    else
6468
1.03M
    {
6469
1.03M
      if( ( template_value_size != 0 )
6470
876k
       && ( template_value_size != substitution_value_data_size ) )
6471
305
      {
6472
305
        libcerror_error_set(
6473
305
         error,
6474
305
         LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
6475
305
         LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
6476
305
         "%s: invalid substitution value data size value out of bounds.",
6477
305
         function );
6478
6479
305
        goto on_error;
6480
305
      }
6481
1.03M
      else if( ( substitution_value_type == LIBFWEVT_VALUE_TYPE_STRING_UTF16 )
6482
92.9k
            && ( ( substitution_value_data_size % 2 ) != 0 ) )
6483
31
      {
6484
31
        libcerror_error_set(
6485
31
         error,
6486
31
         LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
6487
31
         LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
6488
31
         "%s: invalid UTF-16 substitution value data size value out of bounds.",
6489
31
         function );
6490
6491
31
        goto on_error;
6492
31
      }
6493
1.03M
      if( libfwevt_xml_tag_set_value_data(
6494
1.03M
           xml_tag,
6495
1.03M
           &( binary_data[ binary_data_offset ] ),
6496
1.03M
           (size_t) substitution_value_data_size,
6497
1.03M
           error ) != 1 )
6498
0
      {
6499
0
        libcerror_error_set(
6500
0
         error,
6501
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
6502
0
         LIBCERROR_RUNTIME_ERROR_SET_FAILED,
6503
0
         "%s: unable to set value data.",
6504
0
         function );
6505
6506
0
        goto on_error;
6507
0
      }
6508
1.03M
    }
6509
7.66M
    if( libfwevt_xml_tag_set_flags(
6510
7.66M
         xml_tag,
6511
7.66M
         LIBFWEVT_XML_TAG_FLAG_IS_TEMPLATE_DEFINITION,
6512
7.66M
         error ) != 1 )
6513
0
    {
6514
0
      libcerror_error_set(
6515
0
       error,
6516
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
6517
0
       LIBCERROR_RUNTIME_ERROR_SET_FAILED,
6518
0
       "%s: unable to set flags.",
6519
0
       function );
6520
6521
0
      goto on_error;
6522
0
    }
6523
#if defined( HAVE_DEBUG_OUTPUT )
6524
    if( libcnotify_verbose != 0 )
6525
    {
6526
      if( libfwevt_xml_tag_debug_print_value_data_segment(
6527
           xml_tag,
6528
           0,
6529
           0,
6530
           ascii_codepage,
6531
           error ) != 1 )
6532
      {
6533
        libcerror_error_set(
6534
         error,
6535
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
6536
         LIBCERROR_RUNTIME_ERROR_PRINT_FAILED,
6537
         "%s: unable to print value data segment: 0.",
6538
         function );
6539
6540
        goto on_error;
6541
      }
6542
    }
6543
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
6544
7.66M
  }
6545
7.91M
  *template_value_offset = safe_template_value_offset;
6546
6547
7.91M
  return( 1 );
6548
6549
62.7k
on_error:
6550
62.7k
  if( xml_sub_token != NULL )
6551
60.4k
  {
6552
60.4k
    libfwevt_xml_token_free(
6553
60.4k
     &xml_sub_token,
6554
60.4k
     NULL );
6555
60.4k
  }
6556
62.7k
  return( -1 );
6557
7.97M
}
6558
6559
/* Retrieves the size of the UTF-8 formatted string of the XML document
6560
 * Returns 1 if successful or -1 on error
6561
 */
6562
int libfwevt_xml_document_get_utf8_xml_string_size(
6563
     libfwevt_xml_document_t *xml_document,
6564
     size_t *utf8_string_size,
6565
     libcerror_error_t **error )
6566
56.1k
{
6567
56.1k
  libfwevt_internal_xml_document_t *internal_xml_document = NULL;
6568
56.1k
  static char *function                                   = "libfwevt_xml_document_get_utf8_xml_string_size";
6569
6570
56.1k
  if( xml_document == NULL )
6571
0
  {
6572
0
    libcerror_error_set(
6573
0
     error,
6574
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
6575
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
6576
0
     "%s: invalid XML document.",
6577
0
     function );
6578
6579
0
    return( -1 );
6580
0
  }
6581
56.1k
  internal_xml_document = (libfwevt_internal_xml_document_t *) xml_document;
6582
6583
/* TODO pass codepage */
6584
56.1k
  if( libfwevt_xml_tag_get_utf8_xml_string_size(
6585
56.1k
       internal_xml_document->root_xml_tag,
6586
56.1k
       0,
6587
56.1k
       utf8_string_size,
6588
56.1k
       LIBUNA_CODEPAGE_WINDOWS_1252,
6589
56.1k
       error ) != 1 )
6590
34.1k
  {
6591
34.1k
    libcerror_error_set(
6592
34.1k
     error,
6593
34.1k
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
6594
34.1k
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
6595
34.1k
     "%s: unable to retrieve size of UTF-8 string of root XML tag.",
6596
34.1k
     function );
6597
6598
34.1k
    return( -1 );
6599
34.1k
  }
6600
22.0k
  return( 1 );
6601
56.1k
}
6602
6603
/* Retrieves the UTF-8 formatted string of the XML document
6604
 * Returns 1 if successful or -1 on error
6605
 */
6606
int libfwevt_xml_document_get_utf8_xml_string(
6607
     libfwevt_xml_document_t *xml_document,
6608
     uint8_t *utf8_string,
6609
     size_t utf8_string_size,
6610
     libcerror_error_t **error )
6611
21.9k
{
6612
21.9k
  libfwevt_internal_xml_document_t *internal_xml_document = NULL;
6613
21.9k
  static char *function                                   = "libfwevt_xml_document_get_utf8_xml_string";
6614
21.9k
  size_t utf8_string_index                                = 0;
6615
6616
21.9k
  if( xml_document == NULL )
6617
0
  {
6618
0
    libcerror_error_set(
6619
0
     error,
6620
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
6621
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
6622
0
     "%s: invalid XML document.",
6623
0
     function );
6624
6625
0
    return( -1 );
6626
0
  }
6627
21.9k
  internal_xml_document = (libfwevt_internal_xml_document_t *) xml_document;
6628
6629
/* TODO pass codepage */
6630
21.9k
  if( libfwevt_xml_tag_get_utf8_xml_string_with_index(
6631
21.9k
       internal_xml_document->root_xml_tag,
6632
21.9k
       0,
6633
21.9k
       utf8_string,
6634
21.9k
       utf8_string_size,
6635
21.9k
       &utf8_string_index,
6636
21.9k
       LIBUNA_CODEPAGE_WINDOWS_1252,
6637
21.9k
       error ) != 1 )
6638
1.18k
  {
6639
1.18k
    libcerror_error_set(
6640
1.18k
     error,
6641
1.18k
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
6642
1.18k
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
6643
1.18k
     "%s: unable to retrieve UTF-8 string of root XML tag.",
6644
1.18k
     function );
6645
6646
1.18k
    return( -1 );
6647
1.18k
  }
6648
20.8k
  return( 1 );
6649
21.9k
}
6650
6651
/* Retrieves the size of the UTF-16 formatted string of the XML document
6652
 * Returns 1 if successful or -1 on error
6653
 */
6654
int libfwevt_xml_document_get_utf16_xml_string_size(
6655
     libfwevt_xml_document_t *xml_document,
6656
     size_t *utf16_string_size,
6657
     libcerror_error_t **error )
6658
50.1k
{
6659
50.1k
  libfwevt_internal_xml_document_t *internal_xml_document = NULL;
6660
50.1k
  static char *function                                   = "libfwevt_xml_document_get_utf16_xml_string_size";
6661
6662
50.1k
  if( xml_document == NULL )
6663
0
  {
6664
0
    libcerror_error_set(
6665
0
     error,
6666
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
6667
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
6668
0
     "%s: invalid XML document.",
6669
0
     function );
6670
6671
0
    return( -1 );
6672
0
  }
6673
50.1k
  internal_xml_document = (libfwevt_internal_xml_document_t *) xml_document;
6674
6675
/* TODO pass codepage */
6676
50.1k
  if( libfwevt_xml_tag_get_utf16_xml_string_size(
6677
50.1k
       internal_xml_document->root_xml_tag,
6678
50.1k
       0,
6679
50.1k
       utf16_string_size,
6680
50.1k
       LIBUNA_CODEPAGE_WINDOWS_1252,
6681
50.1k
       error ) != 1 )
6682
33.7k
  {
6683
33.7k
    libcerror_error_set(
6684
33.7k
     error,
6685
33.7k
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
6686
33.7k
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
6687
33.7k
     "%s: unable to retrieve size of UTF-16 string of root XML tag.",
6688
33.7k
     function );
6689
6690
33.7k
    return( -1 );
6691
33.7k
  }
6692
16.4k
  return( 1 );
6693
50.1k
}
6694
6695
/* Retrieves the UTF-16 formatted string of the XML document
6696
 * Returns 1 if successful or -1 on error
6697
 */
6698
int libfwevt_xml_document_get_utf16_xml_string(
6699
     libfwevt_xml_document_t *xml_document,
6700
     uint16_t *utf16_string,
6701
     size_t utf16_string_size,
6702
     libcerror_error_t **error )
6703
16.4k
{
6704
16.4k
  libfwevt_internal_xml_document_t *internal_xml_document = NULL;
6705
16.4k
  static char *function                                   = "libfwevt_xml_document_get_utf16_xml_string";
6706
16.4k
  size_t utf16_string_index                               = 0;
6707
6708
16.4k
  if( xml_document == NULL )
6709
0
  {
6710
0
    libcerror_error_set(
6711
0
     error,
6712
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
6713
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
6714
0
     "%s: invalid XML document.",
6715
0
     function );
6716
6717
0
    return( -1 );
6718
0
  }
6719
16.4k
  internal_xml_document = (libfwevt_internal_xml_document_t *) xml_document;
6720
6721
/* TODO pass codepage */
6722
16.4k
  if( libfwevt_xml_tag_get_utf16_xml_string_with_index(
6723
16.4k
       internal_xml_document->root_xml_tag,
6724
16.4k
       0,
6725
16.4k
       utf16_string,
6726
16.4k
       utf16_string_size,
6727
16.4k
       &utf16_string_index,
6728
16.4k
       LIBUNA_CODEPAGE_WINDOWS_1252,
6729
16.4k
       error ) != 1 )
6730
1.16k
  {
6731
1.16k
    libcerror_error_set(
6732
1.16k
     error,
6733
1.16k
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
6734
1.16k
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
6735
1.16k
     "%s: unable to retrieve UTF-16 string of root XML tag.",
6736
1.16k
     function );
6737
6738
1.16k
    return( -1 );
6739
1.16k
  }
6740
15.2k
  return( 1 );
6741
16.4k
}
6742
6743
#if defined( HAVE_DEBUG_OUTPUT )
6744
6745
/* Debug prints the XML document
6746
 * Returns 1 if successful or -1 on error
6747
 */
6748
int libfwevt_xml_document_debug_print(
6749
     libfwevt_xml_document_t *xml_document,
6750
     libcerror_error_t **error )
6751
{
6752
  libfwevt_internal_xml_document_t *internal_xml_document = NULL;
6753
  static char *function                                   = "libfwevt_xml_document_print";
6754
6755
  if( xml_document == NULL )
6756
  {
6757
    libcerror_error_set(
6758
     error,
6759
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
6760
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
6761
     "%s: invalid binary XML document.",
6762
     function );
6763
6764
    return( -1 );
6765
  }
6766
  internal_xml_document = (libfwevt_internal_xml_document_t *) xml_document;
6767
6768
/* TODO pass codepage */
6769
  if( libfwevt_xml_tag_debug_print(
6770
       internal_xml_document->root_xml_tag,
6771
       0,
6772
       LIBUNA_CODEPAGE_WINDOWS_1252,
6773
       error ) != 1 )
6774
  {
6775
    libcerror_error_set(
6776
     error,
6777
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
6778
     LIBCERROR_RUNTIME_ERROR_PRINT_FAILED,
6779
     "%s: unable to print root XML tag.",
6780
     function );
6781
6782
    return( -1 );
6783
  }
6784
  return( 1 );
6785
}
6786
6787
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
6788