systemless 0.119.0

High-Level Emulation for classic Macintosh applications
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
//! Compact Pro containers and Cyclos Smaller Installer payloads share fork streams.
//! Recognition uses directory checksums or installer resources, never a filename.

const MAX_EXPANDED: usize = 256 * 1024 * 1024;

pub(crate) struct File {
    pub name: String,
    pub file_type: [u8; 4],
    pub creator: [u8; 4],
    pub finder_flags: u16,
    pub data: Vec<u8>,
    pub rsrc: Vec<u8>,
}

struct Entry {
    name: String,
    metadata: [u8; 45],
}

fn take<'a>(bytes: &'a [u8], position: &mut usize, count: usize) -> Result<&'a [u8], String> {
    let end = position
        .checked_add(count)
        .ok_or("installer offset overflow")?;
    let value = bytes
        .get(*position..end)
        .ok_or("truncated Smaller Installer")?;
    *position = end;
    Ok(value)
}

fn word(bytes: &[u8]) -> usize {
    u16::from_be_bytes([bytes[0], bytes[1]]) as usize
}

fn long(bytes: &[u8]) -> usize {
    u32::from_be_bytes(bytes[..4].try_into().unwrap()) as usize
}

fn directory(
    bytes: &[u8],
    position: &mut usize,
    count: usize,
    prefix: &str,
    depth: usize,
    entries: &mut Vec<Entry>,
) -> Result<(), String> {
    if depth > 32 {
        return Err("Smaller Installer folder nesting exceeds limit".into());
    }
    let mut remaining = count;
    while remaining > 0 {
        let size = take(bytes, position, 1)?[0];
        let name =
            crate::trap::types::decode_mac_roman(take(bytes, position, (size & 127) as usize)?);
        if name.is_empty() || matches!(name.as_str(), "." | "..") || name.contains([':', '\0']) {
            return Err("invalid Smaller Installer path component".into());
        }
        // HFS permits a literal slash in a basename. Encode the component before
        // adding VFS hierarchy separators, as the StuffIt loader does.
        let name = crate::trap::TrapDispatcher::encode_hfs_component_for_vfs(&name);
        let path = if prefix.is_empty() {
            name
        } else {
            format!("{prefix}/{name}")
        };
        remaining -= 1;
        if size & 128 != 0 {
            let children = word(take(bytes, position, 2)?);
            if children > remaining {
                return Err("invalid Smaller Installer folder count".into());
            }
            directory(bytes, position, children, &path, depth + 1, entries)?;
            remaining -= children;
        } else {
            entries.push(Entry {
                name: path,
                metadata: take(bytes, position, 45)?.try_into().unwrap(),
            });
        }
    }
    Ok(())
}

pub(crate) fn expand(bytes: &[u8], resources: &[u8]) -> Result<Option<Vec<File>>, String> {
    if !resources
        .windows(b"Smaller Installer".len())
        .any(|window| window == b"Smaller Installer")
    {
        return Ok(None);
    }
    let mut position = 4;
    let count = word(take(bytes, &mut position, 2)?);
    take(bytes, &mut position, 4)?;
    if count == 0 {
        return Err("empty Smaller Installer directory".into());
    }
    let mut entries = Vec::new();
    directory(bytes, &mut position, count, "", 0, &mut entries)?;
    expand_entries(bytes, entries, position, 0..position).map(Some)
}

