PageRenderTime 61ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/src/org/apache/poi/ddf/EscherBSERecord.java

https://github.com/minstrelsy/SimpleAndroidDocView
Java | 371 lines | 235 code | 46 blank | 90 comment | 21 complexity | 55468369a48912dbce2e789206e3986d MD5 | raw file
Possible License(s): Apache-2.0
  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.ddf;
  16. import org.apache.poi.util.HexDump;
  17. import org.apache.poi.util.LittleEndian;
  18. /**
  19. * The BSE record is related closely to the <code>EscherBlipRecord</code> and stores
  20. * extra information about the blip. A blip record is actually stored inside
  21. * the BSE record even though the BSE record isn't actually a container record.
  22. *
  23. * @author Glen Stampoultzis
  24. * @see EscherBlipRecord
  25. */
  26. public final class EscherBSERecord extends EscherRecord {
  27. public static final short RECORD_ID = (short) 0xF007;
  28. public static final String RECORD_DESCRIPTION = "MsofbtBSE";
  29. public static final byte BT_ERROR = 0;
  30. public static final byte BT_UNKNOWN = 1;
  31. public static final byte BT_EMF = 2;
  32. public static final byte BT_WMF = 3;
  33. public static final byte BT_PICT = 4;
  34. public static final byte BT_JPEG = 5;
  35. public static final byte BT_PNG = 6;
  36. public static final byte BT_DIB = 7;
  37. private byte field_1_blipTypeWin32;
  38. private byte field_2_blipTypeMacOS;
  39. private byte[] field_3_uid; // 16 bytes
  40. private short field_4_tag;
  41. private int field_5_size;
  42. private int field_6_ref;
  43. private int field_7_offset;
  44. private byte field_8_usage;
  45. private byte field_9_name;
  46. private byte field_10_unused2;
  47. private byte field_11_unused3;
  48. private EscherBlipRecord field_12_blipRecord;
  49. private byte[] _remainingData;
  50. public int fillFields(byte[] data, int offset, EscherRecordFactory recordFactory) {
  51. int bytesRemaining = readHeader( data, offset );
  52. int pos = offset + 8;
  53. field_1_blipTypeWin32 = data[pos];
  54. field_2_blipTypeMacOS = data[pos + 1];
  55. System.arraycopy( data, pos + 2, field_3_uid = new byte[16], 0, 16 );
  56. field_4_tag = LittleEndian.getShort( data, pos + 18 );
  57. field_5_size = LittleEndian.getInt( data, pos + 20 );
  58. field_6_ref = LittleEndian.getInt( data, pos + 24 );
  59. field_7_offset = LittleEndian.getInt( data, pos + 28 );
  60. field_8_usage = data[pos + 32];
  61. field_9_name = data[pos + 33];
  62. field_10_unused2 = data[pos + 34];
  63. field_11_unused3 = data[pos + 35];
  64. bytesRemaining -= 36;
  65. int bytesRead = 0;
  66. if (bytesRemaining > 0) {
  67. // Some older escher formats skip this last record
  68. field_12_blipRecord = (EscherBlipRecord) recordFactory.createRecord( data, pos + 36 );
  69. bytesRead = field_12_blipRecord.fillFields( data, pos + 36, recordFactory );
  70. }
  71. pos += 36 + bytesRead;
  72. bytesRemaining -= bytesRead;
  73. _remainingData = new byte[bytesRemaining];
  74. System.arraycopy( data, pos, _remainingData, 0, bytesRemaining );
  75. return bytesRemaining + 8 + 36 + (field_12_blipRecord == null ? 0 : field_12_blipRecord.getRecordSize()) ;
  76. }
  77. public int serialize(int offset, byte[] data, EscherSerializationListener listener) {
  78. listener.beforeRecordSerialize( offset, getRecordId(), this );
  79. if (_remainingData == null)
  80. _remainingData = new byte[0];
  81. LittleEndian.putShort( data, offset, getOptions() );
  82. LittleEndian.putShort( data, offset + 2, getRecordId() );
  83. if (_remainingData == null) _remainingData = new byte[0];
  84. int blipSize = field_12_blipRecord == null ? 0 : field_12_blipRecord.getRecordSize();
  85. int remainingBytes = _remainingData.length + 36 + blipSize;
  86. LittleEndian.putInt( data, offset + 4, remainingBytes );
  87. data[offset + 8] = field_1_blipTypeWin32;
  88. data[offset + 9] = field_2_blipTypeMacOS;
  89. for ( int i = 0; i < 16; i++ )
  90. data[offset + 10 + i] = field_3_uid[i];
  91. LittleEndian.putShort( data, offset + 26, field_4_tag );
  92. LittleEndian.putInt( data, offset + 28, field_5_size );
  93. LittleEndian.putInt( data, offset + 32, field_6_ref );
  94. LittleEndian.putInt( data, offset + 36, field_7_offset );
  95. data[offset + 40] = field_8_usage;
  96. data[offset + 41] = field_9_name;
  97. data[offset + 42] = field_10_unused2;
  98. data[offset + 43] = field_11_unused3;
  99. int bytesWritten = 0;
  100. if (field_12_blipRecord != null)
  101. {
  102. bytesWritten = field_12_blipRecord.serialize( offset + 44, data, new NullEscherSerializationListener() );
  103. }
  104. if (_remainingData == null)
  105. _remainingData = new byte[0];
  106. System.arraycopy( _remainingData, 0, data, offset + 44 + bytesWritten, _remainingData.length );
  107. int pos = offset + 8 + 36 + _remainingData.length + bytesWritten;
  108. listener.afterRecordSerialize(pos, getRecordId(), pos - offset, this);
  109. return pos - offset;
  110. }
  111. public int getRecordSize() {
  112. int field_12_size = 0;
  113. if(field_12_blipRecord != null) {
  114. field_12_size = field_12_blipRecord.getRecordSize();
  115. }
  116. int remaining_size = 0;
  117. if(_remainingData != null) {
  118. remaining_size = _remainingData.length;
  119. }
  120. return 8 + 1 + 1 + 16 + 2 + 4 + 4 + 4 + 1 + 1 +
  121. 1 + 1 + field_12_size + remaining_size;
  122. }
  123. public String getRecordName() {
  124. return "BSE";
  125. }
  126. /**
  127. * The expected blip type under windows (failure to match this blip type will result in
  128. * Excel converting to this format).
  129. */
  130. public byte getBlipTypeWin32() {
  131. return field_1_blipTypeWin32;
  132. }
  133. /**
  134. * Set the expected win32 blip type
  135. */
  136. public void setBlipTypeWin32(byte blipTypeWin32) {
  137. field_1_blipTypeWin32 = blipTypeWin32;
  138. }
  139. /**
  140. * The expected blip type under MacOS (failure to match this blip type will result in
  141. * Excel converting to this format).
  142. */
  143. public byte getBlipTypeMacOS() {
  144. return field_2_blipTypeMacOS;
  145. }
  146. /**
  147. * Set the expected MacOS blip type
  148. */
  149. public void setBlipTypeMacOS(byte blipTypeMacOS) {
  150. field_2_blipTypeMacOS = blipTypeMacOS;
  151. }
  152. /**
  153. * 16 byte MD4 checksum.
  154. */
  155. public byte[] getUid() {
  156. return field_3_uid;
  157. }
  158. /**
  159. * 16 byte MD4 checksum.
  160. */
  161. public void setUid(byte[] uid) {
  162. field_3_uid = uid;
  163. }
  164. /**
  165. * unused
  166. */
  167. public short getTag() {
  168. return field_4_tag;
  169. }
  170. /**
  171. * unused
  172. */
  173. public void setTag(short tag) {
  174. field_4_tag = tag;
  175. }
  176. /**
  177. * Blip size in stream.
  178. */
  179. public int getSize() {
  180. return field_5_size;
  181. }
  182. /**
  183. * Blip size in stream.
  184. */
  185. public void setSize(int size) {
  186. field_5_size = size;
  187. }
  188. /**
  189. * The reference count of this blip.
  190. */
  191. public int getRef() {
  192. return field_6_ref;
  193. }
  194. /**
  195. * The reference count of this blip.
  196. */
  197. public void setRef(int ref) {
  198. field_6_ref = ref;
  199. }
  200. /**
  201. * File offset in the delay stream.
  202. */
  203. public int getOffset() {
  204. return field_7_offset;
  205. }
  206. /**
  207. * File offset in the delay stream.
  208. */
  209. public void setOffset(int offset) {
  210. field_7_offset = offset;
  211. }
  212. /**
  213. * Defines the way this blip is used.
  214. */
  215. public byte getUsage() {
  216. return field_8_usage;
  217. }
  218. /**
  219. * Defines the way this blip is used.
  220. */
  221. public void setUsage(byte usage) {
  222. field_8_usage = usage;
  223. }
  224. /**
  225. * The length in characters of the blip name.
  226. */
  227. public byte getName() {
  228. return field_9_name;
  229. }
  230. /**
  231. * The length in characters of the blip name.
  232. */
  233. public void setName(byte name) {
  234. field_9_name = name;
  235. }
  236. public byte getUnused2() {
  237. return field_10_unused2;
  238. }
  239. public void setUnused2(byte unused2) {
  240. field_10_unused2 = unused2;
  241. }
  242. public byte getUnused3() {
  243. return field_11_unused3;
  244. }
  245. public void setUnused3(byte unused3) {
  246. field_11_unused3 = unused3;
  247. }
  248. public EscherBlipRecord getBlipRecord() {
  249. return field_12_blipRecord;
  250. }
  251. public void setBlipRecord(EscherBlipRecord blipRecord) {
  252. field_12_blipRecord = blipRecord;
  253. }
  254. /**
  255. * Any remaining data in this record.
  256. */
  257. public byte[] getRemainingData() {
  258. return _remainingData;
  259. }
  260. /**
  261. * Any remaining data in this record.
  262. */
  263. public void setRemainingData(byte[] remainingData) {
  264. _remainingData = remainingData;
  265. }
  266. public String toString() {
  267. String extraData = _remainingData == null ? null : HexDump.toHex(_remainingData, 32);
  268. return getClass().getName() + ":" + '\n' +
  269. " RecordId: 0x" + HexDump.toHex( RECORD_ID ) + '\n' +
  270. " Version: 0x" + HexDump.toHex( getVersion() ) + '\n' +
  271. " Instance: 0x" + HexDump.toHex( getInstance() ) + '\n' +
  272. " BlipTypeWin32: " + field_1_blipTypeWin32 + '\n' +
  273. " BlipTypeMacOS: " + field_2_blipTypeMacOS + '\n' +
  274. " SUID: " + (field_3_uid == null ? "" : HexDump.toHex(field_3_uid)) + '\n' +
  275. " Tag: " + field_4_tag + '\n' +
  276. " Size: " + field_5_size + '\n' +
  277. " Ref: " + field_6_ref + '\n' +
  278. " Offset: " + field_7_offset + '\n' +
  279. " Usage: " + field_8_usage + '\n' +
  280. " Name: " + field_9_name + '\n' +
  281. " Unused2: " + field_10_unused2 + '\n' +
  282. " Unused3: " + field_11_unused3 + '\n' +
  283. " blipRecord: " + field_12_blipRecord + '\n' +
  284. " Extra Data:" + '\n' + extraData;
  285. }
  286. @Override
  287. public String toXml(String tab) {
  288. StringBuilder builder = new StringBuilder();
  289. builder.append(tab).append(formatXmlRecordHeader(getClass().getSimpleName(), HexDump.toHex(getRecordId()), HexDump.toHex(getVersion()), HexDump.toHex(getInstance())))
  290. .append(tab).append("\t").append("<BlipTypeWin32>").append(field_1_blipTypeWin32).append("</BlipTypeWin32>\n")
  291. .append(tab).append("\t").append("<BlipTypeMacOS>").append(field_2_blipTypeMacOS).append("</BlipTypeMacOS>\n")
  292. .append(tab).append("\t").append("<SUID>").append(field_3_uid == null ? "" : HexDump.toHex(field_3_uid)).append("</SUID>\n")
  293. .append(tab).append("\t").append("<Tag>").append(field_4_tag).append("</Tag>\n")
  294. .append(tab).append("\t").append("<Size>").append(field_5_size).append("</Size>\n")
  295. .append(tab).append("\t").append("<Ref>").append(field_6_ref).append("</Ref>\n")
  296. .append(tab).append("\t").append("<Offset>").append(field_7_offset).append("</Offset>\n")
  297. .append(tab).append("\t").append("<Usage>").append(field_8_usage).append("</Usage>\n")
  298. .append(tab).append("\t").append("<Name>").append(field_9_name).append("</Name>\n")
  299. .append(tab).append("\t").append("<Unused2>").append(field_10_unused2).append("</Unused2>\n")
  300. .append(tab).append("\t").append("<Unused3>").append(field_11_unused3).append("</Unused3>\n");
  301. builder.append(tab).append("</").append(getClass().getSimpleName()).append(">\n");
  302. return builder.toString();
  303. }
  304. /**
  305. * Retrieve the string representation given a blip id.
  306. */
  307. public static String getBlipType(byte b) {
  308. switch (b) {
  309. case BT_ERROR: return " ERROR";
  310. case BT_UNKNOWN: return " UNKNOWN";
  311. case BT_EMF: return " EMF";
  312. case BT_WMF: return " WMF";
  313. case BT_PICT: return " PICT";
  314. case BT_JPEG: return " JPEG";
  315. case BT_PNG: return " PNG";
  316. case BT_DIB: return " DIB";
  317. }
  318. if ( b < 32 ) {
  319. return " NotKnown";
  320. }
  321. return " Client";
  322. }
  323. }