Make sure my interpretation of this Python code snippet is correct

I am analyzing a set of python scripts and stumbled upon this snippet. I'm not sure my interpretation is correct, since I have not come across similar C or Java code, and I do not know Python.

for i in xrange(self.num_sections):
        offset, a1,a2,a3,a4 = struct.unpack('>LBBBB', self.data_file[78+i*8:78+i*8+8])
        flags, val = a1, a2<<16|a3<<8|a4
        self.sections.append( (offset, flags, val) )

My interpretation is this:

for each item in num_sections
  convert the data_file range into a big-endian unsigned long, and 4 unsigned char
  insert unpacked values into offset, a1, a2, a3 and a4 variables

  set flags to = a1
  set val to a2 shifted left 16 bits then OR'd with a3 shifted right 8 bits 
  then OR'd with a4

Essentially, I think the original decompression operation extracts 8 bytes, dumps 4 of them as unsigned long, and then adds the rest in sequential order to the a * variables.

+5
source share
1 answer

Yes, your interpretation is correct.

0
source

All Articles