readPackIndex function
Reads and checks the header and index of a pack.
Implementation
PackIndex readPackIndex(Uint8List pack, PackKeys keys) {
if (pack.length < owpHeaderLength ||
pack[0] != owpMagic[0] || pack[1] != owpMagic[1] ||
pack[2] != owpMagic[2] || pack[3] != owpMagic[3]) {
throw AssetPackException('Not an openworld asset pack');
}
final nonce = Uint8List.sublistView(pack, 4, 16);
final len = pack.buffer.asByteData(pack.offsetInBytes).getUint32(16, Endian.little);
final mac = Uint8List.sublistView(pack, 20, 52);
if (owpHeaderLength + len > pack.length) {
throw AssetPackException('Pack is truncated');
}
final cipher = Uint8List.sublistView(pack, owpHeaderLength, owpHeaderLength + len);
if (!macEquals(mac, indexMac(keys.macKey, nonce, len, cipher))) {
throw AssetPackException('Pack index failed authentication (wrong key or damaged file)');
}
final json = jsonDecode(utf8.decode(chacha20(keys.encKey, nonce, cipher))) as List;
return PackIndex(
[for (final e in json) PackEntry.fromJson(e as Map<String, dynamic>)],
owpHeaderLength + len,
);
}