team-ljyzipelo

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