team-ljyzipelo

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