#! /usr/bin/python3

import os

f = bytes(open("out.bin","rb").read())

#print(f)
try:
    os.mkdir("entries")
except OSError as exc:
    pass
n_entries = int.from_bytes(f[0:4],byteorder="little")
i=0;
last_off = 0
for i in range(n_entries):
    curr = {}
    curr["offset"] = int.from_bytes(f[4+4*i:8+4*i],byteorder="little")
    if(curr["offset"] == 0):
        go=0;
    curr["sz"]     = int.from_bytes(f[curr["offset"]:curr["offset"]+4],byteorder="little")
    spe= ''
    comp_sz = (int.from_bytes(f[4+4*(i+1):8+4*(i+1)],byteorder="little") - curr["offset"] -4)
    if( curr["sz"] != comp_sz):
        spe = "IMAGE"
        curr["sz"] = 256
    print("%d : 0x%08x (%d)\t\tsz:0x%08x (%d) %s" % (i,curr["offset"],curr["offset"],curr["sz"] ,curr["sz"] , spe ))
    i+=1
    last_off = curr["offset"]
    o = open("entries/ent_0x%08x.bin" % (curr["offset"]),"wb")
    o.write(f[ curr["offset"]:curr["offset"]+curr["sz"]   ] )
    o.close()
