Search in sources :

Example 6 with POIFSBigBlockSize

use of org.apache.poi.poifs.common.POIFSBigBlockSize in project poi by apache.

the class BATBlock method getSBATBlockAndIndex.

/**
     * Returns the BATBlock that handles the specified offset,
     *  and the relative index within it, for the mini stream.
     * The List of BATBlocks must be in sequential order
     */
public static BATBlockAndIndex getSBATBlockAndIndex(final int offset, final HeaderBlock header, final List<BATBlock> sbats) {
    POIFSBigBlockSize bigBlockSize = header.getBigBlockSize();
    int entriesPerBlock = bigBlockSize.getBATEntriesPerBlock();
    // SBATs are so much easier, as they're chained streams
    int whichSBAT = offset / entriesPerBlock;
    int index = offset % entriesPerBlock;
    return new BATBlockAndIndex(index, sbats.get(whichSBAT));
}
Also used : POIFSBigBlockSize(org.apache.poi.poifs.common.POIFSBigBlockSize)

Example 7 with POIFSBigBlockSize

use of org.apache.poi.poifs.common.POIFSBigBlockSize in project poi by apache.

the class DocumentBlock method getDataInputBlock.

public static DataInputBlock getDataInputBlock(DocumentBlock[] blocks, int offset) {
    if (blocks == null || blocks.length == 0) {
        return null;
    }
    // Key things about the size of the block
    POIFSBigBlockSize bigBlockSize = blocks[0].bigBlockSize;
    int BLOCK_SHIFT = bigBlockSize.getHeaderValue();
    int BLOCK_SIZE = bigBlockSize.getBigBlockSize();
    int BLOCK_MASK = BLOCK_SIZE - 1;
    // Now do the offset lookup
    int firstBlockIndex = offset >> BLOCK_SHIFT;
    int firstBlockOffset = offset & BLOCK_MASK;
    return new DataInputBlock(blocks[firstBlockIndex]._data, firstBlockOffset);
}
Also used : POIFSBigBlockSize(org.apache.poi.poifs.common.POIFSBigBlockSize)

Example 8 with POIFSBigBlockSize

use of org.apache.poi.poifs.common.POIFSBigBlockSize in project poi by apache.

the class TestBlockAllocationTableReader method testBadSectorAllocationTableSize_bug48085.

/**
	 * Bugzilla 48085 describes an error where a corrupted Excel file causes POI to throw an
	 * {@link OutOfMemoryError}.
	 */
public void testBadSectorAllocationTableSize_bug48085() {
    int BLOCK_SIZE = 512;
    POIFSBigBlockSize bigBlockSize = POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS;
    assertEquals(BLOCK_SIZE, bigBlockSize.getBigBlockSize());
    // 512 bytes take from the start of bugzilla attachment 24444
    byte[] initData = HexRead.readFromString("D0 CF 11 E0 A1 B1 1A E1 20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20 3E 20 03 20 FE FF 09 20" + "06 20 20 20 20 20 20 20 20 20 20 20 01 20 20 20  01 20 20 20 20 20 20 20 20 10 20 20 02 20 20 20" + "02 20 20 20 FE FF FF FF 20 20 20 20 20 20 20 20  ");
    // the rest of the block is 'FF'
    byte[] data = new byte[BLOCK_SIZE];
    Arrays.fill(data, (byte) 0xFF);
    System.arraycopy(initData, 0, data, 0, initData.length);
    // similar code to POIFSFileSystem.<init>:
    InputStream stream = new ByteArrayInputStream(data);
    HeaderBlock hb;
    RawDataBlockList dataBlocks;
    try {
        hb = new HeaderBlock(stream);
        dataBlocks = new RawDataBlockList(stream, bigBlockSize);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    try {
        new BlockAllocationTableReader(POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS, hb.getBATCount(), hb.getBATArray(), hb.getXBATCount(), hb.getXBATIndex(), dataBlocks);
    } catch (IOException e) {
        // expected during successful test
        assertEquals("Block count 538976257 is too high. POI maximum is 65535.", e.getMessage());
    } catch (OutOfMemoryError e) {
        if (e.getStackTrace()[1].getMethodName().equals("testBadSectorAllocationTableSize")) {
            throw new AssertionFailedError("Identified bug 48085");
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) POIFSBigBlockSize(org.apache.poi.poifs.common.POIFSBigBlockSize) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) AssertionFailedError(junit.framework.AssertionFailedError)

Aggregations

POIFSBigBlockSize (org.apache.poi.poifs.common.POIFSBigBlockSize)8 InputStream (java.io.InputStream)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 HeaderBlock (org.apache.poi.poifs.storage.HeaderBlock)2 RawDataBlockList (org.apache.poi.poifs.storage.RawDataBlockList)2 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 AssertionFailedError (junit.framework.AssertionFailedError)1 POIDataSamples (org.apache.poi.POIDataSamples)1 PropertyTable (org.apache.poi.poifs.property.PropertyTable)1 BlockAllocationTableReader (org.apache.poi.poifs.storage.BlockAllocationTableReader)1