decode method

  1. @override
BigInt decode(
  1. Input input
)
override

Implementation

@override
BigInt decode(Input input) {
  var prefix = input.read();
  switch (prefix % 4) {
    case 0:
      {
        return BigInt.from(prefix >> 2);
      }
    case 1:
      {
        prefix >>= 2;
        prefix |= input.read() << 6;
        assertion(prefix > 0x3f && prefix <= 0x3fff, 'Out of range');
        return BigInt.from(prefix);
      }
    case 2:
      {
        prefix >>= 2;
        prefix |=
            (input.read() << 6) | (input.read() << 14) | (input.read() << 22);
        assertion(prefix > 0x3fff && prefix <= 0x3fffffff, 'Out of range');
        return BigInt.from(prefix);
      }
    default:
      {
        final bytesNeeded = (prefix >> 2) + 4;
        var value = BigInt.zero;
        for (var i = 0; i < bytesNeeded; i++) {
          value |= BigInt.from(input.read()) << (8 * i);
        }
        assertion(value > BigInt.from(0x3fffffff), 'Out of range');
        return value;
      }
  }
}