/// Recognize a normal Compact Pro container by its header and directory CRC.
/// Check metadata before interpreting names, so arbitrary data forks are untouched.
pub(crate) fn expand_compact_pro(bytes: &[u8]) -> Result<Option<Vec<File>>, String> {
    if bytes.len() < 15 || bytes[0] != 1 || bytes[1] != 1 {
        return Ok(None);
    }
    let index = long(&bytes[4..8]);
    if index < 8 || index.checked_add(7).is_none_or(|end| end > bytes.len()) {
        return Ok(None);
    }
    let count = word(&bytes[index + 4..index + 6]);
    let mut end = index + 7;
    // The comment precedes the flattened directory records.
    if take(bytes, &mut end, bytes[index + 6] as usize).is_err() {
        return Ok(None);
    }
    let records_start = end;
    for _ in 0..count {
        let Ok(size) = take(bytes, &mut end, 1) else {
            return Ok(None);
        };
        let size = size[0];
        if take(bytes, &mut end, (size & 127) as usize).is_err()
            || take(bytes, &mut end, if size & 128 != 0 { 2 } else { 45 }).is_err()
        {
            return Ok(None);
        }
    }
    if crc(bytes[index + 4..end].iter().copied()) != long(&bytes[index..]) as u32 {
        return Ok(None);
    }
    if count == 0 {
        return Err("empty Compact Pro directory".into());
    }
    let mut entries = Vec::new();
    let mut position = records_start;
    directory(bytes, &mut position, count, "", 0, &mut entries)?;
    expand_entries(bytes, entries, 8, index..end).map(Some)
}

fn expand_entries(
    bytes: &[u8],
    entries: Vec<Entry>,
    minimum_fork_offset: usize,
    directory_range: std::ops::Range<usize>,
) -> Result<Vec<File>, String> {
    let mut expanded = 0usize;
    let mut files = Vec::new();
    let mut names = std::collections::BTreeSet::new();
    for entry in entries {
        let m = entry.metadata;
        let flags = word(&m[27..]);
        if m[0] != 1 || flags & !6 != 0 {
            return Err("unsupported Smaller Installer volume or flags".into());
        }
        if !names.insert(entry.name.to_lowercase()) {
            return Err("duplicate Smaller Installer path".into());
        }
        let resource_len = long(&m[29..]);
        let data_len = long(&m[33..]);
        expanded = expanded
            .checked_add(resource_len)
            .and_then(|size| size.checked_add(data_len))
            .ok_or("installer expansion overflow")?;
        if expanded > MAX_EXPANDED {
            return Err("Smaller Installer expanded size exceeds limit".into());
        }
        let mut offset = long(&m[1..]);
        let fork_end = offset
            .checked_add(long(&m[37..]))
            .and_then(|end| end.checked_add(long(&m[41..])))
            .ok_or("Compact Pro fork offset overflow")?;
        // Installer payloads have a leading directory; normal containers may
        // store forks on either side of their trailing directory.
        if offset < minimum_fork_offset
            || (offset < directory_range.end && fork_end > directory_range.start)
        {
            return Err("Compact Pro fork overlaps header or directory".into());
        }
        let rp = take(bytes, &mut offset, long(&m[37..]))?;
        let dp = take(bytes, &mut offset, long(&m[41..]))?;
        let rsrc = decode(rp, resource_len, flags & 2 != 0)?;
        let data = decode(dp, data_len, flags & 4 != 0)?;
        if crc(rsrc.iter().chain(data.iter()).copied()) != long(&m[23..]) as u32 {
            return Err(format!(
                "Smaller Installer checksum mismatch: {}",
                entry.name
            ));
        }
        files.push(File {
            name: entry.name,
            file_type: m[5..9].try_into().unwrap(),
            creator: m[9..13].try_into().unwrap(),
            finder_flags: word(&m[21..]) as u16,
            data,
            rsrc,
        });
    }
    Ok(files)
}

fn crc(bytes: impl Iterator<Item = u8>) -> u32 {
    let mut value = u32::MAX;
    for byte in bytes {
        value ^= byte as u32;
        for _ in 0..8 {
            value = (value >> 1) ^ (0xedb88320 & 0u32.wrapping_sub(value & 1));
        }
    }
    value
}

