unityrs-core 1.0.0

Read and write Unity asset bundles, serialized files and typetrees.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// Ported from UnityPy 1.25.2 (MIT): UnityPy/files/BundleFile.py. See THIRD_PARTY_LICENSES.
//! Block outcomes and runs, as the `bundle` module docs describe them: each
//! block's kept length or failure, measuring decodes, the known prefix a
//! read searches for its blocks, and the cached runs a read slices, or
//! decodes and publishes. A read costs O(log B + log R) steps besides the
//! blocks it learns or decodes, each counted as the open's work (`index`'s
//! module docs).

use std::ops::Range;
use std::sync::PoisonError;

use bytes::Bytes;

use super::ahead::Ahead;
use super::decode::decode_block_into;
use super::index::{Published, STEP, search_steps};
use super::open::known_at_open;
use super::{BundleFile, no_entry};
use crate::budget::DecodeBuf;
use crate::codec::Codec;
use crate::counters::SpanRow;
use crate::error::{Error, LimitKind};
use crate::limits::MeteredVec;
use crate::once::get_or_publish_result;

/// A read's blocks, `first..end`, from joined offset `first_at` to `end_at`,
/// with the run decoded from `first` when the walk that found them decoded
/// it.
struct Span {
    first: usize,
    first_at: u64,
    end: usize,
    end_at: u64,
    buf: Option<DecodeBuf>,
}

impl BundleFile {
    /// Block `i`'s length, if known (an error is the block's kept failure).
    fn known(&self, i: usize) -> Option<Result<u64, Error>> {
        Some(
            self.lengths
                .get(i)?
                .get()?
                .as_ref()
                .map(|n| *n)
                .map_err(|k| k.error().clone()),
        )
    }

    /// Counts `n` steps as the open's work (`index`'s module docs), refused
    /// with [`LimitKind::DecodeWork`] when they would pass its allowance.
    fn steps(&self, n: u64) -> Result<(), Error> {
        if n > 0 {
            self.meter().count_decode(n.saturating_mul(STEP))?;
        }
        Ok(())
    }

    /// Adds block `k`, of length `len`, to the known prefix: one step.
    fn learn(&self, k: usize, len: u64) -> Result<(), Error> {
        self.steps(1)?;
        self.ends.learn(k, len);
        Ok(())
    }

    /// Decodes block `i` onto the end of `out` and records its length (a
    /// failure the input decides is kept, a refusal is not): the length.
    /// What it wrote is counted ([`Self::counts`]), as written again when
    /// the block's length was known before it began, but from its stored
    /// bytes at open.
    fn decode_into(&self, i: usize, out: &mut DecodeBuf) -> Result<u64, Error> {
        let before = out.len();
        let (Some(b), Some(range)) = (self.blocks.get(i), self.stored.get(i)) else {
            return Err(no_entry(i));
        };
        let known = self.lengths.get(i).is_some_and(|c| c.get().is_some());
        let again = known && !known_at_open(b, &self.ctx);
        let input = self.source.slice(range.clone());
        let got = decode_block_into(
            &self.ctx,
            self.meter(),
            input,
            b.uncompressed,
            u32::from(b.flags_u16()),
            i,
            out,
        )
        .map(|()| u64::try_from(out.len() - before).unwrap_or(u64::MAX));
        if got.is_err() {
            out.truncate(before);
        }
        let cell = self.lengths.get(i).ok_or_else(|| no_entry(i))?;
        let mine = got?;
        self.counts.wrote(mine, again);
        let published = *get_or_publish_result(cell, self.meter(), || Ok(mine))?;
        if published != mine {
            return Err(Error::Decompress {
                codec: Codec::from_flags(u32::from(b.flags_u16())),
                detail: format!("block {i} decoded to {mine} bytes, before to {published}").into(),
            });
        }
        Ok(mine)
    }

    /// Decodes block `i` and records the outcome, keeping a failure the
    /// input decides.
    fn decode_or_keep(&self, i: usize, out: &mut DecodeBuf) -> Result<u64, Error> {
        let got = self.decode_into(i, out);
        if let (Err(e), Some(cell)) = (&got, self.lengths.get(i)) {
            // A refusal (meter, budget) is not kept; anything else is.
            let _ = get_or_publish_result(cell, self.meter(), || Err(e.clone()));
        }
        got
    }

