-
Notifications
You must be signed in to change notification settings - Fork 89
/
XmlTreeLib.c
1816 lines (1538 loc) · 50.2 KB
/
XmlTreeLib.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
@file
Implementation of XmlTreeLib using the faster Xml lib modified to work in UEFI.
Copyright (C) Microsoft Corporation.
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include <Uefi.h> // UEFI base types
#include <Library/MemoryAllocationLib.h> // Memory allocation
#include <Library/BaseLib.h> // Safe string functions
#include <Library/DebugLib.h> // DEBUG tracing
#include <Library/BaseMemoryLib.h> // SetMem()
#include <XmlTypes.h>
#include <Library/XmlTreeLib.h> // Our header
#include "fasterxml/fasterxml.h" // XML Engine
#include "fasterxml/xmlerr.h" // XML errors
#ifndef fDoOnce
#define fDoOnce FALSE
#endif // fDoOnce
#ifndef MAX_PATH
#define MAX_PATH (260)
#endif // MAX_PATH
#ifndef ARRAYSIZE
#define ARRAYSIZE(A) sizeof(A)/sizeof(A[0])
#endif // ARRAYSIZE
// DEFINE the max number of nodes deep the parser will support
#define MAX_RECURSIVE_LEVEL (25)
//
// Private function prototypes
//
UINTN
_GetXmlUnEscapedLength (
IN CONST CHAR8 *String,
IN UINTN MaxStringLength
);
UINTN
_GetXmlEscapedLength (
IN CONST CHAR8 *String,
IN UINTN MaxStringLength
);
/**
Given a character, determine if it is white space.
ch -- Character to test.
TRUE if ch == whitespace.
**/
BOOLEAN
EFIAPI
IsWhiteSpace (
IN CHAR8 ch
)
{
BOOLEAN fIsWhiteSpace = FALSE;
switch (ch) {
case '\0':
case '\r':
case ' ':
case '\t':
case '\n':
{
fIsWhiteSpace = TRUE;
break;
}
}
return fIsWhiteSpace;
}// IsWhiteSpace()
/**
Function to safely free a buffer. If
the buffer is NULL then just return.
**/
VOID
SafeFreeBuffer (
CHAR8 **ppBuff
)
{
if (ppBuff && *ppBuff) {
FreePool (*ppBuff);
*ppBuff = NULL;
}
}// SafeFreeBuffer()
//
// Public functions
//
/**
This function creates a new XML tree.
@param[in] Parent -- Optional parent for this node.
@param[in] Name -- Name for this node.
@param[in] Value -- Optional value for this node.
@param[out] Node -- Optional return pointer for this node.
@return EFI_SUCCESS or underlying failure code.
**/
EFI_STATUS
EFIAPI
AddNode (
IN XmlNode *Parent OPTIONAL,
IN CONST CHAR8 *Name,
IN CONST CHAR8 *Value OPTIONAL,
OUT XmlNode **Node OPTIONAL
)
{
EFI_STATUS Status = EFI_SUCCESS;
CHAR8 *NodeValue = NULL;
CHAR8 *NodeName = NULL;
XmlNode *NodeTemp = NULL;
do {
if ((Name == NULL) || (AsciiStrLen (Name) == 0)) {
DEBUG ((EFI_D_ERROR, "ERROR: AddNode(), pszName or length was NULL\n"));
Status = EFI_INVALID_PARAMETER;
break;
}
if (Node) {
*Node = NULL;
}
NodeTemp = (XmlNode *)AllocateZeroPool (sizeof (XmlNode));
if (NodeTemp == NULL) {
Status = EFI_OUT_OF_RESOURCES;
break;
}
NodeName = (CHAR8 *)AllocateZeroPool (AsciiStrLen (Name) + 1);
if (NodeName == NULL) {
Status = EFI_OUT_OF_RESOURCES;
break;
}
Status = AsciiStrCpyS (NodeName, AsciiStrLen (Name) + 1, Name);
if (EFI_ERROR (Status)) {
break;
}
if (Value && (*Value != '\0')) {
Status = XmlUnEscape (Value, XML_MAX_ELEMENT_VALUE_LENGTH, &NodeValue);
// NodeValue = AllocateZeroPool(AsciiStrLen(Value) + 1);
if (EFI_ERROR (Status)) {
break;
}
}
//
// Fill in the node names and values...
//
NodeTemp->ParentNode = Parent;
NodeTemp->Name = NodeName;
NodeTemp->Value = NodeValue;
//
// Initialize our list head entries.
//
InitializeListHead (&(NodeTemp->ChildrenListHead));
InitializeListHead (&(NodeTemp->AttributesListHead));
//
// If we have a parent, we need to add this node to the Parent's child list.
//
if (Parent) {
//
// Add the node to the parent's child list.
//
InsertTailList (&(Parent->ChildrenListHead), &NodeTemp->Link);
//
// Increase the number of child nodes that the parent owns.
//
Parent->NumChildren++;
}
//
// Let the caller have the node pointer now.
//
if (Node) {
*Node = NodeTemp;
}
} while (fDoOnce);
//
// Cleanup on error...
//
if (EFI_ERROR (Status)) {
//
// SafeFreeBuffer() only frees the memory if the pointer is not NULL.
// It then sets the pointer to null.
//
SafeFreeBuffer ((CHAR8 **)&NodeTemp);
SafeFreeBuffer (&NodeName);
SafeFreeBuffer (&NodeValue);
}
return Status;
}// AddNode()
/**
This function adds an existing tree to a parent node.
@param Parent -- Parent to add to.
@param Tree -- Existing Tree to to add to the parent as a child.
@return EFI_SUCCESS or underlying failure code.
**/
EFI_STATUS
EFIAPI
AddChildTree (
IN XmlNode *Parent,
IN XmlNode *Tree
)
{
EFI_STATUS Status = EFI_SUCCESS;
do {
if ((Parent == NULL) || (Tree == NULL)) {
Status = EFI_INVALID_PARAMETER;
break;
}
//
// Add the node to the parent's child list.
//
InsertTailList (&(Parent->ChildrenListHead), &Tree->Link);
//
// Increase the number of child nodes that the parent owns.
//
Parent->NumChildren++;
//
// Set the node's new parent...
//
Tree->ParentNode = Parent;
} while (fDoOnce);
return Status;
}// AddChildNodeList()
/**
This adds an attribute to an existing XmlNode.
@param Parent -- Parent for this node.
@param Name -- Name for this node.
@param Value -- Optional value for this node.
@return EFI_SUCCESS or underlying failure code.
**/
EFI_STATUS
EFIAPI
AddAttributeToNode (
IN XmlNode *Parent,
IN CONST CHAR8 *Name,
IN CONST CHAR8 *Value
)
{
EFI_STATUS Status = EFI_SUCCESS;
CHAR8 *AsciiString = NULL;
XmlAttribute *Attribute = NULL;
do {
if ((Parent == NULL) || (Name == NULL) || (AsciiStrLen (Name) == 0) || (Value == NULL) || (AsciiStrLen (Value) == 0)) {
DEBUG ((EFI_D_ERROR, "ERROR: AddAttributeToNode(), invalid parameter\n"));
Status = EFI_INVALID_PARAMETER;
break;
}
//
// Allocate the attribute structure
//
Attribute = (XmlAttribute *)AllocateZeroPool (sizeof (XmlAttribute));
if (Attribute == NULL) {
Status = EFI_OUT_OF_RESOURCES;
break;
}
//
// Allocate and store the name...
//
AsciiString = (CHAR8 *)AllocateZeroPool (AsciiStrLen (Name) + 1);
if (AsciiString == NULL) {
Status = EFI_OUT_OF_RESOURCES;
break;
}
Status = AsciiStrCpyS (AsciiString, AsciiStrLen (Name) + 1, Name);
if (EFI_ERROR (Status)) {
break;
}
Attribute->Name = AsciiString;
//
// Allocate and store the value...
//
AsciiString = NULL;
Status = XmlUnEscape (Value, XML_MAX_ATTRIBUTE_VALUE_LENGTH, &AsciiString);
if (EFI_ERROR (Status)) {
break;
}
Attribute->Value = AsciiString;
//
// Add the node to the parent's child list and increase the number of
// attributes within this node.
//
InsertTailList (&(Parent->AttributesListHead), &(Attribute->Link));
Parent->NumAttributes++;
Attribute->Parent = Parent;
} while (fDoOnce);
//
// Cleanup on error...
//
if (EFI_ERROR (Status)) {
//
// SafeFreeBuffer() only frees the memory if the pointer is not NULL.
// It then sets the pointer to null.
//
if (Attribute) {
SafeFreeBuffer ((CHAR8 **)&Attribute->Name);
SafeFreeBuffer ((CHAR8 **)&Attribute->Value);
SafeFreeBuffer ((CHAR8 **)&Attribute);
}
}
return Status;
}// AddAttributeToNode()
/**
This function frees the string resources associated with the node,
and removes it from it's parent node list if it has one.
@param Node -- Node to free.
@return EFI_SUCCESS or underlying failure code.
**/
EFI_STATUS
EFIAPI
DeleteNode (
IN XmlNode *Node
)
{
EFI_STATUS Status = EFI_SUCCESS;
LIST_ENTRY *Link = NULL;
// check for null
if (Node == NULL) {
return EFI_INVALID_PARAMETER;
}
// delete any children - can't use for loop because removal breaks iterator
while (!IsListEmpty (&Node->ChildrenListHead)) {
Link = GetFirstNode (&Node->ChildrenListHead);
Status = DeleteNode ((XmlNode *)Link);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "%a Failed to delete node. Status = %r\n", __FUNCTION__, Status));
ASSERT_EFI_ERROR (Status);
}
// Now remove it from our children list
RemoveEntryList (Link);
Node->NumChildren--;
FreePool (Link);
}
// all children gone....
// now remove all attributes - can't use for loop because removal breaks iterator
while (!IsListEmpty (&Node->AttributesListHead)) {
Link = GetFirstNode (&Node->AttributesListHead);
Status = DeleteAttribute ((XmlAttribute *)Link);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "%a Failed to delete attribute. Status = %r\n", __FUNCTION__, Status));
ASSERT_EFI_ERROR (Status);
}
// now remove from Attribute list
RemoveEntryList (Link);
Node->NumAttributes--;
FreePool (Link);
}// go to next attribute
// now free our node memory
SafeFreeBuffer (&(Node->XmlDeclaration.Declaration));
SafeFreeBuffer (&(Node->Name));
SafeFreeBuffer (&(Node->Value));
Node->ParentNode = NULL;
return Status;
}// DeleteNode()
/**
This function frees the string resources associated with the attribute,
and removes it from it's parent attribute list.
@param Attribute -- Attribute to free.
@return EFI_SUCCESS or underlying failure code.
**/
EFI_STATUS
EFIAPI
DeleteAttribute (
IN XmlAttribute *Attribute
)
{
EFI_STATUS Status = EFI_SUCCESS;
if (Attribute == NULL) {
return EFI_INVALID_PARAMETER;
}
SafeFreeBuffer (&(Attribute->Name));
SafeFreeBuffer (&(Attribute->Value));
Attribute->Parent = NULL;
return Status;
}// DeleteAttribute()
/**
Function to calculate the size of the Ascii string needed
to print this XmlNode and its children.
This uses a shortened XML format, minimal whitespace, and xml encoded strings
This is an internal function. Public function is CalculateXmlDocSize
**/
EFI_STATUS
EFIAPI
_CaclSizeRecursively (
IN CONST XmlNode *Node,
IN BOOLEAN Escaped,
OUT UINTN *Size,
IN UINTN Level
)
{
XmlAttribute *Att = NULL;
LIST_ENTRY *Link = NULL;
UINTN NameSize = 0;
EFI_STATUS Status = EFI_SUCCESS;
if ((Node == NULL) || (Size == NULL)) {
return EFI_INVALID_PARAMETER;
}
if (Level > MAX_RECURSIVE_LEVEL) {
DEBUG ((DEBUG_ERROR, "!!!ERROR: BAD XML. Allowable recursive depth exceeded.\n"));
return EFI_BAD_BUFFER_SIZE;
}
/* handle the xml decl*/
if (Node->XmlDeclaration.Declaration != NULL) {
if (Node->ParentNode != NULL) {
DEBUG ((DEBUG_ERROR, "!!!ERROR: BAD XML. Should not have XmlDeclaration for a non-root node\n"));
}
*Size = (*Size) + AsciiStrLen (Node->XmlDeclaration.Declaration);
}
/* Handle start tag*/
NameSize = AsciiStrLen (Node->Name);
*Size = (*Size) + NameSize + 1; // '<'
// Loop attributes
for (Link = Node->AttributesListHead.ForwardLink; Link != &(Node->AttributesListHead); Link = Link->ForwardLink) {
Att = (XmlAttribute *)Link;
*Size = (*Size) + AsciiStrLen (Att->Name) + 4; // '=', ' ','"','"'
if (Escaped) {
*Size = (*Size) + _GetXmlEscapedLength (Att->Value, XML_MAX_ATTRIBUTE_VALUE_LENGTH);
} else {
*Size = (*Size) + AsciiStrLen (Att->Value);
}
}
// handle children and ending
if ((Node->Value == NULL) && (Node->NumChildren == 0)) {
// Special short cut on the node - Use empty node notation />
*Size = (*Size) + 3; // ' ', '/', '>'
} else {
// longer notation
*Size = (*Size) + 1; // '>'
// Show Value if value
if (Node->Value != NULL) {
if (Escaped) {
*Size = (*Size) + _GetXmlEscapedLength (Node->Value, XML_MAX_ELEMENT_VALUE_LENGTH);
} else {
*Size = (*Size) + AsciiStrLen (Node->Value);
}
}
// Process all children
if (Node->NumChildren > 0) {
UINTN child = 0; // use for debugging only
// loop children
for (Link = Node->ChildrenListHead.ForwardLink; Link != &(Node->ChildrenListHead); Link = Link->ForwardLink, child++) {
Status = _CaclSizeRecursively ((CONST XmlNode *)Link, Escaped, Size, Level+1);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "%a - Error Status from child index %d of element: %a\n", __FUNCTION__, child, Node->Name));
return Status;
}
}
} // end children loop
*Size = (*Size) + NameSize + 3; // '<', '/', '>/
} // end of node
return EFI_SUCCESS;
}
/**
Function to calculate the size of the Ascii string needed
to print this XmlNode and its children. Generally assumed it will
be the root node.
This uses a shortened XML format and uses minimal whitespace.
@param Node - The root node of the xml tree to start printing from
@param Size - the number of Ascii characters in the string. This does not include the NULL terminator
**/
EFI_STATUS
EFIAPI
CalculateXmlDocSize (
IN CONST XmlNode *Node,
IN BOOLEAN Escaped,
OUT UINTN *Size
)
{
if ((Node == NULL) || (Size == NULL)) {
return EFI_INVALID_PARAMETER;
}
if (Node->ParentNode != NULL) {
DEBUG ((DEBUG_WARN, "%a - Called with node other than root node. Siblings will not be traversed.\n", __FUNCTION__));
}
*Size = 0;
return _CaclSizeRecursively (Node, Escaped, Size, 0);
}
/**
Internal function to convert an Xml Node and its children into an Ascii string
using shortened Xml Notation and no whitespace.
Public function is XmlTreeToString
**/
EFI_STATUS
EFIAPI
_ToStringRecursively (
IN CONST XmlNode *Node,
IN UINTN BufferSize,
IN OUT CHAR8 *String,
IN UINTN Level,
IN BOOLEAN Escaped
)
{
XmlAttribute *Att = NULL;
LIST_ENTRY *Link = NULL;
EFI_STATUS Status = EFI_SUCCESS;
if ((Node == NULL) || (String == NULL)) {
return EFI_INVALID_PARAMETER;
}
if (Level > MAX_RECURSIVE_LEVEL) {
DEBUG ((DEBUG_ERROR, "!!!ERROR: BAD XML. Allowable recursive depth exceeded.\n"));
return EFI_BAD_BUFFER_SIZE;
}
/* handle the xml decl*/
if (Node->XmlDeclaration.Declaration != NULL) {
if (Node->ParentNode != NULL) {
DEBUG ((DEBUG_ERROR, "!!!ERROR: BAD XML. Should not have XmlDeclaration for a non-root node\n"));
}
Status = AsciiStrCatS (String, BufferSize, Node->XmlDeclaration.Declaration);
if (EFI_ERROR (Status)) {
goto EXIT;
}
}
/* Handle start tag*/
Status = AsciiStrCatS (String, BufferSize, "<");
if (EFI_ERROR (Status)) {
goto EXIT;
}
Status = AsciiStrCatS (String, BufferSize, Node->Name);
if (EFI_ERROR (Status)) {
goto EXIT;
}
// Loop attributes
for (Link = Node->AttributesListHead.ForwardLink; Link != &(Node->AttributesListHead); Link = Link->ForwardLink) {
Att = (XmlAttribute *)Link;
Status = AsciiStrCatS (String, BufferSize, " ");
if (EFI_ERROR (Status)) {
goto EXIT;
}
Status = AsciiStrCatS (String, BufferSize, Att->Name);
if (EFI_ERROR (Status)) {
goto EXIT;
}
Status = AsciiStrCatS (String, BufferSize, "=\"");
if (EFI_ERROR (Status)) {
goto EXIT;
}
if (Escaped) {
CHAR8 *EscapedString = NULL;
Status = XmlEscape (Att->Value, XML_MAX_ATTRIBUTE_VALUE_LENGTH, &EscapedString);
if (EFI_ERROR (Status)) {
goto EXIT;
}
Status = AsciiStrCatS (String, BufferSize, EscapedString);
SafeFreeBuffer (&EscapedString);
} else {
Status = AsciiStrCatS (String, BufferSize, Att->Value);
}
if (EFI_ERROR (Status)) {
goto EXIT;
}
Status = AsciiStrCatS (String, BufferSize, "\"");
if (EFI_ERROR (Status)) {
goto EXIT;
}
}
// handle children and ending
if ((Node->Value == NULL) && (Node->NumChildren == 0)) {
// Special short cut on the node - Use empty node notation />
Status = AsciiStrCatS (String, BufferSize, " />");
if (EFI_ERROR (Status)) {
goto EXIT;
}
} else {
// longer notation
Status = AsciiStrCatS (String, BufferSize, ">");
if (EFI_ERROR (Status)) {
goto EXIT;
}
// Show Value if value
if (Node->Value != NULL) {
if (Escaped) {
CHAR8 *EscapedString = NULL;
Status = XmlEscape (Node->Value, XML_MAX_ELEMENT_VALUE_LENGTH, &EscapedString);
if (EFI_ERROR (Status)) {
goto EXIT;
}
Status = AsciiStrCatS (String, BufferSize, EscapedString);
SafeFreeBuffer (&EscapedString);
} else {
Status = AsciiStrCatS (String, BufferSize, Node->Value);
}
if (EFI_ERROR (Status)) {
goto EXIT;
}
}
// Process all children
if (Node->NumChildren > 0) {
UINTN child = 0; // use for debugging only
// loop children
for (Link = Node->ChildrenListHead.ForwardLink; Link != &(Node->ChildrenListHead); Link = Link->ForwardLink, child++) {
Status = _ToStringRecursively ((CONST XmlNode *)Link, BufferSize, String, Level+1, Escaped);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "%a - Error Status from child index %d of element: %a\n", __FUNCTION__, child, Node->Name));
goto EXIT;
}
}
} // end children loop
Status = AsciiStrCatS (String, BufferSize, "</");
if (EFI_ERROR (Status)) {
goto EXIT;
}
Status = AsciiStrCatS (String, BufferSize, Node->Name);
if (EFI_ERROR (Status)) {
goto EXIT;
}
Status = AsciiStrCatS (String, BufferSize, ">");
if (EFI_ERROR (Status)) {
goto EXIT;
}
} // end of node
EXIT:
return Status;
}
/**
Public function to create an ascii string from an xml node tree.
This will use shortened XML notation and no whitespace. (ideal for data transfer)
@param[in] Node - Root node or first node to start printing.
@param Escaped - Should the Xml be escaped. Generally this should be true
@param[out] BufferSize - Number of bytes that the string needed. Includes Null terminator
@param[out] String - Pointer to pointer of ASCII buffer. Will be set for return to caller. Needs to be freed using FreePool when finished
**/
EFI_STATUS
EFIAPI
XmlTreeToString (
IN CONST XmlNode *Node,
IN BOOLEAN Escaped,
OUT UINTN *BufferSize,
OUT CHAR8 **String
)
{
EFI_STATUS Status;
UINTN Size = 0;
CHAR8 *XmlString = NULL;
if ((Node == NULL) || (BufferSize == NULL) || (String == NULL)) {
return EFI_INVALID_PARAMETER;
}
if (Node->ParentNode != NULL) {
DEBUG ((DEBUG_WARN, "%a - Called with node other than root node. Siblings will not be traversed.\n", __FUNCTION__));
}
Status = CalculateXmlDocSize (Node, Escaped, &Size);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "%a - Error in CalculateXmlDocSize %r\n", __FUNCTION__, Status));
return Status;
}
Size++; // for the NULL char
DEBUG ((DEBUG_INFO, "%a - Pre Calculated Size of string is 0x%X\n", __FUNCTION__, Size));
XmlString = (CHAR8 *)AllocatePool (Size);
if (XmlString == NULL) {
DEBUG ((DEBUG_ERROR, "Failed to allocate string for XML"));
return EFI_OUT_OF_RESOURCES;
}
*XmlString = '\0'; // set to null for ascii str cat
Status = _ToStringRecursively (Node, Size, XmlString, 0, Escaped);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "%a - Failed to convert xml node tree into string. %r\n", __FUNCTION__, Status));
return Status;
}
DEBUG ((DEBUG_INFO, "%a - Pre Calculated Length of string is 0x%X. AsciiStrLen is 0x%X\n", __FUNCTION__, (Size - 1), AsciiStrLen (XmlString)));
*String = XmlString;
*BufferSize = Size;
return EFI_SUCCESS;
}
/**
Engine parsing code which will build a XmlNode for the XmlTree
*Internal Function
@param[in] XmlDocument -- XML document.
@param[in] XmlDocumentSize -- Size of the XML document.
@param[in out] Root -- Pointer to receive the node list.
Return Value:
EFI_SUCCESS or underlying failure code.
**/
EFI_STATUS
EFIAPI
BuildNodeList (
IN CONST CHAR8 *XmlDocument,
IN UINTN XmlDocumentSize,
IN OUT XmlNode **Root
)
{
EFI_STATUS Status = EFI_INVALID_PARAMETER;
UINTN EncodingLength = 0;
XmlNode *CurrentNode = NULL;
CHAR8 *XmlDeclaration = NULL;
CHAR8 *StartDoc = NULL;
UINT64 ProcessedCharacters = 0;
BOOLEAN ProcessedNode = FALSE;
XML_TOKENIZATION_STATE State;
XML_TOKENIZATION_INIT Init;
XML_LINE_AND_COLUMN Location;
CHAR8 Element[MAX_PATH];
CHAR8 HyperSpace[MAX_PATH];
CHAR8 EndElement[MAX_PATH];
CHAR8 AttributeName[MAX_PATH];
CHAR8 AttributeValue[MAX_PATH];
//
// Zero everthing out to start.
//
ZeroMem (&State, sizeof (State));
ZeroMem (&Init, sizeof (Init));
ZeroMem (&Location, sizeof (Location));
ZeroMem (Element, ARRAYSIZE (Element));
ZeroMem (HyperSpace, ARRAYSIZE (HyperSpace));
ZeroMem (EndElement, ARRAYSIZE (EndElement));
ZeroMem (AttributeName, ARRAYSIZE (AttributeName));
ZeroMem (AttributeValue, ARRAYSIZE (AttributeValue));
//
// Initialize the XML engine.
//
Init.Size = sizeof (Init);
Init.XmlData = (VOID *)XmlDocument;
Init.XmlDataSize = (UINT32)XmlDocumentSize;
Init.SupportPosition = TRUE;
if ((XmlDocument == NULL) || (XmlDocumentSize == 0) || (Root == NULL)) {
Status = EFI_INVALID_PARAMETER;
goto Exit;
}
*Root = NULL;
//
// Start by initializing the tokenizer with our data. Note that we don't
// pass along the optional "special string" and normal comparison functions,
// as the XML parser has its own implementation.
//
Status = RtlXmlInitializeTokenization (&State, &Init);
if (EFI_ERROR (Status)) {
DEBUG ((EFI_D_ERROR, "Failed to initialize tokenization\n"));
goto Exit;
}
//
// Always determine the encoding of an xml stream. This should be done before
// the first "next" call on the tokenizer to make sure the correct character
// decoder is selected.
//
Status = RtlXmlDetermineStreamEncoding (&State, &EncodingLength);
if (EFI_ERROR (Status)) {
DEBUG ((EFI_D_ERROR, "Failed to determine encoding type\n"));
goto Exit;
}
//
// Finding the encoding may have adjusted the real pointer for the top of the
// document to skip past the BOM
//
State.RawTokenState.pvCursor = (VOID *)(((UINTN)State.RawTokenState.pvCursor) + EncodingLength);
do {
XML_TOKEN Next;
//
// Acquire the next token from the parser.
//
Status = RtlXmlNextToken (&State, &Next, FALSE);
if (EFI_ERROR (Status)) {
DEBUG ((EFI_D_ERROR, "Failed to get the next token, Status = %r\n", Status)); // MS_CHANGE
goto Exit;
} else if (Next.fError) {
//
// Errors in parse, such as bad characters, come out here in the
// fError member
//
DEBUG ((EFI_D_ERROR, "Error during tokenization, Next.fError = %d\n", Next.fError)); // MS_CHANGE
Status = EFI_ABORTED; // MS_CHANGE
goto Exit;
}
if (StartDoc == NULL) {
StartDoc = (CHAR8 *)Next.Run.pvData;
}
ProcessedCharacters += Next.Run.cbData;
if (ProcessedCharacters >= XmlDocumentSize) {
DEBUG ((DEBUG_VERBOSE, "Reached the specified number of characters, ending...\n"));
if (ProcessedNode == FALSE) {
DEBUG ((EFI_D_ERROR, "ERROR: We reached the end, and no nodes were created.\n"));
Status = EFI_INVALID_PARAMETER;
} else {
Status = EFI_SUCCESS;
}
goto Exit;
}
//
// Get the current location.
//
Status = RtlXmlGetCurrentLocation (&State, &Location);
if (EFI_ERROR (Status)) {
DEBUG ((EFI_D_ERROR, "Failed to get location information.\n"));
}
if (Next.Run.pvData == NULL) {
DEBUG ((EFI_D_ERROR, "ERROR: Next.Run.pvData == NULL\n"));
Status = EFI_INVALID_PARAMETER;
goto Exit;
}
//
// Pick off the XML declaration if there is one.
//
if (Next.State == XTSS_XMLDECL_CLOSE) {
const UINTN EndlineSize = 2;
XmlDeclaration = AllocateZeroPool (State.Location.Column + EndlineSize);
if (XmlDeclaration == NULL) {
Status = EFI_OUT_OF_RESOURCES;
goto Exit;
}
Status = AsciiStrnCpyS (XmlDeclaration, State.Location.Column + EndlineSize, StartDoc, State.Location.Column + 1);
if (EFI_ERROR (Status)) {
goto Exit;
}
} else if (Next.State == XTSS_ELEMENT_NAME) {
//
// We found an element name, so create a new node for it.
//
Status = AsciiStrnCpyS (Element, ARRAYSIZE (Element), (CHAR8 *)Next.Run.pvData, (UINTN)Next.Run.ulCharacters);
if (EFI_ERROR (Status)) {
DEBUG ((EFI_D_ERROR, "ERROR, AsciiStrnCpyS() failed\n"));
goto Exit;
}
DEBUG ((DEBUG_VERBOSE, "New, adding node: '%a'\n", Element));
if (*Root == NULL) {
//
// This is the root node.
//
Status = AddNode (NULL, Element, NULL, Root);
if (EFI_ERROR (Status)) {
goto Exit;
}
//
// Add the declaration if we had one.
//
(*Root)->XmlDeclaration.Declaration = XmlDeclaration;
CurrentNode = *Root;
} else {
Status = AddNode (CurrentNode, Element, NULL, &CurrentNode);
if (EFI_ERROR (Status)) {
goto Exit;
}
}
//
// Mark that we have successfully added a new node, so we can check
// for valid XML when the end of the document is reached.
//
ProcessedNode = TRUE;
} else if (Next.State == XTSS_STREAM_HYPERSPACE) {
BOOLEAN NotWhiteSpace = FALSE;
// MS_CHANGE [begin]
CHAR8 *LocalHyperSpace;
UINTN LocalSize;
UINTN TempSize;
LocalHyperSpace = (CHAR8 *)Next.Run.pvData;
LocalSize = (UINTN)Next.Run.ulCharacters;
TempSize = LocalSize;
if (TempSize >= MAX_PATH) {
TempSize = MAX_PATH - 1;
}
Status = AsciiStrnCpyS (HyperSpace, ARRAYSIZE (HyperSpace), (CHAR8 *)Next.Run.pvData, TempSize);
// MS_CHANGE [end]
if (EFI_ERROR (Status)) {
goto Exit;
}
//
// See if we have a value.
//
for (UINT64 i = 0; i < Next.Run.ulCharacters; i++) {