mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 06:24:39 +00:00
Merge remote-tracking branch 'origin/codex/fbx-strict-ascii' into fredapps/i778-fbx-batch
This commit is contained in:
+43
-8
@@ -235,9 +235,18 @@ pub fn is_binary(bytes: &[u8]) -> bool {
|
||||
|
||||
/// Read an FBX file (binary or ASCII) into its records.
|
||||
pub fn read_document(bytes: &[u8], limits: &Limits) -> Result<Document> {
|
||||
read_document_impl(bytes, limits, false)
|
||||
}
|
||||
|
||||
/// Read records strictly, checking ASCII delimiters and the binary closing record.
|
||||
pub fn read_document_strict(bytes: &[u8], limits: &Limits) -> Result<Document> {
|
||||
read_document_impl(bytes, limits, true)
|
||||
}
|
||||
|
||||
fn read_document_impl(bytes: &[u8], limits: &Limits, strict: bool) -> Result<Document> {
|
||||
let budget = Budget::new(limits.max_data_bytes);
|
||||
let (version, records) = if is_binary(bytes) {
|
||||
read_binary(bytes, &budget)?
|
||||
read_binary(bytes, &budget, strict)?
|
||||
} else {
|
||||
let text = match std::str::from_utf8(bytes) {
|
||||
Ok(text) => std::borrow::Cow::Borrowed(text),
|
||||
@@ -248,7 +257,7 @@ pub fn read_document(bytes: &[u8], limits: &Limits) -> Result<Document> {
|
||||
String::from_utf8_lossy(bytes)
|
||||
}
|
||||
};
|
||||
read_ascii(&text, &budget)?
|
||||
read_ascii(&text, &budget, strict)?
|
||||
};
|
||||
Ok(Document { version, records, left: budget.left.get() })
|
||||
}
|
||||
@@ -423,21 +432,28 @@ fn object_name(b: &[u8]) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
fn read_binary(bytes: &[u8], budget: &Budget) -> Result<(u32, Vec<Record>)> {
|
||||
fn read_binary(bytes: &[u8], budget: &Budget, strict: bool) -> Result<(u32, Vec<Record>)> {
|
||||
let mut pos = BINARY_MAGIC.len();
|
||||
let records_budget = Budget::new(budget.limit);
|
||||
let version = Bin { d: bytes, wide: false, budget, records_budget: &records_budget }.u32(&mut pos)?;
|
||||
let bin = Bin { d: bytes, wide: version >= 7500, budget, records_budget: &records_budget };
|
||||
let mut records = vec![];
|
||||
let mut closed = false;
|
||||
while pos < bytes.len() {
|
||||
match bin.record(&mut pos, bytes.len(), 0) {
|
||||
Ok(Some(n)) => records.push(n),
|
||||
Ok(None) => break,
|
||||
Ok(None) => {
|
||||
closed = true;
|
||||
break;
|
||||
}
|
||||
// A damaged tail (footer, padding) after complete sections is ignored.
|
||||
Err(crate::Error::Malformed(_)) if records.iter().any(|n| n.name == "Objects") => break,
|
||||
Err(crate::Error::Malformed(_)) if !strict && records.iter().any(|n| n.name == "Objects") => break,
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
}
|
||||
if strict && !closed {
|
||||
return Err(malformed(KIND, "missing binary closing record"));
|
||||
}
|
||||
Ok((version, records))
|
||||
}
|
||||
|
||||
@@ -459,6 +475,8 @@ struct Ascii<'a> {
|
||||
text: &'a str,
|
||||
pos: usize,
|
||||
lookahead: Option<Tok<'a>>,
|
||||
strict: bool,
|
||||
unterminated: bool,
|
||||
}
|
||||
|
||||
impl<'a> Ascii<'a> {
|
||||
@@ -486,6 +504,7 @@ impl<'a> Ascii<'a> {
|
||||
'"' => {
|
||||
let tail = rest.get(1..).unwrap_or("");
|
||||
let end = tail.find('"').unwrap_or(tail.len());
|
||||
self.unterminated |= end == tail.len();
|
||||
self.pos += 1 + end + usize::from(end < tail.len());
|
||||
return Some(Tok::Value(tail.get(..end).unwrap_or(""), true));
|
||||
}
|
||||
@@ -530,11 +549,14 @@ fn ascii_list(scan: &mut Ascii<'_>, budget: &Budget, records_budget: &Budget, de
|
||||
while let Some(t) = scan.take() {
|
||||
match t {
|
||||
Tok::Close if depth > 0 => return Ok(out),
|
||||
Tok::Close if scan.strict => return Err(malformed(KIND, "unbalanced closing brace")),
|
||||
Tok::Key(name) => {
|
||||
records_budget.take(std::mem::size_of::<Record>().checked_add(name.len()), "its records")?;
|
||||
let mut record = Record { name: name.to_string(), ..Record::default() };
|
||||
let mut needs_comma = false;
|
||||
loop {
|
||||
match scan.peek() {
|
||||
Some(Tok::Value(..) | Tok::Count) if scan.strict && needs_comma => break,
|
||||
Some(Tok::Value(value, quoted)) => {
|
||||
let number = if quoted {
|
||||
None
|
||||
@@ -548,9 +570,15 @@ fn ascii_list(scan: &mut Ascii<'_>, budget: &Budget, records_budget: &Budget, de
|
||||
.props
|
||||
.push(number.unwrap_or_else(|| Prop::Str(if quoted { value.replace(""", "\"") } else { value.to_string() })));
|
||||
}
|
||||
Some(Tok::Count | Tok::Comma) => {}
|
||||
Some(Tok::Count) => {}
|
||||
Some(Tok::Comma) => {
|
||||
needs_comma = false;
|
||||
scan.take();
|
||||
continue;
|
||||
}
|
||||
_ => break,
|
||||
}
|
||||
needs_comma = true;
|
||||
scan.take();
|
||||
}
|
||||
if scan.peek() == Some(Tok::Open) {
|
||||
@@ -559,16 +587,23 @@ fn ascii_list(scan: &mut Ascii<'_>, budget: &Budget, records_budget: &Budget, de
|
||||
}
|
||||
out.push(record);
|
||||
}
|
||||
_ if scan.strict => return Err(malformed(KIND, "expected a property name")),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
if scan.strict && depth > 0 {
|
||||
return Err(malformed(KIND, "missing closing brace"));
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
fn read_ascii(text: &str, budget: &Budget) -> Result<(u32, Vec<Record>)> {
|
||||
let mut scan = Ascii { text, pos: 0, lookahead: None };
|
||||
fn read_ascii(text: &str, budget: &Budget, strict: bool) -> Result<(u32, Vec<Record>)> {
|
||||
let mut scan = Ascii { text, pos: 0, lookahead: None, strict, unterminated: false };
|
||||
let records_budget = Budget::new(budget.limit);
|
||||
let records = ascii_list(&mut scan, budget, &records_budget, 0)?;
|
||||
if strict && scan.unterminated {
|
||||
return Err(malformed(KIND, "unterminated string"));
|
||||
}
|
||||
let version = records
|
||||
.iter()
|
||||
.find(|n| n.name == "FBXHeaderExtension")
|
||||
|
||||
@@ -783,3 +783,23 @@ fn ascii_budget_applies_while_reading_values_and_records() {
|
||||
let ignored = format!(";{}\nObjects: {{}}", ",".repeat(10_000));
|
||||
assert!(read_document(ignored.as_bytes(), &limits).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn strict_ascii_reports_unbalanced_and_unterminated_records() {
|
||||
let strict = |s: &str| crate::fbx::read_document_strict(s.as_bytes(), &Limits::default());
|
||||
assert!(strict(ASCII).is_ok());
|
||||
for s in ["Objects: { A: 1", "Objects: { } }", "Objects: { A: \"unterminated", "Objects: { A: 1 2 }"] {
|
||||
assert!(strict(s).is_err(), "{s}");
|
||||
}
|
||||
assert!(read_document(b"Objects: { A: 1", &Limits::default()).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn strict_binary_requires_a_closing_record() {
|
||||
let doc = document(ASCII.as_bytes());
|
||||
let bytes = write_binary(&doc.records, 7400, false);
|
||||
assert!(crate::fbx::read_document_strict(&bytes, &Limits::default()).is_ok());
|
||||
let truncated = &bytes[..bytes.len() - 29];
|
||||
assert!(read_document(truncated, &Limits::default()).is_ok());
|
||||
assert!(crate::fbx::read_document_strict(truncated, &Limits::default()).is_err());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user