From 39be27e2a60d8153883c92c6b0f93e01043f0b32 Mon Sep 17 00:00:00 2001 From: Pieter-Jan Briers Date: Tue, 11 Jun 2024 18:44:18 +0200 Subject: [PATCH] 0% tested fix compiler warnings with async zstd --- Robust.Cdn/Helpers/ZStd.cs | 135 +++++++++++++++++++------------------ 1 file changed, 71 insertions(+), 64 deletions(-) diff --git a/Robust.Cdn/Helpers/ZStd.cs b/Robust.Cdn/Helpers/ZStd.cs index b4e6373..6037059 100644 --- a/Robust.Cdn/Helpers/ZStd.cs +++ b/Robust.Cdn/Helpers/ZStd.cs @@ -211,30 +211,33 @@ public override async ValueTask ReadAsync( return 0; } - unsafe + var outputPos = DecompressChunk(); + if (outputPos > 0) + return outputPos; + } while (true); + + unsafe int DecompressChunk() + { + fixed (byte* inputPtr = _buffer) + fixed (byte* outputPtr = buffer.Span) { - fixed (byte* inputPtr = _buffer) - fixed (byte* outputPtr = buffer.Span) - { - ZSTD_outBuffer outputBuf = default; - outputBuf.dst = outputPtr; - outputBuf.pos = 0; - outputBuf.size = (nuint)buffer.Length; - ZSTD_inBuffer inputBuf = default; - inputBuf.src = inputPtr; - inputBuf.pos = (nuint)_bufferPos; - inputBuf.size = (nuint)_bufferSize; - - var ret = ZSTD_decompressStream(_ctx, &outputBuf, &inputBuf); - - _bufferPos = (int)inputBuf.pos; - ZStdException.ThrowIfError(ret); - - if (outputBuf.pos > 0) - return (int)outputBuf.pos; - } + ZSTD_outBuffer outputBuf = default; + outputBuf.dst = outputPtr; + outputBuf.pos = 0; + outputBuf.size = (nuint)buffer.Length; + ZSTD_inBuffer inputBuf = default; + inputBuf.src = inputPtr; + inputBuf.pos = (nuint)_bufferPos; + inputBuf.size = (nuint)_bufferSize; + + var ret = ZSTD_decompressStream(_ctx, &outputBuf, &inputBuf); + + _bufferPos = (int)inputBuf.pos; + ZStdException.ThrowIfError(ret); + + return (int)outputBuf.pos; } - } while (true); + } } public override long Seek(long offset, SeekOrigin origin) @@ -345,25 +348,7 @@ private async ValueTask FlushInternalAsync(ZSTD_EndDirective directive, Cancella while (true) { - nuint err; - unsafe - { - fixed (byte* outPtr = _buffer) - { - ZSTD_outBuffer outBuf = default; - outBuf.size = (nuint)_buffer.Length; - outBuf.pos = outBufPos; - outBuf.dst = outPtr; - - ZSTD_inBuffer inBuf; - - - err = ZSTD_compressStream2(Context.Context, &outBuf, &inBuf, directive); - ZStdException.ThrowIfError(err); - outBufPos = outBuf.pos; - _bufferPos = (int)outBuf.pos; - } - } + var err = FlushChunk(); await _baseStream.WriteAsync(_buffer.AsMemory(0, (int)outBufPos), cancel); _bufferPos = 0; @@ -374,6 +359,26 @@ private async ValueTask FlushInternalAsync(ZSTD_EndDirective directive, Cancella } await _baseStream.FlushAsync(cancel); + + unsafe nuint FlushChunk() + { + fixed (byte* outPtr = _buffer) + { + ZSTD_outBuffer outBuf = default; + outBuf.size = (nuint)_buffer.Length; + outBuf.pos = outBufPos; + outBuf.dst = outPtr; + + ZSTD_inBuffer inBuf; + + var err = ZSTD_compressStream2(Context.Context, &outBuf, &inBuf, directive); + ZStdException.ThrowIfError(err); + outBufPos = outBuf.pos; + _bufferPos = (int)outBuf.pos; + + return err; + } + } } public override int Read(byte[] buffer, int offset, int count) @@ -446,29 +451,7 @@ public override async ValueTask WriteAsync( while (true) { - unsafe - { - fixed (byte* outPtr = _buffer) - fixed (byte* inPtr = buffer.Span) - { - ZSTD_outBuffer outBuf = default; - outBuf.size = (nuint)_buffer.Length; - outBuf.pos = outBufPos; - - ZSTD_inBuffer inBuf = default; - inBuf.pos = inBufPos; - inBuf.size = inBufSize; - - outBuf.dst = outPtr; - inBuf.src = inPtr; - - var err = ZSTD_compressStream2(Context.Context, &outBuf, &inBuf, ZSTD_EndDirective.ZSTD_e_continue); - ZStdException.ThrowIfError(err); - _bufferPos = (int)outBuf.pos; - outBufPos = outBuf.pos; - inBufPos = inBuf.pos; - } - } + CompressChunk(); if (inBufPos >= inBufSize) break; @@ -478,6 +461,30 @@ public override async ValueTask WriteAsync( _bufferPos = 0; outBufPos = 0; } + + unsafe void CompressChunk() + { + fixed (byte* outPtr = _buffer) + fixed (byte* inPtr = buffer.Span) + { + ZSTD_outBuffer outBuf = default; + outBuf.size = (nuint)_buffer.Length; + outBuf.pos = outBufPos; + + ZSTD_inBuffer inBuf = default; + inBuf.pos = inBufPos; + inBuf.size = inBufSize; + + outBuf.dst = outPtr; + inBuf.src = inPtr; + + var err = ZSTD_compressStream2(Context.Context, &outBuf, &inBuf, ZSTD_EndDirective.ZSTD_e_continue); + ZStdException.ThrowIfError(err); + _bufferPos = (int)outBuf.pos; + outBufPos = outBuf.pos; + inBufPos = inBuf.pos; + } + } } public override bool CanRead => false;