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.

EscherMetafileBlip.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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 java.awt.Dimension;
  17. import java.awt.Rectangle;
  18. import java.io.ByteArrayInputStream;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.IOException;
  21. import java.util.Collections;
  22. import java.util.LinkedHashMap;
  23. import java.util.Map;
  24. import java.util.function.Supplier;
  25. import java.util.zip.DeflaterOutputStream;
  26. import java.util.zip.InflaterInputStream;
  27. import org.apache.logging.log4j.LogManager;
  28. import org.apache.logging.log4j.Logger;
  29. import org.apache.poi.hssf.usermodel.HSSFPictureData;
  30. import org.apache.poi.util.IOUtils;
  31. import org.apache.poi.util.LittleEndian;
  32. import static org.apache.logging.log4j.util.Unbox.box;
  33. public final class EscherMetafileBlip extends EscherBlipRecord {
  34. private static final Logger LOGGER = LogManager.getLogger(EscherMetafileBlip.class);
  35. //arbitrarily selected; may need to increase
  36. private static final int MAX_RECORD_LENGTH = 100_000_000;
  37. public static final short RECORD_ID_EMF = EscherRecordTypes.BLIP_EMF.typeID;
  38. public static final short RECORD_ID_WMF = EscherRecordTypes.BLIP_WMF.typeID;
  39. public static final short RECORD_ID_PICT = EscherRecordTypes.BLIP_PICT.typeID;
  40. private static final int HEADER_SIZE = 8;
  41. private final byte[] field_1_UID = new byte[16];
  42. /**
  43. * The primary UID is only saved to disk if (blip_instance ^ blip_signature == 1)
  44. */
  45. private final byte[] field_2_UID = new byte[16];
  46. private int field_2_cb;
  47. private int field_3_rcBounds_x1;
  48. private int field_3_rcBounds_y1;
  49. private int field_3_rcBounds_x2;
  50. private int field_3_rcBounds_y2;
  51. private int field_4_ptSize_w;
  52. private int field_4_ptSize_h;
  53. private int field_5_cbSave;
  54. private byte field_6_fCompression;
  55. private byte field_7_fFilter;
  56. private byte[] raw_pictureData;
  57. private byte[] remainingData;
  58. public EscherMetafileBlip() {}
  59. public EscherMetafileBlip(EscherMetafileBlip other) {
  60. super(other);
  61. System.arraycopy(other.field_1_UID, 0, field_1_UID, 0, field_1_UID.length);
  62. System.arraycopy(other.field_2_UID, 0, field_2_UID, 0, field_2_UID.length);
  63. field_2_cb = other.field_2_cb;
  64. field_3_rcBounds_x1 = other.field_3_rcBounds_x1;
  65. field_3_rcBounds_y1 = other.field_3_rcBounds_y1;
  66. field_3_rcBounds_x2 = other.field_3_rcBounds_x2;
  67. field_3_rcBounds_y2 = other.field_3_rcBounds_y2;
  68. field_4_ptSize_h = other.field_4_ptSize_h;
  69. field_4_ptSize_w = other.field_4_ptSize_w;
  70. field_5_cbSave = other.field_5_cbSave;
  71. field_6_fCompression = other.field_6_fCompression;
  72. field_7_fFilter = other.field_7_fFilter;
  73. raw_pictureData = (other.raw_pictureData == null) ? null : other.raw_pictureData.clone();
  74. remainingData = (other.remainingData == null) ? null : other.remainingData.clone();
  75. }
  76. @Override
  77. public int fillFields(byte[] data, int offset, EscherRecordFactory recordFactory) {
  78. int bytesAfterHeader = readHeader( data, offset );
  79. int pos = offset + HEADER_SIZE;
  80. System.arraycopy( data, pos, field_1_UID, 0, 16 ); pos += 16;
  81. if((getOptions() ^ getSignature()) == 0x10){
  82. System.arraycopy( data, pos, field_2_UID, 0, 16 ); pos += 16;
  83. }
  84. field_2_cb = LittleEndian.getInt( data, pos ); pos += 4;
  85. field_3_rcBounds_x1 = LittleEndian.getInt( data, pos ); pos += 4;
  86. field_3_rcBounds_y1 = LittleEndian.getInt( data, pos ); pos += 4;
  87. field_3_rcBounds_x2 = LittleEndian.getInt( data, pos ); pos += 4;
  88. field_3_rcBounds_y2 = LittleEndian.getInt( data, pos ); pos += 4;
  89. field_4_ptSize_w = LittleEndian.getInt( data, pos ); pos += 4;
  90. field_4_ptSize_h = LittleEndian.getInt( data, pos ); pos += 4;
  91. field_5_cbSave = LittleEndian.getInt( data, pos ); pos += 4;
  92. field_6_fCompression = data[pos]; pos++;
  93. field_7_fFilter = data[pos]; pos++;
  94. raw_pictureData = IOUtils.safelyClone(data, pos, field_5_cbSave, MAX_RECORD_LENGTH);
  95. pos += field_5_cbSave;
  96. // 0 means DEFLATE compression
  97. // 0xFE means no compression
  98. if (field_6_fCompression == 0) {
  99. super.setPictureData(inflatePictureData(raw_pictureData));
  100. } else {
  101. super.setPictureData(raw_pictureData);
  102. }
  103. int remaining = bytesAfterHeader - pos + offset + HEADER_SIZE;
  104. if(remaining > 0) {
  105. remainingData = IOUtils.safelyClone(data, pos, remaining, MAX_RECORD_LENGTH);
  106. }
  107. return bytesAfterHeader + HEADER_SIZE;
  108. }
  109. @Override
  110. public int serialize(int offset, byte[] data, EscherSerializationListener listener) {
  111. listener.beforeRecordSerialize(offset, getRecordId(), this);
  112. int pos = offset;
  113. LittleEndian.putShort( data, pos, getOptions() ); pos += 2;
  114. LittleEndian.putShort( data, pos, getRecordId() ); pos += 2;
  115. LittleEndian.putInt( data, pos, getRecordSize() - HEADER_SIZE ); pos += 4;
  116. System.arraycopy( field_1_UID, 0, data, pos, field_1_UID.length ); pos += field_1_UID.length;
  117. if ((getOptions() ^ getSignature()) == 0x10) {
  118. System.arraycopy( field_2_UID, 0, data, pos, field_2_UID.length ); pos += field_2_UID.length;
  119. }
  120. LittleEndian.putInt( data, pos, field_2_cb ); pos += 4;
  121. LittleEndian.putInt( data, pos, field_3_rcBounds_x1 ); pos += 4;
  122. LittleEndian.putInt( data, pos, field_3_rcBounds_y1 ); pos += 4;
  123. LittleEndian.putInt( data, pos, field_3_rcBounds_x2 ); pos += 4;
  124. LittleEndian.putInt( data, pos, field_3_rcBounds_y2 ); pos += 4;
  125. LittleEndian.putInt( data, pos, field_4_ptSize_w ); pos += 4;
  126. LittleEndian.putInt( data, pos, field_4_ptSize_h ); pos += 4;
  127. LittleEndian.putInt( data, pos, field_5_cbSave ); pos += 4;
  128. data[pos] = field_6_fCompression; pos++;
  129. data[pos] = field_7_fFilter; pos++;
  130. System.arraycopy( raw_pictureData, 0, data, pos, raw_pictureData.length ); pos += raw_pictureData.length;
  131. if(remainingData != null) {
  132. System.arraycopy( remainingData, 0, data, pos, remainingData.length ); pos += remainingData.length;
  133. }
  134. listener.afterRecordSerialize(offset + getRecordSize(), getRecordId(), getRecordSize(), this);
  135. return getRecordSize();
  136. }
  137. /**
  138. * Decompresses the provided data, returning the inflated result.
  139. *
  140. * @param data the deflated picture data.
  141. * @return the inflated picture data.
  142. */
  143. private static byte[] inflatePictureData(byte[] data) {
  144. try {
  145. InflaterInputStream in = new InflaterInputStream(
  146. new ByteArrayInputStream( data ) );
  147. ByteArrayOutputStream out = new ByteArrayOutputStream();
  148. byte[] buf = new byte[4096];
  149. int readBytes;
  150. while ((readBytes = in.read(buf)) > 0) {
  151. out.write(buf, 0, readBytes);
  152. }
  153. return out.toByteArray();
  154. } catch (IOException e) {
  155. LOGGER.atWarn().withThrowable(e).log("Possibly corrupt compression or non-compressed data");
  156. return data;
  157. }
  158. }
  159. @Override
  160. public int getRecordSize() {
  161. int size = 8 + 50 + raw_pictureData.length;
  162. if(remainingData != null) {
  163. size += remainingData.length;
  164. }
  165. if((getOptions() ^ getSignature()) == 0x10){
  166. size += field_2_UID.length;
  167. }
  168. return size;
  169. }
  170. /**
  171. * Gets the first MD4, that specifies the unique identifier of the
  172. * uncompressed blip data
  173. *
  174. * @return the first MD4
  175. */
  176. public byte[] getUID() {
  177. return field_1_UID;
  178. }
  179. /**
  180. * Sets the first MD4, that specifies the unique identifier of the
  181. * uncompressed blip data
  182. *
  183. * @param uid the first MD4
  184. */
  185. public void setUID(byte[] uid) {
  186. if (uid == null || uid.length != 16) {
  187. throw new IllegalArgumentException("uid must be byte[16]");
  188. }
  189. System.arraycopy(uid, 0, field_1_UID, 0, field_1_UID.length);
  190. }
  191. /**
  192. * Gets the second MD4, that specifies the unique identifier of the
  193. * uncompressed blip data
  194. *
  195. * @return the second MD4
  196. */
  197. public byte[] getPrimaryUID() {
  198. return field_2_UID;
  199. }
  200. /**
  201. * Sets the second MD4, that specifies the unique identifier of the
  202. * uncompressed blip data
  203. *
  204. * @param primaryUID the second MD4
  205. */
  206. public void setPrimaryUID(byte[] primaryUID) {
  207. if (primaryUID == null || primaryUID.length != 16) {
  208. throw new IllegalArgumentException("primaryUID must be byte[16]");
  209. }
  210. System.arraycopy(primaryUID, 0, field_2_UID, 0, field_2_UID.length);
  211. }
  212. /**
  213. * Gets the uncompressed size (in bytes)
  214. *
  215. * @return the uncompressed size
  216. */
  217. public int getUncompressedSize() {
  218. return field_2_cb;
  219. }
  220. /**
  221. * Sets the uncompressed size (in bytes)
  222. *
  223. * @param uncompressedSize the uncompressed size
  224. */
  225. public void setUncompressedSize(int uncompressedSize) {
  226. field_2_cb = uncompressedSize;
  227. }
  228. /**
  229. * Get the clipping region of the metafile
  230. *
  231. * @return the clipping region
  232. */
  233. public Rectangle getBounds() {
  234. return new Rectangle(field_3_rcBounds_x1,
  235. field_3_rcBounds_y1,
  236. field_3_rcBounds_x2 - field_3_rcBounds_x1,
  237. field_3_rcBounds_y2 - field_3_rcBounds_y1);
  238. }
  239. /**
  240. * Sets the clipping region
  241. *
  242. * @param bounds the clipping region
  243. */
  244. public void setBounds(Rectangle bounds) {
  245. field_3_rcBounds_x1 = bounds.x;
  246. field_3_rcBounds_y1 = bounds.y;
  247. field_3_rcBounds_x2 = bounds.x + bounds.width;
  248. field_3_rcBounds_y2 = bounds.y + bounds.height;
  249. }
  250. /**
  251. * Gets the dimensions of the metafile
  252. *
  253. * @return the dimensions of the metafile
  254. */
  255. public Dimension getSizeEMU() {
  256. return new Dimension(field_4_ptSize_w, field_4_ptSize_h);
  257. }
  258. /**
  259. * Gets the dimensions of the metafile
  260. *
  261. * @param sizeEMU the dimensions of the metafile
  262. */
  263. public void setSizeEMU(Dimension sizeEMU) {
  264. field_4_ptSize_w = sizeEMU.width;
  265. field_4_ptSize_h = sizeEMU.height;
  266. }
  267. /**
  268. * Gets the compressed size of the metafile (in bytes)
  269. *
  270. * @return the compressed size
  271. */
  272. public int getCompressedSize() {
  273. return field_5_cbSave;
  274. }
  275. /**
  276. * Sets the compressed size of the metafile (in bytes)
  277. *
  278. * @param compressedSize the compressed size
  279. */
  280. public void setCompressedSize(int compressedSize) {
  281. field_5_cbSave = compressedSize;
  282. }
  283. /**
  284. * Gets the compression of the metafile
  285. *
  286. * @return true, if the metafile is compressed
  287. */
  288. public boolean isCompressed() {
  289. return (field_6_fCompression == 0);
  290. }
  291. /**
  292. * Sets the compression of the metafile
  293. *
  294. * @param compressed the compression state, true if it's compressed
  295. */
  296. public void setCompressed(boolean compressed) {
  297. field_6_fCompression = compressed ? 0 : (byte)0xFE;
  298. }
  299. /**
  300. * Gets the filter byte - this is usually 0xFE
  301. *
  302. * @return the filter byte
  303. */
  304. public byte getFilter() {
  305. return field_7_fFilter;
  306. }
  307. /**
  308. * Sets the filter byte - this is usually 0xFE
  309. *
  310. * @param filter the filter byte
  311. */
  312. public void setFilter(byte filter) {
  313. field_7_fFilter = filter;
  314. }
  315. /**
  316. * Returns any remaining bytes
  317. *
  318. * @return any remaining bytes
  319. */
  320. public byte[] getRemainingData() {
  321. return remainingData;
  322. }
  323. /**
  324. * Return the blip signature
  325. *
  326. * @return the blip signature
  327. */
  328. public short getSignature() {
  329. switch (EscherRecordTypes.forTypeID(getRecordId())) {
  330. case BLIP_EMF: return HSSFPictureData.MSOBI_EMF;
  331. case BLIP_WMF: return HSSFPictureData.MSOBI_WMF;
  332. case BLIP_PICT: return HSSFPictureData.MSOBI_PICT;
  333. }
  334. LOGGER.atWarn().log("Unknown metafile: {}", box(getRecordId()));
  335. return 0;
  336. }
  337. @Override
  338. public void setPictureData(byte[] pictureData) {
  339. super.setPictureData(pictureData);
  340. setUncompressedSize(pictureData.length);
  341. // info of chicago project:
  342. // "... LZ compression algorithm in the format used by GNU Zip deflate/inflate with a 32k window ..."
  343. // not sure what to do, when lookup tables exceed 32k ...
  344. try {
  345. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  346. DeflaterOutputStream dos = new DeflaterOutputStream(bos);
  347. dos.write(pictureData);
  348. dos.close();
  349. raw_pictureData = bos.toByteArray();
  350. } catch (IOException e) {
  351. throw new RuntimeException("Can't compress metafile picture data", e);
  352. }
  353. setCompressedSize(raw_pictureData.length);
  354. setCompressed(true);
  355. }
  356. @Override
  357. public Map<String, Supplier<?>> getGenericProperties() {
  358. final Map<String, Supplier<?>> m = new LinkedHashMap<>(super.getGenericProperties());
  359. m.put("uid", this::getUID);
  360. m.put("uncompressedSize", this::getUncompressedSize);
  361. m.put("bounds", this::getBounds);
  362. m.put("sizeInEMU", this::getSizeEMU);
  363. m.put("compressedSize", this::getCompressedSize);
  364. m.put("isCompressed", this::isCompressed);
  365. m.put("filter", this::getFilter);
  366. return Collections.unmodifiableMap(m);
  367. }
  368. @Override
  369. public EscherMetafileBlip copy() {
  370. return new EscherMetafileBlip(this);
  371. }
  372. }