1use crate::toc::Loc;
5use crate::{Rd, Result, bad, capacity, latin1, u32_len};
6
7const R: &str = "bundle region";
8const M: &str = "bundle manifest";
9pub const BUNDLE_SALT: u32 = 0x7065_636E;
11pub const BUNDLE_MAGIC: u32 = 0xED1C_EDB8;
13
14pub type FileRef = (Loc, u32, u32);
16
17pub fn read_region(r: &[u8]) -> Result<(Vec<FileRef>, Vec<u8>)> {
23 let rd = Rd::new(r, R);
24 let int = |i: usize| rd.i32be(crate::at(0, i, 4));
25 let (mo, ms, lo, total, dato, ro, co, total2) = (
26 int(0)?,
27 int(1)?,
28 rd.u32be(8)?,
29 int(3)?,
30 rd.u32be(16)?,
31 rd.u32be(20)?,
32 rd.u32be(24)?,
33 int(8)?,
34 );
35 let (Ok(mo), Ok(ms), Ok(total)) = (usize::try_from(mo), usize::try_from(ms), usize::try_from(total))
36 else {
37 return Err(bad(
38 R,
39 0,
40 "header is invalid: expected a metadata offset, metadata size and file count of 0 or more",
41 ));
42 };
43 if total2 < 0 || usize::try_from(total2) != Ok(total) || ro != dato || co != dato {
44 return Err(bad(
45 R,
46 0,
47 "header is invalid: expected the two file counts equal and the three data offsets equal",
48 ));
49 }
50 let inline = if ms > 0 {
51 rd.bytes(mo, ms)?.to_vec()
52 } else {
53 Vec::new()
54 };
55 let mut files = Vec::with_capacity(capacity(R, total, 9, r.len())?);
57 let mut pos = dato as usize;
58 let mut cur: Option<Loc> = None;
59 for i in 0..total {
60 let flag = rd.u8(crate::at(lo as usize, i, 1))?;
61 if flag == 1 {
62 let enc = rd.u32be(pos)?;
63 pos = pos.saturating_add(4);
64 cur = Some(Loc {
65 patch: enc & 0x00FF_0000 != 0,
66 chunk: (enc >> 8) & 0xFF,
67 archive: u16::from(enc.to_le_bytes()[0]),
68 });
69 } else if flag != 0 {
70 let id = rd.u64be(pos)?;
71 cur = Some(Loc::decode(id).ok_or_else(|| bad(R, pos, crate::toc::unrepresentable(id)))?);
72 pos = pos.saturating_add(8);
73 }
74 let loc = cur.ok_or_else(|| bad(R, pos, "first file has no archive identifier"))?;
75 files.push((loc, rd.u32be(pos)?, rd.u32be(pos.saturating_add(4))?));
76 pos = pos.saturating_add(8);
77 }
78 Ok((files, inline))
79}
80
81pub fn write_region(files: &[FileRef], inline: &[u8]) -> Result<Vec<u8>> {
85 let mut table = Vec::new();
86 let mut flags = Vec::with_capacity(files.len());
87 let mut prev = None;
88 for &(loc, off, size) in files {
89 if prev == Some(loc) {
90 flags.push(0u8);
91 } else {
92 flags.push(0x80);
93 table.extend(loc.encode().to_be_bytes());
94 prev = Some(loc);
95 }
96 table.extend(off.to_be_bytes());
97 table.extend(size.to_be_bytes());
98 }
99 if let Some(f) = flags.first_mut() {
100 *f |= 0x04;
101 }
102 let data_at = 0x24u32;
103 let too_large = || {
104 bad(
105 R,
106 0,
107 "the file table and inline manifest do not fit 32-bit offsets",
108 )
109 };
110 let table_end = u32_len(R, table.len())?
111 .checked_add(data_at)
112 .ok_or_else(too_large)?;
113 let inline_len = u32_len(R, inline.len())?;
114 let meta_at = if inline.is_empty() { 0 } else { table_end };
115 let loc_at = if inline.is_empty() {
116 table_end
117 } else {
118 table_end.checked_add(inline_len).ok_or_else(too_large)?
119 };
120 let n = u32_len(R, files.len())?;
121 let mut out = Vec::with_capacity(
122 table
123 .len()
124 .saturating_add(inline.len())
125 .saturating_add(flags.len())
126 .saturating_add(0x24),
127 );
128 for v in [meta_at, inline_len, loc_at, n, data_at, data_at, data_at, 0, n] {
129 out.extend(v.to_be_bytes());
130 }
131 out.extend(table);
132 out.extend(inline);
133 out.extend(flags);
134 Ok(out)
135}
136
137#[derive(Clone, Copy, Debug, PartialEq, Eq)]
139pub enum Kind {
140 Ebx,
142 Res,
144 Chunk,
146}
147
148#[derive(Clone, Debug, PartialEq, Eq)]
151pub struct Asset {
152 pub kind: Kind,
154 pub name: String,
156 pub sha1: [u8; 20],
158 pub size: u32,
161 pub rtype: u32,
163 pub rmeta: [u8; 16],
165 pub rid: u64,
167 pub guid: [u8; 16],
169 pub loff: u32,
173 pub lsize: u32,
176}
177
178impl Asset {
179 pub fn chunk_size(&self) -> u64 {
181 u64::from(self.loff).saturating_add(u64::from(self.lsize))
182 }
183
184 fn new(kind: Kind) -> Self {
185 Self {
186 kind,
187 name: String::new(),
188 sha1: [0; 20],
189 size: 0,
190 rtype: 0,
191 rmeta: [0; 16],
192 rid: 0,
193 guid: [0; 16],
194 loff: 0,
195 lsize: 0,
196 }
197 }
198}
199
200#[derive(Clone, Debug, PartialEq, Eq)]
202pub struct Manifest {
203 pub ebx: Vec<Asset>,
205 pub res: Vec<Asset>,
207 pub chunks: Vec<Asset>,
209 pub meta: Vec<u8>,
211 pub big_endian: bool,
213}
214
215impl Manifest {
216 pub fn count(&self) -> usize {
218 self.ebx
219 .len()
220 .saturating_add(self.res.len())
221 .saturating_add(self.chunks.len())
222 }
223 pub fn res_file(&self, i: usize) -> usize {
225 self.ebx.len().saturating_add(i).saturating_add(1)
226 }
227 pub fn chunk_file(&self, i: usize) -> usize {
229 self.ebx
230 .len()
231 .saturating_add(self.res.len())
232 .saturating_add(i)
233 .saturating_add(1)
234 }
235}
236
237#[derive(Clone, Debug, PartialEq, Eq)]
240pub struct BundleMetric {
241 pub resource_type: u32,
243 pub name: String,
245 pub value: u32,
247}
248
249#[derive(Clone, Debug, PartialEq, Eq)]
252pub enum ChunkMetaBody {
253 Empty,
255 FirstMip(i32),
257 Container {
259 container: String,
261 metrics: Vec<BundleMetric>,
263 },
264}
265
266#[derive(Clone, Debug, PartialEq, Eq)]
268pub struct ChunkMeta {
269 pub h64: u64,
271 pub meta: ChunkMetaBody,
273}
274
275fn meta_err(detail: impl Into<String>) -> crate::Error {
276 bad(M, 0, detail)
277}
278
279fn le_u32(n: &crate::db::Node, what: &str, ty: u8) -> Result<[u8; 4]> {
280 match (n.ty == ty, n.payload.as_slice()) {
281 (true, &[a, b, c, d]) => Ok([a, b, c, d]),
282 _ => Err(meta_err(format!(
283 "chunkMeta {what}: expected a 4-byte value of type {ty}"
284 ))),
285 }
286}
287
288pub fn read_chunk_meta(meta: &[u8]) -> Result<Vec<ChunkMeta>> {
292 use crate::db;
293 if meta.is_empty() {
294 return Ok(Vec::new());
295 }
296 let (root, end) = db::read_node(meta, 0)?;
297 if end != meta.len() || root.ty != 1 || root.name != "chunkMeta" {
298 return Err(meta_err(
299 "chunkMeta: expected one list named chunkMeta that fills the metadata",
300 ));
301 }
302 root.children
303 .iter()
304 .map(|row| {
305 let [h64, body] = row.children.as_slice() else {
306 return Err(meta_err("chunkMeta row: expected the children h64 and meta"));
307 };
308 if row.ty != 2 || h64.name != "h64" || body.name != "meta" || body.ty != 2 {
309 return Err(meta_err("chunkMeta row: expected an object of h64 and meta"));
310 }
311 let h64 = match (h64.ty, h64.payload.as_slice()) {
312 (9, p) => u64::from_le_bytes(
313 p.try_into()
314 .map_err(|_| meta_err("chunkMeta h64: expected 8 bytes"))?,
315 ),
316 _ => return Err(meta_err("chunkMeta h64: expected a value of type 9")),
317 };
318 let names: Vec<&str> = body.children.iter().map(|c| c.name.as_str()).collect();
319 let meta = match names.as_slice() {
320 [] => ChunkMetaBody::Empty,
321 ["firstMip"] => ChunkMetaBody::FirstMip(i32::from_le_bytes(le_u32(
322 body.children
323 .first()
324 .ok_or_else(|| meta_err("chunkMeta firstMip is missing"))?,
325 "firstMip",
326 8,
327 )?)),
328 ["container", "metrics"] => {
329 let [container, metrics] = body.children.as_slice() else {
330 return Err(meta_err("chunkMeta: expected container and metrics"));
331 };
332 if container.ty != 7 || metrics.ty != 1 {
333 return Err(meta_err("chunkMeta: container is a string and metrics a list"));
334 }
335 ChunkMetaBody::Container {
336 container: container.text(),
337 metrics: metrics.children.iter().map(metric).collect::<Result<_>>()?,
338 }
339 }
340 other => {
341 return Err(meta_err(format!(
342 "chunkMeta meta: children {other:?} are no known shape"
343 )));
344 }
345 };
346 Ok(ChunkMeta { h64, meta })
347 })
348 .collect()
349}
350
351fn metric(n: &crate::db::Node) -> Result<BundleMetric> {
352 let [kind, value] = n.children.as_slice() else {
353 return Err(meta_err("chunkMeta metric: expected resourceType and value"));
354 };
355 if n.ty != 2 || kind.name != "resourceType" || value.name != "value" || value.ty != 2 {
356 return Err(meta_err(
357 "chunkMeta metric: expected an object of resourceType and value",
358 ));
359 }
360 let [number] = value.children.as_slice() else {
361 return Err(meta_err("chunkMeta metric: expected one number in value"));
362 };
363 Ok(BundleMetric {
364 resource_type: u32::from_le_bytes(le_u32(kind, "resourceType", 8)?),
365 name: number.name.clone(),
366 value: u32::from_le_bytes(le_u32(number, "metric value", 8)?),
367 })
368}
369
370fn container(children: Vec<crate::db::Node>) -> crate::db::Node {
371 crate::db::Node {
372 ty: 2,
373 children,
374 terminated: true,
375 ..crate::db::Node::default()
376 }
377}
378
379fn leaf(name: &str, ty: u8, payload: Vec<u8>) -> crate::db::Node {
380 crate::db::Node {
381 ty,
382 named: true,
383 name: name.to_string(),
384 payload,
385 ..crate::db::Node::default()
386 }
387}
388
389pub fn write_chunk_meta(rows: &[ChunkMeta]) -> Result<Vec<u8>> {
393 use crate::db;
394 let named = |mut n: db::Node, name: &str| {
395 n.named = true;
396 n.name = name.to_string();
397 n
398 };
399 let children = rows
400 .iter()
401 .map(|r| {
402 let body = match &r.meta {
403 ChunkMetaBody::Empty => Vec::new(),
404 ChunkMetaBody::FirstMip(m) => vec![leaf("firstMip", 8, m.to_le_bytes().to_vec())],
405 ChunkMetaBody::Container {
406 container: c,
407 metrics,
408 } => {
409 let mut text = latin1(c, M)?;
410 text.push(0);
411 vec![
412 leaf("container", 7, text),
413 named(
414 db::Node {
415 ty: 1,
416 terminated: true,
417 children: metrics
418 .iter()
419 .map(|m| {
420 container(vec![
421 leaf("resourceType", 8, m.resource_type.to_le_bytes().to_vec()),
422 named(
423 container(vec![leaf(
424 &m.name,
425 8,
426 m.value.to_le_bytes().to_vec(),
427 )]),
428 "value",
429 ),
430 ])
431 })
432 .collect(),
433 ..db::Node::default()
434 },
435 "metrics",
436 ),
437 ]
438 }
439 };
440 Ok(container(vec![
441 leaf("h64", 9, r.h64.to_le_bytes().to_vec()),
442 named(container(body), "meta"),
443 ]))
444 })
445 .collect::<Result<Vec<_>>>()?;
446 let root = named(
447 db::Node {
448 ty: 1,
449 terminated: true,
450 children,
451 ..db::Node::default()
452 },
453 "chunkMeta",
454 );
455 let mut out = Vec::new();
456 db::write_node(&root, &mut out)?;
457 Ok(out)
458}
459
460impl Manifest {
461 pub fn chunk_meta(&self) -> Result<Vec<ChunkMeta>> {
463 read_chunk_meta(&self.meta)
464 }
465}
466
467pub fn is_manifest(m: &[u8]) -> bool {
469 let rd = Rd::new(m, M);
470 let (Ok(declared), Ok(be), Ok(le)) = (rd.u32be(0), rd.u32be(4), rd.u32le(4)) else {
471 return false;
472 };
473 m.len() >= 36
474 && usize::try_from(declared).ok().and_then(|d| d.checked_add(4)) == Some(m.len())
475 && (be ^ BUNDLE_SALT == BUNDLE_MAGIC || le ^ BUNDLE_SALT == BUNDLE_MAGIC)
476}
477
478pub fn read_manifest(m: &[u8]) -> Result<Manifest> {
481 let rd = Rd::new(m, M);
482 if m.len() < 36 {
483 return Err(bad(M, 0, "header is truncated"));
484 }
485 let declared = rd.u32be(0)?;
486 if usize::try_from(declared).ok().and_then(|d| d.checked_add(4)) != Some(m.len()) {
487 return Err(bad(
488 M,
489 0,
490 format!(
491 "declared size {declared} does not match: expected {}, the bytes after the size word",
492 m.len().saturating_sub(4)
493 ),
494 ));
495 }
496 let big_endian = rd.u32be(4)? ^ BUNDLE_SALT == BUNDLE_MAGIC;
497 if !big_endian && rd.u32le(4)? ^ BUNDLE_SALT != BUNDLE_MAGIC {
498 return Err(bad(M, 4, "magic is unsupported"));
499 }
500 let u32e = |at: usize| if big_endian { rd.u32be(at) } else { rd.u32le(at) };
501 let u64e = |at: usize| if big_endian { rd.u64be(at) } else { rd.u64le(at) };
502 let size = |at: usize| u32e(at).map(|v| v as usize);
503 let (total, ne, nr, nc) = (size(8)?, size(12)?, size(16)?, size(20)?);
504 let (strings, mo, ms) = (size(24)?.saturating_add(4), size(28)?, size(32)?);
505 if ne.checked_add(nr).and_then(|n| n.checked_add(nc)) != Some(total) {
506 return Err(bad(
507 M,
508 8,
509 format!(
510 "asset counts do not add up: {ne} EBX + {nr} resources + {nc} chunks, expected the total {total}"
511 ),
512 ));
513 }
514 let mut pos = 36usize;
515 let mut hashes = Vec::with_capacity(capacity(M, total, 20, m.len().saturating_sub(36))?);
516 for _ in 0..total {
517 hashes.push(rd.arr::<20>(pos)?);
518 pos = pos.saturating_add(20);
519 }
520 let mut b = Manifest {
521 ebx: Vec::new(),
522 res: Vec::new(),
523 chunks: Vec::new(),
524 meta: Vec::new(),
525 big_endian,
526 };
527 for (n, kind) in [(ne, Kind::Ebx), (nr, Kind::Res)] {
528 for _ in 0..n {
529 let mut a = Asset::new(kind);
530 a.name = rd.cstr(strings.saturating_add(size(pos)?))?;
531 a.size = u32e(pos.saturating_add(4))?;
532 pos = pos.saturating_add(8);
533 if kind == Kind::Ebx {
534 b.ebx.push(a)
535 } else {
536 b.res.push(a)
537 }
538 }
539 }
540 for (a, hash) in b.ebx.iter_mut().chain(b.res.iter_mut()).zip(&hashes) {
542 a.sha1 = *hash;
543 }
544 for a in &mut b.res {
545 a.rtype = u32e(pos)?;
546 pos = pos.saturating_add(4);
547 }
548 for a in &mut b.res {
549 a.rmeta = rd.arr(pos)?;
550 pos = pos.saturating_add(16);
551 }
552 for a in &mut b.res {
553 a.rid = u64e(pos)?;
554 pos = pos.saturating_add(8);
555 }
556 let chunk_hashes = hashes.iter().skip(ne.saturating_add(nr));
557 for hash in chunk_hashes.take(nc) {
558 let mut a = Asset::new(Kind::Chunk);
559 a.guid = rd.arr(pos)?;
560 if big_endian {
561 a.guid.reverse();
562 }
563 a.loff = u32e(pos.saturating_add(16))?;
564 a.lsize = u32e(pos.saturating_add(20))?;
565 pos = pos.saturating_add(24);
566 a.sha1 = *hash;
567 a.size = a.lsize;
568 b.chunks.push(a);
569 }
570 if ms > 0 {
571 b.meta = rd.bytes(mo.saturating_add(4), ms)?.to_vec();
572 }
573 Ok(b)
574}
575
576pub fn write_manifest(b: &Manifest) -> Result<Vec<u8>> {
580 if b.big_endian {
581 return Err(bad(M, 0, "only little-endian manifests are written"));
582 }
583 let total = b.count();
584 let mut strings = Vec::new();
585 let mut offsets = Vec::with_capacity(b.ebx.len().saturating_add(b.res.len()));
586 for a in b.ebx.iter().chain(&b.res) {
587 offsets.push(u32_len(M, strings.len())?);
588 strings.extend(latin1(&a.name, M)?);
589 strings.push(0);
590 }
591 let assets_with_names = b.ebx.len().saturating_add(b.res.len());
593 let string_at = 36usize
594 .saturating_add(crate::at(0, total, 20))
595 .saturating_add(crate::at(0, assets_with_names, 8))
596 .saturating_add(crate::at(0, b.res.len(), 28))
597 .saturating_add(crate::at(0, b.chunks.len(), 24));
598 let meta_at = string_at.saturating_add(strings.len());
599 let len = meta_at.saturating_add(b.meta.len());
600 let after_size = |at: usize| u32_len(M, at.saturating_sub(4));
602 let mut out = Vec::with_capacity(len);
603 out.extend(after_size(len)?.to_be_bytes());
604 out.extend((BUNDLE_MAGIC ^ BUNDLE_SALT).to_le_bytes());
605 let meta_field = if b.meta.is_empty() {
606 0
607 } else {
608 after_size(meta_at)?
609 };
610 for v in [
611 u32_len(M, total)?,
612 u32_len(M, b.ebx.len())?,
613 u32_len(M, b.res.len())?,
614 u32_len(M, b.chunks.len())?,
615 after_size(string_at)?,
616 meta_field,
617 u32_len(M, b.meta.len())?,
618 ] {
619 out.extend(v.to_le_bytes());
620 }
621 for a in b.ebx.iter().chain(&b.res).chain(&b.chunks) {
622 out.extend(a.sha1);
623 }
624 for (a, off) in b.ebx.iter().chain(&b.res).zip(&offsets) {
625 out.extend(off.to_le_bytes());
626 out.extend(a.size.to_le_bytes());
627 }
628 for a in &b.res {
629 out.extend(a.rtype.to_le_bytes());
630 }
631 for a in &b.res {
632 out.extend(a.rmeta);
633 }
634 for a in &b.res {
635 out.extend(a.rid.to_le_bytes());
636 }
637 for a in &b.chunks {
638 out.extend(a.guid);
639 out.extend(a.loff.to_le_bytes());
640 out.extend(a.lsize.to_le_bytes());
641 }
642 out.extend(strings);
643 out.extend(&b.meta);
644 debug_assert_eq!(out.len(), len);
645 Ok(out)
646}