/// <summary> /// Create an array of XBATBlocks from an array of int block /// allocation table entries /// </summary> /// <param name="entries">the array of int entries</param> /// <param name="startBlock">the start block of the array of XBAT blocks</param> /// <returns>the newly created array of BATBlocks</returns> public static BATBlock[] CreateXBATBlocks(POIFSBigBlockSize bigBlockSize, int[] entries, int startBlock) { int block_count = CalculateXBATStorageRequirements(entries.Length); BATBlock[] blocks = new BATBlock[block_count]; int index = 0; int remaining = entries.Length; if (block_count != 0) { for (int j = 0; j < entries.Length; j += _entries_per_xbat_block) { blocks[index++] = new BATBlock(bigBlockSize, entries, j, (remaining > _entries_per_xbat_block) ? j + _entries_per_xbat_block : entries.Length); remaining -= _entries_per_xbat_block; } for (index = 0; index < blocks.Length - 1; index++) { blocks[index].SetXBATChain(bigBlockSize, startBlock + index + 1); } blocks[index].SetXBATChain(bigBlockSize, POIFSConstants.END_OF_CHAIN); } return(blocks); }
/// <summary> /// Create the BATBlocks we need /// </summary> /// <returns>start block index of BAT blocks</returns> public int CreateBlocks() { int xbat_blocks = 0; int bat_blocks = 0; while (true) { int calculated_bat_blocks = BATBlock.CalculateStorageRequirements(_bigBlockSize, bat_blocks + xbat_blocks + _entries.Count); int calculated_xbat_blocks = HeaderBlockWriter.CalculateXBATStorageRequirements(_bigBlockSize, calculated_bat_blocks); if ((bat_blocks == calculated_bat_blocks) && (xbat_blocks == calculated_xbat_blocks)) { // stable ... we're OK break; } else { bat_blocks = calculated_bat_blocks; xbat_blocks = calculated_xbat_blocks; } } int startBlock = AllocateSpace(bat_blocks); AllocateSpace(xbat_blocks); SimpleCreateBlocks(); return(startBlock); }
///** // * Creates a single BATBlock, with all the values set to empty. // */ public static BATBlock CreateEmptyBATBlock(POIFSBigBlockSize bigBlockSize, bool isXBAT) { BATBlock block = new BATBlock(bigBlockSize); if (isXBAT) { block.SetXBATChain(bigBlockSize, POIFSConstants.END_OF_CHAIN); } return(block); }
/** * Create a single BATBlock from the byte buffer, which must hold at least * one big block of data to be read. */ public static BATBlock CreateBATBlock(POIFSBigBlockSize bigBlockSize, BinaryReader data) { // Create an empty block BATBlock block = new BATBlock(bigBlockSize); // Fill it byte[] buffer = new byte[LittleEndianConsts.INT_SIZE]; for (int i = 0; i < block._values.Length; i++) { data.Read(buffer, 0, buffer.Length); block._values[i] = LittleEndian.GetInt(buffer); } block.RecomputeFree(); // All done return(block); }
/// <summary> /// Create an array of BATBlocks from an array of int block /// allocation table entries /// </summary> /// <param name="entries">the array of int entries</param> /// <returns>the newly created array of BATBlocks</returns> public static BATBlock[] CreateBATBlocks(POIFSBigBlockSize bigBlockSize, int[] entries) { int block_count = CalculateStorageRequirements(entries.Length); BATBlock[] blocks = new BATBlock[block_count]; int index = 0; int remaining = entries.Length; for (int j = 0; j < entries.Length; j += _entries_per_block) { blocks[index++] = new BATBlock(bigBlockSize, entries, j, (remaining > _entries_per_block) ? j + _entries_per_block : entries.Length); remaining -= _entries_per_block; } return(blocks); }
/// <summary> /// Set BAT block parameters. Assumes that all BAT blocks are /// contiguous. Will construct XBAT blocks if necessary and return /// the array of newly constructed XBAT blocks. /// </summary> /// <param name="blockCount">count of BAT blocks</param> /// <param name="startBlock">index of first BAT block</param> /// <returns>array of XBAT blocks; may be zero Length, will not be /// null</returns> public BATBlock[] SetBATBlocks(int blockCount, int startBlock) { BATBlock[] rvalue; POIFSBigBlockSize bigBlockSize = _header_block.BigBlockSize; _header_block.BATCount = blockCount; int limit = Math.Min(blockCount, _max_bats_in_header); int[] bat_blocks = new int[limit]; for (int j = 0; j < limit; j++) { bat_blocks[j] = startBlock + j; } _header_block.BATArray = bat_blocks; if (blockCount > _max_bats_in_header) { int excess_blocks = blockCount - _max_bats_in_header; int[] excess_block_array = new int[excess_blocks]; for (int j = 0; j < excess_blocks; j++) { excess_block_array[j] = startBlock + j + _max_bats_in_header; } rvalue = BATBlock.CreateXBATBlocks(bigBlockSize, excess_block_array, startBlock + blockCount); _header_block.XBATStart = startBlock + blockCount; } else { rvalue = BATBlock.CreateXBATBlocks(bigBlockSize, new int[0], 0); _header_block.XBATStart = POIFSConstants.END_OF_CHAIN; } _header_block.XBATCount = rvalue.Length; return(rvalue); }
//public static BATBlock CreateBATBlock(POIFSBigBlockSize bigBlockSize, byte[] data) public static BATBlock CreateBATBlock(POIFSBigBlockSize bigBlockSize, ByteBuffer data) { // Create an empty block BATBlock block = new BATBlock(bigBlockSize); // Fill it byte[] buffer = new byte[LittleEndianConsts.INT_SIZE]; //int index = 0; for (int i = 0; i < block._values.Length; i++) { //data.Read(buffer, 0, buffer.Length); //for (int j = 0; j < buffer.Length; j++, index++) // buffer[j] = data[index]; data.Read(buffer); block._values[i] = LittleEndian.GetInt(buffer); } block.RecomputeFree(); // All done return(block); }
//public static void WriteBlock(BATBlock bat, byte[] block) public static void WriteBlock(BATBlock bat, ByteBuffer block) { bat.WriteData(block); }
/// <summary> /// create the BATBlocks /// </summary> internal void SimpleCreateBlocks() { _blocks = BATBlock.CreateBATBlocks(_bigBlockSize, _entries.ToArray()); }
public BATBlockAndIndex(int index, BATBlock block) { this.index = index; this.block = block; }
/// <summary> /// For a given number of BAT blocks, calculate how many XBAT /// blocks will be needed /// </summary> /// <param name="blockCount">number of BAT blocks</param> /// <returns>number of XBAT blocks needed</returns> public static int CalculateXBATStorageRequirements(POIFSBigBlockSize bigBlockSize, int blockCount) { return((blockCount > _max_bats_in_header) ? BATBlock.CalculateXBATStorageRequirements(bigBlockSize, blockCount - _max_bats_in_header) : 0); }