struct Bits<'a> {
    bytes: &'a [u8],
    position: usize,
}
impl Bits<'_> {
    fn read(&mut self, count: usize) -> Result<usize, String> {
        let mut value = 0;
        for _ in 0..count {
            let byte = self
                .bytes
                .get(self.position / 8)
                .ok_or("truncated Compact Pro bitstream")?;
            value = (value << 1) | ((byte >> (7 - self.position % 8)) & 1) as usize;
            self.position += 1;
        }
        Ok(value)
    }
}

#[derive(Default)]
struct Code {
    symbols: Vec<u8>,
    counts: [usize; 16],
    first_codes: [usize; 16],
    first_symbols: [usize; 16],
}
impl Code {
    fn read(bits: &mut Bits<'_>, count: usize) -> Result<Self, String> {
        let packed_count = bits.read(8)?;
        if packed_count * 2 > count {
            return Err("oversized Compact Pro prefix table".into());
        }
        let mut lengths = Vec::new();
        for pair in 0..packed_count {
            let byte = bits.read(8)?;
            for (symbol, length) in [(pair * 2, byte >> 4), (pair * 2 + 1, byte & 15)] {
                if length != 0 {
                    lengths.push((length, symbol));
                }
            }
        }
        lengths.sort_unstable();
        let mut code = 0;
        let mut previous = 0;
        let mut table = Self::default();
        for (length, symbol) in lengths {
            code <<= length - previous;
            if code >= 1 << length {
                return Err("oversubscribed Compact Pro prefix table".into());
            }
            if table.counts[length] == 0 {
                table.first_codes[length] = code;
                table.first_symbols[length] = table.symbols.len();
            }
            table.counts[length] += 1;
            table.symbols.push(symbol as u8);
            code += 1;
            previous = length;
        }
        Ok(table)
    }
    fn symbol(&self, bits: &mut Bits<'_>) -> Result<u8, String> {
        let mut code = 0;
        for length in 1..=15 {
            code = (code << 1) | bits.read(1)?;
            if let Some(index) = code.checked_sub(self.first_codes[length]) {
                if index < self.counts[length] {
                    return Ok(self.symbols[self.first_symbols[length] + index]);
                }
            }
        }
        Err("invalid Compact Pro prefix code".into())
    }
}

struct Lzh<'a> {
    bits: Bits<'a>,
    codes: [Code; 3],
    block_start: usize,
    charge: usize,
    initialized: bool,
    window: [u8; 8192],
    cursor: usize,
    distance: usize,
    remaining: usize,
}
impl<'a> Lzh<'a> {
    fn new(bytes: &'a [u8]) -> Self {
        Self {
            bits: Bits { bytes, position: 0 },
            codes: Default::default(),
            block_start: 0,
            charge: 0,
            initialized: false,
            window: [0; 8192],
            cursor: 0,
            distance: 0,
            remaining: 0,
        }
    }
    fn next(&mut self) -> Result<u8, String> {
        if self.remaining == 0 {
            if !self.initialized || self.charge >= 0x1fff0 {
                if self.initialized {
                    self.bits.position = self.bits.position.div_ceil(8) * 8;
                    let padding = if (self.bits.position / 8 - self.block_start) & 1 == 0 {
                        2
                    } else {
                        3
                    };
                    self.bits.read(padding * 8)?;
                }
                self.codes = [
                    Code::read(&mut self.bits, 256)?,
                    Code::read(&mut self.bits, 64)?,
                    Code::read(&mut self.bits, 128)?,
                ];
                self.block_start = self.bits.position / 8;
                self.charge = 0;
                self.initialized = true;
            }
            if self.bits.read(1)? != 0 {
                self.charge += 2;
                let byte = self.codes[0].symbol(&mut self.bits)?;
                return Ok(self.record(byte));
            }
            self.charge += 3;
            self.remaining = self.codes[1].symbol(&mut self.bits)? as usize;
            if self.remaining == 0 {
                return Err("zero-length Compact Pro match".into());
            }
            self.distance =
                ((self.codes[2].symbol(&mut self.bits)? as usize) << 6) | self.bits.read(6)?;
        }
        self.remaining -= 1;
        let byte = self.window[self.cursor.wrapping_sub(self.distance) & 8191];
        Ok(self.record(byte))
    }
    fn record(&mut self, byte: u8) -> u8 {
        self.window[self.cursor & 8191] = byte;
        self.cursor = self.cursor.wrapping_add(1);
        byte
    }
}

