decompressFrame method

Uint8List decompressFrame(
  1. Uint8List data
)

Decompression data from lz4 frame

Implementation

Uint8List decompressFrame(Uint8List data) {
  if (!ListEquality().equals(_magickHeader, data.sublist(0, 4)) ||
      data.length < 7) {
    throw Exception('Invalid data');
  }

  final srcBuffer = data.getPointer();
  final estimateDstBufferSize =
      _validateFrameAndGetEstimatedDecodeBufferSize(data, srcBuffer);

  final contextPtr = malloc.allocate<Uint64>(1);
  _createDecompressionContext(contextPtr);
  final context = Pointer.fromAddress(contextPtr[0]);

  final dstSizePtr = malloc.allocate<Uint64>(1);
  final srcSizePtr = malloc.allocate<Uint64>(1);

  final dstBuffer = malloc.allocate<Uint8>(estimateDstBufferSize);

  try {
    srcSizePtr.value = data.length;
    int srcPtrOffset = 0;
    int nextSrcSize = 0;
    final decompressed = BytesBuilder(copy: true);
    do {
      dstSizePtr.value = estimateDstBufferSize;
      nextSrcSize = _decompressFrame(context, dstBuffer, dstSizePtr,
          srcBuffer.elementAt(srcPtrOffset), srcSizePtr);
      final srcSize = srcSizePtr.value;
      srcPtrOffset += srcSize;
      final dstSize = dstSizePtr.value;
      decompressed.add(dstBuffer.asTypedList(dstSize));
    } while (nextSrcSize > 0);
    return decompressed.takeBytes();
  } finally {
    _freeDecompressionContext(context);
    malloc.free(contextPtr);
    malloc.free(dstSizePtr);
    malloc.free(srcSizePtr);
    malloc.free(srcBuffer);
    malloc.free(dstBuffer);
  }
}