forked from superjamie/snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmzh.c
98 lines (85 loc) · 2.85 KB
/
mzh.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* mzh.c - Prints MZ Header for DOS MZ executable format
*
* Jamie Bainbridge <[email protected]> 2016
*
* Thanks to DJ Delorie for the header description
* http://www.delorie.com/djgpp/doc/exe/
*
* This work is contributed to the public domain
* http://creativecommons.org/publicdomain/zero/1.0/
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
struct EXE {
unsigned short signature; /* == 0x5a4D */
unsigned short bytes_in_last_block;
unsigned short blocks_in_file;
unsigned short num_relocs;
unsigned short header_paragraphs;
unsigned short min_extra_paragraphs;
unsigned short max_extra_paragraphs;
unsigned short ss;
unsigned short sp;
unsigned short checksum;
unsigned short ip;
unsigned short cs;
unsigned short reloc_table_offset;
unsigned short overlay_number;
};
struct EXE_RELOC {
unsigned short offset;
unsigned short segment;
};
int main (int argc, char *argv[]) {
int fd;
int exe_data_start, extra_data_start;
struct EXE exe;
if (argc != 2) {
printf("Usage: %s [mz binary]\n", argv[0]);
exit(1);
}
fd = open(argv[1], O_RDONLY);
if (fd == -1) {
perror("Error opening file");
exit(1);
}
read(fd, &exe, 26);
exe_data_start = exe.header_paragraphs * 16L;
extra_data_start = exe.blocks_in_file * 512L;
if (exe.bytes_in_last_block)
extra_data_start -= (512 - exe.bytes_in_last_block);
printf("MZ Header for file %s\n\n"
"Signature (0x5a4d) 0x%04x\n"
"Number of bytes used in last block: %4d\n"
"Number of 512b blocks in EXE file: %4d\n"
"No of relocation entries after header: %4d\n"
"No of 16b paragraphs in header: %4d\n"
"No of 16b paragraphs of memory required: %4d\n"
"Max 16b paragraphs of mem to allocate: %5d\n"
"Stack Segment (SS) register value: 0x%04x\n"
"Stack Pointer (SP) register value: 0x%04x\n"
"Checksum (should be zero): 0x%04x\n"
"Instruction Pointer (IP) reg value: 0x%04x\n"
"Code Segment (CS) register value: 0x%04x\n"
"Offset of first relocation item: 0x%04x\n"
"Overlay Number (usually zero): 0x%04x\n"
"\n",
argv[1], exe.signature, exe.bytes_in_last_block,
exe.blocks_in_file, exe.num_relocs, exe.header_paragraphs,
exe.min_extra_paragraphs, exe.max_extra_paragraphs,
exe.ss, exe.sp, exe.checksum, exe.ip, exe.cs,
exe.reloc_table_offset, exe.overlay_number
);
printf("Computed values\n\n"
"Start of EXE data is at: 0x%04x\n"
"First byte after EXE data is at: 0x%06x\n"
"Total size of EXE data: %6d\n"
"\n",
exe_data_start, extra_data_start, (extra_data_start - exe_data_start - 1)
);
return 0;
}