More general changes, and get mobidedrm and kindlekey to work on Mac.

This commit is contained in:
Apprentice Harper 2020-10-14 16:23:49 +01:00
parent e31752e334
commit 781268e17e
32 changed files with 479 additions and 497 deletions

View File

@ -220,7 +220,7 @@ class DeDRM(FileTypePlugin):
# Attempt to decrypt epub with each encryption key (generated or provided). # Attempt to decrypt epub with each encryption key (generated or provided).
for keyname, userkey in dedrmprefs['bandnkeys'].items(): for keyname, userkey in dedrmprefs['bandnkeys'].items():
keyname_masked = u"".join(("X" if (x.isdigit()) else x) for x in keyname) keyname_masked = "".join(("X" if (x.isdigit()) else x) for x in keyname)
print("{0} v{1}: Trying Encryption key {2:s}".format(PLUGIN_NAME, PLUGIN_VERSION, keyname_masked)) print("{0} v{1}: Trying Encryption key {2:s}".format(PLUGIN_NAME, PLUGIN_VERSION, keyname_masked))
of = self.temporary_file(".epub") of = self.temporary_file(".epub")
@ -359,7 +359,7 @@ class DeDRM(FileTypePlugin):
except: except:
print("{0} v{1}: Exception when getting default Adobe Key after {2:.1f} seconds".format(PLUGIN_NAME, PLUGIN_VERSION, time.time()-self.starttime)) print("{0} v{1}: Exception when getting default Adobe Key after {2:.1f} seconds".format(PLUGIN_NAME, PLUGIN_VERSION, time.time()-self.starttime))
traceback.print_exc() traceback.print_exc()
self.default_key = u"" self.default_key = ""
newkeys = [] newkeys = []
for keyvalue in defaultkeys: for keyvalue in defaultkeys:
@ -462,7 +462,7 @@ class DeDRM(FileTypePlugin):
except: except:
print("{0} v{1}: Exception when getting default Adobe Key after {2:.1f} seconds".format(PLUGIN_NAME, PLUGIN_VERSION, time.time()-self.starttime)) print("{0} v{1}: Exception when getting default Adobe Key after {2:.1f} seconds".format(PLUGIN_NAME, PLUGIN_VERSION, time.time()-self.starttime))
traceback.print_exc() traceback.print_exc()
self.default_key = u"" self.default_key = ""
newkeys = [] newkeys = []
for keyvalue in defaultkeys: for keyvalue in defaultkeys:
@ -590,7 +590,7 @@ class DeDRM(FileTypePlugin):
dedrmprefs = prefs.DeDRM_Prefs() dedrmprefs = prefs.DeDRM_Prefs()
# Attempt to decrypt epub with each encryption key (generated or provided). # Attempt to decrypt epub with each encryption key (generated or provided).
for keyname, userkey in dedrmprefs['ereaderkeys'].items(): for keyname, userkey in dedrmprefs['ereaderkeys'].items():
keyname_masked = u"".join(("X" if (x.isdigit()) else x) for x in keyname) keyname_masked = "".join(("X" if (x.isdigit()) else x) for x in keyname)
print("{0} v{1}: Trying Encryption key {2:s}".format(PLUGIN_NAME, PLUGIN_VERSION, keyname_masked)) print("{0} v{1}: Trying Encryption key {2:s}".format(PLUGIN_NAME, PLUGIN_VERSION, keyname_masked))
of = self.temporary_file(".pmlz") of = self.temporary_file(".pmlz")

View File

@ -1,12 +1,12 @@
import sys import sys
import Tkinter import tkinter
import Tkconstants import tkinter.constants
class ActivityBar(Tkinter.Frame): class ActivityBar(tkinter.Frame):
def __init__(self, master, length=300, height=20, barwidth=15, interval=50, bg='white', fillcolor='orchid1',\ def __init__(self, master, length=300, height=20, barwidth=15, interval=50, bg='white', fillcolor='orchid1',\
bd=2, relief=Tkconstants.GROOVE, *args, **kw): bd=2, relief=tkinter.constants.GROOVE, *args, **kw):
Tkinter.Frame.__init__(self, master, bg=bg, width=length, height=height, *args, **kw) tkinter.Frame.__init__(self, master, bg=bg, width=length, height=height, *args, **kw)
self._master = master self._master = master
self._interval = interval self._interval = interval
self._maximum = length self._maximum = length
@ -20,7 +20,7 @@ class ActivityBar(Tkinter.Frame):
stopx = self._maximum stopx = self._maximum
# self._canv = Tkinter.Canvas(self, bg=self['bg'], width=self['width'], height=self['height'],\ # self._canv = Tkinter.Canvas(self, bg=self['bg'], width=self['width'], height=self['height'],\
# highlightthickness=0, relief='flat', bd=0) # highlightthickness=0, relief='flat', bd=0)
self._canv = Tkinter.Canvas(self, bg=self['bg'], width=self['width'], height=self['height'],\ self._canv = tkinter.Canvas(self, bg=self['bg'], width=self['width'], height=self['height'],\
highlightthickness=0, relief=relief, bd=bd) highlightthickness=0, relief=relief, bd=bd)
self._canv.pack(fill='both', expand=1) self._canv.pack(fill='both', expand=1)
self._rect = self._canv.create_rectangle(0, 0, self._canv.winfo_reqwidth(), self._canv.winfo_reqheight(), fill=fillcolor, width=0) self._rect = self._canv.create_rectangle(0, 0, self._canv.winfo_reqwidth(), self._canv.winfo_reqheight(), fill=fillcolor, width=0)

View File

@ -496,7 +496,7 @@ def cli_main():
# save to the specified file or directory # save to the specified file or directory
outpath = args[0] outpath = args[0]
if not os.path.isabs(outpath): if not os.path.isabs(outpath):
outpath = os.path.abspath(outpath) outpath = os.path.abspath(outpath)
else: else:
# save to the same directory as the script # save to the same directory as the script
outpath = os.path.dirname(argv[0]) outpath = os.path.dirname(argv[0])

View File

@ -176,8 +176,8 @@ class Rijndael(BlockCipher):
self.blockSize = blockSize # blockSize is in bytes self.blockSize = blockSize # blockSize is in bytes
self.padding = padding # change default to noPadding() to get normal ECB behavior self.padding = padding # change default to noPadding() to get normal ECB behavior
assert( keySize%4==0 and NrTable[4].has_key(keySize/4)),'key size must be 16,20,24,29 or 32 bytes' assert( keySize%4==0 and keySize/4 in NrTable[4]),'key size must be 16,20,24,29 or 32 bytes'
assert( blockSize%4==0 and NrTable.has_key(blockSize/4)), 'block size must be 16,20,24,29 or 32 bytes' assert( blockSize%4==0 and blockSize/4 in NrTable), 'block size must be 16,20,24,29 or 32 bytes'
self.Nb = self.blockSize/4 # Nb is number of columns of 32 bit words self.Nb = self.blockSize/4 # Nb is number of columns of 32 bit words
self.Nk = keySize/4 # Nk is the key length in 32-bit words self.Nk = keySize/4 # Nk is the key length in 32-bit words

View File

@ -324,7 +324,7 @@ def usage(progname):
print("Get backup.ab file using adb backup com.amazon.kindle for Android 4.0+.") print("Get backup.ab file using adb backup com.amazon.kindle for Android 4.0+.")
print("Otherwise extract AmazonSecureStorage.xml from /data/data/com.amazon.kindle/shared_prefs/AmazonSecureStorage.xml") print("Otherwise extract AmazonSecureStorage.xml from /data/data/com.amazon.kindle/shared_prefs/AmazonSecureStorage.xml")
print("Or map_data_storage.db from /data/data/com.amazon.kindle/databases/map_data_storage.db") print("Or map_data_storage.db from /data/data/com.amazon.kindle/databases/map_data_storage.db")
print(u"") print("")
print("Usage:") print("Usage:")
print(" {0:s} [-h] [-b <backup.ab>] [<outfile.k4a>]".format(progname)) print(" {0:s} [-h] [-b <backup.ab>] [<outfile.k4a>]".format(progname))

View File

@ -4,6 +4,7 @@
import sys, os import sys, os
import locale import locale
import codecs import codecs
import importlib
# get sys.argv arguments and encode them into utf-8 # get sys.argv arguments and encode them into utf-8
def unicode_argv(): def unicode_argv():
@ -34,15 +35,13 @@ def unicode_argv():
# Remove Python executable and commands if present # Remove Python executable and commands if present
start = argc.value - len(sys.argv) start = argc.value - len(sys.argv)
return [argv[i] for i in return [argv[i] for i in
xrange(start, argc.value)] range(start, argc.value)]
# if we don't have any arguments at all, just pass back script name # if we don't have any arguments at all, just pass back script name
# this should never happen # this should never happen
return ["DeDRM.py"] return ["DeDRM.py"]
else: else:
argvencoding = sys.stdin.encoding argvencoding = sys.stdin.encoding or "utf-8"
if argvencoding == None: return [arg if isinstance(arg, str) else str(arg, argvencoding) for arg in sys.argv]
argvencoding = "utf-8"
return arg
def add_cp65001_codec(): def add_cp65001_codec():
@ -59,7 +58,7 @@ def set_utf8_default_encoding():
return return
# Regenerate setdefaultencoding. # Regenerate setdefaultencoding.
reload(sys) importlib.reload(sys)
sys.setdefaultencoding('utf-8') sys.setdefaultencoding('utf-8')
for attr in dir(locale): for attr in dir(locale):

View File

