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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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.ByteArrayInputStream;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import org.apache.poi.util.HexDump;
  20. import org.apache.poi.util.LittleEndian;
  21. import org.apache.poi.util.LittleEndianByteArrayOutputStream;
  22. import org.apache.poi.util.LittleEndianInputStream;
  23. /**
  24. * OBJRECORD (0x005D)<p/>
  25. *
  26. * The obj record is used to hold various graphic objects and controls.
  27. *
  28. * @author Glen Stampoultzis (glens at apache.org)
  29. */
  30. public final class ObjRecord extends Record {
  31. public final static short sid = 0x005D;
  32. private static final int NORMAL_PAD_ALIGNMENT = 2;
  33. private static int MAX_PAD_ALIGNMENT = 4;
  34. private List<SubRecord> subrecords;
  35. /** used when POI has no idea what is going on */
  36. private final byte[] _uninterpretedData;
  37. /**
  38. * Excel seems to tolerate padding to quad or double byte length
  39. */
  40. private boolean _isPaddedToQuadByteMultiple;
  41. //00000000 15 00 12 00 01 00 01 00 11 60 00 00 00 00 00 0D .........`......
  42. //00000010 26 01 00 00 00 00 00 00 00 00 &.........
  43. public ObjRecord() {
  44. subrecords = new ArrayList<SubRecord>(2);
  45. // TODO - ensure 2 sub-records (ftCmo 15h, and ftEnd 00h) are always created
  46. _uninterpretedData = null;
  47. }
  48. public ObjRecord(RecordInputStream in) {
  49. // TODO - problems with OBJ sub-records stream
  50. // MS spec says first sub-record is always CommonObjectDataSubRecord,
  51. // and last is
  52. // always EndSubRecord. OOO spec does not mention ObjRecord(0x005D).
  53. // Existing POI test data seems to violate that rule. Some test data
  54. // seems to contain
  55. // garbage, and a crash is only averted by stopping at what looks like
  56. // the 'EndSubRecord'
  57. // Check if this can be continued, if so then the
  58. // following wont work properly
  59. byte[] subRecordData = in.readRemainder();
  60. if (LittleEndian.getUShort(subRecordData, 0) != CommonObjectDataSubRecord.sid) {
  61. // seems to occur in just one junit on "OddStyleRecord.xls" (file created by CrystalReports)
  62. // Excel tolerates the funny ObjRecord, and replaces it with a corrected version
  63. // The exact logic/reasoning is not yet understood
  64. _uninterpretedData = subRecordData;
  65. subrecords = null;
  66. return;
  67. }
  68. //YK: files produced by OO violate the condition below
  69. /*
  70. if (subRecordData.length % 2 != 0) {
  71. String msg = "Unexpected length of subRecordData : " + HexDump.toHex(subRecordData);
  72. throw new RecordFormatException(msg);
  73. }
  74. */
  75. subrecords = new ArrayList<SubRecord>();
  76. ByteArrayInputStream bais = new ByteArrayInputStream(subRecordData);
  77. LittleEndianInputStream subRecStream = new LittleEndianInputStream(bais);
  78. CommonObjectDataSubRecord cmo = (CommonObjectDataSubRecord)SubRecord.createSubRecord(subRecStream, 0);
  79. subrecords.add(cmo);
  80. while (true) {
  81. SubRecord subRecord = SubRecord.createSubRecord(subRecStream, cmo.getObjectType());
  82. subrecords.add(subRecord);
  83. if (subRecord.isTerminating()) {
  84. break;
  85. }
  86. }
  87. int nRemainingBytes = bais.available();
  88. if (nRemainingBytes > 0) {
  89. // At present (Oct-2008), most unit test samples have (subRecordData.length % 2 == 0)
  90. _isPaddedToQuadByteMultiple = subRecordData.length % MAX_PAD_ALIGNMENT == 0;
  91. if (nRemainingBytes >= (_isPaddedToQuadByteMultiple ? MAX_PAD_ALIGNMENT : NORMAL_PAD_ALIGNMENT)) {
  92. if (!canPaddingBeDiscarded(subRecordData, nRemainingBytes)) {
  93. String msg = "Leftover " + nRemainingBytes
  94. + " bytes in subrecord data " + HexDump.toHex(subRecordData);
  95. throw new RecordFormatException(msg);
  96. }
  97. _isPaddedToQuadByteMultiple = false;
  98. }
  99. } else {
  100. _isPaddedToQuadByteMultiple = false;
  101. }
  102. _uninterpretedData = null;
  103. }
  104. /**
  105. * Some XLS files have ObjRecords with nearly 8Kb of excessive padding. These were probably
  106. * written by a version of POI (around 3.1) which incorrectly interpreted the second short of
  107. * the ftLbs subrecord (0x1FEE) as a length, and read that many bytes as padding (other bugs
  108. * helped allow this to occur).
  109. *
  110. * Excel reads files with this excessive padding OK, truncating the over-sized ObjRecord back
  111. * to the its proper size. POI does the same.
  112. */
  113. private static boolean canPaddingBeDiscarded(byte[] data, int nRemainingBytes) {
  114. // make sure none of the padding looks important
  115. for(int i=data.length-nRemainingBytes; i<data.length; i++) {
  116. if (data[i] != 0x00) {
  117. return false;
  118. }
  119. }
  120. return true;
  121. }
  122. public String toString() {
  123. StringBuffer sb = new StringBuffer();
  124. sb.append("[OBJ]\n");
  125. for (int i = 0; i < subrecords.size(); i++) {
  126. SubRecord record = subrecords.get(i);
  127. sb.append("SUBRECORD: ").append(record.toString());
  128. }
  129. sb.append("[/OBJ]\n");
  130. return sb.toString();
  131. }
  132. public int getRecordSize() {
  133. if (_uninterpretedData != null) {
  134. return _uninterpretedData.length + 4;
  135. }
  136. int size = 0;
  137. for (int i=subrecords.size()-1; i>=0; i--) {
  138. SubRecord record = subrecords.get(i);
  139. size += record.getDataSize()+4;
  140. }
  141. if (_isPaddedToQuadByteMultiple) {
  142. while (size % MAX_PAD_ALIGNMENT != 0) {
  143. size++;
  144. }
  145. } else {
  146. while (size % NORMAL_PAD_ALIGNMENT != 0) {
  147. size++;
  148. }
  149. }
  150. return size + 4;
  151. }
  152. public int serialize(int offset, byte[] data) {
  153. int recSize = getRecordSize();
  154. int dataSize = recSize - 4;
  155. LittleEndianByteArrayOutputStream out = new LittleEndianByteArrayOutputStream(data, offset, recSize);
  156. out.writeShort(sid);
  157. out.writeShort(dataSize);
  158. if (_uninterpretedData == null) {
  159. for (int i = 0; i < subrecords.size(); i++) {
  160. SubRecord record = subrecords.get(i);
  161. record.serialize(out);
  162. }
  163. int expectedEndIx = offset+dataSize;
  164. // padding
  165. while (out.getWriteIndex() < expectedEndIx) {
  166. out.writeByte(0);
  167. }
  168. } else {
  169. out.write(_uninterpretedData);
  170. }
  171. return recSize;
  172. }
  173. public short getSid() {
  174. return sid;
  175. }
  176. public List<SubRecord> getSubRecords() {
  177. return subrecords;
  178. }
  179. public void clearSubRecords() {
  180. subrecords.clear();
  181. }
  182. public void addSubRecord(int index, SubRecord element) {
  183. subrecords.add(index, element);
  184. }
  185. public boolean addSubRecord(SubRecord o) {
  186. return subrecords.add(o);
  187. }
  188. public Object clone() {
  189. ObjRecord rec = new ObjRecord();
  190. for (int i = 0; i < subrecords.size(); i++) {
  191. SubRecord record = subrecords.get(i);
  192. rec.addSubRecord((SubRecord) record.clone());
  193. }
  194. return rec;
  195. }
  196. }