unityrs-core 1.0.0

Read and write Unity asset bundles, serialized files and typetrees.
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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
// Ported from UnityPy 1.25.2 (MIT): UnityPy/helpers/ImportHelper.py. See THIRD_PARTY_LICENSES.
// Ported from CPython 3.11 (PSF-2.0): Lib/gzip.py, _read_exact, _read_gzip_header, _GzipReader.read and _read_eof. See THIRD_PARTY_LICENSES.
//! `ImportHelper.check_file_type` and `parse_file`'s naming rule
//! (`helpers/ImportHelper.py:44-156`), quirks included: every rule of
//! `docs/CANONICAL_DUMP.md` section 4.2 (`l0`) and section 7 (L0 detection).
//!
//! Notable rules: eight U+00FA characters as a signature (the UTF-8 bytes
//! `C3 BA` x 8, never `FA` x 8); a zip only when byte 4 is NUL; a 20-byte file
//! with no NUL raises from bytes, and from a path, whose stream reader drops
//! the last byte it read, is a resource ([`check_file_type_with_reader`]);
//! a gzip file must decompress in full, every member and
//! trailer checked (the P0 spike inflated only 20 bytes, which this fixes).
//! That is UnityPy's own behaviour, not a unityrs policy: its streaming
//! `read_string_to_null` tests `Position != Length` per byte
//! (`streams/EndianBinaryReader.py:136-144`), and `Length` seeks the
//! `GzipFile` to its end (`streams/EndianBinaryReader.py:307-312`), which inflates every member. The
//! output is streamed through a scratch buffer and only its first 20 bytes
//! and its length are kept, so detection charges nothing: the scratch (at
//! most 64 KiB) is part of plan section 3.15's constant, as are the
//! inflater's state and 32 KiB window, one member's at a time. Also:
//! `brotli` at 0x20; the SerializedFile header read big-endian.
//!
//! **The inflater is zlib's.** flate2 runs `zlib-rs` (`unityrs-core`'s
//! manifest), a port of zlib that keeps its rules, so a stream inflates
//! or raises as under `CPython`'s zlib, which `gzip._GzipReader` runs. One
//! rule differs in flate2's default backend, `miniz_oxide`: a match reaching
//! before its member's first output byte is zlib's "invalid distance too
//! far back", where `miniz_oxide` copies zeros from its window (round 9:
//! a corrupt stream detected as `ResourceFile` or `WebFile`, and a bundle
//! UnityPy fails loaded). `tests/fixtures/c/detect.json` pins it.
//!
//! **Work.** One stream inflates at most [`Limits::max_inflate`] bytes. A
//! container detects each of its entries ([`check_entry_type`]), and many
//! directory rows can name one small stream that inflates to the bound, so
//! there every inflated byte is also counted as the open's work, the
//! allowance block decodes draw on ([`Limits::max_decode_work`],
//! [`Meter::count_decode`]): the open's detections together inflate no more
//! than that, and the one that passes it is refused
//! ([`DetectError::Work`], which the container reports as the open's
//! `LimitKind::DecodeWork` limit), as is every one after it. The bytes of
//! the stream a detection reads count too, inflated or not: a header's file
//! name or comment searched for its NUL, deflate input that inflates to
//! nothing (empty blocks), each member's header and trailer, and the zero
//! padding after it, all of which each row naming the stream reads again
//! (round 4's sweep: a stream of one tiny member and a gigabyte of padding
//! counted a few bytes per row). A detection that fails counts what it read
//! and inflated before the failure, and returns the stream's own error
//! (round 5: a malformed deflate stream returned before its consumed input
//! was counted, so every row read it again uncounted).
//!
//! Owner: package C.

use std::sync::Arc;

use flate2::{Crc, Decompress, FlushDecompress, Status};

use crate::error::{LimitError, LimitKind};
use crate::limits::{Limits, Meter};
use crate::stream::ReaderKind;

