genericDataRange function

(int, int)? genericDataRange(
  1. Uint8List header,
  2. String name, {
  3. required int headerEnd,
  4. int maxBytes = maxGenericDataBytes,
})

The [begin, end) byte range of the GenericBinaryData section named name, from the header flatbuffer that follows the 32-byte preamble (the file's header ends at headerEnd), or null when the bundle has none. A section that carries the name in another form — on a section of another data type, or as a value that is not a string — is a FormatException, not "none": it is data this release cannot read.

Implementation

(int, int)? genericDataRange(
  Uint8List header,
  String name, {
  required int headerEnd,
  int maxBytes = maxGenericDataBytes,
}) {
  try {
    final fb = LitertlmFlatBuffer(header);
    // LiteRTLMMetaData.section_metadata = 1; SectionMetadata.objects = 0.
    final sectionMetadata = fb.table(fb.root(), 1);
    if (sectionMetadata == null) return null;
    final objects = fb.vector(sectionMetadata, 0);
    if (objects == null) return null;
    (int, int)? found;
    for (var i = 0; i < fb.length(objects); i++) {
      final section = fb.element(objects, i);
      // SectionObject: items = 0, begin_offset = 1, end_offset = 2,
      // data_type = 3.
      if (!_named(fb, section, name)) continue;
      final type = fb.uint8(section, 3);
      if (type != _genericBinaryData) {
        throw FormatException(
          'the $name section has data type $type, not '
          'GenericBinaryData ($_genericBinaryData)',
        );
      }
      if (found != null) {
        throw FormatException(
          'the .litertlm file carries the $name section twice',
        );
      }
      final begin = fb.uint64(section, 1);
      final end = fb.uint64(section, 2);
      // A uint64 at or above 2^63 reads as negative.
      if (begin < headerEnd || end < begin || end - begin > maxBytes) {
        throw FormatException('the $name section $begin..$end is invalid');
      }
      found = (begin, end);
    }
    return found;
  } on RangeError {
    throw const FormatException('.litertlm header flatbuffer is truncated');
  }
}