-
Notifications
You must be signed in to change notification settings - Fork 1
/
fileio.c
358 lines (313 loc) · 7.74 KB
/
fileio.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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
* Jaguar Cartridge Encryption Software
* Portable C version
*
* Copyright 1993-1995 Atari Corporation
* All Rights Reserved
*
* Written by Eric R. Smith
* Based on the 680x0 assembly language
* version by Dave Staugas.
*/
/* input/output functions */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "rsa.h"
#include "fileio.h"
/* storage for magic signature for cart (0x2000 bytes) */
extern byte inbuf[];
/* total size of the ROM we're creating (always a power of 2 and > 0x2000) */
extern size_t romsize;
/*
* read an ASCII assembly language file (with
* statements like dc.b $00,$1a) and convert it
* to binary, placing it into the given buffer.
* At most "bufsize" bytes will be read.
* Note that we *only* handle dc.b and hexadecimal
* numbers; this is an extremely small subset of
* possible inputs, but it's the only way the
* key files are stored, so it's all we need.
* Adding support for decimal numbers and words
* or longs would be a bit of work.
*
* Returns: 0 if a read error occurs
* the number of bytes written into the
* buffer on success
*/
int
ReadAsmFile(FILE *fh, byte *buffer, int bufsize)
{
int c; /* character retrieved */
int val; /* current byte value */
int numbytes; /* number of bytes written into buffer */
numbytes = 0;
while (numbytes < bufsize) {
/* look for the next '$' character */
do {
c = fgetc(fh);
if (c < 0) /* end of file */
return numbytes;
} while (c != '$');
/* now read 2 hex digits to make a byte */
/* first digit */
c = fgetc(fh);
if (c < 0) {
fprintf(stderr, "ERROR: unexpected end-of-file\n");
return 0; /* error */
}
c = toupper(c); /* convert lower case to upper */
if (c >= 'A' && c <= 'F')
val = 16*(10 + (c-'A'));
else if (c >= '0' && c <= '9')
val = 16*(c-'0');
else {
fprintf(stderr, "ERROR: illegal hex digit (%c)\n", c);
return 0;
}
/* second digit */
c = fgetc(fh);
if (c < 0) {
fprintf(stderr, "ERROR: unexpected end-of-file\n");
return 0; /* error */
}
c = toupper(c); /* convert lower case to upper */
if (c >= 'A' && c <= 'F')
val += 10 + (c-'A');
else if (c >= '0' && c <= '9')
val += c-'0';
else {
fprintf(stderr, "ERROR: illegal hex digit (%c)\n", c);
return 0;
}
/* stuff the byte into the buffer */
*buffer++ = val;
numbytes++;
}
return numbytes;
}
/*
* open a file whose base name is derived from
* "filename", but whose extension is "ext".
*/
FILE *
fopen_with_extension(char *filename, char *ext, char *mode)
{
char tempfilename[256];
char *lastperiod;
char *src, *dst, c;
FILE *f;
src = filename;
dst = tempfilename;
lastperiod = (char *)0;
while ( (c = *src++) != 0) {
if (c == '.')
lastperiod = dst;
else if (c == '/' || c == '\\')
lastperiod = (char *)0;
*dst++ = c;
}
if (lastperiod) {
strcpy(lastperiod, ext);
} else {
strcat(tempfilename, ext);
}
/* Do error reporting and bailout here, so our callers
* don't have to.
*/
f= fopen(tempfilename, mode);
if (!f) {
perror(tempfilename);
exit(2);
}
return f;
}
/*
* Write an output file in HI/LO format. If the file is larger than
* 2 megabytes and the "nosplit" flag is 0, then the file is split up
* in 1 megabyte pieces into foo.HI0/foo.LO0, foo.HI1/foo.LO1, etc.
*/
/* files bigger than this much are split */
#define SPLITSIZE 0x200000
void
WriteHILO(char *filename, size_t srcoffset, int nosplit)
{
FILE *hi, *lo; /* file pointers for high and low data, respectively */
FILE *infile; /* input file */
size_t byteswritten; /* bytes of ROM written */
size_t bytesleftinfile; /* bytes left to write in this 2 megabyte piece */
char hiext[6]; /* file extensions for hi/lo parts of file */
char loext[6];
byte *outbuf; /* pointer to output data */
byte tmpbuf[4]; /* input buffer */
if ( romsize <= SPLITSIZE || nosplit) {
strcpy(hiext, ".HI");
strcpy(loext, ".LO");
nosplit = 1;
} else {
strcpy(hiext, ".HI0");
strcpy(loext, ".LO0");
}
hi = fopen_with_extension(filename, hiext, "wb");
lo = fopen_with_extension(filename, loext, "wb");
/* write the first 0x2000 bytes, including the RSA'd stuff */
outbuf = inbuf;
bytesleftinfile = SPLITSIZE;
for (byteswritten = 0; byteswritten < 0x2000; byteswritten +=4 ) {
fputc(outbuf[1],hi);
fputc(outbuf[0], hi);
fputc(outbuf[3], lo);
fputc(outbuf[2], lo);
outbuf += 4;
bytesleftinfile -= 4;
}
/* open the file to be ROMed */
infile = fopen(filename, "rb");
if (!infile) {
perror(filename);
exit(1);
}
if (fseek(infile, srcoffset, SEEK_SET))
{
perror(filename);
exit(1);
}
outbuf = tmpbuf; /* use small buffer for temporary I/O */
while (byteswritten < romsize) {
if (bytesleftinfile <= 0 && !nosplit) {
/* we've filled up 2 megs of data, open a new set of files */
/* (!nosplit means that large files *are* split) */
fclose(lo);
fclose(hi);
/* change the file extension from .HI0 to .HI1, etc. */
hiext[3]++; loext[3]++;
/* re-open files */
hi = fopen_with_extension(filename, hiext, "wb");
lo = fopen_with_extension(filename, loext, "wb");
bytesleftinfile = SPLITSIZE;
}
/*
* HACK ALERT: fgetc() returns -1 (0xffff) on end of file.
* This is perfect for us, since we want to stuff FFs into the
* end of the buffer... so the lack of error checking below
* is *deliberate*
*/
outbuf[0] = fgetc(infile);
outbuf[1] = fgetc(infile);
outbuf[2] = fgetc(infile);
outbuf[3] = fgetc(infile);
fputc(outbuf[1],hi);
fputc(outbuf[0], hi);
fputc(outbuf[3], lo);
fputc(outbuf[2], lo);
byteswritten += 4;
bytesleftinfile -= 4;
}
fclose(infile);
fclose(hi);
fclose(lo);
}
/*
* Write an output file in 4xROM format; foo.U1 contains
* the first byte of each longword, foo.U2 contains the
* second byte, etc.
*/
void Write4xROM(char * filename, size_t srcoffset)
{
FILE *u1, *u2, *u3, *u4; /* file pointers for the four pieces respectively */
FILE *infile; /* input file */
size_t byteswritten; /* bytes of ROM written */
int c;
byte *outbuf;
/*
* create the output file buffers
*/
u1 = fopen_with_extension(filename, ".U1", "wb");
u2 = fopen_with_extension(filename, ".U2", "wb");
u3 = fopen_with_extension(filename, ".U3", "wb");
u4 = fopen_with_extension(filename, ".U4", "wb");
/*
* spit out the 0x2000 size header, with RSA signature etc.
*/
outbuf = inbuf;
for (byteswritten = 0; byteswritten < 0x2000; byteswritten += 4) {
fputc(outbuf[0], u4);
fputc(outbuf[1], u3);
fputc(outbuf[2], u2);
fputc(outbuf[3], u1);
outbuf += 4;
}
/*
* now send out the rest of the file
*/
infile = fopen(filename, "rb");
if (!infile) {
perror(filename);
exit(1);
}
if (fseek(infile, srcoffset, SEEK_SET))
{
perror(filename);
exit(1);
}
while (byteswritten < romsize) {
c = fgetc(infile);
fputc(c, u4);
c = fgetc(infile);
fputc(c, u3);
c = fgetc(infile);
fputc(c, u2);
c = fgetc(infile);
fputc(c, u1);
byteswritten += 4;
}
fclose(infile);
fclose(u1);
fclose(u2);
fclose(u3);
fclose(u4);
}
/*
* Write out as a single ROM cartridge image
*/
void Write1xROM(char * filename, size_t srcoffset)
{
FILE * u1; /* output file */
FILE * infile; /* input file */
size_t byteswritten; /* bytes of ROM written */
int c;
/*
* Create the output file buffers
*/
u1 = fopen_with_extension(filename, ".U1", "wb");
/*
* Spit out the 0x2000 size header, with RSA signature etc.
*/
for(byteswritten=0; byteswritten<0x2000; byteswritten++)
{
fputc(inbuf[byteswritten], u1);
}
/*
* Now send out the rest of the file
*/
infile = fopen(filename, "rb");
if (!infile)
{
perror(filename);
exit(1);
}
if (fseek(infile, srcoffset, SEEK_SET))
{
perror(filename);
exit(1);
}
while (byteswritten < romsize)
{
c = fgetc(infile);
fputc(c, u1);
byteswritten++;
}
fclose(infile);
fclose(u1);
}