use std::sync::Arc;
use flate2::{Crc, Decompress, FlushDecompress, Status};
use crate::error::{LimitError, LimitKind};
use crate::limits::{Limits, Meter};
use crate::stream::ReaderKind;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum FileType {
AssetsFile,
BundleFile,
WebFile,
ResourceFile,
Zip,
}
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum DetectError {
#[error(transparent)]
Allocator(#[from] crate::budget::AllocError),
#[error("unterminated signature in a 20-byte file")]
Unterminated,
#[error("Unterminated string: [b'']")]
UnterminatedStream,
#[error("gzip: {0}")]
Gzip(Arc<str>),
#[error("gzip: {detail}")]
GzipCrc {
detail: Arc<str>,
expected: u32,
actual: u32,
},
#[error(transparent)]
InflateLimit(LimitError),
#[error(transparent)]
Work(LimitError),
}
pub fn check_file_type(data: &[u8], limits: &Limits) -> Result<FileType, DetectError> {
detect(data, limits, None, ReaderKind::Memory)
}
pub fn check_file_type_with_reader(
data: &[u8],
limits: &Limits,
reader: ReaderKind,
) -> Result<FileType, DetectError> {
detect(data, limits, None, reader)
}
pub fn check_entry_type(
data: &[u8],
limits: &Limits,
meter: &Meter,
) -> Result<FileType, DetectError> {
detect(data, limits, Some(meter), ReaderKind::Memory)
}
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),
}
}
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),
Signed::Gzip if is_web_data(&gzip_signature(data, limits.max_inflate, work)?) => {
Ok(FileType::WebFile)
}
Signed::Gzip => Ok(past_gzip(data)),
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Signed {
Type(FileType),
Gzip,
}
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> {
if data.len() < 20 {
return Ok(Signed::Type(FileType::ResourceFile));
}
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();
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));
}
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)))
}
fn past_gzip(data: &[u8]) -> FileType {
if data.get(0x20..0x26) == Some(b"brotli".as_slice()) {
return FileType::WebFile;
}
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
}
}
const BUNDLE_SIGNATURES: [&[u8]; 4] = [
b"UnityWeb",
b"UnityRaw",
"\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];
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)
}
const SCRATCH: usize = 64 << 10;
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";
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)
}
fn count_work(meter: &Meter, bytes: u64) -> Result<(), DetectError> {
meter.count_work(bytes).map_err(|refused| {
meter.add_work(bytes);
DetectError::Work(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)
}
}
}
fn inflate_limit(total: u64, max_inflate: u64) -> DetectError {
DetectError::InflateLimit(LimitError::refused(LimitKind::Inflate, total, max_inflate))
}
fn read_from(from: usize, to: usize) -> u64 {
u64::try_from(to.saturating_sub(from)).unwrap_or(u64::MAX)
}
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)
}
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 {
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(())
}
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 {
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)?;
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()));
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)?;
}
}
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(())
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ParsedAs {
SerializedFile,
BundleFile,
WebFile,
Raw,
Zip,
Error,
}
#[must_use]
pub fn parsed_as(ty: FileType, name: &[u8], is_entry: bool) -> ParsedAs {
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,
}
}
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;
#[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"
);
}
}