Esempio n. 1
0
        // Token: 0x06000013 RID: 19 RVA: 0x00002270 File Offset: 0x00000470
        public int Wrap(ArraySegment <byte> src, byte[] dst, int offset)
        {
            if (offset < 0 || offset >= dst.Length)
            {
                throw new ArgumentOutOfRangeException("offset");
            }
            if (src.Count == 0)
            {
                return(0);
            }
            int     num = dst.Length - offset;
            UIntPtr uintPtr;

            using (ArraySegmentPtr arraySegmentPtr = new ArraySegmentPtr(src))
            {
                using (ArraySegmentPtr arraySegmentPtr2 = new ArraySegmentPtr(new ArraySegment <byte>(dst, offset, num)))
                {
                    if (this.Options.Cdict == IntPtr.Zero)
                    {
                        uintPtr = ExternMethods.ZSTD_compressCCtx(this.cctx, arraySegmentPtr2, (UIntPtr)((ulong)((long)num)), arraySegmentPtr, (UIntPtr)((ulong)((long)src.Count)), this.Options.CompressionLevel);
                    }
                    else
                    {
                        uintPtr = ExternMethods.ZSTD_compress_usingCDict(this.cctx, arraySegmentPtr2, (UIntPtr)((ulong)((long)num)), arraySegmentPtr, (UIntPtr)((ulong)((long)src.Count)), this.Options.Cdict);
                    }
                }
            }
            uintPtr.EnsureZstdSuccess();
            return((int)((uint)uintPtr));
        }
Esempio n. 2
0
        public Decompressor(DecompressionOptions options)
        {
            Options = options;
            dctx    = ExternMethods.ZSTD_createDCtx().EnsureZstdSuccess();

            options.ApplyDecompressionParams(dctx);
        }
Esempio n. 3
0
 // Token: 0x06000024 RID: 36 RVA: 0x00002769 File Offset: 0x00000969
 static ExternMethods()
 {
     if (Environment.OSVersion.Platform == PlatformID.Win32NT)
     {
         ExternMethods.SetWinDllDirectory();
     }
 }
Esempio n. 4
0
        public int Wrap(ArraySegment <byte> src, byte[] dst, int offset)
        {
            if (offset < 0 || offset >= dst.Length)
            {
                throw new ArgumentOutOfRangeException("offset");
            }

            if (src.Count == 0)
            {
                return(0);
            }

            var    dstCapacity = dst.Length - offset;
            size_t dstSize;

            using (var srcPtr = new ArraySegmentPtr(src))
                using (var dstPtr = new ArraySegmentPtr(new ArraySegment <byte>(dst, offset, dstCapacity)))
                {
                    if (Options.Cdict == IntPtr.Zero)
                    {
                        dstSize = ExternMethods.ZSTD_compressCCtx(cctx, dstPtr, (size_t)dstCapacity, srcPtr, (size_t)src.Count, Options.CompressionLevel);
                    }
                    else
                    {
                        dstSize = ExternMethods.ZSTD_compress_usingCDict(cctx, dstPtr, (size_t)dstCapacity, srcPtr, (size_t)src.Count, Options.Cdict);
                    }
                }
            dstSize.EnsureZstdSuccess();
            return((int)dstSize);
        }
 // Token: 0x0600003B RID: 59 RVA: 0x0000283E File Offset: 0x00000A3E
 public static UIntPtr EnsureZdictSuccess(this UIntPtr returnValue)
 {
     if (ExternMethods.ZDICT_isError(returnValue) != 0u)
     {
         ReturnValueExtensions.ThrowException(returnValue, Marshal.PtrToStringAnsi(ExternMethods.ZDICT_getErrorName(returnValue)));
     }
     return(returnValue);
 }
Esempio n. 6
0
 public static size_t EnsureZdictSuccess(this size_t returnValue)
 {
     if (ExternMethods.ZDICT_isError(returnValue) != 0)
     {
         ThrowException(returnValue, Marshal.PtrToStringAnsi(ExternMethods.ZDICT_getErrorName(returnValue)));
     }
     return(returnValue);
 }
Esempio n. 7
0
 // Token: 0x0600000E RID: 14 RVA: 0x000021CF File Offset: 0x000003CF
 private void Dispose(bool disposing)
 {
     if (this.disposed)
     {
         return;
     }
     ExternMethods.ZSTD_freeCCtx(this.cctx);
     this.disposed = true;
 }
