| 1 | use crate::protocol::ErrorCode; |
| 2 | |
| 3 | type Result<T> = std::result::Result<T, ErrorCode>; |
| 4 | |
| 5 | #[derive(Clone, Copy, PartialEq, Eq)] |
| 6 | pub(crate) enum Policy { |
| 7 | Streaming, |
| 8 | Recovery, |
| 9 | Names, |
| 10 | } |
| 11 | |
| 12 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 13 | pub(crate) struct AesExtra { |
| 14 | pub version: u16, |
| 15 | pub method: u16, |
| 16 | } |
| 17 | |
| 18 | #[derive(Default, Debug)] |
| 19 | pub(crate) struct Extra<'a> { |
| 20 | pub aes: Option<AesExtra>, |
| 21 | pub zip64: Option<&'a [u8]>, |
| 22 | pub unicode: Option<&'a [u8]>, |
| 23 | } |
| 24 | |
| 25 | fn u16at(bytes: &[u8], at: usize) -> u16 { |
| 26 | u16::from_le_bytes(bytes[at..at + 2].try_into().unwrap()) |
| 27 | } |
| 28 | fn u32at(bytes: &[u8], at: usize) -> u32 { |
| 29 | u32::from_le_bytes(bytes[at..at + 4].try_into().unwrap()) |
| 30 | } |
| 31 | fn u64at(bytes: &[u8], at: usize) -> u64 { |
| 32 | u64::from_le_bytes(bytes[at..at + 8].try_into().unwrap()) |
| 33 | } |
| 34 | |
| 35 | fn aes(value: &[u8]) -> Result<AesExtra> { |
| 36 | if value.len() < 7 { |
| 37 | return Err(ErrorCode::Integrity); |
| 38 | } |
| 39 | let version = u16at(value, 0); |
| 40 | let method = u16at(value, 5); |
| 41 | if !matches!(version, 1 | 2) |
| 42 | || &value[2..4] != b"AE" |
| 43 | || value[4] != 3 |
| 44 | || !matches!(method, 0 | 8) |
| 45 | { |
| 46 | return Err(ErrorCode::UnsupportedFeature); |
| 47 | } |
| 48 | Ok(AesExtra { version, method }) |
| 49 | } |
| 50 | |
| 51 | pub(crate) fn parse(bytes: &[u8], policy: Policy) -> Result<Extra<'_>> { |
| 52 | let mut found = Extra::default(); |
| 53 | let mut deferred_aes = None; |
| 54 | let mut duplicate_aes = false; |
| 55 | let mut at = 0; |
| 56 | while at < bytes.len() { |
| 57 | if bytes.len() - at < 4 { |
| 58 | return Err(ErrorCode::Integrity); |
| 59 | } |
| 60 | let tag = u16at(bytes, at); |
| 61 | let length = u16at(bytes, at + 2) as usize; |
| 62 | // 복구 경로는 확장 크기와 강한 암호화를 해석하지 않음. |
| 63 | if policy == Policy::Recovery && matches!(tag, 1 | 0x0017 | 0x0018) { |
| 64 | return Err(ErrorCode::UnsupportedFeature); |
| 65 | } |
| 66 | let end = at.checked_add(4 + length).ok_or(ErrorCode::Integrity)?; |
| 67 | let value = bytes.get(at + 4..end).ok_or(ErrorCode::Integrity)?; |
| 68 | match (policy, tag) { |
| 69 | (Policy::Streaming, 1) => { |
| 70 | if found.zip64.replace(value).is_some() { |
| 71 | return Err(ErrorCode::Integrity); |
| 72 | } |
| 73 | } |
| 74 | (Policy::Streaming, 0x9901) => { |
| 75 | if deferred_aes.is_some() { |
| 76 | duplicate_aes = true; |
| 77 | } else { |
| 78 | deferred_aes = Some(value); |
| 79 | } |
| 80 | } |
| 81 | (Policy::Names, 0x7075) => { |
| 82 | if found.unicode.is_some() || length < 5 { |
| 83 | return Err(ErrorCode::Integrity); |
| 84 | } |
| 85 | found.unicode = Some(value); |
| 86 | } |
| 87 | _ => {} |
| 88 | } |
| 89 | at = end; |
| 90 | } |
| 91 | // 스트리밍은 전체 필드 경계를 확인한 뒤 AES 지원 여부를 판단함. |
| 92 | if let Some(value) = deferred_aes { |
| 93 | found.aes = Some(aes(value)?); |
| 94 | if duplicate_aes { |
| 95 | return Err(ErrorCode::Integrity); |
| 96 | } |
| 97 | } |
| 98 | Ok(found) |
| 99 | } |
| 100 | |
| 101 | pub(crate) fn large_value(value: u32, extra: &mut &[u8]) -> Result<u64> { |
| 102 | if value != u32::MAX { |
| 103 | return Ok(value as u64); |
| 104 | } |
| 105 | if extra.len() < 8 { |
| 106 | return Err(ErrorCode::Integrity); |
| 107 | } |
| 108 | let result = u64at(extra, 0); |
| 109 | *extra = &extra[8..]; |
| 110 | Ok(result) |
| 111 | } |
| 112 | pub(crate) fn expanded(value: u64, maximum: u64, large: u64) -> Result<u64> { |
| 113 | if value == maximum || value == large { |
| 114 | Ok(large) |
| 115 | } else { |
| 116 | Err(ErrorCode::Integrity) |
| 117 | } |
| 118 | } |
| 119 | pub(crate) fn disk_start(value: u16, extra: &mut &[u8]) -> Result<u32> { |
| 120 | if value != u16::MAX { |
| 121 | return Ok(value as u32); |
| 122 | } |
| 123 | if extra.len() < 4 { |
| 124 | return Err(ErrorCode::Integrity); |
| 125 | } |
| 126 | let result = u32at(extra, 0); |
| 127 | *extra = &extra[4..]; |
| 128 | Ok(result) |
| 129 | } |
| 130 | pub(crate) fn local_sizes(size: u32, compressed: u32, extra: Option<&[u8]>) -> Result<(u64, u64)> { |
| 131 | if let Some(extra) = extra { |
| 132 | // 로컬 ZIP64 추가 필드에는 두 크기가 함께 들어감. |
| 133 | if extra.len() < 16 { |
| 134 | return Err(ErrorCode::Integrity); |
| 135 | } |
| 136 | Ok(( |
| 137 | expanded(size as u64, u32::MAX as u64, u64at(extra, 0))?, |
| 138 | expanded(compressed as u64, u32::MAX as u64, u64at(extra, 8))?, |
| 139 | )) |
| 140 | } else if size == u32::MAX || compressed == u32::MAX { |
| 141 | Err(ErrorCode::Integrity) |
| 142 | } else { |
| 143 | Ok((size as u64, compressed as u64)) |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | #[cfg(test)] |
| 148 | #[path = "../../../tests/rust-archive-worker/zip_extra.rs"] |
| 149 | mod tests; |