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.

HemfCommentEMFPlus.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 java.util.ArrayList;
  18. import java.util.List;
  19. import org.apache.poi.hemf.hemfplus.record.HemfPlusRecord;
  20. import org.apache.poi.hemf.hemfplus.record.HemfPlusRecordType;
  21. import org.apache.poi.util.IOUtils;
  22. import org.apache.poi.util.Internal;
  23. import org.apache.poi.util.LittleEndian;
  24. import org.apache.poi.util.RecordFormatException;
  25. /**
  26. * An HemfCommentEMFPlus may contain one or more EMFPlus records
  27. */
  28. @Internal
  29. public class HemfCommentEMFPlus extends AbstractHemfComment {
  30. private static final int MAX_RECORD_LENGTH = 1_000_000;
  31. long dataSize;
  32. public HemfCommentEMFPlus(byte[] rawBytes) {
  33. //these rawBytes contain only the EMFPlusRecord(s?)
  34. //the EmfComment type, size, datasize and comment identifier have all been stripped.
  35. //The EmfPlus type, flags, size, data size should start at rawBytes[0]
  36. super(rawBytes);
  37. }
  38. public List<HemfPlusRecord> getRecords() {
  39. return HemfPlusParser.parse(getRawBytes());
  40. }
  41. private static class HemfPlusParser {
  42. public static List<HemfPlusRecord> parse(byte[] bytes) {
  43. List<HemfPlusRecord> records = new ArrayList<>();
  44. int offset = 0;
  45. while (offset < bytes.length) {
  46. if (offset + 12 > bytes.length) {
  47. //if header will go beyond bytes, stop now
  48. //TODO: log or throw
  49. break;
  50. }
  51. int type = LittleEndian.getUShort(bytes, offset); offset += LittleEndian.SHORT_SIZE;
  52. int flags = LittleEndian.getUShort(bytes, offset); offset += LittleEndian.SHORT_SIZE;
  53. long sizeLong = LittleEndian.getUInt(bytes, offset); offset += LittleEndian.INT_SIZE;
  54. if (sizeLong >= Integer.MAX_VALUE) {
  55. throw new RecordFormatException("size of emf record >= Integer.MAX_VALUE");
  56. }
  57. int size = (int)sizeLong;
  58. long dataSizeLong = LittleEndian.getUInt(bytes, offset); offset += LittleEndian.INT_SIZE;
  59. if (dataSizeLong >= Integer.MAX_VALUE) {
  60. throw new RuntimeException("data size of emfplus record cannot be >= Integer.MAX_VALUE");
  61. }
  62. int dataSize = (int)dataSizeLong;
  63. if (dataSize + offset > bytes.length) {
  64. //TODO: log or throw?
  65. break;
  66. }
  67. HemfPlusRecord record = buildRecord(type, flags, dataSize, offset, bytes);
  68. records.add(record);
  69. offset += dataSize;
  70. }
  71. return records;
  72. }
  73. private static HemfPlusRecord buildRecord(int recordId, int flags, int size, int offset, byte[] bytes) {
  74. HemfPlusRecord record = null;
  75. HemfPlusRecordType type = HemfPlusRecordType.getById(recordId);
  76. if (type == null) {
  77. throw new RuntimeException("Undefined record of type:"+recordId);
  78. }
  79. try {
  80. record = type.clazz.newInstance();
  81. } catch (InstantiationException e) {
  82. throw new RuntimeException(e);
  83. } catch (IllegalAccessException e) {
  84. throw new RuntimeException(e);
  85. }
  86. byte[] dataBytes = IOUtils.safelyAllocate(size, MAX_RECORD_LENGTH);
  87. System.arraycopy(bytes, offset, dataBytes, 0, size);
  88. try {
  89. record.init(dataBytes, recordId, flags);
  90. } catch (IOException e) {
  91. throw new RuntimeException(e);
  92. }
  93. return record;
  94. }
  95. }
  96. }