Commits

Alexander Konovalov  committed 1d1246c

Removed old code

  • Participants
  • Parent commits 1bbb3cf

Comments (0)

Files changed (10)

File premake4.lua

     targetdir "bin"
     
     includedirs { "inc" }
-    libdirs "lib"
     
     configuration { "debug" }
       defines { "DEBUG" }
       
       links "mhook"
       links "ws2_32"
-      
     
     project "disasm"
       kind "StaticLib"
       links { "disasm" }
       
       files { "src/mhook/**.cpp",
-              "src/mhook/**.hpp" }
-              
-    project "dinput8-wrapper"
-      kind "StaticLib"
-      language "C++"
-      includedirs { "src" } 
-      
-      files { "src/dinput8-wrapper/**.cpp",
-              "src/dinput8-wrapper/**.hpp" }
-      
-      includedirs { "$(DXSDK_DIR)/Include" }
-      configuration "x32"
-        libdirs { "$(DXSDK_DIR)/Lib/x86" }
-      configuration "x64"
-        libdirs { "$(DXSDK_DIR)/Lib/x64" }
-    
-    project "dll-loader"
-        kind "ConsoleApp"
-        language "C++"
-        
-        files { "src/dll-loader/**.cpp",
-                "src/dll-loader/**.hpp" }
-    
-    project "network-logger"
-      kind "SharedLib"
-      language "C++"
-      includedirs { "src" }
-      
-      files { "src/network-logger/**.cpp",
-              "src/network-logger/**.hpp" }
-      
-      links { "mhook" }
-    
-    project "keyboard-logger"
-      kind "SharedLib"
-      language "C++"
-      includedirs { "src" }
-      
-      files { "src/keyboard-logger/**.cpp",
-              "src/keyboard-logger/**.hpp" }
-      
-      links { "mhook" }
-      links { "dinput8-wrapper" }
-      
-      includedirs { "$(DXSDK_DIR)/Include" }
-      configuration "x32"
-        libdirs { "$(DXSDK_DIR)/Lib/x86" }
-      configuration "x64"
-        libdirs { "$(DXSDK_DIR)/Lib/x64" }
-      configuration "*"
-        links { "dxguid" }
+              "src/mhook/**.hpp" }

File src/dinput8-wrapper/IDirectInput8WrapperBase.cpp

-#define DIRECTINPUT_VERSION 0x0800
-#include "IDirectInput8WrapperBase.hpp"
-
-#undef STDMETHOD
-#undef STDMETHOD_
-#define STDMETHOD(method)        COM_DECLSPEC_NOTHROW HRESULT STDMETHODCALLTYPE method
-#define STDMETHOD_(type,method)  COM_DECLSPEC_NOTHROW type STDMETHODCALLTYPE method
-
-namespace dinput8wrapper {
-
-IDirectInput8WrapperBase::IDirectInput8WrapperBase(IDirectInput8* di) : m_di(di) { }
-IDirectInput8* IDirectInput8WrapperBase::getWrappedInterface() { return m_di; }
-
-/*** IUnknown methods ***/
-HRESULT STDMETHODCALLTYPE IDirectInput8WrapperBase::QueryInterface(REFIID riid, void** ppvObj) {
-  return m_di->QueryInterface(riid, ppvObj);
-}
-ULONG STDMETHODCALLTYPE IDirectInput8WrapperBase::AddRef() {
-  return m_di->AddRef();
-}
-ULONG STDMETHODCALLTYPE IDirectInput8WrapperBase::Release() {
-  ULONG count = m_di->Release();
-  if (0 == count) {
-    delete this;
-  }
-  return count;
-}
-
-/*** IDirectInput8 methods ***/
-STDMETHOD(IDirectInput8WrapperBase::CreateDevice)(REFGUID rguid, IDirectInputDevice8** device, LPUNKNOWN unknown) {
-  return m_di->CreateDevice(rguid, device, unknown);
-}
-
-STDMETHOD(IDirectInput8WrapperBase::EnumDevices)(DWORD devType,LPDIENUMDEVICESCALLBACK callback, LPVOID ref, DWORD flags) {
-  return m_di->EnumDevices(devType, callback, ref, flags);
-}
-
-STDMETHOD(IDirectInput8WrapperBase::GetDeviceStatus)(REFGUID rguid) {
-  return m_di->GetDeviceStatus(rguid);
-}
-
-STDMETHOD(IDirectInput8WrapperBase::RunControlPanel)(HWND owner, DWORD flags) {
-  return m_di->RunControlPanel(owner, flags);
-}
-
-STDMETHOD(IDirectInput8WrapperBase::Initialize)(HINSTANCE instance, DWORD version) {
-  return m_di->Initialize(instance, version);
-}
-
-STDMETHOD(IDirectInput8WrapperBase::FindDevice)(REFGUID rguid, LPCTSTR name, LPGUID guidInstance) {
-  return m_di->FindDevice(rguid, name, guidInstance);
-}
-
-STDMETHOD(IDirectInput8WrapperBase::EnumDevicesBySemantics)(LPCTSTR username, LPDIACTIONFORMAT action,
-  LPDIENUMDEVICESBYSEMANTICSCB callback, LPVOID ref, DWORD flags) {
-  return m_di->EnumDevicesBySemantics(username, action, callback, ref, flags);
-}
-
-STDMETHOD(IDirectInput8WrapperBase::ConfigureDevices)(LPDICONFIGUREDEVICESCALLBACK callback, LPDICONFIGUREDEVICESPARAMS params,
-  DWORD flags, LPVOID ref) {
-  return m_di->ConfigureDevices(callback, params, flags, ref);
-}
-
-}; // namespace dinput8wrapper

