Decode a decompressed ATSC-v1 or predictive-v2 section directly into FileOps, using only this change's authored bytes. A 512-MiB payload/content cap and 1-GiB expanded allocation budget bound malformed counts and reference amplification.
(payload: &[u8], content: &[u8])
| 247 | /// authored bytes. A 512-MiB payload/content cap and 1-GiB expanded allocation |
| 248 | /// budget bound malformed counts and reference amplification. |
| 249 | pub fn decode(payload: &[u8], content: &[u8]) -> FormatResult<Vec<FileOps>> { |
| 250 | if payload.len() > MAX_PAYLOAD || content.len() > MAX_PAYLOAD { |
| 251 | return Err(super::super::FormatError::SemanticLimitExceeded); |
| 252 | } |
| 253 | let (c, predictive) = if payload.starts_with(b"ATSC\x02") { |
| 254 | let (c, p) = predictive::Control::parse(payload)?; |
| 255 | (c, Some(p)) |
| 256 | } else { |
| 257 | let bytes = payload |
| 258 | .strip_prefix(MAGIC) |
| 259 | .ok_or_else(|| invalid("unknown magic/version"))?; |
| 260 | let (c, rest): (BorrowedColumns<'_>, _) = postcard::take_from_bytes(bytes)?; |
| 261 | if !rest.is_empty() { |
| 262 | return Err(invalid("trailing payload bytes")); |
| 263 | } |
| 264 | (c, None) |
| 265 | }; |
| 266 | if c.content_hash != *blake3::hash(content).as_bytes() { |
| 267 | return Err(invalid("authored content binding mismatch")); |
| 268 | } |
| 269 | let mut d = Decoder { |
| 270 | c, |
| 271 | content, |
| 272 | branch_owner: Delta::default(), |
| 273 | branch_index: Delta::default(), |
| 274 | leaf_owner: Delta::default(), |
| 275 | leaf_index: Delta::default(), |
| 276 | old_number: Delta::default(), |
| 277 | new_number: Delta::default(), |
| 278 | position: Delta::default(), |
| 279 | reference: Delta::default(), |
| 280 | cursor: 0, |
| 281 | literal_end: 0, |
| 282 | remaining: MAX_EXPANDED, |
| 283 | predictive, |
| 284 | }; |
| 285 | d.charge(d.c.metadata.len(), 1)?; |
| 286 | let count = d.count::<FileOps>(roles::FILES)?; |
| 287 | let mut files = Vec::new(); |
| 288 | files |
| 289 | .try_reserve_exact(count) |
| 290 | .map_err(|_| invalid("file allocation failed"))?; |
| 291 | for _ in 0..count { |
| 292 | let ((trunk, path, op), rest): ((TrunkId, String, Option<TrunkOp>), _) = |
| 293 | postcard::take_from_bytes(d.c.metadata)?; |
| 294 | d.c.metadata = rest; |
| 295 | let mut file = FileOps::new(trunk, path, op); |
| 296 | let lines = d.count::<LineOps>(roles::LINES)?; |
| 297 | file.line_ops_mut() |
| 298 | .try_reserve_exact(lines) |
| 299 | .map_err(|_| invalid("line allocation failed"))?; |
| 300 | for _ in 0..lines { |
| 301 | file.add_line_op(d.line()?); |
| 302 | } |
| 303 | files.push(file); |
| 304 | } |
| 305 | if d.literal_end != d.c.literals.len() |
| 306 | || [ |