decode static method

Manifest decode(
  1. Uint8List bytes
)

Parses a manifest. Throws VxsFormatException when invalid.

Implementation

static Manifest decode(Uint8List bytes) {
  if (bytes.length < 16) {
    throw const VxsFormatException('manifest too short');
  }
  if (bytes[0] != manifestMagic || bytes[1] != manifestVersion) {
    throw const VxsFormatException('bad manifest header');
  }
  final generation = VxsLe.u32(bytes, 4);
  final count = VxsLe.u32(bytes, 8);
  final bodyEnd = bytes.length - 4;
  if (VxsLe.u32(bytes, bodyEnd) != VxsCrc32c.compute(bytes, 0, bodyEnd)) {
    throw const VxsFormatException('manifest checksum mismatch');
  }
  var pos = 12;
  final segments = <SegmentInfo>[];
  for (var i = 0; i < count; i++) {
    if (pos + 2 > bodyEnd) {
      throw const VxsFormatException('truncated manifest entry');
    }
    final nameLength = VxsLe.u16(bytes, pos);
    pos += 2;
    if (pos + nameLength + 8 > bodyEnd) {
      throw const VxsFormatException('truncated manifest entry');
    }
    final name = String.fromCharCodes(bytes, pos, pos + nameLength);
    pos += nameLength;
    final entryCount = VxsLe.u32(bytes, pos);
    pos += 4;
    final byteLength = VxsLe.u32(bytes, pos);
    pos += 4;
    segments.add(
      SegmentInfo(name: name, entryCount: entryCount, byteLength: byteLength),
    );
  }
  if (pos != bodyEnd) {
    throw const VxsFormatException('trailing bytes in manifest');
  }
  return Manifest(generation: generation, segments: segments);
}