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
use crate::{
chvalid::XmlCharValid,
error::XmlParserErrors,
parser::{
XML_MAX_NAME_LENGTH, XML_MAX_TEXT_LENGTH, XmlParserCtxt, XmlParserInputState,
XmlParserOption, xml_fatal_err, xml_fatal_err_msg,
},
};
impl XmlParserCtxt<'_> {
/// Parse an XML Literal
///
/// ```text
/// [11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'")
/// ```
///
/// Returns the SystemLiteral parsed or NULL
#[doc(alias = "xmlParseSystemLiteral")]
fn parse_system_literal(&mut self) -> Option<String> {
let max_length = if self.options & XmlParserOption::XmlParseHuge as i32 != 0 {
XML_MAX_TEXT_LENGTH
} else {
XML_MAX_NAME_LENGTH
};
let state = self.instate;
if !matches!(self.current_byte(), b'"' | b'\'') {
xml_fatal_err(self, XmlParserErrors::XmlErrLiteralNotStarted, None);
return None;
}
let stop = self.current_byte();
self.skip_char();
let mut buf = String::new();
self.instate = XmlParserInputState::XmlParserSystemLiteral;
let mut cur = self.current_char();
while let Some(nc) = cur.filter(|&cur| cur.is_xml_char() && cur != stop as char) {
buf.push(nc);
if buf.len() > max_length {
xml_fatal_err(
self,
XmlParserErrors::XmlErrNameTooLong,
Some("SystemLiteral"),
);
self.instate = state;
return None;
}
self.advance_with_line_handling(nc.len_utf8());
cur = self.current_char();
}
if matches!(self.instate, XmlParserInputState::XmlParserEOF) {
return None;
}
self.instate = state;
if cur.is_none_or(|cur| !cur.is_xml_char()) {
xml_fatal_err(self, XmlParserErrors::XmlErrLiteralNotFinished, None);
} else {
self.skip_char();
}
Some(buf)
}
/// Parse an XML public literal
///
/// ```text
/// [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'"
/// ```
///
/// Returns the PubidLiteral parsed or NULL.
#[doc(alias = "xmlParsePubidLiteral")]
fn parse_pubid_literal(&mut self) -> Option<String> {
let max_length = if self.options & XmlParserOption::XmlParseHuge as i32 != 0 {
XML_MAX_TEXT_LENGTH
} else {
XML_MAX_NAME_LENGTH
};
let oldstate = self.instate;
if !matches!(self.current_byte(), b'"' | b'\'') {
xml_fatal_err(self, XmlParserErrors::XmlErrLiteralNotStarted, None);
return None;
}
let stop = self.current_byte();
self.skip_char();
let mut buf = String::new();
self.instate = XmlParserInputState::XmlParserPublicLiteral;
let mut cur = self.current_byte();
while cur.is_xml_pubid_char() && cur != stop {
// Since PubidChar is a subset of ASCII,
// there is no problem with casting to `char`.
buf.push(cur as char);
if buf.len() > max_length {
xml_fatal_err(self, XmlParserErrors::XmlErrNameTooLong, Some("Public ID"));
return None;
}
self.skip_char();
cur = self.current_byte();
}
if matches!(self.instate, XmlParserInputState::XmlParserEOF) {
return None;
}
if cur != stop {
xml_fatal_err(self, XmlParserErrors::XmlErrLiteralNotFinished, None);
} else {
self.advance(1);
}
self.instate = oldstate;
Some(buf)
}
/// Parse an External ID or a Public ID
///
/// # Note
/// Productions [75] and [83] interact badly since [75] can generate
/// 'PUBLIC' S PubidLiteral S SystemLiteral
///
/// ```text
/// [75] ExternalID ::= 'SYSTEM' S SystemLiteral | 'PUBLIC' S PubidLiteral S SystemLiteral
/// [83] PublicID ::= 'PUBLIC' S PubidLiteral
/// ```
///
/// If ExternalID is parsed and PubidLiteral is found, return `(PubidLiteral, SystemLiteral)`,
/// if ExternalID is parsed and PubidLiteral is not found, return `(None, SystemLiteral)`,
/// if PublicID is parsed, return `(PubidLiteral, None)`,
/// otherwise, return `(None, None)`.
#[doc(alias = "xmlParseExternalID")]
pub(crate) fn parse_external_id(&mut self, strict: bool) -> (Option<String>, Option<String>) {
let mut uri = None;
let mut public_id = None;
if self.content_bytes().starts_with(b"SYSTEM") {
self.advance(6);
if self.skip_blanks() == 0 {
xml_fatal_err_msg(
self,
XmlParserErrors::XmlErrSpaceRequired,
"Space required after b'SYSTEM'\n",
);
}
uri = self.parse_system_literal();
if uri.is_none() {
xml_fatal_err(self, XmlParserErrors::XmlErrURIRequired, None);
}
} else if self.content_bytes().starts_with(b"PUBLIC") {
self.advance(6);
if self.skip_blanks() == 0 {
xml_fatal_err_msg(
self,
XmlParserErrors::XmlErrSpaceRequired,
"Space required after 'PUBLIC'\n",
);
}
public_id = self.parse_pubid_literal();
if public_id.is_none() {
xml_fatal_err(self, XmlParserErrors::XmlErrPubidRequired, None);
}
if strict {
// We don't handle [83] so "S SystemLiteral" is required.
if self.skip_blanks() == 0 {
xml_fatal_err_msg(
self,
XmlParserErrors::XmlErrSpaceRequired,
"Space required after the Public Identifier\n",
);
}
} else {
// We handle [83] so we return immediately, if
// "S SystemLiteral" is not detected. We skip blanks if no
// system literal was found, but this is harmless since we must
// be at the end of a NotationDecl.
if self.skip_blanks() == 0 {
return (public_id, None);
}
if self.current_byte() != b'\'' && self.current_byte() != b'"' {
return (public_id, None);
}
}
uri = self.parse_system_literal();
if uri.is_none() {
xml_fatal_err(self, XmlParserErrors::XmlErrURIRequired, None);
}
}
(public_id, uri)
}
}