# Path: simple_com.py
# class COMInterface(ctypes.c_void_p):
#     _functions_ = {
#         "QueryInterface": ctypes.WINFUNCTYPE(HRESULT, ctypes.c_void_p, ctypes.c_void_p)(0, "QueryInterface"),
#         "AddRef": ctypes.WINFUNCTYPE(HRESULT)(1, "AddRef"),
#         "Release": ctypes.WINFUNCTYPE(HRESULT)(2, "Release")
#     }
# 
#     def __getattr__(self, name):
#         if name in self._functions_:
#             return functools.partial(self._functions_[name], self)
#         return super(COMInterface, self).__getattribute__(name)
# 
# class IDebugOutputCallbacksVtable(ComVtable):
#     _funcs_ = ComVtable._funcs_ + [("Output", [ctypes.HRESULT, ctypes.c_void_p, ctypes.c_ulong, ctypes.c_char_p], None)]
# 
# Path: driver_upgrade.py
# DU_MEMALLOC_IOCTL = 0x220027
# 
# DU_KCALL_IOCTL = 0x220037
# 
# DU_OUT_IOCTL = 0x220057
# 
# DU_IN_IOCTL = 0x220067
# 
# Path: dbgtype.py
# class DbgEngType(DbgEngTypeBase):
#     @property
#     def name(self):
#         return self.kdbg.get_type_name(self.module, self.typeid)
# 
#     @property
#     def is_array(self):
#         "Todo: Il doit bien y avoir un truc moins crade :D"
#         return self.name.endswith("[]")
# 
#     @property
#     def is_pointer(self):
#         "Todo: Il doit bien y avoir un truc moins crade :D"
#         return self.name.endswith("*")
# 
#     @property
#     def fields(self):
#         children = self.kdbg.get_childs_types(self.module, self.typeid)
#         return [DbgEngField(self.module, x, self.kdbg) for x in children.Types]
# 
#     @property
#     def fields_dict(self):
#         return {x.name: x for x in self.fields}
# 
#     @property
#     def number_elt(self):
#         return self.SymGetTypeInfo(TI_GET_COUNT)
# 
#     def __repr__(self):
#         return '<DbgEngType "{0}">'.format(self.name)
# 
#     def __call__(self, addr):
#         return get_mapped_type(self, addr)


import os
import struct
import itertools
import functools
import ctypes
import resource_emulation
import driver_upgrade
import windows
import windows.hooks
import windows.winproxy as winproxy
from os.path import realpath, dirname
from ctypes import byref, WINFUNCTYPE, HRESULT, WinError
from simple_com import COMInterface, IDebugOutputCallbacksVtable
from driver_upgrade import DU_MEMALLOC_IOCTL, DU_KCALL_IOCTL, DU_OUT_IOCTL, DU_IN_IOCTL
from windows.generated_def.winstructs import *
from dbgdef import *
from dbgtype import DbgEngType
        # https://msdn.microsoft.com/en-us/library/windows/hardware/ff554310%28v=vs.85%29.aspx
        "ReadPhysical": WINFUNCTYPE(HRESULT, ULONG64, PVOID, ULONG, PULONG)(10, "ReadPhysical"),
        # https://msdn.microsoft.com/en-us/library/windows/hardware/ff561432%28v=vs.85%29.aspx
        "WritePhysical": WINFUNCTYPE(HRESULT, ULONG64, PVOID, ULONG, PULONG)(11, "WritePhysical"),
        # https://msdn.microsoft.com/en-us/library/windows/hardware/ff553573%28v=vs.85%29.aspx
        "ReadIo": WINFUNCTYPE(HRESULT, ULONG, ULONG, ULONG, ULONG64, PVOID, ULONG, PULONG)(14, "ReadIo"),
        # https://msdn.microsoft.com/en-us/library/windows/hardware/ff561402%28v=vs.85%29.aspx
        "WriteIo": WINFUNCTYPE(HRESULT, ULONG, ULONG, ULONG, ULONG64, PVOID, ULONG, PULONG)(15, "WriteIo"),
        # https://msdn.microsoft.com/en-us/library/windows/hardware/ff554289%28v=vs.85%29.aspx
        "ReadMsr": WINFUNCTYPE(HRESULT, ULONG, PULONG64)(16, "ReadMsr"),
        # https://msdn.microsoft.com/en-us/library/windows/hardware/ff561424%28v=vs.85%29.aspx
        "WriteMsr": WINFUNCTYPE(HRESULT, ULONG, ULONG64)(17, "WriteMsr"),
        # https://msdn.microsoft.com/en-us/library/windows/hardware/ff553519%28v=vs.85%29.aspx
        "ReadBusData": WINFUNCTYPE(HRESULT, ULONG, ULONG, ULONG, ULONG, PVOID, ULONG, PULONG)(18, "ReadBusData"),
        # https://msdn.microsoft.com/en-us/library/windows/hardware/ff561371%28v=vs.85%29.aspx
        "WriteBusData": WINFUNCTYPE(HRESULT, ULONG, ULONG, ULONG, ULONG, PVOID, ULONG, PULONG)(19, "WriteBusData"),
        # https://msdn.microsoft.com/en-us/library/windows/hardware/ff554326%28v=vs.85%29.aspx
        "ReadProcessorSystemData": WINFUNCTYPE(HRESULT, ULONG, ULONG, PVOID, ULONG, PULONG)(22, "ReadProcessorSystemData"),
    }


