diff options
author | NoDRM <[email protected]> | 2022-08-06 20:10:51 +0200 |
---|---|---|
committer | NoDRM <[email protected]> | 2022-08-06 20:10:51 +0200 |
commit | 9276d77f6328866394fcbc568b843c4d2cdd3065 (patch) | |
tree | c2555b61913fe14daafc8993e791e6cb626216d2 | |
parent | de23b5c221d89a047807910edbc561992b1a88e3 (diff) |
Couple Python 2 fixes in (unsupported) standalone scripts
7 files changed, 88 insertions, 31 deletions
diff --git a/Other_Tools/DRM_Key_Scripts/Adobe_Digital_Editions/adobekey.pyw b/Other_Tools/DRM_Key_Scripts/Adobe_Digital_Editions/adobekey.pyw index cbc3038..8e9061e 100644 --- a/Other_Tools/DRM_Key_Scripts/Adobe_Digital_Editions/adobekey.pyw +++ b/Other_Tools/DRM_Key_Scripts/Adobe_Digital_Editions/adobekey.pyw @@ -68,12 +68,20 @@ class SafeUnbuffered: if self.encoding == None: self.encoding = "utf-8" def write(self, data): - if isinstance(data,unicode): + if isinstance(data,str) or isinstance(data,unicode): + # str for Python3, unicode for Python2 data = data.encode(self.encoding,"replace") - self.stream.write(data) - self.stream.flush() + try: + buffer = getattr(self.stream, 'buffer', self.stream) + # self.stream.buffer for Python3, self.stream for Python2 + buffer.write(data) + buffer.flush() + except: + # We can do nothing if a write fails + raise def __getattr__(self, attr): return getattr(self.stream, attr) + try: from calibre.constants import iswindows, isosx diff --git a/Other_Tools/DRM_Key_Scripts/Barnes_and_Noble_ePubs/ignoblekey.pyw b/Other_Tools/DRM_Key_Scripts/Barnes_and_Noble_ePubs/ignoblekey.pyw index f6a93eb..b6ddb2c 100644 --- a/Other_Tools/DRM_Key_Scripts/Barnes_and_Noble_ePubs/ignoblekey.pyw +++ b/Other_Tools/DRM_Key_Scripts/Barnes_and_Noble_ePubs/ignoblekey.pyw @@ -39,12 +39,20 @@ class SafeUnbuffered: if self.encoding == None: self.encoding = "utf-8" def write(self, data): - if isinstance(data,unicode): + if isinstance(data,str) or isinstance(data,unicode): + # str for Python3, unicode for Python2 data = data.encode(self.encoding,"replace") - self.stream.write(data) - self.stream.flush() + try: + buffer = getattr(self.stream, 'buffer', self.stream) + # self.stream.buffer for Python3, self.stream for Python2 + buffer.write(data) + buffer.flush() + except: + # We can do nothing if a write fails + raise def __getattr__(self, attr): return getattr(self.stream, attr) + try: from calibre.constants import iswindows, isosx diff --git a/Other_Tools/DRM_Key_Scripts/Barnes_and_Noble_ePubs/ignoblekeyfetch.pyw b/Other_Tools/DRM_Key_Scripts/Barnes_and_Noble_ePubs/ignoblekeyfetch.pyw index e9637a1..3bed979 100644 --- a/Other_Tools/DRM_Key_Scripts/Barnes_and_Noble_ePubs/ignoblekeyfetch.pyw +++ b/Other_Tools/DRM_Key_Scripts/Barnes_and_Noble_ePubs/ignoblekeyfetch.pyw @@ -45,12 +45,20 @@ class SafeUnbuffered: if self.encoding == None: self.encoding = "utf-8" def write(self, data): - if isinstance(data,unicode): + if isinstance(data,str) or isinstance(data,unicode): + # str for Python3, unicode for Python2 data = data.encode(self.encoding,"replace") - self.stream.write(data) - self.stream.flush() + try: + buffer = getattr(self.stream, 'buffer', self.stream) + # self.stream.buffer for Python3, self.stream for Python2 + buffer.write(data) + buffer.flush() + except: + # We can do nothing if a write fails + raise def __getattr__(self, attr): return getattr(self.stream, attr) + try: from calibre.constants import iswindows, isosx diff --git a/Other_Tools/DRM_Key_Scripts/Barnes_and_Noble_ePubs/ignoblekeygen.pyw b/Other_Tools/DRM_Key_Scripts/Barnes_and_Noble_ePubs/ignoblekeygen.pyw index d2917c7..8d3ea1a 100644 --- a/Other_Tools/DRM_Key_Scripts/Barnes_and_Noble_ePubs/ignoblekeygen.pyw +++ b/Other_Tools/DRM_Key_Scripts/Barnes_and_Noble_ePubs/ignoblekeygen.pyw @@ -56,12 +56,20 @@ class SafeUnbuffered: if self.encoding == None: self.encoding = "utf-8" def write(self, data): - if isinstance(data,unicode): + if isinstance(data,str) or isinstance(data,unicode): + # str for Python3, unicode for Python2 data = data.encode(self.encoding,"replace") - self.stream.write(data) - self.stream.flush() + try: + buffer = getattr(self.stream, 'buffer', self.stream) + # self.stream.buffer for Python3, self.stream for Python2 + buffer.write(data) + buffer.flush() + except: + # We can do nothing if a write fails + raise def __getattr__(self, attr): return getattr(self.stream, attr) + try: from calibre.constants import iswindows, isosx diff --git a/Other_Tools/DRM_Key_Scripts/Kindle_for_Android/androidkindlekey.pyw b/Other_Tools/DRM_Key_Scripts/Kindle_for_Android/androidkindlekey.pyw index ff8d1ee..1d8cc89 100644 --- a/Other_Tools/DRM_Key_Scripts/Kindle_for_Android/androidkindlekey.pyw +++ b/Other_Tools/DRM_Key_Scripts/Kindle_for_Android/androidkindlekey.pyw @@ -48,12 +48,20 @@ class SafeUnbuffered: if self.encoding == None: self.encoding = "utf-8" def write(self, data): - if isinstance(data,unicode): + if isinstance(data,str) or isinstance(data,unicode): + # str for Python3, unicode for Python2 data = data.encode(self.encoding,"replace") - self.stream.write(data) - self.stream.flush() + try: + buffer = getattr(self.stream, 'buffer', self.stream) + # self.stream.buffer for Python3, self.stream for Python2 + buffer.write(data) + buffer.flush() + except: + # We can do nothing if a write fails + raise def __getattr__(self, attr): return getattr(self.stream, attr) + try: from calibre.constants import iswindows, isosx diff --git a/Other_Tools/DRM_Key_Scripts/Kindle_for_Mac_and_PC/kindlekey.pyw b/Other_Tools/DRM_Key_Scripts/Kindle_for_Mac_and_PC/kindlekey.pyw index 99b8d22..a463f1f 100644 --- a/Other_Tools/DRM_Key_Scripts/Kindle_for_Mac_and_PC/kindlekey.pyw +++ b/Other_Tools/DRM_Key_Scripts/Kindle_for_Mac_and_PC/kindlekey.pyw @@ -51,12 +51,20 @@ class SafeUnbuffered: if self.encoding == None: self.encoding = "utf-8" def write(self, data): - if isinstance(data,unicode): + if isinstance(data,str) or isinstance(data,unicode): + # str for Python3, unicode for Python2 data = data.encode(self.encoding,"replace") - self.stream.write(data) - self.stream.flush() + try: + buffer = getattr(self.stream, 'buffer', self.stream) + # self.stream.buffer for Python3, self.stream for Python2 + buffer.write(data) + buffer.flush() + except: + # We can do nothing if a write fails + raise def __getattr__(self, attr): return getattr(self.stream, attr) + try: from calibre.constants import iswindows, isosx @@ -292,7 +300,7 @@ if iswindows: numBlocks, numExtraBytes = divmod(len(self.bytesToDecrypt), self.blockSize) if more == None: # no more calls to decrypt, should have all the data if numExtraBytes != 0: - raise DecryptNotBlockAlignedError, 'Data not block aligned on decrypt' + raise DecryptNotBlockAlignedError('Data not block aligned on decrypt') # hold back some bytes in case last decrypt has zero len if (more != None) and (numExtraBytes == 0) and (numBlocks >0) : @@ -334,7 +342,7 @@ if iswindows: def removePad(self, paddedBinaryString, blockSize): """ Remove padding from a binary string """ if not(0<len(paddedBinaryString)): - raise DecryptNotBlockAlignedError, 'Expected More Data' + raise DecryptNotBlockAlignedError('Expected More Data') return paddedBinaryString[:-ord(paddedBinaryString[-1])] class noPadding(Pad): @@ -364,8 +372,8 @@ if iswindows: self.blockSize = blockSize # blockSize is in bytes 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( blockSize%4==0 and NrTable.has_key(blockSize/4)), 'block 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 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.Nk = keySize/4 # Nk is the key length in 32-bit words @@ -642,7 +650,7 @@ if iswindows: def __init__(self, key = None, padding = padWithPadLen(), keySize=16): """ Initialize AES, keySize is in bytes """ if not (keySize == 16 or keySize == 24 or keySize == 32) : - raise BadKeySizeError, 'Illegal AES key size, must be 16, 24, or 32 bytes' + raise BadKeySizeError('Illegal AES key size, must be 16, 24, or 32 bytes') Rijndael.__init__( self, key, padding=padding, keySize=keySize, blockSize=16 ) @@ -782,10 +790,11 @@ if iswindows: # [c_char_p, c_ulong, c_char_p, c_ulong, c_ulong, c_ulong, c_char_p]) def pbkdf2(self, passwd, salt, iter, keylen): - def xorstr( a, b ): + def xorbytes( a, b ): if len(a) != len(b): - raise Exception("xorstr(): lengths differ") - return ''.join((chr(ord(x)^ord(y)) for x, y in zip(a, b))) + raise Exception("xorbytes(): lengths differ") + return bytes([x ^ y for x, y in zip(a, b)]) + def prf( h, data ): hm = h.copy() @@ -797,13 +806,13 @@ if iswindows: T = U for i in range(2, itercount+1): U = prf( h, U ) - T = xorstr( T, U ) + T = xorbytes( T, U ) return T sha = hashlib.sha1 digest_size = sha().digest_size # l - number of output blocks to produce - l = keylen / digest_size + l = keylen // digest_size if keylen % digest_size != 0: l += 1 h = hmac.new( passwd, None, sha ) diff --git a/Other_Tools/DRM_Key_Scripts/Kindle_for_iOS/kindleiospidgen.pyw b/Other_Tools/DRM_Key_Scripts/Kindle_for_iOS/kindleiospidgen.pyw index 0c0b11b..ed8a6cf 100644 --- a/Other_Tools/DRM_Key_Scripts/Kindle_for_iOS/kindleiospidgen.pyw +++ b/Other_Tools/DRM_Key_Scripts/Kindle_for_iOS/kindleiospidgen.pyw @@ -32,12 +32,20 @@ class SafeUnbuffered: if self.encoding == None: self.encoding = "utf-8" def write(self, data): - if isinstance(data,unicode): + if isinstance(data,str) or isinstance(data,unicode): + # str for Python3, unicode for Python2 data = data.encode(self.encoding,"replace") - self.stream.write(data) - self.stream.flush() + try: + buffer = getattr(self.stream, 'buffer', self.stream) + # self.stream.buffer for Python3, self.stream for Python2 + buffer.write(data) + buffer.flush() + except: + # We can do nothing if a write fails + raise def __getattr__(self, attr): return getattr(self.stream, attr) + iswindows = sys.platform.startswith('win') isosx = sys.platform.startswith('darwin') |