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.

Chunk.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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.hdgf.chunks;
  16. import java.util.ArrayList;
  17. import org.apache.poi.hdgf.chunks.ChunkFactory.CommandDefinition;
  18. import org.apache.poi.util.LittleEndian;
  19. import org.apache.poi.util.POILogFactory;
  20. import org.apache.poi.util.POILogger;
  21. /**
  22. * Base of all chunks, which hold data, flags etc
  23. */
  24. public final class Chunk {
  25. /**
  26. * The contents of the chunk, excluding the header,
  27. * trailer and separator
  28. */
  29. private byte[] contents;
  30. private ChunkHeader header;
  31. /** May be null */
  32. private ChunkTrailer trailer;
  33. /** May be null */
  34. private ChunkSeparator separator;
  35. /** The possible different commands we can hold */
  36. private CommandDefinition[] commandDefinitions;
  37. /** The command+value pairs we hold */
  38. private Command[] commands;
  39. /* The blocks (if any) we hold */
  40. //private Block[] blocks
  41. /** The name of the chunk, as found from the commandDefinitions */
  42. private String name;
  43. /** For logging warnings about the structure of the file */
  44. private POILogger logger = POILogFactory.getLogger(Chunk.class);
  45. public Chunk(ChunkHeader header, ChunkTrailer trailer, ChunkSeparator separator, byte[] contents) {
  46. this.header = header;
  47. this.trailer = trailer;
  48. this.separator = separator;
  49. this.contents = contents.clone();
  50. }
  51. public byte[] _getContents() {
  52. return contents;
  53. }
  54. public ChunkHeader getHeader() {
  55. return header;
  56. }
  57. /**
  58. * Gets the separator between this chunk and the next, if it exists
  59. *
  60. * @return the separator
  61. */
  62. public ChunkSeparator getSeparator() {
  63. return separator;
  64. }
  65. /**
  66. * Gets the trailer for this chunk, if it exists
  67. *
  68. * @return the trailer
  69. */
  70. public ChunkTrailer getTrailer() {
  71. return trailer;
  72. }
  73. /**
  74. * Gets the command definitions, which define and describe much
  75. * of the data held by the chunk.
  76. *
  77. * @return the command definitions
  78. */
  79. @SuppressWarnings("unused")
  80. public CommandDefinition[] getCommandDefinitions() {
  81. return commandDefinitions;
  82. }
  83. void setCommandDefinitions(CommandDefinition[] commandDefinitions) {
  84. this.commandDefinitions = commandDefinitions;
  85. }
  86. public Command[] getCommands() {
  87. return commands;
  88. }
  89. /**
  90. * Get the name of the chunk, as found from the CommandDefinitions
  91. *
  92. * @return the name of the chunk
  93. */
  94. public String getName() {
  95. return name;
  96. }
  97. /**
  98. * Returns the size of the chunk, including any
  99. * headers, trailers and separators.
  100. *
  101. * @return the size of the chunk
  102. */
  103. public int getOnDiskSize() {
  104. int size = header.getSizeInBytes() + contents.length;
  105. if(trailer != null) {
  106. size += trailer.getTrailerData().length;
  107. }
  108. if(separator != null) {
  109. size += separator.separatorData.length;
  110. }
  111. return size;
  112. }
  113. /**
  114. * Uses our CommandDefinitions to process the commands
  115. * our chunk type has, and figure out the
  116. * values for them.
  117. */
  118. void processCommands() {
  119. if(commandDefinitions == null) {
  120. throw new IllegalStateException("You must supply the command definitions before calling processCommands!");
  121. }
  122. // Loop over the definitions, building the commands
  123. // and getting their values
  124. ArrayList<Command> commandList = new ArrayList<>();
  125. for(CommandDefinition cdef : commandDefinitions) {
  126. int type = cdef.getType();
  127. int offset = cdef.getOffset();
  128. // Handle virtual commands
  129. if(type == 10) {
  130. name = cdef.getName();
  131. continue;
  132. } else if(type == 18) {
  133. continue;
  134. }
  135. // Build the appropriate command for the type
  136. Command command;
  137. if(type == 11 || type == 21) {
  138. command = new BlockOffsetCommand(cdef);
  139. } else {
  140. command = new Command(cdef);
  141. }
  142. // Bizarrely, many of the offsets are from the start of the
  143. // header, not from the start of the chunk body
  144. switch(type) {
  145. case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
  146. case 11: case 21:
  147. case 12: case 16: case 17: case 28: case 29:
  148. // Offset is from start of chunk (case 18 has been taken care of above)
  149. break;
  150. default:
  151. // Offset is from start of header!
  152. if(offset >= 19) {
  153. offset -= 19;
  154. }
  155. }
  156. // Check we seem to have enough data
  157. if(offset >= contents.length) {
  158. logger.log(POILogger.WARN,
  159. "Command offset " + offset + " past end of data at " + contents.length
  160. );
  161. continue;
  162. }
  163. try {
  164. // Process
  165. switch(type) {
  166. // Types 0->7 = a flag at bit 0->7
  167. case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
  168. command.value = ((contents[offset] >>> type) & 1) == 1;
  169. break;
  170. case 8:
  171. command.value = contents[offset];
  172. break;
  173. case 9:
  174. command.value = LittleEndian.getDouble(contents, offset);
  175. break;
  176. case 12:
  177. // A Little Endian String
  178. // Starts 8 bytes into the data segment
  179. // Ends at end of data, or 00 00
  180. // Ensure we have enough data
  181. if(contents.length < 8) {
  182. command.value = "";
  183. break;
  184. }
  185. // Find the end point
  186. int startsAt = 8;
  187. int endsAt = startsAt;
  188. for(int j=startsAt; j<contents.length-1 && endsAt == startsAt; j++) {
  189. if(contents[j] == 0 && contents[j+1] == 0) {
  190. endsAt = j;
  191. }
  192. }
  193. if(endsAt == startsAt) {
  194. endsAt = contents.length;
  195. }
  196. int strLen = endsAt - startsAt;
  197. command.value = new String(contents, startsAt, strLen, header.getChunkCharset().name());
  198. break;
  199. case 25:
  200. command.value = LittleEndian.getShort(contents, offset);
  201. break;
  202. case 26:
  203. command.value = LittleEndian.getInt(contents, offset);
  204. break;
  205. // Types 11 and 21 hold the offset to the blocks
  206. case 11: case 21:
  207. if(offset < contents.length - 3) {
  208. int bOffset = (int)LittleEndian.getUInt(contents, offset);
  209. BlockOffsetCommand bcmd = (BlockOffsetCommand)command;
  210. bcmd.setOffset(bOffset);
  211. }
  212. break;
  213. default:
  214. logger.log(POILogger.INFO,
  215. "Command of type " + type + " not processed!");
  216. }
  217. }
  218. catch (Exception e) {
  219. logger.log(POILogger.ERROR, "Unexpected error processing command, ignoring and continuing. Command: " +
  220. command, e);
  221. }
  222. // Add to the array
  223. commandList.add(command);
  224. }
  225. // Save the commands we liked the look of
  226. this.commands = commandList.toArray(
  227. new Command[0]);
  228. // Now build up the blocks, if we had a command that tells
  229. // us where a block is
  230. }
  231. /**
  232. * A command in the visio file. In order to make things fun,
  233. * all the chunk actually stores is the value of the command.
  234. * You have to have your own lookup table to figure out what
  235. * the commands are based on the chunk type.
  236. */
  237. public static class Command {
  238. protected Object value;
  239. private CommandDefinition definition;
  240. private Command(CommandDefinition definition, Object value) {
  241. this.definition = definition;
  242. this.value = value;
  243. }
  244. private Command(CommandDefinition definition) {
  245. this(definition, null);
  246. }
  247. public CommandDefinition getDefinition() { return definition; }
  248. public Object getValue() { return value; }
  249. }
  250. /*
  251. * A special kind of command that is an artificat of how we
  252. * process CommandDefinitions, and so doesn't actually exist
  253. * in the chunk
  254. */
  255. // public static class VirtualCommand extends Command {
  256. // private VirtualCommand(CommandDefinition definition) {
  257. // super(definition);
  258. // }
  259. // }
  260. /**
  261. * A special kind of command that holds the offset to
  262. * a block
  263. */
  264. private static final class BlockOffsetCommand extends Command {
  265. private BlockOffsetCommand(CommandDefinition definition) {
  266. super(definition, null);
  267. }
  268. private void setOffset(int offset) {
  269. value = offset;
  270. }
  271. }
  272. }