    /// Decodes blocks `from..to`, already known, onto the end of `out`: a
    /// step each, besides what they write.
    fn decode_again(&self, from: usize, to: usize, out: &mut DecodeBuf) -> Result<(), Error> {
        for k in from..to {
            self.decode_or_keep(k, out)?;
            self.steps(1)?;
        }
        Ok(())
    }

    /// [`Self::decode_or_keep`] of block `k`, which starts at joined offset
    /// `at`, onto the end of a run's buffer, whose room may grow into what
    /// the run's next blocks declare ([`Ahead`]).
    fn decode_in_run(
        &self,
        k: usize,
        at: u64,
        out: &mut DecodeBuf,
        ahead: &mut Ahead,
    ) -> Result<u64, Error> {
        let declared = |i: usize| self.blocks.get(i).map(|b| u64::from(b.uncompressed));
        out.set_ahead(ahead.before(k, at, declared));
        self.decode_or_keep(k, out)
    }

    /// A run's buffer with room for `initial` bytes, charged before it is
    /// allocated. The room is memory; each block's decode counts what it
    /// writes into it as the open's work (`DecodeBuf`, round 6).
    fn new_buf(&self, initial: u64) -> Result<DecodeBuf, Error> {
        Ok(DecodeBuf::new(
            self.meter(),
            initial,
            u64::MAX,
            LimitKind::BlockOutput,
        )?)
    }

    /// A measuring decode's buffer, dropped unused: its bytes are counted as
    /// discarded ([`Self::counts`]).
    fn discard(&self, scratch: Option<DecodeBuf>) {
        if let Some(s) = scratch {
            self.counts
                .dropped(u64::try_from(s.len()).unwrap_or(u64::MAX));
        }
    }

    /// Block `k`'s length, measured when it is not known: decoded into a new
    /// buffer, which comes back with it.
    fn measure(&self, k: usize) -> Result<(u64, Option<DecodeBuf>), Error> {
        if let Some(len) = self.known(k) {
            return Ok((len?, None));
        }
        let mut scratch = self.new_buf(0)?;
        let len = self.decode_or_keep(k, &mut scratch)?;
        Ok((len, Some(scratch)))
    }

    /// Every block's length, measured: the known prefix is extended to the
    /// last block, unknown blocks decoded with their output discarded. The
    /// joined total; once every block is known, one lookup.
    pub(super) fn measure_all(&self) -> Result<u64, Error> {
        if let Some(data) = &self.legacy_data {
            return Ok(data.len() as u64);
        }
        let n = self.blocks.len();
        for k in self.ends.known()..n {
            let (len, scratch) = self.measure(k)?;
            self.discard(scratch);
            self.learn(k, len)?;
        }
        Ok(self.ends.start(n))
    }

    /// The joined bytes `[lo, min(hi, total))`. Every block starting below
    /// `hi` gets a known length (the known prefix is extended to it, decoding
    /// the unknown blocks; one wholly before `lo` is decoded with its output
    /// discarded), the blocks overlapping the read form its span, and the
    /// span is a cached run's slice, a slice of the input (consecutive stored
    /// blocks, which is then cached as a run), or decoded into a new run that
    /// is published. Blocks the known prefix holds are found by binary
    /// search, and a cached run by one search of the run index.
    pub(super) fn read_span(&self, lo: u64, hi: u64, row: SpanRow) -> Result<Bytes, Error> {
        Ok(self.read_span_pinned(lo, hi, row)?.0)
    }

    /// [`Self::read_span`], with the length of the buffer the bytes are a
    /// slice of, which a slice of them keeps alive ([`Self::pins`]). Every
    /// read of the joined data comes here, for the row its caller names
    /// (`load_all` for the whole file), and is counted once under the test
    /// feature (`crate::testing`, "Span reads").
    pub(super) fn read_span_pinned(
        &self,
        lo: u64,
        hi: u64,
        row: SpanRow,
    ) -> Result<(Bytes, u64), Error> {
        if let Some(data) = &self.legacy_data {
            let start = usize::try_from(lo).unwrap_or(usize::MAX).min(data.len());
            let end = usize::try_from(hi)
                .unwrap_or(usize::MAX)
                .clamp(start, data.len());
            let pinned = if self.header.signature.as_bytes() == b"UnityRaw" {
                self.pinned()
            } else {
                data.len() as u64
            };
            return Ok((data.slice(start..end), pinned));
        }

        crate::counters::span_read(&self.origin, row);
        let Some((first, first_at, buf)) = self.first_block(lo, hi)? else {
            return Ok((Bytes::new(), 0));
        };
        let Span {
            first,
            first_at,
            end,
            end_at,
            buf,
        } = self.span_end(first, first_at, buf, hi)?;
        let from = usize::try_from(lo.saturating_sub(first_at)).unwrap_or(usize::MAX);
        let to = usize::try_from(hi.min(end_at).saturating_sub(first_at)).unwrap_or(usize::MAX);
        let (run, pinned) = if let Some(b) = buf {
            self.publish(first, end, first_at, b.into_bytes()?, true)
        } else if let Some(hit) = self.cached(first, end, first_at)? {
            hit
        } else if let Some(bytes) = self.stored_span(first, end)? {
            self.publish(first, end, first_at, bytes, false)
        } else {
            let mut b = self.new_buf(end_at.saturating_sub(first_at))?;
            self.decode_again(first, end, &mut b)?;
            self.publish(first, end, first_at, b.into_bytes()?, true)
        };
        let to = to.min(run.len());
        Ok((run.slice(from.min(to)..to), pinned))
    }

