Skip to main content

unityrs_core/files/
web.rs

1// Ported from UnityPy 1.25.2 (MIT): UnityPy/files/WebFile.py. See THIRD_PARTY_LICENSES.
2//! `WebFile` (`files/WebFile.py:15-56`): `UnityWebData`/`TuanjieWebData`,
3//! plain, gzip or brotli. Inflation is eager, bounded by `max_inflate` and
4//! charged as decode work; directory members are parsed once on demand.
5//! [`WebFile::load_all`] provides UnityPy's eager directory-order load.
6
7use std::sync::Arc;
8
9use bytes::Bytes;
10
11use crate::detect::{DetectError, check_entry_type};
12use crate::error::{Error, Unsupported};
13use crate::files::{File, FileSlot, ParentKind, parse_file};
14use crate::limits::{GrantScope, MeteredVec, heap_cost};
15use crate::once::{CellError, get_or_publish_result};
16use crate::stream::{Endian, Reader, ReaderKind};
17use crate::typetree::parse::string_to_null;
18mod inflate;
19mod write;
20use crate::files::{DirEntry, Parent};
21use crate::limits::{Charge, Meter};
22use crate::options::FileContext;
23use crate::raw::RawStr;
24pub use write::WebSaveOptions;
25
26/// Decode a standalone Brotli stream with the WebFile's metered inflater.
27pub fn decompress_brotli(
28    data: &Bytes,
29    meter: &Arc<Meter>,
30    context: &FileContext,
31) -> Result<Bytes, Error> {
32    inflate::brotli(data, meter, context)
33}
34
35/// Encode a standalone Brotli stream using deterministic stored metablocks.
36/// Its decoded bytes equal the input; compressed bytes differ from UnityPy's
37/// default quality-11 compressor, as for WebFile's existing Tier B writer.
38pub fn compress_brotli(
39    data: &[u8],
40    meter: &Arc<Meter>,
41) -> Result<Bytes, crate::files::save::SaveError> {
42    write::brotli(data, meter)
43}
44
45/// How a `WebFile` is packed.
46#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
47pub enum WebPacker {
48    /// Plain.
49    #[default]
50    None,
51    /// gzip.
52    Gzip,
53    /// brotli.
54    Brotli,
55}
56
57/// A `WebFile`.
58pub struct WebFile {
59    pub(crate) writable: super::cab::WritableCabs,
60    pub(super) changed: std::sync::atomic::AtomicBool,
61    name: RawStr,
62    signature: RawStr,
63    packer: WebPacker,
64    directory: Box<[DirEntry]>,
65    data: Bytes,
66    files: Box<[FileSlot]>,
67    pub(super) entries: Box<[super::teardown::FileCell]>,
68    reader: ReaderKind,
69    constructor_position: i128,
70    source_depth: u32,
71    this: std::sync::Weak<Self>,
72    parent: Option<Parent>,
73    pub(super) dependency_path: super::DependencyPath,
74    ctx: Arc<FileContext>,
75    /// The handle, its directory and its inflated data, charged to the input.
76    /// Last so all the storage it covers is freed before its refund.
77    pub(super) charge: Charge,
78}
79
80impl Drop for WebFile {
81    fn drop(&mut self) {
82        super::teardown::drain(&mut self.entries, &self.charge);
83    }
84}
85
86impl std::fmt::Debug for WebFile {
87    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
88        f.debug_struct("WebFile")
89            .field("name", &self.name)
90            .field("signature", &self.signature)
91            .field("packer", &self.packer)
92            .finish_non_exhaustive()
93    }
94}
95
96impl WebFile {
97    /// Opens a WebFile from bytes, inflating gzip or brotli under the input meter.
98    pub fn open(
99        name: RawStr,
100        data: Bytes,
101        parent: Option<Parent>,
102        meter: &Arc<Meter>,
103        ctx: &Arc<FileContext>,
104    ) -> Result<Arc<Self>, Error> {
105        Self::open_at_depth(name, data, parent, meter, ctx, ReaderKind::Memory, 0)
106    }
107
108    #[allow(clippy::too_many_arguments)]
109    pub(crate) fn open_at_depth(
110        name: RawStr,
111        data: Bytes,
112        parent: Option<Parent>,
113        meter: &Arc<Meter>,
114        ctx: &Arc<FileContext>,
115        reader: ReaderKind,
116        source_depth: u32,
117    ) -> Result<Arc<Self>, Error> {
118        Self::open_with_state(
119            name,
120            data,
121            parent,
122            meter,
123            ctx,
124            reader,
125            source_depth,
126            &mut None,
127        )
128    }
129
130    #[allow(clippy::too_many_arguments)]
131    pub(crate) fn open_with_state(
132        name: RawStr,
133        data: Bytes,
134        parent: Option<Parent>,
135        meter: &Arc<Meter>,
136        ctx: &Arc<FileContext>,
137        reader: ReaderKind,
138        source_depth: u32,
139        endian: &mut Option<Endian>,
140    ) -> Result<Arc<Self>, Error> {
141        let mut charge = meter.charge(heap_cost((size_of::<Self>() + 16) as u64))?;
142        let (data, packer, reader) = if data.starts_with(&[31, 139]) {
143            (
144                inflate::gzip(&data, meter, ctx)?,
145                WebPacker::Gzip,
146                ReaderKind::Memory,
147            )
148        } else if data.get(32..38) == Some(b"brotli") {
149            (
150                inflate::brotli(&data, meter, ctx)?,
151                WebPacker::Brotli,
152                ReaderKind::Memory,
153            )
154        } else {
155            *endian = Some(Endian::Little);
156            (data, WebPacker::None, reader)
157        };
158        let mut r = Reader::with_kind(&data, Endian::Little, reader);
159        // files/WebFile.py:39-56: prefix signature, signed table and strict UTF-8.
160        let signature = string_to_null(&mut r, &data, meter)?;
161        if !signature.as_bytes().starts_with(b"UnityWebData")
162            && !signature.as_bytes().starts_with(b"TuanjieWebData")
163        {
164            return Err(Error::WebSignature(RawStr::charged(
165                signature.as_bytes(),
166                meter,
167            )?));
168        }
169        let end = i128::from(r.read_i32()?);
170        let mut directory = MeteredVec::new(meter);
171        while r.position() < end {
172            meter.count_decode(12)?;
173            let offset = i64::from(r.read_i32()?);
174            let size = i64::from(r.read_i32()?);
175            let n = r.read_i32()?;
176            // Tier U: below -1 can rewind into this row forever. -1 is
177            // Tier A and reads the remaining bytes through the reader.
178            if n < -1 {
179                return Err(Error::Unsupported(Unsupported::WebFileNameLength));
180            }
181            let range = r.read_range(i64::from(n))?;
182            let name = data.slice(range);
183            meter.count_decode(name.len() as u64)?;
184            std::str::from_utf8(&name).map_err(|_| inflate::bad("invalid UTF-8 member name"))?;
185            directory.push(DirEntry {
186                path: RawStr::from_shared(name),
187                offset,
188                size,
189                flags: None,
190            })?;
191        }
192        // Predict only the final successful cursor. Entry reads and their
193        // validation still run in directory order during eager loading.
194        let constructor_position = directory.as_slice().last().map_or(r.position(), |last| {
195            reader
196                .read(data.len(), i128::from(last.offset), last.size)
197                .map_or(r.position(), |(_, position)| position)
198        });
199        let files = super::bundle::open::files_dict(&directory, meter)?;
200        let mut entries = MeteredVec::new(meter);
201        for _ in 0..directory.len() {
202            entries.push(super::teardown::FileCell::new())?;
203        }
204        let directory = super::bundle::open::keep(directory, &mut charge)?;
205        let files = super::bundle::open::keep(files, &mut charge)?;
206        let entries = super::bundle::open::keep(entries, &mut charge)?;
207        Ok(Arc::new_cyclic(|this| Self {
208            writable: crate::files::cab::WritableCabs::new(meter),
209            changed: std::sync::atomic::AtomicBool::new(false),
210            name,
211            signature,
212            packer,
213            directory,
214            data,
215            files,
216            entries,
217            reader,
218            source_depth,
219            constructor_position,
220            parent,
221            dependency_path: super::DependencyPath::default(),
222            charge,
223            ctx: ctx.clone(),
224            this: this.clone(),
225        }))
226    }
227
228    /// Final cursor of the uncompressed constructor reader.
229    #[must_use]
230    pub fn constructor_position(&self) -> i128 {
231        self.constructor_position
232    }
233
234    /// Loaded files dict: first key position, last row value. Newly created
235    /// resource writers are in [`Self::writeable_cabs`] and follow it on save.
236    #[must_use]
237    pub fn files(&self) -> &[FileSlot] {
238        &self.files
239    }
240
241    /// The bytes read by the file's own reader, with signed offsets and sizes.
242    pub fn entry_bytes(&self, i: usize) -> Result<Bytes, Error> {
243        let row = self
244            .directory
245            .get(i)
246            .ok_or_else(|| inflate::bad("no directory row"))?;
247        let mut r = Reader::with_kind(&self.data, Endian::Little, self.reader);
248        r.set_position(i128::from(row.offset))?;
249        Ok(self.data.slice(r.read_range(row.size)?))
250    }
251
252    /// Detect and parse an entry once; nested files share this input's meter.
253    pub fn entry(&self, i: usize) -> Result<File, Error> {
254        let cell = self
255            .entries
256            .get(i)
257            .ok_or_else(|| inflate::bad("no directory row"))?;
258        get_or_publish_result(cell, self.meter(), || {
259            let scope = GrantScope::enter(self.meter());
260            let got = self.parse_entry(i);
261            let granted = scope.finish(self.meter());
262            if got.as_ref().is_err_and(|e| !e.is_transient()) {
263                self.meter().add_work(granted);
264            }
265            got
266        })
267        .cloned()
268    }
269    fn parse_entry(&self, i: usize) -> Result<File, Error> {
270        let row = self
271            .directory
272            .get(i)
273            .ok_or_else(|| inflate::bad("no directory row"))?;
274        let got = (|| {
275            let data = self.entry_bytes(i)?;
276            let ty =
277                check_entry_type(&data, &self.ctx.limits, self.meter()).map_err(|e| match e {
278                    DetectError::Work(l) => Error::Limit(l),
279                    e => Error::Detect(e),
280                })?;
281            let parent = Parent {
282                name: self.name.clone(),
283                kind: ParentKind::Web {
284                    web: self.this.clone(),
285                },
286                depth: self
287                    .parent
288                    .as_ref()
289                    .map_or(self.source_depth, |p| p.depth)
290                    .saturating_add(1),
291                pinned: if self.packer == WebPacker::None {
292                    self.parent.as_ref().map_or(self.data.len() as u64, |p| {
293                        p.pinned.max(self.data.len() as u64)
294                    })
295                } else {
296                    self.data.len() as u64
297                },
298            };
299            self.dependency_path.parse_child(self.meter(), i, || {
300                parse_file(
301                    row.path.clone(),
302                    data,
303                    ty,
304                    Some(parent),
305                    self.meter(),
306                    &self.ctx,
307                )
308            })
309        })();
310        got.map_err(|e| match e {
311            Error::Entry { .. } | Error::Limit(_) | Error::Budget(_) | Error::Allocator(_) => e,
312            e => Error::Entry {
313                name: row.path.clone(),
314                source: Arc::new(e),
315            },
316        })
317    }
318    /// Eagerly load every row in directory order, including overwritten keys.
319    pub fn load_all(&self) -> Result<(), Error> {
320        for i in 0..self.directory.len() {
321            match self.entry(i)? {
322                File::Bundle(b) => b.load_all()?,
323                File::Web(w) => w.load_all()?,
324                _ => {}
325            }
326        }
327        Ok(())
328    }
329
330    /// The meter of the input it belongs to.
331    #[must_use]
332    pub fn meter(&self) -> &Arc<Meter> {
333        self.charge.meter()
334    }
335
336    /// The name it was opened under.
337    #[must_use]
338    pub fn name(&self) -> &RawStr {
339        &self.name
340    }
341
342    /// Its decompressed bytes.
343    #[must_use]
344    pub fn data(&self) -> &Bytes {
345        &self.data
346    }
347
348    /// The container it is an entry of.
349    #[must_use]
350    pub fn parent(&self) -> Option<&Parent> {
351        self.parent.as_ref()
352    }
353
354    /// The context it was opened with.
355    #[must_use]
356    pub fn context(&self) -> &Arc<FileContext> {
357        &self.ctx
358    }
359
360    /// The signature.
361    #[must_use]
362    pub fn signature(&self) -> &RawStr {
363        &self.signature
364    }
365
366    /// The packer.
367    #[must_use]
368    pub fn packer(&self) -> WebPacker {
369        self.packer
370    }
371
372    /// The directory (`flags` always `None`).
373    #[must_use]
374    pub fn directory(&self) -> &[DirEntry] {
375        &self.directory
376    }
377}