Search in sources :

Example 1 with NPropertyTable

use of org.apache.poi.poifs.property.NPropertyTable in project poi by apache.

the class POIFSDump method main.

public static void main(String[] args) throws IOException {
    if (args.length == 0) {
        System.err.println("Must specify at least one file to dump");
        System.exit(1);
    }
    boolean dumpProps = false, dumpMini = false;
    for (String filename : args) {
        if (filename.equalsIgnoreCase("-dumprops") || filename.equalsIgnoreCase("-dump-props") || filename.equalsIgnoreCase("-dump-properties")) {
            dumpProps = true;
            continue;
        }
        if (filename.equalsIgnoreCase("-dumpmini") || filename.equalsIgnoreCase("-dump-mini") || filename.equalsIgnoreCase("-dump-ministream") || filename.equalsIgnoreCase("-dump-mini-stream")) {
            dumpMini = true;
            continue;
        }
        System.out.println("Dumping " + filename);
        FileInputStream is = new FileInputStream(filename);
        NPOIFSFileSystem fs;
        try {
            fs = new NPOIFSFileSystem(is);
        } finally {
            is.close();
        }
        try {
            DirectoryEntry root = fs.getRoot();
            String filenameWithoutPath = new File(filename).getName();
            File dumpDir = new File(filenameWithoutPath + "_dump");
            File file = new File(dumpDir, root.getName());
            if (!file.exists() && !file.mkdirs()) {
                throw new IOException("Could not create directory " + file);
            }
            dump(root, file);
            if (dumpProps) {
                HeaderBlock header = fs.getHeaderBlock();
                dump(fs, header.getPropertyStart(), "properties", file);
            }
            if (dumpMini) {
                NPropertyTable props = fs.getPropertyTable();
                int startBlock = props.getRoot().getStartBlock();
                if (startBlock == POIFSConstants.END_OF_CHAIN) {
                    System.err.println("No Mini Stream in file");
                } else {
                    dump(fs, startBlock, "mini-stream", file);
                }
            }
        } finally {
            fs.close();
        }
    }
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) HeaderBlock(org.apache.poi.poifs.storage.HeaderBlock) NPropertyTable(org.apache.poi.poifs.property.NPropertyTable) IOException(java.io.IOException) DirectoryEntry(org.apache.poi.poifs.filesystem.DirectoryEntry) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 2 with NPropertyTable

use of org.apache.poi.poifs.property.NPropertyTable in project poi by apache.

the class TestNPOIFSFileSystem method writeZeroLengthEntries.