@ -17,10 +17,10 @@
# the rights to use, copy, modify, merge, publish, distribute, sublicense, # the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the # and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions: # Software is furnished to do so, subject to the following conditions:
# #
# The above copyright notice and this permission notice shall be included in # The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software. # all copies or substantial portions of the Software.
# #
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
@ -166,15 +166,15 @@ def AskFolder(
def BrowseCallback(hwnd, uMsg, lParam, lpData): def BrowseCallback(hwnd, uMsg, lParam, lpData):
if uMsg == BFFM_INITIALIZED: if uMsg == BFFM_INITIALIZED:
if actionButtonLabel: if actionButtonLabel:
label = actionButtonLabel.decode('utf-8', 'replace') label = str(actionButtonLabel, errors='replace')
user32.SendMessageW(hwnd, BFFM_SETOKTEXT, 0, label) user32.SendMessageW(hwnd, BFFM_SETOKTEXT, 0, label)
if cancelButtonLabel: if cancelButtonLabel:
label = cancelButtonLabel.decode('utf-8', 'replace') label = str(cancelButtonLabel, errors='replace')
cancelButton = user32.GetDlgItem(hwnd, IDCANCEL) cancelButton = user32.GetDlgItem(hwnd, IDCANCEL)
if cancelButton: if cancelButton:
user32.SetWindowTextW(cancelButton, label) user32.SetWindowTextW(cancelButton, label)
if windowTitle: if windowTitle:
title = windowTitle.decode('utf-8', 'replace') title = str(windowTitle, errors='replace')
user32.SetWindowTextW(hwnd, title) user32.SetWindowTextW(hwnd, title)
if defaultLocation: if defaultLocation:
user32.SendMessageW(hwnd, BFFM_SETSELECTIONW, 1, defaultLocation.replace('/', '\\')) user32.SendMessageW(hwnd, BFFM_SETSELECTIONW, 1, defaultLocation.replace('/', '\\'))

View File

@ -153,7 +153,7 @@ class ConfigWidget(QWidget):
# Copy the HTML helpfile to the plugin directory each time the # Copy the HTML helpfile to the plugin directory each time the
# link is clicked in case the helpfile is updated in newer plugins. # link is clicked in case the helpfile is updated in newer plugins.
file_path = os.path.join(config_dir, "plugins", "DeDRM", "help", help_file_name) file_path = os.path.join(config_dir, "plugins", "DeDRM", "help", help_file_name)
with open(file_path,'wb') as f: with open(file_path,'w') as f:
f.write(self.load_resource(help_file_name)) f.write(self.load_resource(help_file_name))
return file_path return file_path
url = 'file:///' + get_help_file_resource() url = 'file:///' + get_help_file_resource()
@ -181,14 +181,14 @@ class ConfigWidget(QWidget):
class ManageKeysDialog(QDialog): class ManageKeysDialog(QDialog):
def __init__(self, parent, key_type_name, plugin_keys, create_key, keyfile_ext = u"", wineprefix = None): def __init__(self, parent, key_type_name, plugin_keys, create_key, keyfile_ext = "", wineprefix = None):
QDialog.__init__(self,parent) QDialog.__init__(self,parent)
self.parent = parent self.parent = parent
self.key_type_name = key_type_name self.key_type_name = key_type_name
self.plugin_keys = plugin_keys self.plugin_keys = plugin_keys
self.create_key = create_key self.create_key = create_key
self.keyfile_ext = keyfile_ext self.keyfile_ext = keyfile_ext
self.import_key = (keyfile_ext != u"") self.import_key = (keyfile_ext != "")
self.binary_file = (keyfile_ext == "der") self.binary_file = (keyfile_ext == "der")
self.json_file = (keyfile_ext == "k4i") self.json_file = (keyfile_ext == "k4i")
self.android_file = (keyfile_ext == "k4a") self.android_file = (keyfile_ext == "k4a")
@ -279,8 +279,8 @@ class ManageKeysDialog(QDialog):
def getwineprefix(self): def getwineprefix(self):
if self.wineprefix is not None: if self.wineprefix is not None:
return self.wp_lineedit.text().strip() return str(self.wp_lineedit.text()).strip()
return u"" return ""
def populate_list(self): def populate_list(self):
if type(self.plugin_keys) == dict: if type(self.plugin_keys) == dict:
@ -300,7 +300,7 @@ class ManageKeysDialog(QDialog):
new_key_value = d.key_value new_key_value = d.key_value
if type(self.plugin_keys) == dict: if type(self.plugin_keys) == dict:
if new_key_value in self.plugin_keys.values(): if new_key_value in self.plugin_keys.values():
old_key_name = [name for name, value in self.plugin_keys.iteritems() if value == new_key_value][0] old_key_name = [name for name, value in self.plugin_keys.items() if value == new_key_value][0]
info_dialog(None, "{0} {1}: Duplicate {2}".format(PLUGIN_NAME, PLUGIN_VERSION,self.key_type_name), info_dialog(None, "{0} {1}: Duplicate {2}".format(PLUGIN_NAME, PLUGIN_VERSION,self.key_type_name),
"The new {1} is the same as the existing {1} named <strong>{0}</strong> and has not been added.".format(old_key_name,self.key_type_name), show=True) "The new {1} is the same as the existing {1} named <strong>{0}</strong> and has not been added.".format(old_key_name,self.key_type_name), show=True)
return return
@ -328,7 +328,7 @@ class ManageKeysDialog(QDialog):
if d.result() != d.Accepted: if d.result() != d.Accepted:
# rename cancelled or moot. # rename cancelled or moot.
return return
keyname = self.listy.currentItem().text() keyname = str(self.listy.currentItem().text())
if not question_dialog(self, "{0} {1}: Confirm Rename".format(PLUGIN_NAME, PLUGIN_VERSION), "Do you really want to rename the {2} named <strong>{0}</strong> to <strong>{1}</strong>?".format(keyname,d.key_name,self.key_type_name), show_copy_button=False, default_yes=False): if not question_dialog(self, "{0} {1}: Confirm Rename".format(PLUGIN_NAME, PLUGIN_VERSION), "Do you really want to rename the {2} named <strong>{0}</strong> to <strong>{1}</strong>?".format(keyname,d.key_name,self.key_type_name), show_copy_button=False, default_yes=False):
return return
self.plugin_keys[d.key_name] = self.plugin_keys[keyname] self.plugin_keys[d.key_name] = self.plugin_keys[keyname]
@ -340,7 +340,7 @@ class ManageKeysDialog(QDialog):
def delete_key(self): def delete_key(self):
if not self.listy.currentItem(): if not self.listy.currentItem():
return return
keyname = self.listy.currentItem().text() keyname = str(self.listy.currentItem().text())
if not question_dialog(self, "{0} {1}: Confirm Delete".format(PLUGIN_NAME, PLUGIN_VERSION), "Do you really want to delete the {1} <strong>{0}</strong>?".format(keyname, self.key_type_name), show_copy_button=False, default_yes=False): if not question_dialog(self, "{0} {1}: Confirm Delete".format(PLUGIN_NAME, PLUGIN_VERSION), "Do you really want to delete the {1} <strong>{0}</strong>?".format(keyname, self.key_type_name), show_copy_button=False, default_yes=False):
return return
if type(self.plugin_keys) == dict: if type(self.plugin_keys) == dict:
@ -357,7 +357,7 @@ class ManageKeysDialog(QDialog):
# link is clicked in case the helpfile is updated in newer plugins. # link is clicked in case the helpfile is updated in newer plugins.
help_file_name = "{0}_{1}_Help.htm".format(PLUGIN_NAME, self.key_type_name) help_file_name = "{0}_{1}_Help.htm".format(PLUGIN_NAME, self.key_type_name)
file_path = os.path.join(config_dir, "plugins", "DeDRM", "help", help_file_name) file_path = os.path.join(config_dir, "plugins", "DeDRM", "help", help_file_name)
with open(file_path,'wb') as f: with open(file_path,'w') as f:
f.write(self.parent.load_resource(help_file_name)) f.write(self.parent.load_resource(help_file_name))
return file_path return file_path
url = 'file:///' + get_help_file_resource() url = 'file:///' + get_help_file_resource()
@ -378,7 +378,7 @@ class ManageKeysDialog(QDialog):
with open(fpath,'rb') as keyfile: with open(fpath,'rb') as keyfile:
new_key_value = keyfile.read() new_key_value = keyfile.read()
if self.binary_file: if self.binary_file:
new_key_value = new_key_value.hex() new_key_value = new_key_value.encode('hex')
elif self.json_file: elif self.json_file:
new_key_value = json.loads(new_key_value) new_key_value = json.loads(new_key_value)
elif self.android_file: elif self.android_file:
@ -395,7 +395,7 @@ class ManageKeysDialog(QDialog):
break break
if not match: if not match:
if new_key_value in self.plugin_keys.values(): if new_key_value in self.plugin_keys.values():
old_key_name = [name for name, value in self.plugin_keys.iteritems() if value == new_key_value][0] old_key_name = [name for name, value in self.plugin_keys.items() if value == new_key_value][0]
skipped += 1 skipped += 1
info_dialog(None, "{0} {1}".format(PLUGIN_NAME, PLUGIN_VERSION), info_dialog(None, "{0} {1}".format(PLUGIN_NAME, PLUGIN_VERSION),
"The key in file {0} is the same as the existing key <strong>{1}</strong> and has been skipped.".format(filename,old_key_name), show_copy_button=False, show=True) "The key in file {0} is the same as the existing key <strong>{1}</strong> and has been skipped.".format(filename,old_key_name), show_copy_button=False, show=True)
@ -403,7 +403,7 @@ class ManageKeysDialog(QDialog):
counter += 1 counter += 1
self.plugin_keys[new_key_name] = new_key_value self.plugin_keys[new_key_name] = new_key_value
msg = u"" msg = ""
if counter+skipped > 1: if counter+skipped > 1:
if counter > 0: if counter > 0:
msg += "Imported <strong>{0:d}</strong> key {1}. ".format(counter, "file" if counter == 1 else "files") msg += "Imported <strong>{0:d}</strong> key {1}. ".format(counter, "file" if counter == 1 else "files")
@ -424,22 +424,22 @@ class ManageKeysDialog(QDialog):
r = error_dialog(None, "{0} {1}".format(PLUGIN_NAME, PLUGIN_VERSION), r = error_dialog(None, "{0} {1}".format(PLUGIN_NAME, PLUGIN_VERSION),
_(errmsg), show=True, show_copy_button=False) _(errmsg), show=True, show_copy_button=False)
return return
keyname = self.listy.currentItem().text() keyname = str(self.listy.currentItem().text())
unique_dlg_name = PLUGIN_NAME + "export {0} keys".format(self.key_type_name).replace(' ', '_') #takes care of automatically remembering last directory unique_dlg_name = PLUGIN_NAME + "export {0} keys".format(self.key_type_name).replace(' ', '_') #takes care of automatically remembering last directory
caption = "Save {0} File as...".format(self.key_type_name) caption = "Save {0} File as...".format(self.key_type_name)
filters = [("{0} Files".format(self.key_type_name), ["{0}".format(self.keyfile_ext)])] filters = [("{0} Files".format(self.key_type_name), ["{0}".format(self.keyfile_ext)])]
defaultname = "{0}.{1}".format(keyname, self.keyfile_ext) defaultname = "{0}.{1}".format(keyname, self.keyfile_ext)
filename = choose_save_file(self, unique_dlg_name, caption, filters, all_files=False, initial_filename=defaultname) filename = choose_save_file(self, unique_dlg_name, caption, filters, all_files=False, initial_filename=defaultname)
if filename: if filename:
with open(filename, 'wb') as fname: with file(filename, 'wb') as fname:
if self.binary_file: if self.binary_file:
fname.write(self.plugin_keys[keyname].decode('hex')) fname.write(self.plugin_keys[keyname].decode('hex'))
elif self.json_file: elif self.json_file:
fname.write(json.dumps(self.plugin_keys[keyname]).encode()) fname.write(json.dumps(self.plugin_keys[keyname]))
elif self.android_file: elif self.android_file:
for key in self.plugin_keys[keyname]: for key in self.plugin_keys[keyname]:
fname.write(key.encode()) fname.write(key)
fname.write(b"\n") fname.write("\n")
else: else:
fname.write(self.plugin_keys[keyname]) fname.write(self.plugin_keys[keyname])
@ -475,7 +475,7 @@ class RenameKeyDialog(QDialog):
self.resize(self.sizeHint()) self.resize(self.sizeHint())
def accept(self): def accept(self):
if not self.key_ledit.text() or self.key_ledit.text().isspace(): if not str(self.key_ledit.text()) or str(self.key_ledit.text()).isspace():
errmsg = "Key name field cannot be empty!" errmsg = "Key name field cannot be empty!"
return error_dialog(None, "{0} {1}".format(PLUGIN_NAME, PLUGIN_VERSION), return error_dialog(None, "{0} {1}".format(PLUGIN_NAME, PLUGIN_VERSION),
_(errmsg), show=True, show_copy_button=False) _(errmsg), show=True, show_copy_button=False)
@ -496,7 +496,7 @@ class RenameKeyDialog(QDialog):
@property @property
def key_name(self): def key_name(self):
return self.key_ledit.text().strip() return str(self.key_ledit.text()).strip()
@ -513,7 +513,7 @@ class AddBandNKeyDialog(QDialog):
layout = QVBoxLayout(self) layout = QVBoxLayout(self)
self.setLayout(layout) self.setLayout(layout)
data_group_box = QGroupBox(u"", self) data_group_box = QGroupBox("", self)
layout.addWidget(data_group_box) layout.addWidget(data_group_box)
data_group_box_layout = QVBoxLayout() data_group_box_layout = QVBoxLayout()
data_group_box.setLayout(data_group_box_layout) data_group_box.setLayout(data_group_box_layout)
@ -530,7 +530,7 @@ class AddBandNKeyDialog(QDialog):
name_group = QHBoxLayout() name_group = QHBoxLayout()
data_group_box_layout.addLayout(name_group) data_group_box_layout.addLayout(name_group)
name_group.addWidget(QLabel("B&N/nook account email address:", self)) name_group.addWidget(QLabel("B&N/nook account email address:", self))
self.name_ledit = QLineEdit(u"", self) self.name_ledit = QLineEdit("", self)
self.name_ledit.setToolTip(_("<p>Enter your email address as it appears in your B&N " + self.name_ledit.setToolTip(_("<p>Enter your email address as it appears in your B&N " +
"account.</p>" + "account.</p>" +
"<p>It will only be used to generate this " + "<p>It will only be used to generate this " +
@ -545,7 +545,7 @@ class AddBandNKeyDialog(QDialog):
ccn_group = QHBoxLayout() ccn_group = QHBoxLayout()
data_group_box_layout.addLayout(ccn_group) data_group_box_layout.addLayout(ccn_group)
ccn_group.addWidget(QLabel("B&N/nook account password:", self)) ccn_group.addWidget(QLabel("B&N/nook account password:", self))
self.cc_ledit = QLineEdit(u"", self) self.cc_ledit = QLineEdit("", self)
self.cc_ledit.setToolTip(_("<p>Enter the password " + self.cc_ledit.setToolTip(_("<p>Enter the password " +
"for your B&N account.</p>" + "for your B&N account.</p>" +
"<p>The password will only be used to generate this " + "<p>The password will only be used to generate this " +
@ -560,7 +560,7 @@ class AddBandNKeyDialog(QDialog):
key_group = QHBoxLayout() key_group = QHBoxLayout()
data_group_box_layout.addLayout(key_group) data_group_box_layout.addLayout(key_group)
key_group.addWidget(QLabel("Retrieved key:", self)) key_group.addWidget(QLabel("Retrieved key:", self))
self.key_display = QLabel(u"", self) self.key_display = QLabel("", self)
self.key_display.setToolTip(_("Click the Retrieve Key button to fetch your B&N encryption key from the B&N servers")) self.key_display.setToolTip(_("Click the Retrieve Key button to fetch your B&N encryption key from the B&N servers"))
key_group.addWidget(self.key_display) key_group.addWidget(self.key_display)
self.retrieve_button = QtGui.QPushButton(self) self.retrieve_button = QtGui.QPushButton(self)
@ -579,19 +579,19 @@ class AddBandNKeyDialog(QDialog):
@property @property
def key_name(self): def key_name(self):
return self.key_ledit.text().strip() return str(self.key_ledit.text()).strip()
@property @property
def key_value(self): def key_value(self):
return self.key_display.text().strip() return str(self.key_display.text()).strip()
@property @property
def user_name(self): def user_name(self):
return self.name_ledit.text().strip().lower().replace(' ','') return str(self.name_ledit.text()).strip().lower().replace(' ','')
@property @property
def cc_number(self): def cc_number(self):
return self.cc_ledit.text().strip() return str(self.cc_ledit.text()).strip()
def retrieve_key(self): def retrieve_key(self):
from calibre_plugins.dedrm.ignoblekeyfetch import fetch_key as fetch_bandn_key from calibre_plugins.dedrm.ignoblekeyfetch import fetch_key as fetch_bandn_key
@ -623,7 +623,7 @@ class AddEReaderDialog(QDialog):
layout = QVBoxLayout(self) layout = QVBoxLayout(self)
self.setLayout(layout) self.setLayout(layout)
data_group_box = QGroupBox(u"", self) data_group_box = QGroupBox("", self)
layout.addWidget(data_group_box) layout.addWidget(data_group_box)
data_group_box_layout = QVBoxLayout() data_group_box_layout = QVBoxLayout()
data_group_box.setLayout(data_group_box_layout) data_group_box.setLayout(data_group_box_layout)
@ -638,7 +638,7 @@ class AddEReaderDialog(QDialog):
name_group = QHBoxLayout() name_group = QHBoxLayout()
data_group_box_layout.addLayout(name_group) data_group_box_layout.addLayout(name_group)
name_group.addWidget(QLabel("Your Name:", self)) name_group.addWidget(QLabel("Your Name:", self))
self.name_ledit = QLineEdit(u"", self) self.name_ledit = QLineEdit("", self)
self.name_ledit.setToolTip("Enter the name for this eReader key, usually the name on your credit card.\nIt will only be used to generate this one-time key and won\'t be stored anywhere in calibre or on your computer.\n(ex: Mr Jonathan Q Smith)") self.name_ledit.setToolTip("Enter the name for this eReader key, usually the name on your credit card.\nIt will only be used to generate this one-time key and won\'t be stored anywhere in calibre or on your computer.\n(ex: Mr Jonathan Q Smith)")
name_group.addWidget(self.name_ledit) name_group.addWidget(self.name_ledit)
name_disclaimer_label = QLabel(_("(Will not be saved in configuration data)"), self) name_disclaimer_label = QLabel(_("(Will not be saved in configuration data)"), self)
@ -648,7 +648,7 @@ class AddEReaderDialog(QDialog):
ccn_group = QHBoxLayout() ccn_group = QHBoxLayout()
data_group_box_layout.addLayout(ccn_group) data_group_box_layout.addLayout(ccn_group)
ccn_group.addWidget(QLabel("Credit Card#:", self)) ccn_group.addWidget(QLabel("Credit Card#:", self))
self.cc_ledit = QLineEdit(u"", self) self.cc_ledit = QLineEdit("", self)
self.cc_ledit.setToolTip("<p>Enter the last 8 digits of credit card number for this eReader key.\nThey will only be used to generate this one-time key and won\'t be stored anywhere in calibre or on your computer.") self.cc_ledit.setToolTip("<p>Enter the last 8 digits of credit card number for this eReader key.\nThey will only be used to generate this one-time key and won\'t be stored anywhere in calibre or on your computer.")
ccn_group.addWidget(self.cc_ledit) ccn_group.addWidget(self.cc_ledit)
ccn_disclaimer_label = QLabel(_('(Will not be saved in configuration data)'), self) ccn_disclaimer_label = QLabel(_('(Will not be saved in configuration data)'), self)
@ -665,7 +665,7 @@ class AddEReaderDialog(QDialog):
@property @property
def key_name(self): def key_name(self):
return self.key_ledit.text().strip() return str(self.key_ledit.text()).strip()
@property @property
def key_value(self): def key_value(self):
@ -674,11 +674,11 @@ class AddEReaderDialog(QDialog):
@property @property
def user_name(self): def user_name(self):
return self.name_ledit.text().strip().lower().replace(' ','') return str(self.name_ledit.text()).strip().lower().replace(' ','')
@property @property
def cc_number(self): def cc_number(self):
return self.cc_ledit.text().strip().replace(' ', '').replace('-','') return str(self.cc_ledit.text()).strip().replace(' ', '').replace('-','')
def accept(self): def accept(self):
@ -708,7 +708,7 @@ class AddAdeptDialog(QDialog):
defaultkeys = adeptkeys() defaultkeys = adeptkeys()
else: # linux else: # linux
from wineutils import WineGetKeys from .wineutils import WineGetKeys
scriptpath = os.path.join(parent.parent.alfdir,"adobekey.py") scriptpath = os.path.join(parent.parent.alfdir,"adobekey.py")
defaultkeys = WineGetKeys(scriptpath, ".der",parent.getwineprefix()) defaultkeys = WineGetKeys(scriptpath, ".der",parent.getwineprefix())
@ -716,12 +716,12 @@ class AddAdeptDialog(QDialog):
self.default_key = defaultkeys[0] self.default_key = defaultkeys[0]
except: except:
traceback.print_exc() traceback.print_exc()
self.default_key = u"" self.default_key = ""
self.button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) self.button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
if len(self.default_key)>0: if len(self.default_key)>0:
data_group_box = QGroupBox(u"", self) data_group_box = QGroupBox("", self)
layout.addWidget(data_group_box) layout.addWidget(data_group_box)
data_group_box_layout = QVBoxLayout() data_group_box_layout = QVBoxLayout()
data_group_box.setLayout(data_group_box_layout) data_group_box.setLayout(data_group_box_layout)
@ -748,7 +748,7 @@ class AddAdeptDialog(QDialog):
@property @property
def key_name(self): def key_name(self):
return self.key_ledit.text().strip() return str(self.key_ledit.text()).strip()
@property @property
def key_value(self): def key_value(self):
@ -779,7 +779,7 @@ class AddKindleDialog(QDialog):
defaultkeys = kindlekeys() defaultkeys = kindlekeys()
else: # linux else: # linux
from wineutils import WineGetKeys from .wineutils import WineGetKeys
scriptpath = os.path.join(parent.parent.alfdir,"kindlekey.py") scriptpath = os.path.join(parent.parent.alfdir,"kindlekey.py")
defaultkeys = WineGetKeys(scriptpath, ".k4i",parent.getwineprefix()) defaultkeys = WineGetKeys(scriptpath, ".k4i",parent.getwineprefix())
@ -787,12 +787,12 @@ class AddKindleDialog(QDialog):
self.default_key = defaultkeys[0] self.default_key = defaultkeys[0]
except: except:
traceback.print_exc() traceback.print_exc()
self.default_key = u"" self.default_key = ""
self.button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) self.button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
if len(self.default_key)>0: if len(self.default_key)>0:
data_group_box = QGroupBox(u"", self) data_group_box = QGroupBox("", self)
layout.addWidget(data_group_box) layout.addWidget(data_group_box)
data_group_box_layout = QVBoxLayout() data_group_box_layout = QVBoxLayout()
data_group_box.setLayout(data_group_box_layout) data_group_box.setLayout(data_group_box_layout)
@ -820,7 +820,7 @@ class AddKindleDialog(QDialog):
@property @property
def key_name(self): def key_name(self):
return self.key_ledit.text().strip() return str(self.key_ledit.text()).strip()
@property @property
def key_value(self): def key_value(self):
@ -845,7 +845,7 @@ class AddSerialDialog(QDialog):
layout = QVBoxLayout(self) layout = QVBoxLayout(self)
self.setLayout(layout) self.setLayout(layout)
data_group_box = QGroupBox(u"", self) data_group_box = QGroupBox("", self)
layout.addWidget(data_group_box) layout.addWidget(data_group_box)
data_group_box_layout = QVBoxLayout() data_group_box_layout = QVBoxLayout()
data_group_box.setLayout(data_group_box_layout) data_group_box.setLayout(data_group_box_layout)
@ -866,11 +866,11 @@ class AddSerialDialog(QDialog):
@property @property
def key_name(self): def key_name(self):
return self.key_ledit.text().strip() return str(self.key_ledit.text()).strip()
@property @property
def key_value(self): def key_value(self):
return self.key_ledit.text().replace(' ', '') return str(self.key_ledit.text()).replace(' ', '')
def accept(self): def accept(self):
if len(self.key_name) == 0 or self.key_name.isspace(): if len(self.key_name) == 0 or self.key_name.isspace():
@ -892,7 +892,7 @@ class AddAndroidDialog(QDialog):
self.setLayout(layout) self.setLayout(layout)
self.button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) self.button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
data_group_box = QGroupBox(u"", self) data_group_box = QGroupBox("", self)
layout.addWidget(data_group_box) layout.addWidget(data_group_box)
data_group_box_layout = QVBoxLayout() data_group_box_layout = QVBoxLayout()
data_group_box.setLayout(data_group_box_layout) data_group_box.setLayout(data_group_box_layout)
@ -903,14 +903,14 @@ class AddAndroidDialog(QDialog):
add_btn.setToolTip("Import Kindle for Android backup file.") add_btn.setToolTip("Import Kindle for Android backup file.")
add_btn.clicked.connect(self.get_android_file) add_btn.clicked.connect(self.get_android_file)
file_group.addWidget(add_btn) file_group.addWidget(add_btn)
self.selected_file_name = QLabel(u"",self) self.selected_file_name = QLabel("",self)
self.selected_file_name.setAlignment(Qt.AlignHCenter) self.selected_file_name.setAlignment(Qt.AlignHCenter)
file_group.addWidget(self.selected_file_name) file_group.addWidget(self.selected_file_name)
key_group = QHBoxLayout() key_group = QHBoxLayout()
data_group_box_layout.addLayout(key_group) data_group_box_layout.addLayout(key_group)
key_group.addWidget(QLabel("Unique Key Name:", self)) key_group.addWidget(QLabel("Unique Key Name:", self))
self.key_ledit = QLineEdit(u"", self) self.key_ledit = QLineEdit("", self)
self.key_ledit.setToolTip("<p>Enter an identifying name for the Android for Kindle key.") self.key_ledit.setToolTip("<p>Enter an identifying name for the Android for Kindle key.")
key_group.addWidget(self.key_ledit) key_group.addWidget(self.key_ledit)
#key_label = QLabel(_(''), self) #key_label = QLabel(_(''), self)
@ -924,11 +924,11 @@ class AddAndroidDialog(QDialog):
@property @property
def key_name(self): def key_name(self):
return self.key_ledit.text().strip() return str(self.key_ledit.text()).strip()
@property @property
def file_name(self): def file_name(self):
return self.selected_file_name.text().strip() return str(self.selected_file_name.text()).strip()
@property @property
def key_value(self): def key_value(self):
@ -940,7 +940,7 @@ class AddAndroidDialog(QDialog):
filters = [("Kindle for Android backup files", ['db','ab','xml'])] filters = [("Kindle for Android backup files", ['db','ab','xml'])]
files = choose_files(self, unique_dlg_name, caption, filters, all_files=False) files = choose_files(self, unique_dlg_name, caption, filters, all_files=False)
self.serials_from_file = [] self.serials_from_file = []
file_name = u"" file_name = ""
if files: if files:
# find the first selected file that yields some serial numbers # find the first selected file that yields some serial numbers
for filename in files: for filename in files:
@ -973,7 +973,7 @@ class AddPIDDialog(QDialog):
layout = QVBoxLayout(self) layout = QVBoxLayout(self)
self.setLayout(layout) self.setLayout(layout)
data_group_box = QGroupBox(u"", self) data_group_box = QGroupBox("", self)
layout.addWidget(data_group_box) layout.addWidget(data_group_box)
data_group_box_layout = QVBoxLayout() data_group_box_layout = QVBoxLayout()
data_group_box.setLayout(data_group_box_layout) data_group_box.setLayout(data_group_box_layout)
@ -994,11 +994,11 @@ class AddPIDDialog(QDialog):
@property @property
def key_name(self): def key_name(self):
return self.key_ledit.text().strip() return str(self.key_ledit.text()).strip()
@property @property
def key_value(self): def key_value(self):
return self.key_ledit.text().strip() return str(self.key_ledit.text()).strip()
def accept(self): def accept(self):
if len(self.key_name) == 0 or self.key_name.isspace(): if len(self.key_name) == 0 or self.key_name.isspace():

View File

@ -66,10 +66,11 @@ class SafeUnbuffered:
if self.encoding == None: if self.encoding == None:
self.encoding = "utf-8" self.encoding = "utf-8"
def write(self, data): def write(self, data):
if isinstance(data,unicode): if isinstance(data, str):
data = data.encode(self.encoding,"replace") data = data.encode(self.encoding,"replace")
self.stream.write(data) self.stream.buffer.write(data)
self.stream.flush() self.stream.buffer.flush()
def __getattr__(self, attr): def __getattr__(self, attr):
return getattr(self.stream, attr) return getattr(self.stream, attr)
@ -107,15 +108,13 @@ def unicode_argv():
# Remove Python executable and commands if present # Remove Python executable and commands if present
start = argc.value - len(sys.argv) start = argc.value - len(sys.argv)
return [argv[i] for i in return [argv[i] for i in
xrange(start, argc.value)] range(start, argc.value)]
# if we don't have any arguments at all, just pass back script name # if we don't have any arguments at all, just pass back script name
# this should never happen # this should never happen
return ["epubtest.py"] return ["epubtest.py"]
else: else:
argvencoding = sys.stdin.encoding argvencoding = sys.stdin.encoding or "utf-8"
if argvencoding == None: return [arg if isinstance(arg, str) else str(arg, argvencoding) for arg in sys.argv]
argvencoding = "utf-8"
return arg
_FILENAME_LEN_OFFSET = 26 _FILENAME_LEN_OFFSET = 26
_EXTRA_LEN_OFFSET = 28 _EXTRA_LEN_OFFSET = 28

View File

@ -2,7 +2,6 @@
# vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab # vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab
# For use with Topaz Scripts Version 2.6 # For use with Topaz Scripts Version 2.6
from __future__ import print_function
import sys import sys
import csv import csv
import os import os
@ -95,7 +94,7 @@ class DocParser(object):
# change the origin to minx, miny and calc max height and width # change the origin to minx, miny and calc max height and width
maxw = maxws[0] + xs[0] - minx maxw = maxws[0] + xs[0] - minx
maxh = maxhs[0] + ys[0] - miny maxh = maxhs[0] + ys[0] - miny
for j in xrange(0, len(xs)): for j in range(0, len(xs)):
xs[j] = xs[j] - minx xs[j] = xs[j] - minx
ys[j] = ys[j] - miny ys[j] = ys[j] - miny
maxw = max( maxw, (maxws[j] + xs[j]) ) maxw = max( maxw, (maxws[j] + xs[j]) )
@ -107,10 +106,10 @@ class DocParser(object):
ifile.write('<!DOCTYPE svg PUBLIC "-//W3C/DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n') ifile.write('<!DOCTYPE svg PUBLIC "-//W3C/DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n')
ifile.write('<svg width="%dpx" height="%dpx" viewBox="0 0 %d %d" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1">\n' % (math.floor(maxw/10), math.floor(maxh/10), maxw, maxh)) ifile.write('<svg width="%dpx" height="%dpx" viewBox="0 0 %d %d" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1">\n' % (math.floor(maxw/10), math.floor(maxh/10), maxw, maxh))
ifile.write('<defs>\n') ifile.write('<defs>\n')
for j in xrange(0,len(gdefs)): for j in range(0,len(gdefs)):
ifile.write(gdefs[j]) ifile.write(gdefs[j])
ifile.write('</defs>\n') ifile.write('</defs>\n')
for j in xrange(0,len(gids)): for j in range(0,len(gids)):
ifile.write('<use xlink:href="#gl%d" x="%d" y="%d" />\n' % (gids[j], xs[j], ys[j])) ifile.write('<use xlink:href="#gl%d" x="%d" y="%d" />\n' % (gids[j], xs[j], ys[j]))
ifile.write('</svg>') ifile.write('</svg>')
ifile.close() ifile.close()
@ -139,7 +138,7 @@ class DocParser(object):
else: else:
end = min(self.docSize, end) end = min(self.docSize, end)
foundat = -1 foundat = -1
for j in xrange(pos, end): for j in range(pos, end):
item = self.docList[j] item = self.docList[j]
if item.find('=') >= 0: if item.find('=') >= 0:
(name, argres) = item.split('=',1) (name, argres) = item.split('=',1)
@ -300,7 +299,7 @@ class DocParser(object):
if not makeImage : if not makeImage :
# standard all word paragraph # standard all word paragraph
for wordnum in xrange(first, last): for wordnum in range(first, last):
result.append(('ocr', wordnum)) result.append(('ocr', wordnum))
return pclass, result return pclass, result
@ -320,17 +319,17 @@ class DocParser(object):
# by reverting to text based paragraph # by reverting to text based paragraph
if firstGlyph >= lastGlyph: if firstGlyph >= lastGlyph:
# revert to standard text based paragraph # revert to standard text based paragraph
for wordnum in xrange(first, last): for wordnum in range(first, last):
result.append(('ocr', wordnum)) result.append(('ocr', wordnum))
return pclass, result return pclass, result
for glyphnum in xrange(firstGlyph, lastGlyph): for glyphnum in range(firstGlyph, lastGlyph):
glyphList.append(glyphnum) glyphList.append(glyphnum)
# include any extratokens if they exist # include any extratokens if they exist
(pos, sfg) = self.findinDoc('extratokens.firstGlyph',start,end) (pos, sfg) = self.findinDoc('extratokens.firstGlyph',start,end)
(pos, slg) = self.findinDoc('extratokens.lastGlyph',start,end) (pos, slg) = self.findinDoc('extratokens.lastGlyph',start,end)
if (sfg != None) and (slg != None): if (sfg != None) and (slg != None):
for glyphnum in xrange(int(sfg), int(slg)): for glyphnum in range(int(sfg), int(slg)):
glyphList.append(glyphnum) glyphList.append(glyphnum)
num = self.svgcount num = self.svgcount
self.glyphs_to_image(glyphList) self.glyphs_to_image(glyphList)
@ -405,14 +404,14 @@ class DocParser(object):
result.append(('img' + word_class, int(argres))) result.append(('img' + word_class, int(argres)))
if (sp_first != -1) and (sp_last != -1): if (sp_first != -1) and (sp_last != -1):
for wordnum in xrange(sp_first, sp_last): for wordnum in range(sp_first, sp_last):
result.append(('ocr', wordnum)) result.append(('ocr', wordnum))
sp_first = -1 sp_first = -1
sp_last = -1 sp_last = -1
if (gl_first != -1) and (gl_last != -1): if (gl_first != -1) and (gl_last != -1):
glyphList = [] glyphList = []
for glyphnum in xrange(gl_first, gl_last): for glyphnum in range(gl_first, gl_last):
glyphList.append(glyphnum) glyphList.append(glyphnum)
num = self.svgcount num = self.svgcount
self.glyphs_to_image(glyphList) self.glyphs_to_image(glyphList)
@ -422,7 +421,7 @@ class DocParser(object):
gl_last = -1 gl_last = -1
if (ws_first != -1) and (ws_last != -1): if (ws_first != -1) and (ws_last != -1):
for wordnum in xrange(ws_first, ws_last): for wordnum in range(ws_first, ws_last):
result.append(('ocr', wordnum)) result.append(('ocr', wordnum))
ws_first = -1 ws_first = -1
ws_last = -1 ws_last = -1
@ -454,7 +453,7 @@ class DocParser(object):
cnt = len(pdesc) cnt = len(pdesc)
for j in xrange( 0, cnt) : for j in range( 0, cnt) :
(wtype, num) = pdesc[j] (wtype, num) = pdesc[j]
@ -541,7 +540,7 @@ class DocParser(object):
lstart = 0 lstart = 0
cnt = len(pdesc) cnt = len(pdesc)
for j in xrange( 0, cnt) : for j in range( 0, cnt) :
(wtype, num) = pdesc[j] (wtype, num) = pdesc[j]
@ -654,7 +653,7 @@ class DocParser(object):
# process each region on the page and convert what you can to html # process each region on the page and convert what you can to html
for j in xrange(regcnt): for j in range(regcnt):
(etype, start) = pageDesc[j] (etype, start) = pageDesc[j]
(ntype, end) = pageDesc[j+1] (ntype, end) = pageDesc[j+1]

View File

@ -73,7 +73,7 @@ class PParser(object):
else: else:
end = min(self.docSize, end) end = min(self.docSize, end)
foundat = -1 foundat = -1
for j in xrange(pos, end): for j in range(pos, end):
item = self.flatdoc[j] item = self.flatdoc[j]
if item.find('=') >= 0: if item.find('=') >= 0:
(name, argres) = item.split('=',1) (name, argres) = item.split('=',1)
@ -101,7 +101,7 @@ class PParser(object):
def getData(self, path): def getData(self, path):
result = None result = None
cnt = len(self.flatdoc) cnt = len(self.flatdoc)
for j in xrange(cnt): for j in range(cnt):
item = self.flatdoc[j] item = self.flatdoc[j]
if item.find('=') >= 0: if item.find('=') >= 0:
(name, argt) = item.split('=') (name, argt) = item.split('=')
@ -113,7 +113,7 @@ class PParser(object):
result = argres result = argres
break break
if (len(argres) > 0) : if (len(argres) > 0) :
for j in xrange(0,len(argres)): for j in range(0,len(argres)):
argres[j] = int(argres[j]) argres[j] = int(argres[j])
return result return result
@ -127,7 +127,7 @@ class PParser(object):
name = item name = item
argres = [] argres = []
if (len(argres) > 0) : if (len(argres) > 0) :
for j in xrange(0,len(argres)): for j in range(0,len(argres)):
argres[j] = int(argres[j]) argres[j] = int(argres[j])
if (name.endswith(path)): if (name.endswith(path)):
result = argres result = argres
@ -136,7 +136,7 @@ class PParser(object):
def getDataTemp(self, path): def getDataTemp(self, path):
result = None result = None
cnt = len(self.temp) cnt = len(self.temp)
for j in xrange(cnt): for j in range(cnt):
item = self.temp[j] item = self.temp[j]
if item.find('=') >= 0: if item.find('=') >= 0:
(name, argt) = item.split('=') (name, argt) = item.split('=')
@ -149,7 +149,7 @@ class PParser(object):
self.temp.pop(j) self.temp.pop(j)
break break
if (len(argres) > 0) : if (len(argres) > 0) :
for j in xrange(0,len(argres)): for j in range(0,len(argres)):
argres[j] = int(argres[j]) argres[j] = int(argres[j])
return result return result
@ -220,15 +220,15 @@ def convert2SVG(gdict, flat_xml, pageid, previd, nextid, svgDir, raw, meta_array
if (pp.gid != None): if (pp.gid != None):
mlst.append('<defs>\n') mlst.append('<defs>\n')
gdefs = pp.getGlyphs() gdefs = pp.getGlyphs()
for j in xrange(0,len(gdefs)): for j in range(0,len(gdefs)):
mlst.append(gdefs[j]) mlst.append(gdefs[j])
mlst.append('</defs>\n') mlst.append('</defs>\n')
img = pp.getImages() img = pp.getImages()
if (img != None): if (img != None):
for j in xrange(0,len(img)): for j in range(0,len(img)):
mlst.append(img[j]) mlst.append(img[j])
if (pp.gid != None): if (pp.gid != None):
for j in xrange(0,len(pp.gid)): for j in range(0,len(pp.gid)):
mlst.append('<use xlink:href="#gl%d" x="%d" y="%d" />\n' % (pp.gid[j], pp.gx[j], pp.gy[j])) mlst.append('<use xlink:href="#gl%d" x="%d" y="%d" />\n' % (pp.gid[j], pp.gx[j], pp.gy[j]))
if (img == None or len(img) == 0) and (pp.gid == None or len(pp.gid) == 0): if (img == None or len(img) == 0) and (pp.gid == None or len(pp.gid) == 0):
xpos = "%d" % (pp.pw // 3) xpos = "%d" % (pp.pw // 3)

View File

@ -44,10 +44,10 @@ if inCalibre :
from calibre_plugins.dedrm import flatxml2svg from calibre_plugins.dedrm import flatxml2svg
from calibre_plugins.dedrm import stylexml2css from calibre_plugins.dedrm import stylexml2css
else : else :
import convert2xml from . import convert2xml
import flatxml2html from . import flatxml2html
import flatxml2svg from . import flatxml2svg
import stylexml2css from . import stylexml2css
# global switch # global switch
buildXML = False buildXML = False

View File

@ -322,79 +322,79 @@ def cli_main():
def gui_main(): def gui_main():
try: try:
import Tkinter import tkinter
import Tkconstants import tkinter.constants
import tkFileDialog import tkinter.filedialog
import tkMessageBox import tkinter.messagebox
import traceback import traceback
except: except:
return cli_main() return cli_main()
class DecryptionDialog(Tkinter.Frame): class DecryptionDialog(tkinter.Frame):
def __init__(self, root): def __init__(self, root):
Tkinter.Frame.__init__(self, root, border=5) tkinter.Frame.__init__(self, root, border=5)
self.status = Tkinter.Label(self, text="Select files for decryption") self.status = tkinter.Label(self, text="Select files for decryption")
self.status.pack(fill=Tkconstants.X, expand=1) self.status.pack(fill=tkinter.constants.X, expand=1)
body = Tkinter.Frame(self) body = tkinter.Frame(self)
body.pack(fill=Tkconstants.X, expand=1) body.pack(fill=tkinter.constants.X, expand=1)
sticky = Tkconstants.E + Tkconstants.W sticky = tkinter.constants.E + tkinter.constants.W
body.grid_columnconfigure(1, weight=2) body.grid_columnconfigure(1, weight=2)
Tkinter.Label(body, text="Key file").grid(row=0) tkinter.Label(body, text="Key file").grid(row=0)
self.keypath = Tkinter.Entry(body, width=30) self.keypath = tkinter.Entry(body, width=30)
self.keypath.grid(row=0, column=1, sticky=sticky) self.keypath.grid(row=0, column=1, sticky=sticky)
if os.path.exists("bnepubkey.b64"): if os.path.exists("bnepubkey.b64"):
self.keypath.insert(0, "bnepubkey.b64") self.keypath.insert(0, "bnepubkey.b64")
button = Tkinter.Button(body, text="...", command=self.get_keypath) button = tkinter.Button(body, text="...", command=self.get_keypath)
button.grid(row=0, column=2) button.grid(row=0, column=2)
Tkinter.Label(body, text="Input file").grid(row=1) tkinter.Label(body, text="Input file").grid(row=1)
self.inpath = Tkinter.Entry(body, width=30) self.inpath = tkinter.Entry(body, width=30)
self.inpath.grid(row=1, column=1, sticky=sticky) self.inpath.grid(row=1, column=1, sticky=sticky)
button = Tkinter.Button(body, text="...", command=self.get_inpath) button = tkinter.Button(body, text="...", command=self.get_inpath)
button.grid(row=1, column=2) button.grid(row=1, column=2)
Tkinter.Label(body, text="Output file").grid(row=2) tkinter.Label(body, text="Output file").grid(row=2)
self.outpath = Tkinter.Entry(body, width=30) self.outpath = tkinter.Entry(body, width=30)
self.outpath.grid(row=2, column=1, sticky=sticky) self.outpath.grid(row=2, column=1, sticky=sticky)
button = Tkinter.Button(body, text="...", command=self.get_outpath) button = tkinter.Button(body, text="...", command=self.get_outpath)
button.grid(row=2, column=2) button.grid(row=2, column=2)
buttons = Tkinter.Frame(self) buttons = tkinter.Frame(self)
buttons.pack() buttons.pack()
botton = Tkinter.Button( botton = tkinter.Button(
buttons, text="Decrypt", width=10, command=self.decrypt) buttons, text="Decrypt", width=10, command=self.decrypt)
botton.pack(side=Tkconstants.LEFT) botton.pack(side=tkinter.constants.LEFT)
Tkinter.Frame(buttons, width=10).pack(side=Tkconstants.LEFT) tkinter.Frame(buttons, width=10).pack(side=tkinter.constants.LEFT)
button = Tkinter.Button( button = tkinter.Button(
buttons, text="Quit", width=10, command=self.quit) buttons, text="Quit", width=10, command=self.quit)
button.pack(side=Tkconstants.RIGHT) button.pack(side=tkinter.constants.RIGHT)
def get_keypath(self): def get_keypath(self):
keypath = tkFileDialog.askopenfilename( keypath = tkinter.filedialog.askopenfilename(
parent=None, title="Select Barnes & Noble \'.b64\' key file", parent=None, title="Select Barnes & Noble \'.b64\' key file",
defaultextension=".b64", defaultextension=".b64",
filetypes=[('base64-encoded files', '.b64'), filetypes=[('base64-encoded files', '.b64'),
('All Files', '.*')]) ('All Files', '.*')])
if keypath: if keypath:
keypath = os.path.normpath(keypath) keypath = os.path.normpath(keypath)
self.keypath.delete(0, Tkconstants.END) self.keypath.delete(0, tkinter.constants.END)
self.keypath.insert(0, keypath) self.keypath.insert(0, keypath)
return return
def get_inpath(self): def get_inpath(self):
inpath = tkFileDialog.askopenfilename( inpath = tkinter.filedialog.askopenfilename(
parent=None, title="Select B&N-encrypted ePub file to decrypt", parent=None, title="Select B&N-encrypted ePub file to decrypt",
defaultextension=".epub", filetypes=[('ePub files', '.epub')]) defaultextension=".epub", filetypes=[('ePub files', '.epub')])
if inpath: if inpath:
inpath = os.path.normpath(inpath) inpath = os.path.normpath(inpath)
self.inpath.delete(0, Tkconstants.END) self.inpath.delete(0, tkinter.constants.END)
self.inpath.insert(0, inpath) self.inpath.insert(0, inpath)
return return
def get_outpath(self): def get_outpath(self):
outpath = tkFileDialog.asksaveasfilename( outpath = tkinter.filedialog.asksaveasfilename(
parent=None, title="Select unencrypted ePub file to produce", parent=None, title="Select unencrypted ePub file to produce",
defaultextension=".epub", filetypes=[('ePub files', '.epub')]) defaultextension=".epub", filetypes=[('ePub files', '.epub')])
if outpath: if outpath:
outpath = os.path.normpath(outpath) outpath = os.path.normpath(outpath)
self.outpath.delete(0, Tkconstants.END) self.outpath.delete(0, tkinter.constants.END)
self.outpath.insert(0, outpath) self.outpath.insert(0, outpath)
return return
@ -426,11 +426,11 @@ def gui_main():
else: else:
self.status['text'] = "The was an error decrypting the file." self.status['text'] = "The was an error decrypting the file."
root = Tkinter.Tk() root = tkinter.Tk()
root.title("Barnes & Noble ePub Decrypter v.{0}".format(__version__)) root.title("Barnes & Noble ePub Decrypter v.{0}".format(__version__))
root.resizable(True, False) root.resizable(True, False)
root.minsize(300, 0) root.minsize(300, 0)
DecryptionDialog(root).pack(fill=Tkconstants.X, expand=1) DecryptionDialog(root).pack(fill=tkinter.constants.X, expand=1)
root.mainloop() root.mainloop()
return 0 return 0

View File

@ -19,7 +19,7 @@ Get Barnes & Noble EPUB user key from nook Studio log file
""" """
__license__ = 'GPL v3' __license__ = 'GPL v3'
__version__ = "1.1" __version__ = "2.0"
import sys import sys
import os import os
@ -37,10 +37,11 @@ class SafeUnbuffered:
if self.encoding == None: if self.encoding == None:
self.encoding = "utf-8" self.encoding = "utf-8"
def write(self, data): def write(self, data):
if isinstance(data,str): if isinstance(data, str):
data = data.encode(self.encoding,"replace") data = data.encode(self.encoding,"replace")
self.stream.buffer.write(data) self.stream.buffer.write(data)
self.stream.buffer.flush() self.stream.buffer.flush()
def __getattr__(self, attr): def __getattr__(self, attr):
return getattr(self.stream, attr) return getattr(self.stream, attr)
@ -276,27 +277,27 @@ def cli_main():
def gui_main(): def gui_main():
try: try:
import Tkinter import tkinter
import Tkconstants import tkinter.constants
import tkMessageBox import tkinter.messagebox
import traceback import traceback
except: except:
return cli_main() return cli_main()
class ExceptionDialog(Tkinter.Frame): class ExceptionDialog(tkinter.Frame):
def __init__(self, root, text): def __init__(self, root, text):
Tkinter.Frame.__init__(self, root, border=5) tkinter.Frame.__init__(self, root, border=5)
label = Tkinter.Label(self, text="Unexpected error:", label = tkinter.Label(self, text="Unexpected error:",
anchor=Tkconstants.W, justify=Tkconstants.LEFT) anchor=tkinter.constants.W, justify=tkinter.constants.LEFT)
label.pack(fill=Tkconstants.X, expand=0) label.pack(fill=tkinter.constants.X, expand=0)
self.text = Tkinter.Text(self) self.text = tkinter.Text(self)
self.text.pack(fill=Tkconstants.BOTH, expand=1) self.text.pack(fill=tkinter.constants.BOTH, expand=1)
self.text.insert(Tkconstants.END, text) self.text.insert(tkinter.constants.END, text)
argv=unicode_argv() argv=unicode_argv()
root = Tkinter.Tk() root = tkinter.Tk()
root.withdraw() root.withdraw()
progpath, progname = os.path.split(argv[0]) progpath, progname = os.path.split(argv[0])
success = False success = False
@ -314,14 +315,14 @@ def gui_main():
with open(outfile, 'w') as keyfileout: with open(outfile, 'w') as keyfileout:
keyfileout.write(key) keyfileout.write(key)
success = True success = True
tkMessageBox.showinfo(progname, "Key successfully retrieved to {0}".format(outfile)) tkinter.messagebox.showinfo(progname, "Key successfully retrieved to {0}".format(outfile))
except DrmException as e: except DrmException as e:
tkMessageBox.showerror(progname, "Error: {0}".format(str(e))) tkinter.messagebox.showerror(progname, "Error: {0}".format(str(e)))
except Exception: except Exception:
root.wm_state('normal') root.wm_state('normal')
root.title(progname) root.title(progname)
text = traceback.format_exc() text = traceback.format_exc()
ExceptionDialog(root, text).pack(fill=Tkconstants.BOTH, expand=1) ExceptionDialog(root, text).pack(fill=tkinter.constants.BOTH, expand=1)
root.mainloop() root.mainloop()
if not success: if not success:
return 1 return 1

View File

@ -44,10 +44,11 @@ class SafeUnbuffered:
if self.encoding == None: if self.encoding == None:
self.encoding = "utf-8" self.encoding = "utf-8"
def write(self, data): def write(self, data):
if isinstance(data,bytes): if isinstance(data, str):
data = data.encode(self.encoding,"replace") data = data.encode(self.encoding,"replace")
self.stream.write(data) self.stream.buffer.write(data)
self.stream.flush() self.stream.buffer.flush()
def __getattr__(self, attr): def __getattr__(self, attr):
return getattr(self.stream, attr) return getattr(self.stream, attr)
@ -99,9 +100,9 @@ class IGNOBLEError(Exception):
def fetch_key(email, password): def fetch_key(email, password):
# change email and password to utf-8 if unicode # change email and password to utf-8 if unicode
if type(email)==bytes: if type(email)==str:
email = email.encode('utf-8') email = email.encode('utf-8')
if type(password)==bytes: if type(password)==str:
password = password.encode('utf-8') password = password.encode('utf-8')
import random import random
@ -163,54 +164,54 @@ def cli_main():
def gui_main(): def gui_main():
try: try:
import Tkinter import tkinter
import tkFileDialog import tkinter.filedialog
import Tkconstants import tkinter.constants
import tkMessageBox import tkinter.messagebox
import traceback import traceback
except: except:
return cli_main() return cli_main()
class DecryptionDialog(Tkinter.Frame): class DecryptionDialog(tkinter.Frame):
def __init__(self, root): def __init__(self, root):
Tkinter.Frame.__init__(self, root, border=5) tkinter.Frame.__init__(self, root, border=5)
self.status = Tkinter.Label(self, text="Enter parameters") self.status = tkinter.Label(self, text="Enter parameters")
self.status.pack(fill=Tkconstants.X, expand=1) self.status.pack(fill=tkinter.constants.X, expand=1)
body = Tkinter.Frame(self) body = tkinter.Frame(self)
body.pack(fill=Tkconstants.X, expand=1) body.pack(fill=tkinter.constants.X, expand=1)
sticky = Tkconstants.E + Tkconstants.W sticky = tkinter.constants.E + tkinter.constants.W
body.grid_columnconfigure(1, weight=2) body.grid_columnconfigure(1, weight=2)
Tkinter.Label(body, text="Account email address").grid(row=0) tkinter.Label(body, text="Account email address").grid(row=0)
self.name = Tkinter.Entry(body, width=40) self.name = tkinter.Entry(body, width=40)
self.name.grid(row=0, column=1, sticky=sticky) self.name.grid(row=0, column=1, sticky=sticky)
Tkinter.Label(body, text="Account password").grid(row=1) tkinter.Label(body, text="Account password").grid(row=1)
self.ccn = Tkinter.Entry(body, width=40) self.ccn = tkinter.Entry(body, width=40)
self.ccn.grid(row=1, column=1, sticky=sticky) self.ccn.grid(row=1, column=1, sticky=sticky)
Tkinter.Label(body, text="Output file").grid(row=2) tkinter.Label(body, text="Output file").grid(row=2)
self.keypath = Tkinter.Entry(body, width=40) self.keypath = tkinter.Entry(body, width=40)
self.keypath.grid(row=2, column=1, sticky=sticky) self.keypath.grid(row=2, column=1, sticky=sticky)
self.keypath.insert(2, "bnepubkey.b64") self.keypath.insert(2, "bnepubkey.b64")
button = Tkinter.Button(body, text="...", command=self.get_keypath) button = tkinter.Button(body, text="...", command=self.get_keypath)
button.grid(row=2, column=2) button.grid(row=2, column=2)
buttons = Tkinter.Frame(self) buttons = tkinter.Frame(self)
buttons.pack() buttons.pack()
botton = Tkinter.Button( botton = tkinter.Button(
buttons, text="Fetch", width=10, command=self.generate) buttons, text="Fetch", width=10, command=self.generate)
botton.pack(side=Tkconstants.LEFT) botton.pack(side=tkinter.constants.LEFT)
Tkinter.Frame(buttons, width=10).pack(side=Tkconstants.LEFT) tkinter.Frame(buttons, width=10).pack(side=tkinter.constants.LEFT)
button = Tkinter.Button( button = tkinter.Button(
buttons, text="Quit", width=10, command=self.quit) buttons, text="Quit", width=10, command=self.quit)
button.pack(side=Tkconstants.RIGHT) button.pack(side=tkinter.constants.RIGHT)
def get_keypath(self): def get_keypath(self):
keypath = tkFileDialog.asksaveasfilename( keypath = tkinter.filedialog.asksaveasfilename(
parent=None, title="Select B&N ePub key file to produce", parent=None, title="Select B&N ePub key file to produce",
defaultextension=".b64", defaultextension=".b64",
filetypes=[('base64-encoded files', '.b64'), filetypes=[('base64-encoded files', '.b64'),
('All Files', '.*')]) ('All Files', '.*')])
if keypath: if keypath:
keypath = os.path.normpath(keypath) keypath = os.path.normpath(keypath)
self.keypath.delete(0, Tkconstants.END) self.keypath.delete(0, tkinter.constants.END)
self.keypath.insert(0, keypath) self.keypath.insert(0, keypath)
return return
@ -239,11 +240,11 @@ def gui_main():
else: else:
self.status['text'] = "Keyfile fetch failed." self.status['text'] = "Keyfile fetch failed."
root = Tkinter.Tk() root = tkinter.Tk()
root.title("Barnes & Noble ePub Keyfile Fetch v.{0}".format(__version__)) root.title("Barnes & Noble ePub Keyfile Fetch v.{0}".format(__version__))
root.resizable(True, False) root.resizable(True, False)
root.minsize(300, 0) root.minsize(300, 0)
DecryptionDialog(root).pack(fill=Tkconstants.X, expand=1) DecryptionDialog(root).pack(fill=tkinter.constants.X, expand=1)
root.mainloop() root.mainloop()
return 0 return 0

View File

@ -54,10 +54,11 @@ class SafeUnbuffered:
if self.encoding == None: if self.encoding == None:
self.encoding = "utf-8" self.encoding = "utf-8"
def write(self, data): def write(self, data):
if isinstance(data,bytes): if isinstance(data, str):
data = data.encode(self.encoding,"replace") data = data.encode(self.encoding,"replace")
self.stream.write(data) self.stream.buffer.write(data)
self.stream.flush() self.stream.buffer.flush()
def __getattr__(self, attr): def __getattr__(self, attr):
return getattr(self.stream, attr) return getattr(self.stream, attr)
@ -235,54 +236,54 @@ def cli_main():
def gui_main(): def gui_main():
try: try:
import Tkinter import tkinter
import Tkconstants import tkinter.constants
import tkMessageBox import tkinter.messagebox
import tkFileDialog import tkinter.filedialog
import traceback import traceback
except: except:
return cli_main() return cli_main()
class DecryptionDialog(Tkinter.Frame): class DecryptionDialog(tkinter.Frame):
def __init__(self, root): def __init__(self, root):
Tkinter.Frame.__init__(self, root, border=5) tkinter.Frame.__init__(self, root, border=5)
self.status = Tkinter.Label(self, text="Enter parameters") self.status = tkinter.Label(self, text="Enter parameters")
self.status.pack(fill=Tkconstants.X, expand=1) self.status.pack(fill=tkinter.constants.X, expand=1)
body = Tkinter.Frame(self) body = tkinter.Frame(self)
body.pack(fill=Tkconstants.X, expand=1) body.pack(fill=tkinter.constants.X, expand=1)
sticky = Tkconstants.E + Tkconstants.W sticky = tkinter.constants.E + tkinter.constants.W
body.grid_columnconfigure(1, weight=2) body.grid_columnconfigure(1, weight=2)
Tkinter.Label(body, text="Account Name").grid(row=0) tkinter.Label(body, text="Account Name").grid(row=0)
self.name = Tkinter.Entry(body, width=40) self.name = tkinter.Entry(body, width=40)
self.name.grid(row=0, column=1, sticky=sticky) self.name.grid(row=0, column=1, sticky=sticky)
Tkinter.Label(body, text="CC#").grid(row=1) tkinter.Label(body, text="CC#").grid(row=1)
self.ccn = Tkinter.Entry(body, width=40) self.ccn = tkinter.Entry(body, width=40)
self.ccn.grid(row=1, column=1, sticky=sticky) self.ccn.grid(row=1, column=1, sticky=sticky)
Tkinter.Label(body, text="Output file").grid(row=2) tkinter.Label(body, text="Output file").grid(row=2)
self.keypath = Tkinter.Entry(body, width=40) self.keypath = tkinter.Entry(body, width=40)
self.keypath.grid(row=2, column=1, sticky=sticky) self.keypath.grid(row=2, column=1, sticky=sticky)
self.keypath.insert(2, "bnepubkey.b64") self.keypath.insert(2, "bnepubkey.b64")
button = Tkinter.Button(body, text="...", command=self.get_keypath) button = tkinter.Button(body, text="...", command=self.get_keypath)
button.grid(row=2, column=2) button.grid(row=2, column=2)
buttons = Tkinter.Frame(self) buttons = tkinter.Frame(self)
buttons.pack() buttons.pack()
botton = Tkinter.Button( botton = tkinter.Button(
buttons, text="Generate", width=10, command=self.generate) buttons, text="Generate", width=10, command=self.generate)
botton.pack(side=Tkconstants.LEFT) botton.pack(side=tkinter.constants.LEFT)
Tkinter.Frame(buttons, width=10).pack(side=Tkconstants.LEFT) tkinter.Frame(buttons, width=10).pack(side=tkinter.constants.LEFT)
button = Tkinter.Button( button = tkinter.Button(
buttons, text="Quit", width=10, command=self.quit) buttons, text="Quit", width=10, command=self.quit)
button.pack(side=Tkconstants.RIGHT) button.pack(side=tkinter.constants.RIGHT)
def get_keypath(self): def get_keypath(self):
keypath = tkFileDialog.asksaveasfilename( keypath = tkinter.filedialog.asksaveasfilename(
parent=None, title="Select B&N ePub key file to produce", parent=None, title="Select B&N ePub key file to produce",
defaultextension=".b64", defaultextension=".b64",
filetypes=[('base64-encoded files', '.b64'), filetypes=[('base64-encoded files', '.b64'),
('All Files', '.*')]) ('All Files', '.*')])
if keypath: if keypath:
keypath = os.path.normpath(keypath) keypath = os.path.normpath(keypath)
self.keypath.delete(0, Tkconstants.END) self.keypath.delete(0, tkinter.constants.END)
self.keypath.insert(0, keypath) self.keypath.insert(0, keypath)
return return
@ -308,10 +309,10 @@ def gui_main():
open(keypath,'wb').write(userkey) open(keypath,'wb').write(userkey)
self.status['text'] = "Keyfile successfully generated" self.status['text'] = "Keyfile successfully generated"
root = Tkinter.Tk() root = tkinter.Tk()
if AES is None: if AES is None:
root.withdraw() root.withdraw()
tkMessageBox.showerror( tkinter.messagebox.showerror(
"Ignoble EPUB Keyfile Generator", "Ignoble EPUB Keyfile Generator",
"This script requires OpenSSL or PyCrypto, which must be installed " "This script requires OpenSSL or PyCrypto, which must be installed "
"separately. Read the top-of-script comment for details.") "separately. Read the top-of-script comment for details.")
@ -319,7 +320,7 @@ def gui_main():
root.title("Barnes & Noble ePub Keyfile Generator v.{0}".format(__version__)) root.title("Barnes & Noble ePub Keyfile Generator v.{0}".format(__version__))
root.resizable(True, False) root.resizable(True, False)
root.minsize(300, 0) root.minsize(300, 0)
DecryptionDialog(root).pack(fill=Tkconstants.X, expand=1) DecryptionDialog(root).pack(fill=tkinter.constants.X, expand=1)
root.mainloop() root.mainloop()
return 0 return 0

View File

@ -19,10 +19,9 @@
""" """
Decrypts Barnes & Noble encrypted PDF files. Decrypts Barnes & Noble encrypted PDF files.
""" """
from __future__ import print_function
__license__ = 'GPL v3' __license__ = 'GPL v3'
__version__ = "0.1" __version__ = "0.2"
import sys import sys
import os import os
@ -44,10 +43,11 @@ class SafeUnbuffered:
if self.encoding == None: if self.encoding == None:
self.encoding = "utf-8" self.encoding = "utf-8"
def write(self, data): def write(self, data):
if isinstance(data,unicode): if isinstance(data, str):
data = data.encode(self.encoding,"replace") data = data.encode(self.encoding,"replace")
self.stream.write(data) self.stream.buffer.write(data)
self.stream.flush() self.stream.buffer.flush()
def __getattr__(self, attr): def __getattr__(self, attr):
return getattr(self.stream, attr) return getattr(self.stream, attr)
@ -82,7 +82,7 @@ def unicode_argv():
# Remove Python executable and commands if present # Remove Python executable and commands if present
start = argc.value - len(sys.argv) start = argc.value - len(sys.argv)
return [argv[i] for i in return [argv[i] for i in
xrange(start, argc.value)] range(start, argc.value)]
return ["ignoblepdf.py"] return ["ignoblepdf.py"]
else: else:
argvencoding = sys.stdin.encoding or "utf-8" argvencoding = sys.stdin.encoding or "utf-8"
@ -236,13 +236,7 @@ def _load_crypto():
ARC4, AES = _load_crypto() ARC4, AES = _load_crypto()
try: from io import BytesIO
from cStringIO import StringIO
except ImportError:
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
# Do we generate cross reference streams on output? # Do we generate cross reference streams on output?
@ -1015,7 +1009,7 @@ class PDFStream(PDFObject):
# will get errors if the document is encrypted. # will get errors if the document is encrypted.
data = zlib.decompress(data) data = zlib.decompress(data)
elif f in LITERALS_LZW_DECODE: elif f in LITERALS_LZW_DECODE:
data = ''.join(LZWDecoder(StringIO(data)).run()) data = ''.join(LZWDecoder(BytesIO(data)).run())
elif f in LITERALS_ASCII85_DECODE: elif f in LITERALS_ASCII85_DECODE:
data = ascii85decode(data) data = ascii85decode(data)
elif f == LITERAL_CRYPT: elif f == LITERAL_CRYPT:
@ -1039,7 +1033,7 @@ class PDFStream(PDFObject):
columns = int_value(params['Columns']) columns = int_value(params['Columns'])
buf = '' buf = ''
ent0 = '\x00' * columns ent0 = '\x00' * columns
for i in xrange(0, len(data), columns+1): for i in range(0, len(data), columns+1):
pred = data[i] pred = data[i]
ent1 = data[i+1:i+1+columns] ent1 = data[i+1:i+1+columns]
if pred == '\x02': if pred == '\x02':
@ -1121,7 +1115,7 @@ class PDFXRef(object):
(start, nobjs) = map(int, f) (start, nobjs) = map(int, f)
except ValueError: except ValueError:
raise PDFNoValidXRef('Invalid line: %r: line=%r' % (parser, line)) raise PDFNoValidXRef('Invalid line: %r: line=%r' % (parser, line))
for objid in xrange(start, start+nobjs): for objid in range(start, start+nobjs):
try: try:
(_, line) = parser.nextline() (_, line) = parser.nextline()
except PSEOF: except PSEOF:
@ -1173,7 +1167,7 @@ class PDFXRefStream(object):
def objids(self): def objids(self):
for first, size in self.index: for first, size in self.index:
for objid in xrange(first, first + size): for objid in range(first, first + size):
yield objid yield objid
def load(self, parser, debug=0): def load(self, parser, debug=0):
@ -1382,7 +1376,7 @@ class PDFDocument(object):
hash.update('ffffffff'.decode('hex')) hash.update('ffffffff'.decode('hex'))
if 5 <= R: if 5 <= R:
# 8 # 8
for _ in xrange(50): for _ in range(50):
hash = hashlib.md5(hash.digest()[:length/8]) hash = hashlib.md5(hash.digest()[:length/8])
key = hash.digest()[:length/8] key = hash.digest()[:length/8]
if R == 2: if R == 2:
@ -1393,7 +1387,7 @@ class PDFDocument(object):
hash = hashlib.md5(self.PASSWORD_PADDING) # 2 hash = hashlib.md5(self.PASSWORD_PADDING) # 2
hash.update(docid[0]) # 3 hash.update(docid[0]) # 3
x = ARC4.new(key).decrypt(hash.digest()[:16]) # 4 x = ARC4.new(key).decrypt(hash.digest()[:16]) # 4
for i in xrange(1,19+1): for i in range(1,19+1):
k = ''.join( chr(ord(c) ^ i) for c in key ) k = ''.join( chr(ord(c) ^ i) for c in key )
x = ARC4.new(k).decrypt(x) x = ARC4.new(k).decrypt(x)
u1 = x+x # 32bytes total u1 = x+x # 32bytes total
@ -1781,7 +1775,7 @@ class PDFParser(PSStackParser):
class PDFObjStrmParser(PDFParser): class PDFObjStrmParser(PDFParser):
def __init__(self, data, doc): def __init__(self, data, doc):
PSStackParser.__init__(self, StringIO(data)) PSStackParser.__init__(self, BytesIO(data))
self.doc = doc self.doc = doc
return return
@ -1856,7 +1850,7 @@ class PDFSerializer(object):
if not gen_xref_stm: if not gen_xref_stm:
self.write('xref\n') self.write('xref\n')
self.write('0 %d\n' % (maxobj + 1,)) self.write('0 %d\n' % (maxobj + 1,))
for objid in xrange(0, maxobj + 1): for objid in range(0, maxobj + 1):
if objid in xrefs: if objid in xrefs:
# force the genno to be 0 # force the genno to be 0
self.write("%010d 00000 n \n" % xrefs[objid][0]) self.write("%010d 00000 n \n" % xrefs[objid][0])
@ -2043,79 +2037,79 @@ def cli_main():
def gui_main(): def gui_main():
try: try:
import Tkinter import tkinter
import Tkconstants import tkinter.constants
import tkFileDialog import tkinter.filedialog
import tkMessageBox import tkinter.messagebox
import traceback import traceback
except: except:
return cli_main() return cli_main()
class DecryptionDialog(Tkinter.Frame): class DecryptionDialog(tkinter.Frame):
def __init__(self, root): def __init__(self, root):
Tkinter.Frame.__init__(self, root, border=5) tkinter.Frame.__init__(self, root, border=5)
self.status = Tkinter.Label(self, text="Select files for decryption") self.status = tkinter.Label(self, text="Select files for decryption")
self.status.pack(fill=Tkconstants.X, expand=1) self.status.pack(fill=tkinter.constants.X, expand=1)
body = Tkinter.Frame(self) body = tkinter.Frame(self)
body.pack(fill=Tkconstants.X, expand=1) body.pack(fill=tkinter.constants.X, expand=1)
sticky = Tkconstants.E + Tkconstants.W sticky = tkinter.constants.E + tkinter.constants.W
body.grid_columnconfigure(1, weight=2) body.grid_columnconfigure(1, weight=2)
Tkinter.Label(body, text="Key file").grid(row=0) tkinter.Label(body, text="Key file").grid(row=0)
self.keypath = Tkinter.Entry(body, width=30) self.keypath = tkinter.Entry(body, width=30)
self.keypath.grid(row=0, column=1, sticky=sticky) self.keypath.grid(row=0, column=1, sticky=sticky)
if os.path.exists("bnpdfkey.b64"): if os.path.exists("bnpdfkey.b64"):
self.keypath.insert(0, "bnpdfkey.b64") self.keypath.insert(0, "bnpdfkey.b64")
button = Tkinter.Button(body, text="...", command=self.get_keypath) button = tkinter.Button(body, text="...", command=self.get_keypath)
button.grid(row=0, column=2) button.grid(row=0, column=2)
Tkinter.Label(body, text="Input file").grid(row=1) tkinter.Label(body, text="Input file").grid(row=1)
self.inpath = Tkinter.Entry(body, width=30) self.inpath = tkinter.Entry(body, width=30)
self.inpath.grid(row=1, column=1, sticky=sticky) self.inpath.grid(row=1, column=1, sticky=sticky)
button = Tkinter.Button(body, text="...", command=self.get_inpath) button = tkinter.Button(body, text="...", command=self.get_inpath)
button.grid(row=1, column=2) button.grid(row=1, column=2)
Tkinter.Label(body, text="Output file").grid(row=2) tkinter.Label(body, text="Output file").grid(row=2)
self.outpath = Tkinter.Entry(body, width=30) self.outpath = tkinter.Entry(body, width=30)
self.outpath.grid(row=2, column=1, sticky=sticky) self.outpath.grid(row=2, column=1, sticky=sticky)
button = Tkinter.Button(body, text="...", command=self.get_outpath) button = tkinter.Button(body, text="...", command=self.get_outpath)
button.grid(row=2, column=2) button.grid(row=2, column=2)
buttons = Tkinter.Frame(self) buttons = tkinter.Frame(self)
buttons.pack() buttons.pack()
botton = Tkinter.Button( botton = tkinter.Button(
buttons, text="Decrypt", width=10, command=self.decrypt) buttons, text="Decrypt", width=10, command=self.decrypt)
botton.pack(side=Tkconstants.LEFT) botton.pack(side=tkinter.constants.LEFT)
Tkinter.Frame(buttons, width=10).pack(side=Tkconstants.LEFT) tkinter.Frame(buttons, width=10).pack(side=tkinter.constants.LEFT)
button = Tkinter.Button( button = tkinter.Button(
buttons, text="Quit", width=10, command=self.quit) buttons, text="Quit", width=10, command=self.quit)
button.pack(side=Tkconstants.RIGHT) button.pack(side=tkinter.constants.RIGHT)
def get_keypath(self): def get_keypath(self):
keypath = tkFileDialog.askopenfilename( keypath = tkinter.filedialog.askopenfilename(
parent=None, title="Select Barnes & Noble \'.b64\' key file", parent=None, title="Select Barnes & Noble \'.b64\' key file",
defaultextension=".b64", defaultextension=".b64",
filetypes=[('base64-encoded files', '.b64'), filetypes=[('base64-encoded files', '.b64'),
('All Files', '.*')]) ('All Files', '.*')])
if keypath: if keypath:
keypath = os.path.normpath(keypath) keypath = os.path.normpath(keypath)
self.keypath.delete(0, Tkconstants.END) self.keypath.delete(0, tkinter.constants.END)
self.keypath.insert(0, keypath) self.keypath.insert(0, keypath)
return return
def get_inpath(self): def get_inpath(self):
inpath = tkFileDialog.askopenfilename( inpath = tkinter.filedialog.askopenfilename(
parent=None, title="Select B&N-encrypted PDF file to decrypt", parent=None, title="Select B&N-encrypted PDF file to decrypt",
defaultextension=".pdf", filetypes=[('PDF files', '.pdf')]) defaultextension=".pdf", filetypes=[('PDF files', '.pdf')])
if inpath: if inpath:
inpath = os.path.normpath(inpath) inpath = os.path.normpath(inpath)
self.inpath.delete(0, Tkconstants.END) self.inpath.delete(0, tkinter.constants.END)
self.inpath.insert(0, inpath) self.inpath.insert(0, inpath)
return return
def get_outpath(self): def get_outpath(self):
outpath = tkFileDialog.asksaveasfilename( outpath = tkinter.filedialog.asksaveasfilename(
parent=None, title="Select unencrypted PDF file to produce", parent=None, title="Select unencrypted PDF file to produce",
defaultextension=".pdf", filetypes=[('PDF files', '.pdf')]) defaultextension=".pdf", filetypes=[('PDF files', '.pdf')])
if outpath: if outpath:
outpath = os.path.normpath(outpath) outpath = os.path.normpath(outpath)
self.outpath.delete(0, Tkconstants.END) self.outpath.delete(0, tkinter.constants.END)
self.outpath.insert(0, outpath) self.outpath.insert(0, outpath)
return return
@ -2148,10 +2142,10 @@ def gui_main():
self.status['text'] = "The was an error decrypting the file." self.status['text'] = "The was an error decrypting the file."
root = Tkinter.Tk() root = tkinter.Tk()
if AES is None: if AES is None:
root.withdraw() root.withdraw()
tkMessageBox.showerror( tkinter.messagebox.showerror(
"IGNOBLE PDF", "IGNOBLE PDF",
"This script requires OpenSSL or PyCrypto, which must be installed " "This script requires OpenSSL or PyCrypto, which must be installed "
"separately. Read the top-of-script comment for details.") "separately. Read the top-of-script comment for details.")
@ -2159,7 +2153,7 @@ def gui_main():
root.title("Barnes & Noble PDF Decrypter v.{0}".format(__version__)) root.title("Barnes & Noble PDF Decrypter v.{0}".format(__version__))
root.resizable(True, False) root.resizable(True, False)
root.minsize(370, 0) root.minsize(370, 0)
DecryptionDialog(root).pack(fill=Tkconstants.X, expand=1) DecryptionDialog(root).pack(fill=tkinter.constants.X, expand=1)
root.mainloop() root.mainloop()
return 0 return 0

View File

@ -58,10 +58,11 @@ class SafeUnbuffered:
if self.encoding == None: if self.encoding == None:
self.encoding = "utf-8" self.encoding = "utf-8"
def write(self, data): def write(self, data):
if isinstance(data,bytes): if isinstance(data, str):
data = data.encode(self.encoding,"replace") data = data.encode(self.encoding,"replace")
self.stream.write(data) self.stream.buffer.write(data)
self.stream.flush() self.stream.buffer.flush()
def __getattr__(self, attr): def __getattr__(self, attr):
return getattr(self.stream, attr) return getattr(self.stream, attr)
@ -473,79 +474,79 @@ def cli_main():
def gui_main(): def gui_main():
try: try:
import Tkinter import tkinter
import Tkconstants import tkinter_constants
import tkFileDialog import tkinter_filedialog
import tkMessageBox import tkinter_messagebox
import traceback import traceback
except: except:
return cli_main() return cli_main()
class DecryptionDialog(Tkinter.Frame): class DecryptionDialog(tkinter.Frame):
def __init__(self, root): def __init__(self, root):
Tkinter.Frame.__init__(self, root, border=5) tkinter.Frame.__init__(self, root, border=5)
self.status = Tkinter.Label(self, text="Select files for decryption") self.status = tkinter.Label(self, text="Select files for decryption")
self.status.pack(fill=Tkconstants.X, expand=1) self.status.pack(fill=tkinter_constants.X, expand=1)
body = Tkinter.Frame(self) body = tkinter.Frame(self)
body.pack(fill=Tkconstants.X, expand=1) body.pack(fill=tkinter_constants.X, expand=1)
sticky = Tkconstants.E + Tkconstants.W sticky = tkinter_constants.E + tkinter_constants.W
body.grid_columnconfigure(1, weight=2) body.grid_columnconfigure(1, weight=2)
Tkinter.Label(body, text="Key file").grid(row=0) tkinter.Label(body, text="Key file").grid(row=0)
self.keypath = Tkinter.Entry(body, width=30) self.keypath = tkinter.Entry(body, width=30)
self.keypath.grid(row=0, column=1, sticky=sticky) self.keypath.grid(row=0, column=1, sticky=sticky)
if os.path.exists("adeptkey.der"): if os.path.exists("adeptkey.der"):
self.keypath.insert(0, "adeptkey.der") self.keypath.insert(0, "adeptkey.der")
button = Tkinter.Button(body, text="...", command=self.get_keypath) button = tkinter.Button(body, text="...", command=self.get_keypath)
button.grid(row=0, column=2) button.grid(row=0, column=2)
Tkinter.Label(body, text="Input file").grid(row=1) tkinter.Label(body, text="Input file").grid(row=1)
self.inpath = Tkinter.Entry(body, width=30) self.inpath = tkinter.Entry(body, width=30)
self.inpath.grid(row=1, column=1, sticky=sticky) self.inpath.grid(row=1, column=1, sticky=sticky)
button = Tkinter.Button(body, text="...", command=self.get_inpath) button = tkinter.Button(body, text="...", command=self.get_inpath)
button.grid(row=1, column=2) button.grid(row=1, column=2)
Tkinter.Label(body, text="Output file").grid(row=2) tkinter.Label(body, text="Output file").grid(row=2)
self.outpath = Tkinter.Entry(body, width=30) self.outpath = tkinter.Entry(body, width=30)
self.outpath.grid(row=2, column=1, sticky=sticky) self.outpath.grid(row=2, column=1, sticky=sticky)
button = Tkinter.Button(body, text="...", command=self.get_outpath) button = tkinter.Button(body, text="...", command=self.get_outpath)
button.grid(row=2, column=2) button.grid(row=2, column=2)
buttons = Tkinter.Frame(self) buttons = tkinter.Frame(self)
buttons.pack() buttons.pack()
botton = Tkinter.Button( botton = tkinter.Button(
buttons, text="Decrypt", width=10, command=self.decrypt) buttons, text="Decrypt", width=10, command=self.decrypt)
botton.pack(side=Tkconstants.LEFT) botton.pack(side=tkinter_constants.LEFT)
Tkinter.Frame(buttons, width=10).pack(side=Tkconstants.LEFT) tkinter.Frame(buttons, width=10).pack(side=tkinter_constants.LEFT)
button = Tkinter.Button( button = tkinter.Button(
buttons, text="Quit", width=10, command=self.quit) buttons, text="Quit", width=10, command=self.quit)
button.pack(side=Tkconstants.RIGHT) button.pack(side=tkinter_constants.RIGHT)
def get_keypath(self): def get_keypath(self):
keypath = tkFileDialog.askopenfilename( keypath = tkinter_filedialog.askopenfilename(
parent=None, title="Select Adobe Adept \'.der\' key file", parent=None, title="Select Adobe Adept \'.der\' key file",
defaultextension=".der", defaultextension=".der",
filetypes=[('Adobe Adept DER-encoded files', '.der'), filetypes=[('Adobe Adept DER-encoded files', '.der'),
('All Files', '.*')]) ('All Files', '.*')])
if keypath: if keypath:
keypath = os.path.normpath(keypath) keypath = os.path.normpath(keypath)
self.keypath.delete(0, Tkconstants.END) self.keypath.delete(0, tkinter_constants.END)
self.keypath.insert(0, keypath) self.keypath.insert(0, keypath)
return return
def get_inpath(self): def get_inpath(self):
inpath = tkFileDialog.askopenfilename( inpath = tkinter_filedialog.askopenfilename(
parent=None, title="Select ADEPT-encrypted ePub file to decrypt", parent=None, title="Select ADEPT-encrypted ePub file to decrypt",
defaultextension=".epub", filetypes=[('ePub files', '.epub')]) defaultextension=".epub", filetypes=[('ePub files', '.epub')])
if inpath: if inpath:
inpath = os.path.normpath(inpath) inpath = os.path.normpath(inpath)
self.inpath.delete(0, Tkconstants.END) self.inpath.delete(0, tkinter_constants.END)
self.inpath.insert(0, inpath) self.inpath.insert(0, inpath)
return return
def get_outpath(self): def get_outpath(self):
outpath = tkFileDialog.asksaveasfilename( outpath = tkinter_filedialog.asksaveasfilename(
parent=None, title="Select unencrypted ePub file to produce", parent=None, title="Select unencrypted ePub file to produce",
defaultextension=".epub", filetypes=[('ePub files', '.epub')]) defaultextension=".epub", filetypes=[('ePub files', '.epub')])
if outpath: if outpath:
outpath = os.path.normpath(outpath) outpath = os.path.normpath(outpath)
self.outpath.delete(0, Tkconstants.END) self.outpath.delete(0, tkinter_constants.END)
self.outpath.insert(0, outpath) self.outpath.insert(0, outpath)
return return
@ -577,11 +578,11 @@ def gui_main():
else: else:
self.status['text'] = "The was an error decrypting the file." self.status['text'] = "The was an error decrypting the file."
root = Tkinter.Tk() root = tkinter.Tk()
root.title("Adobe Adept ePub Decrypter v.{0}".format(__version__)) root.title("Adobe Adept ePub Decrypter v.{0}".format(__version__))
root.resizable(True, False) root.resizable(True, False)
root.minsize(300, 0) root.minsize(300, 0)
DecryptionDialog(root).pack(fill=Tkconstants.X, expand=1) DecryptionDialog(root).pack(fill=tkinter_constants.X, expand=1)
root.mainloop() root.mainloop()
return 0 return 0

View File

@ -71,13 +71,14 @@ class SafeUnbuffered:
def __init__(self, stream): def __init__(self, stream):
self.stream = stream self.stream = stream
self.encoding = stream.encoding self.encoding = stream.encoding
if self.encoding is None: if self.encoding == None:
self.encoding = "utf-8" self.encoding = "utf-8"
def write(self, data): def write(self, data):
if isinstance(data,bytes): if isinstance(data, str):
data = data.encode(self.encoding,"replace") data = data.encode(self.encoding,"replace")
self.stream.write(data) self.stream.buffer.write(data)
self.stream.flush() self.stream.buffer.flush()
def __getattr__(self, attr): def __getattr__(self, attr):
return getattr(self.stream, attr) return getattr(self.stream, attr)
@ -115,10 +116,8 @@ def unicode_argv():
range(start, argc.value)] range(start, argc.value)]
return ["ineptpdf.py"] return ["ineptpdf.py"]
else: else:
argvencoding = sys.stdin.encoding argvencoding = sys.stdin.encoding or "utf-8"
if argvencoding is None: return [arg if isinstance(arg, str) else str(arg, argvencoding) for arg in sys.argv]
argvencoding = "utf-8"
return sys.argv
class ADEPTError(Exception): class ADEPTError(Exception):
@ -404,13 +403,7 @@ def _load_crypto():
ARC4, RSA, AES = _load_crypto() ARC4, RSA, AES = _load_crypto()
try: from io import BytesIO
from cStringIO import StringIO
except ImportError:
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
# Do we generate cross reference streams on output? # Do we generate cross reference streams on output?
@ -443,11 +436,11 @@ def nunpack(s, default=0):
if not l: if not l:
return default return default
elif l == 1: elif l == 1:
return s return ord(s)
elif l == 2: elif l == 2:
return struct.unpack('>H', s)[0] return struct.unpack('>H', s)[0]
elif l == 3: elif l == 3:
return struct.unpack('>L', b'\x00'+s)[0] return struct.unpack('>L', '\x00'+s)[0]
elif l == 4: elif l == 4:
return struct.unpack('>L', s)[0] return struct.unpack('>L', s)[0]
else: else:
@ -486,7 +479,7 @@ class PSLiteral(PSObject):
name = [] name = []
for char in self.name: for char in self.name:
if not char.isalnum(): if not char.isalnum():
char = b'#%02x' % char char = b'#%02x' % ord(char)
name.append(char) name.append(char)
return b'/%s' % ''.join(name) return b'/%s' % ''.join(name)
@ -1183,7 +1176,7 @@ class PDFStream(PDFObject):
# will get errors if the document is encrypted. # will get errors if the document is encrypted.
data = zlib.decompress(data) data = zlib.decompress(data)
elif f in LITERALS_LZW_DECODE: elif f in LITERALS_LZW_DECODE:
data = ''.join(LZWDecoder(StringIO(data)).run()) data = ''.join(LZWDecoder(BytesIO(data)).run())
elif f in LITERALS_ASCII85_DECODE: elif f in LITERALS_ASCII85_DECODE:
data = ascii85decode(data) data = ascii85decode(data)
elif f == LITERAL_CRYPT: elif f == LITERAL_CRYPT:
@ -1950,7 +1943,7 @@ class PDFParser(PSStackParser):
class PDFObjStrmParser(PDFParser): class PDFObjStrmParser(PDFParser):
def __init__(self, data, doc): def __init__(self, data, doc):
PSStackParser.__init__(self, StringIO(data)) PSStackParser.__init__(self, BytesIO(data))
self.doc = doc self.doc = doc
return return
@ -2212,79 +2205,79 @@ def cli_main():
def gui_main(): def gui_main():
try: try:
import Tkinter import tkinter
import Tkconstants import tkinter.constants
import tkFileDialog import tkinter.filedialog
import tkMessageBox import tkinter.messagebox
import traceback import traceback
except: except:
return cli_main() return cli_main()
class DecryptionDialog(Tkinter.Frame): class DecryptionDialog(tkinter.Frame):
def __init__(self, root): def __init__(self, root):
Tkinter.Frame.__init__(self, root, border=5) tkinter.Frame.__init__(self, root, border=5)
self.status = Tkinter.Label(self, text="Select files for decryption") self.status = tkinter.Label(self, text="Select files for decryption")
self.status.pack(fill=Tkconstants.X, expand=1) self.status.pack(fill=tkinter.constants.X, expand=1)
body = Tkinter.Frame(self) body = tkinter.Frame(self)
body.pack(fill=Tkconstants.X, expand=1) body.pack(fill=tkinter.constants.X, expand=1)
sticky = Tkconstants.E + Tkconstants.W sticky = tkinter.constants.E + tkinter.constants.W
body.grid_columnconfigure(1, weight=2) body.grid_columnconfigure(1, weight=2)
Tkinter.Label(body, text="Key file").grid(row=0) tkinter.Label(body, text="Key file").grid(row=0)
self.keypath = Tkinter.Entry(body, width=30) self.keypath = tkinter.Entry(body, width=30)
self.keypath.grid(row=0, column=1, sticky=sticky) self.keypath.grid(row=0, column=1, sticky=sticky)
if os.path.exists("adeptkey.der"): if os.path.exists("adeptkey.der"):
self.keypath.insert(0, "adeptkey.der") self.keypath.insert(0, "adeptkey.der")
button = Tkinter.Button(body, text="...", command=self.get_keypath) button = tkinter.Button(body, text="...", command=self.get_keypath)
button.grid(row=0, column=2) button.grid(row=0, column=2)
Tkinter.Label(body, text="Input file").grid(row=1) tkinter.Label(body, text="Input file").grid(row=1)
self.inpath = Tkinter.Entry(body, width=30) self.inpath = tkinter.Entry(body, width=30)
self.inpath.grid(row=1, column=1, sticky=sticky) self.inpath.grid(row=1, column=1, sticky=sticky)
button = Tkinter.Button(body, text="...", command=self.get_inpath) button = tkinter.Button(body, text="...", command=self.get_inpath)
button.grid(row=1, column=2) button.grid(row=1, column=2)
Tkinter.Label(body, text="Output file").grid(row=2) tkinter.Label(body, text="Output file").grid(row=2)
self.outpath = Tkinter.Entry(body, width=30) self.outpath = tkinter.Entry(body, width=30)
self.outpath.grid(row=2, column=1, sticky=sticky) self.outpath.grid(row=2, column=1, sticky=sticky)
button = Tkinter.Button(body, text="...", command=self.get_outpath) button = tkinter.Button(body, text="...", command=self.get_outpath)
button.grid(row=2, column=2) button.grid(row=2, column=2)
buttons = Tkinter.Frame(self) buttons = tkinter.Frame(self)
buttons.pack() buttons.pack()
botton = Tkinter.Button( botton = tkinter.Button(
buttons, text="Decrypt", width=10, command=self.decrypt) buttons, text="Decrypt", width=10, command=self.decrypt)
botton.pack(side=Tkconstants.LEFT) botton.pack(side=tkinter.constants.LEFT)
Tkinter.Frame(buttons, width=10).pack(side=Tkconstants.LEFT) tkinter.Frame(buttons, width=10).pack(side=tkinter.constants.LEFT)
button = Tkinter.Button( button = tkinter.Button(
buttons, text="Quit", width=10, command=self.quit) buttons, text="Quit", width=10, command=self.quit)
button.pack(side=Tkconstants.RIGHT) button.pack(side=tkinter.constants.RIGHT)
def get_keypath(self): def get_keypath(self):
keypath = tkFileDialog.askopenfilename( keypath = tkinter.filedialog.askopenfilename(
parent=None, title="Select Adobe Adept \'.der\' key file", parent=None, title="Select Adobe Adept \'.der\' key file",
defaultextension=".der", defaultextension=".der",
filetypes=[('Adobe Adept DER-encoded files', '.der'), filetypes=[('Adobe Adept DER-encoded files', '.der'),
('All Files', '.*')]) ('All Files', '.*')])
if keypath: if keypath:
keypath = os.path.normpath(keypath) keypath = os.path.normpath(keypath)
self.keypath.delete(0, Tkconstants.END) self.keypath.delete(0, tkinter.constants.END)
self.keypath.insert(0, keypath) self.keypath.insert(0, keypath)
return return
def get_inpath(self): def get_inpath(self):
inpath = tkFileDialog.askopenfilename( inpath = tkinter.filedialog.askopenfilename(
parent=None, title="Select ADEPT-encrypted PDF file to decrypt", parent=None, title="Select ADEPT-encrypted PDF file to decrypt",
defaultextension=".pdf", filetypes=[('PDF files', '.pdf')]) defaultextension=".pdf", filetypes=[('PDF files', '.pdf')])
if inpath: if inpath:
inpath = os.path.normpath(inpath) inpath = os.path.normpath(inpath)
self.inpath.delete(0, Tkconstants.END) self.inpath.delete(0, tkinter.constants.END)
self.inpath.insert(0, inpath) self.inpath.insert(0, inpath)
return return
def get_outpath(self): def get_outpath(self):
outpath = tkFileDialog.asksaveasfilename( outpath = tkinter.filedialog.asksaveasfilename(
parent=None, title="Select unencrypted PDF file to produce", parent=None, title="Select unencrypted PDF file to produce",
defaultextension=".pdf", filetypes=[('PDF files', '.pdf')]) defaultextension=".pdf", filetypes=[('PDF files', '.pdf')])
if outpath: if outpath:
outpath = os.path.normpath(outpath) outpath = os.path.normpath(outpath)
self.outpath.delete(0, Tkconstants.END) self.outpath.delete(0, tkinter.constants.END)
self.outpath.insert(0, outpath) self.outpath.insert(0, outpath)
return return
@ -2317,10 +2310,10 @@ def gui_main():
self.status['text'] = "The was an error decrypting the file." self.status['text'] = "The was an error decrypting the file."
root = Tkinter.Tk() root = tkinter.Tk()
if RSA is None: if RSA is None:
root.withdraw() root.withdraw()
tkMessageBox.showerror( tkinter.messagebox.showerror(
"INEPT PDF", "INEPT PDF",
"This script requires OpenSSL or PyCrypto, which must be installed " "This script requires OpenSSL or PyCrypto, which must be installed "
"separately. Read the top-of-script comment for details.") "separately. Read the top-of-script comment for details.")
@ -2328,7 +2321,7 @@ def gui_main():
root.title("Adobe Adept PDF Decrypter v.{0}".format(__version__)) root.title("Adobe Adept PDF Decrypter v.{0}".format(__version__))
root.resizable(True, False) root.resizable(True, False)
root.minsize(370, 0) root.minsize(370, 0)
DecryptionDialog(root).pack(fill=Tkconstants.X, expand=1) DecryptionDialog(root).pack(fill=tkinter.constants.X, expand=1)
root.mainloop() root.mainloop()
return 0 return 0

View File

@ -28,13 +28,7 @@ import os
import os.path import os.path
import struct import struct
try: from io import BytesIO
from cStringIO import StringIO
except ImportError:
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
from Crypto.Cipher import AES from Crypto.Cipher import AES
from Crypto.Util.py3compat import bchr, bord from Crypto.Util.py3compat import bchr, bord
@ -838,7 +832,7 @@ class DrmIonVoucher(object):
b = aes.decrypt(self.ciphertext) b = aes.decrypt(self.ciphertext)
b = pkcs7unpad(b, 16) b = pkcs7unpad(b, 16)
self.drmkey = BinaryIonParser(StringIO(b)) self.drmkey = BinaryIonParser(BytesIO(b))
addprottable(self.drmkey) addprottable(self.drmkey)
_assert(self.drmkey.hasnext() and self.drmkey.next() == TID_LIST and self.drmkey.gettypename() == "com.amazon.drm.KeySet@1.0", _assert(self.drmkey.hasnext() and self.drmkey.next() == TID_LIST and self.drmkey.gettypename() == "com.amazon.drm.KeySet@1.0",
@ -877,7 +871,7 @@ class DrmIonVoucher(object):
self.envelope.next() self.envelope.next()
field = self.envelope.getfieldname() field = self.envelope.getfieldname()
if field == "voucher": if field == "voucher":
self.voucher = BinaryIonParser(StringIO(self.envelope.lobvalue())) self.voucher = BinaryIonParser(BytesIO(self.envelope.lobvalue()))
addprottable(self.voucher) addprottable(self.voucher)
continue continue
elif field != "strategy": elif field != "strategy":

View File

@ -87,11 +87,11 @@ if inCalibre:
from calibre_plugins.dedrm import androidkindlekey from calibre_plugins.dedrm import androidkindlekey
from calibre_plugins.dedrm import kfxdedrm from calibre_plugins.dedrm import kfxdedrm
else: else:
import mobidedrm from . import mobidedrm
import topazextract from . import topazextract
import kgenpids from . import kgenpids
import androidkindlekey from . import androidkindlekey
import kfxdedrm from . import kfxdedrm
# Wrap a stream so that output gets flushed immediately # Wrap a stream so that output gets flushed immediately
# and also make sure that any unicode strings get # and also make sure that any unicode strings get
@ -103,10 +103,11 @@ class SafeUnbuffered:
if self.encoding == None: if self.encoding == None:
self.encoding = "utf-8" self.encoding = "utf-8"
def write(self, data): def write(self, data):
if isinstance(data,bytes): if isinstance(data, str):
data = data.encode(self.encoding,"replace") data = data.encode(self.encoding,"replace")
self.stream.write(data) self.stream.buffer.write(data)
self.stream.flush() self.stream.buffer.flush()
def __getattr__(self, attr): def __getattr__(self, attr):
return getattr(self.stream, attr) return getattr(self.stream, attr)
@ -157,13 +158,13 @@ def unicode_argv():
# and some improvements suggested by jhaisley # and some improvements suggested by jhaisley
def cleanup_name(name): def cleanup_name(name):
# substitute filename unfriendly characters # substitute filename unfriendly characters
name = name.replace("<","[").replace(">","]").replace(" : "," ").replace(": "," ").replace(":","").replace("/","_").replace("\\","_").replace("|","_").replace("\"","\'").replace("*","_").replace("?",u"") name = name.replace("<","[").replace(">","]").replace(" : "," ").replace(": "," ").replace(":","").replace("/","_").replace("\\","_").replace("|","_").replace("\"","\'").replace("*","_").replace("?","")
# white space to single space, delete leading and trailing while space # white space to single space, delete leading and trailing while space
name = re.sub(r"\s", " ", name).strip() name = re.sub(r"\s", " ", name).strip()
# delete control characters # delete control characters
name = u"".join(char for char in name if ord(char)>=32) name = "".join(char for char in name if ord(char)>=32)
# delete non-ascii characters # delete non-ascii characters
name = u"".join(char for char in name if ord(char)<=126) name = "".join(char for char in name if ord(char)<=126)
# remove leading dots # remove leading dots
while len(name)>0 and name[0] == ".": while len(name)>0 and name[0] == ".":
name = name[1:] name = name[1:]

View File

@ -10,13 +10,7 @@ import os
import shutil import shutil
import zipfile import zipfile
try: from io import BytesIO
from cStringIO import StringIO
except ImportError:
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
__license__ = 'GPL v3' __license__ = 'GPL v3'
@ -47,8 +41,8 @@ class KFXZipBook:
if self.voucher is None: if self.voucher is None:
self.decrypt_voucher(totalpids) self.decrypt_voucher(totalpids)
print("Decrypting KFX DRMION: {0}".format(filename)) print("Decrypting KFX DRMION: {0}".format(filename))
outfile = StringIO() outfile = BytesIO()
ion.DrmIon(StringIO(data[8:-8]), lambda name: self.voucher).parse(outfile) ion.DrmIon(BytesIO(data[8:-8]), lambda name: self.voucher).parse(outfile)
self.decrypted[filename] = outfile.getvalue() self.decrypted[filename] = outfile.getvalue()
if not self.decrypted: if not self.decrypted:
@ -78,7 +72,7 @@ class KFXZipBook:
continue continue
try: try:
voucher = ion.DrmIonVoucher(StringIO(data), pid[:dsn_len], pid[dsn_len:]) voucher = ion.DrmIonVoucher(BytesIO(data), pid[:dsn_len], pid[dsn_len:])
voucher.parse() voucher.parse()
voucher.decryptvoucher() voucher.decryptvoucher()
break break

View File

@ -52,11 +52,11 @@ def SHA1(message):
def encode(data, map): def encode(data, map):
result = '' result = ''
for char in data: for char in data:
value = char value = ord(char)
Q = (value ^ 0x80) // len(map) Q = (value ^ 0x80) // len(map)
R = value % len(map) R = value % len(map)
result += chr(map[Q]) result += map[Q]
result += chr(map[R]) result += map[R]
return result return result
# Hash the bytes in data and then encode the digest with the characters in map # Hash the bytes in data and then encode the digest with the characters in map

View File

@ -154,14 +154,15 @@ def primes(n):
return primeList return primeList
# Encode the bytes in data with the characters in map # Encode the bytes in data with the characters in map
# data and map should be byte arrays
def encode(data, map): def encode(data, map):
result = b'' result = b''
for char in data: for char in data:
value = char value = char
Q = (value ^ 0x80) // len(map) Q = (value ^ 0x80) // len(map)
R = value % len(map) R = value % len(map)
result += bytes(map[Q]) result += bytes([map[Q]])
result += bytes(map[R]) result += bytes([map[R]])
return result return result
# Hash the bytes in data and then encode the digest with the characters in map # Hash the bytes in data and then encode the digest with the characters in map
@ -932,6 +933,7 @@ if iswindows:
CryptUnprotectData = CryptUnprotectData() CryptUnprotectData = CryptUnprotectData()
# Returns Environmental Variables that contain unicode # Returns Environmental Variables that contain unicode
# name must be unicode string, not byte string.
def getEnvironmentVariable(name): def getEnvironmentVariable(name):
import ctypes import ctypes
n = ctypes.windll.kernel32.GetEnvironmentVariableW(name, None, 0) n = ctypes.windll.kernel32.GetEnvironmentVariableW(name, None, 0)
@ -1070,7 +1072,7 @@ if iswindows:
if version == 5: # .kinf2011 if version == 5: # .kinf2011
added_entropy = build + guid added_entropy = build + guid
elif version == 6: # .kinf2018 elif version == 6: # .kinf2018
salt = str(0x6d8 * int(build)) + guid salt = str(0x6d8 * int(build)).encode('utf-8') + guid
sp = GetUserName() + '+@#$%+' + GetIDString() sp = GetUserName() + '+@#$%+' + GetIDString()
passwd = encode(SHA256(sp), charMap5) passwd = encode(SHA256(sp), charMap5)
key = KeyIVGen().pbkdf2(passwd, salt, 10000, 0x400)[:32] # this is very slow key = KeyIVGen().pbkdf2(passwd, salt, 10000, 0x400)[:32] # this is very slow
@ -1162,8 +1164,8 @@ if iswindows:
if len(DB)>6: if len(DB)>6:
# store values used in decryption # store values used in decryption
DB['IDString'] = GetIDString() DB[b'IDString'] = GetIDString()
DB['UserName'] = GetUserName() DB[b'UserName'] = GetUserName()
print("Decrypted key file using IDString '{0:s}' and UserName '{1:s}'".format(GetIDString(), GetUserName().encode('hex'))) print("Decrypted key file using IDString '{0:s}' and UserName '{1:s}'".format(GetIDString(), GetUserName().encode('hex')))
else: else:
print("Couldn't decrypt file.") print("Couldn't decrypt file.")
@ -1525,14 +1527,14 @@ elif isosx:
b'krx.notebookexportplugin.data.encryption_key',\ b'krx.notebookexportplugin.data.encryption_key',\
b'proxy.http.password',\ b'proxy.http.password',\
b'proxy.http.username' b'proxy.http.username'
] ]
with open(kInfoFile, 'rb') as infoReader: with open(kInfoFile, 'rb') as infoReader:
filedata = infoReader.read() filedata = infoReader.read()
data = filedata[:-1] data = filedata[:-1]
items = data.split(b'/') items = data.split(b'/')
IDStrings = GetIDStrings() IDStrings = GetIDStrings()
print ("trying username ", GetUserName()) print ("trying username ", GetUserName(), " on file ", kInfoFile)
for IDString in IDStrings: for IDString in IDStrings:
print ("trying IDString:",IDString) print ("trying IDString:",IDString)
try: try:
@ -1545,7 +1547,7 @@ elif isosx:
encryptedValue = decode(headerblob, charMap1) encryptedValue = decode(headerblob, charMap1)
#print ("encryptedvalue: ",encryptedValue) #print ("encryptedvalue: ",encryptedValue)
cleartext = UnprotectHeaderData(encryptedValue) cleartext = UnprotectHeaderData(encryptedValue)
print ("cleartext: ",cleartext) #print ("cleartext: ",cleartext)
# now extract the pieces in the same way # now extract the pieces in the same way
pattern = re.compile(rb'''\[Version:(\d+)\]\[Build:(\d+)\]\[Cksum:([^\]]+)\]\[Guid:([\{\}a-z0-9\-]+)\]''', re.IGNORECASE) pattern = re.compile(rb'''\[Version:(\d+)\]\[Build:(\d+)\]\[Cksum:([^\]]+)\]\[Guid:([\{\}a-z0-9\-]+)\]''', re.IGNORECASE)
@ -1554,26 +1556,26 @@ elif isosx:
build = m.group(2) build = m.group(2)
guid = m.group(4) guid = m.group(4)
print ("version",version) #print ("version",version)
print ("build",build) #print ("build",build)
print ("guid",guid,"\n") #print ("guid",guid,"\n")
if version == 5: # .kinf2011: identical to K4PC, except the build number gets multiplied if version == 5: # .kinf2011: identical to K4PC, except the build number gets multiplied
entropy = bytes(0x2df * int(build)) + guid entropy = str(0x2df * int(build)).encode('utf-8') + guid
cud = CryptUnprotectData(entropy,IDString) cud = CryptUnprotectData(entropy,IDString)
print ("entropy",entropy) #print ("entropy",entropy)
print ("cud",cud) #print ("cud",cud)
elif version == 6: # .kinf2018: identical to K4PC elif version == 6: # .kinf2018: identical to K4PC
salt = bytes(0x6d8 * int(build)) + guid salt = str(0x6d8 * int(build)).encode('utf-8') + guid
sp = GetUserName() + b'+@#$%+' + IDString sp = GetUserName() + b'+@#$%+' + IDString
passwd = encode(SHA256(sp), charMap5) passwd = encode(SHA256(sp), charMap5)
key = LibCrypto().keyivgen(passwd, salt, 10000, 0x400)[:32] key = LibCrypto().keyivgen(passwd, salt, 10000, 0x400)[:32]
print ("salt",salt) #print ("salt",salt)
print ("sp",sp) #print ("sp",sp)
print ("passwd",passwd) #print ("passwd",passwd)
print ("key",key) #print ("key",key)
# loop through the item records until all are processed # loop through the item records until all are processed
while len(items) > 0: while len(items) > 0:
@ -1669,9 +1671,9 @@ elif isosx:
pass pass
if len(DB)>6: if len(DB)>6:
# store values used in decryption # store values used in decryption
print("Decrypted key file using IDString '{0:s}' and UserName '{1:s}'".format(IDString, GetUserName())) print("Decrypted key file using IDString '{0:s}' and UserName '{1:s}'".format(IDString.decode('utf-8'), GetUserName().decode('utf-8')))
DB['IDString'] = IDString DB[b'IDString'] = IDString
DB['UserName'] = GetUserName() DB[b'UserName'] = GetUserName()
else: else:
print("Couldn't decrypt file.") print("Couldn't decrypt file.")
DB = {} DB = {}
@ -1690,7 +1692,7 @@ def kindlekeys(files = []):
if key: if key:
# convert all values to hex, just in case. # convert all values to hex, just in case.
for keyname in key: for keyname in key:
key[keyname]=key[keyname].encode('hex') key[keyname]=key[keyname].hex().encode('utf-8')
keys.append(key) keys.append(key)
return keys return keys
@ -1701,7 +1703,7 @@ def getkey(outpath, files=[]):
if len(keys) > 0: if len(keys) > 0:
if not os.path.isdir(outpath): if not os.path.isdir(outpath):
outfile = outpath outfile = outpath
with file(outfile, 'w') as keyfileout: with open(outfile, 'w') as keyfileout:
keyfileout.write(json.dumps(keys[0])) keyfileout.write(json.dumps(keys[0]))
print("Saved a key to {0}".format(outfile)) print("Saved a key to {0}".format(outfile))
else: else:
@ -1712,8 +1714,9 @@ def getkey(outpath, files=[]):
outfile = os.path.join(outpath,"kindlekey{0:d}.k4i".format(keycount)) outfile = os.path.join(outpath,"kindlekey{0:d}.k4i".format(keycount))
if not os.path.exists(outfile): if not os.path.exists(outfile):
break break
with file(outfile, 'w') as keyfileout: unikey = {k.decode("utf-8"):v.decode("utf-8") for k,v in key.items()}
keyfileout.write(json.dumps(key)) with open(outfile, 'w') as keyfileout:
keyfileout.write(json.dumps(unikey))
print("Saved a key to {0}".format(outfile)) print("Saved a key to {0}".format(outfile))
return True return True
return False return False
@ -1771,27 +1774,27 @@ def cli_main():
def gui_main(): def gui_main():
try: try:
import Tkinter import tkinter
import Tkconstants import tkinter.constants
import tkMessageBox import tkinter.messagebox
import traceback import traceback
except: except:
return cli_main() return cli_main()
class ExceptionDialog(Tkinter.Frame): class ExceptionDialog(tkinter.Frame):
def __init__(self, root, text): def __init__(self, root, text):
Tkinter.Frame.__init__(self, root, border=5) tkinter.Frame.__init__(self, root, border=5)
label = Tkinter.Label(self, text="Unexpected error:", label = tkinter.Label(self, text="Unexpected error:",
anchor=Tkconstants.W, justify=Tkconstants.LEFT) anchor=tkinter.constants.W, justify=tkinter.constants.LEFT)
label.pack(fill=Tkconstants.X, expand=0) label.pack(fill=tkinter.constants.X, expand=0)
self.text = Tkinter.Text(self) self.text = tkinter.Text(self)
self.text.pack(fill=Tkconstants.BOTH, expand=1) self.text.pack(fill=tkinter.constants.BOTH, expand=1)
self.text.insert(Tkconstants.END, text) self.text.insert(tkinter.constants.END, text)
argv=unicode_argv() argv=unicode_argv()
root = Tkinter.Tk() root = tkinter.Tk()
root.withdraw() root.withdraw()
progpath, progname = os.path.split(argv[0]) progpath, progname = os.path.split(argv[0])
success = False success = False
@ -1805,24 +1808,23 @@ def gui_main():
if not os.path.exists(outfile): if not os.path.exists(outfile):
break break
with file(outfile, 'w') as keyfileout: with open(outfile, 'w') as keyfileout:
keyfileout.write(json.dumps(key)) keyfileout.write(json.dumps(key))
success = True success = True
tkMessageBox.showinfo(progname, "Key successfully retrieved to {0}".format(outfile)) tkinter.messagebox.showinfo(progname, "Key successfully retrieved to {0}".format(outfile))
except DrmException as e: except DrmException as e:
tkMessageBox.showerror(progname, "Error: {0}".format(str(e))) tkinter.messagebox.showerror(progname, "Error: {0}".format(str(e)))
except Exception: except Exception:
root.wm_state('normal') root.wm_state('normal')
root.title(progname) root.title(progname)
text = traceback.format_exc() text = traceback.format_exc()
ExceptionDialog(root, text).pack(fill=Tkconstants.BOTH, expand=1) ExceptionDialog(root, text).pack(fill=tkinter.constants.BOTH, expand=1)
root.mainloop() root.mainloop()
if not success: if not success:
return 1 return 1
return 0 return 0
if __name__ == '__main__': if __name__ == '__main__':
print ("here")
if len(sys.argv) > 1: if len(sys.argv) > 1:
sys.exit(cli_main()) sys.exit(cli_main())
sys.exit(gui_main()) sys.exit(gui_main())

View File

@ -12,7 +12,7 @@
# 0.5 moved unicode_argv call inside main for Windows DeDRM compatibility # 0.5 moved unicode_argv call inside main for Windows DeDRM compatibility
# 1.0 Python 3 for calibre 5.0 # 1.0 Python 3 for calibre 5.0
from __future__ import print_function
import sys import sys
import binascii import binascii
@ -26,10 +26,11 @@ class SafeUnbuffered:
if self.encoding == None: if self.encoding == None:
self.encoding = "utf-8" self.encoding = "utf-8"
def write(self, data): def write(self, data):
if isinstance(data,bytes): if isinstance(data, str):
data = data.encode(self.encoding,"replace") data = data.encode(self.encoding,"replace")
self.stream.write(data) self.stream.buffer.write(data)
self.stream.flush() self.stream.buffer.flush()
def __getattr__(self, attr): def __getattr__(self, attr):
return getattr(self.stream, attr) return getattr(self.stream, attr)
@ -69,10 +70,8 @@ def unicode_argv():
# this should never happen # this should never happen
return ["kindlepid.py"] return ["kindlepid.py"]
else: else:
argvencoding = sys.stdin.encoding argvencoding = sys.stdin.encoding or "utf-8"
if argvencoding == None: return [arg if isinstance(arg, str) else str(arg, argvencoding) for arg in sys.argv]
argvencoding = "utf-8"
return sys.argv
letters = 'ABCDEFGHIJKLMNPQRSTUVWXYZ123456789' letters = 'ABCDEFGHIJKLMNPQRSTUVWXYZ123456789'
@ -137,6 +136,6 @@ def cli_main():
if __name__ == "__main__": if __name__ == "__main__":
#sys.stdout=SafeUnbuffered(sys.stdout) sys.stdout=SafeUnbuffered(sys.stdout)
#sys.stderr=SafeUnbuffered(sys.stderr) sys.stderr=SafeUnbuffered(sys.stderr)
sys.exit(cli_main()) sys.exit(cli_main())

View File

@ -73,7 +73,7 @@ __version__ = "1.00"
# 0.40 - moved unicode_argv call inside main for Windows DeDRM compatibility # 0.40 - moved unicode_argv call inside main for Windows DeDRM compatibility
# 0.41 - Fixed potential unicode problem in command line calls # 0.41 - Fixed potential unicode problem in command line calls
# 0.42 - Added GPL v3 licence. updated/removed some print statements # 0.42 - Added GPL v3 licence. updated/removed some print statements
# 3.00 - Added Python 3 compatibility for calibre 5.0 # 1.00 - Python 3 compatibility for calibre 5.0
import sys import sys
import os import os
@ -94,10 +94,11 @@ class SafeUnbuffered:
if self.encoding == None: if self.encoding == None:
self.encoding = "utf-8" self.encoding = "utf-8"
def write(self, data): def write(self, data):
if isinstance(data,bytes): if isinstance(data, str):
data = data.encode(self.encoding,"replace") data = data.encode(self.encoding,"replace")
self.stream.write(data) self.stream.buffer.write(data)
self.stream.flush() self.stream.buffer.flush()
def __getattr__(self, attr): def __getattr__(self, attr):
return getattr(self.stream, attr) return getattr(self.stream, attr)
@ -152,12 +153,12 @@ class DrmException(Exception):
# Implementation of Pukall Cipher 1 # Implementation of Pukall Cipher 1
def PC1(key, src, decryption=True): def PC1(key, src, decryption=True):
# if we can get it from alfcrypto, use that # if we can get it from alfcrypto, use that
#try: try:
# return Pukall_Cipher().PC1(key,src,decryption) return Pukall_Cipher().PC1(key,src,decryption)
#except NameError: except NameError:
# pass pass
#except TypeError: except TypeError:
# pass pass
# use slow python version, since Pukall_Cipher didn't load # use slow python version, since Pukall_Cipher didn't load
sum1 = 0; sum1 = 0;

View File

@ -2,10 +2,11 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab # vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab
from __future__ import print_function
import sys import sys
import os import os
import re import re
import traceback
import calibre_plugins.dedrm.ineptepub import calibre_plugins.dedrm.ineptepub
import calibre_plugins.dedrm.ignobleepub import calibre_plugins.dedrm.ignobleepub
import calibre_plugins.dedrm.epubtest import calibre_plugins.dedrm.epubtest
@ -13,7 +14,6 @@ import calibre_plugins.dedrm.zipfix
import calibre_plugins.dedrm.ineptpdf import calibre_plugins.dedrm.ineptpdf
import calibre_plugins.dedrm.erdr2pml import calibre_plugins.dedrm.erdr2pml
import calibre_plugins.dedrm.k4mobidedrm import calibre_plugins.dedrm.k4mobidedrm
import traceback
def decryptepub(infile, outdir, rscpath): def decryptepub(infile, outdir, rscpath):
errlog = '' errlog = ''

View File

@ -1,23 +1,23 @@
#!/usr/bin/env python #!/usr/bin/env python
# vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab # vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab
import Tkinter import tkinter
import Tkconstants import tkinter.constants
# basic scrolled text widget # basic scrolled text widget
class ScrolledText(Tkinter.Text): class ScrolledText(tkinter.Text):
def __init__(self, master=None, **kw): def __init__(self, master=None, **kw):
self.frame = Tkinter.Frame(master) self.frame = tkinter.Frame(master)
self.vbar = Tkinter.Scrollbar(self.frame) self.vbar = tkinter.Scrollbar(self.frame)
self.vbar.pack(side=Tkconstants.RIGHT, fill=Tkconstants.Y) self.vbar.pack(side=tkinter.constants.RIGHT, fill=tkinter.constants.Y)
kw.update({'yscrollcommand': self.vbar.set}) kw.update({'yscrollcommand': self.vbar.set})
Tkinter.Text.__init__(self, self.frame, **kw) tkinter.Text.__init__(self, self.frame, **kw)
self.pack(side=Tkconstants.LEFT, fill=Tkconstants.BOTH, expand=True) self.pack(side=tkinter.constants.LEFT, fill=tkinter.constants.BOTH, expand=True)
self.vbar['command'] = self.yview self.vbar['command'] = self.yview
# Copy geometry methods of self.frame without overriding Text # Copy geometry methods of self.frame without overriding Text
# methods = hack! # methods = hack!
text_meths = vars(Tkinter.Text).keys() text_meths = list(vars(tkinter.Text).keys())
methods = vars(Tkinter.Pack).keys() + vars(Tkinter.Grid).keys() + vars(Tkinter.Place).keys() methods = list(vars(tkinter.Pack).keys()) + list(vars(tkinter.Grid).keys()) + list(vars(tkinter.Place).keys())
methods = set(methods).difference(text_meths) methods = set(methods).difference(text_meths)
for m in methods: for m in methods:
if m[0] != '_' and m != 'config' and m != 'configure': if m[0] != '_' and m != 'config' and m != 'configure':

View File

@ -2,7 +2,7 @@
# vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab # vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab
# For use with Topaz Scripts Version 2.6 # For use with Topaz Scripts Version 2.6
from __future__ import print_function
import csv import csv
import sys import sys
import os import os
@ -58,7 +58,7 @@ class DocParser(object):
else: else:
end = min(cnt,end) end = min(cnt,end)
foundat = -1 foundat = -1
for j in xrange(pos, end): for j in range(pos, end):
item = docList[j] item = docList[j]
if item.find('=') >= 0: if item.find('=') >= 0:
(name, argres) = item.split('=',1) (name, argres) = item.split('=',1)
@ -116,7 +116,7 @@ class DocParser(object):
# process each style converting what you can # process each style converting what you can
if debug: print(' ', 'Processing styles.') if debug: print(' ', 'Processing styles.')
for j in xrange(stylecnt): for j in range(stylecnt):
if debug: print(' ', 'Processing style %d' %(j)) if debug: print(' ', 'Processing style %d' %(j))
start = styleList[j] start = styleList[j]
end = styleList[j+1] end = styleList[j+1]

View File

@ -9,7 +9,6 @@
# 5.0 - Fixed potential unicode problem with command line interface # 5.0 - Fixed potential unicode problem with command line interface
# 6.0 - Added Python 3 compatibility for calibre 5.0 # 6.0 - Added Python 3 compatibility for calibre 5.0
from __future__ import print_function
__version__ = '6.0' __version__ = '6.0'
import sys import sys
@ -23,6 +22,9 @@ try:
except: except:
from alfcrypto import Topaz_Cipher from alfcrypto import Topaz_Cipher
# Wrap a stream so that output gets flushed immediately
# and also make sure that any unicode strings get
# encoded using "replace" before writing them.
class SafeUnbuffered: class SafeUnbuffered:
def __init__(self, stream): def __init__(self, stream):
self.stream = stream self.stream = stream
@ -30,10 +32,11 @@ class SafeUnbuffered:
if self.encoding == None: if self.encoding == None:
self.encoding = "utf-8" self.encoding = "utf-8"
def write(self, data): def write(self, data):
if isinstance(data,bytes): if isinstance(data, str):
data = data.encode(self.encoding,"replace") data = data.encode(self.encoding,"replace")
self.stream.write(data) self.stream.buffer.write(data)
self.stream.flush() self.stream.buffer.flush()
def __getattr__(self, attr): def __getattr__(self, attr):
return getattr(self.stream, attr) return getattr(self.stream, attr)
@ -94,7 +97,7 @@ class DrmException(Exception):
# recursive zip creation support routine # recursive zip creation support routine
def zipUpDir(myzip, tdir, localname): def zipUpDir(myzip, tdir, localname):
currentdir = tdir currentdir = tdir
if localname != u"": if localname != "":
currentdir = os.path.join(currentdir,localname) currentdir = os.path.join(currentdir,localname)
list = os.listdir(currentdir) list = os.listdir(currentdir)
for file in list: for file in list:

View File

@ -19,8 +19,8 @@ DETAILED_MESSAGE = \
def uStrCmp (s1, s2, caseless=False): def uStrCmp (s1, s2, caseless=False):
import unicodedata as ud import unicodedata as ud
str1 = s1 if isinstance(s1, unicode) else s1.decode('utf-8') str1 = s1 if isinstance(s1, str) else str(s1)
str2 = s2 if isinstance(s2, unicode) else s2.decode('utf-8') str2 = s2 if isinstance(s2, str) else str(s2)
if caseless: if caseless:
return ud.normalize('NFC', str1.lower()) == ud.normalize('NFC', str2.lower()) return ud.normalize('NFC', str1.lower()) == ud.normalize('NFC', str2.lower())
else: else:

View File

@ -15,7 +15,7 @@
""" """
Re-write zip (or ePub) fixing problems with file names (and mimetype entry). Re-write zip (or ePub) fixing problems with file names (and mimetype entry).
""" """
from __future__ import print_function
__license__ = 'GPL v3' __license__ = 'GPL v3'
__version__ = "1.1" __version__ = "1.1"