Java Code Examples for org.apache.poi.poifs.filesystem.Entry

The following examples show how to use org.apache.poi.poifs.filesystem.Entry. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: DocBleach   Source File: SummaryInformationSanitiser.java    License: MIT License 6 votes vote down vote up
@Override
public boolean test(Entry entry) {
  String entryName = entry.getName();

  if (!SummaryInformation.DEFAULT_STREAM_NAME.equals(entryName)) {
    return true;
  }

  if (!(entry instanceof DocumentEntry)) {
    return true;
  }

  DocumentEntry dsiEntry = (DocumentEntry) entry;
  sanitizeSummaryInformation(session, dsiEntry);

  return true;
}
 
Example 2
Source Project: DocBleach   Source File: SummaryInformationSanitiserTest.java    License: MIT License 6 votes vote down vote up
@Test
void test1() {
  // Test an invalid stream, should be ignored
  Entry entry = mock(Entry.class);
  doReturn("\005RandomString").when(entry).getName();
  assertTrue(instance.test(entry));
  verify(instance, never()).sanitizeSummaryInformation(eq(session), (DocumentEntry) any());

  // Test a valid stream name, but wrong type (should be ignored)
  reset(entry);
  doReturn(SummaryInformation.DEFAULT_STREAM_NAME).when(entry).getName();
  assertTrue(instance.test(entry));
  verify(instance, never()).sanitizeSummaryInformation(eq(session), (DocumentEntry) any());

  reset(instance, entry);

  // Test a valid SummaryInformation name
  DocumentEntry docEntry = mock(DocumentEntry.class);

  doReturn(SummaryInformation.DEFAULT_STREAM_NAME).when(docEntry).getName();
  doNothing().when(instance).sanitizeSummaryInformation(session, docEntry);
  assertTrue(instance.test(docEntry));
  verify(instance, atLeastOnce()).sanitizeSummaryInformation(session, docEntry);
}
 
Example 3
Source Project: dremio-oss   Source File: DirectoryNode.java    License: Apache License 2.0 6 votes vote down vote up
public DirectoryNode(final DirectoryProperty property, DirectoryNode parent, final BlockStore blockStore) {
  super(property, parent);

  Iterator<Property> iter = property.getChildren();

  while (iter.hasNext()) {
    Property child = iter.next();
    Entry childNode;

    if (child.isDirectory()) {
      DirectoryProperty childDir = (DirectoryProperty) child;
      childNode = new DirectoryNode(childDir, this, blockStore);
    } else {
      childNode = new DocumentNode((DocumentProperty) child, this);
    }
    byname.put(childNode.getName(), childNode);
  }

}
 
Example 4
Source Project: lams   Source File: PropertySet.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Writes a property set to a document in a POI filesystem directory.
 *
 * @param dir The directory in the POI filesystem to write the document to.
 * @param name The document's name. If there is already a document with the
 * same name in the directory the latter will be overwritten.
 *
 * @throws WritingNotSupportedException if the filesystem doesn't support writing
 * @throws IOException if the old entry can't be deleted or the new entry be written
 */
public void write(final DirectoryEntry dir, final String name)
throws WritingNotSupportedException, IOException {
    /* If there is already an entry with the same name, remove it. */
    if (dir.hasEntry(name)) {
        final Entry e = dir.getEntry(name);
        e.delete();
    }

    /* Create the new entry. */
    dir.createDocument(name, toInputStream());
}
 
Example 5
Source Project: lams   Source File: POIFSLister.java    License: GNU General Public License v2.0 5 votes vote down vote up
public static void displayDirectory(DirectoryNode dir, String indent, boolean withSizes) {
   System.out.println(indent + dir.getName() + " -");
   String newIndent = indent + "  ";

   boolean hadChildren = false;
   for(Iterator<Entry> it = dir.getEntries(); it.hasNext();) {
      hadChildren = true;
      Entry entry = it.next();
      if (entry instanceof DirectoryNode) {
         displayDirectory((DirectoryNode) entry, newIndent, withSizes);
      } else {
         DocumentNode doc = (DocumentNode) entry;
         String name = doc.getName();
         String size = "";
         if (name.charAt(0) < 10) {
            String altname = "(0x0" + (int) name.charAt(0) + ")" + name.substring(1);
            name = name.substring(1) + " <" + altname + ">";
         }
         if (withSizes) {
            size = " [" + doc.getSize() + " / 0x" + 
                   Integer.toHexString(doc.getSize()) + "]";
         }
         System.out.println(newIndent + name + size);
      }
   }
   if (!hadChildren) {
      System.out.println(newIndent + "(no children)");
   }
}
 
