opys-bundle 0.10.0

The opys bundle: a manifest and the blobs it names, as one zip.
Documentation
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
//! A bundle read in pieces: where each entry is in the file, and an entry
//! decoded from its own bytes alone.
//!
//! A bundle behind a URL is not downloaded to be installed. Its directory is
//! at the end of the file and says where every entry is; the head and the
//! manifest are the first two entries; a blob is fetched only when the
//! install finds it missing. This is the zip half of that — byte slices in,
//! entries out — and the requests are the runtime's.
//!
//! An entry is taken to run from its local header to the next entry's, or to
//! the directory: a zip is its entries end to end, so that range holds the
//! header, the data and whatever trails it, without trusting the local
//! header's own lengths to find where the next begins.

use std::collections::BTreeMap;
use std::io::{self, Write};
use std::ops::Range;

use flate2::write::DeflateDecoder;
use flate2::Crc;
use opys_core::Manifest;

use crate::{
    blob_entry, not_a_bundle, parse_head, parse_manifest, BundleError, HEAD_ENTRY, LOCAL_HEADER,
    LOCAL_HEADER_LEN, MANIFEST_ENTRY,
};

const END: [u8; 4] = *b"PK\x05\x06";
const END_LEN: usize = 22;
const END64_LOCATOR: [u8; 4] = *b"PK\x06\x07";
const END64_LOCATOR_LEN: usize = 20;
const END64: [u8; 4] = *b"PK\x06\x06";
const END64_LEN: usize = 56;
const CENTRAL: [u8; 4] = *b"PK\x01\x02";
const CENTRAL_LEN: usize = 46;
/// The longest a zip's comment can be, and so how far from the end its
/// end-of-directory record can sit.
const LONGEST_COMMENT: usize = u16::MAX as usize;
const ZIP64_EXTRA: u16 = 0x0001;
const ENCRYPTED: u16 = 1;

const STORED: u16 = 0;
const DEFLATED: u16 = 8;

/// One entry: where its bytes are in the file, and what they decode to.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Entry {
    range: Range<u64>,
    method: u16,
    compressed: u64,
    size: u64,
    crc: u32,
}

impl Entry {
    /// The bytes of the file that hold the entry, header to trailer.
    pub fn range(&self) -> Range<u64> {
        self.range.clone()
    }

    /// What the entry decodes to, in bytes.
    pub fn size(&self) -> u64 {
        self.size
    }

    /// A writer that takes the bytes of [`Entry::range`], in order and in
    /// pieces of any size, and writes what they decode to into `out`.
    pub fn decoder<W: Write>(&self, out: W) -> EntryDecoder<W> {
        EntryDecoder {
            entry: self.clone(),
            state: State::Header(Vec::new()),
            out: Some(out),
        }
    }

    /// The entry decoded from `raw`, the bytes of its range.
    fn decode(&self, raw: &[u8]) -> Result<Vec<u8>, BundleError> {
        let mut decoder = self.decoder(Vec::with_capacity(self.size as usize));
        decoder.write_all(raw)?;
        decoder.finish()
    }
}

/// Where every entry of a bundle is, read off the end of the file.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Directory {
    len: u64,
    entries: BTreeMap<String, Entry>,
}

/// What the last bytes of a bundle give of its directory.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Back {
    Directory(Directory),
    /// The directory begins before what was given: it takes this many bytes,
    /// counted from the end of the file — always more than were given.
    Needs(u64),
}

fn short(bytes: &[u8], at: usize) -> u16 {
    u16::from_le_bytes([bytes[at], bytes[at + 1]])
}

fn long(bytes: &[u8], at: usize) -> u32 {
    u32::from_le_bytes([bytes[at], bytes[at + 1], bytes[at + 2], bytes[at + 3]])
}

fn wide(bytes: &[u8], at: usize) -> u64 {
    let mut word = [0; 8];
    word.copy_from_slice(&bytes[at..at + 8]);
    u64::from_le_bytes(word)
}

/// The end-of-directory record's place in `tail`. It is the last thing in
/// the file but a comment, so it is looked for from the back, and a match is
/// only one whose comment runs exactly to the end.
fn end_record(tail: &[u8]) -> Option<usize> {
    let last = tail.len().checked_sub(END_LEN)?;
    (0..=last).rev().find(|&at| {
        tail[at..at + 4] == END && usize::from(short(tail, at + 20)) == tail.len() - at - END_LEN
    })
}