File src/dinput8-wrapper/IDirectInput8WrapperBase.hpp

-#ifndef DINPUT8_WRAPPER_IDIRECTINPUT8WRAPPERBASE_HPP_
-#define DINPUT8_WRAPPER_IDIRECTINPUT8WRAPPERBASE_HPP_
-
-#include <dinput.h>
-
-namespace dinput8wrapper {
-
-class IDirectInput8WrapperBase : public IDirectInput8 {
-public:
-  IDirectInput8WrapperBase(IDirectInput8* di);
-  IDirectInput8* getWrappedInterface();
-
-  /*** IUnknown methods ***/
-  STDMETHOD(QueryInterface)(REFIID riid, void** ppvObj);
-  STDMETHOD_(ULONG, AddRef)();
-  STDMETHOD_(ULONG, Release)();
-
-  /*** IDirectInput8 methods ***/
-  STDMETHOD(CreateDevice)(REFGUID rguid, IDirectInputDevice8** device, LPUNKNOWN unknown);
-  STDMETHOD(EnumDevices)(DWORD devType,LPDIENUMDEVICESCALLBACK callback, LPVOID ref, DWORD flags);
-  STDMETHOD(GetDeviceStatus)(REFGUID rguid);
-  STDMETHOD(RunControlPanel)(HWND owner, DWORD flags);
-  STDMETHOD(Initialize)(HINSTANCE instance, DWORD version);
-  STDMETHOD(FindDevice)(REFGUID rguid, LPCTSTR name, LPGUID guidInstance);
-  STDMETHOD(EnumDevicesBySemantics)(LPCTSTR username, LPDIACTIONFORMAT action,
-    LPDIENUMDEVICESBYSEMANTICSCB callback, LPVOID ref, DWORD flags);
-  STDMETHOD(ConfigureDevices)(LPDICONFIGUREDEVICESCALLBACK callback, LPDICONFIGUREDEVICESPARAMS params,
-    DWORD flags, LPVOID ref);
-
-protected:
-  IDirectInput8* m_di;
-};
-
-}; // namespace dinput8wrapper
-
-#endif//DINPUT8_WRAPPER_IDIRECTINPUT8WRAPPERBASE_HPP_

File src/dinput8-wrapper/IDirectInputDevice8WrapperBase.cpp