    /// What a slice of `run` keeps alive: the buffer of the bundle's own
    /// bytes when `run` is a slice of them (stored blocks; its
    /// [`BundleFile::pinned`]), else `run`'s own length, a decoded run being
    /// a buffer of its own.
    fn pins(&self, run: &Bytes) -> u64 {
        let (src, at) = (self.source.as_ptr().addr(), run.as_ptr().addr());
        let within =
            src <= at && at.saturating_add(run.len()) <= src.saturating_add(self.source.len());
        if within && !run.is_empty() {
            self.pinned()
        } else {
            u64::try_from(run.len()).unwrap_or(u64::MAX)
        }
    }

    /// The read's first block, the first that ends past `lo` if it starts
    /// below `hi`: its index, where it starts and, when the walk decoded it,
    /// its buffer. Found by binary search when the known prefix reaches past
    /// `lo`; otherwise the prefix is extended a block at a time until a
    /// block ends past `lo` or starts at `hi`.
    fn first_block(
        &self,
        lo: u64,
        hi: u64,
    ) -> Result<Option<(usize, u64, Option<DecodeBuf>)>, Error> {
        let known = self.ends.known();
        let (first, probes) = self.ends.first_ending_past(0, known, lo);
        self.steps(probes)?;
        if first < known {
            let at = self.ends.start(first);
            return Ok((at < hi).then_some((first, at, None)));
        }
        let mut at = self.ends.start(known);
        for k in known..self.blocks.len() {
            if at >= hi {
                break;
            }
            let (len, scratch) = self.measure(k)?;
            self.learn(k, len)?;
            if at.saturating_add(len) > lo {
                return Ok(Some((k, at, scratch)));
            }
            self.discard(scratch);
            at = at.saturating_add(len);
        }
        Ok(None)
    }

    /// The read's span from its first block to the last block that starts
    /// below `hi`. Found by binary search when the known prefix reaches `hi`
    /// and the walk decoded nothing; otherwise the prefix is extended to it,
    /// and from the first block of the span that is not known (or from
    /// `first`, when the walk decoded it) the run is decoded: its known
    /// blocks again, then each block as it is learned, its room read ahead
    /// ([`Ahead`]).
    fn span_end(
        &self,
        first: usize,
        first_at: u64,
        mut buf: Option<DecodeBuf>,
        hi: u64,
    ) -> Result<Span, Error> {
        let mut k = first + 1;
        if buf.is_none() {
            let known = self.ends.known();
            let (last, probes) = self.ends.first_ending_at(first, known, hi);
            self.steps(probes)?;
            if last < known {
                return Ok(Span {
                    first,
                    first_at,
                    end: last + 1,
                    end_at: self.ends.end(last),
                    buf: None,
                });
            }
            k = k.max(known);
        }
        let mut at = self.ends.start(k);
        let mut ahead = Ahead::new(first, hi);
        while k < self.blocks.len() && at < hi {
            let len = match (self.known(k), buf.as_mut()) {
                (Some(len), None) => len?,
                (_, Some(b)) => self.decode_in_run(k, at, b, &mut ahead)?,
                (None, None) => {
                    let mut b = self.new_buf(0)?;
                    for j in first..k {
                        self.decode_in_run(j, self.ends.start(j), &mut b, &mut ahead)?;
                        self.steps(1)?;
                    }
                    let len = self.decode_in_run(k, at, &mut b, &mut ahead)?;
                    buf = Some(b);
                    len
                }
            };
            self.learn(k, len)?;
            at = at.saturating_add(len);
            k += 1;
        }
        Ok(Span {
            first,
            first_at,
            end: k,
            end_at: at,
            buf,
        })
    }

