base32Decode function

Uint8List base32Decode(
  1. String input
)

Implementation

Uint8List base32Decode(String input) {
  // how many bits we have from the previous character.
  int skip = 0;
  // current byte we're producing.
  int byte = 0;

  final output = Uint8List(((input.length * 4) / 3).ceil() | 0);
  int o = 0;

  final Map<String, int> lookupTable = _alphabet.split('').fold({}, (p, e) {
    p[e] = _alphabet.indexOf(e);
    return p;
  });
  // Add aliases for rfc4648.
  lookupTable
    ..putIfAbsent('0', () => lookupTable['o']!)
    ..putIfAbsent('1', () => lookupTable['i']!);

  void decodeChar(String char) {
    // Consume a character from the stream, store
    // the output in this.output. As before, better
    // to use update().
    final found = lookupTable[char.toLowerCase()];
    assert(found != null, 'Invalid character: $char');
    int val = found!;
    // move to the high bits
    val <<= 3;
    // 32 bit shift?
    byte |= (val & 0xffffffff) >> skip;

    skip += 5;

    if (skip >= 8) {
      // We have enough bytes to produce an output
      output[o++] = byte;
      skip -= 8;

      if (skip > 0) {
        byte = (val << (5 - skip)) & 255;
      } else {
        byte = 0;
      }
    }
  }

  for (int i = 0; i < input.length; i += 1) {
    decodeChar(input[i]);
  }
  return output.sublist(0, o);
}