-
Notifications
You must be signed in to change notification settings - Fork 1
/
putfile.c
569 lines (537 loc) · 15.6 KB
/
putfile.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
#if 0
===========================================================================
P U T F I L E . C
File reader for the mixit program.
Copyright 1991 Atari Games. All rights reserved.
Author: Lyle Rains & Jim Petrick
===========================================================================
#endif
/*
* Imported objects, declarations and definitions:
* stdio.h: NULL, size_t, fprintf(), fread(), fwrite()
* string.h: memset()
* stdlib.h: EXIT_FAILURE, EXIT_SUCCESS, exit(), malloc(), free()
* image.h: LogicalAddr, BlockGroup, MemBlock, Chunk, Page, Image,
* imageWrite(), imageFree()
*/
#include "mixit.h"
#define DEFAULT_BUFLEN 0x1000
extern int debug;
#if !defined(fileno)
extern int fileno(); /* non-ANSI */
#endif
FILE *fout = (FILE *)NULL;
ulong last_address, base_address;
uchar *out_buf;
int out_bufsize;
char current_fname[80];
#ifdef TEST
extern int main(int argc, char **argv);
#endif
#ifdef VMS
#define FOPEN_ARG_VLDA "wb", "rfm=var", "ctx=rec", "mrs=512"
#define FOPEN_ARG_IMG "wb", "rfm=fix", "mrs=512"
#define FOPEN_ARG_APPND "wb"
#define FOPEN_ARG_TXT "w"
#else
#ifdef sun
#define FOPEN_ARG_VLDA "w"
#define FOPEN_ARG_IMG "w"
#define FOPEN_ARG_APPND "w+"
#define FOPEN_ARG_TXT "w"
#else
#define FOPEN_ARG_VLDA "wb"
#define FOPEN_ARG_IMG "wb"
#define FOPEN_ARG_APPND "wb+"
#define FOPEN_ARG_TXT "w"
#endif /* sun */
#endif /* VMS */
/*
#ifdef VMS
if ( gpf->rec_type == GPF_K_VLDA )
fout = fopen(fname, FOPEN_ARG_VLDA);
else if ( gpf->rec_type == GPF_K_LDA || gpf->rec_type == GPF_K_DUMP || gpf->rec_type == GPF_K_IMG || gpf->rec_type == GPF_K_DIO )
fout = fopen(fname, FOPEN_ARG_IMG);
else
#else
if ( gpf->rec_type == GPF_K_VLDA || gpf->rec_type == GPF_K_LDA || gpf->rec_type == GPF_K_DUMP || gpf->rec_type == GPF_K_IMG || gpf->rec_type == GPF_K_DIO || gpf->rec_type == GPF_K_CPE )
if ( gpf->rec_type == GPF_K_DIO )
#ifdef sun
fout = fopen(fname, "w+");
#else
fout = fopen(fname, "wb+");
#endif // sun
else
fout = fopen(fname, "wb");
#endif // VMS
else
fout = fopen(fname, "w");
if ( !fout )
return perr_return(0, "Can't open '%s'", fname);
strcpy(current_fname, fname);
rec_count = 0;
}
}
*/
static int openOutput(const char *fname, const GPF *gpf)
{
const char *fopenArg=NULL;
switch (gpf->rec_type)
{
case GPF_K_VLDA: /* Atari .VLDA or .VLD (32 bit addresses: I/O binary) */
fopenArg = FOPEN_ARG_VLDA;
break;
case GPF_K_DIO: /* Atari DataIO .DIO (I/O binary) */
fopenArg = FOPEN_ARG_APPND;
break;
case GPF_K_DUMP: /* mixit dump .DUMP: .DUM or .DMP (O only same as IMG) */
case GPF_K_IMG: /* Anything else is assumed image format (I/O binary) */
case GPF_K_LDA: /* RT11 .LDA (16 bit addresses: I/O binary) */
case GPF_K_COFF: /* Generic COFF .COFF (I/O binary) */
case GPF_K_ELF: /* Generic ELF .ELF (I/O binary) */
case GPF_K_CPE: /* Sony Playstation .CPE (I/O binary) */
fopenArg = FOPEN_ARG_IMG;
break;
case GPF_K_ROM: /* mixit .ROM (I/O ASCII) */
case GPF_K_MAC: /* macxx .MAC (O only ASCII)*/
case GPF_K_HEX: /* TekHex .HEX (I/O ASCII) */
case GPF_K_DLD: /* MOS Technology? .DLD (I/O ASCII) */
case GPF_K_GNU: /* GNU .ASM or .AS68K () (O only ASCII) */
case GPF_K_INTEL: /* Intel .INTEL (I/O ASCII) */
case GPF_K_MOT: /* Motorola .MOT (I/O ASCII) */
default:
fopenArg = FOPEN_ARG_TXT;
break;
}
if ( fopenArg )
{
fout = fopen(fname, fopenArg);
if ( !fout )
{
return perr_return(0, "Failed fopen('%s','%s')", fname, fopenArg);
}
}
else
{
return err_return(0, "Failed to fopen('%s',NULL). No fopen argument. rec_type=%d", gpf->rec_type);
}
return 1;
}
/*==========================================================================*
* group_data - Handles the /GROUP option by copying a source array to a
* destination array, but only copying the GROUP'ed bytes.
*==========================================================================*/
static int group_data(uchar *dest, uchar *src, int count,
int bytes_per_word, int group_code)
{
int i;
uchar *dstSav = dest;
src += group_code; /* For LSB -> MSB data */
for ( i = 0; i < count; i += bytes_per_word )
{
*dest++ = *src;
src += bytes_per_word;
}
return dest-dstSav;
} /* end group_data */
/*==========================================================================*
* swap_data - Handles the /SWAP option by swapping bytes in the given
* array governed by bytes_per_word.
*==========================================================================*/
static int swap_data(uchar *dest, int count, int bytes_per_word)
{
int i, j;
uchar temp, *dstSav=dest;
if ( count % bytes_per_word )
{
moan("Can't swap %d byte words in a %d byte record", bytes_per_word,
count);
return 0;
}
while ( count > 0 )
{
j = bytes_per_word - 1;
for ( i = 0; i < bytes_per_word / 2; ++i, --j )
{
temp = dest[i];
dest[i] = dest[j];
dest[j] = temp;
}
count -= bytes_per_word;
dest += bytes_per_word;
}
return dest-dstSav;
} /* end swap_data */
#if 0
/*==========================================================================*
* evenodd_data - Handles the /EVEN_WORDS and /ODD_WORDS options by copying
* a source array to a destination array, but only copying the
* half of the words.
*==========================================================================*/
static int evenodd_data(uchar *dest, uchar *src, int count, short bytes_per_word, int odd)
{
int ii, half;
uchar *dstSav=dest;
half = bytes_per_word / 2;
if ( half == 0 )
{
moan("Can't take even/odd %d byte words from a %d byte word", half, bytes_per_word);
return 0;
}
if ( count % bytes_per_word )
{
moan("Can't take (%d byte) words from a %d byte record", half, count);
return 0;
}
if ( odd )
src += half;
for ( ii = 0; ii < count; ii += bytes_per_word )
{
memcpy(dest, src, half);
dest += half;
src += bytes_per_word;
}
return dest-dstSav;
} /* end evenodd_data */
#endif
static int mungBuffer(GPF *gpf, uchar *dstBuf, int bufLen, ulong inpAddr)
{
/* Mung the entire input buffer according to the flags */
if ( (gpf->flags & (GPF_M_GROUP|GPF_M_SWAP)) )
{
if ( (gpf->flags & GPF_M_GROUP) )
{
bufLen = group_data(dstBuf, dstBuf, bufLen, gpf->bytes_per_word, gpf->group_code);
}
else if ( (gpf->flags & GPF_M_SWAP) )
{
bufLen = swap_data(dstBuf, bufLen, gpf->bytes_per_word);
}
#if 0
else if ( (gpf->flags & (GPF_M_EVENW | GPF_M_ODDW)) )
{
int jj;
jj = inpAddr & 1;
jj = jj ^ ((gpf->flags & GPF_M_ODDW) != 0);
bufLen = evenodd_data(dstBuf, dstBuf, bufLen, 2, jj);
}
#endif
}
return bufLen;
}
/*==========================================================================*
| Dump the Image to a file.
*==========================================================================*/
int putfile(char *fname, GPF *gpf)
{
Page *page;
int (*PutRec)(FILE *, uchar *, int, ulong) = NULL;
int (*PutFoot)(FILE *) = NULL;
int (*PutXfer)(FILE *, ulong) = NULL;
int (*PutSym)(FILE *, uchar *, int) = NULL;
int (*PutHead)(FILE *, ulong, ulong) = NULL;
int status;
uint rBytes; /* Bytes per output record desired */
uint rMax;
uchar *buffer;
LogicalAddr lo_range, hi_range, lo_ask, lo;
static ulong rec_count;
LogicalAddr inpOffset;
LogicalAddr outAddr;
uint outLo, outHi, outCount;
if ( !(gpf->flags & GPF_M_APPND) )
{
/* Determine type of the output file */
if ( gpf->rec_type == 0 )
{
if ( (gpf->rec_type = getFormat(fname)) == GPF_K_UNKNOWN )
return err_return(0, "File '%s' of unknown type", fname);
}
if ( !fout )
{
/* File is not yet open so open it */
if ( !openOutput(fname, gpf) )
return 0;
strcpy(current_fname, fname);
rec_count = 0;
}
}
if ( (gpf->flags & GPF_M_DISPL) )
{
#if 0
if ( isatty(fileno(errFile)) )
fputs("\n", errFile);
#endif
printf("%s file '%s' . . .\n", (gpf->flags & (GPF_M_APPND | GPF_M_EOFONLY)) ? "Appending to" : "Creating", fname);
}
/*
* Currently, only .VLDA and .HEX output symbols.
*/
switch (gpf->rec_type) /* Determine output record function */
{
case GPF_K_CPE:
PutRec = PutRec_cpe;
PutHead = PutHead_cpe;
PutFoot = PutFoot_cpe;
PutXfer = PutXfer_cpe;
rBytes = 512 - 1 - 4 - 4;
rMax = 512 - 1 - 4 - 4;
break;
case GPF_K_VLDA:
PutRec = PutRec_vlda;
PutSym = PutSym_vlda;
rBytes = 512 - 7;
rMax = 512 - 7;
break;
case GPF_K_HEX:
PutRec = PutRec_tekhex;
PutSym = PutSym_tekhex;
PutFoot = PutFoot_tekhex;
rBytes = 32;
rMax = 121;
break;
case GPF_K_MOT:
PutRec = PutRec_mot;
PutFoot = PutFoot_mot;
rBytes = 32;
rMax = 246;
break;
case GPF_K_GNU:
PutRec = PutRec_asm;
PutHead = PutHead_asm;
rBytes = 16;
rMax = 80;
break;
case GPF_K_LDA:
PutRec = PutRec_lda;
PutFoot = PutFoot_lda;
rBytes = 249;
rMax = 512;
break;
case GPF_K_MAC:
PutRec = PutRec_mac;
PutHead = PutHead_mac;
rBytes = 16;
rMax = 80;
break;
case GPF_K_INTEL:
PutRec = PutRec_intel;
PutFoot = PutFoot_intel;
rBytes = 32;
rMax = 246;
/* ??? */ break;
case GPF_K_DLD:
PutRec = PutRec_dld;
PutFoot = PutFoot_dld;
rBytes = 32;
rMax = 121;
break;
case GPF_K_ROM:
PutRec = PutRec_rom;
PutHead = PutHead_rom;
rBytes = 16;
rMax = 80;
break;
case GPF_K_DIO:
PutHead = PutHead_dio;
PutFoot = PutFoot_dio;
PutRec = PutRec_dio;
rBytes = 256;
rMax = 256;
if ( !(gpf->flags & GPF_M_FILL) )
{
gpf->flags |= GPF_M_FILL;
gpf->fill_char = 0;
}
break;
case GPF_K_DUMP:
case GPF_K_IMG:
PutRec = PutRec_img;
rBytes = 256;
rMax = 512;
if ( !(gpf->flags & GPF_M_FILL) )
{
gpf->flags |= GPF_M_FILL;
gpf->fill_char = 0;
}
break;
default:
moan("Output Type not implemented: '%s'", fname);
return 0;
}
if ( gpf->rec_size ) /* Explicit record size wanted*/
rBytes = gpf->rec_size;
if ( rBytes > rMax )
rBytes = rMax;
/* Create a buffer to hold the output recs */
if ( (uint)out_bufsize < rMax + 20 )
{
if ( out_buf )
free(out_buf);
out_bufsize = 0;
out_buf = NULL;
}
if ( !out_buf )
{
if ( !(out_buf = (uchar *)malloc(out_bufsize = rMax + 20)) )
return err_return(0, "Can't allocate %d bytes for output record", out_bufsize);
}
if ( (gpf->flags & GPF_M_EOFONLY) ) /* Output footer on EOF ONLY */
{
int rv = (PutFoot) ? PutFoot(fout) : 1;
fclose(fout);
fout = 0;
if ( out_buf )
free(out_buf);
out_bufsize = 0;
out_buf = NULL;
return rv;
}
/* lo_range is relocated low limit */
lo_range = gpf->low_limit;
/* hi_range is relocated high limit */
hi_range = gpf->high_limit;
/* Put out any header needed */
if ( !(gpf->flags & GPF_M_APPND) && PutHead )
PutHead(fout, lo_range, hi_range);
/* Output the transfer address */
if ( PutXfer && gpf->xfer_add )
PutXfer(fout, gpf->xfer_add);
/* Output the symbol records */
if ( (gpf->flags & GPF_M_SYMBOL) && PutSym )
{
for ( page = gpf->image.symbolList; page; page = page->next )
PutSym(fout, (uchar *)page->data, page->end + 1);
}
/* Assume failure */
status = 0;
/* Create a buffer for boundary chunks */
if ( !(buffer = malloc(rBytes)) )
return err_return(0, "Can't allocate temp buffer for putfile");
/* Now output the data records */
if ( !init_reader(gpf) )
return err_return(0, "No data found to output to: %s", fname);
outCount = 0;
outLo = gpf->out_add;
outHi = outLo;
for ( lo_ask = lo_range; lo_ask <= hi_range;)
{
int bytes, err;
err = readImage(gpf, buffer, rBytes, lo_ask, hi_range, &lo, &bytes);
if ( debug )
{
printf("putfile(): readImage(..,rBytes=%d, lo_ask=0x%lX, hi_range=0x%lX, lo=0x%lX, bytes=%d) returned %d\n",
rBytes, lo_ask, hi_range, lo, bytes,
err);
}
if ( err == -1 )
{
moan("Couldn't read %d bytes from image[%d:%d]", rBytes, lo_ask, hi_range);
goto err_exit;
}
else if ( bytes )
{
/* FYI: The contents of buffer have already been relocated by the input function(s) */
/* I.e. gpf->low_limit has been set to gpf->out_add before we get here */
inpOffset = lo - gpf->low_limit;
if ( lo + bytes > hi_range+1 )
bytes = (hi_range+1)-lo; /* clip to the end of data (padding, if desired, happens below) */
/* FYI: The data stored in memory remains byte orientated. If word_size is not 8, the output address and data remain to be converted */
outAddr = inpOffset / gpf->bytes_per_word + gpf->out_add;
/* Do any required conversions of the input and return the number of bytes to output */
outCount = mungBuffer(gpf,buffer,bytes,inpOffset);
if ( debug )
printf("putfile(): After Mung(). Calling PutRec(). lo_ask=0x%lX, hi_range=0x%lX, low_limit=0x%lX, bytes=%d, lo=0x%lX, inpOffset=0x%lX, outAddr=0x%lX, outCount=0x%X, err=%d\n",
lo_ask, hi_range, gpf->low_limit, bytes, lo, inpOffset, outAddr, outCount, err );
if ( PutRec(fout, buffer, outCount, outAddr) )
{
++rec_count;
if ( debug )
{
int ii;
printf("Output %ld: 1 %d-byte record, asked 0x%lX-0x%lX, found 0x%lX-0x%lX, output to 0x%lX-0x%lX\n\t",
rec_count, outCount,
lo_ask, lo_ask + bytes - 1,
lo, lo+bytes-1,
outAddr, outAddr+outCount-1);
for ( ii = 0; ii < (outCount > 16 ? 16 : outCount); ++ii )
{
printf(" %02X", (unsigned char)buffer[ii]);
}
if ( outCount > 16 )
printf(" ...");
printf("\n");
}
}
else
{
moan("PutRec failure\n");
goto err_exit;
}
}
lo_ask = lo + bytes;
if ( outAddr+outCount > outHi )
outHi = outAddr + outCount;
if ( err == 0 )
break;
}
/* Only image and DIO files require padding */
if ( !(gpf->flags & GPF_M_NOPAD) && (gpf->rec_type == GPF_K_IMG || gpf->rec_type == GPF_K_DIO) && rBytes && hi_range < ~1l && lo_ask <= hi_range )
{
lo_ask = (lo_ask - lo_range) / gpf->bytes_per_word + gpf->out_add;
hi_range = (hi_range - lo_range) / gpf->bytes_per_word + gpf->out_add;
if ( debug )
printf("putfile(): Padding file. lo_ask=0x%lX, hi_range=0x%lX (%ld bytes left), rBytes=%d, bytes_per_word=%d\n",
lo_ask, hi_range, hi_range-lo_ask+1, rBytes, gpf->bytes_per_word);
memset(buffer, (gpf->flags & GPF_M_FILL) ? gpf->fill_char : 0, rBytes);
while ( lo_ask <= hi_range )
{
int amount;
amount = rBytes;
if ( (unsigned int)amount > hi_range - lo_ask + 1 )
amount = (int)(hi_range - lo_ask + 1);
if ( amount )
{
if ( PutRec(fout, buffer, amount, lo_ask) )
{
++rec_count;
if ( debug )
{
printf("Output %ld: %d %d-byte filler record, %08lX-%08lX\n", rec_count, 1, amount, lo_ask, lo_ask + amount - 1);
}
}
else
{
moan("PutRec failure\n");
goto err_exit;
}
lo_ask += amount;
}
}
}
status = 1; /* Signal success */
err_exit:
/* Clean out the old image */
imageFree(&gpf->image);
free(buffer);
return status;
}
/* end putfile */
#ifdef TEST
/*==========================================================================*/
int main(int argc, char** argv)
{
GPF gpf;
if ( argc != 3 )
{
fprintf(errFile, "Usage: mytest infile outfile\n");
exit(EXIT_FAILURE);
}
if ( !getfile(argv[1], &gpf) )
exit(1);
if ( !putfile(argv[2], &gpf) )
moan("Putfile failure");
imageFree(&gpf.image);
exit(EXIT_SUCCESS);
} /* end main */
#endif