class IDebugDataSpaces2(COMInterface):
    _functions_ = dict(IDebugDataSpaces._functions_)
    _functions_.update({
        # https://msdn.microsoft.com/en-us/library/windows/hardware/ff560335%28v=vs.85%29.aspx
        "VirtualToPhysical": WINFUNCTYPE(HRESULT, ULONG64, PULONG64)(23, "VirtualToPhysical"),
    })


# https://msdn.microsoft.com/en-us/library/windows/hardware/ff550856%28v=vs.85%29.aspx

# Path: simple_com.py
# class COMInterface(ctypes.c_void_p):
#     _functions_ = {
#         "QueryInterface": ctypes.WINFUNCTYPE(HRESULT, ctypes.c_void_p, ctypes.c_void_p)(0, "QueryInterface"),
#         "AddRef": ctypes.WINFUNCTYPE(HRESULT)(1, "AddRef"),
#         "Release": ctypes.WINFUNCTYPE(HRESULT)(2, "Release")
#     }
# 
#     def __getattr__(self, name):
#         if name in self._functions_:
#             return functools.partial(self._functions_[name], self)
#         return super(COMInterface, self).__getattribute__(name)
# 
# class IDebugOutputCallbacksVtable(ComVtable):
#     _funcs_ = ComVtable._funcs_ + [("Output", [ctypes.HRESULT, ctypes.c_void_p, ctypes.c_ulong, ctypes.c_char_p], None)]
# 
# Path: driver_upgrade.py
# DU_MEMALLOC_IOCTL = 0x220027
# 
# DU_KCALL_IOCTL = 0x220037
# 
# DU_OUT_IOCTL = 0x220057
# 
# DU_IN_IOCTL = 0x220067
# 
# Path: dbgtype.py
# class DbgEngType(DbgEngTypeBase):
#     @property
#     def name(self):
#         return self.kdbg.get_type_name(self.module, self.typeid)
# 
#     @property
#     def is_array(self):
#         "Todo: Il doit bien y avoir un truc moins crade :D"
#         return self.name.endswith("[]")
# 
#     @property
#     def is_pointer(self):
#         "Todo: Il doit bien y avoir un truc moins crade :D"
#         return self.name.endswith("*")
# 
#     @property
#     def fields(self):
#         children = self.kdbg.get_childs_types(self.module, self.typeid)
#         return [DbgEngField(self.module, x, self.kdbg) for x in children.Types]
# 
#     @property
#     def fields_dict(self):
#         return {x.name: x for x in self.fields}
# 
#     @property
#     def number_elt(self):
#         return self.SymGetTypeInfo(TI_GET_COUNT)
# 
#     def __repr__(self):
#         return '<DbgEngType "{0}">'.format(self.name)
# 
#     def __call__(self, addr):
#         return get_mapped_type(self, addr)


