public static byte[] DecompressBytes(byte[] data, int offset, int count) { if (data == null) { throw new ArgumentNullException(nameof(data)); } if (offset < 0) { throw new ArgumentOutOfRangeException(nameof(offset)); } if (count < 0) { throw new ArgumentOutOfRangeException(nameof(count)); } if (count + offset > data.Length) { throw new ArgumentOutOfRangeException(nameof(count), "offset+count > data.length"); } var _lzma_stream = new lzma_stream(); var ret = Native.lzma_auto_decoder(_lzma_stream, ulong.MaxValue, Native.LZMA_CONCATENATED); if (ret == lzma_ret.LZMA_OK) { var decompressed = CodeBuffer(_lzma_stream, data, offset, count); Native.lzma_end(_lzma_stream); return(decompressed); } throw new Exception($"Can not create lzma stream: {ret}"); }
private const int BUFSIZE = 1024 * 512; // for decompress and read operation, a relatively large buffer is reasonable private void Init() { var r = Native.lzma_auto_decoder(_lzma_stream, ulong.MaxValue, Native.LZMA_CONCATENATED); if (r != lzma_ret.LZMA_OK) { throw new Exception($"Can not create lzma stream: {r}"); } _inbuf = Marshal.AllocHGlobal(BUFSIZE); _outbuf = Marshal.AllocHGlobal(BUFSIZE); // init lzma_stream _lzma_stream.next_in = _inbuf; _lzma_stream.next_out = _outbuf; _lzma_stream.avail_in = UIntPtr.Zero; _lzma_stream.avail_out = (UIntPtr)BUFSIZE; }