-#define DIRECTINPUT_VERSION 0x0800
-#include "IDirectInputDevice8WrapperBase.hpp"
-
-#undef STDMETHOD
-#undef STDMETHOD_
-#define STDMETHOD(method)        COM_DECLSPEC_NOTHROW HRESULT STDMETHODCALLTYPE method
-#define STDMETHOD_(type,method)  COM_DECLSPEC_NOTHROW type STDMETHODCALLTYPE method
-
-namespace dinput8wrapper {
-
-IDirectInputDevice8WrapperBase::IDirectInputDevice8WrapperBase(IDirectInputDevice8* device) : m_device(device) { }
-IDirectInputDevice8* IDirectInputDevice8WrapperBase::getWrappedInterface() { return m_device; }
-
-/*** IUnknown methods ***/
-STDMETHOD(IDirectInputDevice8WrapperBase::QueryInterface)(REFIID riid, void** ppvObj) {
-  return m_device->QueryInterface(riid, ppvObj);
-}
-
-STDMETHOD_(ULONG,IDirectInputDevice8WrapperBase::AddRef)() {
-  return m_device->AddRef();
-}
-
-STDMETHOD_(ULONG,IDirectInputDevice8WrapperBase::Release)() {
-  ULONG count = m_device->Release();
-  if(0 == count)
-    delete this;
-
-  return count;
-}
-
-/*** IDirectInputDevice8 methods ***/
-STDMETHOD(IDirectInputDevice8WrapperBase::GetCapabilities)(LPDIDEVCAPS devCaps) {
-  return m_device->GetCapabilities(devCaps);
-}
-STDMETHOD(IDirectInputDevice8WrapperBase::EnumObjects)(LPDIENUMDEVICEOBJECTSCALLBACK callback, LPVOID ref, DWORD flags) {
-  return m_device->EnumObjects(callback, ref, flags);
-}
-STDMETHOD(IDirectInputDevice8WrapperBase::GetProperty)(REFGUID rguid, LPDIPROPHEADER ph) {
-  return m_device->GetProperty(rguid, ph);
-}
-STDMETHOD(IDirectInputDevice8WrapperBase::SetProperty)(REFGUID rguid, LPCDIPROPHEADER ph) {
-  return m_device->SetProperty(rguid, ph);
-}
-STDMETHOD(IDirectInputDevice8WrapperBase::Acquire)() {
-  return m_device->Acquire();
-}
-STDMETHOD(IDirectInputDevice8WrapperBase::Unacquire)() {
-  return m_device->Unacquire();
-}
-STDMETHOD(IDirectInputDevice8WrapperBase::GetDeviceState)(DWORD size, LPVOID data) {
-  return m_device->GetDeviceState(size, data);
-}
-
-STDMETHOD(IDirectInputDevice8WrapperBase::GetDeviceData)(DWORD size, LPDIDEVICEOBJECTDATA data, LPDWORD numElements, DWORD flags) {
-  return m_device->GetDeviceData(size, data, numElements, flags);
-}
-STDMETHOD(IDirectInputDevice8WrapperBase::SetDataFormat)(LPCDIDATAFORMAT df) {
-  return m_device->SetDataFormat(df);
-}
-STDMETHOD(IDirectInputDevice8WrapperBase::SetEventNotification)(HANDLE event) {
-  return m_device->SetEventNotification(event);
-}
-STDMETHOD(IDirectInputDevice8WrapperBase::SetCooperativeLevel)(HWND window, DWORD level) {
-  return m_device->SetCooperativeLevel(window, level);
-}
-STDMETHOD(IDirectInputDevice8WrapperBase::GetObjectInfo)(LPDIDEVICEOBJECTINSTANCE object, DWORD objId, DWORD objHow) {
-  return m_device->GetObjectInfo(object, objId, objHow);
-}
-STDMETHOD(IDirectInputDevice8WrapperBase::GetDeviceInfo)(LPDIDEVICEINSTANCE di) {
-  return m_device->GetDeviceInfo(di);
-}
-STDMETHOD(IDirectInputDevice8WrapperBase::RunControlPanel)(HWND owner, DWORD flags) {
-  return m_device->RunControlPanel(owner, flags);
-}
-STDMETHOD(IDirectInputDevice8WrapperBase::Initialize)(HINSTANCE instance, DWORD version, REFGUID rguid) {
-  return m_device->Initialize(instance, version, rguid);
-}
-STDMETHOD(IDirectInputDevice8WrapperBase::CreateEffect)(REFGUID rguid, LPCDIEFFECT effect_params, LPDIRECTINPUTEFFECT* effect, LPUNKNOWN unknown) {
-  return m_device->CreateEffect(rguid, effect_params, effect, unknown);
-}
-STDMETHOD(IDirectInputDevice8WrapperBase::EnumEffects)(LPDIENUMEFFECTSCALLBACK callback, LPVOID ref, DWORD type) {
-  return m_device->EnumEffects(callback, ref, type);
-}
-STDMETHOD(IDirectInputDevice8WrapperBase::GetEffectInfo)(LPDIEFFECTINFO effect_info, REFGUID rguid) {
-  return m_device->GetEffectInfo(effect_info, rguid);
-}
-STDMETHOD(IDirectInputDevice8WrapperBase::GetForceFeedbackState)(LPDWORD state) {
-  return m_device->GetForceFeedbackState(state);
-}
-STDMETHOD(IDirectInputDevice8WrapperBase::SendForceFeedbackCommand)(DWORD flags) {
-  return m_device->SendForceFeedbackCommand(flags);
-}
-STDMETHOD(IDirectInputDevice8WrapperBase::EnumCreatedEffectObjects)(LPDIENUMCREATEDEFFECTOBJECTSCALLBACK callback, LPVOID ref, DWORD flags) {
-  return m_device->EnumCreatedEffectObjects(callback, ref, flags);
-}
-STDMETHOD(IDirectInputDevice8WrapperBase::Escape)(LPDIEFFESCAPE escape) {
-  return m_device->Escape(escape);
-}
-STDMETHOD(IDirectInputDevice8WrapperBase::Poll)() {
-  return m_device->Poll();
-}
-STDMETHOD(IDirectInputDevice8WrapperBase::SendDeviceData)(DWORD size, LPCDIDEVICEOBJECTDATA data, LPDWORD num_elements, DWORD flags) {
-  return m_device->SendDeviceData(size, data, num_elements, flags);
-}
-STDMETHOD(IDirectInputDevice8WrapperBase::EnumEffectsInFile)(LPCTSTR file_name, LPDIENUMEFFECTSINFILECALLBACK callback, LPVOID ref, DWORD flags) {
-  return m_device->EnumEffectsInFile(file_name, callback, ref, flags);
-}
-STDMETHOD(IDirectInputDevice8WrapperBase::WriteEffectToFile)(LPCTSTR file_name, DWORD num_entries, LPDIFILEEFFECT effects, DWORD flags) {
-  return m_device->WriteEffectToFile(file_name, num_entries, effects, flags);
-}
-STDMETHOD(IDirectInputDevice8WrapperBase::BuildActionMap)(LPDIACTIONFORMAT format, LPCTSTR username, DWORD flags) {
-  return m_device->BuildActionMap(format, username, flags);
-}
-STDMETHOD(IDirectInputDevice8WrapperBase::SetActionMap)(LPDIACTIONFORMAT format, LPCTSTR username, DWORD flags) {
-  return m_device->SetActionMap(format, username, flags);
-}
-STDMETHOD(IDirectInputDevice8WrapperBase::GetImageInfo)(LPDIDEVICEIMAGEINFOHEADER image_header) {
-  return m_device->GetImageInfo(image_header);
-}
-
-}; // namespace dinput8wrapper

File src/dinput8-wrapper/IDirectInputDevice8WrapperBase.hpp

