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.

PropertyTable.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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.property;
  16. import java.io.IOException;
  17. import java.io.OutputStream;
  18. import java.nio.ByteBuffer;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import java.util.Stack;
  22. import org.apache.logging.log4j.LogManager;
  23. import org.apache.logging.log4j.Logger;
  24. import org.apache.poi.poifs.common.POIFSBigBlockSize;
  25. import org.apache.poi.poifs.common.POIFSConstants;
  26. import org.apache.poi.poifs.filesystem.BATManaged;
  27. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  28. import org.apache.poi.poifs.filesystem.POIFSStream;
  29. import org.apache.poi.poifs.storage.HeaderBlock;
  30. import org.apache.poi.util.IOUtils;
  31. import static org.apache.logging.log4j.util.Unbox.box;
  32. /**
  33. * This class embodies the Property Table for a {@link POIFSFileSystem};
  34. * this is basically the directory for all of the documents in the
  35. * filesystem and looks up entries in the filesystem to their
  36. * chain of blocks.
  37. */
  38. public final class PropertyTable implements BATManaged {
  39. private static final Logger LOG = LogManager.getLogger(PropertyTable.class);
  40. //arbitrarily selected; may need to increase
  41. private static final int MAX_RECORD_LENGTH = 100_000;
  42. private final HeaderBlock _header_block;
  43. private final List<Property> _properties = new ArrayList<>();
  44. private final POIFSBigBlockSize _bigBigBlockSize;
  45. public PropertyTable(HeaderBlock headerBlock)
  46. {
  47. _header_block = headerBlock;
  48. _bigBigBlockSize = headerBlock.getBigBlockSize();
  49. addProperty(new RootProperty());
  50. }
  51. /**
  52. * reading constructor (used when we've read in a file and we want
  53. * to extract the property table from it). Populates the
  54. * properties thoroughly
  55. *
  56. * @param headerBlock the header block of the file
  57. * @param filesystem the filesystem to read from
  58. *
  59. * @exception IOException if anything goes wrong (which should be
  60. * a result of the input being NFG)
  61. */
  62. public PropertyTable(final HeaderBlock headerBlock, final POIFSFileSystem filesystem)
  63. throws IOException {
  64. this(
  65. headerBlock,
  66. new POIFSStream(filesystem, headerBlock.getPropertyStart())
  67. );
  68. }
  69. /* only invoked locally and from the junit tests */
  70. PropertyTable(final HeaderBlock headerBlock, final Iterable<ByteBuffer> dataSource)
  71. throws IOException {
  72. _header_block = headerBlock;
  73. _bigBigBlockSize = headerBlock.getBigBlockSize();
  74. for (ByteBuffer bb : dataSource) {
  75. // Turn it into an array
  76. byte[] data;
  77. if (bb.hasArray() && bb.arrayOffset() == 0 &&
  78. bb.array().length == _bigBigBlockSize.getBigBlockSize()) {
  79. data = bb.array();
  80. } else {
  81. data = IOUtils.safelyAllocate(_bigBigBlockSize.getBigBlockSize(), MAX_RECORD_LENGTH);
  82. int toRead = data.length;
  83. if (bb.remaining() < _bigBigBlockSize.getBigBlockSize()) {
  84. // Looks to be a truncated block
  85. // This isn't allowed, but some third party created files
  86. // sometimes do this, and we can normally read anyway
  87. LOG.atWarn().log("Short Property Block, {} bytes instead of the expected {}", box(bb.remaining()),box(_bigBigBlockSize.getBigBlockSize()));
  88. toRead = bb.remaining();
  89. }
  90. bb.get(data, 0, toRead);
  91. }
  92. PropertyFactory.convertToProperties(data, _properties);
  93. }
  94. populatePropertyTree( (DirectoryProperty)_properties.get(0));
  95. }
  96. /**
  97. * Add a property to the list of properties we manage
  98. *
  99. * @param property the new Property to manage
  100. */
  101. public void addProperty(Property property) {
  102. _properties.add(property);
  103. }
  104. /**
  105. * Remove a property from the list of properties we manage
  106. *
  107. * @param property the Property to be removed
  108. */
  109. public void removeProperty(final Property property) {
  110. _properties.remove(property);
  111. }
  112. /**
  113. * Get the root property
  114. *
  115. * @return the root property
  116. */
  117. public RootProperty getRoot() {
  118. // it's always the first element in the List
  119. return ( RootProperty ) _properties.get(0);
  120. }
  121. /**
  122. * Get the start block for the property table
  123. *
  124. * @return start block index
  125. */
  126. public int getStartBlock() {
  127. return _header_block.getPropertyStart();
  128. }
  129. /**
  130. * Set the start block for this instance
  131. *
  132. * @param index index into the array of BigBlock instances making
  133. * up the the filesystem
  134. */
  135. public void setStartBlock(final int index) {
  136. _header_block.setPropertyStart(index);
  137. }
  138. /**
  139. * Return the number of BigBlock's this instance uses
  140. *
  141. * @return count of BigBlock instances
  142. */
  143. public int countBlocks() {
  144. long rawSize = _properties.size() * (long)POIFSConstants.PROPERTY_SIZE;
  145. int blkSize = _bigBigBlockSize.getBigBlockSize();
  146. int numBlocks = (int)(rawSize / blkSize);
  147. if ((rawSize % blkSize) != 0) {
  148. numBlocks++;
  149. }
  150. return numBlocks;
  151. }
  152. /**
  153. * Prepare to be written
  154. */
  155. public void preWrite() {
  156. List<Property> pList = new ArrayList<>();
  157. // give each property its index
  158. int i=0;
  159. for (Property p : _properties) {
  160. // only handle non-null properties
  161. if (p == null) continue;
  162. p.setIndex(i++);
  163. pList.add(p);
  164. }
  165. // prepare each property for writing
  166. for (Property p : pList) p.preWrite();
  167. }
  168. /**
  169. * Writes the properties out into the given low-level stream
  170. */
  171. public void write(POIFSStream stream) throws IOException {
  172. OutputStream os = stream.getOutputStream();
  173. for(Property property : _properties) {
  174. if(property != null) {
  175. property.writeData(os);
  176. }
  177. }
  178. os.close();
  179. // Update the start position if needed
  180. if(getStartBlock() != stream.getStartBlock()) {
  181. setStartBlock(stream.getStartBlock());
  182. }
  183. }
  184. private void populatePropertyTree(DirectoryProperty root) throws IOException {
  185. int index = root.getChildIndex();
  186. if (!Property.isValidIndex(index)) {
  187. // property has no children
  188. return;
  189. }
  190. final Stack<Property> children = new Stack<>();
  191. children.push(_properties.get(index));
  192. while (!children.empty()) {
  193. Property property = children.pop();
  194. if (property == null) {
  195. // unknown / unsupported / corrupted property, skip
  196. continue;
  197. }
  198. root.addChild(property);
  199. if (property.isDirectory()) {
  200. populatePropertyTree(( DirectoryProperty ) property);
  201. }
  202. index = property.getPreviousChildIndex();
  203. if (isValidIndex(index)) {
  204. children.push(_properties.get(index));
  205. }
  206. index = property.getNextChildIndex();
  207. if (isValidIndex(index)) {
  208. children.push(_properties.get(index));
  209. }
  210. }
  211. }
  212. private boolean isValidIndex(int index) {
  213. if (! Property.isValidIndex(index))
  214. return false;
  215. if (index < 0 || index >= _properties.size()) {
  216. LOG.atWarn().log("Property index {} outside the valid range 0..{}", box(index),box(_properties.size()));
  217. return false;
  218. }
  219. return true;
  220. }
  221. }