forked from MagerValp/CGTerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xfer.c
427 lines (359 loc) · 8.76 KB
/
xfer.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <SDL.h>
#include "net.h"
#include "timer.h"
#include "gfx.h"
#include "menu.h"
#include "xfer.h"
#include "xmodem.h"
#include "punter.h"
#include "diskimage.h"
#include "config.h"
char *xfer_tempdlname = "download.tmp";
char *xfer_tempulname = "upload.tmp";
char xfer_filename[256];
FILE *xfer_sendfile, *xfer_recvfile;
Direction xfer_direction;
Protocol xfer_protocol;
int xfer_cancel;
int xfer_saved_bytes;
int xfer_file_size;
unsigned char xfer_buffer[4096];
unsigned int xfer_starttime;
unsigned int xfer_last_kbd_check;
//int xfer_debug = 1;
void xfer_check_kbd(void) {
SDL_Event event;
if (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
exit(1);
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym) {
case SDLK_ESCAPE:
xfer_cancel = 1;
break;
default:
break;
}
break;
}
}
}
void xfer_send_byte(unsigned char c) {
unsigned int t;
t = timer_get_ticks();
if (t > xfer_last_kbd_check + 20) {
xfer_last_kbd_check = t;
xfer_check_kbd();
}
/*
if (xfer_debug) {
t -= xfer_starttime;
printf("xfer_send_byte[%3d.%03d]: %02x\n", t / 1000, t % 1000, c);
}
*/
net_send(c);
}
signed int xfer_recv_byte(int timeout) {
signed int c;
unsigned int starttime, t;
starttime = timer_get_ticks();
while ((c = net_receive()) == -1) {
if (timer_get_ticks() > starttime + timeout) {
t = timer_get_ticks() - xfer_starttime;
printf("xfer_recv_byte[%3d.%03d]: timeout\n", t / 1000, t % 1000);
return(-1);
} else {
timer_delay(1);
if (timer_get_ticks() > xfer_last_kbd_check + 20) {
xfer_check_kbd();
xfer_last_kbd_check = timer_get_ticks();
}
}
}
/*
if (xfer_debug) {
t = timer_get_ticks() - xfer_starttime;
printf("xfer_recv_byte[%3d.%03d]: %02x\n", t / 1000, t % 1000, c);
}
*/
return(c);
}
signed int xfer_recv_byte_error(int timeout, int errorcnt) {
signed int c;
while ((c = xfer_recv_byte(timeout)) == -1 && errorcnt) {
--errorcnt;
}
return(c);
}
int xfer_save_data(unsigned char *data, int length) {
int l;
int written = 0;
while (written < length) {
if ((l = fwrite(data + written, 1, length - written, xfer_recvfile))) {
written += l;
} else {
return(0);
}
}
xfer_saved_bytes += written;
return(written);
}
int xfer_load_data(unsigned char *data, int length) {
int l;
int read = 0;
while (read < length) {
if ((l = fread(data + read, 1, length - read, xfer_sendfile))) {
read += l;
} else {
if (ferror(xfer_sendfile)) {
return(0);
} else {
return(read);
}
}
}
return(read);
}
int xfer_recv(void) {
int status = 0;
xfer_filename[0] = 0;
xfer_cancel = 0;
xfer_starttime = timer_get_ticks();
menu_draw_xfer_progress("No filename", xfer_direction, xfer_protocol);
menu_show();
gfx_vbl();
if ((xfer_recvfile = fopen(xfer_tempdlname, "wb"))) {
xfer_saved_bytes = 0;
if (xfer_protocol == PROT_XMODEM) {
status = xmodem_recv(0);
} else if (xfer_protocol == PROT_XMODEMCRC) {
status = xmodem_recv(1);
} else if (xfer_protocol == PROT_XMODEM1K) {
status = xmodem_recv(1);
} else if (xfer_protocol == PROT_PUNTER) {
status = punter_recv();
}
fclose(xfer_recvfile);
return(status);
} else {
menu_draw_message("Couldn't open tempfile!");
return(0);
}
}
int xfer_copy_from_image(char *imgname, char *src, char *dest) {
DiskImage *di;
ImageFile *imgfile;
unsigned char rawname[16];
FILE *outh;
char buffer[4096];
int l;
if ((di = di_load_image(imgname)) == NULL) {
return(0);
}
di_rawname_from_name(rawname, src);
if ((imgfile = di_open(di, rawname, T_PRG, "rb")) == NULL) {
di_free_image(di);
return(0);
}
if ((outh = fopen(dest, "wb")) == NULL) {
di_close(imgfile);
di_free_image(di);
}
while ((l = di_read(imgfile, buffer, 4096))) {
if (fwrite(buffer, 1, l, outh) != l) {
di_close(imgfile);
di_free_image(di);
fclose(outh);
return(0);
}
}
di_close(imgfile);
di_free_image(di);
fclose(outh);
return(1);
}
void xfer_send(char *filename) {
char name[256];
char *p;
int deletetmp = 0;
xfer_cancel = 0;
xfer_starttime = timer_get_ticks();
menu_draw_xfer_progress(filename, xfer_direction, xfer_protocol);
menu_show();
gfx_vbl();
if ((p = strrchr(cfg_xferdir, '.')) && strlen(p) == 4 && (p[1] == 'd' || p[1] == 'D') && isdigit(p[2]) && isdigit(p[3])) {
if (xfer_copy_from_image(cfg_xferdir, filename, xfer_tempulname)) {
strcpy(name, xfer_tempulname);
deletetmp = 1;
} else {
menu_draw_message("Couldn't open file!");
menu_show();
gfx_vbl();
return;
}
} else {
strcpy(name, cfg_xferdir);
#ifdef WINDOWS
strcat(name, "\\");
#else
strcat(name, "/");
#endif
strcat(name, filename);
}
if ((xfer_sendfile = fopen(name, "rb"))) {
if (fseek(xfer_sendfile, 0, SEEK_END)) {
fclose(xfer_sendfile);
menu_draw_message("Couldn't read file size!");
menu_show();
gfx_vbl();
return;
}
xfer_file_size = ftell(xfer_sendfile);
fseek(xfer_sendfile, 0, SEEK_SET);
if (xfer_protocol == PROT_XMODEM) {
xmodem_send(0);
} else if (xfer_protocol == PROT_XMODEMCRC) {
xmodem_send(0);
} else if (xfer_protocol == PROT_XMODEM1K) {
xmodem_send(1);
} else if (xfer_protocol == PROT_PUNTER) {
menu_draw_message("Not implemented yet");
gfx_vbl();
}
fclose(xfer_sendfile);
} else {
menu_draw_message("Couldn't open file!");
menu_show();
gfx_vbl();
}
if (deletetmp) {
remove(name);
}
}
void xfer_save_file_in_image(char *filename) {
FILE *from;
DiskImage *di;
ImageFile *to;
unsigned char buf[4096];
int bytesleft, l;
unsigned char rawname[16];
if ((di = di_load_image(cfg_xferdir)) == NULL) {
menu_draw_message("Couldn't open disk image");
menu_show();
gfx_vbl();
return;
}
if ((from = fopen(xfer_tempdlname, "rb")) == NULL) {
menu_draw_message("Couldn't open temp file!");
menu_show();
gfx_vbl();
return;
}
di_rawname_from_name(rawname, filename);
to = di_open(di, rawname, T_PRG, "wb");
if (to == NULL) {
fclose(from);
di_free_image(di);
menu_draw_message("Couldn't write file!");
menu_show();
gfx_vbl();
return;
}
bytesleft = xfer_saved_bytes;
while (bytesleft) {
l = fread(buf, 1, 4096, from);
if (ferror(from)) {
menu_draw_message("Read error!");
goto done;
}
if (di_write(to, buf, l) != l) {
menu_draw_message("Write error!");
goto done;
}
bytesleft -= l;
}
if (bytesleft != 0) {
menu_draw_message("I/O error!");
goto done;
}
remove(xfer_tempdlname);
sprintf(buf, "Saved %-24s", filename);
menu_draw_message(buf);
done:
menu_show();
gfx_vbl();
fclose(from);
di_close(to);
di_free_image(di);
}
void xfer_save_file_in_dir(char *filename) {
FILE *from, *to;
unsigned char buf[4096];
int bytesleft, l;
char name[256];
if ((from = fopen(xfer_tempdlname, "rb")) == NULL) {
menu_draw_message("Couldn't open temp file!");
menu_show();
gfx_vbl();
return;
}
strcpy(name, cfg_xferdir);
#ifdef WINDOWS
strcat(name, "\\");
#else
strcat(name, "/");
#endif
strcat(name, filename);
if ((to = fopen(name, "wb")) == NULL) {
fclose(from);
menu_draw_message("Couldn't write file!");
menu_show();
gfx_vbl();
return;
}
bytesleft = xfer_saved_bytes;
while (bytesleft) {
l = fread(buf, 1, 4096, from);
if (ferror(from)) {
menu_draw_message("Read error!");
goto done;
}
bytesleft -= fwrite(buf, 1, l, to);
if (ferror(to)) {
menu_draw_message("Write error!");
goto done;
}
}
if (bytesleft != 0) {
menu_draw_message("I/O error!");
goto done;
}
remove(xfer_tempdlname);
sprintf(buf, "Saved %-24s", filename);
menu_draw_message(buf);
done:
menu_show();
gfx_vbl();
fclose(from);
fclose(to);
}
void xfer_save_file(char *filename) {
char *p;
if ((p = strrchr(cfg_xferdir, '.'))) {
if (strlen(p) == 4) {
if (p[1] == 'd' || p[1] == 'D') {
if (isdigit(p[2]) && isdigit(p[3])) {
xfer_save_file_in_image(filename);
return;
}
}
}
}
xfer_save_file_in_dir(filename);
}