unityrs_core/detect.rs
1// Ported from UnityPy 1.25.2 (MIT): UnityPy/helpers/ImportHelper.py. See THIRD_PARTY_LICENSES.
2// 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.
3//! `ImportHelper.check_file_type` and `parse_file`'s naming rule
4//! (`helpers/ImportHelper.py:44-156`), quirks included: every rule of
5//! `docs/CANONICAL_DUMP.md` section 4.2 (`l0`) and section 7 (L0 detection).
6//!
7//! Notable rules: eight U+00FA characters as a signature (the UTF-8 bytes
8//! `C3 BA` x 8, never `FA` x 8); a zip only when byte 4 is NUL; a 20-byte file
9//! with no NUL raises from bytes, and from a path, whose stream reader drops
10//! the last byte it read, is a resource ([`check_file_type_with_reader`]);
11//! a gzip file must decompress in full, every member and
12//! trailer checked (the P0 spike inflated only 20 bytes, which this fixes).
13//! That is UnityPy's own behaviour, not a unityrs policy: its streaming
14//! `read_string_to_null` tests `Position != Length` per byte
15//! (`streams/EndianBinaryReader.py:136-144`), and `Length` seeks the
16//! `GzipFile` to its end (`streams/EndianBinaryReader.py:307-312`), which inflates every member. The
17//! output is streamed through a scratch buffer and only its first 20 bytes
18//! and its length are kept, so detection charges nothing: the scratch (at
19//! most 64 KiB) is part of plan section 3.15's constant, as are the
20//! inflater's state and 32 KiB window, one member's at a time. Also:
21//! `brotli` at 0x20; the SerializedFile header read big-endian.
22//!
23//! **The inflater is zlib's.** flate2 runs `zlib-rs` (`unityrs-core`'s
24//! manifest), a port of zlib that keeps its rules, so a stream inflates
25//! or raises as under `CPython`'s zlib, which `gzip._GzipReader` runs. One
26//! rule differs in flate2's default backend, `miniz_oxide`: a match reaching
27//! before its member's first output byte is zlib's "invalid distance too
28//! far back", where `miniz_oxide` copies zeros from its window (round 9:
29//! a corrupt stream detected as `ResourceFile` or `WebFile`, and a bundle
30//! UnityPy fails loaded). `tests/fixtures/c/detect.json` pins it.
31//!
32//! **Work.** One stream inflates at most [`Limits::max_inflate`] bytes. A
33//! container detects each of its entries ([`check_entry_type`]), and many
34//! directory rows can name one small stream that inflates to the bound, so
35//! there every inflated byte is also counted as the open's work, the
36//! allowance block decodes draw on ([`Limits::max_decode_work`],
37//! [`Meter::count_decode`]): the open's detections together inflate no more
38//! than that, and the one that passes it is refused
39//! ([`DetectError::Work`], which the container reports as the open's
40//! `LimitKind::DecodeWork` limit), as is every one after it. The bytes of
41//! the stream a detection reads count too, inflated or not: a header's file
42//! name or comment searched for its NUL, deflate input that inflates to
43//! nothing (empty blocks), each member's header and trailer, and the zero
44//! padding after it, all of which each row naming the stream reads again
45//! (round 4's sweep: a stream of one tiny member and a gigabyte of padding
46//! counted a few bytes per row). A detection that fails counts what it read
47//! and inflated before the failure, and returns the stream's own error
48//! (round 5: a malformed deflate stream returned before its consumed input
49//! was counted, so every row read it again uncounted).
50//!
51//! Owner: package C.
52
53use std::sync::Arc;
54
55use flate2::{Crc, Decompress, FlushDecompress, Status};
56
57use crate::error::{LimitError, LimitKind};
58use crate::limits::{Limits, Meter};
59use crate::stream::ReaderKind;
60
61/// UnityPy's `FileType` verdict.
62#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
63pub enum FileType {
64 /// A `SerializedFile`.
65 AssetsFile,
66 /// UnityFS, UnityWeb, UnityRaw or the U+00FA signature.
67 BundleFile,
68 /// `UnityWebData`/`TuanjieWebData`, plain, gzip or brotli.
69 WebFile,
70 /// Anything else.
71 ResourceFile,
72 /// `PK\x03\x04` followed by NUL.
73 Zip,
74}
75
76/// Why detection raised (the dump's `"detect"` error kind).
77#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
78#[non_exhaustive]
79pub enum DetectError {
80 /// A host allocator refusal during detection.
81 #[error(transparent)]
82 Allocator(#[from] crate::budget::AllocError),
83 /// A file of exactly 20 bytes with no NUL in them, read by the memory
84 /// reader (bytes; from a path the stream reader's rule never raises
85 /// here, [`check_file_type_with_reader`]).
86 #[error("unterminated signature in a 20-byte file")]
87 Unterminated,
88 /// A positioned stream's signature read starts past EOF.
89 #[error("Unterminated string: [b'']")]
90 UnterminatedStream,
91 /// A `1f 8b` file whose stream does not decompress in full.
92 #[error("gzip: {0}")]
93 Gzip(Arc<str>),
94 /// CRC mismatch, retaining the canonical core message and its original
95 /// allocation. Consumers that expose `CPython`'s exception can render the
96 /// two checksums without lengthening the cached core message.
97 #[error("gzip: {detail}")]
98 GzipCrc {
99 /// The same message as a legacy gzip CRC failure.
100 detail: Arc<str>,
101 /// Checksum stored in the gzip trailer.
102 expected: u32,
103 /// Checksum computed from the decompressed member.
104 actual: u32,
105 },
106 /// The gzip stream inflates past [`Limits::max_inflate`]: a
107 /// [`LimitKind::Inflate`] refusal, whose value is what the stream had
108 /// inflated to when it was refused, so its text is every limit
109 /// refusal's, `limit Inflate: <value> > <max>`.
110 #[error(transparent)]
111 InflateLimit(LimitError),
112 /// [`check_entry_type`]: the open's work allowance
113 /// ([`Limits::max_decode_work`]) ran out while a gzip stream inflated,
114 /// or had run out before it; a `LimitKind::DecodeWork` limit, whose text
115 /// is `limit DecodeWork: <value> > <max>`.
116 #[error(transparent)]
117 Work(LimitError),
118}
119
120/// `check_file_type` on the bytes (UnityPy's memory reader).
121pub fn check_file_type(data: &[u8], limits: &Limits) -> Result<FileType, DetectError> {
122 detect(data, limits, None, ReaderKind::Memory)
123}
124
125/// [`check_file_type`] by the rules of the reader UnityPy reads `data`
126/// with: the stream reader for a file loaded from a path, whose
127/// `read_string_to_null(20)` drops the last byte it read where no NUL is in
128/// reach and never raises from the start of a file, so a 20-byte file with
129/// no NUL is a `ResourceFile` (or a `WebFile` by its first 19 bytes) where
130/// the memory reader raises ([`DetectError::Unterminated`]).
131pub fn check_file_type_with_reader(
132 data: &[u8],
133 limits: &Limits,
134 reader: ReaderKind,
135) -> Result<FileType, DetectError> {
136 detect(data, limits, None, reader)
137}
138
139/// [`check_file_type`] on a container's entry, with every byte a gzip
140/// stream inflates counted as work of the open `meter` belongs to (module
141/// docs, "Work"): the verdict is the same until the open's allowance runs
142/// out, and [`DetectError::Work`] from then on.
143pub fn check_entry_type(
144 data: &[u8],
145 limits: &Limits,
146 meter: &Meter,
147) -> Result<FileType, DetectError> {
148 detect(data, limits, Some(meter), ReaderKind::Memory)
149}
150
151/// Interpret a detection result for a lazy file walk.
152pub(crate) fn probe_container(
153 detected: Result<FileType, DetectError>,
154) -> Result<bool, DetectError> {
155 match detected {
156 Ok(ty) => Ok(matches!(ty, FileType::BundleFile | FileType::WebFile)),
157 Err(
158 e @ (DetectError::Allocator(_) | DetectError::Work(_) | DetectError::InflateLimit(_)),
159 ) => Err(e),
160 Err(
161 DetectError::Unterminated
162 | DetectError::UnterminatedStream
163 | DetectError::Gzip(_)
164 | DetectError::GzipCrc { .. },
165 ) => Ok(false),
166 }
167}
168
169/// Container-entry detection preserving the reader semantics of ZIP member
170/// streams, while charging gzip probing to the enclosing input's work.
171pub(crate) fn check_entry_type_with_reader(
172 data: &[u8],
173 limits: &Limits,
174 meter: &Meter,
175 reader: ReaderKind,
176) -> Result<FileType, DetectError> {
177 detect(data, limits, Some(meter), reader)
178}
179
180fn detect(
181 data: &[u8],
182 limits: &Limits,
183 work: Option<&Meter>,
184 reader: ReaderKind,
185) -> Result<FileType, DetectError> {
186 detect_at_position(data, limits, work, reader, 0)
187}
188
189pub(crate) fn detect_at_position(
190 data: &[u8],
191 limits: &Limits,
192 work: Option<&Meter>,
193 reader: ReaderKind,
194 position: i128,
195) -> Result<FileType, DetectError> {
196 match by_signature(data, reader, position)? {
197 Signed::Type(ty) => Ok(ty),
198 // UnityPy helpers/ImportHelper.py:75-83
199 Signed::Gzip if is_web_data(&gzip_signature(data, limits.max_inflate, work)?) => {
200 Ok(FileType::WebFile)
201 }
202 Signed::Gzip => Ok(past_gzip(data)),
203 }
204}
205
206/// What detection decides from signatures and the header alone, before it
207/// inflates anything ([`by_signature`]).
208#[derive(Clone, Copy, Debug, PartialEq, Eq)]
209pub(crate) enum Signed {
210 /// The verdict, reached without decoding.
211 Type(FileType),
212 /// Gzip magic on a file of 128 bytes or more: detection inflates the
213 /// stream, raises if it is bad, and is a `WebFile` if it holds web data,
214 /// else goes on to the brotli marker and the header.
215 Gzip,
216}
217
218/// Detection by the memory reader at the start ([`detect`]'s steps), up to
219/// the gzip probe, which it never runs: a prefix-signature check that reads
220/// at most 40 bytes and the length, and neither allocates nor decodes.
221pub(crate) fn signature_only(data: &[u8]) -> Result<Signed, DetectError> {
222 by_signature(data, ReaderKind::Memory, 0)
223}
224
225fn by_signature(data: &[u8], reader: ReaderKind, position: i128) -> Result<Signed, DetectError> {
226 // UnityPy helpers/ImportHelper.py:54-55
227 if data.len() < 20 {
228 return Ok(Signed::Type(FileType::ResourceFile));
229 }
230 // UnityPy helpers/ImportHelper.py:57: the reader's
231 // `read_string_to_null(20)` at the original cursor, then reset to zero.
232 // It is decoded with `surrogateescape`, which
233 // is injective, so comparing the bytes compares the strings.
234 let (range, _) =
235 reader
236 .string_to_null(data, position, 20)
237 .ok_or(if reader == ReaderKind::Memory {
238 DetectError::Unterminated
239 } else {
240 DetectError::UnterminatedStream
241 })?;
242 let signature = data.get(range).unwrap_or_default();
243 // UnityPy helpers/ImportHelper.py:60-70
244 if BUNDLE_SIGNATURES.contains(&signature) {
245 return Ok(Signed::Type(FileType::BundleFile));
246 }
247 if is_web_data(signature) {
248 return Ok(Signed::Type(FileType::WebFile));
249 }
250 if signature == b"PK\x03\x04" {
251 return Ok(Signed::Type(FileType::Zip));
252 }
253 // UnityPy helpers/ImportHelper.py:72-73
254 if data.len() < 128 {
255 return Ok(Signed::Type(FileType::ResourceFile));
256 }
257 if data.get(..2) == Some(GZIP_MAGIC.as_slice()) {
258 return Ok(Signed::Gzip);
259 }
260 Ok(Signed::Type(past_gzip(data)))
261}
262
263/// The steps after the gzip probe, on a file of 128 bytes or more.
264fn past_gzip(data: &[u8]) -> FileType {
265 // UnityPy helpers/ImportHelper.py:84-88
266 if data.get(0x20..0x26) == Some(b"brotli".as_slice()) {
267 return FileType::WebFile;
268 }
269 // UnityPy helpers/ImportHelper.py:90-124: the header read big-endian
270 // (the reader's default) whatever the byte at 16 says; the file holds at
271 // least 128 bytes here, so every read succeeds.
272 let mut metadata_size = be_u32(data, 0);
273 let mut file_size = be_u32(data, 4);
274 let version = be_u32(data, 8);
275 let mut data_offset = be_u32(data, 12);
276 if version >= 22 {
277 metadata_size = be_u32(data, 20);
278 file_size = be_i64(data, 24);
279 data_offset = be_i64(data, 32);
280 }
281 let len = i64::try_from(data.len()).unwrap_or(i64::MAX);
282 let bad = version > 100
283 || [file_size, metadata_size, version, data_offset]
284 .iter()
285 .any(|x| *x < 0 || *x > len)
286 || file_size < metadata_size
287 || file_size < data_offset;
288 if bad {
289 FileType::ResourceFile
290 } else {
291 FileType::AssetsFile
292 }
293}
294
295/// The signatures that make a bundle (`ImportHelper.py:60-65`), as the UTF-8
296/// of the strings UnityPy compares.
297const BUNDLE_SIGNATURES: [&[u8]; 4] = [
298 b"UnityWeb",
299 b"UnityRaw",
300 // Eight U+00FA: the byte 0xFA itself decodes to a surrogate escape and
301 // never matches (CANONICAL_DUMP.md section 7).
302 "\u{fa}\u{fa}\u{fa}\u{fa}\u{fa}\u{fa}\u{fa}\u{fa}".as_bytes(),
303 b"UnityFS",
304];
305
306const GZIP_MAGIC: [u8; 2] = [0x1f, 0x8b];
307
308/// `signature.startswith(("UnityWebData", "TuanjieWebData"))`.
309fn is_web_data(signature: &[u8]) -> bool {
310 signature.starts_with(b"UnityWebData") || signature.starts_with(b"TuanjieWebData")
311}
312
313fn be_u32(data: &[u8], at: usize) -> i64 {
314 data.get(at..at.saturating_add(4))
315 .and_then(|b| <[u8; 4]>::try_from(b).ok())
316 .map_or(0, |b| i64::from(u32::from_be_bytes(b)))
317}
318
319fn be_i64(data: &[u8], at: usize) -> i64 {
320 data.get(at..at.saturating_add(8))
321 .and_then(|b| <[u8; 8]>::try_from(b).ok())
322 .map_or(0, i64::from_be_bytes)
323}
324
325/// The scratch gzip detection inflates into (part of plan section 3.15's
326/// constant).
327const SCRATCH: usize = 64 << 10;
328
329/// `EndianBinaryReader(GzipFile(fileobj=reader)).read_string_to_null(20)`
330/// (`ImportHelper.py:78-80`). Its first `Length` seeks the `GzipFile` to its
331/// end (`streams/EndianBinaryReader.py:307-312`), which inflates and checks
332/// every member as Python 3.11's `gzip._GzipReader` does. The streaming loop
333/// (`streams/EndianBinaryReader.py:136-144`) then keeps each byte it read except the last: byte `i` is kept
334/// when it and every byte before it are not NUL, `i < 19`, and `i + 1` is
335/// below the decompressed length.
336fn gzip_signature(
337 data: &[u8],
338 max_inflate: u64,
339 work: Option<&Meter>,
340) -> Result<Vec<u8>, DetectError> {
341 let mut first = [0u8; 20];
342 let mut first_len = 0usize;
343 let total = gzip_inflate(data, max_inflate, work, |chunk| {
344 let n = chunk.len().min(first.len().saturating_sub(first_len));
345 if let (Some(dst), Some(src)) = (first.get_mut(first_len..first_len + n), chunk.get(..n)) {
346 dst.copy_from_slice(src);
347 }
348 first_len += n;
349 })?;
350 let limit = usize::try_from(total.saturating_sub(1))
351 .unwrap_or(usize::MAX)
352 .min(19)
353 .min(first_len);
354 let head = first.get(..limit).unwrap_or_default();
355 let keep = head.iter().position(|b| *b == 0).unwrap_or(head.len());
356 Ok(head.get(..keep).unwrap_or_default().to_vec())
357}
358
359fn gzip_error(what: &str) -> DetectError {
360 DetectError::Gzip(what.into())
361}
362
363const EOF_MESSAGE: &str =
364 "EOFError: Compressed file ended before the end-of-stream marker was reached";
365
366/// `_read_exact`: `n` bytes at `*pos`, or `EOFError`.
367fn read_exact<'a>(data: &'a [u8], pos: &mut usize, n: usize) -> Result<&'a [u8], DetectError> {
368 let end = pos.saturating_add(n);
369 let got = data.get(*pos..end).ok_or_else(|| gzip_error(EOF_MESSAGE))?;
370 *pos = end;
371 Ok(got)
372}
373
374/// `bytes` inflated, counted as the open's work: when that passes the
375/// allowance, they are counted anyway (they were inflated), so every later
376/// detection of the open is refused before it inflates anything. The
377/// refusal is the one the meter's count made, and counted (CR8; round 4: it
378/// was the work query's afterwards, which a sum past `u64::MAX` under an
379/// allowance of `u64::MAX` answered with nothing, so a counted refusal
380/// returned `Ok`).
381fn count_work(meter: &Meter, bytes: u64) -> Result<(), DetectError> {
382 meter.count_work(bytes).map_err(|refused| {
383 meter.add_work(bytes);
384 DetectError::Work(refused)
385 })
386}
387
388/// `got`, after the `bytes` read or inflated for it are counted as `open`'s
389/// work: a success as [`count_work`] counts it (refused once the allowance
390/// is passed), a failure unchecked, so it returns the stream's own error and
391/// the work after it is refused.
392fn counted<T>(
393 open: Option<&Meter>,
394 bytes: u64,
395 got: Result<T, DetectError>,
396) -> Result<T, DetectError> {
397 let Some(meter) = open else {
398 return got;
399 };
400 match got {
401 Ok(v) => count_work(meter, bytes).map(|()| v),
402 Err(e) => {
403 meter.add_work(bytes);
404 Err(e)
405 }
406 }
407}
408
409/// The stream inflated to `total`, past `max_inflate`.
410fn inflate_limit(total: u64, max_inflate: u64) -> DetectError {
411 DetectError::InflateLimit(LimitError::refused(LimitKind::Inflate, total, max_inflate))
412}
413
414/// The bytes from `from` to `to`, as work.
415fn read_from(from: usize, to: usize) -> u64 {
416 u64::try_from(to.saturating_sub(from)).unwrap_or(u64::MAX)
417}
418
419/// `_read_gzip_header` from the magic at `*pos`, which it moves past the
420/// header, every byte of which, the file name's and comment's searches
421/// included, is counted as `open`'s work when there is one, whether the
422/// header reads or fails.
423fn member_header(data: &[u8], pos: &mut usize, open: Option<&Meter>) -> Result<(), DetectError> {
424 let member = *pos;
425 let got = read_member_header(data, pos);
426 counted(open, read_from(member, *pos), got)
427}
428
429/// [`member_header`]'s reads: `*pos` moves past each byte read, a failing
430/// read's excepted.
431fn read_member_header(data: &[u8], pos: &mut usize) -> Result<(), DetectError> {
432 const FHCRC: u8 = 2;
433 const FEXTRA: u8 = 4;
434 const FNAME: u8 = 8;
435 const FCOMMENT: u8 = 16;
436 *pos += 2;
437 let head = read_exact(data, pos, 8)?;
438 let (method, flag) = (
439 head.first().copied().unwrap_or(0),
440 head.get(1).copied().unwrap_or(0),
441 );
442 if method != 8 {
443 return Err(gzip_error("BadGzipFile: Unknown compression method"));
444 }
445 if flag & FEXTRA != 0 {
446 let n = read_exact(data, pos, 2)?;
447 let n = u16::from_le_bytes([
448 n.first().copied().unwrap_or(0),
449 n.get(1).copied().unwrap_or(0),
450 ]);
451 read_exact(data, pos, usize::from(n))?;
452 }
453 for bit in [FNAME, FCOMMENT] {
454 if flag & bit != 0 {
455 // Up to and including a NUL, or to the end.
456 let rest = data.get(*pos..).unwrap_or_default();
457 *pos += rest
458 .iter()
459 .position(|b| *b == 0)
460 .map_or(rest.len(), |i| i + 1);
461 }
462 }
463 if flag & FHCRC != 0 {
464 read_exact(data, pos, 2)?;
465 }
466 Ok(())
467}
468
469/// Inflates every member of a gzip file as Python 3.11's `gzip._GzipReader`
470/// reads it (`_read_gzip_header`, `read`, `_read_eof`), handing each output
471/// chunk to `sink`; returns the decompressed length. The header's FHCRC is
472/// skipped, not checked, as Python skips it. Each chunk, and every byte of
473/// `data` read for it, is counted as `open`'s work when there is one
474/// (module docs, "Work").
475fn gzip_inflate(
476 data: &[u8],
477 max_inflate: u64,
478 open: Option<&Meter>,
479 mut sink: impl FnMut(&[u8]),
480) -> Result<u64, DetectError> {
481 let mut scratch = vec![0; SCRATCH];
482 gzip_inflate_to(data, max_inflate, open, &mut scratch, |out| {
483 sink(out);
484 Ok(())
485 })
486}
487
488pub(crate) fn gzip_inflate_to<E: From<DetectError>>(
489 data: &[u8],
490 max_inflate: u64,
491 open: Option<&Meter>,
492 scratch: &mut [u8],
493 mut sink: impl FnMut(&[u8]) -> Result<(), E>,
494) -> Result<u64, E> {
495 if let Some(l) = open.and_then(Meter::work_refused) {
496 return Err(DetectError::Work(l).into());
497 }
498 let mut pos = 0usize;
499 let mut total = 0u64;
500 loop {
501 // `_read_gzip_header`: `fp.read(2)` empty ends the file; anything
502 // else must be the magic.
503 let rest = data.get(pos..).unwrap_or_default();
504 if rest.is_empty() {
505 return Ok(total);
506 }
507 if !rest.starts_with(&GZIP_MAGIC) {
508 return Err(gzip_error("BadGzipFile: Not a gzipped file").into());
509 }
510 member_header(data, &mut pos, open)?;
511 // The member's raw deflate stream.
512 let input = data.get(pos..).unwrap_or_default();
513 let mut z = Decompress::new(false);
514 let mut crc = Crc::new();
515 loop {
516 let (in_before, out_before) = (z.total_in(), z.total_out());
517 let at = usize::try_from(in_before).unwrap_or(usize::MAX);
518 let status = z
519 .decompress(
520 input.get(at..).unwrap_or_default(),
521 scratch,
522 FlushDecompress::None,
523 )
524 .map_err(|e| DetectError::Gzip(format!("zlib.error: {e}").into()));
525 // What the call consumed and made counts whether it failed or
526 // not (zlib-rs advances flate2's totals by what it read and
527 // wrote before it reports a failure), and before the stream's
528 // own bound is checked, so the chunk that passes `max_inflate`
529 // is the open's work too.
530 let made = usize::try_from(z.total_out() - out_before).unwrap_or(0);
531 let consumed = z.total_in() - in_before;
532 let made_bytes = u64::try_from(made).unwrap_or(u64::MAX);
533 let status = counted(open, made_bytes.saturating_add(consumed), status)?;
534 let out = scratch.get(..made).unwrap_or_default();
535 crc.update(out);
536 total = total.saturating_add(made_bytes);
537 if total > max_inflate {
538 return Err(inflate_limit(total, max_inflate).into());
539 }
540 sink(out)?;
541 if status == Status::StreamEnd {
542 break;
543 }
544 if z.total_in() == in_before && made == 0 {
545 return Err(gzip_error(EOF_MESSAGE).into());
546 }
547 }
548 pos = pos.saturating_add(usize::try_from(z.total_in()).unwrap_or(usize::MAX));
549 let eof = pos;
550 let got = member_trailer(data, &mut pos, &crc);
551 counted(open, read_from(eof, pos), got)?;
552 }
553}
554
555/// `_read_eof` from `*pos`: the CRC-32 and the size modulo 2^32, checked,
556/// then the zero bytes after them; `*pos` moves past each byte read.
557fn member_trailer(data: &[u8], pos: &mut usize, crc: &Crc) -> Result<(), DetectError> {
558 let trailer = read_exact(data, pos, 8)?;
559 let word = |i: usize| {
560 trailer
561 .get(i..i + 4)
562 .and_then(|b| <[u8; 4]>::try_from(b).ok())
563 .map_or(0, u32::from_le_bytes)
564 };
565 if word(0) != crc.sum() {
566 return Err(DetectError::GzipCrc {
567 detail: "BadGzipFile: CRC check failed".into(),
568 expected: word(0),
569 actual: crc.sum(),
570 });
571 }
572 if word(4) != crc.amount() {
573 return Err(gzip_error("BadGzipFile: Incorrect length of data produced"));
574 }
575 while data.get(*pos) == Some(&0) {
576 *pos += 1;
577 }
578 Ok(())
579}
580
581/// What UnityPy builds from a detected file (`l0.parsed_as`).
582#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
583pub enum ParsedAs {
584 /// A `SerializedFile`.
585 SerializedFile,
586 /// A `BundleFile`.
587 BundleFile,
588 /// A `WebFile`.
589 WebFile,
590 /// UnityPy kept a reader: a resource, a name-rule file, or a zip entry.
591 Raw,
592 /// An input detected as a zip.
593 Zip,
594 /// Detection or parsing raised.
595 Error,
596}
597
598/// `parse_file`'s choice. An `AssetsFile` whose name ends in `.resS`,
599/// `.resource`, `.config`, `.xml` or `.dat` (a case-sensitive byte suffix) stays
600/// raw; a zip is `Zip` only for an input (`is_entry` false), else raw.
601#[must_use]
602pub fn parsed_as(ty: FileType, name: &[u8], is_entry: bool) -> ParsedAs {
603 // UnityPy helpers/ImportHelper.py:138-151; a zip input is refused before
604 // `parse_file` (CANONICAL_DUMP.md section 4.2), an entry stays a reader.
605 match ty {
606 FileType::AssetsFile if raw_by_name(name) => ParsedAs::Raw,
607 FileType::AssetsFile => ParsedAs::SerializedFile,
608 FileType::BundleFile => ParsedAs::BundleFile,
609 FileType::WebFile => ParsedAs::WebFile,
610 FileType::Zip if !is_entry => ParsedAs::Zip,
611 FileType::Zip | FileType::ResourceFile => ParsedAs::Raw,
612 }
613}
614
615/// Whether `name` ends in one of the suffixes that keep an `AssetsFile` raw
616/// ([`parsed_as`]): the only part of a name that changes what `parse_file`
617/// builds from given bytes.
618pub(crate) fn raw_by_name(name: &[u8]) -> bool {
619 const RAW_SUFFIXES: [&[u8]; 5] = [b".resS", b".resource", b".config", b".xml", b".dat"];
620 RAW_SUFFIXES.iter().any(|s| name.ends_with(s))
621}
622
623#[cfg(test)]
624mod tests {
625 use super::{DetectError, count_work};
626 use crate::error::LimitKind;
627 use crate::limits::Meter;
628
629 /// Under an allowance of `u64::MAX`, inflated work whose sum passes
630 /// `u64::MAX` is refused by the meter's count, and that refusal, which
631 /// it counted (CR8), is returned: round 4 asked, and the work query
632 /// that answered then saw the saturated sum within the allowance and
633 /// returned `Ok`. No input inflates that much, so only this test
634 /// reaches it.
635 #[test]
636 fn work_past_u64_max_is_refused_under_an_allowance_of_u64_max() {
637 let meter = Meter::unlimited();
638 meter.add_work(u64::MAX - 5);
639 assert!(count_work(&meter, 5).is_ok(), "the allowance is inclusive");
640 match count_work(&meter, 1) {
641 Err(DetectError::Work(l)) => assert_eq!(
642 (l.kind(), l.value(), l.max()),
643 (LimitKind::DecodeWork, u64::MAX, u64::MAX)
644 ),
645 other => panic!("{other:?}"),
646 }
647 assert_eq!(meter.decoded(), u64::MAX);
648 assert!(
649 meter.work_used_up().is_none(),
650 "the sum saturates at the allowance"
651 );
652 }
653}