/// Read the directory of a bundle `file_len` bytes long from `tail`, its
/// last bytes. Asks for more, by [`Back::Needs`], until the tail holds it.
pub fn directory_from_back(tail: &[u8], file_len: u64) -> Result<Back, BundleError> {
    let given = tail.len() as u64;
    if given > file_len {
        return Err(not_a_bundle("it is shorter than its own tail"));
    }
    // Where an offset counted from the start of the file is in the tail.
    let at = |offset: u64| offset.checked_sub(file_len - given).map(|at| at as usize);
    let short_of = |offset: u64| Ok(Back::Needs(file_len - offset));

    let Some(end) = end_record(tail) else {
        let reach = file_len.min((END_LEN + LONGEST_COMMENT) as u64);
        return if given < reach {
            Ok(Back::Needs(reach))
        } else {
            Err(not_a_bundle("it has no zip directory"))
        };
    };
    let (mut count, mut size, mut offset) = (
        u64::from(short(tail, end + 10)),
        u64::from(long(tail, end + 12)),
        u64::from(long(tail, end + 16)),
    );
    // A zip too large for the classic record says so with all ones and keeps
    // the real numbers in a zip64 record, found through a locator right
    // before the classic one.
    if count == u64::from(u16::MAX) || size == u64::from(u32::MAX) || offset == u64::from(u32::MAX)
    {
        let locator = end
            .checked_sub(END64_LOCATOR_LEN)
            .filter(|&locator| tail[locator..locator + 4] == END64_LOCATOR)
            .ok_or_else(|| not_a_bundle("its zip64 directory has no locator"))?;
        let record = wide(tail, locator + 8);
        let Some(record) = at(record) else {
            return short_of(record);
        };
        if tail
            .get(record..record + END64_LEN)
            .map_or(true, |r| r[..4] != END64)
        {
            return Err(not_a_bundle(
                "its zip64 directory is not where its locator says",
            ));
        }
        (count, size, offset) = (
            wide(tail, record + 32),
            wide(tail, record + 40),
            wide(tail, record + 48),
        );
    }
    let Some(start) = at(offset) else {
        return short_of(offset);
    };
    let central = usize::try_from(size)
        .ok()
        .and_then(|size| tail.get(start..start.checked_add(size)?))
        .ok_or_else(|| not_a_bundle("its directory runs past its end"))?;

    let mut found: Vec<(String, Entry)> = Vec::new();
    let mut at_entry = 0;
    while at_entry < central.len() {
        let fixed = central
            .get(at_entry..at_entry + CENTRAL_LEN)
            .filter(|fixed| fixed[..4] == CENTRAL)
            .ok_or_else(|| not_a_bundle("its directory holds something that is not an entry"))?;
        if short(fixed, 8) & ENCRYPTED != 0 {
            return Err(not_a_bundle("it has an encrypted entry"));
        }
        let (name_len, extra_len, comment_len) = (
            usize::from(short(fixed, 28)),
            usize::from(short(fixed, 30)),
            usize::from(short(fixed, 32)),
        );
        let name_at = at_entry + CENTRAL_LEN;
        let extra_at = name_at + name_len;
        let next = extra_at + extra_len + comment_len;
        let (name, extra) = central
            .get(name_at..extra_at)
            .zip(central.get(extra_at..extra_at + extra_len))
            .filter(|_| next <= central.len())
            .ok_or_else(|| not_a_bundle("an entry of its directory runs past it"))?;
        let name = std::str::from_utf8(name)
            .map_err(|_| not_a_bundle("an entry's name is not UTF-8"))?
            .to_owned();

        let mut size = u64::from(long(fixed, 24));
        let mut compressed = u64::from(long(fixed, 20));
        let mut local = u64::from(long(fixed, 42));
        let mut wide_values = zip64_values(extra)?.into_iter();
        for value in [&mut size, &mut compressed, &mut local] {
            if *value == u64::from(u32::MAX) {
                *value = wide_values
                    .next()
                    .ok_or_else(|| not_a_bundle("an entry lacks its zip64 sizes"))?;
            }
        }
        found.push((
            name,
            Entry {
                range: local..local,
                method: short(fixed, 10),
                compressed,
                size,
                crc: long(fixed, 16),
            },
        ));
        at_entry = next;
    }
    if found.len() as u64 != count {
        return Err(not_a_bundle(
            "its directory does not hold the entries it counts",
        ));
    }

    // Each entry runs to where the next one starts, the last to the
    // directory.
    found.sort_by_key(|(_, entry)| entry.range.start);
    let ends: Vec<u64> = found
        .iter()
        .skip(1)
        .map(|(_, entry)| entry.range.start)
        .chain([offset])
        .collect();
    let mut entries = BTreeMap::new();
    for ((name, mut entry), end) in found.into_iter().zip(ends) {
        if end < entry.range.start + LOCAL_HEADER_LEN as u64 + entry.compressed {
            return Err(not_a_bundle("its entries overlap"));
        }
        entry.range.end = end;
        if entries.insert(name, entry).is_some() {
            return Err(not_a_bundle("it names one entry twice"));
        }
    }
    Ok(Back::Directory(Directory {
        len: file_len,
        entries,
    }))
}

