aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/poi/poifs/filesystem/Ole10Native.java
blob: c2d4a73ae48548e52d0a1b845ac04bce139edd4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/* ====================================================================
   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.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import org.apache.poi.util.IOUtils;
import org.apache.poi.util.LittleEndianByteArrayInputStream;
import org.apache.poi.util.LittleEndianConsts;
import org.apache.poi.util.LittleEndianInput;
import org.apache.poi.util.LittleEndianOutputStream;
import org.apache.poi.util.StringUtil;

/**
 * Represents an Ole10Native record which is wrapped around certain binary
 * files being embedded in OLE2 documents.<p>
 *
 * Ole10Native objects come in different shapes:
 * <ul>
 *     <li>unparsed: we can't identify it's structure</li>
 *     <li>compact: same as unparsed but with a leading flag</li>
 *     <li>parsed - Ole-Class "Package": data + ASCII label,command,filename</li>
 *     <li>parsed - Ole-Class "Package2": as above plus UTF16 label,command,filename</li>
 * </ul>
 */
@SuppressWarnings("unused")
public class Ole10Native {


    public static final String OLE10_NATIVE = "\u0001Ole10Native";
    private static final Charset ISO1 = StandardCharsets.ISO_8859_1;
    // arbitrarily selected; may need to increase
    private static final int MAX_RECORD_LENGTH = 100_000_000;
    // arbitrarily selected; may need to increase
    private static final int MAX_STRING_LENGTH = 1024;

