Sniff the first 4 bytes to determine if `blob` is a plaintext (`NDAS`) or encrypted (`SEGA`) segment. Returns `true` if encrypted. Returns an error if the magic is neither `NDAS` nor `SEGA`.
(blob: &[u8])
| 49 | /// |
| 50 | /// Returns an error if the magic is neither `NDAS` nor `SEGA`. |
| 51 | pub(crate) fn detect_encryption(blob: &[u8]) -> Result<bool, ArrayError> { |
| 52 | if blob.len() < 4 { |
| 53 | return Err(ArrayError::SegmentCorruption { |
| 54 | detail: format!("segment too short to sniff magic: {} bytes", blob.len()), |
| 55 | }); |
| 56 | } |
| 57 | let magic = &blob[..4]; |
| 58 | if magic == NDAS_MAGIC { |
| 59 | Ok(false) |
| 60 | } else if magic == SEGA_MAGIC { |
| 61 | Ok(true) |
| 62 | } else { |
| 63 | Err(ArrayError::SegmentCorruption { |
| 64 | detail: format!( |
| 65 | "unrecognised segment magic: {magic:02x?} \ |
| 66 | (expected NDAS or SEGA)" |
| 67 | ), |
| 68 | }) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | #[cfg(test)] |
| 73 | mod tests { |