slebDecode function

BigInt slebDecode(
  1. BufferPipe pipe
)

Decode a leb encoded buffer into a bigint. The number is decoded with support for negative signed-leb encoding. @param pipe A Buffer containing the signed leb encoded bits.

Implementation

BigInt slebDecode(BufferPipe pipe) {
  // Get the size of the buffer, then cut a buffer of that size.
  final pipeView = Uint8List.fromList(pipe.buffer as List<int>);
  var len = 0;
  for (; len < pipeView.lengthInBytes; len++) {
    if (pipeView[len] < 0x80) {
      // If it's a positive number, we reuse lebDecode.
      if ((pipeView[len] & 0x40) == 0) {
        return lebDecode(pipe);
      }
      break;
    }
  }

  final bytes = Uint8List.fromList(safeRead(pipe as BufferPipe<int>, len + 1));
  var value = BigInt.zero;
  for (var i = bytes.lengthInBytes - 1; i >= 0; i--) {
    value =
        value * BigInt.from(0x80) + BigInt.from(0x80 - (bytes[i] & 0x7f) - 1);
  }
  return -value - BigInt.one;
}