-#ifndef DINPUT8_WRAPPER_IDIRECTINPUT8DEVICEWRAPPERBASE_HPP_
-#define DINPUT8_WRAPPER_IDIRECTINPUT8DEVICEWRAPPERBASE_HPP_
-
-#include <dinput.h>
-
-namespace dinput8wrapper {
-
-class IDirectInputDevice8WrapperBase : public IDirectInputDevice8 {
-public:
-  IDirectInputDevice8WrapperBase(IDirectInputDevice8* device);
-  IDirectInputDevice8* getWrappedInterface();
-
-  /*** IUnknown methods ***/
-  STDMETHOD(QueryInterface)(REFIID riid, void** ppvObj);
-  STDMETHOD_(ULONG,AddRef)();
-  STDMETHOD_(ULONG,Release)();
-
-  /*** IDirectInputDevice8 methods ***/
-  STDMETHOD(GetCapabilities)(LPDIDEVCAPS devCaps);
-  STDMETHOD(EnumObjects)(LPDIENUMDEVICEOBJECTSCALLBACK callback, LPVOID ref, DWORD flags);
-  STDMETHOD(GetProperty)(REFGUID rguid, LPDIPROPHEADER ph);
-  STDMETHOD(SetProperty)(REFGUID rguid, LPCDIPROPHEADER ph);
-  STDMETHOD(Acquire)();
-  STDMETHOD(Unacquire)();
-  STDMETHOD(GetDeviceState)(DWORD size, LPVOID data);
-
-  STDMETHOD(GetDeviceData)(DWORD size, LPDIDEVICEOBJECTDATA data, LPDWORD numElements, DWORD flags);
-  STDMETHOD(SetDataFormat)(LPCDIDATAFORMAT df);
-  STDMETHOD(SetEventNotification)(HANDLE event);
-  STDMETHOD(SetCooperativeLevel)(HWND window, DWORD level);
-  STDMETHOD(GetObjectInfo)(LPDIDEVICEOBJECTINSTANCE object, DWORD objId, DWORD objHow);
-  STDMETHOD(GetDeviceInfo)(LPDIDEVICEINSTANCE di);
-  STDMETHOD(RunControlPanel)(HWND owner, DWORD flags);
-  STDMETHOD(Initialize)(HINSTANCE instance, DWORD version, REFGUID rguid);
-  STDMETHOD(CreateEffect)(REFGUID rguid, LPCDIEFFECT effect_params, LPDIRECTINPUTEFFECT* effect, LPUNKNOWN unknown);
-  STDMETHOD(EnumEffects)(LPDIENUMEFFECTSCALLBACK callback, LPVOID ref, DWORD type);
-  STDMETHOD(GetEffectInfo)(LPDIEFFECTINFO effect_info, REFGUID rguid);
-  STDMETHOD(GetForceFeedbackState)(LPDWORD state);
-  STDMETHOD(SendForceFeedbackCommand)(DWORD flags);
-  STDMETHOD(EnumCreatedEffectObjects)(LPDIENUMCREATEDEFFECTOBJECTSCALLBACK callback, LPVOID ref, DWORD flags);
-  STDMETHOD(Escape)(LPDIEFFESCAPE escape);
-  STDMETHOD(Poll)();
-  STDMETHOD(SendDeviceData)(DWORD size, LPCDIDEVICEOBJECTDATA data, LPDWORD num_elements, DWORD flags);
-  STDMETHOD(EnumEffectsInFile)(LPCTSTR file_name, LPDIENUMEFFECTSINFILECALLBACK callback, LPVOID ref, DWORD flags);
-  STDMETHOD(WriteEffectToFile)(LPCTSTR file_name, DWORD num_entries, LPDIFILEEFFECT effects, DWORD flags);
-  STDMETHOD(BuildActionMap)(LPDIACTIONFORMAT format, LPCTSTR username, DWORD flags);
-  STDMETHOD(SetActionMap)(LPDIACTIONFORMAT format, LPCTSTR username, DWORD flags);
-  STDMETHOD(GetImageInfo)(LPDIDEVICEIMAGEINFOHEADER image_header);
-
-protected:
-  IDirectInputDevice8* m_device;
-};
-
-}; // namespace dinput8wrapper
-
-#endif//DINPUT8_WRAPPER_IDIRECTINPUT8DEVICEWRAPPERBASE_HPP_

File src/dll-loader/main.cpp

