2025-02-28 23:32:54 +03:00
|
|
|
export function decodeVarint(buffer, offset) {
|
2025-03-17 13:37:12 +03:00
|
|
|
let res = this.BigInt(0);
|
2025-02-28 23:32:54 +03:00
|
|
|
let shift = 0;
|
|
|
|
let byte = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (offset >= buffer.length) {
|
|
|
|
throw new RangeError("Index out of bound decoding varint");
|
|
|
|
}
|
|
|
|
|
|
|
|
byte = buffer[offset++];
|
|
|
|
|
2025-03-17 13:37:12 +03:00
|
|
|
const multiplier = this.BigInt(2) ** this.BigInt(shift);
|
|
|
|
const thisByteValue = this.BigInt(byte & 0x7f) * multiplier;
|
2025-02-28 23:32:54 +03:00
|
|
|
shift += 7;
|
2025-03-17 13:37:12 +03:00
|
|
|
res = res + thisByteValue;
|
2025-02-28 23:32:54 +03:00
|
|
|
} while (byte >= 0x80);
|
|
|
|
|
|
|
|
return {
|
|
|
|
value: res,
|
|
|
|
length: shift / 7
|
|
|
|
};
|
|
|
|
}
|