next method

bool next()

Advances to the next field. Returns false at the end of the document.

Implementation

bool next() {
  if (_pos >= _end) return false;
  _entryStart = _pos;
  final key = _bytes[_pos++];
  _wire = key & 0x03;
  final code = key >> 2;
  final delta = code == 0 ? vxsExtendedDeltaBase + _readVarint() : code - 1;
  // The key may include an extended delta varint; the raw value starts after
  // *all* of it, otherwise a verbatim copy would swallow those bytes.
  _valueRawStart = _pos;
  if (delta <= 0) {
    throw VxsFormatException('field ids must ascend', offset: _entryStart);
  }
  _fieldId += delta;
  if (_fieldId > 0xFFFF) {
    throw VxsFormatException('field id overflow', offset: _entryStart);
  }
  switch (_wire) {
    case VxsWire.varint:
      _valueStart = _pos;
      _intValue = _readVarint();
      _valueLength = _pos - _valueStart;
    case VxsWire.fixed32:
      _valueStart = _pos;
      _valueLength = 4;
      _pos += 4;
      if (_pos > _end) {
        throw VxsFormatException('truncated fixed32', offset: _valueStart);
      }
    case VxsWire.fixed64:
      _valueStart = _pos;
      _valueLength = 8;
      _pos += 8;
      if (_pos > _end) {
        throw VxsFormatException('truncated fixed64', offset: _valueStart);
      }
    default:
      final length = _readVarint();
      // Check before adding: a crafted varint can overflow `_pos + length`
      // on a 64-bit VM and slip past the bound check below.
      if (length < 0 || length > _end - _pos) {
        throw VxsFormatException(
          'length-delimited value of $length bytes overruns the document',
          offset: _pos,
        );
      }
      _valueStart = _pos;
      _valueLength = length;
      _pos += length;
  }
  return true;
}