Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions gzip_mod.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import struct, sys, time, os
import zlib
import io
import __builtin__
import builtins

__all__ = ["GzipFile","open"]

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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):
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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 )
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 7 additions & 7 deletions image_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down