/// UnityPy's `FileType` verdict.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum FileType {
    /// A `SerializedFile`.
    AssetsFile,
    /// UnityFS, UnityWeb, UnityRaw or the U+00FA signature.
    BundleFile,
    /// `UnityWebData`/`TuanjieWebData`, plain, gzip or brotli.
    WebFile,
    /// Anything else.
    ResourceFile,
    /// `PK\x03\x04` followed by NUL.
    Zip,
}

/// Why detection raised (the dump's `"detect"` error kind).
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum DetectError {
    /// A host allocator refusal during detection.
    #[error(transparent)]
    Allocator(#[from] crate::budget::AllocError),
    /// A file of exactly 20 bytes with no NUL in them, read by the memory
    /// reader (bytes; from a path the stream reader's rule never raises
    /// here, [`check_file_type_with_reader`]).
    #[error("unterminated signature in a 20-byte file")]
    Unterminated,
    /// A positioned stream's signature read starts past EOF.
    #[error("Unterminated string: [b'']")]
    UnterminatedStream,
    /// A `1f 8b` file whose stream does not decompress in full.
    #[error("gzip: {0}")]
    Gzip(Arc<str>),
    /// CRC mismatch, retaining the canonical core message and its original
    /// allocation. Consumers that expose `CPython`'s exception can render the
    /// two checksums without lengthening the cached core message.
    #[error("gzip: {detail}")]
    GzipCrc {
        /// The same message as a legacy gzip CRC failure.
        detail: Arc<str>,
        /// Checksum stored in the gzip trailer.
        expected: u32,
        /// Checksum computed from the decompressed member.
        actual: u32,
    },
    /// The gzip stream inflates past [`Limits::max_inflate`]: a
    /// [`LimitKind::Inflate`] refusal, whose value is what the stream had
    /// inflated to when it was refused, so its text is every limit
    /// refusal's, `limit Inflate: <value> > <max>`.
    #[error(transparent)]
    InflateLimit(LimitError),
    /// [`check_entry_type`]: the open's work allowance
    /// ([`Limits::max_decode_work`]) ran out while a gzip stream inflated,
    /// or had run out before it; a `LimitKind::DecodeWork` limit, whose text
    /// is `limit DecodeWork: <value> > <max>`.
    #[error(transparent)]
    Work(LimitError),
}

/// `check_file_type` on the bytes (UnityPy's memory reader).
pub fn check_file_type(data: &[u8], limits: &Limits) -> Result<FileType, DetectError> {
    detect(data, limits, None, ReaderKind::Memory)
}

/// [`check_file_type`] by the rules of the reader UnityPy reads `data`
/// with: the stream reader for a file loaded from a path, whose
/// `read_string_to_null(20)` drops the last byte it read where no NUL is in
/// reach and never raises from the start of a file, so a 20-byte file with
/// no NUL is a `ResourceFile` (or a `WebFile` by its first 19 bytes) where
/// the memory reader raises ([`DetectError::Unterminated`]).
pub fn check_file_type_with_reader(
    data: &[u8],
    limits: &Limits,
    reader: ReaderKind,
) -> Result<FileType, DetectError> {
    detect(data, limits, None, reader)
}

/// [`check_file_type`] on a container's entry, with every byte a gzip
/// stream inflates counted as work of the open `meter` belongs to (module
/// docs, "Work"): the verdict is the same until the open's allowance runs
/// out, and [`DetectError::Work`] from then on.
pub fn check_entry_type(
    data: &[u8],
    limits: &Limits,
    meter: &Meter,
) -> Result<FileType, DetectError> {
    detect(data, limits, Some(meter), ReaderKind::Memory)
}

/// Interpret a detection result for a lazy file walk.
pub(crate) fn probe_container(
    detected: Result<FileType, DetectError>,
) -> Result<bool, DetectError> {
    match detected {
        Ok(ty) => Ok(matches!(ty, FileType::BundleFile | FileType::WebFile)),
        Err(
            e @ (DetectError::Allocator(_) | DetectError::Work(_) | DetectError::InflateLimit(_)),
        ) => Err(e),
        Err(
            DetectError::Unterminated
            | DetectError::UnterminatedStream
            | DetectError::Gzip(_)
            | DetectError::GzipCrc { .. },
        ) => Ok(false),
    }
}

/// Container-entry detection preserving the reader semantics of ZIP member
/// streams, while charging gzip probing to the enclosing input's work.
pub(crate) fn check_entry_type_with_reader(
    data: &[u8],
    limits: &Limits,
    meter: &Meter,
    reader: ReaderKind,
) -> Result<FileType, DetectError> {
    detect(data, limits, Some(meter), reader)
}

fn detect(
    data: &[u8],
    limits: &Limits,
    work: Option<&Meter>,
    reader: ReaderKind,
) -> Result<FileType, DetectError> {
    detect_at_position(data, limits, work, reader, 0)
}

pub(crate) fn detect_at_position(
    data: &[u8],
    limits: &Limits,
    work: Option<&Meter>,
    reader: ReaderKind,
    position: i128,
) -> Result<FileType, DetectError> {
    match by_signature(data, reader, position)? {
        Signed::Type(ty) => Ok(ty),
        // UnityPy helpers/ImportHelper.py:75-83
        Signed::Gzip if is_web_data(&gzip_signature(data, limits.max_inflate, work)?) => {
            Ok(FileType::WebFile)
        }
        Signed::Gzip => Ok(past_gzip(data)),
    }
}

/// What detection decides from signatures and the header alone, before it
/// inflates anything ([`by_signature`]).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Signed {
    /// The verdict, reached without decoding.
    Type(FileType),
    /// Gzip magic on a file of 128 bytes or more: detection inflates the
    /// stream, raises if it is bad, and is a `WebFile` if it holds web data,
    /// else goes on to the brotli marker and the header.
    Gzip,
}

