-
Notifications
You must be signed in to change notification settings - Fork 589
/
Copy pathepub_utils.py
178 lines (150 loc) · 6.59 KB
/
epub_utils.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab
# Copyright (c) 2014-2020 Kevin B. Hendricks, and Doug Massay
# Copyright (c) 2014 Kevin B. Hendricks, John Schember, and Doug Massay
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of
# conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list
# of conditions and the following disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
# SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
# WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import os
import binascii
import re
from itertools import cycle
import zipfile
from zipfile import ZipFile
import hashlib
def SHA1(message):
ctx = hashlib.sha1()
ctx.update(message)
return ctx.digest()
epub_mimetype = b'application/epub+zip'
def utf8str(s):
if s is None:
return None
if isinstance(s, bytes):
return s
return s.encode('utf-8', errors='replace')
def epub_file_walk(top):
top = os.fsdecode(top)
rv = []
for base, dnames, names in os.walk(top):
for name in names:
rv.append(os.path.relpath(os.path.join(base, name), top))
return rv
def unzip_epub_to_dir(path_to_epub, destdir):
f = open(os.fsdecode(path_to_epub), 'rb')
sz = ZipFile(f)
for name in sz.namelist():
data = sz.read(name)
name = name.replace("/", os.sep)
filepath = os.path.join(destdir, name)
basedir = os.path.dirname(filepath)
if not os.path.isdir(basedir):
os.makedirs(basedir)
with open(filepath, 'wb') as fp:
fp.write(data)
f.close()
def epub_zip_up_book_contents(ebook_path, epub_filepath):
outzip = zipfile.ZipFile(os.fsdecode(epub_filepath), 'w')
book_path = os.fsdecode(ebook_path)
files = epub_file_walk(book_path)
if 'mimetype' in files:
outzip.write(os.path.join(book_path, 'mimetype'), 'mimetype', zipfile.ZIP_STORED)
else:
raise Exception('mimetype file is missing')
files.remove('mimetype')
for file in files:
filepath = os.path.join(book_path, file)
outzip.write(filepath, file, zipfile.ZIP_DEFLATED)
outzip.close()
def build_container_xml(bookpath_to_opf):
opf_path = os.fsdecode(bookpath_to_opf)
container = '<?xml version="1.0" encoding="UTF-8"?>\n'
container += '<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">\n'
container += ' <rootfiles>\n'
container += '<rootfile full-path="%s" media-type="application/oebps-package+xml"/>' % opf_path
container += ' </rootfiles>\n</container>\n'
return container
# mangled_fonts is a list of the book paths to the font files to be (de)mangled
def build_adobe_encryption_xml(mangled_fonts):
encryption = ''
if mangled_fonts is not None and len(mangled_fonts) > 0:
encryption = '<encryption xmlns="urn:oasis:names:tc:opendocument:xmlns:container" xmlns:enc="http://www.w3.org/2001/04/xmlenc#" xmlns:deenc="http://ns.adobe.com/digitaleditions/enc">\n'
for fontbkpath in mangled_fonts:
encryption += ' <enc:EncryptedData>\n'
encryption += ' <enc:EncryptionMethod Algorithm="http://ns.adobe.com/pdf/enc#RC"/>\n'
encryption += ' <enc:CipherData>\n'
encryption += ' <enc:CipherReference URI="' + fontbkpath + '"/>\n'
encryption += ' </enc:CipherData>\n'
encryption += ' </enc:EncryptedData>\n'
encryption += '</encryption>\n'
return encryption
# mangled_fonts is a list of book paths to the font files to be (de)mangled
def build_idpf_encryption_xml(mangled_fonts):
encryption = ''
if mangled_fonts is not None and len(mangled_fonts) > 0:
encryption = '<encryption xmlns="urn:oasis:names:tc:opendocument:xmlns:container" xmlns:enc="http://www.w3.org/2001/04/xmlenc#">\n'
for fontbkpath in mangled_fonts:
encryption += ' <enc:EncryptedData>\n'
encryption += ' <enc:EncryptionMethod Algorithm="http://www.idpf.org/2008/embedding"/>\n'
encryption += ' <enc:CipherData>\n'
encryption += ' <enc:CipherReference URI="' + fontbkpath + '"/>\n'
encryption += ' </enc:CipherData>\n'
encryption += ' </enc:EncryptedData>\n'
encryption += '</encryption>\n'
return encryption
def Adobe_encryption_key(uid):
# strip it down to simple valid hex characters
# being careful to generate a string of 16 bytes in length
key = utf8str(uid)
if key.startswith(b"urn:uuid:"):
key = key[9:]
key = key.replace(b'-', b'')
key = re.sub(r'[^a-fA-F0-9]', b'', key)
key = binascii.unhexlify((key + key)[:32])
return key
def Idpf_encryption_key(uid):
# remove whitespace changing nothing else
key = utf8str(uid)
key = key.replace(bytes([0x20]), b'')
key = key.replace(bytes([0x09]), b'')
key = key.replace(bytes([0x0d]), b'')
key = key.replace(bytes([0x0a]), b'')
key = SHA1(key)
return key
def Adobe_mangle_fonts(encryption_key, data):
def bord(s):
return s
if not isinstance(data, bytes):
print('Error: font data must be a byte string')
crypt = data[:1024]
key = cycle(iter(map(bord, encryption_key)))
encrypt = b''.join([bytes([x ^ next(key)]) for x in crypt])
return encrypt + data[1024:]
def Idpf_mangle_fonts(encryption_key, data):
def bord(s):
return s
if not isinstance(data, bytes):
print('Error: font data must be a byte string')
crypt = data[:1040]
key = cycle(iter(map(bord, encryption_key)))
encrypt = b''.join([bytes([x ^ next(key)]) for x in crypt])
return encrypt + data[1040:]