@Test
public void writeZeroLengthEntries() throws IOException {
    NPOIFSFileSystem fs1 = new NPOIFSFileSystem();
    DirectoryNode testDir = fs1.getRoot();
    DocumentEntry miniDoc;
    DocumentEntry normDoc;
    DocumentEntry emptyDoc;
    // Add mini and normal sized entries to start
    byte[] mini2 = new byte[] { -42, 0, -1, -2, -3, -4, -42 };
    testDir.createDocument("Mini2", new ByteArrayInputStream(mini2));
    // Add to the main stream
    byte[] main4106 = new byte[4106];
    main4106[0] = 41;
    main4106[4105] = 42;
    testDir.createDocument("Normal4106", new ByteArrayInputStream(main4106));
    // Now add some empty ones
    byte[] empty = new byte[0];
    testDir.createDocument("empty-1", new ByteArrayInputStream(empty));
    testDir.createDocument("empty-2", new ByteArrayInputStream(empty));
    testDir.createDocument("empty-3", new ByteArrayInputStream(empty));
    // Check
    miniDoc = (DocumentEntry) testDir.getEntry("Mini2");
    assertContentsMatches(mini2, miniDoc);
    normDoc = (DocumentEntry) testDir.getEntry("Normal4106");
    assertContentsMatches(main4106, normDoc);
    emptyDoc = (DocumentEntry) testDir.getEntry("empty-1");
    assertContentsMatches(empty, emptyDoc);
    emptyDoc = (DocumentEntry) testDir.getEntry("empty-2");
    assertContentsMatches(empty, emptyDoc);
    emptyDoc = (DocumentEntry) testDir.getEntry("empty-3");
    assertContentsMatches(empty, emptyDoc);
    // Look at the properties entry, and check the empty ones
    //  have zero size and no start block
    NPropertyTable props = fs1._get_property_table();
    Iterator<Property> propsIt = props.getRoot().getChildren();
    Property prop = propsIt.next();
    assertEquals("Mini2", prop.getName());
    assertEquals(0, prop.getStartBlock());
    assertEquals(7, prop.getSize());
    prop = propsIt.next();
    assertEquals("Normal4106", prop.getName());
    // BAT, Props, SBAT, MIni
    assertEquals(4, prop.getStartBlock());
    assertEquals(4106, prop.getSize());
    prop = propsIt.next();
    assertEquals("empty-1", prop.getName());
    assertEquals(POIFSConstants.END_OF_CHAIN, prop.getStartBlock());
    assertEquals(0, prop.getSize());
    prop = propsIt.next();
    assertEquals("empty-2", prop.getName());
    assertEquals(POIFSConstants.END_OF_CHAIN, prop.getStartBlock());
    assertEquals(0, prop.getSize());
    prop = propsIt.next();
    assertEquals("empty-3", prop.getName());
    assertEquals(POIFSConstants.END_OF_CHAIN, prop.getStartBlock());
    assertEquals(0, prop.getSize());
    // Save and re-check
    NPOIFSFileSystem fs2 = writeOutAndReadBack(fs1);
    fs1.close();
    testDir = fs2.getRoot();
    miniDoc = (DocumentEntry) testDir.getEntry("Mini2");
    assertContentsMatches(mini2, miniDoc);
    normDoc = (DocumentEntry) testDir.getEntry("Normal4106");
    assertContentsMatches(main4106, normDoc);
    emptyDoc = (DocumentEntry) testDir.getEntry("empty-1");
    assertContentsMatches(empty, emptyDoc);
    emptyDoc = (DocumentEntry) testDir.getEntry("empty-2");
    assertContentsMatches(empty, emptyDoc);
    emptyDoc = (DocumentEntry) testDir.getEntry("empty-3");
    assertContentsMatches(empty, emptyDoc);
    // Check that a mini-stream was assigned, with one block used
    assertEquals(3, testDir.getProperty().getStartBlock());
    assertEquals(64, testDir.getProperty().getSize());
    // All done
    fs2.close();
}
Also used : NPropertyTable(org.apache.poi.poifs.property.NPropertyTable) Property(org.apache.poi.poifs.property.Property) DirectoryProperty(org.apache.poi.poifs.property.DirectoryProperty) RootProperty(org.apache.poi.poifs.property.RootProperty) Test(org.junit.Test)

Example 3 with NPropertyTable

use of org.apache.poi.poifs.property.NPropertyTable in project poi by apache.

the class NPOIFSFileSystem method readCoreContents.

/**
     * Read and process the PropertiesTable and the
     *  FAT / XFAT blocks, so that we're ready to
     *  work with the file
     */
private void readCoreContents() throws IOException {
    // Grab the block size
    bigBlockSize = _header.getBigBlockSize();
    // Each block should only ever be used by one of the
    //  FAT, XFAT or Property Table. Ensure it does
    ChainLoopDetector loopDetector = getChainLoopDetector();
    // Read the FAT blocks
    for (int fatAt : _header.getBATArray()) {
        readBAT(fatAt, loopDetector);
    }
    // Work out how many FAT blocks remain in the XFATs
    int remainingFATs = _header.getBATCount() - _header.getBATArray().length;
    // Now read the XFAT blocks, and the FATs within them
    BATBlock xfat;
    int nextAt = _header.getXBATIndex();
    for (int i = 0; i < _header.getXBATCount(); i++) {
        loopDetector.claim(nextAt);
        ByteBuffer fatData = getBlockAt(nextAt);
        xfat = BATBlock.createBATBlock(bigBlockSize, fatData);
        xfat.setOurBlockIndex(nextAt);
        nextAt = xfat.getValueAt(bigBlockSize.getXBATEntriesPerBlock());
        _xbat_blocks.add(xfat);
        // Process all the (used) FATs from this XFAT
        int xbatFATs = Math.min(remainingFATs, bigBlockSize.getXBATEntriesPerBlock());
        for (int j = 0; j < xbatFATs; j++) {
            int fatAt = xfat.getValueAt(j);
            if (fatAt == POIFSConstants.UNUSED_BLOCK || fatAt == POIFSConstants.END_OF_CHAIN)
                break;
            readBAT(fatAt, loopDetector);
        }
        remainingFATs -= xbatFATs;
    }
    // We're now able to load steams
    // Use this to read in the properties
    _property_table = new NPropertyTable(_header, this);
    // Finally read the Small Stream FAT (SBAT) blocks
    BATBlock sfat;
    List<BATBlock> sbats = new ArrayList<BATBlock>();
    _mini_store = new NPOIFSMiniStore(this, _property_table.getRoot(), sbats, _header);
    nextAt = _header.getSBATStart();
    for (int i = 0; i < _header.getSBATCount() && nextAt != POIFSConstants.END_OF_CHAIN; i++) {
        loopDetector.claim(nextAt);
        ByteBuffer fatData = getBlockAt(nextAt);
        sfat = BATBlock.createBATBlock(bigBlockSize, fatData);
        sfat.setOurBlockIndex(nextAt);
        sbats.add(sfat);
        nextAt = getNextBlock(nextAt);
    }
}
Also used : BATBlock(org.apache.poi.poifs.storage.BATBlock) NPropertyTable(org.apache.poi.poifs.property.NPropertyTable) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer)