/// Detection by the memory reader at the start ([`detect`]'s steps), up to
/// the gzip probe, which it never runs: a prefix-signature check that reads
/// at most 40 bytes and the length, and neither allocates nor decodes.
pub(crate) fn signature_only(data: &[u8]) -> Result<Signed, DetectError> {
    by_signature(data, ReaderKind::Memory, 0)
}

fn by_signature(data: &[u8], reader: ReaderKind, position: i128) -> Result<Signed, DetectError> {
    // UnityPy helpers/ImportHelper.py:54-55
    if data.len() < 20 {
        return Ok(Signed::Type(FileType::ResourceFile));
    }
    // UnityPy helpers/ImportHelper.py:57: the reader's
    // `read_string_to_null(20)` at the original cursor, then reset to zero.
    // It is decoded with `surrogateescape`, which
    // is injective, so comparing the bytes compares the strings.
    let (range, _) =
        reader
            .string_to_null(data, position, 20)
            .ok_or(if reader == ReaderKind::Memory {
                DetectError::Unterminated
            } else {
                DetectError::UnterminatedStream
            })?;
    let signature = data.get(range).unwrap_or_default();
    // UnityPy helpers/ImportHelper.py:60-70
    if BUNDLE_SIGNATURES.contains(&signature) {
        return Ok(Signed::Type(FileType::BundleFile));
    }
    if is_web_data(signature) {
        return Ok(Signed::Type(FileType::WebFile));
    }
    if signature == b"PK\x03\x04" {
        return Ok(Signed::Type(FileType::Zip));
    }
    // UnityPy helpers/ImportHelper.py:72-73
    if data.len() < 128 {
        return Ok(Signed::Type(FileType::ResourceFile));
    }
    if data.get(..2) == Some(GZIP_MAGIC.as_slice()) {
        return Ok(Signed::Gzip);
    }
    Ok(Signed::Type(past_gzip(data)))
}