Example 6
Source Project: lams   Source File: POIFSDump.java    License: GNU General Public License v2.0 5 votes vote down vote up
public static void dump(DirectoryEntry root, File parent) throws IOException {
    for(Iterator<Entry> it = root.getEntries(); it.hasNext();){
        Entry entry = it.next();
        if(entry instanceof DocumentNode){
            DocumentNode node = (DocumentNode)entry;
            DocumentInputStream is = new DocumentInputStream(node);
            byte[] bytes = IOUtils.toByteArray(is);
            is.close();

            OutputStream out = new FileOutputStream(new File(parent, node.getName().trim()));
            try {
            	out.write(bytes);
            } finally {
            	out.close();
            }
        } else if (entry instanceof DirectoryEntry){
            DirectoryEntry dir = (DirectoryEntry)entry;
            File file = new File(parent, entry.getName());
            if(!file.exists() && !file.mkdirs()) {
                throw new IOException("Could not create directory " + file);
            }
            dump(dir, file);
        } else {
            System.err.println("Skipping unsupported POIFS entry: " + entry);
        }
    }
}
 
Example 7
Source Project: lams   Source File: VBAMacroReader.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Reads all macros from all modules of the opened office file. 
 * @return All the macros and their contents
 *
 * @since 3.15-beta2
 */
public Map<String, String> readMacros() throws IOException {
    final ModuleMap modules = new ModuleMap();
    findMacros(fs.getRoot(), modules);
    
    Map<String, String> moduleSources = new HashMap<String, String>();
    for (Map.Entry<String, Module> entry : modules.entrySet()) {
        Module module = entry.getValue();
        if (module.buf != null && module.buf.length > 0) { // Skip empty modules
            moduleSources.put(entry.getKey(), new String(module.buf, modules.charset));
        }
    }
    return moduleSources;
}
 
Example 8
Source Project: lams   Source File: VBAMacroReader.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Recursively traverses directory structure rooted at <tt>dir</tt>.
 * For each macro module that is found, the module's name and code are
 * added to <tt>modules<tt>.
 *
 * @param dir The directory of entries to look at
 * @param modules The resulting map of modules
 * @throws IOException If reading the VBA module fails
 * @since 3.15-beta2
 */
protected void findMacros(DirectoryNode dir, ModuleMap modules) throws IOException {
    if (VBA_PROJECT_POIFS.equalsIgnoreCase(dir.getName())) {
        // VBA project directory, process
        readMacros(dir, modules);
    } else {
        // Check children
        for (Entry child : dir) {
            if (child instanceof DirectoryNode) {
                findMacros((DirectoryNode)child, modules);
            }
        }
    }
}
 
Example 9
Source Project: lams   Source File: HSSFObjectData.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public DirectoryEntry getDirectory() throws IOException {
    EmbeddedObjectRefSubRecord subRecord = findObjectRecord();

    int streamId = subRecord.getStreamId().intValue();
    String streamName = "MBD" + HexDump.toHex(streamId);

    Entry entry = _root.getEntry(streamName);
    if (entry instanceof DirectoryEntry) {
        return (DirectoryEntry) entry;
    }
    throw new IOException("Stream " + streamName + " was not an OLE2 directory");
}
 
