# Path: x86inst.py
# class OpcodeTooShort(Exception):pass
# class OpcodeNeedsModRM(Exception):pass # for /d info and SIB calculation
# class x86instError(Exception):pass
# class OpcodeDict(dict):
# class MnemonicDict(dict):
# class ModRM:
# class instruction:
# class instructionInstance:
#     def __getitem__(self,key):
#     def __setitem__(self,key,value):
#     def GetOp(self,opcode,modRM=None):
#     def __setitem__(self,key,val):
# def longToBytes(long, bytes=4):
# def longToString(long, bytes=4):
# def longToBytesRepr(long,bytes=4):
#     def __init__(self,byte=None):
#     def LoadFromByte(self,byte):
#     def SaveToByte(self):
#     def HasSIB(self):
#     def RegOpString(self,typ):
#     def RMString(self,typ=None):
#     def GetDisplacementSize(self):
#     def __init__(self,opstr,inststr,desc):
#     def loadRBWD(self,plus,reg):
#     def setOpcodeAndFlags(self):
#     def setHasFlags(self):
#     def GetInstance(self):
#     def __str__(self):
#     def __init__(self,inst):
#     def GetSymbolPatchins(self,modrm=None):
#     def GetSuffixSize(self,modrm=None):
#     def GetInstructionSize(self):
#     def NextInstructionLoc(self):
#     def LoadData(self, data):
#     def DataText(self,data,size,skip=False):
#     def LoadConcreteValues(self, toks):
#     def OpDataAsString(self):
#     def OpText(self):
#             OS = self.OpcodeString
#             IS = self.InstructionString
#             ID = self.Description
#             OS = OS.replace(plus,plus[1:])
#             OS = OS.replace("%X" % self.Opcode[0], '%X' % (self.Opcode[0] + i))
#             IS = IS.replace(reg, regOpcode[reg][i])
# RELATIVE,DIRECT = range(1,3)
# 
# Path: x86PackUnpack.py
# def ulongToString(u): return struct.pack("<L", u)
# 
# Path: x86asm.py
# class x86asmError(Exception): pass
# class labelRef:
# class label:
# class labelDict(dict):
# class constDict(dict):
# class data:
# class codePackage:
# class procedure:
# class assembler:
# def possibleDefault(*toks):
# def possibleImmediateOrRelative(*toks):
# def possibleRegister(*toks):
# def possibleIndirect(*toks):
# def getProperLookup(*toks):
# def findBestMatchTokens(toks):
# def findBestMatch(s):
# def printBestMatch(s):
#     def __init__(self, name):
#     def __init__(self, name,typ=0):
#     def __setitem__(self,key,val):
#     def __setitem__(self,key,val):
#     def __init__(self,name,dat,size=0):
#     def __init__(self):
#     def __init__(self,name, typ=CDECL):
#     def AddArg(self,name,bytes=4):
#     def AddLocal(self,name,bytes=4):
#     def LookupArg(self,name):
#     def LookupLocal(self,name):
#     def LookupVar(self, name):
#     def EmitProcStartCode(self, a):
#     def EmitProcEndCode(self, a):
#     def __init__(self):
#     def registerLabel(self,lbl):
#     def freezeProc(self):
#     def AddInstruction(self,inst):
#     def AI(self,inst):
#     def AddInstructionLabel(self,name,typ=0):
#     def AIL(self,name):
#     def AddData(self,name,dat):
#     def ADStr(self,name,dat):
#     def AddProcedure(self,name,typ=STDCALL):
#     def AP(self,name,typ=STDCALL):
#     def AddArgument(self,name,size=4):
#     def AA(self,name,size=4):
#     def AddLocal(self,name,size=4):
#     def EndProc(self):
#     def EP(self):
#     def AddConstant(self,name,val):
#     def AC(self,name,val):
#     def getVarNameAndSize(t,s):
#     def PROC(self,params):
#     def ARG(self,params):
#     def LOCAL(self,params):
#     def ENDPROC(self,params):
#     def CALL(self,params):
#     def CHARS(self,params):
#     def COMMENT(self,params):
#     def dispatchDirective(self,s):
#     def dispatchStatement(self,s):
#     def DispatchString(self,s):
#     def __call__(self,s):
#     def pass1(self):
#     def Compile(self):
# def _log_header(text):
# def codePackageFromFile(fil,constCallback=None):
# def manglePythonNames(cp):
# STDCALL, CDECL, PYTHON = range(1,4)


from x86inst import RELATIVE,DIRECT
from x86PackUnpack import ulongToString
from x86asm import PYTHON
    from sys import dllhandle
import pyasm.excmem as excmem
import logging, sys
    import win32api, pywintypes
        try:
            addr = win32api.GetProcAddress(dllhandle,funcName)
        except pywintypes.error:
            raise RuntimeError("Unable to resolve external symbol '%s'" % funcName)
        return addr
elif sys.platform in ('linux2'):
    def runtimeResolve(funcName):
        return excmem.GetSymbolAddress(funcName)
else:
    raise RuntimeError("Don't know how to resolve external symbols for platform '%s'" % sys.platform)


class CpToMemory:
    def __init__(self,cp):
        self.cp = cp
        self.symbols = {}
        self.resolvedCode = ''
        
    def LookupAddress(self,sym):
        if self.symbols.has_key(sym):
            return self.symbols[sym]
        else: #try runtime resolution, currently windows specific
            funcaddress = runtimeResolve(sym)
            self.symbols[sym] = funcaddress
            return funcaddress

    def BindPythonFunctions(self,glb=None,bindFunction=excmem.BindFunctionAddress):
        if glb is None:
            glb = globals()
        for proc in self.cp.CodeSymbols:

