qualia_core_db/q42/field_envelope/
metadata.rs1use super::*;
2#[derive(Clone, Copy, Debug)]
4pub struct Block<'a> {
5 raw: &'a [u8],
6 body: &'a [u8],
7}
8impl<'a> Block<'a> {
9 pub fn id(self) -> u32 {
10 u32le(self.raw, 0)
11 }
12 pub fn schema_type(self) -> u16 {
13 u16le(self.raw, 4)
14 }
15 pub fn revision(self) -> u16 {
16 u16le(self.raw, 6)
17 }
18 pub fn flags(self) -> u32 {
19 u32le(self.raw, 12)
20 }
21 pub fn fields(self) -> Fields<'a> {
22 Fields {
23 remaining: self.body,
24 }
25 }
26 pub fn raw_bytes(self) -> &'a [u8] {
27 self.raw
28 }
29}
30#[derive(Clone, Copy, Debug)]
31pub struct Field<'a> {
32 raw: &'a [u8],
33 value: &'a [u8],
34}
35impl<'a> Field<'a> {
36 pub fn key(self) -> u16 {
37 u16le(self.raw, 0)
38 }
39 pub fn type_tag(self) -> u8 {
40 self.raw[2]
41 }
42 pub fn flags(self) -> u8 {
43 self.raw[3]
44 }
45 pub fn element_count(self) -> u32 {
46 u32le(self.raw, 4)
47 }
48 pub fn value(self) -> &'a [u8] {
49 self.value
50 }
51 pub fn raw_bytes(self) -> &'a [u8] {
52 self.raw
53 }
54}
55#[derive(Clone, Debug)]
57pub struct Blocks<'a> {
58 remaining: &'a [u8],
59}
60impl<'a> Blocks<'a> {
61 pub(super) fn new(bytes: &'a [u8]) -> Self {
62 Self { remaining: bytes }
63 }
64}
65impl<'a> Iterator for Blocks<'a> {
66 type Item = Block<'a>;
67 fn next(&mut self) -> Option<Self::Item> {
68 if self.remaining.is_empty() {
69 return None;
70 }
71 let n = u32le(self.remaining, 8) as usize;
72 let total = 16 + ((n + 7) & !7);
73 let (raw, tail) = self.remaining.split_at(total);
74 self.remaining = tail;
75 Some(Block {
76 raw,
77 body: &raw[16..16 + n],
78 })
79 }
80}
81#[derive(Clone, Debug)]
82pub struct Fields<'a> {
83 remaining: &'a [u8],
84}
85impl<'a> Iterator for Fields<'a> {
86 type Item = Field<'a>;
87 fn next(&mut self) -> Option<Self::Item> {
88 if self.remaining.is_empty() {
89 return None;
90 }
91 let n = u32le(self.remaining, 8) as usize;
92 let total = 16 + ((n + 7) & !7);
93 let (raw, tail) = self.remaining.split_at(total);
94 self.remaining = tail;
95 Some(Field {
96 raw,
97 value: &raw[16..16 + n],
98 })
99 }
100}
101fn item(bytes: &[u8]) -> Result<(&[u8], &[u8], &[u8]), Error> {
102 let h = bytes.get(..16).ok_or(Error::Metadata)?;
103 let n = u64::from(u32le(h, 8));
104 let padded = align(n, 8)?;
105 let value = extent(bytes, 16, n)?;
106 zero(extent(bytes, 16 + n, padded - n)?)?;
107 let total = 16u64.checked_add(padded).ok_or(Error::Overflow)?;
108 let tail = bytes
109 .get(usize::try_from(total).map_err(|_| Error::Extent)?..)
110 .ok_or(Error::Metadata)?;
111 Ok((h, value, tail))
112}
113pub(super) fn validate(mut bytes: &[u8], limits: Limits) -> Result<(), Error> {
116 if bytes.len() as u64 > limits.metadata_bytes {
117 return Err(Error::Limit);
118 }
119 let mut previous = 0;
120 let mut blocks = 0u32;
121 while !bytes.is_empty() {
122 blocks = blocks.checked_add(1).ok_or(Error::Overflow)?;
123 if blocks > limits.blocks {
124 return Err(Error::Limit);
125 }
126 let (h, mut body, tail) = item(bytes)?;
127 bytes = tail;
128 let id = u32le(h, 0);
129 if id <= previous || u32le(h, 12) & !1 != 0 {
130 return Err(Error::Metadata);
131 }
132 previous = id;
133 let mut previous_key = 0;
134 let mut fields = 0u32;
135 while !body.is_empty() {
136 fields = fields.checked_add(1).ok_or(Error::Overflow)?;
137 if fields > limits.fields_per_block {
138 return Err(Error::Limit);
139 }
140 let (h, value, tail) = item(body)?;
141 body = tail;
142 let key = u16le(h, 0);
143 if key <= previous_key || h[3] & !1 != 0 || u32le(h, 12) != 0 {
144 return Err(Error::Metadata);
145 }
146 previous_key = key;
147 validate_primitive(h[2], u32le(h, 4), value, limits)?;
148 }
149 }
150 Ok(())
151}
152
153pub(super) fn validate_primitive(
155 tag: u8,
156 count: u32,
157 value: &[u8],
158 limits: Limits,
159) -> Result<(), Error> {
160 let width = match tag {
161 1 | 4 | 8 | 9 | 11 => Some(4u64),
162 2 | 3 | 5 => Some(8),
163 6 | 7 => Some(1),
164 10 => Some(2),
165 _ => None,
166 };
167 if let Some(width) = width {
168 let expected = u64::from(count).checked_mul(width).ok_or(Error::Overflow)?;
169 if expected != value.len() as u64 {
170 return Err(Error::Numeric);
171 }
172 }
173 match tag {
174 4 => {
175 for v in value.chunks_exact(4) {
176 if !f32::from_le_bytes(v.try_into().unwrap()).is_finite() {
177 return Err(Error::Numeric);
178 }
179 }
180 }
181 5 => {
182 for v in value.chunks_exact(8) {
183 if !f64::from_le_bytes(v.try_into().unwrap()).is_finite() {
184 return Err(Error::Numeric);
185 }
186 }
187 }
188 6 => {
189 if value.len() as u64 > u64::from(limits.string_bytes) {
190 return Err(Error::Limit);
191 }
192 if value.iter().any(u8::is_ascii_control) || std::str::from_utf8(value).is_err() {
193 return Err(Error::Metadata);
194 }
195 }
196 _ => {}
197 }
198 Ok(())
199}