| | @@ -338,6 +338,40 @@ fn large_value(value: u32, extra: &mut &[u8]) -> Result<u64> { |
| 338 | 338 | *extra = &extra[8..]; |
| 339 | 339 | Ok(result) |
| 340 | 340 | } |
| 341 | +추가: fn expanded(value: u64, maximum: u64, large: u64) -> Result<u64> { |
| 342 | +추가: if value == maximum || value == large { |
| 343 | +추가: Ok(large) |
| 344 | +추가: } else { |
| 345 | +추가: Err(ErrorCode::Integrity) |
| 346 | +추가: } |
| 347 | +추가: } |
| 348 | +추가: fn disk_start(value: u16, extra: &mut &[u8]) -> Result<u32> { |
| 349 | +추가: if value != u16::MAX { |
| 350 | +추가: return Ok(value as u32); |
| 351 | +추가: } |
| 352 | +추가: if extra.len() < 4 { |
| 353 | +추가: return Err(ErrorCode::Integrity); |
| 354 | +추가: } |
| 355 | +추가: let result = u32at(extra, 0); |
| 356 | +추가: *extra = &extra[4..]; |
| 357 | +추가: Ok(result) |
| 358 | +추가: } |
| 359 | +추가: fn local_sizes(size: u32, compressed: u32, extra: Option<&[u8]>) -> Result<(u64, u64)> { |
| 360 | +추가: if let Some(extra) = extra { |
| 361 | +추가: // 로컬 ZIP64 추가 필드에는 두 크기가 함께 들어감. |
| 362 | +추가: if extra.len() < 16 { |
| 363 | +추가: return Err(ErrorCode::Integrity); |
| 364 | +추가: } |
| 365 | +추가: Ok(( |
| 366 | +추가: expanded(size as u64, u32::MAX as u64, u64at(extra, 0))?, |
| 367 | +추가: expanded(compressed as u64, u32::MAX as u64, u64at(extra, 8))?, |
| 368 | +추가: )) |
| 369 | +추가: } else if size == u32::MAX || compressed == u32::MAX { |
| 370 | +추가: Err(ErrorCode::Integrity) |
| 371 | +추가: } else { |
| 372 | +추가: Ok((size as u64, compressed as u64)) |
| 373 | +추가: } |
| 374 | +추가: } |
| 341 | 375 | fn zip( |
| 342 | 376 | source: &mut File, |
| 343 | 377 | len: u64, |
| | @@ -358,18 +392,16 @@ fn zip( |
| 358 | 392 | }) |
| 359 | 393 | .ok_or(ErrorCode::Integrity)?; |
| 360 | 394 | let h = &tail[end..]; |
| 361 | | -삭제: if u16at(h, 4) != 0 || u16at(h, 6) != 0 { |
| 362 | | -삭제: return Err(ErrorCode::UnsupportedFeature); |
| 363 | | -삭제: } |
| 395 | +추가: let mut disk = u16at(h, 4) as u64; |
| 396 | +추가: let mut central_disk = u16at(h, 6) as u64; |
| 397 | +추가: let mut entries_on_disk = u16at(h, 8) as u64; |
| 364 | 398 | let mut count = u16at(h, 10) as u64; |
| 365 | | -삭제: if count != u16at(h, 8) as u64 { |
| 366 | | -삭제: return Err(ErrorCode::UnsupportedFeature); |
| 367 | | -삭제: } |
| 368 | 399 | let mut central_size = u32at(h, 12) as u64; |
| 369 | 400 | let mut central = u32at(h, 16) as u64; |
| 370 | 401 | let mut central_end = tail_at + end as u64; |
| 371 | | -삭제: let required_zip64 = |
| 372 | | -삭제: count == u16::MAX as u64 || central_size == u32::MAX as u64 || central == u32::MAX as u64; |
| 402 | +추가: let required_zip64 = [disk, central_disk, entries_on_disk, count].contains(&(u16::MAX as u64)) |
| 403 | +추가: || central_size == u32::MAX as u64 |
| 404 | +추가: || central == u32::MAX as u64; |
| 373 | 405 | let locator = if central_end >= 20 { |
| 374 | 406 | bytes(source, central_end - 20, 20)? |
| 375 | 407 | } else { |
| | @@ -396,20 +428,17 @@ fn zip( |
| 396 | 428 | { |
| 397 | 429 | return Err(ErrorCode::Integrity); |
| 398 | 430 | } |
| 399 | | -삭제: if u32at(&large, 16) != 0 |
| 400 | | -삭제: || u32at(&large, 20) != 0 |
| 401 | | -삭제: || u64at(&large, 24) != u64at(&large, 32) |
| 402 | | -삭제: { |
| 403 | | -삭제: return Err(ErrorCode::UnsupportedFeature); |
| 404 | | -삭제: } |
| 405 | | -삭제: let (n, size, start) = (u64at(&large, 32), u64at(&large, 40), u64at(&large, 48)); |
| 406 | | -삭제: if (count != u16::MAX as u64 && count != n) |
| 407 | | -삭제: || (central_size != u32::MAX as u64 && central_size != size) |
| 408 | | -삭제: || (central != u32::MAX as u64 && central != start) |
| 409 | | -삭제: { |
| 410 | | -삭제: return Err(ErrorCode::Integrity); |
| 411 | | -삭제: } |
| 412 | | -삭제: (count, central_size, central, central_end) = (n, size, start, offset); |
| 431 | +추가: // 각 필드의 확장값을 해석한 뒤 단일 디스크 조건을 검사함. |
| 432 | +추가: disk = expanded(disk, u16::MAX as u64, u32at(&large, 16) as u64)?; |
| 433 | +추가: central_disk = expanded(central_disk, u16::MAX as u64, u32at(&large, 20) as u64)?; |
| 434 | +추가: entries_on_disk = expanded(entries_on_disk, u16::MAX as u64, u64at(&large, 24))?; |
| 435 | +추가: count = expanded(count, u16::MAX as u64, u64at(&large, 32))?; |
| 436 | +추가: central_size = expanded(central_size, u32::MAX as u64, u64at(&large, 40))?; |
| 437 | +추가: central = expanded(central, u32::MAX as u64, u64at(&large, 48))?; |
| 438 | +추가: central_end = offset; |
| 439 | +추가: } |
| 440 | +추가: if disk != 0 || central_disk != 0 || entries_on_disk != count { |
| 441 | +추가: return Err(ErrorCode::UnsupportedFeature); |
| 413 | 442 | } |
| 414 | 443 | if count > limits.entries as u64 { |
| 415 | 444 | return Err(ErrorCode::Limit); |
| | @@ -434,7 +463,7 @@ fn zip( |
| 434 | 463 | } |
| 435 | 464 | let flags = u16at(&h, 8); |
| 436 | 465 | let method = u16at(&h, 10); |
| 437 | | -삭제: if flags & !0x080f != 0 || !matches!(method, 0 | 8 | 99) || u16at(&h, 34) != 0 { |
| 466 | +추가: if flags & !0x080f != 0 || !matches!(method, 0 | 8 | 99) { |
| 438 | 467 | return Err(ErrorCode::UnsupportedFeature); |
| 439 | 468 | } |
| 440 | 469 | let crc = u32at(&h, 16); |
| | @@ -460,7 +489,7 @@ fn zip( |
| 460 | 489 | let size = large_value(u32at(&h, 24), &mut large)?; |
| 461 | 490 | let compressed = large_value(u32at(&h, 20), &mut large)?; |
| 462 | 491 | let local = large_value(u32at(&h, 42), &mut large)?; |
| 463 | | -삭제: if u16at(&h, 34) == 65535 && (large.len() < 4 || u32at(large, 0) != 0) { |
| 492 | +추가: if disk_start(u16at(&h, 34), &mut large)? != 0 { |
| 464 | 493 | return Err(ErrorCode::UnsupportedFeature); |
| 465 | 494 | } |
| 466 | 495 | let encrypted = flags & 1 != 0; |
| | @@ -522,17 +551,17 @@ fn zip( |
| 522 | 551 | return Err(ErrorCode::Integrity); |
| 523 | 552 | } |
| 524 | 553 | let (local_aes, local_large) = extra(&bytes(source, local + 30 + ln, le as usize)?)?; |
| 525 | | -삭제: if local_aes != aes { |
| 554 | +추가: if local_aes != aes || local_large.as_ref().is_some_and(|values| values.len() < 16) { |
| 526 | 555 | return Err(ErrorCode::Integrity); |
| 527 | 556 | } |
| 528 | 557 | let size_zip64 = u32at(&h, 20) == u32::MAX |
| 529 | 558 | || u32at(&h, 24) == u32::MAX |
| 530 | 559 | || u32at(&lh, 18) == u32::MAX |
| 531 | | -삭제: || u32at(&lh, 22) == u32::MAX; |
| 560 | +추가: || u32at(&lh, 22) == u32::MAX |
| 561 | +추가: || local_large.is_some(); |
| 532 | 562 | if flags & 8 == 0 { |
| 533 | | -삭제: let mut values = local_large.as_deref().unwrap_or(&[]); |
| 534 | | -삭제: let local_size = large_value(u32at(&lh, 22), &mut values)?; |
| 535 | | -삭제: let local_compressed = large_value(u32at(&lh, 18), &mut values)?; |
| 563 | +추가: let (local_size, local_compressed) = |
| 564 | +추가: local_sizes(u32at(&lh, 22), u32at(&lh, 18), local_large.as_deref())?; |
| 536 | 565 | if u32at(&lh, 14) != crc || local_size != size || local_compressed != compressed { |
| 537 | 566 | return Err(ErrorCode::Integrity); |
| 538 | 567 | } |