From c797410ead871d80fa4fd2d9bca3ec2b6aeba4f1 Mon Sep 17 00:00:00 2001 From: pcmagas Date: Fri, 16 Dec 2022 21:33:01 +0200 Subject: [PATCH] Switch to python3. --- gzip_mod.py | 38 +++++++++++++++++++------------------- image_types.py | 14 +++++++------- utils.py | 1 + 3 files changed, 27 insertions(+), 26 deletions(-) diff --git a/gzip_mod.py b/gzip_mod.py index 718aae6..a2ad7d1 100644 --- a/gzip_mod.py +++ b/gzip_mod.py @@ -8,7 +8,7 @@ import struct, sys, time, os import zlib import io -import __builtin__ +import builtins __all__ = ["GzipFile","open"] @@ -91,7 +91,7 @@ def __init__(self, filename=None, mode=None, if mode and 'b' not in mode: mode += 'b' if fileobj is None: - fileobj = self.myfileobj = __builtin__.open(filename, mode or 'rb') + fileobj = self.myfileobj = builtins.open(filename, mode or 'rb') if filename is None: # Issue #13781: os.fdopen() creates a fileobj with a bogus name # attribute. Avoid saving this in the gzip header's filename field. @@ -126,7 +126,7 @@ def __init__(self, filename=None, mode=None, zlib.DEF_MEM_LEVEL, 0) else: - raise IOError, "Mode " + mode + " not supported" + raise IOError("Mode " + mode + " not supported") self.fileobj = fileobj self.offset = 0 @@ -156,7 +156,7 @@ def _check_closed(self): def _init_write(self, filename): self.name = filename - self.crc = zlib.crc32("") & 0xffffffffL + self.crc = zlib.crc32("") & int("0xffffffffL",0) self.size = 0 self.writebuf = [] self.bufsize = 0 @@ -172,16 +172,16 @@ def _write_gzip_header(self): self.fileobj.write('\x03') def _init_read(self): - self.crc = zlib.crc32("") & 0xffffffffL + self.crc = zlib.crc32("") & int("0xffffffffL",0) self.size = 0 def _read_gzip_header(self): magic = self.fileobj.read(2) if magic != '\037\213': - raise IOError, 'Not a gzipped file' + raise IOError('Not a gzipped file') method = ord( self.fileobj.read(1) ) if method != 8: - raise IOError, 'Unknown compression method' + raise IOError('Unknown compression method') flag = ord( self.fileobj.read(1) ) self.mtime = read32(self.fileobj) # extraflag = self.fileobj.read(1) @@ -215,7 +215,7 @@ def write(self,data): raise IOError(errno.EBADF, "write() on read-only GzipFile object") if self.fileobj is None: - raise ValueError, "write() on closed GzipFile object" + raise ValueError("write() on closed GzipFile object") # Convert data type if called by io.BufferedWriter. if isinstance(data, memoryview): @@ -224,7 +224,7 @@ def write(self,data): if len(data) > 0: self.fileobj.write(self.compress.compress(data)) self.size += len(data) - self.crc = zlib.crc32(data, self.crc) & 0xffffffffL + self.crc = zlib.crc32(data, self.crc) & int("0xffffffffL",0) self.offset += len(data) return len(data) @@ -268,7 +268,7 @@ def _unread(self, buf): def _read(self, size=1024): if self.fileobj is None: - raise EOFError, "Reached EOF" + raise EOFError("Reached EOF") if self._new_member: # If the _new_member flag is set, we have to @@ -279,7 +279,7 @@ def _read(self, size=1024): pos = self.fileobj.tell() # Save current position self.fileobj.seek(0, 2) # Seek to end of file if pos == self.fileobj.tell(): - raise EOFError, "Reached EOF" + raise EOFError("Reached EOF") else: self.fileobj.seek( pos ) # Return to original position @@ -298,7 +298,7 @@ def _read(self, size=1024): uncompress = self.decompress.flush() self._read_eof() self._add_read_data( uncompress ) - raise EOFError, 'Reached EOF' + raise EOFError('Reached EOF') uncompress = self.decompress.decompress(buf) self._add_read_data( uncompress ) @@ -317,7 +317,7 @@ def _read(self, size=1024): self._new_member = True def _add_read_data(self, data): - self.crc = zlib.crc32(data, self.crc) & 0xffffffffL + self.crc = zlib.crc32(data, self.crc) & int("0xffffffffL",0) offset = self.offset - self.extrastart self.extrabuf = self.extrabuf[offset:] + data self.extrasize = self.extrasize + len(data) @@ -336,8 +336,8 @@ def _read_eof(self): if crc32 != self.crc: raise IOError("CRC check failed %s != %s" % (hex(crc32), hex(self.crc))) - elif isize != (self.size & 0xffffffffL): - raise IOError, "Incorrect length of data produced" + elif isize != (self.size & int("0xffffffffL",0)): + raise IOError("Incorrect length of data produced") # Gzip files can be padded with zeroes and still have archives. # Consume all zero bytes and set the file position to the first @@ -362,7 +362,7 @@ def close(self): fileobj.write(self.compress.flush()) write32u(fileobj, self.crc) # self.size may exceed 2GB, or even 4GB - write32u(fileobj, self.size & 0xffffffffL) + write32u(fileobj, self.size & int("0xffffffffL",0)) finally: myfileobj = self.myfileobj if myfileobj: @@ -486,16 +486,16 @@ def _test(): g = sys.stdout else: if arg[-3:] != ".gz": - print "filename doesn't end in .gz:", repr(arg) + print ("filename doesn't end in .gz:", repr(arg)) continue f = open(arg, "rb") - g = __builtin__.open(arg[:-3], "wb") + g = builtins.open(arg[:-3], "wb") else: if arg == "-": f = sys.stdin g = GzipFile(filename="", mode="wb", fileobj=sys.stdout) else: - f = __builtin__.open(arg, "rb") + f = builtins.open(arg, "rb") g = open(arg + ".gz", "wb") while True: chunk = f.read(1024) diff --git a/image_types.py b/image_types.py index 9d5b192..18de52d 100644 --- a/image_types.py +++ b/image_types.py @@ -2,7 +2,7 @@ from block_descriptor import * from Crypto.Cipher import AES import hashlib -import cStringIO +from io import BytesIO import gzip import json import gzip_mod @@ -11,7 +11,7 @@ class Image: def __init__(self, image_data, read=True): - self.stream = cStringIO.StringIO(image_data) + self.stream = BytesIO(image_data) self.stream_len = len(image_data) if read: self.readHeader() @@ -115,8 +115,8 @@ def writeManifest(self): print('[-] Failed to write manifest to file!') def createImage(self): - self.stream = cStringIO.StringIO() - content_stream = cStringIO.StringIO() + self.stream = BytesIO() + content_stream = BytesIO() gzip_wrapper = gzip_mod.GzipFile(filename = None, mode = 'wb', fileobj = content_stream, compresslevel = 6) for block in self.blocks: print('[+] Writing block with name %s and version %s to stream...' % (block.block_name, block.block_version)) @@ -166,7 +166,7 @@ def getKeyPair(self): return dict(key=key, iv=self.iv[:16]) def createImage(self, fw_version, stage2_image): - self.stream = cStringIO.StringIO() + self.stream = BytesIO() self.nullpad = '\x00' * 32 self.nullpad2 = '\x00' * 32 self.fw_version = fw_version @@ -202,7 +202,7 @@ def validateType(self): cur_pos = self.stream.tell() self.stream.seek(32) # Skip original image digest digest = hashlib.new('sha256') - digest.update(('\x00' * 32) + self.stream.read()) + digest.update((b'\x00' * 32) + self.stream.read()) self.stream.seek(cur_pos) return digest.digest() == self.image_digest @@ -232,7 +232,7 @@ def getKeyPair(self): return dict(key=key, iv=self.iv[:16]) def createImage(self, fw_version, stage2_image): - self.stream = cStringIO.StringIO() + self.stream = BytesIO() self.fw_version = fw_version self.filesize = len(stage2_image) self.key_factor = os.urandom(32) diff --git a/utils.py b/utils.py index 301e345..24bd166 100644 --- a/utils.py +++ b/utils.py @@ -2,6 +2,7 @@ def nullpad_str(s, length): return s + ('\x00' * (length - len(s))) def unnullpad_str(s): + s = s.decode() if '\x00' not in s: return s