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.

HemfCommentRecord.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.hemf.record;
  16. import java.io.IOException;
  17. import org.apache.poi.util.IOUtils;
  18. import org.apache.poi.util.Internal;
  19. import org.apache.poi.util.LittleEndian;
  20. import org.apache.poi.util.LittleEndianInputStream;
  21. import org.apache.poi.util.RecordFormatException;
  22. /**
  23. * This is the outer comment record that is recognized
  24. * by the initial parse by {@link HemfRecordType#comment}.
  25. * However, there are four types of comment: EMR_COMMENT,
  26. * EMR_COMMENT_EMFPLUS, EMR_COMMENT_EMFSPOOL, and EMF_COMMENT_PUBLIC.
  27. * To get the underlying comment, call {@link #getComment()}.
  28. *
  29. */
  30. @Internal
  31. public class HemfCommentRecord implements HemfRecord {
  32. private static final int MAX_RECORD_LENGTH = 1_000_000;
  33. public final static long COMMENT_EMFSPOOL = 0x00000000;
  34. public final static long COMMENT_EMFPLUS = 0x2B464D45;
  35. public final static long COMMENT_PUBLIC = 0x43494447;
  36. private AbstractHemfComment comment;
  37. @Override
  38. public HemfRecordType getRecordType() {
  39. return HemfRecordType.comment;
  40. }
  41. @Override
  42. public long init(LittleEndianInputStream leis, long recordId, long recordSize) throws IOException {
  43. long dataSize = leis.readUInt(); recordSize -= LittleEndian.INT_SIZE;
  44. byte[] optionalCommentIndentifierBuffer = new byte[4];
  45. leis.readFully(optionalCommentIndentifierBuffer);
  46. dataSize = dataSize-LittleEndian.INT_SIZE; //size minus the first int which could be a comment identifier
  47. recordSize -= LittleEndian.INT_SIZE;
  48. long optionalCommentIdentifier = LittleEndian.getInt(optionalCommentIndentifierBuffer) & 0x00FFFFFFFFL;
  49. if (optionalCommentIdentifier == COMMENT_EMFSPOOL) {
  50. comment = new HemfCommentEMFSpool(readToByteArray(leis, dataSize, recordSize));
  51. } else if (optionalCommentIdentifier == COMMENT_EMFPLUS) {
  52. comment = new HemfCommentEMFPlus(readToByteArray(leis, dataSize, recordSize));
  53. } else if (optionalCommentIdentifier == COMMENT_PUBLIC) {
  54. comment = CommentPublicParser.parse(readToByteArray(leis, dataSize, recordSize));
  55. } else {
  56. comment = new HemfComment(readToByteArray(optionalCommentIndentifierBuffer, leis, dataSize, recordSize));
  57. }
  58. return recordSize;
  59. }
  60. //this prepends the initial "int" which turned out NOT to be
  61. //a signifier of emfplus, spool, public.
  62. private byte[] readToByteArray(byte[] initialBytes, LittleEndianInputStream leis,
  63. long remainingDataSize, long remainingRecordSize) throws IOException {
  64. if (remainingDataSize > Integer.MAX_VALUE) {
  65. throw new RecordFormatException("Data size can't be > Integer.MAX_VALUE");
  66. }
  67. if (remainingRecordSize > Integer.MAX_VALUE) {
  68. throw new RecordFormatException("Record size can't be > Integer.MAX_VALUE");
  69. }
  70. if (remainingRecordSize == 0) {
  71. return new byte[0];
  72. }
  73. int dataSize = (int)remainingDataSize;
  74. int recordSize = (int)remainingRecordSize;
  75. byte[] arr = IOUtils.safelyAllocate(dataSize+initialBytes.length, MAX_RECORD_LENGTH);
  76. System.arraycopy(initialBytes,0,arr, 0, initialBytes.length);
  77. long read = IOUtils.readFully(leis, arr, initialBytes.length, dataSize);
  78. if (read != dataSize) {
  79. throw new RecordFormatException("InputStream ended before full record could be read");
  80. }
  81. long toSkip = recordSize-dataSize;
  82. long skipped = IOUtils.skipFully(leis, toSkip);
  83. if (toSkip != skipped) {
  84. throw new RecordFormatException("InputStream ended before full record could be read");
  85. }
  86. return arr;
  87. }
  88. private byte[] readToByteArray(LittleEndianInputStream leis, long dataSize, long recordSize) throws IOException {
  89. if (recordSize == 0) {
  90. return new byte[0];
  91. }
  92. byte[] arr = IOUtils.safelyAllocate(dataSize, MAX_RECORD_LENGTH);
  93. long read = IOUtils.readFully(leis, arr);
  94. if (read != dataSize) {
  95. throw new RecordFormatException("InputStream ended before full record could be read");
  96. }
  97. long toSkip = recordSize-dataSize;
  98. long skipped = IOUtils.skipFully(leis, recordSize-dataSize);
  99. if (toSkip != skipped) {
  100. throw new RecordFormatException("InputStream ended before full record could be read");
  101. }
  102. return arr;
  103. }
  104. public AbstractHemfComment getComment() {
  105. return comment;
  106. }
  107. private static class CommentPublicParser {
  108. private static final long WINDOWS_METAFILE = 0x80000001L; //wmf
  109. private static final long BEGINGROUP = 0x00000002; //beginning of a group of drawing records
  110. private static final long ENDGROUP = 0x00000003; //end of a group of drawing records
  111. private static final long MULTIFORMATS = 0x40000004; //allows multiple definitions of an image, including encapsulated postscript
  112. private static final long UNICODE_STRING = 0x00000040; //reserved. must not be used
  113. private static final long UNICODE_END = 0x00000080; //reserved, must not be used
  114. private static AbstractHemfComment parse(byte[] bytes) {
  115. long publicCommentIdentifier = LittleEndian.getUInt(bytes, 0);
  116. if (publicCommentIdentifier == WINDOWS_METAFILE) {
  117. return new HemfCommentPublic.WindowsMetafile(bytes);
  118. } else if (publicCommentIdentifier == BEGINGROUP) {
  119. return new HemfCommentPublic.BeginGroup(bytes);
  120. } else if (publicCommentIdentifier == ENDGROUP) {
  121. return new HemfCommentPublic.EndGroup(bytes);
  122. } else if (publicCommentIdentifier == MULTIFORMATS) {
  123. return new HemfCommentPublic.MultiFormats(bytes);
  124. } else if (publicCommentIdentifier == UNICODE_STRING || publicCommentIdentifier == UNICODE_END) {
  125. throw new RuntimeException("UNICODE_STRING/UNICODE_END values are reserved in CommentPublic records");
  126. }
  127. throw new RuntimeException("Unrecognized public comment type:" +publicCommentIdentifier + " ; " + WINDOWS_METAFILE);
  128. }
  129. }
  130. }