use std::io::BufRead;
use crate::import::{ImportError, Origin};
pub(crate) const MAX_DEPTH: usize = 64;
pub(crate) const MAX_NAME_BYTES: usize = 64;
pub(crate) const MAX_VALUE_BYTES: usize = 256;
pub(crate) const MAX_ELEMENTS: u64 = 1 << 24;
pub(crate) const MAX_ENTITY_BYTES: usize = 16;
const MAX_TEXT_BYTES: usize = 512;
pub(crate) enum Event {
Start { self_closing: bool },
End,
Eof,
}
#[derive(Debug, Default)]
pub(crate) struct Element {
pub(crate) name: Vec<u8>,
values: Vec<(Vec<u8>, Vec<u8>)>,
}
impl Element {
fn clear(&mut self) {
self.name.clear();
self.values.clear();
}
pub(crate) fn value(&self, name: &[u8]) -> Option<&str> {
self.values
.iter()
.find(|(key, _)| key == name)
.and_then(|(_, value)| std::str::from_utf8(value).ok())
}
}
pub(crate) struct Parser<'a> {
input: &'a mut dyn BufRead,
buffer: Vec<u8>,
position: usize,
line: u64,
element_bytes: usize,
max_element_bytes: usize,
elements: u64,
pub(crate) element: Element,
format: &'static str,
kept: &'static [&'static [u8]],
lossy: &'static [&'static [u8]],
max_value_bytes: usize,
depth: usize,
capture: bool,
text: Vec<u8>,
}
impl<'a> Parser<'a> {
pub(crate) fn new(
input: &'a mut dyn BufRead,
max_element_bytes: usize,
format: &'static str,
kept: &'static [&'static [u8]],
) -> Self {
Self {
input,
buffer: Vec::new(),
position: 0,
line: 1,
element_bytes: 0,
max_element_bytes,
elements: 0,
element: Element::default(),
format,
kept,
lossy: &[],
max_value_bytes: MAX_VALUE_BYTES,
depth: 0,
capture: false,
text: Vec::new(),
}
}
pub(crate) fn with_lossy(mut self, lossy: &'static [&'static [u8]]) -> Self {
self.lossy = lossy;
self
}
pub(crate) fn with_max_value_bytes(mut self, bytes: usize) -> Self {
self.max_value_bytes = bytes;
self
}
pub(crate) fn begin_text(&mut self) {
self.text.clear();
self.capture = true;
}
pub(crate) fn take_text(&mut self) -> String {
self.capture = false;
let text = std::mem::take(&mut self.text);
String::from_utf8_lossy(&text).trim().to_string()
}
pub(crate) fn origin(&self) -> Origin {
Origin::line(self.line)
}
pub(crate) fn malformed(&self, message: String) -> ImportError {
ImportError::Malformed {
format: self.format,
origin: self.origin(),
message,
}
}
fn fill(&mut self) -> Result<bool, ImportError> {
if self.position < self.buffer.len() {
return Ok(true);
}
let Self {
input,
buffer,
position,
..
} = self;
buffer.clear();
*position = 0;
let available = input.fill_buf()?;
if available.is_empty() {
return Ok(false);
}
let taken = available.len();
buffer.extend_from_slice(available);
input.consume(taken);
Ok(true)
}
fn peek(&mut self) -> Result<Option<u8>, ImportError> {
if !self.fill()? {
return Ok(None);
}
Ok(Some(self.buffer[self.position]))
}
fn bump(&mut self) -> Result<Option<u8>, ImportError> {
let Some(byte) = self.peek()? else {
return Ok(None);
};
self.position += 1;
if byte == b'\n' {
self.line += 1;
}
self.element_bytes += 1;
if self.element_bytes > self.max_element_bytes {
return Err(ImportError::LineTooLong {
origin: self.origin(),
limit: self.max_element_bytes,
});
}
Ok(Some(byte))
}
pub(crate) fn next_event(&mut self) -> Result<Event, ImportError> {
loop {
loop {
match self.peek()? {
None => return Ok(Event::Eof),
Some(b'<') => break,
Some(b'&') if self.capture => {
self.element_bytes = 0;
let resolved = self.entity()?;
self.keep_text(&resolved)?;
}
Some(byte) => {
self.element_bytes = 0;
self.bump()?;
if self.capture {
self.keep_text(&[byte])?;
}
}
}
}
self.element_bytes = 0;
self.bump()?;
match self.peek()? {
None => return Err(self.malformed("the document ends inside a tag".to_string())),
Some(b'?') => {
self.skip_until(b"?>")?;
continue;
}
Some(b'!') => {
self.declaration()?;
continue;
}
Some(b'/') => {
self.bump()?;
self.element.clear();
self.read_name()?;
self.skip_until(b">")?;
self.depth = self.depth.saturating_sub(1);
return Ok(Event::End);
}
Some(_) => {
self.elements += 1;
if self.elements > MAX_ELEMENTS {
return Err(self.malformed(format!("more than {MAX_ELEMENTS} elements")));
}
self.element.clear();
self.read_name()?;
let self_closing = self.read_attributes()?;
if !self_closing {
self.depth += 1;
if self.depth > MAX_DEPTH {
return Err(self
.malformed(format!("elements nested more than {MAX_DEPTH} deep")));
}
}
return Ok(Event::Start { self_closing });
}
}
}
}
fn declaration(&mut self) -> Result<(), ImportError> {
self.bump()?;
if self.matches(b"--")? {
return self.skip_until(b"-->");
}
if self.matches(b"[CDATA[")? {
return self.skip_until(b"]]>");
}
if self.matches(b"DOCTYPE")? {
return self.doctype();
}
Err(self.malformed(
"a declaration other than a comment, CDATA or DOCTYPE: this parser \
accepts no declarations, which is what makes entity expansion \
impossible rather than merely unimplemented"
.to_string(),
))
}
fn doctype(&mut self) -> Result<(), ImportError> {
loop {
let Some(byte) = self.bump()? else {
return Err(self.malformed("the document ends inside a DOCTYPE".to_string()));
};
match byte {
b'>' => return Ok(()),
b'[' => {
return Err(self.malformed(
"a DOCTYPE with an internal subset, which is where entity \
declarations live. Only a bare '<!DOCTYPE name>' is accepted"
.to_string(),
));
}
b'S' | b'P' => {
let word = if byte == b'S' {
b"YSTEM".as_slice()
} else {
b"UBLIC".as_slice()
};
if self.matches(word)? {
return Err(self.malformed(
"a DOCTYPE naming an external identifier, which asks the \
parser to fetch a document. Only a bare \
'<!DOCTYPE name>' is accepted"
.to_string(),
));
}
}
_ => {}
}
}
}
fn read_name(&mut self) -> Result<(), ImportError> {
loop {
match self.peek()? {
None => return Err(self.malformed("the document ends inside a name".to_string())),
Some(byte) if is_name_byte(byte) => {
self.bump()?;
if self.element.name.len() >= MAX_NAME_BYTES {
return Err(
self.malformed(format!("a name longer than {MAX_NAME_BYTES} bytes"))
);
}
self.element.name.push(byte);
}
Some(_) => return Ok(()),
}
}
}
fn read_attributes(&mut self) -> Result<bool, ImportError> {
loop {
self.skip_whitespace()?;
match self.peek()? {
None => {
return Err(self.malformed("the document ends inside a tag".to_string()));
}
Some(b'>') => {
self.bump()?;
return Ok(false);
}
Some(b'/') => {
self.bump()?;
if self.peek()? != Some(b'>') {
return Err(self.malformed("'/' not followed by '>'".to_string()));
}
self.bump()?;
return Ok(true);
}
Some(_) => self.read_attribute()?,
}
}
}
fn read_attribute(&mut self) -> Result<(), ImportError> {
let mut name = Vec::new();
loop {
match self.peek()? {
None => return Err(self.malformed("the document ends inside a tag".to_string())),
Some(byte) if is_name_byte(byte) => {
self.bump()?;
if name.len() >= MAX_NAME_BYTES {
return Err(self.malformed(format!(
"an attribute name longer than {MAX_NAME_BYTES} bytes"
)));
}
name.push(byte);
}
Some(_) => break,
}
}
self.skip_whitespace()?;
if self.peek()? != Some(b'=') {
return Err(self.malformed(format!(
"attribute '{}' has no value",
String::from_utf8_lossy(&name)
)));
}
self.bump()?;
self.skip_whitespace()?;
let keep = self.kept.contains(&name.as_slice());
let lossy = keep && self.lossy.contains(&name.as_slice());
if let Some(value) = self.read_value(keep, lossy)?
&& keep
{
self.element.values.push((name, value));
}
Ok(())
}
fn read_value(&mut self, keep: bool, lossy: bool) -> Result<Option<Vec<u8>>, ImportError> {
let Some(quote) = self.peek()? else {
return Err(self.malformed("the document ends inside a tag".to_string()));
};
if quote != b'"' && quote != b'\'' {
return Err(self.malformed("an attribute value that is not quoted".to_string()));
}
self.bump()?;
let mut value = Vec::new();
let mut dropped = false;
loop {
let Some(byte) = self.peek()? else {
return Err(
self.malformed("the document ends inside an attribute value".to_string())
);
};
if byte == quote {
self.bump()?;
return Ok((!dropped).then_some(value));
}
let bytes = if byte == b'&' {
self.entity()?
} else {
self.bump()?;
vec![byte]
};
if keep && !dropped {
if value.len() + bytes.len() > self.max_value_bytes {
if !lossy {
return Err(self.malformed(format!(
"an attribute value longer than {} bytes",
self.max_value_bytes
)));
}
dropped = true;
value = Vec::new();
} else {
value.extend_from_slice(&bytes);
}
}
}
}
fn entity(&mut self) -> Result<Vec<u8>, ImportError> {
self.bump()?;
let mut name = Vec::new();
loop {
let Some(byte) = self.peek()? else {
return Err(self.malformed("the document ends inside an entity".to_string()));
};
self.bump()?;
if byte == b';' {
break;
}
if name.len() >= MAX_ENTITY_BYTES {
return Err(self.malformed("an entity reference with no ';'".to_string()));
}
name.push(byte);
}
match name.as_slice() {
b"lt" => Ok(vec![b'<']),
b"gt" => Ok(vec![b'>']),
b"amp" => Ok(vec![b'&']),
b"quot" => Ok(vec![b'"']),
b"apos" => Ok(vec![b'\'']),
reference if reference.first() == Some(&b'#') => self.character_reference(reference),
other => Err(self.malformed(format!(
"'&{};' is not a predefined entity, and this parser permits no \
declarations, so nothing can have defined it",
String::from_utf8_lossy(other)
))),
}
}
fn character_reference(&self, reference: &[u8]) -> Result<Vec<u8>, ImportError> {
let digits = String::from_utf8_lossy(&reference[1..]);
let code = match digits.strip_prefix(['x', 'X']) {
Some(hex) => u32::from_str_radix(hex, 16).ok(),
None => digits.parse::<u32>().ok(),
};
let Some(character) = code.and_then(char::from_u32) else {
return Err(self.malformed(format!("'&{digits};' is not a character")));
};
let mut encoded = [0u8; 4];
Ok(character.encode_utf8(&mut encoded).as_bytes().to_vec())
}
fn matches(&mut self, expected: &[u8]) -> Result<bool, ImportError> {
for wanted in expected {
match self.peek()? {
Some(byte) if byte == *wanted => {
self.bump()?;
}
_ => return Ok(false),
}
}
Ok(true)
}
fn skip_whitespace(&mut self) -> Result<(), ImportError> {
while matches!(self.peek()?, Some(b' ' | b'\t' | b'\r' | b'\n')) {
self.bump()?;
}
Ok(())
}
fn skip_until(&mut self, terminator: &[u8]) -> Result<(), ImportError> {
let mut matched = 0usize;
loop {
let Some(byte) = self.bump()? else {
return Err(self.malformed(format!(
"the document ends before '{}'",
String::from_utf8_lossy(terminator)
)));
};
if byte == terminator[matched] {
matched += 1;
if matched == terminator.len() {
return Ok(());
}
} else {
matched = usize::from(byte == terminator[0]);
}
}
}
}
impl Parser<'_> {
fn keep_text(&mut self, bytes: &[u8]) -> Result<(), ImportError> {
if self.text.len() + bytes.len() > MAX_TEXT_BYTES {
return Err(self.malformed(format!("element text longer than {MAX_TEXT_BYTES} bytes")));
}
self.text.extend_from_slice(bytes);
Ok(())
}
}
fn is_name_byte(byte: u8) -> bool {
!matches!(
byte,
b' ' | b'\t' | b'\r' | b'\n' | b'/' | b'>' | b'=' | b'<'
)
}