fn decode(bytes: &[u8], size: usize, compressed: bool) -> Result<Vec<u8>, String> {
    if size > MAX_EXPANDED {
        return Err("Compact Pro fork exceeds limit".into());
    }
    if size == 0 {
        return if bytes.is_empty() {
            Ok(Vec::new())
        } else {
            Err("nonempty packed zero-length fork".into())
        };
    }
    let mut lzh = compressed.then(|| Lzh::new(bytes));
    let mut position = 0;
    let mut next = || -> Result<u8, String> {
        if let Some(stream) = &mut lzh {
            stream.next()
        } else {
            Ok(take(bytes, &mut position, 1)?[0])
        }
    };
    let mut output = Vec::with_capacity(size);
    let mut previous = 0;
    let mut escaped = false;
    while output.len() < size {
        let byte = if escaped { 0x81 } else { next()? };
        escaped = false;
        if byte != 0x81 {
            output.push(byte);
            previous = byte;
            continue;
        }
        match next()? {
            0x82 => match next()? {
                0 => {
                    output.extend_from_slice(&[0x81, 0x82]);
                    previous = 0x82;
                }
                count @ 2..=255 => {
                    let run = count as usize - 1;
                    if run > size - output.len() {
                        return Err("Compact Pro run exceeds fork length".into());
                    }
                    output.resize(output.len() + run, previous);
                }
                _ => return Err("invalid Compact Pro repeat count".into()),
            },
            0x81 => {
                output.push(0x81);
                previous = 0x81;
                escaped = true;
            }
            byte => {
                output.extend_from_slice(&[0x81, byte]);
                previous = byte;
            }
        }
        if output.len() > size {
            return Err("Compact Pro escape exceeds fork length".into());
        }
    }
    Ok(output)
}

