From 804e52c4d429bf5fc9211ab74b52c1686b4c9069 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 20 Nov 2026 22:27:22 +0000 Subject: [PATCH] Add YMODEM-G, ZMODEM crash recovery, Skip file and text delays The transfer items of HyperTerminal that the GUI did not have: - YMODEM-G (DMC_XFER_YMODEM_G): streamed blocks with no ACKs; an error ends the transfer. A sender asked with C does plain YMODEM, and the receiver takes the end of lrzsz's sb, which sends no empty block 0. - ZMODEM crash recovery (dmc_xfer_options.resume, and the sender's ZCRECOV): a part of a file in the folder is continued with a ZRPOS from its length, a whole one is skipped, and a part is kept when the transfer stops. The dialogs list "ZMODEM with Crash Recovery". - Skip file (dmc_xfer_skip): ZSKIP for ZMODEM, X in the ACK for Kermit. The progress dialog has a Skip file button. - Plain text line and character delays (dmc_xfer_options), read from dmodem.ini [ascii] until the ASCII Setup dialog comes. Also: a ZSKIP while the sender waits for a ZCRCQ's ACK now ends the file; before, the sender failed with "no answer". Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01B82eRRekyRUhR43KbmGNXK --- README.md | 2 +- core/include/dmodemctl.h | 23 ++++ core/src/xfer.c | 121 +++++++++++++++++-- core/src/xfer.h | 13 +- core/src/xfer_kermit.c | 4 + core/src/xfer_xy.c | 111 +++++++++++++---- core/src/xfer_z.c | 43 +++++-- core/test/xfer_test.c | 250 +++++++++++++++++++++++++++++++++++++++ docs/design.md | 22 +++- windows/res/app.rc | 1 + windows/src/resource.h | 1 + windows/src/xfer.cpp | 129 ++++++++++++++++---- 12 files changed, 652 insertions(+), 68 deletions(-) diff --git a/README.md b/README.md index 5e15f81..b0d87e2 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ libdmodemctl. Thus they operate in the same way. the Minitel and the Tektronix 4014. They have sixel, ReGIS and Tektronix graphics, and the font of each machine (the SyncTERM copies of their ROMs, LGPL-2.0-or-later: `core/licenses/syncterm-fonts-LGPL-2.0.txt`) -- **File transfers** (Windows app): send and receive files on the modem line or on a Telnet, rlogin or SSH login. The protocols are the same as the D-Modem app: ZMODEM (with Automatic Receive when the remote side starts sz, rz or kermit -s), YMODEM, XMODEM, XMODEM-1K, Kermit, Intel HEX, Motorola S-records and plain text. Use the Transfer menu, or the Send and Receive buttons of the Terminal page +- **File transfers** (Windows app): send and receive files on the modem line or on a Telnet, rlogin or SSH login. The protocols are the same as the D-Modem app: ZMODEM (with Automatic Receive when the remote side starts sz, rz or kermit -s, and with Crash Recovery), YMODEM, YMODEM-G, XMODEM, XMODEM-1K, Kermit, Intel HEX, Motorola S-records and plain text. Use the Transfer menu, or the Send and Receive buttons of the Terminal page - **Copy and capture** (Windows app): select text with the mouse (Shift and the mouse when the remote side uses the mouse), then copy it. Paste to Host, Select All, Capture Text (to a file), Capture to Printer, and Print (the screen or the selection). Text that the remote side sends to its printer port goes to the printer. Use the Edit and Transfer menus, or click the right mouse button on the terminal - **Settings**: each modem setting as a native control, from AT!M - **Call Stats**: the rate, modulation, error control and line figures of the last call (AT#UD, ATI11) diff --git a/core/include/dmodemctl.h b/core/include/dmodemctl.h index eb9bb7c..d4e8e9d 100644 --- a/core/include/dmodemctl.h +++ b/core/include/dmodemctl.h @@ -667,6 +667,7 @@ DMC_API void dmc_net_resize(dmc_net *n, int cols, int rows); enum { DMC_XFER_ZMODEM, DMC_XFER_YMODEM, + DMC_XFER_YMODEM_G, /* YMODEM with no ACKs (an error-free line) */ DMC_XFER_XMODEM, DMC_XFER_XMODEM_1K, DMC_XFER_KERMIT, @@ -685,6 +686,17 @@ DMC_API const char *dmc_xfer_command(int protocol, int remote_sends); typedef struct dmc_xfer dmc_xfer; +/* how a transfer goes (all 0: as the protocol does by default) */ +typedef struct dmc_xfer_options { + /* ZMODEM crash recovery: a part of a file that is in the folder is + continued, and a part received is kept when the transfer stops + (receiving); the receiver is asked to continue (sending) */ + int resume; + /* plain text sent: a wait after each line, and after each + character, in ms */ + int line_delay_ms, char_delay_ms; +} dmc_xfer_options; + /* files from the remote into folder (name: XMODEM's file, which it does not name; Intel HEX's, S-records' and plain text's too) */ DMC_API dmc_xfer *dmc_xfer_receive(int protocol, const char *folder, @@ -692,6 +704,17 @@ DMC_API dmc_xfer *dmc_xfer_receive(int protocol, const char *folder, /* files to the remote (XMODEM, the hex records and plain text: the first) */ DMC_API dmc_xfer *dmc_xfer_send(int protocol, const char *const *paths, int n); +/* the same, with options (NULL: the defaults) */ +DMC_API dmc_xfer *dmc_xfer_receive_opt(int protocol, const char *folder, + const char *name, + const dmc_xfer_options *o); +DMC_API dmc_xfer *dmc_xfer_send_opt(int protocol, const char *const *paths, + int n, const dmc_xfer_options *o); +/* the file being received skipped, and the transfer goes on to the next + (ZMODEM and Kermit; the others ignore it) */ +DMC_API void dmc_xfer_skip(dmc_xfer *x); +/* whether the protocol can skip a file (receiving) */ +DMC_API int dmc_xfer_can_skip(int protocol); /* bytes from the remote */ DMC_API void dmc_xfer_feed(dmc_xfer *x, const char *data, int n); /* bytes for the remote: how many */ diff --git a/core/src/xfer.c b/core/src/xfer.c index 5dd4212..a15ceb9 100644 --- a/core/src/xfer.c +++ b/core/src/xfer.c @@ -350,6 +350,46 @@ void xfer_create(dmc_xfer *x, const char *name) snprintf(x->dst_name, sizeof(x->dst_name), "%s", try_name); } +long long xfer_create_resume(dmc_xfer *x, const char *name, long long size) +{ + if (x->dst) + xfer_abandon(x); + char safe[XFER_NAME]; + safe_name(name, safe, sizeof(safe)); + join(x->dst_path, sizeof(x->dst_path), x->folder, safe); + FILE *f = fopen(x->dst_path, "rb"); + if (!f) { + xfer_create(x, name); + x->keep_partial = 1; + return 0; + } + fseek(f, 0, SEEK_END); + long long have = (long long)ftell(f); + fclose(f); + if (size >= 0 && have > size) { + /* (longer: not a part of this file) */ + xfer_create(x, name); + x->keep_partial = 1; + return 0; + } + snprintf(x->dst_name, sizeof(x->dst_name), "%s", safe); + if (size >= 0 && have == size) + return -2; + x->dst = fopen(x->dst_path, "ab"); + if (!x->dst) + xfer_fail(x, "can't write %s", safe); + x->keep_partial = 1; + return have; +} + +int xfer_skip_wanted(dmc_xfer *x) +{ + if (!x->skip) + return 0; + x->skip = 0; + return 1; +} + void xfer_dst_write(dmc_xfer *x, const void *data, int n) { if (n > 0 && fwrite(data, 1, (size_t)n, x->dst) != (size_t)n) @@ -398,6 +438,7 @@ void xfer_done(dmc_xfer *x, long long mtime) return; int bad = fclose(x->dst) != 0; x->dst = NULL; + x->keep_partial = 0; if (bad) { remove(x->dst_path); xfer_fail(x, "can't write %s", x->dst_name); @@ -419,7 +460,10 @@ void xfer_abandon(dmc_xfer *x) return; fclose(x->dst); x->dst = NULL; - remove(x->dst_path); + /* (crash recovery: the part kept, to continue it the next time) */ + if (!x->keep_partial) + remove(x->dst_path); + x->keep_partial = 0; } void xfer_open(dmc_xfer *x, const struct xfer_file *f) @@ -487,6 +531,7 @@ void text_send(dmc_xfer *x) const struct xfer_file *f = &x->files[0]; unsigned char buf[1024]; long long sent = 0; + int line_ms = x->opt.line_delay_ms, char_ms = x->opt.char_delay_ms; xfer_file_start(x, f->name, f->size); xfer_open(x, f); for (;;) { @@ -495,8 +540,30 @@ void text_send(dmc_xfer *x) int n = xfer_src_read(x, buf, (int)sizeof(buf)); if (n <= 0) break; - xfer_write(x, buf, n); - sent += n; + if (line_ms <= 0 && char_ms <= 0) { + xfer_write(x, buf, n); + sent += n; + xfer_bytes(x, sent); + continue; + } + /* paced, as HyperTerminal's ASCII Setup does it: a wait after + each character, and after each line end (CR, LF, or the LF + of CR LF) */ + for (int i = 0; i < n; i++) { + int c = buf[i]; + xfer_put(x, c); + sent++; + /* (the LF of a CR LF ends the line, not the CR) */ + int line_end = c == '\n' || + (c == '\r' && (i + 1 >= n || buf[i + 1] != '\n')); + int ms = char_ms + (line_end ? line_ms : 0); + if (ms > 0) { + xfer_bytes(x, sent); + xfer_sleep(ms); + if (x->cancelled) + xfer_cancelled(x, "cancelled"); + } + } xfer_bytes(x, sent); } xfer_close(x); @@ -538,7 +605,8 @@ void text_receive(dmc_xfer *x) /* the protocols, by number */ static const char *const labels[DMC_XFER_PROTOCOLS] = { - "ZMODEM", "YMODEM", "XMODEM", "XMODEM-1K", "Kermit", "Intel HEX", + "ZMODEM", "YMODEM", "YMODEM-G", "XMODEM", "XMODEM-1K", "Kermit", + "Intel HEX", "Motorola S-records", "Plain text", }; @@ -549,7 +617,8 @@ const char *dmc_xfer_label(int p) int dmc_xfer_batch(int p) { - return p == DMC_XFER_ZMODEM || p == DMC_XFER_YMODEM || p == DMC_XFER_KERMIT; + return p == DMC_XFER_ZMODEM || p == DMC_XFER_YMODEM || + p == DMC_XFER_YMODEM_G || p == DMC_XFER_KERMIT; } const char *dmc_xfer_command(int p, int remote_sends) @@ -557,6 +626,9 @@ const char *dmc_xfer_command(int p, int remote_sends) switch (p) { case DMC_XFER_ZMODEM: return remote_sends ? "sz -b " : "rz -b"; case DMC_XFER_YMODEM: return remote_sends ? "sb " : "rb"; + /* (sb sends YMODEM-G when asked with G; lrzsz's rb asks with C, and + the transfer is then plain YMODEM) */ + case DMC_XFER_YMODEM_G: return remote_sends ? "sb " : "rb"; case DMC_XFER_XMODEM: return remote_sends ? "sx " : "rx -c "; case DMC_XFER_XMODEM_1K: return remote_sends ? "sx -k " : "rx -c "; case DMC_XFER_KERMIT: return remote_sends ? "kermit -i -s " : "kermit -i -r"; @@ -575,7 +647,8 @@ static void run(dmc_xfer *x) if (x->sending) { switch (p) { case DMC_XFER_ZMODEM: z_send(x); break; - case DMC_XFER_YMODEM: xy_send_y(x); break; + case DMC_XFER_YMODEM: xy_send_y(x, 0); break; + case DMC_XFER_YMODEM_G: xy_send_y(x, 1); break; case DMC_XFER_XMODEM: xy_send_x(x, 0); break; case DMC_XFER_XMODEM_1K: xy_send_x(x, 1); break; case DMC_XFER_KERMIT: kermit_send(x); break; @@ -586,7 +659,8 @@ static void run(dmc_xfer *x) } else { switch (p) { case DMC_XFER_ZMODEM: z_receive(x); break; - case DMC_XFER_YMODEM: xy_receive_y(x); break; + case DMC_XFER_YMODEM: xy_receive_y(x, 0); break; + case DMC_XFER_YMODEM_G: xy_receive_y(x, 1); break; case DMC_XFER_XMODEM: case DMC_XFER_XMODEM_1K: xy_receive_x(x); break; case DMC_XFER_KERMIT: kermit_receive(x); break; @@ -686,23 +760,54 @@ static dmc_xfer *start(dmc_xfer *x) } dmc_xfer *dmc_xfer_receive(int protocol, const char *folder, const char *name) +{ + return dmc_xfer_receive_opt(protocol, folder, name, NULL); +} + +dmc_xfer *dmc_xfer_send(int protocol, const char *const *paths, int n) +{ + return dmc_xfer_send_opt(protocol, paths, n, NULL); +} + +void dmc_xfer_skip(dmc_xfer *x) +{ + if (!x || !dmc_xfer_can_skip(x->protocol) || x->sending) + return; + lock(x); + x->skip = 1; + wake(x, 1); + unlock(x); +} + +int dmc_xfer_can_skip(int protocol) +{ + return protocol == DMC_XFER_ZMODEM || protocol == DMC_XFER_KERMIT; +} + +dmc_xfer *dmc_xfer_receive_opt(int protocol, const char *folder, + const char *name, const dmc_xfer_options *o) { dmc_xfer *x = make(protocol, 0); if (!x) return NULL; + if (o) + x->opt = *o; snprintf(x->folder, sizeof(x->folder), "%s", folder ? folder : ""); snprintf(x->xname, sizeof(x->xname), "%s", name && *name ? name : "received.bin"); return start(x); } -dmc_xfer *dmc_xfer_send(int protocol, const char *const *paths, int n) +dmc_xfer *dmc_xfer_send_opt(int protocol, const char *const *paths, int n, + const dmc_xfer_options *o) { if (n <= 0 || !paths) return NULL; dmc_xfer *x = make(protocol, 1); if (!x) return NULL; + if (o) + x->opt = *o; if (!dmc_xfer_batch(protocol)) n = 1; x->files = (struct xfer_file *)calloc((size_t)n, sizeof(*x->files)); diff --git a/core/src/xfer.h b/core/src/xfer.h index c788a57..d377eb3 100644 --- a/core/src/xfer.h +++ b/core/src/xfer.h @@ -74,6 +74,9 @@ struct dmc_xfer { FILE *dst; /* the file being received */ char dst_path[XFER_PATH], dst_name[XFER_NAME + 16]; int keep_on_cancel; /* plain text: what came is kept */ + int keep_partial; /* crash recovery: a part is kept */ + dmc_xfer_options opt; + volatile int skip; /* the user skipped the file now */ void *heap; /* freed at the end */ void *proto; /* the protocol's state (Kermit's) */ void (*proto_free)(void *); /* how it's freed (NULL: free) */ @@ -122,6 +125,12 @@ long long xfer_size_now(dmc_xfer *x); /* a received file made in the folder (its name made safe and unique): x->dst open, x->dst_name the name it got */ void xfer_create(dmc_xfer *x, const char *name); +/* the same for crash recovery: a part of the file in the folder + continued (its length, appended to), or a new one (0); -2 when the + file is there whole (size known), and nothing opened */ +long long xfer_create_resume(dmc_xfer *x, const char *name, long long size); +/* the user's skip of the file now, taken (once) */ +int xfer_skip_wanted(dmc_xfer *x); /* the received file written */ void xfer_dst_write(dmc_xfer *x, const void *data, int n); /* the received file whole (its time set, if mtime > 0), or dropped */ @@ -141,9 +150,9 @@ unsigned long xfer_crc32_byte(unsigned long crc, int b); /* the protocols (xfer_xy.c, xfer_z.c, xfer_kermit.c, xfer_hex.c) */ void xy_receive_x(dmc_xfer *x); -void xy_receive_y(dmc_xfer *x); +void xy_receive_y(dmc_xfer *x, int g); void xy_send_x(dmc_xfer *x, int one_k); -void xy_send_y(dmc_xfer *x); +void xy_send_y(dmc_xfer *x, int g); void z_receive(dmc_xfer *x); void z_send(dmc_xfer *x); void kermit_receive(dmc_xfer *x); diff --git a/core/src/xfer_kermit.c b/core/src/xfer_kermit.c index 6423892..ec93d26 100644 --- a/core/src/xfer_kermit.c +++ b/core/src/xfer_kermit.c @@ -781,6 +781,10 @@ void kermit_receive(dmc_xfer *x) kfail(k, "data before a file's name"); got_bytes += decode(k, k->pkt, k->plen, NULL, 0); xfer_bytes(x, got_bytes); + /* the user's skip: X in the ACK (the sender ends the + file with Z D, and the file is dropped) */ + if (xfer_skip_wanted(x)) + reply[rn++] = 'X'; break; case 'Z': if (x->dst) { diff --git a/core/src/xfer_xy.c b/core/src/xfer_xy.c index 2a4c57e..f4f651a 100644 --- a/core/src/xfer_xy.c +++ b/core/src/xfer_xy.c @@ -7,6 +7,13 @@ * sized in block 0), both ways. A bad block is asked for * again (NAK) up to MAX_ERRORS times; CAN CAN from the other * side ends it. + * + * YMODEM-G: the receiver asks with G, and the sender sends + * the blocks with no wait for an ACK (for a line that + * corrects its errors, as a modem's V.42). An error ends + * the transfer, as no block can be sent again. The EOT of + * each file is still acknowledged. A sender asked with C + * does plain YMODEM. */ #include #include @@ -21,6 +28,7 @@ #define CAN 0x18 #define SUB 0x1a #define C 'C' +#define G 'G' #define MAX_ERRORS 10 #define TIMEOUT 10000 @@ -36,8 +44,16 @@ struct rx { int crc; /* CRC, else the checksum */ int eot; /* the sender's EOT came */ int bad; /* a damaged block came */ + int g; /* YMODEM-G: no ACKs, no repeats */ }; +/* YMODEM-G's end on an error: no block can come again */ +static void g_fail(dmc_xfer *x) +{ + xfer_write(x, "\x18\x18\x18\x18\x18", 5); + xfer_fail(x, "an error on the line (YMODEM-G cannot send a block again)"); +} + static int sum8(const unsigned char *p, int n) { int s = 0; @@ -102,16 +118,17 @@ static void read_block(dmc_xfer *x, struct rx *r, struct block *b, int use_crc, /* the first block, asked for with C (CRC) and, for XMODEM, NAK (a checksum) when C isn't answered */ -static void start_and_read(dmc_xfer *x, struct rx *r, struct block *b, int ymodem) +static void start_and_read(dmc_xfer *x, struct rx *r, struct block *b, int ymodem, + int tries) { int crc = 1; r->bad = 0; - for (int attempt = 0; attempt < 10; attempt++) { + for (int attempt = 0; attempt < tries; attempt++) { if (attempt == 4 && !ymodem) crc = 0; /* (a damaged block 0 asked for again with NAK: a C could be taken for its ACK and a request for the data) */ - xfer_put(x, crc && !(ymodem && r->bad) ? C : NAK); + xfer_put(x, r->g ? G : crc && !(ymodem && r->bad) ? C : NAK); read_block(x, r, b, crc, 3000); if (b->number >= 0) { r->crc = crc; @@ -132,7 +149,7 @@ static void receive_file(dmc_xfer *x, struct rx *r, long long size, int ymodem, if (held) *held_n = -1; if (!ymodem) { - start_and_read(x, r, b, 0); + start_and_read(x, r, b, 0, 10); have = b->number >= 0; } for (;;) { @@ -141,15 +158,18 @@ static void receive_file(dmc_xfer *x, struct rx *r, long long size, int ymodem, if (b->number < 0) { if (r->eot) { /* YMODEM: the first EOT answered NAK, the - second ACK (to be sure it was one) */ + second ACK (to be sure it was one); + YMODEM-G: ACK */ eots++; - if (ymodem && eots == 1) { + if (ymodem && eots == 1 && !r->g) { xfer_put(x, NAK); continue; } xfer_put(x, ACK); break; } + if (r->g) + g_fail(x); if (++errors > MAX_ERRORS) xfer_fail(x, "too many errors on the line"); xfer_error(x); @@ -173,7 +193,8 @@ static void receive_file(dmc_xfer *x, struct rx *r, long long size, int ymodem, written += len; expect++; errors = 0; - xfer_put(x, ACK); + if (!r->g) + xfer_put(x, ACK); xfer_bytes(x, written); } else if (b->number == ((expect - 1) & 0xff)) { /* a repeat of one already had (our ACK lost) */ @@ -187,7 +208,7 @@ static void receive_file(dmc_xfer *x, struct rx *r, long long size, int ymodem, void xy_receive_x(dmc_xfer *x) { - struct rx r = { 1, 0, 0 }; + struct rx r = { 1, 0, 0, 0 }; unsigned char held[1024]; int held_n = -1; xfer_create(x, x->xname); @@ -203,13 +224,17 @@ void xy_receive_x(dmc_xfer *x) xfer_file_done(x); } -void xy_receive_y(dmc_xfer *x) +void xy_receive_y(dmc_xfer *x, int g) { - struct rx r = { 1, 0, 0 }; + struct rx r = { 1, 0, 0, g }; struct block bb0, *b0 = &bb0; + int got = 0; for (;;) { /* block 0: the file's name and size, or an empty one (the end) */ - start_and_read(x, &r, b0, 1); + start_and_read(x, &r, b0, 1, g && got ? 2 : 10); + /* (lrzsz's sb in YMODEM-G ends with no empty block 0) */ + if (b0->number < 0 && g && got) + return; if (b0->number < 0) xfer_fail(x, "the sender didn't start"); if (b0->number != 0) @@ -234,25 +259,33 @@ void xy_receive_y(dmc_xfer *x) size = v; if (*end == ' ') mtime = strtoll(end + 1, NULL, 8); - xfer_put(x, ACK); + /* (YMODEM-G: block 0 not acknowledged, G asks for the data) */ + if (!g) + xfer_put(x, ACK); xfer_create(x, name); xfer_file_start(x, x->dst_name, size); - xfer_put(x, C); + xfer_put(x, g ? G : C); receive_file(x, &r, size, 1, NULL, NULL); xfer_done(x, mtime); xfer_file_done(x); + got++; } } /* ------------------------------------------------------------------ */ /* sending */ -/* C (CRC: 1) or NAK (a checksum: 0) from the receiver; -1 for neither */ -static int wait_for_start(dmc_xfer *x) +/* C (CRC: 1) or NAK (a checksum: 0) from the receiver, G (2) when g; + -1 for none */ +static int wait_for_start(dmc_xfer *x, int g) { long long end = xfer_now() + 60000; while (xfer_now() < end) { switch (xfer_read(x, TIMEOUT)) { + case G: + if (g) + return 2; + break; case C: return 1; case NAK: return 0; case CAN: @@ -327,9 +360,20 @@ static void send_block(dmc_xfer *x, int n, const unsigned char *data, int size, } } +/* YMODEM-G: the receiver's CAN CAN, if it sent them (no wait) */ +static void g_check(dmc_xfer *x) +{ + while (xfer_available(x)) { + int c = xfer_read(x, 0); + if (c == CAN && xfer_read(x, 1000) == CAN) + xfer_cancelled(x, "cancelled by the receiver"); + } +} + /* the open file's data in blocks of block_size (a short last one in 128, - padded with SUB), then EOT until acknowledged */ -static void send_data(dmc_xfer *x, int block_size, int use_crc) + padded with SUB), then EOT until acknowledged; g: the blocks with no + wait for an ACK (YMODEM-G) */ +static void send_data(dmc_xfer *x, int block_size, int use_crc, int g) { unsigned char buf[1024]; int n = 1; @@ -346,7 +390,13 @@ static void send_data(dmc_xfer *x, int block_size, int use_crc) break; int size = len <= 128 ? 128 : block_size; memset(buf + len, SUB, (size_t)(size - len)); - send_block(x, n, buf, size, use_crc); + if (g) { + unsigned char bytes[1024 + 5]; + xfer_write(x, bytes, block_bytes(bytes, n, buf, size, 1)); + g_check(x); + } else { + send_block(x, n, buf, size, use_crc); + } sent += len; n++; xfer_bytes(x, sent); @@ -366,23 +416,26 @@ static void send_data(dmc_xfer *x, int block_size, int use_crc) void xy_send_x(dmc_xfer *x, int one_k) { const struct xfer_file *f = &x->files[0]; - int use_crc = wait_for_start(x); + int use_crc = wait_for_start(x, 0); if (use_crc < 0) xfer_fail(x, "the receiver didn't start"); xfer_file_start(x, f->name, f->size); xfer_open(x, f); - send_data(x, one_k ? 1024 : 128, use_crc); + send_data(x, one_k ? 1024 : 128, use_crc, 0); xfer_close(x); xfer_file_done(x); } -void xy_send_y(dmc_xfer *x) +void xy_send_y(dmc_xfer *x, int g) { unsigned char block0[1024]; for (int i = 0; i < x->nfiles; i++) { const struct xfer_file *f = &x->files[i]; - if (wait_for_start(x) != 1) + /* (asked with C: plain YMODEM, also for YMODEM-G) */ + int start = wait_for_start(x, g); + if (start < 1) xfer_fail(x, "the receiver didn't start"); + int streaming = start == 2; memset(block0, 0, sizeof(block0)); int nl = (int)strlen(f->name); if (nl > 900) @@ -393,17 +446,23 @@ void xy_send_y(dmc_xfer *x) f->size >= 0 ? f->size : 0LL, (unsigned long long)(f->mtime > 0 ? f->mtime : 0)); memcpy(block0 + nl + 1, meta, (size_t)ml); - send_block(x, 0, block0, nl + 1 + ml > 128 ? 1024 : 128, 1); + int size0 = nl + 1 + ml > 128 ? 1024 : 128; + if (streaming) { + unsigned char bytes[1024 + 5]; + xfer_write(x, bytes, block_bytes(bytes, 0, block0, size0, 1)); + } else { + send_block(x, 0, block0, size0, 1); + } xfer_file_start(x, f->name, f->size); - if (wait_for_start(x) != 1) + if (wait_for_start(x, g) != (streaming ? 2 : 1)) xfer_fail(x, "the receiver didn't ask for the data"); xfer_open(x, f); - send_data(x, 1024, 1); + send_data(x, 1024, 1, streaming); xfer_close(x); xfer_file_done(x); } /* the batch over: an empty block 0 */ - if (wait_for_start(x) != 1) + if (wait_for_start(x, g) < 1) xfer_fail(x, "the receiver didn't ask for the end"); /* (rb exits as it takes it: its ACK can be lost with it) */ unsigned char empty[128], bytes[133]; diff --git a/core/src/xfer_z.c b/core/src/xfer_z.c index fe8dde2..9dca210 100644 --- a/core/src/xfer_z.c +++ b/core/src/xfer_z.c @@ -38,6 +38,7 @@ enum { #define CANOVIO 0x02 #define CANFC32 0x20 #define ZCBIN 1 +#define ZCRECOV 3 /* ZF0: continue a part of the file */ #define BLOCK 1024 /* bytes between the sender's ZCRCQs (its window) */ @@ -459,9 +460,9 @@ static void zrinit(struct z *z) send_hex(z, hdr_flags(ZRINIT, CANFDX | CANOVIO | CANFC32)); } -/* a file's data after its ZFILE (its info, len bytes): 1 received, 0 - skipped */ -static int receive_file(struct z *z, const unsigned char *info, int len) +/* a file's data after its ZFILE (its info, len bytes; f0 its ZF0): 1 + received, 0 skipped */ +static int receive_file(struct z *z, const unsigned char *info, int len, int f0) { dmc_xfer *x = z->x; char name[XFER_NAME], meta[256]; @@ -482,9 +483,21 @@ static int receive_file(struct z *z, const unsigned char *info, int len) size = v; if (*e == ' ') mtime = strtoll(e + 1, NULL, 8); - xfer_create(x, name); - xfer_file_start(x, x->dst_name, size); long long pos = 0, rpos_at = 0; + /* crash recovery (ours, or the sender's ZCRECOV): from the end of a + part that is in the folder */ + if (x->opt.resume || f0 == ZCRECOV) { + pos = xfer_create_resume(x, name, size); + if (pos == -2) { + /* (there whole already) */ + send_hex(z, hdr_pos(ZSKIP, 0)); + return 0; + } + } else { + xfer_create(x, name); + } + xfer_file_start(x, x->dst_name, size); + xfer_bytes(x, pos); int errors = 0, ok = 0; struct header h; /* (until the sender has had time to hear our ZRPOS, what still comes @@ -494,6 +507,13 @@ static int receive_file(struct z *z, const unsigned char *info, int len) #define WAITING() (xfer_now() - rpos_at < RPOS_GRACE_MS) RPOS(); for (;;) { + /* the user's skip: ZSKIP, and the file dropped (the sender + goes on to its next) */ + if (xfer_skip_wanted(x)) { + send_hex(z, hdr_pos(ZSKIP, 0)); + xfer_purge(x); + break; + } /* (8 KB of a stream with no header in it: ours lost; asked for again) */ int st = read_header(z, 8 * 1024, 1, &h); @@ -536,6 +556,8 @@ static int receive_file(struct z *z, const unsigned char *info, int len) send_hex(z, hdr_pos(ZACK, pos)); else if (end == ZCRCE) break; + if (x->skip) + break; } } else if (h.type == ZEOF) { if (hpos(&h) != pos) { @@ -611,7 +633,7 @@ void z_receive(dmc_xfer *x) break; } memcpy(info, z->data, (size_t)n); - if (receive_file(z, info, n)) { + if (receive_file(z, info, n, h.p[3])) { xfer_file_done(x); zrinit(z); } @@ -677,7 +699,8 @@ static struct header wait_for_zrinit(struct z *z) } } -/* a ZCRCQ's answer: -1 for its ACK, else the position a ZRPOS asks for */ +/* a ZCRCQ's answer: -1 for its ACK, -2 for a ZSKIP (the receiver + skipped the file), else the position a ZRPOS asks for */ static long long wait_for_ack(struct z *z, long long pos) { struct header h; @@ -688,6 +711,8 @@ static long long wait_for_ack(struct z *z, long long pos) return -1; if (h.type == ZRPOS) return hpos(&h); + if (h.type == ZSKIP) + return -2; if (h.type == ZCAN || h.type == ZABORT) xfer_cancelled(z->x, "cancelled by the receiver"); } @@ -766,6 +791,8 @@ static void stream(struct z *z, long long start) if (end == ZCRCQ) { since_ack = 0; long long to = wait_for_ack(z, pos); + if (to == -2) + return; if (to >= 0) { BACK(to); restart = 1; @@ -818,7 +845,7 @@ static int send_file(struct z *z, const struct xfer_file *f, int files_left, int tries = 0; struct header h; while (start < 0) { - send_bin(z, hdr_flags(ZFILE, ZCBIN)); + send_bin(z, hdr_flags(ZFILE, z->x->opt.resume ? ZCRECOV : ZCBIN)); send_data(z, info, n, ZCRCW); for (;;) { int got = header_or_null(z, &h); diff --git a/core/test/xfer_test.c b/core/test/xfer_test.c index 1660478..cdfe4d8 100644 --- a/core/test/xfer_test.c +++ b/core/test/xfer_test.c @@ -177,6 +177,245 @@ static void both_ways(int p, int noise) dmc_xfer_free(r); } +/* YMODEM-G: an error ends it on both sides (no block again); a sender + asked with C does plain YMODEM */ +static void ymodem_g(void) +{ + char src[600], dst[600], a[700], got[700]; + snprintf(src, sizeof(src), "%s/src", tmp); + snprintf(dst, sizeof(dst), "%s/dst", tmp); + empty_dir(src); + empty_dir(dst); + snprintf(a, sizeof(a), "%s/data.bin", src); + write_file(a, big, sizeof(big)); + const char *paths[1] = { a }; + dmc_xfer *r = dmc_xfer_receive(DMC_XFER_YMODEM_G, dst, NULL); + dmc_xfer *s = dmc_xfer_send(DMC_XFER_YMODEM_G, paths, 1); + link2(s, r, 3001); + dmc_xfer_status ss, rs; + dmc_xfer_get_status(s, &ss); + dmc_xfer_get_status(r, &rs); + CHECK(strstr(rs.result, "YMODEM-G") != NULL, "YMODEM-G noisy: the receiver: '%s'", + rs.result); + CHECK(ss.result[0], "YMODEM-G noisy: the sender went on"); + snprintf(got, sizeof(got), "%s/data.bin", dst); + FILE *f = fopen(got, "rb"); + CHECK(!f, "YMODEM-G noisy: the part of the file was kept"); + if (f) + fclose(f); + dmc_xfer_free(s); + dmc_xfer_free(r); + + empty_dir(dst); + r = dmc_xfer_receive(DMC_XFER_YMODEM, dst, NULL); + s = dmc_xfer_send(DMC_XFER_YMODEM_G, paths, 1); + link2(s, r, 0); + dmc_xfer_get_status(s, &ss); + dmc_xfer_get_status(r, &rs); + CHECK(!ss.result[0] && !rs.result[0], "YMODEM-G to YMODEM: '%s' '%s'", + ss.result, rs.result); + check_same(got, big, sizeof(big), 0, "YMODEM-G to YMODEM"); + dmc_xfer_free(s); + dmc_xfer_free(r); +} + +/* link2 with the receiver's skip once it has some bytes of the first file */ +static void link2_skip(dmc_xfer *a, dmc_xfer *b) +{ + char buf[4096]; + int skipped = 0; + long long end = xfer_now() + 120000; + for (;;) { + dmc_xfer_status sa, sb; + dmc_xfer_get_status(a, &sa); + dmc_xfer_get_status(b, &sb); + if (!sa.running && !sb.running) + break; + if (xfer_now() > end) { + CHECK(0, "the skip: it took too long"); + dmc_xfer_cancel(a); + dmc_xfer_cancel(b); + } + if (!skipped && sb.bytes > 8000 && !strcmp(sb.file, "data.bin")) { + dmc_xfer_skip(b); + skipped = 1; + } + int moved = 0, n; + /* (a slow line: 4 KB a turn) */ + if ((n = dmc_xfer_output(a, buf, sizeof(buf))) > 0) { + dmc_xfer_feed(b, buf, n); + moved = 1; + } + while ((n = dmc_xfer_output(b, buf, sizeof(buf))) > 0) { + dmc_xfer_feed(a, buf, n); + moved = 1; + } + xfer_sleep(moved ? 1 : 2); + } +} + +/* Skip File: the first file dropped, the second received */ +static void skip_file(int p) +{ + char src[600], dst[600], a[700], b[700], got[700]; + const char *what = dmc_xfer_label(p); + snprintf(src, sizeof(src), "%s/src", tmp); + snprintf(dst, sizeof(dst), "%s/dst", tmp); + empty_dir(src); + empty_dir(dst); + snprintf(a, sizeof(a), "%s/data.bin", src); + snprintf(b, sizeof(b), "%s/hello.txt", src); + write_file(a, big, sizeof(big)); + write_file(b, small, sizeof(small) - 1); + const char *paths[2] = { a, b }; + dmc_xfer *r = dmc_xfer_receive(p, dst, NULL); + dmc_xfer *s = dmc_xfer_send(p, paths, 2); + link2_skip(s, r); + dmc_xfer_status ss, rs; + dmc_xfer_get_status(s, &ss); + dmc_xfer_get_status(r, &rs); + CHECK(!ss.result[0] && !rs.result[0], "%s skip: '%s' '%s'", what, ss.result, + rs.result); + snprintf(got, sizeof(got), "%s/data.bin", dst); + FILE *f = fopen(got, "rb"); + CHECK(!f, "%s skip: the skipped file is there", what); + if (f) + fclose(f); + snprintf(got, sizeof(got), "%s/hello.txt", dst); + check_same(got, small, sizeof(small) - 1, 0, what); + CHECK(rs.files == 1, "%s skip: %d files", what, rs.files); + dmc_xfer_free(s); + dmc_xfer_free(r); +} + +/* ZMODEM crash recovery: a part kept when the transfer stops, and + continued the next time */ +static void resume(void) +{ + char src[600], dst[600], a[700], got[700]; + snprintf(src, sizeof(src), "%s/src", tmp); + snprintf(dst, sizeof(dst), "%s/dst", tmp); + empty_dir(src); + empty_dir(dst); + snprintf(a, sizeof(a), "%s/data.bin", src); + write_file(a, big, sizeof(big)); + snprintf(got, sizeof(got), "%s/data.bin", dst); + const char *paths[1] = { a }; + dmc_xfer_options o; + memset(&o, 0, sizeof(o)); + o.resume = 1; + + /* a part there: the receiver's option continues it */ + write_file(got, big, 15000); + dmc_xfer *r = dmc_xfer_receive_opt(DMC_XFER_ZMODEM, dst, NULL, &o); + dmc_xfer *s = dmc_xfer_send(DMC_XFER_ZMODEM, paths, 1); + long long t0 = xfer_now(); + link2(s, r, 0); + (void)t0; + dmc_xfer_status ss, rs; + dmc_xfer_get_status(s, &ss); + dmc_xfer_get_status(r, &rs); + CHECK(!ss.result[0] && !rs.result[0], "resume: '%s' '%s'", ss.result, rs.result); + check_same(got, big, sizeof(big), 0, "resume (the receiver's option)"); + dmc_xfer_free(s); + dmc_xfer_free(r); + + /* the same with the sender's ZCRECOV */ + write_file(got, big, 20000); + r = dmc_xfer_receive(DMC_XFER_ZMODEM, dst, NULL); + s = dmc_xfer_send_opt(DMC_XFER_ZMODEM, paths, 1, &o); + link2(s, r, 0); + dmc_xfer_get_status(r, &rs); + CHECK(!rs.result[0], "resume (ZCRECOV): '%s'", rs.result); + check_same(got, big, sizeof(big), 0, "resume (the sender's ZCRECOV)"); + dmc_xfer_free(s); + dmc_xfer_free(r); + + /* there whole: skipped, and not made again as "data (2).bin" */ + r = dmc_xfer_receive_opt(DMC_XFER_ZMODEM, dst, NULL, &o); + s = dmc_xfer_send(DMC_XFER_ZMODEM, paths, 1); + link2(s, r, 0); + dmc_xfer_get_status(r, &rs); + CHECK(!rs.result[0], "resume (whole): '%s'", rs.result); + char two[700]; + snprintf(two, sizeof(two), "%s/data (2).bin", dst); + FILE *f = fopen(two, "rb"); + CHECK(!f, "resume (whole): a second copy was made"); + if (f) + fclose(f); + dmc_xfer_free(s); + dmc_xfer_free(r); + + /* stopped in the middle: the part kept */ + remove(got); + r = dmc_xfer_receive_opt(DMC_XFER_ZMODEM, dst, NULL, &o); + s = dmc_xfer_send(DMC_XFER_ZMODEM, paths, 1); + char buf[4096]; + for (int turns = 0; turns < 20000; turns++) { + dmc_xfer_status st; + dmc_xfer_get_status(r, &st); + if (st.bytes > 10000) + break; + int n; + if ((n = dmc_xfer_output(s, buf, 1024)) > 0) + dmc_xfer_feed(r, buf, n); + while ((n = dmc_xfer_output(r, buf, sizeof(buf))) > 0) + dmc_xfer_feed(s, buf, n); + xfer_sleep(1); + } + dmc_xfer_cancel(r); + dmc_xfer_cancel(s); + link2(s, r, 0); + size_t n = 0; + unsigned char *part = read_file(got, &n); + CHECK(part && n > 0 && n < sizeof(big) && !memcmp(part, big, n), + "resume: the part was not kept (%zu bytes)", n); + free(part); + dmc_xfer_free(s); + dmc_xfer_free(r); +} + +/* plain text with the waits of ASCII Setup */ +static void text_delays(void) +{ + char src[600], dst[600], a[700], got[700]; + snprintf(src, sizeof(src), "%s/src", tmp); + snprintf(dst, sizeof(dst), "%s/dst", tmp); + empty_dir(src); + empty_dir(dst); + static const unsigned char text[] = "one\r\ntwo\r\nthree\rfour\n"; + snprintf(a, sizeof(a), "%s/t.txt", src); + write_file(a, text, sizeof(text) - 1); + const char *paths[1] = { a }; + dmc_xfer_options o; + memset(&o, 0, sizeof(o)); + o.line_delay_ms = 100; + o.char_delay_ms = 5; + xfer_text_quiet_ms = 600; + dmc_xfer *r = dmc_xfer_receive(DMC_XFER_TEXT, dst, "t.txt"); + long long t0 = xfer_now(); + dmc_xfer *s = dmc_xfer_send_opt(DMC_XFER_TEXT, paths, 1, &o); + for (;;) { + dmc_xfer_status st; + dmc_xfer_get_status(s, &st); + char buf[256]; + int n; + while ((n = dmc_xfer_output(s, buf, sizeof(buf))) > 0) + dmc_xfer_feed(r, buf, n); + if (!st.running) + break; + xfer_sleep(1); + } + long long took = xfer_now() - t0; + /* 4 line ends and 21 characters */ + CHECK(took >= 4 * 100 + 21 * 5, "text delays: %lld ms", took); + link2(s, r, 0); + snprintf(got, sizeof(got), "%s/t.txt", dst); + check_same(got, text, sizeof(text) - 1, 0, "text delays"); + dmc_xfer_free(s); + dmc_xfer_free(r); +} + static void hex_both_ways(int p) { char src[600], dst[600], a[700], got[700]; @@ -430,6 +669,8 @@ static void interop(void) if (have("sz") && have("rz")) { interop_one(DMC_XFER_ZMODEM, "sz -b", "rz -b", 0); interop_one(DMC_XFER_YMODEM, "sz --ymodem -b", "rz --ymodem -b", 0); + /* (sb streams when asked with G; rb asks with C: plain YMODEM) */ + interop_one(DMC_XFER_YMODEM_G, "sz --ymodem -b", "rz --ymodem -b", 0); interop_one(DMC_XFER_XMODEM, "sz --xmodem -b", "rz --xmodem -b data.bin", 1); interop_one(DMC_XFER_XMODEM_1K, "sz --xmodem -k -b", "rz --xmodem -b data.bin", 1); printf("interop: lrzsz\n"); @@ -457,6 +698,9 @@ int main(void) big[i] = (unsigned char)rand(); int protocols[] = { DMC_XFER_ZMODEM, DMC_XFER_YMODEM, DMC_XFER_XMODEM, DMC_XFER_XMODEM_1K, DMC_XFER_KERMIT }; + both_ways(DMC_XFER_YMODEM_G, 0); + ymodem_g(); + printf("YMODEM-G\n"); for (size_t i = 0; i < sizeof(protocols) / sizeof(protocols[0]); i++) { both_ways(protocols[i], 0); printf("%s: clean line\n", dmc_xfer_label(protocols[i])); @@ -468,7 +712,13 @@ int main(void) hex_both_ways(DMC_XFER_SREC); printf("hex records\n"); text_both_ways(); + text_delays(); printf("plain text\n"); + skip_file(DMC_XFER_ZMODEM); + skip_file(DMC_XFER_KERMIT); + printf("skip file\n"); + resume(); + printf("ZMODEM crash recovery\n"); cancel(); detect(); interop(); diff --git a/docs/design.md b/docs/design.md index a38f825..9fd4f8b 100644 --- a/docs/design.md +++ b/docs/design.md @@ -163,16 +163,36 @@ protocols are: - ZMODEM: CRC-16 and CRC-32 headers, ZDLE escapes, streaming data (ZCRCG) with a ZCRCQ each 16 KB, and recovery from errors with ZRPOS. +- ZMODEM crash recovery (`dmc_xfer_options.resume`, "ZMODEM with Crash + Recovery" in the dialogs). The receiver continues a part of a file that + is in the folder: it opens the file to add to it, and it sends ZRPOS with + the length of the part. The receiver also does this when the sender sets + ZF0 to ZCRECOV (`sz -r`). If the file is there whole, the receiver sends + ZSKIP. When a transfer stops, the receiver keeps the part that it + received. The sender with this option sets ZF0 to ZCRECOV. - YMODEM, XMODEM and XMODEM-1K. +- YMODEM-G: the receiver asks with G. The sender sends the blocks and does + not wait for an ACK. Use it only on a line that corrects its errors (V.42). + An error stops the transfer, because a block cannot come again. The + sender does plain YMODEM when the receiver asks with C (lrzsz's `rb`). + lrzsz's `sb` sends no empty block 0 at the end in YMODEM-G, thus the + receiver ends the batch when no block 0 comes after a file. - Kermit: long packets, block checks 1 to 3, attribute packets, repeat counts and 8-bit prefixes. - Intel HEX and Motorola S-records: the app sends a file of records as it is, and it encodes other files as records. When it receives, it decodes the records to a binary image. -- Plain text: the app sends the file with a pause between lines. When it +- Plain text: the app sends the bytes of the file as they are. With + `line_delay_ms` and `char_delay_ms`, the app waits after each line end + and after each character (as the ASCII Setup of HyperTerminal). When it receives, it keeps the text until the line is quiet. A cancel keeps the text that came. +`dmc_xfer_skip` skips the file that the app receives now. ZMODEM sends +ZSKIP. Kermit puts X in the ACK of a data packet, and the sender ends the +file with Z and D. The progress dialog has a Skip file button for these +two protocols. + Each transfer runs on its thread (CreateThread on Windows, pthreads on other systems). The thread and the app use two queues with a lock: diff --git a/windows/res/app.rc b/windows/res/app.rc index 862fb4c..9da08aa 100644 --- a/windows/res/app.rc +++ b/windows/res/app.rc @@ -216,6 +216,7 @@ BEGIN LTEXT "", IDC_X_BAR, 8, 74, 244, 12 LTEXT "", IDC_X_BYTES, 8, 90, 244, 8 LTEXT "", IDC_X_RESULT, 8, 104, 244, 16, SS_NOPREFIX + PUSHBUTTON "&Skip file", IDC_X_SKIP, 90, 126, 50, 14 PUSHBUTTON "c&ps/bps", IDC_X_UNITS, 146, 126, 50, 14 PUSHBUTTON "Cancel", IDCANCEL, 202, 126, 50, 14 END diff --git a/windows/src/resource.h b/windows/src/resource.h index 0076c26..4cf2d10 100644 --- a/windows/src/resource.h +++ b/windows/src/resource.h @@ -189,5 +189,6 @@ #define IDC_X_UNITS 1231 #define IDC_X_BAR 1232 #define IDC_X_RESULT 1233 +#define IDC_X_SKIP 1234 #endif diff --git a/windows/src/xfer.cpp b/windows/src/xfer.cpp index c2c9109..51ea801 100644 --- a/windows/src/xfer.cpp +++ b/windows/src/xfer.cpp @@ -49,6 +49,42 @@ static bool bps; /* bits a second, else characters */ static char where[MAX_PATH]; static bool send_wanted; /* the remote's rz noticed, not yet answered */ +/* the protocols as the dialogs list them (HyperTerminal's "Zmodem with + Crash Recovery" a ZMODEM with the resume option) */ +static const struct Choice { + int protocol; + int resume; +} choices[] = { + { DMC_XFER_ZMODEM, 0 }, + { DMC_XFER_ZMODEM, 1 }, + { DMC_XFER_YMODEM, 0 }, + { DMC_XFER_YMODEM_G, 0 }, + { DMC_XFER_XMODEM, 0 }, + { DMC_XFER_XMODEM_1K, 0 }, + { DMC_XFER_KERMIT, 0 }, + { DMC_XFER_INTEL_HEX, 0 }, + { DMC_XFER_SREC, 0 }, + { DMC_XFER_TEXT, 0 }, +}; +#define CHOICES ((int)(sizeof(choices) / sizeof(choices[0]))) + +static int choice_of(int protocol, int resume) +{ + for (int i = 0; i < CHOICES; i++) + if (choices[i].protocol == protocol && choices[i].resume == (resume != 0)) + return i; + return 0; +} + +static void choice_label(int i, char *out, int n) +{ + int p = choices[i].protocol; + _snprintf(out, n, "%s%s%s", dmc_xfer_label(p), + choices[i].resume ? " with Crash Recovery" : "", + dmc_xfer_batch(p) ? "" : " (one file)"); + out[n - 1] = '\0'; +} + static void ini(char *out, int n) { char dir[MAX_PATH]; @@ -64,6 +100,14 @@ static int ini_int(const char *key, int def) return (int)GetPrivateProfileInt("transfer", key, def, path); } +/* ASCII Setup's ([ascii]: the text's line and character delays) */ +static int ascii_int(const char *key, int def) +{ + char path[MAX_PATH]; + ini(path, sizeof(path)); + return (int)GetPrivateProfileInt("ascii", key, def, path); +} + static void ini_set(const char *key, const char *v) { char path[MAX_PATH]; @@ -208,6 +252,12 @@ static INT_PTR CALLBACK xfer_proc(HWND d, UINT m, WPARAM wp, LPARAM lp) return TRUE; case WM_COMMAND: switch (LOWORD(wp)) { + case IDC_X_SKIP: + if (cur) { + dmc_xfer_skip(cur); + set_text(IDC_X_RESULT, "Skipping the file..."); + } + return TRUE; case IDCANCEL: if (cur) { dmc_xfer_cancel(cur); @@ -239,21 +289,25 @@ bool xfer_dialog_key(MSG *m) return dlg && IsDialogMessage(dlg, m); } -static void open_dialog(int protocol, bool sending) +static void open_dialog(int protocol, bool sending, bool resume) { if (dlg) DestroyWindow(dlg); dlg = CreateDialog(app.inst, MAKEINTRESOURCE(IDD_XFER), app.main, xfer_proc); if (!dlg) return; - char a[MAX_PATH + 32]; - _snprintf(a, sizeof(a), "%s File - %s", sending ? "Send" : "Receive", - dmc_xfer_label(protocol)); + char a[MAX_PATH + 32], label[80]; + _snprintf(label, sizeof(label), "%s%s", dmc_xfer_label(protocol), + resume ? " (crash recovery)" : ""); + label[sizeof(label) - 1] = '\0'; + _snprintf(a, sizeof(a), "%s File - %s", sending ? "Send" : "Receive", label); a[sizeof(a) - 1] = '\0'; SetWindowText(dlg, a); set_text(IDC_X_VERB, sending ? "Sending:" : "Receiving:"); set_text(IDC_X_WHERE, where); - set_text(IDC_X_PROTO, dmc_xfer_label(protocol)); + set_text(IDC_X_PROTO, label); + /* (Skip file: a receive of the protocols that can tell the sender) */ + EnableWindow(GetDlgItem(dlg, IDC_X_SKIP), !sending && dmc_xfer_can_skip(protocol)); HWND place = GetDlgItem(dlg, IDC_X_BAR); RECT r; GetWindowRect(place, &r); @@ -278,7 +332,8 @@ static void notified(void *ctx) PostMessage(app.main, WM_APP_XFER, 0, 0); } -static bool start(XferLine *l, dmc_xfer *x, int protocol, bool sending, int files) +static bool start(XferLine *l, dmc_xfer *x, int protocol, bool sending, int files, + bool resume) { if (!x) { MessageBox(app.main, "The transfer could not start.", "File Transfer", @@ -302,7 +357,7 @@ static bool start(XferLine *l, dmc_xfer *x, int protocol, bool sending, int file done_bytes = 0; last_files = 0; total_files = files; - open_dialog(protocol, sending); + open_dialog(protocol, sending, resume); dmc_xfer_set_notify(x, notified, NULL); xfer_pump(); return true; @@ -390,10 +445,14 @@ void xfer_cancel() static int receive_protocol(); -static bool start_receive(XferLine *l, int protocol, const char *name) +static bool start_receive(XferLine *l, int protocol, const char *name, bool resume) { folder(where, sizeof(where)); - return start(l, dmc_xfer_receive(protocol, where, name), protocol, false, 0); + dmc_xfer_options o; + memset(&o, 0, sizeof(o)); + o.resume = resume && protocol == DMC_XFER_ZMODEM; + return start(l, dmc_xfer_receive_opt(protocol, where, name, &o), protocol, + false, 0, o.resume != 0); } bool xfer_take(XferLine *l, const char *data, int n) @@ -411,7 +470,9 @@ bool xfer_take(XferLine *l, const char *data, int n) DMC_XFER_ZMODEM; if (at > 0 && *l->view) termview_feed(*l->view, data, at); - if (start_receive(l, p, NULL)) + /* (crash recovery as the Receive File dialog last had it) */ + if (start_receive(l, p, NULL, p == receive_protocol() && + ini_int("receive_resume", 0))) dmc_xfer_feed(cur, data + at, n - at); else if (*l->view) termview_feed(*l->view, data + at, n - at); @@ -444,6 +505,7 @@ struct XferAsk { XferLine *line; bool sending; int protocol; + bool resume; /* ZMODEM with Crash Recovery */ bool remote_ready; /* its rz waits already: nothing to type */ char files[MAX_FILES][MAX_PATH]; int nfiles; @@ -459,11 +521,9 @@ static int receive_protocol() static void protocols_fill(HWND combo, int sel) { SendMessage(combo, CB_RESETCONTENT, 0, 0); - for (int i = 0; i < DMC_XFER_PROTOCOLS; i++) { - char a[64]; - _snprintf(a, sizeof(a), "%s%s", dmc_xfer_label(i), - dmc_xfer_batch(i) ? "" : " (one file)"); - a[sizeof(a) - 1] = '\0'; + for (int i = 0; i < CHOICES; i++) { + char a[80]; + choice_label(i, a, sizeof(a)); SendMessage(combo, CB_ADDSTRING, 0, (LPARAM)a); } SendMessage(combo, CB_SETCURSEL, (WPARAM)sel, 0); @@ -472,10 +532,12 @@ static void protocols_fill(HWND combo, int sel) /* the words under the protocol, and the command it types */ static void protocol_changed(HWND d, XferAsk *a) { - int p = (int)SendDlgItemMessage(d, IDC_F_PROTO, CB_GETCURSEL, 0, 0); - if (p < 0) - p = 0; + int i = (int)SendDlgItemMessage(d, IDC_F_PROTO, CB_GETCURSEL, 0, 0); + if (i < 0 || i >= CHOICES) + i = 0; + int p = choices[i].protocol; a->protocol = p; + a->resume = choices[i].resume != 0; SetDlgItemText(d, IDC_F_COMMAND, a->remote_ready ? "" : dmc_xfer_command(p, !a->sending)); const char *hint; @@ -493,6 +555,18 @@ static void protocol_changed(HWND d, XferAsk *a) "or a boot loader." : "The records that come (their checksums examined) are kept " "as the binary image that they hold, gaps as 0xFF."; + else if (a->resume) + hint = a->sending ? + "The receiver is asked to continue a part of a file that it " + "has (rz -r does). Empty: the remote is already receiving." : + "A part of a file in the folder is continued from its end, and " + "a part received is kept if the transfer stops."; + else if (p == DMC_XFER_YMODEM_G) + hint = a->sending ? + "Blocks go with no wait for an ACK, for a line that corrects " + "its errors (V.42). A receiver that asks for plain YMODEM gets it." : + "Blocks come with no ACKs, for a line that corrects its errors " + "(V.42). An error stops the transfer."; else if (a->sending) hint = "Empty: the remote is already receiving."; else @@ -601,7 +675,7 @@ static INT_PTR CALLBACK ask_proc(HWND d, UINT m, WPARAM wp, LPARAM lp) case WM_INITDIALOG: { a = (XferAsk *)lp; SetWindowLongPtr(d, DWLP_USER, (LONG_PTR)a); - protocols_fill(GetDlgItem(d, IDC_F_PROTO), a->protocol); + protocols_fill(GetDlgItem(d, IDC_F_PROTO), choice_of(a->protocol, a->resume)); SendDlgItemMessage(d, IDC_F_COMMAND, EM_LIMITTEXT, sizeof(a->command) - 1, 0); if (a->sending) { char path[MAX_PATH], v[MAX_PATH]; @@ -645,6 +719,7 @@ static INT_PTR CALLBACK ask_proc(HWND d, UINT m, WPARAM wp, LPARAM lp) a->nfiles = 1; ini_set("file", a->files[0]); ini_set_int("send", a->protocol); + ini_set_int("send_resume", a->resume); } else { if (!text[0]) { MessageBox(d, "Type the folder for the received files.", @@ -654,6 +729,7 @@ static INT_PTR CALLBACK ask_proc(HWND d, UINT m, WPARAM wp, LPARAM lp) CreateDirectory(text, NULL); ini_set("folder", text); ini_set_int("receive", a->protocol); + ini_set_int("receive_resume", a->resume); GetDlgItemText(d, IDC_F_NAME, a->name, sizeof(a->name)); } EndDialog(d, IDOK); @@ -699,6 +775,8 @@ void xfer_send_dialog(XferLine *l, int protocol) ini_int("send", DMC_XFER_ZMODEM); if (a.protocol < 0 || a.protocol >= DMC_XFER_PROTOCOLS) a.protocol = DMC_XFER_ZMODEM; + a.resume = protocol == -1 && a.protocol == DMC_XFER_ZMODEM && + ini_int("send_resume", 0); if (DialogBoxParam(app.inst, MAKEINTRESOURCE(IDD_XSEND), app.main, ask_proc, (LPARAM)&a) != IDOK || !line_ready(l)) return; @@ -717,8 +795,14 @@ void xfer_send_dialog(XferLine *l, int protocol) char *slash = strrchr(where, DIR_SEP[0]); if (slash) *slash = '\0'; - start(l, dmc_xfer_send(a.protocol, paths, a.nfiles), a.protocol, true, - a.nfiles); + /* (plain text paced as ASCII Setup has it) */ + dmc_xfer_options o; + memset(&o, 0, sizeof(o)); + o.resume = a.resume; + o.line_delay_ms = ascii_int("line_delay", 0); + o.char_delay_ms = ascii_int("char_delay", 0); + start(l, dmc_xfer_send_opt(a.protocol, paths, a.nfiles, &o), a.protocol, true, + a.nfiles, a.resume); } void xfer_receive_dialog(XferLine *l) @@ -730,6 +814,7 @@ void xfer_receive_dialog(XferLine *l) a.line = l; a.sending = false; a.protocol = receive_protocol(); + a.resume = a.protocol == DMC_XFER_ZMODEM && ini_int("receive_resume", 0); if (DialogBoxParam(app.inst, MAKEINTRESOURCE(IDD_XRECV), app.main, ask_proc, (LPARAM)&a) != IDOK || !line_ready(l)) return; @@ -754,5 +839,5 @@ void xfer_receive_dialog(XferLine *l) c[sizeof(c) - 1] = '\0'; l->write(c, (int)strlen(c)); } - start_receive(l, a.protocol, name[0] ? name : NULL); + start_receive(l, a.protocol, name[0] ? name : NULL, a.resume); }