-
Notifications
You must be signed in to change notification settings - Fork 17
/
MOBI 2 Text.go
415 lines (293 loc) · 9.44 KB
/
MOBI 2 Text.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
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
/*
File Name: MOBI 2 Text.go
Copyright: 2019 Kleissner Investments s.r.o.
Author: Peter Kleissner
Mobi files use HTML tags.
Did not work:
* https://github.com/766b/mobi is only a writer and does not have a useful reader
* https://github.com/peterbn/mobi a fork of above one.
Works:
* https://github.com/neofight/mobi code basically works, just an in-memory open function had to be forked.
*/
package fileconversion
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"strconv"
"strings"
"unicode/utf8"
html "github.com/levigross/exp-html"
"github.com/neofight/mobi/convert"
"github.com/neofight/mobi/headers"
)
// Mobi2Text converts a MOBI ebook to text
func Mobi2Text(file io.ReadSeeker) (string, error) {
book, _ := mobiOpen(file)
markupText, _ := book.Markup()
text, _ := HTML2Text(strings.NewReader(markupText))
return text, nil
}
// below code is forked from https://github.com/neofight/mobi MOBIFile.go
type mobiBook struct {
file io.ReadSeeker
pdbHeader *headers.PDB
palmDOCHeader *headers.PalmDOC
mobiHeader *headers.MOBI
exthHeader *headers.EXTH
}
func mobiOpen(file io.ReadSeeker) (*mobiBook, error) {
var book mobiBook
var err error
book.file = file
book.pdbHeader, err = headers.ReadPDB(book.file)
if err != nil {
return nil, fmt.Errorf("unable to read PDB header: %v", err)
}
book.palmDOCHeader, err = headers.ReadPalmDOC(book.file)
if err != nil {
return nil, fmt.Errorf("unable to read PalmDOC header: %v", err)
}
book.mobiHeader, err = headers.ReadMOBI(book.file)
if err != nil {
return nil, fmt.Errorf("unable to read MOBI header: %v", err)
}
if book.mobiHeader.EXTHHeaderPresent {
book.exthHeader, err = headers.ReadEXTH(book.file)
if err != nil {
return nil, fmt.Errorf("unable to read EXTH header: %v", err)
}
}
return &book, nil
}
func (mobiFile mobiBook) Cover() ([]byte, error) {
for _, r := range mobiFile.exthHeader.Records {
if r.RecordType == 201 {
coverIndex := mobiFile.mobiHeader.FirstImageIndex + convert.FromUint32(r.RecordData)
record := mobiFile.pdbHeader.Records[coverIndex]
nextRecord := mobiFile.pdbHeader.Records[coverIndex+1]
coverOffset := record.RecordDataOffset
coverSize := nextRecord.RecordDataOffset - coverOffset
_, err := mobiFile.file.Seek(int64(coverOffset), 0)
if err != nil {
return nil, fmt.Errorf("unable to find cover: %v", err)
}
cover := make([]byte, coverSize)
err = binary.Read(mobiFile.file, binary.BigEndian, &cover)
if err != nil {
return nil, fmt.Errorf("unable to read cover: %v", err)
}
return cover, nil
}
}
return nil, nil
}
func (mobiFile mobiBook) Markup() (string, error) {
startIndex := mobiFile.mobiHeader.FirstContentIndex
endIndex := mobiFile.mobiHeader.FirstNonBookIndex - 1
if endIndex > len(mobiFile.pdbHeader.Records)-2 {
endIndex = len(mobiFile.pdbHeader.Records) - 2
}
if endIndex < 0 || startIndex < 0 || startIndex >= len(mobiFile.pdbHeader.Records) {
return "", fmt.Errorf("Invalid header")
}
text := make([]byte, 0)
for index := startIndex; index <= endIndex; index++ {
record := mobiFile.pdbHeader.Records[index]
nextRecord := mobiFile.pdbHeader.Records[index+1]
recordOffset := record.RecordDataOffset
recordSize := nextRecord.RecordDataOffset - recordOffset
_, err := mobiFile.file.Seek(int64(recordOffset), 0)
if err != nil {
return "", fmt.Errorf("unable to find text: %v", err)
}
recordData := make([]byte, recordSize)
err = binary.Read(mobiFile.file, binary.BigEndian, &recordData)
if err != nil {
return "", fmt.Errorf("unable to read text: %v", err)
}
recordText := fromLZ77(recordData)
text = append(text, recordText...)
}
text = text[:mobiFile.palmDOCHeader.TextLength]
if !utf8.Valid(text) {
return "", errors.New("unable to decompress text")
}
return string(text), nil
}
func (mobiFile mobiBook) Text() (string, error) {
markup, err := mobiFile.Markup()
if err != nil {
return "", fmt.Errorf("unable to read markup: %v", err)
}
pos, err := getTOCPosition(markup)
if err != nil {
return "", fmt.Errorf("unable to locate TOC: %v", err)
}
bookmarks, err := parseTOC(markup[pos:])
text := make([]string, 0)
for i := range bookmarks {
start := bookmarks[i]
var end int
if i < len(bookmarks)-1 {
end = bookmarks[i+1]
} else {
end = pos
}
paragraphs, err := parseChapter(markup[start:end])
if err != nil {
return "", fmt.Errorf("unable to parse chapter: %v", err)
}
text = append(text, paragraphs...)
}
return strings.Join(text, "\n\n"), nil
}
func getTOCPosition(markup string) (int, error) {
htmlReader := strings.NewReader(markup)
tokenizer := html.NewTokenizer(htmlReader)
for {
tokenType := tokenizer.Next()
switch {
case tokenType == html.ErrorToken:
return 0, fmt.Errorf("unable to find reference element")
case tokenType == html.SelfClosingTagToken:
token := tokenizer.Token()
if token.Data == "reference" {
filepos, err := attr(token, "filepos")
if err != nil {
return 0, errors.New("filepos attribute missing")
}
pos, err := strconv.Atoi(filepos)
if err != nil {
return 0, errors.New("filepos attribute invalid")
}
return pos, nil
}
}
}
}
func parseTOC(markup string) ([]int, error) {
toc := make([]int, 0)
htmlReader := strings.NewReader(markup)
tokenizer := html.NewTokenizer(htmlReader)
for {
tokenType := tokenizer.Next()
switch {
case tokenType == html.ErrorToken:
return toc[1:], nil
case tokenType == html.StartTagToken:
token := tokenizer.Token()
if token.Data == "a" {
filepos, err := attr(token, "filepos")
if err != nil {
continue
}
pos, err := strconv.Atoi(filepos)
if err != nil {
return nil, errors.New("filepos attribute invalid")
}
toc = append(toc, pos)
}
}
}
}
func parseChapter(markup string) ([]string, error) {
paragraphs := make([]string, 0)
htmlReader := strings.NewReader(markup)
tokenizer := html.NewTokenizer(htmlReader)
for {
tokenType := tokenizer.Next()
switch {
case tokenType == html.ErrorToken:
return paragraphs, nil
case tokenType == html.TextToken:
token := tokenizer.Token()
if len(strings.TrimSpace(token.Data)) > 0 {
paragraphs = append(paragraphs, strings.TrimSpace(token.Data))
}
}
}
}
func attr(t html.Token, name string) (string, error) {
for _, a := range t.Attr {
if a.Key == name {
return a.Val, nil
}
}
return "", fmt.Errorf("attribute %v not found", name)
}
// fromLZ77 is forked from conversion.go because of index out of range panic
func fromLZ77(text []byte) []byte {
var reader = bytes.NewReader(text)
var buffer [4096]byte
var pos int
for {
if pos == 4096 {
break
}
c, err := reader.ReadByte()
if err == io.EOF {
break
}
switch {
// 0x00: "1 literal" copy that byte unmodified to the decompressed stream.
case c == 0x00:
buffer[pos] = c
pos++
// 0x09 to 0x7f: "1 literal" copy that byte unmodified to the decompressed stream.
case c >= 0x09 && c <= 0x7f:
buffer[pos] = c
pos++
// 0x01 to 0x08: "literals": the byte is interpreted as a count from 1 to 8, and that many literals are copied
// unmodified from the compressed stream to the decompressed stream.
case c >= 0x01 && c <= 0x08:
length := int(c)
for i := 0; i < length; i++ {
c, err = reader.ReadByte()
buffer[pos] = c
pos++
}
// 0x80 to 0xbf: "length, distance" pair: the 2 leftmost bits of this byte ('10') are discarded, and the
// following 6 bits are combined with the 8 bits of the next byte to make a 14 bit "distance, length" item.
// Those 14 bits are broken into 11 bits of distance backwards from the current location in the uncompressed
// text, and 3 bits of length to copy from that point (copying n+3 bytes, 3 to 10 bytes).
case c >= 0x80 && c <= 0xbf:
c2, _ := reader.ReadByte()
distance := (int(c&0x3F)<<8 | int(c2)) >> 3
length := int(c2&0x07) + 3
start := pos - distance
for i := 0; i < length; i++ {
// check if index is in range
if start+i >= len(buffer) || start+i < 0 {
return buffer[:pos]
}
c = buffer[start+i]
buffer[pos] = c
pos++
}
// 0xc0 to 0xff: "byte pair": this byte is decoded into 2 characters: a space character, and a letter formed
// from this byte XORed with 0x80.
case c >= 0xc0:
buffer[pos] = ' '
pos++
buffer[pos] = c ^ 0x80
pos++
}
}
return buffer[:pos]
}
// IsFileMOBI checks if the data indicates a MOBI file
func IsFileMOBI(data []byte) bool {
// Mobi files have a header and there is the signature "BOOKMOBI" or "TEXtREAd".
// There are many more more potential signatures https://sno.phy.queensu.ca/~phil/exiftool/TagNames/Palm.html
// Fork from code here http://will.tip.dhappy.org/lib/calibre/dedrm/mobidedrm.py
// if self.header[0x3C:0x3C+8] != 'BOOKMOBI' and self.header[0x3C:0x3C+8] != 'TEXtREAd':
// raise DrmException(u"Invalid file format")
if len(data) < 0x3C+8 {
return false
}
signature := data[0x3C : 0x3C+8]
return bytes.Equal(signature, []byte("BOOKMOBI")) || bytes.Equal(signature, []byte("TEXtREAd"))
}