SqliteHeader.parse constructor

SqliteHeader.parse(
  1. Uint8List bytes
)

Parse the 100-byte header. Throws FormatException on bad magic or unsupported text encoding.

Implementation

factory SqliteHeader.parse(Uint8List bytes) {
  if (bytes.length < 100) {
    throw FormatException('SQLite file too small (${bytes.length} bytes)');
  }
  for (var i = 0; i < magic.length; i++) {
    if (bytes[i] != magic[i]) {
      throw FormatException('Not a SQLite database (bad magic)');
    }
  }
  final bd = ByteData.sublistView(bytes, 0, 100);
  var pageSize = bd.getUint16(16);
  if (pageSize == 1) pageSize = 65536;
  final encoding = bd.getUint32(56);
  if (encoding != 1) {
    throw FormatException(
        'Unsupported text encoding $encoding (only UTF-8 is supported)');
  }
  return SqliteHeader(
    pageSize: pageSize,
    fileFormatWrite: bd.getUint8(18),
    fileFormatRead: bd.getUint8(19),
    reservedSpace: bd.getUint8(20),
    textEncoding: encoding,
    schemaCookie: bd.getUint32(40),
    schemaFormat: bd.getUint32(44),
    userVersion: bd.getUint32(60),
    applicationId: bd.getUint32(68),
    dbSizeInPages: bd.getUint32(28),
  );
}