# Path: krakatau-lib/src/main/resources/Lib/Krakatau/binUnpacker.py
# class binUnpacker(object):
#     def __init__(self, data="", fileName=""):
#         if fileName:
#             self.bytes = open(fileName,'rb').read()
#         else:
#             self.bytes = data
#         self.off = 0
# 
#     def get(self, fmt, forceTuple=False, peek=False):
#         val = struct.unpack_from(fmt, self.bytes, self.off)
# 
#         if not peek:
#             self.off += struct.calcsize(fmt)
#         if not forceTuple and len(val) == 1:
#             val = val[0]
#         return val
# 
#     def getRaw(self, num):
#         val = self.bytes[self.off:self.off+num]
#         self.off += num
#         return val
# 
#     def size(self):
#         return len(self.bytes) - self.off
# 
# Path: krakatau-lib/src/main/resources/Lib/Krakatau/classfile.py
# class ClassFile(object):
#     flagVals = {'PUBLIC':0x0001,
#                 'FINAL':0x0010,
#                 'SUPER':0x0020,
#                 'INTERFACE':0x0200,
#                 'ABSTRACT':0x0400,
#                 'SYNTHETIC':0x1000,
#                 'ANNOTATION':0x2000,
#                 'ENUM':0x4000,
# 
#                 # These flags are only used for InnerClasses attributes
#                 'PRIVATE':0x0002,
#                 'PROTECTED':0x0004,
#                 'STATIC':0x0008,
#                 }
# 
#     def __init__(self, bytestream):
#         magic, minor, major = bytestream.get('>LHH')
#         assert(magic == 0xCAFEBABE)
#         self.version = major,minor
# 
#         const_pool_raw = get_cp_raw(bytestream)
#         flags, self.this, self.super = bytestream.get('>HHH')
# 
#         interface_count = bytestream.get('>H')
#         self.interfaces_raw = [bytestream.get('>H') for _ in range(interface_count)]
# 
#         self.fields_raw = get_fields_raw(bytestream)
#         self.methods_raw = get_methods_raw(bytestream)
# 
#         ic_indices = [i for i,x in enumerate(const_pool_raw) if x == (1, ("InnerClasses",))]
#         self.attributes_raw = get_attributes_raw(bytestream, ic_indices)
#         assert(bytestream.size() == 0)
# 
#         self.flags = frozenset(name for name,mask in ClassFile.flagVals.items() if (mask & flags))
#         self.cpool = constant_pool.ConstPool(const_pool_raw)
#         self.name = self.cpool.getArgsCheck('Class', self.this)
#         self.elementsLoaded = False
# 
#         self.env = self.supername = self.hierarchy = None
#         self.fields = self.methods = self.attributes = None
#         self.all_interfaces = None
# 
#     def loadSupers(self, env, name, subclasses):
#         self.env = env
#         assert(self.name == name)
# 
#         if self.super:
#             self.supername = self.cpool.getArgsCheck('Class', self.super)
#             superclass = self.env.getClass(self.supername, subclasses + (name,), partial=True)
#             self.hierarchy = superclass.hierarchy + (self.name,)
# 
#             # Now get all interfaces for this class (recursively)
#             interfaces = self.env.getInterfaces(self.supername)
#             if 'INTERFACE' in self.flags:
#                 interfaces |= {self.name}
#             for index in self.interfaces_raw:
#                 iname = self.cpool.getArgsCheck('Class', index)
#                 if iname not in interfaces:
#                     interfaces |= self.env.getInterfaces(iname)
#             self.all_interfaces = interfaces
#         else:
#             assert(name == 'java/lang/Object')
#             self.supername = None
#             self.hierarchy = (self.name,)
#             self.all_interfaces = frozenset()
# 
#     def loadElements(self, keepRaw=False):
#         if self.elementsLoaded:
#             return
#         self.fields = [field.Field(m, self, keepRaw) for m in self.fields_raw]
#         self.methods = [method.Method(m, self, keepRaw) for m in self.methods_raw]
#         self.attributes = fixAttributeNames(self.attributes_raw, self.cpool)
# 
#         self.fields_raw = self.methods_raw = None
#         if not keepRaw:
#             self.attributes_raw = None
#         self.elementsLoaded = True
# 
#     def getSuperclassHierarchy(self):
#         return self.hierarchy


