aboutsummaryrefslogtreecommitdiff
path: root/src/zip/driver.rs
blob: e5aa58d50a13869649201ce1f6b0078d3c406c8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use crate::driver::{ArchiveRead, ArchiveWrite, Driver};
use crate::zip::error::{ZipError, ZipResult};
use crate::zip::structs::{deserialize, EOCDR64Locator, CDR, EOCDR, EOCDR64};
use crate::zip::ZipFile;
use std::collections::HashMap as Map;
use std::fs::File;
use std::io::{Read, Seek, SeekFrom, Write};

pub struct Zip<IO = File> {
    io: IO,

    files: Map<String, ZipFile>,
    comment: String,
}

impl<IO> Driver for Zip<IO> {
    type Error = ZipError;

    type IO = IO;
    type File = ZipFile;
}

impl<IO: Read + Seek> ArchiveRead for Zip<IO> {
    fn read(mut io: Self::IO) -> ZipResult<Self> {
        // Search eocdr
        let limit = 65557.min(io.seek(SeekFrom::End(0))?) as i64;
        let start = io.seek(SeekFrom::End(-limit))?;
        let pos = start + {
            let mut buf = vec![0; limit as usize];
            io.read(&mut buf)?;
            buf[..buf.len() - 18]
                .windows(4)
                .rposition(|v| u32::from_le_bytes(v.try_into().unwrap()) == 0x06054b50)
                .ok_or(ZipError::EOCDRNotFound)? as u64
        };

        // Read eocdr
        io.seek(SeekFrom::Start(pos + 4))?;
        let buf = {
            let mut buf = [0; 18];
            io.read(&mut buf)?;
            buf
        };
        let eocdr: EOCDR = deserialize(&buf).map_err(|_| ZipError::InvalidEOCDR)?;
        let comment = {
            let mut buf: Vec<u8> = vec![0; eocdr.comment_len as usize];
            io.read(&mut buf)?;
            String::from_utf8(buf).map_err(|_| ZipError::InvalidArchiveComment)?
        };

        // Try to find eocdr64locator
        io.seek(SeekFrom::Start(pos - 20))?;
        let buf = {
            let mut buf = [0; 20];
            io.read(&mut buf)?;
            buf
        };
        let (cd_pointer, cd_size, cd_records) = if u32::from_le_bytes(buf[0..4].try_into().unwrap())
            == 0x07064b50
        {
            let eocdr64locator: EOCDR64Locator =
                deserialize(&buf[4..]).map_err(|_| ZipError::InvalidEOCDR64Locator)?;

            io.seek(SeekFrom::Start(eocdr64locator.eocdr64_pointer))?;
            let buf = {
                let mut buf = [0; 56];
                io.read(&mut buf)?;
                buf
            };
            if u32::from_le_bytes(buf[0..4].try_into().unwrap()) != 0x06064b50 {
                return Err(ZipError::InvalidEOCDR64Signature.into());
            }
            let eocdr64: EOCDR64 = deserialize(&buf[4..]).map_err(|_| ZipError::InvalidEOCDR64)?;

            (eocdr64.cd_pointer, eocdr64.cd_size, eocdr64.cd_records)
        } else {
            (
                eocdr.cd_pointer as u64,
                eocdr.cd_size as u64,
                eocdr.cd_records as u64,
            )
        };

        // Read cd records
        let mut files = Map::with_capacity(cd_records as usize);
        io.seek(SeekFrom::Start(cd_pointer))?;
        let buf = {
            let mut buf = vec![0; cd_size as usize];
            io.read(&mut buf)?;
            buf
        };

        let mut p: usize = 0;
        for _ in 0..cd_records {
            if u32::from_le_bytes(buf[p..p + 4].try_into().unwrap()) != 0x02014b50 {
                return Err(ZipError::InvalidCDRSignature.into());
            }
            p += 4;
            let cdr: CDR = deserialize(&buf[p..p + 42]).map_err(|_| ZipError::InvalidCDR)?;
            p += 42;
            let name = String::from_utf8(buf[p..p + cdr.name_len as usize].into()).unwrap();
            p += cdr.name_len as usize;
            let extra_fields: Vec<u8> = buf[p..p + cdr.extra_field_len as usize].into();
            p += cdr.extra_field_len as usize;
            let comment = String::from_utf8(buf[p..p + cdr.comment_len as usize].into()).unwrap();
            p += cdr.comment_len as usize;

            files.insert(
                name.clone(),
                ZipFile::new(
                    name,
                    cdr.dos_date,
                    cdr.dos_time,
                    cdr.compression_method,
                    cdr.compressed_size as u64,
                    cdr.size as u64,
                    comment,
                ),
            );
        }

        Ok(Self { io, files, comment })
    }
}

impl<IO: Read + Seek> Zip<IO> {}

impl<IO: Read + Write + Seek> ArchiveWrite for Zip<IO> {}

impl<IO: Read + Write> Zip<IO> {}