Exemplo n.º 1
0
        public override Block Create(uint block)
        {
            if (!vhdFile.BlockAllocationTable.HasData(block))
            {
                if (cachedBlock == null || cachedBlock.BlockIndex != block)
                {
                    cachedBlock = parentBlockFactory.Create(block);
                }
                return(cachedBlock);
            }

            if (cachedBlock == null || cachedBlock.BlockIndex != block)
            {
                IndexRange logicalRange = IndexRange.FromLength(block * GetBlockSize(), this.GetBlockSize());
                if (ExtraBlockIndex.HasValue && block == ExtraBlockIndex)
                {
                    long startIndex = block * GetBlockSize();
                    long size       = vhdFile.Footer.CurrentSize - startIndex;
                    logicalRange = IndexRange.FromLength(startIndex, size);
                }
                cachedBlock = new Block(this)
                {
                    BlockIndex   = block,
                    VhdUniqueId  = this.vhdFile.Footer.UniqueId,
                    LogicalRange = logicalRange,
                    BitMap       = bitMapFactory.Create(block),
                    Empty        = false
                };
            }
            return(cachedBlock);
        }
Exemplo n.º 2
0
    public IGroup Create(ISetting setting, IBlockPattern blockPattern, IGroupPattern groupPattern)
    {
        IGroup group = new Group(setting);

        for (int i = 0; i < groupPattern.Patterns[0].Length; i++)
        {
            IBlock block = _blockFactory.Create(setting, blockPattern.Types[i], groupPattern.Patterns[0][i]);
            group.AddBlock(block);
        }

        group.SetPattern(groupPattern.Patterns);

        return(group);
    }
Exemplo n.º 3
0
        private bool TryFindNextBlock(OngoingBlock ongoingBlock, out Block nextBlock)
        {
            var rnd = Guid.NewGuid().ToString("D");

            nextBlock = _blockFactory.Create(ongoingBlock.Index, ongoingBlock.Node, rnd, ongoingBlock.PreviousHash, ongoingBlock.Transactions);
            Trace.TraceInformation("[node]\t\tCreated next block candidate {0}", nextBlock);

            return(_hashFilter.IsSatisfy(nextBlock.Signature, _nodeSettings.NextHashFactor));
        }
Exemplo n.º 4
0
        public override Block Create(uint block)
        {
            if (!vhdFile.BlockAllocationTable.HasData(block))
            {
                if (cachedBlock == null || cachedBlock.BlockIndex != block)
                {
                    cachedBlock = parentBlockFactory.Create(block);
                }
                return(cachedBlock);
            }

            if (cachedBlock == null || cachedBlock.BlockIndex != block)
            {
                cachedBlock = new Block(this)
                {
                    BlockIndex   = block,
                    VhdUniqueId  = this.vhdFile.Footer.UniqueId,
                    LogicalRange = IndexRange.FromLength(block * GetBlockSize(), vhdFile.Header.BlockSize),
                    BitMap       = bitMapFactory.Create(block),
                    Empty        = false
                };
            }
            return(cachedBlock);
        }
        /// <summary>
        /// Reads the specified number of bytes from the current position.
        /// </summary>
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (count <= 0)
            {
                return(0);
            }

            try
            {
                var rangeToRead = IndexRange.FromLength(this.position, count);

                int writtenCount = 0;
                if (fileDataRange.Intersection(rangeToRead) == null)
                {
                    int readCountFromFooter;
                    if (TryReadFromFooter(rangeToRead, buffer, offset, out readCountFromFooter))
                    {
                        writtenCount += readCountFromFooter;
                    }
                    return(writtenCount);
                }

                rangeToRead = fileDataRange.Intersection(rangeToRead);

                var startingBlock = ByteToBlock(rangeToRead.StartIndex);
                var endingBlock   = ByteToBlock(rangeToRead.EndIndex);

                for (var blockIndex = startingBlock; blockIndex <= endingBlock; blockIndex++)
                {
                    var currentBlock       = blockFactory.Create(blockIndex);
                    var rangeToReadInBlock = currentBlock.LogicalRange.Intersection(rangeToRead);

                    var copyStartIndex = rangeToReadInBlock.StartIndex % blockFactory.GetBlockSize();
                    Buffer.BlockCopy(currentBlock.Data, (int)copyStartIndex, buffer, offset + writtenCount, (int)rangeToReadInBlock.Length);

                    writtenCount += (int)rangeToReadInBlock.Length;
                }
                this.position += writtenCount;

                return(writtenCount);
            }
            catch (Exception e)
            {
                throw new VhdParsingException("Invalid or Corrupted VHD file", e);
            }
        }
Exemplo n.º 6
0
        public void ParserShouldCallSBlockFactoryWhenParsingStatements()
        {
            IList <StatementBase> input = new List <StatementBase>();

            input.Add(StatementCreator.CreateMemberStatement("_someString"));

            IBlockFactory factory = Mocker.StrictMock <IBlockFactory>();

            Expect.Call(factory.Create(input)).Return(new [] { new BlockBaseImpl(input) }).Repeat.Once();

            StubBlockFactoryProvider provider = new StubBlockFactoryProvider(factory);

            CalidusBlockParser parser = new CalidusBlockParser(provider);

            Mocker.ReplayAll();

            parser.Parse(input);

            Mocker.VerifyAll();
        }