• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 //! Utilities for zip handling
18 
19 use anyhow::{bail, Result};
20 use bytes::{Buf, BufMut};
21 use std::io::{Read, Seek, SeekFrom};
22 use zip::ZipArchive;
23 
24 const EOCD_MIN_SIZE: usize = 22;
25 const EOCD_CENTRAL_DIRECTORY_SIZE_FIELD_OFFSET: usize = 12;
26 const EOCD_CENTRAL_DIRECTORY_OFFSET_FIELD_OFFSET: usize = 16;
27 const EOCD_MAGIC: u32 = 0x06054b50;
28 const ZIP64_MARK: u32 = 0xffffffff;
29 
30 #[derive(Debug, PartialEq)]
31 pub struct ZipSections {
32     pub central_directory_offset: u32,
33     pub central_directory_size: u32,
34     pub eocd_offset: u32,
35     pub eocd_size: u32,
36 }
37 
38 /// Discover the layout of a zip file.
zip_sections<R: Read + Seek>(mut reader: R) -> Result<(R, ZipSections)>39 pub fn zip_sections<R: Read + Seek>(mut reader: R) -> Result<(R, ZipSections)> {
40     // open a zip to parse EOCD
41     let archive = ZipArchive::new(reader)?;
42     let eocd_size = archive.comment().len() + EOCD_MIN_SIZE;
43     if archive.offset() != 0 {
44         bail!("Invalid ZIP: offset should be 0, but {}.", archive.offset());
45     }
46     // retrieve reader back
47     reader = archive.into_inner();
48     // the current position should point EOCD offset
49     let eocd_offset = reader.seek(SeekFrom::Current(0))? as u32;
50     let mut eocd = vec![0u8; eocd_size as usize];
51     reader.read_exact(&mut eocd)?;
52     if (&eocd[0..]).get_u32_le() != EOCD_MAGIC {
53         bail!("Invalid ZIP: ZipArchive::new() should point EOCD after reading.");
54     }
55     let (central_directory_size, central_directory_offset) = get_central_directory(&eocd)?;
56     if central_directory_offset == ZIP64_MARK || central_directory_size == ZIP64_MARK {
57         bail!("Unsupported ZIP: ZIP64 is not supported.");
58     }
59     if central_directory_offset + central_directory_size != eocd_offset {
60         bail!("Invalid ZIP: EOCD should follow CD with no extra data or overlap.");
61     }
62 
63     Ok((
64         reader,
65         ZipSections {
66             central_directory_offset,
67             central_directory_size,
68             eocd_offset,
69             eocd_size: eocd_size as u32,
70         },
71     ))
72 }
73 
get_central_directory(buf: &[u8]) -> Result<(u32, u32)>74 fn get_central_directory(buf: &[u8]) -> Result<(u32, u32)> {
75     if buf.len() < EOCD_MIN_SIZE {
76         bail!("Invalid EOCD size: {}", buf.len());
77     }
78     let mut buf = &buf[EOCD_CENTRAL_DIRECTORY_SIZE_FIELD_OFFSET..];
79     let size = buf.get_u32_le();
80     let offset = buf.get_u32_le();
81     Ok((size, offset))
82 }
83 
84 /// Update EOCD's central_directory_offset field.
set_central_directory_offset(buf: &mut [u8], value: u32) -> Result<()>85 pub fn set_central_directory_offset(buf: &mut [u8], value: u32) -> Result<()> {
86     if buf.len() < EOCD_MIN_SIZE {
87         bail!("Invalid EOCD size: {}", buf.len());
88     }
89     (&mut buf[EOCD_CENTRAL_DIRECTORY_OFFSET_FIELD_OFFSET..]).put_u32_le(value);
90     Ok(())
91 }
92 
93 #[cfg(test)]
94 mod tests {
95     use super::*;
96     use crate::testing::assert_contains;
97     use std::io::{Cursor, Write};
98     use zip::{write::FileOptions, ZipWriter};
99 
create_test_zip() -> Cursor<Vec<u8>>100     fn create_test_zip() -> Cursor<Vec<u8>> {
101         let mut writer = ZipWriter::new(Cursor::new(Vec::new()));
102         writer.start_file("testfile", FileOptions::default()).unwrap();
103         writer.write_all(b"testcontent").unwrap();
104         writer.finish().unwrap()
105     }
106 
107     #[test]
test_zip_sections()108     fn test_zip_sections() {
109         let (cursor, sections) = zip_sections(create_test_zip()).unwrap();
110         assert_eq!(sections.eocd_offset, (cursor.get_ref().len() - EOCD_MIN_SIZE) as u32);
111     }
112 
113     #[test]
test_reject_if_extra_data_between_cd_and_eocd()114     fn test_reject_if_extra_data_between_cd_and_eocd() {
115         // prepare normal zip
116         let buf = create_test_zip().into_inner();
117 
118         // insert garbage between CD and EOCD.
119         // by the way, to mock zip-rs, use CD as garbage. This is implementation detail of zip-rs,
120         // which reads CD at (eocd_offset - cd_size) instead of at cd_offset from EOCD.
121         let (pre_eocd, eocd) = buf.split_at(buf.len() - EOCD_MIN_SIZE);
122         let (_, cd_offset) = get_central_directory(eocd).unwrap();
123         let cd = &pre_eocd[cd_offset as usize..];
124 
125         // ZipArchive::new() succeeds, but we should reject
126         let res = zip_sections(Cursor::new([pre_eocd, cd, eocd].concat()));
127         assert!(res.is_err());
128         assert_contains(&res.err().unwrap().to_string(), "Invalid ZIP: offset should be 0");
129     }
130 }
131