public void Destream_can_read_streamed_blocks()
        {
            var blocks = new HashBlock[5];

            for (int index = 0; index < blocks.Length; index++)
            {
                blocks[index] = new HashBlock
                {
                    Checksum = (uint)index,
                    Length   = index * 2,
                    Offset   = 9223372036854775807L,
                    Hash     = new byte[16]
                };
                new Random().NextBytes(blocks[index].Hash);
            }
            var ms = new MemoryStream();

            HashBlockStreamer.Stream(blocks, ms);
            ms.Seek(0, SeekOrigin.Begin);
            HashBlock[] newBlocks = HashBlockStreamer.Destream(ms);
            Assert.AreEqual(blocks.Length, newBlocks.Length);
            for (int index = 0; index < newBlocks.Length; index++)
            {
                HashBlock newBlock = newBlocks[index];
                HashBlock oldBlock = blocks[index];
                Assert.AreEqual(oldBlock.Checksum, newBlock.Checksum);
                Assert.AreEqual(oldBlock.Length, newBlock.Length);
                Assert.AreEqual(oldBlock.Offset, newBlock.Offset);
                Assert.IsTrue(oldBlock.Hash.SequenceEqual(newBlock.Hash));
            }
        }
Example #2
0
        public HashBlock Allocate(uint desired, Client c)
        {
            HashBlock block = null;

            HashBlock free = FindFreeBlock(desired);
            if (free != null)
            {
                if (free.Count > desired)
                {
                    // Split this block
                    block = new HashBlock();
                    block.Owner = c;
                    block.Start = free.Start;
                    block.Count = desired;
                    mBusyBlocks.Add(block);

                    free.Start += desired;
                    free.Count -= desired;
                }
                else
                {
                    // Remove the free block from the free list
                    mFreeBlocks.Remove(free);

                    // Assign the free block to the client
                    free.Owner = c;

                    // Add the block to the busy list
                    block = free;
                    mBusyBlocks.Add(block);
                }
            }

            return block;
        }
Example #3
0
            protected override void ReadData(BinaryReader r)
            {
                if (Tag != TAG)
                {
                    throw new Exception($"Invalid header magic number 0x{Tag:4X}");
                }

                var payloadType = r.ReadInt32BE();
                var hashType    = r.ReadInt32BE();
                var reserved    = r.ReadInt32BE();
                var hashLen     = r.ReadInt32BE();

                if (hashLen > Length)
                {
                    throw new Exception("Error parsing sin file");
                }

                var pos = r.BaseStream.Position;

                while (r.BaseStream.Position < pos + hashLen)
                {
                    Blocks.Add(HashBlock.Read(r, hashType));
                }

                var certLen = r.ReadInt32BE();

                certLen = (certLen + 3) / 4 * 4; // ???
                var cert = r.ReadBytes(certLen);
            }
Example #4
0
        public void GetDeltas_returns_three_CopyDeltas_object_when_matching_hash_is_provided_for_triple_repeat()
        {
            var checksumMock = new Mock <IRollingChecksum>();

            checksumMock.Setup(x => x.Value).Returns(0);
            var hashMock = new Mock <IHashAlgorithm>();

            byte[] dummyBytes     = InitializeDummyMD5Hash(hashMock);
            var    gen            = new DeltaGenerator(checksumMock.Object, hashMock.Object);
            int    blockSize      = 10;
            var    magicHashBlock = new HashBlock {
                Checksum = 0, Hash = dummyBytes, Offset = 42, Length = 43
            };

            gen.Initialize(blockSize, new[] { magicHashBlock });
            var bytes = new byte[blockSize * 3];

            for (int i = 0; i < bytes.Length; ++i)
            {
                bytes[i] = 0;
            }
            IEnumerable <IDelta> deltas = gen.GetDeltas(new MemoryStream(bytes));

            Assert.AreEqual(3, deltas.Count());
            Assert.IsTrue(deltas.All(d => d is CopyDelta));
            Assert.IsTrue(deltas.All(d => (d as CopyDelta).Offset == magicHashBlock.Offset));
        }
