aboutsummaryrefslogtreecommitdiffstats
path: root/poi-scratchpad/src/main/java/org/apache/poi/hslf/dev/SlideShowDumper.java
blob: 8b43555d076c3881d0416c3a943077682488cfea (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
/* ====================================================================
   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.hslf.dev;

import org.apache.poi.ddf.DefaultEscherRecordFactory;
import org.apache.poi.ddf.EscherContainerRecord;
import org.apache.poi.ddf.EscherRecord;
import org.apache.poi.ddf.EscherTextboxRecord;
import org.apache.poi.hslf.record.HSLFEscherRecordFactory;
import org.apache.poi.hslf.record.RecordTypes;
import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.HexDump;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.LittleEndian;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Locale;

/**
 * This class provides a way to "peek" inside a powerpoint file. It
 * will print out all the types it finds, and for those it knows aren't
 * atoms, what they contain
 * <p>
 * To figure out what things are, and if they are atoms or not, used the
 * list in hslf.record.RecordTypes
 * <p>
 * To peek inside PPDrawings, which hold Escher drawings, we use the
 * DDF package from POI (but we can fake it by using the Escher listings
 * from hslf.record.RecordTypes also)
 */
public final class SlideShowDumper {

    //arbitrarily selected; may need to increase
    private static final int DEFAULT_MAX_RECORD_LENGTH = 100_000;
    private static int MAX_RECORD_LENGTH = DEFAULT_MAX_RECORD_LENGTH;

    private byte[] docstream;

    /**
     * Do we try to use DDF to understand the escher objects?
     */
    private boolean ddfEscher;
    /**
     * Do we use our own built-in basic escher groker to understand the escher objects?
     */
    private boolean basicEscher;

    private PrintStream out;

    /**
     * @param length the max record length allowed for SlideShowDumper
     */
    public static void setMaxRecordLength(int length) {
        MAX_RECORD_LENGTH = length;
    }

    /**
     * @return the max record length allowed for SlideShowDumper
     */
    public static int getMaxRecordLength() {
        return MAX_RECORD_LENGTH;
    }

    /**
     * right now this function takes one parameter: a ppt file, and outputs
     * a dump of what it contains
     */
    public static void main(String[] args) throws IOException {
        if (args.length == 0) {
            System.err.println("Usage: SlideShowDumper [-escher|-basicescher] <filename>");
            return;
        }

        String filename = args[0];
        if (args.length > 1) {
            filename = args[1];
        }

        try (POIFSFileSystem poifs = new POIFSFileSystem(new File(filename))) {
            SlideShowDumper foo = new SlideShowDumper(poifs, System.out);

            if (args.length > 1) {
                if (args[0].equalsIgnoreCase("-escher")) {
                    foo.setDDFEscher(true);
                } else {
                    foo.setBasicEscher(true);
                }
            }

            foo.printDump();
        }
    }

    /**
     * Constructs a Powerpoint dump from a POIFS Filesystem. Parses the
     * document and dumps out the contents
     *
     * @param filesystem the POIFS FileSystem to read from
     * @throws IOException if there is a problem while parsing the document.
     */
    public SlideShowDumper(POIFSFileSystem filesystem, PrintStream out) throws IOException {
        // Grab the document stream
        InputStream is = filesystem.createDocumentInputStream(HSLFSlideShow.POWERPOINT_DOCUMENT);
        docstream = IOUtils.toByteArray(is);
        is.close();
        this.out = out;
    }

    /**
     * Control dumping of any Escher records found - should DDF be used?
     */
    public void setDDFEscher(boolean grok) {
        ddfEscher = grok;
        basicEscher = !(grok);
    }

    /**
     * Control dumping of any Escher records found - should our built in
     * basic groker be used?
     */
    public void setBasicEscher(boolean grok) {
        basicEscher = grok;
        ddfEscher = !(grok);
    }

    public void printDump() throws IOException {
        // The format of records in a powerpoint file are:
        //   <little endian 2 byte "info">
        //   <little endian 2 byte "type">
        //   <little endian 4 byte "length">
        // If it has a zero length, following it will be another record
        //      <xx xx yy yy 00 00 00 00> <xx xx yy yy zz zz zz zz>
        // If it has a length, depending on its type it may have children or data
        // If it has children, these will follow straight away
        //      <xx xx yy yy zz zz zz zz <xx xx yy yy zz zz zz zz>>
        // If it has data, this will come straight after, and run for the length
        //      <xx xx yy yy zz zz zz zz dd dd dd dd dd dd dd>
        // All lengths given exclude the 8 byte record header
        // (Data records are known as Atoms)

        // Document should start with:
        //   0F 00 E8 03 ## ## ## ##
        //     (type 1000 = document, info 00 0f is normal, rest is document length)
        //   01 00 E9 03 28 00 00 00
        //     (type 1001 = document atom, info 00 01 normal, 28 bytes long)

        // When parsing a document, look to see if you know about that type
        //  of the current record. If you know it's a type that has children,
        //  process the record's data area looking for more records
        // If you know about the type and it doesn't have children, either do
        //  something with the data (eg TextRun) or skip over it
        // Otherwise, check the first byte. If you do a BINARY_AND on it with
        //  0x0f (15) and get back 0x0f, you know it has children. Otherwise
        //  it doesn't

        walkTree(0, 0, docstream.length);
    }

    public void walkTree(int depth, int startPos, int maxLen) throws IOException {
        int pos = startPos;
        int endPos = startPos + maxLen;
        final String ind = (depth == 0) ? "%1$s" : "%1$" + depth + "s";
        while (pos <= endPos - 8) {
            long type = LittleEndian.getUShort(docstream, pos + 2);
            long len = LittleEndian.getUInt(docstream, pos + 4);
            byte opt = docstream[pos];

            String fmt = ind + "At position %2$d (%2$04x): type is %3$d (%3$04x), len is %4$d (%4$04x)";
            out.printf(Locale.ROOT, (fmt) + "%n", "", pos, type, len);

            // See if we know about the type of it
            String recordName = RecordTypes.forTypeID((short) type).name();

            // Jump over header, and think about going on more
            pos += 8;
            out.printf(Locale.ROOT, ind + "That's a %2$s%n", "", recordName);

            if (len < 0 /*|| len > Integer.MAX_VALUE*/) {
                // stop processing of invalid header data
                continue;
            }

            // Now check if it's a container or not
            int container = opt & 0x0f;

            // BinaryTagData seems to contain records, but it
            //  isn't tagged as doing so. Try stepping in anyway
            if (type == 5003L && opt == 0L) {
                container = 0x0f;
            }

            out.println();
            if (type != 0L && container == 0x0f) {
                if (type == 1035L || type == 1036L) {
                    // Special Handling of 1035=PPDrawingGroup and 1036=PPDrawing
                    if (ddfEscher) {
                        // Seems to be:
                        walkEscherDDF((depth + 3), pos + 8, (int) len - 8);
                    } else if (basicEscher) {
                        walkEscherBasic((depth + 3), pos + 8, (int) len - 8);
                    }
                } else {
                    // General container record handling code
                    walkTree((depth + 2), pos, (int) len);
                }
            }

            pos += (int) Math.min(len, Integer.MAX_VALUE);
        }
    }

    /**
     * Use the DDF code to walk the Escher records
     */
    public void walkEscherDDF(int indent, int pos, int len) {
        if (len < 8) {
            return;
        }

        final String ind = (indent == 0) ? "%1$s" : "%1$" + indent + "s";

        byte[] contents = IOUtils.safelyClone(docstream, pos, len, MAX_RECORD_LENGTH);
        DefaultEscherRecordFactory erf = new HSLFEscherRecordFactory();
        EscherRecord record = erf.createRecord(contents, 0);

        // For now, try filling in the fields
        record.fillFields(contents, 0, erf);

        long atomType = LittleEndian.getUShort(contents, 2);
        // This lacks the 8 byte header size
        long atomLen = LittleEndian.getUShort(contents, 4);
        // This (should) include the 8 byte header size
        int recordLen = record.getRecordSize();

        String fmt = ind + "At position %2$d (%2$04x): type is %3$d (%3$04x), len is %4$d (%4$04x) (%5$d) - record claims %6$d";
        out.printf(Locale.ROOT, (fmt) + "%n", "", pos, atomType, atomLen, atomLen + 8, recordLen);


        // Check for corrupt / lying ones
        if (recordLen != 8 && (recordLen != (atomLen + 8))) {
            out.printf(Locale.ROOT, ind + "** Atom length of %2d (%3d) doesn't match record length of %4d%n", atomLen, atomLen + 8, recordLen);
        }

        // Print the record's details
        String recordStr = record.toString().replace("\n", String.format(Locale.ROOT, "\n" + ind, ""));
        out.printf(Locale.ROOT, ind + "%2$s%n", "", recordStr);

        if (record instanceof EscherContainerRecord) {
            walkEscherDDF((indent + 3), pos + 8, (int) atomLen);
        }

        // Handle records that seem to lie
        if (atomType == 61451L) {
            // Normally claims a size of 8
            recordLen = (int) atomLen + 8;
        }
        if (atomType == 61453L) {
            // Returns EscherContainerRecord, but really msofbtClientTextbox
            recordLen = (int) atomLen + 8;
            record.fillFields(contents, 0, erf);
            if (!(record instanceof EscherTextboxRecord)) {
                out.printf(Locale.ROOT, ind + "%2$s%n", "", "** Really a msofbtClientTextbox !");
            }
        }

        // Decide on what to do, based on how the lengths match up
        if (recordLen == 8 && atomLen > 8) {
            // Assume it has children, rather than being corrupted
            walkEscherDDF((indent + 3), pos + 8, (int) atomLen);
        }
        // Wind on our length + our header
        pos = Math.toIntExact(pos + atomLen) + 8;
        len = Math.toIntExact(len - atomLen) - 8;

        // Move on to the next one, if we're not at the end yet
        if (len >= 8) {
            walkEscherDDF(indent, pos, len);
        }
    }

    /**
     * Use the basic record format groking code to walk the Escher records
     */
    public void walkEscherBasic(int indent, int pos, int len) throws IOException {
        if (len < 8) {
            return;
        }

        final String ind = (indent == 0) ? "%1$s" : "%1$" + indent + "s";

        long type = LittleEndian.getUShort(docstream, pos + 2);
        long atomlen = LittleEndian.getUInt(docstream, pos + 4);

        String fmt = ind + "At position %2$d ($2$04x): type is %3$d (%3$04x), len is %4$d (%4$04x)";
        out.printf(Locale.ROOT, (fmt) + "%n", "", pos, type, atomlen);

        String typeName = RecordTypes.forTypeID((short) type).name();
        out.printf(Locale.ROOT, ind + "%2$s%n", "That's an Escher Record: ", typeName);

        // Record specific dumps
        if (type == 61453L) {
            // Text Box. Print out first 8 bytes of data, then 8 4 later
            HexDump.dump(docstream, 0, out, pos + 8, 8);
            HexDump.dump(docstream, 0, out, pos + 20, 8);
            out.println();
        }


        // Blank line before next entry
        out.println();

        // Look in children if we are a container
        if (type == 61443L || type == 61444L) {
            walkEscherBasic((indent + 3), pos + 8, (int) atomlen);
        }

        // Keep going if not yet at end
        if (atomlen < len) {
            int atomleni = (int) atomlen;
            walkEscherBasic(indent, pos + atomleni + 8, len - atomleni - 8);
        }
    }
}