-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathppt.go
360 lines (322 loc) · 10 KB
/
ppt.go
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
package goppt
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"strings"
"github.com/richardlehane/mscfb"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform"
"github.com/KSpaceer/goppt/internal/ioadapters"
)
// skipped metadata or non-readable records in slide container
var slideSkippedRecordsTypes = []recordType{
recordTypeExternalObjectList,
recordTypeEnvironment,
recordTypeSoundCollection,
recordTypeDrawingGroup,
recordTypeSlideListWithText,
recordTypeList,
recordTypeHeadersFooters,
recordTypeHeadersFooters,
}
// skipped metadata or non-readable records in drawing container
var drawingSkippedRecordsTypes = []recordType{
recordTypeSlideShowSlideInfoAtom,
recordTypeHeadersFooters,
recordTypeRoundTripSlideSyncInfo12,
}
const (
userPersistIDRefOffset = 16
)
// ExtractText parses PPT file represented by Reader r and extracts text from it.
func ExtractText(r io.Reader) (string, error) {
ra := ioadapters.ToReaderAt(r)
d, err := mscfb.New(ra)
if err != nil {
return "", err
}
currentUser, pptDocument := getCurrentUserAndPPTDoc(d)
if err := isValidPPT(currentUser, pptDocument); err != nil {
return "", err
}
offsetPersistDirectory, liveRecord, err := getUserEditAtomsData(currentUser, pptDocument)
if err != nil {
return "", err
}
persistDirEntries, err := getPersistDirectoryEntries(pptDocument, offsetPersistDirectory)
if err != nil {
return "", err
}
// get DocumentContainer reference
docPersistIDRef := liveRecord.LongAt(userPersistIDRefOffset)
documentContainer, err := readRecord(pptDocument, persistDirEntries[docPersistIDRef], recordTypeDocument)
if err != nil {
return "", err
}
return readSlides(documentContainer, pptDocument, persistDirEntries)
}
// toMemoryBuffer transforms io.Reader to in-memory io.ReaderAt
// getCurrentUserAndPPTDoc extracts necessary mscfb files from PPT file
func getCurrentUserAndPPTDoc(r *mscfb.Reader) (currentUser *mscfb.File, pptDocument *mscfb.File) {
for _, f := range r.File {
switch f.Name {
case "Current User":
currentUser = f
case "PowerPoint Document":
pptDocument = f
}
}
return currentUser, pptDocument
}
// isValidPPT checks if provided file is valid, meaning
// it has both "Current User" and "PowerPoint Document" files
// and "Current User"'s CurrentUserAtom record has valid header token
func isValidPPT(currentUser, pptDocument *mscfb.File) error {
const (
headerTokenOffset = 12
encryptedDocumentToken = 0xF3D1C4DF
plainDocumentToken = 0xE391C05F
)
if currentUser == nil || pptDocument == nil {
return fmt.Errorf(".ppt file must contain \"Current User\" and \"PowerPoint Document\" streams")
}
var b [4]byte
_, err := currentUser.ReadAt(b[:], headerTokenOffset)
if err != nil {
return err
}
headerToken := binary.LittleEndian.Uint32(b[:])
if headerToken != plainDocumentToken && headerToken != encryptedDocumentToken {
return fmt.Errorf("invalid UserEditAtom header token %X", headerToken)
}
return nil
}
// getUserEditAtomsData extracts "live record" and persist directory offsets
// according to section 2.1.2 of specification (https://msopenspecs.azureedge.net/files/MS-PPT/%5bMS-PPT%5d-210422.pdf)
func getUserEditAtomsData(currentUser, pptDocument *mscfb.File) (
persistDirectoryOffsets []int64,
liveRecord record,
err error,
) {
const (
offsetLastEditInitialPosition = 16
offsetLastEditPosition = 8
persistDirectoryOffsetPosition = 12
)
var b [4]byte
_, err = currentUser.ReadAt(b[:], offsetLastEditInitialPosition)
if err != nil {
return nil, record{}, err
}
offsetLastEdit := binary.LittleEndian.Uint32(b[:])
for {
liveRecord, err = readRecord(pptDocument, int64(offsetLastEdit), recordTypeUserEditAtom)
if err != nil {
if errors.Is(err, errMismatchRecordType) {
break
}
return nil, record{}, err
}
persistDirectoryOffsets = append(
persistDirectoryOffsets,
int64(liveRecord.LongAt(persistDirectoryOffsetPosition)),
)
offsetLastEdit = liveRecord.LongAt(offsetLastEditPosition)
if offsetLastEdit == 0 {
break
}
}
return persistDirectoryOffsets, liveRecord, err
}
// getPersistDirectoryEntries transforms offsets into persists directory identifiers and persist offsets according
// to section 2.1.2 of specification (https://msopenspecs.azureedge.net/files/MS-PPT/%5bMS-PPT%5d-210422.pdf)
func getPersistDirectoryEntries(pptDocument *mscfb.File, offsets []int64) (map[uint32]int64, error) {
const persistOffsetEntrySize = 4
persistDirEntries := make(map[uint32]int64)
for i := len(offsets) - 1; i >= 0; i-- {
rgPersistDirEntry, err := readRecord(pptDocument, offsets[i], recordTypePersistDirectoryAtom)
if err != nil {
return nil, err
}
rgPersistDirEntryData := rgPersistDirEntry.recordData
for j := 0; j < len(rgPersistDirEntryData); {
persist := rgPersistDirEntryData.LongAt(j)
persistID := persist & 0x000FFFFF
cPersist := ((persist & 0xFFF00000) >> 20) & 0x00000FFF
j += 4
for k := uint32(0); k < cPersist; k++ {
persistDirEntries[persistID+k] = int64(rgPersistDirEntryData.LongAt(j + int(k)*persistOffsetEntrySize))
}
j += int(cPersist * persistOffsetEntrySize)
}
}
return persistDirEntries, nil
}
// readSlides reads text from slides of given DocumentContainer
func readSlides(documentContainer, pptDocument io.ReaderAt, persistDirEntries map[uint32]int64) (string, error) {
const slideSkipInitialOffset = 48
offset, err := skipRecords(documentContainer, slideSkipInitialOffset, slideSkippedRecordsTypes)
if err != nil {
return "", err
}
slideList, err := readRecord(documentContainer, offset, recordTypeSlideListWithText)
if err != nil {
return "", err
}
utf16Decoder := unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM).NewDecoder()
var out strings.Builder
n := len(slideList.Data())
for i := 0; i < n; {
block, err := readRecord(slideList, int64(i), recordTypeUnspecified)
if err != nil {
return "", err
}
switch block.Type() {
case recordTypeSlidePersistAtom:
err = readTextFromSlidePersistAtom(block, pptDocument, persistDirEntries, &out, utf16Decoder)
case recordTypeTextCharsAtom:
err = readTextFromTextCharsAtom(block, &out, utf16Decoder)
case recordTypeTextBytesAtom:
err = readTextFromTextBytesAtom(block, &out, utf16Decoder)
}
if err != nil {
return "", err
}
i += len(block.Data()) + 8
}
return out.String(), nil
}
func readTextFromSlidePersistAtom(
block record,
pptDocument io.ReaderAt,
persistDirEntries map[uint32]int64,
out *strings.Builder,
utf16Decoder *encoding.Decoder,
) error {
const (
slidePersistAtomSkipInitialOffset = 32
headerRecordTypeOffset = 2
)
persistDirID := block.LongAt(0)
// extract slide from persist directory
slide, err := readRecord(pptDocument, persistDirEntries[persistDirID], recordTypeSlide)
if err != nil {
return err
}
// skip metadata
offset, err := skipRecords(slide, slidePersistAtomSkipInitialOffset, drawingSkippedRecordsTypes)
if err != nil {
return err
}
drawing, err := readRecord(slide, offset, recordTypeDrawing)
if err != nil {
return err
}
drawingBytes := drawing.Data()
from := 0
for {
// instead of parsing binary PPT format, search text records directly
pocketIdx := matchPocket(drawingBytes, from)
if pocketIdx == -1 {
break
}
// check if it is really a text record - recordType bytes must be preceded by 1-byte version and 3-byte instance
// fields with zero values
if pocketIdx >= 2 && bytes.Equal(drawingBytes[pocketIdx-headerRecordTypeOffset:pocketIdx], []byte{0x00, 0x00}) {
var rec record
if drawingBytes[pocketIdx] == recordTypeTextBytesAtom.LowerPart() {
rec, err = readRecord(drawing, int64(pocketIdx-headerRecordTypeOffset), recordTypeTextBytesAtom)
if err != nil {
return err
}
err = readTextFromTextBytesAtom(rec, out, utf16Decoder)
} else {
rec, err = readRecord(drawing, int64(pocketIdx-headerRecordTypeOffset), recordTypeTextCharsAtom)
if err != nil {
return err
}
err = readTextFromTextCharsAtom(rec, out, utf16Decoder)
}
if err != nil {
return err
}
}
from = pocketIdx + 2
}
return nil
}
func matchPocket(data []byte, from int) int {
data = data[from:]
n := len(data)
for i := 0; i < n; i++ {
switch data[i] {
case recordTypeTextCharsAtom.LowerPart(), recordTypeTextBytesAtom.LowerPart():
if i < n-1 && data[i+1] == 0x0F {
return i + from
}
}
}
return -1
}
// readTextFromTextCharsAtom simply transforms UTF-16LE data into UTF-8 data
func readTextFromTextCharsAtom(atom record, out *strings.Builder, dec *encoding.Decoder) error {
dec.Reset()
transformed, err := dec.Bytes(atom.Data())
if err != nil {
return err
}
out.Write(transformed)
out.WriteByte(' ')
return nil
}
func readTextFromTextBytesAtom(atom record, out *strings.Builder, dec *encoding.Decoder) error {
dec.Reset()
transformed, err := decodeTextBytesAtom(atom.Data(), dec)
if err != nil {
return err
}
out.Write(transformed)
out.WriteByte(' ')
return nil
}
// decodeTextBytesAtom transforms text from TextBytesAtom, which is an array of bytes representing lower parts of UTF-16
// characters into UTF-8 data
func decodeTextBytesAtom(data []byte, dec *encoding.Decoder) ([]byte, error) {
var (
// buffer for UTF-16 char
buf [2]byte
err error
)
result := make([]byte, 0, len(data))
for i := range data {
// filling upper part of character with zero
clear(buf[:])
// fill lower part with byte
buf[0] = data[i]
// transform single UTF-16 char into UTF-8 rune and append it into result
result, _, err = transform.Append(dec, result, buf[:])
if err != nil {
return nil, err
}
}
return result, nil
}
// skipRecords reads headers and skips data of records of provided types
func skipRecords(r io.ReaderAt, initialOffset int64, skippedRecordsTypes []recordType) (int64, error) {
offset := initialOffset
for i := range skippedRecordsTypes {
rec, err := readRecordHeaderOnly(r, offset, skippedRecordsTypes[i])
if err != nil {
if errors.Is(err, errMismatchRecordType) {
continue
}
return 0, err
}
offset += int64(rec.Length() + headerSize)
}
return offset, nil
}