Permalink
Browse files

DBG: various small improvements to the mov command

  • Loading branch information...
1 parent 6c9eb3d commit 9118d7f9d815506173b704ee6e04fe609e21480a @mrexodia mrexodia committed Jan 10, 2017
Showing with 14 additions and 32 deletions.
  1. +4 −27 src/dbg/commands/cmd-general-purpose.cpp
  2. +10 −5 src/dbg/stringutils.cpp
@@ -316,44 +316,21 @@ bool cbInstrMov(int argc, char* argv[])
{
//do some checks on the data
String dataText = srcText.substr(1, srcText.length() - 2);
- int len = (int)dataText.length();
- if(len % 2)
+ std::vector<unsigned char> data;
+ if(!StringUtils::FromCompressedHex(dataText, data))
{
- dprintf(QT_TRANSLATE_NOOP("DBG", "Invalid hex string \"%s\" (length not divisible by 2)\n"), dataText.c_str());
+ dprintf(QT_TRANSLATE_NOOP("DBG", "Invalid hex string \"%s\"\n"), dataText.c_str());
return false;
}
- for(int i = 0; i < len; i++)
- {
- if(!isxdigit(dataText[i]))
- {
- dprintf(QT_TRANSLATE_NOOP("DBG", "Invalid hex string \"%s\" (contains invalid characters)\n"), dataText.c_str());
- return false;
- }
- }
//Check the destination
duint dest;
if(!valfromstring(argv[1], &dest) || !MemIsValidReadPtr(dest))
{
dprintf(QT_TRANSLATE_NOOP("DBG", "Invalid destination \"%s\"\n"), argv[1]);
return false;
}
- //Convert text to byte array (very ugly)
- Memory<unsigned char*> data(len / 2);
- for(int i = 0, j = 0; i < len; i += 2, j++)
- {
- char b[3] = "";
- b[0] = dataText[i];
- b[1] = dataText[i + 1];
- int res = 0;
- if(sscanf_s(b, "%X", &res) != 1)
- {
- dprintf(QT_TRANSLATE_NOOP("DBG", "Invalid hex byte \"%s\"\n"), b);
- return false;
- }
- data()[j] = res;
- }
//Move data to destination
- if(!MemWrite(dest, data(), data.size()))
+ if(!MemPatch(dest, data.data(), data.size()))
{
dprintf(QT_TRANSLATE_NOOP("DBG", "Failed to write to %p\n"), dest);
return false;
@@ -475,16 +475,21 @@ bool StringUtils::FromCompressedHex(const String & text, std::vector<unsigned ch
String repeatStr;
for(size_t i = 0; i < size;)
{
+ if(isspace(text[i])) //skip whitespace
+ {
+ i++;
+ continue;
+ }
auto high = hex2int(text[i++]); //eat high nibble
- if(i >= size)
+ if(i >= size) //not enough data
return false;
auto low = hex2int(text[i++]); //eat low nibble
- if(high == -1 || low == -1)
+ if(high == -1 || low == -1) //invalid character
return false;
auto lastCh = (high << 4) | low;
data.push_back(lastCh);
- if(i >= size)
+ if(i >= size) //end of buffer
break;
if(text[i] == '{')
@@ -494,13 +499,13 @@ bool StringUtils::FromCompressedHex(const String & text, std::vector<unsigned ch
while(text[i] != '}')
{
repeatStr.push_back(text[i++]); //eat character
- if(i >= size)
+ if(i >= size) //unexpected end of buffer (missing '}')
return false;
}
i++; //eat '}'
duint repeat = 0;
- if(!convertNumber(repeatStr.c_str(), repeat, 16) || !repeat)
+ if(!convertNumber(repeatStr.c_str(), repeat, 16) || !repeat) //conversion failed or repeat zero times
return false;
for(size_t j = 1; j < repeat; j++)
data.push_back(lastCh);

0 comments on commit 9118d7f

Please sign in to comment.