mirror of https://github.com/apache/poi.git
#62951 - FileMagic not correctly identified
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1847429 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
d2cf03d353
commit
283ebe0e08
|
@ -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>
|
||||
|
|
|
@ -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)));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue