Skip to main content

unityrs_testkit/bundle/
matrix.rs

1// Ported from UnityPy 1.25.2 (MIT): UnityPy/files/BundleFile.py, UnityPy/files/ObjectReader.py, UnityPy/files/SerializedFile.py, UnityPy/helpers/ImportHelper.py, UnityPy/helpers/TypeTreeNode.py. See THIRD_PARTY_LICENSES.
2//! Package C's synthetic matrix ([`matrix`]) for the L0-L1 parity check,
3//! and the specs it is built from.
4
5use super::{BlockOverride, BundleSpec, EntrySpec, TestCodec, build, build_with_signature};
6use crate::writer::ByteWriter;
7
8/// Package C's synthetic matrix for the L0-L1 parity check (plan section
9/// 7.4, "Done when"): `(name, bytes)` inputs covering every `SerializedFile`
10/// format both endians, UnityFS 6-8 with every codec, blocks-info at either
11/// end, both alignment rules, padding, both flag sets, UnityWeb and UnityRaw
12/// at format 6, nested bundles, repeated paths, block overrides, and an
13/// AssetBundle (class 142) loose and inside a bundle, whose container rows
14/// the parse reads (C request 9). `tools/fixtures/files.py` records UnityPy's
15/// L0 and L1 records for each.
16#[must_use]
17// A table of cases, one per line.
18#[allow(clippy::too_many_lines)]
19pub fn matrix() -> Vec<(String, Vec<u8>)> {
20    use crate::serialized::build as file;
21    let mut out = Vec::new();
22    for format in 1..=22 {
23        for big in [false, true] {
24            let end = if big { "be" } else { "le" };
25            out.push((
26                format!("CAB-f{format}-{end}"),
27                file(&matrix_file(format, big)),
28            ));
29        }
30    }
31    let mut no_trees = matrix_file(17, false);
32    no_trees.type_trees = false;
33    out.push(("CAB-f17-notrees".into(), file(&no_trees)));
34    out.push(("sample.resS".into(), file(&matrix_file(22, false))));
35    let mut blank = matrix_file(22, false);
36    blank.unity_version = Vec::new();
37    out.push(("CAB-blank-version".into(), file(&blank)));
38    // An AssetBundle (class 142), whose container the parse reads (C9).
39    let assetbundle = |hostile: bool| {
40        use crate::env::{Bundle, Obj, row};
41        crate::env::serialized(
42            &["archive:/CAB-x/CAB-x"],
43            &[
44                Obj::named(1, "n", &[(0, 2), (1, 5)]),
45                Obj::asset_bundle(
46                    2,
47                    Bundle {
48                        preload: vec![(0, 1), (1, 5)],
49                        rows: vec![
50                            row("assets/n.prefab", (0, 2), (0, 1)),
51                            row("assets/n.prefab", (1, 1), (0, 2)),
52                        ],
53                        hostile,
54                        ..Bundle::default()
55                    },
56                ),
57            ],
58        )
59    };
60    out.push(("CAB-assetbundle".into(), assetbundle(false)));
61    out.push((
62        "assetbundle.bundle".into(),
63        crate::env::bundle(&[("CAB-assetbundle", assetbundle(false))]),
64    ));
65    // The same AssetBundle's container as a damaged tree holds it
66    // (`env::Bundle::hostile`): UnityPy loads it, each row's members it
67    // holds as no string or `int` dumped as `null`.
68    out.push(("CAB-assetbundle-hostile".into(), assetbundle(true)));
69
70    let codecs = [
71        ("none", TestCodec::None),
72        ("lzma", TestCodec::Lzma),
73        ("lz4", TestCodec::Lz4),
74        ("lz4hc", TestCodec::Lz4Hc),
75    ];
76    for format in [6, 7, 8] {
77        for (cname, codec) in codecs {
78            for at_end in [false, true] {
79                let spec = BundleSpec {
80                    format,
81                    data_codec: codec,
82                    info_codec: codec,
83                    info_at_end: at_end,
84                    block_size: 160,
85                    entries: matrix_entries(),
86                    ..BundleSpec::default()
87                };
88                let end = if at_end { "end" } else { "front" };
89                out.push((format!("b{format}-{cname}-{end}.bundle"), build(&spec)));
90            }
91        }
92    }
93    let base = || BundleSpec {
94        block_size: 160,
95        entries: matrix_entries(),
96        ..BundleSpec::default()
97    };
98    let mut cases: Vec<(&str, BundleSpec)> = vec![
99        (
100            "padding.bundle",
101            BundleSpec {
102                extra_flags: 0x200,
103                ..base()
104            },
105        ),
106        (
107            "padding-f6.bundle",
108            BundleSpec {
109                format: 6,
110                extra_flags: 0x200,
111                ..base()
112            },
113        ),
114        (
115            "old-flags-encrypted.bundle",
116            BundleSpec {
117                version_engine: b"2020.3.33f1".to_vec(),
118                extra_flags: 0x200,
119                ..base()
120            },
121        ),
122        (
123            "new-flags-encrypted.bundle",
124            BundleSpec {
125                extra_flags: 0x400,
126                ..base()
127            },
128        ),
129        (
130            "f6-2019.4.15.bundle",
131            BundleSpec {
132                format: 6,
133                version_engine: b"2019.4.15f1".to_vec(),
134                ..base()
135            },
136        ),
137        (
138            "f6-2019.4.14.bundle",
139            BundleSpec {
140                format: 6,
141                version_engine: b"2019.4.14f1".to_vec(),
142                ..base()
143            },
144        ),
145        (
146            "f6-2018.bundle",
147            BundleSpec {
148                format: 6,
149                version_engine: b"2018.4.36f1".to_vec(),
150                ..base()
151            },
152        ),
153        (
154            "engine-big-major.bundle",
155            BundleSpec {
156                version_engine: b"133091.1.1f1".to_vec(),
157                ..base()
158            },
159        ),
160        (
161            "engine-major-0.bundle",
162            BundleSpec {
163                version_engine: b"0.5.1f1".to_vec(),
164                ..base()
165            },
166        ),
167        (
168            "engine-nomatch.bundle",
169            BundleSpec {
170                version_engine: b"abc".to_vec(),
171                ..base()
172            },
173        ),
174        (
175            "engine-type-byte.bundle",
176            BundleSpec {
177                version_engine: b"2020.3.34f1024".to_vec(),
178                ..base()
179            },
180        ),
181        (
182            "engine-2021.3.2.bundle",
183            BundleSpec {
184                version_engine: b"2021.3.2ff1".to_vec(),
185                ..base()
186            },
187        ),
188        (
189            "one-block.bundle",
190            BundleSpec {
191                block_size: 1 << 20,
192                ..base()
193            },
194        ),
195        (
196            "tiny-blocks.bundle",
197            BundleSpec {
198                block_size: 7,
199                ..base()
200            },
201        ),
202        (
203            "lz4-declared-larger.bundle",
204            BundleSpec {
205                block_overrides: vec![over(0, None, Some(500), None)],
206                ..base()
207            },
208        ),
209        (
210            "lz4-declared-smaller.bundle",
211            BundleSpec {
212                block_overrides: vec![over(0, None, Some(100), None)],
213                ..base()
214            },
215        ),
216        (
217            "lzma-overlong.bundle",
218            BundleSpec {
219                data_codec: TestCodec::Lzma,
220                block_overrides: vec![over(0, None, Some(100), None)],
221                ..base()
222            },
223        ),
224        (
225            "stored-declared-differs.bundle",
226            BundleSpec {
227                data_codec: TestCodec::None,
228                block_overrides: vec![over(1, None, Some(40), None)],
229                ..base()
230            },
231        ),
232        (
233            "stored-compressed-larger.bundle",
234            BundleSpec {
235                data_codec: TestCodec::None,
236                block_overrides: vec![over(0, None, None, Some(200))],
237                ..base()
238            },
239        ),
240        (
241            "lzham-block.bundle",
242            BundleSpec {
243                block_overrides: vec![over(2, Some(TestCodec::Raw(4)), None, None)],
244                ..base()
245            },
246        ),
247        (
248            "unknown-codec-block.bundle",
249            BundleSpec {
250                block_overrides: vec![over(1, Some(TestCodec::Raw(9)), None, None)],
251                ..base()
252            },
253        ),
254        (
255            "unknown-codec-info.bundle",
256            BundleSpec {
257                info_codec: TestCodec::Raw(7),
258                ..base()
259            },
260        ),
261        (
262            "repeated-path.bundle",
263            BundleSpec {
264                entries: {
265                    let mut e = matrix_entries();
266                    e.push(EntrySpec {
267                        name: b"CAB-entry".to_vec(),
268                        data: file(&matrix_file(21, true)),
269                        flags: 4,
270                    });
271                    e
272                },
273                ..base()
274            },
275        ),
276        (
277            "blank-version-entry.bundle",
278            BundleSpec {
279                entries: vec![EntrySpec {
280                    name: b"CAB-blank".to_vec(),
281                    data: file(&blank),
282                    flags: 4,
283                }],
284                ..base()
285            },
286        ),
287        (
288            "nested.bundle",
289            BundleSpec {
290                entries: vec![
291                    EntrySpec {
292                        name: b"inner".to_vec(),
293                        data: build(&base()),
294                        flags: 0,
295                    },
296                    EntrySpec {
297                        name: b"CAB-outer".to_vec(),
298                        data: file(&matrix_file(19, false)),
299                        flags: 4,
300                    },
301                ],
302                ..base()
303            },
304        ),
305        (
306            "bad-entry.bundle",
307            BundleSpec {
308                entries: vec![
309                    EntrySpec {
310                        name: b"CAB-good".to_vec(),
311                        data: file(&matrix_file(22, false)),
312                        flags: 4,
313                    },
314                    EntrySpec {
315                        name: b"CAB-bad".to_vec(),
316                        data: file(&blank)[..200].to_vec(),
317                        flags: 4,
318                    },
319                ],
320                ..base()
321            },
322        ),
323        (
324            "empty.bundle",
325            BundleSpec {
326                entries: Vec::new(),
327                ..base()
328            },
329        ),
330    ];
331    let whole = build(&base());
332    cases.push((
333        "truncated.bundle",
334        BundleSpec {
335            truncate_to: Some(whole.len() - 30),
336            data_codec: TestCodec::None,
337            ..base()
338        },
339    ));
340    for (name, spec) in cases {
341        out.push((name.to_owned(), build(&spec)));
342    }
343    for sig in [&b"UnityWeb"[..], b"UnityRaw"] {
344        let spec = BundleSpec {
345            format: 6,
346            ..base()
347        };
348        let name = format!("{}-f6.bundle", String::from_utf8_lossy(sig));
349        out.push((name, build_with_signature(&spec, sig)));
350    }
351    out.extend(negative_position_sites(&base()));
352    out
353}
354
355/// Plan 3.3's negative-position sites, each patched into a well-formed
356/// input: a negative `node_count` and `stringbuffer_size` (UnityPy
357/// helpers/TypeTreeNode.py:113-118 reads `read(24 * node_count)`), a
358/// format-8 header whose `metadata_size` exceeds `file_size`
359/// (files/SerializedFile.py:266; detection, helpers/ImportHelper.py:110-121,
360/// already keeps such a file raw, so the oracle records a raw file),
361/// blocks-info at the end whose stored size
362/// exceeds the file (files/BundleFile.py:134-135), and a format-22 object
363/// whose `byte_start` plus `data_offset` passes `i64::MAX`
364/// (files/ObjectReader.py:82-86).
365fn negative_position_sites(bundle: &BundleSpec) -> Vec<(String, Vec<u8>)> {
366    use crate::blob::{Strings, blob_tree};
367    use crate::serialized::build as file;
368    let mut out = Vec::new();
369    for (name, field, value) in [
370        ("CAB-negative-node-count", 0usize, -1i32),
371        ("CAB-negative-stringbuffer", 4, -5),
372    ] {
373        let spec = matrix_file(22, false);
374        let mut bytes = file(&spec);
375        let nodes = spec.types.first().map_or(&[][..], |t| t.nodes.as_slice());
376        let blob = blob_tree(nodes, 22, false, Strings::Local, &[]);
377        if let Some(at) = bytes.windows(blob.len()).position(|w| w == blob.as_slice()) {
378            bytes[at + field..at + field + 4].copy_from_slice(&value.to_le_bytes());
379            out.push((name.to_owned(), bytes));
380        }
381    }
382    let mut f8 = file(&matrix_file(8, false));
383    let past = u32::try_from(f8.len())
384        .unwrap_or(u32::MAX)
385        .saturating_add(64);
386    f8[0..4].copy_from_slice(&past.to_be_bytes());
387    out.push(("CAB-f8-metadata-past-start".into(), f8));
388    let mut end = build(&BundleSpec {
389        info_at_end: true,
390        ..bundle.clone()
391    });
392    // The header: signature and format (12 bytes), the two version strings,
393    // the total size (8 bytes), then the stored blocks-info size.
394    let nul =
395        |b: &[u8], from: usize| from + b[from..].iter().position(|c| *c == 0).unwrap_or(0) + 1;
396    let at = nul(&end, nul(&end, 12)) + 8;
397    let past = u32::try_from(end.len())
398        .unwrap_or(u32::MAX)
399        .saturating_add(100);
400    end[at..at + 4].copy_from_slice(&past.to_be_bytes());
401    out.push(("info-end-past-start.bundle".into(), end));
402    // The first object's row (path id 1, start 0, size 12) given the start
403    // `i64::MAX`: detection bounds `data_offset` by the file, not a row.
404    let mut big = file(&matrix_file(22, false));
405    let row: Vec<u8> = [
406        &1i64.to_le_bytes()[..],
407        &0i64.to_le_bytes(),
408        &12u32.to_le_bytes(),
409    ]
410    .concat();
411    if let Some(at) = big.windows(row.len()).position(|w| w == row.as_slice()) {
412        big[at + 8..at + 16].copy_from_slice(&i64::MAX.to_le_bytes());
413        out.push(("CAB-f22-byte-start-past-i64".into(), big));
414    }
415    out
416}
417
418fn over(
419    block: usize,
420    codec: Option<TestCodec>,
421    uncompressed: Option<u32>,
422    compressed: Option<u32>,
423) -> BlockOverride {
424    BlockOverride {
425        block,
426        codec,
427        uncompressed,
428        compressed,
429    }
430}
431
432/// A bundle's entries in the matrix: a `SerializedFile` and its resource.
433fn matrix_entries() -> Vec<EntrySpec> {
434    vec![
435        EntrySpec {
436            name: b"CAB-entry".to_vec(),
437            data: crate::serialized::build(&matrix_file(22, false)),
438            flags: 4,
439        },
440        EntrySpec {
441            name: b"CAB-entry.resS".to_vec(),
442            data: (0..300u32)
443                .map(|i| u8::try_from(i % 251).unwrap_or(0))
444                .collect(),
445            flags: 0,
446        },
447    ]
448}
449
450/// A `SerializedFile` of `format` exercising each table: two types (one
451/// that reads a script id), repeated path ids, script indexes, an external,
452/// a ref type from 20, user information.
453fn matrix_file(format: u32, big_endian: bool) -> crate::serialized::SerializedSpec {
454    use crate::blob::NodeSpec;
455    use crate::serialized::{ExternalSpec, ObjectSpec, SerializedSpec, TypeSpec};
456    let nodes = vec![
457        NodeSpec::new(0, "GameObject", "Base"),
458        NodeSpec::new(1, "int", "m_A").size(4),
459        NodeSpec::new(1, "UInt8", "m_B").size(1).aligned(),
460        NodeSpec::new(1, "vector", "m_C"),
461        NodeSpec::new(2, "Array", "Array").size(-1),
462        NodeSpec::new(3, "int", "size").size(4),
463        NodeSpec::new(3, "char", "data").size(1),
464    ];
465    let script_class = if format < 16 { -3 } else { 114 };
466    let types = vec![
467        TypeSpec {
468            class_id: 1,
469            script_type_index: -1,
470            old_type_hash: [7; 16],
471            nodes: nodes.clone(),
472            dependencies: vec![1, 2],
473            ..TypeSpec::default()
474        },
475        TypeSpec {
476            class_id: script_class,
477            script_type_index: 0,
478            script_id: [5; 16],
479            old_type_hash: [6; 16],
480            nodes: vec![
481                NodeSpec::new(0, "MonoBehaviour", "Base"),
482                NodeSpec::new(1, "int", "m_X").size(4),
483            ],
484            ..TypeSpec::default()
485        },
486    ];
487    let type_id = |i: usize| -> i32 {
488        if format >= 16 {
489            i32::try_from(i).unwrap_or(0)
490        } else {
491            types.get(i).map_or(0, |t| t.class_id)
492        }
493    };
494    let class = |i: usize| {
495        u16::try_from(types.get(i).map_or(0, |t| t.class_id.rem_euclid(65536))).unwrap_or(0)
496    };
497    let object = |path_id: i64, t: usize, script: Option<i16>| ObjectSpec {
498        path_id,
499        type_id: type_id(t),
500        class_id: class(t),
501        // m_A, m_B aligned, and m_C's count (0), each written as a field
502        // (`writer::with_fields`).
503        data: {
504            let mut w = ByteWriter::le();
505            w.i32(1).u8(2).align(4).i32(0);
506            w.into_inner()
507        },
508        script_type_index: script,
509        ..ObjectSpec::default()
510    };
511    let objects = vec![
512        object(1, 0, None),
513        object(-3, 1, Some(0)),
514        object(2, 0, None),
515        object(-3, 1, Some(1)),
516    ];
517    SerializedSpec {
518        format,
519        big_endian,
520        unity_version: b"2022.3.0f1".to_vec(),
521        platform: 19,
522        types,
523        objects,
524        scripts: vec![(0, 11), (1, 1 << 40)],
525        // Below format 6 the external has no `temp_empty` (UnityPy leaves it
526        // unset and the oracle records `null`, C request 3).
527        externals: vec![ExternalSpec {
528            temp_empty: Vec::new(),
529            guid: [3; 16],
530            kind: 3,
531            path: b"library/unity default resources".to_vec(),
532        }],
533        ref_types: vec![TypeSpec {
534            class_id: 114,
535            script_type_index: 0,
536            script_id: [9; 16],
537            old_type_hash: [8; 16],
538            nodes: vec![
539                NodeSpec::new(0, "Ref", "Base"),
540                NodeSpec::new(1, "float", "m_F").size(4),
541            ],
542            ref_names: Some((b"Foo".to_vec(), b"Ns".to_vec(), b"Asm.dll".to_vec())),
543            ..TypeSpec::default()
544        }],
545        user_information: b"ui".to_vec(),
546        big_id_enabled: i32::from(format == 10 || format == 12),
547        ..SerializedSpec::default()
548    }
549}