-
Notifications
You must be signed in to change notification settings - Fork 2
/
zip2disk.c
295 lines (253 loc) · 6.88 KB
/
zip2disk.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
/**
* @file zip2disk.c
* Convert four Zip-Code files to a 1541 disk image
* @author Marko Mäkelä (marko.makela at iki.fi)
* @author Paul David Doherty ([email protected])
*/
/*
** Copyright © 1993-1997,2001,2006,2021,2022 Marko Mäkelä
** Original version © 1993 Paul David Doherty ([email protected])
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef MSDOS
/** Directory path separator character */
#define PATH_SEPARATOR '\\'
#else
/** Directory path separator character */
#define PATH_SEPARATOR '/'
#endif /* MSDOS */
/** Suffix for output file names */
static const char out_suffix[] = ".d64";
/** Input file */
static FILE* infile;
/** Output file */
static FILE* outfile;
/** number of sectors on the current track */
static unsigned max_sect;
/** current track number */
static unsigned track;
/** decoding buffer for current track */
static unsigned char trackbuf[21][256];
/** flag: which sectors on the current track have been decoded? */
static int trackbuf_decoded[21];
/** input file name */
static char* inname;
/** first character of the actual file name */
static char* fname;
/** output file name */
static char* outname;
/** Initialize the files
* @param filename the base file name
* @retval 0 on success
* @retval 1 on out of memory
* @retval 3 if no output could be created
*/
static int
init_files (const char* filename)
{
/* flag: was an output file name already specified? */
int outflag = !!outname;
size_t i = strlen (filename);
if (!(inname = malloc (i + 3)) ||
(!outname && !(outname = malloc (i + sizeof out_suffix))))
return 1;
/* copy the base filename */
memcpy (inname, filename, i + 1);
if (!outflag) {
memcpy (outname, filename, i);
memcpy (outname + i, out_suffix, sizeof out_suffix);
}
/* modify input filename */
for (fname = inname + i;
fname > inname && *fname != PATH_SEPARATOR; fname--);
if (fname > inname)
fname++;
memmove (fname + 2, fname, i + 1 - (size_t) (fname - inname));
fname[1] = '!';
/* try to create output file */
if (!(outfile = fopen (outname, "wb"))) {
return 3;
}
return 0;
}
/** Open an input file
* @param number ASCII number of the input file
* @return 0 on success; 1 on error
*/
static int
open_file (char number)
{
*fname = number;
if (infile)
fclose (infile);
if (!(infile = fopen (inname, "rb")) ||
-1 == fseek (infile, (number == '1') ? 4 : 2, 0))
return 1;
return 0;
}
/** Decode a sector
* @return 0 on success; 1 on failure
*/
static int
read_sector (void)
{
int trk, sec, ch;
trk = fgetc (infile);
sec = fgetc (infile);
if ((unsigned) (trk & 0x3f) != track || sec < 0 ||
(unsigned) sec >= max_sect ||
trackbuf_decoded[sec]) {
Error:
fprintf (stderr, "zip2disk: Input file %s is corrupted.\n", inname);
return 1;
}
trackbuf_decoded[sec] = 1;
/* RLE compressed sector */
if (trk & 0x80) {
/* number of decompressed bytes */
int count = 0;
/* length of the compressed stream, and the escape character */
int len = fgetc (infile), esc = fgetc (infile);
if (len == EOF || esc == EOF)
goto Error;
while (len--) {
ch = fgetc (infile);
if (ch == EOF)
goto Error;
if (ch != esc) {
if (count > 255)
goto Error;
trackbuf[sec][count++] = (unsigned char) ch;
}
else if (len >= 2) {
/* escape character: get the number of repetitions */
int repnum = fgetc (infile);
/* get the repeated character */
ch = fgetc (infile);
if (repnum < 0 || repnum + count > 256 || ch == EOF)
goto Error;
memset (trackbuf[sec] + count, ch, (unsigned) repnum);
count += repnum;
len -= 2;
}
else
goto Error;
}
if (count != 256)
goto Error;
}
/* whole sector filled with a single character */
else if (trk & 0x40) {
if ((ch = fgetc (infile)) == EOF)
goto Error;
memset (trackbuf[sec], ch, 256);
}
/* no compression */
else if (256 != fread (trackbuf[sec], 1, 256, infile))
goto Error;
return 0;
}
/** Decode a track
* @return 0 on success; 1 on failure
*/
static int
read_track (void)
{
unsigned s;
memset (trackbuf_decoded, 0, sizeof trackbuf_decoded);
for (s = max_sect; s--; )
if (read_sector ())
return 1;
return 0;
}
/** Main program
* @param argc number of command-line arguments
* @param argv contents of command-line arguments
* @retval 0 on success
* @retval 1 on usage error
* @retval 2 on out of memory
* @retval 3 on input or output error
* @retval 4 on ZipCode format error
*/
int
main (int argc, char** argv)
{
int status;
if (argc != 2 && argc != 3) {
fputs ("ZipCode disk image extractor v1.2.2\n"
"Usage: zip2disk zip_image_name [disk_image_name]\n", stderr);
return 1;
}
outname = (argc == 3) ? argv[2] : 0;
switch (init_files (argv[1])) {
case 3:
fprintf (stderr, "zip2disk: Could not create %s.\n", outname);
status = 3;
goto ErrExit;
case 1:
fputs ("zip2disk: Out of memory.\n", stderr);
status = 2;
goto ErrExit;
}
for (track = 1; track <= 35; track++) {
max_sect = 17U + ((track < 31) ? 1U : 0U) + ((track < 25) ? 1U : 0U) +
((track < 18) ? 2U : 0U);
switch (track) {
case 1:
if (open_file ('1')) {
OpenError:
fprintf (stderr, "zip2disk: Error in opening file %s.\n", inname);
status = 3;
goto ErrExit;
}
break;
case 9:
if (open_file ('2'))
goto OpenError;
break;
case 17:
if (open_file ('3'))
goto OpenError;
break;
case 26:
if (open_file ('4'))
goto OpenError;
break;
}
if (read_track ()) {
fclose (infile);
fclose (outfile);
status = 4;
goto ErrExit;
}
if (max_sect != fwrite (trackbuf, 256, max_sect, outfile)) {
fclose (infile);
fclose (outfile);
fputs ("zip2disk: Error in writing the output file.\n", stderr);
status = 3;
goto ErrExit;
}
}
fclose (infile);
fclose (outfile);
status = 0;
ErrExit:
free (inname);
return status;
}