BVB Source Codes

x64dbg Show datainst_helper.cpp Source code

Return Download x64dbg: download datainst_helper.cpp Source code - Download x64dbg Source code - Type:.cpp
  1. #include "datainst_helper.h"
  2. #include "encodemap.h"
  3. #include "stringutils.h"
  4. #include "value.h"
  5. #include <capstone_wrapper.h>
  6.  
  7. std::unordered_map<ENCODETYPE, std::string> disasmMap;
  8. std::unordered_map<std::string, ENCODETYPE> assembleMap;
  9.  
  10. void initDataInstMap()
  11. {
  12.     disasmMap.clear();
  13.     assembleMap.clear();
  14. #define INSTRUCTION_MAP(type, desc)  disasmMap[type] = desc; \
  15.     assembleMap[desc] = type;
  16.  
  17.     INSTRUCTION_MAP(enc_byte, "byte")
  18.     INSTRUCTION_MAP(enc_word, "word")
  19.     INSTRUCTION_MAP(enc_dword, "dword")
  20.     INSTRUCTION_MAP(enc_fword, "fword")
  21.     INSTRUCTION_MAP(enc_qword, "qword")
  22.     INSTRUCTION_MAP(enc_tbyte, "tbyte")
  23.     INSTRUCTION_MAP(enc_oword, "oword")
  24.     INSTRUCTION_MAP(enc_real4, "real4")
  25.     INSTRUCTION_MAP(enc_real8, "real8")
  26.     INSTRUCTION_MAP(enc_real10, "real10")
  27.     INSTRUCTION_MAP(enc_mmword, "mmword")
  28.     INSTRUCTION_MAP(enc_xmmword, "xmmword")
  29.     INSTRUCTION_MAP(enc_ymmword, "ymmword")
  30.     INSTRUCTION_MAP(enc_ascii, "ascii")
  31.     INSTRUCTION_MAP(enc_unicode, "unicode")
  32.  
  33. #undef INSTRUCTION_MAP
  34.  
  35. #define INSTRUCTION_ASSEMBLE_MAP(type, desc)  assembleMap[desc] = type;
  36.     INSTRUCTION_ASSEMBLE_MAP(enc_byte, "db")
  37.     INSTRUCTION_ASSEMBLE_MAP(enc_word, "dw")
  38.     INSTRUCTION_ASSEMBLE_MAP(enc_dword, "dd")
  39.     INSTRUCTION_ASSEMBLE_MAP(enc_qword, "dq")
  40.  
  41. #undef INSTRUCTION_ASSEMBLE_MAP
  42.  
  43. }
  44.  
  45. String GetDataTypeString(void* buffer, duint size, ENCODETYPE type)
  46. {
  47.     switch(type)
  48.     {
  49.     case enc_byte:
  50.         return StringUtils::ToIntegralString<unsigned char>(buffer);
  51.     case enc_word:
  52.         return StringUtils::ToIntegralString<unsigned short>(buffer);
  53.     case enc_dword:
  54.         return StringUtils::ToIntegralString<unsigned int>(buffer);
  55.     case enc_fword:
  56.         return StringUtils::ToHex((unsigned char*)buffer, 6, true);
  57.     case enc_qword:
  58.         return StringUtils::ToIntegralString<unsigned long long int>(buffer);
  59.     case enc_tbyte:
  60.         return StringUtils::ToHex((unsigned char*)buffer, 10, true);
  61.     case enc_oword:
  62.         return StringUtils::ToHex((unsigned char*)buffer, 16, true);
  63.     case enc_mmword:
  64.     case enc_xmmword:
  65.     case enc_ymmword:
  66.         return StringUtils::ToHex((unsigned char*)buffer, size, false);
  67.     case enc_real4:
  68.         return StringUtils::ToFloatingString<float>(buffer);
  69.     case enc_real8:
  70.         return StringUtils::ToFloatingString<double>(buffer);
  71.     case enc_real10:
  72.         return StringUtils::ToHex((unsigned char*)buffer, 10, true);
  73.     //return ToLongDoubleString(buffer);
  74.     case enc_ascii:
  75.         return String((const char*)buffer, size);
  76.     case enc_unicode:
  77.         return StringUtils::Utf16ToUtf8(WString((const wchar_t*)buffer, size / sizeof(wchar_t)));
  78.     default:
  79.         return StringUtils::ToIntegralString<unsigned char>(buffer);
  80.     }
  81. }
  82.  
  83. String GetDataInstMnemonic(ENCODETYPE type)
  84. {
  85.     if(disasmMap.find(type) == disasmMap.end())
  86.         type = enc_byte;
  87.     if(disasmMap.find(type) == disasmMap.end())
  88.         return "???";
  89.     return disasmMap[type];
  90. }
  91.  
  92. String GetDataInstString(void* buffer, duint size, ENCODETYPE type)
  93. {
  94.     return GetDataInstMnemonic(type) + " " + GetDataTypeString(buffer, size, type);
  95. }
  96. duint decodesimpledata(const unsigned char* buffer, ENCODETYPE type)
  97. {
  98.     switch(type)
  99.     {
  100.     case enc_byte:
  101.         return *((unsigned char*)buffer);
  102.     case enc_word:
  103.         return *((unsigned short*)buffer);
  104.     case enc_dword:
  105.         return *((unsigned int*)buffer);
  106. #ifdef _WIN64
  107.     case enc_qword:
  108.         return *((unsigned long long int*)buffer);
  109. #endif // _WIN64
  110.     }
  111.     return 0;
  112. }
  113.  
  114. struct DataInstruction
  115. {
  116.     ENCODETYPE type;
  117.     String operand;
  118. };
  119.  
  120. bool parsedatainstruction(const char* instruction, DataInstruction & di)
  121. {
  122.     di.type = enc_unknown;
  123.     di.operand.clear();
  124.     String instStr = StringUtils::Trim(String(instruction));
  125.     size_t pos = instStr.find_first_of(" \t");
  126.     String opcode = instStr.substr(0, pos);
  127.     std::transform(opcode.begin(), opcode.end(), opcode.begin(), ::tolower);
  128.     if(assembleMap.find(opcode) == assembleMap.end())
  129.         return false;
  130.     di.type = assembleMap[opcode];
  131.     pos = instStr.find_first_not_of(" \t", pos);
  132.     if(pos == String::npos)
  133.         return false;
  134.     di.operand = instStr.substr(pos);
  135.     return true;
  136. }
  137.  
  138. bool isdatainstruction(const char* instruction)
  139. {
  140.     DataInstruction di;
  141.     parsedatainstruction(instruction, di);
  142.  
  143.     return di.type != enc_unknown;
  144. }
  145.  
  146. bool tryassembledata(duint addr, unsigned char* dest, int destsize, int* size, const char* instruction, char* error)
  147. {
  148.     DataInstruction di;
  149.     if(!parsedatainstruction(instruction, di))
  150.     {
  151.         if(di.operand.empty())
  152.             strcpy_s(error, MAX_ERROR_SIZE, GuiTranslateText(QT_TRANSLATE_NOOP("DBG", "Missing operand")));
  153.         return false;
  154.     }
  155.  
  156.     duint retsize = GetEncodeTypeSize(di.type);
  157.     String buffer;
  158.  
  159.     switch(di.type)
  160.     {
  161.     case enc_byte:
  162.     case enc_word:
  163.     case enc_dword:
  164.     case enc_fword:
  165.     case enc_qword:
  166.     {
  167.         unsigned long long result = 0;
  168.         if(!convertLongLongNumber(di.operand.c_str(), result, 16))
  169.         {
  170.             strcpy_s(error, MAX_ERROR_SIZE, GuiTranslateText(QT_TRANSLATE_NOOP("DBG", "Failed to convert operand")));
  171.             return false;
  172.         }
  173.         auto buf = (char*)&result;
  174.         for(auto i = retsize; i < sizeof(result); i++)
  175.             if(buf[i])
  176.             {
  177.                 strcpy_s(error, MAX_ERROR_SIZE, GuiTranslateText(QT_TRANSLATE_NOOP("DBG", "Operand value too big")));
  178.                 return false;
  179.             }
  180.         buffer = String(buf, retsize);
  181.     }
  182.     break;
  183.  
  184.     case enc_tbyte:
  185.     case enc_oword:
  186.     {
  187.         std::vector<unsigned char> data;
  188.         if(!StringUtils::FromHex(StringUtils::PadLeft(di.operand, retsize * 2, '0'), data, true))
  189.         {
  190.             strcpy_s(error, MAX_ERROR_SIZE, GuiTranslateText(QT_TRANSLATE_NOOP("DBG", "Invalid operand (FromHex failed)")));
  191.             return false;
  192.         }
  193.         if(data.size() != retsize)
  194.         {
  195. #ifdef _WIN64
  196.             sprintf_s(error, MAX_ERROR_SIZE, GuiTranslateText(QT_TRANSLATE_NOOP("DBG", "Invalid size (expected %llu, got %llu)")), retsize, data.size());
  197. #else //x86
  198.             sprintf_s(error, MAX_ERROR_SIZE, GuiTranslateText(QT_TRANSLATE_NOOP("DBG", "Invalid size (expected %u, got %u)")), retsize, data.size());
  199. #endif //_WIN64
  200.             return false;
  201.         }
  202.         buffer = String((char*)data.data(), data.size());
  203.     }
  204.     break;
  205.  
  206.     case enc_mmword:
  207.     case enc_xmmword:
  208.     case enc_ymmword:
  209.     {
  210.         std::vector<unsigned char> data;
  211.         if(!StringUtils::FromHex(StringUtils::PadLeft(di.operand, retsize * 2, '0'), data, false))
  212.         {
  213.             strcpy_s(error, MAX_ERROR_SIZE, GuiTranslateText(QT_TRANSLATE_NOOP("DBG", "Invalid operand (FromHex failed)")));
  214.             return false;
  215.         }
  216.         if(data.size() != retsize)
  217.         {
  218. #ifdef _WIN64
  219.             sprintf_s(error, MAX_ERROR_SIZE, GuiTranslateText(QT_TRANSLATE_NOOP("DBG", "Invalid size (expected %llu, got %llu)")), retsize, data.size());
  220. #else //x86
  221.             sprintf_s(error, MAX_ERROR_SIZE, GuiTranslateText(QT_TRANSLATE_NOOP("DBG", "Invalid size (expected %u, got %u)")), retsize, data.size());
  222. #endif //_WIN64
  223.             return false;
  224.         }
  225.         buffer = String((char*)data.data(), data.size());
  226.     }
  227.     break;
  228.  
  229.     case enc_real4:
  230.     {
  231.         retsize = 4;
  232.         float f = std::stof(di.operand);
  233.         buffer = String((char*)&f, 4);
  234.         break;
  235.     }
  236.  
  237.     case enc_real8:
  238.     {
  239.         retsize = 8;
  240.         double d = std::stod(di.operand);
  241.         buffer = String((char*)&d, 8);
  242.         break;
  243.     }
  244.  
  245.     case enc_real10:
  246.         strcpy_s(error, MAX_ERROR_SIZE, GuiTranslateText(QT_TRANSLATE_NOOP("DBG", "80bit extended float is not supported")));
  247.         return false; //80 bit float is not supported in MSVC, need to add other library
  248.  
  249.     case enc_ascii:
  250.     {
  251.         String unescaped;
  252.         if(!StringUtils::Unescape(di.operand, unescaped))
  253.         {
  254.             strcpy_s(error, MAX_ERROR_SIZE, GuiTranslateText(QT_TRANSLATE_NOOP("DBG", "Invalid string literal")));
  255.             return false;
  256.         }
  257.         if(unescaped.size() > size_t(destsize))
  258.         {
  259.             strcpy_s(error, MAX_ERROR_SIZE, GuiTranslateText(QT_TRANSLATE_NOOP("DBG", "String too long")));
  260.             if(size)
  261.             {
  262.                 *size = int(unescaped.size());  //return correct size
  263.                 return dest == nullptr;
  264.             }
  265.             return false;
  266.         }
  267.         retsize = unescaped.size();
  268.         buffer = unescaped;
  269.     }
  270.     break;
  271.  
  272.     case enc_unicode:
  273.     {
  274.         String unescaped;
  275.         if(!StringUtils::Unescape(di.operand, unescaped))
  276.         {
  277.             strcpy_s(error, MAX_ERROR_SIZE, GuiTranslateText(QT_TRANSLATE_NOOP("DBG", "Invalid string literal")));
  278.             return false;
  279.         }
  280.         WString unicode = StringUtils::Utf8ToUtf16(unescaped);
  281.  
  282.         if(unicode.size()*sizeof(wchar_t) > size_t(destsize))
  283.         {
  284.             strcpy_s(error, MAX_ERROR_SIZE, GuiTranslateText(QT_TRANSLATE_NOOP("DBG", "String too long")));
  285.             if(size)
  286.             {
  287.                 retsize = unicode.size() * 2; //return correct size
  288.                 return dest == nullptr;
  289.             }
  290.  
  291.             return false;
  292.         }
  293.         retsize = unicode.size() * 2;
  294.         buffer = String((char*)unicode.c_str(), retsize);
  295.     }
  296.     break;
  297.  
  298.     default:
  299.         return false;
  300.     }
  301.  
  302.     if(retsize > size_t(destsize))
  303.     {
  304.         strcpy_s(error, MAX_ERROR_SIZE, GuiTranslateText(QT_TRANSLATE_NOOP("DBG", "Dest buffer too small")));
  305.         return false;
  306.     }
  307.  
  308.     if(size)
  309.         *size = int(retsize);
  310.  
  311.     if(dest)
  312.         memcpy_s((char*)dest, retsize, buffer.c_str(), retsize);
  313.  
  314.     return true;
  315. }
  316.  
  317. bool trydisasm(const unsigned char* buffer, duint addr, DISASM_INSTR* instr, duint codesize)
  318. {
  319.     ENCODETYPE type = EncodeMapGetType(addr, codesize);
  320.     duint size = EncodeMapGetSize(addr, codesize);
  321.     if(type == enc_unknown || type == enc_code)
  322.         return false;
  323.     memset(instr, 0, sizeof(DISASM_INSTR));
  324.     instr->argcount = 1;
  325.     instr->type = instr_normal;
  326.     instr->arg[0].type = arg_normal;
  327.     instr->arg[0].value = decodesimpledata(buffer, type);
  328.     strcpy_s(instr->arg[0].mnemonic, GetDataTypeString((void*)buffer, size, type).c_str());
  329.     instr->instr_size = int(size);
  330.     String str = GetDataInstString((void*)buffer, MAX_DISASM_BUFFER, type);
  331.     strcpy_s(instr->instruction, str.c_str());
  332.     return true;
  333. }
  334.  
  335. bool trydisasmfast(const unsigned char* data, duint addr, BASIC_INSTRUCTION_INFO* basicinfo, duint codesize)
  336. {
  337.     ENCODETYPE type = EncodeMapGetType(addr, codesize);
  338.     duint size = EncodeMapGetSize(addr, codesize);
  339.     if(type == enc_unknown || type == enc_code)
  340.         return false;
  341.     memset(basicinfo, 0, sizeof(BASIC_INSTRUCTION_INFO));
  342.     basicinfo->type = TYPE_VALUE;
  343.     basicinfo->size = int(size);
  344.     String str = GetDataInstString((void*)data, MAX_DISASM_BUFFER, type);
  345.     strcpy_s(basicinfo->instruction, str.c_str());
  346.     basicinfo->value.size = VALUE_SIZE(size);
  347.     basicinfo->value.value = decodesimpledata(data, type);
  348.     return true;
  349. }
  350.  
