Example #1
0
 private static DocumentBlock[] ConvertRawBlocksToBigBlocks(ListManagedBlock[] blocks)
 {
     DocumentBlock[] result = new DocumentBlock[blocks.Length];
     for (int i = 0; i < result.Length; i++)
         result[i] = new DocumentBlock((RawDataBlock)blocks[i]);
     return result;
 }
Example #2
0
 private static SmallDocumentBlock[] ConvertRawBlocksToSmallBlocks(ListManagedBlock[] blocks)
 {
     if (blocks is SmallDocumentBlock[])
         return (SmallDocumentBlock[])blocks;
     SmallDocumentBlock[] result = new SmallDocumentBlock[blocks.Length];
     System.Array.Copy(blocks, 0, result, 0, blocks.Length);
     return result;
 }
Example #3
0
        /// <summary>
        /// Convert raw data blocks to an array of Property's
        /// </summary>
        /// <param name="blocks">The blocks to be converted</param>
        /// <returns>the converted List of Property objects. May contain
        /// nulls, but will not be null</returns>
        public static List<Property> ConvertToProperties(ListManagedBlock [] blocks)
        {
            List<Property> properties = new List<Property>();

            for (int j = 0; j < blocks.Length; j++)
            {
                byte[] data = blocks[j].Data;
                ConvertToProperties(data, properties);
            }

            return properties;
        }
