-
Notifications
You must be signed in to change notification settings - Fork 1
/
EXEFIXUP.C
206 lines (172 loc) · 4.85 KB
/
EXEFIXUP.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
* exefixup.c v0.02 Andrew Kieschnick <andrewk@mail.utexas.edu>
*
* displays PS-X EXE header information
* offers to fix incorrect t_size
* offers to pad to 2048-byte boundary for cd-rom use
*
* THIS SOURCE WAS MODIFIED (SLIGHTLY) TO WORK UNDER DOS
* IF YOU USE UNIX, GET THE THE UNMODIFIED SOURCE
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
unsigned int char2int(unsigned char *foo)
{
return foo[3]*16777216 + foo[2]*65536 + foo[1]*256 + foo[0];
}
void int2char(unsigned int foo, unsigned char *bar)
{
bar[3]=foo>>24;
bar[2]=foo>>16;
bar[1]=foo>>8;
bar[0]=foo;
}
void usage(void)
{
printf("Usage: exefixup <filename>\n\n");
printf("\t<filename>\ta PS-X EXE file\n\n");
printf("\tdisplays EXE header\n");
printf("\toffers to correct a wrong t_size\n");
printf("\toffers to pad to 2048-byte boundary\n\n");
exit(0);
}
void main(int argc, char *argv[])
{
FILE *exe;
FILE *out;
unsigned char data[8];
char filename[256];
int i;
unsigned int header_data[12];
unsigned int size;
unsigned int padsize;
char yesno='Z';
printf("exefixup v0.02 Andrew Kieschnick <andrewk@mail.utexas.edu>\n\n");
if (argc!=2)
usage();
strncpy(filename,argv[1],256);
exe=fopen(filename, "rb");
if (!exe)
{
printf("ERROR: Can't open %s\n",filename);
exit(-1);
}
for(i=0;i<8;i++)
fscanf(exe, "%c", &data[i]);
data[8]=0;
if (strncmp(data, "PS-X EXE", 8))
{
printf("ERROR: Not a PS-X EXE file\n");
exit(-1);
}
for(i=0;i<12;i++)
{
fscanf(exe, "%c", &data[0]);
fscanf(exe, "%c", &data[1]);
fscanf(exe, "%c", &data[2]);
fscanf(exe, "%c", &data[3]);
header_data[i]=char2int(data);
}
printf("id\tPS-X EXE\n");
printf("text\t0x%.8x\n", header_data[0]);
printf("data\t0x%.8x\n", header_data[1]);
printf("pc0\t0x%.8x\n", header_data[2]);
printf("gp0\t0x%.8x\n", header_data[3]);
printf("t_addr\t0x%.8x\n", header_data[4]);
printf("t_size\t0x%.8x\n", header_data[5]);
printf("d_addr\t0x%.8x\n", header_data[6]);
printf("d_size\t0x%.8x\n", header_data[7]);
printf("b_addr\t0x%.8x\n", header_data[8]);
printf("b_size\t0x%.8x\n", header_data[9]);
printf("s_addr\t0x%.8x\n", header_data[10]);
printf("s_size\t0x%.8x\n\n", header_data[11]);
fseek(exe, 0, SEEK_END);
size=ftell(exe)-2048;
padsize=2048-(size%2048);
if (padsize!=2048)
{
printf("WARNING: EXE size is not a multiple of 2048!\n");
while ((yesno!='Y')&&(yesno!='N'))
{
printf("Write a padded EXE (to padded.exe) ? ");
scanf("%c%*c", &yesno);
yesno=toupper(yesno);
}
if (yesno=='Y')
{
out = fopen("padded.exe", "wb");
header_data[5]=size+padsize;
fprintf(out, "PS-X EXE");
for(i=0;i<12;i++)
{
int2char(header_data[i], data);
fprintf(out, "%c%c%c%c", data[0], data[1], data[2], data[3]);
}
fseek(exe, 56, SEEK_SET);
for(i=0;i<size+1992;i++)
{
fscanf(exe, "%c", &data[0]);
fprintf(out, "%c", data[0]);
}
for(i=0;i<padsize;i++)
fprintf(out, "%c", 0);
size=header_data[5];
fclose(out);
}
}
yesno='Z';
if (size!=header_data[5])
{
printf("WARNING: EXE header t_size does not match filesize-2048\n");
printf("EXE header:\t 0x%.8x bytes\n", header_data[5]);
printf("filesize-2048:\t 0x%.8x bytes\n", size);
while ((yesno!='Y')&&(yesno!='N'))
{
printf("Write a corrected EXE (to fixed.exe) ? ");
scanf("%c%*c", &yesno);
yesno=toupper(yesno);
}
if (yesno=='Y')
{
out = fopen("fixed.exe", "wb");
fprintf(out, "PS-X EXE");
for(i=0;i<5;i++)
{
int2char(header_data[i], data);
fprintf(out, "%c%c%c%c", data[0], data[1], data[2], data[3]);
}
int2char(size, data);
fprintf(out, "%c%c%c%c", data[0], data[1], data[2], data[3]);
for(i=6;i<12;i++)
{
int2char(header_data[i], data);
fprintf(out, "%c%c%c%c", data[0], data[1], data[2], data[3]);
}
fseek(exe, 56, SEEK_SET);
for(i=0;i<size+1992;i++)
{
fscanf(exe, "%c", &data[0]);
fprintf(out, "%c", data[0]);
}
fclose(out);
}
}
fclose(exe);
}