    /// A cached run covering blocks `first..end`, sliced to start at block
    /// `first` (joined offset `first_at`): one search of the run index. With
    /// what a slice of it keeps alive ([`Self::pins`]).
    fn cached(
        &self,
        first: usize,
        end: usize,
        first_at: u64,
    ) -> Result<Option<(Bytes, u64)>, Error> {
        let hit = {
            let runs = self.runs.lock().unwrap_or_else(PoisonError::into_inner);
            self.steps(search_steps(runs.len()))?;
            runs.covering(first, end)
        };
        Ok(hit.map(|(f, bytes)| {
            (
                self.slice_from_block(f, &bytes, first_at),
                self.pins(&bytes),
            )
        }))
    }

    /// `bytes`, a run from block `f`, from joined offset `first_at` on (every
    /// block before `f` is in the known prefix).
    fn slice_from_block(&self, f: usize, bytes: &Bytes, first_at: u64) -> Bytes {
        let skip = first_at.saturating_sub(self.ends.start(f));
        let skip = usize::try_from(skip).unwrap_or(usize::MAX).min(bytes.len());
        bytes.slice(skip..)
    }

    /// Blocks `first..end` as one slice of the input, when every one is
    /// stored, stored blocks are their own bytes ([`crate::codec::Codecs::stores_as_is`]),
    /// and each follows the previous in the input: a step for each block it
    /// checks. Every block of the span
    /// has a known length, and a stored block's is known only within the
    /// block cap ([`crate::limits::Limits::max_block_output`]: preset at open
    /// only within it, and otherwise from its codec, which refuses a range
    /// past it), so no block of a slice is past the cap.
    fn stored_span(&self, first: usize, end: usize) -> Result<Option<Bytes>, Error> {
        if !self.ctx.codecs.stores_as_is() {
            return Ok(None);
        }
        let mut at: Option<Range<usize>> = None;
        for k in first..end {
            self.steps(1)?;
            let (Some(b), Some(r)) = (self.blocks.get(k), self.stored.get(k)) else {
                return Ok(None);
            };
            if Codec::from_flags(u32::from(b.flags_u16())) != Codec::None {
                return Ok(None);
            }
            if r.is_empty() {
                continue;
            }
            at = match at {
                None => Some(r.clone()),
                Some(a) if a.end == r.start => Some(a.start..r.end),
                Some(_) => return Ok(None),
            };
        }
        Ok(Some(at.map_or_else(Bytes::new, |r| self.source.slice(r))))
    }

    /// Publishes `bytes` as the run of blocks `first..end` (from joined
    /// offset `first_at`) and returns the bytes from block `first` on: a
    /// cached run that covers the span answers instead, and the runs the new
    /// one covers are dropped, after the lock. Its search and every run it
    /// drops are steps, each run counted as it is found. When the meter
    /// refuses them, the room for the runs it drops or the index's growth,
    /// the run is returned uncached and the index is unchanged, as a refused
    /// growth of the table left it. A `decoded` run a cached one answers for
    /// is dropped, its bytes counted as discarded ([`Self::counts`]). With
    /// what a slice of the bytes returned keeps alive ([`Self::pins`]).
    fn publish(
        &self,
        first: usize,
        end: usize,
        first_at: u64,
        bytes: Bytes,
        decoded: bool,
    ) -> (Bytes, u64) {
        let mut dropped: MeteredVec<Bytes> = MeteredVec::new(self.meter());
        let out = {
            let mut runs = self.runs.lock().unwrap_or_else(PoisonError::into_inner);
            let uncached = |bytes: Bytes| {
                let pinned = self.pins(&bytes);
                (bytes, pinned)
            };
            if self.steps(search_steps(runs.len())).is_err() {
                return uncached(bytes);
            }
            for _ in runs.inside(first, end) {
                if self.steps(1).is_err() {
                    return uncached(bytes);
                }
            }
            match runs.publish(first, end, &bytes, &mut dropped) {
                Published::Covered(f, b) => {
                    if decoded {
                        self.counts
                            .dropped(u64::try_from(bytes.len()).unwrap_or(u64::MAX));
                    }
                    (self.slice_from_block(f, &b, first_at), self.pins(&b))
                }
                Published::Cached | Published::Refused => uncached(bytes),
            }
        };
        drop(dropped);
        out
    }
}