/// The zip64 sizes and offset an entry's extra field holds, in the order
/// they stand in for the classic ones.
fn zip64_values(mut extra: &[u8]) -> Result<Vec<u64>, BundleError> {
    while extra.len() >= 4 {
        let (id, len) = (short(extra, 0), usize::from(short(extra, 2)));
        let body = extra
            .get(4..4 + len)
            .ok_or_else(|| not_a_bundle("an entry's extra field runs past it"))?;
        if id == ZIP64_EXTRA {
            return Ok(body.chunks_exact(8).map(|word| wide(word, 0)).collect());
        }
        extra = &extra[4 + len..];
    }
    Ok(Vec::new())
}

impl Directory {
    /// How long the bundle is.
    pub fn file_len(&self) -> u64 {
        self.len
    }

    /// The bytes that hold the head and the manifest: the two entries an
    /// install reads before it knows which blobs it wants.
    pub fn front(&self) -> Result<Range<u64>, BundleError> {
        let head = self.entry(HEAD_ENTRY)?;
        let manifest = self.entry(MANIFEST_ENTRY)?;
        Ok(head.range.start.min(manifest.range.start)..head.range.end.max(manifest.range.end))
    }

    /// The manifest, from `front`, the bytes of [`Directory::front`]. Makes
    /// the promises [`crate::open_bundle`] makes: the head is in this
    /// reader's format, read before the manifest is, and every blob the
    /// manifest names is in the bundle.
    pub fn open(&self, front: &[u8]) -> Result<Manifest, BundleError> {
        let base = self.front()?.start;
        let entry_bytes = |name: &'static str| {
            let entry = self.entry(name)?;
            let raw = front
                .get((entry.range.start - base) as usize..(entry.range.end - base) as usize)
                .ok_or_else(|| not_a_bundle("its front is shorter than its directory says"))?;
            entry.decode(raw)
        };
        parse_head(&entry_bytes(HEAD_ENTRY)?)?;
        let manifest = parse_manifest(&entry_bytes(MANIFEST_ENTRY)?)?;
        for id in manifest.blob_ids() {
            self.blob(id)?;
        }
        Ok(manifest)
    }

    /// Where the blob `id` is.
    pub fn blob(&self, id: &str) -> Result<&Entry, BundleError> {
        self.entries
            .get(&blob_entry(id))
            .ok_or_else(|| BundleError::MissingBlob(id.to_owned()))
    }

    fn entry(&self, name: &'static str) -> Result<&Entry, BundleError> {
        self.entries
            .get(name)
            .ok_or(BundleError::MissingEntry(name))
    }
}

/// Where an [`EntryDecoder`] is in the bytes it is given.
enum State<W: Write> {
    /// The local header, gathered until its names and extras are in.
    Header(Vec<u8>),
    /// The data, this much of it still to come.
    Data { left: u64, sink: Sink<W> },
    /// Past the data: a trailer, or nothing.
    Done(Checked<W>),
}

enum Sink<W: Write> {
    Stored(Checked<W>),
    Deflated(DeflateDecoder<Checked<W>>),
}

/// Counts and checksums what is written through it.
struct Checked<W> {
    inner: W,
    crc: Crc,
}

impl<W: Write> Write for Checked<W> {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        let written = self.inner.write(buf)?;
        self.crc.update(&buf[..written]);
        Ok(written)
    }

    fn flush(&mut self) -> io::Result<()> {
        self.inner.flush()
    }
}

