decodeVarint static method
Decodes a Bitcoin-style varint at offset.
Implementation
static LayoutDecodeResult<BigInt> decodeVarint(
List<int> bytes, {
int offset = 0,
}) {
_requireAvailable(bytes, offset, 1);
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;
}
_requireAvailable(bytes, offset + 1, size);
final BigInt value = BigintUtils.fromBytes(
bytes,
offset: offset + 1,
length: size,
byteOrder: Endian.little,
);
if (!value.isSafeInt()) {
throw BuError(
DefaultErrorType.invalidLength,
"Failed to decode varint integer. Value is too large.",
);
}
return LayoutDecodeResult(consumed: size + 1, value: value);
}