Example 10
Source Project: DocBleach   Source File: ObjectRemover.java    License: MIT License 5 votes vote down vote up
@Override
public boolean test(Entry entry) {
  String entryName = entry.getName();

  if (!isObject(entryName)) {
    return true;
  }

  LOGGER.info("Found Compound Objects, removing them.");
  StringBuilder infos = new StringBuilder();
  if (entry instanceof DirectoryEntry) {
    Set<String> entryNames = ((DirectoryEntry) entry).getEntryNames();
    LOGGER.trace("Compound Objects' entries: {}", entryNames);
    infos.append("Entries: ").append(entryNames);
  } else if (entry instanceof DocumentEntry) {
    int size = ((DocumentEntry) entry).getSize();
    infos.append("Size: ").append(size);
  }

  Threat threat = Threat.builder()
      .type(ThreatType.EXTERNAL_CONTENT)
      .severity(ThreatSeverity.HIGH)
      .action(ThreatAction.REMOVE)
      .location(entryName)
      .details(infos.toString())
      .build();

  session.recordThreat(threat);

  return false;
}
 
Example 11
Source Project: DocBleach   Source File: MacroRemover.java    License: MIT License 5 votes vote down vote up
@Override
public boolean test(Entry entry) {
  String entryName = entry.getName();

  // Matches _VBA_PROJECT_CUR, VBA, ... :)
  if (!isMacro(entryName)) {
    return true;
  }

  LOGGER.info("Found Macros, removing them.");
  StringBuilder infos = new StringBuilder();
  if (entry instanceof DirectoryEntry) {
    Set<String> entryNames = ((DirectoryEntry) entry).getEntryNames();
    LOGGER.trace("Macros' entries: {}", entryNames);
    infos.append("Entries: ").append(entryNames);
  } else if (entry instanceof DocumentEntry) {
    int size = ((DocumentEntry) entry).getSize();
    infos.append("Size: ").append(size);
  }

  Threat threat = Threat.builder()
      .type(ThreatType.ACTIVE_CONTENT)
      .severity(ThreatSeverity.EXTREME)
      .action(ThreatAction.REMOVE)
      .location(entryName)
      .details(infos.toString())
      .build();

  session.recordThreat(threat);

  return false;
}
 
Example 12
Source Project: DocBleach   Source File: OLE2Bleach.java    License: MIT License 5 votes vote down vote up
protected void sanitize(BleachSession session, DirectoryEntry rootIn, DirectoryEntry rootOut) {
  LOGGER.debug("Entries before: {}", rootIn.getEntryNames());
  // Save the changes to a new file

  // Returns false if the entry should be removed
  Predicate<Entry> visitor =
      ((Predicate<Entry>) (e -> true))
          .and(new MacroRemover(session))
          .and(new ObjectRemover(session))
          .and(new SummaryInformationSanitiser(session));

  LOGGER.debug("Root ClassID: {}", rootIn.getStorageClsid());
  // https://blogs.msdn.microsoft.com/heaths/2006/02/27/identifying-windows-installer-file-types/
  rootOut.setStorageClsid(rootIn.getStorageClsid());

  rootIn
      .getEntries()
      .forEachRemaining(
          entry -> {
            if (!visitor.test(entry)) {
              return;
            }
            copyNodesRecursively(session, entry, rootOut);
          });

  LOGGER.debug("Entries after: {}", rootOut.getEntryNames());
  // Save the changes to a new file
}
 
Example 13
Source Project: DocBleach   Source File: MacroRemoverTest.java    License: MIT License 5 votes vote down vote up
@Test
void testRemovesMacro() {
  Entry entry = mock(Entry.class);
  doReturn("_VBA_PROJECT_CUR").when(entry).getName();
  assertFalse(instance.test(entry), "A macro entry should not be copied over");
  BleachTestBase.assertThreatsFound(session, 1);
  reset(session);
}
 
Example 14
Source Project: DocBleach   Source File: MacroRemoverTest.java    License: MIT License 5 votes vote down vote up
@Test
void testKeepsEverythingElse() {
  Entry entry = mock(Entry.class);
  doReturn(SummaryInformation.DEFAULT_STREAM_NAME).when(entry).getName();
  assertTrue(instance.test(entry), "Non-macro streams should be ignored");
  BleachTestBase.assertThreatsFound(session, 0);
  reset(session);

  doReturn("RandomName").when(entry).getName();
  assertTrue(instance.test(entry), "Non-macro streams should be ignored");
  BleachTestBase.assertThreatsFound(session, 0);
  reset(session);
}
 