/// Decodes an entry from the bytes of its range, as they arrive.
pub struct EntryDecoder<W: Write> {
    entry: Entry,
    state: State<W>,
    out: Option<W>,
}

fn invalid(why: &'static str) -> io::Error {
    io::Error::new(io::ErrorKind::InvalidData, why)
}

impl<W: Write> EntryDecoder<W> {
    /// Once the local header is whole: where the data starts in it, and the
    /// state that reads the data the way the directory says.
    fn begin(&mut self, header: &[u8]) -> io::Result<Option<(usize, State<W>)>> {
        if header.len() < LOCAL_HEADER_LEN {
            return Ok(None);
        }
        if header[..4] != LOCAL_HEADER {
            return Err(invalid("an entry does not begin with its local header"));
        }
        let start =
            LOCAL_HEADER_LEN + usize::from(short(header, 26)) + usize::from(short(header, 28));
        if header.len() < start {
            return Ok(None);
        }
        let out = Checked {
            inner: self.out.take().expect("the sink is made once"),
            crc: Crc::new(),
        };
        let sink = match self.entry.method {
            STORED => Sink::Stored(out),
            DEFLATED => Sink::Deflated(DeflateDecoder::new(out)),
            _ => return Err(invalid("an entry is compressed in a way a bundle is not")),
        };
        let data = State::Data {
            left: self.entry.compressed,
            sink,
        };
        Ok(Some((start, data)))
    }

    /// Where the decoded bytes go, to take what has been written so far.
    pub fn get_mut(&mut self) -> &mut W {
        match &mut self.state {
            State::Header(_) => self.out.as_mut().expect("the sink is made once"),
            State::Data {
                sink: Sink::Stored(out),
                ..
            }
            | State::Done(out) => &mut out.inner,
            State::Data {
                sink: Sink::Deflated(out),
                ..
            } => &mut out.get_mut().inner,
        }
    }

    /// Check what was written and hand back where it went.
    pub fn finish(self) -> Result<W, BundleError> {
        let checked = match self.state {
            State::Done(checked) => checked,
            State::Data { left: 0, sink } => finish_sink(sink)?,
            _ => return Err(not_a_bundle("an entry ends before its data does")),
        };
        if checked.crc.amount() as u64 != self.entry.size & u64::from(u32::MAX)
            || checked.crc.sum() != self.entry.crc
        {
            return Err(not_a_bundle(
                "an entry does not decode to what its directory says",
            ));
        }
        Ok(checked.inner)
    }
}

fn finish_sink<W: Write>(sink: Sink<W>) -> io::Result<Checked<W>> {
    match sink {
        Sink::Stored(checked) => Ok(checked),
        Sink::Deflated(decoder) => decoder.finish(),
    }
}

impl<W: Write> Write for EntryDecoder<W> {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        let mut rest = buf;
        while !rest.is_empty() {
            self.state = match std::mem::replace(&mut self.state, State::Header(Vec::new())) {
                State::Header(mut header) => {
                    let before = header.len();
                    header.extend_from_slice(rest);
                    match self.begin(&header)? {
                        // What came in past the header is data.
                        Some((start, data)) => {
                            rest = &rest[start - before..];
                            data
                        }
                        None => {
                            rest = &[];
                            State::Header(header)
                        }
                    }
                }
                State::Data { left, mut sink } => {
                    let take = rest.len().min(usize::try_from(left).unwrap_or(usize::MAX));
                    match &mut sink {
                        Sink::Stored(out) => out.write_all(&rest[..take])?,
                        Sink::Deflated(out) => out.write_all(&rest[..take])?,
                    }
                    rest = &rest[take..];
                    match left - take as u64 {
                        0 => State::Done(finish_sink(sink)?),
                        left => State::Data { left, sink },
                    }
                }
                // A data descriptor, which the directory already had.
                done @ State::Done(_) => {
                    rest = &[];
                    done
                }
            };
        }
        Ok(buf.len())
    }

    fn flush(&mut self) -> io::Result<()> {
        match &mut self.state {
            State::Data {
                sink: Sink::Stored(out),
                ..
            }
            | State::Done(out) => out.flush(),
            State::Data {
                sink: Sink::Deflated(out),
                ..
            } => out.flush(),
            State::Header(_) => Ok(()),
        }
    }
}