inflate function

Uint8List? inflate(
  1. Uint8List data
)

A raw DEFLATE stream, with no zlib wrapper around it — zlibInflate's own worker, public because a caller that already has the wrapper stripped (or none at all, as in a raw .deflate stream) should not have to fake one back on to call this.

Returns null on anything this reader cannot make sense of: a reserved block type, a stored block whose length and its own complement disagree, a Huffman code with no match in fifteen bits, a back-reference reaching before the start of the output. Every one of those is a malformed or truncated stream, and this makes the same choice zlibInflate does about what a malformed stream is worth answering.

Implementation

Uint8List? inflate(Uint8List data) {
  final reader = _BitReader(data);
  final out = _ByteSink();
  try {
    while (true) {
      final isFinal = reader.readBits(1) == 1;
      final type = reader.readBits(2);
      switch (type) {
        case 0:
          if (!_stored(reader, out)) return null;
        case 1:
          if (!_huffmanBlock(reader, out, _fixedLiteral, _fixedDistance)) {
            return null;
          }
        case 2:
          final tables = _dynamicTables(reader);
          if (tables == null) return null;
          if (!_huffmanBlock(reader, out, tables.$1, tables.$2)) return null;
        default:
          return null; // type 3 is reserved
      }
      if (isFinal) break;
    }
  } on _Truncated {
    return null;
  }
  return out.toBytes();
}