convert method

  1. @override
Uint8List convert(
  1. String input
)
override

Decodes the specified data. If encoding is ascii85, the data is expected to start with <~ and and end with ~>. No checks are actually made for this, but output will be unexpected if this is not the case.

The input to decode. May be a String. If ascii85, it is expected to be enclosed in <~ and ~>.

Implementation

@override

/// Decodes the specified data. If encoding is ascii85, the data is
/// expected to start with <~ and and end with ~>.
/// No checks are actually made for this,
/// but output will be unexpected if this is not the case.
///
/// The [input] to decode. May be a String.
/// If ascii85, it is expected to be enclosed in <~ and ~>.
Uint8List convert(String input) {
  if (input.isEmpty) {
    return Uint8List(0);
  }
  var bytes = Uint8List.fromList(input.codeUnits);
  var dataLength = bytes.length;
  if (algo == AlgoType.ascii85) {
    dataLength -= (ASCII85_ENC_START.length + ASCII85_ENC_END.length);
  }

  if (algo == AlgoType.z85 && dataLength % 5 != 0) {
    throw FormatException('Wrong length');
  }

  var padding = (dataLength % 5 == 0) ? 0 : 5 - dataLength % 5;

  var bufferStart = (algo == AlgoType.ascii85) ? ASCII85_ENC_START.length : 0;
  var bufferEnd = bufferStart + dataLength;

  var result = Uint8List(4 * ((bufferEnd - bufferStart) / 5).ceil());

  var nextValidByte = (index) {
    if (index < bufferEnd) {
      while (_IGNORE_CHARS.contains(bytes[index])) {
        padding = (padding + 1) % 5;
        index++; // skip newline character
      }
    }
    return index;
  };

  var writeIndex = 0;
  for (var i = bufferStart; i < bufferEnd;) {
    var num = 0;
    var starti = i;

    i = nextValidByte(i);
    num = (_baseMap[bytes[i]]) * (QUAD85 as int);

    i = nextValidByte(i + 1);
    num += (i >= bufferEnd ? 84 : _baseMap[bytes[i]]) * (TRIO85 as int);

    i = nextValidByte(i + 1);
    num += (i >= bufferEnd ? 84 : _baseMap[bytes[i]]) * (DUO85 as int);

    i = nextValidByte(i + 1);
    num += (i >= bufferEnd ? 84 : _baseMap[bytes[i]]) * SING85;

    i = nextValidByte(i + 1);
    num += (i >= bufferEnd ? 84 : _baseMap[bytes[i]]);

    i = nextValidByte(i + 1);

    if (algo == AlgoType.z85 && starti + 5 != i) {
      throw FormatException('Wrong length');
    }

    if (num > NUM_MAX_VALUE || num < 0) {
      throw FormatException('Bogus data');
    }
    result[writeIndex] = (num >> 24);
    result[writeIndex + 1] = (num >> 16);
    result[writeIndex + 2] = (num >> 8);
    result[writeIndex + 3] = (num & 0xff);
    writeIndex += 4;
  }

  return result.sublist(0, writeIndex - padding);
}