]> source.dussan.org Git - poi.git/commitdiff
#62951 - FileMagic not correctly identified
authorAndreas Beeker <kiwiwings@apache.org>
Sun, 25 Nov 2018 20:50:13 +0000 (20:50 +0000)
committerAndreas Beeker <kiwiwings@apache.org>
Sun, 25 Nov 2018 20:50:13 +0000 (20:50 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1847429 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/poifs/filesystem/FileMagic.java
src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSFileSystem.java

index 4ac616082e08a0c099a9fa1e9129a9be6f7a061c..bab62c6437b3337b51594a7a3dc72ea4063143d8 100644 (file)
@@ -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>
index 83b085bfd3eb02fb11055caae32e4dae9e63dc1c..1fffceafeb7840aea7df05e40e74fd48e053e0d3 100644 (file)
@@ -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)));
+       }
 }