Skip to main content

unityrs_core/files/web/
write.rs

1// Ported from UnityPy 1.25.2 (MIT): UnityPy/files/WebFile.py. See THIRD_PARTY_LICENSES.
2//! WebFile directory rebuilding (`files/WebFile.py:58-112`). Compression
3//! preserves decompressed bytes, with deterministic stored streams. Brotli's
4//! metadata marker corrects the oracle writer's undetectable output (Tier C).
5
6use super::{WebFile, WebPacker};
7use crate::files::save::{SaveError, SaveLayout, SaveOptions, Writer, narrow};
8use crate::files::{BundlePacker, BundleSaveOptions, File};
9use crate::limits::{Meter, MeteredVec};
10use crate::stream::Endian;
11use bytes::Bytes;
12use std::sync::Arc;
13
14/// WebFile save layout and compression. The default writes plain bytes and
15/// `UnityWebData1.0`, as UnityPy does, independently of the input's settings.
16#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
17pub struct WebSaveOptions {
18    /// Layout to use for nested serialized files and bundles.
19    pub layout: SaveLayout,
20    /// The enclosing WebFile's compression.
21    pub packer: WebPacker,
22}
23
24const _: () = crate::send_sync::<WebSaveOptions>();
25
26impl WebFile {
27    /// Rebuild the live ordered files dictionary. Duplicate input names retain
28    /// their first position and last value. Children use their default packer.
29    /// Saving leaves the source bytes and change flags intact.
30    pub fn save(&self, options: WebSaveOptions) -> Result<Bytes, SaveError> {
31        self.save_with_signature(options, "UnityWebData1.0")
32    }
33
34    /// Save with an explicit signature, matching UnityPy's `signature=`.
35    /// Semantic layout refuses signatures that the reader cannot round trip.
36    pub fn save_with_signature(
37        &self,
38        options: WebSaveOptions,
39        signature: &str,
40    ) -> Result<Bytes, SaveError> {
41        if options.layout == SaveLayout::Semantic {
42            if signature.len() >= crate::stream::STRING_TO_NULL_WINDOW
43                || (!signature.starts_with("UnityWebData")
44                    && !signature.starts_with("TuanjieWebData"))
45            {
46                return Err(SaveError::Range("WebFile signature"));
47            }
48            self.meter().count_decode(signature.len() as u64)?;
49            if signature.as_bytes().contains(&0) {
50                return Err(SaveError::Range("WebFile signature"));
51            }
52        }
53        let mut header_size = add(signature.len(), 5)?;
54        let cabs = self.writeable_cabs()?;
55        for cab in cabs.as_slice() {
56            cab.validate_name(options.layout, true)?;
57        }
58        for slot in &self.files {
59            self.meter().count_decode(12)?;
60            header_size = add(header_size, add(12, slot.name.as_bytes().len())?)?;
61        }
62        for cab in cabs.as_slice() {
63            header_size = add(header_size, add(12, cab.name().len())?)?;
64        }
65        let mut offset: i32 = narrow(header_size, "WebFile header size")?;
66        let mut data = Writer::new(self.meter(), Endian::Little)?;
67        let mut sizes =
68            MeteredVec::with_capacity(self.meter(), add(self.files.len(), cabs.len())?)?;
69        for slot in &self.files {
70            let child = match self.entry(slot.dir_index)? {
71                File::Raw(raw) => raw.data.clone(),
72                File::Serialized(sf) => sf.save(SaveOptions {
73                    layout: options.layout,
74                })?,
75                File::Bundle(bundle) => bundle.save(BundleSaveOptions {
76                    layout: options.layout,
77                    packer: BundlePacker::None,
78                })?,
79                File::Web(web) => web.save(WebSaveOptions {
80                    layout: options.layout,
81                    packer: WebPacker::None,
82                })?,
83            };
84            let size: i32 = narrow(child.len(), "WebFile entry size")?;
85            narrow::<i32, _>(
86                add(header_size, add(data.len(), child.len())?)?,
87                "WebFile size",
88            )?;
89            sizes.push(size)?;
90            data.bytes(&child)?;
91        }
92        for cab in cabs.as_slice() {
93            let child = cab.save()?;
94            let size: i32 = narrow(child.len(), "WebFile entry size")?;
95            narrow::<i32, _>(
96                add(header_size, add(data.len(), child.len())?)?,
97                "WebFile size",
98            )?;
99            sizes.push(size)?;
100            data.bytes(&child)?;
101        }
102        let mut out = Writer::new(self.meter(), Endian::Little)?;
103        out.cstr(signature.as_bytes())?;
104        out.i32(offset)?;
105        for (slot, size) in self.files.iter().zip(sizes.as_slice()) {
106            out.i32(offset)?;
107            out.i32(*size)?;
108            out.i32(narrow(slot.name.as_bytes().len(), "WebFile member name")?)?;
109            out.bytes(slot.name.as_bytes())?;
110            offset = offset
111                .checked_add(*size)
112                .ok_or(SaveError::Range("WebFile offset"))?;
113        }
114        for (cab, size) in cabs
115            .as_slice()
116            .iter()
117            .zip(sizes.as_slice().iter().skip(self.files.len()))
118        {
119            out.i32(offset)?;
120            out.i32(*size)?;
121            out.i32(narrow(cab.name().len(), "WebFile member name")?)?;
122            out.bytes(cab.name().as_bytes())?;
123            offset = offset
124                .checked_add(*size)
125                .ok_or(SaveError::Range("WebFile offset"))?;
126        }
127        out.bytes(data.slice())?;
128        drop(data);
129        if options.layout == SaveLayout::Semantic
130            && options.packer == WebPacker::None
131            && out.slice().get(32..38) == Some(b"brotli")
132        {
133            return Err(SaveError::Range("WebFile brotli marker collision"));
134        }
135        match options.packer {
136            WebPacker::None => out.finish(),
137            WebPacker::Gzip => gzip(out.slice(), self),
138            WebPacker::Brotli => brotli(out.slice(), self.meter()),
139        }
140    }
141}
142
143fn add(a: usize, b: usize) -> Result<usize, SaveError> {
144    a.checked_add(b).ok_or(SaveError::Range("WebFile size"))
145}
146
147// RFC 1951 stored blocks and RFC 1952 framing. No codec state or new heap
148// allocation outside Writer; output is charged before every write. UnityPy's
149// gzip helper chooses its own clock and compressor (`helpers/CompressionHelper.py:145-155`).
150fn gzip(data: &[u8], web: &WebFile) -> Result<Bytes, SaveError> {
151    let mut out = Writer::new(web.meter(), Endian::Little)?;
152    out.bytes(&[31, 139, 8, 0, 0, 0, 0, 0, 0, 255])?;
153    for (i, chunk) in data.chunks(65535).enumerate() {
154        out.bytes(&[u8::from(i == (data.len() - 1) / 65535)])?;
155        let size: u16 = narrow(chunk.len(), "gzip block size")?;
156        out.u16(size)?;
157        out.u16(!size)?;
158        out.bytes(chunk)?;
159    }
160    if data.is_empty() {
161        out.bytes(&[1, 0, 0, 255, 255])?;
162    }
163    web.meter().count_decode(data.len() as u64)?;
164    let mut crc = !0u32;
165    for byte in data {
166        crc ^= u32::from(*byte);
167        for _ in 0..8 {
168            crc = (crc >> 1) ^ (0xedb8_8320 & 0u32.wrapping_sub(crc & 1));
169        }
170    }
171    out.u32(!crc)?;
172    out.u32(narrow(data.len(), "gzip input size")?)?;
173    out.finish()
174}
175
176// RFC 7932 sections 9.1 and 9.2: WBITS=16, a skipped metadata block with
177// 36 bytes, then nonfinal uncompressed 16-bit-length metablocks, and ISLAST
178// with ISLASTEMPTY. Metadata puts Unity's detector marker at byte 32 without
179// changing decompressed data. All bit headers fit fixed stack arrays.
180pub(super) fn brotli(data: &[u8], meter: &Arc<Meter>) -> Result<Bytes, SaveError> {
181    let mut out = Writer::new(meter, Endian::Little)?;
182    let mut metadata = [0; 38];
183    metadata[..2].copy_from_slice(&[0xac, 0x11]);
184    metadata[32..].copy_from_slice(b"brotli");
185    out.bytes(&metadata)?;
186    for chunk in data.chunks(65536) {
187        let len: u32 = narrow(chunk.len() - 1, "brotli block size")?;
188        let bits = (len << 3) | (1 << 19);
189        out.bytes(&bits.to_le_bytes()[..3])?;
190        out.bytes(chunk)?;
191    }
192    out.bytes(&[3])?;
193    out.finish()
194}