getVariableEncInt method

Tuple2<BigInt, int> getVariableEncInt(
  1. int startOffset
)

Lê um inteiro length‑encoded a partir de startOffset.

O formato length‑encoded é definido pelo primeiro byte:

  • Se for < 0xfb, esse próprio byte é o valor.
  • Se for 0xfc, lê 2 bytes (uint16).
  • Se for 0xfd, lê 3 bytes (uint24).
  • Se for 0xfe, lê 8 bytes (uint64).

Retorna uma Tuple2:

  • item1: valor lido como BigInt.
  • item2: número total de bytes consumidos (1 para o header, +2/+3/+8, dependendo do caso).

Implementation

Tuple2<BigInt, int> getVariableEncInt(int startOffset) {
  final firstByte = getUint8(startOffset);

  if (firstByte < 0xfb) {
    // Valor direto (ex.: 0x05).
    return Tuple2(BigInt.from(firstByte), 1);
  }

  if (firstByte == 0xfc) {
    // Próximos 2 bytes.
    final radix = getUint8(startOffset + 2).toRadixString(16).padLeft(2, '0')
        + getUint8(startOffset + 1).toRadixString(16).padLeft(2, '0');
    return Tuple2(BigInt.parse(radix, radix: 16), 3);
  }

  if (firstByte == 0xfd) {
    // Próximos 3 bytes.
    final radix = getUint8(startOffset + 3).toRadixString(16).padLeft(2, '0')
        + getUint8(startOffset + 2).toRadixString(16).padLeft(2, '0')
        + getUint8(startOffset + 1).toRadixString(16).padLeft(2, '0');
    return Tuple2(BigInt.parse(radix, radix: 16), 4);
  }

  if (firstByte == 0xfe) {
    // Próximos 8 bytes.
    final radix = getUint8(startOffset + 8).toRadixString(16).padLeft(2, '0')
        + getUint8(startOffset + 7).toRadixString(16).padLeft(2, '0')
        + getUint8(startOffset + 6).toRadixString(16).padLeft(2, '0')
        + getUint8(startOffset + 5).toRadixString(16).padLeft(2, '0')
        + getUint8(startOffset + 4).toRadixString(16).padLeft(2, '0')
        + getUint8(startOffset + 3).toRadixString(16).padLeft(2, '0')
        + getUint8(startOffset + 2).toRadixString(16).padLeft(2, '0')
        + getUint8(startOffset + 1).toRadixString(16).padLeft(2, '0');
    return Tuple2(BigInt.parse(radix, radix: 16), 9);
  }

  throw MySQLProtocolException(
    "Wrong first byte, while decoding getVariableEncInt",
  );
}