diff --git a/Adobe_EPUB_Tools/README_ineptepub.txt b/Adobe_EPUB_Tools/README_ineptepub.txt
index d764357..25813a4 100644
--- a/Adobe_EPUB_Tools/README_ineptepub.txt
+++ b/Adobe_EPUB_Tools/README_ineptepub.txt
@@ -1,6 +1,6 @@
From Apprentice Alf's Blog
-Adobe Adept ePub and PDF, .epub, .pdf
+Adobe Adept ePub, .epub
This directory includes modified versions of the I♥CABBAGES Adobe Adept inept scripts for epubs. These scripts have been modified to work with OpenSSL on Windows as well as Linux and Mac OS X. His original scripts can be found in the clearly labelled folder. If a Windows User has OpenSSL installed, these scripts will make use of it in place of PyCrypto.
@@ -11,20 +11,8 @@ http://i-u2665-cabbages.blogspot.com/2009_02_01_archive.html
There are two scripts:
-The first is called ineptkey_v5.1.pyw. Simply double-click to launch it and it will create a key file that is needed later to actually remove the DRM. This script need only be run once unless you change your ADE account information.
+The first is called ineptkey_vX.X.pyw. Simply double-click to launch it and it will create a key file that is needed later to actually remove the DRM. This script need only be run once unless you change your ADE account information.
-The second is called in ineptepub_v5.3.pyw. Simply double-click to launch it. It will ask for your previously generated key file and the path to the book you want to remove the DRM from.
+The second is called in ineptepub_vX.X.pyw. Simply double-click to launch it. It will ask for your previously generated key file and the path to the book you want to remove the DRM from.
Both of these scripts are gui python programs. Python 2.X (32 bit) is already installed in Mac OSX. We recommend ActiveState's Active Python Version 2.X (32 bit) for Windows users.
-
-The latest version of ineptpdf to use is version 8.4.42, which improves support for some PDF files.
-
-ineptpdf version 8.4.42 can be found here:
-
-http://pastebin.com/kuKMXXsC
-
-It is not included in the tools archive.
-
-If that link is down, please check out the following website for some of the latest releases of these tools:
-
-http://ainept.freewebspace.com/
diff --git a/Adobe_EPUB_Tools/ineptepub.pyw b/Adobe_EPUB_Tools/ineptepub.pyw
index 442c37a..9d95720 100644
--- a/Adobe_EPUB_Tools/ineptepub.pyw
+++ b/Adobe_EPUB_Tools/ineptepub.pyw
@@ -1,7 +1,7 @@
#! /usr/bin/python
# -*- coding: utf-8 -*-
-# ineptepub.pyw, version 5.4
+# ineptepub.pyw, version 5.5
# Copyright © 2009-2010 i♥cabbages
# Released under the terms of the GNU General Public Licence, version 3 or
@@ -26,6 +26,7 @@
# 5.2 - Fix ctypes error causing segfaults on some systems
# 5.3 - add support for OpenSSL on Windows, fix bug with some versions of libcrypto 0.9.8 prior to path level o
# 5.4 - add support for encoding to 'utf-8' when building up list of files to decrypt from encryption.xml
+# 5.5 - On Windows try PyCrypto first, OpenSSL next
"""
Decrypt Adobe ADEPT-encrypted EPUB books.
@@ -259,7 +260,10 @@ def _load_crypto_pycrypto():
def _load_crypto():
AES = RSA = None
- for loader in (_load_crypto_libcrypto, _load_crypto_pycrypto):
+ cryptolist = (_load_crypto_libcrypto, _load_crypto_pycrypto)
+ if sys.platform.startswith('win'):
+ cryptolist = (_load_crypto_pycrypto, _load_crypto_libcrypto)
+ for loader in cryptolist:
try:
AES, RSA = loader()
break
diff --git a/Adobe_EPUB_Tools/ineptkey.pyw b/Adobe_EPUB_Tools/ineptkey.pyw
index bd66e78..fd90508 100644
--- a/Adobe_EPUB_Tools/ineptkey.pyw
+++ b/Adobe_EPUB_Tools/ineptkey.pyw
@@ -1,7 +1,7 @@
#! /usr/bin/python
# -*- coding: utf-8 -*-
-# ineptkey.pyw, version 5
+# ineptkey.pyw, version 5.3
# Copyright © 2009-2010 i♥cabbages
# Released under the terms of the GNU General Public Licence, version 3 or
@@ -32,6 +32,7 @@
# Clean up and merge OS X support by unknown
# 5.1 - add support for using OpenSSL on Windows in place of PyCrypto
# 5.2 - added support for output of key to a particular file
+# 5.3 - On Windows try PyCrypto first, OpenSSL next
"""
Retrieve Adobe ADEPT user key.
@@ -115,7 +116,7 @@ if sys.platform.startswith('win'):
def _load_crypto():
AES = None
- for loader in (_load_crypto_libcrypto, _load_crypto_pycrypto):
+ for loader in (_load_crypto_pycrypto, _load_crypto_libcrypto):
try:
AES = loader()
break
diff --git a/Adobe_PDF_Tools/README_ineptpdf.txt b/Adobe_PDF_Tools/README_ineptpdf.txt
new file mode 100644
index 0000000..2b03d83
--- /dev/null
+++ b/Adobe_PDF_Tools/README_ineptpdf.txt
@@ -0,0 +1,18 @@
+From Apprentice Alf's Blog
+
+Adobe Adept PDF, .pdf
+
+This directory includes modified versions of the I♥CABBAGES Adobe Adept inept scripts for pdfs. These scripts have been modified to work with OpenSSL on Windows as well as Linux and Mac OS X. If a Windows User has OpenSSL installed, these scripts will make use of it in place of PyCrypto.
+
+The wonderful I♥CABBAGES has produced scripts that will remove the DRM from ePubs and PDFs encryped with Adobe’s DRM. These scripts require installation of the PyCrypto python package *or* the OpenSSL library on Windows. For Mac OS X and Linux boxes, these scripts use the already installed OpenSSL libcrypto so there is no additional requirements for these platforms.
+
+For more info, see the author's blog:
+http://i-u2665-cabbages.blogspot.com/2009_02_01_archive.html
+
+There are two scripts:
+
+The first is called ineptkey_vX.X.pyw. Simply double-click to launch it and it will create a key file that is needed later to actually remove the DRM. This script need only be run once unless you change your ADE account information.
+
+The second is called in ineptpdf_vX.X.pyw. Simply double-click to launch it. It will ask for your previously generated key file and the path to the book you want to remove the DRM from.
+
+Both of these scripts are gui python programs. Python 2.X (32 bit) is already installed in Mac OSX. We recommend ActiveState's Active Python Version 2.X (32 bit) for Windows users.
diff --git a/Adobe_PDF_Tools/ineptkey.pyw b/Adobe_PDF_Tools/ineptkey.pyw
index bd66e78..fd90508 100644
--- a/Adobe_PDF_Tools/ineptkey.pyw
+++ b/Adobe_PDF_Tools/ineptkey.pyw
@@ -1,7 +1,7 @@
#! /usr/bin/python
# -*- coding: utf-8 -*-
-# ineptkey.pyw, version 5
+# ineptkey.pyw, version 5.3
# Copyright © 2009-2010 i♥cabbages
# Released under the terms of the GNU General Public Licence, version 3 or
@@ -32,6 +32,7 @@
# Clean up and merge OS X support by unknown
# 5.1 - add support for using OpenSSL on Windows in place of PyCrypto
# 5.2 - added support for output of key to a particular file
+# 5.3 - On Windows try PyCrypto first, OpenSSL next
"""
Retrieve Adobe ADEPT user key.
@@ -115,7 +116,7 @@ if sys.platform.startswith('win'):
def _load_crypto():
AES = None
- for loader in (_load_crypto_libcrypto, _load_crypto_pycrypto):
+ for loader in (_load_crypto_pycrypto, _load_crypto_libcrypto):
try:
AES = loader()
break
diff --git a/Adobe_PDF_Tools/ineptpdf.pyw b/Adobe_PDF_Tools/ineptpdf.pyw
index b896f01..d73e069 100644
--- a/Adobe_PDF_Tools/ineptpdf.pyw
+++ b/Adobe_PDF_Tools/ineptpdf.pyw
@@ -1,5 +1,5 @@
#! /usr/bin/env python
-# ineptpdf.pyw, version 7.6
+# ineptpdf.pyw, version 7.7
# To run this program install Python 2.6 from http://www.python.org/download/
# and OpenSSL (already installed on Mac OS X and Linux) OR
@@ -29,6 +29,7 @@
# implemented ARC4 interface to OpenSSL
# fixed minor typos
# 7.6 - backported AES and other fixes from version 8.4.48
+# 7.7 - On Windows try PyCrypto first and OpenSSL next
"""
Decrypts Adobe ADEPT-encrypted PDF files.
@@ -319,7 +320,10 @@ def _load_crypto_pycrypto():
def _load_crypto():
ARC4 = RSA = AES = None
- for loader in (_load_crypto_libcrypto, _load_crypto_pycrypto):
+ cryptolist = (_load_crypto_libcrypto, _load_crypto_pycrypto)
+ if sys.platform.startswith('win'):
+ cryptolist = (_load_crypto_pycrypto, _load_crypto_libcrypto)
+ for loader in cryptolist:
try:
ARC4, RSA, AES = loader()
break
diff --git a/Barnes_and_Noble_EPUB_Tools/README_ignoble_epub.txt b/Barnes_and_Noble_EPUB_Tools/README_ignoble_epub.txt
index afcbfe3..ae07006 100644
--- a/Barnes_and_Noble_EPUB_Tools/README_ignoble_epub.txt
+++ b/Barnes_and_Noble_EPUB_Tools/README_ignoble_epub.txt
@@ -5,13 +5,13 @@ Barnes and Noble EPUB ebooks use a form of Social DRM which requires information
For more info, see the author's blog:
http://i-u2665-cabbages.blogspot.com/2009_12_01_archive.html
-The original scripts by IHeartCabbages are available here as well. These scripts have been modified to allow the use of OpenSSL in place of PyCrypto to make them easier to run on Linux and Mac OS X.
+The original scripts by IHeartCabbages are available here as well. These scripts have been modified to allow the use of OpenSSL in place of PyCrypto to make them easier to run on Linux and Mac OS X, as well as to fix some minor bugs/
There are 2 scripts:
-The first is ignoblekeygen_v2.pyw. Double-click to launch it and provide the required information, and this program will generate a key file needed to remove the DRM from the books. This key file need only be generated once unless either you change your credit card number or your name on the credit card (or if you use a different credit card to purchase your book).
+The first is ignoblekeygen_vX.X.pyw. Double-click to launch it and provide the required information, and this program will generate a key file needed to remove the DRM from the books. This key file need only be generated once unless either you change your credit card number or your name on the credit card (or if you use a different credit card to purchase your book).
-The second is ignobleepub_v3.pyw. Double-click it and it will ask for your key file and the path to the book to remove the DRM from.
+The second is ignobleepub_vX.X.pyw. Double-click it and it will ask for your key file and the path to the book to remove the DRM from.
All of these scripts are gui python programs. Python 2.X (32 bit) is already installed in Mac OSX. We recommend ActiveState's Active Python Version 2.X (32 bit) for Windows users.
diff --git a/Barnes_and_Noble_EPUB_Tools/ignobleepub.pyw b/Barnes_and_Noble_EPUB_Tools/ignobleepub.pyw
index 469713a..0afc2bc 100644
--- a/Barnes_and_Noble_EPUB_Tools/ignobleepub.pyw
+++ b/Barnes_and_Noble_EPUB_Tools/ignobleepub.pyw
@@ -1,6 +1,6 @@
#! /usr/bin/python
-# ignobleepub.pyw, version 3
+# ignobleepub.pyw, version 3.3
# To run this program install Python 2.6 from
# and OpenSSL or PyCrypto from http://www.voidspace.org.uk/python/modules.shtml#pycrypto
@@ -12,6 +12,9 @@
# 2 - Added OS X support by using OpenSSL when available
# 3 - screen out improper key lengths to prevent segfaults on Linux
# 3.1 - Allow Windows versions of libcrypto to be found
+# 3.2 - add support for encoding to 'utf-8' when building up list of files to cecrypt from encryption.xml
+# 3.3 - On Windows try PyCrypto first and OpenSSL next
+
from __future__ import with_statement
@@ -105,15 +108,18 @@ def _load_crypto_pycrypto():
def _load_crypto():
AES = None
- for loader in (_load_crypto_libcrypto, _load_crypto_pycrypto):
+ cryptolist = (_load_crypto_libcrypto, _load_crypto_pycrypto)
+ if sys.platform.startswith('win'):
+ cryptolist = (_load_crypto_pycrypto, _load_crypto_libcrypto)
+ for loader in cryptolist:
try:
AES = loader()
break
except (ImportError, IGNOBLEError):
pass
return AES
-AES = _load_crypto()
+AES = _load_crypto()
diff --git a/Barnes_and_Noble_EPUB_Tools/ignoblekeygen.pyw b/Barnes_and_Noble_EPUB_Tools/ignoblekeygen.pyw
index 479c11d..b2607ea 100644
--- a/Barnes_and_Noble_EPUB_Tools/ignoblekeygen.pyw
+++ b/Barnes_and_Noble_EPUB_Tools/ignoblekeygen.pyw
@@ -1,6 +1,6 @@
#! /usr/bin/python
-# ignoblekeygen.pyw, version 2
+# ignoblekeygen.pyw, version 2.2
# To run this program install Python 2.6 from
# and OpenSSL or PyCrypto from http://www.voidspace.org.uk/python/modules.shtml#pycrypto
@@ -11,7 +11,7 @@
# 1 - Initial release
# 2 - Add OS X support by using OpenSSL when available (taken/modified from ineptepub v5)
# 2.1 - Allow Windows versions of libcrypto to be found
-
+# 2.2 - On Windows try PyCrypto first and then OpenSSL next
"""
Generate Barnes & Noble EPUB user key from name and credit card number.
"""
@@ -102,11 +102,12 @@ def _load_crypto_pycrypto():
return AES
-
-
def _load_crypto():
AES = None
- for loader in (_load_crypto_libcrypto, _load_crypto_pycrypto):
+ cryptolist = (_load_crypto_libcrypto, _load_crypto_pycrypto)
+ if sys.platform.startswith('win'):
+ cryptolist = (_load_crypto_pycrypto, _load_crypto_libcrypto)
+ for loader in cryptolist:
try:
AES = loader()
break
diff --git a/Calibre_Plugins/K4MobiDeDRM_plugin/kgenpids.py b/Calibre_Plugins/K4MobiDeDRM_plugin/kgenpids.py
index 5c44bfa..6dcbf73 100644
--- a/Calibre_Plugins/K4MobiDeDRM_plugin/kgenpids.py
+++ b/Calibre_Plugins/K4MobiDeDRM_plugin/kgenpids.py
@@ -83,7 +83,8 @@ def parseKindleInfo(kInfoFile):
DB[splito[0]] =splito[1]
return DB
-# Get a record from the Kindle.info file for the key "hashedKey" (already hashed and encoded). Return the decoded and decrypted record
+# Get a record from the Kindle.info file for the key "hashedKey" (already hashed and encoded).
+# Return the decoded and decrypted record
def getKindleInfoValueForHash(hashedKey):
global kindleDatabase
global charMap1
@@ -95,12 +96,14 @@ def getKindleInfoValueForHash(hashedKey):
cleartext = CryptUnprotectData(encryptedValue)
return decode(cleartext, charMap1)
-# Get a record from the Kindle.info file for the string in "key" (plaintext). Return the decoded and decrypted record
+# Get a record from the Kindle.info file for the string in "key" (plaintext).
+# Return the decoded and decrypted record
def getKindleInfoValueForKey(key):
global charMap2
return getKindleInfoValueForHash(encodeHash(key,charMap2))
-# Find if the original string for a hashed/encoded string is known. If so return the original string othwise return an empty string.
+# Find if the original string for a hashed/encoded string is known.
+# If so return the original string othwise return an empty string.
def findNameForHash(hash):
global charMap2
names = ["kindle.account.tokens","kindle.cookie.item","eulaVersionAccepted","login_date","kindle.token.item","login","kindle.key.item","kindle.name.info","kindle.device.info", "MazamaRandomNumber"]
@@ -222,7 +225,7 @@ def pidFromSerial(s, l):
# Parse the EXTH header records and use the Kindle serial number to calculate the book pid.
def getKindlePid(pidlst, rec209, token, serialnum):
- if rec209 != None:
+ if rec209 != None and token != None:
# Compute book PID
pidHash = SHA1(serialnum+rec209+token)
bookPID = encodePID(pidHash)
@@ -248,6 +251,7 @@ def getK4Pids(pidlst, rec209, token, kInfoFile=None):
kindleDatabase = parseKindleInfo(kInfoFile)
except Exception, message:
print(message)
+ kindleDatabase = None
pass
if kindleDatabase == None :
@@ -272,8 +276,8 @@ def getK4Pids(pidlst, rec209, token, kInfoFile=None):
pidlst.append(devicePID)
# Compute book PID
- if rec209 == None:
- print "\nNo EXTH record type 209 - Perhaps not a K4 file?"
+ if rec209 == None or token == None:
+ print "\nNo EXTH record type 209 or token - Perhaps not a K4 file?"
return pidlst
# Get the kindle account token
diff --git a/Calibre_Plugins/K4MobiDeDRM_plugin/topazextract.py b/Calibre_Plugins/K4MobiDeDRM_plugin/topazextract.py
index e371d76..732bbae 100644
--- a/Calibre_Plugins/K4MobiDeDRM_plugin/topazextract.py
+++ b/Calibre_Plugins/K4MobiDeDRM_plugin/topazextract.py
@@ -164,9 +164,10 @@ class TopazBook:
def getPIDMetaInfo(self):
keysRecord = None
- KeysRecordRecord = None
+ keysRecordRecord = None
if 'keys' in self.bookMetadata:
keysRecord = self.bookMetadata['keys']
+ if keysRecord in self.bookMetadata:
keysRecordRecord = self.bookMetadata[keysRecord]
return keysRecord, keysRecordRecord
@@ -395,6 +396,7 @@ def main(argv=sys.argv):
myzip = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
zipUpDir(myzip, tempdir, '')
myzip.close()
+ shutil.rmtree(tempdir, True)
return 1
print " Creating HTML ZIP Archive"
@@ -424,7 +426,7 @@ def main(argv=sys.argv):
zipUpDir(myzip3, tempdir, 'img')
myzip3.close()
- shutil.rmtree(tempdir)
+ shutil.rmtree(tempdir, True)
return 0
diff --git a/Calibre_Plugins/README-Ineptpdf-plugin.txt b/Calibre_Plugins/README-Ineptpdf-plugin.txt
index 13c938b..4d668fc 100644
--- a/Calibre_Plugins/README-Ineptpdf-plugin.txt
+++ b/Calibre_Plugins/README-Ineptpdf-plugin.txt
@@ -10,6 +10,9 @@ Installation:
Go to Calibre's Preferences page... click on the Plugins button. Use the file dialog button to select the plugin's zip file (ineptpdf_vXX_plugin.zip) and click the 'Add' button. you're done.
+Please note: Calibre does not provide any immediate feedback to indicate that adding the plugin was a success. You can always click on the File-Type plugins to see if the plugin was added.
+
+
Configuration:
When first run, the plugin will attempt to find your Adobe Digital Editions installation (on Windows and Mac OS's). If successful, it will create an 'adeptkey.der' file and save it in Calibre's configuration directory. It will use that file on subsequent runs. If there are already '*.der' files in the directory, the plugin won't attempt to
diff --git a/Calibre_Plugins/README-K4MobiDeDRM-plugin.txt b/Calibre_Plugins/README-K4MobiDeDRM-plugin.txt
index ee9dc06..9d392f3 100644
--- a/Calibre_Plugins/README-K4MobiDeDRM-plugin.txt
+++ b/Calibre_Plugins/README-K4MobiDeDRM-plugin.txt
@@ -7,6 +7,8 @@ This plugin is meant to remove the DRM from .prc, .azw, .azw1, and .tpz ebooks.
Installation:
Go to Calibre's Preferences page... click on the Plugins button. Use the file dialog button to select the plugin's zip file (K4MobiDeDRM_vXX_plugin.zip) and click the 'Add' button. You're done.
+Please note: Calibre does not provide any immediate feedback to indicate that adding the plugin was a success. You can always click on the File-Type plugins to see if the plugin was added.
+
Configuration:
Highlight the plugin (K4MobiDeDRM under the "File type plugins" category) and click the "Customize Plugin" button on Calibre's Preferences->Plugins page. Enter a comma separated list of your 10 digit PIDs. Include in this list (again separated by commas) any 16 digit serial numbers the standalone Kindles you may have (these typically begin "B0...") This is not needed if you only want to decode "Kindle for PC" or "Kindle for Mac" books.
diff --git a/Calibre_Plugins/README-eReaderPDB2PML-plugin.txt b/Calibre_Plugins/README-eReaderPDB2PML-plugin.txt
index 75dfda5..573f8ec 100644
--- a/Calibre_Plugins/README-eReaderPDB2PML-plugin.txt
+++ b/Calibre_Plugins/README-eReaderPDB2PML-plugin.txt
@@ -7,6 +7,8 @@ This plugin is meant to convert secure Ereader files (PDB) to unsecured PMLZ fil
Installation:
Go to Calibre's Preferences page... click on the Plugins button. Use the file dialog button to select the plugin's zip file (eReaderPDB2PML_vXX_plugin.zip) and click the 'Add' button. You're done.
+Please note: Calibre does not provide any immediate feedback to indicate that adding the plugin was a success. You can always click on the File-Type plugins to see if the plugin was added.
+
Configuration:
Highlight the plugin (eReader PDB 2 PML under the "File type plugins" category) and click the "Customize Plugin" button on Calibre's Preferences->Plugins page. Enter your name and last 8 digits of the credit card number separated by a comma: Your Name,12341234
diff --git a/Calibre_Plugins/README-ignobleepub-plugin.txt b/Calibre_Plugins/README-ignobleepub-plugin.txt
index 6fd92cd..15de927 100644
--- a/Calibre_Plugins/README-ignobleepub-plugin.txt
+++ b/Calibre_Plugins/README-ignobleepub-plugin.txt
@@ -12,6 +12,8 @@ Installation:
Go to Calibre's Preferences page... click on the Plugins button. Use the file dialog button to select the plugin's zip file (ignobleepub_vXX_plugin.zip) and
click the 'Add' button. you're done.
+Please note: Calibre does not provide any immediate feedback to indicate that adding the plugin was a success. You can always click on the File-Type plugins to see if the plugin was added.
+
Configuration:
1) The easiest way to configure the plugin is to enter your name (Barnes & Noble account name) and credit card number (the one used to purchase the books) into the plugin's customization window. It's the same info you would enter into the ignoblekeygen script. Highlight the plugin (Ignoble Epub DeDRM) and click the "Customize Plugin" button on
diff --git a/Calibre_Plugins/README-ineptepub-plugin.txt b/Calibre_Plugins/README-ineptepub-plugin.txt
index 3777939..56f95b8 100644
--- a/Calibre_Plugins/README-ineptepub-plugin.txt
+++ b/Calibre_Plugins/README-ineptepub-plugin.txt
@@ -10,6 +10,9 @@ Installation:
Go to Calibre's Preferences page... click on the Plugins button. Use the file dialog button to select the plugin's zip file (ineptepub_vXX_plugin.zip) and click the 'Add' button. you're done.
+Please note: Calibre does not provide any immediate feedback to indicate that adding the plugin was a success. You can always click on the File-Type plugins to see if the plugin was added.
+
+
Configuration:
When first run, the plugin will attempt to find your Adobe Digital Editions installation (on Windows and Mac OS's). If successful, it will create an 'adeptkey.der' file and save it in Calibre's configuration directory. It will use that file on subsequent runs. If there are already '*.der' files in the directory, the plugin won't attempt to
diff --git a/Calibre_Plugins/eReaderPDB2PML_plugin.zip b/Calibre_Plugins/eReaderPDB2PML_plugin.zip
index f496b34..7174e72 100644
Binary files a/Calibre_Plugins/eReaderPDB2PML_plugin.zip and b/Calibre_Plugins/eReaderPDB2PML_plugin.zip differ
diff --git a/Calibre_Plugins/eReaderPDB2PML_plugin/eReaderPDB2PML_plugin.py b/Calibre_Plugins/eReaderPDB2PML_plugin/eReaderPDB2PML_plugin.py
index 405ef7c..5585cf5 100644
--- a/Calibre_Plugins/eReaderPDB2PML_plugin/eReaderPDB2PML_plugin.py
+++ b/Calibre_Plugins/eReaderPDB2PML_plugin/eReaderPDB2PML_plugin.py
@@ -42,7 +42,7 @@ class eRdrDeDRM(FileTypePlugin):
Credit given to The Dark Reverser for the original standalone script.'
supported_platforms = ['linux', 'osx', 'windows'] # Platforms this plugin will run on
author = 'DiapDealer' # The author of this plugin
- version = (0, 0, 3) # The version number of this plugin
+ version = (0, 0, 4) # The version number of this plugin
file_types = set(['pdb']) # The file types that this plugin will be applied to
on_import = True # Run this plugin during the import
@@ -76,7 +76,6 @@ class eRdrDeDRM(FileTypePlugin):
if pmlfilepath and pmlfilepath != 1:
import zipfile
- import shutil
print " Creating PMLZ file"
myZipFile = zipfile.ZipFile(pmlzfile.name,'w',zipfile.ZIP_STORED, False)
list = os.listdir(outdir)
diff --git a/Calibre_Plugins/eReaderPDB2PML_plugin/erdr2pml.py b/Calibre_Plugins/eReaderPDB2PML_plugin/erdr2pml.py
index f4ad3c4..ce6945d 100644
--- a/Calibre_Plugins/eReaderPDB2PML_plugin/erdr2pml.py
+++ b/Calibre_Plugins/eReaderPDB2PML_plugin/erdr2pml.py
@@ -56,32 +56,9 @@
# 0.15 - enabled high-ascii to pml character encoding. DropBook now works on Mac.
# 0.16 - convert to use openssl DES (very very fast) or pure python DES if openssl's libcrypto is not available
# 0.17 - added support for pycrypto's DES as well
+# 0.18 - on Windows try PyCrypto first and OpenSSL next
-Des = None
-
-import openssl_des
-Des = openssl_des.load_libcrypto()
-
-# if that did not work then try pycrypto version of DES
-if Des == None:
- import pycrypto_des
- Des = pycrypto_des.load_pycrypto()
-
-# if that did not work then use pure python implementation
-# of DES and try to speed it up with Psycho
-if Des == None:
- import python_des
- Des = python_des.Des
- # Import Psyco if available
- try:
- # http://psyco.sourceforge.net
- import psyco
- psyco.full()
- except ImportError:
- pass
-
-
-__version__='0.17'
+__version__='0.18'
class Unbuffered:
def __init__(self, stream):
@@ -97,6 +74,37 @@ sys.stdout=Unbuffered(sys.stdout)
import struct, binascii, getopt, zlib, os, os.path, urllib, tempfile
+Des = None
+if sys.platform.startswith('win'):
+ # first try with pycrypto
+ import pycrypto_des
+ Des = pycrypto_des.load_pycrypto()
+ if Des == None:
+ # they try with openssl
+ import openssl_des
+ Des = openssl_des.load_libcrypto()
+else:
+ # first try with openssl
+ import openssl_des
+ Des = openssl_des.load_libcrypto()
+ if Des == None:
+ # then try with pycrypto
+ import pycrypto_des
+ Des = pycrypto_des.load_pycrypto()
+
+# if that did not work then use pure python implementation
+# of DES and try to speed it up with Psycho
+if Des == None:
+ import python_des
+ Des = python_des.Des
+ # Import Psyco if available
+ try:
+ # http://psyco.sourceforge.net
+ import psyco
+ psyco.full()
+ except ImportError:
+ pass
+
try:
from hashlib import sha1
except ImportError:
@@ -460,7 +468,7 @@ def main(argv=None):
myZipFile.write(imagePath, localname)
myZipFile.close()
# remove temporary directory
- shutil.rmtree(outdir)
+ shutil.rmtree(outdir, True)
end_time = time.time()
search_time = end_time - start_time
diff --git a/Calibre_Plugins/ignobleepub_plugin.zip b/Calibre_Plugins/ignobleepub_plugin.zip
index 3698353..5ef5c41 100644
Binary files a/Calibre_Plugins/ignobleepub_plugin.zip and b/Calibre_Plugins/ignobleepub_plugin.zip differ
diff --git a/Calibre_Plugins/ignobleepub_plugin/ignobleepub_plugin.py b/Calibre_Plugins/ignobleepub_plugin/ignobleepub_plugin.py
index e0a8da7..1e79c26 100644
--- a/Calibre_Plugins/ignobleepub_plugin/ignobleepub_plugin.py
+++ b/Calibre_Plugins/ignobleepub_plugin/ignobleepub_plugin.py
@@ -45,6 +45,7 @@
# 0.1.1 - Allow Windows users to make use of openssl if they have it installed.
# - Incorporated SomeUpdates zipfix routine.
# 0.1.2 - bug fix for non-ascii file names in encryption.xml
+# 0.1.3 - Try PyCrypto on Windows first
"""
Decrypt Barnes & Noble ADEPT encrypted EPUB books.
@@ -169,7 +170,10 @@ def _load_crypto_pycrypto():
def _load_crypto():
_aes = _aes2 = None
- for loader in (_load_crypto_libcrypto, _load_crypto_pycrypto):
+ cryptolist = (_load_crypto_libcrypto, _load_crypto_pycrypto)
+ if sys.platform.startswith('win'):
+ cryptolist = (_load_crypto_pycrypto, _load_crypto_libcrypto)
+ for loader in cryptolist:
try:
_aes, _aes2 = loader()
break
@@ -267,7 +271,7 @@ class IgnobleDeDRM(FileTypePlugin):
Credit given to I <3 Cabbages for the original stand-alone scripts.'
supported_platforms = ['linux', 'osx', 'windows']
author = 'DiapDealer'
- version = (0, 1, 2)
+ version = (0, 1, 3)
minimum_calibre_version = (0, 6, 44) # Compiled python libraries cannot be imported in earlier versions.
file_types = set(['epub'])
on_import = True
diff --git a/Calibre_Plugins/ineptepub_plugin.zip b/Calibre_Plugins/ineptepub_plugin.zip
index a0047a7..4a8f8eb 100644
Binary files a/Calibre_Plugins/ineptepub_plugin.zip and b/Calibre_Plugins/ineptepub_plugin.zip differ
diff --git a/Calibre_Plugins/ineptepub_plugin/ade_key.py b/Calibre_Plugins/ineptepub_plugin/ade_key.py
index eb9ae3d..4b743f7 100644
--- a/Calibre_Plugins/ineptepub_plugin/ade_key.py
+++ b/Calibre_Plugins/ineptepub_plugin/ade_key.py
@@ -79,7 +79,7 @@ if iswindows:
def _load_crypto():
AES = None
- for loader in (_load_crypto_libcrypto, _load_crypto_pycrypto):
+ for loader in (_load_crypto_pycrypto, _load_crypto_libcrypto):
try:
AES = loader()
break
diff --git a/Calibre_Plugins/ineptepub_plugin/ineptepub_plugin.py b/Calibre_Plugins/ineptepub_plugin/ineptepub_plugin.py
index da36a87..beb924e 100644
--- a/Calibre_Plugins/ineptepub_plugin/ineptepub_plugin.py
+++ b/Calibre_Plugins/ineptepub_plugin/ineptepub_plugin.py
@@ -46,7 +46,7 @@
# 0.1.2 - Removed Carbon dependency for Mac users. Fixes an issue that was a
# result of Calibre changing to python 2.7.
# 0.1.3 - bug fix for epubs with non-ascii chars in file names
-
+# 0.1.4 - default to try PyCrypto first on Windows
"""
@@ -285,7 +285,10 @@ def _load_crypto_pycrypto():
def _load_crypto():
_aes = _rsa = None
- for loader in (_load_crypto_libcrypto, _load_crypto_pycrypto):
+ cryptolist = (_load_crypto_libcrypto, _load_crypto_pycrypto)
+ if sys.platform.startswith('win'):
+ cryptolist = (_load_crypto_pycrypto, _load_crypto_libcrypto)
+ for loader in cryptolist:
try:
_aes, _rsa = loader()
break
@@ -368,7 +371,7 @@ class IneptDeDRM(FileTypePlugin):
Credit given to I <3 Cabbages for the original stand-alone scripts.'
supported_platforms = ['linux', 'osx', 'windows']
author = 'DiapDealer'
- version = (0, 1, 3)
+ version = (0, 1, 4)
minimum_calibre_version = (0, 6, 44) # Compiled python libraries cannot be imported in earlier versions.
file_types = set(['epub'])
on_import = True
diff --git a/Calibre_Plugins/ineptpdf_plugin.zip b/Calibre_Plugins/ineptpdf_plugin.zip
index df017d7..eccc1b8 100644
Binary files a/Calibre_Plugins/ineptpdf_plugin.zip and b/Calibre_Plugins/ineptpdf_plugin.zip differ
diff --git a/Calibre_Plugins/ineptpdf_plugin/ade_key.py b/Calibre_Plugins/ineptpdf_plugin/ade_key.py
index eb9ae3d..4b743f7 100644
--- a/Calibre_Plugins/ineptpdf_plugin/ade_key.py
+++ b/Calibre_Plugins/ineptpdf_plugin/ade_key.py
@@ -79,7 +79,7 @@ if iswindows:
def _load_crypto():
AES = None
- for loader in (_load_crypto_libcrypto, _load_crypto_pycrypto):
+ for loader in (_load_crypto_pycrypto, _load_crypto_libcrypto):
try:
AES = loader()
break
diff --git a/Calibre_Plugins/ineptpdf_plugin/ineptpdf_plugin.py b/Calibre_Plugins/ineptpdf_plugin/ineptpdf_plugin.py
index c2092de..d519820 100644
--- a/Calibre_Plugins/ineptpdf_plugin/ineptpdf_plugin.py
+++ b/Calibre_Plugins/ineptpdf_plugin/ineptpdf_plugin.py
@@ -336,7 +336,10 @@ def _load_crypto_pycrypto():
def _load_crypto():
ARC4 = RSA = AES = None
- for loader in (_load_crypto_libcrypto, _load_crypto_pycrypto):
+ cryptolist = (_load_crypto_libcrypto, _load_crypto_pycrypto)
+ if sys.platform.startswith('win'):
+ cryptolist = (_load_crypto_pycrypto, _load_crypto_libcrypto)
+ for loader in cryptolist:
try:
ARC4, RSA, AES = loader()
break
@@ -2113,7 +2116,7 @@ class IneptPDFDeDRM(FileTypePlugin):
Credit given to I <3 Cabbages for the original stand-alone scripts.'
supported_platforms = ['linux', 'osx', 'windows']
author = 'DiapDealer'
- version = (0, 1, 1)
+ version = (0, 1, 2)
minimum_calibre_version = (0, 6, 44) # Compiled python libraries cannot be imported in earlier versions.
file_types = set(['pdf'])
on_import = True
diff --git a/Calibre_Plugins/k4mobidedrm_plugin.zip b/Calibre_Plugins/k4mobidedrm_plugin.zip
index 2a4dcee..aff65bd 100644
Binary files a/Calibre_Plugins/k4mobidedrm_plugin.zip and b/Calibre_Plugins/k4mobidedrm_plugin.zip differ
diff --git a/Calibre_Plugins/k4mobidedrm_plugin/k4mobidedrm_plugin.py b/Calibre_Plugins/k4mobidedrm_plugin/k4mobidedrm_plugin.py
index c23b897..53567e9 100644
--- a/Calibre_Plugins/k4mobidedrm_plugin/k4mobidedrm_plugin.py
+++ b/Calibre_Plugins/k4mobidedrm_plugin/k4mobidedrm_plugin.py
@@ -28,7 +28,7 @@
from __future__ import with_statement
-__version__ = '1.4'
+__version__ = '1.9'
class Unbuffered:
def __init__(self, stream):
@@ -163,6 +163,7 @@ def main(argv=sys.argv):
myzip = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
zipUpDir(myzip, tempdir, '')
myzip.close()
+ shutil.rmtree(tempdir, True)
return 1
if mobi:
@@ -198,7 +199,7 @@ def main(argv=sys.argv):
zipUpDir(myzip3, tempdir, 'img')
myzip3.close()
- shutil.rmtree(tempdir)
+ shutil.rmtree(tempdir, True)
return 0
if __name__ == '__main__':
@@ -214,7 +215,7 @@ if not __name__ == "__main__" and inCalibre:
Provided by the work of many including DiapDealer, SomeUpdates, IHeartCabbages, CMBDTC, Skindle, DarkReverser, ApprenticeAlf, etc.'
supported_platforms = ['osx', 'windows', 'linux'] # Platforms this plugin will run on
author = 'DiapDealer, SomeUpdates' # The author of this plugin
- version = (0, 1, 7) # The version number of this plugin
+ version = (0, 1, 9) # The version number of this plugin
file_types = set(['prc','mobi','azw','azw1','tpz']) # The file types that this plugin will be applied to
on_import = True # Run this plugin during the import
priority = 210 # run this plugin before mobidedrm, k4pcdedrm, k4dedrm
diff --git a/Calibre_Plugins/k4mobidedrm_plugin/k4mutils.py b/Calibre_Plugins/k4mobidedrm_plugin/k4mutils.py
index 4aa14dd..1b501ba 100644
--- a/Calibre_Plugins/k4mobidedrm_plugin/k4mutils.py
+++ b/Calibre_Plugins/k4mobidedrm_plugin/k4mutils.py
@@ -6,7 +6,7 @@ import os
import subprocess
-class K4MDrmException(Exception):
+class DrmException(Exception):
pass
@@ -18,7 +18,7 @@ def _load_crypto_libcrypto():
libcrypto = find_library('crypto')
if libcrypto is None:
- raise K4MDrmException('libcrypto not found')
+ raise DrmException('libcrypto not found')
libcrypto = CDLL(libcrypto)
AES_MAXNR = 14
@@ -51,19 +51,19 @@ def _load_crypto_libcrypto():
def set_decrypt_key(self, userkey, iv):
self._blocksize = len(userkey)
if (self._blocksize != 16) and (self._blocksize != 24) and (self._blocksize != 32) :
- raise K4MDrmException('AES improper key used')
+ raise DrmException('AES improper key used')
return
keyctx = self._keyctx = AES_KEY()
self.iv = iv
rv = AES_set_decrypt_key(userkey, len(userkey) * 8, keyctx)
if rv < 0:
- raise K4MDrmException('Failed to initialize AES key')
+ raise DrmException('Failed to initialize AES key')
def decrypt(self, data):
out = create_string_buffer(len(data))
rv = AES_cbc_encrypt(data, out, len(data), self._keyctx, self.iv, 0)
if rv == 0:
- raise K4MDrmException('AES decryption failed')
+ raise DrmException('AES decryption failed')
return out.raw
def keyivgen(self, passwd):
@@ -81,7 +81,7 @@ def _load_crypto():
LibCrypto = None
try:
LibCrypto = _load_crypto_libcrypto()
- except (ImportError, K4MDrmException):
+ except (ImportError, DrmException):
pass
return LibCrypto
@@ -185,8 +185,10 @@ def openKindleInfo(kInfoFile=None):
if pp >= 0:
kinfopath = resline
break
- if not os.path.exists(kinfopath):
- raise K4MDrmException('Error: .kindle-info file can not be found')
+ if not os.path.isfile(kinfopath):
+ raise DrmException('Error: .kindle-info file can not be found')
return open(kinfopath,'r')
else:
+ if not os.path.isfile(kinfoFile):
+ raise DrmException('Error: kindle-info file can not be found')
return open(kInfoFile, 'r')
diff --git a/Calibre_Plugins/k4mobidedrm_plugin/k4pcutils.py b/Calibre_Plugins/k4mobidedrm_plugin/k4pcutils.py
index 3f95660..efc310d 100644
--- a/Calibre_Plugins/k4mobidedrm_plugin/k4pcutils.py
+++ b/Calibre_Plugins/k4mobidedrm_plugin/k4pcutils.py
@@ -99,7 +99,12 @@ CryptUnprotectData = CryptUnprotectData()
def openKindleInfo(kInfoFile=None):
if kInfoFile == None:
regkey = winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\\")
- path = winreg.QueryValueEx(regkey, 'Local AppData')[0]
- return open(path+'\\Amazon\\Kindle For PC\\{AMAwzsaPaaZAzmZzZQzgZCAkZ3AjA_AY}\\kindle.info','r')
+ path = winreg.QueryValueEx(regkey, 'Local AppData')[0]
+ kinfopath = path +'\\Amazon\\Kindle For PC\\{AMAwzsaPaaZAzmZzZQzgZCAkZ3AjA_AY}\\kindle.info'
+ if not os.path.isfile(kinfopath):
+ raise DrmException('Error: kindle.info file can not be found')
+ return open(kinfopath,'r')
else:
+ if not os.path.isfile(kInfoFile):
+ raise DrmException('Error: kindle.info file can not be found')
return open(kInfoFile, 'r')
diff --git a/Calibre_Plugins/k4mobidedrm_plugin/mobidedrm.py b/Calibre_Plugins/k4mobidedrm_plugin/mobidedrm.py
index cc83224..864b545 100644
--- a/Calibre_Plugins/k4mobidedrm_plugin/mobidedrm.py
+++ b/Calibre_Plugins/k4mobidedrm_plugin/mobidedrm.py
@@ -42,8 +42,10 @@
# 0.20 - Correction: It seems that multibyte entries are encrypted in a v6 file.
# 0.21 - Added support for multiple pids
# 0.22 - revised structure to hold MobiBook as a class to allow an extended interface
+# 0.23 - fixed problem with older files with no EXTH section
+# 0.24 - add support for type 1 encryption and 'TEXtREAd' books as well
-__version__ = '0.22'
+__version__ = '0.24'
import sys
@@ -57,6 +59,7 @@ class Unbuffered:
return getattr(self.stream, attr)
sys.stdout=Unbuffered(sys.stdout)
+import os
import struct
import binascii
@@ -154,8 +157,10 @@ class MobiBook:
# initial sanity check on file
self.data_file = file(infile, 'rb').read()
self.header = self.data_file[0:78]
- if self.header[0x3C:0x3C+8] != 'BOOKMOBI':
+ if self.header[0x3C:0x3C+8] != 'BOOKMOBI' and self.header[0x3C:0x3C+8] != 'TEXtREAd':
raise DrmException("invalid file format")
+ self.magic = self.header[0x3C:0x3C+8]
+ self.crypto_type = -1
# build up section offset and flag info
self.num_sections, = struct.unpack('>H', self.header[76:78])
@@ -168,6 +173,14 @@ class MobiBook:
# parse information from section 0
self.sect = self.loadSection(0)
self.records, = struct.unpack('>H', self.sect[0x8:0x8+2])
+
+ if self.magic == 'TEXtREAd':
+ print "Book has format: ", self.magic
+ self.extra_data_flags = 0
+ self.mobi_length = 0
+ self.mobi_version = -1
+ self.meta_array = {}
+ return
self.mobi_length, = struct.unpack('>L',self.sect[0x14:0x18])
self.mobi_version, = struct.unpack('>L',self.sect[0x68:0x6C])
print "MOBI header version = %d, length = %d" %(self.mobi_version, self.mobi_length)
@@ -182,18 +195,23 @@ class MobiBook:
# if exth region exists parse it for metadata array
self.meta_array = {}
- exth_flag, = struct.unpack('>L', self.sect[0x80:0x84])
- exth = ''
- if exth_flag & 0x40:
- exth = self.sect[16 + self.mobi_length:]
- nitems, = struct.unpack('>I', exth[8:12])
- pos = 12
- for i in xrange(nitems):
- type, size = struct.unpack('>II', exth[pos: pos + 8])
- content = exth[pos + 8: pos + size]
- self.meta_array[type] = content
- pos += size
-
+ try:
+ exth_flag, = struct.unpack('>L', self.sect[0x80:0x84])
+ exth = 'NONE'
+ if exth_flag & 0x40:
+ exth = self.sect[16 + self.mobi_length:]
+ if (len(exth) >= 4) and (exth[:4] == 'EXTH'):
+ nitems, = struct.unpack('>I', exth[8:12])
+ pos = 12
+ for i in xrange(nitems):
+ type, size = struct.unpack('>II', exth[pos: pos + 8])
+ content = exth[pos + 8: pos + size]
+ self.meta_array[type] = content
+ pos += size
+ except:
+ self.meta_array = {}
+ pass
+
def getBookTitle(self):
title = ''
if 503 in self.meta_array:
@@ -269,12 +287,12 @@ class MobiBook:
def processBook(self, pidlist):
crypto_type, = struct.unpack('>H', self.sect[0xC:0xC+2])
+ print 'Crypto Type is: ', crypto_type
+ self.crypto_type = crypto_type
if crypto_type == 0:
print "This book is not encrypted."
return self.data_file
- if crypto_type == 1:
- raise DrmException("Cannot decode Mobipocket encryption type 1")
- if crypto_type != 2:
+ if crypto_type != 2 and crypto_type != 1:
raise DrmException("Cannot decode unknown Mobipocket encryption type %d" % crypto_type)
goodpids = []
@@ -286,23 +304,32 @@ class MobiBook:
elif len(pid)==8:
goodpids.append(pid)
- # calculate the keys
- drm_ptr, drm_count, drm_size, drm_flags = struct.unpack('>LLLL', self.sect[0xA8:0xA8+16])
- if drm_count == 0:
- raise DrmException("Not yet initialised with PID. Must be opened with Mobipocket Reader first.")
- found_key, pid = self.parseDRM(self.sect[drm_ptr:drm_ptr+drm_size], drm_count, goodpids)
- if not found_key:
- raise DrmException("No key found. Most likely the correct PID has not been given.")
+ if self.crypto_type == 1:
+ t1_keyvec = "QDCVEPMU675RUBSZ"
+ if self.magic == 'TEXtREAd':
+ bookkey_data = self.sect[0x0E:0x0E+16]
+ else:
+ bookkey_data = self.sect[0x90:0x90+16]
+ pid = "00000000"
+ found_key = PC1(t1_keyvec, bookkey_data)
+ else :
+ # calculate the keys
+ drm_ptr, drm_count, drm_size, drm_flags = struct.unpack('>LLLL', self.sect[0xA8:0xA8+16])
+ if drm_count == 0:
+ raise DrmException("Not yet initialised with PID. Must be opened with Mobipocket Reader first.")
+ found_key, pid = self.parseDRM(self.sect[drm_ptr:drm_ptr+drm_size], drm_count, goodpids)
+ if not found_key:
+ raise DrmException("No key found. Most likely the correct PID has not been given.")
+ # kill the drm keys
+ self.patchSection(0, "\0" * drm_size, drm_ptr)
+ # kill the drm pointers
+ self.patchSection(0, "\xff" * 4 + "\0" * 12, 0xA8)
if pid=="00000000":
print "File has default encryption, no specific PID."
else:
print "File is encoded with PID "+checksumPid(pid)+"."
- # kill the drm keys
- self.patchSection(0, "\0" * drm_size, drm_ptr)
- # kill the drm pointers
- self.patchSection(0, "\xff" * 4 + "\0" * 12, 0xA8)
# clear the crypto type
self.patchSection(0, "\0" * 2, 0xC)
diff --git a/DeDRM_Macintosh_Application/DeDRM.app.txt b/DeDRM_Macintosh_Application/DeDRM.app.txt
index 60d2f86..66b743f 100644
--- a/DeDRM_Macintosh_Application/DeDRM.app.txt
+++ b/DeDRM_Macintosh_Application/DeDRM.app.txt
@@ -55,7 +55,7 @@ on GetTools()
set AdobePDFTool to POSIX path of file (path to me as text) & "Contents/Resources/ineptpdf.pyw"
set ZipFixTool to POSIX path of file (path to me as text) & "Contents/Resources/zipfix.py"
set ProgressApp to POSIX path of file ((path to resource "DeDRM Progress.app") as string)
- if not fileexists(eReaderTool) or not fileexists(MobipocketTool) or not fileexists(BNKeyGenTool) or not fileexists(BNePubTool) or not fileexists(AdobeKeyGenTool) or not fileexists(AdobeePubTool) or not folderexists(ProgressApp) then
+ if not fileexists(eReaderTool) or not fileexists(MobipocketTool) or not fileexists(BNKeyGenTool) or not fileexists(BNePubTool) or not fileexists(AdobeKeyGenTool) or not fileexists(AdobePDFTool) or not fileexists(AdobeePubTool) or not folderexists(ProgressApp) then
display dialog "De-drm scripts or support files are missing from this package. Get a fresh copy." buttons {"Bother"} default button 1 with title "DeDRM Applescript" with icon stop
return false
end if
@@ -68,7 +68,7 @@ on unlockmobifile(encryptedFile)
try
set BOOKMOBI to read file encryptedFile from 61 for 8
end try
- if BOOKMOBI is not "BOOKMOBI" then
+ if BOOKMOBI is not "BOOKMOBI" and BOOKMOBI is not "TEXtREAd" then
set TOPAZ to "NOT"
try
set TOPAZ to read file encryptedFile from 1 for 4
@@ -98,9 +98,9 @@ on unlockmobifile(encryptedFile)
set shellcommand to shellcommand & " -k " & quoted form of KindleInfoPath
end repeat
set Serialstring to GetSerialstring()
- if Serialstring is not "" then set shellcommand to shellcommand & " -s " & Serialstring
+ if Serialstring is not "" then set shellcommand to shellcommand & " -s " & quoted form of Serialstring
set PIDstring to GetPIDstring()
- if PIDstring is not "" then set shellcommand to shellcommand & " -p " & PIDstring
+ if PIDstring is not "" then set shellcommand to shellcommand & " -p " & quoted form of PIDstring
set shellcommand to shellcommand & " " & (quoted form of encryptedFilePath) & " " & (quoted form of unlockedFileParentFolderPath)
--display dialog "shellcommand: " default answer shellcommand buttons {"OK"} default button 1 giving up after 10
try
@@ -108,8 +108,34 @@ on unlockmobifile(encryptedFile)
on error errmsg
set shellresult to errmsg
end try
+ --display dialog shellresult
try
- if (offset of "Error" in shellresult) > 0 then
+ repeat
+ if (totalebooks > 1) or (offset of "No key found" in shellresult) is 0 then
+ exit repeat
+ end if
+ -- ask for another PID as we're only doing one ebook
+ set newPID to "None"
+ set DialogPrompt to "Couldn't decode " & fileName & ". Do you have another PID to try?"
+ try
+ set dialogresult to (display dialog DialogPrompt default answer "" buttons {"Try This One", "No Extra PID"} with title "DeDRM Applescript" default button 2)
+ if button returned of dialogresult is "Try This One" then
+ set newPID to text returned of dialogresult
+ end if
+ end try
+ if newPID is "None" or (length of newPID is not 10 and length of newPID is not 8) then
+ exit repeat
+ end if
+ set shellcommand to "python " & (quoted form of MobipocketTool) & " -p " & quoted form of newPID & " " & (quoted form of encryptedFilePath) & " " & (quoted form of unlockedFileParentFolderPath)
+ --display dialog "shellcommand: " default answer shellcommand buttons {"OK"} default button 1 giving up after 10
+ try
+ set shellresult to do shell script shellcommand
+ on error errmsg
+ set shellresult to errmsg
+ end try
+ --display dialog shellresult
+ end repeat
+ if (offset of "Error" in shellresult) > 0 or (offset of "No key found" in shellresult) > 0 then
set ErrorCount to ErrorCount + 1
set ErrorList to (ErrorList & fileName & fileExtension & " couldn't be decoded:
" & shellresult as text) & "
@@ -322,6 +348,7 @@ on unlockepubfile(encryptedFile)
end repeat
if decoded is "NO" then
+ set shellresult to "no keys"
-- now try Adobe ePub
repeat with AdeptKey in AdeptKeyList
set shellcommand to "python " & (quoted form of AdobeePubTool) & " " & (quoted form of AdeptKey) & " " & (quoted form of fixedFilePath) & " " & (quoted form of unlockedFilePath)
@@ -350,6 +377,11 @@ on unlockepubfile(encryptedFile)
set ErrorCount to ErrorCount + 1
set ErrorList to (ErrorList & fileName & fileExtension & " couldn't be decoded: no keys.
+")
+ else if (offset of "not an ADEPT EPUB" in shellresult) is not 0 then
+ set WarningCount to WarningCount + 1
+ set WarningList to (WarningList & fileName & " doesn't seem to be encrypted.
+
")
else
set ErrorCount to ErrorCount + 1
@@ -392,7 +424,7 @@ on unlockpdffile(encryptedFile)
set decoded to "NO"
-- first we must check we have a PDF script
- GetIneptPDF(false)
+ --GetIneptPDF(false)
if not fileexists(AdobePDFTool) then
set ErrorCount to ErrorCount + 1
set ErrorList to ErrorList & encryptedFile & " is a PDF file and no ineptpdf script found.
@@ -591,7 +623,7 @@ on GetPIDs()
Enter any additional Mobipocket PIDs for your Mobipocket books one at a time:"
set FinishedButton to "No More"
end if
- set dialogresult to (display dialog DialogPrompt default answer "" buttons {"Delete All", "Add", FinishedButton} with title "DeDRM Applescript 2/6" default button 2)
+ set dialogresult to (display dialog DialogPrompt default answer "" buttons {"Delete All", "Add", FinishedButton} with title "DeDRM Applescript 2/5" default button 2)
if button returned of dialogresult is "Add" then
set PID to text returned of dialogresult
set PIDlength to length of PID
@@ -627,7 +659,7 @@ on GetSerials()
Enter any additional Kindle Serial Numbers one at a time:"
set FinishedButton to "No More"
end if
- set dialogresult to (display dialog DialogPrompt default answer "" buttons {"Delete All", "Add", FinishedButton} with title "DeDRM Applescript 3/6" default button 2)
+ set dialogresult to (display dialog DialogPrompt default answer "" buttons {"Delete All", "Add", FinishedButton} with title "DeDRM Applescript 3/5" default button 2)
if button returned of dialogresult is "Add" then
set Serial to text returned of dialogresult
set Seriallength to length of Serial
@@ -735,7 +767,7 @@ Please enter any additional "
set DialogPrompt to DialogPrompt & "eReader/Barnes & Noble Name,Number key pairs one at a time. If you're only decoding eReader files, the last 8 digits of the Number will do. The full 15 or 16 are only needed for Barnes & Noble ePubs. Only the last eight will be stored or displayed. Please separate the name and number with a comma and click \"Add\". Or to add a an already generated .b64 file, just click \"Add\" with nothing in the text field."
set dialogtitle to "DeDRM Applescript"
if (running) then
- set dialogtitle to dialogtitle & " 4/6"
+ set dialogtitle to dialogtitle & " 4/5"
end if
set dialogresult to (display dialog DialogPrompt default answer bnKeyText buttons {"Delete All", "Add", FinishedButton} with title dialogtitle default button 2)
if button returned of dialogresult is "Add" then
@@ -823,7 +855,7 @@ on GetAdeptKeyFiles()
To add extra key files (.der), click the AddÉ button."
set FinishedButton to "No More"
end if
- set dialogresult to (display dialog DialogPrompt buttons {"Forget All", "AddÉ", FinishedButton} with title "DeDRM Applescript 5/6" default button 2)
+ set dialogresult to (display dialog DialogPrompt buttons {"Forget All", "AddÉ", FinishedButton} with title "DeDRM Applescript 5/5" default button 2)
if button returned of dialogresult is "AddÉ" then
try
set newFile to (choose file with prompt "Please select an Adept key file") as text
@@ -1023,9 +1055,9 @@ on ReadPrefs()
try
set AdeptKeyList to value of property list item "AdeptKeys" of property list file preferencesFilePath
end try
- try
- set AdobePDFTool to value of property list item "IneptPDF" of property list file preferencesFilePath
- end try
+ --try
+ --set AdobePDFTool to value of property list item "IneptPDF" of property list file preferencesFilePath
+ --end try
end tell
end if
set newList to {}
@@ -1053,7 +1085,7 @@ on WritePrefs()
make new property list item at end of property list items of contents of myPrefs with properties {kind:list, name:"KindleInfoFiles", value:KindleInfoList}
make new property list item at end of property list items of contents of myPrefs with properties {kind:list, name:"bnKeys", value:bnKeys}
make new property list item at end of property list items of contents of myPrefs with properties {kind:list, name:"AdeptKeys", value:AdeptKeyList}
- make new property list item at end of property list items of contents of myPrefs with properties {kind:string, name:"IneptPDF", value:AdobePDFTool}
+ --make new property list item at end of property list items of contents of myPrefs with properties {kind:string, name:"IneptPDF", value:AdobePDFTool}
end tell
end WritePrefs
@@ -1103,9 +1135,9 @@ on run
if GetTools() then
display dialog "Drag&Drop encrypted ebooks onto this AppleScript's icon in the Finder to decode them after you have finished configuring it and it has quit.
-Click the Continue button to enter any PIDs for Mobipocket ebooks; serial numbers for Kindle ebooks; name,number key pairs for Barnes & Noble/eReader ebooks; to select extra Barnes & Noble .b64 key files; to select extra Adobe Adept .der key files; and to find th optional ineptpdf.pyw script.
+Click the Continue button to enter any PIDs for Mobipocket ebooks; serial numbers for Kindle ebooks; name,number key pairs for Barnes & Noble/eReader ebooks; to select extra Barnes & Noble .b64 key files; or to select extra Adobe Adept .der key files.
-***You do not need to enter any extra info if decoding ebooks downloaded to your installation of Kindle for Mac, or Adobe Digital Editions. If you do not have any PIDS; serial numbers; name,number keys, .b64 or .der files to add or want to decode PDF files, just click the Cancel button.***
+***You do not need to enter any extra info if decoding ebooks downloaded to your installation of Kindle for Mac, or Adobe Digital Editions. If you do not have any PIDS; serial numbers; name,number keys, .b64 or .der files, just click the Cancel button.***
Please only use to get access to your own books. Authors, publishers and ebook stores need to make money to produce more ebooks. Don't cheat them.
@@ -1117,7 +1149,7 @@ Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
For more information, please refer to
-" with title "DeDRM Applescript 1/6" buttons {"Cancel", "Continue"} default button 2
+" with title "DeDRM Applescript 1/5" buttons {"Cancel", "Continue"} default button 2
ReadPrefs()
GetAdeptKey(true)
@@ -1125,7 +1157,7 @@ For more information, please refer to
GetSerials()
GetKeys(true)
GetAdeptKeyFiles()
- GetIneptPDF(true)
+ --GetIneptPDF(true)
--GetKindleInfoFiles()
WritePrefs()
end if
diff --git a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Info.plist b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Info.plist
index 46ca744..0788a6e 100644
--- a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Info.plist
+++ b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Info.plist
@@ -24,7 +24,7 @@
CFBundleExecutable
droplet
CFBundleGetInfoString
- DeDRM 1.4, Copyright © 2010 by Apprentice Alf.
+ DeDRM 1.6, Copyright © 2010–2011 by Apprentice Alf.
CFBundleIconFile
droplet
CFBundleInfoDictionaryVersion
@@ -34,11 +34,11 @@
CFBundlePackageType
APPL
CFBundleShortVersionString
- 1.4
- LSMinimumSystemVersion
- 10.5.0
+ 1.6
CFBundleSignature
dplt
+ LSMinimumSystemVersion
+ 10.5.0
LSRequiresCarbon
WindowState
@@ -46,9 +46,9 @@
name
ScriptWindowState
positionOfDivider
- 739
+ 686
savedFrame
- 1533 -24 1262 818 1440 -150 1680 1050
+ 2161 -75 907 765 1440 -150 1680 1050
selectedTabView
result
diff --git a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/Scripts/main.scpt b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/Scripts/main.scpt
index 1224e93..b400b10 100644
Binary files a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/Scripts/main.scpt and b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/Scripts/main.scpt differ
diff --git a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/erdr2pml.py b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/erdr2pml.py
index daa6b21..ce6945d 100644
--- a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/erdr2pml.py
+++ b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/erdr2pml.py
@@ -55,29 +55,10 @@
# 0.14 - contributed enhancement to support --make-pmlz switch
# 0.15 - enabled high-ascii to pml character encoding. DropBook now works on Mac.
# 0.16 - convert to use openssl DES (very very fast) or pure python DES if openssl's libcrypto is not available
+# 0.17 - added support for pycrypto's DES as well
+# 0.18 - on Windows try PyCrypto first and OpenSSL next
-Des = None
-
-import openssl_des
-Des = openssl_des.load_libcrypto()
-
-# if that did not work then use pure python implementation
-# of DES and try to speed it up with Psycho
-if Des == None:
- import python_des
- Des = python_des.Des
- # Import Psyco if available
- try:
- # Dumb speed hack 1
- # http://psyco.sourceforge.net
- import psyco
- psyco.full()
- pass
- except ImportError:
- pass
-
-
-__version__='0.16'
+__version__='0.18'
class Unbuffered:
def __init__(self, stream):
@@ -93,6 +74,37 @@ sys.stdout=Unbuffered(sys.stdout)
import struct, binascii, getopt, zlib, os, os.path, urllib, tempfile
+Des = None
+if sys.platform.startswith('win'):
+ # first try with pycrypto
+ import pycrypto_des
+ Des = pycrypto_des.load_pycrypto()
+ if Des == None:
+ # they try with openssl
+ import openssl_des
+ Des = openssl_des.load_libcrypto()
+else:
+ # first try with openssl
+ import openssl_des
+ Des = openssl_des.load_libcrypto()
+ if Des == None:
+ # then try with pycrypto
+ import pycrypto_des
+ Des = pycrypto_des.load_pycrypto()
+
+# if that did not work then use pure python implementation
+# of DES and try to speed it up with Psycho
+if Des == None:
+ import python_des
+ Des = python_des.Des
+ # Import Psyco if available
+ try:
+ # http://psyco.sourceforge.net
+ import psyco
+ psyco.full()
+ except ImportError:
+ pass
+
try:
from hashlib import sha1
except ImportError:
@@ -456,7 +468,7 @@ def main(argv=None):
myZipFile.write(imagePath, localname)
myZipFile.close()
# remove temporary directory
- shutil.rmtree(outdir)
+ shutil.rmtree(outdir, True)
end_time = time.time()
search_time = end_time - start_time
diff --git a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/ignobleepub.pyw b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/ignobleepub.pyw
index 469713a..0afc2bc 100644
--- a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/ignobleepub.pyw
+++ b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/ignobleepub.pyw
@@ -1,6 +1,6 @@
#! /usr/bin/python
-# ignobleepub.pyw, version 3
+# ignobleepub.pyw, version 3.3
# To run this program install Python 2.6 from
# and OpenSSL or PyCrypto from http://www.voidspace.org.uk/python/modules.shtml#pycrypto
@@ -12,6 +12,9 @@
# 2 - Added OS X support by using OpenSSL when available
# 3 - screen out improper key lengths to prevent segfaults on Linux
# 3.1 - Allow Windows versions of libcrypto to be found
+# 3.2 - add support for encoding to 'utf-8' when building up list of files to cecrypt from encryption.xml
+# 3.3 - On Windows try PyCrypto first and OpenSSL next
+
from __future__ import with_statement
@@ -105,15 +108,18 @@ def _load_crypto_pycrypto():
def _load_crypto():
AES = None
- for loader in (_load_crypto_libcrypto, _load_crypto_pycrypto):
+ cryptolist = (_load_crypto_libcrypto, _load_crypto_pycrypto)
+ if sys.platform.startswith('win'):
+ cryptolist = (_load_crypto_pycrypto, _load_crypto_libcrypto)
+ for loader in cryptolist:
try:
AES = loader()
break
except (ImportError, IGNOBLEError):
pass
return AES
-AES = _load_crypto()
+AES = _load_crypto()
diff --git a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/ignoblekeygen.pyw b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/ignoblekeygen.pyw
index 479c11d..b2607ea 100644
--- a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/ignoblekeygen.pyw
+++ b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/ignoblekeygen.pyw
@@ -1,6 +1,6 @@
#! /usr/bin/python
-# ignoblekeygen.pyw, version 2
+# ignoblekeygen.pyw, version 2.2
# To run this program install Python 2.6 from
# and OpenSSL or PyCrypto from http://www.voidspace.org.uk/python/modules.shtml#pycrypto
@@ -11,7 +11,7 @@
# 1 - Initial release
# 2 - Add OS X support by using OpenSSL when available (taken/modified from ineptepub v5)
# 2.1 - Allow Windows versions of libcrypto to be found
-
+# 2.2 - On Windows try PyCrypto first and then OpenSSL next
"""
Generate Barnes & Noble EPUB user key from name and credit card number.
"""
@@ -102,11 +102,12 @@ def _load_crypto_pycrypto():
return AES
-
-
def _load_crypto():
AES = None
- for loader in (_load_crypto_libcrypto, _load_crypto_pycrypto):
+ cryptolist = (_load_crypto_libcrypto, _load_crypto_pycrypto)
+ if sys.platform.startswith('win'):
+ cryptolist = (_load_crypto_pycrypto, _load_crypto_libcrypto)
+ for loader in cryptolist:
try:
AES = loader()
break
diff --git a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/ineptepub.pyw b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/ineptepub.pyw
index 442c37a..9d95720 100644
--- a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/ineptepub.pyw
+++ b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/ineptepub.pyw
@@ -1,7 +1,7 @@
#! /usr/bin/python
# -*- coding: utf-8 -*-
-# ineptepub.pyw, version 5.4
+# ineptepub.pyw, version 5.5
# Copyright © 2009-2010 i♥cabbages
# Released under the terms of the GNU General Public Licence, version 3 or
@@ -26,6 +26,7 @@
# 5.2 - Fix ctypes error causing segfaults on some systems
# 5.3 - add support for OpenSSL on Windows, fix bug with some versions of libcrypto 0.9.8 prior to path level o
# 5.4 - add support for encoding to 'utf-8' when building up list of files to decrypt from encryption.xml
+# 5.5 - On Windows try PyCrypto first, OpenSSL next
"""
Decrypt Adobe ADEPT-encrypted EPUB books.
@@ -259,7 +260,10 @@ def _load_crypto_pycrypto():
def _load_crypto():
AES = RSA = None
- for loader in (_load_crypto_libcrypto, _load_crypto_pycrypto):
+ cryptolist = (_load_crypto_libcrypto, _load_crypto_pycrypto)
+ if sys.platform.startswith('win'):
+ cryptolist = (_load_crypto_pycrypto, _load_crypto_libcrypto)
+ for loader in cryptolist:
try:
AES, RSA = loader()
break
diff --git a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/ineptkey.pyw b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/ineptkey.pyw
index bd66e78..fd90508 100644
--- a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/ineptkey.pyw
+++ b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/ineptkey.pyw
@@ -1,7 +1,7 @@
#! /usr/bin/python
# -*- coding: utf-8 -*-
-# ineptkey.pyw, version 5
+# ineptkey.pyw, version 5.3
# Copyright © 2009-2010 i♥cabbages
# Released under the terms of the GNU General Public Licence, version 3 or
@@ -32,6 +32,7 @@
# Clean up and merge OS X support by unknown
# 5.1 - add support for using OpenSSL on Windows in place of PyCrypto
# 5.2 - added support for output of key to a particular file
+# 5.3 - On Windows try PyCrypto first, OpenSSL next
"""
Retrieve Adobe ADEPT user key.
@@ -115,7 +116,7 @@ if sys.platform.startswith('win'):
def _load_crypto():
AES = None
- for loader in (_load_crypto_libcrypto, _load_crypto_pycrypto):
+ for loader in (_load_crypto_pycrypto, _load_crypto_libcrypto):
try:
AES = loader()
break
diff --git a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/ineptpdf.pyw b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/ineptpdf.pyw
new file mode 100644
index 0000000..d73e069
--- /dev/null
+++ b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/ineptpdf.pyw
@@ -0,0 +1,2221 @@
+#! /usr/bin/env python
+# ineptpdf.pyw, version 7.7
+
+# To run this program install Python 2.6 from http://www.python.org/download/
+# and OpenSSL (already installed on Mac OS X and Linux) OR
+# PyCrypto from http://www.voidspace.org.uk/python/modules.shtml#pycrypto
+# (make sure to install the version for Python 2.6). Save this script file as
+# ineptpdf.pyw and double-click on it to run it.
+
+# Revision history:
+# 1 - Initial release
+# 2 - Improved determination of key-generation algorithm
+# 3 - Correctly handle PDF >=1.5 cross-reference streams
+# 4 - Removal of ciando's personal ID
+# 5 - Automated decryption of a complete directory
+# 6.1 - backward compatibility for 1.7.1 and old adeptkey.der
+# 7 - Get cross reference streams and object streams working for input.
+# Not yet supported on output but this only effects file size,
+# not functionality. (anon2)
+# 7.1 - Correct a problem when an old trailer is not followed by startxref
+# 7.2 - Correct malformed Mac OS resource forks for Stanza (anon2)
+# - Support for cross ref streams on output (decreases file size)
+# 7.3 - Correct bug in trailer with cross ref stream that caused the error
+# "The root object is missing or invalid" in Adobe Reader. (anon2)
+# 7.4 - Force all generation numbers in output file to be 0, like in v6.
+# Fallback code for wrong xref improved (search till last trailer
+# instead of first) (anon2)
+# 7.5 - allow support for OpenSSL to replace pycrypto on all platforms
+# implemented ARC4 interface to OpenSSL
+# fixed minor typos
+# 7.6 - backported AES and other fixes from version 8.4.48
+# 7.7 - On Windows try PyCrypto first and OpenSSL next
+
+"""
+Decrypts Adobe ADEPT-encrypted PDF files.
+"""
+
+from __future__ import with_statement
+
+__license__ = 'GPL v3'
+
+import sys
+import os
+import re
+import zlib
+import struct
+import hashlib
+from itertools import chain, islice
+import xml.etree.ElementTree as etree
+import Tkinter
+import Tkconstants
+import tkFileDialog
+import tkMessageBox
+
+class ADEPTError(Exception):
+ pass
+
+
+import hashlib
+
+def SHA256(message):
+ ctx = hashlib.sha256()
+ ctx.update(message)
+ return ctx.digest()
+
+
+def _load_crypto_libcrypto():
+ from ctypes import CDLL, POINTER, c_void_p, c_char_p, c_int, c_long, \
+ Structure, c_ulong, create_string_buffer, cast
+ from ctypes.util import find_library
+
+ if sys.platform.startswith('win'):
+ libcrypto = find_library('libeay32')
+ else:
+ libcrypto = find_library('crypto')
+
+ if libcrypto is None:
+ raise ADEPTError('libcrypto not found')
+ libcrypto = CDLL(libcrypto)
+
+ AES_MAXNR = 14
+
+ RSA_NO_PADDING = 3
+
+ c_char_pp = POINTER(c_char_p)
+ c_int_p = POINTER(c_int)
+
+ class AES_KEY(Structure):
+ _fields_ = [('rd_key', c_long * (4 * (AES_MAXNR + 1))), ('rounds', c_int)]
+ AES_KEY_p = POINTER(AES_KEY)
+
+ class RC4_KEY(Structure):
+ _fields_ = [('x', c_int), ('y', c_int), ('box', c_int * 256)]
+ RC4_KEY_p = POINTER(RC4_KEY)
+
+ class RSA(Structure):
+ pass
+ RSA_p = POINTER(RSA)
+
+ def F(restype, name, argtypes):
+ func = getattr(libcrypto, name)
+ func.restype = restype
+ func.argtypes = argtypes
+ return func
+
+ AES_cbc_encrypt = F(None, 'AES_cbc_encrypt',[c_char_p, c_char_p, c_ulong, AES_KEY_p, c_char_p,c_int])
+ AES_set_decrypt_key = F(c_int, 'AES_set_decrypt_key',[c_char_p, c_int, AES_KEY_p])
+
+ RC4_set_key = F(None,'RC4_set_key',[RC4_KEY_p, c_int, c_char_p])
+ RC4_crypt = F(None,'RC4',[RC4_KEY_p, c_int, c_char_p, c_char_p])
+
+ d2i_RSAPrivateKey = F(RSA_p, 'd2i_RSAPrivateKey',
+ [RSA_p, c_char_pp, c_long])
+ RSA_size = F(c_int, 'RSA_size', [RSA_p])
+ RSA_private_decrypt = F(c_int, 'RSA_private_decrypt',
+ [c_int, c_char_p, c_char_p, RSA_p, c_int])
+ RSA_free = F(None, 'RSA_free', [RSA_p])
+
+ class RSA(object):
+ def __init__(self, der):
+ buf = create_string_buffer(der)
+ pp = c_char_pp(cast(buf, c_char_p))
+ rsa = self._rsa = d2i_RSAPrivateKey(None, pp, len(der))
+ if rsa is None:
+ raise ADEPTError('Error parsing ADEPT user key DER')
+
+ def decrypt(self, from_):
+ rsa = self._rsa
+ to = create_string_buffer(RSA_size(rsa))
+ dlen = RSA_private_decrypt(len(from_), from_, to, rsa,
+ RSA_NO_PADDING)
+ if dlen < 0:
+ raise ADEPTError('RSA decryption failed')
+ return to[1:dlen]
+
+ def __del__(self):
+ if self._rsa is not None:
+ RSA_free(self._rsa)
+ self._rsa = None
+
+ class ARC4(object):
+ @classmethod
+ def new(cls, userkey):
+ self = ARC4()
+ self._blocksize = len(userkey)
+ key = self._key = RC4_KEY()
+ RC4_set_key(key, self._blocksize, userkey)
+ return self
+ def __init__(self):
+ self._blocksize = 0
+ self._key = None
+ def decrypt(self, data):
+ out = create_string_buffer(len(data))
+ RC4_crypt(self._key, len(data), data, out)
+ return out.raw
+
+ class AES(object):
+ @classmethod
+ def new(cls, userkey, mode, iv):
+ self = AES()
+ self._blocksize = len(userkey)
+ # mode is ignored since CBCMODE is only thing supported/used so far
+ self._mode = mode
+ if (self._blocksize != 16) and (self._blocksize != 24) and (self._blocksize != 32) :
+ raise ADEPTError('AES improper key used')
+ return
+ keyctx = self._keyctx = AES_KEY()
+ self._iv = iv
+ rv = AES_set_decrypt_key(userkey, len(userkey) * 8, keyctx)
+ if rv < 0:
+ raise ADEPTError('Failed to initialize AES key')
+ return self
+ def __init__(self):
+ self._blocksize = 0
+ self._keyctx = None
+ self._iv = 0
+ self._mode = 0
+ def decrypt(self, data):
+ out = create_string_buffer(len(data))
+ rv = AES_cbc_encrypt(data, out, len(data), self._keyctx, self._iv, 0)
+ if rv == 0:
+ raise ADEPTError('AES decryption failed')
+ return out.raw
+
+ return (ARC4, RSA, AES)
+
+
+def _load_crypto_pycrypto():
+ from Crypto.PublicKey import RSA as _RSA
+ from Crypto.Cipher import ARC4 as _ARC4
+ from Crypto.Cipher import AES as _AES
+
+ # ASN.1 parsing code from tlslite
+ class ASN1Error(Exception):
+ pass
+
+ class ASN1Parser(object):
+ class Parser(object):
+ def __init__(self, bytes):
+ self.bytes = bytes
+ self.index = 0
+
+ def get(self, length):
+ if self.index + length > len(self.bytes):
+ raise ASN1Error("Error decoding ASN.1")
+ x = 0
+ for count in range(length):
+ x <<= 8
+ x |= self.bytes[self.index]
+ self.index += 1
+ return x
+
+ def getFixBytes(self, lengthBytes):
+ bytes = self.bytes[self.index : self.index+lengthBytes]
+ self.index += lengthBytes
+ return bytes
+
+ def getVarBytes(self, lengthLength):
+ lengthBytes = self.get(lengthLength)
+ return self.getFixBytes(lengthBytes)
+
+ def getFixList(self, length, lengthList):
+ l = [0] * lengthList
+ for x in range(lengthList):
+ l[x] = self.get(length)
+ return l
+
+ def getVarList(self, length, lengthLength):
+ lengthList = self.get(lengthLength)
+ if lengthList % length != 0:
+ raise ASN1Error("Error decoding ASN.1")
+ lengthList = int(lengthList/length)
+ l = [0] * lengthList
+ for x in range(lengthList):
+ l[x] = self.get(length)
+ return l
+
+ def startLengthCheck(self, lengthLength):
+ self.lengthCheck = self.get(lengthLength)
+ self.indexCheck = self.index
+
+ def setLengthCheck(self, length):
+ self.lengthCheck = length
+ self.indexCheck = self.index
+
+ def stopLengthCheck(self):
+ if (self.index - self.indexCheck) != self.lengthCheck:
+ raise ASN1Error("Error decoding ASN.1")
+
+ def atLengthCheck(self):
+ if (self.index - self.indexCheck) < self.lengthCheck:
+ return False
+ elif (self.index - self.indexCheck) == self.lengthCheck:
+ return True
+ else:
+ raise ASN1Error("Error decoding ASN.1")
+
+ def __init__(self, bytes):
+ p = self.Parser(bytes)
+ p.get(1)
+ self.length = self._getASN1Length(p)
+ self.value = p.getFixBytes(self.length)
+
+ def getChild(self, which):
+ p = self.Parser(self.value)
+ for x in range(which+1):
+ markIndex = p.index
+ p.get(1)
+ length = self._getASN1Length(p)
+ p.getFixBytes(length)
+ return ASN1Parser(p.bytes[markIndex:p.index])
+
+ def _getASN1Length(self, p):
+ firstLength = p.get(1)
+ if firstLength<=127:
+ return firstLength
+ else:
+ lengthLength = firstLength & 0x7F
+ return p.get(lengthLength)
+
+ class ARC4(object):
+ @classmethod
+ def new(cls, userkey):
+ self = ARC4()
+ self._arc4 = _ARC4.new(userkey)
+ return self
+ def __init__(self):
+ self._arc4 = None
+ def decrypt(self, data):
+ return self._arc4.decrypt(data)
+
+ class AES(object):
+ @classmethod
+ def new(cls, userkey, mode, iv):
+ self = AES()
+ self._aes = _AES.new(userkey, mode, iv)
+ return self
+ def __init__(self):
+ self._aes = None
+ def decrypt(self, data):
+ return self._aes.decrypt(data)
+
+ class RSA(object):
+ def __init__(self, der):
+ key = ASN1Parser([ord(x) for x in der])
+ key = [key.getChild(x).value for x in xrange(1, 4)]
+ key = [self.bytesToNumber(v) for v in key]
+ self._rsa = _RSA.construct(key)
+
+ def bytesToNumber(self, bytes):
+ total = 0L
+ for byte in bytes:
+ total = (total << 8) + byte
+ return total
+
+ def decrypt(self, data):
+ return self._rsa.decrypt(data)
+
+ return (ARC4, RSA, AES)
+
+def _load_crypto():
+ ARC4 = RSA = AES = None
+ cryptolist = (_load_crypto_libcrypto, _load_crypto_pycrypto)
+ if sys.platform.startswith('win'):
+ cryptolist = (_load_crypto_pycrypto, _load_crypto_libcrypto)
+ for loader in cryptolist:
+ try:
+ ARC4, RSA, AES = loader()
+ break
+ except (ImportError, ADEPTError):
+ pass
+ return (ARC4, RSA, AES)
+ARC4, RSA, AES = _load_crypto()
+
+
+try:
+ from cStringIO import StringIO
+except ImportError:
+ from StringIO import StringIO
+
+
+# Do we generate cross reference streams on output?
+# 0 = never
+# 1 = only if present in input
+# 2 = always
+
+GEN_XREF_STM = 1
+
+# This is the value for the current document
+gen_xref_stm = False # will be set in PDFSerializer
+
+# PDF parsing routines from pdfminer, with changes for EBX_HANDLER
+
+# Utilities
+
+def choplist(n, seq):
+ '''Groups every n elements of the list.'''
+ r = []
+ for x in seq:
+ r.append(x)
+ if len(r) == n:
+ yield tuple(r)
+ r = []
+ return
+
+def nunpack(s, default=0):
+ '''Unpacks up to 4 bytes big endian.'''
+ l = len(s)
+ if not l:
+ return default
+ elif l == 1:
+ return ord(s)
+ elif l == 2:
+ return struct.unpack('>H', s)[0]
+ elif l == 3:
+ return struct.unpack('>L', '\x00'+s)[0]
+ elif l == 4:
+ return struct.unpack('>L', s)[0]
+ else:
+ return TypeError('invalid length: %d' % l)
+
+
+STRICT = 0
+
+
+# PS Exceptions
+
+class PSException(Exception): pass
+class PSEOF(PSException): pass
+class PSSyntaxError(PSException): pass
+class PSTypeError(PSException): pass
+class PSValueError(PSException): pass
+
+
+# Basic PostScript Types
+
+
+# PSLiteral
+class PSObject(object): pass
+
+class PSLiteral(PSObject):
+ '''
+ PS literals (e.g. "/Name").
+ Caution: Never create these objects directly.
+ Use PSLiteralTable.intern() instead.
+ '''
+ def __init__(self, name):
+ self.name = name
+ return
+
+ def __repr__(self):
+ name = []
+ for char in self.name:
+ if not char.isalnum():
+ char = '#%02x' % ord(char)
+ name.append(char)
+ return '/%s' % ''.join(name)
+
+# PSKeyword
+class PSKeyword(PSObject):
+ '''
+ PS keywords (e.g. "showpage").
+ Caution: Never create these objects directly.
+ Use PSKeywordTable.intern() instead.
+ '''
+ def __init__(self, name):
+ self.name = name
+ return
+
+ def __repr__(self):
+ return self.name
+
+# PSSymbolTable
+class PSSymbolTable(object):
+
+ '''
+ Symbol table that stores PSLiteral or PSKeyword.
+ '''
+
+ def __init__(self, classe):
+ self.dic = {}
+ self.classe = classe
+ return
+
+ def intern(self, name):
+ if name in self.dic:
+ lit = self.dic[name]
+ else:
+ lit = self.classe(name)
+ self.dic[name] = lit
+ return lit
+
+PSLiteralTable = PSSymbolTable(PSLiteral)
+PSKeywordTable = PSSymbolTable(PSKeyword)
+LIT = PSLiteralTable.intern
+KWD = PSKeywordTable.intern
+KEYWORD_BRACE_BEGIN = KWD('{')
+KEYWORD_BRACE_END = KWD('}')
+KEYWORD_ARRAY_BEGIN = KWD('[')
+KEYWORD_ARRAY_END = KWD(']')
+KEYWORD_DICT_BEGIN = KWD('<<')
+KEYWORD_DICT_END = KWD('>>')
+
+
+def literal_name(x):
+ if not isinstance(x, PSLiteral):
+ if STRICT:
+ raise PSTypeError('Literal required: %r' % x)
+ else:
+ return str(x)
+ return x.name
+
+def keyword_name(x):
+ if not isinstance(x, PSKeyword):
+ if STRICT:
+ raise PSTypeError('Keyword required: %r' % x)
+ else:
+ return str(x)
+ return x.name
+
+
+## PSBaseParser
+##
+EOL = re.compile(r'[\r\n]')
+SPC = re.compile(r'\s')
+NONSPC = re.compile(r'\S')
+HEX = re.compile(r'[0-9a-fA-F]')
+END_LITERAL = re.compile(r'[#/%\[\]()<>{}\s]')
+END_HEX_STRING = re.compile(r'[^\s0-9a-fA-F]')
+HEX_PAIR = re.compile(r'[0-9a-fA-F]{2}|.')
+END_NUMBER = re.compile(r'[^0-9]')
+END_KEYWORD = re.compile(r'[#/%\[\]()<>{}\s]')
+END_STRING = re.compile(r'[()\134]')
+OCT_STRING = re.compile(r'[0-7]')
+ESC_STRING = { 'b':8, 't':9, 'n':10, 'f':12, 'r':13, '(':40, ')':41, '\\':92 }
+
+class PSBaseParser(object):
+
+ '''
+ Most basic PostScript parser that performs only basic tokenization.
+ '''
+ BUFSIZ = 4096
+
+ def __init__(self, fp):
+ self.fp = fp
+ self.seek(0)
+ return
+
+ def __repr__(self):
+ return '' % (self.fp, self.bufpos)
+
+ def flush(self):
+ return
+
+ def close(self):
+ self.flush()
+ return
+
+ def tell(self):
+ return self.bufpos+self.charpos
+
+ def poll(self, pos=None, n=80):
+ pos0 = self.fp.tell()
+ if not pos:
+ pos = self.bufpos+self.charpos
+ self.fp.seek(pos)
+ ##print >>sys.stderr, 'poll(%d): %r' % (pos, self.fp.read(n))
+ self.fp.seek(pos0)
+ return
+
+ def seek(self, pos):
+ '''
+ Seeks the parser to the given position.
+ '''
+ self.fp.seek(pos)
+ # reset the status for nextline()
+ self.bufpos = pos
+ self.buf = ''
+ self.charpos = 0
+ # reset the status for nexttoken()
+ self.parse1 = self.parse_main
+ self.tokens = []
+ return
+
+ def fillbuf(self):
+ if self.charpos < len(self.buf): return
+ # fetch next chunk.
+ self.bufpos = self.fp.tell()
+ self.buf = self.fp.read(self.BUFSIZ)
+ if not self.buf:
+ raise PSEOF('Unexpected EOF')
+ self.charpos = 0
+ return
+
+ def parse_main(self, s, i):
+ m = NONSPC.search(s, i)
+ if not m:
+ return (self.parse_main, len(s))
+ j = m.start(0)
+ c = s[j]
+ self.tokenstart = self.bufpos+j
+ if c == '%':
+ self.token = '%'
+ return (self.parse_comment, j+1)
+ if c == '/':
+ self.token = ''
+ return (self.parse_literal, j+1)
+ if c in '-+' or c.isdigit():
+ self.token = c
+ return (self.parse_number, j+1)
+ if c == '.':
+ self.token = c
+ return (self.parse_float, j+1)
+ if c.isalpha():
+ self.token = c
+ return (self.parse_keyword, j+1)
+ if c == '(':
+ self.token = ''
+ self.paren = 1
+ return (self.parse_string, j+1)
+ if c == '<':
+ self.token = ''
+ return (self.parse_wopen, j+1)
+ if c == '>':
+ self.token = ''
+ return (self.parse_wclose, j+1)
+ self.add_token(KWD(c))
+ return (self.parse_main, j+1)
+
+ def add_token(self, obj):
+ self.tokens.append((self.tokenstart, obj))
+ return
+
+ def parse_comment(self, s, i):
+ m = EOL.search(s, i)
+ if not m:
+ self.token += s[i:]
+ return (self.parse_comment, len(s))
+ j = m.start(0)
+ self.token += s[i:j]
+ # We ignore comments.
+ #self.tokens.append(self.token)
+ return (self.parse_main, j)
+
+ def parse_literal(self, s, i):
+ m = END_LITERAL.search(s, i)
+ if not m:
+ self.token += s[i:]
+ return (self.parse_literal, len(s))
+ j = m.start(0)
+ self.token += s[i:j]
+ c = s[j]
+ if c == '#':
+ self.hex = ''
+ return (self.parse_literal_hex, j+1)
+ self.add_token(LIT(self.token))
+ return (self.parse_main, j)
+
+ def parse_literal_hex(self, s, i):
+ c = s[i]
+ if HEX.match(c) and len(self.hex) < 2:
+ self.hex += c
+ return (self.parse_literal_hex, i+1)
+ if self.hex:
+ self.token += chr(int(self.hex, 16))
+ return (self.parse_literal, i)
+
+ def parse_number(self, s, i):
+ m = END_NUMBER.search(s, i)
+ if not m:
+ self.token += s[i:]
+ return (self.parse_number, len(s))
+ j = m.start(0)
+ self.token += s[i:j]
+ c = s[j]
+ if c == '.':
+ self.token += c
+ return (self.parse_float, j+1)
+ try:
+ self.add_token(int(self.token))
+ except ValueError:
+ pass
+ return (self.parse_main, j)
+ def parse_float(self, s, i):
+ m = END_NUMBER.search(s, i)
+ if not m:
+ self.token += s[i:]
+ return (self.parse_float, len(s))
+ j = m.start(0)
+ self.token += s[i:j]
+ self.add_token(float(self.token))
+ return (self.parse_main, j)
+
+ def parse_keyword(self, s, i):
+ m = END_KEYWORD.search(s, i)
+ if not m:
+ self.token += s[i:]
+ return (self.parse_keyword, len(s))
+ j = m.start(0)
+ self.token += s[i:j]
+ if self.token == 'true':
+ token = True
+ elif self.token == 'false':
+ token = False
+ else:
+ token = KWD(self.token)
+ self.add_token(token)
+ return (self.parse_main, j)
+
+ def parse_string(self, s, i):
+ m = END_STRING.search(s, i)
+ if not m:
+ self.token += s[i:]
+ return (self.parse_string, len(s))
+ j = m.start(0)
+ self.token += s[i:j]
+ c = s[j]
+ if c == '\\':
+ self.oct = ''
+ return (self.parse_string_1, j+1)
+ if c == '(':
+ self.paren += 1
+ self.token += c
+ return (self.parse_string, j+1)
+ if c == ')':
+ self.paren -= 1
+ if self.paren:
+ self.token += c
+ return (self.parse_string, j+1)
+ self.add_token(self.token)
+ return (self.parse_main, j+1)
+ def parse_string_1(self, s, i):
+ c = s[i]
+ if OCT_STRING.match(c) and len(self.oct) < 3:
+ self.oct += c
+ return (self.parse_string_1, i+1)
+ if self.oct:
+ self.token += chr(int(self.oct, 8))
+ return (self.parse_string, i)
+ if c in ESC_STRING:
+ self.token += chr(ESC_STRING[c])
+ return (self.parse_string, i+1)
+
+ def parse_wopen(self, s, i):
+ c = s[i]
+ if c.isspace() or HEX.match(c):
+ return (self.parse_hexstring, i)
+ if c == '<':
+ self.add_token(KEYWORD_DICT_BEGIN)
+ i += 1
+ return (self.parse_main, i)
+
+ def parse_wclose(self, s, i):
+ c = s[i]
+ if c == '>':
+ self.add_token(KEYWORD_DICT_END)
+ i += 1
+ return (self.parse_main, i)
+
+ def parse_hexstring(self, s, i):
+ m = END_HEX_STRING.search(s, i)
+ if not m:
+ self.token += s[i:]
+ return (self.parse_hexstring, len(s))
+ j = m.start(0)
+ self.token += s[i:j]
+ token = HEX_PAIR.sub(lambda m: chr(int(m.group(0), 16)),
+ SPC.sub('', self.token))
+ self.add_token(token)
+ return (self.parse_main, j)
+
+ def nexttoken(self):
+ while not self.tokens:
+ self.fillbuf()
+ (self.parse1, self.charpos) = self.parse1(self.buf, self.charpos)
+ token = self.tokens.pop(0)
+ return token
+
+ def nextline(self):
+ '''
+ Fetches a next line that ends either with \\r or \\n.
+ '''
+ linebuf = ''
+ linepos = self.bufpos + self.charpos
+ eol = False
+ while 1:
+ self.fillbuf()
+ if eol:
+ c = self.buf[self.charpos]
+ # handle '\r\n'
+ if c == '\n':
+ linebuf += c
+ self.charpos += 1
+ break
+ m = EOL.search(self.buf, self.charpos)
+ if m:
+ linebuf += self.buf[self.charpos:m.end(0)]
+ self.charpos = m.end(0)
+ if linebuf[-1] == '\r':
+ eol = True
+ else:
+ break
+ else:
+ linebuf += self.buf[self.charpos:]
+ self.charpos = len(self.buf)
+ return (linepos, linebuf)
+
+ def revreadlines(self):
+ '''
+ Fetches a next line backword. This is used to locate
+ the trailers at the end of a file.
+ '''
+ self.fp.seek(0, 2)
+ pos = self.fp.tell()
+ buf = ''
+ while 0 < pos:
+ prevpos = pos
+ pos = max(0, pos-self.BUFSIZ)
+ self.fp.seek(pos)
+ s = self.fp.read(prevpos-pos)
+ if not s: break
+ while 1:
+ n = max(s.rfind('\r'), s.rfind('\n'))
+ if n == -1:
+ buf = s + buf
+ break
+ yield s[n:]+buf
+ s = s[:n]
+ buf = ''
+ return
+
+
+## PSStackParser
+##
+class PSStackParser(PSBaseParser):
+
+ def __init__(self, fp):
+ PSBaseParser.__init__(self, fp)
+ self.reset()
+ return
+
+ def reset(self):
+ self.context = []
+ self.curtype = None
+ self.curstack = []
+ self.results = []
+ return
+
+ def seek(self, pos):
+ PSBaseParser.seek(self, pos)
+ self.reset()
+ return
+
+ def push(self, *objs):
+ self.curstack.extend(objs)
+ return
+ def pop(self, n):
+ objs = self.curstack[-n:]
+ self.curstack[-n:] = []
+ return objs
+ def popall(self):
+ objs = self.curstack
+ self.curstack = []
+ return objs
+ def add_results(self, *objs):
+ self.results.extend(objs)
+ return
+
+ def start_type(self, pos, type):
+ self.context.append((pos, self.curtype, self.curstack))
+ (self.curtype, self.curstack) = (type, [])
+ return
+ def end_type(self, type):
+ if self.curtype != type:
+ raise PSTypeError('Type mismatch: %r != %r' % (self.curtype, type))
+ objs = [ obj for (_,obj) in self.curstack ]
+ (pos, self.curtype, self.curstack) = self.context.pop()
+ return (pos, objs)
+
+ def do_keyword(self, pos, token):
+ return
+
+ def nextobject(self, direct=False):
+ '''
+ Yields a list of objects: keywords, literals, strings,
+ numbers, arrays and dictionaries. Arrays and dictionaries
+ are represented as Python sequence and dictionaries.
+ '''
+ while not self.results:
+ (pos, token) = self.nexttoken()
+ ##print (pos,token), (self.curtype, self.curstack)
+ if (isinstance(token, int) or
+ isinstance(token, float) or
+ isinstance(token, bool) or
+ isinstance(token, str) or
+ isinstance(token, PSLiteral)):
+ # normal token
+ self.push((pos, token))
+ elif token == KEYWORD_ARRAY_BEGIN:
+ # begin array
+ self.start_type(pos, 'a')
+ elif token == KEYWORD_ARRAY_END:
+ # end array
+ try:
+ self.push(self.end_type('a'))
+ except PSTypeError:
+ if STRICT: raise
+ elif token == KEYWORD_DICT_BEGIN:
+ # begin dictionary
+ self.start_type(pos, 'd')
+ elif token == KEYWORD_DICT_END:
+ # end dictionary
+ try:
+ (pos, objs) = self.end_type('d')
+ if len(objs) % 2 != 0:
+ raise PSSyntaxError(
+ 'Invalid dictionary construct: %r' % objs)
+ d = dict((literal_name(k), v) \
+ for (k,v) in choplist(2, objs))
+ self.push((pos, d))
+ except PSTypeError:
+ if STRICT: raise
+ else:
+ self.do_keyword(pos, token)
+ if self.context:
+ continue
+ else:
+ if direct:
+ return self.pop(1)[0]
+ self.flush()
+ obj = self.results.pop(0)
+ return obj
+
+
+LITERAL_CRYPT = PSLiteralTable.intern('Crypt')
+LITERALS_FLATE_DECODE = (PSLiteralTable.intern('FlateDecode'), PSLiteralTable.intern('Fl'))
+LITERALS_LZW_DECODE = (PSLiteralTable.intern('LZWDecode'), PSLiteralTable.intern('LZW'))
+LITERALS_ASCII85_DECODE = (PSLiteralTable.intern('ASCII85Decode'), PSLiteralTable.intern('A85'))
+
+
+## PDF Objects
+##
+class PDFObject(PSObject): pass
+
+class PDFException(PSException): pass
+class PDFTypeError(PDFException): pass
+class PDFValueError(PDFException): pass
+class PDFNotImplementedError(PSException): pass
+
+
+## PDFObjRef
+##
+class PDFObjRef(PDFObject):
+
+ def __init__(self, doc, objid, genno):
+ if objid == 0:
+ if STRICT:
+ raise PDFValueError('PDF object id cannot be 0.')
+ self.doc = doc
+ self.objid = objid
+ self.genno = genno
+ return
+
+ def __repr__(self):
+ return '' % (self.objid, self.genno)
+
+ def resolve(self):
+ return self.doc.getobj(self.objid)
+
+
+# resolve
+def resolve1(x):
+ '''
+ Resolve an object. If this is an array or dictionary,
+ it may still contains some indirect objects inside.
+ '''
+ while isinstance(x, PDFObjRef):
+ x = x.resolve()
+ return x
+
+def resolve_all(x):
+ '''
+ Recursively resolve X and all the internals.
+ Make sure there is no indirect reference within the nested object.
+ This procedure might be slow.
+ '''
+ while isinstance(x, PDFObjRef):
+ x = x.resolve()
+ if isinstance(x, list):
+ x = [ resolve_all(v) for v in x ]
+ elif isinstance(x, dict):
+ for (k,v) in x.iteritems():
+ x[k] = resolve_all(v)
+ return x
+
+def decipher_all(decipher, objid, genno, x):
+ '''
+ Recursively decipher X.
+ '''
+ if isinstance(x, str):
+ return decipher(objid, genno, x)
+ decf = lambda v: decipher_all(decipher, objid, genno, v)
+ if isinstance(x, list):
+ x = [decf(v) for v in x]
+ elif isinstance(x, dict):
+ x = dict((k, decf(v)) for (k, v) in x.iteritems())
+ return x
+
+
+# Type cheking
+def int_value(x):
+ x = resolve1(x)
+ if not isinstance(x, int):
+ if STRICT:
+ raise PDFTypeError('Integer required: %r' % x)
+ return 0
+ return x
+
+def float_value(x):
+ x = resolve1(x)
+ if not isinstance(x, float):
+ if STRICT:
+ raise PDFTypeError('Float required: %r' % x)
+ return 0.0
+ return x
+
+def num_value(x):
+ x = resolve1(x)
+ if not (isinstance(x, int) or isinstance(x, float)):
+ if STRICT:
+ raise PDFTypeError('Int or Float required: %r' % x)
+ return 0
+ return x
+
+def str_value(x):
+ x = resolve1(x)
+ if not isinstance(x, str):
+ if STRICT:
+ raise PDFTypeError('String required: %r' % x)
+ return ''
+ return x
+
+def list_value(x):
+ x = resolve1(x)
+ if not (isinstance(x, list) or isinstance(x, tuple)):
+ if STRICT:
+ raise PDFTypeError('List required: %r' % x)
+ return []
+ return x
+
+def dict_value(x):
+ x = resolve1(x)
+ if not isinstance(x, dict):
+ if STRICT:
+ raise PDFTypeError('Dict required: %r' % x)
+ return {}
+ return x
+
+def stream_value(x):
+ x = resolve1(x)
+ if not isinstance(x, PDFStream):
+ if STRICT:
+ raise PDFTypeError('PDFStream required: %r' % x)
+ return PDFStream({}, '')
+ return x
+
+# ascii85decode(data)
+def ascii85decode(data):
+ n = b = 0
+ out = ''
+ for c in data:
+ if '!' <= c and c <= 'u':
+ n += 1
+ b = b*85+(ord(c)-33)
+ if n == 5:
+ out += struct.pack('>L',b)
+ n = b = 0
+ elif c == 'z':
+ assert n == 0
+ out += '\0\0\0\0'
+ elif c == '~':
+ if n:
+ for _ in range(5-n):
+ b = b*85+84
+ out += struct.pack('>L',b)[:n-1]
+ break
+ return out
+
+
+## PDFStream type
+class PDFStream(PDFObject):
+ def __init__(self, dic, rawdata, decipher=None):
+ length = int_value(dic.get('Length', 0))
+ eol = rawdata[length:]
+ # quick and dirty fix for false length attribute,
+ # might not work if the pdf stream parser has a problem
+ if decipher != None and decipher.__name__ == 'decrypt_aes':
+ if (len(rawdata) % 16) != 0:
+ cutdiv = len(rawdata) // 16
+ rawdata = rawdata[:16*cutdiv]
+ else:
+ if eol in ('\r', '\n', '\r\n'):
+ rawdata = rawdata[:length]
+
+ self.dic = dic
+ self.rawdata = rawdata
+ self.decipher = decipher
+ self.data = None
+ self.decdata = None
+ self.objid = None
+ self.genno = None
+ return
+
+ def set_objid(self, objid, genno):
+ self.objid = objid
+ self.genno = genno
+ return
+
+ def __repr__(self):
+ if self.rawdata:
+ return '' % \
+ (self.objid, len(self.rawdata), self.dic)
+ else:
+ return '' % \
+ (self.objid, len(self.data), self.dic)
+
+ def decode(self):
+ assert self.data is None and self.rawdata is not None
+ data = self.rawdata
+ if self.decipher:
+ # Handle encryption
+ data = self.decipher(self.objid, self.genno, data)
+ if gen_xref_stm:
+ self.decdata = data # keep decrypted data
+ if 'Filter' not in self.dic:
+ self.data = data
+ self.rawdata = None
+ ##print self.dict
+ return
+ filters = self.dic['Filter']
+ if not isinstance(filters, list):
+ filters = [ filters ]
+ for f in filters:
+ if f in LITERALS_FLATE_DECODE:
+ # will get errors if the document is encrypted.
+ data = zlib.decompress(data)
+ elif f in LITERALS_LZW_DECODE:
+ data = ''.join(LZWDecoder(StringIO(data)).run())
+ elif f in LITERALS_ASCII85_DECODE:
+ data = ascii85decode(data)
+ elif f == LITERAL_CRYPT:
+ raise PDFNotImplementedError('/Crypt filter is unsupported')
+ else:
+ raise PDFNotImplementedError('Unsupported filter: %r' % f)
+ # apply predictors
+ if 'DP' in self.dic:
+ params = self.dic['DP']
+ else:
+ params = self.dic.get('DecodeParms', {})
+ if 'Predictor' in params:
+ pred = int_value(params['Predictor'])
+ if pred:
+ if pred != 12:
+ raise PDFNotImplementedError(
+ 'Unsupported predictor: %r' % pred)
+ if 'Columns' not in params:
+ raise PDFValueError(
+ 'Columns undefined for predictor=12')
+ columns = int_value(params['Columns'])
+ buf = ''
+ ent0 = '\x00' * columns
+ for i in xrange(0, len(data), columns+1):
+ pred = data[i]
+ ent1 = data[i+1:i+1+columns]
+ if pred == '\x02':
+ ent1 = ''.join(chr((ord(a)+ord(b)) & 255) \
+ for (a,b) in zip(ent0,ent1))
+ buf += ent1
+ ent0 = ent1
+ data = buf
+ self.data = data
+ self.rawdata = None
+ return
+
+ def get_data(self):
+ if self.data is None:
+ self.decode()
+ return self.data
+
+ def get_rawdata(self):
+ return self.rawdata
+
+ def get_decdata(self):
+ if self.decdata is not None:
+ return self.decdata
+ data = self.rawdata
+ if self.decipher and data:
+ # Handle encryption
+ data = self.decipher(self.objid, self.genno, data)
+ return data
+
+
+## PDF Exceptions
+##
+class PDFSyntaxError(PDFException): pass
+class PDFNoValidXRef(PDFSyntaxError): pass
+class PDFEncryptionError(PDFException): pass
+class PDFPasswordIncorrect(PDFEncryptionError): pass
+
+# some predefined literals and keywords.
+LITERAL_OBJSTM = PSLiteralTable.intern('ObjStm')
+LITERAL_XREF = PSLiteralTable.intern('XRef')
+LITERAL_PAGE = PSLiteralTable.intern('Page')
+LITERAL_PAGES = PSLiteralTable.intern('Pages')
+LITERAL_CATALOG = PSLiteralTable.intern('Catalog')
+
+
+## XRefs
+##
+
+## PDFXRef
+##
+class PDFXRef(object):
+
+ def __init__(self):
+ self.offsets = None
+ return
+
+ def __repr__(self):
+ return '' % len(self.offsets)
+
+ def objids(self):
+ return self.offsets.iterkeys()
+
+ def load(self, parser):
+ self.offsets = {}
+ while 1:
+ try:
+ (pos, line) = parser.nextline()
+ except PSEOF:
+ raise PDFNoValidXRef('Unexpected EOF - file corrupted?')
+ if not line:
+ raise PDFNoValidXRef('Premature eof: %r' % parser)
+ if line.startswith('trailer'):
+ parser.seek(pos)
+ break
+ f = line.strip().split(' ')
+ if len(f) != 2:
+ raise PDFNoValidXRef('Trailer not found: %r: line=%r' % (parser, line))
+ try:
+ (start, nobjs) = map(int, f)
+ except ValueError:
+ raise PDFNoValidXRef('Invalid line: %r: line=%r' % (parser, line))
+ for objid in xrange(start, start+nobjs):
+ try:
+ (_, line) = parser.nextline()
+ except PSEOF:
+ raise PDFNoValidXRef('Unexpected EOF - file corrupted?')
+ f = line.strip().split(' ')
+ if len(f) != 3:
+ raise PDFNoValidXRef('Invalid XRef format: %r, line=%r' % (parser, line))
+ (pos, genno, use) = f
+ if use != 'n': continue
+ self.offsets[objid] = (int(genno), int(pos))
+ self.load_trailer(parser)
+ return
+
+ KEYWORD_TRAILER = PSKeywordTable.intern('trailer')
+ def load_trailer(self, parser):
+ try:
+ (_,kwd) = parser.nexttoken()
+ assert kwd is self.KEYWORD_TRAILER
+ (_,dic) = parser.nextobject(direct=True)
+ except PSEOF:
+ x = parser.pop(1)
+ if not x:
+ raise PDFNoValidXRef('Unexpected EOF - file corrupted')
+ (_,dic) = x[0]
+ self.trailer = dict_value(dic)
+ return
+
+ def getpos(self, objid):
+ try:
+ (genno, pos) = self.offsets[objid]
+ except KeyError:
+ raise
+ return (None, pos)
+
+
+## PDFXRefStream
+##
+class PDFXRefStream(object):
+
+ def __init__(self):
+ self.index = None
+ self.data = None
+ self.entlen = None
+ self.fl1 = self.fl2 = self.fl3 = None
+ return
+
+ def __repr__(self):
+ return '' % self.index
+
+ def objids(self):
+ for first, size in self.index:
+ for objid in xrange(first, first + size):
+ yield objid
+
+ def load(self, parser, debug=0):
+ (_,objid) = parser.nexttoken() # ignored
+ (_,genno) = parser.nexttoken() # ignored
+ (_,kwd) = parser.nexttoken()
+ (_,stream) = parser.nextobject()
+ if not isinstance(stream, PDFStream) or \
+ stream.dic['Type'] is not LITERAL_XREF:
+ raise PDFNoValidXRef('Invalid PDF stream spec.')
+ size = stream.dic['Size']
+ index = stream.dic.get('Index', (0,size))
+ self.index = zip(islice(index, 0, None, 2),
+ islice(index, 1, None, 2))
+ (self.fl1, self.fl2, self.fl3) = stream.dic['W']
+ self.data = stream.get_data()
+ self.entlen = self.fl1+self.fl2+self.fl3
+ self.trailer = stream.dic
+ return
+
+ def getpos(self, objid):
+ offset = 0
+ for first, size in self.index:
+ if first <= objid and objid < (first + size):
+ break
+ offset += size
+ else:
+ raise KeyError(objid)
+ i = self.entlen * ((objid - first) + offset)
+ ent = self.data[i:i+self.entlen]
+ f1 = nunpack(ent[:self.fl1], 1)
+ if f1 == 1:
+ pos = nunpack(ent[self.fl1:self.fl1+self.fl2])
+ genno = nunpack(ent[self.fl1+self.fl2:])
+ return (None, pos)
+ elif f1 == 2:
+ objid = nunpack(ent[self.fl1:self.fl1+self.fl2])
+ index = nunpack(ent[self.fl1+self.fl2:])
+ return (objid, index)
+ # this is a free object
+ raise KeyError(objid)
+
+
+## PDFDocument
+##
+## A PDFDocument object represents a PDF document.
+## Since a PDF file is usually pretty big, normally it is not loaded
+## at once. Rather it is parsed dynamically as processing goes.
+## A PDF parser is associated with the document.
+##
+class PDFDocument(object):
+
+ def __init__(self):
+ self.xrefs = []
+ self.objs = {}
+ self.parsed_objs = {}
+ self.root = None
+ self.catalog = None
+ self.parser = None
+ self.encryption = None
+ self.decipher = None
+ return
+
+ # set_parser(parser)
+ # Associates the document with an (already initialized) parser object.
+ def set_parser(self, parser):
+ if self.parser: return
+ self.parser = parser
+ # The document is set to be temporarily ready during collecting
+ # all the basic information about the document, e.g.
+ # the header, the encryption information, and the access rights
+ # for the document.
+ self.ready = True
+ # Retrieve the information of each header that was appended
+ # (maybe multiple times) at the end of the document.
+ self.xrefs = parser.read_xref()
+ for xref in self.xrefs:
+ trailer = xref.trailer
+ if not trailer: continue
+
+ # If there's an encryption info, remember it.
+ if 'Encrypt' in trailer:
+ #assert not self.encryption
+ try:
+ self.encryption = (list_value(trailer['ID']),
+ dict_value(trailer['Encrypt']))
+ # fix for bad files
+ except:
+ self.encryption = ('ffffffffffffffffffffffffffffffffffff',
+ dict_value(trailer['Encrypt']))
+ if 'Root' in trailer:
+ self.set_root(dict_value(trailer['Root']))
+ break
+ else:
+ raise PDFSyntaxError('No /Root object! - Is this really a PDF?')
+ # The document is set to be non-ready again, until all the
+ # proper initialization (asking the password key and
+ # verifying the access permission, so on) is finished.
+ self.ready = False
+ return
+
+ # set_root(root)
+ # Set the Root dictionary of the document.
+ # Each PDF file must have exactly one /Root dictionary.
+ def set_root(self, root):
+ self.root = root
+ self.catalog = dict_value(self.root)
+ if self.catalog.get('Type') is not LITERAL_CATALOG:
+ if STRICT:
+ raise PDFSyntaxError('Catalog not found!')
+ return
+ # initialize(password='')
+ # Perform the initialization with a given password.
+ # This step is mandatory even if there's no password associated
+ # with the document.
+ def initialize(self, password=''):
+ if not self.encryption:
+ self.is_printable = self.is_modifiable = self.is_extractable = True
+ self.ready = True
+ return
+ (docid, param) = self.encryption
+ type = literal_name(param['Filter'])
+ if type == 'Adobe.APS':
+ return self.initialize_adobe_ps(password, docid, param)
+ if type == 'Standard':
+ return self.initialize_standard(password, docid, param)
+ if type == 'EBX_HANDLER':
+ return self.initialize_ebx(password, docid, param)
+ raise PDFEncryptionError('Unknown filter: param=%r' % param)
+
+ def initialize_adobe_ps(self, password, docid, param):
+ global KEYFILEPATH
+ self.decrypt_key = self.genkey_adobe_ps(param)
+ self.genkey = self.genkey_v4
+ self.decipher = self.decrypt_aes
+ self.ready = True
+ return
+
+ def genkey_adobe_ps(self, param):
+ # nice little offline principal keys dictionary
+ # global static principal key for German Onleihe / Bibliothek Digital
+ principalkeys = { 'bibliothek-digital.de': 'rRwGv2tbpKov1krvv7PO0ws9S436/lArPlfipz5Pqhw='.decode('base64')}
+ self.is_printable = self.is_modifiable = self.is_extractable = True
+ length = int_value(param.get('Length', 0)) / 8
+ edcdata = str_value(param.get('EDCData')).decode('base64')
+ pdrllic = str_value(param.get('PDRLLic')).decode('base64')
+ pdrlpol = str_value(param.get('PDRLPol')).decode('base64')
+ edclist = []
+ for pair in edcdata.split('\n'):
+ edclist.append(pair)
+ # principal key request
+ for key in principalkeys:
+ if key in pdrllic:
+ principalkey = principalkeys[key]
+ else:
+ raise ADEPTError('Cannot find principal key for this pdf')
+ shakey = SHA256(principalkey)
+ ivector = 16 * chr(0)
+ plaintext = AES.new(shakey,AES.MODE_CBC,ivector).decrypt(edclist[9].decode('base64'))
+ if plaintext[-16:] != 16 * chr(16):
+ raise ADEPTError('Offlinekey cannot be decrypted, aborting ...')
+ pdrlpol = AES.new(plaintext[16:32],AES.MODE_CBC,edclist[2].decode('base64')).decrypt(pdrlpol)
+ if ord(pdrlpol[-1]) < 1 or ord(pdrlpol[-1]) > 16:
+ raise ADEPTError('Could not decrypt PDRLPol, aborting ...')
+ else:
+ cutter = -1 * ord(pdrlpol[-1])
+ pdrlpol = pdrlpol[:cutter]
+ return plaintext[:16]
+
+ PASSWORD_PADDING = '(\xbfN^Nu\x8aAd\x00NV\xff\xfa\x01\x08..' \
+ '\x00\xb6\xd0h>\x80/\x0c\xa9\xfedSiz'
+ # experimental aes pw support
+ def initialize_standard(self, password, docid, param):
+ # copy from a global variable
+ V = int_value(param.get('V', 0))
+ if (V <=0 or V > 4):
+ raise PDFEncryptionError('Unknown algorithm: param=%r' % param)
+ length = int_value(param.get('Length', 40)) # Key length (bits)
+ O = str_value(param['O'])
+ R = int_value(param['R']) # Revision
+ if 5 <= R:
+ raise PDFEncryptionError('Unknown revision: %r' % R)
+ U = str_value(param['U'])
+ P = int_value(param['P'])
+ try:
+ EncMetadata = str_value(param['EncryptMetadata'])
+ except:
+ EncMetadata = 'True'
+ self.is_printable = bool(P & 4)
+ self.is_modifiable = bool(P & 8)
+ self.is_extractable = bool(P & 16)
+ self.is_annotationable = bool(P & 32)
+ self.is_formsenabled = bool(P & 256)
+ self.is_textextractable = bool(P & 512)
+ self.is_assemblable = bool(P & 1024)
+ self.is_formprintable = bool(P & 2048)
+ # Algorithm 3.2
+ password = (password+self.PASSWORD_PADDING)[:32] # 1
+ hash = hashlib.md5(password) # 2
+ hash.update(O) # 3
+ hash.update(struct.pack('= 3:
+ # Algorithm 3.5
+ hash = hashlib.md5(self.PASSWORD_PADDING) # 2
+ hash.update(docid[0]) # 3
+ x = ARC4.new(key).decrypt(hash.digest()[:16]) # 4
+ for i in xrange(1,19+1):
+ k = ''.join( chr(ord(c) ^ i) for c in key )
+ x = ARC4.new(k).decrypt(x)
+ u1 = x+x # 32bytes total
+ if R == 2:
+ is_authenticated = (u1 == U)
+ else:
+ is_authenticated = (u1[:16] == U[:16])
+ if not is_authenticated:
+ raise ADEPTError('Password is not correct.')
+ self.decrypt_key = key
+ # genkey method
+ if V == 1 or V == 2:
+ self.genkey = self.genkey_v2
+ elif V == 3:
+ self.genkey = self.genkey_v3
+ elif V == 4:
+ self.genkey = self.genkey_v2
+ #self.genkey = self.genkey_v3 if V == 3 else self.genkey_v2
+ # rc4
+ if V != 4:
+ self.decipher = self.decipher_rc4 # XXX may be AES
+ # aes
+ elif V == 4 and Length == 128:
+ elf.decipher = self.decipher_aes
+ elif V == 4 and Length == 256:
+ raise PDFNotImplementedError('AES256 encryption is currently unsupported')
+ self.ready = True
+ return
+
+ def initialize_ebx(self, password, docid, param):
+ self.is_printable = self.is_modifiable = self.is_extractable = True
+ with open(password, 'rb') as f:
+ keyder = f.read()
+ rsa = RSA(keyder)
+ length = int_value(param.get('Length', 0)) / 8
+ rights = str_value(param.get('ADEPT_LICENSE')).decode('base64')
+ rights = zlib.decompress(rights, -15)
+ rights = etree.fromstring(rights)
+ expr = './/{http://ns.adobe.com/adept}encryptedKey'
+ bookkey = ''.join(rights.findtext(expr)).decode('base64')
+ bookkey = rsa.decrypt(bookkey)
+ if bookkey[0] != '\x02':
+ raise ADEPTError('error decrypting book session key')
+ index = bookkey.index('\0') + 1
+ bookkey = bookkey[index:]
+ ebx_V = int_value(param.get('V', 4))
+ ebx_type = int_value(param.get('EBX_ENCRYPTIONTYPE', 6))
+ # added because of the booktype / decryption book session key error
+ if ebx_V == 3:
+ V = 3
+ elif ebx_V < 4 or ebx_type < 6:
+ V = ord(bookkey[0])
+ bookkey = bookkey[1:]
+ else:
+ V = 2
+ if length and len(bookkey) != length:
+ raise ADEPTError('error decrypting book session key')
+ self.decrypt_key = bookkey
+ self.genkey = self.genkey_v3 if V == 3 else self.genkey_v2
+ self.decipher = self.decrypt_rc4
+ self.ready = True
+ return
+
+ # genkey functions
+ def genkey_v2(self, objid, genno):
+ objid = struct.pack(' PDFObjStmRef.maxindex:
+ PDFObjStmRef.maxindex = index
+
+
+## PDFParser
+##
+class PDFParser(PSStackParser):
+
+ def __init__(self, doc, fp):
+ PSStackParser.__init__(self, fp)
+ self.doc = doc
+ self.doc.set_parser(self)
+ return
+
+ def __repr__(self):
+ return ''
+
+ KEYWORD_R = PSKeywordTable.intern('R')
+ KEYWORD_ENDOBJ = PSKeywordTable.intern('endobj')
+ KEYWORD_STREAM = PSKeywordTable.intern('stream')
+ KEYWORD_XREF = PSKeywordTable.intern('xref')
+ KEYWORD_STARTXREF = PSKeywordTable.intern('startxref')
+ def do_keyword(self, pos, token):
+ if token in (self.KEYWORD_XREF, self.KEYWORD_STARTXREF):
+ self.add_results(*self.pop(1))
+ return
+ if token is self.KEYWORD_ENDOBJ:
+ self.add_results(*self.pop(4))
+ return
+
+ if token is self.KEYWORD_R:
+ # reference to indirect object
+ try:
+ ((_,objid), (_,genno)) = self.pop(2)
+ (objid, genno) = (int(objid), int(genno))
+ obj = PDFObjRef(self.doc, objid, genno)
+ self.push((pos, obj))
+ except PSSyntaxError:
+ pass
+ return
+
+ if token is self.KEYWORD_STREAM:
+ # stream object
+ ((_,dic),) = self.pop(1)
+ dic = dict_value(dic)
+ try:
+ objlen = int_value(dic['Length'])
+ except KeyError:
+ if STRICT:
+ raise PDFSyntaxError('/Length is undefined: %r' % dic)
+ objlen = 0
+ self.seek(pos)
+ try:
+ (_, line) = self.nextline() # 'stream'
+ except PSEOF:
+ if STRICT:
+ raise PDFSyntaxError('Unexpected EOF')
+ return
+ pos += len(line)
+ self.fp.seek(pos)
+ data = self.fp.read(objlen)
+ self.seek(pos+objlen)
+ while 1:
+ try:
+ (linepos, line) = self.nextline()
+ except PSEOF:
+ if STRICT:
+ raise PDFSyntaxError('Unexpected EOF')
+ break
+ if 'endstream' in line:
+ i = line.index('endstream')
+ objlen += i
+ data += line[:i]
+ break
+ objlen += len(line)
+ data += line
+ self.seek(pos+objlen)
+ obj = PDFStream(dic, data, self.doc.decipher)
+ self.push((pos, obj))
+ return
+
+ # others
+ self.push((pos, token))
+ return
+
+ def find_xref(self):
+ # search the last xref table by scanning the file backwards.
+ prev = None
+ for line in self.revreadlines():
+ line = line.strip()
+ if line == 'startxref': break
+ if line:
+ prev = line
+ else:
+ raise PDFNoValidXRef('Unexpected EOF')
+ return int(prev)
+
+ # read xref table
+ def read_xref_from(self, start, xrefs):
+ self.seek(start)
+ self.reset()
+ try:
+ (pos, token) = self.nexttoken()
+ except PSEOF:
+ raise PDFNoValidXRef('Unexpected EOF')
+ if isinstance(token, int):
+ # XRefStream: PDF-1.5
+ if GEN_XREF_STM == 1:
+ global gen_xref_stm
+ gen_xref_stm = True
+ self.seek(pos)
+ self.reset()
+ xref = PDFXRefStream()
+ xref.load(self)
+ else:
+ if token is not self.KEYWORD_XREF:
+ raise PDFNoValidXRef('xref not found: pos=%d, token=%r' %
+ (pos, token))
+ self.nextline()
+ xref = PDFXRef()
+ xref.load(self)
+ xrefs.append(xref)
+ trailer = xref.trailer
+ if 'XRefStm' in trailer:
+ pos = int_value(trailer['XRefStm'])
+ self.read_xref_from(pos, xrefs)
+ if 'Prev' in trailer:
+ # find previous xref
+ pos = int_value(trailer['Prev'])
+ self.read_xref_from(pos, xrefs)
+ return
+
+ # read xref tables and trailers
+ def read_xref(self):
+ xrefs = []
+ trailerpos = None
+ try:
+ pos = self.find_xref()
+ self.read_xref_from(pos, xrefs)
+ except PDFNoValidXRef:
+ # fallback
+ self.seek(0)
+ pat = re.compile(r'^(\d+)\s+(\d+)\s+obj\b')
+ offsets = {}
+ xref = PDFXRef()
+ while 1:
+ try:
+ (pos, line) = self.nextline()
+ except PSEOF:
+ break
+ if line.startswith('trailer'):
+ trailerpos = pos # remember last trailer
+ m = pat.match(line)
+ if not m: continue
+ (objid, genno) = m.groups()
+ offsets[int(objid)] = (0, pos)
+ if not offsets: raise
+ xref.offsets = offsets
+ if trailerpos:
+ self.seek(trailerpos)
+ xref.load_trailer(self)
+ xrefs.append(xref)
+ return xrefs
+
+## PDFObjStrmParser
+##
+class PDFObjStrmParser(PDFParser):
+
+ def __init__(self, data, doc):
+ PSStackParser.__init__(self, StringIO(data))
+ self.doc = doc
+ return
+
+ def flush(self):
+ self.add_results(*self.popall())
+ return
+
+ KEYWORD_R = KWD('R')
+ def do_keyword(self, pos, token):
+ if token is self.KEYWORD_R:
+ # reference to indirect object
+ try:
+ ((_,objid), (_,genno)) = self.pop(2)
+ (objid, genno) = (int(objid), int(genno))
+ obj = PDFObjRef(self.doc, objid, genno)
+ self.push((pos, obj))
+ except PSSyntaxError:
+ pass
+ return
+ # others
+ self.push((pos, token))
+ return
+
+###
+### My own code, for which there is none else to blame
+
+class PDFSerializer(object):
+ def __init__(self, inf, keypath):
+ global GEN_XREF_STM, gen_xref_stm
+ gen_xref_stm = GEN_XREF_STM > 1
+ self.version = inf.read(8)
+ inf.seek(0)
+ self.doc = doc = PDFDocument()
+ parser = PDFParser(doc, inf)
+ doc.initialize(keypath)
+ self.objids = objids = set()
+ for xref in reversed(doc.xrefs):
+ trailer = xref.trailer
+ for objid in xref.objids():
+ objids.add(objid)
+ trailer = dict(trailer)
+ trailer.pop('Prev', None)
+ trailer.pop('XRefStm', None)
+ if 'Encrypt' in trailer:
+ objids.remove(trailer.pop('Encrypt').objid)
+ self.trailer = trailer
+
+ def dump(self, outf):
+ self.outf = outf
+ self.write(self.version)
+ self.write('\n%\xe2\xe3\xcf\xd3\n')
+ doc = self.doc
+ objids = self.objids
+ xrefs = {}
+ maxobj = max(objids)
+ trailer = dict(self.trailer)
+ trailer['Size'] = maxobj + 1
+ for objid in objids:
+ obj = doc.getobj(objid)
+ if isinstance(obj, PDFObjStmRef):
+ xrefs[objid] = obj
+ continue
+ if obj is not None:
+ try:
+ genno = obj.genno
+ except AttributeError:
+ genno = 0
+ xrefs[objid] = (self.tell(), genno)
+ self.serialize_indirect(objid, obj)
+ startxref = self.tell()
+
+ if not gen_xref_stm:
+ self.write('xref\n')
+ self.write('0 %d\n' % (maxobj + 1,))
+ for objid in xrange(0, maxobj + 1):
+ if objid in xrefs:
+ # force the genno to be 0
+ self.write("%010d 00000 n \n" % xrefs[objid][0])
+ else:
+ self.write("%010d %05d f \n" % (0, 65535))
+
+ self.write('trailer\n')
+ self.serialize_object(trailer)
+ self.write('\nstartxref\n%d\n%%%%EOF' % startxref)
+
+ else: # Generate crossref stream.
+
+ # Calculate size of entries
+ maxoffset = max(startxref, maxobj)
+ maxindex = PDFObjStmRef.maxindex
+ fl2 = 2
+ power = 65536
+ while maxoffset >= power:
+ fl2 += 1
+ power *= 256
+ fl3 = 1
+ power = 256
+ while maxindex >= power:
+ fl3 += 1
+ power *= 256
+
+ index = []
+ first = None
+ prev = None
+ data = []
+ # Put the xrefstream's reference in itself
+ startxref = self.tell()
+ maxobj += 1
+ xrefs[maxobj] = (startxref, 0)
+ for objid in sorted(xrefs):
+ if first is None:
+ first = objid
+ elif objid != prev + 1:
+ index.extend((first, prev - first + 1))
+ first = objid
+ prev = objid
+ objref = xrefs[objid]
+ if isinstance(objref, PDFObjStmRef):
+ f1 = 2
+ f2 = objref.stmid
+ f3 = objref.index
+ else:
+ f1 = 1
+ f2 = objref[0]
+ # we force all generation numbers to be 0
+ # f3 = objref[1]
+ f3 = 0
+
+ data.append(struct.pack('>B', f1))
+ data.append(struct.pack('>L', f2)[-fl2:])
+ data.append(struct.pack('>L', f3)[-fl3:])
+ index.extend((first, prev - first + 1))
+ data = zlib.compress(''.join(data))
+ dic = {'Type': LITERAL_XREF, 'Size': prev + 1, 'Index': index,
+ 'W': [1, fl2, fl3], 'Length': len(data),
+ 'Filter': LITERALS_FLATE_DECODE[0],
+ 'Root': trailer['Root'],}
+ if 'Info' in trailer:
+ dic['Info'] = trailer['Info']
+ xrefstm = PDFStream(dic, data)
+ self.serialize_indirect(maxobj, xrefstm)
+ self.write('startxref\n%d\n%%%%EOF' % startxref)
+ def write(self, data):
+ self.outf.write(data)
+ self.last = data[-1:]
+
+ def tell(self):
+ return self.outf.tell()
+
+ def escape_string(self, string):
+ string = string.replace('\\', '\\\\')
+ string = string.replace('\n', r'\n')
+ string = string.replace('(', r'\(')
+ string = string.replace(')', r'\)')
+ # get rid of ciando id
+ regularexp = re.compile(r'http://www.ciando.com/index.cfm/intRefererID/\d{5}')
+ if regularexp.match(string): return ('http://www.ciando.com')
+ return string
+
+ def serialize_object(self, obj):
+ if isinstance(obj, dict):
+ # Correct malformed Mac OS resource forks for Stanza
+ if 'ResFork' in obj and 'Type' in obj and 'Subtype' not in obj \
+ and isinstance(obj['Type'], int):
+ obj['Subtype'] = obj['Type']
+ del obj['Type']
+ # end - hope this doesn't have bad effects
+ self.write('<<')
+ for key, val in obj.items():
+ self.write('/%s' % key)
+ self.serialize_object(val)
+ self.write('>>')
+ elif isinstance(obj, list):
+ self.write('[')
+ for val in obj:
+ self.serialize_object(val)
+ self.write(']')
+ elif isinstance(obj, str):
+ self.write('(%s)' % self.escape_string(obj))
+ elif isinstance(obj, bool):
+ if self.last.isalnum():
+ self.write(' ')
+ self.write(str(obj).lower())
+ elif isinstance(obj, (int, long, float)):
+ if self.last.isalnum():
+ self.write(' ')
+ self.write(str(obj))
+ elif isinstance(obj, PDFObjRef):
+ if self.last.isalnum():
+ self.write(' ')
+ self.write('%d %d R' % (obj.objid, 0))
+ elif isinstance(obj, PDFStream):
+ ### If we don't generate cross ref streams the object streams
+ ### are no longer useful, as we have extracted all objects from
+ ### them. Therefore leave them out from the output.
+ if obj.dic.get('Type') == LITERAL_OBJSTM and not gen_xref_stm:
+ self.write('(deleted)')
+ else:
+ data = obj.get_decdata()
+ self.serialize_object(obj.dic)
+ self.write('stream\n')
+ self.write(data)
+ self.write('\nendstream')
+ else:
+ data = str(obj)
+ if data[0].isalnum() and self.last.isalnum():
+ self.write(' ')
+ self.write(data)
+
+ def serialize_indirect(self, objid, obj):
+ self.write('%d 0 obj' % (objid,))
+ self.serialize_object(obj)
+ if self.last.isalnum():
+ self.write('\n')
+ self.write('endobj\n')
+
+def cli_main(argv=sys.argv):
+ progname = os.path.basename(argv[0])
+ if RSA is None:
+ print "%s: This script requires OpenSSL or PyCrypto, which must be installed " \
+ "separately. Read the top-of-script comment for details." % \
+ (progname,)
+ return 1
+ if len(argv) != 4:
+ print "usage: %s KEYFILE INBOOK OUTBOOK" % (progname,)
+ return 1
+ keypath, inpath, outpath = argv[1:]
+ with open(inpath, 'rb') as inf:
+ serializer = PDFSerializer(inf, keypath)
+ # hope this will fix the 'bad file descriptor' problem
+ with open(outpath, 'wb') as outf:
+ # help construct to make sure the method runs to the end
+ serializer.dump(outf)
+ return 0
+
+
+class DecryptionDialog(Tkinter.Frame):
+ def __init__(self, root):
+ Tkinter.Frame.__init__(self, root, border=5)
+ ltext='Select file for decryption\n'
+ self.status = Tkinter.Label(self, text=ltext)
+ self.status.pack(fill=Tkconstants.X, expand=1)
+ body = Tkinter.Frame(self)
+ body.pack(fill=Tkconstants.X, expand=1)
+ sticky = Tkconstants.E + Tkconstants.W
+ body.grid_columnconfigure(1, weight=2)
+ Tkinter.Label(body, text='Key file').grid(row=0)
+ self.keypath = Tkinter.Entry(body, width=30)
+ self.keypath.grid(row=0, column=1, sticky=sticky)
+ if os.path.exists('adeptkey.der'):
+ self.keypath.insert(0, 'adeptkey.der')
+ button = Tkinter.Button(body, text="...", command=self.get_keypath)
+ button.grid(row=0, column=2)
+ Tkinter.Label(body, text='Input file').grid(row=1)
+ self.inpath = Tkinter.Entry(body, width=30)
+ self.inpath.grid(row=1, column=1, sticky=sticky)
+ button = Tkinter.Button(body, text="...", command=self.get_inpath)
+ button.grid(row=1, column=2)
+ Tkinter.Label(body, text='Output file').grid(row=2)
+ self.outpath = Tkinter.Entry(body, width=30)
+ self.outpath.grid(row=2, column=1, sticky=sticky)
+ button = Tkinter.Button(body, text="...", command=self.get_outpath)
+ button.grid(row=2, column=2)
+ buttons = Tkinter.Frame(self)
+ buttons.pack()
+
+
+ botton = Tkinter.Button(
+ buttons, text="Decrypt", width=10, command=self.decrypt)
+ botton.pack(side=Tkconstants.LEFT)
+ Tkinter.Frame(buttons, width=10).pack(side=Tkconstants.LEFT)
+ button = Tkinter.Button(
+ buttons, text="Quit", width=10, command=self.quit)
+ button.pack(side=Tkconstants.RIGHT)
+
+
+ def get_keypath(self):
+ keypath = tkFileDialog.askopenfilename(
+ parent=None, title='Select ADEPT key file',
+ defaultextension='.der', filetypes=[('DER-encoded files', '.der'),
+ ('All Files', '.*')])
+ if keypath:
+ keypath = os.path.normpath(os.path.realpath(keypath))
+ self.keypath.delete(0, Tkconstants.END)
+ self.keypath.insert(0, keypath)
+ return
+
+ def get_inpath(self):
+ inpath = tkFileDialog.askopenfilename(
+ parent=None, title='Select ADEPT encrypted PDF file to decrypt',
+ defaultextension='.pdf', filetypes=[('PDF files', '.pdf'),
+ ('All files', '.*')])
+ if inpath:
+ inpath = os.path.normpath(os.path.realpath(inpath))
+ self.inpath.delete(0, Tkconstants.END)
+ self.inpath.insert(0, inpath)
+ return
+
+ def get_outpath(self):
+ outpath = tkFileDialog.asksaveasfilename(
+ parent=None, title='Select unencrypted PDF file to produce',
+ defaultextension='.pdf', filetypes=[('PDF files', '.pdf'),
+ ('All files', '.*')])
+ if outpath:
+ outpath = os.path.normpath(os.path.realpath(outpath))
+ self.outpath.delete(0, Tkconstants.END)
+ self.outpath.insert(0, outpath)
+ return
+
+ def decrypt(self):
+ keypath = self.keypath.get()
+ inpath = self.inpath.get()
+ outpath = self.outpath.get()
+ if not keypath or not os.path.exists(keypath):
+ # keyfile doesn't exist
+ self.status['text'] = 'Specified Adept key file does not exist'
+ return
+ if not inpath or not os.path.exists(inpath):
+ self.status['text'] = 'Specified input file does not exist'
+ return
+ if not outpath:
+ self.status['text'] = 'Output file not specified'
+ return
+ if inpath == outpath:
+ self.status['text'] = 'Must have different input and output files'
+ return
+ # patch for non-ascii characters
+ argv = [sys.argv[0], keypath, inpath, outpath]
+ self.status['text'] = 'Processing ...'
+ try:
+ cli_main(argv)
+ except Exception, a:
+ self.status['text'] = 'Error: ' + str(a)
+ return
+ self.status['text'] = 'File successfully decrypted.\n'+\
+ 'Close this window or decrypt another pdf file.'
+ return
+
+def gui_main():
+ root = Tkinter.Tk()
+ if RSA is None:
+ root.withdraw()
+ tkMessageBox.showerror(
+ "INEPT PDF",
+ "This script requires OpenSSL or PyCrypto, which must be installed "
+ "separately. Read the top-of-script comment for details.")
+ return 1
+ root.title('INEPT PDF Decrypter')
+ root.resizable(True, False)
+ root.minsize(370, 0)
+ DecryptionDialog(root).pack(fill=Tkconstants.X, expand=1)
+ root.mainloop()
+ return 0
+
+
+if __name__ == '__main__':
+ if len(sys.argv) > 1:
+ sys.exit(cli_main())
+ sys.exit(gui_main())
diff --git a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/k4mobidedrm.py b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/k4mobidedrm.py
index 5059fc4..f9625a6 100644
--- a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/k4mobidedrm.py
+++ b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/k4mobidedrm.py
@@ -163,6 +163,7 @@ def main(argv=sys.argv):
myzip = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
zipUpDir(myzip, tempdir, '')
myzip.close()
+ shutil.rmtree(tempdir, True)
return 1
if mobi:
@@ -198,7 +199,7 @@ def main(argv=sys.argv):
zipUpDir(myzip3, tempdir, 'img')
myzip3.close()
- shutil.rmtree(tempdir)
+ shutil.rmtree(tempdir, True)
return 0
if __name__ == '__main__':
diff --git a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/k4mutils.py b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/k4mutils.py
index 4aa14dd..1b501ba 100644
--- a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/k4mutils.py
+++ b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/k4mutils.py
@@ -6,7 +6,7 @@ import os
import subprocess
-class K4MDrmException(Exception):
+class DrmException(Exception):
pass
@@ -18,7 +18,7 @@ def _load_crypto_libcrypto():
libcrypto = find_library('crypto')
if libcrypto is None:
- raise K4MDrmException('libcrypto not found')
+ raise DrmException('libcrypto not found')
libcrypto = CDLL(libcrypto)
AES_MAXNR = 14
@@ -51,19 +51,19 @@ def _load_crypto_libcrypto():
def set_decrypt_key(self, userkey, iv):
self._blocksize = len(userkey)
if (self._blocksize != 16) and (self._blocksize != 24) and (self._blocksize != 32) :
- raise K4MDrmException('AES improper key used')
+ raise DrmException('AES improper key used')
return
keyctx = self._keyctx = AES_KEY()
self.iv = iv
rv = AES_set_decrypt_key(userkey, len(userkey) * 8, keyctx)
if rv < 0:
- raise K4MDrmException('Failed to initialize AES key')
+ raise DrmException('Failed to initialize AES key')
def decrypt(self, data):
out = create_string_buffer(len(data))
rv = AES_cbc_encrypt(data, out, len(data), self._keyctx, self.iv, 0)
if rv == 0:
- raise K4MDrmException('AES decryption failed')
+ raise DrmException('AES decryption failed')
return out.raw
def keyivgen(self, passwd):
@@ -81,7 +81,7 @@ def _load_crypto():
LibCrypto = None
try:
LibCrypto = _load_crypto_libcrypto()
- except (ImportError, K4MDrmException):
+ except (ImportError, DrmException):
pass
return LibCrypto
@@ -185,8 +185,10 @@ def openKindleInfo(kInfoFile=None):
if pp >= 0:
kinfopath = resline
break
- if not os.path.exists(kinfopath):
- raise K4MDrmException('Error: .kindle-info file can not be found')
+ if not os.path.isfile(kinfopath):
+ raise DrmException('Error: .kindle-info file can not be found')
return open(kinfopath,'r')
else:
+ if not os.path.isfile(kinfoFile):
+ raise DrmException('Error: kindle-info file can not be found')
return open(kInfoFile, 'r')
diff --git a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/k4pcutils.py b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/k4pcutils.py
index 337b992..efc310d 100644
--- a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/k4pcutils.py
+++ b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/k4pcutils.py
@@ -19,17 +19,12 @@ advapi32 = windll.advapi32
crypt32 = windll.crypt32
-#
# Various character maps used to decrypt books. Probably supposed to act as obfuscation
-#
charMap1 = "n5Pr6St7Uv8Wx9YzAb0Cd1Ef2Gh3Jk4M"
charMap2 = "AaZzB0bYyCc1XxDdW2wEeVv3FfUuG4g-TtHh5SsIiR6rJjQq7KkPpL8lOoMm9Nn_"
charMap3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
charMap4 = "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789"
-#
-# Exceptions for all the problems that might happen during the script
-#
class DrmException(Exception):
pass
@@ -104,7 +99,12 @@ CryptUnprotectData = CryptUnprotectData()
def openKindleInfo(kInfoFile=None):
if kInfoFile == None:
regkey = winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\\")
- path = winreg.QueryValueEx(regkey, 'Local AppData')[0]
- return open(path+'\\Amazon\\Kindle For PC\\{AMAwzsaPaaZAzmZzZQzgZCAkZ3AjA_AY}\\kindle.info','r')
+ path = winreg.QueryValueEx(regkey, 'Local AppData')[0]
+ kinfopath = path +'\\Amazon\\Kindle For PC\\{AMAwzsaPaaZAzmZzZQzgZCAkZ3AjA_AY}\\kindle.info'
+ if not os.path.isfile(kinfopath):
+ raise DrmException('Error: kindle.info file can not be found')
+ return open(kinfopath,'r')
else:
+ if not os.path.isfile(kInfoFile):
+ raise DrmException('Error: kindle.info file can not be found')
return open(kInfoFile, 'r')
diff --git a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/kgenpids.py b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/kgenpids.py
index 5c44bfa..6dcbf73 100644
--- a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/kgenpids.py
+++ b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/kgenpids.py
@@ -83,7 +83,8 @@ def parseKindleInfo(kInfoFile):
DB[splito[0]] =splito[1]
return DB
-# Get a record from the Kindle.info file for the key "hashedKey" (already hashed and encoded). Return the decoded and decrypted record
+# Get a record from the Kindle.info file for the key "hashedKey" (already hashed and encoded).
+# Return the decoded and decrypted record
def getKindleInfoValueForHash(hashedKey):
global kindleDatabase
global charMap1
@@ -95,12 +96,14 @@ def getKindleInfoValueForHash(hashedKey):
cleartext = CryptUnprotectData(encryptedValue)
return decode(cleartext, charMap1)
-# Get a record from the Kindle.info file for the string in "key" (plaintext). Return the decoded and decrypted record
+# Get a record from the Kindle.info file for the string in "key" (plaintext).
+# Return the decoded and decrypted record
def getKindleInfoValueForKey(key):
global charMap2
return getKindleInfoValueForHash(encodeHash(key,charMap2))
-# Find if the original string for a hashed/encoded string is known. If so return the original string othwise return an empty string.
+# Find if the original string for a hashed/encoded string is known.
+# If so return the original string othwise return an empty string.
def findNameForHash(hash):
global charMap2
names = ["kindle.account.tokens","kindle.cookie.item","eulaVersionAccepted","login_date","kindle.token.item","login","kindle.key.item","kindle.name.info","kindle.device.info", "MazamaRandomNumber"]
@@ -222,7 +225,7 @@ def pidFromSerial(s, l):
# Parse the EXTH header records and use the Kindle serial number to calculate the book pid.
def getKindlePid(pidlst, rec209, token, serialnum):
- if rec209 != None:
+ if rec209 != None and token != None:
# Compute book PID
pidHash = SHA1(serialnum+rec209+token)
bookPID = encodePID(pidHash)
@@ -248,6 +251,7 @@ def getK4Pids(pidlst, rec209, token, kInfoFile=None):
kindleDatabase = parseKindleInfo(kInfoFile)
except Exception, message:
print(message)
+ kindleDatabase = None
pass
if kindleDatabase == None :
@@ -272,8 +276,8 @@ def getK4Pids(pidlst, rec209, token, kInfoFile=None):
pidlst.append(devicePID)
# Compute book PID
- if rec209 == None:
- print "\nNo EXTH record type 209 - Perhaps not a K4 file?"
+ if rec209 == None or token == None:
+ print "\nNo EXTH record type 209 or token - Perhaps not a K4 file?"
return pidlst
# Get the kindle account token
diff --git a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/mobidedrm.py b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/mobidedrm.py
index cc83224..864b545 100644
--- a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/mobidedrm.py
+++ b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/mobidedrm.py
@@ -42,8 +42,10 @@
# 0.20 - Correction: It seems that multibyte entries are encrypted in a v6 file.
# 0.21 - Added support for multiple pids
# 0.22 - revised structure to hold MobiBook as a class to allow an extended interface
+# 0.23 - fixed problem with older files with no EXTH section
+# 0.24 - add support for type 1 encryption and 'TEXtREAd' books as well
-__version__ = '0.22'
+__version__ = '0.24'
import sys
@@ -57,6 +59,7 @@ class Unbuffered:
return getattr(self.stream, attr)
sys.stdout=Unbuffered(sys.stdout)
+import os
import struct
import binascii
@@ -154,8 +157,10 @@ class MobiBook:
# initial sanity check on file
self.data_file = file(infile, 'rb').read()
self.header = self.data_file[0:78]
- if self.header[0x3C:0x3C+8] != 'BOOKMOBI':
+ if self.header[0x3C:0x3C+8] != 'BOOKMOBI' and self.header[0x3C:0x3C+8] != 'TEXtREAd':
raise DrmException("invalid file format")
+ self.magic = self.header[0x3C:0x3C+8]
+ self.crypto_type = -1
# build up section offset and flag info
self.num_sections, = struct.unpack('>H', self.header[76:78])
@@ -168,6 +173,14 @@ class MobiBook:
# parse information from section 0
self.sect = self.loadSection(0)
self.records, = struct.unpack('>H', self.sect[0x8:0x8+2])
+
+ if self.magic == 'TEXtREAd':
+ print "Book has format: ", self.magic
+ self.extra_data_flags = 0
+ self.mobi_length = 0
+ self.mobi_version = -1
+ self.meta_array = {}
+ return
self.mobi_length, = struct.unpack('>L',self.sect[0x14:0x18])
self.mobi_version, = struct.unpack('>L',self.sect[0x68:0x6C])
print "MOBI header version = %d, length = %d" %(self.mobi_version, self.mobi_length)
@@ -182,18 +195,23 @@ class MobiBook:
# if exth region exists parse it for metadata array
self.meta_array = {}
- exth_flag, = struct.unpack('>L', self.sect[0x80:0x84])
- exth = ''
- if exth_flag & 0x40:
- exth = self.sect[16 + self.mobi_length:]
- nitems, = struct.unpack('>I', exth[8:12])
- pos = 12
- for i in xrange(nitems):
- type, size = struct.unpack('>II', exth[pos: pos + 8])
- content = exth[pos + 8: pos + size]
- self.meta_array[type] = content
- pos += size
-
+ try:
+ exth_flag, = struct.unpack('>L', self.sect[0x80:0x84])
+ exth = 'NONE'
+ if exth_flag & 0x40:
+ exth = self.sect[16 + self.mobi_length:]
+ if (len(exth) >= 4) and (exth[:4] == 'EXTH'):
+ nitems, = struct.unpack('>I', exth[8:12])
+ pos = 12
+ for i in xrange(nitems):
+ type, size = struct.unpack('>II', exth[pos: pos + 8])
+ content = exth[pos + 8: pos + size]
+ self.meta_array[type] = content
+ pos += size
+ except:
+ self.meta_array = {}
+ pass
+
def getBookTitle(self):
title = ''
if 503 in self.meta_array:
@@ -269,12 +287,12 @@ class MobiBook:
def processBook(self, pidlist):
crypto_type, = struct.unpack('>H', self.sect[0xC:0xC+2])
+ print 'Crypto Type is: ', crypto_type
+ self.crypto_type = crypto_type
if crypto_type == 0:
print "This book is not encrypted."
return self.data_file
- if crypto_type == 1:
- raise DrmException("Cannot decode Mobipocket encryption type 1")
- if crypto_type != 2:
+ if crypto_type != 2 and crypto_type != 1:
raise DrmException("Cannot decode unknown Mobipocket encryption type %d" % crypto_type)
goodpids = []
@@ -286,23 +304,32 @@ class MobiBook:
elif len(pid)==8:
goodpids.append(pid)
- # calculate the keys
- drm_ptr, drm_count, drm_size, drm_flags = struct.unpack('>LLLL', self.sect[0xA8:0xA8+16])
- if drm_count == 0:
- raise DrmException("Not yet initialised with PID. Must be opened with Mobipocket Reader first.")
- found_key, pid = self.parseDRM(self.sect[drm_ptr:drm_ptr+drm_size], drm_count, goodpids)
- if not found_key:
- raise DrmException("No key found. Most likely the correct PID has not been given.")
+ if self.crypto_type == 1:
+ t1_keyvec = "QDCVEPMU675RUBSZ"
+ if self.magic == 'TEXtREAd':
+ bookkey_data = self.sect[0x0E:0x0E+16]
+ else:
+ bookkey_data = self.sect[0x90:0x90+16]
+ pid = "00000000"
+ found_key = PC1(t1_keyvec, bookkey_data)
+ else :
+ # calculate the keys
+ drm_ptr, drm_count, drm_size, drm_flags = struct.unpack('>LLLL', self.sect[0xA8:0xA8+16])
+ if drm_count == 0:
+ raise DrmException("Not yet initialised with PID. Must be opened with Mobipocket Reader first.")
+ found_key, pid = self.parseDRM(self.sect[drm_ptr:drm_ptr+drm_size], drm_count, goodpids)
+ if not found_key:
+ raise DrmException("No key found. Most likely the correct PID has not been given.")
+ # kill the drm keys
+ self.patchSection(0, "\0" * drm_size, drm_ptr)
+ # kill the drm pointers
+ self.patchSection(0, "\xff" * 4 + "\0" * 12, 0xA8)
if pid=="00000000":
print "File has default encryption, no specific PID."
else:
print "File is encoded with PID "+checksumPid(pid)+"."
- # kill the drm keys
- self.patchSection(0, "\0" * drm_size, drm_ptr)
- # kill the drm pointers
- self.patchSection(0, "\xff" * 4 + "\0" * 12, 0xA8)
# clear the crypto type
self.patchSection(0, "\0" * 2, 0xC)
diff --git a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/topazextract.py b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/topazextract.py
index e371d76..732bbae 100644
--- a/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/topazextract.py
+++ b/DeDRM_Macintosh_Application/DeDRM.app/Contents/Resources/topazextract.py
@@ -164,9 +164,10 @@ class TopazBook:
def getPIDMetaInfo(self):
keysRecord = None
- KeysRecordRecord = None
+ keysRecordRecord = None
if 'keys' in self.bookMetadata:
keysRecord = self.bookMetadata['keys']
+ if keysRecord in self.bookMetadata:
keysRecordRecord = self.bookMetadata[keysRecord]
return keysRecord, keysRecordRecord
@@ -395,6 +396,7 @@ def main(argv=sys.argv):
myzip = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
zipUpDir(myzip, tempdir, '')
myzip.close()
+ shutil.rmtree(tempdir, True)
return 1
print " Creating HTML ZIP Archive"
@@ -424,7 +426,7 @@ def main(argv=sys.argv):
zipUpDir(myzip3, tempdir, 'img')
myzip3.close()
- shutil.rmtree(tempdir)
+ shutil.rmtree(tempdir, True)
return 0
diff --git a/KindleBooks_Tools/KindleBooks/KindleBooks.pyw b/KindleBooks_Tools/KindleBooks/KindleBooks.pyw
index 222bb27..d4f37fb 100644
--- a/KindleBooks_Tools/KindleBooks/KindleBooks.pyw
+++ b/KindleBooks_Tools/KindleBooks/KindleBooks.pyw
@@ -110,6 +110,8 @@ class MainDialog(Tkinter.Frame):
def showCmdOutput(self, msg):
if msg and msg !='':
msg = msg.encode('utf-8')
+ if sys.platform.startswith('win'):
+ msg = msg.replace('\r\n','\n')
self.stext.insert(Tkconstants.END,msg)
self.stext.yview_pickplace(Tkconstants.END)
return
diff --git a/KindleBooks_Tools/KindleBooks/lib/k4mobidedrm.py b/KindleBooks_Tools/KindleBooks/lib/k4mobidedrm.py
index 5059fc4..b7fa317 100644
--- a/KindleBooks_Tools/KindleBooks/lib/k4mobidedrm.py
+++ b/KindleBooks_Tools/KindleBooks/lib/k4mobidedrm.py
@@ -28,7 +28,7 @@
from __future__ import with_statement
-__version__ = '1.4'
+__version__ = '1.9'
class Unbuffered:
def __init__(self, stream):
@@ -163,6 +163,7 @@ def main(argv=sys.argv):
myzip = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
zipUpDir(myzip, tempdir, '')
myzip.close()
+ shutil.rmtree(tempdir, True)
return 1
if mobi:
@@ -198,7 +199,7 @@ def main(argv=sys.argv):
zipUpDir(myzip3, tempdir, 'img')
myzip3.close()
- shutil.rmtree(tempdir)
+ shutil.rmtree(tempdir, True)
return 0
if __name__ == '__main__':
@@ -214,7 +215,7 @@ if not __name__ == "__main__" and inCalibre:
Provided by the work of many including DiapDealer, SomeUpdates, IHeartCabbages, CMBDTC, Skindle, DarkReverser, ApprenticeAlf, etc.'
supported_platforms = ['osx', 'windows', 'linux'] # Platforms this plugin will run on
author = 'DiapDealer, SomeUpdates' # The author of this plugin
- version = (0, 1, 7) # The version number of this plugin
+ version = (0, 1, 9) # The version number of this plugin
file_types = set(['prc','mobi','azw','azw1','tpz']) # The file types that this plugin will be applied to
on_import = True # Run this plugin during the import
priority = 210 # run this plugin before mobidedrm, k4pcdedrm, k4dedrm
diff --git a/KindleBooks_Tools/KindleBooks/lib/k4mutils.py b/KindleBooks_Tools/KindleBooks/lib/k4mutils.py
index 4aa14dd..1b501ba 100644
--- a/KindleBooks_Tools/KindleBooks/lib/k4mutils.py
+++ b/KindleBooks_Tools/KindleBooks/lib/k4mutils.py
@@ -6,7 +6,7 @@ import os
import subprocess
-class K4MDrmException(Exception):
+class DrmException(Exception):
pass
@@ -18,7 +18,7 @@ def _load_crypto_libcrypto():
libcrypto = find_library('crypto')
if libcrypto is None:
- raise K4MDrmException('libcrypto not found')
+ raise DrmException('libcrypto not found')
libcrypto = CDLL(libcrypto)
AES_MAXNR = 14
@@ -51,19 +51,19 @@ def _load_crypto_libcrypto():
def set_decrypt_key(self, userkey, iv):
self._blocksize = len(userkey)
if (self._blocksize != 16) and (self._blocksize != 24) and (self._blocksize != 32) :
- raise K4MDrmException('AES improper key used')
+ raise DrmException('AES improper key used')
return
keyctx = self._keyctx = AES_KEY()
self.iv = iv
rv = AES_set_decrypt_key(userkey, len(userkey) * 8, keyctx)
if rv < 0:
- raise K4MDrmException('Failed to initialize AES key')
+ raise DrmException('Failed to initialize AES key')
def decrypt(self, data):
out = create_string_buffer(len(data))
rv = AES_cbc_encrypt(data, out, len(data), self._keyctx, self.iv, 0)
if rv == 0:
- raise K4MDrmException('AES decryption failed')
+ raise DrmException('AES decryption failed')
return out.raw
def keyivgen(self, passwd):
@@ -81,7 +81,7 @@ def _load_crypto():
LibCrypto = None
try:
LibCrypto = _load_crypto_libcrypto()
- except (ImportError, K4MDrmException):
+ except (ImportError, DrmException):
pass
return LibCrypto
@@ -185,8 +185,10 @@ def openKindleInfo(kInfoFile=None):
if pp >= 0:
kinfopath = resline
break
- if not os.path.exists(kinfopath):
- raise K4MDrmException('Error: .kindle-info file can not be found')
+ if not os.path.isfile(kinfopath):
+ raise DrmException('Error: .kindle-info file can not be found')
return open(kinfopath,'r')
else:
+ if not os.path.isfile(kinfoFile):
+ raise DrmException('Error: kindle-info file can not be found')
return open(kInfoFile, 'r')
diff --git a/KindleBooks_Tools/KindleBooks/lib/k4pcutils.py b/KindleBooks_Tools/KindleBooks/lib/k4pcutils.py
index 3f95660..efc310d 100644
--- a/KindleBooks_Tools/KindleBooks/lib/k4pcutils.py
+++ b/KindleBooks_Tools/KindleBooks/lib/k4pcutils.py
@@ -99,7 +99,12 @@ CryptUnprotectData = CryptUnprotectData()
def openKindleInfo(kInfoFile=None):
if kInfoFile == None:
regkey = winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\\")
- path = winreg.QueryValueEx(regkey, 'Local AppData')[0]
- return open(path+'\\Amazon\\Kindle For PC\\{AMAwzsaPaaZAzmZzZQzgZCAkZ3AjA_AY}\\kindle.info','r')
+ path = winreg.QueryValueEx(regkey, 'Local AppData')[0]
+ kinfopath = path +'\\Amazon\\Kindle For PC\\{AMAwzsaPaaZAzmZzZQzgZCAkZ3AjA_AY}\\kindle.info'
+ if not os.path.isfile(kinfopath):
+ raise DrmException('Error: kindle.info file can not be found')
+ return open(kinfopath,'r')
else:
+ if not os.path.isfile(kInfoFile):
+ raise DrmException('Error: kindle.info file can not be found')
return open(kInfoFile, 'r')
diff --git a/KindleBooks_Tools/KindleBooks/lib/kgenpids.py b/KindleBooks_Tools/KindleBooks/lib/kgenpids.py
index 5c44bfa..6dcbf73 100644
--- a/KindleBooks_Tools/KindleBooks/lib/kgenpids.py
+++ b/KindleBooks_Tools/KindleBooks/lib/kgenpids.py
@@ -83,7 +83,8 @@ def parseKindleInfo(kInfoFile):
DB[splito[0]] =splito[1]
return DB
-# Get a record from the Kindle.info file for the key "hashedKey" (already hashed and encoded). Return the decoded and decrypted record
+# Get a record from the Kindle.info file for the key "hashedKey" (already hashed and encoded).
+# Return the decoded and decrypted record
def getKindleInfoValueForHash(hashedKey):
global kindleDatabase
global charMap1
@@ -95,12 +96,14 @@ def getKindleInfoValueForHash(hashedKey):
cleartext = CryptUnprotectData(encryptedValue)
return decode(cleartext, charMap1)
-# Get a record from the Kindle.info file for the string in "key" (plaintext). Return the decoded and decrypted record
+# Get a record from the Kindle.info file for the string in "key" (plaintext).
+# Return the decoded and decrypted record
def getKindleInfoValueForKey(key):
global charMap2
return getKindleInfoValueForHash(encodeHash(key,charMap2))
-# Find if the original string for a hashed/encoded string is known. If so return the original string othwise return an empty string.
+# Find if the original string for a hashed/encoded string is known.
+# If so return the original string othwise return an empty string.
def findNameForHash(hash):
global charMap2
names = ["kindle.account.tokens","kindle.cookie.item","eulaVersionAccepted","login_date","kindle.token.item","login","kindle.key.item","kindle.name.info","kindle.device.info", "MazamaRandomNumber"]
@@ -222,7 +225,7 @@ def pidFromSerial(s, l):
# Parse the EXTH header records and use the Kindle serial number to calculate the book pid.
def getKindlePid(pidlst, rec209, token, serialnum):
- if rec209 != None:
+ if rec209 != None and token != None:
# Compute book PID
pidHash = SHA1(serialnum+rec209+token)
bookPID = encodePID(pidHash)
@@ -248,6 +251,7 @@ def getK4Pids(pidlst, rec209, token, kInfoFile=None):
kindleDatabase = parseKindleInfo(kInfoFile)
except Exception, message:
print(message)
+ kindleDatabase = None
pass
if kindleDatabase == None :
@@ -272,8 +276,8 @@ def getK4Pids(pidlst, rec209, token, kInfoFile=None):
pidlst.append(devicePID)
# Compute book PID
- if rec209 == None:
- print "\nNo EXTH record type 209 - Perhaps not a K4 file?"
+ if rec209 == None or token == None:
+ print "\nNo EXTH record type 209 or token - Perhaps not a K4 file?"
return pidlst
# Get the kindle account token
diff --git a/KindleBooks_Tools/KindleBooks/lib/mobidedrm.py b/KindleBooks_Tools/KindleBooks/lib/mobidedrm.py
index cc83224..864b545 100644
--- a/KindleBooks_Tools/KindleBooks/lib/mobidedrm.py
+++ b/KindleBooks_Tools/KindleBooks/lib/mobidedrm.py
@@ -42,8 +42,10 @@
# 0.20 - Correction: It seems that multibyte entries are encrypted in a v6 file.
# 0.21 - Added support for multiple pids
# 0.22 - revised structure to hold MobiBook as a class to allow an extended interface
+# 0.23 - fixed problem with older files with no EXTH section
+# 0.24 - add support for type 1 encryption and 'TEXtREAd' books as well
-__version__ = '0.22'
+__version__ = '0.24'
import sys
@@ -57,6 +59,7 @@ class Unbuffered:
return getattr(self.stream, attr)
sys.stdout=Unbuffered(sys.stdout)
+import os
import struct
import binascii
@@ -154,8 +157,10 @@ class MobiBook:
# initial sanity check on file
self.data_file = file(infile, 'rb').read()
self.header = self.data_file[0:78]
- if self.header[0x3C:0x3C+8] != 'BOOKMOBI':
+ if self.header[0x3C:0x3C+8] != 'BOOKMOBI' and self.header[0x3C:0x3C+8] != 'TEXtREAd':
raise DrmException("invalid file format")
+ self.magic = self.header[0x3C:0x3C+8]
+ self.crypto_type = -1
# build up section offset and flag info
self.num_sections, = struct.unpack('>H', self.header[76:78])
@@ -168,6 +173,14 @@ class MobiBook:
# parse information from section 0
self.sect = self.loadSection(0)
self.records, = struct.unpack('>H', self.sect[0x8:0x8+2])
+
+ if self.magic == 'TEXtREAd':
+ print "Book has format: ", self.magic
+ self.extra_data_flags = 0
+ self.mobi_length = 0
+ self.mobi_version = -1
+ self.meta_array = {}
+ return
self.mobi_length, = struct.unpack('>L',self.sect[0x14:0x18])
self.mobi_version, = struct.unpack('>L',self.sect[0x68:0x6C])
print "MOBI header version = %d, length = %d" %(self.mobi_version, self.mobi_length)
@@ -182,18 +195,23 @@ class MobiBook:
# if exth region exists parse it for metadata array
self.meta_array = {}
- exth_flag, = struct.unpack('>L', self.sect[0x80:0x84])
- exth = ''
- if exth_flag & 0x40:
- exth = self.sect[16 + self.mobi_length:]
- nitems, = struct.unpack('>I', exth[8:12])
- pos = 12
- for i in xrange(nitems):
- type, size = struct.unpack('>II', exth[pos: pos + 8])
- content = exth[pos + 8: pos + size]
- self.meta_array[type] = content
- pos += size
-
+ try:
+ exth_flag, = struct.unpack('>L', self.sect[0x80:0x84])
+ exth = 'NONE'
+ if exth_flag & 0x40:
+ exth = self.sect[16 + self.mobi_length:]
+ if (len(exth) >= 4) and (exth[:4] == 'EXTH'):
+ nitems, = struct.unpack('>I', exth[8:12])
+ pos = 12
+ for i in xrange(nitems):
+ type, size = struct.unpack('>II', exth[pos: pos + 8])
+ content = exth[pos + 8: pos + size]
+ self.meta_array[type] = content
+ pos += size
+ except:
+ self.meta_array = {}
+ pass
+
def getBookTitle(self):
title = ''
if 503 in self.meta_array:
@@ -269,12 +287,12 @@ class MobiBook:
def processBook(self, pidlist):
crypto_type, = struct.unpack('>H', self.sect[0xC:0xC+2])
+ print 'Crypto Type is: ', crypto_type
+ self.crypto_type = crypto_type
if crypto_type == 0:
print "This book is not encrypted."
return self.data_file
- if crypto_type == 1:
- raise DrmException("Cannot decode Mobipocket encryption type 1")
- if crypto_type != 2:
+ if crypto_type != 2 and crypto_type != 1:
raise DrmException("Cannot decode unknown Mobipocket encryption type %d" % crypto_type)
goodpids = []
@@ -286,23 +304,32 @@ class MobiBook:
elif len(pid)==8:
goodpids.append(pid)
- # calculate the keys
- drm_ptr, drm_count, drm_size, drm_flags = struct.unpack('>LLLL', self.sect[0xA8:0xA8+16])
- if drm_count == 0:
- raise DrmException("Not yet initialised with PID. Must be opened with Mobipocket Reader first.")
- found_key, pid = self.parseDRM(self.sect[drm_ptr:drm_ptr+drm_size], drm_count, goodpids)
- if not found_key:
- raise DrmException("No key found. Most likely the correct PID has not been given.")
+ if self.crypto_type == 1:
+ t1_keyvec = "QDCVEPMU675RUBSZ"
+ if self.magic == 'TEXtREAd':
+ bookkey_data = self.sect[0x0E:0x0E+16]
+ else:
+ bookkey_data = self.sect[0x90:0x90+16]
+ pid = "00000000"
+ found_key = PC1(t1_keyvec, bookkey_data)
+ else :
+ # calculate the keys
+ drm_ptr, drm_count, drm_size, drm_flags = struct.unpack('>LLLL', self.sect[0xA8:0xA8+16])
+ if drm_count == 0:
+ raise DrmException("Not yet initialised with PID. Must be opened with Mobipocket Reader first.")
+ found_key, pid = self.parseDRM(self.sect[drm_ptr:drm_ptr+drm_size], drm_count, goodpids)
+ if not found_key:
+ raise DrmException("No key found. Most likely the correct PID has not been given.")
+ # kill the drm keys
+ self.patchSection(0, "\0" * drm_size, drm_ptr)
+ # kill the drm pointers
+ self.patchSection(0, "\xff" * 4 + "\0" * 12, 0xA8)
if pid=="00000000":
print "File has default encryption, no specific PID."
else:
print "File is encoded with PID "+checksumPid(pid)+"."
- # kill the drm keys
- self.patchSection(0, "\0" * drm_size, drm_ptr)
- # kill the drm pointers
- self.patchSection(0, "\xff" * 4 + "\0" * 12, 0xA8)
# clear the crypto type
self.patchSection(0, "\0" * 2, 0xC)
diff --git a/KindleBooks_Tools/KindleBooks/lib/topazextract.py b/KindleBooks_Tools/KindleBooks/lib/topazextract.py
index e371d76..732bbae 100644
--- a/KindleBooks_Tools/KindleBooks/lib/topazextract.py
+++ b/KindleBooks_Tools/KindleBooks/lib/topazextract.py
@@ -164,9 +164,10 @@ class TopazBook:
def getPIDMetaInfo(self):
keysRecord = None
- KeysRecordRecord = None
+ keysRecordRecord = None
if 'keys' in self.bookMetadata:
keysRecord = self.bookMetadata['keys']
+ if keysRecord in self.bookMetadata:
keysRecordRecord = self.bookMetadata[keysRecord]
return keysRecord, keysRecordRecord
@@ -395,6 +396,7 @@ def main(argv=sys.argv):
myzip = zipfile.ZipFile(zipname,'w',zipfile.ZIP_DEFLATED, False)
zipUpDir(myzip, tempdir, '')
myzip.close()
+ shutil.rmtree(tempdir, True)
return 1
print " Creating HTML ZIP Archive"
@@ -424,7 +426,7 @@ def main(argv=sys.argv):
zipUpDir(myzip3, tempdir, 'img')
myzip3.close()
- shutil.rmtree(tempdir)
+ shutil.rmtree(tempdir, True)
return 0
diff --git a/KindleBooks_Tools/Kindle_4_Mac_Unswindle/lib/mobidedrm.py b/KindleBooks_Tools/Kindle_4_Mac_Unswindle/lib/mobidedrm.py
index 183432c..864b545 100644
--- a/KindleBooks_Tools/Kindle_4_Mac_Unswindle/lib/mobidedrm.py
+++ b/KindleBooks_Tools/Kindle_4_Mac_Unswindle/lib/mobidedrm.py
@@ -24,7 +24,7 @@
# 0.14 - Working out when the extra data flags are present has been problematic
# Versions 7 through 9 have tried to tweak the conditions, but have been
# only partially successful. Closer examination of lots of sample
-# files reveals that a confusin has arisen because trailing data entries
+# files reveals that a confusion has arisen because trailing data entries
# are not encrypted, but it turns out that the multibyte entries
# in utf8 file are encrypted. (Although neither kind gets compressed.)
# This knowledge leads to a simplification of the test for the
@@ -39,13 +39,15 @@
# Removed the disabled Calibre plug-in code
# Permit use of 8-digit PIDs
# 0.19 - It seems that multibyte entries aren't encrypted in a v6 file either.
-# 0.20 - Corretion: It seems that multibyte entries are encrypted in a v6 file.
+# 0.20 - Correction: It seems that multibyte entries are encrypted in a v6 file.
+# 0.21 - Added support for multiple pids
+# 0.22 - revised structure to hold MobiBook as a class to allow an extended interface
+# 0.23 - fixed problem with older files with no EXTH section
+# 0.24 - add support for type 1 encryption and 'TEXtREAd' books as well
-__version__ = '0.20'
+__version__ = '0.24'
import sys
-import struct
-import binascii
class Unbuffered:
def __init__(self, stream):
@@ -55,10 +57,20 @@ class Unbuffered:
self.stream.flush()
def __getattr__(self, attr):
return getattr(self.stream, attr)
+sys.stdout=Unbuffered(sys.stdout)
+
+import os
+import struct
+import binascii
class DrmException(Exception):
pass
+
+#
+# MobiBook Utility Routines
+#
+
# Implementation of Pukall Cipher 1
def PC1(key, src, decryption=True):
sum1 = 0;
@@ -70,7 +82,6 @@ def PC1(key, src, decryption=True):
wkey = []
for i in xrange(8):
wkey.append(ord(key[i*2])<<8 | ord(key[i*2+1]))
-
dst = ""
for i in xrange(len(src)):
temp1 = 0;
@@ -131,7 +142,9 @@ def getSizeOfTrailingDataEntries(ptr, size, flags):
num += (ord(ptr[size - num - 1]) & 0x3) + 1
return num
-class DrmStripper:
+
+
+class MobiBook:
def loadSection(self, section):
if (section + 1 == self.num_sections):
endoff = len(self.data_file)
@@ -140,6 +153,93 @@ class DrmStripper:
off = self.sections[section][0]
return self.data_file[off:endoff]
+ def __init__(self, infile):
+ # initial sanity check on file
+ self.data_file = file(infile, 'rb').read()
+ self.header = self.data_file[0:78]
+ if self.header[0x3C:0x3C+8] != 'BOOKMOBI' and self.header[0x3C:0x3C+8] != 'TEXtREAd':
+ raise DrmException("invalid file format")
+ self.magic = self.header[0x3C:0x3C+8]
+ self.crypto_type = -1
+
+ # build up section offset and flag info
+ self.num_sections, = struct.unpack('>H', self.header[76:78])
+ self.sections = []
+ for i in xrange(self.num_sections):
+ offset, a1,a2,a3,a4 = struct.unpack('>LBBBB', self.data_file[78+i*8:78+i*8+8])
+ flags, val = a1, a2<<16|a3<<8|a4
+ self.sections.append( (offset, flags, val) )
+
+ # parse information from section 0
+ self.sect = self.loadSection(0)
+ self.records, = struct.unpack('>H', self.sect[0x8:0x8+2])
+
+ if self.magic == 'TEXtREAd':
+ print "Book has format: ", self.magic
+ self.extra_data_flags = 0
+ self.mobi_length = 0
+ self.mobi_version = -1
+ self.meta_array = {}
+ return
+ self.mobi_length, = struct.unpack('>L',self.sect[0x14:0x18])
+ self.mobi_version, = struct.unpack('>L',self.sect[0x68:0x6C])
+ print "MOBI header version = %d, length = %d" %(self.mobi_version, self.mobi_length)
+ self.extra_data_flags = 0
+ if (self.mobi_length >= 0xE4) and (self.mobi_version >= 5):
+ self.extra_data_flags, = struct.unpack('>H', self.sect[0xF2:0xF4])
+ print "Extra Data Flags = %d" % self.extra_data_flags
+ if self.mobi_version < 7:
+ # multibyte utf8 data is included in the encryption for mobi_version 6 and below
+ # so clear that byte so that we leave it to be decrypted.
+ self.extra_data_flags &= 0xFFFE
+
+ # if exth region exists parse it for metadata array
+ self.meta_array = {}
+ try:
+ exth_flag, = struct.unpack('>L', self.sect[0x80:0x84])
+ exth = 'NONE'
+ if exth_flag & 0x40:
+ exth = self.sect[16 + self.mobi_length:]
+ if (len(exth) >= 4) and (exth[:4] == 'EXTH'):
+ nitems, = struct.unpack('>I', exth[8:12])
+ pos = 12
+ for i in xrange(nitems):
+ type, size = struct.unpack('>II', exth[pos: pos + 8])
+ content = exth[pos + 8: pos + size]
+ self.meta_array[type] = content
+ pos += size
+ except:
+ self.meta_array = {}
+ pass
+
+ def getBookTitle(self):
+ title = ''
+ if 503 in self.meta_array:
+ title = self.meta_array[503]
+ else :
+ toff, tlen = struct.unpack('>II', self.sect[0x54:0x5c])
+ tend = toff + tlen
+ title = self.sect[toff:tend]
+ if title == '':
+ title = self.header[:32]
+ title = title.split("\0")[0]
+ return title
+
+ def getPIDMetaInfo(self):
+ rec209 = None
+ token = None
+ if 209 in self.meta_array:
+ rec209 = self.meta_array[209]
+ data = rec209
+ # Parse the 209 data to find the the exth record with the token data.
+ # The last character of the 209 data points to the record with the token.
+ # Always 208 from my experience, but I'll leave the logic in case that changes.
+ for i in xrange(len(data)):
+ if ord(data[i]) != 0:
+ if self.meta_array[ord(data[i])] != None:
+ token = self.meta_array[ord(data[i])]
+ return rec209, token
+
def patch(self, off, new):
self.data_file = self.data_file[:off] + new + self.data_file[off+len(new):]
@@ -152,134 +252,131 @@ class DrmStripper:
assert off + in_off + len(new) <= endoff
self.patch(off + in_off, new)
- def parseDRM(self, data, count, pid):
- pid = pid.ljust(16,'\0')
- keyvec1 = "\x72\x38\x33\xB0\xB4\xF2\xE3\xCA\xDF\x09\x01\xD6\xE2\xE0\x3F\x96"
- temp_key = PC1(keyvec1, pid, False)
- temp_key_sum = sum(map(ord,temp_key)) & 0xff
+ def parseDRM(self, data, count, pidlist):
found_key = None
- for i in xrange(count):
- verification, size, type, cksum, cookie = struct.unpack('>LLLBxxx32s', data[i*0x30:i*0x30+0x30])
- cookie = PC1(temp_key, cookie)
- ver,flags,finalkey,expiry,expiry2 = struct.unpack('>LL16sLL', cookie)
- if verification == ver and cksum == temp_key_sum and (flags & 0x1F) == 1:
- found_key = finalkey
+ keyvec1 = "\x72\x38\x33\xB0\xB4\xF2\xE3\xCA\xDF\x09\x01\xD6\xE2\xE0\x3F\x96"
+ for pid in pidlist:
+ bigpid = pid.ljust(16,'\0')
+ temp_key = PC1(keyvec1, bigpid, False)
+ temp_key_sum = sum(map(ord,temp_key)) & 0xff
+ found_key = None
+ for i in xrange(count):
+ verification, size, type, cksum, cookie = struct.unpack('>LLLBxxx32s', data[i*0x30:i*0x30+0x30])
+ if cksum == temp_key_sum:
+ cookie = PC1(temp_key, cookie)
+ ver,flags,finalkey,expiry,expiry2 = struct.unpack('>LL16sLL', cookie)
+ if verification == ver and (flags & 0x1F) == 1:
+ found_key = finalkey
+ break
+ if found_key != None:
break
if not found_key:
# Then try the default encoding that doesn't require a PID
+ pid = "00000000"
temp_key = keyvec1
temp_key_sum = sum(map(ord,temp_key)) & 0xff
for i in xrange(count):
verification, size, type, cksum, cookie = struct.unpack('>LLLBxxx32s', data[i*0x30:i*0x30+0x30])
- cookie = PC1(temp_key, cookie)
- ver,flags,finalkey,expiry,expiry2 = struct.unpack('>LL16sLL', cookie)
- if verification == ver and cksum == temp_key_sum:
- found_key = finalkey
- break
- return found_key
+ if cksum == temp_key_sum:
+ cookie = PC1(temp_key, cookie)
+ ver,flags,finalkey,expiry,expiry2 = struct.unpack('>LL16sLL', cookie)
+ if verification == ver:
+ found_key = finalkey
+ break
+ return [found_key,pid]
- def __init__(self, data_file, pid):
- if len(pid)==10:
- if checksumPid(pid[0:-2]) != pid:
- raise DrmException("invalid PID checksum")
- pid = pid[0:-2]
- elif len(pid)==8:
- print "PID without checksum given. With checksum PID is "+checksumPid(pid)
- else:
- raise DrmException("Invalid PID length")
-
- self.data_file = data_file
- header = data_file[0:72]
- if header[0x3C:0x3C+8] != 'BOOKMOBI':
- raise DrmException("invalid file format")
- self.num_sections, = struct.unpack('>H', data_file[76:78])
-
- self.sections = []
- for i in xrange(self.num_sections):
- offset, a1,a2,a3,a4 = struct.unpack('>LBBBB', data_file[78+i*8:78+i*8+8])
- flags, val = a1, a2<<16|a3<<8|a4
- self.sections.append( (offset, flags, val) )
-
- sect = self.loadSection(0)
- records, = struct.unpack('>H', sect[0x8:0x8+2])
- mobi_length, = struct.unpack('>L',sect[0x14:0x18])
- mobi_version, = struct.unpack('>L',sect[0x68:0x6C])
- extra_data_flags = 0
- print "MOBI header version = %d, length = %d" %(mobi_version, mobi_length)
- if (mobi_length >= 0xE4) and (mobi_version >= 5):
- extra_data_flags, = struct.unpack('>H', sect[0xF2:0xF4])
- print "Extra Data Flags = %d" %extra_data_flags
- if mobi_version < 7:
- # multibyte utf8 data is included in the encryption for mobi_version 6 and below
- # so clear that byte so that we leave it to be decrypted.
- extra_data_flags &= 0xFFFE
-
- crypto_type, = struct.unpack('>H', sect[0xC:0xC+2])
+ def processBook(self, pidlist):
+ crypto_type, = struct.unpack('>H', self.sect[0xC:0xC+2])
+ print 'Crypto Type is: ', crypto_type
+ self.crypto_type = crypto_type
if crypto_type == 0:
print "This book is not encrypted."
- else:
- if crypto_type == 1:
- raise DrmException("cannot decode Mobipocket encryption type 1")
- if crypto_type != 2:
- raise DrmException("unknown encryption type: %d" % crypto_type)
+ return self.data_file
+ if crypto_type != 2 and crypto_type != 1:
+ raise DrmException("Cannot decode unknown Mobipocket encryption type %d" % crypto_type)
+ goodpids = []
+ for pid in pidlist:
+ if len(pid)==10:
+ if checksumPid(pid[0:-2]) != pid:
+ print "Warning: PID " + pid + " has incorrect checksum, should have been "+checksumPid(pid[0:-2])
+ goodpids.append(pid[0:-2])
+ elif len(pid)==8:
+ goodpids.append(pid)
+
+ if self.crypto_type == 1:
+ t1_keyvec = "QDCVEPMU675RUBSZ"
+ if self.magic == 'TEXtREAd':
+ bookkey_data = self.sect[0x0E:0x0E+16]
+ else:
+ bookkey_data = self.sect[0x90:0x90+16]
+ pid = "00000000"
+ found_key = PC1(t1_keyvec, bookkey_data)
+ else :
# calculate the keys
- drm_ptr, drm_count, drm_size, drm_flags = struct.unpack('>LLLL', sect[0xA8:0xA8+16])
+ drm_ptr, drm_count, drm_size, drm_flags = struct.unpack('>LLLL', self.sect[0xA8:0xA8+16])
if drm_count == 0:
- raise DrmException("no PIDs found in this file")
- found_key = self.parseDRM(sect[drm_ptr:drm_ptr+drm_size], drm_count, pid)
+ raise DrmException("Not yet initialised with PID. Must be opened with Mobipocket Reader first.")
+ found_key, pid = self.parseDRM(self.sect[drm_ptr:drm_ptr+drm_size], drm_count, goodpids)
if not found_key:
- raise DrmException("no key found. maybe the PID is incorrect")
-
+ raise DrmException("No key found. Most likely the correct PID has not been given.")
# kill the drm keys
self.patchSection(0, "\0" * drm_size, drm_ptr)
# kill the drm pointers
self.patchSection(0, "\xff" * 4 + "\0" * 12, 0xA8)
- # clear the crypto type
- self.patchSection(0, "\0" * 2, 0xC)
+
+ if pid=="00000000":
+ print "File has default encryption, no specific PID."
+ else:
+ print "File is encoded with PID "+checksumPid(pid)+"."
- # decrypt sections
- print "Decrypting. Please wait . . .",
- new_data = self.data_file[:self.sections[1][0]]
- for i in xrange(1, records+1):
- data = self.loadSection(i)
- extra_size = getSizeOfTrailingDataEntries(data, len(data), extra_data_flags)
- if i%100 == 0:
- print ".",
- # print "record %d, extra_size %d" %(i,extra_size)
- new_data += PC1(found_key, data[0:len(data) - extra_size])
- if extra_size > 0:
- new_data += data[-extra_size:]
- #self.patchSection(i, PC1(found_key, data[0:len(data) - extra_size]))
- if self.num_sections > records+1:
- new_data += self.data_file[self.sections[records+1][0]:]
- self.data_file = new_data
- print "done"
+ # clear the crypto type
+ self.patchSection(0, "\0" * 2, 0xC)
- def getResult(self):
+ # decrypt sections
+ print "Decrypting. Please wait . . .",
+ new_data = self.data_file[:self.sections[1][0]]
+ for i in xrange(1, self.records+1):
+ data = self.loadSection(i)
+ extra_size = getSizeOfTrailingDataEntries(data, len(data), self.extra_data_flags)
+ if i%100 == 0:
+ print ".",
+ # print "record %d, extra_size %d" %(i,extra_size)
+ new_data += PC1(found_key, data[0:len(data) - extra_size])
+ if extra_size > 0:
+ new_data += data[-extra_size:]
+ if self.num_sections > self.records+1:
+ new_data += self.data_file[self.sections[self.records+1][0]:]
+ self.data_file = new_data
+ print "done"
return self.data_file
def getUnencryptedBook(infile,pid):
- sys.stdout=Unbuffered(sys.stdout)
- data_file = file(infile, 'rb').read()
- strippedFile = DrmStripper(data_file, pid)
- return strippedFile.getResult()
+ if not os.path.isfile(infile):
+ raise DrmException('Input File Not Found')
+ book = MobiBook(infile)
+ return book.processBook([pid])
+
+def getUnencryptedBookWithList(infile,pidlist):
+ if not os.path.isfile(infile):
+ raise DrmException('Input File Not Found')
+ book = MobiBook(infile)
+ return book.processBook(pidlist)
def main(argv=sys.argv):
- sys.stdout=Unbuffered(sys.stdout)
print ('MobiDeDrm v%(__version__)s. '
'Copyright 2008-2010 The Dark Reverser.' % globals())
if len(argv)<4:
print "Removes protection from Mobipocket books"
print "Usage:"
- print " %s " % sys.argv[0]
+ print " %s " % sys.argv[0]
return 1
else:
infile = argv[1]
outfile = argv[2]
- pid = argv[3]
+ pidlist = argv[3].split(',')
try:
- stripped_file = getUnencryptedBook(infile, pid)
+ stripped_file = getUnencryptedBookWithList(infile, pidlist)
file(outfile, 'wb').write(stripped_file)
except DrmException, e:
print "Error: %s" % e
diff --git a/KindleBooks_Tools/Kindle_4_PC_Unswindle/mobidedrm.py b/KindleBooks_Tools/Kindle_4_PC_Unswindle/mobidedrm.py
index 183432c..864b545 100644
--- a/KindleBooks_Tools/Kindle_4_PC_Unswindle/mobidedrm.py
+++ b/KindleBooks_Tools/Kindle_4_PC_Unswindle/mobidedrm.py
@@ -24,7 +24,7 @@
# 0.14 - Working out when the extra data flags are present has been problematic
# Versions 7 through 9 have tried to tweak the conditions, but have been
# only partially successful. Closer examination of lots of sample
-# files reveals that a confusin has arisen because trailing data entries
+# files reveals that a confusion has arisen because trailing data entries
# are not encrypted, but it turns out that the multibyte entries
# in utf8 file are encrypted. (Although neither kind gets compressed.)
# This knowledge leads to a simplification of the test for the
@@ -39,13 +39,15 @@
# Removed the disabled Calibre plug-in code
# Permit use of 8-digit PIDs
# 0.19 - It seems that multibyte entries aren't encrypted in a v6 file either.
-# 0.20 - Corretion: It seems that multibyte entries are encrypted in a v6 file.
+# 0.20 - Correction: It seems that multibyte entries are encrypted in a v6 file.
+# 0.21 - Added support for multiple pids
+# 0.22 - revised structure to hold MobiBook as a class to allow an extended interface
+# 0.23 - fixed problem with older files with no EXTH section
+# 0.24 - add support for type 1 encryption and 'TEXtREAd' books as well
-__version__ = '0.20'
+__version__ = '0.24'
import sys
-import struct
-import binascii
class Unbuffered:
def __init__(self, stream):
@@ -55,10 +57,20 @@ class Unbuffered:
self.stream.flush()
def __getattr__(self, attr):
return getattr(self.stream, attr)
+sys.stdout=Unbuffered(sys.stdout)
+
+import os
+import struct
+import binascii
class DrmException(Exception):
pass
+
+#
+# MobiBook Utility Routines
+#
+
# Implementation of Pukall Cipher 1
def PC1(key, src, decryption=True):
sum1 = 0;
@@ -70,7 +82,6 @@ def PC1(key, src, decryption=True):
wkey = []
for i in xrange(8):
wkey.append(ord(key[i*2])<<8 | ord(key[i*2+1]))
-
dst = ""
for i in xrange(len(src)):
temp1 = 0;
@@ -131,7 +142,9 @@ def getSizeOfTrailingDataEntries(ptr, size, flags):
num += (ord(ptr[size - num - 1]) & 0x3) + 1
return num
-class DrmStripper:
+
+
+class MobiBook:
def loadSection(self, section):
if (section + 1 == self.num_sections):
endoff = len(self.data_file)
@@ -140,6 +153,93 @@ class DrmStripper:
off = self.sections[section][0]
return self.data_file[off:endoff]
+ def __init__(self, infile):
+ # initial sanity check on file
+ self.data_file = file(infile, 'rb').read()
+ self.header = self.data_file[0:78]
+ if self.header[0x3C:0x3C+8] != 'BOOKMOBI' and self.header[0x3C:0x3C+8] != 'TEXtREAd':
+ raise DrmException("invalid file format")
+ self.magic = self.header[0x3C:0x3C+8]
+ self.crypto_type = -1
+
+ # build up section offset and flag info
+ self.num_sections, = struct.unpack('>H', self.header[76:78])
+ self.sections = []
+ for i in xrange(self.num_sections):
+ offset, a1,a2,a3,a4 = struct.unpack('>LBBBB', self.data_file[78+i*8:78+i*8+8])
+ flags, val = a1, a2<<16|a3<<8|a4
+ self.sections.append( (offset, flags, val) )
+
+ # parse information from section 0
+ self.sect = self.loadSection(0)
+ self.records, = struct.unpack('>H', self.sect[0x8:0x8+2])
+
+ if self.magic == 'TEXtREAd':
+ print "Book has format: ", self.magic
+ self.extra_data_flags = 0
+ self.mobi_length = 0
+ self.mobi_version = -1
+ self.meta_array = {}
+ return
+ self.mobi_length, = struct.unpack('>L',self.sect[0x14:0x18])
+ self.mobi_version, = struct.unpack('>L',self.sect[0x68:0x6C])
+ print "MOBI header version = %d, length = %d" %(self.mobi_version, self.mobi_length)
+ self.extra_data_flags = 0
+ if (self.mobi_length >= 0xE4) and (self.mobi_version >= 5):
+ self.extra_data_flags, = struct.unpack('>H', self.sect[0xF2:0xF4])
+ print "Extra Data Flags = %d" % self.extra_data_flags
+ if self.mobi_version < 7:
+ # multibyte utf8 data is included in the encryption for mobi_version 6 and below
+ # so clear that byte so that we leave it to be decrypted.
+ self.extra_data_flags &= 0xFFFE
+
+ # if exth region exists parse it for metadata array
+ self.meta_array = {}
+ try:
+ exth_flag, = struct.unpack('>L', self.sect[0x80:0x84])
+ exth = 'NONE'
+ if exth_flag & 0x40:
+ exth = self.sect[16 + self.mobi_length:]
+ if (len(exth) >= 4) and (exth[:4] == 'EXTH'):
+ nitems, = struct.unpack('>I', exth[8:12])
+ pos = 12
+ for i in xrange(nitems):
+ type, size = struct.unpack('>II', exth[pos: pos + 8])
+ content = exth[pos + 8: pos + size]
+ self.meta_array[type] = content
+ pos += size
+ except:
+ self.meta_array = {}
+ pass
+
+ def getBookTitle(self):
+ title = ''
+ if 503 in self.meta_array:
+ title = self.meta_array[503]
+ else :
+ toff, tlen = struct.unpack('>II', self.sect[0x54:0x5c])
+ tend = toff + tlen
+ title = self.sect[toff:tend]
+ if title == '':
+ title = self.header[:32]
+ title = title.split("\0")[0]
+ return title
+
+ def getPIDMetaInfo(self):
+ rec209 = None
+ token = None
+ if 209 in self.meta_array:
+ rec209 = self.meta_array[209]
+ data = rec209
+ # Parse the 209 data to find the the exth record with the token data.
+ # The last character of the 209 data points to the record with the token.
+ # Always 208 from my experience, but I'll leave the logic in case that changes.
+ for i in xrange(len(data)):
+ if ord(data[i]) != 0:
+ if self.meta_array[ord(data[i])] != None:
+ token = self.meta_array[ord(data[i])]
+ return rec209, token
+
def patch(self, off, new):
self.data_file = self.data_file[:off] + new + self.data_file[off+len(new):]
@@ -152,134 +252,131 @@ class DrmStripper:
assert off + in_off + len(new) <= endoff
self.patch(off + in_off, new)
- def parseDRM(self, data, count, pid):
- pid = pid.ljust(16,'\0')
- keyvec1 = "\x72\x38\x33\xB0\xB4\xF2\xE3\xCA\xDF\x09\x01\xD6\xE2\xE0\x3F\x96"
- temp_key = PC1(keyvec1, pid, False)
- temp_key_sum = sum(map(ord,temp_key)) & 0xff
+ def parseDRM(self, data, count, pidlist):
found_key = None
- for i in xrange(count):
- verification, size, type, cksum, cookie = struct.unpack('>LLLBxxx32s', data[i*0x30:i*0x30+0x30])
- cookie = PC1(temp_key, cookie)
- ver,flags,finalkey,expiry,expiry2 = struct.unpack('>LL16sLL', cookie)
- if verification == ver and cksum == temp_key_sum and (flags & 0x1F) == 1:
- found_key = finalkey
+ keyvec1 = "\x72\x38\x33\xB0\xB4\xF2\xE3\xCA\xDF\x09\x01\xD6\xE2\xE0\x3F\x96"
+ for pid in pidlist:
+ bigpid = pid.ljust(16,'\0')
+ temp_key = PC1(keyvec1, bigpid, False)
+ temp_key_sum = sum(map(ord,temp_key)) & 0xff
+ found_key = None
+ for i in xrange(count):
+ verification, size, type, cksum, cookie = struct.unpack('>LLLBxxx32s', data[i*0x30:i*0x30+0x30])
+ if cksum == temp_key_sum:
+ cookie = PC1(temp_key, cookie)
+ ver,flags,finalkey,expiry,expiry2 = struct.unpack('>LL16sLL', cookie)
+ if verification == ver and (flags & 0x1F) == 1:
+ found_key = finalkey
+ break
+ if found_key != None:
break
if not found_key:
# Then try the default encoding that doesn't require a PID
+ pid = "00000000"
temp_key = keyvec1
temp_key_sum = sum(map(ord,temp_key)) & 0xff
for i in xrange(count):
verification, size, type, cksum, cookie = struct.unpack('>LLLBxxx32s', data[i*0x30:i*0x30+0x30])
- cookie = PC1(temp_key, cookie)
- ver,flags,finalkey,expiry,expiry2 = struct.unpack('>LL16sLL', cookie)
- if verification == ver and cksum == temp_key_sum:
- found_key = finalkey
- break
- return found_key
+ if cksum == temp_key_sum:
+ cookie = PC1(temp_key, cookie)
+ ver,flags,finalkey,expiry,expiry2 = struct.unpack('>LL16sLL', cookie)
+ if verification == ver:
+ found_key = finalkey
+ break
+ return [found_key,pid]
- def __init__(self, data_file, pid):
- if len(pid)==10:
- if checksumPid(pid[0:-2]) != pid:
- raise DrmException("invalid PID checksum")
- pid = pid[0:-2]
- elif len(pid)==8:
- print "PID without checksum given. With checksum PID is "+checksumPid(pid)
- else:
- raise DrmException("Invalid PID length")
-
- self.data_file = data_file
- header = data_file[0:72]
- if header[0x3C:0x3C+8] != 'BOOKMOBI':
- raise DrmException("invalid file format")
- self.num_sections, = struct.unpack('>H', data_file[76:78])
-
- self.sections = []
- for i in xrange(self.num_sections):
- offset, a1,a2,a3,a4 = struct.unpack('>LBBBB', data_file[78+i*8:78+i*8+8])
- flags, val = a1, a2<<16|a3<<8|a4
- self.sections.append( (offset, flags, val) )
-
- sect = self.loadSection(0)
- records, = struct.unpack('>H', sect[0x8:0x8+2])
- mobi_length, = struct.unpack('>L',sect[0x14:0x18])
- mobi_version, = struct.unpack('>L',sect[0x68:0x6C])
- extra_data_flags = 0
- print "MOBI header version = %d, length = %d" %(mobi_version, mobi_length)
- if (mobi_length >= 0xE4) and (mobi_version >= 5):
- extra_data_flags, = struct.unpack('>H', sect[0xF2:0xF4])
- print "Extra Data Flags = %d" %extra_data_flags
- if mobi_version < 7:
- # multibyte utf8 data is included in the encryption for mobi_version 6 and below
- # so clear that byte so that we leave it to be decrypted.
- extra_data_flags &= 0xFFFE
-
- crypto_type, = struct.unpack('>H', sect[0xC:0xC+2])
+ def processBook(self, pidlist):
+ crypto_type, = struct.unpack('>H', self.sect[0xC:0xC+2])
+ print 'Crypto Type is: ', crypto_type
+ self.crypto_type = crypto_type
if crypto_type == 0:
print "This book is not encrypted."
- else:
- if crypto_type == 1:
- raise DrmException("cannot decode Mobipocket encryption type 1")
- if crypto_type != 2:
- raise DrmException("unknown encryption type: %d" % crypto_type)
+ return self.data_file
+ if crypto_type != 2 and crypto_type != 1:
+ raise DrmException("Cannot decode unknown Mobipocket encryption type %d" % crypto_type)
+ goodpids = []
+ for pid in pidlist:
+ if len(pid)==10:
+ if checksumPid(pid[0:-2]) != pid:
+ print "Warning: PID " + pid + " has incorrect checksum, should have been "+checksumPid(pid[0:-2])
+ goodpids.append(pid[0:-2])
+ elif len(pid)==8:
+ goodpids.append(pid)
+
+ if self.crypto_type == 1:
+ t1_keyvec = "QDCVEPMU675RUBSZ"
+ if self.magic == 'TEXtREAd':
+ bookkey_data = self.sect[0x0E:0x0E+16]
+ else:
+ bookkey_data = self.sect[0x90:0x90+16]
+ pid = "00000000"
+ found_key = PC1(t1_keyvec, bookkey_data)
+ else :
# calculate the keys
- drm_ptr, drm_count, drm_size, drm_flags = struct.unpack('>LLLL', sect[0xA8:0xA8+16])
+ drm_ptr, drm_count, drm_size, drm_flags = struct.unpack('>LLLL', self.sect[0xA8:0xA8+16])
if drm_count == 0:
- raise DrmException("no PIDs found in this file")
- found_key = self.parseDRM(sect[drm_ptr:drm_ptr+drm_size], drm_count, pid)
+ raise DrmException("Not yet initialised with PID. Must be opened with Mobipocket Reader first.")
+ found_key, pid = self.parseDRM(self.sect[drm_ptr:drm_ptr+drm_size], drm_count, goodpids)
if not found_key:
- raise DrmException("no key found. maybe the PID is incorrect")
-
+ raise DrmException("No key found. Most likely the correct PID has not been given.")
# kill the drm keys
self.patchSection(0, "\0" * drm_size, drm_ptr)
# kill the drm pointers
self.patchSection(0, "\xff" * 4 + "\0" * 12, 0xA8)
- # clear the crypto type
- self.patchSection(0, "\0" * 2, 0xC)
+
+ if pid=="00000000":
+ print "File has default encryption, no specific PID."
+ else:
+ print "File is encoded with PID "+checksumPid(pid)+"."
- # decrypt sections
- print "Decrypting. Please wait . . .",
- new_data = self.data_file[:self.sections[1][0]]
- for i in xrange(1, records+1):
- data = self.loadSection(i)
- extra_size = getSizeOfTrailingDataEntries(data, len(data), extra_data_flags)
- if i%100 == 0:
- print ".",
- # print "record %d, extra_size %d" %(i,extra_size)
- new_data += PC1(found_key, data[0:len(data) - extra_size])
- if extra_size > 0:
- new_data += data[-extra_size:]
- #self.patchSection(i, PC1(found_key, data[0:len(data) - extra_size]))
- if self.num_sections > records+1:
- new_data += self.data_file[self.sections[records+1][0]:]
- self.data_file = new_data
- print "done"
+ # clear the crypto type
+ self.patchSection(0, "\0" * 2, 0xC)
- def getResult(self):
+ # decrypt sections
+ print "Decrypting. Please wait . . .",
+ new_data = self.data_file[:self.sections[1][0]]
+ for i in xrange(1, self.records+1):
+ data = self.loadSection(i)
+ extra_size = getSizeOfTrailingDataEntries(data, len(data), self.extra_data_flags)
+ if i%100 == 0:
+ print ".",
+ # print "record %d, extra_size %d" %(i,extra_size)
+ new_data += PC1(found_key, data[0:len(data) - extra_size])
+ if extra_size > 0:
+ new_data += data[-extra_size:]
+ if self.num_sections > self.records+1:
+ new_data += self.data_file[self.sections[self.records+1][0]:]
+ self.data_file = new_data
+ print "done"
return self.data_file
def getUnencryptedBook(infile,pid):
- sys.stdout=Unbuffered(sys.stdout)
- data_file = file(infile, 'rb').read()
- strippedFile = DrmStripper(data_file, pid)
- return strippedFile.getResult()
+ if not os.path.isfile(infile):
+ raise DrmException('Input File Not Found')
+ book = MobiBook(infile)
+ return book.processBook([pid])
+
+def getUnencryptedBookWithList(infile,pidlist):
+ if not os.path.isfile(infile):
+ raise DrmException('Input File Not Found')
+ book = MobiBook(infile)
+ return book.processBook(pidlist)
def main(argv=sys.argv):
- sys.stdout=Unbuffered(sys.stdout)
print ('MobiDeDrm v%(__version__)s. '
'Copyright 2008-2010 The Dark Reverser.' % globals())
if len(argv)<4:
print "Removes protection from Mobipocket books"
print "Usage:"
- print " %s " % sys.argv[0]
+ print " %s " % sys.argv[0]
return 1
else:
infile = argv[1]
outfile = argv[2]
- pid = argv[3]
+ pidlist = argv[3].split(',')
try:
- stripped_file = getUnencryptedBook(infile, pid)
+ stripped_file = getUnencryptedBookWithList(infile, pidlist)
file(outfile, 'wb').write(stripped_file)
except DrmException, e:
print "Error: %s" % e
diff --git a/KindleBooks_Tools/MobiDeDRM.py b/KindleBooks_Tools/MobiDeDRM.py
index 536eb78..864b545 100644
--- a/KindleBooks_Tools/MobiDeDRM.py
+++ b/KindleBooks_Tools/MobiDeDRM.py
@@ -42,9 +42,10 @@
# 0.20 - Correction: It seems that multibyte entries are encrypted in a v6 file.
# 0.21 - Added support for multiple pids
# 0.22 - revised structure to hold MobiBook as a class to allow an extended interface
-# 0.23 - fixed problem with older files with no EXTH section
+# 0.23 - fixed problem with older files with no EXTH section
+# 0.24 - add support for type 1 encryption and 'TEXtREAd' books as well
-__version__ = '0.23'
+__version__ = '0.24'
import sys
@@ -58,6 +59,7 @@ class Unbuffered:
return getattr(self.stream, attr)
sys.stdout=Unbuffered(sys.stdout)
+import os
import struct
import binascii
@@ -155,8 +157,10 @@ class MobiBook:
# initial sanity check on file
self.data_file = file(infile, 'rb').read()
self.header = self.data_file[0:78]
- if self.header[0x3C:0x3C+8] != 'BOOKMOBI':
+ if self.header[0x3C:0x3C+8] != 'BOOKMOBI' and self.header[0x3C:0x3C+8] != 'TEXtREAd':
raise DrmException("invalid file format")
+ self.magic = self.header[0x3C:0x3C+8]
+ self.crypto_type = -1
# build up section offset and flag info
self.num_sections, = struct.unpack('>H', self.header[76:78])
@@ -169,6 +173,14 @@ class MobiBook:
# parse information from section 0
self.sect = self.loadSection(0)
self.records, = struct.unpack('>H', self.sect[0x8:0x8+2])
+
+ if self.magic == 'TEXtREAd':
+ print "Book has format: ", self.magic
+ self.extra_data_flags = 0
+ self.mobi_length = 0
+ self.mobi_version = -1
+ self.meta_array = {}
+ return
self.mobi_length, = struct.unpack('>L',self.sect[0x14:0x18])
self.mobi_version, = struct.unpack('>L',self.sect[0x68:0x6C])
print "MOBI header version = %d, length = %d" %(self.mobi_version, self.mobi_length)
@@ -199,7 +211,7 @@ class MobiBook:
except:
self.meta_array = {}
pass
-
+
def getBookTitle(self):
title = ''
if 503 in self.meta_array:
@@ -275,12 +287,12 @@ class MobiBook:
def processBook(self, pidlist):
crypto_type, = struct.unpack('>H', self.sect[0xC:0xC+2])
+ print 'Crypto Type is: ', crypto_type
+ self.crypto_type = crypto_type
if crypto_type == 0:
print "This book is not encrypted."
return self.data_file
- if crypto_type == 1:
- raise DrmException("Cannot decode Mobipocket encryption type 1")
- if crypto_type != 2:
+ if crypto_type != 2 and crypto_type != 1:
raise DrmException("Cannot decode unknown Mobipocket encryption type %d" % crypto_type)
goodpids = []
@@ -292,23 +304,32 @@ class MobiBook:
elif len(pid)==8:
goodpids.append(pid)
- # calculate the keys
- drm_ptr, drm_count, drm_size, drm_flags = struct.unpack('>LLLL', self.sect[0xA8:0xA8+16])
- if drm_count == 0:
- raise DrmException("Not yet initialised with PID. Must be opened with Mobipocket Reader first.")
- found_key, pid = self.parseDRM(self.sect[drm_ptr:drm_ptr+drm_size], drm_count, goodpids)
- if not found_key:
- raise DrmException("No key found. Most likely the correct PID has not been given.")
+ if self.crypto_type == 1:
+ t1_keyvec = "QDCVEPMU675RUBSZ"
+ if self.magic == 'TEXtREAd':
+ bookkey_data = self.sect[0x0E:0x0E+16]
+ else:
+ bookkey_data = self.sect[0x90:0x90+16]
+ pid = "00000000"
+ found_key = PC1(t1_keyvec, bookkey_data)
+ else :
+ # calculate the keys
+ drm_ptr, drm_count, drm_size, drm_flags = struct.unpack('>LLLL', self.sect[0xA8:0xA8+16])
+ if drm_count == 0:
+ raise DrmException("Not yet initialised with PID. Must be opened with Mobipocket Reader first.")
+ found_key, pid = self.parseDRM(self.sect[drm_ptr:drm_ptr+drm_size], drm_count, goodpids)
+ if not found_key:
+ raise DrmException("No key found. Most likely the correct PID has not been given.")
+ # kill the drm keys
+ self.patchSection(0, "\0" * drm_size, drm_ptr)
+ # kill the drm pointers
+ self.patchSection(0, "\xff" * 4 + "\0" * 12, 0xA8)
if pid=="00000000":
print "File has default encryption, no specific PID."
else:
print "File is encoded with PID "+checksumPid(pid)+"."
- # kill the drm keys
- self.patchSection(0, "\0" * drm_size, drm_ptr)
- # kill the drm pointers
- self.patchSection(0, "\xff" * 4 + "\0" * 12, 0xA8)
# clear the crypto type
self.patchSection(0, "\0" * 2, 0xC)
diff --git a/Mobi_Additional_Tools/FindTopazEbooks.pyw b/Mobi_Additional_Tools/FindTopazEbooks.pyw
new file mode 100644
index 0000000..6a0df30
--- /dev/null
+++ b/Mobi_Additional_Tools/FindTopazEbooks.pyw
@@ -0,0 +1,216 @@
+#!/usr/bin/env python
+
+# This is a simple tool to identify all Amazon Topaz ebooks in a specific directory.
+# There always seems to be confusion since Topaz books downloaded to K4PC/Mac can have
+# almost any extension (.azw, .azw1, .prc, tpz). While the .azw1 and .tpz extensions
+# are fairly easy to indentify, the others are not (without opening the files in an editor).
+
+# To run the tool with the GUI frontend, just double-click on the 'FindTopazFiles.pyw' file
+# and select the folder where all of the ebooks in question are located. Then click 'Search'.
+# The program will list the file names of the ebooks that are indentified as being Topaz.
+# You can then isolate those books and use the Topaz tools to decrypt and convert them.
+
+# You can also run the script from a command line... supplying the folder to search
+# as a parameter: python FindTopazEbooks.pyw "C:\My Folder" (change appropriately for
+# your particular O.S.)
+
+# ** NOTE: This program does NOT decrypt or modify Topaz files in any way. It simply identifies them.
+
+# PLEASE DO NOT PIRATE EBOOKS!
+
+# We want all authors and publishers, and eBook stores to live
+# long and prosperous lives but at the same time we just want to
+# be able to read OUR books on whatever device we want and to keep
+# readable for a long, long time
+
+# This borrows very heavily from works by CMBDTC, IHeartCabbages, skindle,
+# unswindle, DarkReverser, ApprenticeAlf, DiapDealer, some_updates
+# and many many others
+
+# Revision history:
+# 1 - Initial release.
+
+from __future__ import with_statement
+
+__license__ = 'GPL v3'
+
+import sys
+import os
+import re
+import shutil
+import Tkinter
+import Tkconstants
+import tkFileDialog
+import tkMessageBox
+
+
+class ScrolledText(Tkinter.Text):
+ def __init__(self, master=None, **kw):
+ self.frame = Tkinter.Frame(master)
+ self.vbar = Tkinter.Scrollbar(self.frame)
+ self.vbar.pack(side=Tkconstants.RIGHT, fill=Tkconstants.Y)
+ kw.update({'yscrollcommand': self.vbar.set})
+ Tkinter.Text.__init__(self, self.frame, **kw)
+ self.pack(side=Tkconstants.LEFT, fill=Tkconstants.BOTH, expand=True)
+ self.vbar['command'] = self.yview
+ # Copy geometry methods of self.frame without overriding Text
+ # methods = hack!
+ text_meths = vars(Tkinter.Text).keys()
+ methods = vars(Tkinter.Pack).keys() + vars(Tkinter.Grid).keys() + vars(Tkinter.Place).keys()
+ methods = set(methods).difference(text_meths)
+ for m in methods:
+ if m[0] != '_' and m != 'config' and m != 'configure':
+ setattr(self, m, getattr(self.frame, m))
+
+ def __str__(self):
+ return str(self.frame)
+
+
+def cli_main(argv=sys.argv, obj=None):
+ progname = os.path.basename(argv[0])
+ if len(argv) != 2:
+ print "usage: %s DIRECTORY" % (progname,)
+ return 1
+
+ if obj == None:
+ print "\nTopaz search results:\n"
+ else:
+ obj.stext.insert(Tkconstants.END,"Topaz search results:\n\n")
+
+ inpath = argv[1]
+ files = os.listdir(inpath)
+ filefilter = re.compile("(\.azw$)|(\.azw1$)|(\.prc$)|(\.tpz$)", re.IGNORECASE)
+ files = filter(filefilter.search, files)
+
+ if files:
+ topazcount = 0
+ totalcount = 0
+ for filename in files:
+ with open(os.path.join(inpath, filename), 'rb') as f:
+ try:
+ if f.read().startswith('TPZ'):
+ f.close()
+ basename, extension = os.path.splitext(filename)
+ if obj == None:
+ print " %s is a Topaz formatted ebook." % filename
+ """
+ if extension == '.azw' or extension == '.prc':
+ print " renaming to %s" % (basename + '.tpz')
+ shutil.move(os.path.join(inpath, filename),
+ os.path.join(inpath, basename + '.tpz'))
+ """
+ else:
+ msg1 = " %s is a Topaz formatted ebook.\n" % filename
+ obj.stext.insert(Tkconstants.END,msg1)
+ """
+ if extension == '.azw' or extension == '.prc':
+ msg2 = " renaming to %s\n" % (basename + '.tpz')
+ obj.stext.insert(Tkconstants.END,msg2)
+ shutil.move(os.path.join(inpath, filename),
+ os.path.join(inpath, basename + '.tpz'))
+ """
+ topazcount += 1
+ except:
+ if obj == None:
+ print " Error reading %s." % filename
+ else:
+ msg = " Error reading or %s.\n" % filename
+ obj.stext.insert(Tkconstants.END,msg)
+ pass
+ totalcount += 1
+ if topazcount == 0:
+ if obj == None:
+ print "\nNo Topaz books found in %s." % inpath
+ else:
+ msg = "\nNo Topaz books found in %s.\n\n" % inpath
+ obj.stext.insert(Tkconstants.END,msg)
+ else:
+ if obj == None:
+ print "\n%i Topaz books found in %s\n%i total books checked.\n" % (topazcount, inpath, totalcount)
+ else:
+ msg = "\n%i Topaz books found in %s\n%i total books checked.\n\n" %(topazcount, inpath, totalcount)
+ obj.stext.insert(Tkconstants.END,msg)
+ else:
+ if obj == None:
+ print "No typical Topaz file extensions found in %s.\n" % inpath
+ else:
+ msg = "No typical Topaz file extensions found in %s.\n\n" % inpath
+ obj.stext.insert(Tkconstants.END,msg)
+
+ return 0
+
+
+class DecryptionDialog(Tkinter.Frame):
+ def __init__(self, root):
+ Tkinter.Frame.__init__(self, root, border=5)
+ ltext='Search a directory for Topaz eBooks\n'
+ self.status = Tkinter.Label(self, text=ltext)
+ self.status.pack(fill=Tkconstants.X, expand=1)
+ body = Tkinter.Frame(self)
+ body.pack(fill=Tkconstants.X, expand=1)
+ sticky = Tkconstants.E + Tkconstants.W
+ body.grid_columnconfigure(1, weight=2)
+ Tkinter.Label(body, text='Directory to Search').grid(row=1)
+ self.inpath = Tkinter.Entry(body, width=30)
+ self.inpath.grid(row=1, column=1, sticky=sticky)
+ button = Tkinter.Button(body, text="...", command=self.get_inpath)
+ button.grid(row=1, column=2)
+ msg1 = 'Topaz search results \n\n'
+ self.stext = ScrolledText(body, bd=5, relief=Tkconstants.RIDGE,
+ height=15, width=60, wrap=Tkconstants.WORD)
+ self.stext.grid(row=4, column=0, columnspan=2,sticky=sticky)
+ #self.stext.insert(Tkconstants.END,msg1)
+ buttons = Tkinter.Frame(self)
+ buttons.pack()
+
+
+ self.botton = Tkinter.Button(
+ buttons, text="Search", width=10, command=self.search)
+ self.botton.pack(side=Tkconstants.LEFT)
+ Tkinter.Frame(buttons, width=10).pack(side=Tkconstants.LEFT)
+ self.button = Tkinter.Button(
+ buttons, text="Quit", width=10, command=self.quit)
+ self.button.pack(side=Tkconstants.RIGHT)
+
+ def get_inpath(self):
+ cwd = os.getcwdu()
+ cwd = cwd.encode('utf-8')
+ inpath = tkFileDialog.askdirectory(
+ parent=None, title='Directory to search',
+ initialdir=cwd, initialfile=None)
+ if inpath:
+ inpath = os.path.normpath(inpath)
+ self.inpath.delete(0, Tkconstants.END)
+ self.inpath.insert(0, inpath)
+ return
+
+
+ def search(self):
+ inpath = self.inpath.get()
+ if not inpath or not os.path.exists(inpath):
+ self.status['text'] = 'Specified directory does not exist'
+ return
+ argv = [sys.argv[0], inpath]
+ self.status['text'] = 'Searching...'
+ self.botton.configure(state='disabled')
+ cli_main(argv, self)
+ self.status['text'] = 'Search a directory for Topaz files'
+ self.botton.configure(state='normal')
+
+ return
+
+
+def gui_main():
+ root = Tkinter.Tk()
+ root.title('Topaz eBook Finder')
+ root.resizable(True, False)
+ root.minsize(370, 0)
+ DecryptionDialog(root).pack(fill=Tkconstants.X, expand=1)
+ root.mainloop()
+ return 0
+
+
+if __name__ == '__main__':
+ if len(sys.argv) > 1:
+ sys.exit(cli_main())
+ sys.exit(gui_main())
\ No newline at end of file
diff --git a/Mobi_Additional_Tools/KindlePID.pyw b/Mobi_Additional_Tools/KindlePID.pyw
index 771e288..73f7d3f 100644
--- a/Mobi_Additional_Tools/KindlePID.pyw
+++ b/Mobi_Additional_Tools/KindlePID.pyw
@@ -74,6 +74,8 @@ class MainDialog(Tkinter.Frame):
def showCmdOutput(self, msg):
if msg and msg !='':
msg = msg.encode('utf-8')
+ if sys.platform.startswith('win'):
+ msg = msg.replace('\r\n','\n')
self.stext.insert(Tkconstants.END,msg)
self.stext.yview_pickplace(Tkconstants.END)
return
diff --git a/Mobi_Additional_Tools/Kindleizer.pyw b/Mobi_Additional_Tools/Kindleizer.pyw
index a94b7d3..a725626 100644
--- a/Mobi_Additional_Tools/Kindleizer.pyw
+++ b/Mobi_Additional_Tools/Kindleizer.pyw
@@ -83,6 +83,8 @@ class MainDialog(Tkinter.Frame):
def showCmdOutput(self, msg):
if msg and msg !='':
msg = msg.encode('utf-8')
+ if sys.platform.startswith('win'):
+ msg = msg.replace('\r\n','\n')
self.stext.insert(Tkconstants.END,msg)
self.stext.yview_pickplace(Tkconstants.END)
return
diff --git a/Mobi_Additional_Tools/MobiDeDRM.pyw b/Mobi_Additional_Tools/MobiDeDRM.pyw
new file mode 100644
index 0000000..055d050
--- /dev/null
+++ b/Mobi_Additional_Tools/MobiDeDRM.pyw
@@ -0,0 +1,199 @@
+#!/usr/bin/env python
+# vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab
+
+import sys
+sys.path.append('lib')
+import os, os.path, urllib
+import subprocess
+from subprocess import Popen, PIPE, STDOUT
+import subasyncio
+from subasyncio import Process
+import Tkinter
+import Tkconstants
+import tkFileDialog
+import tkMessageBox
+from scrolltextwidget import ScrolledText
+
+class MainDialog(Tkinter.Frame):
+ def __init__(self, root):
+ Tkinter.Frame.__init__(self, root, border=5)
+ self.root = root
+ self.interval = 2000
+ self.p2 = None
+ self.status = Tkinter.Label(self, text='Remove Encryption from a Mobi eBook')
+ self.status.pack(fill=Tkconstants.X, expand=1)
+ body = Tkinter.Frame(self)
+ body.pack(fill=Tkconstants.X, expand=1)
+ sticky = Tkconstants.E + Tkconstants.W
+ body.grid_columnconfigure(1, weight=2)
+
+ Tkinter.Label(body, text='Mobi eBook input file').grid(row=0, sticky=Tkconstants.E)
+ self.mobipath = Tkinter.Entry(body, width=50)
+ self.mobipath.grid(row=0, column=1, sticky=sticky)
+ cwd = os.getcwdu()
+ cwd = cwd.encode('utf-8')
+ self.mobipath.insert(0, cwd)
+ button = Tkinter.Button(body, text="...", command=self.get_mobipath)
+ button.grid(row=0, column=2)
+
+ Tkinter.Label(body, text='Name for Unencrypted Output File').grid(row=1, sticky=Tkconstants.E)
+ self.outpath = Tkinter.Entry(body, width=50)
+ self.outpath.grid(row=1, column=1, sticky=sticky)
+ self.outpath.insert(0, '')
+ button = Tkinter.Button(body, text="...", command=self.get_outpath)
+ button.grid(row=1, column=2)
+
+ Tkinter.Label(body, text='10 Character PID').grid(row=2, sticky=Tkconstants.E)
+ self.pidnum = Tkinter.StringVar()
+ self.pidinfo = Tkinter.Entry(body, width=12, textvariable=self.pidnum)
+ self.pidinfo.grid(row=2, column=1, sticky=sticky)
+
+ msg1 = 'Conversion Log \n\n'
+ self.stext = ScrolledText(body, bd=5, relief=Tkconstants.RIDGE, height=15, width=60, wrap=Tkconstants.WORD)
+ self.stext.grid(row=3, column=0, columnspan=2,sticky=sticky)
+ self.stext.insert(Tkconstants.END,msg1)
+
+ buttons = Tkinter.Frame(self)
+ buttons.pack()
+ self.sbotton = Tkinter.Button(
+ buttons, text="Start", width=10, command=self.convertit)
+ self.sbotton.pack(side=Tkconstants.LEFT)
+
+ Tkinter.Frame(buttons, width=10).pack(side=Tkconstants.LEFT)
+ self.qbutton = Tkinter.Button(
+ buttons, text="Quit", width=10, command=self.quitting)
+ self.qbutton.pack(side=Tkconstants.RIGHT)
+
+ # read from subprocess pipe without blocking
+ # invoked every interval via the widget "after"
+ # option being used, so need to reset it for the next time
+ def processPipe(self):
+ poll = self.p2.wait('nowait')
+ if poll != None:
+ text = self.p2.readerr()
+ text += self.p2.read()
+ msg = text + '\n\n' + 'Encryption successfully removed\n'
+ if poll != 0:
+ msg = text + '\n\n' + 'Error: Encryption Removal Failed\n'
+ self.showCmdOutput(msg)
+ self.p2 = None
+ self.sbotton.configure(state='normal')
+ return
+ text = self.p2.readerr()
+ text += self.p2.read()
+ self.showCmdOutput(text)
+ # make sure we get invoked again by event loop after interval
+ self.stext.after(self.interval,self.processPipe)
+ return
+
+ # post output from subprocess in scrolled text widget
+ def showCmdOutput(self, msg):
+ if msg and msg !='':
+ msg = msg.encode('utf-8')
+ if sys.platform.startswith('win'):
+ msg = msg.replace('\r\n','\n')
+ self.stext.insert(Tkconstants.END,msg)
+ self.stext.yview_pickplace(Tkconstants.END)
+ return
+
+ # run as a subprocess via pipes and collect stdout
+ def mobirdr(self, infile, outfile, pidnum):
+ # os.putenv('PYTHONUNBUFFERED', '1')
+ cmdline = 'python ./lib/mobidedrm.py "' + infile + '" "' + outfile + '" "' + pidnum + '"'
+ if sys.platform[0:3] == 'win':
+ search_path = os.environ['PATH']
+ search_path = search_path.lower()
+ if search_path.find('python') >= 0:
+ cmdline = 'python lib\mobidedrm.py "' + infile + '" "' + outfile + '" "' + pidnum + '"'
+ else :
+ cmdline = 'lib\mobidedrm.py "' + infile + '" "' + outfile + '" "' + pidnum + '"'
+
+ cmdline = cmdline.encode(sys.getfilesystemencoding())
+ p2 = Process(cmdline, shell=True, bufsize=1, stdin=None, stdout=PIPE, stderr=PIPE, close_fds=False)
+ return p2
+
+
+ def get_mobipath(self):
+ mobipath = tkFileDialog.askopenfilename(
+ parent=None, title='Select Mobi eBook File',
+ defaultextension='.prc', filetypes=[('Mobi eBook File', '.prc'), ('Mobi eBook File', '.azw'),('Mobi eBook File', '.mobi'),
+ ('All Files', '.*')])
+ if mobipath:
+ mobipath = os.path.normpath(mobipath)
+ self.mobipath.delete(0, Tkconstants.END)
+ self.mobipath.insert(0, mobipath)
+ return
+
+ def get_outpath(self):
+ mobipath = self.mobipath.get()
+ initname = os.path.basename(mobipath)
+ p = initname.find('.')
+ if p >= 0: initname = initname[0:p]
+ initname += '_nodrm.mobi'
+ outpath = tkFileDialog.asksaveasfilename(
+ parent=None, title='Select Unencrypted Mobi File to produce',
+ defaultextension='.mobi', initialfile=initname,
+ filetypes=[('Mobi files', '.mobi'), ('All files', '.*')])
+ if outpath:
+ outpath = os.path.normpath(outpath)
+ self.outpath.delete(0, Tkconstants.END)
+ self.outpath.insert(0, outpath)
+ return
+
+ def quitting(self):
+ # kill any still running subprocess
+ if self.p2 != None:
+ if (self.p2.wait('nowait') == None):
+ self.p2.terminate()
+ self.root.destroy()
+
+ # actually ready to run the subprocess and get its output
+ def convertit(self):
+ # now disable the button to prevent multiple launches
+ self.sbotton.configure(state='disabled')
+ mobipath = self.mobipath.get()
+ outpath = self.outpath.get()
+ pidnum = self.pidinfo.get()
+ if not mobipath or not os.path.exists(mobipath):
+ self.status['text'] = 'Specified Mobi eBook file does not exist'
+ self.sbotton.configure(state='normal')
+ return
+ if not outpath:
+ self.status['text'] = 'No output file specified'
+ self.sbotton.configure(state='normal')
+ return
+ if not pidnum or pidnum == '':
+ self.status['text'] = 'No PID specified'
+ self.sbotton.configure(state='normal')
+ return
+
+ log = 'Command = "python mobidedrm.py"\n'
+ log += 'Mobi Path = "'+ mobipath + '"\n'
+ log += 'Output File = "' + outpath + '"\n'
+ log += 'PID = "' + pidnum + '"\n'
+ log += '\n\n'
+ log += 'Please Wait ...\n\n'
+ log = log.encode('utf-8')
+ self.stext.insert(Tkconstants.END,log)
+ self.p2 = self.mobirdr(mobipath, outpath, pidnum)
+
+ # python does not seem to allow you to create
+ # your own eventloop which every other gui does - strange
+ # so need to use the widget "after" command to force
+ # event loop to run non-gui events every interval
+ self.stext.after(self.interval,self.processPipe)
+ return
+
+
+def main(argv=None):
+ root = Tkinter.Tk()
+ root.title('Mobi eBook Encryption Removal')
+ root.resizable(True, False)
+ root.minsize(300, 0)
+ MainDialog(root).pack(fill=Tkconstants.X, expand=1)
+ root.mainloop()
+ return 0
+
+
+if __name__ == "__main__":
+ sys.exit(main())
diff --git a/Mobi_Additional_Tools/lib/kindlepid.py b/Mobi_Additional_Tools/lib/kindlepid.py
index 15ca5e5..5041bd4 100644
--- a/Mobi_Additional_Tools/lib/kindlepid.py
+++ b/Mobi_Additional_Tools/lib/kindlepid.py
@@ -68,7 +68,7 @@ def main(argv=sys.argv):
print "Usage: kindlepid.py /"
return 1
if len(serial)==16:
- if serial.startswith("B00"):
+ if serial.startswith("B"):
print "Kindle serial number detected"
else:
print "Warning: unrecognized serial number. Please recheck input."
diff --git a/Mobi_Additional_Tools/lib/mobidedrm.py b/Mobi_Additional_Tools/lib/mobidedrm.py
index 183432c..864b545 100644
--- a/Mobi_Additional_Tools/lib/mobidedrm.py
+++ b/Mobi_Additional_Tools/lib/mobidedrm.py
@@ -24,7 +24,7 @@
# 0.14 - Working out when the extra data flags are present has been problematic
# Versions 7 through 9 have tried to tweak the conditions, but have been
# only partially successful. Closer examination of lots of sample
-# files reveals that a confusin has arisen because trailing data entries
+# files reveals that a confusion has arisen because trailing data entries
# are not encrypted, but it turns out that the multibyte entries
# in utf8 file are encrypted. (Although neither kind gets compressed.)
# This knowledge leads to a simplification of the test for the
@@ -39,13 +39,15 @@
# Removed the disabled Calibre plug-in code
# Permit use of 8-digit PIDs
# 0.19 - It seems that multibyte entries aren't encrypted in a v6 file either.
-# 0.20 - Corretion: It seems that multibyte entries are encrypted in a v6 file.
+# 0.20 - Correction: It seems that multibyte entries are encrypted in a v6 file.
+# 0.21 - Added support for multiple pids
+# 0.22 - revised structure to hold MobiBook as a class to allow an extended interface
+# 0.23 - fixed problem with older files with no EXTH section
+# 0.24 - add support for type 1 encryption and 'TEXtREAd' books as well
-__version__ = '0.20'
+__version__ = '0.24'
import sys
-import struct
-import binascii
class Unbuffered:
def __init__(self, stream):
@@ -55,10 +57,20 @@ class Unbuffered:
self.stream.flush()
def __getattr__(self, attr):
return getattr(self.stream, attr)
+sys.stdout=Unbuffered(sys.stdout)
+
+import os
+import struct
+import binascii
class DrmException(Exception):
pass
+
+#
+# MobiBook Utility Routines
+#
+
# Implementation of Pukall Cipher 1
def PC1(key, src, decryption=True):
sum1 = 0;
@@ -70,7 +82,6 @@ def PC1(key, src, decryption=True):
wkey = []
for i in xrange(8):
wkey.append(ord(key[i*2])<<8 | ord(key[i*2+1]))
-
dst = ""
for i in xrange(len(src)):
temp1 = 0;
@@ -131,7 +142,9 @@ def getSizeOfTrailingDataEntries(ptr, size, flags):
num += (ord(ptr[size - num - 1]) & 0x3) + 1
return num
-class DrmStripper:
+
+
+class MobiBook:
def loadSection(self, section):
if (section + 1 == self.num_sections):
endoff = len(self.data_file)
@@ -140,6 +153,93 @@ class DrmStripper:
off = self.sections[section][0]
return self.data_file[off:endoff]
+ def __init__(self, infile):
+ # initial sanity check on file
+ self.data_file = file(infile, 'rb').read()
+ self.header = self.data_file[0:78]
+ if self.header[0x3C:0x3C+8] != 'BOOKMOBI' and self.header[0x3C:0x3C+8] != 'TEXtREAd':
+ raise DrmException("invalid file format")
+ self.magic = self.header[0x3C:0x3C+8]
+ self.crypto_type = -1
+
+ # build up section offset and flag info
+ self.num_sections, = struct.unpack('>H', self.header[76:78])
+ self.sections = []
+ for i in xrange(self.num_sections):
+ offset, a1,a2,a3,a4 = struct.unpack('>LBBBB', self.data_file[78+i*8:78+i*8+8])
+ flags, val = a1, a2<<16|a3<<8|a4
+ self.sections.append( (offset, flags, val) )
+
+ # parse information from section 0
+ self.sect = self.loadSection(0)
+ self.records, = struct.unpack('>H', self.sect[0x8:0x8+2])
+
+ if self.magic == 'TEXtREAd':
+ print "Book has format: ", self.magic
+ self.extra_data_flags = 0
+ self.mobi_length = 0
+ self.mobi_version = -1
+ self.meta_array = {}
+ return
+ self.mobi_length, = struct.unpack('>L',self.sect[0x14:0x18])
+ self.mobi_version, = struct.unpack('>L',self.sect[0x68:0x6C])
+ print "MOBI header version = %d, length = %d" %(self.mobi_version, self.mobi_length)
+ self.extra_data_flags = 0
+ if (self.mobi_length >= 0xE4) and (self.mobi_version >= 5):
+ self.extra_data_flags, = struct.unpack('>H', self.sect[0xF2:0xF4])
+ print "Extra Data Flags = %d" % self.extra_data_flags
+ if self.mobi_version < 7:
+ # multibyte utf8 data is included in the encryption for mobi_version 6 and below
+ # so clear that byte so that we leave it to be decrypted.
+ self.extra_data_flags &= 0xFFFE
+
+ # if exth region exists parse it for metadata array
+ self.meta_array = {}
+ try:
+ exth_flag, = struct.unpack('>L', self.sect[0x80:0x84])
+ exth = 'NONE'
+ if exth_flag & 0x40:
+ exth = self.sect[16 + self.mobi_length:]
+ if (len(exth) >= 4) and (exth[:4] == 'EXTH'):
+ nitems, = struct.unpack('>I', exth[8:12])
+ pos = 12
+ for i in xrange(nitems):
+ type, size = struct.unpack('>II', exth[pos: pos + 8])
+ content = exth[pos + 8: pos + size]
+ self.meta_array[type] = content
+ pos += size
+ except:
+ self.meta_array = {}
+ pass
+
+ def getBookTitle(self):
+ title = ''
+ if 503 in self.meta_array:
+ title = self.meta_array[503]
+ else :
+ toff, tlen = struct.unpack('>II', self.sect[0x54:0x5c])
+ tend = toff + tlen
+ title = self.sect[toff:tend]
+ if title == '':
+ title = self.header[:32]
+ title = title.split("\0")[0]
+ return title
+
+ def getPIDMetaInfo(self):
+ rec209 = None
+ token = None
+ if 209 in self.meta_array:
+ rec209 = self.meta_array[209]
+ data = rec209
+ # Parse the 209 data to find the the exth record with the token data.
+ # The last character of the 209 data points to the record with the token.
+ # Always 208 from my experience, but I'll leave the logic in case that changes.
+ for i in xrange(len(data)):
+ if ord(data[i]) != 0:
+ if self.meta_array[ord(data[i])] != None:
+ token = self.meta_array[ord(data[i])]
+ return rec209, token
+
def patch(self, off, new):
self.data_file = self.data_file[:off] + new + self.data_file[off+len(new):]
@@ -152,134 +252,131 @@ class DrmStripper:
assert off + in_off + len(new) <= endoff
self.patch(off + in_off, new)
- def parseDRM(self, data, count, pid):
- pid = pid.ljust(16,'\0')
- keyvec1 = "\x72\x38\x33\xB0\xB4\xF2\xE3\xCA\xDF\x09\x01\xD6\xE2\xE0\x3F\x96"
- temp_key = PC1(keyvec1, pid, False)
- temp_key_sum = sum(map(ord,temp_key)) & 0xff
+ def parseDRM(self, data, count, pidlist):
found_key = None
- for i in xrange(count):
- verification, size, type, cksum, cookie = struct.unpack('>LLLBxxx32s', data[i*0x30:i*0x30+0x30])
- cookie = PC1(temp_key, cookie)
- ver,flags,finalkey,expiry,expiry2 = struct.unpack('>LL16sLL', cookie)
- if verification == ver and cksum == temp_key_sum and (flags & 0x1F) == 1:
- found_key = finalkey
+ keyvec1 = "\x72\x38\x33\xB0\xB4\xF2\xE3\xCA\xDF\x09\x01\xD6\xE2\xE0\x3F\x96"
+ for pid in pidlist:
+ bigpid = pid.ljust(16,'\0')
+ temp_key = PC1(keyvec1, bigpid, False)
+ temp_key_sum = sum(map(ord,temp_key)) & 0xff
+ found_key = None
+ for i in xrange(count):
+ verification, size, type, cksum, cookie = struct.unpack('>LLLBxxx32s', data[i*0x30:i*0x30+0x30])
+ if cksum == temp_key_sum:
+ cookie = PC1(temp_key, cookie)
+ ver,flags,finalkey,expiry,expiry2 = struct.unpack('>LL16sLL', cookie)
+ if verification == ver and (flags & 0x1F) == 1:
+ found_key = finalkey
+ break
+ if found_key != None:
break
if not found_key:
# Then try the default encoding that doesn't require a PID
+ pid = "00000000"
temp_key = keyvec1
temp_key_sum = sum(map(ord,temp_key)) & 0xff
for i in xrange(count):
verification, size, type, cksum, cookie = struct.unpack('>LLLBxxx32s', data[i*0x30:i*0x30+0x30])
- cookie = PC1(temp_key, cookie)
- ver,flags,finalkey,expiry,expiry2 = struct.unpack('>LL16sLL', cookie)
- if verification == ver and cksum == temp_key_sum:
- found_key = finalkey
- break
- return found_key
+ if cksum == temp_key_sum:
+ cookie = PC1(temp_key, cookie)
+ ver,flags,finalkey,expiry,expiry2 = struct.unpack('>LL16sLL', cookie)
+ if verification == ver:
+ found_key = finalkey
+ break
+ return [found_key,pid]
- def __init__(self, data_file, pid):
- if len(pid)==10:
- if checksumPid(pid[0:-2]) != pid:
- raise DrmException("invalid PID checksum")
- pid = pid[0:-2]
- elif len(pid)==8:
- print "PID without checksum given. With checksum PID is "+checksumPid(pid)
- else:
- raise DrmException("Invalid PID length")
-
- self.data_file = data_file
- header = data_file[0:72]
- if header[0x3C:0x3C+8] != 'BOOKMOBI':
- raise DrmException("invalid file format")
- self.num_sections, = struct.unpack('>H', data_file[76:78])
-
- self.sections = []
- for i in xrange(self.num_sections):
- offset, a1,a2,a3,a4 = struct.unpack('>LBBBB', data_file[78+i*8:78+i*8+8])
- flags, val = a1, a2<<16|a3<<8|a4
- self.sections.append( (offset, flags, val) )
-
- sect = self.loadSection(0)
- records, = struct.unpack('>H', sect[0x8:0x8+2])
- mobi_length, = struct.unpack('>L',sect[0x14:0x18])
- mobi_version, = struct.unpack('>L',sect[0x68:0x6C])
- extra_data_flags = 0
- print "MOBI header version = %d, length = %d" %(mobi_version, mobi_length)
- if (mobi_length >= 0xE4) and (mobi_version >= 5):
- extra_data_flags, = struct.unpack('>H', sect[0xF2:0xF4])
- print "Extra Data Flags = %d" %extra_data_flags
- if mobi_version < 7:
- # multibyte utf8 data is included in the encryption for mobi_version 6 and below
- # so clear that byte so that we leave it to be decrypted.
- extra_data_flags &= 0xFFFE
-
- crypto_type, = struct.unpack('>H', sect[0xC:0xC+2])
+ def processBook(self, pidlist):
+ crypto_type, = struct.unpack('>H', self.sect[0xC:0xC+2])
+ print 'Crypto Type is: ', crypto_type
+ self.crypto_type = crypto_type
if crypto_type == 0:
print "This book is not encrypted."
- else:
- if crypto_type == 1:
- raise DrmException("cannot decode Mobipocket encryption type 1")
- if crypto_type != 2:
- raise DrmException("unknown encryption type: %d" % crypto_type)
+ return self.data_file
+ if crypto_type != 2 and crypto_type != 1:
+ raise DrmException("Cannot decode unknown Mobipocket encryption type %d" % crypto_type)
+ goodpids = []
+ for pid in pidlist:
+ if len(pid)==10:
+ if checksumPid(pid[0:-2]) != pid:
+ print "Warning: PID " + pid + " has incorrect checksum, should have been "+checksumPid(pid[0:-2])
+ goodpids.append(pid[0:-2])
+ elif len(pid)==8:
+ goodpids.append(pid)
+
+ if self.crypto_type == 1:
+ t1_keyvec = "QDCVEPMU675RUBSZ"
+ if self.magic == 'TEXtREAd':
+ bookkey_data = self.sect[0x0E:0x0E+16]
+ else:
+ bookkey_data = self.sect[0x90:0x90+16]
+ pid = "00000000"
+ found_key = PC1(t1_keyvec, bookkey_data)
+ else :
# calculate the keys
- drm_ptr, drm_count, drm_size, drm_flags = struct.unpack('>LLLL', sect[0xA8:0xA8+16])
+ drm_ptr, drm_count, drm_size, drm_flags = struct.unpack('>LLLL', self.sect[0xA8:0xA8+16])
if drm_count == 0:
- raise DrmException("no PIDs found in this file")
- found_key = self.parseDRM(sect[drm_ptr:drm_ptr+drm_size], drm_count, pid)
+ raise DrmException("Not yet initialised with PID. Must be opened with Mobipocket Reader first.")
+ found_key, pid = self.parseDRM(self.sect[drm_ptr:drm_ptr+drm_size], drm_count, goodpids)
if not found_key:
- raise DrmException("no key found. maybe the PID is incorrect")
-
+ raise DrmException("No key found. Most likely the correct PID has not been given.")
# kill the drm keys
self.patchSection(0, "\0" * drm_size, drm_ptr)
# kill the drm pointers
self.patchSection(0, "\xff" * 4 + "\0" * 12, 0xA8)
- # clear the crypto type
- self.patchSection(0, "\0" * 2, 0xC)
+
+ if pid=="00000000":
+ print "File has default encryption, no specific PID."
+ else:
+ print "File is encoded with PID "+checksumPid(pid)+"."
- # decrypt sections
- print "Decrypting. Please wait . . .",
- new_data = self.data_file[:self.sections[1][0]]
- for i in xrange(1, records+1):
- data = self.loadSection(i)
- extra_size = getSizeOfTrailingDataEntries(data, len(data), extra_data_flags)
- if i%100 == 0:
- print ".",
- # print "record %d, extra_size %d" %(i,extra_size)
- new_data += PC1(found_key, data[0:len(data) - extra_size])
- if extra_size > 0:
- new_data += data[-extra_size:]
- #self.patchSection(i, PC1(found_key, data[0:len(data) - extra_size]))
- if self.num_sections > records+1:
- new_data += self.data_file[self.sections[records+1][0]:]
- self.data_file = new_data
- print "done"
+ # clear the crypto type
+ self.patchSection(0, "\0" * 2, 0xC)
- def getResult(self):
+ # decrypt sections
+ print "Decrypting. Please wait . . .",
+ new_data = self.data_file[:self.sections[1][0]]
+ for i in xrange(1, self.records+1):
+ data = self.loadSection(i)
+ extra_size = getSizeOfTrailingDataEntries(data, len(data), self.extra_data_flags)
+ if i%100 == 0:
+ print ".",
+ # print "record %d, extra_size %d" %(i,extra_size)
+ new_data += PC1(found_key, data[0:len(data) - extra_size])
+ if extra_size > 0:
+ new_data += data[-extra_size:]
+ if self.num_sections > self.records+1:
+ new_data += self.data_file[self.sections[self.records+1][0]:]
+ self.data_file = new_data
+ print "done"
return self.data_file
def getUnencryptedBook(infile,pid):
- sys.stdout=Unbuffered(sys.stdout)
- data_file = file(infile, 'rb').read()
- strippedFile = DrmStripper(data_file, pid)
- return strippedFile.getResult()
+ if not os.path.isfile(infile):
+ raise DrmException('Input File Not Found')
+ book = MobiBook(infile)
+ return book.processBook([pid])
+
+def getUnencryptedBookWithList(infile,pidlist):
+ if not os.path.isfile(infile):
+ raise DrmException('Input File Not Found')
+ book = MobiBook(infile)
+ return book.processBook(pidlist)
def main(argv=sys.argv):
- sys.stdout=Unbuffered(sys.stdout)
print ('MobiDeDrm v%(__version__)s. '
'Copyright 2008-2010 The Dark Reverser.' % globals())
if len(argv)<4:
print "Removes protection from Mobipocket books"
print "Usage:"
- print " %s " % sys.argv[0]
+ print " %s " % sys.argv[0]
return 1
else:
infile = argv[1]
outfile = argv[2]
- pid = argv[3]
+ pidlist = argv[3].split(',')
try:
- stripped_file = getUnencryptedBook(infile, pid)
+ stripped_file = getUnencryptedBookWithList(infile, pidlist)
file(outfile, 'wb').write(stripped_file)
except DrmException, e:
print "Error: %s" % e
diff --git a/ReadMe_First.txt b/ReadMe_First.txt
new file mode 100644
index 0000000..738fc9a
--- /dev/null
+++ b/ReadMe_First.txt
@@ -0,0 +1,96 @@
+
+
+Welcome to the tools!
+
+The set includes tools to remove DRM from eReader PDB books, Barnes and Noble ePubs, Adobe ePubs, Adobe PDFs, and Kindle/Mobi ebooks (including Topaz).
+
+
+This ReadMe_First.txt is meant to give users a quick overview of what is available and how to get started.
+
+
+Calibre Users (Mac OS X, Linux, Windows)
+-------------
+If you are a calibre user, the quickest and easiest way to remove DRM form your ebooks is to open the Calibre_Plugins folder and install each of the plugins following the instructions and configuration directions provided in each plugins README file.
+
+Once installed and configured, you can simply import a DRM book into Calibre and end up with the DeDRM version in the Calibre database.
+
+These plugins work for Windows, Mac OS X, and Linux
+
+
+
+Mac OS X Users (Mac OS X 10.5 and 10.6)
+--------------
+Drag the DeDRM X.X.app droplet to your Desktop. Double-click on it once and it will guide you through collecting the data it needs to remove the DRM.
+
+To use it simply drag a book onto the droplet, and a DeDRM version will appear. This tools supports dragging and dropping of folders of ebooks as well.
+
+
+
+Not a Calibre or a Mac OS X DeDRM User?
+----------------------------------------
+There are a number of python based tools that have graphical user interfaces to make them easy to use. To use any of these tools, you need to have Python 2.5, 2.6, or 2.7 for 32 bits installed on your machine as well as a matching PyCrypto or OpenSSL for some tools.
+
+On Mac OS X (10.5 and 10.6) and Linux (recent versions), your systems already have the proper Python and OpenSSL installed. So nothing need be done, you can already run these tools by double-clicking on the .pyw python scripts.
+
+Users of Mac OS X 10.3 and 10.4, need to download and install the "32-bit Mac Installer disk Image (2.7.X) for OS X 10.3 and later from http://www.python.org/download/releases/2.7.1/
+
+On Windows, you need to install a 32 bit version of Python (even on Windows 64) plus a matching 32 bit version of PyCrypto *OR* OpenSSL. See the end of this document for details.
+
+The scripts are organized by type of ebook you need to remove the DRM from. Choose from among:
+
+ "Adobe_ePub_Tools"
+ "Adobe_PDF_Tools"
+ "Barnes_and_Noble_ePub_Tools"
+ "eReader_PDB_Tools"
+ "KindleBooks_Tools"
+
+by simply opening that folder.
+
+In the "KindleBooks_Tools" folder the primary tool is in the "KindleBooks" folder.
+
+If you are a Windows user, or a Linux platform using Wine, or Mac OS X or have trouble running the KindleBooks tools, there are two other tools provided. These are called "Kindle_4_Mac_Unswindle" and "Kindle_4_PC_Unswindle".
+
+Look for a README inside of the relevant folder to get you started.
+
+
+
+Additional Tools
+----------------------
+Some additional tools are also provided in the "Mobi_Additional_Tools" folder. There are tools for working with "Kindle for iPhone/iPod_Touch/iPad", finding Topaz ebooks, unpacking Mobi ebooks (without DRM) to get to the Mobi markup language inside, and etc.
+
+There is also an "ePub_Fixer" folder that can be used to fix broken DRM epubs that sometimes provided by Adobe and Barnes and Noble that actually violate the zip standard.
+
+Check out their readmes for more info.
+
+
+
+Windows and Python Tools
+------------------------
+We strongly recommend ActiveState's Active Python 2.6 or 2.7 Community Edition for Windows (x86) 32 bits. This can be downloaded for free from:
+
+ http://www.activestate.com/activepython/downloads
+
+
+In addition, Windows Users need one of PyCrypto OR OpenSSL.
+
+For OpenSSL:
+
+ Win32 OpenSSL v0.9.8o (8Mb)
+ http://www.slproweb.com/download/Win32OpenSSL-0_9_8o.exe
+ (if you get an error message about missing Visual C++
+ redistributables... cancel the install and install the
+ below support program from Microsoft, THEN install OpenSSL)
+
+ Visual C++ 2008 Redistributables (1.7Mb)
+ http://www.microsoft.com/downloads/details.aspx?familyid=9B2DA534-3E03-4391-8A4D-074B9F2BC1BF
+
+For PyCrypto:
+
+ There are many places to get PyCrypto installers for Windows. One such place is:
+
+ http://www.voidspace.org.uk/python/modules.shtml
+
+ Please get the latest PyCrypto meant for Windows 32 bit that matches the version of Python you installed (2.7, or 2.6)
+
+Once Windows users have installed Python 2.X for 32 bits, and the matching OpenSSL OR PyCrypto pieces, they too are ready to run the scripts.
+
diff --git a/ePub_Fixer/README_ePub_Fixer.txt b/ePub_Fixer/README_ePub_Fixer.txt
index f7316f8..b864d07 100644
--- a/ePub_Fixer/README_ePub_Fixer.txt
+++ b/ePub_Fixer/README_ePub_Fixer.txt
@@ -1,6 +1,6 @@
ePub_Fixer
-ePubs are specially crafted zip archives. Unfortunately, many of te DRM encoded Adobe Adept and Barnes & Noble ePubs are not "proper" zip archives in that the names of some files in the zip central directory do NOT match the local name given in archive itself. This type of zip archive is technically incorrect/corrupted and can not be read by many other programs.
+ePubs are specially crafted zip archives. Unfortunately, many of the DRM encoded Adobe Adept and Barnes & Noble ePubs are not "proper" zip archives in that the names of some files in the zip central directory do NOT match the local name given in archive itself. This type of zip archive is technically incorrect/corrupted and can not be read by many other programs.
ePub_Fixer was designed to fix improperly created zip archives of this type.
diff --git a/ePub_Fixer/ePub_Fixer.pyw b/ePub_Fixer/ePub_Fixer.pyw
index 6e3a53e..33f3f59 100644
--- a/ePub_Fixer/ePub_Fixer.pyw
+++ b/ePub_Fixer/ePub_Fixer.pyw
@@ -88,6 +88,8 @@ class MainDialog(Tkinter.Frame):
def showCmdOutput(self, msg):
if msg and msg !='':
msg = msg.encode('utf-8')
+ if sys.platform.startswith('win'):
+ msg = msg.replace('\r\n','\n')
self.stext.insert(Tkconstants.END,msg)
self.stext.yview_pickplace(Tkconstants.END)
return
diff --git a/eReader_PDB_Tools/Pml2HTML.pyw b/eReader_PDB_Tools/Pml2HTML.pyw
index b02922e..ead07ae 100644
--- a/eReader_PDB_Tools/Pml2HTML.pyw
+++ b/eReader_PDB_Tools/Pml2HTML.pyw
@@ -85,6 +85,8 @@ class MainDialog(Tkinter.Frame):
def showCmdOutput(self, msg):
if msg and msg !='':
msg = msg.encode('utf-8')
+ if sys.platform.startswith('win'):
+ msg = msg.replace('\r\n','\n')
self.stext.insert(Tkconstants.END,msg)
self.stext.yview_pickplace(Tkconstants.END)
return
diff --git a/eReader_PDB_Tools/eReaderPDB2PML.pyw b/eReader_PDB_Tools/eReaderPDB2PML.pyw
index 16da8cd..e230a44 100644
--- a/eReader_PDB_Tools/eReaderPDB2PML.pyw
+++ b/eReader_PDB_Tools/eReaderPDB2PML.pyw
@@ -98,6 +98,8 @@ class MainDialog(Tkinter.Frame):
def showCmdOutput(self, msg):
if msg and msg !='':
msg = msg.encode('utf-8')
+ if sys.platform.startswith('win'):
+ msg = msg.replace('\r\n','\n')
self.stext.insert(Tkconstants.END,msg)
self.stext.yview_pickplace(Tkconstants.END)
return
diff --git a/eReader_PDB_Tools/eReaderPDB2PMLZ.pyw b/eReader_PDB_Tools/eReaderPDB2PMLZ.pyw
index 635f3fe..82fcfb2 100644
--- a/eReader_PDB_Tools/eReaderPDB2PMLZ.pyw
+++ b/eReader_PDB_Tools/eReaderPDB2PMLZ.pyw
@@ -89,6 +89,8 @@ class MainDialog(Tkinter.Frame):
def showCmdOutput(self, msg):
if msg and msg !='':
msg = msg.encode('utf-8')
+ if sys.platform.startswith('win'):
+ msg = msg.replace('\r\n','\n')
self.stext.insert(Tkconstants.END,msg)
self.stext.yview_pickplace(Tkconstants.END)
return
diff --git a/eReader_PDB_Tools/lib/eReaderPDB2PML_plugin.py b/eReader_PDB_Tools/lib/eReaderPDB2PML_plugin.py
index 405ef7c..ad172a3 100644
--- a/eReader_PDB_Tools/lib/eReaderPDB2PML_plugin.py
+++ b/eReader_PDB_Tools/lib/eReaderPDB2PML_plugin.py
@@ -76,7 +76,6 @@ class eRdrDeDRM(FileTypePlugin):
if pmlfilepath and pmlfilepath != 1:
import zipfile
- import shutil
print " Creating PMLZ file"
myZipFile = zipfile.ZipFile(pmlzfile.name,'w',zipfile.ZIP_STORED, False)
list = os.listdir(outdir)
diff --git a/eReader_PDB_Tools/lib/erdr2pml.py b/eReader_PDB_Tools/lib/erdr2pml.py
index f4ad3c4..ce6945d 100644
--- a/eReader_PDB_Tools/lib/erdr2pml.py
+++ b/eReader_PDB_Tools/lib/erdr2pml.py
@@ -56,32 +56,9 @@
# 0.15 - enabled high-ascii to pml character encoding. DropBook now works on Mac.
# 0.16 - convert to use openssl DES (very very fast) or pure python DES if openssl's libcrypto is not available
# 0.17 - added support for pycrypto's DES as well
+# 0.18 - on Windows try PyCrypto first and OpenSSL next
-Des = None
-
-import openssl_des
-Des = openssl_des.load_libcrypto()
-
-# if that did not work then try pycrypto version of DES
-if Des == None:
- import pycrypto_des
- Des = pycrypto_des.load_pycrypto()
-
-# if that did not work then use pure python implementation
-# of DES and try to speed it up with Psycho
-if Des == None:
- import python_des
- Des = python_des.Des
- # Import Psyco if available
- try:
- # http://psyco.sourceforge.net
- import psyco
- psyco.full()
- except ImportError:
- pass
-
-
-__version__='0.17'
+__version__='0.18'
class Unbuffered:
def __init__(self, stream):
@@ -97,6 +74,37 @@ sys.stdout=Unbuffered(sys.stdout)
import struct, binascii, getopt, zlib, os, os.path, urllib, tempfile
+Des = None
+if sys.platform.startswith('win'):
+ # first try with pycrypto
+ import pycrypto_des
+ Des = pycrypto_des.load_pycrypto()
+ if Des == None:
+ # they try with openssl
+ import openssl_des
+ Des = openssl_des.load_libcrypto()
+else:
+ # first try with openssl
+ import openssl_des
+ Des = openssl_des.load_libcrypto()
+ if Des == None:
+ # then try with pycrypto
+ import pycrypto_des
+ Des = pycrypto_des.load_pycrypto()
+
+# if that did not work then use pure python implementation
+# of DES and try to speed it up with Psycho
+if Des == None:
+ import python_des
+ Des = python_des.Des
+ # Import Psyco if available
+ try:
+ # http://psyco.sourceforge.net
+ import psyco
+ psyco.full()
+ except ImportError:
+ pass
+
try:
from hashlib import sha1
except ImportError:
@@ -460,7 +468,7 @@ def main(argv=None):
myZipFile.write(imagePath, localname)
myZipFile.close()
# remove temporary directory
- shutil.rmtree(outdir)
+ shutil.rmtree(outdir, True)
end_time = time.time()
search_time = end_time - start_time