1use std::collections::{BTreeMap, BTreeSet, HashMap};
5use std::io::{Cursor, Read};
6
7use cadmpeg_container::ArchiveSnapshot;
8use cadmpeg_core::decode::{DecodeArena, DecodeContext, DecodePolicy, ExpandSpec, View};
9use cadmpeg_core::CodecError;
10use serde::{Deserialize, Serialize};
11
12pub mod appearance;
14
15pub mod framing;
17
18pub mod property;
20use cadmpeg_ir::scalar::FiniteReal;
21use property::{DecodedProperty, PropertyContent, PropertyValue, RepeatedValues};
22
23mod layout;
25use layout::{continuation_page, record_start_page, terminal_page};
26
27pub const STREAM_HEADER_LEN: usize = layout::instance_stream_header::LEN;
29pub const PAGE_SIZE: usize = layout::record_start_page::LEN;
31pub const RECORD_MARKER: &[u8] = &record_start_page::MARKER_VALUE;
33pub const CONTINUATION_MARKER: &[u8] = &continuation_page::MARKER_VALUE;
35pub const TERMINAL_MARKER: &[u8] = &terminal_page::MARKER_VALUE;
37const MAX_SCHEMA_BYTES: u64 = 128 * 1024 * 1024;
38const MAX_RECOVERY_VALUES: u64 = 1_024;
39const MAX_INHERITED_PROPERTIES: usize = 1 << 18;
42
43fn take_lp_utf8_capped(bytes: &[u8], at: &mut usize, max: usize) -> Option<String> {
44 let count = usize::try_from(View::u32_le_at(bytes, *at)?).ok()?;
45 let start = at.checked_add(4)?;
46 *at = start;
47 if count > max {
48 return None;
49 }
50 let end = start.checked_add(count)?;
51 let value = std::str::from_utf8(bytes.get(start..end)?).ok()?.to_owned();
52 *at = end;
53 Some(value)
54}
55
56fn read_entry_bounded(
57 ctx: &DecodeContext<'_>,
58 entry: &mut impl Read,
59 declared_size: u64,
60) -> Result<Vec<u8>, CodecError> {
61 if declared_size > MAX_SCHEMA_BYTES {
62 return Err(schema_too_large(declared_size));
63 }
64 let mut writer = ctx.begin_expand(ExpandSpec::Exact(declared_size))?;
65 let mut limited = entry.take(MAX_SCHEMA_BYTES + 1);
66 let mut chunk = [0_u8; 16 * 1024];
67 loop {
68 let read = limited.read(&mut chunk)?;
69 if read == 0 {
70 break;
71 }
72 let count = cadmpeg_core::decode::u64_from_index(read);
73 let total = writer
74 .written()
75 .checked_add(count)
76 .ok_or_else(|| CodecError::malformed("Protein schema byte count overflows"))?;
77 if total > MAX_SCHEMA_BYTES {
78 return Err(schema_too_large(total));
79 }
80 writer.write(&chunk[..read])?;
81 }
82 writer.finalize_owned()
83}
84
85fn schema_too_large(bytes: u64) -> CodecError {
87 CodecError::NotImplemented(format!(
88 "Protein schema of {bytes} bytes exceeds the {MAX_SCHEMA_BYTES}-byte schema cap"
89 ))
90}
91
92#[derive(Clone, Copy, Debug, Eq, PartialEq)]
93enum ValueCarrier {
94 Boolean,
95 Integer,
96 Float,
97 UnitFloat,
98 Distance,
99 String,
100 Color,
101}
102
103#[derive(Clone, Copy, Debug, Eq, PartialEq)]
104enum ValueLayout {
105 Single(ValueCarrier),
106 Multiple(ValueCarrier),
107 TextureUri,
108}
109
110#[derive(Clone, Debug)]
111enum Property {
112 Reference {
113 multiple: bool,
114 },
115 Value {
116 layout: ValueLayout,
117 connectable: bool,
118 },
119}
120
121#[derive(Debug, Default)]
122struct Schema {
123 base: Option<String>,
124 properties: BTreeMap<String, Property>,
125}
126
127pub struct SchemaCatalog {
129 schemas: HashMap<String, Schema>,
130 properties: HashMap<String, BTreeMap<String, Property>>,
131 inherited_entries: usize,
132}
133
134impl SchemaCatalog {
135 fn new(schemas: HashMap<String, Schema>) -> Self {
136 Self {
137 schemas,
138 properties: HashMap::new(),
139 inherited_entries: 0,
140 }
141 }
142
143 pub fn load<'a>(
146 ctx: &DecodeContext<'a>,
147 protein: View<'a>,
148 ) -> Result<Option<Self>, CodecError> {
149 let archive = ArchiveSnapshot::new(ctx, protein)?;
150 let mut schemas = HashMap::new();
151 for entry in archive.entries() {
152 if !is_schema_entry(&entry.name) {
153 continue;
154 }
155 if entry.uncompressed_size > MAX_SCHEMA_BYTES {
156 return Err(schema_too_large(entry.uncompressed_size));
157 }
158 let xml = archive.open(ctx, &entry.name)?;
159 parse_schema_document(ctx, &entry.name, xml.window(), &mut schemas)?;
160 }
161 if schemas.is_empty() {
162 return Ok(None);
163 }
164 Ok(Some(Self::new(schemas)))
165 }
166
167 pub fn load_for_edit(protein: &[u8]) -> Result<Self, CodecError> {
169 let arena = DecodeArena::new();
170 let (ctx, _) = DecodeContext::from_root_bytes(protein, &arena, &DecodePolicy::default())?;
171 Ok(Self::new(schemas(&ctx, protein)?))
172 }
173
174 fn properties_for(
175 &mut self,
176 ctx: &DecodeContext<'_>,
177 name: &str,
178 ) -> Result<&BTreeMap<String, Property>, CodecError> {
179 resolve_inheritance(
180 ctx,
181 &self.schemas,
182 &mut self.properties,
183 &mut self.inherited_entries,
184 name,
185 )?;
186 self.properties.get(name).ok_or_else(|| {
187 CodecError::malformed(format_args!(
188 "Protein instance references absent schema {name}"
189 ))
190 })
191 }
192}
193
194fn resolve_inheritance(
195 ctx: &DecodeContext<'_>,
196 schemas: &HashMap<String, Schema>,
197 resolved: &mut HashMap<String, BTreeMap<String, Property>>,
198 inherited_entries: &mut usize,
199 name: &str,
200) -> Result<(), CodecError> {
201 let mut path = Vec::new();
202 let mut active = BTreeSet::new();
203 let mut depth_guards = Vec::new();
204 let mut current = name;
205 while !resolved.contains_key(current) {
206 if active.contains(current) {
207 return Err(CodecError::malformed(format_args!(
208 "Protein schema inheritance contains a cycle at {current}"
209 )));
210 }
211 let schema = schemas.get(current).ok_or_else(|| {
212 CodecError::malformed(format_args!(
213 "Protein instance references absent schema {current}"
214 ))
215 })?;
216 depth_guards.push(ctx.enter_nested("Protein schema inheritance")?);
217 active.insert(current);
218 path.push(current);
219 let Some(base) = schema.base.as_deref() else {
220 break;
221 };
222 current = base;
223 }
224 while let Some(current) = path.pop() {
225 let schema = schemas.get(current).ok_or_else(|| {
226 CodecError::malformed(format_args!(
227 "Protein instance references absent schema {current}"
228 ))
229 })?;
230 let inherited = schema.base.as_deref().and_then(|base| resolved.get(base));
231 inherited
232 .map_or(0, BTreeMap::len)
233 .checked_add(schema.properties.len())
234 .and_then(|entries| entries.checked_add(*inherited_entries))
235 .filter(|entries| *entries <= MAX_INHERITED_PROPERTIES)
236 .ok_or_else(|| inherited_properties_exceeded(current))?;
237 let mut properties = inherited.cloned().unwrap_or_default();
238 properties.extend(schema.properties.clone());
239 *inherited_entries += properties.len();
240 resolved.insert(current.to_owned(), properties);
241 drop(depth_guards.pop());
242 }
243 Ok(())
244}
245
246fn inherited_properties_exceeded(schema: &str) -> CodecError {
248 CodecError::NotImplemented(format!(
249 "Protein schema {schema} inherits past the {MAX_INHERITED_PROPERTIES}-entry inherited property cap"
250 ))
251}
252
253#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
255pub struct DecodedRecord {
256 pub ordinal: u64,
258 pub logical_offset: usize,
260 pub schema: String,
262 pub guid: String,
264 pub base: String,
266 pub asset_lib_id: String,
269 pub properties: BTreeMap<String, DecodedProperty>,
271}
272
273#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
275pub struct RejectedRecord {
276 pub ordinal: u64,
278 pub detail: String,
280}
281
282#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
284pub struct DecodeOutcome {
285 pub records: Vec<DecodedRecord>,
287 pub rejected: Vec<RejectedRecord>,
289}
290
291pub fn decode_detailed<'a>(
293 ctx: &DecodeContext<'a>,
294 protein: View<'a>,
295 instance: View<'a>,
296) -> Result<DecodeOutcome, CodecError> {
297 let mut catalog =
298 SchemaCatalog::load(ctx, protein)?.unwrap_or_else(|| SchemaCatalog::new(HashMap::new()));
299 let frames = framing::record_frames(instance.window())?;
300 decode_frames(ctx, &mut catalog, &frames)
301}
302
303pub fn decode_frames_for_edit(
305 protein: &[u8],
306 frames: &[framing::RecordFrame],
307) -> Result<DecodeOutcome, CodecError> {
308 let arena = DecodeArena::new();
309 let (ctx, _) = DecodeContext::from_root_bytes(protein, &arena, &DecodePolicy::default())?;
310 let mut catalog = SchemaCatalog::new(schemas(&ctx, protein)?);
311 decode_frames(&ctx, &mut catalog, frames)
312}
313
314pub fn decode_frames(
316 ctx: &DecodeContext<'_>,
317 catalog: &mut SchemaCatalog,
318 frames: &[framing::RecordFrame],
319) -> Result<DecodeOutcome, CodecError> {
320 let mut outcome = DecodeOutcome::default();
321 for (ordinal, frame) in frames.iter().enumerate() {
322 let ordinal = cadmpeg_core::decode::u64_from_index(ordinal);
323 match decode_record(ctx, frame.bytes(), catalog, ordinal, frame.logical_offset()) {
324 Ok(Some(record)) => {
325 outcome.records.push(record);
326 }
327 Ok(None) => {
328 outcome.rejected.push(RejectedRecord {
329 ordinal,
330 detail: "Protein instance record header is malformed".to_owned(),
331 });
332 }
333 Err(error @ CodecError::ResourceLimit(_)) => return Err(error),
334 Err(error) => {
335 outcome.rejected.push(RejectedRecord {
336 ordinal,
337 detail: error.to_string(),
338 });
339 }
340 }
341 }
342 Ok(outcome)
343}
344
345pub fn has_schemas(protein: &[u8]) -> bool {
347 let Ok(mut archive) = zip::ZipArchive::new(Cursor::new(protein)) else {
348 return false;
349 };
350 (0..archive.len()).any(|index| {
358 archive
359 .by_index_raw(index)
360 .is_ok_and(|entry| entry.name().is_ok_and(|name| is_schema_entry(&name)))
361 })
362}
363
364fn is_schema_entry(name: &str) -> bool {
365 (name.starts_with("Schemas/") || name.contains("/Schemas/")) && name.ends_with("Schema.xml")
366}
367
368fn schemas(ctx: &DecodeContext<'_>, protein: &[u8]) -> Result<HashMap<String, Schema>, CodecError> {
369 let mut archive = zip::ZipArchive::new(Cursor::new(protein)).map_err(|error| {
370 CodecError::malformed(format_args!("cannot open nested Protein ZIP: {error}"))
371 })?;
372 let mut schemas = HashMap::new();
373 let mut entry_names = BTreeSet::new();
374 for index in 0..archive.len() {
375 let (name, is_zstd) = {
380 let entry = archive.by_index_raw(index).map_err(|error| {
381 CodecError::malformed(format_args!("cannot read nested Protein entry: {error}"))
382 })?;
383 let name = entry
384 .name()
385 .map_err(|error| {
386 CodecError::malformed(format_args!("invalid Protein entry name: {error}"))
387 })?
388 .into_owned();
389 (name, entry.compression() == zip::CompressionMethod::ZSTD)
390 };
391 if !entry_names.insert(name.clone()) {
392 return Err(CodecError::malformed(format_args!(
393 "Protein archive defines entry {name} more than once"
394 )));
395 }
396 if !is_schema_entry(&name) {
397 continue;
398 }
399 let bytes = if is_zstd {
400 let mut entry = archive.by_index_raw(index).map_err(|error| {
402 CodecError::malformed(format_args!("cannot read nested Protein entry: {error}"))
403 })?;
404 let size = entry.size();
405 if size > MAX_SCHEMA_BYTES {
406 return Err(schema_too_large(size));
407 }
408 let crc32 = entry.crc32();
409 let stored_size = entry.compressed_size();
410 let stored = read_entry_bounded(ctx, &mut entry, stored_size)?;
411 cadmpeg_container::compression::expand_zstd_member_owned(
412 ctx,
413 &stored,
414 ExpandSpec::Exact(size),
415 crc32,
416 )?
417 } else {
418 let mut entry = archive.by_index(index).map_err(|error| {
419 CodecError::malformed(format_args!("cannot read nested Protein entry: {error}"))
420 })?;
421 let size = entry.size();
422 read_entry_bounded(ctx, &mut entry, size)?
423 };
424 parse_schema_document(ctx, &name, &bytes, &mut schemas)?;
425 }
426 Ok(schemas)
427}
428
429fn parse_schema_document(
430 ctx: &DecodeContext<'_>,
431 name: &str,
432 bytes: &[u8],
433 schemas: &mut HashMap<String, Schema>,
434) -> Result<(), CodecError> {
435 let xml = std::str::from_utf8(bytes).map_err(|error| {
436 CodecError::malformed(format_args!("Protein schema {name} is not UTF-8: {error}"))
437 })?;
438 let document = ctx
439 .parse_xml(xml, "Protein schema XML tree")
440 .map_err(|error| {
441 let CodecError::Malformed(error) = error else {
442 return error;
443 };
444 CodecError::malformed(format_args!(
445 "Protein schema {name} is malformed XML: {error}"
446 ))
447 })?;
448 let root = document.root_element();
449 let uid = root
450 .children()
451 .find(|node| node.has_tag_name("UID"))
452 .and_then(|node| node.attribute("val"))
453 .ok_or_else(|| CodecError::malformed(format_args!("Protein schema {name} has no UID")))?;
454 let mut schema = Schema::default();
455 for node in root.children().filter(roxmltree::Node::is_element) {
456 if node.has_tag_name("Base") {
457 if let Some(value) = node.attribute("val") {
458 schema.base = Some(value.to_owned());
459 }
460 continue;
461 }
462 if node.has_tag_name("PropertyAlias") {
463 continue;
464 }
465 if node.attribute("readonly") == Some("true")
466 || node.attribute("definitionIteratorData") == Some("true")
467 {
468 continue;
469 }
470 let Some(property) = schema_property(node) else {
471 continue;
472 };
473 let Some(id) = node.attribute("id") else {
474 continue;
475 };
476 if schema.properties.contains_key(id) {
477 return Err(CodecError::malformed(format_args!(
478 "Protein schema {uid} declares property {id} more than once"
479 )));
480 }
481 schema.properties.insert(id.to_owned(), property);
482 }
483 if schemas.contains_key(uid) {
484 return Err(CodecError::malformed(format_args!(
485 "Protein archive defines schema {uid} more than once"
486 )));
487 }
488 schemas.insert(uid.to_owned(), schema);
489 Ok(())
490}
491
492fn schema_property(node: roxmltree::Node<'_, '_>) -> Option<Property> {
493 let multiple = node.attribute("allowmultiplevalues") == Some("true");
494 let connectable = node.attribute("allowconnectedassets").is_some();
495 let carrier = match node.tag_name().name() {
496 "Reference" => return Some(Property::Reference { multiple }),
497 "TextureURI" => {
498 return Some(Property::Value {
499 layout: ValueLayout::TextureUri,
500 connectable,
501 });
502 }
503 "Boolean" => ValueCarrier::Boolean,
504 "Integer" | "Choice" => ValueCarrier::Integer,
505 "Float" if node.attribute("unit").is_some() => ValueCarrier::UnitFloat,
506 "Float" => ValueCarrier::Float,
507 "Distance" => ValueCarrier::Distance,
508 "String" | "Uuid" | "URL" => ValueCarrier::String,
509 "Color" => ValueCarrier::Color,
510 _ => return None,
511 };
512 Some(Property::Value {
513 layout: if multiple {
514 ValueLayout::Multiple(carrier)
515 } else {
516 ValueLayout::Single(carrier)
517 },
518 connectable,
519 })
520}
521
522fn decode_record(
523 ctx: &DecodeContext<'_>,
524 record: &[u8],
525 catalog: &mut SchemaCatalog,
526 ordinal: u64,
527 logical_offset: usize,
528) -> Result<Option<DecodedRecord>, CodecError> {
529 decode_record_with_selection(
530 ctx,
531 record,
532 catalog,
533 ordinal,
534 logical_offset,
535 instance_property_serializes,
536 )
537}
538
539fn decode_record_with_selection(
540 ctx: &DecodeContext<'_>,
541 record: &[u8],
542 catalog: &mut SchemaCatalog,
543 ordinal: u64,
544 logical_offset: usize,
545 serializes: impl Fn(&str) -> bool,
546) -> Result<Option<DecodedRecord>, CodecError> {
547 if !record.starts_with(RECORD_MARKER) {
548 return Ok(None);
549 }
550 let mut at = RECORD_MARKER.len();
551 let Some(schema) = take_lp_utf8_capped(record, &mut at, 1_048_576) else {
552 return Ok(None);
553 };
554 let Some(guid) = take_lp_utf8_capped(record, &mut at, 1_048_576) else {
555 return Ok(None);
556 };
557 let Some(base) = take_lp_utf8_capped(record, &mut at, 1_048_576) else {
558 return Ok(None);
559 };
560 let Some(asset_lib_id) = take_lp_utf8_capped(record, &mut at, 1_048_576) else {
565 return Ok(None);
566 };
567 let properties = catalog.properties_for(ctx, &schema)?;
568 let mut values = BTreeMap::new();
569 for (id, property) in properties {
570 if !serializes(id) {
571 continue;
572 }
573 let property_at = at;
574 let value_offset = if matches!(
575 property,
576 Property::Value {
577 layout: ValueLayout::Single(ValueCarrier::UnitFloat | ValueCarrier::Distance),
578 ..
579 }
580 ) {
581 property_at.checked_add(4).ok_or_else(|| {
582 CodecError::malformed(format_args!(
583 "Protein {schema} instance {guid} property {id} offset overflows usize"
584 ))
585 })?
586 } else {
587 property_at
588 };
589 let value_error = |error: CodecError, at: usize| {
590 if matches!(&error, CodecError::ResourceLimit(_)) {
591 return error;
592 }
593 CodecError::malformed(format_args!(
594 "Protein {schema} instance {guid} property {id} at {property_at}..{at}/{}: {error}",
595 record.len()
596 ))
597 };
598 let connection_error = |error: CodecError, at: usize| {
599 if matches!(&error, CodecError::ResourceLimit(_)) {
600 return error;
601 }
602 CodecError::malformed(format_args!(
603 "Protein {schema} instance {guid} property {id} connection at {at}/{}: {error}",
604 record.len()
605 ))
606 };
607 let content = match property {
608 Property::Reference { multiple } => {
609 let count = (*multiple)
610 .then(|| read_count(record, &mut at, id))
611 .transpose()
612 .map_err(|error| value_error(error, at))?;
613 let targets = read_connections(record, &mut at)
614 .map_err(|error| connection_error(error, at))?;
615 match count {
616 Some(count) => match std::num::NonZeroUsize::new(count) {
617 Some(count) => PropertyContent::MultipleReferences { count, targets },
618 None => PropertyContent::Value {
619 value: PropertyValue::Multiple(RepeatedValues::default()),
620 connections: targets,
621 },
622 },
623 None => PropertyContent::Reference(targets),
624 }
625 }
626 Property::Value {
627 layout,
628 connectable,
629 } => {
630 let value = read_property(record, &mut at, *layout, id)
631 .map_err(|error| value_error(error, at))?;
632 let connections = (*connectable)
633 .then(|| read_connections(record, &mut at))
634 .transpose()
635 .map_err(|error| connection_error(error, at))?
636 .unwrap_or_default();
637 PropertyContent::Value { value, connections }
638 }
639 };
640 values.insert(
641 id.clone(),
642 DecodedProperty {
643 value_offset,
644 content,
645 },
646 );
647 }
648 if at != record.len() {
649 return Err(CodecError::malformed(format_args!(
650 "Protein {schema} instance {guid} consumed {at} of {} record bytes",
651 record.len()
652 )));
653 }
654 Ok(Some(DecodedRecord {
655 ordinal,
656 logical_offset,
657 schema,
658 guid,
659 base,
660 asset_lib_id,
661 properties: values,
662 }))
663}
664
665fn instance_property_serializes(id: &str) -> bool {
668 !matches!(id, "AssetLibID" | "texture_MapChannel_ID_Advanced")
669}
670
671fn read_property(
672 bytes: &[u8],
673 at: &mut usize,
674 layout: ValueLayout,
675 id: &str,
676) -> Result<PropertyValue, CodecError> {
677 match layout {
678 ValueLayout::Single(carrier) => read_value(bytes, at, carrier, id),
679 ValueLayout::TextureUri => read_texture_uri(bytes, at, id),
682 ValueLayout::Multiple(carrier) => {
683 let count = read_count(bytes, at, id)?;
684 let mut values = Vec::new();
685 for _ in 0..count {
686 values.push(read_value(bytes, at, carrier, id)?);
687 }
688 Ok(PropertyValue::Multiple(
689 values.try_into().map_err(CodecError::malformed)?,
690 ))
691 }
692 }
693}
694
695fn read_texture_uri(bytes: &[u8], at: &mut usize, id: &str) -> Result<PropertyValue, CodecError> {
698 let malformed = || CodecError::malformed(format_args!("Protein property {id} is truncated"));
699 let kind = take::<1>(bytes, at).ok_or_else(malformed)?[0];
700 if kind == 1 {
701 return Ok(PropertyValue::TextureUri(vec![take_lp_utf8_capped(
702 bytes, at, 1_048_576,
703 )
704 .ok_or_else(malformed)?]));
705 }
706 if kind != 0 {
707 return Err(CodecError::malformed(format_args!(
708 "Protein TextureURI property {id} has invalid kind {kind}"
709 )));
710 }
711 let count = read_count(bytes, at, id)?;
712 let mut paths = Vec::new();
713 for _ in 0..count {
714 paths.push(take_lp_utf8_capped(bytes, at, 1_048_576).ok_or_else(malformed)?);
715 }
716 Ok(PropertyValue::TextureUri(paths))
717}
718
719fn read_count(bytes: &[u8], at: &mut usize, id: &str) -> Result<usize, CodecError> {
720 let count = usize::try_from(read_u32_le(bytes, at).ok_or_else(|| {
721 CodecError::malformed(format_args!("Protein property {id} is truncated"))
722 })?)
723 .map_err(|_| CodecError::Malformed("Protein value count exceeds usize".into()))?;
724 if cadmpeg_core::decode::u64_from_index(count) > MAX_RECOVERY_VALUES {
725 return Err(CodecError::malformed(format_args!(
726 "Protein property {id} declares {count} values; at most {MAX_RECOVERY_VALUES} are decoded"
727 )));
728 }
729 Ok(count)
730}
731
732fn read_u32_le(bytes: &[u8], at: &mut usize) -> Option<u32> {
733 let value = View::u32_le_at(bytes, *at)?;
734 *at = (*at).checked_add(4)?;
735 Some(value)
736}
737
738fn read_f64_le(bytes: &[u8], at: &mut usize) -> Option<f64> {
739 let value = View::f64_le_at(bytes, *at)?;
740 *at = (*at).checked_add(8)?;
741 Some(value)
742}
743
744fn read_value(
745 bytes: &[u8],
746 at: &mut usize,
747 carrier: ValueCarrier,
748 id: &str,
749) -> Result<PropertyValue, CodecError> {
750 let malformed = || CodecError::malformed(format_args!("Protein property {id} is truncated"));
751 Ok(match carrier {
752 ValueCarrier::Boolean => {
753 PropertyValue::Boolean(take::<1>(bytes, at).ok_or_else(malformed)?[0] != 0)
754 }
755 ValueCarrier::Integer => {
756 PropertyValue::Integer(read_u32_le(bytes, at).ok_or_else(malformed)?)
757 }
758 ValueCarrier::Float => PropertyValue::Float(finite_value(
759 read_f64_le(bytes, at).ok_or_else(malformed)?,
760 id,
761 )?),
762 ValueCarrier::UnitFloat => PropertyValue::UnitFloat {
763 unit: read_u32_le(bytes, at).ok_or_else(malformed)?,
764 value: finite_value(read_f64_le(bytes, at).ok_or_else(malformed)?, id)?,
765 },
766 ValueCarrier::Distance => PropertyValue::Distance {
767 unit: read_u32_le(bytes, at).ok_or_else(malformed)?,
768 value: finite_value(read_f64_le(bytes, at).ok_or_else(malformed)?, id)?,
769 },
770 ValueCarrier::String => {
771 PropertyValue::String(take_lp_utf8_capped(bytes, at, 1_048_576).ok_or_else(malformed)?)
772 }
773 ValueCarrier::Color => {
774 let mut rgba = [FiniteReal::ZERO; 4];
775 for value in &mut rgba {
776 *value = finite_value(read_f64_le(bytes, at).ok_or_else(malformed)?, id)?;
777 }
778 PropertyValue::Color(rgba)
779 }
780 })
781}
782
783fn finite_value(value: f64, id: &str) -> Result<FiniteReal, CodecError> {
784 FiniteReal::new(value)
785 .ok_or_else(|| CodecError::malformed(format_args!("Protein property {id} is not finite")))
786}
787
788fn read_connections(bytes: &[u8], at: &mut usize) -> Result<Vec<String>, CodecError> {
792 let Some(present) = take::<1>(bytes, at) else {
793 return Err(CodecError::Malformed(
794 "Protein property connection flag is truncated".into(),
795 ));
796 };
797 if present == [0] {
798 return Ok(Vec::new());
799 }
800 if present != [1] {
801 return Err(CodecError::malformed(format_args!(
802 "Protein property has invalid connection flag {}",
803 present[0]
804 )));
805 }
806 let kind = take::<1>(bytes, at).ok_or_else(|| {
807 CodecError::Malformed("Protein property connection kind is truncated".into())
808 })?;
809 if kind != [1] {
810 return Err(CodecError::malformed(format_args!(
811 "Protein property has invalid connection kind {}",
812 kind[0]
813 )));
814 }
815 let count = read_count(bytes, at, "connection")?;
816 let mut connections = Vec::new();
817 for _ in 0..count {
818 connections.push(take_lp_utf8_capped(bytes, at, 1_048_576).ok_or_else(|| {
819 CodecError::Malformed("Protein property connection GUID is truncated".into())
820 })?);
821 }
822 Ok(connections)
823}
824
825fn take<const N: usize>(bytes: &[u8], at: &mut usize) -> Option<[u8; N]> {
826 let end = at.checked_add(N)?;
827 let value = bytes.get(*at..end)?.try_into().ok()?;
828 *at = end;
829 Some(value)
830}
831
832#[cfg(test)]
833mod tests {
834 #![allow(clippy::unwrap_used)] use std::io::{Cursor, Write};
837
838 use cadmpeg_core::decode::{DecodeArena, DecodeContext, DecodePolicy, ResourceDimension};
839 use cadmpeg_core::CodecError;
840
841 use super::{
842 framing, instance_property_serializes, read_connections, read_texture_uri, read_value,
843 FiniteReal, RepeatedValues, ValueCarrier, CONTINUATION_MARKER, PAGE_SIZE, RECORD_MARKER,
844 STREAM_HEADER_LEN, TERMINAL_MARKER,
845 };
846 use crate::property::{PropertyContent, PropertyValue};
847
848 fn decode_fixture(protein: &[u8], instance: &[u8]) -> Result<super::DecodeOutcome, CodecError> {
849 let mut bytes = protein.to_vec();
850 bytes.extend_from_slice(instance);
851 let arena = DecodeArena::new();
852 let (ctx, root) = DecodeContext::from_root_bytes(&bytes, &arena, &DecodePolicy::service())?;
853 let protein_view = root.child(0, protein.len()).expect("fixture Protein range");
854 let instance_view = root
855 .child(protein.len(), bytes.len())
856 .expect("fixture instance range");
857 super::decode_detailed(&ctx, protein_view, instance_view)
858 }
859
860 fn with_service_context<T>(
861 bytes: &[u8],
862 use_context: impl FnOnce(&DecodeContext<'_>) -> T,
863 ) -> T {
864 let arena = DecodeArena::new();
865 let (ctx, _) = DecodeContext::from_root_bytes(bytes, &arena, &DecodePolicy::service())
866 .expect("build the decode context");
867 use_context(&ctx)
868 }
869
870 #[test]
871 fn unit_float_preserves_serialized_tags_and_finite_values() {
872 for unit in [
873 0x1001u32,
874 0x11001,
875 0x21008,
876 0x37001,
877 0x43003,
878 0x45001,
879 0x46001,
880 0x47001,
881 0x48001,
882 0x200d,
883 0x200e,
884 0x2016,
885 u32::MAX,
886 ] {
887 let mut bytes = unit.to_le_bytes().to_vec();
888 bytes.extend_from_slice(&(-0.0f64).to_le_bytes());
889 let mut offset = 0;
890 let value = super::read_value(&bytes, &mut offset, super::ValueCarrier::UnitFloat, "x")
891 .expect("complete finite tagged scalar");
892 assert_eq!(offset, 12);
893 let PropertyValue::UnitFloat {
894 unit: actual,
895 value,
896 } = value
897 else {
898 panic!("unit tag must survive the scalar boundary");
899 };
900 assert_eq!(actual, unit);
901 assert_eq!(value.get().to_bits(), (-0.0f64).to_bits());
902 }
903 for value in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
904 let mut bytes = 0x1001u32.to_le_bytes().to_vec();
905 bytes.extend_from_slice(&value.to_le_bytes());
906 assert!(
907 super::read_value(&bytes, &mut 0, super::ValueCarrier::UnitFloat, "x").is_err()
908 );
909 }
910 }
911
912 #[test]
913 fn schema_edit_reader_reads_the_declared_bytes() {
914 let arena = DecodeArena::new();
915 let (ctx, _) =
916 DecodeContext::from_root_bytes(b"xml", &arena, &DecodePolicy::service()).expect("root");
917 assert_eq!(
918 super::read_entry_bounded(&ctx, &mut Cursor::new(b"xml"), 3)
919 .expect("read the archive entry"),
920 b"xml"
921 );
922 }
923
924 #[test]
925 fn schema_size_cap_is_unsupported_and_expansion_limit_refuses() {
926 with_service_context(&[], |ctx| {
927 let error =
928 super::read_entry_bounded(ctx, &mut Cursor::new(b""), super::MAX_SCHEMA_BYTES + 1)
929 .expect_err("local byte ceiling");
930 assert!(matches!(error, CodecError::NotImplemented(_)), "{error:?}");
931 });
932 let mut policy = DecodePolicy::service();
933 policy.limits.max_decompressed_bytes_per_expand = 2;
934 let arena = DecodeArena::new();
935 let (ctx, _) = DecodeContext::from_root_bytes(b"xml", &arena, &policy).expect("root");
936 let error = super::read_entry_bounded(&ctx, &mut Cursor::new(b"xml"), 3)
937 .expect_err("caller expansion limit");
938 assert!(
939 matches!(error, CodecError::ResourceLimit(limit) if limit.dimension == ResourceDimension::DecompressedBytes)
940 );
941 }
942
943 #[test]
944 fn schema_expansion_refuses_before_xml_materialization() {
945 let xml = br#"<Schema><UID val="Simple"/><String id="comment"/></Schema>"#;
946 let mut writer = zip::ZipWriter::new(Cursor::new(Vec::new()));
947 writer
948 .start_file(
949 "Schemas/SimpleSchema.xml",
950 zip::write::SimpleFileOptions::default()
951 .compression_method(zip::CompressionMethod::Deflated),
952 )
953 .expect("schema entry starts");
954 writer.write_all(xml).expect("schema XML writes");
955 let protein = writer.finish().expect("archive finishes").into_inner();
956 let arena = DecodeArena::new();
957 let mut policy = DecodePolicy::service();
958 policy.limits.max_decompressed_bytes_per_expand =
959 cadmpeg_core::decode::u64_from_index(xml.len()) - 1;
960 let (ctx, root) = DecodeContext::from_root_bytes(&protein, &arena, &policy)
961 .expect("ZIP fits input limit");
962 assert!(matches!(
963 super::SchemaCatalog::load(&ctx, root),
964 Err(CodecError::ResourceLimit(limit))
965 if limit.dimension == ResourceDimension::DecompressedBytes
966 ));
967 let arena = DecodeArena::new();
968 let (ctx, root) =
969 DecodeContext::from_root_bytes(&protein, &arena, &DecodePolicy::service())
970 .expect("ZIP fits service input limit");
971 assert!(super::SchemaCatalog::load(&ctx, root)
972 .expect("load the schema catalog")
973 .is_some());
974 }
975
976 #[test]
977 fn schema_past_the_zstd_window_allowance_is_unsupported_without_a_refusal() {
978 use cadmpeg_test_support::zip_fixture::{self, RawEntry};
979 let xml = br#"<Schema><UID val="Simple"/><String id="comment"/></Schema>"#;
980 let protein = zip_fixture::archive(&[RawEntry::compressed(
984 "Schemas/SimpleSchema.xml",
985 zip_fixture::ZSTD,
986 &zip_fixture::zstd_streamed_frame(27, xml),
987 xml,
988 )]);
989 let arena = DecodeArena::new();
990 let (ctx, root) =
991 DecodeContext::from_root_bytes(&protein, &arena, &DecodePolicy::service())
992 .expect("ZIP fits service input limit");
993 let error = super::SchemaCatalog::load(&ctx, root)
994 .err()
995 .expect("the schema cannot be read");
996 assert!(
997 matches!(&error, CodecError::NotImplemented(message) if message.ends_with("per-expansion allowance")),
998 "{error:?}"
999 );
1000 assert_eq!(ctx.resource_refusal(), None);
1001 assert!(ctx.finish_session().is_ok());
1002 }
1003
1004 #[test]
1005 fn schema_inheritance_refuses_at_active_depth_limit() {
1006 let schemas = std::collections::HashMap::from([
1007 (
1008 "AChild".into(),
1009 super::Schema {
1010 base: Some("ZBase".into()),
1011 ..super::Schema::default()
1012 },
1013 ),
1014 ("ZBase".into(), super::Schema::default()),
1015 ]);
1016 let arena = DecodeArena::new();
1017 let mut policy = DecodePolicy::service();
1018 policy.limits.max_recursion_depth = 1;
1019 let (ctx, _) =
1020 DecodeContext::from_root_bytes(&[], &arena, &policy).expect("fixture fits input limit");
1021 assert!(matches!(
1022 super::resolve_inheritance(
1023 &ctx,
1024 &schemas,
1025 &mut std::collections::HashMap::new(),
1026 &mut 0,
1027 "AChild"
1028 ),
1029 Err(CodecError::ResourceLimit(limit))
1030 if limit.dimension == ResourceDimension::RecursionDepth
1031 ));
1032 let arena = DecodeArena::new();
1033 let (ctx, _) = DecodeContext::from_root_bytes(&[], &arena, &DecodePolicy::service())
1034 .expect("build the decode context");
1035 assert!(super::resolve_inheritance(
1036 &ctx,
1037 &schemas,
1038 &mut std::collections::HashMap::new(),
1039 &mut 0,
1040 "AChild"
1041 )
1042 .is_ok());
1043 }
1044
1045 #[test]
1046 fn edit_decode_uses_default_inheritance_limit() {
1047 fn archive_with_depth(depth: usize) -> Vec<u8> {
1048 let entries = (0..depth)
1049 .map(|index| {
1050 let name = format!("Schemas/Schema{index}/Schema.xml");
1051 let base = index
1052 .checked_sub(1)
1053 .map_or_else(String::new, |base| format!("<Base val=\"Schema{base}\"/>"));
1054 let xml = format!("<Schema><UID val=\"Schema{index}\"/>{base}</Schema>");
1055 (name, xml)
1056 })
1057 .collect::<Vec<_>>();
1058 let borrowed = entries
1059 .iter()
1060 .map(|(name, xml)| (name.as_str(), xml.as_str()))
1061 .collect::<Vec<_>>();
1062 schema_archive(&borrowed)
1063 }
1064
1065 fn frames_for_schema(name: &str) -> Vec<framing::RecordFrame> {
1066 let mut record = Vec::new();
1067 for value in [name, "guid", "base", ""] {
1068 push_lp(&mut record, value);
1069 }
1070 framing::record_frames(&paged_stream(&[&record])).expect("fixture framing is valid")
1071 }
1072
1073 let normal =
1074 super::decode_frames_for_edit(&archive_with_depth(1), &frames_for_schema("Schema0"))
1075 .expect("one schema fits the default policy");
1076 assert_eq!(normal.records.len(), 1, "{:?}", normal.rejected);
1077 assert!(matches!(
1078 super::decode_frames_for_edit(&archive_with_depth(257), &frames_for_schema("Schema256")),
1079 Err(CodecError::ResourceLimit(limit))
1080 if limit.dimension == ResourceDimension::RecursionDepth
1081 ));
1082 }
1083
1084 #[test]
1085 fn unused_schema_with_absent_base_does_not_reject_selected_record() {
1086 let mut record = Vec::new();
1087 for value in ["Good", "guid", "base", ""] {
1088 push_lp(&mut record, value);
1089 }
1090 let stream = paged_stream(&[&record]);
1091 let frames = framing::record_frames(&stream).expect("fixture framing is valid");
1092 let mut catalog = super::SchemaCatalog::new(std::collections::HashMap::from([
1093 ("Good".into(), super::Schema::default()),
1094 (
1095 "Unused".into(),
1096 super::Schema {
1097 base: Some("Absent".into()),
1098 ..super::Schema::default()
1099 },
1100 ),
1101 ]));
1102 let arena = DecodeArena::new();
1103 let (ctx, _) = DecodeContext::from_root_bytes(&stream, &arena, &DecodePolicy::service())
1104 .expect("stream fits service input limit");
1105 let outcome = super::decode_frames(&ctx, &mut catalog, &frames)
1106 .expect("unused schema is not resolved");
1107 assert_eq!(outcome.records.len(), 1);
1108 assert!(outcome.rejected.is_empty());
1109 }
1110
1111 #[test]
1112 fn duplicate_local_protein_properties_are_rejected() {
1113 let xml = br#"<Schema><UID val="Simple"/><Float id="value"/><String id="value"/></Schema>"#;
1114 with_service_context(xml, |ctx| {
1115 let error = super::parse_schema_document(
1116 ctx,
1117 "schema",
1118 xml,
1119 &mut std::collections::HashMap::new(),
1120 )
1121 .expect_err("local ids must be unique");
1122 assert!(
1123 matches!(error, CodecError::Malformed(message) if message == "Protein schema Simple declares property value more than once")
1124 );
1125 });
1126 }
1127
1128 #[test]
1129 fn schema_archive_size_ceiling_is_unsupported() {
1130 let mut bytes = schema_archive(&[(
1131 "Schemas/SimpleSchema.xml",
1132 "<Schema><UID val=\"Simple\"/></Schema>",
1133 )]);
1134 let size = u32::try_from(super::MAX_SCHEMA_BYTES + 1)
1135 .expect("local ceiling fits ZIP32")
1136 .to_le_bytes();
1137 let central = bytes
1138 .windows(4)
1139 .position(|bytes| bytes == b"PK\x01\x02")
1140 .expect("central header");
1141 bytes[central + 24..central + 28].copy_from_slice(&size);
1142 bytes[22..26].copy_from_slice(&size);
1143 let arena = DecodeArena::new();
1144 let (ctx, root) =
1145 DecodeContext::from_root_bytes(&bytes, &arena, &DecodePolicy::service()).expect("root");
1146 let error = super::SchemaCatalog::load(&ctx, root)
1147 .err()
1148 .expect("local size ceiling");
1149 assert!(matches!(error, CodecError::NotImplemented(_)), "{error:?}");
1150 }
1151
1152 #[test]
1153 fn large_protein_lists_past_the_value_cap_are_malformed() {
1154 let mut repeated = 1_025_u32.to_le_bytes().to_vec();
1155 repeated.extend(std::iter::repeat_n(0_u8, 1_025 * 4));
1156 let mut paths = vec![0_u8];
1157 paths.extend_from_slice(&repeated);
1158 let mut connections = vec![1_u8, 1];
1159 connections.extend_from_slice(&repeated);
1160 let over_cap = |error: CodecError| matches!(&error, CodecError::Malformed(message) if message.contains("at most 1024"));
1161 assert!(over_cap(
1162 super::read_property(
1163 &repeated,
1164 &mut 0,
1165 super::ValueLayout::Multiple(ValueCarrier::Integer),
1166 "values",
1167 )
1168 .expect_err("value cap")
1169 ));
1170 assert!(over_cap(
1171 read_texture_uri(&paths, &mut 0, "paths").expect_err("URI value cap")
1172 ));
1173 assert!(over_cap(
1174 read_connections(&connections, &mut 0).expect_err("connection value cap")
1175 ));
1176 }
1177
1178 #[test]
1179 fn a_record_past_the_value_cap_is_rejected_and_decoding_continues() {
1180 let mut catalog = super::SchemaCatalog {
1181 schemas: std::collections::HashMap::new(),
1182 properties: std::collections::HashMap::from([(
1183 "Simple".into(),
1184 std::collections::BTreeMap::from([(
1185 "values".into(),
1186 super::Property::Value {
1187 layout: super::ValueLayout::Multiple(ValueCarrier::Integer),
1188 connectable: false,
1189 },
1190 )]),
1191 )]),
1192 inherited_entries: 0,
1193 };
1194 let mut over = Vec::new();
1195 let mut within = Vec::new();
1196 for value in ["Simple", "guid", "base", ""] {
1197 push_lp(&mut over, value);
1198 push_lp(&mut within, value);
1199 }
1200 over.extend_from_slice(&1_025_u32.to_le_bytes());
1201 over.extend(std::iter::repeat_n(0_u8, 1_025 * 4));
1202 within.extend_from_slice(&1_u32.to_le_bytes());
1203 within.extend_from_slice(&7_u32.to_le_bytes());
1204 let stream = paged_stream(&[&over, &within]);
1205 let frames = framing::record_frames(&stream).expect("fixture framing is valid");
1206 let arena = DecodeArena::new();
1207 let (ctx, _) = DecodeContext::from_root_bytes(&stream, &arena, &DecodePolicy::service())
1208 .expect("build the decode context");
1209 let outcome = super::decode_frames(&ctx, &mut catalog, &frames)
1210 .expect("a capped record is rejected, not refused");
1211 assert_eq!(outcome.records.len(), 1);
1212 assert_eq!(outcome.rejected.len(), 1);
1213 assert_eq!(outcome.rejected[0].ordinal, 0);
1214 assert!(outcome.rejected[0].detail.contains("at most 1024"));
1215 }
1216
1217 #[test]
1218 fn absent_schema_rejects_the_record() {
1219 let mut record = Vec::new();
1220 for value in ["Absent", "guid", "base", ""] {
1221 push_lp(&mut record, value);
1222 }
1223 let stream = paged_stream(&[&record]);
1224 let frames = framing::record_frames(&stream).expect("fixture framing is valid");
1225 let mut catalog = super::SchemaCatalog::new(std::collections::HashMap::new());
1226 let arena = DecodeArena::new();
1227 let (ctx, _) = DecodeContext::from_root_bytes(&stream, &arena, &DecodePolicy::service())
1228 .expect("build the decode context");
1229 assert_eq!(
1230 super::decode_frames(&ctx, &mut catalog, &frames)
1231 .expect("an absent schema rejects only its record")
1232 .rejected
1233 .len(),
1234 1
1235 );
1236 }
1237
1238 #[test]
1239 fn inherited_property_cap_rejects_one_record_and_decoding_continues() {
1240 let wide = (0..=super::MAX_INHERITED_PROPERTIES / 2)
1241 .map(|index| {
1242 (
1243 format!("p{index}"),
1244 super::Property::Reference { multiple: false },
1245 )
1246 })
1247 .collect();
1248 let mut catalog = super::SchemaCatalog::new(std::collections::HashMap::from([
1249 (
1250 "Wide".into(),
1251 super::Schema {
1252 base: None,
1253 properties: wide,
1254 },
1255 ),
1256 (
1257 "Child".into(),
1258 super::Schema {
1259 base: Some("Wide".into()),
1260 ..super::Schema::default()
1261 },
1262 ),
1263 ("Small".into(), super::Schema::default()),
1264 ]));
1265 let mut child = Vec::new();
1266 let mut small = Vec::new();
1267 for value in ["Child", "guid", "base", ""] {
1268 push_lp(&mut child, value);
1269 }
1270 for value in ["Small", "guid", "base", ""] {
1271 push_lp(&mut small, value);
1272 }
1273 let stream = paged_stream(&[&child, &small, &child]);
1274 let frames = framing::record_frames(&stream).expect("fixture framing is valid");
1275 let arena = DecodeArena::new();
1276 let (ctx, _) = DecodeContext::from_root_bytes(&stream, &arena, &DecodePolicy::service())
1277 .expect("build the decode context");
1278 let outcome = super::decode_frames(&ctx, &mut catalog, &frames)
1279 .expect("a capped closure rejects its records, not the decode");
1280 assert_eq!(outcome.records.len(), 1);
1281 assert_eq!(outcome.records[0].schema, "Small");
1282 assert_eq!(
1283 outcome
1284 .rejected
1285 .iter()
1286 .map(|rejected| rejected.ordinal)
1287 .collect::<Vec<_>>(),
1288 [0, 2]
1289 );
1290 assert!(outcome.rejected.iter().all(|rejected| rejected
1291 .detail
1292 .contains("262144-entry inherited property cap")));
1293 assert!(catalog.properties.contains_key("Wide"));
1294 assert!(!catalog.properties.contains_key("Child"));
1295 assert_eq!(
1296 catalog.inherited_entries,
1297 super::MAX_INHERITED_PROPERTIES / 2 + 1
1298 );
1299 }
1300
1301 #[test]
1302 fn compatibility_selection_keeps_the_header_out_of_the_value_block() {
1303 assert!(!instance_property_serializes("AssetLibID"));
1304 assert!(!instance_property_serializes(
1305 "texture_MapChannel_ID_Advanced"
1306 ));
1307 assert!(instance_property_serializes("ExchangeGUID"));
1308 assert!(instance_property_serializes("swatch"));
1309 assert!(instance_property_serializes("interior_model"));
1310 assert!(instance_property_serializes("texture_MapChannel"));
1311 assert!(instance_property_serializes(
1312 "texture_MapChannel_UVWSource_Advanced"
1313 ));
1314 assert!(instance_property_serializes("common_Shared_Asset"));
1315 assert!(instance_property_serializes("common_Tint_color_colorspace"));
1316 }
1317
1318 #[test]
1319 fn color_carries_no_marker_byte_whether_or_not_it_is_connectable() {
1320 let rgba = [0.1_f64, 0.2, 0.3, 1.0];
1321 let bare = rgba
1322 .into_iter()
1323 .flat_map(f64::to_le_bytes)
1324 .collect::<Vec<_>>();
1325 for id in ["metal_f0", "common_Tint_color"] {
1326 let mut at = 0;
1327 assert_eq!(
1328 read_value(&bare, &mut at, ValueCarrier::Color, id).unwrap(),
1329 PropertyValue::Color(rgba.map(|value| FiniteReal::new(value).expect("finite")))
1330 );
1331 assert_eq!(at, bare.len());
1332 }
1333 }
1334
1335 #[test]
1336 fn connection_and_texture_uri_blocks_carry_a_kind_byte() {
1337 let mut connections = vec![1, 1];
1338 connections.extend_from_slice(&2u32.to_le_bytes());
1339 push_lp(&mut connections, "first-guid");
1340 push_lp(&mut connections, "second-guid");
1341 let mut at = 0;
1342 assert_eq!(
1343 read_connections(&connections, &mut at).unwrap(),
1344 ["first-guid", "second-guid"]
1345 );
1346 assert_eq!(at, connections.len());
1347
1348 let mut at = 0;
1349 assert!(read_connections(&[0], &mut at).unwrap().is_empty());
1350 assert_eq!(at, 1);
1351
1352 let mut counted = vec![0];
1353 counted.extend_from_slice(&1u32.to_le_bytes());
1354 push_lp(&mut counted, "cloud/resource/one");
1355 let mut at = 0;
1356 assert_eq!(
1357 read_texture_uri(&counted, &mut at, "unifiedbitmap_Bitmap").unwrap(),
1358 PropertyValue::TextureUri(vec!["cloud/resource/one".into()])
1359 );
1360 assert_eq!(at, counted.len());
1361
1362 let mut single = vec![1];
1363 push_lp(&mut single, "local_bitmap.png");
1364 let mut at = 0;
1365 assert_eq!(
1366 read_texture_uri(&single, &mut at, "unifiedbitmap_Bitmap").unwrap(),
1367 PropertyValue::TextureUri(vec!["local_bitmap.png".into()])
1368 );
1369 assert_eq!(at, single.len());
1370 }
1371
1372 #[test]
1373 fn multiple_references_keep_one_connection_block_including_zero_values() {
1374 let protein = schema_archive(&[(
1375 "Schemas/ReferencesSchema.xml",
1376 r#"<Schema><UID val="References"/><Reference id="targets" allowmultiplevalues="true"/></Schema>"#,
1377 )]);
1378 for count in [0_u32, 2] {
1379 let mut record = Vec::new();
1380 for value in ["References", "asset-guid", "Reference", ""] {
1381 push_lp(&mut record, value);
1382 }
1383 record.extend_from_slice(&count.to_le_bytes());
1384 push_connections(&mut record, &["target"]);
1385 let outcome = decode_fixture(&protein, &paged_stream(&[&record])).unwrap();
1386 assert!(outcome.rejected.is_empty(), "{:?}", outcome.rejected);
1387 let records = outcome.records;
1388 assert_eq!(records.len(), 1);
1389 assert_eq!(
1390 records[0].properties["targets"].value().is_none(),
1391 count != 0
1392 );
1393 assert_eq!(
1394 records[0].properties["targets"].content,
1395 match std::num::NonZeroUsize::new(cadmpeg_core::decode::index_from_u32(count)) {
1396 Some(count) => PropertyContent::MultipleReferences {
1397 count,
1398 targets: vec!["target".into()],
1399 },
1400 None => PropertyContent::Value {
1401 value: PropertyValue::Multiple(RepeatedValues::default()),
1402 connections: vec!["target".into()],
1403 },
1404 }
1405 );
1406 }
1407 }
1408
1409 #[test]
1410 fn schema_driven_record_uses_inheritance_and_serialized_property_ids() {
1411 let protein = schema_archive(&[
1412 (
1413 "Schemas/CommonSchema.xml",
1414 r#"<Schema>
1415 <UID val="CommonSchema"/>
1416 <String id="AssetLibID" val=""/>
1417 <Uuid id="ExchangeGUID" val=""/>
1418 <Color id="a_color" allowconnectedassets="single"/>
1419 <Boolean id="ignored_readonly" readonly="true"/>
1420 <Integer id="revision" public="false" val="1"/>
1421 </Schema>"#,
1422 ),
1423 (
1424 "Asset/Schemas/TextureSchema.xml",
1425 r#"<Schema>
1426 <UID val="TextureSchema"/>
1427 <Base val="CommonSchema"/>
1428 <PropertyAlias id="renamed_color" property="a_color"/>
1429 <Distance id="b_distance"/>
1430 <TextureURI id="c_uri" allowmultiplevalues="true"/>
1431 <Float id="d_unit_float" unit="unitless"/>
1432 <Reference id="e_reference"/>
1433 <Float id="f_profile" allowmultiplevalues="true"/>
1434 <String id="swatch" public="false" val=""/>
1435 <Integer id="ignored_definition" definitionIteratorData="true"/>
1436 <String id="metadata_still_serializes" metadata="true"/>
1437 </Schema>"#,
1438 ),
1439 ]);
1440 let mut values = Vec::new();
1441 push_lp(&mut values, ""); for value in [0.1_f64, 0.2, 0.3, 1.0] {
1443 values.extend_from_slice(&value.to_le_bytes()); }
1445 push_connections(&mut values, &["first-guid", "second-guid"]);
1446 values.extend_from_slice(&0x2016_u32.to_le_bytes());
1447 values.extend_from_slice(&2.5_f64.to_le_bytes()); values.push(0);
1449 values.extend_from_slice(&2u32.to_le_bytes());
1450 push_lp(&mut values, "cloud/resource/one");
1451 push_lp(&mut values, "cloud/resource/two"); values.extend_from_slice(&0x200e_u32.to_le_bytes());
1453 values.extend_from_slice(&4.5_f64.to_le_bytes()); push_connections(&mut values, &["reference-guid"]); values.extend_from_slice(&2u32.to_le_bytes());
1456 values.extend_from_slice(&0.25_f64.to_le_bytes());
1457 values.extend_from_slice(&0.75_f64.to_le_bytes()); push_lp(&mut values, "Comments"); values.extend_from_slice(&1u32.to_le_bytes()); push_lp(&mut values, "Swatch-Torus"); let mut record = Vec::new();
1463 for value in ["TextureSchema", "asset-guid", "Texture", ""] {
1464 push_lp(&mut record, value);
1465 }
1466 record.extend_from_slice(&values);
1467
1468 let outcome =
1469 decode_fixture(&protein, &paged_stream(&[&record])).expect("schema record decodes");
1470 assert!(outcome.rejected.is_empty());
1471 let records = outcome.records;
1472 assert_eq!(records.len(), 1);
1473 let properties = &records[0].properties;
1474 assert_eq!(
1475 properties["a_color"].value().unwrap().clone(),
1476 PropertyValue::Color(
1477 [0.1, 0.2, 0.3, 1.0].map(|value| FiniteReal::new(value).expect("finite"))
1478 )
1479 );
1480 assert_eq!(
1481 properties["a_color"].connections(),
1482 ["first-guid", "second-guid"]
1483 );
1484 assert!(properties["a_color"].value_offset > RECORD_MARKER.len());
1485 assert_eq!(
1486 properties["b_distance"].value().unwrap().clone(),
1487 PropertyValue::Distance {
1488 unit: 0x2016,
1489 value: FiniteReal::new(2.5).expect("finite"),
1490 }
1491 );
1492 assert_eq!(
1493 properties["c_uri"].value().unwrap().clone(),
1494 PropertyValue::TextureUri(vec![
1495 "cloud/resource/one".into(),
1496 "cloud/resource/two".into(),
1497 ])
1498 );
1499 assert_eq!(
1500 properties["d_unit_float"].value().unwrap().clone(),
1501 PropertyValue::UnitFloat {
1502 unit: 0x200e,
1503 value: FiniteReal::new(4.5).expect("finite")
1504 }
1505 );
1506 assert_eq!(
1507 properties["e_reference"].connections(),
1508 vec!["reference-guid"]
1509 );
1510 assert_eq!(
1511 properties["f_profile"].value().unwrap().clone(),
1512 PropertyValue::Multiple(
1513 vec![
1514 PropertyValue::Float(FiniteReal::new(0.25).expect("finite")),
1515 PropertyValue::Float(FiniteReal::new(0.75).expect("finite"))
1516 ]
1517 .try_into()
1518 .expect("same carrier")
1519 )
1520 );
1521 assert_eq!(
1522 properties["metadata_still_serializes"]
1523 .value()
1524 .unwrap()
1525 .clone(),
1526 PropertyValue::String("Comments".into())
1527 );
1528 assert_eq!(
1530 properties["revision"].value().unwrap().clone(),
1531 PropertyValue::Integer(1)
1532 );
1533 assert_eq!(
1534 properties["swatch"].value().unwrap().clone(),
1535 PropertyValue::String("Swatch-Torus".into())
1536 );
1537 assert_eq!(
1538 properties["ExchangeGUID"].value().unwrap().clone(),
1539 PropertyValue::String(String::new())
1540 );
1541 assert!(!properties.contains_key("AssetLibID"));
1543 assert!(!properties.contains_key("renamed_color"));
1544 assert!(!properties.contains_key("ignored_readonly"));
1545 assert!(!properties.contains_key("ignored_definition"));
1546 }
1547
1548 #[test]
1549 fn detailed_decode_accounts_for_a_rejected_record_and_continues() {
1550 let protein = schema_archive(&[(
1551 "Schemas/SimpleSchema.xml",
1552 r#"<Schema><UID val="SimpleSchema"/><String id="comment"/></Schema>"#,
1553 )]);
1554 let record = |guid: &str| {
1555 let mut bytes = Vec::new();
1556 for value in ["SimpleSchema", guid, "Simple", ""] {
1557 push_lp(&mut bytes, value);
1558 }
1559 push_lp(&mut bytes, &"x".repeat(160));
1560 bytes
1561 };
1562 let first = record("first-guid");
1563 let malformed = vec![0xff; 160];
1564 let third = record("third-guid");
1565 let instance = paged_stream(&[&first, &malformed, &third]);
1566
1567 let outcome = decode_fixture(&protein, &instance).expect("framed records decode");
1568 assert_eq!(
1569 outcome
1570 .records
1571 .iter()
1572 .map(|record| (record.ordinal, record.guid.as_str()))
1573 .collect::<Vec<_>>(),
1574 [(0, "first-guid"), (2, "third-guid")]
1575 );
1576 assert_eq!(outcome.rejected.len(), 1);
1577 assert_eq!(outcome.rejected[0].ordinal, 1);
1578 assert!(!outcome.rejected[0].detail.is_empty());
1579 }
1580
1581 #[test]
1582 fn detailed_decode_rejects_invalid_page_framing() {
1583 let protein = schema_archive(&[(
1584 "Schemas/SimpleSchema.xml",
1585 r#"<Schema><UID val="SimpleSchema"/><String id="comment"/></Schema>"#,
1586 )]);
1587 let error = decode_fixture(&protein, &[0; 16])
1588 .expect_err("a header without any complete page is malformed");
1589 assert!(matches!(error, CodecError::Malformed(message)
1590 if message == "Protein page stream is shorter than its header and one page"));
1591 }
1592
1593 #[test]
1594 fn compatibility_texture_decode_selects_advanced_id_omission() {
1595 let protein = schema_archive(&[(
1596 "Schemas/BitmapSchema.xml",
1597 r#"<Schema>
1598 <UID val="BitmapSchema"/>
1599 <String id="AssetLibID" val=""/>
1600 <Integer id="texture_MapChannel" val="1"/>
1601 <Integer id="texture_MapChannel_ID_Advanced" val="1"/>
1602 <Integer id="texture_MapChannel_UVWSource_Advanced" val="0"/>
1603 <String id="swatch" public="false" val=""/>
1604 </Schema>"#,
1605 )]);
1606 let mut record = Vec::new();
1607 let padded_name = "Bitmap".repeat(32);
1608 for value in ["BitmapSchema", "asset-guid", &padded_name, ""] {
1609 push_lp(&mut record, value);
1610 }
1611 push_lp(&mut record, ""); record.extend_from_slice(&1u32.to_le_bytes()); record.extend_from_slice(&0u32.to_le_bytes()); let outcome =
1615 decode_fixture(&protein, &paged_stream(&[&record])).expect("texture record decodes");
1616 assert!(outcome.rejected.is_empty());
1617 let records = outcome.records;
1618 assert_eq!(records.len(), 1);
1619 let properties = &records[0].properties;
1620 assert!(!properties.contains_key("texture_MapChannel_ID_Advanced"));
1621 assert_eq!(
1622 properties["texture_MapChannel"].value().unwrap().clone(),
1623 PropertyValue::Integer(1)
1624 );
1625 assert_eq!(
1626 properties["texture_MapChannel_UVWSource_Advanced"]
1627 .value()
1628 .unwrap()
1629 .clone(),
1630 PropertyValue::Integer(0)
1631 );
1632 assert_eq!(
1633 properties["swatch"].value().unwrap().clone(),
1634 PropertyValue::String(String::new())
1635 );
1636 }
1637
1638 #[test]
1639 fn four_texture_omission_branches_agree_on_the_later_properties() {
1640 let protein = schema_archive(&[(
1641 "Schemas/BitmapSchema.xml",
1642 r#"<Schema><UID val="BitmapSchema"/>
1643 <String id="AssetLibID" val=""/>
1644 <String id="swatch" public="false" val=""/>
1645 <Boolean id="texture_LinkTextureTransforms" val="false"/>
1646 <Integer id="texture_MapChannel" val="1"/>
1647 <Integer id="texture_MapChannel_ID_Advanced" val="1"/>
1648 <Integer id="texture_MapChannel_UVWSource_Advanced" val="0"/>
1649 <Boolean id="texture_OffsetLock" val="false"/>
1650 <Float id="texture_UOffset" val="0"/>
1651 </Schema>"#,
1652 )]);
1653 let mut record = RECORD_MARKER.to_vec();
1654 for value in ["BitmapSchema", "texture-guid", "Bitmap", "", ""] {
1655 push_lp(&mut record, value);
1656 }
1657 record.push(0);
1658 record.extend_from_slice(&1_u32.to_le_bytes());
1659 record.extend_from_slice(&0_u32.to_le_bytes());
1660 record.push(1);
1661 record.extend_from_slice(&0.375_f64.to_le_bytes());
1662 with_service_context(&protein, |ctx| {
1663 let mut catalog = super::SchemaCatalog::load_for_edit(&protein).unwrap();
1664 for omitted in [
1665 "swatch",
1666 "texture_MapChannel",
1667 "texture_MapChannel_ID_Advanced",
1668 "texture_MapChannel_UVWSource_Advanced",
1669 ] {
1670 let decoded =
1671 super::decode_record_with_selection(ctx, &record, &mut catalog, 0, 0, |id| {
1672 id != "AssetLibID" && id != omitted
1673 })
1674 .expect("exact branch")
1675 .expect("typed record");
1676 let properties = decoded.properties;
1677 assert!(!properties.contains_key(omitted));
1678 assert_eq!(
1679 properties["texture_OffsetLock"].value_offset,
1680 record.len() - 9
1681 );
1682 assert_eq!(
1683 properties["texture_OffsetLock"].value(),
1684 Some(&PropertyValue::Boolean(true))
1685 );
1686 assert_eq!(
1687 properties["texture_UOffset"].value(),
1688 Some(&PropertyValue::Float(FiniteReal::new(0.375).unwrap()))
1689 );
1690 if omitted == "swatch" {
1691 assert_eq!(
1692 properties["texture_MapChannel"].value(),
1693 Some(&PropertyValue::Integer(0))
1694 );
1695 }
1696 if omitted == "texture_MapChannel_UVWSource_Advanced" {
1697 assert_eq!(
1698 properties["texture_MapChannel_ID_Advanced"].value(),
1699 Some(&PropertyValue::Integer(0))
1700 );
1701 }
1702 }
1703 });
1704 }
1705
1706 #[test]
1707 fn a_record_spanning_several_pages_ends_at_its_terminal_page() {
1708 let long = "x".repeat(400);
1709 let mut first = Vec::new();
1710 for value in ["S", "guid-one", &long, ""] {
1711 push_lp(&mut first, value);
1712 }
1713 let short = "y".repeat(140);
1714 let mut second = Vec::new();
1715 for value in ["S", "guid-two", &short, ""] {
1716 push_lp(&mut second, value);
1717 }
1718 let stream = paged_stream(&[&first, &second]);
1719 let frames = framing::record_frames(&stream).expect("stream is paged");
1720 assert_eq!(frames.len(), 2);
1721 assert_eq!(frames[0].logical_offset(), 0);
1722 assert_eq!(frames[0].bytes(), [RECORD_MARKER, &first].concat());
1723 assert_eq!(frames[1].logical_offset(), frames[0].bytes().len());
1724 assert_eq!(frames[1].bytes(), [RECORD_MARKER, &second].concat());
1725 assert!(stream.len() > 16 + 3 * PAGE_SIZE, "record one spans pages");
1726
1727 assert!(framing::record_frames(&[]).is_err());
1728 let mut truncated = stream.clone();
1729 truncated.truncate(16 + PAGE_SIZE + 1);
1730 assert!(framing::record_frames(&truncated).is_err());
1731 }
1732
1733 #[test]
1734 fn standalone_terminal_page_carries_one_short_record() {
1735 let mut record = Vec::new();
1736 for value in ["S", "guid", "base", "library"] {
1737 push_lp(&mut record, value);
1738 }
1739 let mut stream = u32::try_from(PAGE_SIZE)
1740 .expect("test page size fits u32")
1741 .to_le_bytes()
1742 .to_vec();
1743 stream.resize(STREAM_HEADER_LEN, 0);
1744 stream.extend_from_slice(TERMINAL_MARKER);
1745 stream.extend_from_slice(
1746 &u16::try_from(record.len())
1747 .expect("test record fits u16")
1748 .to_le_bytes(),
1749 );
1750 stream.extend_from_slice(&[1, 0]);
1751 stream.extend_from_slice(&record);
1752 stream.resize(STREAM_HEADER_LEN + PAGE_SIZE, 0);
1753
1754 let frames = framing::record_frames(&stream).expect("standalone terminal page");
1755 assert_eq!(frames.len(), 1);
1756 assert_eq!(frames[0].logical_offset(), 0);
1757 assert_eq!(frames[0].bytes(), [RECORD_MARKER, &record].concat());
1758 }
1759
1760 fn paged_stream(records: &[&[u8]]) -> Vec<u8> {
1763 const BODY: usize = PAGE_SIZE - 8;
1764 let mut out = u32::try_from(PAGE_SIZE)
1765 .expect("test page size fits u32")
1766 .to_le_bytes()
1767 .to_vec();
1768 out.resize(STREAM_HEADER_LEN, 0);
1769 let mut page = |header: [u8; 8], body: &[u8]| {
1770 out.extend_from_slice(&header);
1771 out.extend_from_slice(body);
1772 out.resize(out.len() + BODY - body.len(), 0);
1773 };
1774 let opening = |marker: &[u8]| {
1775 let mut header = [0_u8; 8];
1776 header[4..8].copy_from_slice(marker);
1777 header
1778 };
1779 for record in records {
1780 if record.len() < BODY {
1783 let mut header = [0_u8; 8];
1784 header[..4].copy_from_slice(TERMINAL_MARKER);
1785 header[4..6].copy_from_slice(
1786 &u16::try_from(record.len())
1787 .expect("test record fits u16")
1788 .to_le_bytes(),
1789 );
1790 page(header, record);
1791 continue;
1792 }
1793 let (head, rest) = record.split_at(BODY);
1794 page(opening(RECORD_MARKER), head);
1795 let mut chunks = rest.chunks(BODY).peekable();
1796 while let Some(chunk) = chunks.next() {
1797 if chunks.peek().is_some() {
1798 page(opening(CONTINUATION_MARKER), chunk);
1799 } else {
1800 let mut header = [0_u8; 8];
1801 header[0..4].copy_from_slice(TERMINAL_MARKER);
1802 header[4..6].copy_from_slice(
1803 &u16::try_from(chunk.len())
1804 .expect("test chunk fits u16")
1805 .to_le_bytes(),
1806 );
1807 page(header, chunk);
1808 }
1809 }
1810 }
1811 out
1812 }
1813
1814 fn schema_archive(entries: &[(&str, &str)]) -> Vec<u8> {
1815 let options = zip::write::SimpleFileOptions::default()
1816 .compression_method(zip::CompressionMethod::Stored)
1817 .system(zip::System::Unix);
1818 let mut archive = zip::ZipWriter::new(Cursor::new(Vec::new()));
1819 for (name, xml) in entries {
1820 archive.start_file(name, options).expect("start schema");
1821 archive.write_all(xml.as_bytes()).expect("write schema");
1822 }
1823 archive.finish().expect("finish schemas").into_inner()
1824 }
1825
1826 fn mark_entry_method_unsupported(archive: &mut [u8], name: &str) {
1829 const UNSUPPORTED_METHOD: [u8; 2] = 0xfffe_u16.to_le_bytes();
1830 let mut patched = 0;
1831 for (signature, method_at, name_len_at, name_at) in
1832 [(b"PK\x03\x04", 8, 26, 30), (b"PK\x01\x02", 10, 28, 46)]
1833 {
1834 let mut offset = 0;
1835 while offset + name_at <= archive.len() {
1836 if &archive[offset..offset + 4] == signature {
1837 let name_len = usize::from(u16::from_le_bytes([
1838 archive[offset + name_len_at],
1839 archive[offset + name_len_at + 1],
1840 ]));
1841 let name_range = offset + name_at..offset + name_at + name_len;
1842 if archive.get(name_range) == Some(name.as_bytes()) {
1843 archive[offset + method_at..offset + method_at + 2]
1844 .copy_from_slice(&UNSUPPORTED_METHOD);
1845 patched += 1;
1846 }
1847 }
1848 offset += 1;
1849 }
1850 }
1851 assert_eq!(patched, 2, "one local header and one central record");
1852 }
1853
1854 #[test]
1858 fn an_undecodable_non_schema_member_does_not_hide_the_schemas() {
1859 const SCHEMA: &str = r#"<Schema><UID val="Simple"/><String id="comment"/></Schema>"#;
1860 let mut protein = schema_archive(&[
1861 ("payload.bin", "opaque"),
1862 ("Schemas/SimpleSchema.xml", SCHEMA),
1863 ]);
1864 mark_entry_method_unsupported(&mut protein, "payload.bin");
1865
1866 let mut archive = zip::ZipArchive::new(Cursor::new(protein.as_slice())).expect("archive");
1868 assert!(archive.by_name("payload.bin").is_err());
1869 drop(archive);
1870
1871 assert!(super::has_schemas(&protein));
1872 let arena = DecodeArena::new();
1873 let (ctx, root) =
1874 DecodeContext::from_root_bytes(&protein, &arena, &DecodePolicy::service())
1875 .expect("ZIP fits service input limit");
1876 assert!(super::SchemaCatalog::load(&ctx, root)
1878 .expect("the schema entry is readable")
1879 .is_some());
1880 let catalog = super::SchemaCatalog::load_for_edit(&protein)
1882 .expect("the schema entry is readable for edit");
1883 assert!(catalog.schemas.contains_key("Simple"));
1884
1885 let mut only = schema_archive(&[("Schemas/SimpleSchema.xml", SCHEMA)]);
1888 mark_entry_method_unsupported(&mut only, "Schemas/SimpleSchema.xml");
1889 assert!(super::has_schemas(&only));
1890 }
1891
1892 fn push_lp(bytes: &mut Vec<u8>, value: &str) {
1893 bytes.extend_from_slice(
1894 &u32::try_from(value.len())
1895 .expect("test value fits u32")
1896 .to_le_bytes(),
1897 );
1898 bytes.extend_from_slice(value.as_bytes());
1899 }
1900
1901 fn push_connections(bytes: &mut Vec<u8>, values: &[&str]) {
1902 bytes.extend_from_slice(&[1, 1]);
1903 bytes.extend_from_slice(
1904 &u32::try_from(values.len())
1905 .expect("test count fits u32")
1906 .to_le_bytes(),
1907 );
1908 for value in values {
1909 push_lp(bytes, value);
1910 }
1911 }
1912}