/// The steps after the gzip probe, on a file of 128 bytes or more.
fn past_gzip(data: &[u8]) -> FileType {
    // UnityPy helpers/ImportHelper.py:84-88
    if data.get(0x20..0x26) == Some(b"brotli".as_slice()) {
        return FileType::WebFile;
    }
    // UnityPy helpers/ImportHelper.py:90-124: the header read big-endian
    // (the reader's default) whatever the byte at 16 says; the file holds at
    // least 128 bytes here, so every read succeeds.
    let mut metadata_size = be_u32(data, 0);
    let mut file_size = be_u32(data, 4);
    let version = be_u32(data, 8);
    let mut data_offset = be_u32(data, 12);
    if version >= 22 {
        metadata_size = be_u32(data, 20);
        file_size = be_i64(data, 24);
        data_offset = be_i64(data, 32);
    }
    let len = i64::try_from(data.len()).unwrap_or(i64::MAX);
    let bad = version > 100
        || [file_size, metadata_size, version, data_offset]
            .iter()
            .any(|x| *x < 0 || *x > len)
        || file_size < metadata_size
        || file_size < data_offset;
    if bad {
        FileType::ResourceFile
    } else {
        FileType::AssetsFile
    }
}

/// The signatures that make a bundle (`ImportHelper.py:60-65`), as the UTF-8
/// of the strings UnityPy compares.
const BUNDLE_SIGNATURES: [&[u8]; 4] = [
    b"UnityWeb",
    b"UnityRaw",
    // Eight U+00FA: the byte 0xFA itself decodes to a surrogate escape and
    // never matches (CANONICAL_DUMP.md section 7).
    "\u{fa}\u{fa}\u{fa}\u{fa}\u{fa}\u{fa}\u{fa}\u{fa}".as_bytes(),
    b"UnityFS",
];

const GZIP_MAGIC: [u8; 2] = [0x1f, 0x8b];

/// `signature.startswith(("UnityWebData", "TuanjieWebData"))`.
fn is_web_data(signature: &[u8]) -> bool {
    signature.starts_with(b"UnityWebData") || signature.starts_with(b"TuanjieWebData")
}

fn be_u32(data: &[u8], at: usize) -> i64 {
    data.get(at..at.saturating_add(4))
        .and_then(|b| <[u8; 4]>::try_from(b).ok())
        .map_or(0, |b| i64::from(u32::from_be_bytes(b)))
}

fn be_i64(data: &[u8], at: usize) -> i64 {
    data.get(at..at.saturating_add(8))
        .and_then(|b| <[u8; 8]>::try_from(b).ok())
        .map_or(0, i64::from_be_bytes)
}

/// The scratch gzip detection inflates into (part of plan section 3.15's
/// constant).
const SCRATCH: usize = 64 << 10;

/// `EndianBinaryReader(GzipFile(fileobj=reader)).read_string_to_null(20)`
/// (`ImportHelper.py:78-80`). Its first `Length` seeks the `GzipFile` to its
/// end (`streams/EndianBinaryReader.py:307-312`), which inflates and checks
/// every member as Python 3.11's `gzip._GzipReader` does. The streaming loop
/// (`streams/EndianBinaryReader.py:136-144`) then keeps each byte it read except the last: byte `i` is kept
/// when it and every byte before it are not NUL, `i < 19`, and `i + 1` is
/// below the decompressed length.
fn gzip_signature(
    data: &[u8],
    max_inflate: u64,
    work: Option<&Meter>,
) -> Result<Vec<u8>, DetectError> {
    let mut first = [0u8; 20];
    let mut first_len = 0usize;
    let total = gzip_inflate(data, max_inflate, work, |chunk| {
        let n = chunk.len().min(first.len().saturating_sub(first_len));
        if let (Some(dst), Some(src)) = (first.get_mut(first_len..first_len + n), chunk.get(..n)) {
            dst.copy_from_slice(src);
        }
        first_len += n;
    })?;
    let limit = usize::try_from(total.saturating_sub(1))
        .unwrap_or(usize::MAX)
        .min(19)
        .min(first_len);
    let head = first.get(..limit).unwrap_or_default();
    let keep = head.iter().position(|b| *b == 0).unwrap_or(head.len());
    Ok(head.get(..keep).unwrap_or_default().to_vec())
}

