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
use anyxml_uri::uri::URIString;
use crate::{
ParserSpec,
error::XMLError,
sax::{
EntityDecl,
error::{error, fatal_error, validity_error},
handler::SAXHandler,
parser::{ParserOption, XMLReader},
source::InputSource,
},
};
impl<'a, Spec: ParserSpec<Reader = InputSource<'a>>, H: SAXHandler> XMLReader<Spec, H> {
/// ```text
/// [70] EntityDecl ::= GEDecl | PEDecl
/// [71] GEDecl ::= '<!ENTITY' S Name S EntityDef S? '>'
/// [72] PEDecl ::= '<!ENTITY' S '%' S Name S PEDef S? '>'
/// [73] EntityDef ::= EntityValue | (ExternalID NDataDecl?)
/// [74] PEDef ::= EntityValue | ExternalID
/// ```
pub(crate) fn parse_entity_decl(&mut self) -> Result<(), XMLError> {
self.grow()?;
if !self.source.content_bytes().starts_with(b"<!ENTITY") {
fatal_error!(
self,
ParserInvalidEntityDecl,
"Entity declaration must start with '<!ENTITY'."
);
return Err(XMLError::ParserInvalidEntityDecl);
}
// skip '<!ENTITY'
self.source.advance(8);
self.locator.update_column(|c| c + 8);
// The base URI of the entity is determined by the position of the first occurrence
// of '<', so it must be saved at this point before the parameter entity is resolved.
let base_uri = self.base_uri.clone();
let base_source_id = self.source.source_id();
let is_external_markup = self.is_external_markup();
let mut s = self.skip_whitespaces_with_handle_peref(true)?;
self.grow()?;
let mut pe = false;
if self.source.content_bytes().starts_with(b"%") {
if s == 0 {
fatal_error!(
self,
ParserInvalidEntityDecl,
"Whitespaces are required before '%' in a parameter entity declaration."
);
}
pe = true;
// skip '%'
self.source.advance(1);
self.locator.update_column(|c| c + 1);
s = self.skip_whitespaces_with_handle_peref(true)?;
}
if s == 0 {
fatal_error!(
self,
ParserInvalidEntityDecl,
"Whitespaces are required before Name in an entity declaration."
);
}
let mut name = if pe { "%".to_owned() } else { String::new() };
if self.config.is_enable(ParserOption::Namespaces) {
self.parse_ncname(&mut name)?;
} else {
self.parse_name(&mut name)?;
}
if self.skip_whitespaces_with_handle_peref(true)? == 0 {
fatal_error!(
self,
ParserInvalidEntityDecl,
"Whitespaces are required after Name in entity declaration."
);
}
self.grow()?;
let decl = match self.source.content_bytes() {
[b'"' | b'\'', ..] => {
let mut buffer = String::new();
self.parse_entity_value(&mut buffer)?;
self.skip_whitespaces_with_handle_peref(true)?;
if !self.fatal_error_occurred {
self.handler.internal_entity_decl(&name, &buffer);
}
if pe {
EntityDecl::InternalParameterEntity {
base_uri,
replacement_text: buffer.into_boxed_str(),
}
} else {
EntityDecl::InternalGeneralEntity {
base_uri,
replacement_text: buffer.into_boxed_str(),
in_external_markup: is_external_markup,
}
}
}
[b'S', b'Y', b'S', b'T', b'E', b'M', ..] | [b'P', b'U', b'B', b'L', b'I', b'C', ..] => {
let mut system_id = String::new();
let mut public_id = None;
self.parse_external_id(&mut system_id, &mut public_id)?;
let s = self.skip_whitespaces_with_handle_peref(true)?;
// If this is a general entity declaration, NDataDecl may follow.
// [73] EntityDef ::= EntityValue | (ExternalID NDataDecl?)
// If this is a parameter entity declaration, '>' must follow.
// [74] PEDef ::= EntityValue | ExternalID
let mut ndata = None::<String>;
if !pe && !self.source.content_bytes().starts_with(b">") {
if s == 0 {
fatal_error!(
self,
ParserInvalidEntityDecl,
"Whitespaces are required between ExternalID and NDataDecl."
);
}
if !self.source.content_bytes().starts_with(b"NDATA") {
fatal_error!(
self,
ParserInvalidEntityDecl,
"NDataDecl must start with 'NDATA'."
);
return Err(XMLError::ParserInvalidEntityDecl);
}
// skip 'NDATA'
self.source.advance(5);
self.locator.update_column(|c| c + 5);
if self.skip_whitespaces_with_handle_peref(true)? == 0 {
fatal_error!(
self,
ParserInvalidEntityDecl,
"Whitespaces are required after 'NDATA' in entity declaration."
);
}
let ndata = ndata.get_or_insert_default();
if self.config.is_enable(ParserOption::Namespaces) {
self.parse_ncname(ndata)?;
} else {
self.parse_name(ndata)?;
}
// Since notation declarations may appear after this declaration,
// the [VC: Notation Declared] check is performed after reading the entire DTD.
self.skip_whitespaces_with_handle_peref(true)?;
}
let system_id = URIString::parse(system_id)?;
if !pe && !self.fatal_error_occurred {
if let Some(ndata) = ndata.as_deref() {
self.handler.unparsed_entity_decl(
&name,
public_id.as_deref(),
&system_id,
ndata,
);
} else {
self.handler
.external_entity_decl(&name, public_id.as_deref(), &system_id);
}
}
if pe {
EntityDecl::ExternalParameterEntity {
base_uri,
system_id: system_id.into(),
public_id: public_id.map(From::from),
}
} else if let Some(notation) = ndata {
EntityDecl::ExternalGeneralUnparsedEntity {
base_uri,
system_id: system_id.into(),
public_id: public_id.map(From::from),
notation_name: notation.into(),
}
} else {
EntityDecl::ExternalGeneralParsedEntity {
base_uri,
system_id: system_id.into(),
public_id: public_id.map(From::from),
in_external_markup: is_external_markup,
}
}
}
[_, ..] => {
fatal_error!(
self,
ParserInvalidEntityDecl,
"Neither EntityValue nor ExternalID are found in entity declaration."
);
return Err(XMLError::ParserInvalidEntityDecl);
}
[] => return Err(XMLError::ParserUnexpectedEOF),
};
if self.source.source_id() != base_source_id {
// [VC: Proper Declaration/PE Nesting]
validity_error!(
self,
ParserEntityIncorrectNesting,
"A parameter entity in an element declaration is nested incorrectly."
);
}
if !self.source.content_bytes().starts_with(b">") {
fatal_error!(
self,
ParserInvalidEntityDecl,
"Entity declaration does not end with '>'."
);
return Err(XMLError::ParserInvalidEntityDecl);
}
// skip '>'
self.source.advance(1);
self.locator.update_column(|c| c + 1);
// Duplicate declarations are not errors.
//
// Reference: 4.2 Entity Declarations
// > If the same entity is declared more than once, the first declaration
// > encountered is binding; at user option, an XML processor MAY issue a
// > warning if entities are declared multiple times.
if let Err(XMLError::ParserIncorrectPredefinedEntityDecl) =
self.entities.insert(name.as_ref(), decl)
{
error!(
self,
ParserIncorrectPredefinedEntityDecl,
"Predefined entity '{}' is redefined incorrectly.",
name
);
}
self.has_parameter_entity |= pe;
Ok(())
}
}