columnarDecompressWithNative function

Uint8List? columnarDecompressWithNative(
  1. Uint8List compressed,
  2. int algorithm
)

algorithm is 1 = zstd, 2 = lz4 (see Rust CompressionType).

Implementation

Uint8List? columnarDecompressWithNative(
  Uint8List compressed,
  int algorithm,
) {
  _bindOnce();
  final d = _decomp;
  final freeFn = _decompFree;
  if (d == null || freeFn == null) {
    return null;
  }
  final inLen = compressed.lengthInBytes;
  if (inLen > 0x7fffffff) {
    return null;
  }
  var inP = ffi.Pointer<ffi.Uint8>.fromAddress(0);
  var inOwned = false;
  if (inLen > 0) {
    inP = malloc<ffi.Uint8>(inLen);
    inOwned = true;
    inP.asTypedList(inLen).setAll(0, compressed);
  }
  final outP = malloc<ffi.Pointer<ffi.Uint8>>();
  outP.value = ffi.Pointer<ffi.Uint8>.fromAddress(0);
  final oLen = malloc<ffi.Uint32>();
  final oCap = malloc<ffi.Uint32>();
  try {
    final st = d(algorithm, inP, inLen, outP, oLen, oCap);
    if (st != 0) {
      return null;
    }
    final ptr = outP.value;
    if (ptr.address == 0) {
      return null;
    }
    final len = oLen.value;
    final cap = oCap.value;
    if (len >= zeroCopyResultThresholdBytes && _decompressFinalizer != null) {
      final view = ptr.asTypedList(len);
      final owner = _DecompressZeroCopyOwner(ptr.address);
      try {
        _decompressZeroCopyOwners[view] = owner;
        _decompressFinalizer!.attach(
          owner,
          ptr.cast(),
          detach: owner,
          externalSize: len,
        );
      } on Object {
        freeFn(ptr, len, cap);
        rethrow;
      }
      _pendingDecompressRelease[ptr.address] = (len, cap);
      return view;
    }
    final out = Uint8List.fromList(ptr.asTypedList(len));
    freeFn(ptr, len, cap);
    return out;
  } finally {
    if (inOwned) {
      malloc.free(inP);
    }
    malloc.free(outP);
    malloc.free(oLen);
    malloc.free(oCap);
  }
}