| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | use std::io::{self, Write}; |
| 6 | |
| 7 | use flate2::Compression; |
| 8 | use flate2::write::DeflateEncoder; |
| 9 | |
| 10 | const MAX32: u64 = 0xFFFF_FFFF; |
| 11 | |
| 12 | const UTF8: u16 = 1 << 11; |
| 13 | const DEFLATE: u16 = 8; |
| 14 | |
| 15 | |
| 16 | macro_rules! le { |
| 17 | ($buf:expr, $($v:expr),+ $(,)?) => { $( $buf.extend_from_slice(&$v.to_le_bytes()); )+ }; |
| 18 | } |
| 19 | |
| 20 | /// A file's compressed bytes, ready to add. |
| 21 | pub struct Deflated { |
| 22 | data: Vec<u8>, |
| 23 | crc: u32, |
| 24 | size: u64, |
| 25 | } |
| 26 | |
| 27 | impl Deflated { |
| 28 | pub fn new(bytes: &[u8]) -> io::Result<Self> { |
| 29 | Self::with_level(bytes, Compression::default()) |
| 30 | } |
| 31 | |
| 32 | |
| 33 | |
| 34 | pub fn fast(bytes: &[u8]) -> io::Result<Self> { |
| 35 | Self::with_level(bytes, Compression::none()) |
| 36 | } |
| 37 | |
| 38 | fn with_level(bytes: &[u8], level: Compression) -> io::Result<Self> { |
| 39 | let mut enc = DeflateEncoder::new(Vec::with_capacity(bytes.len() / 4 + 64), level); |
| 40 | enc.write_all(bytes)?; |
| 41 | Ok(Self { data: enc.finish()?, crc: crc32fast::hash(bytes), size: bytes.len() as u64 }) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | struct Entry { |
| 46 | name: u32, |
| 47 | crc: u32, |
| 48 | csize: u64, |
| 49 | size: u64, |
| 50 | offset: u64, |
| 51 | } |
| 52 | |
| 53 | pub struct ZipOut<W: Write> { |
| 54 | out: W, |
| 55 | at: u64, |
| 56 | entries: Vec<Entry>, |
| 57 | date: u16, |
| 58 | } |
| 59 | |
| 60 | impl<W: Write> ZipOut<W> { |
| 61 | |
| 62 | pub fn new(out: W, (year, month, day): (i64, i64, i64)) -> Self { |
| 63 | let date = (((year - 1980).clamp(0, 127) as u16) << 9) | ((month as u16) << 5) | day as u16; |
| 64 | Self { out, at: 0, entries: Vec::new(), date } |
| 65 | } |
| 66 | |
| 67 | |
| 68 | pub fn add(&mut self, name: u32, path: &str, file: Deflated) -> io::Result<()> { |
| 69 | let csize = file.data.len() as u64; |
| 70 | let big = file.size >= MAX32 || csize >= MAX32; |
| 71 | let (c32, s32) = if big { (MAX32, MAX32) } else { (csize, file.size) }; |
| 72 | let mut h = Vec::with_capacity(50 + path.len()); |
| 73 | le!(h, 0x0403_4b50u32, if big { 45u16 } else { 20u16 }, UTF8, DEFLATE, 0u16, self.date, file.crc); |
| 74 | le!(h, c32 as u32, s32 as u32, path.len() as u16, if big { 20u16 } else { 0u16 }); |
| 75 | h.extend_from_slice(path.as_bytes()); |
| 76 | if big { |
| 77 | le!(h, 1u16, 16u16, file.size, csize); |
| 78 | } |
| 79 | self.out.write_all(&h)?; |
| 80 | self.out.write_all(&file.data)?; |
| 81 | self.entries.push(Entry { name, crc: file.crc, csize, size: file.size, offset: self.at }); |
| 82 | self.at += h.len() as u64 + csize; |
| 83 | Ok(()) |
| 84 | } |
| 85 | |
| 86 | |
| 87 | pub fn finish<'a>(mut self, path: impl Fn(u32) -> &'a str) -> io::Result<W> { |
| 88 | let start = self.at; |
| 89 | let mut h = Vec::with_capacity(256); |
| 90 | for e in &self.entries { |
| 91 | let path = path(e.name); |
| 92 | let big = e.size >= MAX32 || e.csize >= MAX32 || e.offset >= MAX32; |
| 93 | let small = |v: u64| if big { MAX32 as u32 } else { v as u32 }; |
| 94 | h.clear(); |
| 95 | le!(h, 0x0201_4b50u32, 45u16, if big { 45u16 } else { 20u16 }, UTF8, DEFLATE, 0u16, self.date, e.crc); |
| 96 | le!(h, small(e.csize), small(e.size), path.len() as u16, if big { 28u16 } else { 0u16 }); |
| 97 | le!(h, 0u16, 0u16, 0u16, 0u32, small(e.offset)); |
| 98 | h.extend_from_slice(path.as_bytes()); |
| 99 | if big { |
| 100 | le!(h, 1u16, 24u16, e.size, e.csize, e.offset); |
| 101 | } |
| 102 | self.out.write_all(&h)?; |
| 103 | self.at += h.len() as u64; |
| 104 | } |
| 105 | let (count, size) = (self.entries.len() as u64, self.at - start); |
| 106 | h.clear(); |
| 107 | if count >= 0xFFFF || size >= MAX32 || start >= MAX32 { |
| 108 | le!(h, 0x0606_4b50u32, 44u64, 45u16, 45u16, 0u32, 0u32, count, count, size, start); |
| 109 | le!(h, 0x0706_4b50u32, 0u32, self.at, 1u32); |
| 110 | } |
| 111 | let count16 = count.min(0xFFFF) as u16; |
| 112 | le!(h, 0x0605_4b50u32, 0u16, 0u16, count16, count16, size.min(MAX32) as u32, start.min(MAX32) as u32, 0u16); |
| 113 | self.out.write_all(&h)?; |
| 114 | self.out.flush()?; |
| 115 | Ok(self.out) |
| 116 | } |
| 117 | } |