downloaddatainst_helper.cpp Source code - Download x64dbg Source code
Related Source Codes/Software:
pencil - Multiplatform GUI Prototyping/Wireframing 2017-04-16
rainloop-webmail - Simple, modern & fast web-based email client ... 2017-04-16
qt - Qt binding for Go (Golang) which supports Windows ... 2017-04-16
MLeaksFinder - Find memory leaks in your iOS app at develop time. 2017-04-16
jsfeat - JavaScript Computer Vision library. 2017-04-16
later - A javascript library for defining recurring schedu... 2017-04-16
Android-ItemTouchHelper-Demo - Basic example of using ItemTouchHelper to add drag... 2017-04-16
onionshare - Securely and anonymously share a file of any size ... 2017-04-16
android-viewflow - A horizontal view scroller library for Android 2017-04-16
css-in-js - React: CSS in JS techniques comparison. 2017-04-16
mama2 - Mother plan - all firewood high flame 2017-04-23
BlurEffectForAndroidDesign - Sample to show how to implement blur graphical tri... 2017-04-23
sphinx_rtd_theme - Sphinx theme for readthedocs.org 2017-04-23
rouge - A pure-ruby code highlighter that is compatible wi... 2017-04-23
spring-security-oauth - Support for adding OAuth1(a) and OAuth2 features (... 2017-04-23
Toucan - Fabulous Image Processing in Swift 2017-04-23
CoffeeScriptRedux - 2017-04-23
breakpoint - Really simple media queries in Sa 2017-04-23
libsvm - 2017-04-22
grr - GRR Rapid Response: remote live forensics for inci... 2017-04-22

 Back to top