def read_ILS_section_into(ils_data, entries_by_nr, dest):
    buf = SeqBuffer(ils_data)
    while not buf.at_eof():
        nr = buf.unpackVarint()
        section = entries_by_nr[nr]
        data = buf.readBytes(section.size)
        section.set_bytes(data)
def read_ILS_section_into(ils_data, entries_by_nr, dest):
    buf = SeqBuffer(ils_data)
    while not buf.at_eof():
        nr = buf.unpackVarint()
        section = entries_by_nr[nr]
        data = buf.readBytes(section.size)
        section.set_bytes(data)
def parse_abmp_section(blob, file):
    buf = SeqBuffer(blob)
    v1 = buf.unpackVarint()
    v2 = buf.unpackVarint()
    section_count = buf.unpackVarint()
    print("ABMP header: %s" % [v1,v2, section_count])

    #w1sum=0; w2sum=0; w3sum=0; w4sum=0
    csum=0; usum=0
    sections = []
    for i in range(section_count):
        id = buf.unpackVarint()
        offset = buf.unpackVarint() # offset
        comp_size = buf.unpackVarint() # size in file
        decomp_size = buf.unpackVarint() # size, decompressed
        repr_mode = buf.unpackVarint()
        [tag] = buf.unpack('<4s')
        tag = rev(tag)
        print("ABMP entry: %s" % ([id, tag,
                                   offset, comp_size, decomp_size,
                                   repr_mode]))
        sections.append(ABMPEntry(id, tag, comp_size, offset, repr_mode))

    print("Bytes left in ABMP section: %d" % buf.bytes_left())
    # print "Sums: %s" % [csum, usum]
    return sections
def parse_abmp_section(blob, file):
    buf = SeqBuffer(blob)
    v1 = buf.unpackVarint()
    v2 = buf.unpackVarint()
    section_count = buf.unpackVarint()
    print("ABMP header: %s" % [v1,v2, section_count])

    #w1sum=0; w2sum=0; w3sum=0; w4sum=0
    csum=0; usum=0
    sections = []
    for i in range(section_count):
        id = buf.unpackVarint()
        offset = buf.unpackVarint() # offset
        comp_size = buf.unpackVarint() # size in file
        decomp_size = buf.unpackVarint() # size, decompressed
        repr_mode = buf.unpackVarint()
        [tag] = buf.unpack('<4s')
        tag = rev(tag)
        print("ABMP entry: %s" % ([id, tag,
                                   offset, comp_size, decomp_size,
                                   repr_mode]))
        sections.append(ABMPEntry(id, tag, comp_size, offset, repr_mode))

    print "Bytes left in ABMP section: %d" % buf.bytes_left()
    # print "Sums: %s" % [csum, usum]
    return sections
def create_section_map(f, loader_context):
    while True:
        xsectheader = f.read(4)
        if len(xsectheader) < 4: break
        [stag] = struct.unpack('<4s', xsectheader)
        stag = rev(stag)
        ssize = read_varint(f)
        print("stag=%s ssize=%d" % (stag, ssize))
        if ssize==0:
            break
        else:
            sect_data = f.read(ssize)
        if stag == "Fcdr" or stag == "FGEI":
            sect_data = zlib.decompress(sect_data)
            print("ssize decompressed=%d" % (len(sect_data)))
        elif stag == "ABMP":
            buf = SeqBuffer(sect_data)
            sect_data_null = buf.unpackVarint()
            sect_data_size = buf.unpackVarint()
            sect_data = zlib.decompress(buf.peek_bytes_left())
            del buf
            print("ssize decompressed=%d=%d" % (sect_data_size, len(sect_data)))
            abmp = parse_abmp_section(sect_data, f)
            print("DB| ABMP: %s" % abmp)
        if stag != "ABMP":
            print ("DB| %s -> %s" % (stag, sect_data))
    section_base_pos = f.tell()

    # entries_by_nr = {}
    # for e in abmp:
    #     entries_by_nr[e.nr] = e

    # Fetch the sections:
    sections = []
    sections_in_ils_by_nr = {}
    ils_section_bytes = None
    for e in abmp:
        snr = e.nr
        print("DB| section nr %s: %s" % (snr,e))
        if e.offset == -1:
            # Compressed, in ILS section.
            section = LateSectionImpl(snr,e.tag,e.size)
            sections_in_ils_by_nr[snr] = section
        elif e.repr_mode==0:
            section = ZSectionImpl(snr, e.tag, e.size,
                                   section_base_pos + e.offset, f)
        elif e.repr_mode==1:
            section = UncompSectionImpl(snr, e.tag, e.size, e.offset, f)
            # f.seek(section_base_pos + e.offset)
        
            # raw_sdata = f.read(e.size)
            # if e.repr_mode==0:
            #     sdata = zlib.decompress(raw_sdata)
            # elif e.repr_mode==1:
            #     sdata = raw_sdata
        else:
            raise "unknown repr_mode: %d" % e.repr_mode
            # entries_by_nr[snr] = (e.tag,sdata)
        sections.append(section)
        if e.tag=="ILS ":
            ils_section_bytes = section.bytes()

    if sections_in_ils_by_nr:
        read_ILS_section_into(ils_section_bytes,
                              sections_in_ils_by_nr,
                              sections)

    # print "Sections=%s" % (sections.keys())
    print("Sections:")
    for e in sections:
        print("  %d: %s" % (e.nr, e))

    # Debug:
    # for snr in sections:
    #     (tag,data) = sections[snr]
    #     if tag=="Lscr" or tag=="LctX":# or tag=="Lnam":
    #         print "DB| %d: %s->%s" % (snr, tag, data)
    #     if tag=="Lnam":
    #         print "DB| %d: %s->%s" % (snr, tag, LnamSection.parse(data))

    return SectionMapImpl(sections)