public static int GetBlockSize(LZ4BlockSize blockSizeType)
        {
            switch (blockSizeType)
            {
            case LZ4BlockSize.Max64Kb:
                return(64 * KB);

            case LZ4BlockSize.Max256Kb:
                return(256 * KB);

            case LZ4BlockSize.Max1Mb:
                return(1 * MB);

            case LZ4BlockSize.Max4Mb:
                return(4 * MB);
            }

            return(-1);
        }
    public static int GetBlockSize(LZ4BlockSize blockSizeType)
    {
      switch (blockSizeType)
      {
        case LZ4BlockSize.Max64Kb:
          return 64*KB;
        case LZ4BlockSize.Max256Kb:
          return 256*KB;
        case LZ4BlockSize.Max1Mb:
          return 1*MB;
        case LZ4BlockSize.Max4Mb:
          return 4*MB;
      }

      return -1;
    }
    //readonly unsafe LZ4Preferences* _prefs;
    #endregion Private Members

    #region Ctor
    /// <summary>
    /// Initializes a new instance of the <see cref="LZ4Stream"/> class.
    /// </summary>
    /// <param name="baseStream">The base stream.</param>
    /// <param name="mode">The compression mode</param>
    /// <param name="level">The compression level. Relevant for <see cref="CompressionMode.Compress"/></param>
    /// <param name="blockSize">Size of the block.</param>
    /// <exception cref="ArgumentNullException"></exception>
    /// <exception cref="LZ4Exception">
    /// </exception>
    /// <exception cref="ArgumentOutOfRangeException">mode</exception>
    /// <exception cref="OutOfMemoryException">There is insufficient memory to satisfy the request.</exception>
    /// <exception cref="ArgumentException">Steam compression mode contradicts base-stream read/write availability</exception>
    public unsafe LZ4Stream(Stream baseStream, CompressionMode mode, Lz4CompressionLevel level = Lz4CompressionLevel.Fastest, LZ4BlockSize blockSize = LZ4BlockSize.Max4Mb)
    {
      if (baseStream == null) throw new ArgumentNullException(nameof(baseStream));
      if (mode == CompressionMode.Compress && !baseStream.CanWrite)
        throw new ArgumentException("stream is set to compress, but base-stream cannot be written to", nameof(baseStream));
      if (mode == CompressionMode.Decompress && !baseStream.CanRead)
        throw new ArgumentException("stream is set to decompress, but base-stream cannot be read from", nameof(baseStream));
      Contract.EndContractBlock();

      _baseStream = baseStream;
      _mode = mode;

      void* ctx;
      size_t rc;
      switch (mode) {
        case CompressionMode.Compress:
          rc = LZ4F_createCompressionContext(&ctx, Lz4Version);
          _ctx = ctx;

          if (LZ4F_isError(rc))
            throw new LZ4Exception(rc);

          LZ4Preferences prefs;
          prefs.AutoFlush = Lz4AutoFlush.Enabled;
          prefs.CompressionLevel = level;
          prefs.FrameInfo.BlockMode = LZ4BlockMode.Independent;
          prefs.FrameInfo.BlockSize = blockSize;
          prefs.FrameInfo.ContentChecksum = LZ4ContentChecksum.Enabled;

          MaxInputBufferSize = GetBlockSize(blockSize);
          _dstBuffer = new NativeBuffer(LZ4F_compressFrameBound((IntPtr) MaxInputBufferSize).ToInt32());

          var headerSize = LZ4F_compressBegin(_ctx, _dstBuffer.Ptr, (IntPtr) _dstBuffer.Size, &prefs);
          if (LZ4F_isError(headerSize))
            throw new LZ4Exception(headerSize,
              $"File header generation failed: {GetErrorName(headerSize)}");

          _baseStream.Write(_dstBuffer.Buffer, 0, headerSize.ToInt32());
          break;
        case CompressionMode.Decompress:
          rc = LZ4F_createDecompressionContext(&ctx, Lz4Version);
          _ctx = ctx;

          if (LZ4F_isError(rc))
            throw new LZ4Exception(rc);

          _srcBuffer = new NativeBuffer(DECOMPRESSION_BUFFER_SIZE);

          baseStream.Read(_srcBuffer.Buffer, 0, sizeof (int));

          var outSize = 0UL;
          var inSize = (ulong) sizeof (int);
          var frameHeader = stackalloc byte[MAX_FRAME_HEADER_SIZE];
          rc = LZ4F_decompress(_ctx, frameHeader, (IntPtr*) &outSize, _srcBuffer.Ptr, (IntPtr*) &inSize);

          if (LZ4F_isError(rc))
            throw new LZ4Exception(rc);

          _nextSizeToRead = (ulong) rc;
          break;
        default:
          throw new ArgumentOutOfRangeException(nameof(mode), $"Unsupported mode: {mode}");
      }
    }
