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.

HSLFSlideShowImpl.java 36KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  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.usermodel;
  16. import static org.apache.poi.hslf.usermodel.HSLFSlideShow.POWERPOINT_DOCUMENT;
  17. import static org.apache.poi.hslf.usermodel.HSLFSlideShow.PP95_DOCUMENT;
  18. import java.io.ByteArrayInputStream;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.Closeable;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.io.OutputStream;
  25. import java.util.ArrayList;
  26. import java.util.Collections;
  27. import java.util.HashMap;
  28. import java.util.List;
  29. import java.util.Map;
  30. import java.util.NavigableMap;
  31. import java.util.TreeMap;
  32. import org.apache.poi.POIDocument;
  33. import org.apache.poi.hpsf.PropertySet;
  34. import org.apache.poi.hslf.exceptions.CorruptPowerPointFileException;
  35. import org.apache.poi.hslf.exceptions.HSLFException;
  36. import org.apache.poi.hslf.exceptions.OldPowerPointFormatException;
  37. import org.apache.poi.hslf.record.CurrentUserAtom;
  38. import org.apache.poi.hslf.record.DocumentEncryptionAtom;
  39. import org.apache.poi.hslf.record.ExOleObjStg;
  40. import org.apache.poi.hslf.record.PersistPtrHolder;
  41. import org.apache.poi.hslf.record.PersistRecord;
  42. import org.apache.poi.hslf.record.PositionDependentRecord;
  43. import org.apache.poi.hslf.record.Record;
  44. import org.apache.poi.hslf.record.RecordTypes;
  45. import org.apache.poi.hslf.record.UserEditAtom;
  46. import org.apache.poi.poifs.crypt.EncryptionInfo;
  47. import org.apache.poi.poifs.filesystem.DirectoryNode;
  48. import org.apache.poi.poifs.filesystem.DocumentEntry;
  49. import org.apache.poi.poifs.filesystem.DocumentInputStream;
  50. import org.apache.poi.poifs.filesystem.EntryUtils;
  51. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  52. import org.apache.poi.sl.usermodel.PictureData.PictureType;
  53. import org.apache.poi.util.IOUtils;
  54. import org.apache.poi.util.LittleEndian;
  55. import org.apache.poi.util.LittleEndianConsts;
  56. import org.apache.poi.util.POILogFactory;
  57. import org.apache.poi.util.POILogger;
  58. /**
  59. * This class contains the main functionality for the Powerpoint file
  60. * "reader". It is only a very basic class for now
  61. */
  62. public final class HSLFSlideShowImpl extends POIDocument implements Closeable {
  63. private static final POILogger logger = POILogFactory.getLogger(HSLFSlideShowImpl.class);
  64. static final int UNSET_OFFSET = -1;
  65. //arbitrarily selected; may need to increase
  66. private static final int MAX_RECORD_LENGTH = 200_000_000;
  67. private static final String DUAL_STORAGE_NAME = "PP97_DUALSTORAGE";
  68. // Holds metadata on where things are in our document
  69. private CurrentUserAtom currentUser;
  70. // Low level contents of the file
  71. private byte[] _docstream;
  72. // Low level contents
  73. private org.apache.poi.hslf.record.Record[] _records;
  74. // Raw Pictures contained in the pictures stream
  75. private List<HSLFPictureData> _pictures;
  76. // Embedded objects stored in storage records in the document stream, lazily populated.
  77. private HSLFObjectData[] _objects;
  78. /**
  79. * Constructs a Powerpoint document from fileName. Parses the document
  80. * and places all the important stuff into data structures.
  81. *
  82. * @param fileName The name of the file to read.
  83. * @throws IOException if there is a problem while parsing the document.
  84. */
  85. @SuppressWarnings("resource")
  86. public HSLFSlideShowImpl(String fileName) throws IOException {
  87. this(new POIFSFileSystem(new File(fileName)));
  88. }
  89. /**
  90. * Constructs a Powerpoint document from an input stream. Parses the
  91. * document and places all the important stuff into data structures.
  92. *
  93. * @param inputStream the source of the data
  94. * @throws IOException if there is a problem while parsing the document.
  95. */
  96. @SuppressWarnings("resource")
  97. public HSLFSlideShowImpl(InputStream inputStream) throws IOException {
  98. //do Ole stuff
  99. this(new POIFSFileSystem(inputStream));
  100. }
  101. /**
  102. * Constructs a Powerpoint document from a POIFS Filesystem. Parses the
  103. * document and places all the important stuff into data structures.
  104. *
  105. * @param filesystem the POIFS FileSystem to read from
  106. * @throws IOException if there is a problem while parsing the document.
  107. */
  108. public HSLFSlideShowImpl(POIFSFileSystem filesystem) throws IOException {
  109. this(filesystem.getRoot());
  110. }
  111. /**
  112. * Constructs a Powerpoint document from a specific point in a
  113. * POIFS Filesystem. Parses the document and places all the
  114. * important stuff into data structures.
  115. *
  116. * @param dir the POIFS directory to read from
  117. * @throws IOException if there is a problem while parsing the document.
  118. */
  119. public HSLFSlideShowImpl(DirectoryNode dir) throws IOException {
  120. super(handleDualStorage(dir));
  121. try {
  122. // First up, grab the "Current User" stream
  123. // We need this before we can detect Encrypted Documents
  124. readCurrentUserStream();
  125. // Next up, grab the data that makes up the
  126. // PowerPoint stream
  127. readPowerPointStream();
  128. // Now, build records based on the PowerPoint stream
  129. buildRecords();
  130. // Look for any other streams
  131. readOtherStreams();
  132. } catch (RuntimeException | IOException e) {
  133. // clean up the filesystem when we cannot read it here to avoid
  134. // leaking file handles
  135. dir.getFileSystem().close();
  136. throw e;
  137. }
  138. }
  139. private static DirectoryNode handleDualStorage(DirectoryNode dir) throws IOException {
  140. // when there's a dual storage entry, use it, as the outer document can't be read quite probably ...
  141. if (!dir.hasEntry(DUAL_STORAGE_NAME)) {
  142. return dir;
  143. }
  144. return (DirectoryNode) dir.getEntry(DUAL_STORAGE_NAME);
  145. }
  146. /**
  147. * Constructs a new, empty, Powerpoint document.
  148. */
  149. public static HSLFSlideShowImpl create() {
  150. InputStream is = HSLFSlideShowImpl.class.getResourceAsStream("/org/apache/poi/hslf/data/empty.ppt");
  151. if (is == null) {
  152. throw new HSLFException("Missing resource 'empty.ppt'");
  153. }
  154. try {
  155. try {
  156. return new HSLFSlideShowImpl(is);
  157. } finally {
  158. is.close();
  159. }
  160. } catch (IOException e) {
  161. throw new HSLFException(e);
  162. }
  163. }
  164. /**
  165. * Extracts the main PowerPoint document stream from the
  166. * POI file, ready to be passed
  167. *
  168. * @throws IOException when the powerpoint can't be read
  169. */
  170. private void readPowerPointStream() throws IOException {
  171. final DirectoryNode dir = getDirectory();
  172. if (!dir.hasEntry(POWERPOINT_DOCUMENT) && dir.hasEntry(PP95_DOCUMENT)) {
  173. throw new OldPowerPointFormatException("You seem to have supplied a PowerPoint95 file, which isn't supported");
  174. }
  175. // Get the main document stream
  176. DocumentEntry docProps = (DocumentEntry)dir.getEntry(POWERPOINT_DOCUMENT);
  177. // Grab the document stream
  178. int len = docProps.getSize();
  179. try (InputStream is = dir.createDocumentInputStream(docProps)) {
  180. _docstream = IOUtils.toByteArray(is, len);
  181. }
  182. }
  183. /**
  184. * Builds the list of records, based on the contents
  185. * of the PowerPoint stream
  186. */
  187. private void buildRecords() throws IOException {
  188. // The format of records in a powerpoint file are:
  189. // <little endian 2 byte "info">
  190. // <little endian 2 byte "type">
  191. // <little endian 4 byte "length">
  192. // If it has a zero length, following it will be another record
  193. // <xx xx yy yy 00 00 00 00> <xx xx yy yy zz zz zz zz>
  194. // If it has a length, depending on its type it may have children or data
  195. // If it has children, these will follow straight away
  196. // <xx xx yy yy zz zz zz zz <xx xx yy yy zz zz zz zz>>
  197. // If it has data, this will come straigh after, and run for the length
  198. // <xx xx yy yy zz zz zz zz dd dd dd dd dd dd dd>
  199. // All lengths given exclude the 8 byte record header
  200. // (Data records are known as Atoms)
  201. // Document should start with:
  202. // 0F 00 E8 03 ## ## ## ##
  203. // (type 1000 = document, info 00 0f is normal, rest is document length)
  204. // 01 00 E9 03 28 00 00 00
  205. // (type 1001 = document atom, info 00 01 normal, 28 bytes long)
  206. // 80 16 00 00 E0 10 00 00 xx xx xx xx xx xx xx xx
  207. // 05 00 00 00 0A 00 00 00 xx xx xx
  208. // (the contents of the document atom, not sure what it means yet)
  209. // (records then follow)
  210. // When parsing a document, look to see if you know about that type
  211. // of the current record. If you know it's a type that has children,
  212. // process the record's data area looking for more records
  213. // If you know about the type and it doesn't have children, either do
  214. // something with the data (eg TextRun) or skip over it
  215. // If you don't know about the type, play safe and skip over it (using
  216. // its length to know where the next record will start)
  217. //
  218. _records = read(_docstream, (int) currentUser.getCurrentEditOffset());
  219. }
  220. private org.apache.poi.hslf.record.Record[] read(byte[] docstream, int usrOffset) throws IOException {
  221. //sort found records by offset.
  222. //(it is not necessary but SlideShow.findMostRecentCoreRecords() expects them sorted)
  223. NavigableMap<Integer, Record> records = new TreeMap<>(); // offset -> record
  224. Map<Integer, Integer> persistIds = new HashMap<>(); // offset -> persistId
  225. initRecordOffsets(docstream, usrOffset, records, persistIds);
  226. HSLFSlideShowEncrypted decryptData = new HSLFSlideShowEncrypted(docstream, records);
  227. for (Map.Entry<Integer, Record> entry : records.entrySet()) {
  228. Integer offset = entry.getKey();
  229. org.apache.poi.hslf.record.Record record = entry.getValue();
  230. Integer persistId = persistIds.get(offset);
  231. if (record == null) {
  232. // all plain records have been already added,
  233. // only new records need to be decrypted (tbd #35897)
  234. decryptData.decryptRecord(docstream, persistId, offset);
  235. record = Record.buildRecordAtOffset(docstream, offset);
  236. entry.setValue(record);
  237. }
  238. if (record instanceof PersistRecord) {
  239. ((PersistRecord) record).setPersistId(persistId);
  240. }
  241. }
  242. decryptData.close();
  243. return records.values().toArray(new org.apache.poi.hslf.record.Record[0]);
  244. }
  245. private void initRecordOffsets(byte[] docstream, int usrOffset, NavigableMap<Integer, Record> recordMap, Map<Integer, Integer> offset2id) {
  246. while (usrOffset != 0) {
  247. UserEditAtom usr = (UserEditAtom) Record.buildRecordAtOffset(docstream, usrOffset);
  248. recordMap.put(usrOffset, usr);
  249. int psrOffset = usr.getPersistPointersOffset();
  250. PersistPtrHolder ptr = (PersistPtrHolder) Record.buildRecordAtOffset(docstream, psrOffset);
  251. recordMap.put(psrOffset, ptr);
  252. for (Map.Entry<Integer, Integer> entry : ptr.getSlideLocationsLookup().entrySet()) {
  253. Integer offset = entry.getValue();
  254. Integer id = entry.getKey();
  255. recordMap.put(offset, null); // reserve a slot for the record
  256. offset2id.put(offset, id);
  257. }
  258. usrOffset = usr.getLastUserEditAtomOffset();
  259. // check for corrupted user edit atom and try to repair it
  260. // if the next user edit atom offset is already known, we would go into an endless loop
  261. if (usrOffset > 0 && recordMap.containsKey(usrOffset)) {
  262. // a user edit atom is usually located 36 byte before the smallest known record offset
  263. usrOffset = recordMap.firstKey() - 36;
  264. // check that we really are located on a user edit atom
  265. int ver_inst = LittleEndian.getUShort(docstream, usrOffset);
  266. int type = LittleEndian.getUShort(docstream, usrOffset + 2);
  267. int len = LittleEndian.getInt(docstream, usrOffset + 4);
  268. if (ver_inst == 0 && type == 4085 && (len == 0x1C || len == 0x20)) {
  269. logger.log(POILogger.WARN, "Repairing invalid user edit atom");
  270. usr.setLastUserEditAtomOffset(usrOffset);
  271. } else {
  272. throw new CorruptPowerPointFileException("Powerpoint document contains invalid user edit atom");
  273. }
  274. }
  275. }
  276. }
  277. public DocumentEncryptionAtom getDocumentEncryptionAtom() {
  278. for (org.apache.poi.hslf.record.Record r : _records) {
  279. if (r instanceof DocumentEncryptionAtom) {
  280. return (DocumentEncryptionAtom) r;
  281. }
  282. }
  283. return null;
  284. }
  285. /**
  286. * Find the "Current User" stream, and load it
  287. */
  288. private void readCurrentUserStream() {
  289. try {
  290. currentUser = new CurrentUserAtom(getDirectory());
  291. } catch (IOException ie) {
  292. logger.log(POILogger.ERROR, "Error finding Current User Atom:\n" + ie);
  293. currentUser = new CurrentUserAtom();
  294. }
  295. }
  296. /**
  297. * Find any other streams from the filesystem, and load them
  298. */
  299. private void readOtherStreams() {
  300. // Currently, there aren't any
  301. }
  302. /**
  303. * Find and read in pictures contained in this presentation.
  304. * This is lazily called as and when we want to touch pictures.
  305. */
  306. private void readPictures() throws IOException {
  307. _pictures = new ArrayList<>();
  308. // if the presentation doesn't contain pictures - will use a null set instead
  309. if (!getDirectory().hasEntry("Pictures")) {
  310. return;
  311. }
  312. DocumentEntry entry = (DocumentEntry) getDirectory().getEntry("Pictures");
  313. DocumentInputStream is = getDirectory().createDocumentInputStream(entry);
  314. byte[] pictstream = IOUtils.toByteArray(is, entry.getSize());
  315. is.close();
  316. try (HSLFSlideShowEncrypted decryptData = new HSLFSlideShowEncrypted(getDocumentEncryptionAtom())) {
  317. int pos = 0;
  318. // An empty picture record (length 0) will take up 8 bytes
  319. while (pos <= (pictstream.length - 8)) {
  320. int offset = pos;
  321. decryptData.decryptPicture(pictstream, offset);
  322. // Image signature
  323. int signature = LittleEndian.getUShort(pictstream, pos);
  324. pos += LittleEndianConsts.SHORT_SIZE;
  325. // Image type + 0xF018
  326. int type = LittleEndian.getUShort(pictstream, pos);
  327. pos += LittleEndianConsts.SHORT_SIZE;
  328. // Image size (excluding the 8 byte header)
  329. int imgsize = LittleEndian.getInt(pictstream, pos);
  330. pos += LittleEndianConsts.INT_SIZE;
  331. // When parsing the BStoreDelay stream, [MS-ODRAW] says that we
  332. // should terminate if the type isn't 0xf007 or 0xf018->0xf117
  333. if (!((type == 0xf007) || (type >= 0xf018 && type <= 0xf117))) {
  334. break;
  335. }
  336. // The image size must be 0 or greater
  337. // (0 is allowed, but odd, since we do wind on by the header each
  338. // time, so we won't get stuck)
  339. if (imgsize < 0) {
  340. throw new CorruptPowerPointFileException("The file contains a picture, at position " + _pictures.size() + ", which has a negatively sized data length, so we can't trust any of the picture data");
  341. }
  342. // If they type (including the bonus 0xF018) is 0, skip it
  343. PictureType pt = PictureType.forNativeID(type - 0xF018);
  344. if (pt == null) {
  345. logger.log(POILogger.ERROR, "Problem reading picture: Invalid image type 0, on picture with length " + imgsize + ".\nYou document will probably become corrupted if you save it!");
  346. logger.log(POILogger.ERROR, "" + pos);
  347. } else {
  348. //The pictstream can be truncated halfway through a picture.
  349. //This is not a problem if the pictstream contains extra pictures
  350. //that are not used in any slide -- BUG-60305
  351. if (pos + imgsize > pictstream.length) {
  352. logger.log(POILogger.WARN, "\"Pictures\" stream may have ended early. In some circumstances, this is not a problem; " +
  353. "in others, this could indicate a corrupt file");
  354. break;
  355. }
  356. // Build the PictureData object from the data
  357. try {
  358. HSLFPictureData pict = HSLFPictureData.create(pt);
  359. pict.setSignature(signature);
  360. // Copy the data, ready to pass to PictureData
  361. byte[] imgdata = IOUtils.safelyClone(pictstream, pos, imgsize, MAX_RECORD_LENGTH);
  362. pict.setRawData(imgdata);
  363. pict.setOffset(offset);
  364. pict.setIndex(_pictures.size());
  365. _pictures.add(pict);
  366. } catch (IllegalArgumentException e) {
  367. logger.log(POILogger.ERROR, "Problem reading picture: " + e + "\nYou document will probably become corrupted if you save it!");
  368. }
  369. }
  370. pos += imgsize;
  371. }
  372. }
  373. }
  374. /**
  375. * remove duplicated UserEditAtoms and merge PersistPtrHolder, i.e.
  376. * remove document edit history
  377. */
  378. public void normalizeRecords() {
  379. try {
  380. updateAndWriteDependantRecords(null, null);
  381. } catch (IOException e) {
  382. throw new CorruptPowerPointFileException(e);
  383. }
  384. _records = HSLFSlideShowEncrypted.normalizeRecords(_records);
  385. }
  386. /**
  387. * This is a helper functions, which is needed for adding new position dependent records
  388. * or finally write the slideshow to a file.
  389. *
  390. * @param os the stream to write to, if null only the references are updated
  391. * @param interestingRecords a map of interesting records (PersistPtrHolder and UserEditAtom)
  392. * referenced by their RecordType. Only the very last of each type will be saved to the map.
  393. * May be null, if not needed.
  394. */
  395. @SuppressWarnings("WeakerAccess")
  396. public void updateAndWriteDependantRecords(OutputStream os, Map<RecordTypes, PositionDependentRecord> interestingRecords)
  397. throws IOException {
  398. // For position dependent records, hold where they were and now are
  399. // As we go along, update, and hand over, to any Position Dependent
  400. // records we happen across
  401. Map<Integer, Integer> oldToNewPositions = new HashMap<>();
  402. // First pass - figure out where all the position dependent
  403. // records are going to end up, in the new scheme
  404. // (Annoyingly, some powerpoint files have PersistPtrHolders
  405. // that reference slides after the PersistPtrHolder)
  406. UserEditAtom usr = null;
  407. PersistPtrHolder ptr = null;
  408. CountingOS cos = new CountingOS();
  409. for (org.apache.poi.hslf.record.Record record : _records) {
  410. // all top level records are position dependent
  411. assert (record instanceof PositionDependentRecord);
  412. PositionDependentRecord pdr = (PositionDependentRecord) record;
  413. int oldPos = pdr.getLastOnDiskOffset();
  414. int newPos = cos.size();
  415. pdr.setLastOnDiskOffset(newPos);
  416. if (oldPos != UNSET_OFFSET) {
  417. // new records don't need a mapping, as they aren't in a relation yet
  418. oldToNewPositions.put(oldPos, newPos);
  419. }
  420. // Grab interesting records as they come past
  421. // this will only save the very last record of each type
  422. RecordTypes saveme = null;
  423. int recordType = (int) record.getRecordType();
  424. if (recordType == RecordTypes.PersistPtrIncrementalBlock.typeID) {
  425. saveme = RecordTypes.PersistPtrIncrementalBlock;
  426. ptr = (PersistPtrHolder) pdr;
  427. } else if (recordType == RecordTypes.UserEditAtom.typeID) {
  428. saveme = RecordTypes.UserEditAtom;
  429. usr = (UserEditAtom) pdr;
  430. }
  431. if (interestingRecords != null && saveme != null) {
  432. interestingRecords.put(saveme, pdr);
  433. }
  434. // Dummy write out, so the position winds on properly
  435. record.writeOut(cos);
  436. }
  437. cos.close();
  438. if (usr == null || ptr == null) {
  439. throw new HSLFException("UserEditAtom or PersistPtr can't be determined.");
  440. }
  441. Map<Integer, Integer> persistIds = new HashMap<>();
  442. for (Map.Entry<Integer, Integer> entry : ptr.getSlideLocationsLookup().entrySet()) {
  443. persistIds.put(oldToNewPositions.get(entry.getValue()), entry.getKey());
  444. }
  445. try (HSLFSlideShowEncrypted encData = new HSLFSlideShowEncrypted(getDocumentEncryptionAtom())) {
  446. for (org.apache.poi.hslf.record.Record record : _records) {
  447. assert (record instanceof PositionDependentRecord);
  448. // We've already figured out their new location, and
  449. // told them that
  450. // Tell them of the positions of the other records though
  451. PositionDependentRecord pdr = (PositionDependentRecord) record;
  452. Integer persistId = persistIds.get(pdr.getLastOnDiskOffset());
  453. if (persistId == null) {
  454. persistId = 0;
  455. }
  456. // For now, we're only handling PositionDependentRecord's that
  457. // happen at the top level.
  458. // In future, we'll need the handle them everywhere, but that's
  459. // a bit trickier
  460. pdr.updateOtherRecordReferences(oldToNewPositions);
  461. // Whatever happens, write out that record tree
  462. if (os != null) {
  463. record.writeOut(encData.encryptRecord(os, persistId, record));
  464. }
  465. }
  466. }
  467. // Update and write out the Current User atom
  468. int oldLastUserEditAtomPos = (int) currentUser.getCurrentEditOffset();
  469. Integer newLastUserEditAtomPos = oldToNewPositions.get(oldLastUserEditAtomPos);
  470. if (newLastUserEditAtomPos == null || usr.getLastOnDiskOffset() != newLastUserEditAtomPos) {
  471. throw new HSLFException("Couldn't find the new location of the last UserEditAtom that used to be at " + oldLastUserEditAtomPos);
  472. }
  473. currentUser.setCurrentEditOffset(usr.getLastOnDiskOffset());
  474. }
  475. /**
  476. * Writes out the slideshow to the currently open file.
  477. * <p>
  478. * <p>This will fail (with an {@link IllegalStateException} if the
  479. * slideshow was opened read-only, opened from an {@link InputStream}
  480. * instead of a File, or if this is not the root document. For those cases,
  481. * you must use {@link #write(OutputStream)} or {@link #write(File)} to
  482. * write to a brand new document.
  483. *
  484. * @throws IOException thrown on errors writing to the file
  485. * @throws IllegalStateException if this isn't from a writable File
  486. * @since POI 3.15 beta 3
  487. */
  488. @Override
  489. public void write() throws IOException {
  490. validateInPlaceWritePossible();
  491. // Write the PowerPoint streams to the current FileSystem
  492. // No need to do anything to other streams, already there!
  493. write(getDirectory().getFileSystem(), false);
  494. // Sync with the File on disk
  495. getDirectory().getFileSystem().writeFilesystem();
  496. }
  497. /**
  498. * Writes out the slideshow file the is represented by an instance
  499. * of this class.
  500. * <p>This will write out only the common OLE2 streams. If you require all
  501. * streams to be written out, use {@link #write(File, boolean)}
  502. * with <code>preserveNodes</code> set to <code>true</code>.
  503. *
  504. * @param newFile The File to write to.
  505. * @throws IOException If there is an unexpected IOException from writing to the File
  506. */
  507. @Override
  508. public void write(File newFile) throws IOException {
  509. // Write out, but only the common streams
  510. write(newFile, false);
  511. }
  512. /**
  513. * Writes out the slideshow file the is represented by an instance
  514. * of this class.
  515. * If you require all streams to be written out (eg Marcos, embeded
  516. * documents), then set <code>preserveNodes</code> set to <code>true</code>
  517. *
  518. * @param newFile The File to write to.
  519. * @param preserveNodes Should all OLE2 streams be written back out, or only the common ones?
  520. * @throws IOException If there is an unexpected IOException from writing to the File
  521. */
  522. public void write(File newFile, boolean preserveNodes) throws IOException {
  523. // Get a new FileSystem to write into
  524. try (POIFSFileSystem outFS = POIFSFileSystem.create(newFile)) {
  525. // Write into the new FileSystem
  526. write(outFS, preserveNodes);
  527. // Send the POIFSFileSystem object out to the underlying stream
  528. outFS.writeFilesystem();
  529. }
  530. }
  531. /**
  532. * Writes out the slideshow file the is represented by an instance
  533. * of this class.
  534. * <p>This will write out only the common OLE2 streams. If you require all
  535. * streams to be written out, use {@link #write(OutputStream, boolean)}
  536. * with <code>preserveNodes</code> set to <code>true</code>.
  537. *
  538. * @param out The OutputStream to write to.
  539. * @throws IOException If there is an unexpected IOException from
  540. * the passed in OutputStream
  541. */
  542. @Override
  543. public void write(OutputStream out) throws IOException {
  544. // Write out, but only the common streams
  545. write(out, false);
  546. }
  547. /**
  548. * Writes out the slideshow file the is represented by an instance
  549. * of this class.
  550. * If you require all streams to be written out (eg Marcos, embeded
  551. * documents), then set <code>preserveNodes</code> set to <code>true</code>
  552. *
  553. * @param out The OutputStream to write to.
  554. * @param preserveNodes Should all OLE2 streams be written back out, or only the common ones?
  555. * @throws IOException If there is an unexpected IOException from
  556. * the passed in OutputStream
  557. */
  558. public void write(OutputStream out, boolean preserveNodes) throws IOException {
  559. // Get a new FileSystem to write into
  560. try (POIFSFileSystem outFS = new POIFSFileSystem()) {
  561. // Write into the new FileSystem
  562. write(outFS, preserveNodes);
  563. // Send the POIFSFileSystem object out to the underlying stream
  564. outFS.writeFilesystem(out);
  565. }
  566. }
  567. private void write(POIFSFileSystem outFS, boolean copyAllOtherNodes) throws IOException {
  568. // read properties and pictures, with old encryption settings where appropriate
  569. if (_pictures == null) {
  570. readPictures();
  571. }
  572. getDocumentSummaryInformation();
  573. // The list of entries we've written out
  574. final List<String> writtenEntries = new ArrayList<>(1);
  575. // set new encryption settings
  576. try (HSLFSlideShowEncrypted encryptedSS = new HSLFSlideShowEncrypted(getDocumentEncryptionAtom())) {
  577. _records = encryptedSS.updateEncryptionRecord(_records);
  578. // Write out the Property Streams
  579. writeProperties(outFS, writtenEntries);
  580. BufAccessBAOS baos = new BufAccessBAOS();
  581. // For position dependent records, hold where they were and now are
  582. // As we go along, update, and hand over, to any Position Dependent
  583. // records we happen across
  584. updateAndWriteDependantRecords(baos, null);
  585. // Update our cached copy of the bytes that make up the PPT stream
  586. _docstream = baos.toByteArray();
  587. baos.close();
  588. // Write the PPT stream into the POIFS layer
  589. ByteArrayInputStream bais = new ByteArrayInputStream(_docstream);
  590. outFS.createOrUpdateDocument(bais, POWERPOINT_DOCUMENT);
  591. writtenEntries.add(POWERPOINT_DOCUMENT);
  592. currentUser.setEncrypted(encryptedSS.getDocumentEncryptionAtom() != null);
  593. currentUser.writeToFS(outFS);
  594. writtenEntries.add("Current User");
  595. if (_pictures.size() > 0) {
  596. BufAccessBAOS pict = new BufAccessBAOS();
  597. for (HSLFPictureData p : _pictures) {
  598. int offset = pict.size();
  599. p.write(pict);
  600. encryptedSS.encryptPicture(pict.getBuf(), offset);
  601. }
  602. outFS.createOrUpdateDocument(
  603. new ByteArrayInputStream(pict.getBuf(), 0, pict.size()), "Pictures"
  604. );
  605. writtenEntries.add("Pictures");
  606. pict.close();
  607. }
  608. }
  609. // If requested, copy over any other streams we spot, eg Macros
  610. if (copyAllOtherNodes) {
  611. EntryUtils.copyNodes(getDirectory().getFileSystem(), outFS, writtenEntries);
  612. }
  613. }
  614. @Override
  615. public EncryptionInfo getEncryptionInfo() {
  616. DocumentEncryptionAtom dea = getDocumentEncryptionAtom();
  617. return (dea != null) ? dea.getEncryptionInfo() : null;
  618. }
  619. /* ******************* adding methods follow ********************* */
  620. /**
  621. * Adds a new root level record, at the end, but before the last
  622. * PersistPtrIncrementalBlock.
  623. */
  624. @SuppressWarnings({"UnusedReturnValue", "WeakerAccess"})
  625. public synchronized int appendRootLevelRecord(Record newRecord) {
  626. int addedAt = -1;
  627. org.apache.poi.hslf.record.Record[] r = new org.apache.poi.hslf.record.Record[_records.length + 1];
  628. boolean added = false;
  629. for (int i = (_records.length - 1); i >= 0; i--) {
  630. if (added) {
  631. // Just copy over
  632. r[i] = _records[i];
  633. } else {
  634. r[(i + 1)] = _records[i];
  635. if (_records[i] instanceof PersistPtrHolder) {
  636. r[i] = newRecord;
  637. added = true;
  638. addedAt = i;
  639. }
  640. }
  641. }
  642. _records = r;
  643. return addedAt;
  644. }
  645. /**
  646. * Add a new picture to this presentation.
  647. *
  648. * @return offset of this picture in the Pictures stream
  649. */
  650. public int addPicture(HSLFPictureData img) {
  651. // Process any existing pictures if we haven't yet
  652. if (_pictures == null) {
  653. try {
  654. readPictures();
  655. } catch (IOException e) {
  656. throw new CorruptPowerPointFileException(e.getMessage());
  657. }
  658. }
  659. // Add the new picture in
  660. int offset = 0;
  661. if (_pictures.size() > 0) {
  662. HSLFPictureData prev = _pictures.get(_pictures.size() - 1);
  663. offset = prev.getOffset() + prev.getRawData().length + 8;
  664. }
  665. img.setOffset(offset);
  666. img.setIndex(_pictures.size() + 1);
  667. _pictures.add(img);
  668. return offset;
  669. }
  670. /* ******************* fetching methods follow ********************* */
  671. /**
  672. * Returns an array of all the records found in the slideshow
  673. */
  674. public org.apache.poi.hslf.record.Record[] getRecords() {
  675. return _records;
  676. }
  677. /**
  678. * Returns an array of the bytes of the file. Only correct after a
  679. * call to open or write - at all other times might be wrong!
  680. */
  681. public byte[] getUnderlyingBytes() {
  682. return _docstream;
  683. }
  684. /**
  685. * Fetch the Current User Atom of the document
  686. */
  687. public CurrentUserAtom getCurrentUserAtom() {
  688. return currentUser;
  689. }
  690. /**
  691. * Return list of pictures contained in this presentation
  692. *
  693. * @return list with the read pictures or an empty list if the
  694. * presentation doesn't contain pictures.
  695. */
  696. public List<HSLFPictureData> getPictureData() {
  697. if (_pictures == null) {
  698. try {
  699. readPictures();
  700. } catch (IOException e) {
  701. throw new CorruptPowerPointFileException(e.getMessage());
  702. }
  703. }
  704. return Collections.unmodifiableList(_pictures);
  705. }
  706. /**
  707. * Gets embedded object data from the slide show.
  708. *
  709. * @return the embedded objects.
  710. */
  711. public HSLFObjectData[] getEmbeddedObjects() {
  712. if (_objects == null) {
  713. List<HSLFObjectData> objects = new ArrayList<>();
  714. for (org.apache.poi.hslf.record.Record r : _records) {
  715. if (r instanceof ExOleObjStg) {
  716. objects.add(new HSLFObjectData((ExOleObjStg) r));
  717. }
  718. }
  719. _objects = objects.toArray(new HSLFObjectData[0]);
  720. }
  721. return _objects;
  722. }
  723. @Override
  724. public void close() throws IOException {
  725. // only close the filesystem, if we are based on the root node.
  726. // embedded documents/slideshows shouldn't close the parent container
  727. if (getDirectory().getParent() == null ||
  728. getDirectory().getName().equals(DUAL_STORAGE_NAME)) {
  729. POIFSFileSystem fs = getDirectory().getFileSystem();
  730. if (fs != null) {
  731. fs.close();
  732. }
  733. }
  734. }
  735. @Override
  736. protected String getEncryptedPropertyStreamName() {
  737. return "EncryptedSummary";
  738. }
  739. void writePropertiesImpl() throws IOException {
  740. super.writeProperties();
  741. }
  742. PropertySet getPropertySetImpl(String setName) throws IOException {
  743. return super.getPropertySet(setName);
  744. }
  745. PropertySet getPropertySetImpl(String setName, EncryptionInfo encryptionInfo) throws IOException {
  746. return super.getPropertySet(setName, encryptionInfo);
  747. }
  748. void writePropertiesImpl(POIFSFileSystem outFS, List<String> writtenEntries) throws IOException {
  749. super.writeProperties(outFS, writtenEntries);
  750. }
  751. void validateInPlaceWritePossibleImpl() throws IllegalStateException {
  752. super.validateInPlaceWritePossible();
  753. }
  754. void clearDirectoryImpl() {
  755. super.clearDirectory();
  756. }
  757. boolean initDirectoryImpl() {
  758. return super.initDirectory();
  759. }
  760. void replaceDirectoryImpl(DirectoryNode newDirectory) {
  761. super.replaceDirectory(newDirectory);
  762. }
  763. private static class BufAccessBAOS extends ByteArrayOutputStream {
  764. public byte[] getBuf() {
  765. return buf;
  766. }
  767. }
  768. private static class CountingOS extends OutputStream {
  769. int count;
  770. @Override
  771. public void write(int b) throws IOException {
  772. count++;
  773. }
  774. @Override
  775. public void write(byte[] b) throws IOException {
  776. count += b.length;
  777. }
  778. @Override
  779. public void write(byte[] b, int off, int len) throws IOException {
  780. count += len;
  781. }
  782. public int size() {
  783. return count;
  784. }
  785. }
  786. }