您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

FileMagic.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.poifs.filesystem;
  16. import static org.apache.poi.poifs.common.POIFSConstants.OOXML_FILE_HEADER;
  17. import static org.apache.poi.poifs.common.POIFSConstants.RAW_XML_FILE_HEADER;
  18. import static java.nio.charset.StandardCharsets.UTF_8;
  19. import java.io.BufferedInputStream;
  20. import java.io.File;
  21. import java.io.FileInputStream;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import org.apache.poi.poifs.storage.HeaderBlockConstants;
  25. import org.apache.poi.util.IOUtils;
  26. import org.apache.poi.util.LittleEndian;
  27. import org.apache.poi.util.LocaleUtil;
  28. /**
  29. * The file magic number, i.e. the file identification based on the first bytes
  30. * of the file
  31. */
  32. public enum FileMagic {
  33. /** OLE2 / BIFF8+ stream used for Office 97 and higher documents */
  34. OLE2(HeaderBlockConstants._signature),
  35. /** OOXML / ZIP stream */
  36. OOXML(OOXML_FILE_HEADER),
  37. /** XML file */
  38. XML(RAW_XML_FILE_HEADER),
  39. /** BIFF2 raw stream - for Excel 2 */
  40. BIFF2(new byte[]{
  41. 0x09, 0x00, // sid=0x0009
  42. 0x04, 0x00, // size=0x0004
  43. 0x00, 0x00, // unused
  44. '?', 0x00 // '?' = multiple values
  45. }),
  46. /** BIFF3 raw stream - for Excel 3 */
  47. BIFF3(new byte[]{
  48. 0x09, 0x02, // sid=0x0209
  49. 0x06, 0x00, // size=0x0006
  50. 0x00, 0x00, // unused
  51. '?', 0x00 // '?' = multiple values
  52. }),
  53. /** BIFF4 raw stream - for Excel 4 */
  54. BIFF4(new byte[]{
  55. 0x09, 0x04, // sid=0x0409
  56. 0x06, 0x00, // size=0x0006
  57. 0x00, 0x00, // unused
  58. '?', 0x00 // '? = multiple values
  59. },new byte[]{
  60. 0x09, 0x04, // sid=0x0409
  61. 0x06, 0x00, // size=0x0006
  62. 0x00, 0x00, // unused
  63. 0x00, 0x01
  64. }),
  65. /** Old MS Write raw stream */
  66. MSWRITE(
  67. new byte[]{0x31, (byte)0xbe, 0x00, 0x00 },
  68. new byte[]{0x32, (byte)0xbe, 0x00, 0x00 }),
  69. /** RTF document */
  70. RTF("{\\rtf"),
  71. /** PDF document */
  72. PDF("%PDF"),
  73. /** Some different HTML documents */
  74. HTML("<!DOCTYP",
  75. "<html","\n\r<html","\r\n<html","\r<html","\n<html",
  76. "<HTML","\r\n<HTML","\n\r<HTML","\r<HTML","\n<HTML"),
  77. WORD2(new byte[]{ (byte)0xdb, (byte)0xa5, 0x2d, 0x00}),
  78. /** JPEG image */
  79. JPEG(
  80. new byte[]{ (byte)0xFF, (byte)0xD8, (byte)0xFF, (byte)0xDB },
  81. new byte[]{ (byte)0xFF, (byte)0xD8, (byte)0xFF, (byte)0xE0, '?', '?', 'J', 'F', 'I', 'F', 0x00, 0x01 },
  82. new byte[]{ (byte)0xFF, (byte)0xD8, (byte)0xFF, (byte)0xEE },
  83. new byte[]{ (byte)0xFF, (byte)0xD8, (byte)0xFF, (byte)0xE1, '?', '?', 'E', 'x', 'i', 'f', 0x00, 0x00 }),
  84. /** GIF image */
  85. GIF("GIF87a","GIF89a"),
  86. /** PNG Image */
  87. PNG(new byte[]{ (byte)0x89, 'P', 'N', 'G', 0x0D, 0x0A, 0x1A, 0x0A }),
  88. /** TIFF Image */
  89. TIFF("II*\u0000", "MM\u0000*" ),
  90. // keep UNKNOWN always as last enum!
  91. /** UNKNOWN magic */
  92. UNKNOWN(new byte[0]);
  93. final byte[][] magic;
  94. FileMagic(long magic) {
  95. this.magic = new byte[1][8];
  96. LittleEndian.putLong(this.magic[0], 0, magic);
  97. }
  98. FileMagic(byte[]... magic) {
  99. this.magic = magic;
  100. }
  101. FileMagic(String... magic) {
  102. this.magic = new byte[magic.length][];
  103. int i=0;
  104. for (String s : magic) {
  105. this.magic[i++] = s.getBytes(LocaleUtil.CHARSET_1252);
  106. }
  107. }
  108. public static FileMagic valueOf(byte[] magic) {
  109. for (FileMagic fm : values()) {
  110. for (byte[] ma : fm.magic) {
  111. if (findMagic(ma, magic)) {
  112. return fm;
  113. }
  114. }
  115. }
  116. return UNKNOWN;
  117. }
  118. private static boolean findMagic(byte[] expected, byte[] actual) {
  119. int i=0;
  120. for (byte expectedByte : expected) {
  121. if (actual[i++] != expectedByte && expectedByte != '?') {
  122. return false;
  123. }
  124. }
  125. return true;
  126. }
  127. /**
  128. * Get the file magic of the supplied {@link File}<p>
  129. *
  130. * Even if this method returns {@link FileMagic#UNKNOWN} it could potentially mean,
  131. * that the ZIP stream has leading junk bytes
  132. *
  133. * @param inp a file to be identified
  134. */
  135. public static FileMagic valueOf(final File inp) throws IOException {
  136. try (FileInputStream fis = new FileInputStream(inp)) {
  137. final byte[] data = IOUtils.toByteArray(fis, 8);
  138. return FileMagic.valueOf(data);
  139. }
  140. }
  141. /**
  142. * Get the file magic of the supplied InputStream (which MUST
  143. * support mark and reset).<p>
  144. *
  145. * If unsure if your InputStream does support mark / reset,
  146. * use {@link #prepareToCheckMagic(InputStream)} to wrap it and make
  147. * sure to always use that, and not the original!<p>
  148. *
  149. * Even if this method returns {@link FileMagic#UNKNOWN} it could potentially mean,
  150. * that the ZIP stream has leading junk bytes
  151. *
  152. * @param inp An InputStream which supports either mark/reset
  153. */
  154. public static FileMagic valueOf(InputStream inp) throws IOException {
  155. if (!inp.markSupported()) {
  156. throw new IOException("getFileMagic() only operates on streams which support mark(int)");
  157. }
  158. // Grab the first 8 bytes
  159. byte[] data = IOUtils.peekFirst8Bytes(inp);
  160. return FileMagic.valueOf(data);
  161. }
  162. /**
  163. * Checks if an {@link InputStream} can be reset (i.e. used for checking the header magic) and wraps it if not
  164. *
  165. * @param stream stream to be checked for wrapping
  166. * @return a mark enabled stream
  167. */
  168. public static InputStream prepareToCheckMagic(InputStream stream) {
  169. if (stream.markSupported()) {
  170. return stream;
  171. }
  172. // we used to process the data via a PushbackInputStream, but user code could provide a too small one
  173. // so we use a BufferedInputStream instead now
  174. return new BufferedInputStream(stream);
  175. }
  176. }