Esempio n. 4
0
        //readonly unsafe LZ4Preferences* _prefs;
        #endregion Private Members

        #region Ctor
        /// <summary>
        /// Initializes a new instance of the <see cref="LZ4Stream"/> class.
        /// </summary>
        /// <param name="baseStream">The base stream.</param>
        /// <param name="mode">The compression mode</param>
        /// <param name="level">The compression level. Relevant for <see cref="CompressionMode.Compress"/></param>
        /// <param name="blockSize">Size of the block.</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="LZ4Exception">
        /// </exception>
        /// <exception cref="ArgumentOutOfRangeException">mode</exception>
        /// <exception cref="OutOfMemoryException">There is insufficient memory to satisfy the request.</exception>
        /// <exception cref="ArgumentException">Steam compression mode contradicts base-stream read/write availability</exception>
        public unsafe LZ4Stream(Stream baseStream, CompressionMode mode, Lz4CompressionLevel level = Lz4CompressionLevel.Fastest, LZ4BlockSize blockSize = LZ4BlockSize.Max4Mb)
        {
            if (baseStream == null)
            {
                throw new ArgumentNullException(nameof(baseStream));
            }
            if (mode == CompressionMode.Compress && !baseStream.CanWrite)
            {
                throw new ArgumentException("stream is set to compress, but base-stream cannot be written to", nameof(baseStream));
            }
            if (mode == CompressionMode.Decompress && !baseStream.CanRead)
            {
                throw new ArgumentException("stream is set to decompress, but base-stream cannot be read from", nameof(baseStream));
            }
            Contract.EndContractBlock();

            _baseStream = baseStream;
            _mode       = mode;

            void * ctx;
            size_t rc;

            switch (mode)
            {
            case CompressionMode.Compress:
                rc   = LZ4F_createCompressionContext(&ctx, Lz4Version);
                _ctx = ctx;

                if (LZ4F_isError(rc))
                {
                    throw new LZ4Exception(rc);
                }

                LZ4Preferences prefs;
                prefs.AutoFlush                 = Lz4AutoFlush.Enabled;
                prefs.CompressionLevel          = level;
                prefs.FrameInfo.BlockMode       = LZ4BlockMode.Independent;
                prefs.FrameInfo.BlockSize       = blockSize;
                prefs.FrameInfo.ContentChecksum = LZ4ContentChecksum.Enabled;

                MaxInputBufferSize = GetBlockSize(blockSize);
                _dstBuffer         = new NativeBuffer(LZ4F_compressFrameBound((IntPtr)MaxInputBufferSize).ToInt32());

                var headerSize = LZ4F_compressBegin(_ctx, _dstBuffer.Ptr, (IntPtr)_dstBuffer.Size, &prefs);
                if (LZ4F_isError(headerSize))
                {
                    throw new LZ4Exception(headerSize,
                                           $"File header generation failed: {GetErrorName(headerSize)}");
                }

                _baseStream.Write(_dstBuffer.Buffer, 0, headerSize.ToInt32());
                break;

            case CompressionMode.Decompress:
                rc   = LZ4F_createDecompressionContext(&ctx, Lz4Version);
                _ctx = ctx;

                if (LZ4F_isError(rc))
                {
                    throw new LZ4Exception(rc);
                }

                _srcBuffer = new NativeBuffer(DECOMPRESSION_BUFFER_SIZE);

                baseStream.Read(_srcBuffer.Buffer, 0, sizeof(int));

                var outSize     = 0UL;
                var inSize      = (ulong)sizeof(int);
                var frameHeader = stackalloc byte[MAX_FRAME_HEADER_SIZE];
                rc = LZ4F_decompress(_ctx, frameHeader, (IntPtr *)&outSize, _srcBuffer.Ptr, (IntPtr *)&inSize);

                if (LZ4F_isError(rc))
                {
                    throw new LZ4Exception(rc);
                }

                _nextSizeToRead = (ulong)rc;
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(mode), $"Unsupported mode: {mode}");
            }
        }