fn gzip_error(what: &str) -> DetectError {
    DetectError::Gzip(what.into())
}

const EOF_MESSAGE: &str =
    "EOFError: Compressed file ended before the end-of-stream marker was reached";

/// `_read_exact`: `n` bytes at `*pos`, or `EOFError`.
fn read_exact<'a>(data: &'a [u8], pos: &mut usize, n: usize) -> Result<&'a [u8], DetectError> {
    let end = pos.saturating_add(n);
    let got = data.get(*pos..end).ok_or_else(|| gzip_error(EOF_MESSAGE))?;
    *pos = end;
    Ok(got)
}

/// `bytes` inflated, counted as the open's work: when that passes the
/// allowance, they are counted anyway (they were inflated), so every later
/// detection of the open is refused before it inflates anything. The
/// refusal is the one the meter's count made, and counted (CR8; round 4: it
/// was the work query's afterwards, which a sum past `u64::MAX` under an
/// allowance of `u64::MAX` answered with nothing, so a counted refusal
/// returned `Ok`).
fn count_work(meter: &Meter, bytes: u64) -> Result<(), DetectError> {
    meter.count_work(bytes).map_err(|refused| {
        meter.add_work(bytes);
        DetectError::Work(refused)
    })
}

/// `got`, after the `bytes` read or inflated for it are counted as `open`'s
/// work: a success as [`count_work`] counts it (refused once the allowance
/// is passed), a failure unchecked, so it returns the stream's own error and
/// the work after it is refused.
fn counted<T>(
    open: Option<&Meter>,
    bytes: u64,
    got: Result<T, DetectError>,
) -> Result<T, DetectError> {
    let Some(meter) = open else {
        return got;
    };
    match got {
        Ok(v) => count_work(meter, bytes).map(|()| v),
        Err(e) => {
            meter.add_work(bytes);
            Err(e)
        }
    }
}

/// The stream inflated to `total`, past `max_inflate`.
fn inflate_limit(total: u64, max_inflate: u64) -> DetectError {
    DetectError::InflateLimit(LimitError::refused(LimitKind::Inflate, total, max_inflate))
}

/// The bytes from `from` to `to`, as work.
fn read_from(from: usize, to: usize) -> u64 {
    u64::try_from(to.saturating_sub(from)).unwrap_or(u64::MAX)
}

/// `_read_gzip_header` from the magic at `*pos`, which it moves past the
/// header, every byte of which, the file name's and comment's searches
/// included, is counted as `open`'s work when there is one, whether the
/// header reads or fails.
fn member_header(data: &[u8], pos: &mut usize, open: Option<&Meter>) -> Result<(), DetectError> {
    let member = *pos;
    let got = read_member_header(data, pos);
    counted(open, read_from(member, *pos), got)
}

/// [`member_header`]'s reads: `*pos` moves past each byte read, a failing
/// read's excepted.
fn read_member_header(data: &[u8], pos: &mut usize) -> Result<(), DetectError> {
    const FHCRC: u8 = 2;
    const FEXTRA: u8 = 4;
    const FNAME: u8 = 8;
    const FCOMMENT: u8 = 16;
    *pos += 2;
    let head = read_exact(data, pos, 8)?;
    let (method, flag) = (
        head.first().copied().unwrap_or(0),
        head.get(1).copied().unwrap_or(0),
    );
    if method != 8 {
        return Err(gzip_error("BadGzipFile: Unknown compression method"));
    }
    if flag & FEXTRA != 0 {
        let n = read_exact(data, pos, 2)?;
        let n = u16::from_le_bytes([
            n.first().copied().unwrap_or(0),
            n.get(1).copied().unwrap_or(0),
        ]);
        read_exact(data, pos, usize::from(n))?;
    }
    for bit in [FNAME, FCOMMENT] {
        if flag & bit != 0 {
            // Up to and including a NUL, or to the end.
            let rest = data.get(*pos..).unwrap_or_default();
            *pos += rest
                .iter()
                .position(|b| *b == 0)
                .map_or(rest.len(), |i| i + 1);
        }
    }
    if flag & FHCRC != 0 {
        read_exact(data, pos, 2)?;
    }
    Ok(())
}

