-
Notifications
You must be signed in to change notification settings - Fork 343
Api brotli changes #1621
Api brotli changes #1621
Changes from 1 commit
f2da3ba
da92380
790191c
f3b3e58
c87498f
e152fd1
d06e4b4
fb317f5
8fef201
d31a1c6
78a1a75
d9b7587
5a6f037
fb51724
1972d8b
6739e69
59f3981
959a119
7f8b3d4
d4ddd1f
38ef5ec
649f42c
516f4c6
e1aa814
1120e4f
2f71a9b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,7 +50,7 @@ public void InitializeEncoder() | |
|
||
public void SetQuality(uint quality) | ||
{ | ||
if (quality < MinQuality || quality > MaxQuality) | ||
if (quality > MaxQuality) | ||
{ | ||
throw new ArgumentException(BrotliEx.WrongQuality); | ||
} | ||
|
@@ -64,7 +64,7 @@ public void SetQuality() | |
|
||
public void SetWindow(uint window) | ||
{ | ||
if (window < MinWindowBits || window > MaxWindowBits) | ||
if (window - MinWindowBits > MaxWindowBits - MinWindowBits) | ||
{ | ||
throw new ArgumentException(BrotliEx.WrongWindowSize); | ||
} | ||
|
@@ -103,9 +103,9 @@ private static TransformationStatus GetTransformationStatusFromBrotliDecoderResu | |
return TransformationStatus.InvalidData; | ||
} | ||
|
||
public static TransformationStatus FlushEncoder(Span<byte> source, Span<byte> destination, out int bytesConsumed, out int bytesWritten, ref State state, bool is_finished = true) | ||
public static TransformationStatus FlushEncoder(Span<byte> source, Span<byte> destination, out int bytesConsumed, out int bytesWritten, ref State state, bool isFinished = true) | ||
{ | ||
BrotliEncoderOperation operation = is_finished ? BrotliEncoderOperation.Finish : BrotliEncoderOperation.Flush; | ||
BrotliEncoderOperation operation = isFinished ? BrotliEncoderOperation.Finish : BrotliEncoderOperation.Flush; | ||
bytesConsumed = source.Length; | ||
bytesWritten = destination.Length; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These should not be set to source.Length and destination.Length in the beginning. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. really it should be because we send this variables to the native library (where they means array size) |
||
if (state.BrotliNativeState == IntPtr.Zero) return TransformationStatus.InvalidData; | ||
|
@@ -171,7 +171,6 @@ public static TransformationStatus Compress(ReadOnlySpan<byte> source, Span<byte | |
public static TransformationStatus Decompress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesConsumed, out int bytesWritten, ref State state) | ||
{ | ||
bool endOfStream = false; | ||
bool errorDetected = false; | ||
bytesConsumed = source.Length; | ||
bytesWritten = destination.Length; | ||
BrotliDecoderResult LastDecoderResult = BrotliDecoderResult.NeedsMoreInput; | ||
|
||
|
@@ -202,17 +201,13 @@ public static TransformationStatus Decompress(ReadOnlySpan<byte> source, Span<by | |
endOfStream = true; | ||
|
||
} | ||
|
||
if (endOfStream && !BrotliNative.BrotliDecoderIsFinished(state.BrotliNativeState)) | ||
{ | ||
errorDetected = true; | ||
} | ||
if (LastDecoderResult == BrotliDecoderResult.Error || errorDetected) | ||
if (LastDecoderResult == BrotliDecoderResult.Error || endOfStream && !BrotliNative.BrotliDecoderIsFinished(state.BrotliNativeState)) | ||
{ | ||
var error = BrotliNative.BrotliDecoderGetErrorCode(state.BrotliNativeState); | ||
var text = BrotliNative.BrotliDecoderErrorString(error); | ||
throw new System.IO.IOException(text + BrotliEx.unableDecode); | ||
} | ||
if (endOfStream && !BrotliNative.BrotliDecoderIsFinished(state.BrotliNativeState) && LastDecoderResult == BrotliDecoderResult.NeedsMoreInput) | ||
if (LastDecoderResult == BrotliDecoderResult.NeedsMoreInput) | ||
{ | ||
throw new System.IO.IOException(BrotliEx.FinishDecompress); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,7 +43,10 @@ public partial class BrotliStream : Stream | |
|
||
public BrotliStream(Stream baseStream, CompressionMode mode, bool leaveOpen, int bufferSize, CompressionLevel quality) : this(baseStream, mode, leaveOpen, bufferSize) | ||
{ | ||
if (_mode == CompressionMode.Decompress) throw new System.IO.IOException(BrotliEx.QualityAndWinSize); | ||
if (_mode == CompressionMode.Decompress) | ||
{ | ||
throw new System.IO.IOException(BrotliEx.QualityAndWinSize); | ||
} | ||
else | ||
{ | ||
_state.SetQuality((uint)Brotli.GetQualityFromCompressionLevel(quality)); | ||
|
@@ -238,7 +241,8 @@ public override int Read(byte[] buffer, int offset, int count) | |
{ | ||
throw new TimeoutException(BrotliEx.TimeoutRead); | ||
} | ||
while (true) { | ||
while (true) | ||
{ | ||
if (transformationResult == TransformationStatus.NeedMoreSourceData) | ||
{ | ||
_availableInput = _stream.Read(_nextInput, 0, (int)_bufferSize); | ||
|
@@ -252,15 +256,16 @@ public override int Read(byte[] buffer, int offset, int count) | |
{ | ||
BufferStream.Write(_bufferOutput, 0, _availableOutput); | ||
bytesRead += _availableOutput; | ||
} else | ||
} | ||
else | ||
{ | ||
endOfStream = true; | ||
break; | ||
} | ||
transformationResult = Brotli.Decompress(_nextInput, _bufferOutput, out _availableInput, out _availableOutput, ref _state); | ||
if (bytesRead >= count) break; | ||
} | ||
if (transformationResult==TransformationStatus.Done) | ||
if (transformationResult == TransformationStatus.Done) | ||
{ | ||
bytesRead += _availableOutput; | ||
BufferStream.Write(_bufferOutput, 0, _availableOutput); | ||
|
@@ -310,18 +315,18 @@ public override void Write(byte[] buffer, int offset, int count) | |
throw new TimeoutException(BrotliEx.TimeoutWrite); | ||
} | ||
copyLen = bytesRemain > _bufferSize ? _bufferSize : bytesRemain; | ||
byte[] _bufferInput = new byte[copyLen]; | ||
Array.Copy(buffer, currentOffset, _bufferInput, 0, copyLen); | ||
byte[] bufferInput = new byte[copyLen]; | ||
|
||
Array.Copy(buffer, currentOffset, bufferInput, 0, copyLen); | ||
bytesRemain -= copyLen; | ||
currentOffset += copyLen; | ||
_availableInput = copyLen; | ||
_availableOutput = _bufferSize; | ||
TransformationStatus transformationResult = TransformationStatus.DestinationTooSmall; | ||
while (transformationResult == TransformationStatus.DestinationTooSmall) | ||
{ | ||
transformationResult = Brotli.Compress(_bufferInput, _bufferOutput, out _availableInput, out _availableOutput, ref _state); | ||
if (transformationResult==TransformationStatus.InvalidData) throw new System.IO.IOException(BrotliEx.unableEncode); | ||
if (transformationResult==TransformationStatus.DestinationTooSmall) _stream.Write(_bufferOutput, 0, _availableOutput); | ||
transformationResult = Brotli.Compress(bufferInput, _bufferOutput, out _availableInput, out _availableOutput, ref _state); | ||
if (transformationResult == TransformationStatus.InvalidData) throw new System.IO.IOException(BrotliEx.unableEncode); | ||
if (transformationResult == TransformationStatus.DestinationTooSmall) _stream.Write(_bufferOutput, 0, _availableOutput); | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why change the input source to Span instead of ReadOnlySpan?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is different methods, but yes their should be ReadOnlySpan also