aboutsummaryrefslogtreecommitdiff
path: root/src/zip/file/read.rs
blob: 56f42daf31bb81a62da897d49f0c71ebe05a46b1 (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
use crate::driver::FileDriver;
use crate::zip::{CompressionMethod, ZipError, ZipFileInfo, ZipResult};
use bzip2::read::BzDecoder;
use flate2::read::DeflateDecoder;
use std::io::{
    Error as IoError, ErrorKind as IoErrorKind, Read, Result as IoResult, Seek, SeekFrom,
};
use xz2::read::XzDecoder;

enum IoProxy<Io: Read> {
    Store(Io),
    Deflate(DeflateDecoder<Io>),
    BZip2(BzDecoder<Io>),
    XZ(XzDecoder<Io>),
}

pub struct ZipFileReader<'d, Io: Read> {
    io: IoProxy<&'d mut Io>,
    info: &'d ZipFileInfo,

    bounds: (u64, u64),
    cursor: u64,
}

impl<'d, Io: Read> FileDriver for ZipFileReader<'d, Io> {
    type Io = Io;
    type FileInfo = ZipFileInfo;

    fn info(&self) -> &Self::FileInfo {
        self.info
    }
}

impl<'d, Io: Read + Seek> ZipFileReader<'d, Io> {
    pub fn new(io: &'d mut Io, info: &'d ZipFileInfo) -> ZipResult<Self> {
        io.seek(SeekFrom::Start(info.header_pointer))?;
        let buf = {
            let mut buf = [0; 30];
            io.read(&mut buf)?;
            buf
        };
        if u32::from_le_bytes(buf[..4].try_into().unwrap()) != 0x04034b50 {
            return Err(ZipError::InvalidFileHeaderSignature.into());
        }
        let data_pointer = info.header_pointer
            + 30
            + u16::from_le_bytes(buf[26..28].try_into().unwrap()) as u64
            + u16::from_le_bytes(buf[28..30].try_into().unwrap()) as u64;
        io.seek(SeekFrom::Start(data_pointer))?;

        Ok(Self {
            io: match info.compression_method {
                CompressionMethod::Store => IoProxy::Store(io),
                CompressionMethod::Deflate => IoProxy::Deflate(DeflateDecoder::new(io)),
                CompressionMethod::BZip2 => IoProxy::BZip2(BzDecoder::new(io)),
                CompressionMethod::LZMA => IoProxy::XZ(XzDecoder::new(io)),
                CompressionMethod::XZ => IoProxy::XZ(XzDecoder::new(io)),
            },
            info,

            bounds: (data_pointer, data_pointer + info.compressed_size),
            cursor: data_pointer,
        })
    }

    pub fn seekable(&self) -> bool {
        match self.io {
            IoProxy::Store(..) => true,
            _ => false,
        }
    }
}

impl<'d, Io: Read> Read for ZipFileReader<'d, Io> {
    fn read(&mut self, buf: &mut [u8]) -> IoResult<usize> {
        let upper = buf.len().min((self.bounds.1 - self.cursor) as usize);
        let bytes = match &mut self.io {
            IoProxy::Store(io) => io.read(&mut buf[..upper]),
            IoProxy::Deflate(io) => io.read(&mut buf[..upper]),
            IoProxy::BZip2(io) => io.read(&mut buf[..upper]),
            IoProxy::XZ(io) => io.read(&mut buf[..upper]),
        }?;
        self.cursor += upper as u64;
        Ok(bytes)
    }
}

impl<'d, Io: Read + Seek> Seek for ZipFileReader<'d, Io> {
    fn seek(&mut self, pos: SeekFrom) -> IoResult<u64> {
        match &mut self.io {
            IoProxy::Store(io) => {
                self.cursor = match pos {
                    SeekFrom::Start(offset) => self.bounds.0 + offset,
                    SeekFrom::End(offset) => {
                        let cursor = self.bounds.1.saturating_add_signed(offset);
                        if cursor < self.bounds.0 {
                            return Err(IoError::new(
                                IoErrorKind::InvalidInput,
                                ZipError::NegativeFileOffset,
                            ));
                        }
                        cursor
                    }
                    SeekFrom::Current(offset) => {
                        let cursor = self.cursor.saturating_add_signed(offset);
                        if cursor < self.bounds.0 {
                            return Err(IoError::new(
                                IoErrorKind::InvalidInput,
                                ZipError::NegativeFileOffset,
                            ));
                        }
                        cursor
                    }
                }
                .min(self.bounds.1);

                Ok(io.seek(SeekFrom::Start(self.cursor))? - self.bounds.0)
            }
            _ => Err(IoError::new(
                IoErrorKind::Unsupported,
                ZipError::CompressionDataIsUnseekable,
            )),
        }
    }
}