Example #4
0
 public RawDataBlockList Clone()
 {
     var bl = new RawDataBlockList();
     bl.BAT = this.BAT;
     var blist = new ListManagedBlock[this.RemainingBlocks()];
     for(int i = 0; i < this.RemainingBlocks(); i++)
     {
         blist[i] = Get(i);
     }
     bl.SetBlocks(blist);
     return bl;
 }
        /// <summary>
        /// walk the entries from a specified point and return the
        /// associated blocks. The associated blocks are Removed from the block list
        /// </summary>
        /// <param name="startBlock">the first block in the chain</param>
        /// <param name="blockList">the raw data block list</param>
        /// <returns>array of ListManagedBlocks, in their correct order</returns>
        public ListManagedBlock[] FetchBlocks(int startBlock, int headerPropertiesStartBlock,
                                              BlockList blockList)
        {
            IList blocks       = new ArrayList();
            int   currentBlock = startBlock;
            bool  firstPass    = true;

            while (currentBlock != POIFSConstants.END_OF_CHAIN)
            {
                try
                {
                    blocks.Add(blockList.Remove(currentBlock));
                    currentBlock = _entries[currentBlock];
                    firstPass    = false;
                }
                catch (Exception)
                {
                    if (currentBlock == headerPropertiesStartBlock)
                    {
                        // Special case where things are in the wrong order
                        Console.Error.WriteLine("Warning, header block comes after data blocks in POIFS block listing");
                        currentBlock = POIFSConstants.END_OF_CHAIN;
                    }
                    else if (currentBlock == 0 && firstPass)
                    {
                        // Special case where the termination isn't done right
                        //  on an empty set
                        Console.Error.WriteLine("Warning, incorrectly terminated empty data blocks in POIFS block listing (should end at -2, ended at 0)");
                        currentBlock = POIFSConstants.END_OF_CHAIN;
                    }
                    else
                    {
                        // Ripple up
                        throw;
                    }
                }
            }
            ListManagedBlock[] array = new ListManagedBlock[blocks.Count];
            blocks.CopyTo(array, 0);

            return(array);
        }
        /// <summary>
        /// walk the entries from a specified point and return the
        /// associated blocks. The associated blocks are Removed from the block list
        /// </summary>
        /// <param name="startBlock">the first block in the chain</param>
        /// <param name="headerPropertiesStartBlock"></param>
        /// <param name="blockList">the raw data block list</param>
        /// <returns>array of ListManagedBlocks, in their correct order</returns>
        public ListManagedBlock[] FetchBlocks(int startBlock, int headerPropertiesStartBlock,
                                              BlockList blockList)
        {
            List <ListManagedBlock> blocks = new List <ListManagedBlock>();
            int              currentBlock  = startBlock;
            bool             firstPass     = true;
            ListManagedBlock dataBlock     = null;

            while (currentBlock != POIFSConstants.END_OF_CHAIN)
            {
                try
                {
                    dataBlock = blockList.Remove(currentBlock);
                    blocks.Add(dataBlock);
                    currentBlock = _entries[currentBlock];
                    firstPass    = false;
                }
                catch (Exception)
                {
                    if (currentBlock == headerPropertiesStartBlock)
                    {
                        // Special case where things are in the wrong order
                        _logger.Log(POILogger.WARN, "Warning, header block comes after data blocks in POIFS block listing");
                        currentBlock = POIFSConstants.END_OF_CHAIN;
                    }
                    else if (currentBlock == 0 && firstPass)
                    {
                        // Special case where the termination isn't done right
                        //  on an empty set
                        _logger.Log(POILogger.WARN, "Warning, incorrectly terminated empty data blocks in POIFS block listing (should end at -2, ended at 0)");
                        currentBlock = POIFSConstants.END_OF_CHAIN;
                    }
                    else
                    {
                        // Ripple up
                        throw;
                    }
                }
            }
            ListManagedBlock[] array = blocks.ToArray();
            return(array);
        }
        /// <summary>
        /// Convert raw data blocks to an array of Property's
        /// </summary>
        /// <param name="blocks">The blocks to be converted</param>
        /// <returns>the converted List of Property objects. May contain
        /// nulls, but will not be null</returns>
        public static IList ConvertToProperties(ListManagedBlock [] blocks)
        {
            IList properties = new ArrayList();

            for (int j = 0; j < blocks.Length; j++)
            {
                byte[] data           = blocks[ j ].Data;
                int    property_count = data.Length
                                        / POIFSConstants.PROPERTY_SIZE;
                int    offset         = 0;

                for (int k = 0; k < property_count; k++)
                {
                    switch (data[ offset + PropertyConstants.PROPERTY_TYPE_OFFSET ])
                    {

                        case PropertyConstants.DIRECTORY_TYPE :
                            properties
                                .Add(new DirectoryProperty(properties.Count,
                                                           data, offset));
                            break;

                        case PropertyConstants.DOCUMENT_TYPE :
                            properties.Add(new DocumentProperty(properties.Count,
                                                                data, offset));
                            break;

                        case PropertyConstants.ROOT_TYPE :
                            properties.Add(new RootProperty(properties.Count,
                                                            data, offset));
                            break;

                        default :
                            properties.Add(null);
                            break;
                    }
                    offset += POIFSConstants.PROPERTY_SIZE;
                }
            }
            return properties;
        }
Example #8
0
        /// <summary>
        /// Remove and return the specified block from the list
        /// </summary>
        /// <param name="index">the index of the specified block</param>
        /// <returns>the specified block</returns>
        public virtual ListManagedBlock Remove(int index)
        {
            ListManagedBlock result = null;

            try
            {
                result = _blocks[index];
                if (result == null)
                {
                    throw new IOException("block[ " + index
                                          + " ] already removed");
                }
                _blocks[index] = null;
            }
            catch (IndexOutOfRangeException)
            {
                throw new IOException("Cannot remove block[ " + index
                                      + " ]; out of range[ 0 - " +
                                      (_blocks.Length - 1) + " ]");
            }
            return(result);
        }
