Ejemplo n.º 1
0
        public static OperationStatus Compress(ReadOnlySpan <byte> source, Span <byte> destination, out int bytesConsumed, out int bytesWritten, ref State state)
        {
            EnsureInitialized(ref state, true);
            bytesWritten  = destination.Length;
            bytesConsumed = source.Length;
            unsafe
            {
                IntPtr bufIn, bufOut;
                while (bytesConsumed > 0)
                {
                    fixed(byte *inBytes = &MemoryMarshal.GetReference(source))
                    fixed(byte *outBytes = &MemoryMarshal.GetReference(destination))
                    {
                        bufIn  = new IntPtr(inBytes);
                        bufOut = new IntPtr(outBytes);
                        nuint availableOutput = (nuint)bytesWritten;
                        nuint consumed        = (nuint)bytesConsumed;

                        if (!BrotliNative.BrotliEncoderCompressStream(state.BrotliNativeState, BrotliEncoderOperation.Process, ref consumed, ref bufIn, ref availableOutput, ref bufOut, out nuint totalOut))
                        {
                            return(OperationStatus.InvalidData);
                        }
                        ;
                        bytesConsumed = (int)consumed;
                        bytesWritten  = destination.Length - (int)availableOutput;
                        if (availableOutput != (nuint)destination.Length)
                        {
                            return(OperationStatus.DestinationTooSmall);
                        }
                    }
                }
                return(OperationStatus.Done);
            }
        }
Ejemplo n.º 2
0
        public override void Write(byte[] buffer, int offset, int count)
        {
            EnsureCompressionMode();
            ValidateParameters(buffer, offset, count);
            EnsureNotDisposed();
            if (_mode != CompressionMode.Compress)
            {
                totalWrote += count;
            }
            DateTime begin         = DateTime.Now;
            nuint    totalOut      = 0;
            int      bytesRemain   = count;
            int      currentOffset = offset;
            int      copyLen;

            while (bytesRemain > 0)
            {
                TimeSpan ExecutionTime = DateTime.Now - begin;
                if (WriteTimeout > 0 && ExecutionTime.TotalMilliseconds >= WriteTimeout)
                {
                    throw new TimeoutException(BrotliEx.TimeoutWrite);
                }
                copyLen = bytesRemain > _bufferSize ? _bufferSize : bytesRemain;
                Marshal.Copy(buffer, currentOffset, _bufferInput, copyLen);
                bytesRemain    -= copyLen;
                currentOffset  += copyLen;
                _availableInput = (nuint)copyLen;
                _nextInput      = _bufferInput;
                while ((int)_availableInput > 0)
                {
                    if (!BrotliNative.BrotliEncoderCompressStream(_encoder.State, BrotliEncoderOperation.Process, ref _availableInput, ref _nextInput, ref _availableOutput,
                                                                  ref _nextOutput, out totalOut))
                    {
                        throw new System.IO.IOException(BrotliEx.unableEncode);
                    }

                    if (_availableOutput != (nuint)_bufferSize)
                    {
                        var    bytesWrote = (int)((nuint)_bufferSize - _availableOutput);
                        Byte[] buf        = new Byte[bytesWrote];
                        Marshal.Copy(_bufferOutput, buf, 0, bytesWrote);
                        _stream.Write(buf, 0, bytesWrote);
                        _availableOutput = (nuint)_bufferSize;
                        _nextOutput      = _bufferOutput;
                    }
                }
                if (BrotliNative.BrotliEncoderIsFinished(_encoder.State))
                {
                    break;
                }
            }
        }
Ejemplo n.º 3
0
        public static OperationStatus FlushEncoder(ReadOnlySpan <byte> source, Span <byte> destination, out int bytesConsumed, out int bytesWritten, ref State state, bool isFinished = true)
        {
            EnsureInitialized(ref state, true);
            BrotliEncoderOperation operation = isFinished ? BrotliEncoderOperation.Finish : BrotliEncoderOperation.Flush;

            bytesWritten  = destination.Length;
            bytesConsumed = 0;
            if (state.BrotliNativeState == IntPtr.Zero)
            {
                return(OperationStatus.InvalidData);
            }
            if (BrotliNative.BrotliEncoderIsFinished(state.BrotliNativeState))
            {
                return(OperationStatus.Done);
            }
            unsafe
            {
                IntPtr bufIn, bufOut;
                fixed(byte *inBytes = &MemoryMarshal.GetReference(source))
                fixed(byte *outBytes = &MemoryMarshal.GetReference(destination))
                {
                    bufIn  = new IntPtr(inBytes);
                    bufOut = new IntPtr(outBytes);
                    nuint availableOutput = (nuint)destination.Length;
                    nuint consumed        = (nuint)source.Length;

                    if (!BrotliNative.BrotliEncoderCompressStream(state.BrotliNativeState, operation, ref consumed, ref bufIn, ref availableOutput, ref bufOut, out nuint totalOut))
                    {
                        return(OperationStatus.InvalidData);
                    }
                    bytesConsumed = (int)consumed;
                    bytesWritten  = (int)availableOutput;
                }
                bytesWritten = destination.Length - bytesWritten;
                if (bytesWritten > 0)
                {
                    if (BrotliNative.BrotliEncoderIsFinished(state.BrotliNativeState))
                    {
                        return(OperationStatus.Done);
                    }
                    else
                    {
                        return(OperationStatus.DestinationTooSmall);
                    }
                }
            }
            return(OperationStatus.Done);
        }