import collections
import re
from . import instructions, tokenize, parse, assembler, codes
from ..binUnpacker import binUnpacker
from ..classfile import ClassFile
    # def add(s): print s
    add('.version {0[0]} {0[1]}'.format(cls.version))

    class_attributes = getAttributesDict(cls)
    if 'SourceFile' in class_attributes:
        bytes_ = binUnpacker(class_attributes['SourceFile'].pop()[1])
        val_ind = bytes_.get('>H')
        add('.source {}'.format(poolm.utfref(val_ind)))

    if 'BootstrapMethods' in class_attributes:
        bytes_ = binUnpacker(class_attributes['BootstrapMethods'].pop()[1])
        count = bytes_.get('>H')
        for _ in range(count):
            arg1, argc = bytes_.get('>HH')
            args = (arg1,) + bytes_.get('>'+'H'*argc, forceTuple=True)
            poolm.bootstrap_methods.append(args)

    cflags = ' '.join(map(str.lower, cls.flags))
    add('.class {} {}'.format(cflags, poolm.classref(cls.this)))
    add('.super {}'.format(poolm.classref(cls.super)))
    for ii in cls.interfaces_raw:
        add('.implements {}'.format(poolm.classref(ii)))

    for name in class_attributes:
        if name == "InnerClasses":
            assert(len(class_attributes[name]) == 1)
            for name_ind, (length, attr) in class_attributes[name]:
                disInnerClassesAttribute(length, binUnpacker(attr), add, poolm)
        else:
            for name_ind, attr in class_attributes[name]:

