[英]Convert char array to integer value
我在計算 WORD、DWORD 等的正確計算時遇到了一些麻煩。我的腦子里有點問題,可能在這個問題上坐了太久。
我正在閱讀 PE 部分標題。 到目前為止一切正常。 以下是隨機 .exe 文件的示例輸出:
File is 286 Kbytes large
PE-Signature [@ 0x108]
0x00000100: ........ ........ 504500
Collect Information (PE file header):
[WORD] Mashinae Type :014C
[WORD] Number of Sections :0006
[DWORD] TimeStamp :5C6ECB00
[DWORD] Pointer to symbol table:00000000
[DWORD] Number of Symbols :00000000
[WORD] Size of optional header:00E0
現在,如您所見,“可選”標頭的大小是 0x00E0,因此我試圖將其緩沖以備后用。 (Bc。只需閱讀完整的標題會使事情變得更快)。
我遇到問題的地方是我要將小端值轉換為實際整數。 我需要從后面讀取值(所以第二個 WORD [ 00 ] 實際上是要讀取的第一個值)。 然而,第二個值需要以某種方式移動(bc。字節的重要性),這就是我正在努力的地方。 我想解決方案並不難,我只是用完了智慧,哈哈。
這是我的草稿,該函數應該返回一個帶有以下值的整數值:
//get a specific value and safe it for later usage
int getValue(char* memory, int start, int end)
{
if (end <= start)
return 0;
unsigned int retVal = 0;
//now just add up array fields
for (int i = end; i >= start; i--)
{
fprintf(stdout, "\n%02hhx", memory[i]);
retVal &= (memory[i] << 8 * (i- start));
}
fprintf(stdout, "\n\n\n%d",retVal);
return retVal;
}
換句話說,我需要將十六進制值(或字符)數組解析為實際整數,但要考慮字節的重要性。
另外: [指向符號表的指針] 和 [符號數] 似乎總是 0。我猜這是因為二進制文件被剝離了符號,但我不確定,因為我更擅長Linux 二進制分析。 我的假設是否正確?
我真的希望這對你有幫助。 根據我目前的理解,這將獲取開始到結束范圍內的字節,並將它們放入一個整數中:
// here I am converting the chars from hex to int
int getBitPattern(char ch)
{
if (ch >= 48 && ch <= 57)
{
return ch - '0';
}
else if (ch >= 65 && ch <= 70)
{
return ch - 55;
}
else
{
// this is in case of invalid input
return -1;
}
}
int getValue(const char* memory, int start, int end)
{
if (end <= start)
return 0;
unsigned int retVal = 0;
//now just add up array fields
for (int i = end, j = 0; i >= start; i--, ++j)
{
fprintf(stdout, "\n%02hhx", memory[i]);
// bitshift in order to insert the next set of 4 bits into their correct spot
retVal |= (getBitPattern(memory[i]) << (4*j));
}
fprintf(stdout, "\n\n\n%d", retVal);
return retVal;
}
boyanhristov96 通過指出 OR 運算符而不是 AND 的用法幫助了很多,正是他/她的努力導致了這個解決方案
在轉換之前還必須進行到 (unsigned char) 的轉換。
如果不是,變量將簡單地在它的最大正范圍內移動,
導致值
0xFFFFE000 (4294959104)
而不是所需的 0x0000E000 (57344)
我們必須左移 8 位,因為我們想一次移動 2 個 16 位值,例如
0x00FF00<<8; // 操作后為 0xFF0000
最后一個函數也使用了一個 OR,這里是:
//now with or operation and cast
int getValue(const char* memory, int start, int end)
{
if (end <= start)
return 0;
unsigned int retVal = 0;
//now just add up array fields
for (int i = end, j = end-start; i >= start; i--, --j)
{
fprintf(stdout, "\n%02hhx", memory[i]);
retVal |= ((unsigned char)(memory[i]) << (8 * j));
}
fprintf(stdout, "\n\n\n%u", retVal);
return retVal;
}
非常感謝您的幫助
2019 年 12 月 16 日編輯:
返回此處獲取功能的更新版本; 有必要重寫它有兩個原因:1) PE-Header 的偏移量取決於目標二進制文件,所以我們必須首先獲得這個值(在位置 0x3c)。 然后,使用指針從一個值移動到另一個值。 2)計算混亂,我更正了它們,現在它應該按預期工作。 第二個參數是字節長度,fe DWORD - 4 字節
干得好:
//because file shambles
int getValuePNTR(const char* memory, int &start, int size)
{
DWORD retVal = 0;
//now just add up array fields
for (int i = start + size-1,j = size-1; j >= 0; --j ,i--)
{
fprintf(stdout, "\ncycle: %d, memory: [%x]", j, memory[i]);
if ((unsigned char)memory[i] == 00 && j > 0)
retVal <<= 8;
else
retVal |= ((unsigned char)(memory[i]) << (8 * j));
//else
//retVal |= ((unsigned char)(memory[i]));
}
//get the next field after this one
start += size;
return retVal;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.