Example 15
Source Project: DocBleach   Source File: ObjectRemoverTest.java    License: MIT License 5 votes vote down vote up
@Test
void testRemovesMacro() {
  Entry entry = mock(Entry.class);
  doReturn("\u0001CompObj").when(entry).getName();
  assertFalse(instance.test(entry), "An object entry should not be copied over");
  BleachTestBase.assertThreatsFound(session, 1);
  reset(session);
}
 
Example 16
Source Project: DocBleach   Source File: ObjectRemoverTest.java    License: MIT License 5 votes vote down vote up
@Test
void testKeepsEverythingElse() {
  Entry entry = mock(Entry.class);
  doReturn(SummaryInformation.DEFAULT_STREAM_NAME).when(entry).getName();
  assertTrue(instance.test(entry), "Non-object entries should be ignored");
  BleachTestBase.assertThreatsFound(session, 0);
  reset(session);

  doReturn("RandomName").when(entry).getName();
  assertTrue(instance.test(entry), "Non-object entries should be ignored");
  BleachTestBase.assertThreatsFound(session, 0);
  reset(session);
}
 
Example 17
Source Project: dremio-oss   Source File: DirectoryNode.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * get a specified Entry by name
 *
 * @param name the name of the Entry to obtain.
 *
 * @return the specified Entry, if it is directly contained in
 *         this DirectoryEntry
 *
 * @exception IllegalStateException if no Entry with the specified name exists in this DirectoryEntry
 */

public Entry getEntry(final String name) {
  Entry rval = null;

  if (name != null) {
    rval = byname.get(name);
  }
  if (rval == null) { // either a null name was given, or there is no such name
    throw new IllegalStateException("no such entry: \"" + name + "\", had: " + byname.keySet());
  }
  return rval;
}
 
Example 18
Source Project: mpxj   Source File: PoiTreeView.java    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override public String convertValueToText(Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus)
{
   String result = "";
   if (value instanceof Entry)
   {
      result = ((Entry) value).getName();
   }
   return result;
}
 
Example 19
Source Project: mpxj   Source File: PoiTreeModel.java    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override public Object getChild(Object parent, int index)
{
   Object result = null;
   if (parent instanceof DirectoryEntry)
   {
      List<Entry> entries = getChildNodes((DirectoryEntry) parent);
      if (entries.size() > index)
      {
         result = entries.get(index);
      }
   }
   return result;
}
 
Example 20
Source Project: mpxj   Source File: PoiTreeModel.java    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override public int getIndexOfChild(Object parent, Object child)
{
   int result = -1;
   if (parent instanceof DirectoryEntry)
   {
      List<Entry> entries = getChildNodes((DirectoryEntry) parent);
      result = entries.indexOf(child);
   }

   return result;
}
 
Example 21
Source Project: mpxj   Source File: PoiTreeModel.java    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Retrieves child nodes from a directory entry.
 *
 * @param parent parent directory entry
 * @return list of child nodes
 */
private List<Entry> getChildNodes(DirectoryEntry parent)
{
   List<Entry> result = new ArrayList<>();
   Iterator<Entry> entries = parent.getEntries();
   while (entries.hasNext())
   {
      result.add(entries.next());
   }
   return result;
}
 
Example 22
Source Project: lams   Source File: VBAMacroReader.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * Reads VBA Project modules from a VBA Project directory located at
 * <tt>macroDir</tt> into <tt>modules</tt>.
 *
 * @since 3.15-beta2
 */    
