team-ljyzipelo

공개
733줄24 KB
1use crate::{
2 archive::{self, Member, MetadataBudget},
3 crypto,
4 protocol::*,
5 spool,
6};
7use std::{
8 fs::File,
9 io::{Read, Seek, SeekFrom, Write},
10};
11use zeroize::Zeroizing;
12use zlib_rs::{Inflate, InflateFlush, Status, crc32::crc32};
13
14type Result<T> = std::result::Result<T, ErrorCode>;
15const CHUNK: usize = 65536;
16fn at(file: &mut File, offset: u64, bytes: &mut [u8]) -> Result<()> {
17 file.seek(SeekFrom::Start(offset))
18 .map_err(|_| ErrorCode::Io)?;
19 file.read_exact(bytes).map_err(|_| ErrorCode::Integrity)
20}
21fn bytes(file: &mut File, offset: u64, size: usize) -> Result<Vec<u8>> {
22 let mut result = vec![0; size];
23 at(file, offset, &mut result)?;
24 Ok(result)
25}
26fn u16at(b: &[u8], at: usize) -> u16 {
27 u16::from_le_bytes(b[at..at + 2].try_into().unwrap())
28}
29fn u32at(b: &[u8], at: usize) -> u32 {
30 u32::from_le_bytes(b[at..at + 4].try_into().unwrap())
31}
32fn u64at(b: &[u8], at: usize) -> u64 {
33 u64::from_le_bytes(b[at..at + 8].try_into().unwrap())
34}
35fn add(a: u64, b: u64) -> Result<u64> {
36 a.checked_add(b).ok_or(ErrorCode::Integrity)
37}
38fn io<T>(v: std::io::Result<T>) -> Result<T> {
39 v.map_err(crate::space::io_error)
40}
41
42pub(crate) struct Catalog {
43 pub format: Format,
44 pub members: Vec<Member>,
45 pub offsets: Vec<u64>,
46 pub data: File,
47}
48impl Catalog {
49 pub fn open(
50 source: &mut File,
51 len: u64,
52 limits: Limits,
53 decode: bool,
54 password: Option<&[u8]>,
55 check: &mut impl FnMut() -> Result<()>,
56 ) -> Result<Self> {
57 let mut magic = [0; 6];
58 let n = len.min(6) as usize;
59 at(source, 0, &mut magic[..n])?;
60 if magic.starts_with(b"7z\xbc\xaf\x27\x1c") {
61 return crate::seven_zip::open(source, len, limits, decode, check);
62 }
63 if magic.starts_with(b"PK\x03\x04")
64 || magic.starts_with(b"PK\x05\x06")
65 || magic.starts_with(b"PK\x06\x06")
66 {
67 return zip(source, len, limits, decode, password, check);
68 }
69 let compression = if magic.starts_with(b"BZh") {
70 Some(crate::compressed_tar::Compression::Bzip2)
71 } else if &magic == b"\xfd7zXZ\0" {
72 Some(crate::compressed_tar::Compression::Xz)
73 } else {
74 None
75 };
76 if let Some(compression) = compression {
77 let (data, expanded) =
78 crate::compressed_tar::decode(source, compression, limits.output, check)?;
79 if expanded < 1024 {
80 return Err(ErrorCode::Integrity);
81 }
82 return tar(data, expanded, limits, Format::Tar, check);
83 }
84 if magic.starts_with(&[31, 139]) {
85 let mut data = spool::temporary()?;
86 io(source.rewind())?;
87 let expanded = inflate(source, len, true, limits.output, &mut data, check)?.0;
88 let mut header = [0; 512];
89 if expanded >= 512 {
90 at(&mut data, 0, &mut header)?;
91 if matches!(&header[257..263], b"ustar\0" | b"ustar ") {
92 return tar(data, expanded, limits, Format::TarGzip, check);
93 }
94 }
95 return Ok(Self {
96 format: Format::Gzip,
97 members: vec![Member {
98 entry: Entry {
99 entry_id: 0,
100 name: "GZip stream".to_owned(),
101 kind: EntryKind::File,
102 size: expanded,
103 compressed_size: len,
104 encrypted: false,
105 },
106 data: Vec::new(),
107 }],
108 offsets: vec![0],
109 data,
110 });
111 }
112 if len >= 512 {
113 return tar(io(source.try_clone())?, len, limits, Format::Tar, check);
114 }
115 Err(ErrorCode::UnsupportedFormat)
116 }
117}
118
119fn tar(
120 mut data: File,
121 len: u64,
122 limits: Limits,
123 format: Format,
124 check: &mut impl FnMut() -> Result<()>,
125) -> Result<Catalog> {
126 let mut cursor = 0u64;
127 let mut total = 0u64;
128 let mut members = Vec::new();
129 let mut offsets = Vec::new();
130 let mut metadata = MetadataBudget::default();
131 loop {
132 check()?;
133 let mut header = [0; 512];
134 if add(cursor, 512)? > len {
135 return Err(ErrorCode::Integrity);
136 }
137 at(&mut data, cursor, &mut header)?;
138 if header.iter().all(|b| *b == 0) {
139 if add(cursor, 1024)? > len || len % 512 != 0 {
140 return Err(ErrorCode::Integrity);
141 }
142 let mut buffer = Zeroizing::new([0; CHUNK]);
143 let mut remaining = len - cursor - 512;
144 while remaining != 0 {
145 check()?;
146 let n = remaining.min(CHUNK as u64) as usize;
147 io(data.read_exact(&mut buffer[..n]))?;
148 if buffer[..n].iter().any(|b| *b != 0) {
149 return Err(ErrorCode::Integrity);
150 }
151 remaining -= n as u64;
152 }
153 break;
154 }
155 if members.len() >= limits.entries as usize {
156 return Err(ErrorCode::Limit);
157 }
158 let sum = header
159 .iter()
160 .enumerate()
161 .map(|(i, b)| {
162 if (148..156).contains(&i) {
163 32
164 } else {
165 *b as u64
166 }
167 })
168 .sum::<u64>();
169 if sum != archive::octal(&header[148..156])? {
170 return Err(ErrorCode::Integrity);
171 }
172 let mut name = archive::tar_string(&header[..100])?;
173 if matches!(&header[257..263], b"ustar\0" | b"ustar ") {
174 let prefix = archive::tar_string(&header[345..500])?;
175 if !prefix.is_empty() {
176 name = format!("{prefix}/{name}");
177 }
178 } else if header[257..263].iter().any(|b| *b != 0) {
179 return Err(ErrorCode::UnsupportedFeature);
180 }
181 let kind = match header[156] {
182 0 | b'0' => EntryKind::File,
183 b'5' => EntryKind::Directory,
184 b'1' | b'2' => EntryKind::Link,
185 b'x' | b'g' | b'L' | b'K' => return Err(ErrorCode::UnsupportedFeature),
186 _ => EntryKind::Other,
187 };
188 let size = archive::octal(&header[124..136])?;
189 if kind != EntryKind::File && size != 0 {
190 return Err(ErrorCode::Integrity);
191 }
192 total = total
193 .checked_add(size)
194 .filter(|n| *n <= limits.output)
195 .ok_or(ErrorCode::Limit)?;
196 let start = add(cursor, 512)?;
197 cursor = add(
198 start,
199 size.checked_add(511).ok_or(ErrorCode::Integrity)? / 512 * 512,
200 )?;
201 if cursor > len {
202 return Err(ErrorCode::Integrity);
203 }
204 metadata.charge(&name)?;
205 let name = archive::valid_name(&name, kind == EntryKind::Directory)?;
206 offsets.push(start);
207 members.push(Member {
208 entry: Entry {
209 entry_id: members.len() as u32,
210 name,
211 kind,
212 size,
213 compressed_size: size,
214 encrypted: false,
215 },
216 data: Vec::new(),
217 });
218 }
219 archive::validate_members(&members)?;
220 Ok(Catalog {
221 format,
222 members,
223 offsets,
224 data,
225 })
226}
227
228// ZIP 항목과 연결 GZip 스트림의 해제 총량을 제한함.
229fn inflate(
230 input: &mut impl Read,
231 length: u64,
232 gzip: bool,
233 limit: u64,
234 output: &mut File,
235 check: &mut impl FnMut() -> Result<()>,
236) -> Result<(u64, u32)> {
237 let mut remaining = length;
238 let mut buffer = Zeroizing::new([0; CHUNK]);
239 let mut plain = Zeroizing::new([0; CHUNK]);
240 let mut have = 0;
241 let mut used = 0;
242 let mut total = 0u64;
243 let mut crc = 0;
244 loop {
245 let mut stream = Inflate::new(gzip, if gzip { 31 } else { 15 });
246 loop {
247 check()?;
248 if used == have && remaining != 0 {
249 have = input
250 .read(&mut buffer[..remaining.min(CHUNK as u64) as usize])
251 .map_err(|_| ErrorCode::Io)?;
252 if have == 0 {
253 return Err(ErrorCode::Integrity);
254 }
255 remaining -= have as u64;
256 used = 0;
257 }
258 let a = stream.total_in();
259 let b = stream.total_out();
260 let status = stream
261 .decompress(&buffer[used..have], &mut plain[..], InflateFlush::NoFlush)
262 .map_err(|_| ErrorCode::Integrity)?;
263 used += (stream.total_in() - a) as usize;
264 let n = (stream.total_out() - b) as usize;
265 total = total
266 .checked_add(n as u64)
267 .filter(|n| *n <= limit)
268 .ok_or(ErrorCode::Limit)?;
269 crc = crc32(crc, &plain[..n]);
270 crate::space::ensure(output, n as u64)?;
271 io(output.write_all(&plain[..n]))?;
272 if status == Status::StreamEnd {
273 break;
274 }
275 if a == stream.total_in() && b == stream.total_out() {
276 return Err(ErrorCode::Integrity);
277 }
278 }
279 if remaining == 0 && used == have {
280 break;
281 }
282 if !gzip {
283 return Err(ErrorCode::Integrity);
284 }
285 }
286 Ok((total, crc))
287}
288
289struct Packed {
290 offset: u64,
291 size: u64,
292 method: u16,
293 crc: Option<u32>,
294 flags: u16,
295 verifier: u8,
296 aes: bool,
297}
298
299fn extra(bytes: &[u8]) -> Result<(Option<archive::AesExtra>, Option<Vec<u8>>)> {
300 let mut at = 0;
301 let mut aes = Vec::new();
302 let mut large = None;
303 while at < bytes.len() {
304 if bytes.len() - at < 4 {
305 return Err(ErrorCode::Integrity);
306 }
307 let tag = u16at(bytes, at);
308 let len = u16at(bytes, at + 2) as usize;
309 let end = at + 4 + len;
310 if end > bytes.len() {
311 return Err(ErrorCode::Integrity);
312 }
313 if tag == 1 {
314 if large.is_some() {
315 return Err(ErrorCode::Integrity);
316 }
317 large = Some(bytes[at + 4..end].to_vec());
318 } else {
319 aes.extend_from_slice(&bytes[at..end]);
320 }
321 at = end;
322 }
323 Ok((archive::aes_extra(&aes)?, large))
324}
325fn large_value(value: u32, extra: &mut &[u8]) -> Result<u64> {
326 if value != u32::MAX {
327 return Ok(value as u64);
328 }
329 if extra.len() < 8 {
330 return Err(ErrorCode::Integrity);
331 }
332 let result = u64at(extra, 0);
333 *extra = &extra[8..];
334 Ok(result)
335}
336fn expanded(value: u64, maximum: u64, large: u64) -> Result<u64> {
337 if value == maximum || value == large {
338 Ok(large)
339 } else {
340 Err(ErrorCode::Integrity)
341 }
342}
343fn disk_start(value: u16, extra: &mut &[u8]) -> Result<u32> {
344 if value != u16::MAX {
345 return Ok(value as u32);
346 }
347 if extra.len() < 4 {
348 return Err(ErrorCode::Integrity);
349 }
350 let result = u32at(extra, 0);
351 *extra = &extra[4..];
352 Ok(result)
353}
354fn local_sizes(size: u32, compressed: u32, extra: Option<&[u8]>) -> Result<(u64, u64)> {
355 if let Some(extra) = extra {
356 // 로컬 ZIP64 추가 필드에는 두 크기가 함께 들어감.
357 if extra.len() < 16 {
358 return Err(ErrorCode::Integrity);
359 }
360 Ok((
361 expanded(size as u64, u32::MAX as u64, u64at(extra, 0))?,
362 expanded(compressed as u64, u32::MAX as u64, u64at(extra, 8))?,
363 ))
364 } else if size == u32::MAX || compressed == u32::MAX {
365 Err(ErrorCode::Integrity)
366 } else {
367 Ok((size as u64, compressed as u64))
368 }
369}
370fn descriptor_size(
371 bytes: &[u8],
372 crc: u32,
373 compressed: u64,
374 size: u64,
375 wide_required: bool,
376) -> Result<usize> {
377 // 일반 크기 헤더와 64비트 디스크립터를 함께 기록하는 도구도 있음.
378 if bytes.len() >= 20
379 && u32at(bytes, 0) == crc
380 && u64at(bytes, 4) == compressed
381 && u64at(bytes, 12) == size
382 {
383 return Ok(20);
384 }
385 if !wide_required
386 && bytes.len() >= 12
387 && u32at(bytes, 0) == crc
388 && u32at(bytes, 4) as u64 == compressed
389 && u32at(bytes, 8) as u64 == size
390 {
391 return Ok(12);
392 }
393 Err(ErrorCode::Integrity)
394}
395fn zip(
396 source: &mut File,
397 len: u64,
398 limits: Limits,
399 decode: bool,
400 password: Option<&[u8]>,
401 check: &mut impl FnMut() -> Result<()>,
402) -> Result<Catalog> {
403 if len < 22 {
404 return Err(ErrorCode::Integrity);
405 }
406 let tail_at = len.saturating_sub(65557);
407 let tail = bytes(source, tail_at, (len - tail_at) as usize)?;
408 let end = (0..=tail.len() - 22)
409 .rev()
410 .find(|&i| {
411 &tail[i..i + 4] == b"PK\x05\x06" && i + 22 + u16at(&tail, i + 20) as usize == tail.len()
412 })
413 .ok_or(ErrorCode::Integrity)?;
414 let h = &tail[end..];
415 let mut disk = u16at(h, 4) as u64;
416 let mut central_disk = u16at(h, 6) as u64;
417 let mut entries_on_disk = u16at(h, 8) as u64;
418 let mut count = u16at(h, 10) as u64;
419 let mut central_size = u32at(h, 12) as u64;
420 let mut central = u32at(h, 16) as u64;
421 let mut central_end = tail_at + end as u64;
422 let required_zip64 = [disk, central_disk, entries_on_disk, count].contains(&(u16::MAX as u64))
423 || central_size == u32::MAX as u64
424 || central == u32::MAX as u64;
425 let locator = if central_end >= 20 {
426 bytes(source, central_end - 20, 20)?
427 } else {
428 Vec::new()
429 };
430 // 중앙 디렉터리 주석의 동일 서명을 locator로 오인하지 않음
431 let has_locator =
432 locator.starts_with(b"PK\x06\x07") && add(central, central_size)? != central_end;
433 if required_zip64 || has_locator {
434 if locator.len() != 20 {
435 return Err(ErrorCode::Integrity);
436 }
437 if &locator[..4] != b"PK\x06\x07" || u32at(&locator, 4) != 0 || u32at(&locator, 16) != 1 {
438 return Err(ErrorCode::UnsupportedFeature);
439 }
440 let offset = u64at(&locator, 8);
441 if add(offset, 56)? > central_end - 20 {
442 return Err(ErrorCode::Integrity);
443 }
444 let large = bytes(source, offset, 56)?;
445 if &large[..4] != b"PK\x06\x06"
446 || u64at(&large, 4) < 44
447 || add(offset, add(12, u64at(&large, 4))?)? != central_end - 20
448 {
449 return Err(ErrorCode::Integrity);
450 }
451 // 각 필드의 확장값을 해석한 뒤 단일 디스크 조건을 검사함.
452 disk = expanded(disk, u16::MAX as u64, u32at(&large, 16) as u64)?;
453 central_disk = expanded(central_disk, u16::MAX as u64, u32at(&large, 20) as u64)?;
454 entries_on_disk = expanded(entries_on_disk, u16::MAX as u64, u64at(&large, 24))?;
455 count = expanded(count, u16::MAX as u64, u64at(&large, 32))?;
456 central_size = expanded(central_size, u32::MAX as u64, u64at(&large, 40))?;
457 central = expanded(central, u32::MAX as u64, u64at(&large, 48))?;
458 central_end = offset;
459 }
460 if disk != 0 || central_disk != 0 || entries_on_disk != count {
461 return Err(ErrorCode::UnsupportedFeature);
462 }
463 if count > limits.entries as u64 {
464 return Err(ErrorCode::Limit);
465 }
466 if add(central, central_size)? != central_end {
467 return Err(ErrorCode::Integrity);
468 }
469 let mut cursor = central;
470 let mut total = 0u64;
471 let mut metadata = MetadataBudget::default();
472 let mut members = Vec::new();
473 let mut packed = Vec::new();
474 let mut spans = Vec::new();
475 for id in 0..count {
476 check()?;
477 if add(cursor, 46)? > central_end {
478 return Err(ErrorCode::Integrity);
479 }
480 let h = bytes(source, cursor, 46)?;
481 if &h[..4] != b"PK\x01\x02" {
482 return Err(ErrorCode::Integrity);
483 }
484 let flags = u16at(&h, 8);
485 let method = u16at(&h, 10);
486 if flags & !0x080f != 0 || !matches!(method, 0 | 8 | 99) {
487 return Err(ErrorCode::UnsupportedFeature);
488 }
489 let crc = u32at(&h, 16);
490 let (nn, ne, nc) = (
491 u16at(&h, 28) as u64,
492 u16at(&h, 30) as u64,
493 u16at(&h, 32) as u64,
494 );
495 let next = add(cursor, 46 + nn + ne + nc)?;
496 if next > central_end {
497 return Err(ErrorCode::Integrity);
498 }
499 if nn > 4096 {
500 return Err(ErrorCode::UnsafePath);
501 }
502 let raw = bytes(source, cursor + 46, nn as usize)?;
503 let central_extra = bytes(source, cursor + 46 + nn, ne as usize)?;
504 let (aes, large) = extra(&central_extra)?;
505 let name = archive::zip_name(&raw, flags, &central_extra)?;
506 let name = name.as_str();
507 let mut large = large.as_deref().unwrap_or(&[]);
508 let size = large_value(u32at(&h, 24), &mut large)?;
509 let compressed = large_value(u32at(&h, 20), &mut large)?;
510 let local = large_value(u32at(&h, 42), &mut large)?;
511 if disk_start(u16at(&h, 34), &mut large)? != 0 {
512 return Err(ErrorCode::UnsupportedFeature);
513 }
514 let encrypted = flags & 1 != 0;
515 if (method == 99) != aes.is_some()
516 || (method == 99 && !encrypted)
517 || (aes.is_some_and(|a| a.version == 2) && crc != 0)
518 {
519 return Err(ErrorCode::Integrity);
520 }
521 let overhead = if !encrypted {
522 0
523 } else if aes.is_some() {
524 28
525 } else {
526 12
527 };
528 compressed
529 .checked_sub(overhead)
530 .ok_or(ErrorCode::Integrity)?;
531 total = total
532 .checked_add(size)
533 .filter(|n| *n <= limits.output)
534 .ok_or(ErrorCode::Limit)?;
535 let mode = u32at(&h, 38) >> 16;
536 let kind = match mode & 0xf000 {
537 0xa000 => EntryKind::Link,
538 0x4000 => EntryKind::Directory,
539 0 | 0x8000 => {
540 if name.ends_with('/') {
541 EntryKind::Directory
542 } else {
543 EntryKind::File
544 }
545 }
546 _ => EntryKind::Other,
547 };
548 if kind == EntryKind::Directory && size != 0 {
549 return Err(ErrorCode::Integrity);
550 }
551 metadata.charge(name)?;
552 let name = archive::valid_name(name, kind == EntryKind::Directory)?;
553 if add(local, 30)? > central {
554 return Err(ErrorCode::Integrity);
555 }
556 let lh = bytes(source, local, 30)?;
557 if &lh[..4] != b"PK\x03\x04"
558 || u16at(&lh, 6) != flags
559 || u16at(&lh, 8) != method
560 || u16at(&lh, 10) != u16at(&h, 12)
561 {
562 return Err(ErrorCode::Integrity);
563 }
564 let (ln, le) = (u16at(&lh, 26) as u64, u16at(&lh, 28) as u64);
565 let start = add(local, 30 + ln + le)?;
566 if start > central || ln != nn {
567 return Err(ErrorCode::Integrity);
568 }
569 if bytes(source, local + 30, ln as usize)? != raw {
570 return Err(ErrorCode::Integrity);
571 }
572 let (local_aes, local_large) = extra(&bytes(source, local + 30 + ln, le as usize)?)?;
573 if local_aes != aes || local_large.as_ref().is_some_and(|values| values.len() < 16) {
574 return Err(ErrorCode::Integrity);
575 }
576 let size_zip64 = u32at(&h, 20) == u32::MAX
577 || u32at(&h, 24) == u32::MAX
578 || u32at(&lh, 18) == u32::MAX
579 || u32at(&lh, 22) == u32::MAX
580 || local_large.is_some();
581 if flags & 8 == 0 {
582 let (local_size, local_compressed) =
583 local_sizes(u32at(&lh, 22), u32at(&lh, 18), local_large.as_deref())?;
584 if u32at(&lh, 14) != crc || local_size != size || local_compressed != compressed {
585 return Err(ErrorCode::Integrity);
586 }
587 }
588 let mut stop = add(start, compressed)?;
589 if stop > central {
590 return Err(ErrorCode::Integrity);
591 }
592 if flags & 8 != 0 {
593 let descriptor_len = if size_zip64 { 20 } else { 12 };
594 if add(stop, descriptor_len)? > central {
595 return Err(ErrorCode::Integrity);
596 }
597 let signature = bytes(source, stop, 4)?;
598 if signature == b"PK\x07\x08" {
599 stop = add(stop, 4)?;
600 }
601 let d = bytes(source, stop, (central - stop).min(20) as usize)?;
602 let descriptor_len = descriptor_size(&d, crc, compressed, size, size_zip64)?;
603 stop = add(stop, descriptor_len as u64)?;
604 }
605 spans.push((local, stop));
606 packed.push(Packed {
607 offset: start,
608 size: compressed,
609 method: aes.map_or(method, |a| a.method),
610 crc: if aes.is_some_and(|a| a.version == 2) {
611 None
612 } else {
613 Some(crc)
614 },
615 flags,
616 verifier: if flags & 8 != 0 {
617 (u16at(&h, 12) >> 8) as u8
618 } else {
619 (crc >> 24) as u8
620 },
621 aes: aes.is_some(),
622 });
623 members.push(Member {
624 entry: Entry {
625 entry_id: id as u32,
626 name,
627 kind,
628 size,
629 compressed_size: compressed,
630 encrypted,
631 },
632 data: Vec::new(),
633 });
634 cursor = next;
635 }
636 if cursor != central_end {
637 return Err(ErrorCode::Integrity);
638 }
639 spans.sort_unstable();
640 let mut next = 0;
641 for (a, b) in spans {
642 if a != next {
643 return Err(ErrorCode::Integrity);
644 }
645 next = b;
646 }
647 if next != central {
648 return Err(ErrorCode::Integrity);
649 }
650 archive::validate_members(&members)?;
651 let mut data = spool::temporary()?;
652 if decode {
653 crate::space::ensure(&data, total)?;
654 }
655 let mut offsets = Vec::new();
656 for (member, packed) in members.iter().zip(packed) {
657 check()?;
658 offsets.push(io(data.stream_position())?);
659 if !decode {
660 continue;
661 }
662 let (decoder, start, length) = if packed.flags & 1 != 0 {
663 crypto::Decryptor::prepare(
664 source,
665 packed.offset,
666 packed.size,
667 password.ok_or(ErrorCode::WrongPassword)?,
668 packed.aes,
669 packed.verifier,
670 check,
671 )?
672 } else {
673 (crypto::Decryptor::Plain, packed.offset, packed.size)
674 };
675 io(source.seek(SeekFrom::Start(start)))?;
676 let mut reader = Decoded {
677 reader: source.take(length),
678 decoder,
679 };
680 let (size, crc) = if packed.method == 8 {
681 inflate(
682 &mut reader,
683 length,
684 false,
685 member.entry.size,
686 &mut data,
687 check,
688 )?
689 } else {
690 let mut buffer = Zeroizing::new([0; CHUNK]);
691 let mut total = 0u64;
692 let mut crc = 0;
693 loop {
694 check()?;
695 let n = io(reader.read(&mut buffer[..]))?;
696 if n == 0 {
697 break;
698 }
699 total = total
700 .checked_add(n as u64)
701 .filter(|n| *n <= member.entry.size)
702 .ok_or(ErrorCode::Integrity)?;
703 crc = crc32(crc, &buffer[..n]);
704 io(data.write_all(&buffer[..n]))?;
705 }
706 (total, crc)
707 };
708 if size != member.entry.size || packed.crc.is_some_and(|c| c != crc) {
709 return Err(ErrorCode::Integrity);
710 }
711 }
712 Ok(Catalog {
713 format: Format::Zip,
714 members,
715 offsets,
716 data,
717 })
718}
719struct Decoded<R> {
720 reader: R,
721 decoder: crypto::Decryptor,
722}
723impl<R: Read> Read for Decoded<R> {
724 fn read(&mut self, output: &mut [u8]) -> std::io::Result<usize> {
725 let n = self.reader.read(output)?;
726 self.decoder.apply(&mut output[..n]);
727 Ok(n)
728 }
729}
730
731#[cfg(test)]
732#[path = "../../../tests/rust-archive-worker/streaming_bounds.rs"]
733mod bounds;