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.

HemfCommentPublic.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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.ByteArrayInputStream;
  17. import java.io.InputStream;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import org.apache.poi.util.IOUtils;
  21. import org.apache.poi.util.Internal;
  22. import org.apache.poi.util.LittleEndian;
  23. import org.apache.poi.util.LittleEndianConsts;
  24. import org.apache.poi.util.RecordFormatException;
  25. /**
  26. * Container class for four subtypes of HemfCommentPublic: BeginGroup, EndGroup, MultiFormats
  27. * and Windows Metafile.
  28. */
  29. @Internal
  30. public class HemfCommentPublic {
  31. private static final int MAX_RECORD_LENGTH = 1_000_000;
  32. /**
  33. * Stub, to be implemented
  34. */
  35. public static class BeginGroup extends AbstractHemfComment {
  36. public BeginGroup(byte[] rawBytes) {
  37. super(rawBytes);
  38. }
  39. }
  40. /**
  41. * Stub, to be implemented
  42. */
  43. public static class EndGroup extends AbstractHemfComment {
  44. public EndGroup(byte[] rawBytes) {
  45. super(rawBytes);
  46. }
  47. }
  48. public static class MultiFormats extends AbstractHemfComment {
  49. public MultiFormats(byte[] rawBytes) {
  50. super(rawBytes);
  51. }
  52. /**
  53. *
  54. * @return a list of HemfMultFormatsData
  55. */
  56. public List<HemfMultiFormatsData> getData() {
  57. byte[] rawBytes = getRawBytes();
  58. //note that raw bytes includes the public comment identifier
  59. int currentOffset = 4 + 16;//4 public comment identifier, 16 for outputrect
  60. long countFormats = LittleEndian.getUInt(rawBytes, currentOffset);
  61. currentOffset += LittleEndianConsts.INT_SIZE;
  62. List<EmrFormat> emrFormatList = new ArrayList<>();
  63. for (long i = 0; i < countFormats; i++) {
  64. emrFormatList.add(new EmrFormat(rawBytes, currentOffset));
  65. currentOffset += 4 * LittleEndianConsts.INT_SIZE;
  66. }
  67. List<HemfMultiFormatsData> list = new ArrayList<>();
  68. for (EmrFormat emrFormat : emrFormatList) {
  69. byte[] data = IOUtils.safelyAllocate(emrFormat.size, MAX_RECORD_LENGTH);
  70. System.arraycopy(rawBytes, emrFormat.offset-4, data, 0, emrFormat.size);
  71. list.add(new HemfMultiFormatsData(emrFormat.signature, emrFormat.version, data));
  72. }
  73. return list;
  74. }
  75. private static class EmrFormat {
  76. long signature;
  77. long version;
  78. int size;
  79. int offset;
  80. public EmrFormat(byte[] rawBytes, int currentOffset) {
  81. signature = LittleEndian.getUInt(rawBytes, currentOffset); currentOffset += LittleEndianConsts.INT_SIZE;
  82. version = LittleEndian.getUInt(rawBytes, currentOffset); currentOffset += LittleEndianConsts.INT_SIZE;
  83. //spec says this must be a 32bit "aligned" typo for "signed"?
  84. //realistically, this has to be an int...
  85. size = LittleEndian.getInt(rawBytes, currentOffset); currentOffset += LittleEndianConsts.INT_SIZE;
  86. //y, can be long, but realistically?
  87. offset = LittleEndian.getInt(rawBytes, currentOffset); currentOffset += LittleEndianConsts.INT_SIZE;
  88. if (size < 0) {
  89. throw new RecordFormatException("size for emrformat must be > 0");
  90. }
  91. if (offset < 0) {
  92. throw new RecordFormatException("offset for emrformat must be > 0");
  93. }
  94. }
  95. }
  96. }
  97. /**
  98. * Stub, to be implemented
  99. */
  100. public static class WindowsMetafile extends AbstractHemfComment {
  101. private final byte[] wmfBytes;
  102. public WindowsMetafile(byte[] rawBytes) {
  103. super(rawBytes);
  104. int offset = LittleEndianConsts.INT_SIZE;//public comment identifier
  105. int version = LittleEndian.getUShort(rawBytes, offset); offset += LittleEndianConsts.SHORT_SIZE;
  106. int reserved = LittleEndian.getUShort(rawBytes, offset); offset += LittleEndianConsts.SHORT_SIZE;
  107. offset += LittleEndianConsts.INT_SIZE; //checksum
  108. offset += LittleEndianConsts.INT_SIZE; //flags
  109. long winMetafileSizeLong = LittleEndian.getUInt(rawBytes, offset); offset += LittleEndianConsts.INT_SIZE;
  110. if (winMetafileSizeLong == 0L) {
  111. wmfBytes = new byte[0];
  112. return;
  113. }
  114. wmfBytes = IOUtils.safelyAllocate(winMetafileSizeLong, MAX_RECORD_LENGTH);
  115. System.arraycopy(rawBytes, offset, wmfBytes, 0, wmfBytes.length);
  116. }
  117. /**
  118. *
  119. * @return an InputStream for the embedded WMF file
  120. */
  121. public InputStream getWmfInputStream() {
  122. return new ByteArrayInputStream(wmfBytes);
  123. }
  124. }
  125. /**
  126. * This encapulates a single record stored within
  127. * a HemfCommentPublic.MultiFormats record.
  128. */
  129. public static class HemfMultiFormatsData {
  130. long signature;
  131. long version;
  132. byte[] data;
  133. public HemfMultiFormatsData(long signature, long version, byte[] data) {
  134. this.signature = signature;
  135. this.version = version;
  136. this.data = data;
  137. }
  138. public long getSignature() {
  139. return signature;
  140. }
  141. public long getVersion() {
  142. return version;
  143. }
  144. public byte[] getData() {
  145. return data;
  146. }
  147. }
  148. }