poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kiwiwi...@apache.org
Subject svn commit: r1847429 - in /poi: site/src/documentation/content/xdocs/changes.xml trunk/src/java/org/apache/poi/poifs/filesystem/FileMagic.java trunk/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSFileSystem.java
Date Sun, 25 Nov 2018 20:50:13 GMT
Author: kiwiwings
Date: Sun Nov 25 20:50:13 2018
New Revision: 1847429

URL: http://svn.apache.org/viewvc?rev=1847429&view=rev
Log:
#62951 - FileMagic not correctly identified

Modified:
    poi/site/src/documentation/content/xdocs/changes.xml
    poi/trunk/src/java/org/apache/poi/poifs/filesystem/FileMagic.java
    poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSFileSystem.java

Modified: poi/site/src/documentation/content/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/poi/site/src/documentation/content/xdocs/changes.xml?rev=1847429&r1=1847428&r2=1847429&view=diff
==============================================================================
--- poi/site/src/documentation/content/xdocs/changes.xml (original)
+++ poi/site/src/documentation/content/xdocs/changes.xml Sun Nov 25 20:50:13 2018
@@ -92,6 +92,7 @@
         <summary-item>Upgrade to XMLBeans 3.0.2</summary-item>
       </summary>
       <actions>
+        <action dev="PD" type="fix" fixes-bug="62951" context="POI_Overall">FileMagic
not correctly identified</action>
         <action dev="PD" type="fix" fixes-bug="62949" context="SL_Common">SlideShow
rendering - keyframe fractions must be increasing</action>
         <action dev="PD" type="fix" fixes-bug="62921" context="POI_Overall">Provide
OOXMLLite alternative for Java 12+</action>
         <action dev="PD" type="fix" fixes-bug="62625" context="POI_Overall">Handle
off-spec, variant REFERENCE_NAME record structure in VBAMacroReader</action>

Modified: poi/trunk/src/java/org/apache/poi/poifs/filesystem/FileMagic.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/filesystem/FileMagic.java?rev=1847429&r1=1847428&r2=1847429&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/poifs/filesystem/FileMagic.java (original)
+++ poi/trunk/src/java/org/apache/poi/poifs/filesystem/FileMagic.java Sun Nov 25 20:50:13
2018
@@ -78,7 +78,7 @@ public enum FileMagic {
     /** PDF document */
     PDF("%PDF"),
     /** Some different HTML documents */
-    HTML("<!DOCTYP".getBytes(UTF_8), "<html".getBytes(UTF_8)),
+    HTML("<!DOCTYP".getBytes(UTF_8), "<html".getBytes(UTF_8), "<HTML".getBytes(UTF_8)),
     WORD2(new byte[]{ (byte)0xdb, (byte)0xa5, 0x2d, 0x00}),
     // keep UNKNOWN always as last enum!
     /** UNKNOWN magic */
@@ -101,17 +101,8 @@ public enum FileMagic {
 
     public static FileMagic valueOf(byte[] magic) {
         for (FileMagic fm : values()) {
-            int i=0;
-            boolean found = true;
             for (byte[] ma : fm.magic) {
-                for (byte m : ma) {
-                    byte d = magic[i++];
-                    if (!(d == m || (m == 0x70 && (d == 0x10 || d == 0x20 || d ==
0x40)))) {
-                        found = false;
-                        break;
-                    }
-                }
-                if (found) {
+                if (findMagic(ma, magic)) {
                     return fm;
                 }
             }
@@ -119,6 +110,17 @@ public enum FileMagic {
         return UNKNOWN;
     }
 
+    private static boolean findMagic(byte[] cmp, byte[] actual) {
+        int i=0;
+        for (byte m : cmp) {
+            byte d = actual[i++];
+            if (!(d == m || (m == 0x70 && (d == 0x10 || d == 0x20 || d == 0x40))))
{
+                return false;
+            }
+        }
+        return true;
+    }
+
 
     /**
      * Get the file magic of the supplied {@link File}<p>

Modified: poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSFileSystem.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSFileSystem.java?rev=1847429&r1=1847428&r2=1847429&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSFileSystem.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSFileSystem.java Sun Nov
25 20:50:13 2018
@@ -17,6 +17,7 @@
 
 package org.apache.poi.poifs.filesystem;
 
+import static java.nio.charset.StandardCharsets.UTF_8;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
@@ -278,4 +279,18 @@ public final class TestPOIFSFileSystem {
 	private static InputStream openSampleStream(String sampleFileName) {
 		return HSSFTestDataSamples.openSampleFileStream(sampleFileName);
 	}
+
+	@Test
+	public void fileMagics() {
+		for (FileMagic fm : FileMagic.values()) {
+			if (fm == FileMagic.UNKNOWN) {
+				continue;
+			}
+			for (byte[] b : fm.magic) {
+				assertEquals(fm, FileMagic.valueOf(b));
+			}
+		}
+
+		assertEquals(FileMagic.UNKNOWN, FileMagic.valueOf("foobaa".getBytes(UTF_8)));
+	}
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message