Debian Codegraph @

src/ftobin.c

raw symbols SCIP
1 /*
2 the DASM macro assembler (aka small systems cross assembler)
3
4 Copyright (c) 1988-2002 by Matthew Dillon.
5 Copyright (c) 1995 by Olaf "Rhialto" Seibert.
6 Copyright (c) 2003-2008 by Andrew Davie.
7 Copyright (c) 2008 by Peter H. Froehlich.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
21 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24 /**
25 * @file
26 */
27
28 /*
29 * FTOBIN.C
30 *
31 * FTOBIN format infile [outfile]
32 *
33 * format: format used when assembling (asm705/asm65)
34 * 1,2,3 -generate sequenced (raw) binary file
35 *
36 * Used with formats 1 or 2 (format 3 is already raw binary). This
37 * program converts formats 1 and 2 to format 3. This is required
38 * because some assembly programs might want to reverse-index the
39 * origin (go back), usually to lay down a checksum, and this can only be
40 * done with format 2.
41 *
42 * Restrictions: Lowest address must be referenced first.
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 * Formats:
102 *
103 * 1: origin (word:lsb,msb) + data
104 * 2: origin (word:lsb,msb) + length (word:lsb,msb) + data (repeat)
105 * 3: data
106 *
107 * Hex output:
108 *
109 * :lloooo00(ll bytes hex code)cc ll=# of bytes
110 * oooo=origin
111 * cc=invert of checksum all codes
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 /* vim: set tabstop=4 softtabstop=4 expandtab shiftwidth=4 autoindent: */