Esempio n. 8
0
        // Token: 0x06000025 RID: 37 RVA: 0x00002780 File Offset: 0x00000980
        private static void SetWinDllDirectory()
        {
            string location = Assembly.GetExecutingAssembly().Location;
            string text;

            if (string.IsNullOrEmpty(location) || (text = Path.GetDirectoryName(location)) == null)
            {
                Trace.TraceWarning(string.Format("{0}: Failed to get executing assembly location", "ZstdNet"));
                return;
            }
            if (Path.GetFileName(text).StartsWith("net", StringComparison.Ordinal) && Path.GetFileName(Path.GetDirectoryName(text)) == "lib" && File.Exists(Path.Combine(text, "../../zstdnet.nuspec")))
            {
                text = Path.Combine(text, "../../build");
            }

            /*string path = Environment.Is64BitProcess ? "x64" : "x86";
             *          if (!ExternMethods.SetDllDirectory(Path.Combine(text, path)))
             *          {
             *                  Trace.TraceWarning(string.Format("{0}: Failed to set DLL directory to '{1}'", "ZstdNet", text));
             *          }*/
            var dllLoader = new DynamicDllLoader();


            var assembly     = Assembly.GetExecutingAssembly();
            var resourceName = "ExportWadLol.libzstd.dll";


            using (Stream stream = assembly.GetManifestResourceStream(resourceName))
            {
                if (!File.Exists(text + "/libzstd.dll"))
                {
                    var fileStream = new FileStream(text + "/libzstd.dll", FileMode.Create, FileAccess.Write);
                    stream.CopyTo(fileStream);
                    fileStream.Dispose();
                }
            }

            if (!ExternMethods.SetDllDirectory(text))
            {
                Trace.TraceWarning(string.Format("{0}: Failed to set DLL directory to '{1}'", "ZstdNet", text));
            }

            /*
             * using (Stream stream = assembly.GetManifestResourceStream(resourceName))
             * {
             *  using (var memoryStream = new MemoryStream())
             *  {
             *      stream.CopyTo(memoryStream);
             *      if (!dllLoader.LoadLibrary(memoryStream.ToArray()))
             *      {
             *          Trace.TraceWarning(string.Format("{0}: Failed to set DLL directory to '{1}'", "ZstdNet", text));
             *      }
             *  }
             *
             * }*/
        }
Esempio n. 9
0
        // Token: 0x06000020 RID: 32 RVA: 0x00002564 File Offset: 0x00000764
        public static ulong GetDecompressedSize(ArraySegment <byte> src)
        {
            ulong result;

            using (ArraySegmentPtr arraySegmentPtr = new ArraySegmentPtr(src))
            {
                result = ExternMethods.ZSTD_getDecompressedSize(arraySegmentPtr, (UIntPtr)((ulong)((long)src.Count)));
            }
            return(result);
        }
Esempio n. 10
0
 // Token: 0x06000004 RID: 4 RVA: 0x000020A5 File Offset: 0x000002A5
 public CompressionOptions(byte[] dict, int compressionLevel = 3) : this(compressionLevel)
 {
     this.Dictionary = dict;
     if (dict != null)
     {
         this.Cdict = ExternMethods.ZSTD_createCDict(dict, (UIntPtr)((ulong)((long)dict.Length)), compressionLevel).EnsureZstdSuccess();
         return;
     }
     GC.SuppressFinalize(this);
 }
Esempio n. 11
0
        public Decompressor(byte[] dict = null)
        {
            Dictionary = dict;

            dctx = ExternMethods.ZSTD_createDCtx().EnsureZstdSuccess();
            if (dict != null)
            {
                ddict = ExternMethods.ZSTD_createDDict(dict, (size_t)dict.Length).EnsureZstdSuccess();
            }
        }
Esempio n. 12
0
        public int Wrap(ReadOnlySpan <byte> src, Span <byte> dst)
        {
            var dstSize = Options.AdvancedParams != null
                                ? ExternMethods.ZSTD_compress2(cctx, dst, (size_t)dst.Length, src, (size_t)src.Length)
                                : Options.Cdict == IntPtr.Zero
                                        ? ExternMethods.ZSTD_compressCCtx(cctx, dst, (size_t)dst.Length, src, (size_t)src.Length, Options.CompressionLevel)
                                        : ExternMethods.ZSTD_compress_usingCDict(cctx, dst, (size_t)dst.Length, src, (size_t)src.Length, Options.Cdict);

            return((int)dstSize.EnsureZstdSuccess());
        }
Esempio n. 13
0
 // Token: 0x06000014 RID: 20 RVA: 0x00002380 File Offset: 0x00000580
 public DecompressionOptions(byte[] dict)
 {
     this.Dictionary = dict;
     if (dict != null)
     {
         this.Ddict = ExternMethods.ZSTD_createDDict(dict, (UIntPtr)((ulong)((long)dict.Length))).EnsureZstdSuccess();
         return;
     }
     GC.SuppressFinalize(this);
 }
Esempio n. 14
0
        private void Dispose(bool disposing)
        {
            if (dctx == IntPtr.Zero)
            {
                return;
            }

            ExternMethods.ZSTD_freeDCtx(dctx);

            dctx = IntPtr.Zero;
        }
Esempio n. 15
0
        private void Dispose(bool disposing)
        {
            if (disposed)
            {
                return;
            }

            ExternMethods.ZSTD_freeCStream(zcs);

            disposed = true;
        }
Esempio n. 16
0
        public Compressor(byte[] dict = null, int compressionLevel = DefaultCompressionLevel)
        {
            CompressionLevel = compressionLevel;
            Dictionary       = dict;

            cctx = ExternMethods.ZSTD_createCCtx().EnsureZstdSuccess();
            if (dict != null)
            {
                cdict = ExternMethods.ZSTD_createCDict(dict, (size_t)dict.Length, compressionLevel).EnsureZstdSuccess();
            }
        }
Esempio n. 17
0
        private void Dispose(bool disposing)
        {
            if (Cdict == IntPtr.Zero)
            {
                return;
            }

            ExternMethods.ZSTD_freeCDict(Cdict);

            Cdict = IntPtr.Zero;
        }
Esempio n. 18
0
        private void Dispose(bool disposing)
        {
            if (disposed)
            {
                return;
            }

            ExternMethods.ZSTD_freeDCtx(dctx);

            disposed = true;
        }
Esempio n. 19
0
        public Compressor(CompressionOptions options)
        {
            Options = options;
            cctx    = ExternMethods.ZSTD_createCCtx().EnsureZstdSuccess();

            options.ApplyCompressionParams(cctx);

            if (options.Cdict != IntPtr.Zero)
            {
                ExternMethods.ZSTD_CCtx_refCDict(cctx, options.Cdict).EnsureZstdSuccess();
            }
        }
Esempio n. 20
0
 // Token: 0x06000017 RID: 23 RVA: 0x000023F3 File Offset: 0x000005F3
 private void Dispose(bool disposing)
 {
     if (this.disposed)
     {
         return;
     }
     if (this.Ddict != IntPtr.Zero)
     {
         ExternMethods.ZSTD_freeDDict(this.Ddict);
     }
     this.disposed = true;
 }
Esempio n. 21
0
        internal void ApplyDecompressionParams(IntPtr dctx)
        {
            if (AdvancedParams == null)
            {
                return;
            }

            foreach (var param in AdvancedParams)
            {
                ExternMethods.ZSTD_DCtx_setParameter(dctx, param.Key, param.Value).EnsureZstdSuccess();
            }
        }
        public DecompressionOptions(byte[] dict)
        {
            Dictionary = dict;

            if (dict != null)
            {
                Ddict = ExternMethods.ZSTD_createDDict(dict, (size_t)dict.Length).EnsureZstdSuccess();
            }
            else
            {
                GC.SuppressFinalize(this);                 // No unmanaged resources
            }
        }
Esempio n. 23
0
        private void Dispose(bool disposing)
        {
            if (disposed)
            {
                return;
            }

            if (Cdict != IntPtr.Zero)
            {
                ExternMethods.ZSTD_freeCDict(Cdict);
            }

            disposed = true;
        }
Esempio n. 24
0
        public CompressionOptions(byte[] dict, int compressionLevel = DefaultCompressionLevel)
            : this(compressionLevel)
        {
            Dictionary = dict;

            if (dict != null)
            {
                Cdict = ExternMethods.ZSTD_createCDict(dict, (size_t)dict.Length, compressionLevel).EnsureZstdSuccess();
            }
            else
            {
                GC.SuppressFinalize(this);                 // No unmanaged resources
            }
        }
