-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathptchsize.c
More file actions
345 lines (308 loc) · 8.18 KB
/
Copy pathptchsize.c
File metadata and controls
345 lines (308 loc) · 8.18 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/* $Id$
Patch heap size into FreeCOM executable
Useage: PTCHSIZE freecom compiler [{ size }]
Without any argument: Display the internal information
Else: patch the executable
if the sum of "size" arguments evaluates to zero,
the estimated minimum heap size is used.
*/
#undef DEBUG
#include <ctype.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../include/res.h"
#include "../include/infores.h"
enum {
COMPILER_NONE,
COMPILER_WATCOM,
COMPILER_TC2,
COMPILER_TURBOCPP,
COMPILER_BC5,
COMPILER_GCC
};
FILE *freecom = 0;
unsigned char *info = 0;
unsigned infoLen = 0;
unsigned long minSize;
struct {
unsigned alias, hist, dirs, bufsize, extraSpace;
#ifdef __LP64__
unsigned int heapPos;
#else
unsigned long heapPos;
#endif
} ival;
#define PTCHSIZE
#include "../lib/res_r.c" /* make a single file project */
int getInfo (res_majorid_t major,
res_minorid_t minor,
unsigned long length,
int fd,
void *const arg) {
(void)arg;
if(major == RES_ID_INFO && minor == 0) {
if(length >= INT_MAX) {
puts("Info resource is too large");
return 100;
}
if((info = malloc(infoLen = (unsigned)length)) == 0) {
puts("Out of memory");
return 101;
}
if(read(fd, info, (unsigned)length) != (unsigned)length) {
puts("Read error from file");
return 102;
}
return 1; /* Success */
}
return 0; /* Proceed */
}
#define err(num) err_(i, (num))
void err_(int i, int num)
{ printf("Info resource corrupted at offset %u\n", i);
exit(num);
}
void getInfoValues(void)
{ int i;
unsigned *pu;
ival.alias = ival.hist = ival.dirs = ~0;
ival.extraSpace = 2 * 1024 / 16;
ival.bufsize = 256;
ival.heapPos = ~0;
assert(sizeof(ival.heapPos) == 4);
for(i = 0; i < infoLen; i += 1 + info[i]) {
if((unsigned long)i + 1 + info[i + 1] > infoLen)
err(81);
switch(info[i++]) {
case INFO_END: return;
case INFO_POS_HEAPLEN:
if(info[i] != 4) err(82);
memcpy(&ival.heapPos, &info[i + 1], 4);
break;
case INFO_ALIASES:
pu = &ival.alias;
valUnsigned:
if(info[i] != 2) err(83);
memcpy(pu, &info[i + 1], 2);
break;
case INFO_HISTORY:
pu = &ival.hist;
goto valUnsigned;
case INFO_DIRSTACK:
pu = &ival.dirs;
goto valUnsigned;
case INFO_BUFSIZE:
pu = &ival.bufsize;
goto valUnsigned;
case INFO_EXTRA_SPACE:
pu = &ival.extraSpace;
goto valUnsigned;
default:
printf("Warning: Unknown info tag found: %u\n", info[i - 1]);
}
}
}
void prUns(unsigned val, char *name)
{
fputs(name, stdout);
if(val == (unsigned)~0)
puts(" not included or in separated segment");
else printf(" default buffer size: %u\n", val);
}
#define addUns(val) addUns_(&minSize, (val))
void addUns_(unsigned long *minVal, unsigned val)
{
if(val != (unsigned)~0)
*minVal += val;
}
int my_stricmp(const char *a, const char *b)
{
while ((*a != '\0') && (toupper(*a) == toupper(*b))) {
a++;
b++;
}
return toupper(*a) - toupper(*b);
}
void addSize(unsigned short *size, char * const Xp)
{ unsigned long n;
char *p = Xp;
if(memcmp(p, "+", 2) == 0)
n = minSize;
else {
n = 0;
if(*p == '+')
++p;
if(*p == '0' && toupper(p[1]) == 'X') {
/* parse hexadecimal */
++p;
while(isxdigit(*++p))
if(isdigit(*p))
n = n * 16 + *p - '0';
else
n = n * 16 + toupper(*p) - 'A' + 10;
} else {
/* parse decimal */
while(isdigit(*p))
n = n * 10 + *p++ - '0';
}
if(toupper(*p) == 'K') {
n *= 1024;
++p;
if(toupper(*p) == 'B')
++p;
}
if(*p) {
printf("Number parse error: %s\n", Xp);
exit(103);
}
}
if(n > USHRT_MAX || (n + *size) > USHRT_MAX) {
puts("Sum of sizes exceed 64KB");
exit(104);
}
*size += (unsigned)n;
}
int main(int argc, char **argv)
{ struct EXE_header exe;
unsigned short tosize;
int compiler = COMPILER_NONE;
if(argc <= 2) {
puts("Patch heap size into FreeCOM executable\n"
"\nuseage: PTCHSIZE freecom compiler [{ size }]\n"
"\nSize may be given hexadecimal (0x###) or decimal.\n"
"If 'KB' is appended to size, it's multiplied with 1024.\n"
"A leading plus sign is ignored.\n"
"A plus sign by its own is treated as estimated minimum size.\n"
"More than one sizes are summed up.\n"
"If size evaluates to 0 (zero), the restriction is disabled.\n"
"\ne.g.: PTCHSIZE freecom compiler +5K\n"
"means: reserve 5*1024 bytes in addition to minimum size.\n"
"compiler may be one of bc, gcc, tc, tcpp, watcom.\n"
);
return 127;
}
if (!my_stricmp(argv[2], "WATCOM")) compiler = COMPILER_WATCOM;
else if (!my_stricmp(argv[2], "TC")) compiler = COMPILER_TC2;
else if (!my_stricmp(argv[2], "TCPP")) compiler = COMPILER_TURBOCPP;
else if (!my_stricmp(argv[2], "BC")) compiler = COMPILER_BC5;
else if (!my_stricmp(argv[2], "GCC")) compiler = COMPILER_GCC;
else {
printf("Unknown compiler %s\n", argv[2]);
return 78;
}
if(enumFileResources(argv[1], RES_ID_INFO, getInfo, 0) != 1) {
puts("Failed to locate FreeCOM's info resource\n"
"Possible errors:\n"
"\tAn error is issued above.\n"
"\tThe file could not be opened.\n"
"\tThe version of FreeCOM is too old.\n"
"\tFile read error.");
return 80;
}
if((freecom = fopen(argv[1], "r+b")) == 0) {
printf("Cannot open FreeCOM: %s\n", argv[1]);
return 30;
}
if(fread(&exe, sizeof(exe), 1, freecom) != 1) {
printf("Read error from: %s\n", argv[1]);
return 44;
}
/* exe.extraMin is not trustworthy as it might have been tweak
in a previous run */
assert(info);
/* Decompose the info resource */
getInfoValues();
minSize = 5 * (unsigned long)ival.bufsize /* command lines */
+ 3 * (64 + 3 + 8 + 5) /* filenames */
+ 256 /* env var buffer %PATH% etc */
;
addUns(ival.alias);
addUns(ival.hist);
addUns(ival.dirs);
if(minSize > USHRT_MAX) {
puts("Estimated minimum heap size exceeds 64KB.\n"
"There is most probably a corrupted info resource.");
return 71;
}
if(argc == 3
|| (compiler != COMPILER_GCC && ival.heapPos == ~0)
) {
prUns(ival.alias, "Aliases");
prUns(ival.hist, "Command line history");
prUns(ival.dirs, "Directory stack");
prUns(ival.bufsize, "Command line buffer");
if(ival.heapPos == ~0)
puts("Missing heap position -> cannot change heap size");
else {
if(fseek(freecom, ival.heapPos, SEEK_SET) != 0) {
printf("Failed to seek to heap size offset in %s\n", argv[1]);
return 43;
}
assert(sizeof(tosize) == 2);
if(fread(&tosize, sizeof(tosize), 1, freecom) != 1) {
puts("Failed to read heap size from FreeCOM executable.");
return 76;
}
fclose(freecom);
if(tosize)
printf("Heap size restricted to %u bytes\n"
, tosize);
else
puts("Heap size is currently not restricted");
}
printf("Estimated minimum of heap: %u\n", (unsigned)minSize);
return 0;
}
tosize = 0;
argc = 2;
while(argv[++argc])
addSize(&tosize, argv[argc]);
if(tosize && tosize < minSize) {
printf("size smaller than estimated minimum size: %u\n"
, (unsigned)minSize);
return 72;
}
printf("Patching '%s' to heap size of %u bytes\n"
, argv[1], tosize);
/* Watcom already has extraMin minimal and dynamically adjusts its MCB*/
if(tosize) {
if (compiler == COMPILER_GCC) {
/* need to adjust SP */
unsigned startbss = 0x10000 - exe.extraMax * 16;
exe.fSP = startbss + ival.extraSpace * 16 + tosize;
}
exe.extraMin = exe.extraMax = ival.extraSpace + tosize / 16;
}
else {
exe.extraMax = 0xffff;
exe.extraMin = ival.extraSpace;
}
rewind(freecom);
if(fwrite(&exe, sizeof(exe), 1, freecom) != 1) {
printf("Failed to patch heap size into FreeCOM executable.\n"
"File most probably corrupted now: %s\n", argv[1]);
return 77;
}
if (compiler != COMPILER_GCC) {
if(fseek(freecom, ival.heapPos, SEEK_SET) != 0) {
printf("Failed to seek to heap size offset in %s\n", argv[1]);
return 42;
}
assert(sizeof(tosize) == 2);
if(fwrite(&tosize, sizeof(tosize), 1, freecom) != 1) {
printf("Failed to patch heap size into FreeCOM executable.\n"
"File most probably corrupted now: %s\n", argv[1]);
return 75;
}
}
fflush(freecom);
if(ferror(freecom)) {
printf("Failed to patch heap size into FreeCOM executable.\n"
"File most probably corrupted now: %s\n", argv[1]);
return 76;
}
fclose(freecom);
return 0;
}