開発環境 |
Microsoft Visual Studio Community 2017 |
実行環境 |
Windows 10 Home (64bit) |
プロジェクトの種類 |
Visual C++ |
アプリケーションの種類 |
空のプロジェクト |
プロジェクト名 |
dumphdr |
dumphdr.cpp
// マルチバイト文字セット(MBCS)
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BYTE unsigned char
#define WORD unsigned short
char *exehdr[] = {
"Magic number (MZ=5a4d)",
"Bytes on last page of file",
"Pages in file (1page=512bytes)",
"Relocations",
"Size of header in paragraphs (1para=16bytes)",
"Minimum extra paragraphs needed",
"Maximum extra paragraphs needed",
"Initial (relative) SS value",
"Initial SP value",
"Checksum",
"Initial IP value",
"Initial (relative) CS value",
"File address of relocation table",
"Overlay number",
};
char *hdr = "address 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 0123456789abcdef";
char *fmt = " - - - ";
int main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "usage: dumphdr filename\n");
return 1;
}
FILE *pf;
if (fopen_s(&pf, argv[1], "rb")) {
fprintf(stderr, "%sのオープンに失敗しました。\n", argv[1]);
return 1;
}
// メモリに読み込み
long flen = _filelength(_fileno(pf));
BYTE *data = (BYTE*)malloc(flen);
fread(data, 1, flen, pf);
fclose(pf);
printf("_IMAGE_DOS_HEADER (winnt.h)\n");
int pos = 0;
for (int i = 0; i < _countof(exehdr); i++) {
printf("%04x %04x %s\n", pos, *(WORD*)(data + pos), exehdr[i]);
pos += 2;
}
// 16進ダンプ
printf("\n%s\n\n", hdr);
char line[80];
char buf[9];
for (int i = 0; i < flen; i += 16) {
strcpy_s(line, fmt);
sprintf_s(buf, "%08x", i);
memcpy(line, buf, 8);
int len = __min(flen - i, 16);
for (int j = 0; j < len; j++) {
BYTE c = data[i+j];
sprintf_s(buf, "%02x", c);
memcpy(line+9+3*j, buf, 2);
if (c < 0x20 || 0x7e < c) c = '.';
line[57+j] = c;
}
printf("%s\n", line);
}
free(data);
return 0;
}
最終更新:2017年10月29日 18:25