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
use crate::{
error::XMLError,
sax::{
InputSource, ParserSpec, ParserState, SAXHandler, XMLReader,
error::{fatal_error, validity_error},
},
};
impl<'a, Spec: ParserSpec<Reader = InputSource<'a>>, H: SAXHandler + ?Sized> XMLReader<Spec, H> {
/// ```text
/// [30] extSubset ::= TextDecl? extSubsetDecl
/// ```
pub(crate) fn parse_ext_subset(&mut self) -> Result<(), XMLError> {
let old_state = self.state;
self.state = ParserState::InTextDeclaration;
self.grow()?;
if self.source.content_bytes().starts_with(b"<?xml") {
self.parse_text_decl()?;
}
self.source.set_compact_mode();
self.state = ParserState::InExternalSubset;
self.parse_ext_subset_decl()?;
self.state = old_state;
Ok(())
}
/// ```text
/// [31] extSubsetDecl ::= ( markupdecl | conditionalSect | DeclSep)*
/// ```
fn parse_ext_subset_decl(&mut self) -> Result<(), XMLError> {
self.grow()?;
self.skip_whitespaces()?;
loop {
self.grow()?;
match self.source.content_bytes() {
[b'%', ..] => {
let entity_push = self.parse_pe_reference(false)?;
let source_id = self.source.source_id();
self.parse_ext_subset_decl()?;
if self.source.source_id() != source_id {
fatal_error!(
self,
ParserEntityIncorrectNesting,
"A parameter entity in extSubsetDecl is nested incorrectly."
);
return Err(XMLError::ParserEntityIncorrectNesting);
}
if entity_push {
self.pop_source()?;
}
if !self.fatal_error_occurred {
self.handler.end_entity();
}
}
[b'<', b'?', ..] => self.parse_pi()?,
[b'<', b'!', b'-', b'-', ..] => self.parse_comment()?,
[b'<', b'!', b'[', ..] => self.parse_conditional_sect()?,
[b'<', b'!', b'E', b'L', ..] => self.parse_element_decl()?,
[b'<', b'!', b'E', b'N', ..] => self.parse_entity_decl()?,
[b'<', b'!', b'A', ..] => self.parse_attlist_decl()?,
[b'<', b'!', b'N', ..] => self.parse_notation_decl()?,
_ => break Ok(()),
}
self.skip_whitespaces_with_handle_peref(false)?;
}
}
/// ```text
/// [61] conditionalSect ::= includeSect | ignoreSect
/// [62] includeSect ::= '<![' S? 'INCLUDE' S? '[' extSubsetDecl ']]>'
/// [VC: Proper Conditional Section/PE Nesting]
/// [63] ignoreSect ::= '<![' S? 'IGNORE' S? '[' ignoreSectContents* ']]>'
/// [VC: Proper Conditional Section/PE Nesting]
/// [64] ignoreSectContents ::= Ignore ('<![' ignoreSectContents ']]>' Ignore)*
/// [65] Ignore ::= Char* - (Char* ('<![' | ']]>') Char*)
/// ```
fn parse_conditional_sect(&mut self) -> Result<(), XMLError> {
self.grow()?;
if !self.source.content_bytes().starts_with(b"<![") {
fatal_error!(
self,
ParserInvalidConditionalSect,
"A conditional section in DTD does not start with '<!['."
);
return Err(XMLError::ParserInvalidConditionalSect);
}
// skip '<!['
self.source.advance(3);
self.locator.update_column(|c| c + 3);
let base_source_id = self.source.source_id();
self.skip_whitespaces_with_handle_peref(true)?;
self.grow()?;
match self.source.content_bytes() {
[b'I', b'N', b'C', b'L', b'U', b'D', b'E', ..] => {
// skip 'INCLUDE'
self.source.advance(7);
self.locator.update_column(|c| c + 7);
self.skip_whitespaces_with_handle_peref(true)?;
self.grow()?;
if self.source.source_id() != base_source_id {
// [VC: Proper Conditional Section/PE Nesting]
validity_error!(
self,
ParserEntityIncorrectNesting,
"A parameter entity in the conditional section is nested incorrectly."
);
}
if !self.source.content_bytes().starts_with(b"[") {
fatal_error!(
self,
ParserInvalidConditionalSect,
"'[' is not found after 'INCLUDE' in a conditional section."
);
return Err(XMLError::ParserInvalidConditionalSect);
}
// skip '['
self.source.advance(1);
self.locator.update_column(|c| c + 1);
self.parse_ext_subset_decl()?;
if !self.source.content_bytes().starts_with(b"]]>") {
fatal_error!(
self,
ParserInvalidConditionalSect,
"The conditional section does not end with ']]>'."
);
return Err(XMLError::ParserInvalidConditionalSect);
}
// skip ']]>'
self.source.advance(3);
self.locator.update_column(|c| c + 3);
}
[b'I', b'G', b'N', b'O', b'R', b'E', ..] => {
// skip 'IGNORE'
self.source.advance(6);
self.locator.update_column(|c| c + 6);
self.skip_whitespaces_with_handle_peref(true)?;
self.grow()?;
if self.source.source_id() != base_source_id {
// [VC: Proper Conditional Section/PE Nesting]
validity_error!(
self,
ParserEntityIncorrectNesting,
"A parameter entity in the conditional section is nested incorrectly."
);
}
if !self.source.content_bytes().starts_with(b"[") {
fatal_error!(
self,
ParserInvalidConditionalSect,
"'[' is not found after 'IGNORE' in a conditional section."
);
return Err(XMLError::ParserInvalidConditionalSect);
}
// skip '['
self.source.advance(1);
self.locator.update_column(|c| c + 1);
let mut depth = 1;
while depth > 0 {
self.grow()?;
if self.source.content_bytes().starts_with(b"<![") {
depth += 1;
self.source.advance(3);
self.locator.update_column(|c| c + 3);
} else if self.source.content_bytes().starts_with(b"]]>") {
depth -= 1;
self.source.advance(3);
self.locator.update_column(|c| c + 3);
} else {
match self.source.next_char()? {
Some('\r') => {
if self.source.peek_char()? != Some('\n') {
self.locator.update_line(|l| l + 1);
self.locator.set_column(1);
}
}
Some('\n') => {
self.locator.update_line(|l| l + 1);
self.locator.set_column(1);
}
Some(c) if self.is_char(c) => {
self.locator.update_column(|c| c + 1);
}
Some(c) => {
fatal_error!(
self,
ParserInvalidCharacter,
"The character '0x{:X}' is not allowed in the XML document.",
c as u32
);
self.locator.update_column(|c| c + 1);
}
None => return Err(XMLError::ParserUnexpectedEOF),
}
}
}
}
_ => {
fatal_error!(
self,
ParserInvalidConditionalSect,
"A conditional section does not have neither 'INCLUDE' nor 'IGNORE' parameter."
);
return Err(XMLError::ParserInvalidConditionalSect);
}
}
Ok(())
}
}