-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathCFXMLInterface.c
1248 lines (1010 loc) · 42.7 KB
/
CFXMLInterface.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
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
/* CFXMLInterface.c
Copyright (c) 2015 Apple Inc. and the Swift project authors
*/
#include <CoreFoundation/CFRuntime.h>
#include <libxml/globals.h>
#include <libxml/xmlerror.h>
#include <libxml/parser.h>
#include <libxml/parserInternals.h>
#include <libxml/tree.h>
#include <libxml/xmlmemory.h>
#include <libxml/xmlsave.h>
#include <libxml/xpath.h>
#include <libxml/dict.h>
#include "CFInternal.h"
/*
libxml2 does not have nullability annotations and does not import well into swift when given potentially differing versions of the library that might be installed on the host operating system. This is a simple C wrapper to simplify some of that interface layer to libxml2.
*/
CFIndex _kCFXMLInterfaceRecover = XML_PARSE_RECOVER;
CFIndex _kCFXMLInterfaceNoEnt = XML_PARSE_NOENT;
CFIndex _kCFXMLInterfaceDTDLoad = XML_PARSE_DTDLOAD;
CFIndex _kCFXMLInterfaceDTDAttr = XML_PARSE_DTDATTR;
CFIndex _kCFXMLInterfaceDTDValid = XML_PARSE_DTDVALID;
CFIndex _kCFXMLInterfaceNoError = XML_PARSE_NOERROR;
CFIndex _kCFXMLInterfaceNoWarning = XML_PARSE_NOWARNING;
CFIndex _kCFXMLInterfacePedantic = XML_PARSE_PEDANTIC;
CFIndex _kCFXMLInterfaceNoBlanks = XML_PARSE_NOBLANKS;
CFIndex _kCFXMLInterfaceSAX1 = XML_PARSE_SAX1;
CFIndex _kCFXMLInterfaceXInclude = XML_PARSE_XINCLUDE;
CFIndex _kCFXMLInterfaceNoNet = XML_PARSE_NONET;
CFIndex _kCFXMLInterfaceNoDict = XML_PARSE_NODICT;
CFIndex _kCFXMLInterfaceNSClean = XML_PARSE_NSCLEAN;
CFIndex _kCFXMLInterfaceNoCdata = XML_PARSE_NOCDATA;
CFIndex _kCFXMLInterfaceNoXIncnode = XML_PARSE_NOXINCNODE;
CFIndex _kCFXMLInterfaceCompact = XML_PARSE_COMPACT;
CFIndex _kCFXMLInterfaceOld10 = XML_PARSE_OLD10;
CFIndex _kCFXMLInterfaceNoBasefix = XML_PARSE_NOBASEFIX;
CFIndex _kCFXMLInterfaceHuge = XML_PARSE_HUGE;
CFIndex _kCFXMLInterfaceOldsax = XML_PARSE_OLDSAX;
CFIndex _kCFXMLInterfaceIgnoreEnc = XML_PARSE_IGNORE_ENC;
CFIndex _kCFXMLInterfaceBigLines = XML_PARSE_BIG_LINES;
CFIndex _kCFXMLTypeInvalid = 0;
CFIndex _kCFXMLTypeDocument = XML_DOCUMENT_NODE;
CFIndex _kCFXMLTypeElement = XML_ELEMENT_NODE;
CFIndex _kCFXMLTypeAttribute = XML_ATTRIBUTE_NODE;
CFIndex _kCFXMLTypeDTD = XML_DTD_NODE;
CFIndex _kCFXMLDocTypeHTML = XML_DOC_HTML;
CFIndex _kCFXMLDTDNodeTypeEntity = XML_ENTITY_DECL;
CFIndex _kCFXMLDTDNodeTypeAttribute = XML_ATTRIBUTE_DECL;
CFIndex _kCFXMLDTDNodeTypeElement = XML_ELEMENT_DECL;
CFIndex _kCFXMLDTDNodeTypeNotation = XML_NOTATION_NODE;
CFIndex _kCFXMLDTDNodeElementTypeUndefined = XML_ELEMENT_TYPE_UNDEFINED;
CFIndex _kCFXMLDTDNodeElementTypeEmpty = XML_ELEMENT_TYPE_EMPTY;
CFIndex _kCFXMLDTDNodeElementTypeAny = XML_ELEMENT_TYPE_ANY;
CFIndex _kCFXMLDTDNodeElementTypeMixed = XML_ELEMENT_TYPE_MIXED;
CFIndex _kCFXMLDTDNodeElementTypeElement = XML_ELEMENT_TYPE_ELEMENT;
CFIndex _kCFXMLDTDNodeEntityTypeInternalGeneral = XML_INTERNAL_GENERAL_ENTITY;
CFIndex _kCFXMLDTDNodeEntityTypeExternalGeneralParsed = XML_EXTERNAL_GENERAL_PARSED_ENTITY;
CFIndex _kCFXMLDTDNodeEntityTypeExternalGeneralUnparsed = XML_EXTERNAL_GENERAL_UNPARSED_ENTITY;
CFIndex _kCFXMLDTDNodeEntityTypeInternalParameter = XML_INTERNAL_PARAMETER_ENTITY;
CFIndex _kCFXMLDTDNodeEntityTypeExternalParameter = XML_EXTERNAL_PARAMETER_ENTITY;
CFIndex _kCFXMLDTDNodeEntityTypeInternalPredefined = XML_INTERNAL_PREDEFINED_ENTITY;
CFIndex _kCFXMLDTDNodeAttributeTypeCData = XML_ATTRIBUTE_CDATA;
CFIndex _kCFXMLDTDNodeAttributeTypeID = XML_ATTRIBUTE_ID;
CFIndex _kCFXMLDTDNodeAttributeTypeIDRef = XML_ATTRIBUTE_IDREF;
CFIndex _kCFXMLDTDNodeAttributeTypeIDRefs = XML_ATTRIBUTE_IDREFS;
CFIndex _kCFXMLDTDNodeAttributeTypeEntity = XML_ATTRIBUTE_ENTITY;
CFIndex _kCFXMLDTDNodeAttributeTypeEntities = XML_ATTRIBUTE_ENTITIES;
CFIndex _kCFXMLDTDNodeAttributeTypeNMToken = XML_ATTRIBUTE_NMTOKEN;
CFIndex _kCFXMLDTDNodeAttributeTypeNMTokens = XML_ATTRIBUTE_NMTOKENS;
CFIndex _kCFXMLDTDNodeAttributeTypeEnumeration = XML_ATTRIBUTE_ENUMERATION;
CFIndex _kCFXMLDTDNodeAttributeTypeNotation = XML_ATTRIBUTE_NOTATION;
CFIndex _kCFXMLNodePreserveWhitespace = 1 << 25;
CFIndex _kCFXMLNodeCompactEmptyElement = 1 << 2;
CFIndex _kCFXMLNodePrettyPrint = 1 << 17;
CFIndex _kCFXMLNodeLoadExternalEntitiesNever = 1 << 19;
CFIndex _kCFXMLNodeLoadExternalEntitiesAlways = 1 << 14;
// We define this structure because libxml2's "notation" node does not contain the fields
// nearly all other libxml2 node fields contain, that we use extensively.
typedef struct {
void * _private;
xmlElementType type;
const xmlChar* name;
xmlNodePtr children;
xmlNodePtr last;
xmlNodePtr parent;
xmlNodePtr next;
xmlNodePtr prev;
xmlDocPtr doc;
xmlNotationPtr notation;
} _cfxmlNotation;
static xmlExternalEntityLoader __originalLoader = NULL;
static xmlParserInputPtr _xmlExternalEntityLoader(const char *urlStr, const char * ID, xmlParserCtxtPtr context) {
_CFXMLInterface parser = __CFSwiftBridge.NSXMLParser.currentParser();
if (parser != NULL) {
return __CFSwiftBridge.NSXMLParser._xmlExternalEntityWithURL(parser, urlStr, ID, context, __originalLoader);
}
return __originalLoader(urlStr, ID, context);
}
void _CFSetupXMLInterface(void) {
static dispatch_once_t xmlInitGuard;
dispatch_once(&xmlInitGuard, ^{
xmlInitParser();
// set up the external entity loader
__originalLoader = xmlGetExternalEntityLoader();
xmlSetExternalEntityLoader(_xmlExternalEntityLoader);
});
}
_CFXMLInterfaceParserInput _CFXMLInterfaceNoNetExternalEntityLoader(const char *URL, const char *ID, _CFXMLInterfaceParserContext ctxt) {
return xmlNoNetExternalEntityLoader(URL, ID, ctxt);
}
static void _errorCallback(void *ctx, const char *msg, ...) {
xmlParserCtxtPtr context = __CFSwiftBridge.NSXMLParser.getContext((_CFXMLInterface)ctx);
xmlErrorPtr error = xmlCtxtGetLastError(context);
// TODO: reporting
// _reportError(error, (_CFXMLInterface)ctx);
}
_CFXMLInterfaceSAXHandler _CFXMLInterfaceCreateSAXHandler() {
_CFXMLInterfaceSAXHandler saxHandler = (_CFXMLInterfaceSAXHandler)calloc(1, sizeof(struct _xmlSAXHandler));
saxHandler->internalSubset = (internalSubsetSAXFunc)__CFSwiftBridge.NSXMLParser.internalSubset;
saxHandler->isStandalone = (isStandaloneSAXFunc)__CFSwiftBridge.NSXMLParser.isStandalone;
saxHandler->hasInternalSubset = (hasInternalSubsetSAXFunc)__CFSwiftBridge.NSXMLParser.hasInternalSubset;
saxHandler->hasExternalSubset = (hasExternalSubsetSAXFunc)__CFSwiftBridge.NSXMLParser.hasExternalSubset;
saxHandler->getEntity = (getEntitySAXFunc)__CFSwiftBridge.NSXMLParser.getEntity;
saxHandler->notationDecl = (notationDeclSAXFunc)__CFSwiftBridge.NSXMLParser.notationDecl;
saxHandler->attributeDecl = (attributeDeclSAXFunc)__CFSwiftBridge.NSXMLParser.attributeDecl;
saxHandler->elementDecl = (elementDeclSAXFunc)__CFSwiftBridge.NSXMLParser.elementDecl;
saxHandler->unparsedEntityDecl = (unparsedEntityDeclSAXFunc)__CFSwiftBridge.NSXMLParser.unparsedEntityDecl;
saxHandler->startDocument = (startDocumentSAXFunc)__CFSwiftBridge.NSXMLParser.startDocument;
saxHandler->endDocument = (endDocumentSAXFunc)__CFSwiftBridge.NSXMLParser.endDocument;
saxHandler->startElementNs = (startElementNsSAX2Func)__CFSwiftBridge.NSXMLParser.startElementNs;
saxHandler->endElementNs = (endElementNsSAX2Func)__CFSwiftBridge.NSXMLParser.endElementNs;
saxHandler->characters = (charactersSAXFunc)__CFSwiftBridge.NSXMLParser.characters;
saxHandler->processingInstruction = (processingInstructionSAXFunc)__CFSwiftBridge.NSXMLParser.processingInstruction;
saxHandler->error = _errorCallback;
saxHandler->cdataBlock = (cdataBlockSAXFunc)__CFSwiftBridge.NSXMLParser.cdataBlock;
saxHandler->comment = (commentSAXFunc)__CFSwiftBridge.NSXMLParser.comment;
saxHandler->externalSubset = (externalSubsetSAXFunc)__CFSwiftBridge.NSXMLParser.externalSubset;
saxHandler->initialized = XML_SAX2_MAGIC; // make sure start/endElementNS are used
return saxHandler;
}
void _CFXMLInterfaceDestroySAXHandler(_CFXMLInterfaceSAXHandler handler) {
free(handler);
}
void _CFXMLInterfaceSetStructuredErrorFunc(_CFXMLInterface ctx, _CFXMLInterfaceStructuredErrorFunc handler) {
xmlSetStructuredErrorFunc(ctx, (xmlStructuredErrorFunc)handler);
}
_CFXMLInterfaceParserContext _CFXMLInterfaceCreatePushParserCtxt(_CFXMLInterfaceSAXHandler sax, _CFXMLInterface user_data, const char *chunk, int size, const char *filename) {
return xmlCreatePushParserCtxt(sax, user_data, chunk, size, filename);
}
void _CFXMLInterfaceCtxtUseOptions(_CFXMLInterfaceParserContext ctx, CFIndex options) {
xmlCtxtUseOptions(ctx, options);
}
int _CFXMLInterfaceParseChunk(_CFXMLInterfaceParserContext ctxt, const char *chunk, int size, int terminate) {
return xmlParseChunk(ctxt, chunk, size, terminate);
}
void _CFXMLInterfaceStopParser(_CFXMLInterfaceParserContext ctx) {
xmlStopParser(ctx);
}
void _CFXMLInterfaceDestroyContext(_CFXMLInterfaceParserContext ctx) {
if (ctx == NULL) return;
if (ctx->myDoc) {
xmlFreeDoc(ctx->myDoc);
}
xmlFreeParserCtxt(ctx);
}
int _CFXMLInterfaceSAX2GetLineNumber(_CFXMLInterfaceParserContext ctx) {
if (ctx == NULL) return 0;
return xmlSAX2GetLineNumber(ctx);
}
int _CFXMLInterfaceSAX2GetColumnNumber(_CFXMLInterfaceParserContext ctx) {
if (ctx == NULL) return 0;
return xmlSAX2GetColumnNumber(ctx);
}
void _CFXMLInterfaceSAX2InternalSubset(_CFXMLInterfaceParserContext ctx,
const unsigned char *name,
const unsigned char *ExternalID,
const unsigned char *SystemID) {
if (ctx != NULL) xmlSAX2InternalSubset(ctx, name, ExternalID, SystemID);
}
void _CFXMLInterfaceSAX2ExternalSubset(_CFXMLInterfaceParserContext ctx,
const unsigned char *name,
const unsigned char *ExternalID,
const unsigned char *SystemID) {
if (ctx != NULL) xmlSAX2ExternalSubset(ctx, name, ExternalID, SystemID);
}
int _CFXMLInterfaceIsStandalone(_CFXMLInterfaceParserContext ctx) {
if (ctx != NULL) return ctx->myDoc->standalone;
return 0;
}
int _CFXMLInterfaceHasInternalSubset(_CFXMLInterfaceParserContext ctx) {
if (ctx != NULL) return ctx->myDoc->intSubset != NULL;
return 0;
}
int _CFXMLInterfaceHasExternalSubset(_CFXMLInterfaceParserContext ctx) {
if (ctx != NULL) return ctx->myDoc->extSubset != NULL;
return 0;
}
_CFXMLInterfaceEntity _CFXMLInterfaceGetPredefinedEntity(const unsigned char *name) {
return xmlGetPredefinedEntity(name);
}
_CFXMLDTDNodePtr _Nullable _CFXMLDTDNewElementDesc(_CFXMLDTDPtr dtd, const unsigned char* name) {
bool freeDTD = false;
if (!dtd) {
dtd = xmlNewDtd(NULL, (const xmlChar*)"tempDTD", NULL, NULL);
freeDTD = true;
}
if (!name) {
name = (const xmlChar*)"";
}
xmlElementPtr result = xmlAddElementDecl(NULL, dtd, name, XML_ELEMENT_TYPE_ANY, NULL);
if (freeDTD) {
_CFXMLUnlinkNode(result);
xmlFreeDtd(dtd);
}
return result;
}
_CFXMLDTDNodePtr _Nullable _CFXMLDTDNewAttributeDesc(_CFXMLDTDPtr dtd, const unsigned char* name)
{
bool freeDTD = false;
if (!dtd) {
dtd = xmlNewDtd(NULL, (const xmlChar*)"tempDTD", NULL, NULL);
freeDTD = true;
}
if (!name) {
name = (const xmlChar*)"";
}
xmlAttributePtr result = xmlAddAttributeDecl(NULL, dtd, NULL, name, NULL, XML_ATTRIBUTE_ID, XML_ATTRIBUTE_NONE, NULL, NULL);
if (freeDTD) {
_CFXMLUnlinkNode(result);
xmlFreeDtd(dtd);
}
return result;
}
_CFXMLInterfaceEntity _CFXMLInterfaceSAX2GetEntity(_CFXMLInterfaceParserContext ctx, const unsigned char *name) {
if (ctx == NULL) return NULL;
_CFXMLInterfaceEntity entity = xmlSAX2GetEntity(ctx, name);
if (entity && ctx->instate == XML_PARSER_CONTENT) ctx->_private = (void *)1;
return entity;
}
int _CFXMLInterfaceInRecursiveState(_CFXMLInterfaceParserContext ctx) {
return ctx->_private == (void *)1;
}
void _CFXMLInterfaceResetRecursiveState(_CFXMLInterfaceParserContext ctx) {
ctx->_private = NULL;
}
int _CFXMLInterfaceHasDocument(_CFXMLInterfaceParserContext ctx) {
if (ctx == NULL) return 0;
return ctx->myDoc != NULL;
}
void _CFXMLInterfaceFreeEnumeration(_CFXMLInterfaceEnumeration enumeration) {
if (enumeration == NULL) return;
xmlFreeEnumeration(enumeration);
}
void _CFXMLInterfaceSAX2UnparsedEntityDecl(_CFXMLInterfaceParserContext ctx, const unsigned char *name, const unsigned char *publicId, const unsigned char *systemId, const unsigned char *notationName) {
if (ctx == NULL) return;
xmlSAX2UnparsedEntityDecl(ctx, name, publicId, systemId, notationName);
}
CFErrorRef _CFErrorCreateFromXMLInterface(_CFXMLInterfaceError err) {
return CFErrorCreate(kCFAllocatorSystemDefault, CFSTR("NSXMLParserErrorDomain"), err->code, nil);
}
_CFXMLNodePtr _CFXMLNewNode(_CFXMLNamespacePtr namespace, const char* name) {
return xmlNewNode(namespace, (const xmlChar*)name);
}
_CFXMLNodePtr _CFXMLCopyNode(_CFXMLNodePtr node, bool recursive) {
int recurse = recursive ? 1 : 0;
switch (((xmlNodePtr)node)->type) {
case XML_DOCUMENT_NODE:
return xmlCopyDoc(node, recurse);
case XML_DTD_NODE:
return xmlCopyDtd(node);
default:
return xmlCopyNode(node, recurse);
}
}
_CFXMLDocPtr _CFXMLNewDoc(const unsigned char* version) {
return xmlNewDoc(version);
}
_CFXMLNodePtr _CFXMLNewProcessingInstruction(const unsigned char* name, const unsigned char* value) {
return xmlNewPI(name, value);
}
_CFXMLNodePtr _CFXMLNewTextNode(const unsigned char* value) {
return xmlNewText(value);
}
_CFXMLNodePtr _CFXMLNewComment(const unsigned char* value) {
return xmlNewComment(value);
}
_CFXMLNodePtr _CFXMLNewProperty(_CFXMLNodePtr node, const unsigned char* name, const unsigned char* value) {
return xmlNewProp(node, name, value);
}
_CFXMLNamespacePtr _CFXMLNewNamespace(_CFXMLNodePtr node, const unsigned char* uri, const unsigned char* prefix) {
return xmlNewNs(node, uri, prefix);
}
CF_RETURNS_RETAINED CFStringRef _CFXMLNodeURI(_CFXMLNodePtr node) {
xmlNodePtr nodePtr = (xmlNodePtr)node;
switch (nodePtr->type) {
case XML_ATTRIBUTE_NODE:
case XML_ELEMENT_NODE:
return CFStringCreateWithCString(NULL, (const char*)nodePtr->ns->href, kCFStringEncodingUTF8);
case XML_DOCUMENT_NODE:
{
xmlDocPtr doc = (xmlDocPtr)node;
return CFStringCreateWithCString(NULL, (const char*)doc->URL, kCFStringEncodingUTF8);
}
default:
return NULL;
}
}
void _CFXMLNodeSetURI(_CFXMLNodePtr node, const unsigned char* URI) {
xmlNodePtr nodePtr = (xmlNodePtr)node;
switch (nodePtr->type) {
case XML_ATTRIBUTE_NODE:
case XML_ELEMENT_NODE:
if (!URI) {
if (nodePtr->ns) {
xmlFree(nodePtr->ns);
}
nodePtr->ns = NULL;
return;
}
xmlNsPtr ns = xmlSearchNsByHref(nodePtr->doc, nodePtr, URI);
if (!ns) {
if (nodePtr->ns && (nodePtr->ns->href == NULL)) {
nodePtr->ns->href = xmlStrdup(URI);
return;
}
ns = xmlNewNs(nodePtr, URI, NULL);
}
xmlSetNs(nodePtr, ns);
break;
case XML_DOCUMENT_NODE:
{
xmlDocPtr doc = (xmlDocPtr)node;
if (doc->URL) {
xmlFree((xmlChar*)doc->URL);
}
doc->URL = URI;
}
break;
default:
return;
}
}
void _CFXMLNodeSetPrivateData(_CFXMLNodePtr node, void* data) {
if (!node) {
return;
}
((xmlNodePtr)node)->_private = data;
}
void* _Nullable _CFXMLNodeGetPrivateData(_CFXMLNodePtr node) {
return ((xmlNodePtr)node)->_private;
}
_CFXMLNodePtr _CFXMLNodeProperties(_CFXMLNodePtr node) {
return ((xmlNodePtr)node)->properties;
}
CFIndex _CFXMLNodeGetType(_CFXMLNodePtr node) {
if (!node) {
return _kCFXMLTypeInvalid;
}
return ((xmlNodePtr)node)->type;
}
const char* _CFXMLNodeGetName(_CFXMLNodePtr node) {
return (const char*)(((xmlNodePtr)node)->name);
}
void _CFXMLNodeSetName(_CFXMLNodePtr node, const char* name) {
xmlNodeSetName(node, (const xmlChar*)name);
}
CFStringRef _CFXMLNodeGetContent(_CFXMLNodePtr node) {
switch (((xmlNodePtr)node)->type) {
case XML_ELEMENT_DECL:
{
char* buffer = calloc(2048, 1);
xmlSnprintfElementContent(buffer, 2047, ((xmlElementPtr)node)->content, 1);
CFStringRef result = CFStringCreateWithCString(NULL, buffer, kCFStringEncodingUTF8);
free(buffer);
return result;
}
default:
{
xmlChar* content = xmlNodeGetContent(node);
if (content == NULL) {
return NULL;
}
CFStringRef result = CFStringCreateWithCString(NULL, (const char*)content, kCFStringEncodingUTF8);
xmlFree(content);
return result;
}
}
}
void _CFXMLNodeSetContent(_CFXMLNodePtr node, const unsigned char* _Nullable content) {
// So handling set content on XML_ELEMENT_DECL is listed as a TODO !!! in libxml2's source code.
// that means we have to do it ourselves.
switch (((xmlNodePtr)node)->type) {
case XML_ELEMENT_DECL:
{
xmlElementPtr element = (xmlElementPtr)node;
if (content == NULL) {
xmlFreeDocElementContent(element->doc, element->content);
element->content = NULL;
return;
}
// rather than writing custom code to parse the new content into the correct
// xmlElementContent structures, let's leverage what we've already got.
CFMutableStringRef xmlString = CFStringCreateMutable(NULL, 0);
CFStringAppend(xmlString, CFSTR("<!ELEMENT "));
CFStringAppendCString(xmlString, _CFXMLNodeGetName(node), kCFStringEncodingUTF8);
CFStringAppend(xmlString, CFSTR(" "));
CFStringAppendCString(xmlString, (const char*)content, kCFStringEncodingUTF8);
CFStringAppend(xmlString, CFSTR(">"));
size_t bufferSize = CFStringGetMaximumSizeForEncoding(CFStringGetLength(xmlString), kCFStringEncodingUTF8) + 1;
char* buffer = calloc(bufferSize, 1);
CFStringGetCString(xmlString, buffer, bufferSize, kCFStringEncodingUTF8);
xmlElementPtr resultNode = _CFXMLParseDTDNode((const xmlChar*)buffer);
if (resultNode) {
xmlFreeDocElementContent(element->doc, element->content);
_CFXMLFreeNode(element->attributes);
xmlRegFreeRegexp(element->contModel);
element->type = resultNode->type;
element->etype = resultNode->etype;
element->content = resultNode->content;
element->attributes = resultNode->attributes;
element->contModel = resultNode->contModel;
resultNode->content = NULL;
resultNode->attributes = NULL;
resultNode->contModel = NULL;
_CFXMLFreeNode(resultNode);
}
return;
}
default:
if (content == NULL) {
xmlNodeSetContent(node, nil);
return;
}
xmlNodeSetContent(node, content);
}
}
_CFXMLDocPtr _CFXMLNodeGetDocument(_CFXMLNodePtr node) {
return ((xmlNodePtr)node)->doc;
}
CFStringRef _CFXMLEncodeEntities(_CFXMLDocPtr doc, const unsigned char* string) {
if (!string) {
return NULL;
}
const xmlChar* stringResult = xmlEncodeEntitiesReentrant(doc, string);
CFStringRef result = CFStringCreateWithCString(NULL, (const char*)stringResult, kCFStringEncodingUTF8);
xmlFree((xmlChar*)stringResult);
return result;
}
static inline void _removeHashEntry(xmlHashTablePtr table, const xmlChar* name, xmlNodePtr node);
static inline void _removeHashEntry(xmlHashTablePtr table, const xmlChar* name, xmlNodePtr node) {
if (xmlHashLookup(table, name) == node) {
xmlHashRemoveEntry(table, name, NULL);
}
}
void _CFXMLUnlinkNode(_CFXMLNodePtr node) {
// DTD DECL nodes have references in the parent DTD's various hash tables.
// For some reason, libxml2's xmlUnlinkNode doesn't actually remove those references for
// anything other than entities, and even then only if there is a parent document!
// To make matters worse, when you run xmlFreeDtd on the dtd, it actually deallocs everything
// that it has a hash table reference to in addition to all of it's children. So, we need
// to manually remove the node from the correct hash table before we can unlink it.
switch (((xmlNodePtr)node)->type) {
case XML_ELEMENT_DECL:
{
xmlElementPtr elemDecl = (xmlElementPtr)node;
xmlDtdPtr dtd = elemDecl->parent;
_removeHashEntry(dtd->elements, elemDecl->name, node);
}
break;
case XML_ENTITY_DECL:
{
xmlEntityPtr entityDecl = (xmlEntityPtr)node;
xmlDtdPtr dtd = entityDecl->parent;
_removeHashEntry(dtd->entities, entityDecl->name, node);
_removeHashEntry(dtd->pentities, entityDecl->name, node);
}
break;
case XML_ATTRIBUTE_DECL:
{
xmlAttributePtr attrDecl = (xmlAttributePtr)node;
xmlDtdPtr dtd = attrDecl->parent;
if (xmlHashLookup3(dtd->attributes, attrDecl->name, NULL, attrDecl->elem) == node) {
xmlHashRemoveEntry3(dtd->attributes, attrDecl->name, NULL, attrDecl->elem, NULL);
}
}
break;
case XML_NOTATION_NODE:
{
// Since we're handling notation nodes instead of libxml2, we need to do some extra work here
xmlNotationPtr notation = ((_cfxmlNotation*)node)->notation;
xmlDtdPtr dtd = (xmlDtdPtr)((_cfxmlNotation*)node)->parent;
_removeHashEntry(dtd->notations, notation->name, (xmlNodePtr)notation);
return;
}
default:
break;
}
xmlUnlinkNode(node);
}
_CFXMLNodePtr _CFXMLNodeGetFirstChild(_CFXMLNodePtr node) {
return ((xmlNodePtr)node)->children;
}
_CFXMLNodePtr _CFXMLNodeGetLastChild(_CFXMLNodePtr node) {
return ((xmlNodePtr)node)->last;
}
_CFXMLNodePtr _CFXMLNodeGetNextSibling(_CFXMLNodePtr node) {
return ((xmlNodePtr)node)->next;
}
_CFXMLNodePtr _CFXMLNodeGetPrevSibling(_CFXMLNodePtr node) {
return ((xmlNodePtr)node)->prev;
}
_CFXMLNodePtr _CFXMLNodeGetParent(_CFXMLNodePtr node) {
return ((xmlNodePtr)node)->parent;
}
bool _CFXMLDocStandalone(_CFXMLDocPtr doc) {
return ((xmlDocPtr)doc)->standalone == 1;
}
void _CFXMLDocSetStandalone(_CFXMLDocPtr doc, bool standalone) {
((xmlDocPtr)doc)->standalone = standalone ? 1 : 0;
}
_CFXMLNodePtr _CFXMLDocRootElement(_CFXMLDocPtr doc) {
return xmlDocGetRootElement(doc);
}
void _CFXMLDocSetRootElement(_CFXMLDocPtr doc, _CFXMLNodePtr node) {
xmlDocSetRootElement(doc, node);
}
CF_RETURNS_RETAINED CFStringRef _CFXMLDocCharacterEncoding(_CFXMLDocPtr doc) {
return CFStringCreateWithCString(NULL, (const char*)((xmlDocPtr)doc)->encoding, kCFStringEncodingUTF8);
}
void _CFXMLDocSetCharacterEncoding(_CFXMLDocPtr doc, const unsigned char* _Nullable encoding) {
xmlDocPtr docPtr = (xmlDocPtr)doc;
if (docPtr->encoding) {
xmlFree((xmlChar*)docPtr->encoding);
}
docPtr->encoding = encoding;
}
CF_RETURNS_RETAINED CFStringRef _CFXMLDocVersion(_CFXMLDocPtr doc) {
return CFStringCreateWithCString(NULL, (const char*)((xmlDocPtr)doc)->version, kCFStringEncodingUTF8);
}
void _CFXMLDocSetVersion(_CFXMLDocPtr doc, const unsigned char* version) {
xmlDocPtr docPtr = (xmlDocPtr)doc;
if (docPtr->version) {
xmlFree((xmlChar*)docPtr->version);
}
docPtr->version = xmlStrdup(version);
}
int _CFXMLDocProperties(_CFXMLDocPtr doc) {
return ((xmlDocPtr)doc)->properties;
}
void _CFXMLDocSetProperties(_CFXMLDocPtr doc, int newProperties) {
((xmlDocPtr)doc)->properties = newProperties;
}
_CFXMLDTDPtr _Nullable _CFXMLDocDTD(_CFXMLDocPtr doc) {
return xmlGetIntSubset(doc);
}
void _CFXMLDocSetDTD(_CFXMLDocPtr doc, _CFXMLDTDPtr _Nullable dtd) {
if (!dtd) {
((xmlDocPtr)doc)->intSubset = NULL;
return;
}
xmlDocPtr docPtr = (xmlDocPtr)doc;
xmlDtdPtr dtdPtr = (xmlDtdPtr)dtd;
docPtr->intSubset = dtdPtr;
if (docPtr->children == NULL) {
xmlAddChild(doc, dtd);
} else {
xmlAddPrevSibling(docPtr->children, dtd);
}
}
CFIndex _CFXMLNodeGetElementChildCount(_CFXMLNodePtr node) {
return xmlChildElementCount(node);
}
void _CFXMLNodeAddChild(_CFXMLNodePtr node, _CFXMLNodePtr child) {
if (((xmlNodePtr)node)->type == XML_NOTATION_NODE) {// the "artificial" node we created
if (((xmlNodePtr)node)->type == XML_DTD_NODE) {// the only circumstance under which this actually makes sense
xmlNotationPtr notation = ((_cfxmlNotation*)child)->notation;
xmlDtdPtr dtd = (xmlDtdPtr)node;
if (dtd->notations == NULL) {
xmlDictPtr dict = dtd->doc ? dtd->doc->dict : NULL;
dtd->notations = xmlHashCreateDict(0, dict);
}
xmlHashAddEntry(dtd->notations, notation->name, notation);
}
return;
}
xmlAddChild(node, child);
}
void _CFXMLNodeAddPrevSibling(_CFXMLNodePtr node, _CFXMLNodePtr prevSibling) {
xmlAddPrevSibling(node, prevSibling);
}
void _CFXMLNodeAddNextSibling(_CFXMLNodePtr node, _CFXMLNodePtr nextSibling) {
xmlAddNextSibling(node, nextSibling);
}
void _CFXMLNodeReplaceNode(_CFXMLNodePtr node, _CFXMLNodePtr replacement) {
xmlReplaceNode(node, replacement);
}
_CFXMLEntityPtr _CFXMLGetDocEntity(_CFXMLDocPtr doc, const char* entity) {
return xmlGetDocEntity(doc, (const xmlChar*)entity);
}
_CFXMLEntityPtr _CFXMLGetDTDEntity(_CFXMLDocPtr doc, const char* entity) {
return xmlGetDtdEntity(doc, (const xmlChar*)entity);
}
_CFXMLEntityPtr _CFXMLGetParameterEntity(_CFXMLDocPtr doc, const char* entity) {
return xmlGetParameterEntity(doc, (const xmlChar*)entity);
}
CFStringRef _CFXMLGetEntityContent(_CFXMLEntityPtr entity) {
const xmlChar* content = ((xmlEntityPtr)entity)->content;
if (!content) {
return NULL;
}
CFIndex length = ((xmlEntityPtr)entity)->length;
CFStringRef result = CFStringCreateWithBytes(NULL, content, length, kCFStringEncodingUTF8, false);
return result;
}
CFStringRef _CFXMLStringWithOptions(_CFXMLNodePtr node, uint32_t options) {
if (((xmlNodePtr)node)->type == XML_ENTITY_DECL &&
((xmlEntityPtr)node)->etype == XML_INTERNAL_PREDEFINED_ENTITY) {
// predefined entities need special handling, libxml2 just tosses an error and returns a NULL string
// if we try to use xmlSaveTree on a predefined entity
CFMutableStringRef result = CFStringCreateMutable(NULL, 0);
CFStringAppend(result, CFSTR("<!ENTITY "));
CFStringAppendCString(result, (const char*)((xmlEntityPtr)node)->name, kCFStringEncodingUTF8);
CFStringAppend(result, CFSTR(" \""));
CFStringAppendCString(result, (const char*)((xmlEntityPtr)node)->content, kCFStringEncodingUTF8);
CFStringAppend(result, CFSTR("\">"));
return result;
} else if (((xmlNodePtr)node)->type == XML_NOTATION_NODE) {
// This is not actually a thing that occurs naturally in libxml2
xmlNotationPtr notation = ((_cfxmlNotation*)node)->notation;
CFMutableStringRef result = CFStringCreateMutable(NULL, 0);
CFStringAppend(result, CFSTR("<!NOTATION "));
CFStringAppendCString(result, (const char*)notation->name, kCFStringEncodingUTF8);
CFStringAppend(result, CFSTR(" "));
if (notation->PublicID == NULL && notation->SystemID != NULL) {
CFStringAppend(result, CFSTR("SYSTEM "));
} else if (notation->PublicID != NULL) {
CFStringAppend(result, CFSTR("PUBLIC \""));
CFStringAppendCString(result, (const char*)notation->PublicID, kCFStringEncodingUTF8);
CFStringAppend(result, CFSTR("\""));
}
if (notation->SystemID != NULL) {
CFStringAppend(result, CFSTR("\""));
CFStringAppendCString(result, (const char*)notation->SystemID, kCFStringEncodingUTF8);
CFStringAppend(result, CFSTR("\""));
}
CFStringAppend(result, CFSTR(" >"));
return result;
}
xmlBufferPtr buffer = xmlBufferCreate();
uint32_t xmlOptions = XML_SAVE_AS_XML;
if (options & _kCFXMLNodePreserveWhitespace) {
xmlOptions |= XML_SAVE_WSNONSIG;
}
if (!(options & _kCFXMLNodeCompactEmptyElement)) {
xmlOptions |= XML_SAVE_NO_EMPTY;
}
if (options & _kCFXMLNodePrettyPrint) {
xmlOptions |= XML_SAVE_FORMAT;
}
xmlSaveCtxtPtr ctx = xmlSaveToBuffer(buffer, "utf-8", xmlOptions);
xmlSaveTree(ctx, node);
int error = xmlSaveClose(ctx);
if (error == -1) {
return CFSTR("");
}
const xmlChar* bufferContents = xmlBufferContent(buffer);
CFStringRef result = CFStringCreateWithCString(NULL, (const char*)bufferContents, kCFStringEncodingUTF8);
xmlBufferFree(buffer);
return result;
}
CF_RETURNS_RETAINED CFArrayRef _CFXMLNodesForXPath(_CFXMLNodePtr node, const unsigned char* xpath) {
if (((xmlNodePtr)node)->doc == NULL) {
return NULL;
}
xmlXPathContextPtr context = xmlXPathNewContext(((xmlNodePtr)node)->doc);
xmlXPathObjectPtr evalResult = xmlXPathNodeEval(node, xpath, context);
xmlNodeSetPtr nodes = evalResult->nodesetval;
int count = nodes->nodeNr;
CFMutableArrayRef results = CFArrayCreateMutable(NULL, count, NULL);
for (int i = 0; i < count; i++) {
CFArrayAppendValue(results, nodes->nodeTab[i]);
}
xmlFree(context);
xmlFree(evalResult);
return results;
}
_CFXMLNodePtr _CFXMLNodeHasProp(_CFXMLNodePtr node, const unsigned char* propertyName) {
return xmlHasProp(node, propertyName);
}
_CFXMLDocPtr _CFXMLDocPtrFromDataWithOptions(CFDataRef data, int options) {
uint32_t xmlOptions = 0;
if ((options & _kCFXMLNodePreserveWhitespace) == 0) {
xmlOptions |= XML_PARSE_NOBLANKS;
}
if (options & _kCFXMLNodeLoadExternalEntitiesNever) {
xmlOptions &= ~(XML_PARSE_NOENT);
} else {
xmlOptions |= XML_PARSE_NOENT;
}
if (options & _kCFXMLNodeLoadExternalEntitiesAlways) {
xmlOptions |= XML_PARSE_DTDLOAD;
}
return xmlReadMemory((const char*)CFDataGetBytePtr(data), CFDataGetLength(data), NULL, NULL, xmlOptions);
}
CF_RETURNS_RETAINED CFStringRef _CFXMLNodeLocalName(_CFXMLNodePtr node) {
int length = 0;
const xmlChar* result = xmlSplitQName3(((xmlNodePtr)node)->name, &length);
return CFStringCreateWithCString(NULL, (const char*)result, kCFStringEncodingUTF8);
}
CF_RETURNS_RETAINED CFStringRef _CFXMLNodePrefix(_CFXMLNodePtr node) {
xmlChar* result = NULL;
xmlChar* unused = xmlSplitQName2(((xmlNodePtr)node)->name, &result);
CFStringRef resultString = CFStringCreateWithCString(NULL, (const char*)result, kCFStringEncodingUTF8);
xmlFree(result);
xmlFree(unused);
return resultString;
}
void _CFXMLValidityErrorHandler(void* ctxt, const char* msg, ...);
void _CFXMLValidityErrorHandler(void* ctxt, const char* msg, ...) {
char* formattedMessage = calloc(1, 1024);
va_list args;
va_start(args, msg);
vsprintf(formattedMessage, msg, args);
va_end(args);
CFStringRef message = CFStringCreateWithCString(NULL, formattedMessage, kCFStringEncodingUTF8);
CFStringAppend(ctxt, message);
CFRelease(message);
free(formattedMessage);
}
bool _CFXMLDocValidate(_CFXMLDocPtr doc, CFErrorRef _Nullable * error) {
CFMutableStringRef errorMessage = CFStringCreateMutable(NULL, 0);
xmlValidCtxtPtr ctxt = xmlNewValidCtxt();
ctxt->error = &_CFXMLValidityErrorHandler;
ctxt->userData = errorMessage;
int result = xmlValidateDocument(ctxt, doc);
xmlFreeValidCtxt(ctxt);
if (result == 0 && error != NULL) {
CFMutableDictionaryRef userInfo = CFDictionaryCreateMutable(NULL, 1, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(userInfo, kCFErrorLocalizedDescriptionKey, errorMessage);
*error = CFErrorCreate(NULL, CFSTR("NSXMLParserErrorDomain"), 0, userInfo);
CFRelease(userInfo);
}
CFRelease(errorMessage);
return result != 0;
}
_CFXMLDTDPtr _CFXMLNewDTD(_CFXMLDocPtr doc, const unsigned char* name, const unsigned char* publicID, const unsigned char* systemID) {
return xmlNewDtd(doc, name, publicID, systemID);
}
_CFXMLDTDNodePtr _CFXMLParseDTDNode(const unsigned char* xmlString) {
CFDataRef data = CFDataCreateWithBytesNoCopy(NULL, xmlString, xmlStrlen(xmlString), kCFAllocatorNull);
xmlDtdPtr dtd = _CFXMLParseDTDFromData(data, NULL);
CFRelease(data);
if (dtd == NULL) {
return NULL;
}
xmlNodePtr node = dtd->children;
xmlUnlinkNode(node);
return node;
}
_CFXMLDTDPtr _Nullable _CFXMLParseDTD(const unsigned char* URL) {
return xmlParseDTD(NULL, URL);
}
_CFXMLDTDPtr _Nullable _CFXMLParseDTDFromData(CFDataRef data, CFErrorRef _Nullable * error) {
xmlParserInputBufferPtr inBuffer = xmlParserInputBufferCreateMem((const char*)CFDataGetBytePtr(data), CFDataGetLength(data), XML_CHAR_ENCODING_UTF8);
xmlSAXHandler handler;
handler.error = &_CFXMLValidityErrorHandler;
CFMutableStringRef errorMessage = CFStringCreateMutable(NULL, 0);
handler._private = errorMessage;
xmlDtdPtr dtd = xmlIOParseDTD(NULL, inBuffer, XML_CHAR_ENCODING_UTF8);
if (dtd == NULL && error != NULL) {
CFMutableDictionaryRef userInfo = CFDictionaryCreateMutable(NULL, 1, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(userInfo, kCFErrorLocalizedDescriptionKey, errorMessage);
*error = CFErrorCreate(NULL, CFSTR("NSXMLParserErrorDomain"), 0, userInfo);
CFRelease(userInfo);
}
CFRelease(errorMessage);
return dtd;
}
CF_RETURNS_RETAINED CFStringRef _Nullable _CFXMLDTDExternalID(_CFXMLDTDPtr dtd) {
const unsigned char* externalID = ((xmlDtdPtr)dtd)->ExternalID;
if (externalID) {
return CFStringCreateWithCString(NULL, (const char*)externalID, kCFStringEncodingUTF8);
}
return NULL;
}
void _CFXMLDTDSetExternalID(_CFXMLDTDPtr dtd, const unsigned char* externalID) {
xmlDtdPtr dtdPtr = (xmlDtdPtr)dtd;
if (dtdPtr->ExternalID) {
xmlDictPtr dict = dtdPtr->doc ? dtdPtr->doc->dict : NULL;
if (dict) {
if (!xmlDictOwns(dict, dtdPtr->ExternalID)) {