UNPKG

runelib

Version:

runestones encode and decode

44 lines (43 loc) 1.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.decodeLEB128 = exports.encodeLEB128 = void 0; function encodeLEB128(value) { const bytes = []; let more = true; while (more) { let byte = Number(value & BigInt(0x7F)); // Get the lowest 7 bits value >>= BigInt(7); if (value === BigInt(0)) { // No more data to encode more = false; } else { // More bytes to come byte |= 0x80; // Set the continuation bit } bytes.push(byte); } // Convert array to Buffer return bytes; } exports.encodeLEB128 = encodeLEB128; function decodeLEB128(buf) { let n = BigInt(0); for (let i = 0; i < buf.length; i++) { const byte = BigInt(buf[i]); if (i > 18) { throw new Error("Overlong"); } let value = byte & BigInt(127); if ((i == 18) && ((value & BigInt(124)) != BigInt(0))) { throw new Error("Overflow"); } n |= value << (BigInt(7) * BigInt(i)); if ((byte & BigInt(128)) == BigInt(0)) { return { n, len: i + 1 }; } } throw new Error("Unterminated"); } exports.decodeLEB128 = decodeLEB128;