#[cfg(test)]
mod tests {
    use super::*;
    fn simple_tables() -> Vec<u8> {
        // Only literal A, length 3 and offset high part 0 have a one-bit code.
        let mut bytes = vec![33];
        bytes.extend_from_slice(&[0; 32]);
        bytes.extend_from_slice(&[1, 2, 0, 1, 1, 0x10]);
        bytes
    }
    #[test]
    fn lzh_copies_overlapping_window_matches() {
        let mut bytes = simple_tables();
        // Literal A, then a three-byte match one byte back.
        bytes.extend_from_slice(&[0x80, 0x20]);
        assert_eq!(decode(&bytes, 4, true).unwrap(), b"AAAA");
        assert!(decode(&bytes[..bytes.len() - 1], 4, true).is_err());
        assert!(decode(&[2, 0x11, 0x10], 1, true).is_err());
    }
    #[test]
    fn lzh_refreshes_tables_at_block_boundary() {
        let mut bytes = simple_tables();
        // 65,528 literals exhaust a 0x1fff0 token budget, then aligned padding.
        bytes.extend_from_slice(&[0xaa; 16_382]);
        bytes.extend_from_slice(&[0; 2]);
        bytes.extend_from_slice(&simple_tables());
        bytes.push(0x80);
        assert_eq!(decode(&bytes, 65_529, true).unwrap(), vec![b'A'; 65_529]);
    }
    #[test]
    fn rle_handles_runs_and_escape_pairs() {
        assert_eq!(decode(&[b'A', 0x81, 0x82, 4], 4, false).unwrap(), b"AAAA");
        assert_eq!(decode(&[0x81, 0x82, 0], 2, false).unwrap(), [0x81, 0x82]);
        assert_eq!(decode(&[0x81, b'B'], 2, false).unwrap(), [0x81, b'B']);
        assert_eq!(
            decode(&[0x81, 0x81, b'B'], 3, false).unwrap(),
            [0x81, 0x81, b'B']
        );
    }
    #[test]
    fn malformed_streams_fail_without_partial_forks() {
        for stream in [
            &[0x81][..],
            &[0x81, 0x82][..],
            &[0x81, 0x82, 1][..],
            &[b'A', 0x81, 0x82, 255][..],
        ] {
            assert!(decode(stream, 4, false).is_err());
        }
        assert!(decode(&[0x81, b'B'], 1, false).is_err());
        assert!(decode(&[129], 10, true).is_err());
        assert!(decode(&[1], 0, false).is_err());
    }
    fn fixture() -> Vec<u8> {
        let mut bytes = vec![0; 10];
        bytes[4..6].copy_from_slice(&1u16.to_be_bytes());
        bytes.extend_from_slice(&[4, b'T', b'e', b's', b't']);
        let mut m = [0; 45];
        m[0] = 1;
        m[1..5].copy_from_slice(&60u32.to_be_bytes());
        m[5..9].copy_from_slice(b"TEXT");
        m[9..13].copy_from_slice(b"ttxt");
        m[23..27].copy_from_slice(&crc([b'A'].into_iter()).to_be_bytes());
        m[33..37].copy_from_slice(&1u32.to_be_bytes());
        m[41..45].copy_from_slice(&1u32.to_be_bytes());
        bytes.extend_from_slice(&m);
        bytes.push(b'A');
        bytes
    }
    fn compact_fixture() -> Vec<u8> {
        let installer = fixture();
        let mut bytes = vec![1, 1, 0, 0, 0, 0, 0, 9, b'A'];
        bytes.extend_from_slice(&[0, 0, 0, 0, 0, 1, 0]);
        bytes.extend_from_slice(&installer[10..60]);
        bytes[22..26].copy_from_slice(&8u32.to_be_bytes());
        seal_directory(&mut bytes);
        bytes
    }
    fn seal_directory(bytes: &mut [u8]) {
        let index = long(&bytes[4..8]);
        let checksum = crc(bytes[index + 4..].iter().copied());
        bytes[index..index + 4].copy_from_slice(&checksum.to_be_bytes());
    }
    #[test]
    fn compact_pro_recognizes_checked_container_and_preserves_metadata() {
        let bytes = compact_fixture();
        let files = expand_compact_pro(&bytes).unwrap().unwrap();
        assert_eq!(files.len(), 1);
        assert_eq!(files[0].name, "Test");
        assert_eq!(files[0].data, b"A");
        assert!(files[0].rsrc.is_empty());
        assert_eq!(files[0].file_type, *b"TEXT");
        assert_eq!(files[0].creator, *b"ttxt");
        for length in 0..bytes.len() {
            assert!(expand_compact_pro(&bytes[..length]).unwrap().is_none());
        }
        let mut corrupt = bytes.clone();
        corrupt[17] ^= 1;
        assert!(expand_compact_pro(&corrupt).unwrap().is_none());
        let mut corrupt = bytes;
        corrupt[8] = b'B';
        assert!(expand_compact_pro(&corrupt).is_err());
    }
    #[test]
    fn compact_pro_keeps_folder_paths_and_resource_only_applications() {
        let mut bytes = compact_fixture();
        bytes[26..30].copy_from_slice(b"APPL");
        bytes[42..44].copy_from_slice(&0x4000u16.to_be_bytes());
        bytes[50..54].copy_from_slice(&1u32.to_be_bytes());
        bytes[54..58].copy_from_slice(&0u32.to_be_bytes());
        bytes[58..62].copy_from_slice(&1u32.to_be_bytes());
        bytes[62..66].copy_from_slice(&0u32.to_be_bytes());
        bytes[13..15].copy_from_slice(&2u16.to_be_bytes());
        bytes.splice(16..16, [0x84, b'D', b'o', b'c', b's', 0, 1]);
        seal_directory(&mut bytes);
        let files = expand_compact_pro(&bytes).unwrap().unwrap();
        assert_eq!(files[0].name, "Docs/Test");
        assert!(files[0].data.is_empty());
        assert_eq!(files[0].rsrc, b"A");
        assert_eq!(files[0].file_type, *b"APPL");
        assert_eq!(files[0].finder_flags, 0x4000);
    }
    #[test]
    fn compact_pro_preserves_literal_hfs_slashes_within_components() {
        let mut bytes = compact_fixture();
        bytes[18] = b'/';
        seal_directory(&mut bytes);
        let files = expand_compact_pro(&bytes).unwrap().unwrap();
        let expected = crate::trap::TrapDispatcher::encode_hfs_component_for_vfs("T/st");
        assert_eq!(files[0].name, expected);
        assert!(!files[0].name.contains('/'));
        assert_eq!(files[0].data, b"A");
        assert_eq!(files[0].file_type, *b"TEXT");
        assert_eq!(files[0].creator, *b"ttxt");

        // A folder containing a literal slash must remain one directory too.
        bytes[13..15].copy_from_slice(&2u16.to_be_bytes());
        bytes.splice(16..16, [0x84, b'D', b'/', b'c', b's', 0, 1]);
        seal_directory(&mut bytes);
        let files = expand_compact_pro(&bytes).unwrap().unwrap();
        let folder = crate::trap::TrapDispatcher::encode_hfs_component_for_vfs("D/cs");
        assert_eq!(files[0].name, format!("{folder}/{expected}"));
        assert_eq!(files[0].name.matches('/').count(), 1);
        assert_eq!(files[0].data, b"A");
    }

