unwrap static method

Uint8List unwrap(
  1. Uint8List bytes, {
  2. int maxInputBytes = 512 * 1024 * 1024,
  3. int maxOutputBytes = 512 * 1024 * 1024,
  4. int maxDictionaryBytes = 64 * 1024 * 1024,
})

Returns raw XCF bytes, rejecting partial output, bad checksums and nesting.

Implementation

static Uint8List unwrap(Uint8List bytes, {int maxInputBytes = 512 * 1024 * 1024, int maxOutputBytes = 512 * 1024 * 1024, int maxDictionaryBytes = 64 * 1024 * 1024}) {
  if (maxInputBytes < 0 || maxOutputBytes < 0 || maxDictionaryBytes < 0) {
    throw ArgumentError('Envelope limits must be nonnegative');
  }
  if (bytes.length > maxInputBytes) {
    throw const XcfFormatException('Compressed XCF input exceeds the configured limit');
  }
  final XcfEnvelope? envelope = detect(bytes);
  try {
    final Uint8List result;
    switch (envelope) {
      case XcfEnvelope.none:
        result = bytes;
      case XcfEnvelope.gzip:
        result = z.GzipDecoder(maxOutputBytes: maxOutputBytes).convert(bytes);
      case XcfEnvelope.bzip2:
        result = z.Bzip2Decoder(maxInputBytes: maxInputBytes, maxOutputBytes: maxOutputBytes).convert(bytes);
      case XcfEnvelope.xz:
        result = z.XzDecoder(maxInputBytes: maxInputBytes, maxOutputBytes: maxOutputBytes, maxDictionaryBytes: maxDictionaryBytes).convert(bytes);
      case null:
        throw const XcfUnsupportedException('Unrecognized XCF envelope');
    }
    if (result.length > maxOutputBytes) {
      throw const XcfFormatException('Expanded XCF exceeds the configured limit');
    }
    if (!XcfCodec.matches(result)) {
      throw const XcfFormatException('Envelope does not contain a plain XCF document');
    }
    return result;
  } on z.ZCodecException catch (error) {
    throw XcfFormatException('Invalid XCF envelope: $error');
  } on RangeError catch (error) {
    throw XcfFormatException('Truncated XCF envelope: $error');
  }
}