-#include <cstdio>
-#include <cstdlib>
-#include <cassert>
-
-#include <windows.h>
-
-char* MakeFullPath(const char* relPath) {
-  assert(relPath != NULL);
-  assert(strlen(relPath) <= MAX_PATH);
-  
-  // intermediate buffer
-  char buffer[MAX_PATH * 2 + 2];
-  
-  // get current working directory
-  DWORD nLength = GetCurrentDirectoryA(sizeof(buffer), buffer);
-  
-  // check that there is enough space for the whole path
-  assert(nLength + strlen(relPath) + 2 <= sizeof(buffer));
-  
-  // concat paths
-  strcat(buffer, "\\");
-  strcat(buffer, relPath);
-  
-  // duplicate buffer
-  return _strdup(buffer);
-}
-
-HMODULE LoadLibraryRemotely(HANDLE hProcess, const char* path) {  
-  assert(hProcess != INVALID_HANDLE_VALUE);
-  assert(path != NULL);
-  
-  // calc required memory size to store path
-  SIZE_T szPathSize = strlen(path) + 1;
-  
-  // allocate memory
-  LPVOID lpAddress = VirtualAllocEx(hProcess, NULL, szPathSize, MEM_COMMIT, PAGE_READWRITE);
-  if (!lpAddress) {
-    return NULL;
-  }
-  
-  // write path to the allocated memory address
-  DWORD dwWritten;
-  if (!WriteProcessMemory(hProcess, lpAddress, path, szPathSize, &dwWritten)) {
-    VirtualFreeEx(hProcess, lpAddress, 0, MEM_DECOMMIT);
-    return NULL;
-  } else if (dwWritten != szPathSize) {
-    VirtualFreeEx(hProcess, lpAddress, 0, MEM_DECOMMIT);
-    return NULL;
-  }
-
-  // get LoadLibraryA address
-  // XXX: hopefuly it will be the same in the other process!
-  LPTHREAD_START_ROUTINE fnLoadLibrary = 
-    reinterpret_cast<LPTHREAD_START_ROUTINE>(GetProcAddress(LoadLibrary("kernel32.dll"), "LoadLibraryA"));
-
-  // create remote thread with an entry point at LoadLibraryA and with lpAddress as a parameter
-  DWORD dwThreadId;
-  HANDLE hThread = CreateRemoteThread(hProcess, NULL, 0, fnLoadLibrary, lpAddress, 0, &dwThreadId);
-  if (hThread == INVALID_HANDLE_VALUE) {
-    VirtualFreeEx(hProcess, lpAddress, 0, MEM_DECOMMIT);
-    return NULL;
-  }
-  
-  // wait till it exits and get it's exit code
-  DWORD dwResult;
-  WaitForSingleObject(hThread, INFINITE);
-  GetExitCodeThread(hThread, &dwResult);
-  if (dwResult == 0) {
-    VirtualFreeEx(hProcess, lpAddress, 0, MEM_DECOMMIT);
-    return NULL;
-  }
-  
-  VirtualFreeEx(hProcess, lpAddress, 0, MEM_DECOMMIT);
-  return reinterpret_cast<HMODULE>(dwResult);
-}
-
-BOOL CreateProcess(LPCSTR lpCmdLine, BOOL bSuspended, PROCESS_INFORMATION* lpPI) {
-  STARTUPINFOA startupInfo;
-  ZeroMemory(&startupInfo, sizeof(startupInfo));
-  startupInfo.cb = sizeof(startupInfo);
-
-  ZeroMemory(lpPI, sizeof(PROCESS_INFORMATION));
-
-  BOOL bSuccess = CreateProcessA(
-    NULL,                                // no application name
-    (LPSTR) lpCmdLine,                   // command line 
-    NULL,                                // no process security attributes 
-    NULL,                                // no thread security attributes 
-    FALSE,                               // do not inherit handles 
-    (bSuspended) ? CREATE_SUSPENDED : 0, // creation flags
-    NULL,                                // inherit the environment
-    NULL,                                // inherit current directory
-    &startupInfo,                        // startup info
-    lpPI                                 // process information
-  );
-  
-  return bSuccess;
-}
-
-
-int main(int argc, char** argv) {
-  if (argc != 3) {
-    printf("Invalid arguments.");
-    return EXIT_FAILURE;
-  }
-
-  LPSTR lpCmdLine = argv[1];
-  LPSTR lpDLLName = MakeFullPath(argv[2]);
-  PROCESS_INFORMATION pi;
-  
-  if (!CreateProcess(lpCmdLine, TRUE, &pi)) {
-    printf("Could not create process.");
-    return EXIT_FAILURE;
-  }
-  
-  if (!LoadLibraryRemotely(pi.hProcess, lpDLLName)) {
-    printf("Could not load library.");
-    return EXIT_FAILURE;
-  }
-  
-  ResumeThread(pi.hThread);
-}

File src/keyboard-logger/main.cpp