/// Inflates every member of a gzip file as Python 3.11's `gzip._GzipReader`
/// reads it (`_read_gzip_header`, `read`, `_read_eof`), handing each output
/// chunk to `sink`; returns the decompressed length. The header's FHCRC is
/// skipped, not checked, as Python skips it. Each chunk, and every byte of
/// `data` read for it, is counted as `open`'s work when there is one
/// (module docs, "Work").
fn gzip_inflate(
    data: &[u8],
    max_inflate: u64,
    open: Option<&Meter>,
    mut sink: impl FnMut(&[u8]),
) -> Result<u64, DetectError> {
    let mut scratch = vec![0; SCRATCH];
    gzip_inflate_to(data, max_inflate, open, &mut scratch, |out| {
        sink(out);
        Ok(())
    })
}

pub(crate) fn gzip_inflate_to<E: From<DetectError>>(
    data: &[u8],
    max_inflate: u64,
    open: Option<&Meter>,
    scratch: &mut [u8],
    mut sink: impl FnMut(&[u8]) -> Result<(), E>,
) -> Result<u64, E> {
    if let Some(l) = open.and_then(Meter::work_refused) {
        return Err(DetectError::Work(l).into());
    }
    let mut pos = 0usize;
    let mut total = 0u64;
    loop {
        // `_read_gzip_header`: `fp.read(2)` empty ends the file; anything
        // else must be the magic.
        let rest = data.get(pos..).unwrap_or_default();
        if rest.is_empty() {
            return Ok(total);
        }
        if !rest.starts_with(&GZIP_MAGIC) {
            return Err(gzip_error("BadGzipFile: Not a gzipped file").into());
        }
        member_header(data, &mut pos, open)?;
        // The member's raw deflate stream.
        let input = data.get(pos..).unwrap_or_default();
        let mut z = Decompress::new(false);
        let mut crc = Crc::new();
        loop {
            let (in_before, out_before) = (z.total_in(), z.total_out());
            let at = usize::try_from(in_before).unwrap_or(usize::MAX);
            let status = z
                .decompress(
                    input.get(at..).unwrap_or_default(),
                    scratch,
                    FlushDecompress::None,
                )
                .map_err(|e| DetectError::Gzip(format!("zlib.error: {e}").into()));
            // What the call consumed and made counts whether it failed or
            // not (zlib-rs advances flate2's totals by what it read and
            // wrote before it reports a failure), and before the stream's
            // own bound is checked, so the chunk that passes `max_inflate`
            // is the open's work too.
            let made = usize::try_from(z.total_out() - out_before).unwrap_or(0);
            let consumed = z.total_in() - in_before;
            let made_bytes = u64::try_from(made).unwrap_or(u64::MAX);
            let status = counted(open, made_bytes.saturating_add(consumed), status)?;
            let out = scratch.get(..made).unwrap_or_default();
            crc.update(out);
            total = total.saturating_add(made_bytes);
            if total > max_inflate {
                return Err(inflate_limit(total, max_inflate).into());
            }
            sink(out)?;
            if status == Status::StreamEnd {
                break;
            }
            if z.total_in() == in_before && made == 0 {
                return Err(gzip_error(EOF_MESSAGE).into());
            }
        }
        pos = pos.saturating_add(usize::try_from(z.total_in()).unwrap_or(usize::MAX));
        let eof = pos;
        let got = member_trailer(data, &mut pos, &crc);
        counted(open, read_from(eof, pos), got)?;
    }
}

