# Path: tools/imports/shockabsorber/loader/util.py
# class SeqBuffer:  #------------------------------
#     def __init__(self,src, is_little_endian=False):
#     def readTag(self):
#     def unpack(self,be_fmt, le_fmt=None):
#     def unpackString8(self):
#     def unpackVarint(self):
#     def readBytes(self, len):
#     def tell(self):
#     def seek(self,new_offset):
#     def pread_from_to(self, frm, to):
#     def bytes_left(self):
#     def peek_bytes_left(self):
#     def at_eof(self):
# def rev(s):
# def half_expect(actual_value, expected_value, name):


from .util import SeqBuffer, rev, half_expect, int_struct, double_struct
from shockabsorber.model.scripts import ScriptNames
#--------------------------------------------------

def parse_lscr_section(snr, blob, names):
    print "DB| Lscr section #%d:" % (snr,)
    buf = SeqBuffer(blob)
    [v1,v2,totalLength,totalLength2,
     header_length, script_id, count2] = buf.unpack('>4i3H')
    [v3,v4,v5,v6,v7,v8,v9,v10,hvec_length] = buf.unpack('>6ihhh')
    [hvec_offset, flags56, props_count, props_offset, globs_count, globs_offset] = buf.unpack('>iiHiHi')
    [handler_count, handler_offset] = buf.unpack('>Hi')
    [literal_count, literal_offsets_offset, literals_length, literals_offset] = buf.unpack('>Hiii')
    half_expect(v1, 0, "Lscr.v1")
    half_expect(v2, 0, "Lscr.v2")
    half_expect(totalLength2, totalLength, "Lscr.totalLength2")
    half_expect(header_length, 92, "Lscr.header_length")
    #half_expect(v1, 0, "Lscr.v1")
    print "DB| Lscr extras: %s" % ([[v1,v2,totalLength,totalLength2],
                                    [header_length,script_id,count2],
                                    [v3,v4,v5,v6,v7,v8,v9,v10],
                                    [flags56]],)
    print "DB| Lscr offsets: %s" % ([header_length, props_offset, globs_offset, handler_offset, literal_offsets_offset, literals_offset, hvec_offset, totalLength, len(buf.buf)],)
    print "DB|   parts: %s" % ([("props",props_count,props_offset,props_offset+2*props_count),
                                ("globs",globs_count,globs_offset,globs_offset+2*globs_count),
                                ("handlers",handler_count, handler_offset, handler_offset+46*handler_count),
                                ("literal-offsets", literal_count, literal_offsets_offset, literal_offsets_offset+8*literal_count),
                                ("literals", literal_count, literals_length, literals_offset, literals_offset + literals_length),
                                ("hvector", hvec_length, hvec_offset, hvec_offset+2*hvec_length),
                                ("end", totalLength)],)

    ## String offsets table:

# Path: tools/imports/shockabsorber/loader/util.py
# class SeqBuffer:  #------------------------------
#     def __init__(self,src, is_little_endian=False):
#     def readTag(self):
#     def unpack(self,be_fmt, le_fmt=None):
#     def unpackString8(self):
#     def unpackVarint(self):
#     def readBytes(self, len):
#     def tell(self):
#     def seek(self,new_offset):
#     def pread_from_to(self, frm, to):
#     def bytes_left(self):
#     def peek_bytes_left(self):
#     def at_eof(self):
# def rev(s):
# def half_expect(actual_value, expected_value, name):


# Path: tools/imports/shockabsorber/loader/script_parser.py
from .util import SeqBuffer, rev, half_expect, int_struct, double_struct
from shockabsorber.model.scripts import ScriptNames

#--------------------------------------------------

def parse_lscr_section(snr, blob, names):
    print "DB| Lscr section #%d:" % (snr,)
    buf = SeqBuffer(blob)
    [v1,v2,totalLength,totalLength2,
     header_length, script_id, count2] = buf.unpack('>4i3H')
    [v3,v4,v5,v6,v7,v8,v9,v10,hvec_length] = buf.unpack('>6ihhh')
    [hvec_offset, flags56, props_count, props_offset, globs_count, globs_offset] = buf.unpack('>iiHiHi')
    [handler_count, handler_offset] = buf.unpack('>Hi')
    [literal_count, literal_offsets_offset, literals_length, literals_offset] = buf.unpack('>Hiii')
    half_expect(v1, 0, "Lscr.v1")
    half_expect(v2, 0, "Lscr.v2")
    half_expect(totalLength2, totalLength, "Lscr.totalLength2")
    half_expect(header_length, 92, "Lscr.header_length")
    #half_expect(v1, 0, "Lscr.v1")
    print "DB| Lscr extras: %s" % ([[v1,v2,totalLength,totalLength2],
                                    [header_length,script_id,count2],
                                    [v3,v4,v5,v6,v7,v8,v9,v10],
                                    [flags56]],)
    print "DB| Lscr offsets: %s" % ([header_length, props_offset, globs_offset, handler_offset, literal_offsets_offset, literals_offset, hvec_offset, totalLength, len(buf.buf)],)
    print "DB|   parts: %s" % ([("props",props_count,props_offset,props_offset+2*props_count),
                                ("globs",globs_count,globs_offset,globs_offset+2*globs_count),
                                ("handlers",handler_count, handler_offset, handler_offset+46*handler_count),
                                ("literal-offsets", literal_count, literal_offsets_offset, literal_offsets_offset+8*literal_count),
                                ("literals", literal_count, literals_length, literals_offset, literals_offset + literals_length),
                                ("hvector", hvec_length, hvec_offset, hvec_offset+2*hvec_length),
                                ("end", totalLength)],)

    ## String offsets table:

    buf5 = SeqBuffer(buf.buf[literal_offsets_offset : literals_offset])