Streaming zstd decompress with a hard output ceiling. Reads at most `max_len + 1` bytes so an overrun is detected without buffering the whole bomb.
(bytes: &[u8], max_len: usize)
| 322 | /// `max_len + 1` bytes so an overrun is detected without buffering the whole |
| 323 | /// bomb. |
| 324 | fn decompress(bytes: &[u8], max_len: usize) -> Result<Vec<u8>, SyncError> { |
| 325 | let decoder = zstd::stream::read::Decoder::new(bytes).map_err(SyncError::Compression)?; |
| 326 | let mut out = Vec::new(); |
| 327 | let read = decoder |
| 328 | .take(max_len as u64 + 1) |
| 329 | .read_to_end(&mut out) |
| 330 | .map_err(SyncError::Compression)?; |
| 331 | if read > max_len { |
| 332 | return Err(SyncError::TooLarge { limit: max_len }); |
| 333 | } |
| 334 | Ok(out) |
| 335 | } |
| 336 | |
| 337 | #[cfg(test)] |
| 338 | mod tests { |