- Notifications
You must be signed in to change notification settings - Fork 7.8k
/
Copy pathxml_serializer.c
1326 lines (1137 loc) · 49.5 KB
/
xml_serializer.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
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Niels Dossche <nielsdos@php.net> |
+----------------------------------------------------------------------+
*/
#ifdefHAVE_CONFIG_H
#include<config.h>
#endif
#include"php.h"
#if defined(HAVE_LIBXML) && defined(HAVE_DOM)
#include"xml_serializer.h"
#include"private_data.h"
#include"namespace_compat.h"
#include"serialize_common.h"
#include"internal_helpers.h"
#include<libxml/chvalid.h>
// TODO: implement iterative approach instead of recursive?
/* This file implements the XML serialization algorithm.
* https://w3c.github.io/DOM-Parsing/#dom-xmlserializer-serializetostring (Date 2021-05-02)
*
* The following are spec issues that were fixed in this implementation, but are not yet fixed
* in the spec itself:
* https://github.com/w3c/DOM-Parsing/issues/28
* https://github.com/w3c/DOM-Parsing/issues/29
* https://github.com/w3c/DOM-Parsing/issues/38
* https://github.com/w3c/DOM-Parsing/issues/43
* https://github.com/w3c/DOM-Parsing/issues/44
* https://github.com/w3c/DOM-Parsing/issues/45
* https://github.com/w3c/DOM-Parsing/issues/47
* https://github.com/w3c/DOM-Parsing/issues/50
* https://github.com/w3c/DOM-Parsing/issues/52
* https://github.com/w3c/DOM-Parsing/issues/59
* https://github.com/w3c/DOM-Parsing/issues/71
*/
#defineTRY(x) do { if (UNEXPECTED((x) < 0)) { return -1; } } while (0)
#defineTRY_OR_CLEANUP(x) do { if (UNEXPECTED((x) < 0)) { goto cleanup; } } while (0)
#definexmlOutputBufferWriteLit(out, literal) xmlOutputBufferWrite((out), strlen("" literal), "" literal)
/* https://w3c.github.io/DOM-Parsing/#dfn-namespace-prefix-map
* This associates a namespace uri with a list of possible prefixes. */
typedefstruct {
HashTable*ht;
} dom_xml_ns_prefix_map;
/* https://w3c.github.io/DOM-Parsing/#dfn-local-prefixes-map */
typedefstruct {
HashTableht;
} dom_xml_local_prefix_map;
typedefstruct {
constxmlChar*prefix, *name;
} dom_qname_pair;
typedefstructdom_xml_serialize_ctx {
xmlSaveCtxtPtrctxt;
xmlOutputBufferPtrout;
php_dom_private_data*private_data;
} dom_xml_serialize_ctx;
staticintdom_xml_serialization_algorithm(
dom_xml_serialize_ctx*ctx,
dom_xml_ns_prefix_map*namespace_prefix_map,
xmlNodePtrnode,
constxmlChar*namespace,
unsigned int*prefix_index,
intindent,
boolrequire_well_formed
);
staticbooldom_xml_str_equals_treat_nulls_as_empty(constxmlChar*s1, constxmlChar*s2)
{
if (s1==s2) {
return true;
}
if (s1==NULL) {
returns2==NULL||*s2=='\0';
}
if (s2==NULL) {
/* Note: at this point we know that s1 != NULL. */
return*s1=='\0';
}
returnstrcmp((constchar*) s1, (constchar*) s2) ==0;
}
staticzend_always_inlinebooldom_xml_str_equals_treat_nulls_as_nulls(constxmlChar*s1, constxmlChar*s2)
{
if (s1==s2) {
return true;
}
if (s1==NULL||s2==NULL) {
return false;
}
returnstrcmp((constchar*) s1, (constchar*) s2) ==0;
}
staticzend_always_inlinevoiddom_xml_ns_prefix_map_ctor(dom_xml_ns_prefix_map*map)
{
ALLOC_HASHTABLE(map->ht);
zend_hash_init(map->ht, 8, NULL, NULL, false);
}
staticvoiddom_xml_ns_prefix_map_destroy(dom_xml_ns_prefix_map*map)
{
HashTable*list;
ZEND_HASH_MAP_FOREACH_PTR(map->ht, list) {
if (GC_DELREF(list) ==0) {
zval*tmp;
ZEND_HASH_PACKED_FOREACH_VAL(list, tmp) {
if (DOM_Z_IS_OWNED(tmp)) {
efree(Z_PTR_P(tmp));
}
} ZEND_HASH_FOREACH_END();
zend_hash_destroy(list);
efree(list);
}
} ZEND_HASH_FOREACH_END();
zend_hash_destroy(map->ht);
efree(map->ht);
map->ht=NULL;
}
staticzend_always_inlinevoiddom_xml_ns_prefix_map_dtor(dom_xml_ns_prefix_map*map)
{
if (GC_DELREF(map->ht) ==0) {
dom_xml_ns_prefix_map_destroy(map);
}
}
staticzend_always_inlinevoiddom_xml_ns_prefix_map_copy(dom_xml_ns_prefix_map*dst, constdom_xml_ns_prefix_map*src)
{
dst->ht=src->ht;
GC_ADDREF(dst->ht);
}
staticzend_always_inlinevoiddom_xml_local_prefix_map_ctor(dom_xml_local_prefix_map*map)
{
zend_hash_init(&map->ht, 8, NULL, NULL, false);
}
staticzend_always_inlinevoiddom_xml_local_prefix_map_dtor(dom_xml_local_prefix_map*map)
{
zend_hash_destroy(&map->ht);
}
staticzend_always_inlinevoiddom_xml_local_prefix_map_add(
dom_xml_local_prefix_map*map,
constxmlChar*prefix,
size_tprefix_len,
constxmlChar*ns
)
{
ZEND_ASSERT(prefix!=NULL);
zend_hash_str_add_ptr(&map->ht, (constchar*) prefix, prefix_len, (void*) ns);
}
staticzend_always_inlineconstxmlChar*dom_xml_local_prefix_map_find(
constdom_xml_local_prefix_map*map,
constxmlChar*prefix,
size_tprefix_len
)
{
ZEND_ASSERT(prefix!=NULL);
returnzend_hash_str_find_ptr(&map->ht, (constchar*) prefix, prefix_len);
}
staticzend_always_inlinebooldom_xml_local_prefix_map_conflicts(
constdom_xml_local_prefix_map*map,
constxmlChar*prefix,
size_tprefix_len,
constxmlChar*ns
)
{
constxmlChar*result=dom_xml_local_prefix_map_find(map, prefix, prefix_len);
if (result==NULL) {
return false;
}
return !dom_xml_str_equals_treat_nulls_as_empty(result, ns);
}
staticzend_always_inlinebooldom_xml_local_prefix_map_contains(
constdom_xml_local_prefix_map*map,
constxmlChar*prefix,
size_tprefix_len
)
{
returndom_xml_local_prefix_map_find(map, prefix, prefix_len) !=NULL;
}
/* https://w3c.github.io/DOM-Parsing/#dfn-add */
staticvoiddom_xml_ns_prefix_map_add(
dom_xml_ns_prefix_map*map,
constxmlChar*prefix,
boolprefix_owned,
constxmlChar*ns,
size_tns_length
)
{
ZEND_ASSERT(map->ht!=NULL);
ZEND_ASSERT(prefix!=NULL);
if (ns==NULL) {
ns=BAD_CAST"";
}
if (GC_REFCOUNT(map->ht) >1) {
GC_DELREF(map->ht);
map->ht=zend_array_dup(map->ht);
HashTable*list;
ZEND_HASH_MAP_FOREACH_PTR(map->ht, list) {
GC_ADDREF(list);
} ZEND_HASH_FOREACH_END();
}
/* 1. Let candidates list be the result of retrieving a list from map where there exists a key in map
* that matches the value of ns
* or if there is no such key, then let candidates list be null. */
HashTable*list=zend_hash_str_find_ptr(map->ht, (constchar*) ns, ns_length);
/* 2. If candidates list is null, then create a new list with prefix as the only item in the list,
* and associate that list with a new key ns in map. */
if (list==NULL) {
ALLOC_HASHTABLE(list);
zend_hash_init(list, 8, NULL, NULL, false);
zend_hash_str_add_new_ptr(map->ht, (constchar*) ns, ns_length, list);
} elseif (GC_REFCOUNT(list) >1) {
GC_DELREF(list);
list=zend_array_dup(list);
zend_hash_str_update_ptr(map->ht, (constchar*) ns, ns_length, list);
}
/* 3. (Otherwise), append prefix to the end of candidates list. */
zvaltmp;
if (prefix_owned) {
DOM_Z_OWNED(&tmp, prefix);
} else {
DOM_Z_UNOWNED(&tmp, prefix);
}
zend_hash_next_index_insert_new(list, &tmp);
}
/* https://w3c.github.io/DOM-Parsing/#dfn-found */
staticzend_always_inlineHashTable*dom_get_candidates_list(dom_xml_ns_prefix_map*map, constxmlChar*ns, size_tns_length)
{
ZEND_ASSERT(map->ht!=NULL);
/* 1. Let candidates list be the result of retrieving a list from map where there exists a key in map that matches
* the value of ns
* or if there is no such key, then let candidates list be null. */
returnzend_hash_str_find_ptr(map->ht, (constchar*) ns, ns_length);
}
/* https://w3c.github.io/DOM-Parsing/#dfn-found */
staticzend_always_inlinebooldom_prefix_in_candidate_list(constHashTable*list, constxmlChar*prefix)
{
ZEND_ASSERT(prefix!=NULL);
if (list==NULL) {
return false;
}
/* 2. If the value of prefix occurs at least once in candidates list, return true, otherwise return false. */
constchar*tmp;
ZEND_HASH_PACKED_FOREACH_PTR(list, tmp) {
if (dom_xml_str_equals_treat_nulls_as_empty(BAD_CASTtmp, prefix)) {
return true;
}
} ZEND_HASH_FOREACH_END();
return false;
}
/* https://w3c.github.io/DOM-Parsing/#dfn-found */
staticzend_always_inlinebooldom_prefix_found_in_ns_prefix_map(
dom_xml_ns_prefix_map*map,
constxmlChar*prefix,
constxmlChar*ns,
size_tns_length
)
{
ZEND_ASSERT(ns!=NULL);
HashTable*list=dom_get_candidates_list(map, ns, ns_length);
returndom_prefix_in_candidate_list(list, prefix);
}
/* Helper to get the attribute value, will return "" instead of NULL for empty values, to mimic getAttribute()'s behaviour. */
staticzend_always_inlineconstxmlChar*dom_get_attribute_value(constxmlAttr*attr)
{
if (attr->children==NULL) {
returnBAD_CAST"";
}
returnattr->children->content ? attr->children->content : BAD_CAST"";
}
/* https://w3c.github.io/DOM-Parsing/#dfn-recording-the-namespace-information */
staticconstxmlChar*dom_recording_the_namespace_information(
dom_xml_ns_prefix_map*namespace_prefix_map,
dom_xml_local_prefix_map*local_prefixes_map,
xmlNodePtrelement
)
{
ZEND_ASSERT(element->type==XML_ELEMENT_NODE);
/* 1. Let default namespace attr value be null. */
constxmlChar*default_namespace_attr_value=NULL;
/* 2. [MAIN] For each attribute attr in element's attributes, in the order they are specified in the element's attribute list: */
for (xmlAttrPtrattr=element->properties; attr!=NULL; attr=attr->next) {
/* Steps 2.1-2.2 fetch namespace information from the attribute, but let's defer that for simplicity to the if's body. */
/* 2.3. If the attribute namespace is the XMLNS namespace, then: */
if (php_dom_ns_is_fast((xmlNodePtr) attr, php_dom_ns_is_xmlns_magic_token)) {
/* 2.3.1. If attribute prefix is null, then attr is a default namespace declaration.
* Set the default namespace attr value to attr's value and stop running these steps,
* returning to Main to visit the next attribute. */
if (attr->ns->prefix==NULL) {
default_namespace_attr_value=dom_get_attribute_value(attr);
continue;
}
/* 2.3.2. Otherwise, the attribute prefix is not null and attr is a namespace prefix definition.
* Run the following steps: */
else {
/* 2.3.2.1. Let prefix definition be the value of attr's localName. */
constxmlChar*prefix_definition=attr->name;
ZEND_ASSERT(prefix_definition!=NULL);
/* 2.3.2.2. Let namespace definition be the value of attr's value. */
constxmlChar*namespace_definition=dom_get_attribute_value(attr);
ZEND_ASSERT(namespace_definition!=NULL);
/* 2.3.2.3. If namespace definition is the XML namespace, then stop running these steps,
* and return to Main to visit the next attribute. */
if (strcmp((constchar*) namespace_definition, DOM_XML_NS_URI) ==0) {
continue;
}
/* 2.3.2.4. If namespace definition is the empty string (the declarative form of having no namespace),
* then let namespace definition be null instead.
* => This gets delayed until later down. */
size_tnamespace_definition_length=strlen((constchar*) namespace_definition);
/* 2.3.2.5. If prefix definition is found in map given the namespace namespace definition,
* then stop running these steps, and return to Main to visit the next attribute. */
if (dom_prefix_found_in_ns_prefix_map(namespace_prefix_map, prefix_definition, namespace_definition, namespace_definition_length)) {
continue;
}
/* Delayed step 2.3.2.4 */
if (*namespace_definition=='\0') {
namespace_definition=NULL;
}
/* 2.3.2.6. Add the prefix prefix definition to map given namespace namespace definition. */
dom_xml_ns_prefix_map_add(namespace_prefix_map, prefix_definition, false, namespace_definition, namespace_definition_length);
/* 2.3.2.7. Add the value of prefix definition as a new key to the local prefixes map,
* with the namespace definition as the key's value replacing the value of null with the empty string if applicable. */
size_tprefix_definition_length=strlen((constchar*) prefix_definition);
namespace_definition=namespace_definition==NULL ? BAD_CAST"" : namespace_definition;
dom_xml_local_prefix_map_add(local_prefixes_map, prefix_definition, prefix_definition_length, namespace_definition);
}
}
}
/* 3. Return the value of default namespace attr value. */
returndefault_namespace_attr_value;
}
/* https://w3c.github.io/DOM-Parsing/#dfn-retrieving-a-preferred-prefix-string */
staticconstxmlChar*dom_retrieve_a_preferred_prefix_string(
dom_xml_ns_prefix_map*namespace_prefix_map,
dom_xml_local_prefix_map*local_prefixes_map,
constxmlChar*preferred_prefix,
constxmlChar*ns,
size_tns_length
)
{
ZEND_ASSERT(namespace_prefix_map->ht!=NULL);
if (ns==NULL) {
ns=BAD_CAST"";
}
/* 1. Let candidates list be the result of retrieving a list from map where there exists a key in map that matches
* the value of ns or if there is no such key, then stop running these steps, and return the null value. */
HashTable*list=dom_get_candidates_list(namespace_prefix_map, ns, ns_length);
if (list==NULL) {
returnNULL;
}
/* 2. Otherwise, for each prefix value prefix in candidates list, iterating from beginning to end: */
constxmlChar*prefix=NULL;
constxmlChar*last_non_conflicting_in_list=NULL;
/* Reverse so that the "nearest" ns gets priority: https://github.com/w3c/DOM-Parsing/issues/45 */
ZEND_HASH_PACKED_REVERSE_FOREACH_PTR(list, prefix) {
ZEND_ASSERT(prefix!=NULL);
/* 2.1. If prefix matches preferred prefix, then stop running these steps and return prefix. */
/* Adapted for https://github.com/w3c/DOM-Parsing/issues/45 */
if (!dom_xml_local_prefix_map_conflicts(local_prefixes_map, prefix, strlen((constchar*) prefix), ns)) {
if (dom_xml_str_equals_treat_nulls_as_empty(preferred_prefix, prefix)) {
returnprefix;
}
if (last_non_conflicting_in_list==NULL) {
last_non_conflicting_in_list=prefix;
}
}
} ZEND_HASH_FOREACH_END();
/* 2.2. If prefix is the last item in the candidates list, then stop running these steps and return prefix. */
/* Note: previously the last item was "prefix", but we loop backwards now. */
returnlast_non_conflicting_in_list;
}
/* https://w3c.github.io/DOM-Parsing/#dfn-generating-a-prefix */
staticxmlChar*dom_xml_generate_a_prefix(
dom_xml_ns_prefix_map*map,
dom_xml_local_prefix_map*local_prefixes_map,
constxmlChar*new_namespace,
size_tnew_namespace_length,
unsigned int*prefix_index
)
{
/* 1. Let generated prefix be the concatenation of the string "ns" and the current numerical value of prefix index. */
charbuffer[32];
buffer[0] ='n';
buffer[1] ='s';
size_tlength;
do {
length=snprintf(buffer+2, sizeof(buffer) -2, "%u", *prefix_index) +2;
/* 2. Let the value of prefix index be incremented by one. */
(*prefix_index)++;
/* Loop condition is for https://github.com/w3c/DOM-Parsing/issues/44 */
} while (dom_xml_local_prefix_map_contains(local_prefixes_map, (constxmlChar*) buffer, length));
xmlChar*generated_prefix=emalloc(length+1);
memcpy(generated_prefix, buffer, length+1);
/* 3. Add to map the generated prefix given the new namespace namespace. */
dom_xml_ns_prefix_map_add(map, generated_prefix, true, new_namespace, new_namespace_length);
/* Continuation of https://github.com/w3c/DOM-Parsing/issues/44 */
dom_xml_local_prefix_map_add(local_prefixes_map, generated_prefix, length, new_namespace);
/* 4. Return the value of generated prefix. */
returngenerated_prefix;
}
staticintdom_xml_output_qname(xmlOutputBufferPtrout, constdom_qname_pair*qname)
{
if (qname->prefix!=NULL) {
TRY(xmlOutputBufferWriteString(out, (constchar*) qname->prefix));
TRY(xmlOutputBufferWriteLit(out, ":"));
}
returnxmlOutputBufferWriteString(out, (constchar*) qname->name);
}
/* This is a utility method used by both
* https://w3c.github.io/DOM-Parsing/#dfn-xml-serializing-an-element-node
* and https://w3c.github.io/DOM-Parsing/#dfn-serializing-an-attribute-value */
staticintdom_xml_common_text_serialization(xmlOutputBufferPtrout, constchar*content, boolattribute_mode)
{
if (content==NULL) {
return0;
}
constchar*last_output=content;
constchar*mask=attribute_mode ? "&<>\"\t\n\r" : "&<>";
while (true) {
size_tchunk_length=strcspn(content, mask);
content+=chunk_length;
if (*content=='\0') {
break;
}
TRY(xmlOutputBufferWrite(out, content-last_output, last_output));
switch (*content) {
case'&': {
TRY(xmlOutputBufferWriteLit(out, "&"));
break;
}
case'<': {
TRY(xmlOutputBufferWriteLit(out, "<"));
break;
}
case'>': {
TRY(xmlOutputBufferWriteLit(out, ">"));
break;
}
case'"': {
TRY(xmlOutputBufferWriteLit(out, """));
break;
}
/* The following three are added to address https://github.com/w3c/DOM-Parsing/issues/59 */
case'\t': {
TRY(xmlOutputBufferWriteLit(out, "	"));
break;
}
case'\n': {
TRY(xmlOutputBufferWriteLit(out, " "));
break;
}
case'\r': {
TRY(xmlOutputBufferWriteLit(out, " "));
break;
}
}
content++;
last_output=content;
}
returnxmlOutputBufferWrite(out, content-last_output, last_output);
}
staticintdom_xml_check_char_production(constxmlChar*content)
{
// TODO: optimization idea: fast-pass for ASCII-only data
constxmlChar*ptr=content;
while (*ptr!='\0') {
intlen=4;
intc=xmlGetUTF8Char(ptr, &len);
if (c<0|| !xmlIsCharQ(c)) {
return-1;
}
ptr+=len;
}
return0;
}
/* https://w3c.github.io/DOM-Parsing/#xml-serializing-a-text-node */
staticzend_always_inlineintdom_xml_serialize_text_node(xmlOutputBufferPtrout, xmlNodePtrtext, boolrequire_well_formed)
{
/* 1. If the require well-formed flag is set and node's data contains characters that are not matched by the XML Char production,
* then throw an exception. */
if (require_well_formed&&text->content!=NULL) {
TRY(dom_xml_check_char_production(text->content));
}
returndom_xml_common_text_serialization(out, (constchar*) text->content, false);
}
staticzend_always_inlineconstxmlChar*dom_xml_attribute_namespace(constxmlAttr*attr)
{
returnattr->ns==NULL ? NULL : attr->ns->href;
}
staticintdom_xml_serialize_attribute_node_value(xmlOutputBufferPtrout, xmlAttrPtrattr)
{
TRY(xmlOutputBufferWriteString(out, (constchar*) attr->name));
TRY(xmlOutputBufferWriteLit(out, "=\""));
for (xmlNodePtrchild=attr->children; child!=NULL; child=child->next) {
if (child->type==XML_TEXT_NODE) {
if (child->content!=NULL) {
TRY(dom_xml_common_text_serialization(out, (constchar*) child->content, true));
}
} elseif (child->type==XML_ENTITY_REF_NODE) {
TRY(xmlOutputBufferWriteLit(out, "&"));
TRY(dom_xml_common_text_serialization(out, (constchar*) child->name, true));
TRY(xmlOutputBufferWriteLit(out, ";"));
}
}
returnxmlOutputBufferWriteLit(out, "\"");
}
/* These steps are from the attribute serialization algorithm's well-formed checks.
* Note that this does not return a boolean but an int to be compatible with the TRY/TRY_CLEANUP interface
* that we do for compatibility with libxml's interfaces. */
staticzend_always_inlineintdom_xml_check_xmlns_attribute_requirements(constxmlAttr*attr, constxmlChar*candidate_prefix)
{
constxmlChar*attr_value=dom_get_attribute_value(attr);
/* 3.5.2.2. If the require well-formed flag is set and the value of attr's value attribute matches the XMLNS namespace, then throw an exception */
if (strcmp((constchar*) attr_value, DOM_XMLNS_NS_URI) ==0) {
return-1;
}
/* 3.5.2.3. If the require well-formed flag is set and the value of attr's value attribute is the empty string.
* Errata: an "xmlns" attribute is allowed but not one with a prefix, so the idea in the spec is right but the description isn't. */
if (*attr_value=='\0'&&candidate_prefix!=NULL) {
return-1;
}
return0;
}
/* Spec says to do nothing, but that's inconsistent/wrong, see https://github.com/w3c/DOM-Parsing/issues/28
* This does not have a require_well_formed argument because the only way to get here is via saveXML(), which has it off. */
staticintdom_xml_serialize_attribute_node(xmlOutputBufferPtrout, xmlNodePtrattr)
{
if (attr->ns!=NULL&&attr->ns->prefix!=NULL) {
TRY(xmlOutputBufferWriteString(out, (constchar*) attr->ns->prefix));
TRY(xmlOutputBufferWriteLit(out, ":"));
}
returndom_xml_serialize_attribute_node_value(out, (xmlAttrPtr) attr);
}
/* https://w3c.github.io/DOM-Parsing/#dfn-xml-serializing-a-comment-node */
staticintdom_xml_serialize_comment_node(xmlOutputBufferPtrout, xmlNodePtrcomment, boolrequire_well_formed)
{
/* Step 1 deals with well-formed flag */
if (require_well_formed) {
/* node's data contains characters that are not matched by the XML Char production or contains "--"
* (two adjacent U+002D HYPHEN-MINUS characters) or that ends with a "-" (U+002D HYPHEN-MINUS) character,
* then throw an exception */
constxmlChar*ptr=comment->content;
if (ptr!=NULL) {
TRY(dom_xml_check_char_production(ptr));
if (strstr((constchar*) ptr, "--") !=NULL||ptr[strlen((constchar*) ptr) -1] =='-') {
return-1;
}
}
}
TRY(xmlOutputBufferWriteLit(out, "<!--"));
if (EXPECTED(comment->content!=NULL)) {
TRY(xmlOutputBufferWriteString(out, (constchar*) comment->content));
}
returnxmlOutputBufferWriteLit(out, "-->");
}
/* https://w3c.github.io/DOM-Parsing/#xml-serializing-a-processinginstruction-node */
staticintdom_xml_serialize_processing_instruction(xmlOutputBufferPtrout, xmlNodePtrpi, boolrequire_well_formed)
{
/* Steps 1-2 deal with well-formed flag */
if (require_well_formed) {
/* target contains a ":" (U+003A COLON) character or is an ASCII case-insensitive match for the string "xml", then throw an exception */
if (strchr((constchar*) pi->name, ':') !=NULL||strcasecmp((constchar*) pi->name, "xml") ==0) {
return-1;
}
/* node's data contains characters that are not matched by the XML Char production or contains the string "?>"
* (U+003F QUESTION MARK, U+003E GREATER-THAN SIGN), then throw an exception */
if (pi->content!=NULL) {
TRY(dom_xml_check_char_production(pi->content));
if (strstr((constchar*) pi->content, "?>") !=NULL) {
return-1;
}
}
}
TRY(xmlOutputBufferWriteLit(out, "<?"));
TRY(xmlOutputBufferWriteString(out, (constchar*) pi->name));
TRY(xmlOutputBufferWriteLit(out, " "));
if (EXPECTED(pi->content!=NULL)) {
TRY(xmlOutputBufferWriteString(out, (constchar*) pi->content));
}
returnxmlOutputBufferWriteLit(out, "?>");
}
/* https://github.com/w3c/DOM-Parsing/issues/38
* and https://github.com/w3c/DOM-Parsing/blob/ab8d1ac9699ed43ae6de9f4be2b0f3cfc5f3709e/index.html#L1510 */
staticintdom_xml_serialize_cdata_section_node(xmlOutputBufferPtrout, xmlNodePtrcdata)
{
TRY(xmlOutputBufferWriteLit(out, "<![CDATA["));
if (EXPECTED(cdata->content!=NULL)) {
TRY(xmlOutputBufferWriteString(out, (constchar*) cdata->content));
}
returnxmlOutputBufferWriteLit(out, "]]>");
}
staticzend_string*dom_xml_create_localname_set_key(constxmlAttr*attr)
{
if (attr->ns==NULL||attr->ns->href==NULL) {
returnzend_string_init((constchar*) attr->name, strlen((constchar*) attr->name), false);
}
/* Spec requires us to create a tuple as a key, however HashTable doesn't support that natively.
* Fortunately, href and name cannot have embedded NUL bytes in them, so we can create a
* "tuple" by concatenating them against each other, separated by a \0 byte.
*/
returnzend_string_concat3(
(constchar*) attr->ns->href, strlen((constchar*) attr->ns->href),
"", 1, /* include the \0 */
(constchar*) attr->name, strlen((constchar*) attr->name)
);
}
/* https://w3c.github.io/DOM-Parsing/#dfn-xml-serialization-of-the-attributes */
staticintdom_xml_serialize_attributes(
xmlOutputBufferPtrout,
xmlNodePtrelement,
dom_xml_ns_prefix_map*map,
dom_xml_local_prefix_map*local_prefixes_map,
unsigned int*prefix_index,
boolignore_namespace_definition_attribute,
boolrequire_well_formed
)
{
/* 1. Let result be the empty string.
* => We're going to write directly to the output buffer. */
/* 2. Let localname set be a new empty namespace localname set.
* We can do this unconditionally even if we don't use it, because this doesn't allocate memory anyway. */
HashTablelocalname_set;
zend_hash_init(&localname_set, 8, NULL, NULL, false);
/* 3. [LOOP] For each attribute attr in element's attributes, in the order they are specified in the element's attribute list: */
for (xmlAttrPtrattr=element->properties; attr!=NULL; attr=attr->next) {
if (require_well_formed) {
zend_string*key=dom_xml_create_localname_set_key(attr);
/* 3.1. If the require well-formed flag is set and the localname set contains a tuple whose values match those of a
* new tuple consisting of attr's namespaceURI attribute and localName attribute, then throw an exception
* 3.2. Create a new tuple consisting of attr's namespaceURI attribute and localName attribute, and add it to the localname set. */
boolduplicate=zend_hash_add_empty_element(&localname_set, key) ==NULL;
zend_string_release_ex(key, false);
if (duplicate) {
goto cleanup;
}
}
/* 3.3. Let attribute namespace be the value of attr's namespaceURI value. */
constxmlChar*attribute_namespace=dom_xml_attribute_namespace(attr);
/* 3.4. Let candidate prefix be null. */
constxmlChar*candidate_prefix=NULL;
/* 3.5. If attribute namespace is not null, then run these sub-steps: */
if (attribute_namespace!=NULL) {
/* 3.5.1. Let candidate prefix be the result of retrieving a preferred prefix string from map
* given namespace attribute namespace with preferred prefix being attr's prefix value. */
candidate_prefix=dom_retrieve_a_preferred_prefix_string(
map,
local_prefixes_map,
attr->ns->prefix,
attribute_namespace,
strlen((constchar*) attribute_namespace)
);
/* 3.5.2. If the value of attribute namespace is the XMLNS namespace, then run these steps: */
if (php_dom_ns_is_fast((xmlNodePtr) attr, php_dom_ns_is_xmlns_magic_token)) {
constxmlChar*attr_value=dom_get_attribute_value(attr);
/* 3.5.2.1. If any of the following are true, then stop running these steps and goto Loop to visit the next attribute: */
/* the attr's value is the XML namespace; */
if (strcmp((constchar*) attr_value, DOM_XML_NS_URI) ==0) {
continue;
}
/* the attr's prefix is null and the ignore namespace definition attribute flag is true */
if (ignore_namespace_definition_attribute&&attr->ns->prefix==NULL) {
/* https://github.com/w3c/DOM-Parsing/issues/47 */
if (!dom_xml_str_equals_treat_nulls_as_empty(element->ns==NULL ? NULL : element->ns->href, attr_value)) {
continue;
}
}
/* the attr's prefix is not null and either */
if (attr->ns->prefix!=NULL) {
/* the attr's localName is not a key contained in the local prefixes map
* or the attr's localName is present in the local prefixes map but the value of the key does not match attr's value
* and furthermore that the attr's localName (as the prefix to find) is found in the namespace prefix map
* given the namespace consisting of the attr's value */
constxmlChar*value=dom_xml_local_prefix_map_find(local_prefixes_map, attr->name, strlen((constchar*) attr->name));
if (value==NULL||strcmp((constchar*) value, (constchar*) attr_value) !=0) {
if (dom_prefix_found_in_ns_prefix_map(map, attr->name, attr_value, strlen((constchar*) attr_value))) {
continue;
}
}
}
/* 3.5.2.4. the attr's prefix matches the string "xmlns", then let candidate prefix be the string "xmlns". */
if (attr->ns->prefix!=NULL&&strcmp((constchar*) attr->ns->prefix, "xmlns") ==0) {
candidate_prefix=BAD_CAST"xmlns";
}
/* Errata: step 3.5.2.3 can only really be checked if we already know the candidate prefix. */
if (require_well_formed) {
/* 3.5.2.2 and 3.5.2.3 are done by this call. */
TRY_OR_CLEANUP(dom_xml_check_xmlns_attribute_requirements(attr, candidate_prefix));
}
}
/* 3.5.3. Otherwise, the attribute namespace in not the XMLNS namespace. Run these steps: */
elseif (candidate_prefix==NULL) { /* https://github.com/w3c/DOM-Parsing/issues/29 */
/* Continuation of https://github.com/w3c/DOM-Parsing/issues/29 */
if (attr->ns->prefix==NULL
||dom_xml_local_prefix_map_contains(local_prefixes_map, attr->ns->prefix, strlen((constchar*) attr->ns->prefix))) {
/* 3.5.3.1. Let candidate prefix be the result of generating a prefix providing map,
* attribute namespace, and prefix index as input. */
candidate_prefix=dom_xml_generate_a_prefix(
map,
local_prefixes_map,
attribute_namespace,
strlen((constchar*) attribute_namespace),
prefix_index
);
} else {
candidate_prefix=attr->ns->prefix;
/* Continuation of https://github.com/w3c/DOM-Parsing/issues/29 */
dom_xml_ns_prefix_map_add(
map,
candidate_prefix,
false,
attribute_namespace,
strlen((constchar*) attribute_namespace)
);
dom_xml_local_prefix_map_add(
local_prefixes_map,
candidate_prefix,
strlen((constchar*) candidate_prefix),
attribute_namespace
);
}
/* 3.5.3.2. Append the following to result, in the order listed: */
TRY_OR_CLEANUP(xmlOutputBufferWriteLit(out, " xmlns:"));
TRY_OR_CLEANUP(xmlOutputBufferWriteString(out, (constchar*) candidate_prefix));
TRY_OR_CLEANUP(xmlOutputBufferWriteLit(out, "=\""));
TRY_OR_CLEANUP(dom_xml_common_text_serialization(out, (constchar*) attribute_namespace, true));
TRY_OR_CLEANUP(xmlOutputBufferWriteLit(out, "\""));
}
}
/* 3.6. Append a " " (U+0020 SPACE) to result. */
TRY_OR_CLEANUP(xmlOutputBufferWriteLit(out, " "));
/* 3.7. If candidate prefix is not null, then append to result the concatenation of candidate prefix with ":" (U+003A COLON). */
if (candidate_prefix!=NULL) {
TRY_OR_CLEANUP(xmlOutputBufferWriteString(out, (constchar*) candidate_prefix));
TRY_OR_CLEANUP(xmlOutputBufferWriteLit(out, ":"));
}
if (require_well_formed) {
/* 3.8. If the require well-formed flag is set and
* this attr's localName attribute contains the character ":" (U+003A COLON)
* or does not match the XML Name production
* or equals "xmlns" and attribute namespace is null */
if (xmlValidateNCName(attr->name, /* space */0) !=0
|| (strcmp((constchar*) attr->name, "xmlns") ==0&&dom_xml_attribute_namespace(attr) ==NULL)) {
goto cleanup;
}
}
/* 3.9. Append the following strings to result, in the order listed: */
TRY_OR_CLEANUP(dom_xml_serialize_attribute_node_value(out, attr));
}
/* 4. Return the value of result.
* => We're writing directly to the output buffer. */
zend_hash_destroy(&localname_set);
return0;
cleanup:
zend_hash_destroy(&localname_set);
return-1;
}
/* Only format output if there are no text/entityrefs/cdata nodes as children. */
staticbooldom_xml_should_format_element(xmlNodePtrelement)
{
xmlNodePtrchild=element->children;
ZEND_ASSERT(child!=NULL);
do {
if (child->type==XML_TEXT_NODE||child->type==XML_ENTITY_REF_NODE||child->type==XML_CDATA_SECTION_NODE) {
return false;
}
child=child->next;
} while (child!=NULL);
return true;
}
staticintdom_xml_output_indents(xmlOutputBufferPtrout, intindent)
{
TRY(xmlOutputBufferWriteLit(out, "\n"));
for (inti=0; i<indent; i++) {
TRY(xmlOutputBufferWriteLit(out, " "));
}
return0;
}
/* https://w3c.github.io/DOM-Parsing/#dfn-xml-serializing-an-element-node */
staticintdom_xml_serialize_element_node(
dom_xml_serialize_ctx*ctx,
constxmlChar*namespace,
dom_xml_ns_prefix_map*namespace_prefix_map,
xmlNodePtrelement,
unsigned int*prefix_index,
intindent,
boolrequire_well_formed
)
{
/* 1. If the require well-formed flag is set and this node's localName attribute contains
* the character ":" (U+003A COLON) or does not match the XML Name production, then throw an exception. */
if (require_well_formed) {
if (xmlValidateNCName(element->name, /* space */0) !=0) {
return-1;
}
}
boolshould_format=indent >= 0&&element->children!=NULL&&dom_xml_should_format_element(element);
/* 2. Let markup be the string "<" (U+003C LESS-THAN SIGN). */
TRY(xmlOutputBufferWriteLit(ctx->out, "<"));
/* 3. Let qualified name be an empty string.
* => We're going to do it a bit differently.
* To avoid string allocations, we're going to store the qualified name separately as prefix+name.
* If the prefix is NULL then the qualified name will be == name, otherwise == prefix:name. */
dom_qname_pairqualified_name= { NULL, NULL };
/* 4. Let skip end tag be a boolean flag with value false. */
boolskip_end_tag= false;
/* 5. Let ignore namespace definition attribute be a boolean flag with value false. */
boolignore_namespace_definition_attribute= false;
/* 6. Given prefix map, copy a namespace prefix map and let map be the result. */
dom_xml_ns_prefix_mapmap;
dom_xml_ns_prefix_map_copy(&map, namespace_prefix_map);
/* 7. Let local prefixes map be an empty map. */
dom_xml_local_prefix_maplocal_prefixes_map;
dom_xml_local_prefix_map_ctor(&local_prefixes_map);
/* 8. Let local default namespace be the result of recording the namespace information for node given map and local prefixes map. */
constxmlChar*local_default_namespace=dom_recording_the_namespace_information(&map, &local_prefixes_map, element);
/* 9. Let inherited ns be a copy of namespace. */
constxmlChar*inherited_ns=namespace;
/* 10. Let ns be the value of node's namespaceURI attribute. */
constxmlChar*constns=element->ns==NULL ? NULL : element->ns->href;
/* 11. If inherited ns is equal to ns, then: */
if (dom_xml_str_equals_treat_nulls_as_nulls(inherited_ns, ns)) {
/* 11.1. If local default namespace is not null, then set ignore namespace definition attribute to true. */
if (local_default_namespace!=NULL) {
ignore_namespace_definition_attribute= true;
}
/* 11.2. If ns is the XML namespace,
* then append to qualified name the concatenation of the string "xml:" and the value of node's localName. */
if (php_dom_ns_is_fast(element, php_dom_ns_is_xml_magic_token)) {
qualified_name.prefix=BAD_CAST"xml";
qualified_name.name=element->name;
}
/* 11.3. Otherwise, append to qualified name the value of node's localName. */
else {
qualified_name.name=element->name;
}
/* 11.4. Append the value of qualified name to markup. */
TRY_OR_CLEANUP(dom_xml_output_qname(ctx->out, &qualified_name));
}
/* 12. Otherwise, inherited ns is not equal to ns */
else {
/* 12.1. Let prefix be the value of node's prefix attribute. */
constxmlChar*prefix=element->ns==NULL ? NULL : element->ns->prefix;
/* 12.2. Let candidate prefix be the result of retrieving a preferred prefix string prefix from map given namespace ns. */
/* https://github.com/w3c/DOM-Parsing/issues/52 */
constxmlChar*candidate_prefix;
if (prefix==NULL&&dom_xml_str_equals_treat_nulls_as_empty(ns, local_default_namespace)) {
candidate_prefix=NULL;
} else {
size_tns_length=ns==NULL ? 0 : strlen((constchar*) ns);
candidate_prefix=dom_retrieve_a_preferred_prefix_string(&map, &local_prefixes_map, prefix, ns, ns_length);
}
/* 12.3. If the value of prefix matches "xmlns", then run the following steps: */
if (prefix!=NULL&&strcmp((constchar*) prefix, "xmlns") ==0) {
/* 12.3.1. If the require well-formed flag is set, then throw an error. */
if (require_well_formed) {
goto cleanup;
}
/* 12.3.2. Let candidate prefix be the value of prefix. */