Example #5
0
            public static HashBlock Read(BinaryReader r, int hashType)
            {
                var b = new HashBlock();

                b.length = r.ReadInt32BE();
                b.crc    = r.ReadBytes(hashv3len[hashType]);

                return(b);
            }
Example #6
0
 public void FreeBlock(HashBlock block)
 {
     if (block != null)
     {
         mBusyBlocks.Remove(block);
         mFreeBlocks.Insert(0, block);
         block.Owner = null;
     }
 }
        public override bool Equals(object obj)
        {
            ChainedBlock item = obj as ChainedBlock;

            if (item == null)
            {
                return(false);
            }
            return(HashBlock.Equals(item.HashBlock));
        }
Example #8
0
        public void FinishBlock(HashBlock block)
        {
            mBusyBlocks.Remove(block);
            mDoneBlocks.Add(block);

            // Stat Tracking
            {
                mHashesDone = 0;
                foreach (HashBlock hb in mDoneBlocks)
                    mHashesDone += hb.Count;

                //Console.WriteLine("Block finished: " + mHashesDone + " (%" + ((float)mHashesDone / (float)mHashesTotal) * 100 + ")");
            }
        }
Example #9
0
        public void GetDeltas_throws_for_null_stream()
        {
            var dummyBlock =
                new HashBlock
            {
                Checksum = 0,
                Length   = 0,
                Offset   = 0,
                Hash     = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8 }
            };
            DeltaGenerator gen = GetValidDeltaGenerator();

            gen.Initialize(10, new[] { dummyBlock });
            gen.GetDeltas(null).Count();
        }
Example #10
0
        public HashManager()
        {
            mFreeBlocks = new List<HashBlock>();
            mBusyBlocks = new List<HashBlock>();
            mDoneBlocks = new List<HashBlock>();

            // Add initial free block
            HashBlock block = new HashBlock();
            block.Owner = null;
            block.Start = 0;
            block.Count = mHashesTotal;
            mFreeBlocks.Add(block);

            mStartTime = DateTime.Now;
        }
        public void Stream_writes_valid_byte_length()
        {
            var blocks = new HashBlock[5];

            for (int index = 0; index < blocks.Length; index++)
            {
                blocks[index] = new HashBlock {
                    Hash = new byte[16]
                };
            }
            var ms = new MemoryStream();

            HashBlockStreamer.Stream(blocks, ms);
            Assert.AreEqual(blocks.Length * 32 + 4, ms.Length);
        }
Example #12
0
        public HashManager()
        {
            mFreeBlocks = new List <HashBlock>();
            mBusyBlocks = new List <HashBlock>();
            mDoneBlocks = new List <HashBlock>();

            // Add initial free block
            HashBlock block = new HashBlock();

            block.Owner = null;
            block.Start = 0;
            block.Count = mHashesTotal;
            mFreeBlocks.Add(block);

            mStartTime = DateTime.Now;
        }
Example #13
0
        public void FinishBlock(HashBlock block)
        {
            mBusyBlocks.Remove(block);
            mDoneBlocks.Add(block);

            // Stat Tracking
            {
                mHashesDone = 0;
                foreach (HashBlock hb in mDoneBlocks)
                {
                    mHashesDone += hb.Count;
                }

                //Console.WriteLine("Block finished: " + mHashesDone + " (%" + ((float)mHashesDone / (float)mHashesTotal) * 100 + ")");
            }
        }
        public void Stream_writes_valid_length()
        {
            var blocks = new HashBlock[5];

            for (int index = 0; index < blocks.Length; index++)
            {
                blocks[index] = new HashBlock {
                    Hash = new byte[16]
                };
            }
            var ms = new MemoryStream();

            HashBlockStreamer.Stream(blocks, ms);
            ms.Seek(0, SeekOrigin.Begin);
            var buf = new byte[4];

            ms.Read(buf, 0, 4);
            Assert.AreEqual((uint)blocks.Length, BitConverter.ToUInt32(buf, 0));
        }
