Skip to content

Instantly share code, notes, and snippets.

@yakimelon
Created May 29, 2017 02:18
Show Gist options
  • Save yakimelon/817319bde1f30062b6aebc5930a71add to your computer and use it in GitHub Desktop.
Save yakimelon/817319bde1f30062b6aebc5930a71add to your computer and use it in GitHub Desktop.
文字列リソース取得
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// 16進数文字列
int getHexDumpToStr(FILE *fp, char* retStr, int byteSize) {
char buffer[100];
char str[100] = "\0";
const char ln = '\n';
int tmp;
for( int i=0 ; i < byteSize ; i++ ) {
if ( ( tmp = fgetc(fp) ) == EOF ) return -1;
sprintf(buffer, "%02x", tmp);
strcat(str, buffer);
}
strcpy(retStr, str);
return 0;
}
// ASCII文字列で返す
int getAsciiDumpToStr(FILE *fp, char* retStr, int byteSize) {
char buffer[100];
char str[100] = "\0";
const char ln = '\n';
int tmp;
for( int i=0 ; i < byteSize ; i++ ) {
if ( ( tmp = fgetc(fp) ) == EOF ) return -1;
sprintf(buffer, "%c", tmp);
strcat(str, buffer);
}
strcpy(retStr, str);
return 0;
}
int main(){
FILE *fp = fopen("ConsoleApplication1.exe", "r");
char str[1000];
const char STRING_RESOURCES_SIG[100] = "00504144"; // 文字列リソースのマジックシグネチャ
const int SIZE_4_BYTE = 4;
// マジックシグネチャが見つかるまでループ
while(1) {
if ( getHexDumpToStr(fp, str, SIZE_4_BYTE) == -1 ) return -1;
if ( strcmp(str, STRING_RESOURCES_SIG) == 0 ){
break;
}
}
const char STRING_DATA_SIG[100] = "01";
const int SIZE_1_BYTE = 1;
int stringSize;
int firstFlag = 0;
// resources領域から文字列を取得
while(1) {
if ( getHexDumpToStr(fp, str, SIZE_1_BYTE) == -1 ) return -1;
if ( strcmp(str, STRING_DATA_SIG) == 0 ) {
stringSize = fgetc(fp);
if( stringSize == 0 ) {
continue;
} else {
while(1) {
// 文字列をstringSize分取得
getAsciiDumpToStr(fp, str, stringSize);
printf("%s\n", str);
// 文字列の次のバイトが00なら終了
if ( fgetc(fp) == 0 ) break;
stringSize = fgetc(fp);
}
break;
}
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment