forked from MagerValp/CGTerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmodem.c
403 lines (353 loc) · 9.26 KB
/
xmodem.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
#include <string.h>
#include <SDL.h>
#include "gfx.h"
#include "menu.h"
#include "xfer.h"
#include "crc.h"
#define XM_SOH 0x01
#define XM_STX 0x02
#define XM_EOT 0x04
#define XM_ACK 0x06
#define XM_NAK 0x15
#define XM_CAN 0x18
#define XM_PAD 0x1a
#define XM_C 0x43
void xmodem_fail(char *message) {
//printf("xmodem_fail: %s\n", message);
menu_update_xfer_progress(message, xfer_saved_bytes, 0);
gfx_vbl();
while (xfer_recv_byte(1000) >= 0);
xfer_send_byte(XM_CAN);
xfer_send_byte(XM_CAN);
}
void xmodem_retry(char *message) {
//printf("xmodem_retry: %s\n", message);
menu_update_xfer_progress(message, xfer_saved_bytes, 0);
gfx_vbl();
while (xfer_recv_byte(1000) >= 0);
xfer_send_byte(XM_NAK);
}
int xmodem_checksum(int blocksize, int usecrc) {
int i;
unsigned short remotecrc;
unsigned char checksum = 0;
unsigned short crc = 0;
if (blocksize == 128 && usecrc == 0) {
for (i = 0; i < 128; ++i) {
checksum += xfer_buffer[i];
}
//printf("xfer_checksum: %02x (%02x)\n\n", checksum, xfer_buffer[i]);
if (checksum == xfer_buffer[i]) {
return(1);
} else {
return(0);
}
} else if (blocksize == 128 && usecrc) {
crc = crc16_calc(xfer_buffer, 128);
remotecrc = (xfer_buffer[128]<<8) | xfer_buffer[129];
//printf("xfer_crc16: %04x (%04x)\n\n", crc, remotecrc);
if (crc == remotecrc) {
return(1);
} else {
return(0);
}
} else if (blocksize == 1024 && usecrc) {
crc = crc16_calc(xfer_buffer, 1024);
remotecrc = (xfer_buffer[1024]<<8) | xfer_buffer[1025];
//printf("xfer_crc16: %04x (%04x)\n\n", crc, remotecrc);
if (crc == remotecrc) {
return(1);
} else {
return(0);
}
} else {
return(0);
}
}
static unsigned char xmodem_buffer[1024];
static unsigned int xmodem_buffer_len = 0;
int xmodem_save_data(unsigned char *data, int length) {
if (data) {
if (xmodem_buffer_len) {
// flush previous block
if (xfer_save_data(xmodem_buffer, xmodem_buffer_len) != xmodem_buffer_len) {
return(0); // ouch
}
}
// buffer new block
memcpy(xmodem_buffer, data, length);
xmodem_buffer_len = length;
} else { // strip padding and flush
if (xmodem_buffer_len) {
while (xmodem_buffer_len && xmodem_buffer[xmodem_buffer_len - 1] == XM_PAD) {
--xmodem_buffer_len;
}
if (xmodem_buffer_len) {
if (xfer_save_data(xmodem_buffer, xmodem_buffer_len) != xmodem_buffer_len) {
return(0); // ouch
}
}
}
}
return(length);
}
int xmodem_recv(int usecrc) {
int c;
int blocknum, bufctr, errorcnt, blocksize;
int nexthandshake;
xmodem_buffer_len = 0;
blocknum = 1;
if (usecrc) {
nexthandshake = XM_C;
} else {
nexthandshake = XM_NAK;
}
xfer_send_byte(nexthandshake);
menu_update_xfer_progress("Starting...", xfer_saved_bytes, 0);
gfx_vbl();
for (;;) {
if (xfer_cancel) {
xmodem_fail("Cancelling...");
return(0);
}
errorcnt = 0;
while ((c = xfer_recv_byte(10000)) == -1 && errorcnt < 10) {
//printf("Timeout...\n");
xfer_send_byte(nexthandshake);
++errorcnt;
}
nexthandshake = XM_NAK;
switch (c) {
case -2:
// connection closed
xmodem_fail("Disconnected!");
return(0);
break;
case -1:
// timeout
xmodem_fail("Timeout!");
return(0);
break;
case XM_CAN:
xmodem_fail("Remote cancel!");
return(0);
break;
case XM_STX:
//printf("xfer_recv_xmodem: receiving 1k block\n");
blocksize = 1026;
goto getblock;
case XM_SOH:
//printf("xfer_recv_xmodem: receiving 128 byte block\n");
if (usecrc) {
blocksize = 130;
} else {
blocksize = 129;
}
getblock:
// new block
c = xfer_recv_byte_error(1000, 10);
if (c != (blocknum & 255) && c != ((blocknum - 1) & 255)) {
//printf("xfer_recv_xmodem: got block %d, expected %d or %d\n", c, blocknum & 255, (blocknum - 1) & 255);
xmodem_retry("Wrong block, retrying");
return(0);
}
if (255 - xfer_recv_byte_error(1000, 10) != c) {
xmodem_retry("Block mismatch, retrying");
}
if (c == ((blocknum - 1) & 255)) {
--blocknum;
}
//printf("xfer_recv_xmodem: receiving block %d, %d bytes\n", blocknum, blocksize);
//xfer_debug = 0;
for (bufctr = 0; bufctr < blocksize && c >= 0; ++bufctr) {
c = xfer_recv_byte_error(1000, 5);
xfer_buffer[bufctr] = c;
//printf("xfer_recv_xmodem: received byte %d\n", bufctr);
}
//xfer_debug = 1;
if (c < 0) {
//printf("bufctr = %d, c = %d\n", bufctr, c);
// lost sync
xmodem_retry("Lost sync, retrying...");
} else {
if (xmodem_checksum(blocksize & 0xfff0, usecrc)) {
if (blocknum == 0) {
//printf("not saving block 0\n");
} else {
if (xmodem_save_data(xfer_buffer, blocksize & 0xfff0)) {
menu_update_xfer_progress("Downloading...", xfer_saved_bytes, 0);
gfx_vbl();
} else {
xmodem_fail("Write error!");
}
}
xfer_send_byte(XM_ACK);
nexthandshake = XM_ACK;
++blocknum;
//puts("");
} else {
// checksum failed
xmodem_retry("Checksum failed, retrying...");
}
}
break;
case XM_EOT:
// end of transmission
xmodem_save_data(NULL, 0);
xfer_send_byte(XM_ACK);
menu_update_xfer_progress("Transfer complete", xfer_saved_bytes, 0);
gfx_vbl();
return(1);
break;
default:
// purge and cancel
xmodem_fail("Wtf!?");
return(0);
break;
}
}
}
int xmodem_load_block(int blocksize) {
int l;
l = xfer_load_data(xfer_buffer, blocksize);
if (l == 0) {
return(0);
}
if (l < blocksize) {
memset(xfer_buffer + l, XM_PAD, blocksize - l);
}
return(blocksize);
}
void xmodem_send_block(unsigned char blocknum, int blocksize, int usecrc) {
int i;
unsigned char cksum;
unsigned short crc;
//int debug;
//printf("xmodem_send_block: sending block %d, %d bytes\n", blocknum, blocksize);
//puts("xmodem_send_block: sending block header");
xfer_send_byte(blocksize == 128 ? XM_SOH : XM_STX);
xfer_send_byte(blocknum & 0xff);
xfer_send_byte(0xff ^ (blocknum & 0xff));
//puts("xmodem_send_block: sending data");
//debug = xfer_debug;
//xfer_debug = 0;
for (i = 0; i < blocksize; ++i) {
xfer_send_byte(xfer_buffer[i]);
}
//xfer_debug = debug;
if (usecrc) {
crc = crc16_calc(xfer_buffer, blocksize);
//puts("xmodem_send_block: sending crc");
xfer_send_byte(crc>>8);
xfer_send_byte(crc & 0xff);
} else {
cksum = 0;
for (i = 0; i < 128; ++i) {
cksum += xfer_buffer[i];
}
//puts("xmodem_send_block: sending checksum");
xfer_send_byte(cksum);
}
//puts("");
}
int xmodem_send(int send1k) {
int c, usecrc, blocknum, bytesleft, sentbytes, blocksize;
//puts("xmodem_send: waiting for start code");
menu_update_xfer_progress("Starting...", 0, xfer_file_size);
gfx_vbl();
c = 0;
while (c != XM_C && c != XM_NAK) {
c = xfer_recv_byte(1000);
if (xfer_cancel) {
menu_update_xfer_progress("Canceled", 0, xfer_file_size);
gfx_vbl();
return(0);
}
}
if (c == XM_C) {
//puts("xmodem_send: using CRC");
usecrc = 1;
} else {
//puts("xmodem_send: using checksum");
usecrc = 0;
}
/*
menu_update_xfer_progress("Sending header...", 0, xfer_file_size);
gfx_vbl();
memset(xfer_buffer, 'X', 128);
xmodem_send_block(0, 128, usecrc);
*/
bytesleft = xfer_file_size;
sentbytes = 0;
blocknum = 1;
c = XM_NAK;
menu_update_xfer_progress("Uploading...", 0, xfer_file_size);
gfx_vbl();
if (bytesleft > 896 && send1k && usecrc) {
blocksize = 1024;
} else {
blocksize = 128;
}
if (xmodem_load_block(blocksize) == 0) {
menu_update_xfer_progress("Read error", 0, xfer_file_size);
gfx_vbl();
return(0);
}
xmodem_send_block(blocknum, blocksize, usecrc);
while (bytesleft > 0) {
c = xfer_recv_byte(10000);
switch (c) {
case -2:
xmodem_fail("Disconnected!");
return(0);
break;
case XM_CAN:
//puts("xmodem_send: cancel");
menu_update_xfer_progress("Canceled by remote!", sentbytes, xfer_file_size);
gfx_vbl();
return(0);
break;
case XM_ACK:
//puts("xmodem_send: ack");
if (blocknum != 0) {
sentbytes += blocksize;
bytesleft -= blocksize;
}
++blocknum;
menu_update_xfer_progress("Uploading...", sentbytes, xfer_file_size);
gfx_vbl();
if (bytesleft > 896 && send1k && usecrc) {
blocksize = 1024;
} else {
blocksize = 128;
}
if (bytesleft > 0) {
if (xmodem_load_block(blocksize) == 0) {
menu_update_xfer_progress("Read error", 0, xfer_file_size);
gfx_vbl();
return(0);
}
xmodem_send_block(blocknum, blocksize, usecrc);
}
break;
case XM_NAK:
//puts("xmodem_send: nak");
menu_update_xfer_progress("Resending...", sentbytes, xfer_file_size);
gfx_vbl();
xmodem_send_block(blocknum, blocksize, usecrc);
break;
default:
break;
}
}
//puts("xmodem_send: file sent");
menu_update_xfer_progress("Finishing...", sentbytes, xfer_file_size);
gfx_vbl();
xfer_send_byte(XM_EOT);
if (xfer_recv_byte(10000) != XM_ACK) {
xfer_send_byte(XM_EOT);
}
menu_update_xfer_progress("Transfer complete", sentbytes, xfer_file_size);
gfx_vbl();
return(1);
}