Skip to content

Commit

Permalink
Bug47950 -- make stream/directory name lookup in OLE2 case insensitive
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1912438 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
tballison committed Sep 20, 2023
1 parent ce91967 commit 9911738
Show file tree
Hide file tree
Showing 70 changed files with 502 additions and 363 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ public static void handleEvent(final POIFSFileSystem poiFs, final POIFSReaderEve

for (int i=0; i<path.length(); i++) {
String subDir = path.getComponent(i);
if (de.hasEntry(subDir)) {
de = (DirectoryEntry)de.getEntry(subDir);
if (de.hasEntryCaseInsensitive(subDir)) {
de = (DirectoryEntry)de.getEntryCaseInsensitive(subDir);
} else {
de = de.createDirectory(subDir);
if (i == path.length()-1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private static void handleEvent(final POIFSFileSystem poiFs, final POIFSReaderEv

for (int i=0; i<path.length(); i++) {
String subDir = path.getComponent(i);
de = (de.hasEntry(subDir)) ? (DirectoryEntry)de.getEntry(subDir) : de.createDirectory(subDir);
de = (de.hasEntryCaseInsensitive(subDir)) ? (DirectoryEntry)de.getEntryCaseInsensitive(subDir) : de.createDirectory(subDir);
}

de.createDocument(event.getName(), is);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void handleFile(InputStream stream, String path) throws Exception {

private static boolean hasPropertyStream(POIFSFileSystem poifs, String streamName) throws IOException {
DirectoryNode root = poifs.getRoot();
if (!root.hasEntry(streamName)) {
if (!root.hasEntryCaseInsensitive(streamName)) {
return false;
}
try (DocumentInputStream dis = root.createDocumentInputStream(streamName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected void handlePOIXMLDocument(POIXMLDocument doc) throws Exception {
protected static boolean isEncrypted(InputStream stream) throws IOException {
if (FileMagic.valueOf(stream) == FileMagic.OLE2) {
try (POIFSFileSystem poifs = new POIFSFileSystem(stream)) {
if (poifs.getRoot().hasEntry(Decryptor.DEFAULT_POIFS_ENTRY)) {
if (poifs.getRoot().hasEntryCaseInsensitive(Decryptor.DEFAULT_POIFS_ENTRY)) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,13 @@ public POITextExtractor create(POIFSFileSystem fs) throws IOException {
@Override
public POITextExtractor create(DirectoryNode poifsDir, String password) throws IOException {
// First, check for plain OOXML package
if (poifsDir.hasEntry(OOXML_PACKAGE)) {
if (poifsDir.hasEntryCaseInsensitive(OOXML_PACKAGE)) {
try (InputStream is = poifsDir.createDocumentInputStream(OOXML_PACKAGE)) {
return create(is, password);
}
}

if (poifsDir.hasEntry(Decryptor.DEFAULT_POIFS_ENTRY)) {
if (poifsDir.hasEntryCaseInsensitive(Decryptor.DEFAULT_POIFS_ENTRY)) {
EncryptionInfo ei = new EncryptionInfo(poifsDir);
Decryptor dec = ei.getDecryptor();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ void agileEncryption() throws Exception {
assertEquals(decPackLenExpected, payloadExpected.length);

final DirectoryNode root = nfs.getRoot();
final DocumentEntry entry = (DocumentEntry)root.getEntry(Decryptor.DEFAULT_POIFS_ENTRY);
final DocumentEntry entry = (DocumentEntry)root.getEntryCaseInsensitive(Decryptor.DEFAULT_POIFS_ENTRY);
try (InputStream is = root.createDocumentInputStream(entry)) {
// ignore padding block
encPackExpected = IOUtils.toByteArray(is, entry.getSize()-16);
Expand Down Expand Up @@ -225,7 +225,7 @@ void agileEncryption() throws Exception {
decPackLenActual = decActual.getLength();

final DirectoryNode root = nfs.getRoot();
final DocumentEntry entry = (DocumentEntry)root.getEntry(Decryptor.DEFAULT_POIFS_ENTRY);
final DocumentEntry entry = (DocumentEntry)root.getEntryCaseInsensitive(Decryptor.DEFAULT_POIFS_ENTRY);
try (InputStream is = root.createDocumentInputStream(entry)) {
// ignore padding block
encPackActual = IOUtils.toByteArray(is, entry.getSize()-16);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3460,13 +3460,13 @@ void testXLSXinPPT() throws Exception {

// Will be OOXML wrapped in OLE2, not directly SpreadSheet
POIFSFileSystem fs = new POIFSFileSystem(data.getInputStream());
assertTrue(fs.getRoot().hasEntry(OOXML_PACKAGE));
assertFalse(fs.getRoot().hasEntry("Workbook"));
assertTrue(fs.getRoot().hasEntryCaseInsensitive(OOXML_PACKAGE));
assertFalse(fs.getRoot().hasEntryCaseInsensitive("Workbook"));


// Can fetch Package to get OOXML
DirectoryNode root = fs.getRoot();
DocumentEntry docEntry = (DocumentEntry) root.getEntry(OOXML_PACKAGE);
DocumentEntry docEntry = (DocumentEntry) root.getEntryCaseInsensitive(OOXML_PACKAGE);
try (DocumentInputStream dis = new DocumentInputStream(docEntry);
OPCPackage pkg = OPCPackage.open(dis);
XSSFWorkbook wb = new XSSFWorkbook(pkg)) {
Expand Down Expand Up @@ -3921,4 +3921,4 @@ private static void readByCommonsCompress(File temp_excel_poi) throws IOExceptio
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public POITextExtractor create(DirectoryNode poifsDir, String password) throws I
final String oldPW = Biff8EncryptionKey.getCurrentUserPassword();
try {
Biff8EncryptionKey.setCurrentUserPassword(password);
if (poifsDir.hasEntry("WordDocument")) {
if (poifsDir.hasEntryCaseInsensitive("WordDocument")) {
// Old or new style word document?
try {
return new WordExtractor(poifsDir);
Expand All @@ -117,20 +117,20 @@ public POITextExtractor create(DirectoryNode poifsDir, String password) throws I
}
}

if (poifsDir.hasEntry(HSLFSlideShow.POWERPOINT_DOCUMENT) || poifsDir.hasEntry(HSLFSlideShow.PP97_DOCUMENT)) {
if (poifsDir.hasEntryCaseInsensitive(HSLFSlideShow.POWERPOINT_DOCUMENT) || poifsDir.hasEntryCaseInsensitive(HSLFSlideShow.PP97_DOCUMENT)) {
return new SlideShowExtractor<>((HSLFSlideShow)SlideShowFactory.create(poifsDir));
}

if (poifsDir.hasEntry("VisioDocument")) {
if (poifsDir.hasEntryCaseInsensitive("VisioDocument")) {
return new VisioTextExtractor(poifsDir);
}

if (poifsDir.hasEntry("Quill")) {
if (poifsDir.hasEntryCaseInsensitive("Quill")) {
return new PublisherTextExtractor(poifsDir);
}

for (String entryName : OUTLOOK_ENTRY_NAMES) {
if (poifsDir.hasEntry(entryName)) {
if (poifsDir.hasEntryCaseInsensitive(entryName)) {
return new OutlookTextExtractor(poifsDir);
}
}
Expand Down Expand Up @@ -168,7 +168,7 @@ public void identifyEmbeddedResources(POIOLE2TextExtractor ext, List<Entry> dirs
} else if (ext instanceof WordExtractor) {
// These are in ObjectPool -> _... under the root
try {
DirectoryEntry op = (DirectoryEntry) root.getEntry("ObjectPool");
DirectoryEntry op = (DirectoryEntry) root.getEntryCaseInsensitive("ObjectPool");
StreamSupport.stream(op.spliterator(), false)
.filter(entry -> entry.getName().startsWith("_"))
.forEach(dirs::add);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public static void main(String[] args) throws Exception {
*/
public void dumpEscher() throws IOException {
DirectoryNode escherDir = (DirectoryNode)
fs.getRoot().getEntry("Escher");
fs.getRoot().getEntryCaseInsensitive("Escher");

dumpEscherStm(escherDir);
dumpEscherDelayStm(escherDir);
Expand Down Expand Up @@ -343,9 +343,9 @@ protected void dump001CompObj(DirectoryNode dir) {

public void dumpQuill() throws IOException {
DirectoryNode quillDir = (DirectoryNode)
fs.getRoot().getEntry("Quill");
fs.getRoot().getEntryCaseInsensitive("Quill");
DirectoryNode quillSubDir = (DirectoryNode)
quillDir.getEntry("QuillSub");
quillDir.getEntryCaseInsensitive("QuillSub");

dump001CompObj(quillSubDir);
dumpCONTENTSraw(quillSubDir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public HPBFPart(DirectoryNode baseDir, String[] path) throws IOException {
DirectoryNode dir = getDir(baseDir, path);
String name = path[path.length-1];

if (!dir.hasEntry(name)) {
if (!dir.hasEntryCaseInsensitive(name)) {
throw new IllegalArgumentException("File invalid - failed to find document entry '" + name + "'");
}

Expand Down Expand Up @@ -78,7 +78,7 @@ public void writeOut(DirectoryNode baseDir) throws IOException {
DirectoryNode dir = baseDir;
for(int i=0; i<path.length-1; i++) {
try {
dir = (DirectoryNode)dir.getEntry(path[i]);
dir = (DirectoryNode)dir.getEntryCaseInsensitive(path[i]);
} catch(FileNotFoundException e) {
dir.createDirectory(path[i]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public PPTXMLDump(File ppt) throws IOException {
private static byte[] readEntry(POIFSFileSystem fs, String entry)
throws IOException {
DirectoryNode dn = fs.getRoot();
if (!dn.hasEntry(entry)) {
if (!dn.hasEntryCaseInsensitive(entry)) {
return null;
}
try (InputStream is = dn.createDocumentInputStream(entry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public CurrentUserAtom(DirectoryNode dir) throws IOException {
// See how long it is. If it's under 28 bytes long, we can't
// read it
if(_contents.length < 28) {
boolean isPP95 = dir.hasEntry(PP95_DOCUMENT);
boolean isPP95 = dir.hasEntryCaseInsensitive(PP95_DOCUMENT);
// PPT95 has 4 byte size, then data
if (!isPP95 && _contents.length >= 4) {
int size = LittleEndian.getInt(_contents);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,7 @@ public int addEmbed(POIFSFileSystem poiData) {
Map<String,ClassID> olemap = getOleMap();
ClassID classID = null;
for (Map.Entry<String,ClassID> entry : olemap.entrySet()) {
if (root.hasEntry(entry.getKey())) {
if (root.hasEntryCaseInsensitive(entry.getKey())) {
classID = entry.getValue();
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,10 @@ public HSLFSlideShowImpl(DirectoryNode dir) throws IOException {

private static DirectoryNode handleDualStorage(DirectoryNode dir) throws IOException {
// when there's a dual storage entry, use it, as the outer document can't be read quite probably ...
if (!dir.hasEntry(PP97_DOCUMENT)) {
if (!dir.hasEntryCaseInsensitive(PP97_DOCUMENT)) {
return dir;
}
return (DirectoryNode) dir.getEntry(PP97_DOCUMENT);
return (DirectoryNode) dir.getEntryCaseInsensitive(PP97_DOCUMENT);
}

/**
Expand All @@ -226,12 +226,12 @@ public static HSLFSlideShowImpl create() {
private void readPowerPointStream() throws IOException {
final DirectoryNode dir = getDirectory();

if (!dir.hasEntry(POWERPOINT_DOCUMENT) && dir.hasEntry(PP95_DOCUMENT)) {
if (!dir.hasEntryCaseInsensitive(POWERPOINT_DOCUMENT) && dir.hasEntryCaseInsensitive(PP95_DOCUMENT)) {
throw new OldPowerPointFormatException("You seem to have supplied a PowerPoint95 file, which isn't supported");
}

// Get the main document stream
final Entry entry = dir.getEntry(POWERPOINT_DOCUMENT);
final Entry entry = dir.getEntryCaseInsensitive(POWERPOINT_DOCUMENT);
if (!(entry instanceof DocumentEntry)) {
throw new IllegalArgumentException("Had unexpected type of entry for name: " + POWERPOINT_DOCUMENT + ": " + entry.getClass());
}
Expand Down Expand Up @@ -399,12 +399,12 @@ private void readOtherStreams() {
private void readPictures() throws IOException {

// if the presentation doesn't contain pictures, will use an empty collection instead
if (!getDirectory().hasEntry("Pictures")) {
if (!getDirectory().hasEntryCaseInsensitive("Pictures")) {
_pictures = new ArrayList<>();
return;
}

final Entry en = getDirectory().getEntry("Pictures");
final Entry en = getDirectory().getEntryCaseInsensitive("Pictures");
if (!(en instanceof DocumentEntry)) {
throw new IllegalArgumentException("Had unexpected type of entry for name: Pictures: " + en.getClass());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public HWPFDocument(DirectoryNode directory) throws IOException {
String name = (_fib.getFibBase().isFWhichTblStm()) ? STREAM_TABLE_1 : STREAM_TABLE_0;

// Grab the table stream.
if (!directory.hasEntry(name)) {
if (!directory.hasEntryCaseInsensitive(name)) {
throw new IllegalStateException("Table Stream '" + name + "' wasn't found - Either the document is corrupt, or is Word95 (or earlier)");
}

Expand All @@ -271,7 +271,7 @@ public HWPFDocument(DirectoryNode directory) throws IOException {
_fib.fillVariableFields(_mainStream, _tableStream);

// read in the data stream.
_dataStream = directory.hasEntry(STREAM_DATA) ? getDocumentEntryBytes(STREAM_DATA, 0, Integer.MAX_VALUE) : new byte[0];
_dataStream = directory.hasEntryCaseInsensitive(STREAM_DATA) ? getDocumentEntryBytes(STREAM_DATA, 0, Integer.MAX_VALUE) : new byte[0];

// Get the cp of the start of text in the main stream
// The latest spec doc says this is always zero!
Expand Down Expand Up @@ -1033,4 +1033,4 @@ public void delete(int start, int length) {
Range r = new Range(start, start + length, this);
r.delete();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ public HWPFDocumentCore(DirectoryNode directory) throws IOException {
_fib = new FileInformationBlock(_mainStream);

DirectoryEntry objectPoolEntry = null;
if (directory.hasEntry(STREAM_OBJECT_POOL)) {
final Entry entry = directory.getEntry(STREAM_OBJECT_POOL);
if (directory.hasEntryCaseInsensitive(STREAM_OBJECT_POOL)) {
final Entry entry = directory.getEntryCaseInsensitive(STREAM_OBJECT_POOL);
if (!(entry instanceof DirectoryEntry)) {
throw new IllegalArgumentException("Had unexpected type of entry for name: " + STREAM_OBJECT_POOL + ": " + entry.getClass());
}
Expand Down Expand Up @@ -341,7 +341,7 @@ protected void updateEncryptionInfo() {
*/
protected byte[] getDocumentEntryBytes(String name, int encryptionOffset, final int len) throws IOException {
DirectoryNode dir = getDirectory();
final Entry entry = dir.getEntry(name);
final Entry entry = dir.getEntryCaseInsensitive(name);
if (!(entry instanceof DocumentEntry)) {
throw new IllegalArgumentException("Had unexpected type of entry for name: " + name + ": " + entry);
}
Expand All @@ -368,4 +368,4 @@ private InputStream getDecryptedStream(DocumentInputStream dis, int streamSize,
}
return new SequenceInputStream(new ByteArrayInputStream(plain), cis);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ protected boolean processOle2( HWPFDocument wordDocument, Element block,
* even if there is no ExtractorFactory in classpath, still support
* included Word's objects
*/
if ( directoryNode.hasEntry( "WordDocument" ) )
if ( directoryNode.hasEntryCaseInsensitive( "WordDocument" ) )
{
String text = WordToTextConverter.getText( (DirectoryNode) entry );
block.appendChild( textDocumentFacade
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public Entry getObjectById( String objId )

try
{
return _objectPool.getEntry( objId );
return _objectPool.getEntryCaseInsensitive( objId );
}
catch ( FileNotFoundException exc )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,20 @@ void testWithMacroStreams() throws IOException {
assertSlideShowWritesOutTheSame(hssC, pfsC);

// Currently has a Macros stream
assertNotNull(pfsC.getRoot().getEntry("Macros"));
assertNotNull(pfsC.getRoot().getEntryCaseInsensitive("Macros"));

// Write out normally, will loose the macro stream
UnsynchronizedByteArrayOutputStream baos = UnsynchronizedByteArrayOutputStream.builder().get();
hssC.write(baos);
try (POIFSFileSystem pfsNew = new POIFSFileSystem(baos.toInputStream())) {
assertFalse(pfsNew.getRoot().hasEntry("Macros"));
assertFalse(pfsNew.getRoot().hasEntryCaseInsensitive("Macros"));
}

// But if we write out with nodes preserved, will be there
baos.reset();
hssC.write(baos, true);
try (POIFSFileSystem pfsNew = new POIFSFileSystem(baos.toInputStream())) {
assertTrue(pfsNew.getRoot().hasEntry("Macros"));
assertTrue(pfsNew.getRoot().hasEntryCaseInsensitive("Macros"));
}
}
}
Expand Down Expand Up @@ -145,8 +145,8 @@ void assertSlideShowWritesOutTheSame(HSLFSlideShowImpl hss, POIFSFileSystem pfs)

private void assertSame(POIFSFileSystem origPFS, POIFSFileSystem newPFS) throws IOException {
// Check that the "PowerPoint Document" sections have the same size
DocumentEntry oProps = (DocumentEntry) origPFS.getRoot().getEntry(POWERPOINT_DOCUMENT);
DocumentEntry nProps = (DocumentEntry) newPFS.getRoot().getEntry(POWERPOINT_DOCUMENT);
DocumentEntry oProps = (DocumentEntry) origPFS.getRoot().getEntryCaseInsensitive(POWERPOINT_DOCUMENT);
DocumentEntry nProps = (DocumentEntry) newPFS.getRoot().getEntryCaseInsensitive(POWERPOINT_DOCUMENT);
assertEquals(oProps.getSize(), nProps.getSize());


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ void testExtractFromEmbeded() throws IOException {
};

for (int i=0; i<TEST_SET.length; i+=2) {
DirectoryNode dir = (DirectoryNode)root.getEntry(TEST_SET[i]);
assertTrue(dir.hasEntry(HSLFSlideShow.POWERPOINT_DOCUMENT));
DirectoryNode dir = (DirectoryNode)root.getEntryCaseInsensitive(TEST_SET[i]);
assertTrue(dir.hasEntryCaseInsensitive(HSLFSlideShow.POWERPOINT_DOCUMENT));

try (final SlideShow<?,?> ppt = SlideShowFactory.create(dir);
final SlideShowExtractor<?,?> ppe = new SlideShowExtractor<>(ppt)) {
Expand Down
Loading

0 comments on commit 9911738

Please sign in to comment.