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.

VBAMacroReader.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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.macros;
  16. import static org.apache.poi.util.StringUtil.startsWithIgnoreCase;
  17. import static org.apache.poi.util.StringUtil.endsWithIgnoreCase;
  18. import java.io.ByteArrayInputStream;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.Closeable;
  21. import java.io.File;
  22. import java.io.FileInputStream;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.io.PushbackInputStream;
  26. import java.nio.charset.Charset;
  27. import java.nio.charset.StandardCharsets;
  28. import java.util.HashMap;
  29. import java.util.Map;
  30. import java.util.zip.ZipEntry;
  31. import java.util.zip.ZipInputStream;
  32. import org.apache.poi.poifs.filesystem.DirectoryNode;
  33. import org.apache.poi.poifs.filesystem.DocumentInputStream;
  34. import org.apache.poi.poifs.filesystem.DocumentNode;
  35. import org.apache.poi.poifs.filesystem.Entry;
  36. import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
  37. import org.apache.poi.poifs.filesystem.OfficeXmlFileException;
  38. import org.apache.poi.util.HexDump;
  39. import org.apache.poi.util.IOUtils;
  40. import org.apache.poi.util.RLEDecompressingInputStream;
  41. /**
  42. * Finds all VBA Macros in an office file (OLE2/POIFS and OOXML/OPC),
  43. * and returns them.
  44. *
  45. * @since 3.15-beta2
  46. */
  47. public class VBAMacroReader implements Closeable {
  48. protected static final String VBA_PROJECT_OOXML = "vbaProject.bin";
  49. protected static final String VBA_PROJECT_POIFS = "VBA";
  50. private NPOIFSFileSystem fs;
  51. public VBAMacroReader(InputStream rstream) throws IOException {
  52. PushbackInputStream stream = new PushbackInputStream(rstream, 8);
  53. byte[] header8 = IOUtils.peekFirst8Bytes(stream);
  54. if (NPOIFSFileSystem.hasPOIFSHeader(header8)) {
  55. fs = new NPOIFSFileSystem(stream);
  56. } else {
  57. openOOXML(stream);
  58. }
  59. }
  60. public VBAMacroReader(File file) throws IOException {
  61. try {
  62. this.fs = new NPOIFSFileSystem(file);
  63. } catch (OfficeXmlFileException e) {
  64. openOOXML(new FileInputStream(file));
  65. }
  66. }
  67. public VBAMacroReader(NPOIFSFileSystem fs) {
  68. this.fs = fs;
  69. }
  70. private void openOOXML(InputStream zipFile) throws IOException {
  71. ZipInputStream zis = new ZipInputStream(zipFile);
  72. ZipEntry zipEntry;
  73. while ((zipEntry = zis.getNextEntry()) != null) {
  74. if (endsWithIgnoreCase(zipEntry.getName(), VBA_PROJECT_OOXML)) {
  75. try {
  76. // Make a NPOIFS from the contents, and close the stream
  77. this.fs = new NPOIFSFileSystem(zis);
  78. return;
  79. } catch (IOException e) {
  80. // Tidy up
  81. zis.close();
  82. // Pass on
  83. throw e;
  84. }
  85. }
  86. }
  87. zis.close();
  88. throw new IllegalArgumentException("No VBA project found");
  89. }
  90. public void close() throws IOException {
  91. fs.close();
  92. fs = null;
  93. }
  94. /**
  95. * Reads all macros from all modules of the opened office file.
  96. * @return All the macros and their contents
  97. *
  98. * @since 3.15-beta2
  99. */
  100. public Map<String, String> readMacros() throws IOException {
  101. final ModuleMap modules = new ModuleMap();
  102. findMacros(fs.getRoot(), modules);
  103. Map<String, String> moduleSources = new HashMap<String, String>();
  104. for (Map.Entry<String, Module> entry : modules.entrySet()) {
  105. Module module = entry.getValue();
  106. if (module.buf != null && module.buf.length > 0) { // Skip empty modules
  107. moduleSources.put(entry.getKey(), new String(module.buf, modules.charset));
  108. }
  109. }
  110. return moduleSources;
  111. }
  112. protected static class Module {
  113. Integer offset;
  114. byte[] buf;
  115. void read(InputStream in) throws IOException {
  116. final ByteArrayOutputStream out = new ByteArrayOutputStream();
  117. IOUtils.copy(in, out);
  118. out.close();
  119. buf = out.toByteArray();
  120. }
  121. }
  122. protected static class ModuleMap extends HashMap<String, Module> {
  123. Charset charset = Charset.forName("Cp1252"); // default charset
  124. }
  125. /**
  126. * Recursively traverses directory structure rooted at <tt>dir</tt>.
  127. * For each macro module that is found, the module's name and code are
  128. * added to <tt>modules<tt>.
  129. *
  130. * @param dir
  131. * @param modules
  132. * @throws IOException
  133. * @since 3.15-beta2
  134. */
  135. protected void findMacros(DirectoryNode dir, ModuleMap modules) throws IOException {
  136. if (VBA_PROJECT_POIFS.equalsIgnoreCase(dir.getName())) {
  137. // VBA project directory, process
  138. readMacros(dir, modules);
  139. } else {
  140. // Check children
  141. for (Entry child : dir) {
  142. if (child instanceof DirectoryNode) {
  143. findMacros((DirectoryNode)child, modules);
  144. }
  145. }
  146. }
  147. }
  148. /**
  149. * Read <tt>length</tt> bytes of MBCS (multi-byte character set) characters from the stream
  150. *
  151. * @param stream the inputstream to read from
  152. * @param length number of bytes to read from stream
  153. * @param charset the character set encoding of the bytes in the stream
  154. * @return a java String in the supplied character set
  155. * @throws IOException
  156. */
  157. private static String readString(InputStream stream, int length, Charset charset) throws IOException {
  158. byte[] buffer = new byte[length];
  159. int count = stream.read(buffer);
  160. return new String(buffer, 0, count, charset);
  161. }
  162. /**
  163. * reads module from DIR node in input stream and adds it to the modules map for decompression later
  164. * on the second pass through this function, the module will be decompressed
  165. *
  166. * Side-effects: adds a new module to the module map or sets the buf field on the module
  167. * to the decompressed stream contents (the VBA code for one module)
  168. *
  169. * @param in the run-length encoded input stream to read from
  170. * @param streamName the stream name of the module
  171. * @param modules a map to store the modules
  172. * @throws IOException
  173. */
  174. private static void readModule(RLEDecompressingInputStream in, String streamName, ModuleMap modules) throws IOException {
  175. int moduleOffset = in.readInt();
  176. Module module = modules.get(streamName);
  177. if (module == null) {
  178. // First time we've seen the module. Add it to the ModuleMap and decompress it later
  179. module = new Module();
  180. module.offset = moduleOffset;
  181. modules.put(streamName, module);
  182. // Would adding module.read(in) here be correct?
  183. } else {
  184. // Decompress a previously found module and store the decompressed result into module.buf
  185. InputStream stream = new RLEDecompressingInputStream(
  186. new ByteArrayInputStream(module.buf, moduleOffset, module.buf.length - moduleOffset)
  187. );
  188. module.read(stream);
  189. stream.close();
  190. }
  191. }
  192. private static void readModule(DocumentInputStream dis, String name, ModuleMap modules) throws IOException {
  193. Module module = modules.get(name);
  194. // TODO Refactor this to fetch dir then do the rest
  195. if (module == null) {
  196. // no DIR stream with offsets yet, so store the compressed bytes for later
  197. module = new Module();
  198. modules.put(name, module);
  199. module.read(dis);
  200. } else {
  201. if (module.offset == null) {
  202. //This should not happen. bug 59858
  203. throw new IOException("Module offset for '" + name + "' was never read.");
  204. }
  205. // we know the offset already, so decompress immediately on-the-fly
  206. long skippedBytes = dis.skip(module.offset);
  207. if (skippedBytes != module.offset) {
  208. throw new IOException("tried to skip " + module.offset + " bytes, but actually skipped " + skippedBytes + " bytes");
  209. }
  210. InputStream stream = new RLEDecompressingInputStream(dis);
  211. module.read(stream);
  212. stream.close();
  213. }
  214. }
  215. /**
  216. * Skips <tt>n</tt> bytes in an input stream, throwing IOException if the
  217. * number of bytes skipped is different than requested.
  218. * @throws IOException
  219. */
  220. private static void trySkip(InputStream in, long n) throws IOException {
  221. long skippedBytes = in.skip(n);
  222. if (skippedBytes != n) {
  223. if (skippedBytes < 0) {
  224. throw new IOException(
  225. "Tried skipping " + n + " bytes, but no bytes were skipped. "
  226. + "The end of the stream has been reached or the stream is closed.");
  227. } else {
  228. throw new IOException(
  229. "Tried skipping " + n + " bytes, but only " + skippedBytes + " bytes were skipped. "
  230. + "This should never happen.");
  231. }
  232. }
  233. }
  234. // Constants from MS-OVBA: https://msdn.microsoft.com/en-us/library/office/cc313094(v=office.12).aspx
  235. private static final int EOF = -1;
  236. private static final int VERSION_INDEPENDENT_TERMINATOR = 0x0010;
  237. private static final int VERSION_DEPENDENT_TERMINATOR = 0x002B;
  238. private static final int PROJECTVERSION = 0x0009;
  239. private static final int PROJECTCODEPAGE = 0x0003;
  240. private static final int STREAMNAME = 0x001A;
  241. private static final int MODULEOFFSET = 0x0031;
  242. private static final int MODULETYPE_PROCEDURAL = 0x0021;
  243. private static final int MODULETYPE_DOCUMENT_CLASS_OR_DESIGNER = 0x0022;
  244. private static final int PROJECTLCID = 0x0002;
  245. private static final int MODULE_NAME = 0x0019;
  246. private static final int MODULE_NAME_UNICODE = 0x0047;
  247. private static final int MODULE_DOC_STRING = 0x001c;
  248. private static final int STREAMNAME_RESERVED = 0x0032;
  249. /**
  250. * Reads VBA Project modules from a VBA Project directory located at
  251. * <tt>macroDir</tt> into <tt>modules</tt>.
  252. *
  253. * @since 3.15-beta2
  254. */
  255. protected void readMacros(DirectoryNode macroDir, ModuleMap modules) throws IOException {
  256. for (Entry entry : macroDir) {
  257. if (! (entry instanceof DocumentNode)) { continue; }
  258. String name = entry.getName();
  259. DocumentNode document = (DocumentNode)entry;
  260. DocumentInputStream dis = new DocumentInputStream(document);
  261. try {
  262. if ("dir".equalsIgnoreCase(name)) {
  263. // process DIR
  264. RLEDecompressingInputStream in = new RLEDecompressingInputStream(dis);
  265. String streamName = null;
  266. String streamNameUnicode = null;
  267. int recordId = 0;
  268. try {
  269. while (true) {
  270. recordId = in.readShort();
  271. if (EOF == recordId
  272. || VERSION_INDEPENDENT_TERMINATOR == recordId) {
  273. break;
  274. }
  275. int recordLength = in.readInt();
  276. switch (recordId) {
  277. case PROJECTVERSION:
  278. trySkip(in, 6);
  279. break;
  280. case PROJECTCODEPAGE:
  281. int codepage = in.readShort();
  282. modules.charset = Charset.forName("Cp" + codepage);
  283. break;
  284. case STREAMNAME:
  285. streamName = readString(in, recordLength, modules.charset);
  286. int reserved = in.readShort();
  287. if (reserved != STREAMNAME_RESERVED) {
  288. throw new IOException("Expected x0032 after stream name before Unicode stream name, but found: "+
  289. Integer.toHexString(reserved));
  290. }
  291. int unicodeNameRecordLength = in.readInt();
  292. streamNameUnicode = readUnicodeString(in, unicodeNameRecordLength);
  293. //do something with this at some point
  294. break;
  295. case MODULEOFFSET:
  296. readModule(in, streamName, modules);
  297. break;
  298. default:
  299. trySkip(in, recordLength);
  300. break;
  301. }
  302. }
  303. } catch (final IOException e) {
  304. throw new IOException(
  305. "Error occurred while reading macros at section id "
  306. + recordId + " (" + HexDump.shortToHex(recordId) + ")", e);
  307. }
  308. finally {
  309. in.close();
  310. }
  311. } else if (!startsWithIgnoreCase(name, "__SRP")
  312. && !startsWithIgnoreCase(name, "_VBA_PROJECT")) {
  313. // process module, skip __SRP and _VBA_PROJECT since these do not contain macros
  314. readModule(dis, name, modules);
  315. }
  316. }
  317. finally {
  318. dis.close();
  319. }
  320. }
  321. }
  322. private String readUnicodeString(RLEDecompressingInputStream in, int unicodeNameRecordLength) throws IOException {
  323. byte[] buffer = new byte[unicodeNameRecordLength];
  324. IOUtils.readFully(in, buffer);
  325. return new String(buffer, Charset.forName("UTF-16LE"));
  326. }
  327. }