convert method

  1. @override
Uint8List convert(
  1. covariant List<int> input
)
override

Converts input array of numbers with bit-length of source to an array of numbers with bit-length of target. The input array will be treated as a sequence of bits to convert.

After consuming all of input sequence, if there are some non-zero partial word remains, 0 will be padded on the right to make the final word.

Implementation

@override
Uint8List convert(List<int> input) {
  const tailChars = [0, 2, 4, 5, 7];
  final table = alphabet;
  final pad = padding;
  int n = input.length;
  int full = n ~/ 5;
  int rem = n - full * 5;

  int outLen;
  if (pad != null) {
    outLen = (full + (rem == 0 ? 0 : 1)) << 3;
  } else {
    outLen = (full << 3) + tailChars[rem];
  }
  var out = Uint8List(outLen);

  int i = 0, j = 0, b0, b1, b2, b3, b4;
  for (int g = 0; g < full; ++g) {
    b0 = input[i++] & 0xFF;
    b1 = input[i++] & 0xFF;
    b2 = input[i++] & 0xFF;
    b3 = input[i++] & 0xFF;
    b4 = input[i++] & 0xFF;
    out[j++] = table[b0 >> 3];
    out[j++] = table[((b0 & 0x7) << 2) | (b1 >> 6)];
    out[j++] = table[(b1 >> 1) & 0x1F];
    out[j++] = table[((b1 & 0x1) << 4) | (b2 >> 4)];
    out[j++] = table[((b2 & 0xF) << 1) | (b3 >> 7)];
    out[j++] = table[(b3 >> 2) & 0x1F];
    out[j++] = table[((b3 & 0x3) << 3) | (b4 >> 5)];
    out[j++] = table[b4 & 0x1F];
  }

  if (rem > 0) {
    b0 = input[i] & 0xFF;
    b1 = rem > 1 ? input[i + 1] & 0xFF : 0;
    b2 = rem > 2 ? input[i + 2] & 0xFF : 0;
    b3 = rem > 3 ? input[i + 3] & 0xFF : 0;
    out[j++] = table[b0 >> 3];
    out[j++] = table[((b0 & 0x7) << 2) | (b1 >> 6)];
    if (rem >= 2) {
      out[j++] = table[(b1 >> 1) & 0x1F];
      out[j++] = table[((b1 & 0x1) << 4) | (b2 >> 4)];
    }
    if (rem >= 3) {
      out[j++] = table[((b2 & 0xF) << 1) | (b3 >> 7)];
    }
    if (rem >= 4) {
      out[j++] = table[(b3 >> 2) & 0x1F];
      out[j++] = table[(b3 & 0x3) << 3];
    }
    if (pad != null) {
      while (j < outLen) {
        out[j++] = pad;
      }
    }
  }

  return out;
}