Example #15
0
        HashBlock FindFreeBlock(uint desiredSize)
        {
            HashBlock free = null;

            foreach (HashBlock hb in mFreeBlocks)
            {
                if (hb.Count >= desiredSize)
                {
                    free = hb;
                    break;
                }
            }

            if (free == null && mFreeBlocks.Count > 0)
            {
                // Didnt find a large enough block, use the first block instead
                free = mFreeBlocks[0];
            }

            return(free);
        }
Example #16
0
        public void GetDeltas_returns_two_CopyDelta_objects_and_a_middle_ByteDelta_for_interspaced_new_data()
        {
            var checksumMock       = new Mock <IRollingChecksum>();
            int checksumCallNumber = 0;

            // Returns "1" for calls number 2 through 11 inclusive.
            checksumMock.Setup(x => x.Value)
            .Returns(() => (uint)(++checksumCallNumber > 1 && checksumCallNumber <= 11 ? 1 : 0));
            var hashMock = new Mock <IHashAlgorithm>();

            byte[] dummyBytes     = InitializeDummyMD5Hash(hashMock);
            var    gen            = new DeltaGenerator(checksumMock.Object, hashMock.Object);
            int    blockSize      = 10;
            var    magicHashBlock = new HashBlock {
                Checksum = 0, Hash = dummyBytes, Offset = 42, Length = 43
            };

            gen.Initialize(blockSize, new[] { magicHashBlock });
            var bytes = new byte[blockSize * 3];

            for (int i = 0; i < bytes.Length; ++i)
            {
                bytes[i] = 0;
            }
            // Should immediately get a match for an entire block.
            // Then upon trying to match a new one get a mismatch for the next
            // 10 bytes
            IEnumerable <IDelta> deltas = gen.GetDeltas(new MemoryStream(bytes));

            Assert.AreEqual(3, deltas.Count());
            Assert.IsTrue(
                deltas.ElementAt(0) is CopyDelta &&
                deltas.ElementAt(1) is ByteDelta &&
                deltas.ElementAt(2) is CopyDelta);
            Assert.AreEqual(10, (deltas.ElementAt(1) as ByteDelta).Offset);
            Assert.AreEqual(10, (deltas.ElementAt(1) as ByteDelta).Length);
        }
Example #17
0
        public HashBlock Allocate(uint desired, Client c)
        {
            HashBlock block = null;

            HashBlock free = FindFreeBlock(desired);

            if (free != null)
            {
                if (free.Count > desired)
                {
                    // Split this block
                    block       = new HashBlock();
                    block.Owner = c;
                    block.Start = free.Start;
                    block.Count = desired;
                    mBusyBlocks.Add(block);

                    free.Start += desired;
                    free.Count -= desired;
                }
                else
                {
                    // Remove the free block from the free list
                    mFreeBlocks.Remove(free);

                    // Assign the free block to the client
                    free.Owner = c;

                    // Add the block to the busy list
                    block = free;
                    mBusyBlocks.Add(block);
                }
            }

            return(block);
        }
Example #18
0
 public override int GetHashCode()
 {
     return(HashBlock.GetHashCode());
 }
Example #19
0
 public void FreeBlock(HashBlock block)
 {
     if (block != null)
     {
         mBusyBlocks.Remove(block);
         mFreeBlocks.Insert(0, block);
         block.Owner = null;
     }
 }
Example #20
0
 public PacketOption(string Comment = null, PacketBlockFlags PacketFlag = null, HashBlock Hash = null)
 {
     this.Comment    = Comment;
     this.PacketFlag = PacketFlag;
     this.Hash       = Hash;
 }
 public EnchantedPacketOption(string Comment = null, PacketBlockFlags PacketFlag = null, long?DropCount = null, HashBlock Hash = null)
 {
     this.Comment    = Comment;
     this.PacketFlag = PacketFlag;
     this.DropCount  = DropCount;
     this.Hash       = Hash;
 }