Reading a binary file into a struct

前端 未结 4 636
面向向阳花
面向向阳花 2020-12-16 04:58

I have a binary file with a known format/structure.

How do I read all the binary data in to an array of the structure?

Something like (in pseudo code)

<
4条回答
  •  一个人的身影
    2020-12-16 05:56

    Add comments

    import struct 
    

    First just read the binary into an array

    mbr = file('mbrcontent', 'rb').read() 
    

    So you can just fetch some piece of the the array

    partition_table = mbr[446:510] 
    

    and then unpack it as an integer

    signature = struct.unpack('

    a more complex example

    little_endian = (signature == 0xaa55) # should be True 
    print "Little endian:", little_endian 
    PART_FMT = (little_endian and '<' or '>') + ( 
    "B" # status (0x80 = bootable (active), 0x00 = non-bootable) 
    # CHS of first block 
    "B" # Head 
    "B" # Sector is in bits 5; bits 9 of cylinder are in bits 7-6 
    "B" # bits 7-0 of cylinder 
    "B" # partition type 
    # CHS of last block 
    "B" # Head 
    "B" # Sector is in bits 5; bits 9 of cylinder are in bits 7-6 
    "B" # bits 7-0 of cylinder 
    "L" # LBA of first sector in the partition 
    "L" # number of blocks in partition, in little-endian format 
    ) 
    
    PART_SIZE = 16 
    fmt_size = struct.calcsize(PART_FMT) 
    # sanity check expectations 
    assert fmt_size == PART_SIZE, "Partition format string is %i bytes, not %i" % (fmt_size, PART_SIZE) 
    
    def cyl_sector(sector_cyl, cylinder7_0): 
        sector = sector_cyl & 0x1F # bits 5-0 
    
        # bits 7-6 of sector_cyl contain bits 9-8 of the cylinder 
        cyl_high = (sector_cyl >> 5) & 0x03 
        cyl = (cyl_high << 8) | cylinder7_0 
        return sector, cyl 
    
    #I have corrected the indentation, but the change is refused because less than 6 characters, so I am adding this useful comment.
    for partition in range(4): 
        print "Partition #%i" % partition, 
        offset = PART_SIZE * partition 
        (status, start_head, start_sector_cyl, start_cyl7_0, part_type, end_head, end_sector_cyl, end_cyl7_0, 
        lba, blocks ) = struct.unpack( PART_FMT,partition_table[offset:offset + PART_SIZE]) 
        if status == 0x80: 
            print "Bootable", 
        elif status: 
            print "Unknown status [%s]" % hex(status), 
            print "Type=0x%x" % part_type 
            start = (start_head,) + cyl_sector(start_sector_cyl, start_cyl7_0) 
            end = (end_head,) + cyl_sector(end_sector_cyl, end_cyl7_0) 
            print " (Start: Heads:%i\tCyl:%i\tSect:%i)" % start 
            print " (End: Heads:%i\tCyl:%i\tSect:%i)" % end 
            print " LBA:", lba 
            print " Blocks:", blocks 
    

提交回复
热议问题