# Path: x86inst.py
# class OpcodeTooShort(Exception):pass
# class OpcodeNeedsModRM(Exception):pass # for /d info and SIB calculation
# class x86instError(Exception):pass
# class OpcodeDict(dict):
# class MnemonicDict(dict):
# class ModRM:
# class instruction:
# class instructionInstance:
#     def __getitem__(self,key):
#     def __setitem__(self,key,value):
#     def GetOp(self,opcode,modRM=None):
#     def __setitem__(self,key,val):
# def longToBytes(long, bytes=4):
# def longToString(long, bytes=4):
# def longToBytesRepr(long,bytes=4):
#     def __init__(self,byte=None):
#     def LoadFromByte(self,byte):
#     def SaveToByte(self):
#     def HasSIB(self):
#     def RegOpString(self,typ):
#     def RMString(self,typ=None):
#     def GetDisplacementSize(self):
#     def __init__(self,opstr,inststr,desc):
#     def loadRBWD(self,plus,reg):
#     def setOpcodeAndFlags(self):
#     def setHasFlags(self):
#     def GetInstance(self):
#     def __str__(self):
#     def __init__(self,inst):
#     def GetSymbolPatchins(self,modrm=None):
#     def GetSuffixSize(self,modrm=None):
#     def GetInstructionSize(self):
#     def NextInstructionLoc(self):
#     def LoadData(self, data):
#     def DataText(self,data,size,skip=False):
#     def LoadConcreteValues(self, toks):
#     def OpDataAsString(self):
#     def OpText(self):
#             OS = self.OpcodeString
#             IS = self.InstructionString
#             ID = self.Description
#             OS = OS.replace(plus,plus[1:])
#             OS = OS.replace("%X" % self.Opcode[0], '%X' % (self.Opcode[0] + i))
#             IS = IS.replace(reg, regOpcode[reg][i])
# RELATIVE,DIRECT = range(1,3)
# 
# Path: x86PackUnpack.py
# def ulongToString(u): return struct.pack("<L", u)
# 
# Path: x86asm.py
# class x86asmError(Exception): pass
# class labelRef:
# class label:
# class labelDict(dict):
# class constDict(dict):
# class data:
# class codePackage:
# class procedure:
# class assembler:
# def possibleDefault(*toks):
# def possibleImmediateOrRelative(*toks):
# def possibleRegister(*toks):
# def possibleIndirect(*toks):
# def getProperLookup(*toks):
# def findBestMatchTokens(toks):
# def findBestMatch(s):
# def printBestMatch(s):
#     def __init__(self, name):
#     def __init__(self, name,typ=0):
#     def __setitem__(self,key,val):
#     def __setitem__(self,key,val):
#     def __init__(self,name,dat,size=0):
#     def __init__(self):
#     def __init__(self,name, typ=CDECL):
#     def AddArg(self,name,bytes=4):
#     def AddLocal(self,name,bytes=4):
#     def LookupArg(self,name):
#     def LookupLocal(self,name):
#     def LookupVar(self, name):
#     def EmitProcStartCode(self, a):
#     def EmitProcEndCode(self, a):
#     def __init__(self):
#     def registerLabel(self,lbl):
#     def freezeProc(self):
#     def AddInstruction(self,inst):
#     def AI(self,inst):
#     def AddInstructionLabel(self,name,typ=0):
#     def AIL(self,name):
#     def AddData(self,name,dat):
#     def ADStr(self,name,dat):
#     def AddProcedure(self,name,typ=STDCALL):
#     def AP(self,name,typ=STDCALL):
#     def AddArgument(self,name,size=4):
#     def AA(self,name,size=4):
#     def AddLocal(self,name,size=4):
#     def EndProc(self):
#     def EP(self):
#     def AddConstant(self,name,val):
#     def AC(self,name,val):
#     def getVarNameAndSize(t,s):
#     def PROC(self,params):
#     def ARG(self,params):
#     def LOCAL(self,params):
#     def ENDPROC(self,params):
#     def CALL(self,params):
#     def CHARS(self,params):
#     def COMMENT(self,params):
#     def dispatchDirective(self,s):
#     def dispatchStatement(self,s):
#     def DispatchString(self,s):
#     def __call__(self,s):
#     def pass1(self):
#     def Compile(self):
# def _log_header(text):
# def codePackageFromFile(fil,constCallback=None):
# def manglePythonNames(cp):
# STDCALL, CDECL, PYTHON = range(1,4)


# Path: x86cpToMemory.py
from x86inst import RELATIVE,DIRECT
from x86PackUnpack import ulongToString
from x86asm import PYTHON
    from sys import dllhandle
import pyasm.excmem as excmem
import logging, sys
    import win32api, pywintypes

        try:
            addr = win32api.GetProcAddress(dllhandle,funcName)
        except pywintypes.error:
            raise RuntimeError("Unable to resolve external symbol '%s'" % funcName)
        return addr
elif sys.platform in ('linux2'):
    def runtimeResolve(funcName):
        return excmem.GetSymbolAddress(funcName)
else:
    raise RuntimeError("Don't know how to resolve external symbols for platform '%s'" % sys.platform)


class CpToMemory:
    def __init__(self,cp):
        self.cp = cp
        self.symbols = {}
        self.resolvedCode = ''
        
    def LookupAddress(self,sym):
        if self.symbols.has_key(sym):
            return self.symbols[sym]
        else: #try runtime resolution, currently windows specific
            funcaddress = runtimeResolve(sym)
            self.symbols[sym] = funcaddress
            return funcaddress

    def BindPythonFunctions(self,glb=None,bindFunction=excmem.BindFunctionAddress):
        if glb is None:
            glb = globals()
        for proc in self.cp.CodeSymbols:

            if proc[2] == PYTHON: