public void SingleBlockXxh32UsingSpanMatchesTheirs(string text)
        {
            var input    = Encoding.UTF8.GetBytes(text);
            var expected = Theirs32(input);
            var actual   = XXH32.DigestOf(input.AsSpan());

            Assert.Equal(expected, actual);
        }
Example #2
0
        public static void Create(object sender, EventArgs e)
        {
            var tsi = sender as ToolStripMenuItem;

            FileStream openFile = null;

            switch (tsi.Tag)
            {
            case Hash.CRC32:
                ;
                var ofd = new OpenFileDialog
                {
                    Title  = Hash.CRC32.ToString(),
                    Filter = "All Files (*.*)|*.*"
                };

                if (ofd.ShowDialog() != DialogResult.OK)
                {
                    break;
                }
                openFile = File.OpenRead(ofd.FileName);
                MessageBox.Show($"0x{Crc32.Create(new BinaryReaderX(openFile).ReadBytes((int)openFile.Length)):X8}", tsi?.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                openFile.Close();
                break;

            case Hash.SHA256:
                ofd = new OpenFileDialog
                {
                    Title  = Hash.SHA256.ToString(),
                    Filter = "All Files (*.*)|*.*"
                };

                if (ofd.ShowDialog() != DialogResult.OK)
                {
                    break;
                }
                openFile = File.OpenRead(ofd.FileName);
                MessageBox.Show($"{SHA256.Create(new BinaryReaderX(openFile).ReadBytes((int)openFile.Length)):X}", tsi?.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                openFile.Close();
                break;

            case Hash.XXH32:
                ofd = new OpenFileDialog
                {
                    Title  = Hash.XXH32.ToString(),
                    Filter = "All Files (*.*)|*.*"
                };

                if (ofd.ShowDialog() != DialogResult.OK)
                {
                    break;
                }
                openFile = File.OpenRead(ofd.FileName);
                MessageBox.Show($"0x{XXH32.Create(new BinaryReaderX(openFile).ReadBytes((int)openFile.Length)):X8}", tsi?.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                openFile.Close();
                break;
            }
        }
Example #3
0
        private void ReadFrame()
        {
            FlushPeek();

            var magic = TryPeek32();

            if (magic != 0x184D2204)
            {
                throw MagicNumberExpected();
            }

            FlushPeek();

            var FLG_BD = Peek16();

            var FLG = FLG_BD & 0xFF;
            var BD  = (FLG_BD >> 8) & 0xFF;

            var version = (FLG >> 6) & 0x11;

            if (version != 1)
            {
                throw UnknownFrameVersion(version);
            }

            var blockChaining   = ((FLG >> 5) & 0x01) == 0;
            var blockChecksum   = ((FLG >> 4) & 0x01) != 0;
            var hasContentSize  = ((FLG >> 3) & 0x01) != 0;
            var contentChecksum = ((FLG >> 2) & 0x01) != 0;
            var hasDictionary   = (FLG & 0x01) != 0;
            var blockSizeCode   = (BD >> 4) & 0x07;

            var contentLength = hasContentSize ? (long?)Peek64() : null;
            var dictionaryId  = hasDictionary ? (uint?)Peek32() : null;

            var actualHC   = (byte)(XXH32.DigestOf(_buffer16, 0, _index16) >> 8);
            var expectedHC = Peek8();

            if (actualHC != expectedHC)
            {
                throw InvalidHeaderChecksum();
            }

            var blockSize = MaxBlockSize(blockSizeCode);

            if (hasDictionary)
            {
                throw NotImplemented(
                          "Predefined dictionaries feature is not implemented");               // Write32(dictionaryId);
            }
            // ReSharper disable once ExpressionIsAlwaysNull
            _frameInfo = new LZ4Descriptor(
                contentLength, contentChecksum, blockChaining, blockChecksum, dictionaryId,
                blockSize);
            _decoder = _decoderFactory(_frameInfo);
            _buffer  = new byte[blockSize];
        }
        public void HashAlgorithmWrapperReturnsSameResults(int seed, int length)
        {
            var bytes = new byte[length];

            new Random(seed).NextBytes(bytes);

            var expected = XXH32.DigestOf(bytes, 0, bytes.Length);
            var actual   = new XXH32().AsHashAlgorithm().ComputeHash(bytes);

            Assert.Equal(expected, BitConverter.ToUInt32(actual, 0));
        }
Example #5
0
        public static UInt32 xxHash(this String value)
        {
#if DEBUG
            var hash = XXH32.DigestOf(Encoding.UTF8.GetBytes(value));
            if (!_cache.ContainsKey(hash))
            {
                _cache[hash] = value;
            }
#endif
            return(XXH32.DigestOf(Encoding.UTF8.GetBytes(value)));
        }
        static string HashPath(string path)
        {
            string file = Path.GetFileName(path);
            string dir  = Path.GetDirectoryName(path);

            file = $"{XXH32.DigestOf(Encoding.UTF8.GetBytes(file)):x8}";
            if (string.IsNullOrEmpty(dir))
            {
                return(file);
            }
            return(Path.Combine(HashPath(dir), file));
        }
Example #7
0
        private void ReadFrame()
        {
            Read0();

            var magic = TryRead32();

            if (magic != 0x184D2204)
            {
                throw MagicNumberExpected();
            }

            Read0();

            var FLG_BD = Read16();

            var FLG = FLG_BD & 0xFF;
            var BD  = (FLG_BD >> 8) & 0xFF;

            var version = (FLG >> 6) & 0x11;

            if (version != 1)
            {
                throw UnknownFrameVersion(version);
            }

            var blockChaining   = ((FLG >> 5) & 0x01) == 0;
            var blockChecksum   = ((FLG >> 4) & 0x01) != 0;
            var hasContentSize  = ((FLG >> 3) & 0x01) != 0;
            var contentChecksum = ((FLG >> 2) & 0x01) != 0;

            var blockSizeCode = (BD >> 4) & 0x07;

            var contentLength = hasContentSize ? (long?)Read64() : null;

            var actualHC   = (byte)(XXH32.DigestOf(_buffer16, 0, _index16) >> 8);
            var expectedHC = Read8();

            if (actualHC != expectedHC)
            {
                throw InvalidHeaderChecksum();
            }

            var blockSize = MaxBlockSize(blockSizeCode);

            _frameInfo = new LZ4FrameInfo(contentLength, contentChecksum, blockChaining, blockChecksum, blockSize);
            _decoder   = _decoderFactory(_frameInfo);
            _buffer    = new byte[blockSize];
        }
        public void EveryCallToDigestReturnsSameHash(int seed, int length)
        {
            var random = new Random(seed);
            var bytes  = new byte[length];

            random.NextBytes(bytes);

            var expected = XXH32.DigestOf(bytes, 0, bytes.Length);

            var transform = new XXH32();

            transform.Update(bytes, 0, length);

            for (var i = 0; i < 100; i++)
            {
                Assert.Equal(expected, transform.Digest());
            }
        }
Example #9
0
        private static uint Checksum(Stream file)
        {
            var hash   = new XXH32();
            var buffer = new byte[0x10000];

            while (true)
            {
                var read = file.Read(buffer, 0, buffer.Length);
                if (read == 0)
                {
                    break;
                }

                hash.Update(buffer, 0, read);
            }

            return(hash.Digest());
        }
        private void WriteFrame()
        {
            Stash32(0x184D2204);
            FlushStash();

            const int versionCode     = 0x01;
            var       blockChaining   = _descriptor.Chaining;
            var       blockChecksum   = _descriptor.BlockChecksum;
            var       contentChecksum = _descriptor.ContentChecksum;
            var       hasContentSize  = _descriptor.ContentLength.HasValue;
            var       hasDictionary   = _descriptor.Dictionary.HasValue;

            var FLG =
                (versionCode << 6) |
                ((blockChaining ? 0 : 1) << 5) |
                ((blockChecksum ? 1 : 0) << 4) |
                ((hasContentSize ? 1 : 0) << 3) |
                ((contentChecksum ? 1 : 0) << 2) |
                (hasDictionary ? 1 : 0);

            var blockSize = _descriptor.BlockSize;

            var BD = MaxBlockSizeCode(blockSize) << 4;

            Stash16((ushort)((FLG & 0xFF) | (BD & 0xFF) << 8));

            if (hasContentSize)
            {
                throw NotImplemented(
                          "ContentSize feature is not implemented");               // Write64(contentSize);
            }
            if (hasDictionary)
            {
                throw NotImplemented(
                          "Predefined dictionaries feature is not implemented");               // Write32(dictionaryId);
            }
            var HC = (byte)(XXH32.DigestOf(_buffer16, 0, _index16) >> 8);

            Stash8(HC);
            FlushStash();

            _encoder = CreateEncoder();
            _buffer  = new byte[LZ4Codec.MaximumOutputSize(blockSize)];
        }
        public unsafe void EmptyHash()
        {
            var input = Array.Empty <byte>();

            var expected = Theirs32(input);

            var actual1 = XXH32.DigestOf(input, 0, input.Length);

            Assert.Equal(expected, actual1);

            fixed(byte *inputP = input)
            {
                var actual2 = XXH32.DigestOf(inputP, 0);

                Assert.Equal(expected, actual2);
            }

            var actual3 = XXH32.EmptyHash;

            Assert.Equal(expected, actual3);
        }
        public void RestartableHashReturnsSameResultsAsSingleBlock(int seed, int length, int chunk)
        {
            var random = new Random(seed);
            var bytes  = new byte[length];

            random.NextBytes(bytes);

            var expected = XXH32.DigestOf(bytes, 0, bytes.Length);

            var transform = new XXH32();
            var index     = 0;

            while (index < length)
            {
                var l = Math.Min(chunk, length - index);
                transform.Update(bytes, index, l);
                index += l;
            }

            var actual = transform.Digest();

            Assert.Equal(expected, actual);
        }
Example #13
0
 string HashHex(string s)
 {
     return($"{XXH32.DigestOf(Encoding.UTF8.GetBytes(s)):x8}");
 }
Example #14
0
 public uint HashParameters()
 {
     return(XXH32.DigestOf(Encoding.UTF8.GetBytes(ResamplerName + " " + SourceFile + " " + GetResamplerExeArgs())));
 }
Example #15
0
 private protected uint DigestOfStash(int offset = 0) =>
 XXH32.DigestOf(Stash.AsSpan(offset));
Example #16
0
        public static byte[] Decompress(Stream instream, bool precedingSize = false)
        {
            var ms = new MemoryStream();

            using (var bw = new BinaryWriterX(ms, true))
                using (var br2 = new BinaryReaderX(ms, true))
                    using (var br = new BinaryReaderX(instream, true))
                    {
                        uint decompressedSize = 0;
                        if (precedingSize)
                        {
                            decompressedSize = br.ReadUInt32();
                        }

                        var magic = br.ReadUInt32();
                        if (magic != 0x184D2204)
                        {
                            throw new Exception("LZ4 magic isn't valid.");
                        }

                        var flg = br.ReadByte();
                        if ((flg >> 6) != 1)
                        {
                            throw new Exception($"Unsupported Version {flg >> 6}.");
                        }

                        bool blockIndep      = ((flg >> 5) & 1) == 1;
                        bool blockChecksum   = ((flg >> 4) & 1) == 1;
                        bool contentSize     = ((flg >> 3) & 1) == 1;
                        bool contentChecksum = ((flg >> 2) & 1) == 1;
                        bool dictID          = (flg & 1) == 1;

                        var          bd           = br.ReadByte();
                        BlockMaxSize blockMaxSize = (BlockMaxSize)((bd >> 4) & 0x7);

                        if (contentSize)
                        {
                            br.ReadUInt64();
                        }

                        if (dictID)
                        {
                            br.ReadInt32();
                        }

                        var headerLength = 3 + ((contentSize) ? 8 : 0) + ((dictID) ? 4 : 0);
                        br.BaseStream.Position -= headerLength - 1;
                        var header = br.ReadBytes(headerLength - 1);
                        var hc     = br.ReadByte();
                        if (((XXH32.Create(header) >> 8) & 0xFF) != hc)
                        {
                            throw new Exception("Header Checksum is invalid.");
                        }

                        //Blocks
                        while (br.BaseStream.Position < br.BaseStream.Length - ((contentChecksum) ? 4 : 0))
                        {
                            var blockSize = br.ReadInt32();
                            while (blockSize != 0)
                            {
                                var compData = br.ReadBytes(blockSize);
                                var decomp   = DecompressBlock(compData);

                                if (blockChecksum)
                                {
                                    var check = br.ReadUInt32();
                                    if (check != XXH32.Create(compData))
                                    {
                                        throw new Exception("One block checksum was invalid.");
                                    }
                                }

                                bw.Write(decomp);

                                blockSize = br.ReadInt32();
                            }
                        }

                        if (contentChecksum)
                        {
                            br2.BaseStream.Position = 0;
                            if (br.ReadUInt32() != XXH32.Create(br2.ReadAllBytes()))
                            {
                                throw new Exception("Decompressed data is corrupted.");
                            }
                        }

                        if (precedingSize && decompressedSize != br2.BaseStream.Length)
                        {
                            throw new Exception("Preceding decompressed size doesn't match.");
                        }
                    }

            return(ms.ToArray());
        }
Example #17
0
        public static byte[] Compress(Stream instream, bool precedingSize = false, bool blockIndep = true, bool blockChecksum = false, bool contentSize = false, bool contentChecksum = true, bool dictID = false, BlockMaxSize blockMaxSize = BlockMaxSize.MB1)
        {
            var ms = new MemoryStream();

            using (var br = new BinaryReaderX(ms, true))
                using (var bw = new BinaryWriterX(ms, true))
                {
                    if (precedingSize)
                    {
                        bw.Write((uint)instream.Length);
                    }

                    #region Write LZ4 Frame header
                    //Magic
                    bw.Write(0x184D2204);

                    //FLG Byte
                    byte flg = 0x40;
                    if (blockIndep)
                    {
                        flg |= 0x20;
                    }
                    if (blockChecksum)
                    {
                        flg |= 0x10;
                    }
                    if (contentSize)
                    {
                        flg |= 0x08;
                    }
                    if (contentChecksum)
                    {
                        flg |= 0x04;
                    }
                    if (dictID)
                    {
                        flg |= 0x01;
                    }
                    bw.Write(flg);

                    //BD Byte
                    byte bd = (byte)((byte)blockMaxSize << 4);
                    bw.Write(bd);

                    //Content Size
                    if (contentSize)
                    {
                        bw.Write((long)instream.Length);
                    }

                    //Dictionary - STUB since Dictionary usage isn't understood in LZ4
                    if (dictID)
                    {
                        bw.Write(0);
                    }

                    //XXHash32
                    br.BaseStream.Position = 4;
                    var  fieldDesc = br.ReadBytes((int)br.BaseStream.Length - 4);
                    byte hc        = (byte)((XXH32.Create(fieldDesc) >> 8) & 0xFF);
                    bw.Write(hc);
                    #endregion

                    //Write Block Data
                    var comp = LZ4Codec.Encode(new BinaryReaderX(instream, true).ReadAllBytes(), 0, (int)instream.Length);
                    bw.Write(comp.Length);
                    bw.Write(comp);

                    if (blockChecksum)
                    {
                        bw.Write(XXH32.Create(comp));
                    }

                    //End Mark
                    bw.Write(0);

                    //Content checksum
                    if (contentChecksum)
                    {
                        bw.Write(XXH32.Create(new BinaryReaderX(instream, true).ReadAllBytes()));
                    }
                }

            return(ms.ToArray());
        }