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.

SubRecord.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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.hssf.record;
  16. import java.io.ByteArrayOutputStream;
  17. import java.util.Arrays;
  18. import java.util.Map;
  19. import java.util.function.Function;
  20. import java.util.function.Supplier;
  21. import java.util.stream.Collectors;
  22. import org.apache.poi.common.Duplicatable;
  23. import org.apache.poi.common.usermodel.GenericRecord;
  24. import org.apache.poi.util.GenericRecordJsonWriter;
  25. import org.apache.poi.util.GenericRecordUtil;
  26. import org.apache.poi.util.IOUtils;
  27. import org.apache.poi.util.LittleEndianInput;
  28. import org.apache.poi.util.LittleEndianOutput;
  29. import org.apache.poi.util.LittleEndianOutputStream;
  30. /**
  31. * Subrecords are part of the OBJ class.
  32. */
  33. public abstract class SubRecord implements Duplicatable, GenericRecord {
  34. public enum SubRecordTypes {
  35. UNKNOWN(-1, UnknownSubRecord::new),
  36. END(0x0000, EndSubRecord::new),
  37. GROUP_MARKER(0x0006, GroupMarkerSubRecord::new),
  38. FT_CF(0x0007, FtCfSubRecord::new),
  39. FT_PIO_GRBIT(0x0008, FtPioGrbitSubRecord::new),
  40. EMBEDDED_OBJECT_REF(0x0009, EmbeddedObjectRefSubRecord::new),
  41. FT_CBLS(0x000C, FtCblsSubRecord::new),
  42. NOTE_STRUCTURE(0x000D, NoteStructureSubRecord::new),
  43. LBS_DATA(0x0013, LbsDataSubRecord::new),
  44. COMMON_OBJECT_DATA(0x0015, CommonObjectDataSubRecord::new),
  45. ;
  46. @FunctionalInterface
  47. public interface RecordConstructor<T extends SubRecord> {
  48. /**
  49. * read a sub-record from the supplied stream
  50. *
  51. * @param in the stream to read from
  52. * @param cmoOt the objectType field of the containing CommonObjectDataSubRecord,
  53. * we need it to propagate to next sub-records as it defines what data follows
  54. * @return the created sub-record
  55. */
  56. T apply(LittleEndianInput in, int size, int cmoOt);
  57. }
  58. private static final Map<Short,SubRecordTypes> LOOKUP =
  59. Arrays.stream(values()).collect(Collectors.toMap(SubRecordTypes::getSid, Function.identity()));
  60. public final short sid;
  61. public final RecordConstructor<?> recordConstructor;
  62. SubRecordTypes(int sid, RecordConstructor<?> recordConstructor) {
  63. this.sid = (short)sid;
  64. this.recordConstructor = recordConstructor;
  65. }
  66. public static SubRecordTypes forSID(int sid) {
  67. return LOOKUP.getOrDefault((short)sid, UNKNOWN);
  68. }
  69. public short getSid() {
  70. return sid;
  71. }
  72. }
  73. //arbitrarily selected; may need to increase
  74. private static final int MAX_RECORD_LENGTH = 1_000_000;
  75. protected SubRecord() {}
  76. protected SubRecord(SubRecord other) {}
  77. /**
  78. * read a sub-record from the supplied stream
  79. *
  80. * @param in the stream to read from
  81. * @param cmoOt the objectType field of the containing CommonObjectDataSubRecord,
  82. * we need it to propagate to next sub-records as it defines what data follows
  83. * @return the created sub-record
  84. */
  85. public static SubRecord createSubRecord(LittleEndianInput in, int cmoOt) {
  86. int sid = in.readUShort();
  87. // Often (but not always) the datasize for the sub-record
  88. int size = in.readUShort();
  89. SubRecordTypes srt = SubRecordTypes.forSID(sid);
  90. return srt.recordConstructor.apply(in, size, srt == SubRecordTypes.UNKNOWN ? sid : cmoOt);
  91. }
  92. @Override
  93. public final String toString() {
  94. return GenericRecordJsonWriter.marshal(this);
  95. }
  96. /**
  97. * @return the size of the data for this record (which is always 4 bytes less than the total
  98. * record size). Note however, that ushort encoded after the record sid is usually but not
  99. * always the data size.
  100. */
  101. protected abstract int getDataSize();
  102. public byte[] serialize() {
  103. int size = getDataSize() + 4;
  104. ByteArrayOutputStream baos = new ByteArrayOutputStream(size);
  105. serialize(new LittleEndianOutputStream(baos));
  106. if (baos.size() != size) {
  107. throw new RuntimeException("write size mismatch");
  108. }
  109. return baos.toByteArray();
  110. }
  111. public abstract void serialize(LittleEndianOutput out);
  112. /**
  113. * Whether this record terminates the sub-record stream.
  114. * There are two cases when this method must be overridden and return <code>true</code>
  115. * - EndSubRecord (sid = 0x00)
  116. * - LbsDataSubRecord (sid = 0x12)
  117. *
  118. * @return whether this record is the last in the sub-record stream
  119. */
  120. public boolean isTerminating(){
  121. return false;
  122. }
  123. private static final class UnknownSubRecord extends SubRecord {
  124. private final int _sid;
  125. private final byte[] _data;
  126. public UnknownSubRecord(LittleEndianInput in, int size, int sid) {
  127. _sid = sid;
  128. byte[] buf = IOUtils.safelyAllocate(size, MAX_RECORD_LENGTH);
  129. in.readFully(buf);
  130. _data = buf;
  131. }
  132. @Override
  133. protected int getDataSize() {
  134. return _data.length;
  135. }
  136. @Override
  137. public void serialize(LittleEndianOutput out) {
  138. out.writeShort(_sid);
  139. out.writeShort(_data.length);
  140. out.write(_data);
  141. }
  142. @Override
  143. public UnknownSubRecord copy() {
  144. return this;
  145. }
  146. @Override
  147. public SubRecordTypes getGenericRecordType() {
  148. return SubRecordTypes.UNKNOWN;
  149. }
  150. @Override
  151. public Map<String, Supplier<?>> getGenericProperties() {
  152. return GenericRecordUtil.getGenericProperties(
  153. "sid", () -> _sid,
  154. "data", () -> _data
  155. );
  156. }
  157. }
  158. @Override
  159. public abstract SubRecord copy();
  160. @Override
  161. public abstract SubRecordTypes getGenericRecordType();
  162. }