def parse(buf, *args): [type_length] = buf.unpack('>I') med_type = buf.readBytes(type_length) [rest_length] = buf.unpack('>I') info = buf.readBytes(rest_length) buf = SeqBuffer(info) if rest_length >= 4 and buf.readTag( ) == b'FLSH' and med_type == 'vectorShape': [sz2] = buf.unpack('>I') half_expect(sz2, rest_length, "XmedCastType.sz2") misc = buf.unpack('>24i') [npoints] = buf.unpack('>I') misc2 = buf.unpack('>35i') [proplen] = buf.unpack('>I') propname = buf.readBytes(proplen) points = [] for i in range(npoints): if i: [vx] = buf.unpack('>i') half_expect(vx, -0x80000000, 'XmedCastType.vx') pdesc = buf.unpack('>6i') points.append(pdesc) [proplen] = buf.unpack('>I') propname = buf.readBytes(proplen) half_expect(buf.peek_bytes_left(), b'', 'XmedCastType.trailer') print( "DB| XmedCastType.parse: misc=%r npoints=%r misc2=%r, points=%r" % (misc, npoints, misc2, points)) info = (misc, misc2, points) return XmedCastType(med_type, info, *args)
def read_bytes(self): file = self.file file.seek(self.offset) xheader = file.read(8) buf = SeqBuffer(xheader, self.loader_context.is_little_endian) tag = buf.readTag() #[dummy_size] = buf.unpack('>i', '<i') if tag != self.tag: raise Exception("section header is actually %s, not %s as expected" % (tag, self.tag)) return file.read(self.size)
def read_bytes(self): file = self.file file.seek(self.offset) xheader = file.read(8) buf = SeqBuffer(xheader, self.loader_context.is_little_endian) tag = buf.readTag() #[dummy_size] = buf.unpack('>i', '<i') if tag != self.tag: raise Exception( "section header is actually %s, not %s as expected" % (tag, self.tag)) return file.read(self.size)
def find_and_read_section(f, tag_to_find, loader_context): while True: xheader = f.read(8) buf = SeqBuffer(xheader, loader_context.is_little_endian) tag = buf.readTag() [size] = buf.unpack('>i', '<i') print(" tag=%s" % tag) if tag==tag_to_find: blob = f.read(size) return parse_mmap_section(blob, f, loader_context) else: f.seek(size, 1)
def find_and_read_section(f, tag_to_find, loader_context): while True: xheader = f.read(8) buf = SeqBuffer(xheader, loader_context.is_little_endian) tag = buf.readTag() [size] = buf.unpack('>i', '<i') print(" tag=%s" % tag) if tag == tag_to_find: blob = f.read(size) return parse_mmap_section(blob, f, loader_context) else: f.seek(size, 1)
def parse_mmap_section(blob, file, loader_context): buf = SeqBuffer(blob, loader_context.is_little_endian) [v1,v2,nElems,nUsed,junkPtr,v3,freePtr] = buf.unpack('>HHiiiii', '<HHiiiii') print("mmap header: %s" % [v1,v2,nElems,nUsed,junkPtr,v3,freePtr]) sections = [] for i in range(nUsed): tag = buf.readTag() [size, offset, w1,w2, link] = buf.unpack('>IIhhi', '<IIhhi') #print("mmap entry: %s" % [tag, size, offset, w1,w2, link]) if tag=="free" or tag=="junk": section = NullSection(tag) else: section = SectionImpl(tag, size, offset, file, loader_context) sections.append(section) return SectionMap(sections)
def parse_mmap_section(blob, file, loader_context): buf = SeqBuffer(blob, loader_context.is_little_endian) [v1, v2, nElems, nUsed, junkPtr, v3, freePtr] = buf.unpack('>HHiiiii', '<HHiiiii') print("mmap header: %s" % [v1, v2, nElems, nUsed, junkPtr, v3, freePtr]) sections = [] for i in range(nUsed): tag = buf.readTag() [size, offset, w1, w2, link] = buf.unpack('>IIhhi', '<IIhhi') #print("mmap entry: %s" % [tag, size, offset, w1,w2, link]) if tag == b"free" or tag == b"junk": section = NullSection(tag) else: section = SectionImpl(tag, size, offset, file, loader_context) sections.append(section) return SectionMap(sections)
def parse_assoc_table(blob, loader_context): """ Takes a 'KEY*' section and returns an AssociationTable. """ buf = SeqBuffer(blob, loader_context.is_little_endian) [v1,v2,nElems,nValid] = buf.unpack('>HHii', '<HHii') print("KEY* header: %s" % [v1,v2,nElems,nValid]) # v1 = table start offset, v2 = table entry size? atable = AssociationTable() for i in range(nValid): [owned_section_id, composite_id] = buf.unpack('>ii', '<ii') tag = buf.readTag() castlib_assoc_id = composite_id >> 16 owner_section_id = composite_id & 0xFFFF print "DB| KEY* entry #%d: %s" % (i, [tag, owned_section_id, castlib_assoc_id, owner_section_id]) if owner_section_id == 1024: atable.add_library_section(castlib_assoc_id, owned_section_id, tag) else: atable.add_cast_media(owner_section_id, owned_section_id, tag) return atable
def parse_assoc_table(blob, loader_context): """ Takes a 'KEY*' section and returns an AssociationTable. """ buf = SeqBuffer(blob, loader_context.is_little_endian) [v1, v2, nElems, nValid] = buf.unpack('>HHii', '<HHii') print("KEY* header: %s" % [v1, v2, nElems, nValid]) # v1 = table start offset, v2 = table entry size? atable = AssociationTable() for i in range(nValid): [owned_section_id, composite_id] = buf.unpack('>ii', '<ii') tag = buf.readTag() castlib_assoc_id = composite_id >> 16 owner_section_id = composite_id & 0xFFFF print("DB| KEY* entry #%d: %s" % (i, [ bytes(tag), owned_section_id, castlib_assoc_id, owner_section_id ])) if owner_section_id == 1024: atable.add_library_section(castlib_assoc_id, owned_section_id, bytes(tag)) else: atable.add_cast_media(owner_section_id, owned_section_id, bytes(tag)) return atable