    /**
     * Default content of the \u0001Ole entry
     */
    private static final byte[] OLE_MARKER_BYTES =
            {1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    private static final String OLE_MARKER_NAME = "\u0001Ole";


    // 4 bytes, total size of record not including this field
    private int totalSize;
    // 2 bytes, unknown, mostly [02 00]
    private short flags1 = 2;
    // ASCIIZ, stored in this field without the terminating zero
    private String label;
    // ASCIIZ, stored in this field without the terminating zero
    private String fileName;
    // 2 bytes, unknown, mostly [00 00]
    private short flags2;
    // see below
    private short unknown1 = 3;
    // ASCIIZ, stored in this field without the terminating zero
    private String command;
    // varying size, the actual native data
    private byte[] dataBuffer;
    // UTF16-LE String with leading length
    private String command2;
    // UTF16-LE String with leading length
    private String label2;
    // UTF16-LE String with leading length
    private String fileName2;

    /**
     * the field encoding mode - merely a try-and-error guess ...
     **/
    private enum EncodingMode {
        /**
         * the data is stored in parsed format - including label, command, etc.
         */
        parsed,
        /**
         * the data is stored raw after the length field
         */
        unparsed,
        /**
         * the data is stored raw after the length field and the flags1 field
         */
        compact
    }

    private EncodingMode mode;


    /**
     * Creates an instance of this class from an embedded OLE Object. The OLE Object is expected
     * to include a stream &quot;{01}Ole10Native&quot; 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 &quot;{01}Ole10Native&quot; 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 {
        DocumentEntry nativeEntry = (DocumentEntry) directory.getEntry(OLE10_NATIVE);
        try (DocumentInputStream dis = directory.createDocumentInputStream(nativeEntry)) {
            byte[] data = IOUtils.toByteArray(dis, nativeEntry.getSize(), MAX_RECORD_LENGTH);
            return new Ole10Native(data, 0);
        }
    }

    /**
     * Creates an instance and fills the fields based on ... the fields
     */
    public Ole10Native(String label, String filename, String command, byte[] data) {
        setLabel(label);
        setFileName(filename);
        setCommand(command);
        command2 = command;
        setDataBuffer(data);
        mode = EncodingMode.parsed;
    }

    /**
     * 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(final byte[] data, final int offset) throws Ole10NativeException {
        LittleEndianByteArrayInputStream leis = new LittleEndianByteArrayInputStream(data, offset);

        totalSize = leis.readInt();
        leis.limit(totalSize + LittleEndianConsts.INT_SIZE);

        leis.mark(0);

        try {
            flags1 = leis.readShort();
            if (flags1 == 2) {
                leis.mark(0);
                // some files like equations don't have a valid filename,
                // but somehow encode the formula right away in the ole10 header
                boolean validFileName = !Character.isISOControl(leis.readByte());
                leis.reset();

                if (validFileName) {
                    readParsed(leis);
                } else {
                    readCompact(leis);
                }
            } else {
                leis.reset();
                readUnparsed(leis);
            }
        } catch (IOException e) {
            throw new Ole10NativeException("Invalid Ole10Native", e);
        }
    }

    private void readParsed(LittleEndianByteArrayInputStream leis) throws Ole10NativeException, IOException {
        mode = EncodingMode.parsed;
        label = readAsciiZ(leis);
        fileName = readAsciiZ(leis);
        flags2 = leis.readShort();
        unknown1 = leis.readShort();
        command = readAsciiLen(leis);
        dataBuffer = IOUtils.toByteArray(leis, leis.readInt(), MAX_RECORD_LENGTH);

        leis.mark(0);
        short lowSize = leis.readShort();
        if (lowSize != 0) {
            leis.reset();
            command2 = readUtf16(leis);
            label2 = readUtf16(leis);
            fileName2 = readUtf16(leis);
        }
    }

    private void readCompact(LittleEndianByteArrayInputStream leis) throws IOException {
        mode = EncodingMode.compact;
        dataBuffer = IOUtils.toByteArray(leis, totalSize - LittleEndianConsts.SHORT_SIZE, MAX_RECORD_LENGTH);
    }

    private void readUnparsed(LittleEndianByteArrayInputStream leis) throws IOException {
        mode = EncodingMode.unparsed;
        dataBuffer = IOUtils.toByteArray(leis, totalSize, MAX_RECORD_LENGTH);
    }

    /**
     * Add the \1OLE marker entry, which is not the Ole10Native entry.
     * Beside this "\u0001Ole" record there were several other records, e.g. CompObj,
     * OlePresXXX, but it seems, that they aren't necessary
     */
    public static void createOleMarkerEntry(final DirectoryEntry parent) throws IOException {
        if (!parent.hasEntry(OLE_MARKER_NAME)) {
            parent.createDocument(OLE_MARKER_NAME, new ByteArrayInputStream(OLE_MARKER_BYTES));
        }
    }

    /**
     * Add the \1OLE marker entry, which is not the Ole10Native entry.
     * Beside this "\u0001Ole" record there were several other records, e.g. CompObj,
     * OlePresXXX, but it seems, that they aren't necessary
     */
    public static void createOleMarkerEntry(final POIFSFileSystem poifs) throws IOException {
        createOleMarkerEntry(poifs.getRoot());
    }


    /**
     * Read zero terminated string (ASCIIZ).
     */
    private static String readAsciiZ(LittleEndianInput is) throws Ole10NativeException {
        // arbitrary sized buffer - not sure how big strings can get in an Ole10 record
        byte[] buf = new byte[MAX_STRING_LENGTH];
        for (int i=0; i<buf.length; i++) {
            if ((buf[i] = is.readByte()) == 0) {
                return StringUtil.getFromCompressedUnicode(buf, 0, i);
            }
        }
        throw new Ole10NativeException("AsciiZ string was not null terminated after " + MAX_STRING_LENGTH + " bytes - Exiting.");
    }

    private static String readAsciiLen(LittleEndianByteArrayInputStream leis) throws IOException {
        int size = leis.readInt();
        byte[] buf = IOUtils.toByteArray(leis, size, MAX_STRING_LENGTH);
        return (buf.length == 0) ? "" : StringUtil.getFromCompressedUnicode(buf, 0, size - 1);
    }

    private static String readUtf16(LittleEndianByteArrayInputStream leis) throws IOException {
        int size = leis.readInt();
        byte[] buf = IOUtils.toByteArray(leis, size * 2, MAX_STRING_LENGTH);
        return StringUtil.getFromUnicodeLE(buf, 0, size);
    }

    /**
     * Returns the value of the totalSize field - the total length of the
     * structure is totalSize + 4 (value of this field + size of this field).
     *
     * @return the totalSize
     */
    public int getTotalSize() {
        return totalSize;
    }

    /**
     * Returns flags1 - currently unknown - usually 0x0002.
     *
     * @return the flags1
     */
    public short getFlags1() {
        return flags1;
    }

    /**
     * Returns the label field - usually the name of the file (without
     * directory) but probably may be any name specified during
     * packaging/embedding the data.
     *
     * @return the label
     */
    public String getLabel() {
        return label;
    }

    /**
     * Returns the fileName field - usually the name of the file being embedded
     * including the full path.
     *
     * @return the fileName
     */
    public String getFileName() {
        return fileName;
    }

    /**
     * Returns flags2 - currently unknown - mostly 0x0000.
     *
     * @return the flags2
     */
    public short getFlags2() {
        return flags2;
    }

    /**
     * Returns unknown1 field - currently unknown.
     *
     * @return the unknown1
     */
    public short getUnknown1() {
        return unknown1;
    }

    /**
     * Returns the command field - usually the name of the file being embedded
     * including the full path, may be a command specified during embedding the
     * file.
     *
     * @return the command
     */
    public String getCommand() {
        return command;
    }

    /**
     * Returns the size of the embedded file. If the size is 0 (zero), no data
     * has been embedded. To be sure, that no data has been embedded, check
     * whether {@link #getDataBuffer()} returns <code>null</code>.
     *
     * @return the dataSize
     */
    public int getDataSize() {
        return dataBuffer.length;
    }

    /**
     * Returns the buffer containing the embedded file's data, or
     * <code>null</code> 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 returns
     * <code>null</code>.)
     *
     * @return the dataBuffer
     */
    public byte[] getDataBuffer() {
        return dataBuffer;
    }

    /**
     * Have the contents printer out into an OutputStream, used when writing a
     * file back out to disk (Normally, atom classes will keep their bytes
     * around, but non atom classes will just request the bytes from their
     * children, then chuck on their header and return)
     */
    public void writeOut(OutputStream out) throws IOException {
        // byte intbuf[] = new byte[LittleEndianConsts.INT_SIZE];
        // byte shortbuf[] = new byte[LittleEndianConsts.SHORT_SIZE];

        @SuppressWarnings("resource")
        LittleEndianOutputStream leosOut = new LittleEndianOutputStream(out);

        switch (mode) {
            case parsed: {
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                try (LittleEndianOutputStream leos = new LittleEndianOutputStream(bos)) {
                    // total size, will be determined later ..

                    leos.writeShort(getFlags1());
                    leos.write(getLabel().getBytes(ISO1));
                    leos.write(0);
                    leos.write(getFileName().getBytes(ISO1));
                    leos.write(0);
                    leos.writeShort(getFlags2());
                    leos.writeShort(getUnknown1());
                    leos.writeInt(getCommand().length() + 1);
                    leos.write(getCommand().getBytes(ISO1));
                    leos.write(0);
                    leos.writeInt(getDataSize());
                    leos.write(getDataBuffer());

                    if (command2 == null || label2 == null || fileName2 == null) {
                        leos.writeShort(0);
                    } else {
                        leos.writeUInt(command2.length());
                        leos.write(StringUtil.getToUnicodeLE(command2));
                        leos.writeUInt(label2.length());
                        leos.write(StringUtil.getToUnicodeLE(label2));
                        leos.writeUInt(fileName2.length());
                        leos.write(StringUtil.getToUnicodeLE(fileName2));
                    }
                }

                // total size
                leosOut.writeInt(bos.size());
                bos.writeTo(out);
                break;
            }

            case compact:
                leosOut.writeInt(getDataSize() + LittleEndianConsts.SHORT_SIZE);
                leosOut.writeShort(getFlags1());
                out.write(getDataBuffer());
                break;

            default:
            case unparsed:
                leosOut.writeInt(getDataSize());
                out.write(getDataBuffer());
                break;
        }

    }

    public void setFlags1(short flags1) {
        this.flags1 = flags1;
    }

    public void setFlags2(short flags2) {
        this.flags2 = flags2;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public void setCommand(String command) {
        this.command = command;
    }

    public void setUnknown1(short unknown1) {
        this.unknown1 = unknown1;
    }

    public void setDataBuffer(byte[] dataBuffer) {
        this.dataBuffer = dataBuffer.clone();
    }

    /**
     * Get Command string of UTF16 extended OLE packages or {@code null} if not set or not UTF16 extended
     */
    public String getCommand2() {
        return command2;
    }

    /**
     * Set Command string for UTF16 extended OLE packages or {@code null} if not set or not UTF16 extended
     */
    public void setCommand2(String command2) {
        this.command2 = command2;
    }

    /**
     * Get Label string for UTF16 extended OLE packages or {@code null} if not set or not UTF16 extended
     */
    public String getLabel2() {
        return label2;
    }

    /**
     * Set Label string for UTF16 extended OLE packages or {@code null} if not set or not UTF16 extended
     */
    public void setLabel2(String label2) {
        this.label2 = label2;
    }

    /**
     * Get filename string for UTF16 extended OLE packages or {@code null} if not set or not UTF16 extended
     */
    public String getFileName2() {
        return fileName2;
    }

    /**
     * Set filename string for UTF16 extended OLE packages or {@code null} if not set or not UTF16 extended
     */
    public void setFileName2(String fileName2) {
        this.fileName2 = fileName2;
    }
}