protected void readMacros(DirectoryNode macroDir, ModuleMap modules) throws IOException {
    for (Entry entry : macroDir) {
        if (! (entry instanceof DocumentNode)) { continue; }
        
        String name = entry.getName();
        DocumentNode document = (DocumentNode)entry;
        DocumentInputStream dis = new DocumentInputStream(document);
        try {
            if ("dir".equalsIgnoreCase(name)) {
                // process DIR
                RLEDecompressingInputStream in = new RLEDecompressingInputStream(dis);
                String streamName = null;
                int recordId = 0;
                try {
                    while (true) {
                        recordId = in.readShort();
                        if (EOF == recordId
                                || VERSION_INDEPENDENT_TERMINATOR == recordId) {
                            break;
                        }
                        int recordLength = in.readInt();
                        switch (recordId) {
                        case PROJECTVERSION:
                            trySkip(in, 6);
                            break;
                        case PROJECTCODEPAGE:
                            int codepage = in.readShort();
                            modules.charset = Charset.forName(CodePageUtil.codepageToEncoding(codepage, true));
                            break;
                        case STREAMNAME:
                            streamName = readString(in, recordLength, modules.charset);
                            int reserved = in.readShort();
                            if (reserved != STREAMNAME_RESERVED) {
                                throw new IOException("Expected x0032 after stream name before Unicode stream name, but found: "+
                                        Integer.toHexString(reserved));
                            }
                            int unicodeNameRecordLength = in.readInt();
                            readUnicodeString(in, unicodeNameRecordLength);
                            // do something with this at some point
                            break;
                        case MODULEOFFSET:
                            readModule(in, streamName, modules);
                            break;
                        default:
                            trySkip(in, recordLength);
                            break;
                        }
                    }
                } catch (final IOException e) {
                    throw new IOException(
                            "Error occurred while reading macros at section id "
                            + recordId + " (" + HexDump.shortToHex(recordId) + ")", e);
                }
                finally {
                    in.close();
                }
            } else if (!startsWithIgnoreCase(name, "__SRP")
                    && !startsWithIgnoreCase(name, "_VBA_PROJECT")) {
                // process module, skip __SRP and _VBA_PROJECT since these do not contain macros
                readModule(dis, name, modules);
            }
        }
        finally {
            dis.close();
        }
    }
}
 
Example 23
Source Project: lams   Source File: CryptoAPIEncryptor.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * Encrypt the Document-/SummaryInformation and other optionally streams.
 * Opposed to other crypto modes, cryptoapi is record based and can't be used
 * to stream-encrypt a whole file
 * 
 * @see <a href="http://msdn.microsoft.com/en-us/library/dd943321(v=office.12).aspx">2.3.5.4 RC4 CryptoAPI Encrypted Summary Stream</a>
 */
public void setSummaryEntries(DirectoryNode dir, String encryptedStream, NPOIFSFileSystem entries)
throws IOException, GeneralSecurityException {
    CryptoAPIDocumentOutputStream bos = new CryptoAPIDocumentOutputStream(this); // NOSONAR
    byte buf[] = new byte[8];
    
    bos.write(buf, 0, 8); // skip header
    List<StreamDescriptorEntry> descList = new ArrayList<StreamDescriptorEntry>();

    int block = 0;
    for (Entry entry : entries.getRoot()) {
        if (entry.isDirectoryEntry()) {
            continue;
        }
        StreamDescriptorEntry descEntry = new StreamDescriptorEntry();
        descEntry.block = block;
        descEntry.streamOffset = bos.size();
        descEntry.streamName = entry.getName();
        descEntry.flags = StreamDescriptorEntry.flagStream.setValue(0, 1);
        descEntry.reserved2 = 0;
        
        bos.setBlock(block);
        DocumentInputStream dis = dir.createDocumentInputStream(entry);
        IOUtils.copy(dis, bos);
        dis.close();
        
        descEntry.streamSize = bos.size() - descEntry.streamOffset;
        descList.add(descEntry);
        
        block++;
    }
    
    int streamDescriptorArrayOffset = bos.size();
    
    bos.setBlock(0);
    LittleEndian.putUInt(buf, 0, descList.size());
    bos.write(buf, 0, 4);
    
    for (StreamDescriptorEntry sde : descList) {
        LittleEndian.putUInt(buf, 0, sde.streamOffset);
        bos.write(buf, 0, 4);
        LittleEndian.putUInt(buf, 0, sde.streamSize);
        bos.write(buf, 0, 4);
        LittleEndian.putUShort(buf, 0, sde.block);
        bos.write(buf, 0, 2);
        LittleEndian.putUByte(buf, 0, (short)sde.streamName.length());
        bos.write(buf, 0, 1);
        LittleEndian.putUByte(buf, 0, (short)sde.flags);
        bos.write(buf, 0, 1);
        LittleEndian.putUInt(buf, 0, sde.reserved2);
        bos.write(buf, 0, 4);
        byte nameBytes[] = StringUtil.getToUnicodeLE(sde.streamName);
        bos.write(nameBytes, 0, nameBytes.length);
        LittleEndian.putShort(buf, 0, (short)0); // null-termination
        bos.write(buf, 0, 2);
    }
    
    int savedSize = bos.size();
    int streamDescriptorArraySize = savedSize - streamDescriptorArrayOffset;
    LittleEndian.putUInt(buf, 0, streamDescriptorArrayOffset);
    LittleEndian.putUInt(buf, 4, streamDescriptorArraySize);

    bos.reset();
    bos.setBlock(0);
    bos.write(buf, 0, 8);
    bos.setSize(savedSize);
    
    dir.createDocument(encryptedStream, new ByteArrayInputStream(bos.getBuf(), 0, savedSize));
}
 
