2017-10-05 89 views
1

上下文:Ç - 反向轉爐工藝

我試圖扭轉c轉換器的過程。這是原轉換器

#include <stdio.h> 

int main(void) { 
    FILE *f = fopen("input.bin", "rb"); 
    fseek(f, 0, SEEK_END); 
    int s = ftell(f); 
    rewind(f); 

    FILE *o = fopen("output.txt", "wb"); 

    int i; 
    for(i = 0; i < s/4; i++) { 
     unsigned int u; 
     fread(&u, sizeof(u), 1, f); 
     fprintf(o, "u32[%d] = 0x%08x;\n", i, u); 
    } 

    fclose(o); 
    fclose(f); 

    return 0; 
} 

它所做的是它創建了一個名爲output.txt新文件和.bin文件的字節轉換在這樣的順序:

u32[0] = 0x53555441; 
u32[1] = 0x6373b848; 
u32[2] = 0x74654e65; 
u32[3] = 0x83486f53; 
u32[4] = 0x0ebe60ec; 
u32[5] = 0x48000000; 
u32[6] = 0x40245c8d; 
u32[7] = 0x24448948; 
u32[8] = 0xda894840; 
u32[9] = 0x244c8d48; 
u32[10] = 0x024fbf10; 
u32[11] = 0xc0310000; 
u32[12] = 0x482444c7; 
u32[13] = 0x74656b63; 

問題:

現在,我想要做的是相反的。所以,我試圖再次將上述轉換器的輸出字節轉換爲.bin文件,就像它的原始狀態一樣。

我嘗試這樣做,但我卡在環部分:

#include <stdio.h> 

int main(void) { 
    FILE *f = fopen("loader.txt", "rb"); // INPUT 
    fseek(f, 0, SEEK_END); 
    int s = ftell(f); 
    rewind(f); 

    FILE *o = fopen("loader.bin", "wb"); // OUTPUT 

    int i; 
    for(i = 0; i < s/4; i++) { 
     // grab u32 bytes from loader.txt and convert into a .bin 
    } 

    fclose(o); 
    fclose(f); 

    return 0; 
} 

誰能幫我這個?會驚人地appriciated。


編輯

#include <stdio.h> 

int main(void) { 
    FILE *f = fopen("loader.txt", "rb"); // INPUT 
    rewind(f); 

    FILE *o = fopen("loader.bin", "wb"); // OUTPUT 

    int i; 
    for(i = 0; i < s/4; i++) { 
     fgets(f); 
    sscanf(f); 
    } 

    fclose(o); 
    fclose(f); 

    return 0; 
} 
+0

我確定我看到過類似的問題,以前有解決方案,但我現在找不到它... –

+0

@ChrisTurner真的,這是因爲我刪除它,因爲它被標記爲不解釋它好吧,因爲帳戶被鎖定,所以不能發佈問題。 – Mitch

+0

請用有意義的名稱命名變量,單字母變量不能幫助任何人 –

回答

0

這裏是代碼,這似乎做的工作:

#include <stdio.h> 

int main(void) 
{ 
    const char i_file[] = "loader.txt"; 
    const char o_file[] = "loader.bin"; 
    FILE *f = fopen(i_file, "rb"); // INPUT 
    if (f == 0) 
    { 
     fprintf(stderr, "failed to open file '%s' for reading\n", i_file); 
     return 1; 
    } 

    FILE *o = fopen(o_file, "wb"); // OUTPUT 
    if (o == 0) 
    { 
     fprintf(stderr, "failed to open file '%s' for writing\n", o_file); 
     return 1; 
    } 

    char line[1024]; 
    int lineno = 0; 
    while (fgets(line, sizeof(line), f) != 0) 
    { 
     int u32num; 
     unsigned int value; 
     lineno++; 
     if (line[0] == '\n') 
      continue; // Blank lines ignored 
     if (sscanf(line, "u32[%d] = %x", &u32num, &value) == 2) 
     { 
      fseek(o, u32num * sizeof(value), SEEK_SET); 
      if (fwrite(&value, sizeof(value), 1, o) != 1) 
      { 
       fprintf(stderr, "Failed to write 0x%.8X to offset %d in %s\n", 
         value, u32num, o_file); 
       break; 
      } 
     } 
     else 
     { 
      fprintf(stderr, "Input line %d in %s is mal-formatted: %s", lineno, i_file, line); 
      break; 
     } 
    } 

    fclose(o); 
    fclose(f); 

    return 0; 
} 

當從問題的樣本數據運行(loader.txt):

u32[0] = 0x53555441; 
u32[1] = 0x6373b848; 
u32[2] = 0x74654e65; 
u32[3] = 0x83486f53; 
u32[4] = 0x0ebe60ec; 
u32[5] = 0x48000000; 
u32[6] = 0x40245c8d; 
u32[7] = 0x24448948; 
u32[8] = 0xda894840; 
u32[9] = 0x244c8d48; 
u32[10] = 0x024fbf10; 
u32[11] = 0xc0310000; 
u32[12] = 0x482444c7; 
u32[13] = 0x74656b63; 

輸出文件( loader.bin)是:

$ odx loader.bin 
0x0000: 41 54 55 53 48 B8 73 63 65 4E 65 74 53 6F 48 83 ATUSH.sceNetSoH. 
0x0010: EC 60 BE 0E 00 00 00 48 8D 5C 24 40 48 89 44 24 .`.....H.\[email protected]$ 
0x0020: 40 48 89 DA 48 8D 4C 24 10 BF 4F 02 00 00 31 C0 @H..H.L$..O...1. 
0x0030: C7 44 24 48 63 6B 65 74       .D$Hcket 
0x0038: 
$ 

其中odx僅僅是一個土生土長的十六進制轉儲程序(你可以使用odxxd或其他程序來實現類似的結果)。以初始轉換代碼逐字(它在我的嚴格的編譯選項乾淨編譯 - 做得好),並複製loader.bininput.bin,輸出爲:

u32[0] = 0x53555441; 
u32[1] = 0x6373b848; 
u32[2] = 0x74654e65; 
u32[3] = 0x83486f53; 
u32[4] = 0x0ebe60ec; 
u32[5] = 0x48000000; 
u32[6] = 0x40245c8d; 
u32[7] = 0x24448948; 
u32[8] = 0xda894840; 
u32[9] = 0x244c8d48; 
u32[10] = 0x024fbf10; 
u32[11] = 0xc0310000; 
u32[12] = 0x482444c7; 
u32[13] = 0x74656b63; 

這是一樣的原始輸入文件,因此對的程序可以往返數據(依次運行這兩個程序,並將最終輸出數據作爲原始輸入存儲)。請注意,該代碼尚未在長度不是4字節倍數的二進制文件上進行測試;有擔心的問題。

+0

簡直太神奇了。非常感謝! – Mitch