Decode XeTeX input files and streams with original byte spans

This commit is contained in:
gloamxun committed 2026-11-21 17:10:46 +08:00
1 parent 22e3ba993d
commit d11ac0cba1
18 files changed
+1040 -109

No files matched your search

+7
View File
@@ -232,6 +232,13 @@ impl SourceProvider for RecordingHost<'_> {
fn read_line(&mut self, stream: u8) -> Result<Option<Vec<u8>>, String> {
call!(self, read_line(stream))
}
fn read_xe_line(
&mut self,
stream: u8,
framing: texrra_tex_core::execution::XeLineFraming,
) -> Result<Option<Vec<u8>>, String> {
call!(self, read_xe_line(stream, framing))
}
fn close_in(&mut self, stream: u8) -> Result<(), String> {
call!(self, close_in(stream))
}
+72 -35
View File
@@ -112,6 +112,7 @@ struct Input {
reader: Box<dyn BufRead + Send>,
path: PathBuf,
offset: u64,
xe_lookahead: std::collections::VecDeque<u8>,
}
struct Output {
handle: u64,
@@ -1424,6 +1425,67 @@ pub(crate) fn absolute_output(cwd: &Path, path: &Path) -> Result<PathBuf, String
Ok(parent.join(path.file_name().ok_or("output requires a filename")?))
}
impl Files {
fn read_input_line(
&mut self,
stream: u8,
framing: Option<texrra_tex_core::execution::XeLineFraming>,
) -> Result<Option<Vec<u8>>, String> {
let Some(input) = self.inputs.get_mut(&stream) else {
let error = "host input stream is not open".to_owned();
self.stream_io.push(StreamEffect::ReadLine {
stream,
handle: None,
consumed_before: None,
consumed: std::sync::Arc::from([]),
observed_eof: false,
delivered: false,
error: Some(error.clone()),
});
return Err(error);
};
let before = input.offset;
let transfer = match framing {
Some(framing) => native_stream::xe_physical_line(
input.reader.as_mut(), &mut input.xe_lookahead, framing,
),
None => native_stream::physical_line(input.reader.as_mut()),
};
input.offset += transfer.bytes.len() as u64;
let error = transfer.error.map(|e| format!("read stream {stream}: {e}"));
self.stream_io.push(StreamEffect::ReadLine {
stream,
handle: Some(input.handle),
consumed_before: Some(before),
consumed: std::sync::Arc::from(transfer.bytes.clone()),
observed_eof: transfer.observed_eof,
delivered: error.is_none(),
error: error.clone(),
});
if let Some(error) = error {
self.world.observe_read_error(&input.path, error.clone());
Err(error)
} else {
let line = (!transfer.bytes.is_empty()).then_some(transfer.bytes);
self.world.observe_stream_read(&input.path, before, line.as_deref());
if framing == Some(texrra_tex_core::execution::XeLineFraming::Auto) {
// The detection may have inspected bytes in the following
// line. Validate those bytes without advancing delivered I/O.
let peeked = input.xe_lookahead.make_contiguous();
if !peeked.is_empty() {
self.world.observe_stream_read(&input.path, input.offset, Some(peeked));
}
if transfer.observed_eof && line.is_some() {
self.world.observe_stream_read(
&input.path, input.offset + peeked.len() as u64, None,
);
}
}
Ok(line)
}
}
}
impl SourceProvider for Files {
#[cfg(all(target_os = "linux", target_env = "gnu"))]
fn match_pdf_regex(
@@ -1647,6 +1709,7 @@ impl SourceProvider for Files {
reader,
path,
offset: 0,
xe_lookahead: Default::default(),
},
);
Ok(Some(opened))
@@ -1660,41 +1723,14 @@ impl SourceProvider for Files {
result.map(|opened| opened.map(|o| o.path.display().to_string()))
}
fn read_line(&mut self, stream: u8) -> Result<Option<Vec<u8>>, String> {
let Some(input) = self.inputs.get_mut(&stream) else {
let error = "host input stream is not open".to_owned();
self.stream_io.push(StreamEffect::ReadLine {
stream,
handle: None,
consumed_before: None,
consumed: std::sync::Arc::from([]),
observed_eof: false,
delivered: false,
error: Some(error.clone()),
});
return Err(error);
};
let before = input.offset;
let transfer = native_stream::physical_line(input.reader.as_mut());
input.offset += transfer.bytes.len() as u64;
let error = transfer.error.map(|e| format!("read stream {stream}: {e}"));
self.stream_io.push(StreamEffect::ReadLine {
stream,
handle: Some(input.handle),
consumed_before: Some(before),
consumed: std::sync::Arc::from(transfer.bytes.clone()),
observed_eof: transfer.observed_eof,
delivered: error.is_none(),
error: error.clone(),
});
if let Some(error) = error {
self.world.observe_read_error(&input.path, error.clone());
Err(error)
} else {
let line = (!transfer.bytes.is_empty()).then_some(transfer.bytes);
self.world
.observe_stream_read(&input.path, before, line.as_deref());
Ok(line)
}
self.read_input_line(stream, None)
}
fn read_xe_line(
&mut self,
stream: u8,
framing: texrra_tex_core::execution::XeLineFraming,
) -> Result<Option<Vec<u8>>, String> {
self.read_input_line(stream, Some(framing))
}
fn close_in(&mut self, stream: u8) -> Result<(), String> {
self.release_input(stream, ReleaseCause::Explicit, true);
@@ -2562,6 +2598,7 @@ mod generated_file_tests {
handle,
path: root.join("actual-stream"),
offset: 0,
xe_lookahead: Default::default(),
reader: Box::new(BufReader::new(reader)),
},
);
+72 -1
View File
@@ -2,12 +2,13 @@
//! by this handle, not an OS descriptor cursor hidden behind BufReader. This
//! journal does not serialize read-ahead buffers or authorize effect replay.
use std::{
collections::VecDeque,
fs::File,
io::{self, BufRead, Write},
path::PathBuf,
sync::Arc,
};
use texrra_tex_core::resources::ResourceVersion;
use texrra_tex_core::{execution::XeLineFraming, resources::ResourceVersion};
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct DescriptorIdentity {
@@ -208,6 +209,76 @@ pub(crate) fn physical_line(reader: &mut (impl BufRead + ?Sized)) -> LineTransfe
error: result.err(),
}
}
/// One XeTeX physical delimiter. Bytes read for BOM detection that belong to
/// a later line stay in this handle's lookahead until that line is delivered.
pub(crate) fn xe_physical_line(
reader: &mut (impl BufRead + ?Sized),
pending: &mut VecDeque<u8>,
mut framing: XeLineFraming,
) -> LineTransfer {
let mut bytes = Vec::new();
let mut observed_eof = false;
let result = (|| -> io::Result<()> {
if framing == XeLineFraming::Auto {
while pending.len() < 3 {
let buffer = reader.fill_buf()?;
if buffer.is_empty() {
observed_eof = true;
break;
}
let count = (3 - pending.len()).min(buffer.len());
pending.extend(&buffer[..count]);
reader.consume(count);
}
let (detected, bom) = XeLineFraming::detect(pending.make_contiguous());
framing = detected;
bytes.extend(pending.drain(..bom));
}
let width = framing.unit_width();
loop {
if width == 1 && pending.is_empty() {
let buffer = reader.fill_buf()?;
if buffer.is_empty() {
observed_eof = true;
break;
}
let terminator = buffer.iter().position(|&byte| matches!(byte, b'\r' | b'\n'));
let count = terminator.map_or(buffer.len(), |at| at + 1);
bytes.extend_from_slice(&buffer[..count]);
reader.consume(count);
if terminator.is_some() {
break;
}
continue;
}
let start = bytes.len();
while bytes.len() - start < width {
if let Some(byte) = pending.pop_front() {
bytes.push(byte);
continue;
}
let buffer = reader.fill_buf()?;
if buffer.is_empty() {
observed_eof = true;
return Ok(());
}
let count = (width - (bytes.len() - start)).min(buffer.len());
bytes.extend_from_slice(&buffer[..count]);
reader.consume(count);
}
if matches!(framing.code_unit(&bytes[start..]), Some(10 | 13)) {
break;
}
}
Ok(())
})();
LineTransfer {
bytes,
observed_eof,
error: result.err(),
}
}
pub(crate) struct WriteTransfer {
pub accepted: usize,
pub attempts: Vec<WriteAttempt>,