Example 24
Source Project: mpxj   Source File: MppDump.java    License: GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * This method recursively descends the directory structure, dumping
 * details of any files it finds to the output file.
 *
 * @param pw Output PrintWriter
 * @param dir DirectoryEntry to dump
 * @param prefix prefix used to identify path to this object
 * @param showData flag indicating if data is dumped, or just structure
 * @param hex set to true if hex output is required
 * @param indent indent used if displaying structure only
 * @throws Exception Thrown on file read errors
 */
private static void dumpTree(PrintWriter pw, DirectoryEntry dir, String prefix, boolean showData, boolean hex, String indent) throws Exception
{
   long byteCount;

   for (Iterator<Entry> iter = dir.getEntries(); iter.hasNext();)
   {
      Entry entry = iter.next();
      if (entry instanceof DirectoryEntry)
      {
         String childIndent = indent;
         if (childIndent != null)
         {
            childIndent += " ";
         }

         String childPrefix = prefix + "[" + entry.getName() + "].";
         pw.println("start dir: " + prefix + entry.getName());
         dumpTree(pw, (DirectoryEntry) entry, childPrefix, showData, hex, childIndent);
         pw.println("end dir: " + prefix + entry.getName());
      }
      else
         if (entry instanceof DocumentEntry)
         {
            if (showData)
            {
               pw.println("start doc: " + prefix + entry.getName());
               if (hex == true)
               {
                  byteCount = hexdump(new DocumentInputStream((DocumentEntry) entry), pw);
               }
               else
               {
                  byteCount = asciidump(new DocumentInputStream((DocumentEntry) entry), pw);
               }
               pw.println("end doc: " + prefix + entry.getName() + " (" + byteCount + " bytes read)");
            }
            else
            {
               if (indent != null)
               {
                  pw.print(indent);
               }
               pw.println("doc: " + prefix + entry.getName());
            }
         }
         else
         {
            pw.println("found unknown: " + prefix + entry.getName());
         }
   }
}
 
Example 25
Source Project: dremio-oss   Source File: XlsReader.java    License: Apache License 2.0 3 votes vote down vote up
private DocumentEntry getWorkbookEntry() {
  final DirectoryNode dir = new DirectoryNode(propertyTable.getRoot(), null, difats);

  String name = getWorkbookDirEntryName(dir);

  Entry workbook = dir.getEntry(name);
  Preconditions.checkState(!workbook.isDocumentEntry(), "Entry '" + workbook.getName() + "' is not a DocumentEntry");

  return (DocumentEntry) workbook;
}