1use crate::{
7 CompressionMethod, Error, ExtraHeaderId, HostSystem, add,
8 constants::{extra, signature, size, version},
9 invalid,
10 kind::{ExternalAttributes, UnixFileType},
11 record::{
12 Common, GeneralPurposeFlags, MemberHeader, MemberPayload, PayloadFields, SizeField,
13 validate_name,
14 },
15};
16
17const VERSION_MADE_BY: u16 = ((HostSystem::Unix.to_wire() as u16) << 8) | version::ZIP64;
21
22const DEFAULT_TIME: u16 = 0;
24const DEFAULT_DATE: u16 = 0x0021;
25
26#[derive(Clone, Copy, Debug)]
28pub enum EntryKind {
29 File { executable: bool },
31 Directory,
33 SymbolicLink,
35}
36
37pub struct PendingMember<'a> {
42 path: &'a str,
43 method: CompressionMethod,
44 kind: EntryKind,
45}
46
47impl<'a> PendingMember<'a> {
48 pub fn new(path: &'a str, method: CompressionMethod, kind: EntryKind) -> Result<Self, Error> {
53 let member = Self { path, method, kind };
54 if path.is_empty() || member.central_header_size() > usize::from(u16::MAX) {
55 return Err(invalid(0, "empty or oversized filename"));
56 }
57
58 validate_name(path, 0)?;
59 if path.contains('\\') {
60 return Err(invalid(0, "invalid ZIP filename"));
61 }
62 if path.ends_with('/') != matches!(kind, EntryKind::Directory) {
63 return Err(invalid(0, "filename suffix disagrees with member kind"));
64 }
65
66 if matches!(kind, EntryKind::Directory) && method != CompressionMethod::Stored {
67 return Err(invalid(0, "directories must be stored without file data"));
68 }
69
70 Ok(member)
71 }
72
73 pub fn method(&self) -> CompressionMethod {
75 self.method
76 }
77
78 pub fn local_header_size(&self) -> usize {
80 size::LOCAL + extra::HEADER_SIZE + extra::ZIP64_LOCAL_SIZE + self.path.len()
81 }
82
83 pub fn central_header_size(&self) -> usize {
86 size::CENTRAL + extra::HEADER_SIZE + extra::ZIP64_CENTRAL_SIZE + self.path.len()
87 }
88
89 pub fn finish(
91 self,
92 crc: u32,
93 compressed: u64,
94 uncompressed: u64,
95 offset: u64,
96 ) -> Result<CompletedMember<'a>, Error> {
97 let payload = PayloadFields {
98 crc,
99 compressed,
100 uncompressed,
101 }
102 .validate(self.method, offset)?;
103
104 if payload.has_empty_deflate_stream() {
106 return Err(invalid(offset, "empty member has file data"));
107 }
108
109 if matches!(self.kind, EntryKind::Directory) && uncompressed != 0 {
110 return Err(invalid(offset, "directory has file data"));
111 }
112
113 if matches!(self.kind, EntryKind::SymbolicLink) && uncompressed == 0 {
114 return Err(invalid(offset, "empty symbolic-link target"));
115 }
116
117 Ok(CompletedMember {
118 header: self,
119 payload,
120 offset,
121 })
122 }
123}
124
125pub struct CompletedMember<'a> {
127 header: PendingMember<'a>,
128 payload: MemberPayload,
129 offset: u64,
130}
131
132impl CompletedMember<'_> {
133 pub fn local_header(&self) -> Vec<u8> {
135 let mut bytes = Vec::with_capacity(self.header.local_header_size());
136 push32(&mut bytes, signature::LOCAL);
137 self.append_common(&mut bytes, extra::ZIP64_LOCAL_SIZE);
138 self.append_name_and_sizes(&mut bytes, extra::ZIP64_LOCAL_SIZE);
139 bytes
140 }
141
142 pub fn central_header(&self) -> Vec<u8> {
144 let mut bytes = Vec::new();
145 self.append_central_header(&mut bytes);
146 bytes
147 }
148
149 pub fn append_central_header(&self, bytes: &mut Vec<u8>) {
151 bytes.reserve(self.header.central_header_size());
152 push32(bytes, signature::CENTRAL);
153 push16(bytes, VERSION_MADE_BY);
154 self.append_common(bytes, extra::ZIP64_CENTRAL_SIZE);
155 push16(bytes, 0); push16(bytes, 0); push16(bytes, 0); let (file_type, permissions) = match self.header.kind {
160 EntryKind::File { executable: true } => (UnixFileType::Regular, 0o755),
161 EntryKind::File { executable: false } => (UnixFileType::Regular, 0o644),
162 EntryKind::Directory => (UnixFileType::Directory, 0o755),
163 EntryKind::SymbolicLink => (UnixFileType::SymbolicLink, 0o777),
164 };
165 let dos = if matches!(self.header.kind, EntryKind::Directory) {
166 ExternalAttributes::DOS_DIRECTORY
167 } else {
168 0
169 };
170 push32(
171 bytes,
172 (u32::from(u16::from(file_type) | permissions) << ExternalAttributes::UNIX_MODE_SHIFT)
173 | dos,
174 );
175 push32(bytes, u32::MAX);
176
177 self.append_name_and_sizes(bytes, extra::ZIP64_CENTRAL_SIZE);
178 push64(bytes, self.offset);
179 }
180
181 fn append_name_and_sizes(&self, bytes: &mut Vec<u8>, zip64_size: usize) {
182 bytes.extend_from_slice(self.header.path.as_bytes());
183 push16(bytes, u16::from(ExtraHeaderId::Zip64));
184 push16(bytes, zip64_size as u16);
185 push64(bytes, self.payload.fields().uncompressed);
186 push64(bytes, self.payload.fields().compressed);
187 }
188
189 fn append_common(&self, bytes: &mut Vec<u8>, zip64_size: usize) {
190 let header = MemberHeader {
191 version: version::ZIP64,
192 common: Common {
193 flags: GeneralPurposeFlags::UTF8,
194 method: self.header.method,
195 },
196 time: DEFAULT_TIME,
197 date: DEFAULT_DATE,
198 payload: PayloadFields {
199 crc: self.payload.fields().crc,
200 compressed: SizeField::Zip64,
201 uncompressed: SizeField::Zip64,
202 },
203 };
204 bytes.extend_from_slice(&header.to_bytes());
205 push16(bytes, self.header.path.len() as u16);
206 push16(bytes, (extra::HEADER_SIZE + zip64_size) as u16);
207 }
208}
209
210pub fn end_records(count: u64, offset: u64, size: u64) -> Result<Vec<u8>, Error> {
215 let position = add(offset, size)?;
216 let mut bytes = Vec::with_capacity(size::ZIP64_END + size::ZIP64_LOCATOR + size::END);
217
218 push32(&mut bytes, signature::ZIP64_END);
221 push64(&mut bytes, size::ZIP64_END_BODY as u64);
223 push16(&mut bytes, VERSION_MADE_BY);
224 push16(&mut bytes, version::ZIP64);
225 push32(&mut bytes, 0); push32(&mut bytes, 0); push64(&mut bytes, count); push64(&mut bytes, count); push64(&mut bytes, size);
230 push64(&mut bytes, offset);
231
232 push32(&mut bytes, signature::ZIP64_LOCATOR);
235 push32(&mut bytes, 0);
236 push64(&mut bytes, position);
237 push32(&mut bytes, 1); push32(&mut bytes, signature::END);
243 push16(&mut bytes, 0);
244 push16(&mut bytes, 0);
245 push16(&mut bytes, u16::MAX);
246 push16(&mut bytes, u16::MAX);
247 push32(&mut bytes, u32::MAX);
248 push32(&mut bytes, u32::MAX);
249 push16(&mut bytes, 0); Ok(bytes)
252}
253
254fn push16(bytes: &mut Vec<u8>, value: u16) {
255 bytes.extend_from_slice(&value.to_le_bytes());
256}
257
258fn push32(bytes: &mut Vec<u8>, value: u32) {
259 bytes.extend_from_slice(&value.to_le_bytes());
260}
261
262fn push64(bytes: &mut Vec<u8>, value: u64) {
263 bytes.extend_from_slice(&value.to_le_bytes());
264}