-#include <cstdio>
-#include <cstdlib>
-
-#define WIN32_LEAN_AND_MEAN
-#include <windows.h>
-
-#define DIRECTINPUT_VERSION 0x0800
-#include <dinput.h>
-
-#include <dinput8-wrapper/IDirectInput8WrapperBase.hpp>
-#include <dinput8-wrapper/IDirectInputDevice8WrapperBase.hpp>
-
-#include <mhook/mhook.hpp>
-
-template<class T> bool GetBit(T& value, size_t which) {
-  T mask = 1 << which;
-  return (value & mask) != 0;
-}
-template<class T, class B> void SetBit(T& value, size_t which, B bit) {
-  T mask = 1 << which;
-  if (!!bit) {
-    value &= ~mask;
-  } else {
-    value |= mask;
-  }
-}
-
-FARPROC GetProcAddress(LPCSTR lpDLL, LPCSTR lpName) {
-  HMODULE hModule = GetModuleHandleA(lpDLL);
-  if (hModule == NULL) {
-    hModule = LoadLibraryA(lpDLL);
-  }
-  return GetProcAddress(hModule, lpName);
-}
-
-FILE* logFile;
-
-void Log(const char* fmt, ...) {
-  // print to temporary buffer in case we want to do some
-  // post-processing
-  char buffer[4096];
-  va_list args;
-  va_start(args, fmt);
-  vsnprintf(buffer, sizeof(buffer) - 1, fmt, args);
-  va_end(args);
-  
-  // print to the log file and (!)flush it
-  fprintf(logFile, "%s", buffer);
-  fflush(logFile);
-}
-
-void LogByteBuffer(const void* buf, size_t len, size_t bytesPerLine) {
-  typedef unsigned char ubyte;
-  const ubyte* ptr = reinterpret_cast<const ubyte*>(buf);
-  size_t count = 0;
-  size_t lineCount = 0;
-  
-  while (count != len) {
-    // first byte address
-    if (lineCount == 0) {
-      fprintf(logFile, "%.8X  ", count);
-    }
-    
-    fprintf(logFile, "%.2X ", ptr[count]);
-    count += 1;
-    lineCount += 1;
-    
-    if (lineCount == bytesPerLine || count == len) {
-      fprintf(logFile, "\n");
-      lineCount = 0;
-    }
-  }
-  
-  fflush(logFile);
-}
-
-class KeyboardHook : public dinput8wrapper::IDirectInputDevice8WrapperBase {
-public:
-  KeyboardHook(IDirectInputDevice8* device) : dinput8wrapper::IDirectInputDevice8WrapperBase(device) {
-    memset(prevState, 0, sizeof(prevState));
-    memset(currentState, 0, sizeof(currentState));
-    memset(enforcedState, 0, sizeof(enforcedState));
-    
-    SetBit(enforcedState[0x1f], 7, true);
-    SetBit(enforcedState[0x1f], 0, true);
-  }
-  
-  STDMETHOD(GetDeviceState)(DWORD size, LPVOID data) {
-    if (size != 256) {
-      Log("WARN: GetDeviceState called with size not equal 256\n");
-    }
-    
-    // first store the result into a temporary buffer
-    BYTE temp[256];
-    HRESULT hr = m_device->GetDeviceState(size, reinterpret_cast<LPVOID>(temp));
-    
-    // if everything is okay, replace the current and the previous states
-    if (SUCCEEDED(hr)) {
-      memcpy(prevState, currentState, 256);
-      memcpy(currentState, temp, 256);
-    }
-    
-    // copy data for the caller
-    //memcpy(data, currentState, size < 256 ? size : 256);
-    
-    // enforce some keys
-    for (size_t i = 0; i < (size < 256 ? size : 256); i++) {
-      if (GetBit(enforcedState[i], 0)) {
-        SetBit(reinterpret_cast<LPBYTE>(data)[i], 7, GetBit(enforcedState[i], 7));
-      }
-    }
-    
-    for (size_t i = 0; i < 256; i++) {
-      if (prevState[i] != currentState[i]) {
-        Log("Some change in %x key, %d\n", i, GetBit(currentState[i], 7));
-      }
-    }
-    
-    return hr;
-  }
-private:
-  BYTE prevState[256];
-  BYTE currentState[256];
-  BYTE enforcedState[256];
-};
-
-class DirectInput8Hook : public dinput8wrapper::IDirectInput8WrapperBase {
-public:
-	DirectInput8Hook(IDirectInput8* di) : dinput8wrapper::IDirectInput8WrapperBase(di) { };
-
-  /*** IDirectInput8 methods ***/
-  STDMETHOD(CreateDevice)(REFGUID rguid, IDirectInputDevice8** device, LPUNKNOWN unknown) {
-		HRESULT hr = m_di->CreateDevice(rguid, device, unknown);
-
-		if (IsEqualGUID(rguid, GUID_SysKeyboard) && SUCCEEDED(hr)) {
-			Log("Created a keyboard\n");
-			
-			//*device = new KeyboardHook(*device);
-		}
-
-		return hr;
-	}
-};
-
-typedef HRESULT (WINAPI *DirectInput8CreatePtr)(HINSTANCE hinst, DWORD dwVersion, REFIID riidltf, LPVOID *ppvOut, LPUNKNOWN punkOuter);
-
-DirectInput8CreatePtr directInput8CreateFunc;
-
-HRESULT WINAPI DirectInput8CreateHook(HINSTANCE hinst, DWORD dwVersion, REFIID riidltf, LPVOID *ppvOut, LPUNKNOWN punkOuter) {
-	Log("DirectInput8Create called with direct input version %x.\n", dwVersion);
-	
-	IDirectInput8* out;
-	HRESULT res = directInput8CreateFunc(hinst, dwVersion, riidltf, (LPVOID*) &out, punkOuter);
-	*ppvOut = new DirectInput8Hook(out);
-	return res;
-}
-
-typedef BOOL (WINAPI *GetKeyboardStatePtr)(__out_ecount(256) PBYTE lpKeyState);
-GetKeyboardStatePtr getKeyboardStateFunc;
-BOOL WINAPI GetKeyboardStateHook(__out_ecount(256) PBYTE lpKeyState) {
-  //Log("GetKeyboardState called\n");
-  return GetKeyboardState(lpKeyState);
-}
-
-void InitKeyboardLogger() {
-  // open log
-  logFile = fopen("keyboard.log", "w+");
-  Log("Log file opened.\n");
-
-  // get function addresses
-  Log("Getting function addresses.\n");
-  directInput8CreateFunc = reinterpret_cast<DirectInput8CreatePtr>(GetProcAddress("dinput8.dll", "DirectInput8Create"));
-  getKeyboardStateFunc = reinterpret_cast<GetKeyboardStatePtr>(GetProcAddress("user32.dll", "GetKeyboardState"));
-  
-  // install hooks
-  Log("Installing hooks.\n");
-  mhook_install_hook(reinterpret_cast<void**>(&directInput8CreateFunc), reinterpret_cast<void*>(&DirectInput8CreateHook));
-  mhook_install_hook(reinterpret_cast<void**>(&getKeyboardStateFunc), reinterpret_cast<void*>(&GetKeyboardStateHook));
-}
-
-void FinalizeKeyboardLogger() {
-  // remove hooks
-  Log("Removing hooks.\n");
-  mhook_remove_hook(reinterpret_cast<void**>(&directInput8CreateFunc));
-  mhook_remove_hook(reinterpret_cast<void**>(&getKeyboardStateFunc));
-  
-  // close log file
-  Log("Closing log file.\n");
-  fclose(logFile);
-}
-
-int WINAPI DllMain(HMODULE hDLL, DWORD fdwReason, LPVOID lpReserved) {
-	if (fdwReason == DLL_PROCESS_ATTACH) {
-		InitKeyboardLogger();
-	} else if (fdwReason == DLL_PROCESS_DETACH) {
-		FinalizeKeyboardLogger();
-	}
-	return TRUE;
-}

