You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HeaderBlock.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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.storage;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.io.OutputStream;
  19. import java.nio.ByteBuffer;
  20. import java.util.Arrays;
  21. import org.apache.poi.poifs.common.POIFSBigBlockSize;
  22. import org.apache.poi.poifs.common.POIFSConstants;
  23. import org.apache.poi.poifs.filesystem.OfficeXmlFileException;
  24. import org.apache.poi.util.HexDump;
  25. import org.apache.poi.util.IOUtils;
  26. import org.apache.poi.util.IntegerField;
  27. import org.apache.poi.util.LittleEndian;
  28. import org.apache.poi.util.LittleEndianConsts;
  29. import org.apache.poi.util.LongField;
  30. import org.apache.poi.util.POILogFactory;
  31. import org.apache.poi.util.POILogger;
  32. import org.apache.poi.util.ShortField;
  33. /**
  34. * The block containing the archive header
  35. */
  36. public final class HeaderBlock implements HeaderBlockConstants {
  37. private static final POILogger _logger =
  38. POILogFactory.getLogger(HeaderBlock.class);
  39. /**
  40. * What big block size the file uses. Most files
  41. * use 512 bytes, but a few use 4096
  42. */
  43. private final POIFSBigBlockSize bigBlockSize;
  44. /**
  45. * Number of big block allocation table blocks (int).
  46. * (Number of FAT Sectors in Microsoft parlance).
  47. */
  48. private int _bat_count;
  49. /**
  50. * Start of the property set block (int index of the property set
  51. * chain's first big block).
  52. */
  53. private int _property_start;
  54. /**
  55. * start of the small block allocation table (int index of small
  56. * block allocation table's first big block)
  57. */
  58. private int _sbat_start;
  59. /**
  60. * Number of small block allocation table blocks (int)
  61. * (Number of MiniFAT Sectors in Microsoft parlance)
  62. */
  63. private int _sbat_count;
  64. /**
  65. * Big block index for extension to the big block allocation table
  66. */
  67. private int _xbat_start;
  68. /**
  69. * Number of big block allocation table blocks (int)
  70. * (Number of DIFAT Sectors in Microsoft parlance)
  71. */
  72. private int _xbat_count;
  73. /**
  74. * The data. Only ever 512 bytes, because 4096 byte
  75. * files use zeros for the extra header space.
  76. */
  77. private final byte[] _data;
  78. private static final byte _default_value = ( byte ) 0xFF;
  79. /**
  80. * create a new HeaderBlockReader from an InputStream
  81. *
  82. * @param stream the source InputStream
  83. *
  84. * @exception IOException on errors or bad data
  85. */
  86. public HeaderBlock(InputStream stream) throws IOException {
  87. // Grab the first 512 bytes
  88. // (For 4096 sized blocks, the remaining 3584 bytes are zero)
  89. // Then, process the contents
  90. this(readFirst512(stream));
  91. // Fetch the rest of the block if needed
  92. if(bigBlockSize.getBigBlockSize() != 512) {
  93. int rest = bigBlockSize.getBigBlockSize() - 512;
  94. byte[] tmp = new byte[rest];
  95. IOUtils.readFully(stream, tmp);
  96. }
  97. }
  98. public HeaderBlock(ByteBuffer buffer) throws IOException {
  99. this(IOUtils.toByteArray(buffer, POIFSConstants.SMALLER_BIG_BLOCK_SIZE));
  100. }
  101. private HeaderBlock(byte[] data) throws IOException {
  102. this._data = data;
  103. // verify signature
  104. long signature = LittleEndian.getLong(_data, _signature_offset);
  105. if (signature != _signature) {
  106. // Is it one of the usual suspects?
  107. byte[] OOXML_FILE_HEADER = POIFSConstants.OOXML_FILE_HEADER;
  108. if(_data[0] == OOXML_FILE_HEADER[0] &&
  109. _data[1] == OOXML_FILE_HEADER[1] &&
  110. _data[2] == OOXML_FILE_HEADER[2] &&
  111. _data[3] == OOXML_FILE_HEADER[3]) {
  112. throw new OfficeXmlFileException("The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)");
  113. }
  114. if ((signature & 0xFF8FFFFFFFFFFFFFL) == 0x0010000200040009L) {
  115. // BIFF2 raw stream starts with BOF (sid=0x0009, size=0x0004, data=0x00t0)
  116. throw new IllegalArgumentException("The supplied data appears to be in BIFF2 format. "
  117. + "POI only supports BIFF8 format");
  118. }
  119. // Give a generic error
  120. throw new IOException("Invalid header signature; read "
  121. + longToHex(signature) + ", expected "
  122. + longToHex(_signature));
  123. }
  124. // Figure out our block size
  125. if (_data[30] == 12) {
  126. this.bigBlockSize = POIFSConstants.LARGER_BIG_BLOCK_SIZE_DETAILS;
  127. } else if(_data[30] == 9) {
  128. this.bigBlockSize = POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS;
  129. } else {
  130. throw new IOException("Unsupported blocksize (2^"+ _data[30] + "). Expected 2^9 or 2^12.");
  131. }
  132. // Setup the fields to read and write the counts and starts
  133. _bat_count = new IntegerField(_bat_count_offset, data).get();
  134. _property_start = new IntegerField(_property_start_offset,_data).get();
  135. _sbat_start = new IntegerField(_sbat_start_offset, _data).get();
  136. _sbat_count = new IntegerField(_sbat_block_count_offset, _data).get();
  137. _xbat_start = new IntegerField(_xbat_start_offset, _data).get();
  138. _xbat_count = new IntegerField(_xbat_count_offset, _data).get();
  139. }
  140. /**
  141. * Create a single instance initialized with default values
  142. */
  143. public HeaderBlock(POIFSBigBlockSize bigBlockSize)
  144. {
  145. this.bigBlockSize = bigBlockSize;
  146. // Our data is always 512 big no matter what
  147. _data = new byte[ POIFSConstants.SMALLER_BIG_BLOCK_SIZE ];
  148. Arrays.fill(_data, _default_value);
  149. // Set all the default values
  150. new LongField(_signature_offset, _signature, _data);
  151. new IntegerField(0x08, 0, _data);
  152. new IntegerField(0x0c, 0, _data);
  153. new IntegerField(0x10, 0, _data);
  154. new IntegerField(0x14, 0, _data);
  155. new ShortField(0x18, ( short ) 0x3b, _data);
  156. new ShortField(0x1a, ( short ) 0x3, _data);
  157. new ShortField(0x1c, ( short ) -2, _data);
  158. new ShortField(0x1e, bigBlockSize.getHeaderValue(), _data);
  159. new IntegerField(0x20, 0x6, _data);
  160. new IntegerField(0x24, 0, _data);
  161. new IntegerField(0x28, 0, _data);
  162. new IntegerField(0x34, 0, _data);
  163. new IntegerField(0x38, 0x1000, _data);
  164. // Initialise the variables
  165. _bat_count = 0;
  166. _sbat_count = 0;
  167. _xbat_count = 0;
  168. _property_start = POIFSConstants.END_OF_CHAIN;
  169. _sbat_start = POIFSConstants.END_OF_CHAIN;
  170. _xbat_start = POIFSConstants.END_OF_CHAIN;
  171. }
  172. private static byte[] readFirst512(InputStream stream) throws IOException {
  173. // Grab the first 512 bytes
  174. // (For 4096 sized blocks, the remaining 3584 bytes are zero)
  175. byte[] data = new byte[512];
  176. int bsCount = IOUtils.readFully(stream, data);
  177. if(bsCount != 512) {
  178. throw alertShortRead(bsCount, 512);
  179. }
  180. return data;
  181. }
  182. private static String longToHex(long value) {
  183. return new String(HexDump.longToHex(value));
  184. }
  185. private static IOException alertShortRead(int pRead, int expectedReadSize) {
  186. int read;
  187. if (pRead < 0) {
  188. //Can't have -1 bytes read in the error message!
  189. read = 0;
  190. } else {
  191. read = pRead;
  192. }
  193. String type = " byte" + (read == 1 ? (""): ("s"));
  194. return new IOException("Unable to read entire header; "
  195. + read + type + " read; expected "
  196. + expectedReadSize + " bytes");
  197. }
  198. /**
  199. * get start of Property Table
  200. *
  201. * @return the index of the first block of the Property Table
  202. */
  203. public int getPropertyStart() {
  204. return _property_start;
  205. }
  206. /**
  207. * Set start of Property Table
  208. *
  209. * @param startBlock the index of the first block of the Property Table
  210. */
  211. public void setPropertyStart(final int startBlock) {
  212. _property_start = startBlock;
  213. }
  214. /**
  215. * @return start of small block (MiniFAT) allocation table
  216. */
  217. public int getSBATStart() {
  218. return _sbat_start;
  219. }
  220. public int getSBATCount() {
  221. return _sbat_count;
  222. }
  223. /**
  224. * Set start of small block allocation table
  225. *
  226. * @param startBlock the index of the first big block of the small
  227. * block allocation table
  228. */
  229. public void setSBATStart(final int startBlock) {
  230. _sbat_start = startBlock;
  231. }
  232. /**
  233. * Set count of SBAT blocks
  234. *
  235. * @param count the number of SBAT blocks
  236. */
  237. public void setSBATBlockCount(final int count)
  238. {
  239. _sbat_count = count;
  240. }
  241. /**
  242. * @return number of BAT blocks
  243. */
  244. public int getBATCount() {
  245. return _bat_count;
  246. }
  247. /**
  248. * Sets the number of BAT blocks that are used.
  249. * This is the number used in both the BAT and XBAT.
  250. */
  251. public void setBATCount(final int count) {
  252. _bat_count = count;
  253. }
  254. /**
  255. * Returns the offsets to the first (up to) 109
  256. * BAT sectors.
  257. * Any additional BAT sectors are held in the XBAT (DIFAT)
  258. * sectors in a chain.
  259. * @return BAT offset array
  260. */
  261. public int[] getBATArray() {
  262. // Read them in
  263. int[] result = new int[ Math.min(_bat_count,_max_bats_in_header) ];
  264. int offset = _bat_array_offset;
  265. for (int j = 0; j < result.length; j++) {
  266. result[ j ] = LittleEndian.getInt(_data, offset);
  267. offset += LittleEndianConsts.INT_SIZE;
  268. }
  269. return result;
  270. }
  271. /**
  272. * Sets the offsets of the first (up to) 109
  273. * BAT sectors.
  274. */
  275. public void setBATArray(int[] bat_array) {
  276. int count = Math.min(bat_array.length, _max_bats_in_header);
  277. int blank = _max_bats_in_header - count;
  278. int offset = _bat_array_offset;
  279. for(int i=0; i<count; i++) {
  280. LittleEndian.putInt(_data, offset, bat_array[i]);
  281. offset += LittleEndianConsts.INT_SIZE;
  282. }
  283. for(int i=0; i<blank; i++) {
  284. LittleEndian.putInt(_data, offset, POIFSConstants.UNUSED_BLOCK);
  285. offset += LittleEndianConsts.INT_SIZE;
  286. }
  287. }
  288. /**
  289. * @return XBAT (DIFAT) count
  290. */
  291. public int getXBATCount() {
  292. return _xbat_count;
  293. }
  294. /**
  295. * Sets the number of XBAT (DIFAT) blocks used
  296. */
  297. public void setXBATCount(final int count) {
  298. _xbat_count = count;
  299. }
  300. /**
  301. * @return XBAT (DIFAT) index
  302. */
  303. public int getXBATIndex() {
  304. return _xbat_start;
  305. }
  306. /**
  307. * Sets the first XBAT (DIFAT) block location
  308. */
  309. public void setXBATStart(final int startBlock) {
  310. _xbat_start = startBlock;
  311. }
  312. /**
  313. * @return The Big Block size, normally 512 bytes, sometimes 4096 bytes
  314. */
  315. public POIFSBigBlockSize getBigBlockSize() {
  316. return bigBlockSize;
  317. }
  318. /**
  319. * Write the block's data to an OutputStream
  320. *
  321. * @param stream the OutputStream to which the stored data should
  322. * be written
  323. *
  324. * @exception IOException on problems writing to the specified
  325. * stream
  326. */
  327. void writeData(final OutputStream stream)
  328. throws IOException
  329. {
  330. // Update the counts and start positions
  331. new IntegerField(_bat_count_offset, _bat_count, _data);
  332. new IntegerField(_property_start_offset, _property_start, _data);
  333. new IntegerField(_sbat_start_offset, _sbat_start, _data);
  334. new IntegerField(_sbat_block_count_offset, _sbat_count, _data);
  335. new IntegerField(_xbat_start_offset, _xbat_start, _data);
  336. new IntegerField(_xbat_count_offset, _xbat_count, _data);
  337. // Write the main data out
  338. stream.write(_data, 0, 512);
  339. // Now do the padding if needed
  340. for(int i=POIFSConstants.SMALLER_BIG_BLOCK_SIZE; i<bigBlockSize.getBigBlockSize(); i++) {
  341. stream.write(0);
  342. }
  343. }
  344. }