| | @@ -1,6 +1,6 @@ |
| 1 | 1 | use crate::protocol::*; |
| 2 | 2 | use std::collections::{BTreeMap, BTreeSet}; |
| 3 | | -삭제: use zlib_rs::{Deflate, DeflateFlush, Inflate, InflateFlush, Status, crc32::crc32}; |
| 3 | +추가: use zlib_rs::{Deflate, DeflateFlush, Status, crc32::crc32}; |
| 4 | 4 | |
| 5 | 5 | type Result<T> = std::result::Result<T, ErrorCode>; |
| 6 | 6 | pub const MAX_PATH_DEPTH: usize = 64; |
| | @@ -56,9 +56,6 @@ impl MetadataBudget { |
| 56 | 56 | pub(crate) fn charge_bytes(&mut self, bytes: usize, limits: MetadataLimits) -> Result<()> { |
| 57 | 57 | self.charge_cost(bytes, 0, limits) |
| 58 | 58 | } |
| 59 | | -삭제: pub(crate) fn charge(&mut self, name: &str) -> Result<()> { |
| 60 | | -삭제: self.charge_with(name, MetadataLimits::default()) |
| 61 | | -삭제: } |
| 62 | 59 | pub(crate) fn charge_with(&mut self, name: &str, limits: MetadataLimits) -> Result<()> { |
| 63 | 60 | // 이름 복제와 인덱스 노드 비용을 할당 전에 계산함. |
| 64 | 61 | let cost = name |
| | @@ -90,17 +87,6 @@ pub struct Catalog { |
| 90 | 87 | pub format: Format, |
| 91 | 88 | pub members: Vec<Member>, |
| 92 | 89 | } |
| 93 | | -삭제: fn part(b: &[u8], at: usize, n: usize) -> Result<&[u8]> { |
| 94 | | -삭제: b.get(at..at.checked_add(n).ok_or(ErrorCode::Integrity)?) |
| 95 | | -삭제: .ok_or(ErrorCode::Integrity) |
| 96 | | -삭제: } |
| 97 | | -삭제: fn u16at(b: &[u8], at: usize) -> Result<u16> { |
| 98 | | -삭제: Ok(u16::from_le_bytes(part(b, at, 2)?.try_into().unwrap())) |
| 99 | | -삭제: } |
| 100 | | -삭제: fn u32at(b: &[u8], at: usize) -> Result<u32> { |
| 101 | | -삭제: Ok(u32::from_le_bytes(part(b, at, 4)?.try_into().unwrap())) |
| 102 | | -삭제: } |
| 103 | | -삭제: |
| 104 | 90 | pub fn valid_name(name: &str, directory: bool) -> Result<String> { |
| 105 | 91 | let name = if directory { |
| 106 | 92 | name.strip_suffix('/').unwrap_or(name) |
| | @@ -218,61 +204,6 @@ fn bounded_append(output: &mut Vec<u8>, data: &[u8], limit: u64) -> Result<()> { |
| 218 | 204 | output.extend_from_slice(data); |
| 219 | 205 | Ok(()) |
| 220 | 206 | } |
| 221 | | -삭제: fn inflate( |
| 222 | | -삭제: input: &[u8], |
| 223 | | -삭제: gzip: bool, |
| 224 | | -삭제: limit: u64, |
| 225 | | -삭제: check: &mut impl crate::resources::Monitor, |
| 226 | | -삭제: ) -> Result<(Vec<u8>, usize)> { |
| 227 | | -삭제: let mut stream = Inflate::new(gzip, if gzip { 31 } else { 15 }); |
| 228 | | -삭제: let mut out = Vec::new(); |
| 229 | | -삭제: let mut buffer = [0; 65536]; |
| 230 | | -삭제: loop { |
| 231 | | -삭제: check.check()?; |
| 232 | | -삭제: let before_in = stream.total_in(); |
| 233 | | -삭제: let before_out = stream.total_out(); |
| 234 | | -삭제: let status = stream |
| 235 | | -삭제: .decompress( |
| 236 | | -삭제: &input[before_in as usize..], |
| 237 | | -삭제: &mut buffer, |
| 238 | | -삭제: InflateFlush::NoFlush, |
| 239 | | -삭제: ) |
| 240 | | -삭제: .map_err(|_| ErrorCode::Integrity)?; |
| 241 | | -삭제: let written = (stream.total_out() - before_out) as usize; |
| 242 | | -삭제: bounded_append(&mut out, &buffer[..written], limit)?; |
| 243 | | -삭제: if status == Status::StreamEnd { |
| 244 | | -삭제: return Ok((out, stream.total_in() as usize)); |
| 245 | | -삭제: } |
| 246 | | -삭제: if stream.total_in() == before_in && stream.total_out() == before_out { |
| 247 | | -삭제: return Err(ErrorCode::Integrity); |
| 248 | | -삭제: } |
| 249 | | -삭제: } |
| 250 | | -삭제: } |
| 251 | | -삭제: fn gzip( |
| 252 | | -삭제: input: &[u8], |
| 253 | | -삭제: limits: Limits, |
| 254 | | -삭제: check: &mut impl crate::resources::Monitor, |
| 255 | | -삭제: ) -> Result<Vec<u8>> { |
| 256 | | -삭제: let mut at = 0; |
| 257 | | -삭제: let mut result = Vec::new(); |
| 258 | | -삭제: while at < input.len() { |
| 259 | | -삭제: if !input[at..].starts_with(&[31, 139, 8]) { |
| 260 | | -삭제: return Err(ErrorCode::Integrity); |
| 261 | | -삭제: } |
| 262 | | -삭제: let (out, used) = inflate( |
| 263 | | -삭제: &input[at..], |
| 264 | | -삭제: true, |
| 265 | | -삭제: limits.output - result.len() as u64, |
| 266 | | -삭제: check, |
| 267 | | -삭제: )?; |
| 268 | | -삭제: if used == 0 { |
| 269 | | -삭제: return Err(ErrorCode::Integrity); |
| 270 | | -삭제: } |
| 271 | | -삭제: bounded_append(&mut result, &out, limits.output)?; |
| 272 | | -삭제: at += used; |
| 273 | | -삭제: } |
| 274 | | -삭제: Ok(result) |
| 275 | | -삭제: } |
| 276 | 207 | pub(crate) fn tar_string(b: &[u8]) -> Result<String> { |
| 277 | 208 | let n = b.iter().position(|v| *v == 0).unwrap_or(b.len()); |
| 278 | 209 | if b[n..].iter().any(|v| *v != 0) { |
| | @@ -294,98 +225,6 @@ pub(crate) fn octal(b: &[u8]) -> Result<u64> { |
| 294 | 225 | } |
| 295 | 226 | u64::from_str_radix(text, 8).map_err(|_| ErrorCode::Integrity) |
| 296 | 227 | } |
| 297 | | -삭제: fn tar( |
| 298 | | -삭제: input: &[u8], |
| 299 | | -삭제: limits: Limits, |
| 300 | | -삭제: decode: bool, |
| 301 | | -삭제: check: &mut impl crate::resources::Monitor, |
| 302 | | -삭제: ) -> Result<Vec<Member>> { |
| 303 | | -삭제: let mut at = 0; |
| 304 | | -삭제: let mut members = Vec::new(); |
| 305 | | -삭제: let mut total = 0u64; |
| 306 | | -삭제: let mut metadata = MetadataBudget::default(); |
| 307 | | -삭제: loop { |
| 308 | | -삭제: check.check()?; |
| 309 | | -삭제: let header = part(input, at, 512)?; |
| 310 | | -삭제: if header.iter().all(|b| *b == 0) { |
| 311 | | -삭제: if part(input, at + 512, 512)?.iter().any(|b| *b != 0) |
| 312 | | -삭제: || input[at..].iter().any(|b| *b != 0) |
| 313 | | -삭제: || input.len() % 512 != 0 |
| 314 | | -삭제: { |
| 315 | | -삭제: return Err(ErrorCode::Integrity); |
| 316 | | -삭제: } |
| 317 | | -삭제: break; |
| 318 | | -삭제: } |
| 319 | | -삭제: if members.len() >= limits.entries as usize { |
| 320 | | -삭제: return Err(ErrorCode::Limit); |
| 321 | | -삭제: } |
| 322 | | -삭제: let expected = octal(&header[148..156])?; |
| 323 | | -삭제: let checksum = header |
| 324 | | -삭제: .iter() |
| 325 | | -삭제: .enumerate() |
| 326 | | -삭제: .map(|(i, b)| { |
| 327 | | -삭제: if (148..156).contains(&i) { |
| 328 | | -삭제: 32u64 |
| 329 | | -삭제: } else { |
| 330 | | -삭제: *b as u64 |
| 331 | | -삭제: } |
| 332 | | -삭제: }) |
| 333 | | -삭제: .sum::<u64>(); |
| 334 | | -삭제: if checksum != expected { |
| 335 | | -삭제: return Err(ErrorCode::Integrity); |
| 336 | | -삭제: } |
| 337 | | -삭제: let mut name = tar_string(&header[..100])?; |
| 338 | | -삭제: if &header[257..263] == b"ustar\0" || &header[257..263] == b"ustar " { |
| 339 | | -삭제: let prefix = tar_string(&header[345..500])?; |
| 340 | | -삭제: if !prefix.is_empty() { |
| 341 | | -삭제: name = format!("{prefix}/{name}"); |
| 342 | | -삭제: } |
| 343 | | -삭제: } else if header[257..263].iter().any(|b| *b != 0) { |
| 344 | | -삭제: return Err(ErrorCode::UnsupportedFeature); |
| 345 | | -삭제: } |
| 346 | | -삭제: let kind = match header[156] { |
| 347 | | -삭제: 0 | b'0' => EntryKind::File, |
| 348 | | -삭제: b'5' => EntryKind::Directory, |
| 349 | | -삭제: b'1' | b'2' => EntryKind::Link, |
| 350 | | -삭제: b'x' | b'g' | b'L' | b'K' => return Err(ErrorCode::UnsupportedFeature), |
| 351 | | -삭제: _ => EntryKind::Other, |
| 352 | | -삭제: }; |
| 353 | | -삭제: let size = octal(&header[124..136])?; |
| 354 | | -삭제: if kind != EntryKind::File && size != 0 { |
| 355 | | -삭제: return Err(ErrorCode::Integrity); |
| 356 | | -삭제: } |
| 357 | | -삭제: total = total |
| 358 | | -삭제: .checked_add(size) |
| 359 | | -삭제: .filter(|n| *n <= limits.output) |
| 360 | | -삭제: .ok_or(ErrorCode::Limit)?; |
| 361 | | -삭제: let size = usize::try_from(size).map_err(|_| ErrorCode::Limit)?; |
| 362 | | -삭제: let bytes = part(input, at + 512, size)?; |
| 363 | | -삭제: metadata.charge(&name)?; |
| 364 | | -삭제: let name = valid_name(&name, kind == EntryKind::Directory)?; |
| 365 | | -삭제: let data = if decode { bytes.to_vec() } else { Vec::new() }; |
| 366 | | -삭제: members.push(Member { |
| 367 | | -삭제: entry: Entry { |
| 368 | | -삭제: entry_id: members.len() as u32, |
| 369 | | -삭제: name, |
| 370 | | -삭제: kind, |
| 371 | | -삭제: size: size as u64, |
| 372 | | -삭제: compressed_size: size as u64, |
| 373 | | -삭제: encrypted: false, |
| 374 | | -삭제: }, |
| 375 | | -삭제: data, |
| 376 | | -삭제: }); |
| 377 | | -삭제: at = at |
| 378 | | -삭제: .checked_add(512) |
| 379 | | -삭제: .and_then(|n| n.checked_add(size.div_ceil(512) * 512)) |
| 380 | | -삭제: .ok_or(ErrorCode::Integrity)?; |
| 381 | | -삭제: } |
| 382 | | -삭제: validate_members(&members)?; |
| 383 | | -삭제: Ok(members) |
| 384 | | -삭제: } |
| 385 | | -삭제: pub(crate) use crate::zip_extra::AesExtra; |
| 386 | | -삭제: pub(crate) fn aes_extra(extra: &[u8]) -> Result<Option<AesExtra>> { |
| 387 | | -삭제: crate::zip_extra::parse(extra, crate::zip_extra::Policy::Classic).map(|extra| extra.aes) |
| 388 | | -삭제: } |
| 389 | 228 | /// ZIP 항목 이름을 해석함. |
| 390 | 229 | /// |
| 391 | 230 | /// - UTF-8 플래그(비트 11)가 있으면 이름은 UTF-8이어야 하며 아니면 손상으로 봄. |
| | @@ -414,283 +253,6 @@ pub(crate) fn zip_name(raw: &[u8], flags: u16, extra: &[u8]) -> Result<String> { |
| 414 | 253 | .map(str::to_owned) |
| 415 | 254 | .map_err(|_| ErrorCode::UnsupportedFeature) |
| 416 | 255 | } |
| 417 | | -삭제: fn zip( |
| 418 | | -삭제: input: &[u8], |
| 419 | | -삭제: limits: Limits, |
| 420 | | -삭제: decode: bool, |
| 421 | | -삭제: password: Option<&[u8]>, |
| 422 | | -삭제: check: &mut impl crate::resources::Monitor, |
| 423 | | -삭제: ) -> Result<Vec<Member>> { |
| 424 | | -삭제: if input.len() < 22 { |
| 425 | | -삭제: return Err(ErrorCode::Integrity); |
| 426 | | -삭제: } |
| 427 | | -삭제: let end = (input.len().saturating_sub(65557)..=input.len() - 22) |
| 428 | | -삭제: .rev() |
| 429 | | -삭제: .find(|&i| { |
| 430 | | -삭제: input.get(i..i + 4) == Some(b"PK\x05\x06") |
| 431 | | -삭제: && u16at(input, i + 20) |
| 432 | | -삭제: .ok() |
| 433 | | -삭제: .is_some_and(|n| i + 22 + n as usize == input.len()) |
| 434 | | -삭제: }) |
| 435 | | -삭제: .ok_or(ErrorCode::Integrity)?; |
| 436 | | -삭제: let count = u16at(input, end + 10)? as usize; |
| 437 | | -삭제: if u16at(input, end + 4)? != 0 |
| 438 | | -삭제: || u16at(input, end + 6)? != 0 |
| 439 | | -삭제: || count != u16at(input, end + 8)? as usize |
| 440 | | -삭제: { |
| 441 | | -삭제: return Err(ErrorCode::UnsupportedFeature); |
| 442 | | -삭제: } |
| 443 | | -삭제: if count == 65535 || u32at(input, end + 12)? == u32::MAX || u32at(input, end + 16)? == u32::MAX |
| 444 | | -삭제: { |
| 445 | | -삭제: return Err(ErrorCode::UnsupportedFeature); |
| 446 | | -삭제: } |
| 447 | | -삭제: if count > limits.entries as usize { |
| 448 | | -삭제: return Err(ErrorCode::Limit); |
| 449 | | -삭제: } |
| 450 | | -삭제: let central = u32at(input, end + 16)? as usize; |
| 451 | | -삭제: if central.checked_add(u32at(input, end + 12)? as usize) != Some(end) { |
| 452 | | -삭제: return Err(ErrorCode::Integrity); |
| 453 | | -삭제: } |
| 454 | | -삭제: let mut at = central; |
| 455 | | -삭제: let mut members = Vec::new(); |
| 456 | | -삭제: let mut spans = Vec::new(); |
| 457 | | -삭제: let mut total = 0u64; |
| 458 | | -삭제: let mut metadata = MetadataBudget::default(); |
| 459 | | -삭제: for id in 0..count { |
| 460 | | -삭제: check.check()?; |
| 461 | | -삭제: let h = part(input, at, 46)?; |
| 462 | | -삭제: if &h[..4] != b"PK\x01\x02" { |
| 463 | | -삭제: return Err(ErrorCode::Integrity); |
| 464 | | -삭제: } |
| 465 | | -삭제: let flags = u16at(h, 8)?; |
| 466 | | -삭제: let method = u16at(h, 10)?; |
| 467 | | -삭제: if flags & !0x080f != 0 || !matches!(method, 0 | 8 | 99) || u16at(h, 34)? != 0 { |
| 468 | | -삭제: return Err(ErrorCode::UnsupportedFeature); |
| 469 | | -삭제: } |
| 470 | | -삭제: let crc = u32at(h, 16)?; |
| 471 | | -삭제: let compressed = u32at(h, 20)? as usize; |
| 472 | | -삭제: let size = u32at(h, 24)? as usize; |
| 473 | | -삭제: if compressed == u32::MAX as usize || size == u32::MAX as usize { |
| 474 | | -삭제: return Err(ErrorCode::UnsupportedFeature); |
| 475 | | -삭제: } |
| 476 | | -삭제: total = total |
| 477 | | -삭제: .checked_add(size as u64) |
| 478 | | -삭제: .filter(|n| *n <= limits.output) |
| 479 | | -삭제: .ok_or(ErrorCode::Limit)?; |
| 480 | | -삭제: let name_len = u16at(h, 28)? as usize; |
| 481 | | -삭제: let extra_len = u16at(h, 30)? as usize; |
| 482 | | -삭제: let comment_len = u16at(h, 32)? as usize; |
| 483 | | -삭제: let extra = aes_extra(part(input, at + 46 + name_len, extra_len)?)?; |
| 484 | | -삭제: let encrypted = flags & 1 != 0; |
| 485 | | -삭제: if (method == 99) != extra.is_some() || (method == 99 && !encrypted) { |
| 486 | | -삭제: return Err(ErrorCode::Integrity); |
| 487 | | -삭제: } |
| 488 | | -삭제: if extra.is_some_and(|e| e.version == 2) && crc != 0 { |
| 489 | | -삭제: return Err(ErrorCode::Integrity); |
| 490 | | -삭제: } |
| 491 | | -삭제: let raw = part(input, at + 46, name_len)?; |
| 492 | | -삭제: let name = zip_name(raw, flags, part(input, at + 46 + name_len, extra_len)?)?; |
| 493 | | -삭제: let name = name.as_str(); |
| 494 | | -삭제: let mode = u32at(h, 38)? >> 16; |
| 495 | | -삭제: let kind = match mode & 0xf000 { |
| 496 | | -삭제: 0xa000 => EntryKind::Link, |
| 497 | | -삭제: 0x4000 => EntryKind::Directory, |
| 498 | | -삭제: 0 | 0x8000 => { |
| 499 | | -삭제: if name.ends_with('/') { |
| 500 | | -삭제: EntryKind::Directory |
| 501 | | -삭제: } else { |
| 502 | | -삭제: EntryKind::File |
| 503 | | -삭제: } |
| 504 | | -삭제: } |
| 505 | | -삭제: _ => EntryKind::Other, |
| 506 | | -삭제: }; |
| 507 | | -삭제: metadata.charge(name)?; |
| 508 | | -삭제: let name = valid_name(name, kind == EntryKind::Directory)?; |
| 509 | | -삭제: let local = u32at(h, 42)? as usize; |
| 510 | | -삭제: let lh = part(input, local, 30)?; |
| 511 | | -삭제: if &lh[..4] != b"PK\x03\x04" || u16at(lh, 6)? != flags || u16at(lh, 8)? != method { |
| 512 | | -삭제: return Err(ErrorCode::Integrity); |
| 513 | | -삭제: } |
| 514 | | -삭제: let ln = u16at(lh, 26)? as usize; |
| 515 | | -삭제: let le = u16at(lh, 28)? as usize; |
| 516 | | -삭제: if aes_extra(part(input, local + 30 + ln, le)?)? != extra |
| 517 | | -삭제: || u16at(lh, 10)? != u16at(h, 12)? |
| 518 | | -삭제: || part(input, local + 30, ln)? != raw |
| 519 | | -삭제: { |
| 520 | | -삭제: return Err(ErrorCode::Integrity); |
| 521 | | -삭제: } |
| 522 | | -삭제: if flags & 8 == 0 |
| 523 | | -삭제: && (u32at(lh, 14)? != crc |
| 524 | | -삭제: || u32at(lh, 18)? as usize != compressed |
| 525 | | -삭제: || u32at(lh, 22)? as usize != size) |
| 526 | | -삭제: { |
| 527 | | -삭제: return Err(ErrorCode::Integrity); |
| 528 | | -삭제: } |
| 529 | | -삭제: let start = local |
| 530 | | -삭제: .checked_add(30 + ln + le) |
| 531 | | -삭제: .ok_or(ErrorCode::Integrity)?; |
| 532 | | -삭제: let packed = part(input, start, compressed)?; |
| 533 | | -삭제: let mut stop = start + compressed; |
| 534 | | -삭제: if flags & 8 != 0 { |
| 535 | | -삭제: if input.get(stop..stop + 4) == Some(b"PK\x07\x08") { |
| 536 | | -삭제: stop += 4; |
| 537 | | -삭제: } |
| 538 | | -삭제: if u32at(input, stop)? != crc |
| 539 | | -삭제: || u32at(input, stop + 4)? as usize != compressed |
| 540 | | -삭제: || u32at(input, stop + 8)? as usize != size |
| 541 | | -삭제: { |
| 542 | | -삭제: return Err(ErrorCode::Integrity); |
| 543 | | -삭제: } |
| 544 | | -삭제: stop += 12; |
| 545 | | -삭제: } |
| 546 | | -삭제: if stop > central { |
| 547 | | -삭제: return Err(ErrorCode::Integrity); |
| 548 | | -삭제: } |
| 549 | | -삭제: spans.push((local, stop)); |
| 550 | | -삭제: if encrypted && compressed < if extra.is_some() { 28 } else { 12 } { |
| 551 | | -삭제: return Err(ErrorCode::Integrity); |
| 552 | | -삭제: } |
| 553 | | -삭제: let data = if decode { |
| 554 | | -삭제: let decrypted; |
| 555 | | -삭제: let packed = if encrypted { |
| 556 | | -삭제: let password = password.ok_or(ErrorCode::WrongPassword)?; |
| 557 | | -삭제: decrypted = if extra.is_some() { |
| 558 | | -삭제: crate::crypto::aes_decrypt(packed, password, check)? |
| 559 | | -삭제: } else { |
| 560 | | -삭제: let verifier = if flags & 8 != 0 { |
| 561 | | -삭제: (u16at(h, 12)? >> 8) as u8 |
| 562 | | -삭제: } else { |
| 563 | | -삭제: (crc >> 24) as u8 |
| 564 | | -삭제: }; |
| 565 | | -삭제: crate::crypto::zipcrypto_decrypt(packed, password, verifier, check)? |
| 566 | | -삭제: }; |
| 567 | | -삭제: decrypted.as_slice() |
| 568 | | -삭제: } else { |
| 569 | | -삭제: packed |
| 570 | | -삭제: }; |
| 571 | | -삭제: let actual_method = extra.map_or(method, |e| e.method); |
| 572 | | -삭제: let bytes = if actual_method == 0 { |
| 573 | | -삭제: if packed.len() != size { |
| 574 | | -삭제: return Err(ErrorCode::Integrity); |
| 575 | | -삭제: } |
| 576 | | -삭제: packed.to_vec() |
| 577 | | -삭제: } else { |
| 578 | | -삭제: let (out, used) = inflate(packed, false, size as u64, check)?; |
| 579 | | -삭제: if used != packed.len() { |
| 580 | | -삭제: return Err(ErrorCode::Integrity); |
| 581 | | -삭제: } |
| 582 | | -삭제: out |
| 583 | | -삭제: }; |
| 584 | | -삭제: if bytes.len() != size |
| 585 | | -삭제: || (extra.is_none_or(|e| e.version == 1) && crc32(0, &bytes) != crc) |
| 586 | | -삭제: { |
| 587 | | -삭제: return Err(ErrorCode::Integrity); |
| 588 | | -삭제: } |
| 589 | | -삭제: bytes |
| 590 | | -삭제: } else { |
| 591 | | -삭제: Vec::new() |
| 592 | | -삭제: }; |
| 593 | | -삭제: if kind == EntryKind::Directory && size != 0 { |
| 594 | | -삭제: return Err(ErrorCode::Integrity); |
| 595 | | -삭제: } |
| 596 | | -삭제: members.push(Member { |
| 597 | | -삭제: entry: Entry { |
| 598 | | -삭제: entry_id: id as u32, |
| 599 | | -삭제: name, |
| 600 | | -삭제: kind, |
| 601 | | -삭제: size: size as u64, |
| 602 | | -삭제: compressed_size: compressed as u64, |
| 603 | | -삭제: encrypted, |
| 604 | | -삭제: }, |
| 605 | | -삭제: data, |
| 606 | | -삭제: }); |
| 607 | | -삭제: at = at |
| 608 | | -삭제: .checked_add(46 + name_len + extra_len + comment_len) |
| 609 | | -삭제: .ok_or(ErrorCode::Integrity)?; |
| 610 | | -삭제: } |
| 611 | | -삭제: if at != end { |
| 612 | | -삭제: return Err(ErrorCode::Integrity); |
| 613 | | -삭제: } |
| 614 | | -삭제: spans.sort_unstable(); |
| 615 | | -삭제: let mut next = 0; |
| 616 | | -삭제: for (start, stop) in spans { |
| 617 | | -삭제: if start != next { |
| 618 | | -삭제: return Err(ErrorCode::Integrity); |
| 619 | | -삭제: } |
| 620 | | -삭제: next = stop; |
| 621 | | -삭제: } |
| 622 | | -삭제: if next != central { |
| 623 | | -삭제: return Err(ErrorCode::Integrity); |
| 624 | | -삭제: } |
| 625 | | -삭제: validate_members(&members)?; |
| 626 | | -삭제: Ok(members) |
| 627 | | -삭제: } |
| 628 | | -삭제: pub fn parse( |
| 629 | | -삭제: input: &[u8], |
| 630 | | -삭제: limits: Limits, |
| 631 | | -삭제: decode: bool, |
| 632 | | -삭제: check: &mut impl crate::resources::Monitor, |
| 633 | | -삭제: ) -> Result<Catalog> { |
| 634 | | -삭제: parse_with_password(input, limits, decode, None, check) |
| 635 | | -삭제: } |
| 636 | | -삭제: pub fn parse_with_password( |
| 637 | | -삭제: input: &[u8], |
| 638 | | -삭제: limits: Limits, |
| 639 | | -삭제: decode: bool, |
| 640 | | -삭제: password: Option<&[u8]>, |
| 641 | | -삭제: check: &mut impl crate::resources::Monitor, |
| 642 | | -삭제: ) -> Result<Catalog> { |
| 643 | | -삭제: if !limits.valid() { |
| 644 | | -삭제: return Err(ErrorCode::Limit); |
| 645 | | -삭제: } |
| 646 | | -삭제: let limits = limits.legacy(); |
| 647 | | -삭제: if input.len() as u64 > limits.input { |
| 648 | | -삭제: return Err(ErrorCode::Limit); |
| 649 | | -삭제: } |
| 650 | | -삭제: if input.starts_with(b"7z\xbc\xaf\x27\x1c") { |
| 651 | | -삭제: return Ok(Catalog { |
| 652 | | -삭제: format: Format::SevenZip, |
| 653 | | -삭제: members: crate::seven_zip::parse(input, limits, decode, check)?, |
| 654 | | -삭제: }); |
| 655 | | -삭제: } |
| 656 | | -삭제: if input.starts_with(b"PK\x03\x04") || input.starts_with(b"PK\x05\x06") { |
| 657 | | -삭제: return Ok(Catalog { |
| 658 | | -삭제: format: Format::Zip, |
| 659 | | -삭제: members: zip(input, limits, decode, password, check)?, |
| 660 | | -삭제: }); |
| 661 | | -삭제: } |
| 662 | | -삭제: if input.starts_with(&[31, 139]) { |
| 663 | | -삭제: let bytes = gzip(input, limits, check)?; |
| 664 | | -삭제: if bytes.len() >= 512 && matches!(&bytes[257..263], b"ustar\0" | b"ustar ") { |
| 665 | | -삭제: return Ok(Catalog { |
| 666 | | -삭제: format: Format::TarGzip, |
| 667 | | -삭제: members: tar(&bytes, limits, decode, check)?, |
| 668 | | -삭제: }); |
| 669 | | -삭제: } |
| 670 | | -삭제: let len = bytes.len() as u64; |
| 671 | | -삭제: return Ok(Catalog { |
| 672 | | -삭제: format: Format::Gzip, |
| 673 | | -삭제: members: vec![Member { |
| 674 | | -삭제: entry: Entry { |
| 675 | | -삭제: entry_id: 0, |
| 676 | | -삭제: name: "GZip stream".to_owned(), |
| 677 | | -삭제: kind: EntryKind::File, |
| 678 | | -삭제: size: len, |
| 679 | | -삭제: compressed_size: input.len() as u64, |
| 680 | | -삭제: encrypted: false, |
| 681 | | -삭제: }, |
| 682 | | -삭제: data: if decode { bytes } else { Vec::new() }, |
| 683 | | -삭제: }], |
| 684 | | -삭제: }); |
| 685 | | -삭제: } |
| 686 | | -삭제: if input.len() >= 512 { |
| 687 | | -삭제: return Ok(Catalog { |
| 688 | | -삭제: format: Format::Tar, |
| 689 | | -삭제: members: tar(input, limits, decode, check)?, |
| 690 | | -삭제: }); |
| 691 | | -삭제: } |
| 692 | | -삭제: Err(ErrorCode::UnsupportedFormat) |
| 693 | | -삭제: } |
| 694 | 256 | fn deflate( |
| 695 | 257 | input: &[u8], |
| 696 | 258 | limit: u64, |