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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
use std::{borrow::Cow, str::from_utf8_unchecked};
use crate::{
chvalid::XmlCharValid,
encoding::XmlCharEncoding,
error::XmlParserErrors,
libxml::sax2::xml_sax2_get_entity,
parser::{
XmlParserCtxt, XmlParserInputState, XmlParserMode, XmlParserOption,
parse_external_entity_private, xml_err_msg_str, xml_fatal_err, xml_fatal_err_msg,
xml_fatal_err_msg_int, xml_fatal_err_msg_str, xml_parse_balanced_chunk_memory_internal,
xml_warning_msg,
},
tree::{
NodeCommon, XML_ENT_CHECKED, XML_ENT_CHECKED_LT, XML_ENT_CONTAINS_LT, XML_ENT_EXPANDING,
XML_ENT_PARSED, XmlElementType, XmlEntityPtr, XmlEntityType, XmlGenericNodePtr, XmlNodePtr,
xml_doc_copy_node, xml_free_node_list, xml_get_predefined_entity,
},
};
impl XmlParserCtxt<'_> {
/// Parse a numeric character reference. Always consumes '&'.
///
/// ```text
/// [66] CharRef ::= '&#' [0-9]+ ';' | '&#x' [0-9a-fA-F]+ ';'
///
/// [ WFC: Legal Character ]
/// Characters referred to using character references must match the production for Char.
/// ```
///
/// Returns the value parsed (as an int), 0 in case of error
#[doc(alias = "xmlParseCharRef")]
pub(crate) fn parse_char_ref(&mut self) -> Option<char> {
let mut val = 0u32;
let mut count = 0;
// Using RAW/CUR/NEXT is okay since we are working on ASCII range here
if self.content_bytes().starts_with(b"&#x") {
self.advance(3);
self.grow();
while self.current_byte() != b';' {
// loop blocked by count
if count > 20 {
count = 0;
self.grow();
if matches!(self.instate, XmlParserInputState::XmlParserEOF) {
return None;
}
} else {
count += 1;
}
let cur = self.current_byte();
if cur.is_ascii_digit() {
val = val * 16 + (cur - b'0') as u32;
} else if (b'a'..=b'f').contains(&cur) && count < 20 {
val = val * 16 + (cur - b'a') as u32 + 10;
} else if (b'A'..=b'F').contains(&cur) && count < 20 {
val = val * 16 + (cur - b'A') as u32 + 10;
} else {
xml_fatal_err(self, XmlParserErrors::XmlErrInvalidHexCharRef, None);
val = 0;
break;
}
val = val.min(0x110000);
self.skip_char();
count += 1;
}
if self.current_byte() == b';' {
// on purpose to avoid reentrancy problems with NEXT and SKIP
self.input_mut().unwrap().col += 1;
self.input_mut().unwrap().cur += 1;
}
} else if self.current_byte() == b'&' && self.nth_byte(1) == b'#' {
self.advance(2);
self.grow();
while self.current_byte() != b';' {
// loop blocked by count
if count > 20 {
count = 0;
self.grow();
if matches!(self.instate, XmlParserInputState::XmlParserEOF) {
return None;
}
} else {
count += 1;
}
let cur = self.current_byte();
if cur.is_ascii_digit() {
val = val * 10 + (cur - b'0') as u32;
} else {
xml_fatal_err(self, XmlParserErrors::XmlErrInvalidDecCharRef, None);
val = 0;
break;
}
val = val.min(0x110000);
self.skip_char();
count += 1;
}
if self.current_byte() == b';' {
// on purpose to avoid reentrancy problems with NEXT and SKIP
self.input_mut().unwrap().col += 1;
self.input_mut().unwrap().cur += 1;
}
} else {
if self.current_byte() == b'&' {
self.advance(1);
}
xml_fatal_err(self, XmlParserErrors::XmlErrInvalidCharRef, None);
}
// [ WFC: Legal Character ]
// Characters referred to using character references must match the
// production for Char.
if val >= 0x110000 {
xml_fatal_err_msg_int!(
self,
XmlParserErrors::XmlErrInvalidChar,
"xmlParseCharRef: character reference out of bounds\n",
val as i32
);
} else if val.is_xml_char() {
return char::from_u32(val);
} else {
xml_fatal_err_msg_int!(
self,
XmlParserErrors::XmlErrInvalidChar,
format!("xmlParseCharRef: invalid XmlChar value {val}\n").as_str(),
val as i32
);
}
None
}
/// Parse Reference declarations, variant parsing from a string rather
/// than an an input flow.
///
/// ```text
/// [66] CharRef ::= '&#' [0-9]+ ';' | '&#x' [0-9a-fA-F]+ ';'
///
/// [ WFC: Legal Character ]
/// Characters referred to using character references must match the production for Char.
/// ```
///
/// Returns the value parsed (as an int), 0 in case of error, str will be
/// updated to the current value of the index
#[doc(alias = "xmlParseStringCharRef")]
pub(super) fn parse_string_char_ref<'a>(&mut self, s: &'a str) -> (Option<char>, &'a str) {
let mut val = 0;
let mut ptr = s;
if let Some(rem) = ptr.strip_prefix("&#x") {
ptr = rem;
if let Some((dig, rem)) = ptr
.split_once(';')
.filter(|(dig, _)| dig.bytes().all(|b| b.is_ascii_hexdigit()))
{
val = u32::from_str_radix(dig, 16).unwrap_or(0x110000);
ptr = rem;
} else {
xml_fatal_err(self, XmlParserErrors::XmlErrInvalidHexCharRef, None);
}
} else if let Some(rem) = ptr.strip_prefix("&#") {
ptr = rem;
if let Some((dig, rem)) = ptr
.split_once(';')
.filter(|(dig, _)| dig.bytes().all(|b| b.is_ascii_digit()))
{
val = dig.parse::<u32>().unwrap_or(0x110000);
ptr = rem;
} else {
xml_fatal_err(self, XmlParserErrors::XmlErrInvalidDecCharRef, None);
}
} else {
xml_fatal_err(self, XmlParserErrors::XmlErrInvalidCharRef, None);
return (None, s);
}
// [ WFC: Legal Character ]
// Characters referred to using character references must match the
// production for Char.
if val >= 0x110000 {
xml_fatal_err_msg_int!(
self,
XmlParserErrors::XmlErrInvalidChar,
"xmlParseStringCharRef: character reference out of bounds\n",
val as i32
);
} else if val.is_xml_char() {
return (Some(char::from_u32(val).expect("Internal Error")), ptr);
} else {
xml_fatal_err_msg_int!(
self,
XmlParserErrors::XmlErrInvalidChar,
format!("xmlParseStringCharRef: invalid xmlChar value {val}\n").as_str(),
val as i32
);
}
(None, ptr)
}
/// Parse an entitiy reference. Always consumes '&'.
///
/// ```text
/// [68] EntityRef ::= '&' Name ';'
///
/// [ WFC: Entity Declared ]
/// In a document without any DTD, a document with only an internal DTD
/// subset which contains no parameter entity references, or a document
/// with "standalone='yes'", the Name given in the entity reference
/// must match that in an entity declaration, except that well-formed
/// documents need not declare any of the following entities: amp, lt,
/// gt, apos, quot. The declaration of a parameter entity must precede
/// any reference to it. Similarly, the declaration of a general entity
/// must precede any reference to it which appears in a default value in an
/// attribute-list declaration. Note that if entities are declared in the
/// external subset or in external parameter entities, a non-validating
/// processor is not obligated to read and process their declarations;
/// for such documents, the rule that an entity must be declared is a
/// well-formedness constraint only if standalone='yes'.
///
/// [ WFC: Parsed Entity ]
/// An entity reference must not contain the name of an unparsed entity
/// ```
///
/// Returns the xmlEntityPtr if found, or NULL otherwise.
#[doc(alias = "xmlParseEntityRef")]
pub(crate) fn parse_entity_ref(&mut self) -> Option<XmlEntityPtr> {
self.grow();
if matches!(self.instate, XmlParserInputState::XmlParserEOF) {
return None;
}
if self.current_byte() != b'&' {
return None;
}
self.skip_char();
let Some(name) = self.parse_name() else {
xml_fatal_err_msg(
self,
XmlParserErrors::XmlErrNameRequired,
"xmlParseEntityRef: no name\n",
);
return None;
};
if self.current_byte() != b';' {
xml_fatal_err(self, XmlParserErrors::XmlErrEntityRefSemicolMissing, None);
return None;
}
self.skip_char();
// Predefined entities override any extra definition
if self.options & XmlParserOption::XmlParseOldSAX as i32 == 0 {
if let Some(ent) = xml_get_predefined_entity(&name) {
return Some(ent);
}
}
let mut ent = None;
// Ask first SAX for entity resolution, otherwise try the
// entities which may have stored in the parser context.
if let Some(sax) = self.sax.as_deref_mut() {
if let Some(get_entity) = sax.get_entity {
ent = get_entity(self, &name);
}
if self.well_formed
&& ent.is_none()
&& self.options & XmlParserOption::XmlParseOldSAX as i32 != 0
{
ent = xml_get_predefined_entity(&name);
}
if self.well_formed && ent.is_none() && self.user_data.is_none() {
ent = xml_sax2_get_entity(self, &name);
}
}
if matches!(self.instate, XmlParserInputState::XmlParserEOF) {
return None;
}
// [ WFC: Entity Declared ]
// In a document without any DTD, a document with only an
// internal DTD subset which contains no parameter entity
// references, or a document with "standalone='yes'", the
// Name given in the entity reference must match that in an
// entity declaration, except that well-formed documents
// need not declare any of the following entities: amp, lt,
// gt, apos, quot.
// The declaration of a parameter entity must precede any
// reference to it.
// Similarly, the declaration of a general entity must
// precede any reference to it which appears in a default
// value in an attribute-list declaration. Note that if
// entities are declared in the external subset or in
// external parameter entities, a non-validating processor
// is not obligated to read and process their declarations;
// for such documents, the rule that an entity must be
// declared is a well-formedness constraint only if
// standalone='yes'.
if let Some(mut ent) = ent {
if matches!(ent.etype, XmlEntityType::XmlExternalGeneralUnparsedEntity) {
// [ WFC: Parsed Entity ]
// An entity reference must not contain the name of an unparsed entity
xml_fatal_err_msg_str!(
self,
XmlParserErrors::XmlErrUnparsedEntity,
"Entity reference to unparsed entity {}\n",
name
);
} else if matches!(self.instate, XmlParserInputState::XmlParserAttributeValue)
&& matches!(ent.etype, XmlEntityType::XmlExternalGeneralParsedEntity)
{
// [ WFC: No External Entity References ]
// Attribute values cannot contain direct or indirect
// entity references to external entities.
xml_fatal_err_msg_str!(
self,
XmlParserErrors::XmlErrEntityIsExternal,
"Attribute references external entity '{}'\n",
name
);
} else if matches!(self.instate, XmlParserInputState::XmlParserAttributeValue)
&& !matches!(ent.etype, XmlEntityType::XmlInternalPredefinedEntity)
{
// [ WFC: No < in Attribute Values ]
// The replacement text of any entity referred to directly or
// indirectly in an attribute value (other than "<") must not contain a <.
if ent.flags & XML_ENT_CHECKED_LT as i32 == 0 {
if let Some(content) = ent.content.as_deref() {
if content.contains('<') {
ent.flags |= XML_ENT_CONTAINS_LT as i32;
}
}
ent.flags |= XML_ENT_CHECKED_LT as i32;
}
if ent.flags & XML_ENT_CONTAINS_LT as i32 != 0 {
xml_fatal_err_msg_str!(
self,
XmlParserErrors::XmlErrLtInAttribute,
"'<' in entity '{}' is not allowed in attributes values\n",
name
);
}
} else {
// Internal check, no parameter entities here ...
match ent.etype {
XmlEntityType::XmlInternalParameterEntity
| XmlEntityType::XmlExternalParameterEntity => {
xml_fatal_err_msg_str!(
self,
XmlParserErrors::XmlErrEntityIsParameter,
"Attempt to reference the parameter entity '{}'\n",
name
);
}
_ => {}
}
}
} else {
if self.standalone == 1 || (!self.has_external_subset && !self.has_perefs) {
xml_fatal_err_msg_str!(
self,
XmlParserErrors::XmlErrUndeclaredEntity,
"Entity '{}' not defined\n",
name
);
} else {
xml_err_msg_str!(
self,
XmlParserErrors::XmlWarUndeclaredEntity,
"Entity '{}' not defined\n",
name
);
if self.in_subset == 0 {
if let Some(reference) = self.sax.as_deref_mut().and_then(|sax| sax.reference) {
reference(self, &name);
}
}
}
self.valid = 0;
}
// [ WFC: No Recursion ]
// A parsed entity must not contain a recursive reference
// to itself, either directly or indirectly.
// Done somewhere else
ent
}
/// Parse ENTITY references declarations, but this version parses it from
/// a string value.
///
/// ```text
/// [68] EntityRef ::= '&' Name ';'
///
/// [ WFC: Entity Declared ]
/// In a document without any DTD, a document with only an internal DTD
/// subset which contains no parameter entity references, or a document
/// with "standalone='yes'", the Name given in the entity reference
/// must match that in an entity declaration, except that well-formed
/// documents need not declare any of the following entities: amp, lt,
/// gt, apos, quot. The declaration of a parameter entity must precede
/// any reference to it. Similarly, the declaration of a general entity
/// must precede any reference to it which appears in a default value in an
/// attribute-list declaration. Note that if entities are declared in the
/// external subset or in external parameter entities, a non-validating
/// processor is not obligated to read and process their declarations;
/// for such documents, the rule that an entity must be declared is a
/// well-formedness constraint only if standalone='yes'.
///
/// [ WFC: Parsed Entity ]
/// An entity reference must not contain the name of an unparsed entity
/// ```
///
/// Returns the xmlEntityPtr if found, or NULL otherwise. The str pointer
/// is updated to the current location in the string.
#[doc(alias = "xmlParseStringEntityRef")]
pub(super) fn parse_string_entity_ref<'a>(
&mut self,
s: &'a str,
) -> (Option<XmlEntityPtr>, &'a str) {
let Some(ptr) = s.strip_prefix('&') else {
return (None, s);
};
let (Some(name), rem) = self.parse_string_name(ptr) else {
xml_fatal_err_msg(
self,
XmlParserErrors::XmlErrNameRequired,
"xmlParseStringEntityRef: no name\n",
);
return (None, ptr);
};
let Some(ptr) = rem.strip_prefix(';') else {
xml_fatal_err(self, XmlParserErrors::XmlErrEntityRefSemicolMissing, None);
return (None, rem);
};
let mut ent = None;
// Predefined entities override any extra definition
if self.options & XmlParserOption::XmlParseOldSAX as i32 == 0 {
ent = xml_get_predefined_entity(name);
if ent.is_some() {
return (ent, ptr);
}
}
// Ask first SAX for entity resolution, otherwise try the
// entities which may have stored in the parser context.
if let Some(sax) = self.sax.as_deref_mut() {
if let Some(get_entity) = sax.get_entity {
ent = get_entity(self, name);
}
if ent.is_none() && self.options & XmlParserOption::XmlParseOldSAX as i32 != 0 {
ent = xml_get_predefined_entity(name);
}
if ent.is_none() && self.user_data.is_none() {
ent = xml_sax2_get_entity(self, name);
}
}
if matches!(self.instate, XmlParserInputState::XmlParserEOF) {
return (None, s);
}
// [ WFC: Entity Declared ]
// In a document without any DTD, a document with only an
// internal DTD subset which contains no parameter entity
// references, or a document with "standalone='yes'", the
// Name given in the entity reference must match that in an
// entity declaration, except that well-formed documents
// need not declare any of the following entities: amp, lt,
// gt, apos, quot.
// The declaration of a parameter entity must precede any
// reference to it.
// Similarly, the declaration of a general entity must
// precede any reference to it which appears in a default
// value in an attribute-list declaration. Note that if
// entities are declared in the external subset or in
// external parameter entities, a non-validating processor
// is not obligated to read and process their declarations;
// for such documents, the rule that an entity must be
// declared is a well-formedness constraint only if
// standalone='yes'.
if let Some(mut ent) = ent {
if matches!(ent.etype, XmlEntityType::XmlExternalGeneralUnparsedEntity) {
// [ WFC: Parsed Entity ]
// An entity reference must not contain the name of an
// unparsed entity
xml_fatal_err_msg_str!(
self,
XmlParserErrors::XmlErrUnparsedEntity,
"Entity reference to unparsed entity {}\n",
name
);
} else if matches!(self.instate, XmlParserInputState::XmlParserAttributeValue)
&& matches!(ent.etype, XmlEntityType::XmlExternalGeneralParsedEntity)
{
// [ WFC: No External Entity References ]
// Attribute values cannot contain direct or indirect
// entity references to external entities.
xml_fatal_err_msg_str!(
self,
XmlParserErrors::XmlErrEntityIsExternal,
"Attribute references external entity '{}'\n",
name
);
} else if matches!(self.instate, XmlParserInputState::XmlParserAttributeValue)
&& !matches!(ent.etype, XmlEntityType::XmlInternalPredefinedEntity)
{
// [ WFC: No < in Attribute Values ]
// The replacement text of any entity referred to directly or
// indirectly in an attribute value (other than "<") must
// not contain a <.
if ent.flags & XML_ENT_CHECKED_LT as i32 == 0 {
if let Some(content) = ent.content.as_deref() {
if content.contains('<') {
ent.flags |= XML_ENT_CONTAINS_LT as i32;
}
}
ent.flags |= XML_ENT_CHECKED_LT as i32;
}
if ent.flags & XML_ENT_CONTAINS_LT as i32 != 0 {
xml_fatal_err_msg_str!(
self,
XmlParserErrors::XmlErrLtInAttribute,
"'<' in entity '{}' is not allowed in attributes values\n",
name
);
}
} else {
// Internal check, no parameter entities here ...
match ent.etype {
XmlEntityType::XmlInternalParameterEntity
| XmlEntityType::XmlExternalParameterEntity => {
xml_fatal_err_msg_str!(
self,
XmlParserErrors::XmlErrEntityIsParameter,
"Attempt to reference the parameter entity '{}'\n",
name
);
}
_ => {}
}
}
} else if self.standalone == 1 || (!self.has_external_subset && !self.has_perefs) {
xml_fatal_err_msg_str!(
self,
XmlParserErrors::XmlErrUndeclaredEntity,
"Entity '{}' not defined\n",
name
);
} else {
xml_err_msg_str!(
self,
XmlParserErrors::XmlWarUndeclaredEntity,
"Entity '{}' not defined\n",
name
);
}
// [ WFC: No Recursion ]
// A parsed entity must not contain a recursive reference
// to itself, either directly or indirectly.
// Done somewhere else
(ent, ptr)
}
/// Parse PEReference declarations
///
/// ```text
/// [69] PEReference ::= '%' Name ';'
///
/// [ WFC: No Recursion ]
/// A parsed entity must not contain a recursive
/// reference to itself, either directly or indirectly.
///
/// [ WFC: Entity Declared ]
/// In a document without any DTD, a document with only an internal DTD
/// subset which contains no parameter entity references, or a document
/// with "standalone='yes'", ... ... The declaration of a parameter
/// entity must precede any reference to it...
///
/// [ VC: Entity Declared ]
/// In a document with an external subset or external parameter entities
/// with "standalone='no'", ... ... The declaration of a parameter entity
/// must precede any reference to it...
///
/// [ WFC: In DTD ]
/// Parameter-entity references may only appear in the DTD.
/// NOTE: misleading but this is handled.
/// ```
///
/// Returns the string of the entity content.
/// str is updated to the current value of the index
#[doc(alias = "xmlParseStringPEReference")]
pub(super) fn parse_string_pereference<'a>(
&mut self,
s: &'a str,
) -> (Option<XmlEntityPtr>, &'a str) {
let Some(ptr) = s.strip_prefix('%') else {
return (None, s);
};
let (Some(name), rem) = self.parse_string_name(ptr) else {
xml_fatal_err_msg(
self,
XmlParserErrors::XmlErrNameRequired,
"xmlParseStringPEReference: no name\n",
);
return (None, ptr);
};
let Some(ptr) = rem.strip_prefix(';') else {
xml_fatal_err(self, XmlParserErrors::XmlErrEntityRefSemicolMissing, None);
return (None, rem);
};
// Request the entity from SAX
let entity = self
.sax
.as_deref_mut()
.and_then(|sax| sax.get_parameter_entity)
.and_then(|get_parameter_entity| get_parameter_entity(self, name));
if matches!(self.instate, XmlParserInputState::XmlParserEOF) {
return (None, ptr);
}
if let Some(entity) = entity {
// Internal checking in case the entity quest barfed
if !matches!(
entity.etype,
XmlEntityType::XmlInternalParameterEntity
| XmlEntityType::XmlExternalParameterEntity
) {
xml_warning_msg!(
self,
XmlParserErrors::XmlWarUndeclaredEntity,
"%{}; is not a parameter entity\n",
name
);
}
} else {
// [ WFC: Entity Declared ]
// In a document without any DTD, a document with only an
// internal DTD subset which contains no parameter entity
// references, or a document with "standalone='yes'", ...
// ... The declaration of a parameter entity must precede
// any reference to it...
if self.standalone == 1 || (!self.has_external_subset && !self.has_perefs) {
xml_fatal_err_msg_str!(
self,
XmlParserErrors::XmlErrUndeclaredEntity,
"PEReference: %{}; not found\n",
name
);
} else {
// [ VC: Entity Declared ]
// In a document with an external subset or external
// parameter entities with "standalone='no'", ...
// ... The declaration of a parameter entity must
// precede any reference to it...
xml_warning_msg!(
self,
XmlParserErrors::XmlWarUndeclaredEntity,
"PEReference: %{}; not found\n",
name
);
self.valid = 0;
}
}
self.has_perefs = true;
(entity, ptr)
}
/// Parse and handle entity references in content, depending on the SAX interface,
/// this may end-up in a call to character() if this is a CharRef,
/// a predefined entity, if there is no reference() callback.
/// or if the parser was asked to match to that mode.
///
/// Always consumes '&'.
///
/// ```text
/// [67] Reference ::= EntityRef | CharRef
/// ```
#[doc(alias = "xmlParseReference")]
pub(crate) fn parse_reference(&mut self) {
unsafe {
let mut was_checked: i32;
let mut list = None;
let mut ret: XmlParserErrors;
if self.current_byte() != b'&' {
return;
}
// Simple case of a CharRef
if self.nth_byte(1) == b'#' {
let mut out = [0; 16];
let hex = self.nth_byte(2);
let Some(value) = self.parse_char_ref() else {
return;
};
if self.charset != XmlCharEncoding::UTF8 {
// So we are using non-UTF-8 buffers
// Check that the c_char fit on 8bits, if not
// generate a CharRef.
if value as u32 <= 0xFF {
let out = value.encode_utf8(&mut out[..]);
if !self.disable_sax {
if let Some(characters) =
self.sax.as_deref_mut().and_then(|sax| sax.characters)
{
characters(self, out);
}
}
} else {
use std::io::Write;
let mut slice = &mut out[..];
if hex == b'x' || hex == b'X' {
write!(slice, "#x{:X}", value as u32).ok();
} else {
write!(slice, "#{}", value as u32).ok();
}
let rem = slice.len();
let len = out.len() - rem;
if !self.disable_sax {
if let Some(reference) =
self.sax.as_deref_mut().and_then(|sax| sax.reference)
{
// # Safety
// `out` contains only '#', 'x' and ascii hex digit characters.
// Therefore, UTF-8 validation won't fail.
let out = from_utf8_unchecked(&out[..len]);
reference(self, out);
}
}
}
} else {
// Just encode the value in UTF-8
let out = value.encode_utf8(&mut out[..]);
if !self.disable_sax {
if let Some(characters) =
self.sax.as_deref_mut().and_then(|sax| sax.characters)
{
characters(self, out);
}
}
}
return;
}
// We are seeing an entity reference
let Some(mut ent) = self.parse_entity_ref() else {
return;
};
if !self.well_formed {
return;
}
was_checked = ent.flags & XML_ENT_PARSED as i32;
// special case of predefined entities
if matches!(ent.etype, XmlEntityType::XmlInternalPredefinedEntity) {
let Some(val) = ent.content.as_deref() else {
return;
};
// inline the entity.
if !self.disable_sax {
if let Some(characters) = self.sax.as_deref_mut().and_then(|sax| sax.characters)
{
characters(self, val);
}
}
return;
}
// The first reference to the entity trigger a parsing phase
// where the ent.children is filled with the result from the parsing.
// Note: external parsed entities will not be loaded, it is not
// required for a non-validating parser, unless the parsing option
// of validating, or substituting entities were given. Doing so is
// far more secure as the parser will only process data coming from
// the document entity by default.
if ent.flags & XML_ENT_PARSED as i32 == 0
&& (!matches!(ent.etype, XmlEntityType::XmlExternalGeneralParsedEntity)
|| self.options
& (XmlParserOption::XmlParseNoEnt as i32
| XmlParserOption::XmlParseDTDValid as i32)
!= 0)
{
let oldsizeentcopy: u64 = self.sizeentcopy;
// This is a bit hackish but this seems the best
// way to make sure both SAX and DOM entity support
// behaves okay.
let user_data = self.user_data.clone();
// Avoid overflow as much as possible
self.sizeentcopy = 0;
if ent.flags & XML_ENT_EXPANDING as i32 != 0 {
xml_fatal_err(self, XmlParserErrors::XmlErrEntityLoop, None);
self.halt();
return;
}
ent.flags |= XML_ENT_EXPANDING as i32;
// Check that this entity is well formed
// 4.3.2: An internal general parsed entity is well-formed
// if its replacement text matches the production labeled content.
if matches!(ent.etype, XmlEntityType::XmlInternalGeneralEntity) {
self.depth += 1;
ret = xml_parse_balanced_chunk_memory_internal(
self,
ent.content.as_deref().unwrap().as_bytes(),
user_data,
Some(&mut list),
);
self.depth -= 1;
} else if matches!(ent.etype, XmlEntityType::XmlExternalGeneralParsedEntity) {
self.depth += 1;
let has_sax = self.sax.is_some();
let sax = self.sax.take();
let (sax, error) = parse_external_entity_private(
self.my_doc.unwrap(),
self,
sax,
user_data,
self.depth,
ent.uri.as_deref(),
ent.external_id.as_deref(),
Some(&mut list),
);
assert_eq!(has_sax, sax.is_some());
self.sax = sax;
ret = error;
self.depth -= 1;
} else {
ret = XmlParserErrors::XmlErrEntityPEInternal;
xml_err_msg_str!(
self,
XmlParserErrors::XmlErrInternalError,
"invalid entity type found\n"
);
}
ent.flags &= !XML_ENT_EXPANDING as i32;
ent.flags |= (XML_ENT_PARSED | XML_ENT_CHECKED) as i32;
ent.expanded_size = self.sizeentcopy;
if matches!(ret, XmlParserErrors::XmlErrEntityLoop) {
self.halt();
xml_free_node_list(list);
return;
}
if self.parser_entity_check(oldsizeentcopy) != 0 {
xml_free_node_list(list);
return;
}
if let Some(l) = list.filter(|_| matches!(ret, XmlParserErrors::XmlErrOK)) {
ent.children = Some(XmlNodePtr::try_from(l).unwrap());
// Prune it directly in the generated document
// except for single text nodes.
if !self.replace_entities
|| matches!(self.parse_mode, XmlParserMode::XmlParseReader)
|| (matches!(l.element_type(), XmlElementType::XmlTextNode)
&& l.next().is_none())
{
ent.owner = 1;
let mut cur = Some(l);
while let Some(mut now) = cur {
now.set_parent(Some(ent.into()));
if now.document() != ent.doc {
now.set_doc(ent.doc);
}
if now.next().is_none() {
ent.last = Some(XmlNodePtr::try_from(now).unwrap());
}
cur = now.next();
}
list = None;
} else {
ent.owner = 0;
while let Some(mut now) = list {
now.set_parent(self.node.map(|node| node.into()));
now.set_document(self.my_doc);
if now.next().is_none() {
ent.last = Some(XmlNodePtr::try_from(now).unwrap());
}
list = now.next();
}
list = ent.children.map(|children| children.into());
}
} else if !matches!(
ret,
XmlParserErrors::XmlErrOK | XmlParserErrors::XmlWarUndeclaredEntity
) {
xml_fatal_err_msg_str!(
self,
XmlParserErrors::XmlErrUndeclaredEntity,
"Entity '{}' failed to parse\n",
ent.name
);
if ent.content.is_some() {
ent.content = Some(Cow::Borrowed(""));
}
} else if let Some(list) = list.take() {
xml_free_node_list(Some(list));
}
// Prevent entity from being parsed and expanded twice (Bug 760367).
was_checked = 0;
}
// Now that the entity content has been gathered
// provide it to the application, this can take different forms based
// on the parsing modes.
if ent.children.is_none() {
// Probably running in SAX mode and the callbacks don't
// build the entity content. So unless we already went
// though parsing for first checking go though the entity
// content to generate callbacks associated to the entity
if was_checked != 0 {
// This is a bit hackish but this seems the best
// way to make sure both SAX and DOM entity support behaves okay.
let user_data = self.user_data.clone();
if matches!(ent.etype, XmlEntityType::XmlInternalGeneralEntity) {
self.depth += 1;
ret = xml_parse_balanced_chunk_memory_internal(
self,
ent.content.as_deref().unwrap().as_bytes(),
user_data,
None,
);
self.depth -= 1;
} else if matches!(ent.etype, XmlEntityType::XmlExternalGeneralParsedEntity) {
let oldsizeentities: u64 = self.sizeentities;
self.depth += 1;
let has_sax = self.sax.is_some();
let sax = self.sax.take();
let (sax, error) = parse_external_entity_private(
self.my_doc.unwrap(),
self,
sax,
user_data,
self.depth,
ent.uri.as_deref(),
ent.external_id.as_deref(),
None,
);
assert_eq!(has_sax, sax.is_some());
self.sax = sax;
ret = error;
self.depth -= 1;
// Undo the change to sizeentities
self.sizeentities = oldsizeentities;
} else {
ret = XmlParserErrors::XmlErrEntityPEInternal;
xml_err_msg_str!(
self,
XmlParserErrors::XmlErrInternalError,
"invalid entity type found\n"
);
}
if matches!(ret, XmlParserErrors::XmlErrEntityLoop) {
xml_fatal_err(self, XmlParserErrors::XmlErrEntityLoop, None);
return;
}
if self.parser_entity_check(0) != 0 {
return;
}
}
if !self.replace_entities && !self.disable_sax {
if let Some(reference) = self.sax.as_deref_mut().and_then(|sax| sax.reference) {
// Entity reference callback comes second, it's somewhat
// superfluous but a compatibility to historical behaviour
reference(self, &ent.name().unwrap());
}
}
return;
}
// We also check for amplification if entities aren't substituted.
// They might be expanded later.
if was_checked != 0 && self.parser_entity_check(ent.expanded_size) != 0 {
return;
}
// If we didn't get any children for the entity being built
if !self.replace_entities && !self.disable_sax {
if let Some(reference) = self.sax.as_deref_mut().and_then(|sax| sax.reference) {
// Create a node.
reference(self, &ent.name().unwrap());
return;
}
}
if self.replace_entities {
// There is a problem on the handling of _private for entities
// (bug 155816): Should we copy the content of the field from
// the entity (possibly overwriting some value set by the user
// when a copy is created), should we leave it alone, or should
// we try to take care of different situations? The problem
// is exacerbated by the usage of this field by the xmlReader.
// To fix this bug, we look at _private on the created node
// and, if it's NULL, we copy in whatever was in the entity.
// If it's not NULL we leave it alone. This is somewhat of a
// hack - maybe we should have further tests to determine what to do.
if let Some(mut context_node) = self.node {
// Seems we are generating the DOM content, do
// a simple tree copy for all references except the first
// In the first occurrence list contains the replacement.
if (list.is_none() && ent.owner == 0)
|| matches!(self.parse_mode, XmlParserMode::XmlParseReader)
{
let mut first_child = None;
// when operating on a reader, the entities definitions
// are always owning the entities subtree.
// if (self.parseMode == XML_PARSE_READER)
// ent.owner = 1;
let mut cur = ent.children;
while let Some(cur_node) = cur {
let mut nw = xml_doc_copy_node(
XmlGenericNodePtr::from(cur_node),
self.my_doc,
1,
)
.map(|nw| XmlNodePtr::try_from(nw).unwrap());
if let Some(mut now) = nw {
if now._private.is_null() {
now._private = cur_node._private;
}
if first_child.is_none() {
first_child = nw;
}
nw = XmlNodePtr::try_from(
context_node.add_child(now.into()).unwrap(),
)
.ok();
}
if cur == ent.last {
// needed to detect some strange empty
// node cases in the reader tests
if matches!(self.parse_mode, XmlParserMode::XmlParseReader) {
if let Some(mut nw) = nw
.filter(|nw| {
nw.element_type() == XmlElementType::XmlElementNode
})
.filter(|nw| nw.children.is_none())
{
nw.extra = 1;
}
}
break;
}
cur = cur_node
.next
.map(|node| XmlNodePtr::try_from(node).unwrap());
}
} else if list.is_none() || !self.input_tab.is_empty() {
let mut first_child = None;
// Copy the entity child list and make it the new
// entity child list. The goal is to make sure any
// ID or REF referenced will be the one from the
// document content and not the entity copy.
let mut cur = ent.children.take();
let last = ent.last.take();
while let Some(mut cur_node) = cur {
let next = cur_node.next.take();
cur_node.set_parent(None);
let nw = xml_doc_copy_node(cur_node.into(), self.my_doc, 1)
.map(|nw| XmlNodePtr::try_from(nw).unwrap());
if let Some(mut nw) = nw {
if nw._private.is_null() {
nw._private = cur_node._private;
}
if first_child.is_none() {
first_child = Some(cur_node);
}
ent.add_child(nw.into());
}
context_node.add_child(cur_node.into());
if Some(cur_node) == last {
break;
}
cur = next.map(|node| XmlNodePtr::try_from(node).unwrap());
}
if ent.owner == 0 {
ent.owner = 1;
}
} else {
// the name change is to avoid coalescing of the
// node with a possible previous text one which
// would make ent.children a dangling pointer
if matches!(
ent.children.unwrap().element_type(),
XmlElementType::XmlTextNode
) {
ent.children.unwrap().name = Cow::Borrowed("nbktext");
}
if ent.last != ent.children
&& matches!(
ent.last.unwrap().element_type(),
XmlElementType::XmlTextNode
)
{
ent.last.unwrap().name = Cow::Borrowed("nbktext");
}
context_node.add_child_list(ent.children.unwrap().into());
}
}
}
}
}
}