-
Notifications
You must be signed in to change notification settings - Fork 0
/
epub.sh
executable file
·88 lines (71 loc) · 3.06 KB
/
epub.sh
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
#!/opt/homebrew/bin/bash -
shopt -s nullglob
shopt -s extglob
shopt -s globstar
cd "$(dirname "$0")"
if [ ! -f rsa.key ]; then
exit 1
fi
mkdir -p books
cd books
for bookzip in *.zip; do
book="$(basename "$bookzip" .zip)"
if [ ! -d "$book" ]; then
mkdir -p "$book"
unzip "$bookzip" -d "$book"
rm "$bookzip"
fi
if [ ! -f "$book"/META-INF/encryption.xml ]; then
continue
fi
pushd "$book"
[ -d moo_extra/ ] && rm -r moo_extra/
ek="$(base64 -d -i ../"$book".key | openssl pkeyutl -decrypt -inkey "$(dirname "$0")"/rsa.key | xxd -p -c 256)"
opf="$(xmllint --xpath 'string(//*[name()="rootfile"]/@full-path)' META-INF/container.xml)"
bookid="$(xmllint --xpath 'string(//*[name()="dc:identifier"][@id="'"$(xmllint --xpath 'string(/*[name()="package"]/@unique-identifier)' "$opf")"'"])' "$opf")"
title="$(xmllint --xpath 'string(//*[name()="dc:title"])' "$opf")"
i=0
while true; do
i=$((i + 1))
xml="$(xmllint --xpath '//*[name()="EncryptedData"]['"$i"']' META-INF/encryption.xml 2>/dev/null)"
[ "$xml" ] || break
file="$(xmllint --xpath 'string(//*[name()="CipherReference"]/@URI)' - <<<"$xml")"
file="$(printf '%b' "${file//%/\\x}")"
[ ! -f "$file" ] && continue
echo ... "$file"
algorithm="$(xmllint --xpath 'string(//*[name()="EncryptionMethod"]/@Algorithm)' - <<<"$xml")"
if [ "$algorithm" = 'http://www.idpf.org/2008/embedding' ]; then
"$(dirname "$0")"/embedding.py "$file" "$bookid" >"$file".plain
mv "$file".plain "$file"
else
method="$(xmllint --xpath 'string(//*[name()="Compression"]/@Method)' - <<<"$xml")"
length="$(xmllint --xpath 'string(//*[name()="Compression"]/@OriginalLength)' - <<<"$xml")"
iv="$(head -c16 "$file" | xxd -p)"
tail -c+17 "$file" | openssl enc -aes256 -d -K "$ek" -iv "$iv" -nopad -out "$file".plain
if [ "$method" -eq 8 ]; then
cat <(printf "\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x00") "$file".plain | gzip -dc >"$file" 2>/dev/null
size="$(stat -c %s "$file")"
if [ "$size" != "$length" ]; then
echo "$size" != "$length"
exit 1
fi
else
head -n"$length" "$file".plain >"$file"
fi
rm "$file".plain
fi
done
sed -E -i -e 's/ class="moofs([0-9]+|NaN) moolh([0-9]+|NaN)( non-moofont)?"//g' **/*.xhtml
sed -E -i -e 's/ class="(([^"]+) )?moofs([0-9]+|NaN) moolh([0-9]+|NaN)( non-moofont)?"/ class="\2"/g' **/*.xhtml
sed -E -i -e 's/<meta name="moo_white_margins" content="[^"]+"\/>//g' **/*.xhtml
sed -E -i -e 's/<meta content="Bisheng [0-9.]+" name="moo[^"]+"\/>//' "$opf"
if [ -d moo_extra ]; then
sed -E -i -e 's/<item href="\.\.\/moo_extra\/[^>]+\/>//g' "$opf"
rm -r moo_extra
fi
rm META-INF/encryption.xml
rm ../"$book".key
zip -9 -X -r ../"$book"\ "${title//\//_}".epub mimetype !(mimetype)
popd
rm -r "$book"
done