Example #9
0
 /// <summary>
 /// provide blocks to manage
 /// </summary>
 /// <param name="blocks">blocks to be managed</param> 
 public virtual void SetBlocks(ref ListManagedBlock[] blocks)
 {
     _blocks = blocks;
 }
        /// <summary>
        /// Convert an array of blocks into a Set of integer indices
        /// </summary>
        /// <param name="blocks">the array of blocks containing the indices</param>
        /// <param name="raw_blocks">the list of blocks being managed. Unused
        /// blocks will be eliminated from the list</param>
        private void SetEntries(ListManagedBlock[] blocks,
                                BlockList raw_blocks)
        {

            int limit = bigBlockSize.GetBATEntriesPerBlock();

            for (int block_index = 0; block_index < blocks.Length; block_index++)
            {
                byte[] data = blocks[block_index].Data;
                int offset = 0;

                for (int k = 0; k < limit; k++)
                {
                    int entry = LittleEndian.GetInt(data, offset);

                    if (entry == POIFSConstants.UNUSED_BLOCK)
                    {
                        raw_blocks.Zap(_entries.Count);
                    }
                    _entries.Add(entry);
                    offset += LittleEndianConsts.INT_SIZE;
                }

                // discard block
                blocks[block_index] = null;
            }
            raw_blocks.BAT = this;
        }
 /// <summary>
 /// create a BlockAllocationTableReader from an array of raw data blocks
 /// </summary>
 /// <param name="bigBlockSize"></param>
 /// <param name="blocks">the raw data</param>
 /// <param name="raw_block_list">the list holding the managed blocks</param>
 public BlockAllocationTableReader(POIFSBigBlockSize bigBlockSize, ListManagedBlock[] blocks,  
                            BlockList raw_block_list)
     : this(bigBlockSize)
 {
     SetEntries(blocks, raw_block_list);
 }
Example #12
0
 /// <summary>
 /// Constructor from small blocks
 /// </summary>
 /// <param name="name">the name of the POIFSDocument</param>
 /// <param name="blocks">the small blocks making up the POIFSDocument</param>
 /// <param name="length">the actual length of the POIFSDocument</param>
 public POIFSDocument(string name, ListManagedBlock[] blocks, int length)
 {
     this._size = length;
     this._property = new DocumentProperty(name, this._size);
     this._property.Document = this;
     if (Property.IsSmall(this._size))
     {
         this._big_store = new BigBlockStore(this, new RawDataBlock[0]);
         this._small_store = new SmallBlockStore(this, blocks);
     }
     else
     {
         this._big_store = new BigBlockStore(this, blocks);
         this._small_store = new SmallBlockStore(this, new BlockWritable[0]);
     }
 }
Example #13
0
 /// <summary>
 /// Constructor from small blocks
 /// </summary>
 /// <param name="name">the name of the POIFSDocument</param>
 /// <param name="blocks">the small blocks making up the POIFSDocument</param>
 /// <param name="length">the actual length of the POIFSDocument</param>
 public POIFSDocument(string name, ListManagedBlock[] blocks, int length)
     :this(name, POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, blocks, length)
     {
 }
Example #14
0
        public POIFSDocument(string name, POIFSBigBlockSize bigBlockSize, ListManagedBlock[] blocks, int length)
        {
            _size = length;
            _bigBigBlockSize = bigBlockSize;
            _property = new DocumentProperty(name, _size);
            _property.Document = this;

            if (Property.IsSmall(_size))
            {
                _big_store = new BigBlockStore(bigBlockSize, EMPTY_BIG_BLOCK_ARRAY);
                _small_store = new SmallBlockStore(bigBlockSize, ConvertRawBlocksToSmallBlocks(blocks));
            }
            else
            {
                _big_store = new BigBlockStore(bigBlockSize, ConvertRawBlocksToBigBlocks(blocks));
                _small_store = new SmallBlockStore(bigBlockSize, EMPTY_SMALL_BLOCK_ARRAY);
            }
        }
        /// <summary>
        /// walk the entries from a specified point and return the
        /// associated blocks. The associated blocks are Removed from the block list
        /// </summary>
        /// <param name="startBlock">the first block in the chain</param>
        /// <param name="blockList">the raw data block list</param>
        /// <returns>array of ListManagedBlocks, in their correct order</returns>
        public ListManagedBlock[] FetchBlocks(int startBlock, int headerPropertiesStartBlock,
                                        BlockList blockList)
        {
            IList blocks = new ArrayList();
            int currentBlock = startBlock;
            bool firstPass = true;

            while (currentBlock != POIFSConstants.END_OF_CHAIN)
            {
                try
                {
                    blocks.Add(blockList.Remove(currentBlock));
                    currentBlock = _entries[currentBlock];
                    firstPass = false;
                }
                catch(Exception)
                {
                    if (currentBlock == headerPropertiesStartBlock)
                    {
                        // Special case where things are in the wrong order
                        Console.Error.WriteLine("Warning, header block comes after data blocks in POIFS block listing");
                        currentBlock = POIFSConstants.END_OF_CHAIN;
                    }
                    else if (currentBlock == 0 && firstPass)
                    {
                        // Special case where the termination isn't done right
                        //  on an empty set
                        Console.Error.WriteLine("Warning, incorrectly terminated empty data blocks in POIFS block listing (should end at -2, ended at 0)");
                        currentBlock = POIFSConstants.END_OF_CHAIN;
                    }
                    else
                    {
                        // Ripple up
                        throw;
                    }
                }
            }
            ListManagedBlock[] array = new ListManagedBlock[blocks.Count];
            blocks.CopyTo(array, 0);

            return (array);
        }