# Path: krakatau-lib/src/main/resources/Lib/Krakatau/binUnpacker.py
# class binUnpacker(object):
#     def __init__(self, data="", fileName=""):
#         if fileName:
#             self.bytes = open(fileName,'rb').read()
#         else:
#             self.bytes = data
#         self.off = 0
# 
#     def get(self, fmt, forceTuple=False, peek=False):
#         val = struct.unpack_from(fmt, self.bytes, self.off)
# 
#         if not peek:
#             self.off += struct.calcsize(fmt)
#         if not forceTuple and len(val) == 1:
#             val = val[0]
#         return val
# 
#     def getRaw(self, num):
#         val = self.bytes[self.off:self.off+num]
#         self.off += num
#         return val
# 
#     def size(self):
#         return len(self.bytes) - self.off
# 
# Path: krakatau-lib/src/main/resources/Lib/Krakatau/classfile.py
# class ClassFile(object):
#     flagVals = {'PUBLIC':0x0001,
#                 'FINAL':0x0010,
#                 'SUPER':0x0020,
#                 'INTERFACE':0x0200,
#                 'ABSTRACT':0x0400,
#                 'SYNTHETIC':0x1000,
#                 'ANNOTATION':0x2000,
#                 'ENUM':0x4000,
# 
#                 # These flags are only used for InnerClasses attributes
#                 'PRIVATE':0x0002,
#                 'PROTECTED':0x0004,
#                 'STATIC':0x0008,
#                 }
# 
#     def __init__(self, bytestream):
#         magic, minor, major = bytestream.get('>LHH')
#         assert(magic == 0xCAFEBABE)
#         self.version = major,minor
# 
#         const_pool_raw = get_cp_raw(bytestream)
#         flags, self.this, self.super = bytestream.get('>HHH')
# 
#         interface_count = bytestream.get('>H')
#         self.interfaces_raw = [bytestream.get('>H') for _ in range(interface_count)]
# 
#         self.fields_raw = get_fields_raw(bytestream)
#         self.methods_raw = get_methods_raw(bytestream)
# 
#         ic_indices = [i for i,x in enumerate(const_pool_raw) if x == (1, ("InnerClasses",))]
#         self.attributes_raw = get_attributes_raw(bytestream, ic_indices)
#         assert(bytestream.size() == 0)
# 
#         self.flags = frozenset(name for name,mask in ClassFile.flagVals.items() if (mask & flags))
#         self.cpool = constant_pool.ConstPool(const_pool_raw)
#         self.name = self.cpool.getArgsCheck('Class', self.this)
#         self.elementsLoaded = False
# 
#         self.env = self.supername = self.hierarchy = None
#         self.fields = self.methods = self.attributes = None
#         self.all_interfaces = None
# 
#     def loadSupers(self, env, name, subclasses):
#         self.env = env
#         assert(self.name == name)
# 
#         if self.super:
#             self.supername = self.cpool.getArgsCheck('Class', self.super)
#             superclass = self.env.getClass(self.supername, subclasses + (name,), partial=True)
#             self.hierarchy = superclass.hierarchy + (self.name,)
# 
#             # Now get all interfaces for this class (recursively)
#             interfaces = self.env.getInterfaces(self.supername)
#             if 'INTERFACE' in self.flags:
#                 interfaces |= {self.name}
#             for index in self.interfaces_raw:
#                 iname = self.cpool.getArgsCheck('Class', index)
#                 if iname not in interfaces:
#                     interfaces |= self.env.getInterfaces(iname)
#             self.all_interfaces = interfaces
#         else:
#             assert(name == 'java/lang/Object')
#             self.supername = None
#             self.hierarchy = (self.name,)
#             self.all_interfaces = frozenset()
# 
#     def loadElements(self, keepRaw=False):
#         if self.elementsLoaded:
#             return
#         self.fields = [field.Field(m, self, keepRaw) for m in self.fields_raw]
#         self.methods = [method.Method(m, self, keepRaw) for m in self.methods_raw]
#         self.attributes = fixAttributeNames(self.attributes_raw, self.cpool)
# 
#         self.fields_raw = self.methods_raw = None
#         if not keepRaw:
#             self.attributes_raw = None
#         self.elementsLoaded = True
# 
#     def getSuperclassHierarchy(self):
#         return self.hierarchy


# Path: krakatau-lib/src/main/resources/Lib/Krakatau/assembler/disassembler.py
import collections
import re
from . import instructions, tokenize, parse, assembler, codes
from ..binUnpacker import binUnpacker
from ..classfile import ClassFile

    # def add(s): print s
    add('.version {0[0]} {0[1]}'.format(cls.version))

    class_attributes = getAttributesDict(cls)
    if 'SourceFile' in class_attributes:
        bytes_ = binUnpacker(class_attributes['SourceFile'].pop()[1])
        val_ind = bytes_.get('>H')
        add('.source {}'.format(poolm.utfref(val_ind)))

    if 'BootstrapMethods' in class_attributes:
        bytes_ = binUnpacker(class_attributes['BootstrapMethods'].pop()[1])
        count = bytes_.get('>H')
        for _ in range(count):
            arg1, argc = bytes_.get('>HH')
            args = (arg1,) + bytes_.get('>'+'H'*argc, forceTuple=True)
            poolm.bootstrap_methods.append(args)

    cflags = ' '.join(map(str.lower, cls.flags))
    add('.class {} {}'.format(cflags, poolm.classref(cls.this)))
    add('.super {}'.format(poolm.classref(cls.super)))
    for ii in cls.interfaces_raw:
        add('.implements {}'.format(poolm.classref(ii)))

    for name in class_attributes:
        if name == "InnerClasses":
            assert(len(class_attributes[name]) == 1)
            for name_ind, (length, attr) in class_attributes[name]:
                disInnerClassesAttribute(length, binUnpacker(attr), add, poolm)
        else:
            for name_ind, attr in class_attributes[name]:

                disOtherClassAttribute(name_ind, name, binUnpacker(attr), add, poolm)