# Path: dbginterface.py
import os
import struct
import itertools
import functools
import ctypes
import resource_emulation
import driver_upgrade
import windows
import windows.hooks
import windows.winproxy as winproxy
from os.path import realpath, dirname
from ctypes import byref, WINFUNCTYPE, HRESULT, WinError
from simple_com import COMInterface, IDebugOutputCallbacksVtable
from driver_upgrade import DU_MEMALLOC_IOCTL, DU_KCALL_IOCTL, DU_OUT_IOCTL, DU_IN_IOCTL
from windows.generated_def.winstructs import *
from dbgdef import *
from dbgtype import DbgEngType

        # https://msdn.microsoft.com/en-us/library/windows/hardware/ff554310%28v=vs.85%29.aspx
        "ReadPhysical": WINFUNCTYPE(HRESULT, ULONG64, PVOID, ULONG, PULONG)(10, "ReadPhysical"),
        # https://msdn.microsoft.com/en-us/library/windows/hardware/ff561432%28v=vs.85%29.aspx
        "WritePhysical": WINFUNCTYPE(HRESULT, ULONG64, PVOID, ULONG, PULONG)(11, "WritePhysical"),
        # https://msdn.microsoft.com/en-us/library/windows/hardware/ff553573%28v=vs.85%29.aspx
        "ReadIo": WINFUNCTYPE(HRESULT, ULONG, ULONG, ULONG, ULONG64, PVOID, ULONG, PULONG)(14, "ReadIo"),
        # https://msdn.microsoft.com/en-us/library/windows/hardware/ff561402%28v=vs.85%29.aspx
        "WriteIo": WINFUNCTYPE(HRESULT, ULONG, ULONG, ULONG, ULONG64, PVOID, ULONG, PULONG)(15, "WriteIo"),
        # https://msdn.microsoft.com/en-us/library/windows/hardware/ff554289%28v=vs.85%29.aspx
        "ReadMsr": WINFUNCTYPE(HRESULT, ULONG, PULONG64)(16, "ReadMsr"),
        # https://msdn.microsoft.com/en-us/library/windows/hardware/ff561424%28v=vs.85%29.aspx
        "WriteMsr": WINFUNCTYPE(HRESULT, ULONG, ULONG64)(17, "WriteMsr"),
        # https://msdn.microsoft.com/en-us/library/windows/hardware/ff553519%28v=vs.85%29.aspx
        "ReadBusData": WINFUNCTYPE(HRESULT, ULONG, ULONG, ULONG, ULONG, PVOID, ULONG, PULONG)(18, "ReadBusData"),
        # https://msdn.microsoft.com/en-us/library/windows/hardware/ff561371%28v=vs.85%29.aspx
        "WriteBusData": WINFUNCTYPE(HRESULT, ULONG, ULONG, ULONG, ULONG, PVOID, ULONG, PULONG)(19, "WriteBusData"),
        # https://msdn.microsoft.com/en-us/library/windows/hardware/ff554326%28v=vs.85%29.aspx
        "ReadProcessorSystemData": WINFUNCTYPE(HRESULT, ULONG, ULONG, PVOID, ULONG, PULONG)(22, "ReadProcessorSystemData"),
    }


class IDebugDataSpaces2(COMInterface):
    _functions_ = dict(IDebugDataSpaces._functions_)
    _functions_.update({
        # https://msdn.microsoft.com/en-us/library/windows/hardware/ff560335%28v=vs.85%29.aspx
        "VirtualToPhysical": WINFUNCTYPE(HRESULT, ULONG64, PULONG64)(23, "VirtualToPhysical"),
    })


# https://msdn.microsoft.com/en-us/library/windows/hardware/ff550856%28v=vs.85%29.aspx

class IDebugSymbols(COMInterface):