/// `_read_eof` from `*pos`: the CRC-32 and the size modulo 2^32, checked,
/// then the zero bytes after them; `*pos` moves past each byte read.
fn member_trailer(data: &[u8], pos: &mut usize, crc: &Crc) -> Result<(), DetectError> {
    let trailer = read_exact(data, pos, 8)?;
    let word = |i: usize| {
        trailer
            .get(i..i + 4)
            .and_then(|b| <[u8; 4]>::try_from(b).ok())
            .map_or(0, u32::from_le_bytes)
    };
    if word(0) != crc.sum() {
        return Err(DetectError::GzipCrc {
            detail: "BadGzipFile: CRC check failed".into(),
            expected: word(0),
            actual: crc.sum(),
        });
    }
    if word(4) != crc.amount() {
        return Err(gzip_error("BadGzipFile: Incorrect length of data produced"));
    }
    while data.get(*pos) == Some(&0) {
        *pos += 1;
    }
    Ok(())
}

/// What UnityPy builds from a detected file (`l0.parsed_as`).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ParsedAs {
    /// A `SerializedFile`.
    SerializedFile,
    /// A `BundleFile`.
    BundleFile,
    /// A `WebFile`.
    WebFile,
    /// UnityPy kept a reader: a resource, a name-rule file, or a zip entry.
    Raw,
    /// An input detected as a zip.
    Zip,
    /// Detection or parsing raised.
    Error,
}

/// `parse_file`'s choice. An `AssetsFile` whose name ends in `.resS`,
/// `.resource`, `.config`, `.xml` or `.dat` (a case-sensitive byte suffix) stays
/// raw; a zip is `Zip` only for an input (`is_entry` false), else raw.
#[must_use]
pub fn parsed_as(ty: FileType, name: &[u8], is_entry: bool) -> ParsedAs {
    // UnityPy helpers/ImportHelper.py:138-151; a zip input is refused before
    // `parse_file` (CANONICAL_DUMP.md section 4.2), an entry stays a reader.
    match ty {
        FileType::AssetsFile if raw_by_name(name) => ParsedAs::Raw,
        FileType::AssetsFile => ParsedAs::SerializedFile,
        FileType::BundleFile => ParsedAs::BundleFile,
        FileType::WebFile => ParsedAs::WebFile,
        FileType::Zip if !is_entry => ParsedAs::Zip,
        FileType::Zip | FileType::ResourceFile => ParsedAs::Raw,
    }
}

/// Whether `name` ends in one of the suffixes that keep an `AssetsFile` raw
/// ([`parsed_as`]): the only part of a name that changes what `parse_file`
/// builds from given bytes.
pub(crate) fn raw_by_name(name: &[u8]) -> bool {
    const RAW_SUFFIXES: [&[u8]; 5] = [b".resS", b".resource", b".config", b".xml", b".dat"];
    RAW_SUFFIXES.iter().any(|s| name.ends_with(s))
}

#[cfg(test)]
mod tests {
    use super::{DetectError, count_work};
    use crate::error::LimitKind;
    use crate::limits::Meter;

    /// Under an allowance of `u64::MAX`, inflated work whose sum passes
    /// `u64::MAX` is refused by the meter's count, and that refusal, which
    /// it counted (CR8), is returned: round 4 asked, and the work query
    /// that answered then saw the saturated sum within the allowance and
    /// returned `Ok`. No input inflates that much, so only this test
    /// reaches it.
    #[test]
    fn work_past_u64_max_is_refused_under_an_allowance_of_u64_max() {
        let meter = Meter::unlimited();
        meter.add_work(u64::MAX - 5);
        assert!(count_work(&meter, 5).is_ok(), "the allowance is inclusive");
        match count_work(&meter, 1) {
            Err(DetectError::Work(l)) => assert_eq!(
                (l.kind(), l.value(), l.max()),
                (LimitKind::DecodeWork, u64::MAX, u64::MAX)
            ),
            other => panic!("{other:?}"),
        }
        assert_eq!(meter.decoded(), u64::MAX);
        assert!(
            meter.work_used_up().is_none(),
            "the sum saturates at the allowance"
        );
    }
}