decodeVarint static method

LayoutDecodeResult<BigInt> decodeVarint(
  1. List<int> bytes, {
  2. int offset = 0,
})

Implementation

static LayoutDecodeResult<BigInt> decodeVarint(
  List<int> bytes, {
  int offset = 0,
}) {
  final int ni = bytes[offset];
  int size = 0;

  if (ni < 253) {
    return LayoutDecodeResult(consumed: 1, value: BigInt.from(ni));
  }

  if (ni == 253) {
    size = 2;
  } else if (ni == 254) {
    size = 4;
  } else {
    size = 8;
  }

  final BigInt value = fromBytes(
    bytes: bytes,
    offset: offset + 1,
    end: offset + 1 + size,
    byteOrder: Endian.little,
  );
  if (!value.isValidInt) {
    throw LayoutException(
      "Failed to decode varint integer. values is to large.",
    );
  }
  return LayoutDecodeResult(consumed: size + 1, value: value);
}