Comments (5)
I'll take a look at this next week. Can you check reading blicks instead of single bytes? GetByte is least tested API.
from k4os.compression.lz4.
Interesting. So when I simply read the stream as blocks using Stream.Read()
it does not throw an exception (the streams are equivalent), this code:
private static byte[] ReadStream(Stream stream, int length, int blockSize = 1024)
{
byte[] bytes = new byte[length + blockSize];
int numBytesToRead = length;
int numBytesRead = 0;
do
{
int n = stream.Read(bytes, numBytesRead, blockSize);
numBytesRead += n;
numBytesToRead -= n;
}
while (numBytesToRead > 0);
return bytes;
}
private static void ReadBlocks(Stream decompressionStream, Stream intermediateStream)
{
byte[] decompressionBytes = ReadStream(decompressionStream, (int)intermediateStream.Length);
byte[] intermediateBytes = ReadStream(intermediateStream, (int)intermediateStream.Length);
for (int i = 0; i < decompressionBytes.Length; i++)
{
if (decompressionBytes[i] != intermediateBytes[i])
{
throw new Exception("Bytes not equal.");
}
}
}
However, when I read them using the TarInputStream
from the SharpZipLib library they are not equivalent. Namely, tarStream.GetNextEntry()
throws "Header checksum invalid" here. This is the original error that led me down the rabbit hole of comparing the streams, and interestingly TarInputStream
is calling Read()
not ReadByte()
but it's still causing problems. SharpZipLib code for that is here. If you note the comment there about "We have found EOF, and the record is not full!" that is the problem that I referenced in the original post that is causing the garbage data at the end of the stream because SharpZipLib is just returning the same bytes that were read on the previous call to ReadBlock
. My code for reading the tar archives is as follows (again, this fails on tarStream.GetNextEntry()
which is after intermediateTarStream.GetNextEntry()
so the intermediate stream did not throw the same header checksum invalid error):
private static void ReadTar(LZ4DecoderStream decompressionStream, MemoryStream intermediateStream)
{
using (var tarStream = new TarInputStream(decompressionStream, Encoding.UTF8))
using (var intermediateTarStream = new TarInputStream(intermediateStream, Encoding.UTF8))
{
TarEntry tarEntry, intermediateTarEntry = null;
while (true)
{
intermediateTarEntry = intermediateTarStream.GetNextEntry();
tarEntry = tarStream.GetNextEntry();
if (tarEntry == null || intermediateTarEntry == null)
{
if (tarEntry == null && intermediateTarEntry == null)
{
break;
}
else
{
throw new Exception("One stream ended, the other still has data.");
}
}
if (tarEntry.IsDirectory || intermediateTarEntry.IsDirectory)
{
if (tarEntry.IsDirectory && intermediateTarEntry.IsDirectory)
{
continue;
}
else
{
throw new Exception("One stream found a directory and the other didn't");
}
}
using (var originalEntryContents = new MemoryStream())
using (var intermediateEntryContents = new MemoryStream())
{
tarStream.CopyEntryContents(originalEntryContents);
intermediateTarStream.CopyEntryContents(intermediateEntryContents);
ReadByteByByte(originalEntryContents, intermediateEntryContents);
}
}
}
}
from k4os.compression.lz4.
I did some testing with Tar streams and it works fine.
I mean I understand that it might be a bug but it is not a general problem, there must be something specific to data. I actually guess that it might be something about how SharpZipLib call LZ4Stream (for example, tries to read -1 bytes), because I understand that if you decompress first and then use TarInputStream it is all fine? it fails ONLY if TarInputStream reads directly from LZ4Stream?
Anyway, I think without actual files it becomes a wild goose chase. Maybe you can generate fake data having same problem?
from k4os.compression.lz4.
@RLashofRegas any luck reproducing it?
from k4os.compression.lz4.
Sorry been super busy with other things but no. I will try next chance I get but I had tried previously and was not able to reproduce with other files.
from k4os.compression.lz4.
Related Issues (20)
- h264 nal unit invalid HOT 2
- 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
- InvalidDataException: LZ4 frame magic number expected HOT 10
- 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.