    #[test]
    fn compact_pro_still_rejects_invalid_hfs_components() {
        for name in ["", ".", "..", ":", "a:b", "a\0b"] {
            let mut bytes = compact_fixture();
            bytes.splice(
                16..21,
                std::iter::once(name.len() as u8).chain(name.bytes()),
            );
            seal_directory(&mut bytes);
            assert!(expand_compact_pro(&bytes).is_err(), "name {name:?}");
        }
    }

    #[test]
    fn compact_pro_rejects_authenticated_invalid_paths_flags_and_ranges() {
        for (offset, replacement) in [
            (17, b':'), // HFS path separator in a component.
            (21, 2),    // Unsupported volume.
            (49, 1),    // Encrypted fork.
        ] {
            let mut bytes = compact_fixture();
            bytes[offset] = replacement;
            seal_directory(&mut bytes);
            assert!(expand_compact_pro(&bytes).is_err(), "offset {offset}");
        }
        for offset in [0u32, 7, 9, 16, u32::MAX] {
            let mut bytes = compact_fixture();
            bytes[22..26].copy_from_slice(&offset.to_be_bytes());
            seal_directory(&mut bytes);
            assert!(expand_compact_pro(&bytes).is_err(), "fork offset {offset}");
        }
    }
    #[test]
    fn installer_checks_metadata_and_crc() {
        let mut bytes = fixture();
        assert!(expand(&bytes, b"unrelated application").unwrap().is_none());
        let files = expand(&bytes, b"Smaller Installer").unwrap().unwrap();
        assert_eq!(files[0].data, b"A");
        assert_eq!(files[0].file_type, *b"TEXT");
        bytes[60] = b'B';
        assert!(expand(&bytes, b"Smaller Installer").is_err());
        bytes = fixture();
        bytes[42] = 1;
        assert!(expand(&bytes, b"Smaller Installer").is_err());
        bytes = fixture();
        bytes[16..20].copy_from_slice(&0u32.to_be_bytes());
        assert!(expand(&bytes, b"Smaller Installer").is_err());
        bytes = fixture();
        bytes[48..52].copy_from_slice(&u32::MAX.to_be_bytes());
        assert!(expand(&bytes, b"Smaller Installer").is_err());
    }
}