Exemple #1
0
        public void SingleBlockXxh32UsingSpanMatchesTheirs(string text)
        {
            var input    = System.Text.Encoding.UTF8.GetBytes(text);
            var expected = Theirs32(input);
            var actual   = XXHash32.DigestOf(input.AsSpan());

            Assert.Equal(expected, actual);
        }
Exemple #2
0
        public void HashAlgorithmWrapperReturnsSameResults(int seed, int length)
        {
            var bytes = new byte[length];

            new Random(seed).NextBytes(bytes);

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

            Assert.Equal(expected, BitConverter.ToUInt32(actual, 0));
        }
		private void ReadFrame()
		{
			Read0();

			uint? magic = TryRead32();
			if (magic != 0x184D2204)
				throw new InvalidDataException(RS.ExpectLZ4MagicNumber);

            Read0();

			ushort flgBd = Read16();

			int flg = flgBd & 0xFF;
			int bd = (flgBd >> 8) & 0xFF;

			int version = (flg >> 6) & 0x11;

			if (version != 1)
				throw new InvalidDataException(string.Format(RS.LZ4VersionNotSupported, version)); 

			bool blockChaining = ((flg >> 5) & 0x01) == 0;
            bool blockChecksum = ((flg >> 4) & 0x01) != 0;
            bool hasContentSize = ((flg >> 3) & 0x01) != 0;
            bool contentChecksum = ((flg >> 2) & 0x01) != 0;
            bool hasDictionary = (flg & 0x01) != 0;
			int blockSizeCode = (bd >> 4) & 0x07;

			long? contentLength = hasContentSize ? (long?) Read64() : null;
			uint? dictionaryId = hasDictionary ? (uint?) Read32() : null;

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

			if (actualHC != expectedHC)
                throw new InvalidDataException(RS.BadLZ4FrameHeaderChecksum);

			int blockSize = MaxBlockSize(blockSizeCode);

			if (hasDictionary)
            {
                // Write32(dictionaryId);
                throw new NotImplementedException(string.Format(RS.FeatureNotImplementedInType, "Predefined Dictionaries", GetType().Name));
            }

			_frameInfo = new LZ4FrameDescriptor(contentLength, contentChecksum, blockChaining, blockChecksum, dictionaryId, blockSize);
			_decoder = _decoderFactory(_frameInfo);
			_buffer = new byte[blockSize];
		}
		private void WriteFrame()
		{
			Write32(0x184D2204);
			Flush16();

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

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

			int blockSize = _descriptor.BlockSize;

			int bd = MaxBlockSizeCode(blockSize) << 4;

			Write16((ushort) ((flg & 0xFF) | (bd & 0xFF) << 8));

			if (hasContentSize)
            {
                // Write64(contentSize)
                throw new NotImplementedException(string.Format(RS.FeatureNotImplementedInType, "Content Size", GetType().Name));
            }

			if (hasDictionary)
            {
                // Write32(dictionaryId)
                throw new NotImplementedException(string.Format(RS.FeatureNotImplementedInType, "Predefined Dictionaries", GetType().Name));
            }

			byte hc = (byte)(XXHash32.DigestOf(_buffer16, 0, _index16) >> 8);

			Write8(hc);
			Flush16();

			_encoder = CreateEncoder();
			_buffer = new byte[LZ4Codec.MaximumOutputSize(blockSize)];
		}
Exemple #5
0
        public void EveryCallToDigestReturnsSameHash(int seed, int length)
        {
            var random = new Random(seed);
            var bytes  = new byte[length];

            random.NextBytes(bytes);

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

            var transform = new XXHash32();

            transform.Update(bytes, 0, length);

            for (var i = 0; i < 100; i++)
            {
                Assert.Equal(expected, transform.Digest());
            }
        }
Exemple #6
0
        public unsafe void EmptyHash()
        {
            var input = Array.Empty <byte>();

            var expected = Theirs32(input);

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

            Assert.Equal(expected, actual1);

            fixed(byte *inputPtr = input)
            {
                var actual2 = XXHash32.DigestOf(inputPtr, 0);

                Assert.Equal(expected, actual2);
            }

            var actual3 = XXHash32.EmptyHash;

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

            random.NextBytes(bytes);

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

            var transform = new XXHash32();
            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);
        }