Example 4 with NPropertyTable

use of org.apache.poi.poifs.property.NPropertyTable in project poi by apache.

the class TestPOIFSDump method testFailToWrite.

@Test(expected = IndexOutOfBoundsException.class)
public void testFailToWrite() throws IOException {
    File dir = TempFile.createTempFile("TestPOIFSDump", ".tst");
    assertTrue("Had: " + dir, dir.exists());
    assertTrue("Had: " + dir, dir.delete());
    assertTrue("Had: " + dir, dir.mkdirs());
    FileInputStream is = new FileInputStream(TEST_FILE);
    NPOIFSFileSystem fs = new NPOIFSFileSystem(is);
    is.close();
    NPropertyTable props = fs.getPropertyTable();
    assertNotNull(props);
    // try with an invalid startBlock to trigger an exception
    // to validate that file-handles are closed properly
    POIFSDump.dump(fs, 999999999, "mini-stream", dir);
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) NPropertyTable(org.apache.poi.poifs.property.NPropertyTable) TempFile(org.apache.poi.util.TempFile) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 5 with NPropertyTable

use of org.apache.poi.poifs.property.NPropertyTable in project poi by apache.

the class TestNPOIFSFileSystem method propertiesAndFatOnRead.

@Test
public void propertiesAndFatOnRead() throws IOException {
    NPOIFSFileSystem fsA, fsB;
    // With a simple 512 block file
    fsA = new NPOIFSFileSystem(_inst.getFile("BlockSize512.zvi"));
    fsB = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize512.zvi"));
    for (NPOIFSFileSystem fs : new NPOIFSFileSystem[] { fsA, fsB }) {
        // Check the FAT was properly processed:
        // Verify we only got one block
        fs.getBATBlockAndIndex(0);
        fs.getBATBlockAndIndex(1);
        try {
            fs.getBATBlockAndIndex(140);
            fail("Should only be one BAT, but a 2nd was found");
        } catch (IndexOutOfBoundsException e) {
        // expected here
        }
        // Verify a few next offsets
        // 97 -> 98 -> END
        assertEquals(98, fs.getNextBlock(97));
        assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(98));
        // Check the properties
        NPropertyTable props = fs._get_property_table();
        assertEquals(90, props.getStartBlock());
        assertEquals(7, props.countBlocks());
        // Root property tells us about the Mini Stream
        RootProperty root = props.getRoot();
        assertEquals("Root Entry", root.getName());
        assertEquals(11564, root.getSize());
        assertEquals(0, root.getStartBlock());
        // Check its children too
        Property prop;
        Iterator<Property> pi = root.getChildren();
        prop = pi.next();
        assertEquals("Thumbnail", prop.getName());
        prop = pi.next();
        assertEquals("DocumentSummaryInformation", prop.getName());
        prop = pi.next();
        assertEquals("SummaryInformation", prop.getName());
        prop = pi.next();
        assertEquals("Image", prop.getName());
        prop = pi.next();
        assertEquals("Tags", prop.getName());
        assertEquals(false, pi.hasNext());
        // Check the SBAT (Small Blocks FAT) was properly processed
        NPOIFSMiniStore ministore = fs.getMiniStore();
        // Verify we only got two SBAT blocks
        ministore.getBATBlockAndIndex(0);
        ministore.getBATBlockAndIndex(128);
        try {
            ministore.getBATBlockAndIndex(256);
            fail("Should only be two SBATs, but a 3rd was found");
        } catch (IndexOutOfBoundsException e) {
        // expected here
        }
        // Verify a few offsets: 0->50 is a stream
        for (int i = 0; i < 50; i++) {
            assertEquals(i + 1, ministore.getNextBlock(i));
        }
        assertEquals(POIFSConstants.END_OF_CHAIN, ministore.getNextBlock(50));
        fs.close();
    }
    // Now with a simple 4096 block file
    fsA = new NPOIFSFileSystem(_inst.getFile("BlockSize4096.zvi"));
    fsB = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize4096.zvi"));
    for (NPOIFSFileSystem fs : new NPOIFSFileSystem[] { fsA, fsB }) {
        // Check the FAT was properly processed
        // Verify we only got one block
        fs.getBATBlockAndIndex(0);
        fs.getBATBlockAndIndex(1);
        try {
            fs.getBATBlockAndIndex(1040);
            fail("Should only be one BAT, but a 2nd was found");
        } catch (IndexOutOfBoundsException e) {
        // expected here
        }
        // Verify a few next offsets
        // 0 -> 1 -> 2 -> END
        assertEquals(1, fs.getNextBlock(0));
        assertEquals(2, fs.getNextBlock(1));
        assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(2));
        // Check the properties
        NPropertyTable props = fs._get_property_table();
        assertEquals(12, props.getStartBlock());
        assertEquals(1, props.countBlocks());
        // Root property tells us about the Mini Stream
        RootProperty root = props.getRoot();
        assertEquals("Root Entry", root.getName());
        assertEquals(11564, root.getSize());
        assertEquals(0, root.getStartBlock());
        // Check its children too
        Property prop;
        Iterator<Property> pi = root.getChildren();
        prop = pi.next();
        assertEquals("Thumbnail", prop.getName());
        prop = pi.next();
        assertEquals("DocumentSummaryInformation", prop.getName());
        prop = pi.next();
        assertEquals("SummaryInformation", prop.getName());
        prop = pi.next();
        assertEquals("Image", prop.getName());
        prop = pi.next();
        assertEquals("Tags", prop.getName());
        assertEquals(false, pi.hasNext());
        // Check the SBAT (Small Blocks FAT) was properly processed
        NPOIFSMiniStore ministore = fs.getMiniStore();
        // Verify we only got one SBAT block
        ministore.getBATBlockAndIndex(0);
        ministore.getBATBlockAndIndex(128);
        ministore.getBATBlockAndIndex(1023);
        try {
            ministore.getBATBlockAndIndex(1024);
            fail("Should only be one SBAT, but a 2nd was found");
        } catch (IndexOutOfBoundsException e) {
        // expected here
        }
        // Verify a few offsets: 0->50 is a stream
        for (int i = 0; i < 50; i++) {
            assertEquals(i + 1, ministore.getNextBlock(i));
        }
        assertEquals(POIFSConstants.END_OF_CHAIN, ministore.getNextBlock(50));
        fs.close();
    }
}
Also used : RootProperty(org.apache.poi.poifs.property.RootProperty) NPropertyTable(org.apache.poi.poifs.property.NPropertyTable) Property(org.apache.poi.poifs.property.Property) DirectoryProperty(org.apache.poi.poifs.property.DirectoryProperty) RootProperty(org.apache.poi.poifs.property.RootProperty) Test(org.junit.Test)

Aggregations

NPropertyTable (org.apache.poi.poifs.property.NPropertyTable)6 Test (org.junit.Test)4 DirectoryProperty (org.apache.poi.poifs.property.DirectoryProperty)3 Property (org.apache.poi.poifs.property.Property)3 RootProperty (org.apache.poi.poifs.property.RootProperty)3 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)2 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 DirectoryEntry (org.apache.poi.poifs.filesystem.DirectoryEntry)1 BATBlock (org.apache.poi.poifs.storage.BATBlock)1 HeaderBlock (org.apache.poi.poifs.storage.HeaderBlock)1 TempFile (org.apache.poi.util.TempFile)1