MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / decode_frame

Function decode_frame

atomic-core/examples/semantic_codec_lab.rs:211–296  ·  view source on GitHub ↗
(encoded: &[u8], content: &[u8])

Source from the content-addressed store, hash-verified

209}
210
211fn decode_frame(encoded: &[u8], content: &[u8]) -> Result<Vec<u8>> {
212 ensure!(content.len() <= transforms::LIMIT, "authored content limit");
213 let compressed = encoded.strip_prefix(MAGIC).context("unknown lab version")?;
214 let mut bytes = vec![];
215 let limit = transforms::LIMIT * 2 + 4096;
216 zstd::stream::read::Decoder::new(compressed)?
217 .take(limit as u64 + 1)
218 .read_to_end(&mut bytes)?;
219 ensure!(bytes.len() <= limit, "lab frame limit");
220 let (frame, rest): (BorrowedFrame<'_>, _) = postcard::take_from_bytes(&bytes)?;
221 ensure!(rest.is_empty(), "trailing lab data");
222 ensure!(
223 frame.content_hash == *blake3::hash(content).as_bytes(),
224 "lab content binding mismatch"
225 );
226 let mut columns: Vec<Vec<u8>> = vec![];
227 let mut deferred_kind = None;
228 let mut deferred_control = None;
229 let mut total = 0usize;
230 for (i, column) in frame.columns.into_iter().enumerate() {
231 total = total
232 .checked_add(column.raw_len as usize)
233 .context("column size overflow")?;
234 ensure!(total <= transforms::LIMIT, "expanded lab payload limit");
235 columns.push(if column.method == 9 {
236 ensure!(i == 1, "length transform on wrong column");
237 deferred_control = Some((column.data, column.raw_len as usize));
238 vec![]
239 } else if column.method == 7 {
240 ensure!(i == 7, "kind transform on wrong column");
241 deferred_kind = Some((column.data, column.raw_len as usize));
242 vec![]
243 } else if column.method == 6 {
244 ensure!(i == 8, "reference transform on wrong column");
245 ensure!(
246 deferred_control.is_none(),
247 "cyclic reference/control transforms"
248 );
249 context::join_references(column.data, &columns[1], column.raw_len as usize)?
250 } else if column.method == 5 {
251 ensure!(i == 1, "control transform on wrong column");
252 control::join(column.data, column.raw_len as usize)?
253 } else {
254 transforms::decode(column.method, column.data, column.raw_len as usize)?
255 });
256 }
257 if let Some((data, len)) = deferred_control {
258 let refs: [&[u8]; 10] = columns
259 .iter()
260 .map(Vec::as_slice)
261 .collect::<Vec<_>>()
262 .try_into()
263 .ok()
264 .context("column count")?;
265 let view = View::from_columns(frame.split_ids, frame.content_hash, refs);
266 let control = lengths::join(data, &view, content, len)?;
267 columns[1] = control;
268 }

Callers 2

measureFunction · 0.85

Calls 10

join_referencesFunction · 0.85
restore_kindsFunction · 0.85
extendMethod · 0.80
joinFunction · 0.50
decodeFunction · 0.50
contextMethod · 0.45
into_iterMethod · 0.45
pushMethod · 0.45
okMethod · 0.45
iterMethod · 0.45