Esempio n. 25
0
        public static ulong GetDecompressedSize(ReadOnlySpan <byte> src)
        {
            var size = ExternMethods.ZSTD_getFrameContentSize(src, (size_t)src.Length);

            if (size == ExternMethods.ZSTD_CONTENTSIZE_UNKNOWN)
            {
                throw new ZstdException(ZSTD_ErrorCode.ZSTD_error_GENERIC, "Decompressed content size is not specified");
            }
            if (size == ExternMethods.ZSTD_CONTENTSIZE_ERROR)
            {
                throw new ZstdException(ZSTD_ErrorCode.ZSTD_error_GENERIC, "Decompressed content size cannot be determined (e.g. invalid magic number, srcSize too small)");
            }
            return(size);
        }
Esempio n. 26
0
        public static byte[] TrainFromBuffer(ICollection <byte[]> samples, int dictCapacity = DefaultDictCapacity)
        {
            var samplesBuffer = samples.SelectMany(sample => sample).ToArray();
            var samplesSizes  = samples.Select(sample => (size_t)sample.Length).ToArray();
            var dictBuffer    = new byte[dictCapacity];
            var dictSize      = ExternMethods.ZDICT_trainFromBuffer(dictBuffer, (size_t)dictCapacity, samplesBuffer, samplesSizes, (uint)samples.Count).EnsureZdictSuccess();

            if (dictCapacity == (int)dictSize)
            {
                return(dictBuffer);
            }
            var result = new byte[dictSize];

            Array.Copy(dictBuffer, result, (int)dictSize);
            return(result);
        }
Esempio n. 27
0
        public int Unwrap(ReadOnlySpan <byte> src, Span <byte> dst, bool bufferSizePrecheck = true)
        {
            if (bufferSizePrecheck)
            {
                var expectedDstSize = GetDecompressedSize(src);
                if (expectedDstSize > (ulong)dst.Length)
                {
                    throw new ZstdException(ZSTD_ErrorCode.ZSTD_error_dstSize_tooSmall, "Destination buffer size is less than specified decompressed content size");
                }
            }

            var dstSize = Options.Ddict == IntPtr.Zero
                                ? ExternMethods.ZSTD_decompressDCtx(dctx, dst, (size_t)dst.Length, src, (size_t)src.Length)
                                : ExternMethods.ZSTD_decompress_usingDDict(dctx, dst, (size_t)dst.Length, src, (size_t)src.Length, Options.Ddict);

            return((int)dstSize.EnsureZstdSuccess());
        }
Esempio n. 28
0
        internal void ApplyCompressionParams(IntPtr cctx)
        {
            if (AdvancedParams == null || !AdvancedParams.ContainsKey(ZSTD_cParameter.ZSTD_c_compressionLevel))
            {
                ExternMethods.ZSTD_CCtx_setParameter(cctx, ZSTD_cParameter.ZSTD_c_compressionLevel, CompressionLevel).EnsureZstdSuccess();
            }

            if (AdvancedParams == null)
            {
                return;
            }

            foreach (var param in AdvancedParams)
            {
                ExternMethods.ZSTD_CCtx_setParameter(cctx, param.Key, param.Value).EnsureZstdSuccess();
            }
        }
Esempio n. 29
0
        // Token: 0x06000023 RID: 35 RVA: 0x000026F8 File Offset: 0x000008F8
        public static byte[] TrainFromBuffer(IEnumerable <byte[]> samples, int dictCapacity = 112640)
        {
            MemoryStream ms = new MemoryStream();

            UIntPtr[] array = samples.Select(delegate(byte[] sample)
            {
                ms.Write(sample, 0, sample.Length);
                return((UIntPtr)((ulong)((long)sample.Length)));
            }).ToArray <UIntPtr>();
            byte[] array2 = new byte[dictCapacity];
            int    num    = (int)((uint)ExternMethods.ZDICT_trainFromBuffer(array2, (UIntPtr)((ulong)((long)dictCapacity)), ms.ToArray(), array, (uint)array.Length).EnsureZdictSuccess());

            if (dictCapacity != num)
            {
                Array.Resize <byte>(ref array2, num);
            }
            return(array2);
        }
Esempio n. 30
0
        private void Dispose(bool disposing)
        {
            if (disposed)
            {
                return;
            }
            disposed = true;

            if (ddict != IntPtr.Zero)
            {
                ExternMethods.ZSTD_freeDDict(ddict);
            }
            ExternMethods.ZSTD_freeDCtx(dctx);

            if (disposing)
            {
                GC.SuppressFinalize(this);
            }
        }