org.apache.poi.poifs.filesystem.Ole10Native Maven / Gradle / Ivy
The newest version!
/* ==================================================================== Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ package org.apache.poi.poifs.filesystem; import java.io.FileNotFoundException; import java.io.IOException; import org.apache.poi.util.HexDump; import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndianConsts; import org.apache.poi.util.StringUtil; /** * Represents an Ole10Native record which is wrapped around certain binary * files being embedded in OLE2 documents. * * @author Rainer Schwarze */ public class Ole10Native { // (the fields as they appear in the raw record:) private final int totalSize; // 4 bytes, total size of record not including this field private short flags1; // 2 bytes, unknown, mostly [02 00] private final String label; // ASCIIZ, stored in this field without the terminating zero private final String fileName; // ASCIIZ, stored in this field without the terminating zero private short flags2; // 2 bytes, unknown, mostly [00 00] // private byte unknown1Length; // 1 byte, specifying the length of the following byte array (unknown1) private byte[] unknown1; // see below private byte[] unknown2; // 3 bytes, unknown, mostly [00 00 00] private final String command; // ASCIIZ, stored in this field without the terminating zero private final int dataSize; // 4 bytes (if space), size of following buffer private final byte[] dataBuffer; // varying size, the actual native data private short flags3; // some final flags? or zero terminators?, sometimes not there public static final String OLE10_NATIVE = "\u0001Ole10Native"; /** * Creates an instance of this class from an embedded OLE Object. The OLE Object is expected * to include a stream "{01}Ole10Native" which contains the actual * data relevant for this class. * * @param poifs POI Filesystem object * @return Returns an instance of this class * @throws IOException on IO error * @throws Ole10NativeException on invalid or unexcepted data format */ public static Ole10Native createFromEmbeddedOleObject(POIFSFileSystem poifs) throws IOException, Ole10NativeException { return createFromEmbeddedOleObject(poifs.getRoot()); } /** * Creates an instance of this class from an embedded OLE Object. The OLE Object is expected * to include a stream "{01}Ole10Native" which contains the actual * data relevant for this class. * * @param directory POI Filesystem object * @return Returns an instance of this class * @throws IOException on IO error * @throws Ole10NativeException on invalid or unexcepted data format */ public static Ole10Native createFromEmbeddedOleObject(DirectoryNode directory) throws IOException, Ole10NativeException { boolean plain = false; try { directory.getEntry("\u0001Ole10ItemName"); plain = true; } catch (FileNotFoundException ex) { plain = false; } DocumentEntry nativeEntry = (DocumentEntry)directory.getEntry(OLE10_NATIVE); byte[] data = new byte[nativeEntry.getSize()]; directory.createDocumentInputStream(nativeEntry).read(data); return new Ole10Native(data, 0, plain); } /** * Creates an instance and fills the fields based on the data in the given buffer. * * @param data The buffer containing the Ole10Native record * @param offset The start offset of the record in the buffer * @throws Ole10NativeException on invalid or unexcepted data format */ public Ole10Native(byte[] data, int offset) throws Ole10NativeException { this(data, offset, false); } /** * Creates an instance and fills the fields based on the data in the given buffer. * * @param data The buffer containing the Ole10Native record * @param offset The start offset of the record in the buffer * @param plain Specified 'plain' format without filename * @throws Ole10NativeException on invalid or unexcepted data format */ public Ole10Native(byte[] data, int offset, boolean plain) throws Ole10NativeException { int ofs = offset; // current offset, initialized to start if (data.length
. * * @return the dataSize */ public int getDataSize() { return dataSize; } /** * Returns the buffer containing the embedded file's data, orLittleEndianConsts.INT_SIZE) { dataSize = LittleEndian.getInt(data, ofs); ofs += LittleEndianConsts.INT_SIZE; if (dataSize > totalSize || dataSize<0) { throw new Ole10NativeException("Invalid Ole10Native"); } dataBuffer = new byte[dataSize]; System.arraycopy(data, ofs, dataBuffer, 0, dataSize); ofs += dataSize; if (unknown1.length > 0) { flags3 = LittleEndian.getShort(data, ofs); ofs += LittleEndianConsts.SHORT_SIZE; } else { flags3 = 0; } } else { throw new Ole10NativeException("Invalid Ole10Native"); } } } /* * Helper - determine length of zero terminated string (ASCIIZ). */ private static int getStringLength(byte[] data, int ofs) { int len = 0; while (len+ofs null null
* if no data was embedded. Note that an embedding may provide information about * the data, but the actual data is not included. (So label, filename etc. are * available, but this method returnsnull
.) * * @return the dataBuffer */ public byte[] getDataBuffer() { return dataBuffer; } /** * Returns the flags3 - currently unknown. * * @return the flags3 */ public short getFlags3() { return flags3; } }