Ejemplo n.º 4
0
        public override void Write(byte[] buffer, int offset, int count)
        {
            EnsureCompressionMode();
            ValidateParameters(buffer, offset, count);
            EnsureNotDisposed();
            if (_mode != CompressionMode.Compress)
            {
                totalWrote += count;
            }
            nuint totalOut      = 0;
            int   bytesRemain   = count;
            int   currentOffset = offset;
            int   copyLen;

            while (bytesRemain > 0)
            {
                copyLen = bytesRemain > BufferSize ? BufferSize : bytesRemain;
                Marshal.Copy(buffer, currentOffset, BufferIn, copyLen);
                bytesRemain   -= copyLen;
                currentOffset += copyLen;
                AvailIn        = (IntPtr)copyLen;
                NextIn         = BufferIn;
                while ((int)AvailIn > 0)
                {
                    if (!BrotliNative.BrotliEncoderCompressStream(_encoder.State, BrotliNative.BrotliEncoderOperation.Process, ref AvailIn, ref NextIn, ref AvailOut,
                                                                  ref NextOut, out totalOut))
                    {
                        throw new System.IO.IOException("Unable compress stream");
                    }
                    if ((nuint)AvailOut != BufferSize)
                    {
                        var    bytesWrote = (int)(BufferSize - (nuint)AvailOut);
                        Byte[] buf        = new Byte[bytesWrote];
                        Marshal.Copy(BufferOut, buf, 0, bytesWrote);
                        _stream.Write(buf, 0, bytesWrote);
                        AvailOut = new IntPtr((uint)BufferSize);
                        NextOut  = BufferOut;
                    }
                }
                if (BrotliNative.BrotliEncoderIsFinished(_encoder.State))
                {
                    break;
                }
            }
        }
Ejemplo n.º 5
0
        protected virtual void FlushEncoder(Boolean finished)
        {
            if (_encoder.State == IntPtr.Zero)
            {
                return;
            }
            if (BrotliNative.BrotliEncoderIsFinished(_encoder.State))
            {
                return;
            }
            BrotliNative.BrotliEncoderOperation op = finished ? BrotliNative.BrotliEncoderOperation.Finish : BrotliNative.BrotliEncoderOperation.Flush;
            UInt32 totalOut = 0;

            while (true)
            {
                if (!BrotliNative.BrotliEncoderCompressStream(_encoder.State, op, ref AvailIn, ref NextIn, ref AvailOut, ref NextOut, out totalOut))
                {
                    throw new Exception();                                                                                                                                 // unable encode
                }
                var extraData = (nuint)AvailOut != BufferSize;
                if (extraData)
                {
                    var    bytesWrote = (int)(BufferSize - (nuint)AvailOut);
                    Byte[] buf        = new Byte[bytesWrote];
                    Marshal.Copy(BufferOut, buf, 0, bytesWrote);
                    _stream.Write(buf, 0, bytesWrote);
                    AvailOut = (IntPtr)BufferSize;
                    NextOut  = BufferOut;
                }
                if (BrotliNative.BrotliEncoderIsFinished(_encoder.State))
                {
                    break;
                }
                if (!extraData)
                {
                    break;
                }
            }
        }
Ejemplo n.º 6
0
        protected virtual void FlushEncoder(bool finished)
        {
            if (_encoder.State == IntPtr.Zero)
            {
                return;
            }
            if (BrotliNative.BrotliEncoderIsFinished(_encoder.State))
            {
                return;
            }
            BrotliEncoderOperation op = finished ? BrotliEncoderOperation.Finish : BrotliEncoderOperation.Flush;
            nuint totalOut            = 0;

            while (true)
            {
                if (!BrotliNative.BrotliEncoderCompressStream(_encoder.State, op, ref _availableInput, ref _nextInput, ref _availableOutput, ref _nextOutput, out totalOut))
                {
                    throw new System.IO.IOException(BrotliEx.unableEncode);
                }
                var extraData = (nuint)_availableOutput != (nuint)_bufferSize;
                if (extraData)
                {
                    var    bytesWrote = (int)((nuint)_bufferSize - (nuint)_availableOutput);
                    Byte[] buf        = new Byte[bytesWrote];
                    Marshal.Copy(_bufferOutput, buf, 0, bytesWrote);
                    _stream.Write(buf, 0, bytesWrote);
                    _availableOutput = (nuint)_bufferSize;
                    _nextOutput      = _bufferOutput;
                }
                if (BrotliNative.BrotliEncoderIsFinished(_encoder.State))
                {
                    break;
                }
                if (!extraData)
                {
                    break;
                }
            }
        }