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.

Ole10Native.java 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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.poifs.filesystem;
  16. import org.apache.poi.util.*;
  17. import java.io.ByteArrayOutputStream;
  18. import java.io.FileNotFoundException;
  19. import java.io.IOException;
  20. import java.util.Arrays;
  21. /**
  22. * Represents an Ole10Native record which is wrapped around certain binary
  23. * files being embedded in OLE2 documents.
  24. *
  25. * @author Rainer Schwarze
  26. */
  27. public class Ole10Native {
  28. // (the fields as they appear in the raw record:)
  29. private final int totalSize; // 4 bytes, total size of record not including this field
  30. private short flags1; // 2 bytes, unknown, mostly [02 00]
  31. private final String label; // ASCIIZ, stored in this field without the terminating zero
  32. private final String fileName; // ASCIIZ, stored in this field without the terminating zero
  33. private short flags2; // 2 bytes, unknown, mostly [00 00]
  34. // private byte unknown1Length; // 1 byte, specifying the length of the following byte array (unknown1)
  35. private byte[] unknown1; // see below
  36. private byte[] unknown2; // 3 bytes, unknown, mostly [00 00 00]
  37. private final String command; // ASCIIZ, stored in this field without the terminating zero
  38. private final int dataSize; // 4 bytes (if space), size of following buffer
  39. private final byte[] dataBuffer; // varying size, the actual native data
  40. private short flags3; // some final flags? or zero terminators?, sometimes not there
  41. public static final String OLE10_NATIVE = "\u0001Ole10Native";
  42. /**
  43. * Creates an instance of this class from an embedded OLE Object. The OLE Object is expected
  44. * to include a stream "{01}Ole10Native" which contains the actual
  45. * data relevant for this class.
  46. *
  47. * @param poifs POI Filesystem object
  48. * @return Returns an instance of this class
  49. * @throws IOException on IO error
  50. * @throws Ole10NativeException on invalid or unexcepted data format
  51. */
  52. public static Ole10Native createFromEmbeddedOleObject(POIFSFileSystem poifs) throws IOException, Ole10NativeException {
  53. boolean plain = false;
  54. try {
  55. poifs.getRoot().getEntry("\u0001Ole10ItemName");
  56. plain = true;
  57. } catch (FileNotFoundException ex) {
  58. plain = false;
  59. }
  60. DocumentInputStream dis = poifs.createDocumentInputStream(OLE10_NATIVE);
  61. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  62. IOUtils.copy(dis, bos);
  63. byte[] data = bos.toByteArray();
  64. return new Ole10Native(data, 0, plain);
  65. }
  66. /**
  67. * Creates an instance and fills the fields based on the data in the given buffer.
  68. *
  69. * @param data The buffer containing the Ole10Native record
  70. * @param offset The start offset of the record in the buffer
  71. * @throws Ole10NativeException on invalid or unexcepted data format
  72. */
  73. public Ole10Native(byte[] data, int offset) throws Ole10NativeException {
  74. this(data, offset, false);
  75. }
  76. /**
  77. * Creates an instance and fills the fields based on the data in the given buffer.
  78. *
  79. * @param data The buffer containing the Ole10Native record
  80. * @param offset The start offset of the record in the buffer
  81. * @param plain Specified 'plain' format without filename
  82. * @throws Ole10NativeException on invalid or unexcepted data format
  83. */
  84. public Ole10Native(byte[] data, int offset, boolean plain) throws Ole10NativeException {
  85. int ofs = offset; // current offset, initialized to start
  86. if (data.length<offset+2) {
  87. throw new Ole10NativeException("data is too small");
  88. }
  89. totalSize = LittleEndian.getInt(data, ofs);
  90. ofs += LittleEndianConsts.INT_SIZE;
  91. if (plain) {
  92. dataBuffer = new byte[totalSize-4];
  93. System.arraycopy(data, 4, dataBuffer, 0, dataBuffer.length);
  94. dataSize = totalSize - 4;
  95. byte[] oleLabel = new byte[8];
  96. System.arraycopy(dataBuffer, 0, oleLabel, 0, Math.min(dataBuffer.length, 8));
  97. label = "ole-"+ HexDump.toHex(oleLabel);
  98. fileName = label;
  99. command = label;
  100. } else {
  101. flags1 = LittleEndian.getShort(data, ofs);
  102. ofs += LittleEndianConsts.SHORT_SIZE;
  103. int len = getStringLength(data, ofs);
  104. label = StringUtil.getFromCompressedUnicode(data, ofs, len - 1);
  105. ofs += len;
  106. len = getStringLength(data, ofs);
  107. fileName = StringUtil.getFromCompressedUnicode(data, ofs, len - 1);
  108. ofs += len;
  109. flags2 = LittleEndian.getShort(data, ofs);
  110. ofs += LittleEndianConsts.SHORT_SIZE;
  111. len = LittleEndian.getUnsignedByte(data, ofs);
  112. unknown1 = new byte[len];
  113. ofs += len;
  114. len = 3;
  115. unknown2 = new byte[len];
  116. ofs += len;
  117. len = getStringLength(data, ofs);
  118. command = StringUtil.getFromCompressedUnicode(data, ofs, len - 1);
  119. ofs += len;
  120. if (totalSize + LittleEndianConsts.INT_SIZE - ofs > LittleEndianConsts.INT_SIZE) {
  121. dataSize = LittleEndian.getInt(data, ofs);
  122. ofs += LittleEndianConsts.INT_SIZE;
  123. if (dataSize > totalSize || dataSize<0) {
  124. throw new Ole10NativeException("Invalid Ole10Native");
  125. }
  126. dataBuffer = new byte[dataSize];
  127. System.arraycopy(data, ofs, dataBuffer, 0, dataSize);
  128. ofs += dataSize;
  129. if (unknown1.length > 0) {
  130. flags3 = LittleEndian.getShort(data, ofs);
  131. ofs += LittleEndianConsts.SHORT_SIZE;
  132. } else {
  133. flags3 = 0;
  134. }
  135. } else {
  136. throw new Ole10NativeException("Invalid Ole10Native");
  137. }
  138. }
  139. }
  140. /*
  141. * Helper - determine length of zero terminated string (ASCIIZ).
  142. */
  143. private static int getStringLength(byte[] data, int ofs) {
  144. int len = 0;
  145. while (len+ofs<data.length && data[ofs + len] != 0) {
  146. len++;
  147. }
  148. len++;
  149. return len;
  150. }
  151. /**
  152. * Returns the value of the totalSize field - the total length of the structure
  153. * is totalSize + 4 (value of this field + size of this field).
  154. *
  155. * @return the totalSize
  156. */
  157. public int getTotalSize() {
  158. return totalSize;
  159. }
  160. /**
  161. * Returns flags1 - currently unknown - usually 0x0002.
  162. *
  163. * @return the flags1
  164. */
  165. public short getFlags1() {
  166. return flags1;
  167. }
  168. /**
  169. * Returns the label field - usually the name of the file (without directory) but
  170. * probably may be any name specified during packaging/embedding the data.
  171. *
  172. * @return the label
  173. */
  174. public String getLabel() {
  175. return label;
  176. }
  177. /**
  178. * Returns the fileName field - usually the name of the file being embedded
  179. * including the full path.
  180. *
  181. * @return the fileName
  182. */
  183. public String getFileName() {
  184. return fileName;
  185. }
  186. /**
  187. * Returns flags2 - currently unknown - mostly 0x0000.
  188. *
  189. * @return the flags2
  190. */
  191. public short getFlags2() {
  192. return flags2;
  193. }
  194. /**
  195. * Returns unknown1 field - currently unknown.
  196. *
  197. * @return the unknown1
  198. */
  199. public byte[] getUnknown1() {
  200. return unknown1;
  201. }
  202. /**
  203. * Returns the unknown2 field - currently being a byte[3] - mostly {0, 0, 0}.
  204. *
  205. * @return the unknown2
  206. */
  207. public byte[] getUnknown2() {
  208. return unknown2;
  209. }
  210. /**
  211. * Returns the command field - usually the name of the file being embedded
  212. * including the full path, may be a command specified during embedding the file.
  213. *
  214. * @return the command
  215. */
  216. public String getCommand() {
  217. return command;
  218. }
  219. /**
  220. * Returns the size of the embedded file. If the size is 0 (zero), no data has been
  221. * embedded. To be sure, that no data has been embedded, check whether
  222. * {@link #getDataBuffer()} returns <code>null</code>.
  223. *
  224. * @return the dataSize
  225. */
  226. public int getDataSize() {
  227. return dataSize;
  228. }
  229. /**
  230. * Returns the buffer containing the embedded file's data, or <code>null</code>
  231. * if no data was embedded. Note that an embedding may provide information about
  232. * the data, but the actual data is not included. (So label, filename etc. are
  233. * available, but this method returns <code>null</code>.)
  234. *
  235. * @return the dataBuffer
  236. */
  237. public byte[] getDataBuffer() {
  238. return dataBuffer;
  239. }
  240. /**
  241. * Returns the flags3 - currently unknown.
  242. *
  243. * @return the flags3
  244. */
  245. public short getFlags3() {
  246. return flags3;
  247. }
  248. }