選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

SlideShowDumper.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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.hslf.dev;
  16. import org.apache.poi.ddf.DefaultEscherRecordFactory;
  17. import org.apache.poi.ddf.EscherContainerRecord;
  18. import org.apache.poi.ddf.EscherRecord;
  19. import org.apache.poi.ddf.EscherTextboxRecord;
  20. import org.apache.poi.hslf.record.HSLFEscherRecordFactory;
  21. import org.apache.poi.hslf.record.RecordTypes;
  22. import org.apache.poi.hslf.usermodel.HSLFSlideShow;
  23. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  24. import org.apache.poi.util.HexDump;
  25. import org.apache.poi.util.IOUtils;
  26. import org.apache.poi.util.LittleEndian;
  27. import java.io.File;
  28. import java.io.IOException;
  29. import java.io.InputStream;
  30. import java.io.PrintStream;
  31. import java.util.Locale;
  32. /**
  33. * This class provides a way to "peek" inside a powerpoint file. It
  34. * will print out all the types it finds, and for those it knows aren't
  35. * atoms, what they contain
  36. * <p>
  37. * To figure out what things are, and if they are atoms or not, used the
  38. * list in hslf.record.RecordTypes
  39. * <p>
  40. * To peek inside PPDrawings, which hold Escher drawings, we use the
  41. * DDF package from POI (but we can fake it by using the Escher listings
  42. * from hslf.record.RecordTypes also)
  43. */
  44. public final class SlideShowDumper {
  45. //arbitrarily selected; may need to increase
  46. private static final int DEFAULT_MAX_RECORD_LENGTH = 100_000;
  47. private static int MAX_RECORD_LENGTH = DEFAULT_MAX_RECORD_LENGTH;
  48. private byte[] docstream;
  49. /**
  50. * Do we try to use DDF to understand the escher objects?
  51. */
  52. private boolean ddfEscher;
  53. /**
  54. * Do we use our own built-in basic escher groker to understand the escher objects?
  55. */
  56. private boolean basicEscher;
  57. private PrintStream out;
  58. /**
  59. * @param length the max record length allowed for SlideShowDumper
  60. */
  61. public static void setMaxRecordLength(int length) {
  62. MAX_RECORD_LENGTH = length;
  63. }
  64. /**
  65. * @return the max record length allowed for SlideShowDumper
  66. */
  67. public static int getMaxRecordLength() {
  68. return MAX_RECORD_LENGTH;
  69. }
  70. /**
  71. * right now this function takes one parameter: a ppt file, and outputs
  72. * a dump of what it contains
  73. */
  74. public static void main(String[] args) throws IOException {
  75. if (args.length == 0) {
  76. System.err.println("Usage: SlideShowDumper [-escher|-basicescher] <filename>");
  77. return;
  78. }
  79. String filename = args[0];
  80. if (args.length > 1) {
  81. filename = args[1];
  82. }
  83. try (POIFSFileSystem poifs = new POIFSFileSystem(new File(filename))) {
  84. SlideShowDumper foo = new SlideShowDumper(poifs, System.out);
  85. if (args.length > 1) {
  86. if (args[0].equalsIgnoreCase("-escher")) {
  87. foo.setDDFEscher(true);
  88. } else {
  89. foo.setBasicEscher(true);
  90. }
  91. }
  92. foo.printDump();
  93. }
  94. }
  95. /**
  96. * Constructs a Powerpoint dump from a POIFS Filesystem. Parses the
  97. * document and dumps out the contents
  98. *
  99. * @param filesystem the POIFS FileSystem to read from
  100. * @throws IOException if there is a problem while parsing the document.
  101. */
  102. public SlideShowDumper(POIFSFileSystem filesystem, PrintStream out) throws IOException {
  103. // Grab the document stream
  104. InputStream is = filesystem.createDocumentInputStream(HSLFSlideShow.POWERPOINT_DOCUMENT);
  105. docstream = IOUtils.toByteArray(is);
  106. is.close();
  107. this.out = out;
  108. }
  109. /**
  110. * Control dumping of any Escher records found - should DDF be used?
  111. */
  112. public void setDDFEscher(boolean grok) {
  113. ddfEscher = grok;
  114. basicEscher = !(grok);
  115. }
  116. /**
  117. * Control dumping of any Escher records found - should our built in
  118. * basic groker be used?
  119. */
  120. public void setBasicEscher(boolean grok) {
  121. basicEscher = grok;
  122. ddfEscher = !(grok);
  123. }
  124. public void printDump() throws IOException {
  125. // The format of records in a powerpoint file are:
  126. // <little endian 2 byte "info">
  127. // <little endian 2 byte "type">
  128. // <little endian 4 byte "length">
  129. // If it has a zero length, following it will be another record
  130. // <xx xx yy yy 00 00 00 00> <xx xx yy yy zz zz zz zz>
  131. // If it has a length, depending on its type it may have children or data
  132. // If it has children, these will follow straight away
  133. // <xx xx yy yy zz zz zz zz <xx xx yy yy zz zz zz zz>>
  134. // If it has data, this will come straigh after, and run for the length
  135. // <xx xx yy yy zz zz zz zz dd dd dd dd dd dd dd>
  136. // All lengths given exclude the 8 byte record header
  137. // (Data records are known as Atoms)
  138. // Document should start with:
  139. // 0F 00 E8 03 ## ## ## ##
  140. // (type 1000 = document, info 00 0f is normal, rest is document length)
  141. // 01 00 E9 03 28 00 00 00
  142. // (type 1001 = document atom, info 00 01 normal, 28 bytes long)
  143. // When parsing a document, look to see if you know about that type
  144. // of the current record. If you know it's a type that has children,
  145. // process the record's data area looking for more records
  146. // If you know about the type and it doesn't have children, either do
  147. // something with the data (eg TextRun) or skip over it
  148. // Otherwise, check the first byte. If you do a BINARY_AND on it with
  149. // 0x0f (15) and get back 0x0f, you know it has children. Otherwise
  150. // it doesn't
  151. walkTree(0, 0, docstream.length);
  152. }
  153. public void walkTree(int depth, int startPos, int maxLen) throws IOException {
  154. int pos = startPos;
  155. int endPos = startPos + maxLen;
  156. final String ind = (depth == 0) ? "%1$s" : "%1$" + depth + "s";
  157. while (pos <= endPos - 8) {
  158. long type = LittleEndian.getUShort(docstream, pos + 2);
  159. long len = LittleEndian.getUInt(docstream, pos + 4);
  160. byte opt = docstream[pos];
  161. String fmt = ind + "At position %2$d (%2$04x): type is %3$d (%3$04x), len is %4$d (%4$04x)";
  162. out.printf(Locale.ROOT, (fmt) + "%n", "", pos, type, len);
  163. // See if we know about the type of it
  164. String recordName = RecordTypes.forTypeID((short) type).name();
  165. // Jump over header, and think about going on more
  166. pos += 8;
  167. out.printf(Locale.ROOT, ind + "That's a %2$s%n", "", recordName);
  168. if (len < 0 /*|| len > Integer.MAX_VALUE*/) {
  169. // stop processing of invalid header data
  170. continue;
  171. }
  172. // Now check if it's a container or not
  173. int container = opt & 0x0f;
  174. // BinaryTagData seems to contain records, but it
  175. // isn't tagged as doing so. Try stepping in anyway
  176. if (type == 5003L && opt == 0L) {
  177. container = 0x0f;
  178. }
  179. out.println();
  180. if (type != 0L && container == 0x0f) {
  181. if (type == 1035L || type == 1036L) {
  182. // Special Handling of 1035=PPDrawingGroup and 1036=PPDrawing
  183. if (ddfEscher) {
  184. // Seems to be:
  185. walkEscherDDF((depth + 3), pos + 8, (int) len - 8);
  186. } else if (basicEscher) {
  187. walkEscherBasic((depth + 3), pos + 8, (int) len - 8);
  188. }
  189. } else {
  190. // General container record handling code
  191. walkTree((depth + 2), pos, (int) len);
  192. }
  193. }
  194. pos += (int) Math.min(len, Integer.MAX_VALUE);
  195. }
  196. }
  197. /**
  198. * Use the DDF code to walk the Escher records
  199. */
  200. public void walkEscherDDF(int indent, int pos, int len) {
  201. if (len < 8) {
  202. return;
  203. }
  204. final String ind = (indent == 0) ? "%1$s" : "%1$" + indent + "s";
  205. byte[] contents = IOUtils.safelyClone(docstream, pos, len, MAX_RECORD_LENGTH);
  206. DefaultEscherRecordFactory erf = new HSLFEscherRecordFactory();
  207. EscherRecord record = erf.createRecord(contents, 0);
  208. // For now, try filling in the fields
  209. record.fillFields(contents, 0, erf);
  210. long atomType = LittleEndian.getUShort(contents, 2);
  211. // This lacks the 8 byte header size
  212. long atomLen = LittleEndian.getUShort(contents, 4);
  213. // This (should) include the 8 byte header size
  214. int recordLen = record.getRecordSize();
  215. String fmt = ind + "At position %2$d (%2$04x): type is %3$d (%3$04x), len is %4$d (%4$04x) (%5$d) - record claims %6$d";
  216. out.printf(Locale.ROOT, (fmt) + "%n", "", pos, atomType, atomLen, atomLen + 8, recordLen);
  217. // Check for corrupt / lying ones
  218. if (recordLen != 8 && (recordLen != (atomLen + 8))) {
  219. out.printf(Locale.ROOT, ind + "** Atom length of %2d (%3d) doesn't match record length of %4d%n", atomLen, atomLen + 8, recordLen);
  220. }
  221. // Print the record's details
  222. String recordStr = record.toString().replace("\n", String.format(Locale.ROOT, "\n" + ind, ""));
  223. out.printf(Locale.ROOT, ind + "%2$s%n", "", recordStr);
  224. if (record instanceof EscherContainerRecord) {
  225. walkEscherDDF((indent + 3), pos + 8, (int) atomLen);
  226. }
  227. // Handle records that seem to lie
  228. if (atomType == 61451L) {
  229. // Normally claims a size of 8
  230. recordLen = (int) atomLen + 8;
  231. }
  232. if (atomType == 61453L) {
  233. // Returns EscherContainerRecord, but really msofbtClientTextbox
  234. recordLen = (int) atomLen + 8;
  235. record.fillFields(contents, 0, erf);
  236. if (!(record instanceof EscherTextboxRecord)) {
  237. out.printf(Locale.ROOT, ind + "%2$s%n", "", "** Really a msofbtClientTextbox !");
  238. }
  239. }
  240. // Decide on what to do, based on how the lengths match up
  241. if (recordLen == 8 && atomLen > 8) {
  242. // Assume it has children, rather than being corrupted
  243. walkEscherDDF((indent + 3), pos + 8, (int) atomLen);
  244. }
  245. // Wind on our length + our header
  246. pos = Math.toIntExact(pos + atomLen) + 8;
  247. len = Math.toIntExact(len - atomLen) - 8;
  248. // Move on to the next one, if we're not at the end yet
  249. if (len >= 8) {
  250. walkEscherDDF(indent, pos, len);
  251. }
  252. }
  253. /**
  254. * Use the basic record format groking code to walk the Escher records
  255. */
  256. public void walkEscherBasic(int indent, int pos, int len) throws IOException {
  257. if (len < 8) {
  258. return;
  259. }
  260. final String ind = (indent == 0) ? "%1$s" : "%1$" + indent + "s";
  261. long type = LittleEndian.getUShort(docstream, pos + 2);
  262. long atomlen = LittleEndian.getUInt(docstream, pos + 4);
  263. String fmt = ind + "At position %2$d ($2$04x): type is %3$d (%3$04x), len is %4$d (%4$04x)";
  264. out.printf(Locale.ROOT, (fmt) + "%n", "", pos, type, atomlen);
  265. String typeName = RecordTypes.forTypeID((short) type).name();
  266. out.printf(Locale.ROOT, ind + "%2$s%n", "That's an Escher Record: ", typeName);
  267. // Record specific dumps
  268. if (type == 61453L) {
  269. // Text Box. Print out first 8 bytes of data, then 8 4 later
  270. HexDump.dump(docstream, 0, out, pos + 8, 8);
  271. HexDump.dump(docstream, 0, out, pos + 20, 8);
  272. out.println();
  273. }
  274. // Blank line before next entry
  275. out.println();
  276. // Look in children if we are a container
  277. if (type == 61443L || type == 61444L) {
  278. walkEscherBasic((indent + 3), pos + 8, (int) atomlen);
  279. }
  280. // Keep going if not yet at end
  281. if (atomlen < len) {
  282. int atomleni = (int) atomlen;
  283. walkEscherBasic(indent, pos + atomleni + 8, len - atomleni - 8);
  284. }
  285. }
  286. }