packages/leb128/src/leb.js
function decodeBufferCommon(encodedBuffer, index, signed) {
  index = index === undefined ? 0 : index;

  let length = encodedLength(encodedBuffer, index);
  const bitLength = length * 7;
  let byteLength = Math.ceil(bitLength / 8);
  let result = bufs.alloc(byteLength);
  let outIndex = 0;

  while (length > 0) {
    bits.inject(result, outIndex, 7, encodedBuffer[index]);
    outIndex += 7;
    index++;
    length--;
  }

  let signBit;
  let signByte;

  if (signed) {
    // Sign-extend the last byte.
    let lastByte = result[byteLength - 1];
    const endBit = outIndex % 8;
    if (endBit !== 0) {
      const shift = 32 - endBit; // 32 because JS bit ops work on 32-bit ints.
      lastByte = result[byteLength - 1] = ((lastByte << shift) >> shift) & 0xff;
    }
    signBit = lastByte >> 7;
    signByte = signBit * 0xff;
  } else {
    signBit = 0;
    signByte = 0;
  }

  // Slice off any superfluous bytes, that is, ones that add no meaningful
  // bits (because the value would be the same if they were removed).
  while (
    byteLength > 1 &&
    result[byteLength - 1] === signByte &&
    (!signed || result[byteLength - 2] >> 7 === signBit)
  ) {
    byteLength--;
  }
  result = bufs.resize(result, byteLength);

  return { value: result, nextIndex: index };
}
function decodeBufferCommon(encodedBuffer, index, signed) {
  index = index === undefined ? 0 : index;

  let length = encodedLength(encodedBuffer, index);
  const bitLength = length * 7;
  let byteLength = Math.ceil(bitLength / 8);
  let result = bufs.alloc(byteLength);
  let outIndex = 0;

  while (length > 0) {
    bits.inject(result, outIndex, 7, encodedBuffer[index]);
    outIndex += 7;
    index++;
    length--;
  }

  let signBit;
  let signByte;

  if (signed) {
    // Sign-extend the last byte.
    let lastByte = result[byteLength - 1];
    const endBit = outIndex % 8;
    if (endBit !== 0) {
      const shift = 32 - endBit; // 32 because JS bit ops work on 32-bit ints.
      lastByte = result[byteLength - 1] = ((lastByte << shift) >> shift) & 0xff;
    }
    signBit = lastByte >> 7;
    signByte = signBit * 0xff;
  } else {
    signBit = 0;
    signByte = 0;
  }

  // Slice off any superfluous bytes, that is, ones that add no meaningful
  // bits (because the value would be the same if they were removed).
  while (
    byteLength > 1 &&
    result[byteLength - 1] === signByte &&
    (!signed || result[byteLength - 2] >> 7 === signBit)
  ) {
    byteLength--;
  }
  result = bufs.resize(result, byteLength);

  return { value: result, nextIndex: index };
}
function decodeBufferCommon ( encodedBuffer , index , signed ) { index = index === undefined ? 0 : index ; let length = encodedLength ( encodedBuffer , index ) ; const bitLength = length * 7 ; let byteLength = Math . ceil ( bitLength / 8 ) ; let result = bufs . alloc ( byteLength ) ; let outIndex = 0 ; while ( length > 0 ) { bits . inject ( result , outIndex , 7 , encodedBuffer [ index ] ) ; outIndex += 7 ; index ++ ; length -- ; } let signBit ; let signByte ; if ( signed ) { // Sign-extend the last byte. let lastByte = result [ byteLength - 1 ] ; const endBit = outIndex % 8 ; if ( endBit !== 0 ) { const shift = 32 - endBit ; // 32 because JS bit ops work on 32-bit ints. lastByte = result [ byteLength - 1 ] = ( ( lastByte << shift ) >> shift ) & 0xff ; } signBit = lastByte >> 7 ; signByte = signBit * 0xff ; } else { signBit = 0 ; signByte = 0 ; } // Slice off any superfluous bytes, that is, ones that add no meaningful // bits (because the value would be the same if they were removed). while ( byteLength > 1 && result [ byteLength - 1 ] === signByte && ( ! signed || result [ byteLength - 2 ] >> 7 === signBit ) ) { byteLength -- ; } result = bufs . resize ( result , byteLength ) ; return { value : result , nextIndex : index } ; }
Common decoder for both signed and unsigned ints. This takes an
LEB128-encoded buffer, returning a bigint-ish buffer.
Common decoder for both signed and unsigned ints . This takes an LEB128 - encoded buffer returning a bigint - ish buffer .