File src/network-logger/main.cpp

-#include <cstdio>
-#include <cstdlib>
-
-#define WIN32_LEAN_AND_MEAN
-#include <windows.h>
-#include <winsock2.h>
-#include <mhook/mhook.hpp>
-
-FARPROC GetProcAddress(LPCSTR lpDLL, LPCSTR lpName) {
-  HMODULE hModule = LoadLibraryA(lpDLL);
-  return GetProcAddress(hModule, lpName);
-}
-
-typedef int (PASCAL FAR *SendFuncPtr) (
-                     IN SOCKET s,
-                     IN const char FAR * buf,
-                     IN int len,
-                     IN int flags);
-typedef int (PASCAL FAR  *RecvFuncPtr) (
-                     IN SOCKET s,
-    __out_bcount_part(len, return) __out_data_source(NETWORK) char FAR * buf,
-                     IN int len,
-                     IN int flags);
-
-SendFuncPtr sendFunc;
-RecvFuncPtr recvFunc;
-FILE* logFile;
-
-void Log(const char* fmt, ...) {
-  // print to temporary buffer in case we want to do some
-  // post-processing
-  char buffer[4096];
-  va_list args;
-  va_start(args, fmt);
-  vsnprintf(buffer, sizeof(buffer) - 1, fmt, args);
-  va_end(args);
-  
-  // print to the log file and (!)flush it
-  fprintf(logFile, "%s", buffer);
-  fflush(logFile);
-}
-
-void LogByteBuffer(const void* buf, size_t len, size_t bytesPerLine) {
-  typedef unsigned char ubyte;
-  const ubyte* ptr = reinterpret_cast<const ubyte*>(buf);
-  size_t count = 0;
-  size_t lineCount = 0;
-  
-  while (count != len) {
-    // first byte address
-    if (lineCount == 0) {
-      fprintf(logFile, "%.8X  ", count);
-    }
-    
-    fprintf(logFile, "%.2X ", ptr[count]);
-    count += 1;
-    lineCount += 1;
-    
-    if (lineCount == bytesPerLine || count == len) {
-      fprintf(logFile, "\n");
-      lineCount = 0;
-    }
-  }
-  
-  fflush(logFile);
-}
-
-int PASCAL FAR SendHook(IN SOCKET s, IN const char FAR * buf, IN int len, IN int flags) {
-  int sent = sendFunc(s, buf, len, flags);
-  Log("SEND (%x). len = %d, flags = %d, buf = %p, sent = %d\n", static_cast<size_t>(s), len, flags, buf, sent);
-  LogByteBuffer(buf, len, 32);
-  return sent;
-}
-int PASCAL FAR RecvHook(IN SOCKET s, __out_bcount_part(len, return) __out_data_source(NETWORK) char FAR * buf, IN int len, IN int flags) {
-  int recieved = recvFunc(s, buf, len, flags);
-  Log("RECV (%x). len = %d, flags = %d, buf = %p, recieved = %d\n", static_cast<size_t>(s), len, flags, buf, recieved); 
-  LogByteBuffer(buf, recieved, 32);
-  return recieved;
-}
-
-void InitNetworkLogger() {
-  // open log
-  logFile = fopen("network.log", "w+");
-  Log("Log file opened.\n");
-
-  // get function addresses
-  Log("Getting function addresses.\n");
-  sendFunc = reinterpret_cast<SendFuncPtr>(GetProcAddress("ws2_32.dll", "send"));
-  recvFunc = reinterpret_cast<RecvFuncPtr>(GetProcAddress("ws2_32.dll", "recv"));
-  
-  // install hooks
-  Log("Installing hooks.\n");
-  mhook_install_hook(reinterpret_cast<void**>(&sendFunc), reinterpret_cast<void*>(&SendHook));
-  mhook_install_hook(reinterpret_cast<void**>(&recvFunc), reinterpret_cast<void*>(&RecvHook));
-}
-
-void FinalizeNetworkLogger() {
-  // remove hooks
-  Log("Removing hooks.\n");
-  mhook_remove_hook(reinterpret_cast<void**>(&sendFunc));
-  mhook_remove_hook(reinterpret_cast<void**>(&recvFunc));
-  
-  // close log file
-  Log("Closing log file.\n");
-  fclose(logFile);
-}
-
-int WINAPI DllMain(HMODULE hDLL, DWORD fdwReason, LPVOID lpReserved) {
-	if (fdwReason == DLL_PROCESS_ATTACH) {
-		InitNetworkLogger();
-	} else if (fdwReason == DLL_PROCESS_DETACH) {
-		FinalizeNetworkLogger();
-	}
-	return TRUE;
-}