Example #16
0
        /// <summary>
        /// create a list of SmallDocumentBlock's from raw data
        /// </summary>
        /// <param name="blocks">the raw data containing the SmallDocumentBlock</param>
        /// <returns>a List of SmallDocumentBlock's extracted from the input</returns>
        public static IList Extract(ListManagedBlock [] blocks)
        {
            IList sdbs = new ArrayList();

            for (int j = 0; j < blocks.Length; j++)
            {
                byte[] data = blocks[ j ].Data;

                for (int k = 0; k < _blocks_per_big_block; k++)
                {
                    sdbs.Add(new SmallDocumentBlock(data, k));
                }
            }
            return sdbs;
        }
        /// <summary>
        /// walk the entries from a specified point and return the
        /// associated blocks. The associated blocks are Removed from the block list
        /// </summary>
        /// <param name="startBlock">the first block in the chain</param>
        /// <param name="blockList">the raw data block list</param>
        /// <returns>array of ListManagedBlocks, in their correct order</returns>
        public ListManagedBlock[] FetchBlocks(int startBlock,
                                        BlockList blockList)
        {
            IList blocks = new ArrayList();
            int currentBlock = startBlock;

            while (currentBlock != POIFSConstants.END_OF_CHAIN)
            {
                blocks.Add(blockList.Remove(currentBlock));
                currentBlock = _entries[currentBlock];
            }
            ListManagedBlock[] array = new ListManagedBlock[blocks.Count];
            blocks.CopyTo(array, 0);

            return (array);
        }
 /// <summary>
 /// create a BlockAllocationTableReader from an array of raw data blocks
 /// </summary>
 /// <param name="blocks">the raw data</param>
 /// <param name="raw_block_list">the list holding the managed blocks</param>
 public BlockAllocationTableReader(ListManagedBlock[] blocks,
                            BlockList raw_block_list)
     : this()
 {
     SetEntries(blocks, raw_block_list);
 }
Example #19
0
        /// <summary>
        /// create a list of SmallDocumentBlock's from raw data
        /// </summary>
        /// <param name="bigBlockSize"></param>
        /// <param name="blocks">the raw data containing the SmallDocumentBlock</param>
        /// <returns>a List of SmallDocumentBlock's extracted from the input</returns>
        public static List<SmallDocumentBlock> Extract(POIFSBigBlockSize bigBlockSize, ListManagedBlock [] blocks)
        {
            int _blocks_per_big_block = GetBlocksPerBigBlock(bigBlockSize);
            List<SmallDocumentBlock> sdbs = new List<SmallDocumentBlock>();

            for (int j = 0; j < blocks.Length; j++)
            {
                byte[] data = blocks[ j ].Data;

                for (int k = 0; k < _blocks_per_big_block; k++)
                {
                    sdbs.Add(new SmallDocumentBlock(bigBlockSize, data, k));
                }
            }
            return sdbs;
        }