Added md5 hashing of code alone and full driver, added DCB length, and stored link to next driver

This commit is contained in:
Paul Hagstrom 2016-01-07 18:08:49 -05:00
parent 4c8145f8a3
commit 3ef242f49f
1 changed files with 25 additions and 19 deletions

View File

@ -45,13 +45,6 @@ def nibblize(byte, **options):
if options.get("direction") == 'low': if options.get("direction") == 'low':
return str(int(hex(byte & 0x0F), 0)) return str(int(hex(byte & 0x0F), 0))
# this function takes bytes as a parameter, reads those bytes
# in the file, and hashes them.
def md5hash(bytes):
SOS = SOSfile.read(bytes)
hash = hashlib.md5(SOS)
return hash.hexdigest()
# dictionary for device types and sub types. # dictionary for device types and sub types.
dev_types ={273: 'Character Device, Write-Only, Formatter', dev_types ={273: 'Character Device, Write-Only, Formatter',
321: 'Character Device, Write-Only, RS232 Printer', 321: 'Character Device, Write-Only, RS232 Printer',
@ -131,8 +124,8 @@ for i in range(0,len(drivers_list)):
drivers_list[i]['comment_txt'] = 'None' drivers_list[i]['comment_txt'] = 'None'
# these two bytes are the intermediate offset value used to jump # these two bytes are the intermediate offset value used to jump
# to the next major driver. We skip here while going through the DIB. # to the next major driver. This is useful for computing the md5 hash.
SOSfile.seek(2,1) drivers_list[i]['next_driver'] = readUnpack(2, type = 'b')
# Officially, the link pointer is the beginning of the DIB. # Officially, the link pointer is the beginning of the DIB.
# comments are optional. We log dib_start to indicate where the # comments are optional. We log dib_start to indicate where the
@ -246,14 +239,25 @@ for i in range(0,len(drivers_list)):
else: else:
drivers_list[i]['version'] = V + '.' + v0 + v1 drivers_list[i]['version'] = V + '.' + v0 + v1
# calculate distance from beginning of current driver to # device configuration block length
# next driver. reposition pointer to beginning of current dcb_length = readUnpack(2, type = 'b')
# driver. send number of bytes to read to hashing function drivers_list[i]['dcb_length'] = dcb_length
## DISABLED FOR DEBUGGING ## EXPECT BLANK FIELDS IN the CSV
#bytes_to_read = drivers_list[i]['comment_start'] + \ # calculate an md5 hash of the entire driver and of just the code
#drivers_list[+1]['comment_start'] - 4 # portion
#SOSfile.seek(drivers_list[i]['comment_start'] + 4,0) SOSfile.seek(drivers_list[i]['comment_start'], 0)
#drivers_list[i]['md5'] = md5hash(bytes_to_read) # configuration bytes include the comment and parameters that can be changed in SCP
config_bytes = SOSfile.read(4 + drivers_list[i]['comment_len'] + drivers_list[i]['entry'])
# code bytes contain the region between the entry point and the next driver
code_bytes = SOSfile.read(drivers_list[i]['next_driver'] - drivers_list[i]['entry'])
# Hash just the code portion
code_md5 = hashlib.md5(code_bytes)
# Hash the whole driver, which will include both code and parameters
driver_md5 = hashlib.md5(config_bytes)
driver_md5.update(code_bytes)
# Store the resulting hash digest hex strings
drivers_list[i]['driver_md5'] = driver_md5.hexdigest()
drivers_list[i]['code_md5'] = code_md5.hexdigest()
# here we run a new loop to determine how many other DIBs exist # here we run a new loop to determine how many other DIBs exist
# under a major driver. This is primarily for drivers that are designed # under a major driver. This is primarily for drivers that are designed
@ -292,7 +296,7 @@ if exists == False:
csvout = open(output_csv, 'w') csvout = open(output_csv, 'w')
csvout.write('SOS_DRIVER_FILE,comment_start,comment_len,comment_txt,\ csvout.write('SOS_DRIVER_FILE,comment_start,comment_len,comment_txt,\
dib_start,link_ptr,entry,name_len,name,flag,slot_num,num_devices,unit,\ dib_start,link_ptr,entry,name_len,name,flag,slot_num,num_devices,unit,\
dev_type,block_num,mfg,version,md5\n') dev_type,block_num,mfg,version,dcb_length,driver_md5,code_md5\n')
else: else:
csvout = open(output_csv, 'a') csvout = open(output_csv, 'a')
@ -314,7 +318,9 @@ for i in range(0,len(drivers_list)):
str(drivers_list[i]['block_num']) + ',' + \ str(drivers_list[i]['block_num']) + ',' + \
drivers_list[i]['mfg'] + ',' + \ drivers_list[i]['mfg'] + ',' + \
str(drivers_list[i]['version']) + ',' + \ str(drivers_list[i]['version']) + ',' + \
drivers_list[i]['md5'] str(drivers_list[i]['dcb_length']) + ',' + \
drivers_list[i]['driver_md5'] + ',' + \
drivers_list[i]['code_md5']
) )
csvout.write('\n') csvout.write('\n')
csvout.close() csvout.close()