| 1 | use crate::{protocol::*, spool}; |
| 2 | use std::{ |
| 3 | fs::File, |
| 4 | io::{Read, Seek, Write}, |
| 5 | }; |
| 6 | use zeroize::Zeroizing; |
| 7 | use zlib_rs::{Deflate, DeflateFlush, Status, crc32::crc32}; |
| 8 | |
| 9 | type Result<T> = std::result::Result<T, ErrorCode>; |
| 10 | fn io<T>(result: std::io::Result<T>) -> Result<T> { |
| 11 | result.map_err(crate::space::io_error) |
| 12 | } |
| 13 | fn put16(out: &mut Vec<u8>, value: u16) { |
| 14 | out.extend(value.to_le_bytes()); |
| 15 | } |
| 16 | fn put32(out: &mut Vec<u8>, value: u32) { |
| 17 | out.extend(value.to_le_bytes()); |
| 18 | } |
| 19 | fn put64(out: &mut Vec<u8>, value: u64) { |
| 20 | out.extend(value.to_le_bytes()); |
| 21 | } |
| 22 | |
| 23 | fn zip64_extra(size: u64, packed: u64, offset: Option<u64>) -> Vec<u8> { |
| 24 | let mut values = Vec::new(); |
| 25 | if size >= u32::MAX as u64 || packed >= u32::MAX as u64 { |
| 26 | put64(&mut values, size); |
| 27 | put64(&mut values, packed); |
| 28 | } |
| 29 | if let Some(offset) = offset.filter(|n| *n >= u32::MAX as u64) { |
| 30 | put64(&mut values, offset); |
| 31 | } |
| 32 | let mut extra = Vec::new(); |
| 33 | if !values.is_empty() { |
| 34 | put16(&mut extra, 1); |
| 35 | put16(&mut extra, values.len() as u16); |
| 36 | extra.extend(values); |
| 37 | } |
| 38 | extra |
| 39 | } |
| 40 | fn pack( |
| 41 | input: &mut File, |
| 42 | length: u64, |
| 43 | method: Method, |
| 44 | output: &mut File, |
| 45 | check: &mut impl crate::resources::Monitor, |
| 46 | ) -> Result<(u64, u32)> { |
| 47 | io(input.rewind())?; |
| 48 | let mut input = input.take(length); |
| 49 | let mut buffer = Zeroizing::new([0; 65536]); |
| 50 | let mut encoded = Zeroizing::new([0; 65536]); |
| 51 | let mut crc = 0; |
| 52 | let mut size = 0u64; |
| 53 | if method == Method::Store { |
| 54 | loop { |
| 55 | check.check()?; |
| 56 | let n = io(input.read(&mut buffer[..]))?; |
| 57 | if n == 0 { |
| 58 | break; |
| 59 | } |
| 60 | crate::resources::completed(Ok(()), check)?; |
| 61 | crate::space::ensure(output, n as u64)?; |
| 62 | crc = crc32(crc, &buffer[..n]); |
| 63 | io(output.write_all(&buffer[..n]))?; |
| 64 | size += n as u64; |
| 65 | crate::resources::completed(Ok(()), check)?; |
| 66 | } |
| 67 | if size != length { |
| 68 | return Err(ErrorCode::SourceChanged); |
| 69 | } |
| 70 | return Ok((size, crc)); |
| 71 | } |
| 72 | let mut stream = Deflate::new(6, false, 15); |
| 73 | loop { |
| 74 | check.check()?; |
| 75 | let n = io(input.read(&mut buffer[..]))?; |
| 76 | crc = crc32(crc, &buffer[..n]); |
| 77 | let mut used = 0; |
| 78 | loop { |
| 79 | check.check()?; |
| 80 | let a = stream.total_in(); |
| 81 | let b = stream.total_out(); |
| 82 | let status = stream |
| 83 | .compress( |
| 84 | &buffer[used..n], |
| 85 | &mut encoded[..], |
| 86 | if n == 0 { |
| 87 | DeflateFlush::Finish |
| 88 | } else { |
| 89 | DeflateFlush::NoFlush |
| 90 | }, |
| 91 | ) |
| 92 | .map_err(|_| ErrorCode::Internal)?; |
| 93 | used += (stream.total_in() - a) as usize; |
| 94 | let written = (stream.total_out() - b) as usize; |
| 95 | crate::space::ensure(output, written as u64)?; |
| 96 | io(output.write_all(&encoded[..written]))?; |
| 97 | if used != 0 || written != 0 { |
| 98 | crate::resources::completed(Ok(()), check)?; |
| 99 | } |
| 100 | size = size.checked_add(written as u64).ok_or(ErrorCode::Limit)?; |
| 101 | if status == Status::StreamEnd { |
| 102 | if stream.total_in() != length { |
| 103 | return Err(ErrorCode::SourceChanged); |
| 104 | } |
| 105 | return Ok((size, crc)); |
| 106 | } |
| 107 | if used == n && n != 0 { |
| 108 | break; |
| 109 | } |
| 110 | if a == stream.total_in() && b == stream.total_out() { |
| 111 | return Err(ErrorCode::Internal); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | pub(crate) use crate::create_sources::Plan; |
| 117 | pub(crate) fn plan( |
| 118 | sources: &[Source], |
| 119 | limits: Limits, |
| 120 | check: &mut impl crate::resources::Monitor, |
| 121 | ) -> Result<Plan> { |
| 122 | Plan::new(sources, limits, check) |
| 123 | } |
| 124 | pub(crate) fn zip( |
| 125 | plan: &Plan, |
| 126 | method: Method, |
| 127 | limits: Limits, |
| 128 | password: Option<&[u8]>, |
| 129 | check: &mut impl crate::resources::Monitor, |
| 130 | ) -> Result<(File, u64)> { |
| 131 | let mut output = spool::temporary()?; |
| 132 | let mut central = Vec::new(); |
| 133 | let mut total = 0u64; |
| 134 | let mut have_zip64 = false; |
| 135 | for source in &plan.items { |
| 136 | let member = &source.member; |
| 137 | let directory = member.entry.kind == EntryKind::Directory; |
| 138 | let method = if directory { Method::Store } else { method }; |
| 139 | let password = if directory { None } else { password }; |
| 140 | check.check()?; |
| 141 | let remaining = limits |
| 142 | .input |
| 143 | .min(limits.output) |
| 144 | .checked_sub(total) |
| 145 | .ok_or(ErrorCode::Limit)?; |
| 146 | let mut input = crate::resources::completed(source.open(), check)?; |
| 147 | let mut snapshot = if directory { |
| 148 | None |
| 149 | } else { |
| 150 | Some(spool::Snapshot::capture(&mut input.file, remaining, check)?) |
| 151 | }; |
| 152 | let length = snapshot.as_ref().map_or(0, |s| s.len); |
| 153 | total = total |
| 154 | .checked_add(length) |
| 155 | .filter(|n| *n <= limits.output) |
| 156 | .ok_or(ErrorCode::Limit)?; |
| 157 | // 압축 결과의 보수적 상한과 ZIP에 복사할 공간을 함께 확인함. |
| 158 | let bound = length |
| 159 | .checked_add(length / 16383 * 5) |
| 160 | .and_then(|n| n.checked_add(131072)) |
| 161 | .ok_or(ErrorCode::Limit)?; |
| 162 | crate::space::ensure(&output, bound.checked_mul(2).ok_or(ErrorCode::Limit)?)?; |
| 163 | let mut packed = spool::temporary()?; |
| 164 | let (packed_size, crc) = if let Some(snapshot) = snapshot.as_mut() { |
| 165 | pack(&mut snapshot.file, length, method, &mut packed, check)? |
| 166 | } else { |
| 167 | (0, 0) |
| 168 | }; |
| 169 | let packed_size = packed_size |
| 170 | .checked_add(if password.is_some() { 28 } else { 0 }) |
| 171 | .ok_or(ErrorCode::Limit)?; |
| 172 | let offset = io(output.stream_position())?; |
| 173 | let zip_name = if directory { |
| 174 | format!("{}/", member.entry.name) |
| 175 | } else { |
| 176 | member.entry.name.clone() |
| 177 | }; |
| 178 | let name = zip_name.as_bytes(); |
| 179 | let real_method = if method == Method::Store { 0 } else { 8 }; |
| 180 | let flags = if password.is_some() { 0x0801 } else { 0x0800 }; |
| 181 | let wide_sizes = length >= u32::MAX as u64 || packed_size >= u32::MAX as u64; |
| 182 | let wide_offset = offset >= u32::MAX as u64; |
| 183 | have_zip64 |= wide_sizes || wide_offset; |
| 184 | let version = if password.is_some() { |
| 185 | 51 |
| 186 | } else if wide_sizes || wide_offset { |
| 187 | 45 |
| 188 | } else { |
| 189 | 20 |
| 190 | }; |
| 191 | let method = if password.is_some() { 99 } else { real_method }; |
| 192 | let crc = if password.is_some() { 0 } else { crc }; |
| 193 | let mut aes = Vec::new(); |
| 194 | if password.is_some() { |
| 195 | put16(&mut aes, 0x9901); |
| 196 | put16(&mut aes, 7); |
| 197 | put16(&mut aes, 2); |
| 198 | aes.extend(b"AE"); |
| 199 | aes.push(3); |
| 200 | put16(&mut aes, real_method); |
| 201 | } |
| 202 | let mut extra = zip64_extra(length, packed_size, None); |
| 203 | extra.extend(&aes); |
| 204 | let mut header = Vec::new(); |
| 205 | header.extend(b"PK\x03\x04"); |
| 206 | for v in [version, flags, method, 0, 33] { |
| 207 | put16(&mut header, v); |
| 208 | } |
| 209 | put32(&mut header, crc); |
| 210 | put32( |
| 211 | &mut header, |
| 212 | if wide_sizes { |
| 213 | u32::MAX |
| 214 | } else { |
| 215 | packed_size as u32 |
| 216 | }, |
| 217 | ); |
| 218 | put32( |
| 219 | &mut header, |
| 220 | if wide_sizes { u32::MAX } else { length as u32 }, |
| 221 | ); |
| 222 | put16(&mut header, name.len() as u16); |
| 223 | put16(&mut header, extra.len() as u16); |
| 224 | header.extend(name); |
| 225 | header.extend(extra); |
| 226 | io(output.write_all(&header))?; |
| 227 | crate::resources::completed(Ok(()), check)?; |
| 228 | io(packed.rewind())?; |
| 229 | if let Some(password) = password { |
| 230 | crate::crypto::aes_encrypt_file( |
| 231 | &mut packed, |
| 232 | packed_size - 28, |
| 233 | &mut output, |
| 234 | password, |
| 235 | check, |
| 236 | )?; |
| 237 | } else { |
| 238 | let mut buffer = Zeroizing::new([0; 65536]); |
| 239 | loop { |
| 240 | check.check()?; |
| 241 | let n = io(packed.read(&mut buffer[..]))?; |
| 242 | if n == 0 { |
| 243 | break; |
| 244 | } |
| 245 | crate::space::ensure(&output, n as u64)?; |
| 246 | io(output.write_all(&buffer[..n]))?; |
| 247 | crate::resources::completed(Ok(()), check)?; |
| 248 | } |
| 249 | } |
| 250 | let mut extra = zip64_extra(length, packed_size, Some(offset)); |
| 251 | extra.extend(&aes); |
| 252 | central.extend(b"PK\x01\x02"); |
| 253 | put16(&mut central, 0x032d); |
| 254 | for v in [version, flags, method, 0, 33] { |
| 255 | put16(&mut central, v); |
| 256 | } |
| 257 | put32(&mut central, crc); |
| 258 | put32( |
| 259 | &mut central, |
| 260 | if wide_sizes { |
| 261 | u32::MAX |
| 262 | } else { |
| 263 | packed_size as u32 |
| 264 | }, |
| 265 | ); |
| 266 | put32( |
| 267 | &mut central, |
| 268 | if wide_sizes { u32::MAX } else { length as u32 }, |
| 269 | ); |
| 270 | put16(&mut central, name.len() as u16); |
| 271 | put16(&mut central, extra.len() as u16); |
| 272 | for _ in 0..3 { |
| 273 | put16(&mut central, 0); |
| 274 | } |
| 275 | put32( |
| 276 | &mut central, |
| 277 | if directory { |
| 278 | (0o40700 << 16) | 0x10 |
| 279 | } else { |
| 280 | 0o100600 << 16 |
| 281 | }, |
| 282 | ); |
| 283 | put32( |
| 284 | &mut central, |
| 285 | if wide_offset { u32::MAX } else { offset as u32 }, |
| 286 | ); |
| 287 | central.extend(name); |
| 288 | central.extend(extra); |
| 289 | crate::resources::completed(Ok(()), check)?; |
| 290 | } |
| 291 | check.check()?; |
| 292 | let at = io(output.stream_position())?; |
| 293 | crate::space::ensure(&output, central.len() as u64 + 98)?; |
| 294 | if !central.is_empty() { |
| 295 | crate::resources::completed(io(output.write_all(¢ral)), check)?; |
| 296 | } |
| 297 | let large_at = io(output.stream_position())?; |
| 298 | let mut end = Vec::new(); |
| 299 | if have_zip64 |
| 300 | || plan.items.len() >= u16::MAX as usize |
| 301 | || at >= u32::MAX as u64 |
| 302 | || central.len() >= u32::MAX as usize |
| 303 | { |
| 304 | end.extend(b"PK\x06\x06"); |
| 305 | put64(&mut end, 44); |
| 306 | put16(&mut end, 45); |
| 307 | put16(&mut end, 45); |
| 308 | put32(&mut end, 0); |
| 309 | put32(&mut end, 0); |
| 310 | put64(&mut end, plan.items.len() as u64); |
| 311 | put64(&mut end, plan.items.len() as u64); |
| 312 | put64(&mut end, central.len() as u64); |
| 313 | put64(&mut end, at); |
| 314 | end.extend(b"PK\x06\x07"); |
| 315 | put32(&mut end, 0); |
| 316 | put64(&mut end, large_at); |
| 317 | put32(&mut end, 1); |
| 318 | end.extend(b"PK\x05\x06"); |
| 319 | put16(&mut end, 0); |
| 320 | put16(&mut end, 0); |
| 321 | put16(&mut end, u16::MAX); |
| 322 | put16(&mut end, u16::MAX); |
| 323 | put32(&mut end, u32::MAX); |
| 324 | put32(&mut end, u32::MAX); |
| 325 | put16(&mut end, 0); |
| 326 | } else { |
| 327 | end.extend(b"PK\x05\x06"); |
| 328 | put16(&mut end, 0); |
| 329 | put16(&mut end, 0); |
| 330 | put16(&mut end, plan.items.len() as u16); |
| 331 | put16(&mut end, plan.items.len() as u16); |
| 332 | put32(&mut end, central.len() as u32); |
| 333 | put32(&mut end, at as u32); |
| 334 | put16(&mut end, 0); |
| 335 | } |
| 336 | crate::resources::completed(io(output.write_all(&end)), check)?; |
| 337 | plan.verify(check)?; |
| 338 | let size = io(output.stream_position())?; |
| 339 | io(output.rewind())?; |
| 340 | Ok((output, size)) |
| 341 | } |
| 342 | |
| 343 | #[cfg(test)] |
| 344 | #[path = "../../../tests/rust-archive-worker/create_zip_fields.rs"] |
| 345 | mod tests; |