Added function and loop to iterate through file.

This commit is contained in:
ultramagnus_tcv 2015-09-12 11:09:03 -05:00
parent 14bf6bdae1
commit 14c15797f0
2 changed files with 52 additions and 10 deletions

View File

@ -1,8 +1,14 @@
from struct import unpack
import os
def tup2int(offset):
offset = unpack ('< H', SOS)
def tup2int(tup):
return int('.' . join(str(x) for x in tup))
def tup2int():
SOS = SOSfile.read(2)
offset_unpacked = unpack ('< H', SOS)
offset = int('.'.join(str(x) for x in offset_unpacked))
print 'DEBUG: Inside function. Offset is:', hex(offset)
return offset
#Clear SCREEN
@ -11,19 +17,43 @@ print("\033c");
#Is File a SOS DRIVER file?
SOSfile = open('SOSCFFA.DRIVER', 'rb')
SOS = SOSfile.read(10)
filetype, offset = unpack('< 8s H', SOS)
SOS = SOSfile.read(8)
filetype = unpack('< 8s', SOS)
if 'SOS DRVR' in filetype:
print "This is a proper SOS file."
print "Filetype is: %s. First offset is: %04x" % (filetype, offset)
print "Filetype is: %s." % (filetype)
else:
print "This is not a proper SOS file"
#Seek to first driver
def readUnpackAndint(bytes):
SOS = SOSfile.read(bytes)
offset_unpacked = unpack ('< H', SOS)
return int('.'.join(str(x) for x in offset_unpacked))
### At this point, we need the first offset to tell us where to jump to
### find the first actual driver.
# Read immediate two bytes after SOS DRVR to establish first offset value.
offset = readUnpackAndint(2)
print "The first offset value is", hex(offset)
### Now we have the offset needed to jump to the first driver. Let's seek
### to that driver using the offset. This is our first jump.
SOSfile.seek(offset,1)
SOS = SOSfile.read(2) # Read two bytes
marker = unpack('< H', SOS)
# if marker == \x0000 : #Saving for later...
print "This is our new position in the file: ", hex(SOSfile.tell())
offset = readUnpackAndint(2)
print 'New offset is: ' , hex(offset)
### I will establish an indefinite loop that will come around until we
### encounter FF which indicates the last driver. For now, I am manually
### looping to check logic.
while offset != 65535 :
SOSfile.seek(offset,1)
print "This is our new position in the file: ", hex(SOSfile.tell())
offset = readUnpackAndint(2)
print 'New offset is: ' , hex(offset)
SOSfile.close()

View File

@ -61,4 +61,16 @@ I am having trouble with the return portion. I don't really know how to do it. W
Implemented a (very) rudimentary IF/ELSE that checks the first eight bytes of the driver file to see if "SOS DRVR" exist. If not, the script ends. If so, we will continue. This entailed unpacking the data as string-data, converting the resultant tuple into an str, then checking what's there. I did verify via HEX edit that if the string is not "SOS DRVR," the else will be taken.
Is there a way to run the IF/ELSE against the contents of the tuple? It would save my having to convert from tuple to str.
Is there a way to run the IF/ELSE against the contents of the tuple? It would save my having to convert from tuple to str.
(edit: Yes, there is. if element in tuple)
I played further with the function. I am still not able to get it to work, but I am feeling a little more comfortable with the idea.
9/12/2015
After reading little pieces about functions throughout the week and playing with tutorials, I have a function that will take a number of bytes as an argument, read those bytes, unpack into little-endian as hexadecimal, then return the resultant tuple as an integer. Right now, this function only handles two bytes which is what we need for the jumps. Later, I'll try to figure out what to do for strings which requires a different unpack procedure. It may work better as its own function or part of the same. I don't know.
The function is called inside a while loop that indefinitely executes until the encountered two-byte value is FFFF.
It's working... more or less.