use crate::syntax::classify::{is_delimiter, is_whitespace};
use crate::syntax::{Lexer, SyntaxLimits, Token};
use crate::content::ContentError;
#[derive(Debug, Clone, PartialEq)]
pub enum Operand {
Int(i64),
Real(f64),
Bool(bool),
Null,
Name(Vec<u8>),
String(Vec<u8>),
Array(Vec<Operand>),
Dict(Vec<(Vec<u8>, Operand)>),
}
impl Operand {
pub fn as_f64(&self) -> Option<f64> {
match self {
Operand::Int(i) => Some(*i as f64),
Operand::Real(r) => Some(*r),
_ => None,
}
}
pub fn as_int(&self) -> Option<i64> {
match self {
Operand::Int(i) => Some(*i),
_ => None,
}
}
pub fn as_name(&self) -> Option<&[u8]> {
match self {
Operand::Name(n) => Some(n),
_ => None,
}
}
pub fn as_string(&self) -> Option<&[u8]> {
match self {
Operand::String(s) => Some(s),
_ => None,
}
}
pub fn as_array(&self) -> Option<&[Operand]> {
match self {
Operand::Array(a) => Some(a),
_ => None,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum Lexeme {
Operand(Operand),
Operator(Vec<u8>),
InlineImage {
dict: Vec<(Vec<u8>, Operand)>,
data: Vec<u8>,
},
}
#[derive(Debug)]
pub struct ContentLexer<'a> {
lexer: Lexer<'a>,
limits: &'a SyntaxLimits,
last_operator_offset: usize,
}
impl<'a> ContentLexer<'a> {
pub fn new(input: &'a [u8], limits: &'a SyntaxLimits) -> Self {
Self {
lexer: Lexer::new(input, 0, limits),
limits,
last_operator_offset: 0,
}
}
pub fn pos(&self) -> usize {
self.lexer.pos()
}
pub fn last_operator_offset(&self) -> usize {
self.last_operator_offset
}
pub fn skip_byte(&mut self) -> bool {
let pos = self.lexer.pos();
if pos >= self.lexer.input().len() {
return false;
}
self.lexer.set_pos(pos + 1);
true
}
pub fn next_lexeme(&mut self) -> Result<Option<Lexeme>, ContentError> {
let tok = self.lexer.next_token()?;
match tok.value {
Token::Eof => Ok(None),
Token::Keyword(kw) => {
if kw == b"BI" {
self.read_inline_image().map(Some)
} else {
self.last_operator_offset = usize::try_from(tok.start).unwrap_or(usize::MAX);
Ok(Some(Lexeme::Operator(kw)))
}
}
other => Ok(Some(Lexeme::Operand(self.operand_from_token(other, 0)?))),
}
}
fn operand_from_token(&mut self, tok: Token, depth: usize) -> Result<Operand, ContentError> {
if depth > self.limits.max_nesting_depth {
return Err(ContentError::NestingDepth(depth));
}
match tok {
Token::Boolean(b) => Ok(Operand::Bool(b)),
Token::Integer(i) => Ok(Operand::Int(i)),
Token::Real(r) => Ok(Operand::Real(r)),
Token::Null => Ok(Operand::Null),
Token::String(s) => Ok(Operand::String(s)),
Token::Name(n) => Ok(Operand::Name(n)),
Token::ArrayOpen => self.read_array(depth),
Token::DictOpen => Ok(Operand::Dict(self.read_dict(depth)?)),
Token::ArrayClose | Token::DictClose => Err(ContentError::Malformed(
"stray array/dict close in operands".into(),
)),
Token::Keyword(k) => Err(ContentError::Malformed(format!(
"keyword {:?} where operand expected",
String::from_utf8_lossy(&k)
))),
Token::Eof => Err(ContentError::Malformed(
"end of stream inside operand".into(),
)),
}
}
fn read_array(&mut self, depth: usize) -> Result<Operand, ContentError> {
let mut items = Vec::new();
loop {
let tok = self.lexer.next_token()?;
match tok.value {
Token::ArrayClose => return Ok(Operand::Array(items)),
Token::Eof => {
return Err(ContentError::Malformed("unterminated array operand".into()));
}
other => items.push(self.operand_from_token(other, depth + 1)?),
}
}
}
fn read_dict(&mut self, depth: usize) -> Result<Vec<(Vec<u8>, Operand)>, ContentError> {
let mut pairs = Vec::new();
loop {
let key_tok = self.lexer.next_token()?;
let key = match key_tok.value {
Token::DictClose => return Ok(pairs),
Token::Name(n) => n,
Token::Eof => {
return Err(ContentError::Malformed("unterminated dict operand".into()));
}
_ => return Err(ContentError::Malformed("dict key must be a name".into())),
};
let val_tok = self.lexer.next_token()?;
let value = self.operand_from_token(val_tok.value, depth + 1)?;
pairs.push((key, value));
}
}
fn read_inline_image(&mut self) -> Result<Lexeme, ContentError> {
let mut dict = Vec::new();
loop {
let tok = self.lexer.next_token()?;
match tok.value {
Token::Keyword(k) if k == b"ID" => break,
Token::Name(key) => {
let val_tok = self.lexer.next_token()?;
let value = self.operand_from_token(val_tok.value, 0)?;
dict.push((key, value));
}
Token::Eof => {
return Err(ContentError::Malformed("inline image missing ID".into()));
}
_ => {
return Err(ContentError::Malformed(
"inline image key must be a name".into(),
));
}
}
}
let input = self.lexer.input();
let mut start = self.lexer.pos();
if start < input.len() && is_whitespace(input[start]) {
start += 1;
}
let is_mask = matches!(
dict_get(&dict, &[b"IM", b"ImageMask"]),
Some(Operand::Bool(true))
);
let width = dict_get(&dict, &[b"W", b"Width"])
.and_then(Operand::as_int)
.unwrap_or(0);
let height = dict_get(&dict, &[b"H", b"Height"])
.and_then(Operand::as_int)
.unwrap_or(0);
let mut bpc = dict_get(&dict, &[b"BPC", b"BitsPerComponent"])
.and_then(Operand::as_int)
.unwrap_or(0);
if is_mask {
bpc = 1;
}
let cs = dict_get(&dict, &[b"CS", b"ColorSpace"]);
let filters = inline_filters(dict_get(&dict, &[b"F", b"Filter"]));
let declared_len = dict_get(&dict, &[b"L", b"Length"])
.and_then(Operand::as_int)
.filter(|&n| n >= 0)
.map(|n| n as usize);
let exact_len = declared_len.or_else(|| {
if !filters.is_empty() {
return None;
}
let ncomp = inline_ncomp(cs, is_mask)?;
if width <= 0 || height <= 0 || bpc <= 0 {
return None;
}
let row_bytes = ((width as u64) * (bpc as u64) * (ncomp as u64)).div_ceil(8);
usize::try_from(row_bytes.saturating_mul(height as u64)).ok()
});
let framed = exact_len
.and_then(|n| frame_by_length(input, start, n))
.or_else(|| frame_by_scan(input, start, &filters));
let (data_end, resync) = match framed {
Some(fr) => fr,
None => {
let resync = find_ws_bounded_ei(input, start)
.map(|ei| ei + 2)
.unwrap_or(input.len());
self.lexer.set_pos(resync);
return Err(ContentError::InlineImage(
"could not locate EI terminator".into(),
));
}
};
let data = input[start..data_end].to_vec();
self.lexer.set_pos(resync);
Ok(Lexeme::InlineImage { dict, data })
}
}
fn dict_get<'d>(dict: &'d [(Vec<u8>, Operand)], keys: &[&[u8]]) -> Option<&'d Operand> {
dict.iter()
.find(|(k, _)| keys.iter().any(|kk| *kk == k.as_slice()))
.map(|(_, v)| v)
}
fn inline_ncomp(cs: Option<&Operand>, is_mask: bool) -> Option<u32> {
if is_mask {
return Some(1);
}
match cs {
Some(Operand::Name(n)) => match n.as_slice() {
b"G" | b"DeviceGray" | b"CalGray" | b"Gray" => Some(1),
b"RGB" | b"DeviceRGB" | b"CalRGB" => Some(3),
b"CMYK" | b"DeviceCMYK" => Some(4),
b"I" | b"Indexed" => Some(1),
_ => None,
},
Some(Operand::Array(items)) => match items.first() {
Some(Operand::Name(n)) => match n.as_slice() {
b"I" | b"Indexed" => Some(1),
b"CalGray" => Some(1),
b"CalRGB" | b"Lab" => Some(3),
_ => None,
},
_ => None,
},
_ => None,
}
}
fn inline_filters(f: Option<&Operand>) -> Vec<Vec<u8>> {
match f {
Some(Operand::Name(n)) => vec![n.clone()],
Some(Operand::Array(items)) => items
.iter()
.filter_map(|o| match o {
Operand::Name(n) => Some(n.clone()),
_ => None,
})
.collect(),
_ => Vec::new(),
}
}
fn is_dct(name: &[u8]) -> bool {
matches!(name, b"DCT" | b"DCTDecode")
}
fn frame_by_length(input: &[u8], start: usize, n: usize) -> Option<(usize, usize)> {
let data_end = start.checked_add(n)?;
if data_end > input.len() {
return None;
}
let mut j = data_end;
while j < input.len() && is_whitespace(input[j]) {
j += 1;
}
let resync = if input.get(j) == Some(&b'E') && input.get(j + 1) == Some(&b'I') {
j + 2
} else {
find_ws_bounded_ei(input, data_end)
.map(|ei| ei + 2)
.unwrap_or(data_end)
};
Some((data_end, resync))
}
fn frame_by_scan(input: &[u8], start: usize, filters: &[Vec<u8>]) -> Option<(usize, usize)> {
if filters.iter().any(|f| is_dct(f)) {
if let Some(eoi) = rfind2(input, [0xFF, 0xD9], start) {
if let Some(ei) = find_ws_bounded_ei(input, eoi + 2) {
return Some((trim_trailing_ws(input, start, ei), ei + 2));
}
}
}
if let Some(first) = filters.first() {
if is_rle(first)
&& let Some(end) = run_length_eod(input, start)
&& find_ws_bounded_bi(input, start).is_none_or(|bi| bi >= end)
&& let Some(ei) = find_ws_bounded_ei(input, end)
{
return Some((end, ei + 2));
}
let eod: &[u8] = if is_a85(first) {
b"~>"
} else if is_ahx(first) {
b">"
} else {
b""
};
if !eod.is_empty()
&& let Some(end) = find_bytes(input, eod, start)
&& find_ws_bounded_bi(input, start).is_none_or(|bi| bi > end)
&& let Some(ei) = find_ws_bounded_ei(input, end + eod.len())
{
return Some((trim_trailing_ws(input, start, ei), ei + 2));
}
}
let ei = find_ws_bounded_ei(input, start)?;
Some((trim_trailing_ws(input, start, ei), ei + 2))
}
fn is_a85(f: &[u8]) -> bool {
f == b"A85" || f == b"ASCII85Decode"
}
fn is_ahx(f: &[u8]) -> bool {
f == b"AHx" || f == b"ASCIIHexDecode"
}
fn is_rle(f: &[u8]) -> bool {
f == b"RL" || f == b"RunLengthDecode"
}
fn run_length_eod(input: &[u8], start: usize) -> Option<usize> {
let mut pos = start;
loop {
let header = *input.get(pos)?;
pos += 1;
match header {
128 => return Some(pos),
0..=127 => {
let literal_len = usize::from(header) + 1;
pos = pos.checked_add(literal_len)?;
if pos > input.len() {
return None;
}
}
129..=255 => {
input.get(pos)?;
pos += 1;
}
}
}
}
fn find_ws_bounded_bi(input: &[u8], from: usize) -> Option<usize> {
let mut i = from;
while i + 1 < input.len() {
let before_ok = i == 0 || is_whitespace(input[i - 1]);
let after = input.get(i + 2).copied();
let after_ok = after.is_none_or(|b| is_whitespace(b) || is_delimiter(b));
if input[i] == b'B' && input[i + 1] == b'I' && before_ok && after_ok {
return Some(i);
}
i += 1;
}
None
}
fn find_bytes(input: &[u8], needle: &[u8], from: usize) -> Option<usize> {
if from >= input.len() || needle.is_empty() {
return None;
}
input[from..]
.windows(needle.len())
.position(|w| w == needle)
.map(|i| i + from)
}
fn find_ws_bounded_ei(input: &[u8], from: usize) -> Option<usize> {
let mut i = from;
while i + 1 < input.len() {
let before_ok = i == 0 || is_whitespace(input[i - 1]);
let after = input.get(i + 2).copied();
let after_ok = after.is_none_or(|b| is_whitespace(b) || is_delimiter(b));
if input[i] == b'E' && input[i + 1] == b'I' && before_ok && after_ok {
return Some(i);
}
i += 1;
}
None
}
fn rfind2(hay: &[u8], needle: [u8; 2], from: usize) -> Option<usize> {
if from + 2 > hay.len() {
return None;
}
let mut i = hay.len() - 2;
loop {
if i >= from && hay[i] == needle[0] && hay[i + 1] == needle[1] {
return Some(i);
}
if i == from {
return None;
}
i -= 1;
}
}
fn trim_trailing_ws(input: &[u8], start: usize, ei: usize) -> usize {
let mut data_end = ei;
if data_end > start && is_whitespace(input[data_end - 1]) {
data_end -= 1;
}
data_end
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use super::*;
fn lex_all(input: &[u8]) -> Vec<Lexeme> {
let limits = SyntaxLimits::default();
let mut lx = ContentLexer::new(input, &limits);
let mut out = Vec::new();
while let Some(l) = lx.next_lexeme().expect("lex error") {
out.push(l);
}
out
}
fn op(s: &[u8]) -> Lexeme {
Lexeme::Operator(s.to_vec())
}
#[test]
fn operands_then_operator() {
assert_eq!(
lex_all(b"72 720 Td"),
vec![
Lexeme::Operand(Operand::Int(72)),
Lexeme::Operand(Operand::Int(720)),
op(b"Td"),
]
);
}
#[test]
fn reals_and_names() {
assert_eq!(
lex_all(b"/DeviceRGB cs 0.2 0.3 0.4 sc"),
vec![
Lexeme::Operand(Operand::Name(b"DeviceRGB".to_vec())),
op(b"cs"),
Lexeme::Operand(Operand::Real(0.2)),
Lexeme::Operand(Operand::Real(0.3)),
Lexeme::Operand(Operand::Real(0.4)),
op(b"sc"),
]
);
}
#[test]
fn tj_array_operand() {
let out = lex_all(b"[(A) -120 (B)] TJ");
assert_eq!(
out,
vec![
Lexeme::Operand(Operand::Array(vec![
Operand::String(b"A".to_vec()),
Operand::Int(-120),
Operand::String(b"B".to_vec()),
])),
op(b"TJ"),
]
);
}
#[test]
fn booleans_are_operands_not_operators() {
assert_eq!(
lex_all(b"true false BX"),
vec![
Lexeme::Operand(Operand::Bool(true)),
Lexeme::Operand(Operand::Bool(false)),
op(b"BX"),
]
);
}
#[test]
fn text_show_sequence() {
assert_eq!(
lex_all(b"BT /F1 12 Tf 72 720 Td (Page 0) Tj ET"),
vec![
op(b"BT"),
Lexeme::Operand(Operand::Name(b"F1".to_vec())),
Lexeme::Operand(Operand::Int(12)),
op(b"Tf"),
Lexeme::Operand(Operand::Int(72)),
Lexeme::Operand(Operand::Int(720)),
op(b"Td"),
Lexeme::Operand(Operand::String(b"Page 0".to_vec())),
op(b"Tj"),
op(b"ET"),
]
);
}
#[test]
fn inline_image_framing() {
let data: [u8; 12] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
let mut src = b"q BI /W 2 /H 2 /BPC 8 /CS /RGB ID ".to_vec();
src.extend_from_slice(&data);
src.extend_from_slice(b"\nEI Q");
let out = lex_all(&src);
assert_eq!(out.first(), Some(&op(b"q")));
assert_eq!(out.last(), Some(&op(b"Q")));
let Lexeme::InlineImage { dict, data: got } = &out[1] else {
panic!("expected inline image, got {:?}", out[1]);
};
assert_eq!(got, &data);
assert_eq!(dict.len(), 4);
assert_eq!(dict[0], (b"W".to_vec(), Operand::Int(2)));
assert_eq!(dict[3], (b"CS".to_vec(), Operand::Name(b"RGB".to_vec())));
}
#[test]
fn stray_close_is_malformed() {
let limits = SyntaxLimits::default();
let mut lx = ContentLexer::new(b"] Q", &limits);
assert!(matches!(lx.next_lexeme(), Err(ContentError::Malformed(_))));
}
#[test]
fn inline_image_length_frames_past_false_ei() {
let data: [u8; 4] = [0x20, b'E', b'I', 0x20];
let mut src = b"q BI /W 4 /H 1 /BPC 8 /CS /G ID ".to_vec();
src.extend_from_slice(&data);
src.extend_from_slice(b"\nEI Q");
let out = lex_all(&src);
let Lexeme::InlineImage { data: got, .. } = &out[1] else {
panic!("expected inline image, got {:?}", out[1]);
};
assert_eq!(got, &data, "length framing must keep all 4 bytes");
assert_eq!(
out.last(),
Some(&op(b"Q")),
"cursor must resync past EI to Q"
);
}
#[test]
fn inline_image_filtered_dct_frames_on_eoi() {
let payload: [u8; 6] = [0x20, b'E', b'I', 0x20, 0xFF, 0xD9];
let mut src = b"BI /W 2 /H 2 /CS /RGB /F /DCTDecode ID ".to_vec();
src.extend_from_slice(&payload);
src.extend_from_slice(b"\nEI Q");
let out = lex_all(&src);
let Lexeme::InlineImage { data: got, .. } = &out[0] else {
panic!("expected inline image, got {:?}", out[0]);
};
assert_eq!(
got, &payload,
"DCT framing must span the whole payload to FF D9"
);
assert_eq!(out.last(), Some(&op(b"Q")));
}
#[test]
fn inline_image_run_length_frames_on_structural_eod() {
let payload: [u8; 8] = [5, 0x20, b'E', b'I', 0x20, 0x11, 0x22, 128];
let mut src = b"BI /W 6 /H 1 /BPC 8 /CS /G /F /RL ID ".to_vec();
src.extend_from_slice(&payload);
src.extend_from_slice(b"\nEI Q");
let out = lex_all(&src);
let Lexeme::InlineImage { data: got, .. } = &out[0] else {
panic!("expected inline image, got {:?}", out[0]);
};
assert_eq!(
got, &payload,
"RunLength framing must keep the literal false EI and EOD"
);
assert_eq!(out.last(), Some(&op(b"Q")));
}
#[test]
fn run_length_eod_ignores_literal_0x80_and_handles_repeat_packets() {
let encoded = [0, 0x80, 254, 0x41, 128, b' ', b'E', b'I'];
assert_eq!(run_length_eod(&encoded, 0), Some(5));
assert_eq!(run_length_eod(&encoded[..4], 0), None);
}
#[test]
fn inline_image_missing_ei_is_recoverable() {
let limits = SyntaxLimits::default();
let mut src = b"BI /W 2 /H 2 /CS /RGB /F /DCTDecode ID ".to_vec();
src.extend_from_slice(&[0x00, 0x01, 0x02, 0x03]); let mut lx = ContentLexer::new(&src, &limits);
let first = lx.next_lexeme();
assert!(
matches!(first, Err(ContentError::InlineImage(_))),
"missing EI must be a recoverable InlineImage error, got {first:?}"
);
assert!(matches!(lx.next_lexeme(), Ok(None)));
}
}