File src/platform/platform.cpp

-#include "platform.hpp"
-
-#include <sys/timeb.h>
-
-#ifdef _WIN32
-  #define WIN32_LEAN_AND_MEAN
-	#include <windows.h>
-#else
-	#include <sys/time.h>
-#endif
-
-namespace ufo {
-
-double Timestamp() {
-	#if defined (WIN32)
-		static LARGE_INTEGER nFreq = {0}, nCount = {0};
-		static double dReturn = 0.0, dCheckTime = 5.0;
-
-		// check for a new frequency once every 5 seconds
-		// note: this is in case ACPI, etc. alters it
-		if((nFreq.QuadPart == 0) || (dCheckTime < dReturn)) {
-			dCheckTime = dReturn + 5.0;
-
-			// avoid a division by zero by returning zero on error
-			if (!QueryPerformanceFrequency(&nFreq)) {
-				struct timeb tp;
-				ftime(&tp);
-				return (double) tp.time + 0.001 * (double) tp.millitm;
-			}
-		}
-
-		// use the ratio of tick amount divided by frequency to find the hertz
-		QueryPerformanceCounter(&nCount);
-		dReturn = ((double)nCount.QuadPart / (double)nFreq.QuadPart);
-		return dReturn;
-	#else
-		static struct timeval time;
-		gettimeofday(&time, 0);
-		static long secsFirstCall = time.tv_sec;
-		return (double) (time.tv_sec-secsFirstCall) +
-			(double) time.tv_usec / (1000.0 * 1000.0);
-	#endif
-}
-
-char* FormatWin32Error(size_t errCode) {
-	LPCSTR lpMsgBuf;
-
-	FormatMessage(
-		FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
-		NULL,
-		errCode,
-		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
-		(LPTSTR) &lpMsgBuf,
-		0,
-		NULL
-	);
-
-	char* result = _strdup(lpMsgBuf);
-	LocalFree((HLOCAL) lpMsgBuf);
-	return result;
-}
-
-char* FormatWin32Error() {
-	return FormatWin32Error(::GetLastError());
-}
-
-}; // namespace ufo

File src/platform/platform.hpp

-#ifndef UFO_PLATFORM_HPP_
-#define UFO_PLATFORM_HPP_
-
-#include <cstddef>
-
-// The SCHECK() macro is used for checking compile-time assertions, 
-// and will cause a compilation error if its assertion is not met.
-// FIXME: COMPILE_CHECK/COMPILE_ASSERT/CASSERT ?
-template<bool x> struct __SCHECK_F;
-template<      > struct __SCHECK_F <true> {};
-template<int  x> struct __SCHECK_P        {};
-#define staticAssert(B) \
-     typedef __SCHECK_P<sizeof(__SCHECK_F<((B)? true : false)>)> __SCHECK_ASSERT##__LINE__
-
-namespace ufo {
-
-typedef unsigned char UInt8;
-typedef unsigned short UInt16;
-typedef unsigned int UInt32;
-typedef unsigned long long UInt64;
-
-staticAssert(sizeof(UInt8) == 1);
-staticAssert(sizeof(UInt16) == 2);
-staticAssert(sizeof(UInt32) == 4);
-staticAssert(sizeof(UInt64) == 8);
-
-typedef signed char Int8;
-typedef signed short Int16;
-typedef signed int Int32;
-typedef signed long long Int64;
-
-staticAssert(sizeof(Int8) == 1);
-staticAssert(sizeof(Int16) == 2);
-staticAssert(sizeof(Int32) == 4);
-staticAssert(sizeof(Int64) == 8);
-
-typedef float Float32;
-typedef double Float64;
-
-staticAssert(sizeof(Float32) == 4);
-staticAssert(sizeof(Float64) == 8);
-
-typedef size_t UIntPtr;
-typedef ptrdiff_t IntPtr;
-
-typedef void* CodePtr;
-typedef void* DataPtr;
-
-double Timestamp();
-char* FormatWin32Error(size_t errCode);
-char* FormatWin32Error();
-
-template<class T> bool GetBit(T& value, size_t which) {
-  T mask = 1 << which;
-  return (value & mask) != 0;
-}
-template<class T, class B> void SetBit(T& value, size_t which, B bit) {
-  T mask = 1 << which;
-  if (!!bit) {
-    value &= ~mask;
-  } else {
-    value |= mask;
-  }
-}
-
-}; // namespace ufo
-
-#endif//UFO_PLATFORM_HPP_