1use crate::{
326 Cardinality, EnumType, Error, Field, FieldPresence, FieldType, MessageDescriptor,
327 MessageEncoding, Result, Schema, Utf8Validation,
328 constants::{
329 CANONICAL_F32_NAN_BITS, CANONICAL_F64_NAN_BITS, DEFAULT_RECURSION_LIMIT,
330 FIELD_NUMBER_SHIFT, FIXED32_SIZE, FIXED64_SIZE, HARDENED_MAX_AUDIT_BYTES,
331 HARDENED_MAX_FIELD_OCCURRENCES, HARDENED_MAX_LENGTH_DELIMITED_BYTES,
332 HARDENED_MAX_MAP_ENTRIES, HARDENED_MAX_MESSAGE_BYTES, HARDENED_MAX_REPEATED_VALUES,
333 I32_SIGN_SHIFT, I64_SIGN_SHIFT, MAP_KEY_FIELD_NUMBER, MAP_VALUE_FIELD_NUMBER,
334 MAX_FIELD_KEY_BYTES, MAX_FIELD_NUMBER, MAX_TENTH_VARINT_BYTE, MAX_VARINT_BYTES,
335 MIN_FIELD_NUMBER, VARINT_BITS_PER_BYTE, VARINT_CONTINUATION_BIT, VARINT_DATA_MASK,
336 WIRE_TYPE_END_GROUP, WIRE_TYPE_FIXED32, WIRE_TYPE_FIXED64, WIRE_TYPE_LENGTH_DELIMITED,
337 WIRE_TYPE_MASK, WIRE_TYPE_START_GROUP, WIRE_TYPE_VARINT, is_supported_wire_type, make_key,
338 },
339};
340use alloc::{
341 collections::{BTreeMap, BTreeSet},
342 string::{String, ToString},
343 vec::Vec,
344};
345use core::{cmp::Ordering, ops::Range};
346#[derive(Clone, Debug, PartialEq)]
348pub enum Value {
349 Double(f64),
351 Float(f32),
353 Int32(i32),
355 Int64(i64),
357 Uint32(u32),
359 Uint64(u64),
361 Bool(bool),
363 String(String),
365 RawString(Vec<u8>),
367 Bytes(Vec<u8>),
369 Enum(i32),
371 Message(Message),
373 Repeated(Vec<Value>),
375 Map(Vec<(Value, Value)>),
377}
378#[derive(Clone, Debug, PartialEq)]
380pub struct UnknownField {
381 pub number: u32,
383 pub wire_type: u8,
385 pub encoded_value: Vec<u8>,
387}
388#[derive(Clone, Debug, PartialEq)]
394pub struct AddedField {
395 pub name: String,
397 pub number: u32,
399 pub wire_type: u8,
401 pub encoded_value: Vec<u8>,
403}
404
405#[derive(Clone, Copy, Debug, Eq, PartialEq)]
407pub enum AuditTag {
408 SchemaField,
410 UnknownField,
412 UnknownMessage,
414 AddedField,
416 DuplicateDiscarded,
418 DuplicateLastWins,
420 DuplicateMerged,
422}
423
424#[derive(Clone, Copy, Debug, Eq, PartialEq)]
426pub enum UnknownFieldPolicy {
427 Preserve,
429 Drop,
431 Reject,
433}
434
435#[derive(Clone, Copy, Debug, Eq, PartialEq)]
437pub enum DuplicateInputPolicy {
438 Allow,
440 Reject,
442}
443
444#[derive(Clone, Copy, Debug, Eq, PartialEq)]
446pub enum AuditMode {
447 Full,
449 MetadataOnly,
451}
452
453#[derive(Clone, Copy, Debug, Eq, PartialEq)]
455pub enum BooleanValuePolicy {
456 CoerceNonzero,
458 RejectNonCanonical,
460}
461
462#[derive(Clone, Copy, Debug, Eq, PartialEq)]
464pub enum EnumValuePolicy {
465 Preserve,
467 RejectUnknown,
469}
470
471#[derive(Clone, Copy, Debug, Eq, PartialEq)]
473pub struct DecodeOptions {
474 pub max_message_bytes: usize,
476 pub max_recursion_depth: usize,
478 pub max_field_occurrences: usize,
480 pub max_length_delimited_bytes: usize,
482 pub max_repeated_values: usize,
484 pub max_map_entries: usize,
486 pub max_audit_bytes: usize,
488 pub unknown_fields: UnknownFieldPolicy,
490 pub duplicates: DuplicateInputPolicy,
492 pub audit_mode: AuditMode,
494 pub require_minimal_varints: bool,
496 pub booleans: BooleanValuePolicy,
498 pub enum_values: EnumValuePolicy,
500}
501
502impl Default for DecodeOptions {
503 fn default() -> Self {
505 Self {
506 max_message_bytes: usize::MAX,
507 max_recursion_depth: DEFAULT_RECURSION_LIMIT,
508 max_field_occurrences: usize::MAX,
509 max_length_delimited_bytes: usize::MAX,
510 max_repeated_values: usize::MAX,
511 max_map_entries: usize::MAX,
512 max_audit_bytes: usize::MAX,
513 unknown_fields: UnknownFieldPolicy::Preserve,
514 duplicates: DuplicateInputPolicy::Allow,
515 audit_mode: AuditMode::Full,
516 require_minimal_varints: false,
517 booleans: BooleanValuePolicy::CoerceNonzero,
518 enum_values: EnumValuePolicy::Preserve,
519 }
520 }
521}
522
523impl DecodeOptions {
524 pub const fn hardened() -> Self {
526 Self {
527 max_message_bytes: HARDENED_MAX_MESSAGE_BYTES,
528 max_recursion_depth: DEFAULT_RECURSION_LIMIT,
529 max_field_occurrences: HARDENED_MAX_FIELD_OCCURRENCES,
530 max_length_delimited_bytes: HARDENED_MAX_LENGTH_DELIMITED_BYTES,
531 max_repeated_values: HARDENED_MAX_REPEATED_VALUES,
532 max_map_entries: HARDENED_MAX_MAP_ENTRIES,
533 max_audit_bytes: HARDENED_MAX_AUDIT_BYTES,
534 unknown_fields: UnknownFieldPolicy::Reject,
535 duplicates: DuplicateInputPolicy::Reject,
536 audit_mode: AuditMode::MetadataOnly,
537 require_minimal_varints: true,
538 booleans: BooleanValuePolicy::RejectNonCanonical,
539 enum_values: EnumValuePolicy::RejectUnknown,
540 }
541 }
542}
543
544#[derive(Clone, Debug, Eq, PartialEq)]
546pub struct AuditRecord {
547 pub tag: AuditTag,
549 pub field_name: Option<String>,
551 pub field_number: u32,
553 pub wire_type: u8,
555 pub encoded_field: Vec<u8>,
557}
558
559#[derive(Clone, Copy, Debug, Eq, PartialEq)]
561pub enum DuplicatePolicy {
562 LastOnly,
564 PreserveAll,
566}
567
568#[derive(Clone, Copy, Debug, Eq, PartialEq)]
570pub enum FieldOrder {
571 Declaration,
573 FieldNumber,
578}
579
580#[derive(Clone, Copy, Debug, Eq, PartialEq)]
582pub enum MapOrder {
583 Preserve,
585 Key,
587}
588
589#[derive(Clone, Copy, Debug, Eq, PartialEq)]
591pub enum FloatEncoding {
592 Preserve,
594 Normalize,
596}
597
598#[derive(Clone, Copy, Debug, Eq, PartialEq)]
600pub struct EncodeOptions {
601 pub forward_unknown_fields: bool,
603 pub forward_unknown_messages: bool,
605 pub forward_added_fields: bool,
607 pub duplicates: DuplicatePolicy,
609 pub field_order: FieldOrder,
611 pub map_order: MapOrder,
613 pub floats: FloatEncoding,
615 pub max_output_bytes: usize,
617}
618
619impl Default for EncodeOptions {
620 fn default() -> Self {
623 Self {
624 forward_unknown_fields: true,
625 forward_unknown_messages: true,
626 forward_added_fields: true,
627 duplicates: DuplicatePolicy::LastOnly,
628 field_order: FieldOrder::Declaration,
629 map_order: MapOrder::Preserve,
630 floats: FloatEncoding::Preserve,
631 max_output_bytes: usize::MAX,
632 }
633 }
634}
635
636#[derive(Clone, Debug, PartialEq)]
638pub struct EncodeOutput {
639 pub bytes: Vec<u8>,
641 pub audit: Vec<AuditRecord>,
643}
644
645#[derive(Clone, Debug, Default)]
647pub struct Message {
648 pub fields: BTreeMap<String, Value>,
650 pub unknown_fields: Vec<UnknownField>,
652 pub added_fields: Vec<AddedField>,
654 pub audit: Vec<AuditRecord>,
656}
657impl PartialEq for Message {
658 fn eq(&self, other: &Self) -> bool {
660 self.fields == other.fields
661 && self.unknown_fields == other.unknown_fields
662 && self.added_fields == other.added_fields
663 }
664}
665impl Message {
666 pub fn new() -> Self {
668 Self::default()
669 }
670 pub fn insert(&mut self, n: impl Into<String>, v: Value) -> Option<Value> {
674 self.fields.insert(n.into(), v)
675 }
676 pub fn get(&self, n: &str) -> Option<&Value> {
678 self.fields.get(n)
679 }
680
681 pub fn add_field(&mut self, field: AddedField) {
683 let mut encoded_field = Vec::new();
684 vi(make_key(field.number, field.wire_type), &mut encoded_field);
685 encoded_field.extend_from_slice(&field.encoded_value);
686 self.audit.push(AuditRecord {
687 tag: AuditTag::AddedField,
688 field_name: Some(field.name.clone()),
689 field_number: field.number,
690 wire_type: field.wire_type,
691 encoded_field,
692 });
693 self.added_fields.push(field);
694 }
695}
696fn vi(mut n: u64, o: &mut Vec<u8>) {
698 while n > u64::from(VARINT_DATA_MASK) {
699 o.push(n as u8 | VARINT_CONTINUATION_BIT);
700 n >>= VARINT_BITS_PER_BYTE
701 }
702 o.push(n as u8)
703}
704fn rv(b: &[u8], p: &mut usize) -> Result<u64> {
709 read_varint(b, p, false)
710}
711
712fn minimal_varint_length(mut value: u64) -> usize {
714 let mut length = 1;
715 while value > u64::from(VARINT_DATA_MASK) {
716 value >>= VARINT_BITS_PER_BYTE;
717 length += 1;
718 }
719 length
720}
721
722fn read_varint(b: &[u8], p: &mut usize, require_minimal: bool) -> Result<u64> {
724 let s = *p;
725 let mut n = 0;
726 for byte_index in 0..MAX_VARINT_BYTES {
727 let shift = byte_index * VARINT_BITS_PER_BYTE;
728 let x = *b.get(*p).ok_or_else(|| Error::new(s, "truncated varint"))?;
729 *p += 1;
730 if byte_index + 1 == MAX_VARINT_BYTES && x > MAX_TENTH_VARINT_BYTE {
731 return Err(Error::new(s, "varint overflow"));
732 }
733 n |= u64::from(x & VARINT_DATA_MASK) << shift;
734 if x & VARINT_CONTINUATION_BIT == 0 {
735 let encoded_length = p
736 .checked_sub(s)
737 .ok_or_else(|| Error::new(s, "varint cursor moved backwards"))?;
738 if require_minimal && encoded_length != minimal_varint_length(n) {
739 return Err(Error::new(s, "non-minimal varint encoding"));
740 }
741 return Ok(n);
742 }
743 }
744 Err(Error::new(s, "varint overflow"))
745}
746fn read_key(b: &[u8], p: &mut usize) -> Result<(u32, u8)> {
750 read_key_with_policy(b, p, false)
751}
752
753fn read_key_with_policy(b: &[u8], p: &mut usize, require_minimal: bool) -> Result<(u32, u8)> {
755 let start = *p;
756 let key = read_varint(b, p, require_minimal)?;
757 let encoded_size = p
758 .checked_sub(start)
759 .ok_or_else(|| Error::new(start, "field cursor moved backwards"))?;
760 if encoded_size > MAX_FIELD_KEY_BYTES || key > u64::from(u32::MAX) {
761 return Err(Error::new(start, "field tag exceeds 32 bits"));
762 }
763 let number = (key >> FIELD_NUMBER_SHIFT) as u32;
764 if !(MIN_FIELD_NUMBER..=MAX_FIELD_NUMBER).contains(&number) {
765 return Err(Error::new(start, "invalid field number in tag"));
766 }
767 Ok((number, (key & WIRE_TYPE_MASK) as u8))
768}
769fn wire(t: &FieldType) -> u8 {
771 match t {
772 FieldType::Double | FieldType::Fixed64 | FieldType::Sfixed64 => WIRE_TYPE_FIXED64,
773 FieldType::String | FieldType::Bytes | FieldType::Message(_) | FieldType::Map(..) => {
774 WIRE_TYPE_LENGTH_DELIMITED
775 }
776 FieldType::Float | FieldType::Fixed32 | FieldType::Sfixed32 => WIRE_TYPE_FIXED32,
777 _ => WIRE_TYPE_VARINT,
778 }
779}
780fn scalar(
785 t: &FieldType,
786 v: &Value,
787 s: &Schema,
788 options: &EncodeOptions,
789 o: &mut Vec<u8>,
790) -> Result<()> {
791 match (t, v) {
792 (FieldType::Double, Value::Double(x)) => {
793 let value = normalized_f64(*x, options.floats);
794 o.extend(value.to_le_bytes());
795 }
796 (FieldType::Float, Value::Float(x)) => {
797 let value = normalized_f32(*x, options.floats);
798 o.extend(value.to_le_bytes());
799 }
800 (FieldType::Int32, Value::Int32(x)) => vi(*x as i64 as u64, o),
801 (FieldType::Int64, Value::Int64(x)) => vi(*x as u64, o),
802 (FieldType::Uint32, Value::Uint32(x)) => vi(*x as u64, o),
803 (FieldType::Uint64, Value::Uint64(x)) => vi(*x, o),
804 (FieldType::Sint32, Value::Int32(x)) => {
805 vi(((*x << 1) ^ (*x >> I32_SIGN_SHIFT)) as u32 as u64, o)
806 }
807 (FieldType::Sint64, Value::Int64(x)) => vi(((*x << 1) ^ (*x >> I64_SIGN_SHIFT)) as u64, o),
808 (FieldType::Fixed32, Value::Uint32(x)) => o.extend(x.to_le_bytes()),
809 (FieldType::Fixed64, Value::Uint64(x)) => o.extend(x.to_le_bytes()),
810 (FieldType::Sfixed32, Value::Int32(x)) => o.extend(x.to_le_bytes()),
811 (FieldType::Sfixed64, Value::Int64(x)) => o.extend(x.to_le_bytes()),
812 (FieldType::Bool, Value::Bool(x)) => vi(*x as u64, o),
813 (FieldType::Enum(name), Value::Enum(x)) => {
814 if s.enums.get(name).is_some_and(|enumeration| {
815 enumeration.features.enum_type == EnumType::Closed
816 && !enumeration
817 .values
818 .iter()
819 .any(|candidate| candidate.number == *x)
820 }) {
821 return Err(Error::new(0, "unknown value for closed enum"));
822 }
823 vi(*x as i64 as u64, o);
824 }
825 (FieldType::String, Value::String(x)) => {
826 ensure_output_growth(
827 o.len(),
828 minimal_varint_length(x.len() as u64),
829 x.len(),
830 options.max_output_bytes,
831 )?;
832 vi(x.len() as u64, o);
833 o.extend(x.as_bytes())
834 }
835 (FieldType::Bytes, Value::Bytes(x)) => {
836 ensure_output_growth(
837 o.len(),
838 minimal_varint_length(x.len() as u64),
839 x.len(),
840 options.max_output_bytes,
841 )?;
842 vi(x.len() as u64, o);
843 o.extend(x)
844 }
845 (FieldType::Message(n), Value::Message(x)) => {
846 let d = s
847 .message(n)
848 .ok_or_else(|| Error::new(0, "unknown message type"))?;
849 let b = encode_inner(s, d, x, options)?.bytes;
850 ensure_output_growth(
851 o.len(),
852 minimal_varint_length(b.len() as u64),
853 b.len(),
854 options.max_output_bytes,
855 )?;
856 vi(b.len() as u64, o);
857 o.extend(b)
858 }
859 _ => return Err(Error::new(0, "value does not match field type")),
860 }
861 Ok(())
862}
863
864fn ensure_output_growth(
866 current: usize,
867 overhead: usize,
868 payload: usize,
869 limit: usize,
870) -> Result<()> {
871 let required = current
872 .checked_add(overhead)
873 .and_then(|value| value.checked_add(payload))
874 .ok_or_else(|| Error::new(0, "encoded message size overflow"))?;
875 if required > limit {
876 return Err(Error::new(
877 0,
878 "encoded message exceeds configured size limit",
879 ));
880 }
881 Ok(())
882}
883
884fn normalized_f32(value: f32, policy: FloatEncoding) -> f32 {
886 if policy == FloatEncoding::Normalize {
887 if value.is_nan() {
888 f32::from_bits(CANONICAL_F32_NAN_BITS)
889 } else if value == 0.0 {
890 0.0
891 } else {
892 value
893 }
894 } else {
895 value
896 }
897}
898
899fn normalized_f64(value: f64, policy: FloatEncoding) -> f64 {
901 if policy == FloatEncoding::Normalize {
902 if value.is_nan() {
903 f64::from_bits(CANONICAL_F64_NAN_BITS)
904 } else if value == 0.0 {
905 0.0
906 } else {
907 value
908 }
909 } else {
910 value
911 }
912}
913
914fn compare_map_keys(kind: &FieldType, left: &Value, right: &Value) -> Ordering {
916 match (kind, left, right) {
917 (
918 FieldType::Int32 | FieldType::Sint32 | FieldType::Sfixed32,
919 Value::Int32(left),
920 Value::Int32(right),
921 ) => left.cmp(right),
922 (
923 FieldType::Int64 | FieldType::Sint64 | FieldType::Sfixed64,
924 Value::Int64(left),
925 Value::Int64(right),
926 ) => left.cmp(right),
927 (FieldType::Uint32 | FieldType::Fixed32, Value::Uint32(left), Value::Uint32(right)) => {
928 left.cmp(right)
929 }
930 (FieldType::Uint64 | FieldType::Fixed64, Value::Uint64(left), Value::Uint64(right)) => {
931 left.cmp(right)
932 }
933 (FieldType::Bool, Value::Bool(left), Value::Bool(right)) => left.cmp(right),
934 (FieldType::String, Value::String(left), Value::String(right)) => left.cmp(right),
935 (FieldType::String, Value::RawString(left), Value::RawString(right)) => left.cmp(right),
936 _ => Ordering::Equal,
937 }
938}
939pub fn encode(s: &Schema, d: &MessageDescriptor, m: &Message) -> Result<Vec<u8>> {
946 Ok(encode_with_options(s, d, m, &EncodeOptions::default())?.bytes)
947}
948
949pub fn encode_with_options(
959 s: &Schema,
960 d: &MessageDescriptor,
961 m: &Message,
962 options: &EncodeOptions,
963) -> Result<EncodeOutput> {
964 encode_inner(s, d, m, options)
965}
966
967fn validate_message_shape(s: &Schema, d: &MessageDescriptor, m: &Message) -> Result<()> {
970 let mut selected_oneofs = BTreeSet::new();
971 for field in s.fields_for(d) {
972 let Some(value) = m.get(&field.name) else {
973 continue;
974 };
975 if let Some(oneof) = &field.oneof
976 && !selected_oneofs.insert(oneof.as_str())
977 {
978 return Err(Error::new(0, "multiple values supplied for oneof"));
979 }
980 match (&field.kind, field.cardinality, value) {
981 (FieldType::Map(_, _), _, Value::Map(entries)) => {
982 for (index, (key, _)) in entries.iter().enumerate() {
983 if entries[..index].iter().any(|(previous, _)| previous == key) {
984 return Err(Error::new(0, "dynamic map contains duplicate keys"));
985 }
986 }
987 }
988 (FieldType::Map(_, _), _, _) => {
989 return Err(Error::new(0, "map field requires Value::Map"));
990 }
991 (_, Cardinality::Repeated, Value::Repeated(_)) => {}
992 (_, Cardinality::Repeated, _) => {
993 return Err(Error::new(0, "repeated field requires Value::Repeated"));
994 }
995 (_, _, Value::Repeated(_)) => {
996 return Err(Error::new(
997 0,
998 "singular field cannot contain Value::Repeated",
999 ));
1000 }
1001 _ => {}
1002 }
1003 }
1004 Ok(())
1005}
1006
1007fn validate_raw_field(number: u32, wire_type: u8, encoded_value: &[u8]) -> Result<()> {
1009 if !(MIN_FIELD_NUMBER..=MAX_FIELD_NUMBER).contains(&number)
1010 || !is_supported_wire_type(wire_type)
1011 {
1012 return Err(Error::new(0, "raw field has invalid wire metadata"));
1013 }
1014 let mut cursor = 0;
1015 skip(number, wire_type, encoded_value, &mut cursor)?;
1016 if cursor != encoded_value.len() {
1017 return Err(Error::new(cursor, "raw field contains trailing wire data"));
1018 }
1019 Ok(())
1020}
1021
1022fn validate_encoded_occurrence(record: &AuditRecord) -> Result<()> {
1024 let mut cursor = 0;
1025 let (number, wire_type) = read_key(&record.encoded_field, &mut cursor)?;
1026 if number != record.field_number || wire_type != record.wire_type {
1027 return Err(Error::new(
1028 0,
1029 "audit wire metadata does not match encoded field",
1030 ));
1031 }
1032 skip(number, wire_type, &record.encoded_field, &mut cursor)?;
1033 if cursor != record.encoded_field.len() {
1034 return Err(Error::new(
1035 cursor,
1036 "audit field contains trailing wire data",
1037 ));
1038 }
1039 Ok(())
1040}
1041
1042fn encode_inner(
1044 s: &Schema,
1045 d: &MessageDescriptor,
1046 m: &Message,
1047 options: &EncodeOptions,
1048) -> Result<EncodeOutput> {
1049 validate_message_shape(s, d, m)?;
1050 let mut o = Vec::new();
1051 let mut audit = m.audit.clone();
1052 if options.duplicates == DuplicatePolicy::PreserveAll {
1053 for record in &m.audit {
1054 if record.tag == AuditTag::DuplicateDiscarded {
1055 if record.encoded_field.is_empty() {
1056 return Err(Error::new(
1057 0,
1058 "cannot preserve duplicate without retained audit bytes",
1059 ));
1060 }
1061 validate_encoded_occurrence(record)?;
1062 ensure_output_growth(
1063 o.len(),
1064 0,
1065 record.encoded_field.len(),
1066 options.max_output_bytes,
1067 )?;
1068 o.extend_from_slice(&record.encoded_field);
1069 }
1070 }
1071 }
1072 for f in s.fields_for(d) {
1073 if let Some(v) = m.get(&f.name) {
1074 if let FieldType::Map(key_type, value_type) = &f.kind {
1075 let Value::Map(entries) = v else {
1076 return Err(Error::new(0, "map field requires Value::Map"));
1077 };
1078 let mut ordered_entries: Vec<_> = entries.iter().collect();
1079 if options.map_order == MapOrder::Key {
1080 ordered_entries
1081 .sort_by(|left, right| compare_map_keys(key_type, &left.0, &right.0));
1082 }
1083 for (key, value) in ordered_entries {
1084 let mut entry = Vec::new();
1085 vi(make_key(MAP_KEY_FIELD_NUMBER, wire(key_type)), &mut entry);
1086 scalar(key_type, key, s, options, &mut entry)?;
1087 vi(
1088 make_key(MAP_VALUE_FIELD_NUMBER, wire(value_type)),
1089 &mut entry,
1090 );
1091 scalar(value_type, value, s, options, &mut entry)?;
1092 ensure_output_growth(
1093 o.len(),
1094 minimal_varint_length(make_key(f.number, WIRE_TYPE_LENGTH_DELIMITED))
1095 + minimal_varint_length(entry.len() as u64),
1096 entry.len(),
1097 options.max_output_bytes,
1098 )?;
1099 vi(make_key(f.number, WIRE_TYPE_LENGTH_DELIMITED), &mut o);
1100 vi(entry.len() as u64, &mut o);
1101 o.extend(entry);
1102 }
1103 continue;
1104 }
1105 if !f.explicit_presence && is_default(v) {
1106 continue;
1107 }
1108 let xs = if let Value::Repeated(x) = v {
1109 x.as_slice()
1110 } else {
1111 core::slice::from_ref(v)
1112 };
1113 if xs.is_empty() {
1114 continue;
1115 }
1116 let pack = matches!(v, Value::Repeated(_)) && f.packed.unwrap_or(false);
1117 if pack {
1118 let mut z = Vec::new();
1119 for x in xs {
1120 scalar(&f.kind, x, s, options, &mut z)?
1121 }
1122 ensure_output_growth(
1123 o.len(),
1124 minimal_varint_length(make_key(f.number, WIRE_TYPE_LENGTH_DELIMITED))
1125 + minimal_varint_length(z.len() as u64),
1126 z.len(),
1127 options.max_output_bytes,
1128 )?;
1129 vi(make_key(f.number, WIRE_TYPE_LENGTH_DELIMITED), &mut o);
1130 vi(z.len() as u64, &mut o);
1131 o.extend(z)
1132 } else {
1133 for x in xs {
1134 if matches!(f.kind, FieldType::Message(_))
1135 && f.features.message_encoding == MessageEncoding::Delimited
1136 {
1137 let (FieldType::Message(name), Value::Message(message)) = (&f.kind, x)
1138 else {
1139 return Err(Error::new(0, "value does not match message field type"));
1140 };
1141 let nested = s
1142 .message(name)
1143 .ok_or_else(|| Error::new(0, "unknown message type"))?;
1144 vi(make_key(f.number, WIRE_TYPE_START_GROUP), &mut o);
1145 o.extend(encode_inner(s, nested, message, options)?.bytes);
1146 vi(make_key(f.number, WIRE_TYPE_END_GROUP), &mut o);
1147 } else {
1148 vi(make_key(f.number, wire(&f.kind)), &mut o);
1149 if let (FieldType::String, Value::RawString(bytes)) = (&f.kind, x) {
1150 if f.features.utf8_validation != Utf8Validation::None {
1151 return Err(Error::new(
1152 0,
1153 "raw string requires utf8_validation = NONE",
1154 ));
1155 }
1156 ensure_output_growth(
1157 o.len(),
1158 minimal_varint_length(bytes.len() as u64),
1159 bytes.len(),
1160 options.max_output_bytes,
1161 )?;
1162 vi(bytes.len() as u64, &mut o);
1163 o.extend(bytes);
1164 } else {
1165 scalar(&f.kind, x, s, options, &mut o)?
1166 }
1167 }
1168 }
1169 }
1170 } else if f.cardinality == Cardinality::Required
1171 || f.features.field_presence == FieldPresence::LegacyRequired
1172 {
1173 return Err(Error::new(0, "missing required field"));
1174 }
1175 }
1176 for u in &m.unknown_fields {
1177 let forward = if matches!(
1178 u.wire_type,
1179 WIRE_TYPE_LENGTH_DELIMITED | WIRE_TYPE_START_GROUP
1180 ) {
1181 options.forward_unknown_messages
1182 } else {
1183 options.forward_unknown_fields
1184 };
1185 if forward {
1186 validate_raw_field(u.number, u.wire_type, &u.encoded_value)?;
1187 ensure_output_growth(
1188 o.len(),
1189 minimal_varint_length(make_key(u.number, u.wire_type)),
1190 u.encoded_value.len(),
1191 options.max_output_bytes,
1192 )?;
1193 vi(make_key(u.number, u.wire_type), &mut o);
1194 o.extend_from_slice(&u.encoded_value)
1195 }
1196 }
1197 if options.forward_added_fields {
1198 for field in &m.added_fields {
1199 validate_raw_field(field.number, field.wire_type, &field.encoded_value)?;
1200 ensure_output_growth(
1201 o.len(),
1202 minimal_varint_length(make_key(field.number, field.wire_type)),
1203 field.encoded_value.len(),
1204 options.max_output_bytes,
1205 )?;
1206 vi(make_key(field.number, field.wire_type), &mut o);
1207 o.extend_from_slice(&field.encoded_value);
1208 }
1209 }
1210 for name in m.fields.keys() {
1211 if !s.fields_for(d).any(|field| field.name == *name) {
1212 audit.push(AuditRecord {
1213 tag: AuditTag::AddedField,
1214 field_name: Some(name.clone()),
1215 field_number: 0,
1216 wire_type: 0,
1217 encoded_field: Vec::new(),
1218 });
1219 if options.forward_added_fields {
1220 return Err(Error::new(
1221 0,
1222 "schema-absent named field needs raw wire metadata via Message::add_field",
1223 ));
1224 }
1225 }
1226 }
1227 apply_field_order(&mut o, options.field_order)?;
1228 if o.len() > options.max_output_bytes {
1229 return Err(Error::new(
1230 0,
1231 "encoded message exceeds configured size limit",
1232 ));
1233 }
1234 Ok(EncodeOutput { bytes: o, audit })
1235}
1236
1237fn apply_field_order(bytes: &mut Vec<u8>, order: FieldOrder) -> Result<()> {
1242 if order == FieldOrder::Declaration {
1243 return Ok(());
1244 }
1245 let mut cursor = 0;
1246 let mut occurrences = Vec::new();
1247 while cursor < bytes.len() {
1248 let start = cursor;
1249 let (number, wire_type) = read_key(bytes, &mut cursor)?;
1250 skip(number, wire_type, bytes, &mut cursor)?;
1251 occurrences.push((number, audit_bytes(bytes, start, cursor)?));
1252 }
1253 occurrences.sort_by_key(|(number, _)| *number);
1254 bytes.clear();
1255 for (_, occurrence) in occurrences {
1256 bytes.extend_from_slice(&occurrence);
1257 }
1258 Ok(())
1259}
1260fn is_default(value: &Value) -> bool {
1262 match value {
1263 Value::Double(value) => *value == 0.0,
1264 Value::Float(value) => *value == 0.0,
1265 Value::Int32(value) | Value::Enum(value) => *value == 0,
1266 Value::Int64(value) => *value == 0,
1267 Value::Uint32(value) => *value == 0,
1268 Value::Uint64(value) => *value == 0,
1269 Value::Bool(value) => !*value,
1270 Value::String(value) => value.is_empty(),
1271 Value::RawString(value) => value.is_empty(),
1272 Value::Bytes(value) => value.is_empty(),
1273 Value::Repeated(value) => value.is_empty(),
1274 Value::Map(value) => value.is_empty(),
1275 Value::Message(_) => false,
1276 }
1277}
1278
1279fn merge_message(existing: &mut Message, mut incoming: Message) {
1281 for (name, value) in incoming.fields {
1282 match (existing.fields.get_mut(&name), value) {
1283 (Some(Value::Message(old)), Value::Message(new)) => merge_message(old, new),
1284 (Some(Value::Repeated(old)), Value::Repeated(mut new)) => old.append(&mut new),
1285 (Some(Value::Map(old)), Value::Map(new)) => {
1286 for (key, value) in new {
1287 if let Some(entry) = old.iter_mut().find(|(candidate, _)| candidate == &key) {
1288 entry.1 = value;
1289 } else {
1290 old.push((key, value));
1291 }
1292 }
1293 }
1294 (_, value) => {
1295 existing.fields.insert(name, value);
1296 }
1297 }
1298 }
1299 existing.unknown_fields.append(&mut incoming.unknown_fields);
1300 existing.added_fields.append(&mut incoming.added_fields);
1301 existing.audit.append(&mut incoming.audit);
1302}
1303fn take<'a>(b: &'a [u8], p: &mut usize, n: usize) -> Result<&'a [u8]> {
1305 let end = p
1306 .checked_add(n)
1307 .ok_or_else(|| Error::new(*p, "field length overflow"))?;
1308 let x = b
1309 .get(*p..end)
1310 .ok_or_else(|| Error::new(*p, "truncated field"))?;
1311 *p = end;
1312 Ok(x)
1313}
1314fn fixed_bytes<const SIZE: usize>(b: &[u8], p: &mut usize) -> Result<[u8; SIZE]> {
1316 let start = *p;
1317 take(b, p, SIZE)?
1318 .try_into()
1319 .map_err(|_| Error::new(start, "invalid fixed-width field"))
1320}
1321
1322fn audit_bytes(b: &[u8], start: usize, end: usize) -> Result<Vec<u8>> {
1324 b.get(start..end)
1325 .map(<[u8]>::to_vec)
1326 .ok_or_else(|| Error::new(start, "invalid audit byte range"))
1327}
1328
1329struct DecodeContext<'a> {
1331 options: &'a DecodeOptions,
1333 field_occurrences: usize,
1335 audit_bytes: usize,
1337}
1338
1339impl DecodeContext<'_> {
1340 fn count_field(&mut self, offset: usize) -> Result<()> {
1342 self.field_occurrences = self
1343 .field_occurrences
1344 .checked_add(1)
1345 .ok_or_else(|| Error::new(offset, "field occurrence counter overflow"))?;
1346 if self.field_occurrences > self.options.max_field_occurrences {
1347 return Err(Error::new(
1348 offset,
1349 "message exceeds configured field occurrence limit",
1350 ));
1351 }
1352 Ok(())
1353 }
1354
1355 fn varint(&self, bytes: &[u8], cursor: &mut usize) -> Result<u64> {
1357 read_varint(bytes, cursor, self.options.require_minimal_varints)
1358 }
1359
1360 fn length(&self, bytes: &[u8], cursor: &mut usize) -> Result<usize> {
1362 let offset = *cursor;
1363 let length = usize::try_from(self.varint(bytes, cursor)?)
1364 .map_err(|_| Error::new(offset, "length does not fit this target"))?;
1365 if length > self.options.max_length_delimited_bytes {
1366 return Err(Error::new(
1367 offset,
1368 "length-delimited value exceeds configured size limit",
1369 ));
1370 }
1371 Ok(length)
1372 }
1373
1374 fn audit_record(
1376 &mut self,
1377 bytes: &[u8],
1378 range: Range<usize>,
1379 mut record: AuditRecord,
1380 ) -> Result<AuditRecord> {
1381 let encoded_field = if self.options.audit_mode == AuditMode::Full {
1382 let length = range
1383 .end
1384 .checked_sub(range.start)
1385 .ok_or_else(|| Error::new(range.start, "audit cursor moved backwards"))?;
1386 self.audit_bytes = self
1387 .audit_bytes
1388 .checked_add(length)
1389 .ok_or_else(|| Error::new(range.start, "audit byte counter overflow"))?;
1390 if self.audit_bytes > self.options.max_audit_bytes {
1391 return Err(Error::new(
1392 range.start,
1393 "message exceeds configured audit byte limit",
1394 ));
1395 }
1396 audit_bytes(bytes, range.start, range.end)?
1397 } else {
1398 Vec::new()
1399 };
1400 record.encoded_field = encoded_field;
1401 Ok(record)
1402 }
1403}
1404
1405fn val(
1410 t: &FieldType,
1411 w: u8,
1412 b: &[u8],
1413 p: &mut usize,
1414 s: &Schema,
1415 context: &mut DecodeContext<'_>,
1416 depth: usize,
1417) -> Result<Value> {
1418 if w != wire(t) {
1419 return Err(Error::new(*p, "wrong wire type"));
1420 }
1421 Ok(match t {
1422 FieldType::Double => Value::Double(f64::from_le_bytes(fixed_bytes::<FIXED64_SIZE>(b, p)?)),
1423 FieldType::Float => Value::Float(f32::from_le_bytes(fixed_bytes::<FIXED32_SIZE>(b, p)?)),
1424 FieldType::Int32 => {
1425 let raw = context.varint(b, p)?;
1426 let value = raw as i32;
1427 require_canonical_32_bit_varint(raw, value as i64 as u64, *p, context)?;
1428 Value::Int32(value)
1429 }
1430 FieldType::Int64 => Value::Int64(context.varint(b, p)? as i64),
1431 FieldType::Uint32 => {
1432 let raw = context.varint(b, p)?;
1433 let value = raw as u32;
1434 require_canonical_32_bit_varint(raw, u64::from(value), *p, context)?;
1435 Value::Uint32(value)
1436 }
1437 FieldType::Uint64 => Value::Uint64(context.varint(b, p)?),
1438 FieldType::Sint32 => {
1439 let raw = context.varint(b, p)?;
1440 let n = raw as u32;
1441 require_canonical_32_bit_varint(raw, u64::from(n), *p, context)?;
1442 Value::Int32((n >> 1) as i32 ^ -((n & 1) as i32))
1443 }
1444 FieldType::Sint64 => {
1445 let n = context.varint(b, p)?;
1446 Value::Int64((n >> 1) as i64 ^ -((n & 1) as i64))
1447 }
1448 FieldType::Fixed32 => Value::Uint32(u32::from_le_bytes(fixed_bytes::<FIXED32_SIZE>(b, p)?)),
1449 FieldType::Fixed64 => Value::Uint64(u64::from_le_bytes(fixed_bytes::<FIXED64_SIZE>(b, p)?)),
1450 FieldType::Sfixed32 => Value::Int32(i32::from_le_bytes(fixed_bytes::<FIXED32_SIZE>(b, p)?)),
1451 FieldType::Sfixed64 => Value::Int64(i64::from_le_bytes(fixed_bytes::<FIXED64_SIZE>(b, p)?)),
1452 FieldType::Bool => {
1453 let raw = context.varint(b, p)?;
1454 if raw > 1 && context.options.booleans == BooleanValuePolicy::RejectNonCanonical {
1455 return Err(Error::new(*p, "non-canonical boolean value"));
1456 }
1457 Value::Bool(raw != 0)
1458 }
1459 FieldType::Enum(name) => {
1460 let raw = context.varint(b, p)?;
1461 let value = raw as i32;
1462 require_canonical_32_bit_varint(raw, value as i64 as u64, *p, context)?;
1463 if context.options.enum_values == EnumValuePolicy::RejectUnknown
1464 && !s.enums.get(name).is_some_and(|enumeration| {
1465 enumeration
1466 .values
1467 .iter()
1468 .any(|candidate| candidate.number == value)
1469 })
1470 {
1471 return Err(Error::new(*p, "unknown enum value rejected by policy"));
1472 }
1473 Value::Enum(value)
1474 }
1475 FieldType::String => {
1476 let n = context.length(b, p)?;
1477 Value::String(
1478 core::str::from_utf8(take(b, p, n)?)
1479 .map_err(|_| Error::new(*p, "invalid UTF-8"))?
1480 .to_string(),
1481 )
1482 }
1483 FieldType::Bytes => {
1484 let n = context.length(b, p)?;
1485 Value::Bytes(take(b, p, n)?.to_vec())
1486 }
1487 FieldType::Message(n) => {
1488 let z = context.length(b, p)?;
1489 let q = take(b, p, z)?;
1490 Value::Message(decode_inner(
1491 s,
1492 s.message(n)
1493 .ok_or_else(|| Error::new(*p, "unknown message type"))?,
1494 q,
1495 context,
1496 depth
1497 .checked_add(1)
1498 .ok_or_else(|| Error::new(*p, "recursion depth overflow"))?,
1499 )?)
1500 }
1501 FieldType::Map(..) => return Err(Error::new(*p, "map codec not yet supported")),
1502 })
1503}
1504
1505fn delimited_message_body<'a>(
1507 field_number: u32,
1508 bytes: &'a [u8],
1509 cursor: &mut usize,
1510) -> Result<&'a [u8]> {
1511 let body_start = *cursor;
1512 let mut scan = *cursor;
1513 loop {
1514 if scan == bytes.len() {
1515 return Err(Error::new(scan, "unterminated delimited message"));
1516 }
1517 let tag_start = scan;
1518 let (nested_number, nested_wire_type) = read_key(bytes, &mut scan)?;
1519 if nested_wire_type == WIRE_TYPE_END_GROUP {
1520 if nested_number != field_number {
1521 return Err(Error::new(scan, "mismatched delimited message terminator"));
1522 }
1523 *cursor = scan;
1524 return bytes
1525 .get(body_start..tag_start)
1526 .ok_or_else(|| Error::new(body_start, "invalid delimited message bounds"));
1527 }
1528 skip(nested_number, nested_wire_type, bytes, &mut scan)?;
1529 }
1530}
1531
1532fn field_value(
1534 field: &Field,
1535 wire_type: u8,
1536 bytes: &[u8],
1537 cursor: &mut usize,
1538 schema: &Schema,
1539 context: &mut DecodeContext<'_>,
1540 depth: usize,
1541) -> Result<Value> {
1542 if matches!(field.kind, FieldType::String)
1543 && field.features.utf8_validation == Utf8Validation::None
1544 {
1545 if wire_type != WIRE_TYPE_LENGTH_DELIMITED {
1546 return Err(Error::new(*cursor, "string has wrong wire type"));
1547 }
1548 let length = context.length(bytes, cursor)?;
1549 return Ok(Value::RawString(take(bytes, cursor, length)?.to_vec()));
1550 }
1551 if field.features.message_encoding != MessageEncoding::Delimited
1552 || !matches!(field.kind, FieldType::Message(_))
1553 {
1554 return val(
1555 &field.kind,
1556 wire_type,
1557 bytes,
1558 cursor,
1559 schema,
1560 context,
1561 depth,
1562 );
1563 }
1564 let FieldType::Message(name) = &field.kind else {
1565 return Err(Error::new(
1566 *cursor,
1567 "DELIMITED message encoding requires a message field",
1568 ));
1569 };
1570 if wire_type != WIRE_TYPE_START_GROUP {
1571 return Err(Error::new(*cursor, "delimited message has wrong wire type"));
1572 }
1573 let body = delimited_message_body(field.number, bytes, cursor)?;
1574 let nested = schema
1575 .message(name)
1576 .ok_or_else(|| Error::new(*cursor, "unknown message type"))?;
1577 let nested_depth = depth
1578 .checked_add(1)
1579 .ok_or_else(|| Error::new(*cursor, "recursion depth overflow"))?;
1580 Ok(Value::Message(decode_inner(
1581 schema,
1582 nested,
1583 body,
1584 context,
1585 nested_depth,
1586 )?))
1587}
1588
1589fn require_canonical_32_bit_varint(
1591 raw: u64,
1592 canonical: u64,
1593 offset: usize,
1594 context: &DecodeContext<'_>,
1595) -> Result<()> {
1596 if context.options.require_minimal_varints && raw != canonical {
1597 return Err(Error::new(offset, "non-canonical 32-bit varint value"));
1598 }
1599 Ok(())
1600}
1601
1602fn default_value(t: &FieldType) -> Result<Value> {
1604 Ok(match t {
1605 FieldType::Double => Value::Double(0.0),
1606 FieldType::Float => Value::Float(0.0),
1607 FieldType::Int32 | FieldType::Sint32 | FieldType::Sfixed32 => Value::Int32(0),
1608 FieldType::Int64 | FieldType::Sint64 | FieldType::Sfixed64 => Value::Int64(0),
1609 FieldType::Uint32 | FieldType::Fixed32 => Value::Uint32(0),
1610 FieldType::Uint64 | FieldType::Fixed64 => Value::Uint64(0),
1611 FieldType::Bool => Value::Bool(false),
1612 FieldType::String => Value::String(String::new()),
1613 FieldType::Bytes => Value::Bytes(Vec::new()),
1614 FieldType::Enum(_) => Value::Enum(0),
1615 FieldType::Message(_) => Value::Message(Message::new()),
1616 FieldType::Map(..) => return Err(Error::new(0, "a map cannot be a map value")),
1617 })
1618}
1619
1620fn decode_map_entry(
1625 key_type: &FieldType,
1626 value_type: &FieldType,
1627 b: &[u8],
1628 p: &mut usize,
1629 s: &Schema,
1630 context: &mut DecodeContext<'_>,
1631 depth: usize,
1632) -> Result<(Value, Value)> {
1633 let length = context.length(b, p)?;
1634 let entry = take(b, p, length)?;
1635 let mut cursor = 0;
1636 let mut key = None;
1637 let mut value = None;
1638 while cursor < entry.len() {
1639 context.count_field(cursor)?;
1640 let tag = context.varint(entry, &mut cursor)?;
1641 match tag >> FIELD_NUMBER_SHIFT {
1642 field if field == u64::from(MAP_KEY_FIELD_NUMBER) => {
1643 if key.is_some() && context.options.duplicates == DuplicateInputPolicy::Reject {
1644 return Err(Error::new(
1645 cursor,
1646 "duplicate map-entry key rejected by policy",
1647 ));
1648 }
1649 key = Some(val(
1650 key_type,
1651 (tag & WIRE_TYPE_MASK) as u8,
1652 entry,
1653 &mut cursor,
1654 s,
1655 context,
1656 depth,
1657 )?)
1658 }
1659 field if field == u64::from(MAP_VALUE_FIELD_NUMBER) => {
1660 if value.is_some() && context.options.duplicates == DuplicateInputPolicy::Reject {
1661 return Err(Error::new(
1662 cursor,
1663 "duplicate map-entry value rejected by policy",
1664 ));
1665 }
1666 value = Some(val(
1667 value_type,
1668 (tag & WIRE_TYPE_MASK) as u8,
1669 entry,
1670 &mut cursor,
1671 s,
1672 context,
1673 depth,
1674 )?)
1675 }
1676 _ => {
1677 skip_decode(
1678 tag as u32 >> FIELD_NUMBER_SHIFT,
1679 (tag & WIRE_TYPE_MASK) as u8,
1680 entry,
1681 &mut cursor,
1682 context,
1683 depth,
1684 )?;
1685 }
1686 }
1687 }
1688 Ok((
1689 key.unwrap_or(default_value(key_type)?),
1690 value.unwrap_or(default_value(value_type)?),
1691 ))
1692}
1693fn skip(field_number: u32, w: u8, b: &[u8], p: &mut usize) -> Result<Vec<u8>> {
1698 let s = *p;
1699 match w {
1700 WIRE_TYPE_VARINT => {
1701 rv(b, p)?;
1702 }
1703 WIRE_TYPE_FIXED64 => {
1704 take(b, p, FIXED64_SIZE)?;
1705 }
1706 WIRE_TYPE_LENGTH_DELIMITED => {
1707 let offset = *p;
1708 let n = usize::try_from(rv(b, p)?)
1709 .map_err(|_| Error::new(offset, "length does not fit this target"))?;
1710 take(b, p, n)?;
1711 }
1712 WIRE_TYPE_FIXED32 => {
1713 take(b, p, FIXED32_SIZE)?;
1714 }
1715 WIRE_TYPE_START_GROUP => loop {
1716 if *p == b.len() {
1717 return Err(Error::new(*p, "unterminated group"));
1718 }
1719 let (nested_number, nested_wire_type) = read_key(b, p)?;
1720 if nested_wire_type == WIRE_TYPE_END_GROUP {
1721 if nested_number != field_number {
1722 return Err(Error::new(*p, "mismatched end-group field number"));
1723 }
1724 break;
1725 }
1726 skip(nested_number, nested_wire_type, b, p)?;
1727 },
1728 WIRE_TYPE_END_GROUP => return Err(Error::new(*p, "unexpected end-group tag")),
1729 _ => return Err(Error::new(*p, "unsupported wire type")),
1730 }
1731 audit_bytes(b, s, *p)
1732}
1733
1734fn skip_decode(
1736 field_number: u32,
1737 wire_type: u8,
1738 bytes: &[u8],
1739 cursor: &mut usize,
1740 context: &mut DecodeContext<'_>,
1741 depth: usize,
1742) -> Result<Vec<u8>> {
1743 let start = *cursor;
1744 match wire_type {
1745 WIRE_TYPE_VARINT => {
1746 context.varint(bytes, cursor)?;
1747 }
1748 WIRE_TYPE_FIXED64 => {
1749 take(bytes, cursor, FIXED64_SIZE)?;
1750 }
1751 WIRE_TYPE_LENGTH_DELIMITED => {
1752 let length = context.length(bytes, cursor)?;
1753 take(bytes, cursor, length)?;
1754 }
1755 WIRE_TYPE_FIXED32 => {
1756 take(bytes, cursor, FIXED32_SIZE)?;
1757 }
1758 WIRE_TYPE_START_GROUP => {
1759 let nested_depth = depth
1760 .checked_add(1)
1761 .ok_or_else(|| Error::new(*cursor, "recursion depth overflow"))?;
1762 if nested_depth > context.options.max_recursion_depth {
1763 return Err(Error::new(
1764 *cursor,
1765 "group exceeds configured recursion limit",
1766 ));
1767 }
1768 loop {
1769 if *cursor == bytes.len() {
1770 return Err(Error::new(*cursor, "unterminated group"));
1771 }
1772 let field_start = *cursor;
1773 context.count_field(field_start)?;
1774 let (nested_number, nested_wire_type) =
1775 read_key_with_policy(bytes, cursor, context.options.require_minimal_varints)?;
1776 if nested_wire_type == WIRE_TYPE_END_GROUP {
1777 if nested_number != field_number {
1778 return Err(Error::new(*cursor, "mismatched end-group field number"));
1779 }
1780 break;
1781 }
1782 skip_decode(
1783 nested_number,
1784 nested_wire_type,
1785 bytes,
1786 cursor,
1787 context,
1788 nested_depth,
1789 )?;
1790 }
1791 }
1792 WIRE_TYPE_END_GROUP => return Err(Error::new(*cursor, "unexpected end-group tag")),
1793 _ => return Err(Error::new(*cursor, "unsupported wire type")),
1794 }
1795 audit_bytes(bytes, start, *cursor)
1796}
1797pub fn decode(s: &Schema, d: &MessageDescriptor, b: &[u8]) -> Result<Message> {
1807 decode_with_options(s, d, b, &DecodeOptions::default())
1808}
1809
1810pub fn decode_with_options(
1818 schema: &Schema,
1819 descriptor: &MessageDescriptor,
1820 bytes: &[u8],
1821 options: &DecodeOptions,
1822) -> Result<Message> {
1823 let mut context = DecodeContext {
1824 options,
1825 field_occurrences: 0,
1826 audit_bytes: 0,
1827 };
1828 decode_inner(schema, descriptor, bytes, &mut context, 0)
1829}
1830
1831fn decode_inner(
1833 s: &Schema,
1834 d: &MessageDescriptor,
1835 b: &[u8],
1836 context: &mut DecodeContext<'_>,
1837 depth: usize,
1838) -> Result<Message> {
1839 if depth > context.options.max_recursion_depth {
1840 return Err(Error::new(0, "message exceeds configured recursion limit"));
1841 }
1842 if b.len() > context.options.max_message_bytes {
1843 return Err(Error::new(0, "message exceeds configured input size limit"));
1844 }
1845 let (mut m, mut p) = (Message::new(), 0);
1846 while p < b.len() {
1847 let field_start = p;
1848 context.count_field(field_start)?;
1849 let (n, w) = read_key_with_policy(b, &mut p, context.options.require_minimal_varints)?;
1850 let Some(f) = s.field_by_number(d, n) else {
1851 let encoded_value = skip_decode(n, w, b, &mut p, context, depth)?;
1852 if context.options.unknown_fields == UnknownFieldPolicy::Reject {
1853 return Err(Error::new(field_start, "unknown field rejected by policy"));
1854 }
1855 if context.options.unknown_fields == UnknownFieldPolicy::Preserve {
1856 m.unknown_fields.push(UnknownField {
1857 number: n,
1858 wire_type: w,
1859 encoded_value,
1860 });
1861 }
1862 let tag = if matches!(w, WIRE_TYPE_LENGTH_DELIMITED | WIRE_TYPE_START_GROUP) {
1863 AuditTag::UnknownMessage
1864 } else {
1865 AuditTag::UnknownField
1866 };
1867 let record = context.audit_record(
1868 b,
1869 field_start..p,
1870 AuditRecord {
1871 tag,
1872 field_name: None,
1873 field_number: n,
1874 wire_type: w,
1875 encoded_field: Vec::new(),
1876 },
1877 )?;
1878 m.audit.push(record);
1879 continue;
1880 };
1881 if let FieldType::Map(key_type, value_type) = &f.kind {
1882 if w != WIRE_TYPE_LENGTH_DELIMITED {
1883 return Err(Error::new(p, "map field has wrong wire type"));
1884 }
1885 let (key, value) =
1886 decode_map_entry(key_type, value_type, b, &mut p, s, context, depth)?;
1887 let mut entries = match m.fields.remove(&f.name) {
1888 Some(Value::Map(entries)) => entries,
1889 _ => Vec::new(),
1890 };
1891 let duplicate = entries.iter().any(|(candidate, _)| candidate == &key);
1894 if duplicate && context.options.duplicates == DuplicateInputPolicy::Reject {
1895 return Err(Error::new(
1896 field_start,
1897 "duplicate map key rejected by policy",
1898 ));
1899 }
1900 if let Some(existing) = entries.iter_mut().find(|(candidate, _)| candidate == &key) {
1901 existing.1 = value;
1902 } else {
1903 if entries.len() >= context.options.max_map_entries {
1904 return Err(Error::new(
1905 field_start,
1906 "map exceeds configured entry limit",
1907 ));
1908 }
1909 entries.push((key, value));
1910 }
1911 m.insert(f.name.clone(), Value::Map(entries));
1912 if duplicate
1913 && let Some(previous) = m.audit.iter_mut().rev().find(|record| {
1914 record.field_name.as_deref() == Some(&f.name)
1915 && matches!(
1916 record.tag,
1917 AuditTag::SchemaField | AuditTag::DuplicateLastWins
1918 )
1919 })
1920 {
1921 previous.tag = AuditTag::DuplicateDiscarded;
1922 }
1923 let tag = if duplicate {
1924 AuditTag::DuplicateLastWins
1925 } else {
1926 AuditTag::SchemaField
1927 };
1928 let record = context.audit_record(
1929 b,
1930 field_start..p,
1931 AuditRecord {
1932 tag,
1933 field_name: Some(f.name.clone()),
1934 field_number: n,
1935 wire_type: w,
1936 encoded_field: Vec::new(),
1937 },
1938 )?;
1939 m.audit.push(record);
1940 continue;
1941 }
1942 if let FieldType::Enum(name) = &f.kind
1943 && w == WIRE_TYPE_VARINT
1944 && s.enums
1945 .get(name)
1946 .is_some_and(|enumeration| enumeration.features.enum_type == EnumType::Closed)
1947 {
1948 let value_start = p;
1949 let raw = context.varint(b, &mut p)?;
1950 let value = raw as i32;
1951 require_canonical_32_bit_varint(raw, value as i64 as u64, p, context)?;
1952 let known = s.enums.get(name).is_some_and(|enumeration| {
1953 enumeration
1954 .values
1955 .iter()
1956 .any(|candidate| candidate.number == value)
1957 });
1958 if !known {
1959 if context.options.unknown_fields == UnknownFieldPolicy::Reject {
1960 return Err(Error::new(field_start, "closed enum value is unknown"));
1961 }
1962 if context.options.unknown_fields == UnknownFieldPolicy::Preserve {
1963 m.unknown_fields.push(UnknownField {
1964 number: f.number,
1965 wire_type: w,
1966 encoded_value: audit_bytes(b, value_start, p)?,
1967 });
1968 }
1969 let record = context.audit_record(
1970 b,
1971 field_start..p,
1972 AuditRecord {
1973 tag: AuditTag::UnknownField,
1974 field_name: Some(f.name.clone()),
1975 field_number: n,
1976 wire_type: w,
1977 encoded_field: Vec::new(),
1978 },
1979 )?;
1980 m.audit.push(record);
1981 continue;
1982 }
1983 p = value_start;
1984 }
1985 let repeated = f.cardinality == Cardinality::Repeated;
1986 let duplicate = !repeated
1987 && (m.fields.contains_key(&f.name)
1988 || f.oneof.as_ref().is_some_and(|group| {
1989 d.fields.iter().any(|candidate| {
1990 candidate.oneof.as_ref() == Some(group)
1991 && m.fields.contains_key(&candidate.name)
1992 })
1993 }));
1994 if duplicate && context.options.duplicates == DuplicateInputPolicy::Reject {
1995 return Err(Error::new(
1996 field_start,
1997 "duplicate singular field rejected by policy",
1998 ));
1999 }
2000 let mut xs = if repeated {
2001 match m.fields.remove(&f.name) {
2002 Some(Value::Repeated(x)) => x,
2003 _ => Vec::new(),
2004 }
2005 } else {
2006 Vec::new()
2007 };
2008 if repeated
2009 && w == WIRE_TYPE_LENGTH_DELIMITED
2010 && wire(&f.kind) != WIRE_TYPE_LENGTH_DELIMITED
2011 {
2012 let length = context.length(b, &mut p)?;
2013 let packed = take(b, &mut p, length)?;
2014 let mut packed_cursor = 0;
2015 while packed_cursor < packed.len() {
2016 if xs.len() >= context.options.max_repeated_values {
2017 return Err(Error::new(
2018 field_start,
2019 "repeated field exceeds configured value limit",
2020 ));
2021 }
2022 let value = val(
2023 &f.kind,
2024 wire(&f.kind),
2025 packed,
2026 &mut packed_cursor,
2027 s,
2028 context,
2029 depth,
2030 )?;
2031 if let (FieldType::Enum(name), Value::Enum(number)) = (&f.kind, &value)
2032 && s.enums.get(name).is_some_and(|enumeration| {
2033 enumeration.features.enum_type == EnumType::Closed
2034 && !enumeration
2035 .values
2036 .iter()
2037 .any(|candidate| candidate.number == *number)
2038 })
2039 {
2040 if context.options.unknown_fields == UnknownFieldPolicy::Reject {
2041 return Err(Error::new(field_start, "closed enum value is unknown"));
2042 }
2043 if context.options.unknown_fields == UnknownFieldPolicy::Preserve {
2044 let mut encoded_value = Vec::new();
2045 vi(*number as i64 as u64, &mut encoded_value);
2046 m.unknown_fields.push(UnknownField {
2047 number: f.number,
2048 wire_type: WIRE_TYPE_VARINT,
2049 encoded_value,
2050 });
2051 }
2052 } else {
2053 xs.push(value);
2054 }
2055 }
2056 m.insert(f.name.clone(), Value::Repeated(xs));
2057 let record = context.audit_record(
2058 b,
2059 field_start..p,
2060 AuditRecord {
2061 tag: AuditTag::SchemaField,
2062 field_name: Some(f.name.clone()),
2063 field_number: n,
2064 wire_type: w,
2065 encoded_field: Vec::new(),
2066 },
2067 )?;
2068 m.audit.push(record);
2069 continue;
2070 }
2071 let x = field_value(f, w, b, &mut p, s, context, depth)?;
2072 let mut merged = false;
2073 if repeated {
2074 if xs.len() >= context.options.max_repeated_values {
2075 return Err(Error::new(
2076 field_start,
2077 "repeated field exceeds configured value limit",
2078 ));
2079 }
2080 xs.push(x);
2081 m.insert(f.name.clone(), Value::Repeated(xs));
2082 } else {
2083 if let Some(g) = &f.oneof {
2084 for q in &d.fields {
2085 if q.oneof.as_ref() == Some(g) && q.name != f.name {
2086 m.fields.remove(&q.name);
2087 }
2088 }
2089 }
2090 merged = duplicate && matches!(x, Value::Message(_));
2091 match x {
2092 Value::Message(incoming) => {
2093 if let Some(Value::Message(existing)) = m.fields.get_mut(&f.name) {
2094 merge_message(existing, incoming);
2095 } else {
2096 m.insert(f.name.clone(), Value::Message(incoming));
2097 }
2098 }
2099 value => {
2100 m.insert(f.name.clone(), value);
2101 }
2102 }
2103 }
2104 if duplicate
2105 && !merged
2106 && let Some(previous) = m.audit.iter_mut().rev().find(|record| {
2107 let same_field = record.field_name.as_deref() == Some(&f.name);
2108 let same_oneof = f.oneof.as_ref().is_some_and(|group| {
2109 record.field_name.as_deref().is_some_and(|name| {
2110 d.field_by_name(name).and_then(|field| field.oneof.as_ref()) == Some(group)
2111 })
2112 });
2113 (same_field || same_oneof)
2114 && matches!(
2115 record.tag,
2116 AuditTag::SchemaField | AuditTag::DuplicateLastWins
2117 )
2118 })
2119 {
2120 previous.tag = AuditTag::DuplicateDiscarded;
2121 }
2122 let tag = if repeated || !duplicate {
2123 AuditTag::SchemaField
2124 } else if merged {
2125 AuditTag::DuplicateMerged
2126 } else {
2127 AuditTag::DuplicateLastWins
2128 };
2129 let record = context.audit_record(
2130 b,
2131 field_start..p,
2132 AuditRecord {
2133 tag,
2134 field_name: Some(f.name.clone()),
2135 field_number: n,
2136 wire_type: w,
2137 encoded_field: Vec::new(),
2138 },
2139 )?;
2140 m.audit.push(record);
2141 }
2142 for f in s.fields_for(d) {
2143 if (f.cardinality == Cardinality::Required
2144 || f.features.field_presence == FieldPresence::LegacyRequired)
2145 && !m.fields.contains_key(&f.name)
2146 {
2147 return Err(Error::new(0, "missing required field"));
2148 }
2149 }
2150 Ok(m)
2151}