| 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 |
#include <stdio.h> |
| 46 |
#include <stdlib.h> |
| 47 |
#include <string.h> |
| 48 |
#include <unistd.h> |
| 49 |
|
| 50 |
typedef unsigned char ubyte; |
| 51 |
typedef unsigned short uword; |
| 52 |
|
| 53 |
#define PERLINE 16 |
| 54 |
|
| 55 |
static uword getwlh(FILE *in); |
| 56 |
static void exiterr(const char *str); |
| 57 |
static void convert(short format, FILE *in, FILE *out); |
| 58 |
|
| 59 |
int |
| 60 |
main(int ac, char **av) |
| 61 |
{ |
| 62 |
short format; |
| 63 |
FILE *infile; |
| 64 |
FILE *outfile; |
| 65 |
|
| 66 |
if (ac < 3) { |
| 67 |
puts("FTOBIN format infile [outfile]"); |
| 68 |
puts("format 1,2, or 3. 3=raw"); |
| 69 |
puts("(C)Copyright 1988 by Matthew Dillon, All Rights Reserved"); |
| 70 |
exit(EXIT_FAILURE); |
| 71 |
} |
| 72 |
format = strtol(av[1], NULL, 0); |
| 73 |
if (format < 1 || format > 3) |
| 74 |
exiterr("specify infile format 1, 2, or 3"); |
| 75 |
if (format == 3) { |
| 76 |
puts("Note: Format 3 is already a raw binary file, output will"); |
| 77 |
puts("be equivalent to input"); |
| 78 |
} |
| 79 |
infile = fopen(av[2], "r"); |
| 80 |
if (infile == NULL) |
| 81 |
exiterr("unable to open input file"); |
| 82 |
outfile = (av[3]) ? fopen(av[3], "w") : stdout; |
| 83 |
if (outfile == NULL) |
| 84 |
exiterr("unable to open output file"); |
| 85 |
convert(format, infile, outfile); |
| 86 |
fclose(infile); |
| 87 |
fclose(outfile); |
| 88 |
return(0); |
| 89 |
} |
| 90 |
|
| 91 |
static |
| 92 |
void |
| 93 |
exiterr(const char *str) |
| 94 |
{ |
| 95 |
fputs(str, stderr); |
| 96 |
fputs("\n", stderr); |
| 97 |
exit(EXIT_FAILURE); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
static |
| 115 |
void |
| 116 |
convert(short format, FILE *in, FILE *out) |
| 117 |
{ |
| 118 |
uword org = 0; |
| 119 |
uword base = 0; |
| 120 |
long len; |
| 121 |
long maxseek = 0; |
| 122 |
ubyte buf[256]; |
| 123 |
|
| 124 |
if (format < 3) |
| 125 |
base = org = getwlh(in); |
| 126 |
if (format == 2) { |
| 127 |
len = getwlh(in); |
| 128 |
} else { |
| 129 |
long begin = ftell(in); |
| 130 |
fseek(in, 0L, 2); |
| 131 |
len = ftell(in) - begin; |
| 132 |
fseek(in, begin, 0); |
| 133 |
} |
| 134 |
for (;;) { |
| 135 |
if (len > 0) { |
| 136 |
while (len >= ((long) sizeof(buf))) { |
| 137 |
fread(buf, sizeof(buf), 1, in); |
| 138 |
fwrite(buf, sizeof(buf), 1, out); |
| 139 |
len -= sizeof(buf); |
| 140 |
org += sizeof(buf); |
| 141 |
} |
| 142 |
if (len) { |
| 143 |
fread(buf, (short)len, 1, in); |
| 144 |
fwrite(buf, (short)len, 1, out); |
| 145 |
org += len; |
| 146 |
} |
| 147 |
} |
| 148 |
if (format == 2) { |
| 149 |
if (maxseek < org - base) |
| 150 |
maxseek = org - base; |
| 151 |
org = getwlh(in); |
| 152 |
if (feof(in)) |
| 153 |
break; |
| 154 |
len = org - base; |
| 155 |
if (len < 0) { |
| 156 |
puts("ERROR! Reverse indexed to before beginning"); |
| 157 |
puts("*Initial* origin must be the lowest address"); |
| 158 |
return; |
| 159 |
} |
| 160 |
len -= maxseek; |
| 161 |
if (len > 0) |
| 162 |
memset(buf, 255, sizeof(buf)); |
| 163 |
while (len > 0) { |
| 164 |
if (len >= ((long) sizeof(buf))) { |
| 165 |
fwrite(buf, sizeof(buf), 1, out); |
| 166 |
maxseek += sizeof(buf); |
| 167 |
len -= sizeof(buf); |
| 168 |
} else { |
| 169 |
fwrite(buf, (short)len, 1, out); |
| 170 |
maxseek += len; |
| 171 |
len = 0; |
| 172 |
} |
| 173 |
} |
| 174 |
fseek(out, (long)(org - base), 0); |
| 175 |
len = getwlh(in); |
| 176 |
} else { |
| 177 |
break; |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
static |
| 183 |
uword |
| 184 |
getwlh(FILE *in) |
| 185 |
{ |
| 186 |
uword result; |
| 187 |
|
| 188 |
result = getc(in); |
| 189 |
result += getc(in) << 8; |
| 190 |
return(result); |
| 191 |
} |
| 192 |
|
| 193 |
|