Пример #1
0
 public static unsafe LZMAStatus CompressBuffer(void *inBuff, IntPtr inLength, void *outBuff, IntPtr outLength, out IntPtr outPosition, LZMAFilter *filters)
 {
     fixed(IntPtr *op = &outPosition)
     return(LZMANative.lzma_stream_buffer_encode(filters, LZMACheck.LZMA_CHECK_CRC64, null,
                                                 (byte *)inBuff, inLength,
                                                 (byte *)outBuff, op, outLength));
 }
Пример #2
0
 public LZMAOptionLZMA(uint preset)
 {
     fixed(LZMAOptionLZMA *ptr = &this)
     {
         LZMANative.lzma_lzma_preset(ptr, preset);
     }
 }
Пример #3
0
        public static unsafe LZMAStatus DecompressBuffer(void *inBuff, IntPtr inLength, out IntPtr inPosition, void *outBuff, IntPtr outLength, out IntPtr outPosition)
        {
            ulong memLimit = 80 * 1024 * 1024;

            fixed(IntPtr *inp = &inPosition)
            fixed(IntPtr * op = &outPosition)
            return(LZMANative.lzma_stream_buffer_decode(&memLimit, 0, null, inBuff, inp, inLength, outBuff, op, outLength));
        }
Пример #4
0
        public unsafe void Init(Stream stream, CompressionMode mode, LZMAOptionLZMA *opts)
        {
            _compressedStream = stream;
            _mode             = mode;

            _isClosed = false;

            _zstreamBuff   = new byte[sizeof(LZMAStreamNative)];
            _zstreamHandle = GCHandle.Alloc(_zstreamBuff, GCHandleType.Pinned);
            _zstream       = (LZMAStreamNative *)_zstreamHandle.AddrOfPinnedObject().ToPointer();

            _tmpBuffer       = new byte[BufferSize];
            _tmpBufferHandle = GCHandle.Alloc(_tmpBuffer, GCHandleType.Pinned);
            _tmpBufferPtr    = _tmpBufferHandle.AddrOfPinnedObject().ToPointer();

            LZMAStatus ret;

            switch (mode)
            {
            case CompressionMode.Compress:
                // We will always use one filter, + 1 to mark the end of the filter array
                var filters = stackalloc LZMAFilter[2];
                filters[0].id      = LZMANative.LZMA_FILTER_LZMA2;
                filters[0].options = opts;
                filters[1].id      = LZMANative.LZMA_VLI_UNKNOWN;

                ret = LZMANative.lzma_stream_encoder(_zstream, filters, LZMACheck.LZMA_CHECK_CRC64);

                if (ret != LZMAStatus.LZMA_OK)
                {
                    throw new ArgumentException(string.Format("Unable to init LZMA decoder. Return code: {0}", ret));
                }

                _zstream->next_out  = _tmpBufferPtr;
                _zstream->avail_out = (IntPtr)_tmpBuffer.Length;

                break;

            case CompressionMode.Decompress:
                ret = LZMANative.lzma_auto_decoder(_zstream, 1024 * 1024 * 1024, 0);

                if (ret != LZMAStatus.LZMA_OK)
                {
                    throw new ArgumentException(string.Format("Unable to init LZMA decoder. Return code: {0}", ret));
                }
                break;
            }
        }
Пример #5
0
        unsafe void Write(byte *buffer, int count, LZMAAction action)
        {
            if (_mode == CompressionMode.Decompress)
            {
                throw new NotSupportedException("Can't write on a decompress stream!");
            }

            _wasWrittenTo = true;

            _zstream->avail_in = (IntPtr)count;
            _zstream->next_in  = buffer;

            do
            {
                var result = LZMANative.lzma_code(_zstream, action);

                var avail_out = _zstream->avail_out.ToInt32();
                if (avail_out < BufferSize)
                {
                    var outSize = BufferSize - avail_out;
                    _compressedStream.Write(_tmpBuffer, 0, outSize);
                    _zstream->next_out  = _tmpBufferPtr;
                    _zstream->avail_out = (IntPtr)BufferSize;
                }


                // Translate erros into specific exceptions
                switch (result)
                {
                case LZMAStatus.LZMA_OK:
                    continue;

                case LZMAStatus.LZMA_STREAM_END:
                    return;

                case LZMAStatus.LZMA_MEM_ERROR:
                case LZMAStatus.LZMA_MEMLIMIT_ERROR:
                    throw new OutOfMemoryException("liblzma return code: " + result);

                default:
                    throw new Exception("liblzma return code: " + result);
                }
            } while ((_zstream->avail_in.ToInt32() > 0) || action == LZMAAction.LZMA_FINISH);
        }
Пример #6
0
        public unsafe int Read(byte *buffer, int length, int offset, int count)
        {
            if (_mode == CompressionMode.Compress)
            {
                throw new NotSupportedException("Can't read on a compress stream!");
            }

            var exitLoop = false;

            _zstream->next_out  = buffer + offset;
            _zstream->avail_out = (IntPtr)count;

            while (_zstream->avail_out.ToInt32() > 0 && exitLoop == false)
            {
                if (_zstream->avail_in.ToInt32() == 0)
                {
                    var readLength = _compressedStream.Read(_tmpBuffer, 0, _tmpBuffer.Length);
                    _zstream->avail_in = (IntPtr)readLength;
                    _zstream->next_in  = _tmpBufferPtr;
                }
                var result = LZMANative.lzma_code(_zstream, LZMAAction.LZMA_RUN);

                if (result == LZMAStatus.LZMA_OK)
                {
                    continue;
                }

                switch (result)
                {
                case LZMAStatus.LZMA_STREAM_END:
                    exitLoop = true;
                    break;

                case LZMAStatus.LZMA_MEM_ERROR:
                case LZMAStatus.LZMA_MEMLIMIT_ERROR:
                    throw new OutOfMemoryException("liblzma return code: " + result);

                default:
                    throw new Exception("liblzma return code: " + result);
                }
            }

            return(count - (int)_zstream->avail_out);
        }
Пример #7
0
        public unsafe void Init(Stream stream, CompressionMode mode, uint preset)
        {
            _compressedStream = stream;
            _mode             = mode;

            _isClosed = false;

            _zstreamBuff   = new byte[sizeof(LZMAStreamNative)];
            _zstreamHandle = GCHandle.Alloc(_zstreamBuff, GCHandleType.Pinned);
            _zstream       = (LZMAStreamNative *)_zstreamHandle.AddrOfPinnedObject().ToPointer();

            _tmpBuffer       = new byte[BufferSize];
            _tmpBufferHandle = GCHandle.Alloc(_tmpBuffer, GCHandleType.Pinned);
            _tmpBufferPtr    = _tmpBufferHandle.AddrOfPinnedObject().ToPointer();

            LZMAStatus ret;

            switch (mode)
            {
            case CompressionMode.Compress:
                ret = LZMANative.lzma_easy_encoder(_zstream, preset, LZMACheck.LZMA_CHECK_CRC64);

                if (ret != LZMAStatus.LZMA_OK)
                {
                    throw new ArgumentException(string.Format("Unable to init LZMA decoder. Return code: {0}", ret));
                }

                _zstream->next_out  = _tmpBufferPtr;
                _zstream->avail_out = (IntPtr)_tmpBuffer.Length;

                break;

            case CompressionMode.Decompress:
                ret = LZMANative.lzma_auto_decoder(_zstream, 1024 * 1024 * 1024, 0);

                if (ret != LZMAStatus.LZMA_OK)
                {
                    throw new ArgumentException(string.Format("Unable to init LZMA decoder. Return code: {0}", ret));
                }
                break;
            }
        }