Code Monkey home page Code Monkey logo

zstd's People

Contributors

bastianeicher avatar tyler-in avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

zstd's Issues

Decompression stream Read always returns 0 after the first read

When using ZStdDecompressStream to decompress the input data, it's Read method always returns zero after the first read. As per the Stream documentation, a zero return value indicates the end-of-stream. Therefore, it becomes impossible to extract the entire decompressed contents.

Bug can be reproduced by the following code

static void Main(string[] args)
{
    var input = new byte[1024 * 4];
    var compressed = Compress(input);
    Console.WriteLine("Input Length: {0}, Compressed Length: {1}", input.Length, compressed.Length);

    var decompressed = Decompress(compressed);
    Console.WriteLine("Compressed Length: {0}, Decompressed Length: {1}", compressed.Length, decompressed.Length);
}

static byte[] Compress(byte[] input)
{
    using var compressed = new MemoryStream();
    using var compressStream = new ZStdCompressStream(compressed);
    compressStream.Write(input, 0, input.Length);
    compressStream.Flush();
    return compressed.ToArray();
}

static byte[] Decompress(byte[] input)
{
    using var compressed = new MemoryStream(input);
    using var decompressStream = new ZStdDecompressStream(compressed);

    using var output = new MemoryStream();
    var buffer = new byte[1024];
    var bytesRead = 0;

    //This while loop runs only once regardless of the length of buffer
    while ((bytesRead = decompressStream.Read(buffer, 0, buffer.Length)) > 0)
        output.Write(buffer, 0, bytesRead);
    
    return output.ToArray();
}

The program output:

Input Length: 4096, Compressed Length: 18
Compressed Length: 18, Decompressed Length: 1024

OS: Windows 10
Platform: .NET 5.0
Visual Studio: 16.10.4
ImpromptuNinjas.ZStd: 1.4.5.5

Add support for ZDICT_finalizeDictionary for custom dictionary contents

The zstd lib recently stabilized ZDICT_finalizeDictionary() which can be used to create a dictionary with pre-selected contents rather than relying on the default training algorithm. This can be very useful for file types where there is a known repeating sequence that default training does not find.

The below additions should be all that is needed to add support. I would create a PR but was having some git issues due to the repo being over it's LFS transfer limit.

edit: was able to work around LFS issues. PR opened

Extensions Read buffer too small bug

Hi, in the read method of Extensions.cs:

    internal static int Read(this Stream stream, Span<byte> bytes) {
      var count = bytes.Length;
      var copy = ArrayPool<byte>.Shared.Rent(count);
      try {
        var read = stream.Read(copy, 0, count);
        copy.CopyTo(bytes);

        return read;
      }
      finally {
        ArrayPool<byte>.Shared.Return(copy);
      }
    }

if the length of bytes is not a power of 2, then the rented array copy (which is only guaranteed to be at least count bytes in length) will be bigger than count, and the line copy.CopyTo(bytes) will throw. I believe you can instead

...
copy.AsSpan(0, read).CopyTo(bytes);
...

Double dispose // flush

Hi, I think there's a possible bug when attempting to dispose a (de)compress stream twice.

    protected override unsafe void Dispose(bool disposing) {
      Flush();

      if (disposing && !Disposed) {
        Decompressor?.Dispose();
        Marshal.FreeHGlobal((IntPtr) Input.GetPinnedPointer());
        Disposed = true;
      }
...

Flush() uses Input, which is freed on the first call to Dispose. Dispose should be safe to call twice. I think you can just replace it with

    protected override unsafe void Dispose(bool disposing) {
      if (!Disposed) {
          Flush();
          if (disposing) {
              Decompressor?.Dispose();
              Marshal.FreeHGlobal((IntPtr) Input.GetPinnedPointer());
              Disposed = true;
          }
      }
...

Does this seem sensible?

Sign assembly with strong name

I would like to use your library in my project (a cross-platform package manager, called Zero Install). However, it targets .NET Framework 4.5 and uses strong-name signed assemblies.

Could you add strong name signing to your assemblies? The official guidance from Microsoft on topic says strong-names are not usually needed anymore but recommends signing any public libraries for compatibility.

I could provide a small PR for this if you're interested.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.