| 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 | finish_directory(&mut output, ¢ral, plan.items.len(), have_zip64, check)?; |
| 292 | plan.verify(check)?; |
| 293 | let size = io(output.stream_position())?; |
| 294 | io(output.rewind())?; |
| 295 | Ok((output, size)) |
| 296 | } |
| 297 | fn finish_directory( |
| 298 | output: &mut File, |
| 299 | central: &[u8], |
| 300 | count: usize, |
| 301 | have_zip64: bool, |
| 302 | check: &mut impl crate::resources::Monitor, |
| 303 | ) -> Result<()> { |
| 304 | check.check()?; |
| 305 | let at = io(output.stream_position())?; |
| 306 | crate::space::ensure(&output, central.len() as u64 + 98)?; |
| 307 | if !central.is_empty() { |
| 308 | crate::resources::completed(io(output.write_all(¢ral)), check)?; |
| 309 | } |
| 310 | let large_at = io(output.stream_position())?; |
| 311 | let mut end = Vec::new(); |
| 312 | if have_zip64 |
| 313 | || count >= u16::MAX as usize |
| 314 | || at >= u32::MAX as u64 |
| 315 | || central.len() >= u32::MAX as usize |
| 316 | { |
| 317 | end.extend(b"PK\x06\x06"); |
| 318 | put64(&mut end, 44); |
| 319 | put16(&mut end, 45); |
| 320 | put16(&mut end, 45); |
| 321 | put32(&mut end, 0); |
| 322 | put32(&mut end, 0); |
| 323 | put64(&mut end, count as u64); |
| 324 | put64(&mut end, count as u64); |
| 325 | put64(&mut end, central.len() as u64); |
| 326 | put64(&mut end, at); |
| 327 | end.extend(b"PK\x06\x07"); |
| 328 | put32(&mut end, 0); |
| 329 | put64(&mut end, large_at); |
| 330 | put32(&mut end, 1); |
| 331 | end.extend(b"PK\x05\x06"); |
| 332 | put16(&mut end, 0); |
| 333 | put16(&mut end, 0); |
| 334 | put16(&mut end, u16::MAX); |
| 335 | put16(&mut end, u16::MAX); |
| 336 | put32(&mut end, u32::MAX); |
| 337 | put32(&mut end, u32::MAX); |
| 338 | put16(&mut end, 0); |
| 339 | } else { |
| 340 | end.extend(b"PK\x05\x06"); |
| 341 | put16(&mut end, 0); |
| 342 | put16(&mut end, 0); |
| 343 | put16(&mut end, count as u16); |
| 344 | put16(&mut end, count as u16); |
| 345 | put32(&mut end, central.len() as u32); |
| 346 | put32(&mut end, at as u32); |
| 347 | put16(&mut end, 0); |
| 348 | } |
| 349 | crate::resources::completed(io(output.write_all(&end)), check)?; |
| 350 | Ok(()) |
| 351 | } |
| 352 | |
| 353 | pub(crate) fn recovered_zip( |
| 354 | findings: &[RecoveryEntry], |
| 355 | offsets: &[u64], |
| 356 | input: &mut File, |
| 357 | check: &mut impl crate::resources::Monitor, |
| 358 | ) -> Result<(File, u64)> { |
| 359 | if findings.len() != offsets.len() { |
| 360 | return Err(ErrorCode::Internal); |
| 361 | } |
| 362 | let mut output = spool::temporary()?; |
| 363 | let mut central = Vec::new(); |
| 364 | let mut metadata = crate::archive::MetadataBudget::default(); |
| 365 | let mut count = 0; |
| 366 | let mut wide = false; |
| 367 | let mut buffer = [0; 65536]; |
| 368 | for (entry, offset) in findings.iter().zip(offsets) { |
| 369 | if entry.status != RecoveryStatus::Verified { |
| 370 | continue; |
| 371 | } |
| 372 | check.check()?; |
| 373 | let directory = entry.kind == Some(EntryKind::Directory); |
| 374 | let name = entry.name.as_ref().ok_or(ErrorCode::Internal)?; |
| 375 | metadata.charge_with(name, check.metadata_limits())?; |
| 376 | let name = if directory { |
| 377 | format!("{name}/") |
| 378 | } else { |
| 379 | name.clone() |
| 380 | }; |
| 381 | let size = entry.verified_size.ok_or(ErrorCode::Internal)?; |
| 382 | io(input.seek(std::io::SeekFrom::Start(*offset)))?; |
| 383 | let mut left = size; |
| 384 | let mut crc = 0; |
| 385 | while left != 0 { |
| 386 | check.check()?; |
| 387 | let n = io(input.read(&mut buffer[..left.min(65536) as usize]))?; |
| 388 | if n == 0 { |
| 389 | return Err(ErrorCode::Internal); |
| 390 | } |
| 391 | crc = crc32(crc, &buffer[..n]); |
| 392 | left -= n as u64; |
| 393 | crate::resources::completed(Ok(()), check)?; |
| 394 | } |
| 395 | let at = io(output.stream_position())?; |
| 396 | let big = size >= u32::MAX as u64; |
| 397 | wide |= big || at >= u32::MAX as u64; |
| 398 | let ext = zip64_extra(size, size, None); |
| 399 | let mut header = b"PK\x03\x04".to_vec(); |
| 400 | for value in [if big { 45 } else { 20 }, 0x0800, 0, 0, 33] { |
| 401 | put16(&mut header, value); |
| 402 | } |
| 403 | put32(&mut header, crc); |
| 404 | for _ in 0..2 { |
| 405 | put32(&mut header, if big { u32::MAX } else { size as u32 }); |
| 406 | } |
| 407 | put16(&mut header, name.len() as u16); |
| 408 | put16(&mut header, ext.len() as u16); |
| 409 | header.extend(name.as_bytes()); |
| 410 | header.extend(ext); |
| 411 | crate::space::ensure( |
| 412 | &output, |
| 413 | size.checked_add(header.len() as u64) |
| 414 | .ok_or(ErrorCode::Limit)?, |
| 415 | )?; |
| 416 | crate::resources::completed(io(output.write_all(&header)), check)?; |
| 417 | io(input.seek(std::io::SeekFrom::Start(*offset)))?; |
| 418 | let mut left = size; |
| 419 | while left != 0 { |
| 420 | check.check()?; |
| 421 | let n = io(input.read(&mut buffer[..left.min(65536) as usize]))?; |
| 422 | if n == 0 { |
| 423 | return Err(ErrorCode::Internal); |
| 424 | } |
| 425 | crate::space::ensure(&output, n as u64)?; |
| 426 | crate::resources::completed(io(output.write_all(&buffer[..n])), check)?; |
| 427 | left -= n as u64; |
| 428 | } |
| 429 | let ext = zip64_extra(size, size, Some(at)); |
| 430 | central.extend(b"PK\x01\x02"); |
| 431 | for value in [ |
| 432 | 0x032d, |
| 433 | if big || at >= u32::MAX as u64 { 45 } else { 20 }, |
| 434 | 0x0800, |
| 435 | 0, |
| 436 | 0, |
| 437 | 33, |
| 438 | ] { |
| 439 | put16(&mut central, value); |
| 440 | } |
| 441 | put32(&mut central, crc); |
| 442 | for _ in 0..2 { |
| 443 | put32(&mut central, if big { u32::MAX } else { size as u32 }); |
| 444 | } |
| 445 | put16(&mut central, name.len() as u16); |
| 446 | put16(&mut central, ext.len() as u16); |
| 447 | for _ in 0..3 { |
| 448 | put16(&mut central, 0); |
| 449 | } |
| 450 | put32( |
| 451 | &mut central, |
| 452 | if directory { |
| 453 | (0o40700 << 16) | 0x10 |
| 454 | } else { |
| 455 | 0o100600 << 16 |
| 456 | }, |
| 457 | ); |
| 458 | put32( |
| 459 | &mut central, |
| 460 | if at >= u32::MAX as u64 { |
| 461 | u32::MAX |
| 462 | } else { |
| 463 | at as u32 |
| 464 | }, |
| 465 | ); |
| 466 | central.extend(name.as_bytes()); |
| 467 | central.extend(ext); |
| 468 | count += 1; |
| 469 | } |
| 470 | finish_directory(&mut output, ¢ral, count, wide, check)?; |
| 471 | let len = io(output.stream_position())?; |
| 472 | io(output.rewind())?; |
| 473 | Ok((output, len)) |
| 474 | } |
| 475 | |
| 476 | #[cfg(test)] |
| 477 | #[path = "../../../tests/rust-archive-worker/create_zip_fields.rs"] |
| 478 | mod tests; |