Comments (10)
Legacy (lz4net) streams can only be handled by .Legacy library.
So "We even tried Legacy package" is the only way to make it work, therefore "which also fails with a different error" is the only question I have - what was this different error? can you provide code? can you provide file?
from k4os.compression.lz4.
Thanks @MiloszKrajewski for help on this.
This is stack trace while using Legacy .net 6. I also see same error message when using lz4net.netstandard
System.AggregateException
HResult=0x80131500
Message=One or more errors occurred. (Unexpected end of stream)
Source=System.Threading.Tasks.Parallel
StackTrace:
at System.Threading.Tasks.TaskReplicator.Run[TState](ReplicatableUserAction1 action, ParallelOptions options, Boolean stopOnFirstFailure) at System.Threading.Tasks.Parallel.PartitionerForEachWorker[TSource,TLocal](Partitioner
1 source, ParallelOptions parallelOptions, Action1 simpleBody, Action
2 bodyWithState, Action3 bodyWithStateAndIndex, Func
4 bodyWithStateAndLocal, Func5 bodyWithEverything, Func
1 localInit, Action1 localFinally) --- End of stack trace from previous location --- at System.Threading.Tasks.Parallel.ThrowSingleCancellationExceptionOrOtherException(ICollection exceptions, CancellationToken cancelToken, Exception otherException) at System.Threading.Tasks.Parallel.PartitionerForEachWorker[TSource,TLocal](Partitioner
1 source, ParallelOptions parallelOptions, Action1 simpleBody, Action
2 bodyWithState, Action3 bodyWithStateAndIndex, Func
4 bodyWithStateAndLocal, Func5 bodyWithEverything, Func
1 localInit, Action1 localFinally) at System.Threading.Tasks.Parallel.ForEachWorker[TSource,TLocal](IEnumerable
1 source, ParallelOptions parallelOptions, Action1 body, Action
2 bodyWithState, Action3 bodyWithStateAndIndex, Func
4 bodyWithStateAndLocal, Func5 bodyWithEverything, Func
1 localInit, Action1 localFinally) at System.Threading.Tasks.Parallel.ForEach[TSource](IEnumerable
1 source, Action`3 body)
at Program.<
$>d__0.MoveNext() in C:\Code\ConsoleApp1\Program.cs:line 56at Program.(String[] args)
This exception was originally thrown at this call stack:
K4os.Compression.LZ4.Legacy.LZ4Stream.AcquireNextChunk()
K4os.Compression.LZ4.Legacy.LZ4Stream.Read(byte[], int, int)
System.IO.Stream.CopyTo(System.IO.Stream, int)
System.IO.Stream.CopyTo(System.IO.Stream)
System.Threading.Tasks.Parallel.PartitionerForEachWorker.AnonymousMethod__1(ref System.Collections.IEnumerator, int, out bool)
System.Threading.Tasks.Parallel.PartitionerForEachWorker.AnonymousMethod__1(ref System.Collections.IEnumerator, int, out bool)
System.Threading.Tasks.TaskReplicator.Replica.ExecuteAction(out bool)
...
[Call Stack Truncated]
Inner Exception 1:
EndOfStreamException: Unexpected end of stream
Code:
public Stream Decompress(byte[] bytes)
{
var output = new MemoryStream();
var compressStream = new MemoryStream(bytes);
using (var lz4Stream = LZ4Legacy.Decode(compressStream))
{
lz4Stream.CopyTo(output);
}
output.Position = 0;
return output;
}
I even tried calling from .NET Core 3.1, it failed with below error:
System.AggregateException
HResult=0x80131500
Message=One or more errors occurred. (One or more errors occurred. (The type initializer for 'K4os.Compression.LZ4.Engine.LL' threw an exception.))
Source=System.Private.CoreLib
StackTrace:
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification) at System.Threading.Tasks.Task
1.get_Result()
at ConsoleApp2.Program.Main(String[] args) in C:\Code\ConsoleApp2\Program.cs:line 60
This exception was originally thrown at this call stack:
K4os.Compression.LZ4.Internal.Mem.CloneArray(uint[])
K4os.Compression.LZ4.Engine.LL.LL()
Inner Exception 1:
AggregateException: One or more errors occurred. (The type initializer for 'K4os.Compression.LZ4.Engine.LL' threw an exception.)
Inner Exception 2:
TypeInitializationException: The type initializer for 'K4os.Compression.LZ4.Engine.LL' threw an exception.
Inner Exception 3:
FileNotFoundException: Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
from k4os.compression.lz4.
This AggregateException from System.Threading.Tasks.Parallel has nothing to do with LZ4.
What are those bytes[]
in var compressStream = new MemoryStream(bytes);
? Where they are coming from?
IT seems like it is not valid legacy stream (thus Unexpected end of stream
).
Can I get those bytes?
What was the code which created those bytes? Was it as stream? or a block?
Can you show code on the other side (lz4net): get plain bytes and compress them?
from k4os.compression.lz4.
when calling it from .NET 3.1 there is a problem with 'System.Runtime.CompilerServices.Unsafe' which is raised and worked around in #72, but it is a different issue. You primary issue still is: your bytes are most likely not a legacy stream.
from k4os.compression.lz4.
This AggregateException from System.Threading.Tasks.Parallel has nothing to do with LZ4.
What are those
bytes[]
invar compressStream = new MemoryStream(bytes);
? Where they are coming from? IT seems like it is not valid legacy stream (thusUnexpected end of stream
). Can I get those bytes?What was the code which created those bytes? Was it as stream? or a block?
Can you show code on the other side (lz4net): get plain bytes and compress them?
I totally understand AggregateException is nothing to do with Lz4.
Bytes are coming from upstream where decryption happened. Actually full flow, we save SymmerticKeyEncrypted and compressed payload in a column of db. After retrieval we decrypt and then try to decompression.
below is code which does Decryption:
public override byte[] Decrypt(byte[] encryptedMessage)
{
var iv = new byte[_algorithm.BlockSize / 8];
var encryptedBytes = new byte[encryptedMessage.Length - iv.Length];
Array.Copy(encryptedMessage, 0, iv, 0, iv.Length);
Array.Copy(encryptedMessage, iv.Length, encryptedBytes, 0, encryptedBytes.Length);
try
{
return Decrypt(_aesAlgorithm, iv, _key, encryptedBytes);
}
catch (Exception e)
{
return Decrypt(_algorithm, iv, _key, encryptedBytes);
}
}
public static byte[] Decrypt<T>(T algorithm, byte[] iv, byte[] key, byte[] encryptedBytes) where T : SymmetricAlgorithm
{
using (var decryptor = algorithm.CreateDecryptor(key, iv))
using (var memoryStream = new MemoryStream(encryptedBytes))
using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
{
// plain bytes can't be larger than encrypted length
byte[] buffer = new byte[encryptedBytes.Length];
var numBytes = cryptoStream.Read(buffer, 0, buffer.Length);
var bytes = new byte[numBytes];
Array.Copy(buffer, 0, bytes, 0, bytes.Length);
return bytes;
}
}
and then we decompress. Old implementation using lz4net is below:
public class LZ4CompressionProvider : ICompressionProvider
{
public string Prefix => "_lz4:";
private readonly LZ4StreamFlags _streamFlags;
public LZ4CompressionProvider()
{
_streamFlags = LZ4StreamFlags.HighCompression;
}
public LZ4CompressionProvider(LZ4StreamFlags streamFlags)
{
_streamFlags = streamFlags;
}
public static Stream CompressStream(Stream stream)
{
return new LZ4Stream(stream, LZ4StreamMode.Compress, LZ4StreamFlags.HighCompression);
}
public static Stream DecompressStream(Stream stream)
{
return new LZ4Stream(stream, LZ4StreamMode.Decompress);
}
public Stream Compress(byte[] bytes)
{
return ProcessBytes(bytes, LZ4StreamMode.Compress, _streamFlags);
}
public Stream Compress(Stream stream)
{
return new LZ4Stream(stream, LZ4StreamMode.Compress, _streamFlags);
}
public Stream Decompress(byte[] bytes)
{
return ProcessBytes(bytes, LZ4StreamMode.Decompress);
}
public Stream Decompress(Stream stream)
{
return DecompressStream(stream);
}
private MemoryStream ProcessBytes(byte[] bytes, LZ4StreamMode streamMode, LZ4StreamFlags streamFlags = LZ4StreamFlags.Default)
{
var output = new MemoryStream();
var compressStream = new MemoryStream(bytes);
using (var lz4Stream = new LZ4Stream(compressStream, streamMode, streamFlags))
{
lz4Stream.CopyTo(output);
}
output.Position = 0;
return output;
}
}
from k4os.compression.lz4.
So the shortest answer is: I don't know. Maybe you forgot to flush the stream and it is length 0, or maybe you messed with encryption padding. All those extra elements don't help.
Although, to help you a little I prepared example in:
https://github.com/MiloszKrajewski/K4os.Compression.LZ4/tree/master/assets/issue76
You will find two projects there:
- netcoreapp3.1 project using lz4net to compress data
- net6.0 project using k4os to decompress legacy stream without any problem
If you can make this example fail with your data files, this might be something to start from, if not then problem is somewhere else.
from k4os.compression.lz4.
Thanks for pointer. After this, i was able to pinpoint issue and fix it as well. I was running into breaking changes in .NET 6 as described here.
I am able to fix this and now legacy stream is working.
Couple of follow up question:
- What is Plan for Legacy package? Willl you keep on maintaining it for future .net release or you are thinking it to be out of support now onwards?
- We are looking to upgrade to new Stream so thought process is compress new payload with new format and continuing support for existing data read via Legacy path. Do you have any recommendation how should we provide this compatibility. One naive way is, when we write new format, we should add additional metadata stating this is new format and use this why reading it and decompress. With existing data, this metadata will not be there so use legacy decompression?
from k4os.compression.lz4.
- .Legacy assemple: It is what it is as people have old streams, so they need to read them somehow. That's it. I planning keep it working as it is for new .NET versions, but no new functionality.
- So I understand the problem of not knowing which stream it is, but also I cannot do anything: old stream is what it is, I am not able change format now. New stream is compatible with official LZ4 specification, so I cannot change it either.
Few options:
- If you store it in DB: add a column indicating which version it is
- If you store it on disk: add different extension
- If you can peek into first few bytes, you can check if it has magic number (
0x184D2204
)
from k4os.compression.lz4.
- .Legacy assemple: It is what it is as people have old streams, so they need to read them somehow. That's it. I planning keep it working as it is for new .NET versions, but no new functionality.
- So I understand the problem of not knowing which stream it is, but also I cannot do anything: old stream is what it is, I am not able change format now. New stream is compatible with official LZ4 specification, so I cannot change it either.
Few options:
- If you store it in DB: add a column indicating which version it is
- If you store it on disk: add different extension
- If you can peek into first few bytes, you can check if it has magic number (
0x184D2204
)
Can you please double click on #3, peeking into first few byte. Do you means to say 1st byte will have this magic number always in new Lz4.?
from k4os.compression.lz4.
First four bytes of LZ4 "official" stream is 0x184D2204
from k4os.compression.lz4.
Related Issues (20)
- Unity Support HOT 3
- Implicit reference to System.Runtime.CompilerServices.Unsafe appears to cause wrong version to be selected HOT 3
- Chained encoder does not produce the same result as the lz4 cli when chaining is enabled HOT 4
- Migrating from L4zNet to new implementation
- Implement frame format with K4os.Compression.LZ4 HOT 2
- Question about random access, i want to make something similar to the original dictionaryRandoimAccess.c example HOT 5
- why Length return 7 HOT 1
- Question regarding Lz4net archived repo HOT 6
- LZ4 Legacy format support HOT 2
- LZ4EncoderStream Dispose Issue HOT 3
- Compression Issue? HOT 3
- API: LZ4Codec.Decode/Encode(Stream input, Stream output) HOT 12
- LZ4 with Maui HOT 7
- Stream API CopyTo got NotSupportedException HOT 4
- LZ4DecoderStream ReadAsync calls original stream's sync Read internally HOT 2
- [Question] Block vs Stream vs Pickler to store compressed bitmaps in memory HOT 10
- Adding non compressed data at the end of the stream HOT 2
- The `int LZ4Frame#Encode` overloads don't close the frame HOT 2
- The repository contains the code with the incompatible licenses HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from k4os.compression.lz4.