parse static method

DocView parse(
  1. Uint8List buffer, [
  2. int offset = 0,
  3. int? length
])

Parses and validates the document starting at offset.

length is the total document size including any checksum trailer; when omitted the document is parsed out of buffer starting at offset. Checksums are read but not verified; call verifyChecksum or use parseVerified to pay for the extra pass.

Implementation

static DocView parse(Uint8List buffer, [int offset = 0, int? length]) {
  final end = length == null ? buffer.length : offset + length;
  if (end > buffer.length || offset < 0 || end < offset) {
    throw VxsFormatException('document range out of bounds', offset: offset);
  }
  if (end - offset < 4) {
    throw VxsFormatException(
      'document shorter than the 4-byte preamble',
      offset: offset,
    );
  }
  if (buffer[offset] != vxsMagic) {
    throw VxsFormatException(
      'bad magic 0x${buffer[offset].toRadixString(16)}',
      offset: offset,
    );
  }
  final flags = buffer[offset + 1];
  if (flags & vxsReservedFlags != 0) {
    throw VxsFormatException(
      'reserved flag bits set (0x${flags.toRadixString(16)})',
      offset: offset,
    );
  }
  final schemaId = buffer[offset + 2];
  final schemaVersion = buffer[offset + 3];
  final large = flags & vxsFlagLength32 != 0;
  final headerSize = large ? vxsHeaderSizeLarge : vxsHeaderSize;
  if (end - offset < headerSize) {
    throw VxsFormatException(
      'document shorter than the $headerSize-byte header',
      offset: offset,
    );
  }
  final bodyLength = large
      ? VxsLe.u32(buffer, offset + 4)
      : VxsLe.u16(buffer, offset + 4);
  final bodyStart = offset + headerSize;
  final bodyEnd = bodyStart + bodyLength;
  if (bodyEnd > end) {
    throw VxsFormatException(
      'body length $bodyLength exceeds the available ${end - bodyStart} bytes',
      offset: offset,
    );
  }
  if (flags & vxsFlagCrc32c != 0) {
    if (bodyEnd + 4 > end) {
      throw VxsFormatException('missing checksum trailer', offset: bodyEnd);
    }
    final stored = VxsLe.u32(buffer, bodyEnd);
    return DocView._(
      buffer,
      offset,
      bodyStart,
      bodyEnd,
      bodyEnd + 4,
      flags,
      schemaId,
      schemaVersion,
      stored,
    );
  }
  return DocView._(
    buffer,
    offset,
    bodyStart,
    bodyEnd,
    bodyEnd,
    flags,
    schemaId,
    schemaVersion,
    null,
  );
}