mirror of
https://github.com/D-Modem/gui.git
synced 2026-11-28 14:19:58 -08:00
ZMODEM: zstd compression (CANZSTD/ZTZSTD)
ZMODEM with zstd as docs/zmodem-zstd.md of D-Modem specifies, on top of the crash recovery of #22: - core/src/zstd_codec.c: a Zstandard (RFC 8878) encoder and decoder for whole frames, and XXH64, in plain C (a port of the app's Zstd.kt; no libzstd). - xfer_z.c: CANZSTD in ZRINIT ZF1, ZTZSTD in ZFILE ZF2. Each chunk of the file goes as one frame (64 KB, halved after a ZRPOS to 1 KB, doubled after an ACK); the receiver writes only whole frames, so positions stay file offsets and ZRPOS and crash recovery work as before. Files of 0 bytes, with a compressed extension, or whose first 64 KB don't get 5% smaller go as they are. A ZTZSTD file is skipped when we did not set CANZSTD. - dmc_xfer_options.no_zstd stops it, and dmc_xfer_zstd() tells if the file now goes with zstd (the status struct is not changed). - Windows: Transfer > ZMODEM Compression (zstd), on by default, and "(zstd)" after the file name in the progress dialog. - Tests: the specification's vector, XXH64, round trips, against the reference zstd CLI when installed; ZMODEM with zstd in memory (smaller on the line, a noisy line, off on either side, a .zip, resume). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B82eRR1WNeCEtXUcjE8RgY
This commit is contained in:
15 files changed
+2164
-37
No files matched your search
@@ -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, 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
|
||||
- **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, with Crash Recovery, and with zstd compression to the D-Modem BBS and app), 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)
|
||||
|
||||
+2
-1
@@ -28,7 +28,8 @@ set(DMODEMCTL_SOURCES
|
||||
src/xfer_hex.c
|
||||
src/xfer_kermit.c
|
||||
src/xfer_xy.c
|
||||
src/xfer_z.c)
|
||||
src/xfer_z.c
|
||||
src/zstd_codec.c)
|
||||
|
||||
if(DMODEMCTL_SHARED)
|
||||
add_library(dmodemctl SHARED ${DMODEMCTL_SOURCES})
|
||||
|
||||
@@ -695,6 +695,11 @@ typedef struct dmc_xfer_options {
|
||||
/* plain text sent: a wait after each line, and after each
|
||||
character, in ms */
|
||||
int line_delay_ms, char_delay_ms;
|
||||
/* ZMODEM with zstd stopped: files are not compressed (sending), and
|
||||
compressed files are not taken (receiving). When it is not
|
||||
stopped, zstd is used only where the other side can use it too
|
||||
(D-Modem's BBS, GUI and app) */
|
||||
int no_zstd;
|
||||
} dmc_xfer_options;
|
||||
|
||||
/* files from the remote into folder (name: XMODEM's file, which it does
|
||||
@@ -715,6 +720,8 @@ DMC_API dmc_xfer *dmc_xfer_send_opt(int protocol, const char *const *paths,
|
||||
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);
|
||||
/* whether the file now goes compressed with zstd (ZMODEM with zstd) */
|
||||
DMC_API int dmc_xfer_zstd(dmc_xfer *x);
|
||||
/* bytes from the remote */
|
||||
DMC_API void dmc_xfer_feed(dmc_xfer *x, const char *data, int n);
|
||||
/* bytes for the remote: how many */
|
||||
|
||||
@@ -784,6 +784,16 @@ int dmc_xfer_can_skip(int protocol)
|
||||
return protocol == DMC_XFER_ZMODEM || protocol == DMC_XFER_KERMIT;
|
||||
}
|
||||
|
||||
void xfer_zstd(dmc_xfer *x, int on)
|
||||
{
|
||||
x->zstd = on;
|
||||
}
|
||||
|
||||
int dmc_xfer_zstd(dmc_xfer *x)
|
||||
{
|
||||
return x ? x->zstd : 0;
|
||||
}
|
||||
|
||||
dmc_xfer *dmc_xfer_receive_opt(int protocol, const char *folder,
|
||||
const char *name, const dmc_xfer_options *o)
|
||||
{
|
||||
|
||||
@@ -77,6 +77,7 @@ struct dmc_xfer {
|
||||
int keep_partial; /* crash recovery: a part is kept */
|
||||
dmc_xfer_options opt;
|
||||
volatile int skip; /* the user skipped the file now */
|
||||
volatile int zstd; /* the file now goes with zstd */
|
||||
void *heap; /* freed at the end */
|
||||
void *proto; /* the protocol's state (Kermit's) */
|
||||
void (*proto_free)(void *); /* how it's freed (NULL: free) */
|
||||
@@ -129,6 +130,8 @@ void xfer_create(dmc_xfer *x, const char *name);
|
||||
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);
|
||||
/* whether the file now goes compressed with zstd (ZMODEM) */
|
||||
void xfer_zstd(dmc_xfer *x, int on);
|
||||
/* the user's skip of the file now, taken (once) */
|
||||
int xfer_skip_wanted(dmc_xfer *x);
|
||||
/* the received file written */
|
||||
|
||||
+270
-35
@@ -8,11 +8,21 @@
|
||||
* (ZCRCG) with the receiver's ZRPOS putting the sender back
|
||||
* where the line went wrong, files offered by name, size and
|
||||
* time, and ended with ZFIN and "OO".
|
||||
*
|
||||
* With zstd (docs/zmodem-zstd.md of D-Modem): the receiver
|
||||
* tells it can decompress by CANZSTD in its ZRINIT's ZF1, and
|
||||
* the sender asks for it file by file with ZTZSTD in the
|
||||
* ZFILE's ZF2. The file then goes as independent zstd frames,
|
||||
* one for each chunk of the file, and every position on the
|
||||
* line is still an offset in the file: a ZDATA starts a frame,
|
||||
* and the receiver writes (and acknowledges) only whole
|
||||
* frames, so ZRPOS and crash recovery work as without zstd.
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "xfer.h"
|
||||
#include "zstd_codec.h"
|
||||
|
||||
#define ZPAD '*'
|
||||
#define ZDLE 0x18
|
||||
@@ -39,6 +49,17 @@ enum {
|
||||
#define CANFC32 0x20
|
||||
#define ZCBIN 1
|
||||
#define ZCRECOV 3 /* ZF0: continue a part of the file */
|
||||
#define CANZSTD 0x40 /* ZRINIT ZF1: zstd frames taken */
|
||||
#define ZTZSTD 8 /* ZFILE ZF2: the file in zstd frames */
|
||||
|
||||
/* zstd: the file's bytes in each frame the sender makes (halved after a
|
||||
ZRPOS, to ZSTD_MIN_CHUNK; doubled after an ACK), and the most a frame
|
||||
may hold */
|
||||
#define ZSTD_CHUNK (64 * 1024)
|
||||
#define ZSTD_MIN_CHUNK 1024
|
||||
#define ZSTD_MAX (256 * 1024)
|
||||
/* the receiver's frames not complete yet, at the most */
|
||||
#define ZSTD_HELD (ZSTD_MAX + ZSTD_MAX / 8 + 8200 + 64)
|
||||
|
||||
#define BLOCK 1024
|
||||
/* bytes between the sender's ZCRCQs (its window) */
|
||||
@@ -73,6 +94,13 @@ struct z {
|
||||
unsigned char data[8200]; /* a data subpacket that came */
|
||||
unsigned char out[2 * 8200 + 32];
|
||||
int on;
|
||||
int zstd; /* zstd frames: we take them (receiving),
|
||||
the receiver takes them (sending) */
|
||||
unsigned char *held; /* the frame not complete yet */
|
||||
int nheld;
|
||||
unsigned char *frame; /* the frame being sent */
|
||||
unsigned char *chunk; /* the file's bytes in it */
|
||||
int chunk_size; /* how many of them, at the most */
|
||||
};
|
||||
|
||||
static long long hpos(const struct header *h)
|
||||
@@ -90,11 +118,14 @@ static struct header hdr_pos(int type, long long pos)
|
||||
return h;
|
||||
}
|
||||
|
||||
static struct header hdr_flags(int type, int f0)
|
||||
/* a header of flags: ZF0 (p[3]), ZF1 (p[2]) and ZF2 (p[1]) */
|
||||
static struct header hdr_flags(int type, int f0, int f1, int f2)
|
||||
{
|
||||
struct header h;
|
||||
h.type = type;
|
||||
h.p[0] = h.p[1] = h.p[2] = 0;
|
||||
h.p[0] = 0;
|
||||
h.p[1] = (unsigned char)f2;
|
||||
h.p[2] = (unsigned char)f1;
|
||||
h.p[3] = (unsigned char)f0;
|
||||
return h;
|
||||
}
|
||||
@@ -442,6 +473,17 @@ static int read_data_or_fail(struct z *z, int max, int *end)
|
||||
return n;
|
||||
}
|
||||
|
||||
static void z_free(void *p)
|
||||
{
|
||||
struct z *z = (struct z *)p;
|
||||
if (z) {
|
||||
free(z->held);
|
||||
free(z->frame);
|
||||
free(z->chunk);
|
||||
free(z);
|
||||
}
|
||||
}
|
||||
|
||||
static struct z *z_new(dmc_xfer *x)
|
||||
{
|
||||
struct z *z = (struct z *)calloc(1, sizeof(*z));
|
||||
@@ -449,20 +491,119 @@ static struct z *z_new(dmc_xfer *x)
|
||||
xfer_fail(x, "out of memory");
|
||||
z->x = x;
|
||||
x->proto = z;
|
||||
x->proto_free = z_free;
|
||||
return z;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* zstd */
|
||||
|
||||
/* the extensions of files that zstd does not make smaller: sent as they
|
||||
are */
|
||||
static const char *const precompressed[] = {
|
||||
"zip", "gz", "tgz", "bz2", "xz", "zst", "lz", "lzma", "7z", "rar",
|
||||
"arj", "arc", "lzh", "lha", "zoo", "pak", "sqz", "cab", "z", "jpg",
|
||||
"jpeg", "png", "gif", "webp", "mp3", "ogg", "opus", "flac", "mp4",
|
||||
"mkv", "avi", "mov", "webm", "jar", "apk", "docx", "xlsx", "pptx",
|
||||
"odt", "epub", "pdf"
|
||||
};
|
||||
|
||||
static int worth_compressing(const char *name)
|
||||
{
|
||||
const char *dot = NULL;
|
||||
for (const char *p = name; *p; p++)
|
||||
if (*p == '.')
|
||||
dot = p + 1;
|
||||
else if (*p == '/' || *p == '\\')
|
||||
dot = NULL;
|
||||
if (!dot)
|
||||
return 1;
|
||||
for (size_t i = 0; i < sizeof(precompressed) / sizeof(precompressed[0]); i++)
|
||||
if (same_ci(dot, precompressed[i]))
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* whether the start of the open file gets at least 5% smaller with zstd
|
||||
(and the file back at its start) */
|
||||
static int sample_compresses(struct z *z)
|
||||
{
|
||||
dmc_xfer *x = z->x;
|
||||
unsigned char *buf = (unsigned char *)malloc(ZSTD_CHUNK);
|
||||
int n = 0, fn = 0, yes = 1;
|
||||
if (!buf)
|
||||
return 0;
|
||||
while (n < ZSTD_CHUNK) {
|
||||
int r = xfer_src_read(x, buf + n, ZSTD_CHUNK - n);
|
||||
if (r <= 0)
|
||||
break;
|
||||
n += r;
|
||||
}
|
||||
if (fseek(x->src, 0, SEEK_SET) != 0) {
|
||||
free(buf);
|
||||
xfer_fail(x, "can't go back to the start of the file");
|
||||
}
|
||||
unsigned char *f = zc_encode(buf, n, &fn);
|
||||
if (!f || n == 0 || (long long)fn * 100 > (long long)n * 95)
|
||||
yes = 0;
|
||||
free(f);
|
||||
free(buf);
|
||||
return yes;
|
||||
}
|
||||
|
||||
/* the received data with the frames it completes decompressed and
|
||||
written (*wrote the file's bytes): -1 when a frame is bad, the frames
|
||||
held are too many, or a subpacket that ends a segment (ZCRCW, ZCRCE)
|
||||
ends in the middle of a frame; else 0 */
|
||||
static int unpack(struct z *z, const unsigned char *data, int n, int end,
|
||||
long long *wrote)
|
||||
{
|
||||
*wrote = 0;
|
||||
if (!z->held) {
|
||||
z->held = (unsigned char *)malloc(ZSTD_HELD);
|
||||
if (!z->held)
|
||||
xfer_fail(z->x, "out of memory");
|
||||
}
|
||||
if (z->nheld + n > ZSTD_HELD)
|
||||
return -1;
|
||||
memcpy(z->held + z->nheld, data, (size_t)n);
|
||||
z->nheld += n;
|
||||
int at = 0;
|
||||
for (;;) {
|
||||
int len = zc_frame_len(z->held + at, z->nheld - at, ZSTD_MAX);
|
||||
if (len < 0)
|
||||
return -1;
|
||||
if (len == 0)
|
||||
break;
|
||||
int got;
|
||||
unsigned char *c = zc_decode(z->held + at, len, ZSTD_MAX, &got, NULL);
|
||||
if (!c)
|
||||
return -1;
|
||||
xfer_dst_write(z->x, c, got);
|
||||
free(c);
|
||||
*wrote += got;
|
||||
at += len;
|
||||
}
|
||||
memmove(z->held, z->held + at, (size_t)(z->nheld - at));
|
||||
z->nheld -= at;
|
||||
if (z->nheld > 0 && (end == ZCRCW || end == ZCRCE))
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* receiving (the other side runs sz) */
|
||||
|
||||
static void zrinit(struct z *z)
|
||||
{
|
||||
send_hex(z, hdr_flags(ZRINIT, CANFDX | CANOVIO | CANFC32));
|
||||
send_hex(z, hdr_flags(ZRINIT, CANFDX | CANOVIO | CANFC32,
|
||||
z->zstd ? CANZSTD : 0, 0));
|
||||
}
|
||||
|
||||
/* 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)
|
||||
/* a file's data after its ZFILE (its info, len bytes; f0 and f2 its ZF0
|
||||
and ZF2): 1 received, 0 skipped */
|
||||
static int receive_file(struct z *z, const unsigned char *info, int len, int f0,
|
||||
int f2)
|
||||
{
|
||||
dmc_xfer *x = z->x;
|
||||
char name[XFER_NAME], meta[256];
|
||||
@@ -484,6 +625,13 @@ static int receive_file(struct z *z, const unsigned char *info, int len, int f0)
|
||||
if (*e == ' ')
|
||||
mtime = strtoll(e + 1, NULL, 8);
|
||||
long long pos = 0, rpos_at = 0;
|
||||
/* zstd frames: only if we said we take them */
|
||||
int zstd = f2 == ZTZSTD;
|
||||
if (zstd && !z->zstd) {
|
||||
send_hex(z, hdr_pos(ZSKIP, 0));
|
||||
return 0;
|
||||
}
|
||||
z->nheld = 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) {
|
||||
@@ -498,12 +646,14 @@ static int receive_file(struct z *z, const unsigned char *info, int len, int f0)
|
||||
}
|
||||
xfer_file_start(x, x->dst_name, size);
|
||||
xfer_bytes(x, pos);
|
||||
xfer_zstd(x, zstd);
|
||||
int errors = 0, ok = 0;
|
||||
struct header h;
|
||||
/* (until the sender has had time to hear our ZRPOS, what still comes
|
||||
of its stream from before isn't an error, and isn't asked for
|
||||
again: each ZRPOS a restart of its own) */
|
||||
#define RPOS() (send_hex(z, hdr_pos(ZRPOS, pos)), rpos_at = xfer_now())
|
||||
#define RPOS() (z->nheld = 0, send_hex(z, hdr_pos(ZRPOS, pos)), \
|
||||
rpos_at = xfer_now())
|
||||
#define WAITING() (xfer_now() - rpos_at < RPOS_GRACE_MS)
|
||||
RPOS();
|
||||
for (;;) {
|
||||
@@ -533,9 +683,19 @@ static int receive_file(struct z *z, const unsigned char *info, int len, int f0)
|
||||
RPOS();
|
||||
continue;
|
||||
}
|
||||
/* (a ZDATA starts a frame) */
|
||||
z->nheld = 0;
|
||||
for (;;) {
|
||||
int end;
|
||||
long long wrote = 0;
|
||||
int n = read_data_or_fail(z, 8192, &end);
|
||||
if (n >= 0 && zstd) {
|
||||
/* (the whole frames written, even when
|
||||
one after them is bad) */
|
||||
if (unpack(z, z->data, n, end, &wrote) < 0)
|
||||
n = -1;
|
||||
pos += wrote;
|
||||
}
|
||||
if (n < 0) {
|
||||
if (++errors > MAX_ERRORS)
|
||||
xfer_fail(x, "too many errors on the line");
|
||||
@@ -544,8 +704,10 @@ static int receive_file(struct z *z, const unsigned char *info, int len, int f0)
|
||||
RPOS();
|
||||
break;
|
||||
}
|
||||
xfer_dst_write(x, z->data, n);
|
||||
pos += n;
|
||||
if (!zstd) {
|
||||
xfer_dst_write(x, z->data, n);
|
||||
pos += n;
|
||||
}
|
||||
errors = 0;
|
||||
xfer_bytes(x, pos);
|
||||
if (end == ZCRCW) {
|
||||
@@ -584,6 +746,7 @@ static int receive_file(struct z *z, const unsigned char *info, int len, int f0)
|
||||
}
|
||||
#undef RPOS
|
||||
#undef WAITING
|
||||
xfer_zstd(x, 0);
|
||||
if (!ok) {
|
||||
xfer_abandon(x);
|
||||
return 0;
|
||||
@@ -597,6 +760,7 @@ void z_receive(dmc_xfer *x)
|
||||
struct z *z = z_new(x);
|
||||
unsigned char info[4100];
|
||||
int errors = 0;
|
||||
z->zstd = !x->opt.no_zstd;
|
||||
zrinit(z);
|
||||
long long last_header = xfer_now();
|
||||
for (;;) {
|
||||
@@ -633,7 +797,7 @@ void z_receive(dmc_xfer *x)
|
||||
break;
|
||||
}
|
||||
memcpy(info, z->data, (size_t)n);
|
||||
if (receive_file(z, info, n, h.p[3])) {
|
||||
if (receive_file(z, info, n, h.p[3], h.p[1])) {
|
||||
xfer_file_done(x);
|
||||
zrinit(z);
|
||||
}
|
||||
@@ -727,17 +891,41 @@ static long long seek(struct z *z, long long to)
|
||||
return to;
|
||||
}
|
||||
|
||||
/* the receiver's word while we send, if any (what isn't a header's
|
||||
start let go first: an XON, a CR LF): 1 with its header in *h */
|
||||
static int heard(struct z *z, struct header *h)
|
||||
{
|
||||
dmc_xfer *x = z->x;
|
||||
for (;;) {
|
||||
int c = xfer_peek(x);
|
||||
if (c < 0 || c == ZPAD || c == ZDLE)
|
||||
break;
|
||||
xfer_read(x, 0);
|
||||
}
|
||||
return xfer_available(x) && header_or_null(z, h);
|
||||
}
|
||||
|
||||
/* the file's data from start in ZCRCG subpackets, a ZCRCQ every WINDOW
|
||||
bytes to hear how it goes, ZCRCE and ZEOF at the end; a ZRPOS from the
|
||||
receiver puts it back there; done at the receiver's ZRINIT */
|
||||
static void stream(struct z *z, long long start)
|
||||
receiver puts it back there; done at the receiver's ZRINIT. With zstd
|
||||
(zstd set) each chunk of the file goes as one frame, in as many
|
||||
subpackets as it takes, and only a frame's end ends a ZCRCQ or ZCRCE */
|
||||
static void stream(struct z *z, long long start, int zstd)
|
||||
{
|
||||
dmc_xfer *x = z->x;
|
||||
unsigned char buf[BLOCK];
|
||||
int errors = 0;
|
||||
long long at = start, last_back = -1, pos;
|
||||
struct header h;
|
||||
/* (errors counted while no progress is made) */
|
||||
if (zstd && !z->chunk) {
|
||||
z->chunk = (unsigned char *)malloc(ZSTD_CHUNK);
|
||||
if (!z->chunk)
|
||||
xfer_fail(x, "out of memory");
|
||||
}
|
||||
if (z->chunk_size == 0)
|
||||
z->chunk_size = ZSTD_CHUNK;
|
||||
/* (errors counted while no progress is made; a line with errors
|
||||
gets smaller frames) */
|
||||
#define BACK(to) do { \
|
||||
long long to_ = (to); \
|
||||
if (to_ > last_back) \
|
||||
@@ -747,6 +935,8 @@ static void stream(struct z *z, long long start)
|
||||
xfer_fail(x, "too many errors on the line"); \
|
||||
xfer_error(x); \
|
||||
at = to_; \
|
||||
if (z->chunk_size / 2 >= ZSTD_MIN_CHUNK) \
|
||||
z->chunk_size /= 2; \
|
||||
xfer_sleep(20); \
|
||||
xfer_purge(x); \
|
||||
} while (0)
|
||||
@@ -755,15 +945,7 @@ static void stream(struct z *z, long long start)
|
||||
send_bin(z, hdr_pos(ZDATA, pos));
|
||||
int since_ack = 0, restart = 0;
|
||||
for (;;) {
|
||||
/* the receiver's word in between, if any (what isn't a
|
||||
header's start let go first: an XON, a CR LF) */
|
||||
for (;;) {
|
||||
int c = xfer_peek(x);
|
||||
if (c < 0 || c == ZPAD || c == ZDLE)
|
||||
break;
|
||||
xfer_read(x, 0);
|
||||
}
|
||||
if (xfer_available(x) && header_or_null(z, &h)) {
|
||||
if (heard(z, &h)) {
|
||||
if (h.type == ZRPOS) {
|
||||
BACK(hpos(&h));
|
||||
restart = 1;
|
||||
@@ -774,17 +956,59 @@ static void stream(struct z *z, long long start)
|
||||
if (h.type == ZSKIP)
|
||||
return;
|
||||
}
|
||||
int len = 0;
|
||||
while (len < BLOCK) {
|
||||
int r = xfer_src_read(x, buf + len, BLOCK - len);
|
||||
if (r <= 0)
|
||||
int len = 0, end;
|
||||
if (!zstd) {
|
||||
while (len < BLOCK) {
|
||||
int r = xfer_src_read(x, buf + len, BLOCK - len);
|
||||
if (r <= 0)
|
||||
break;
|
||||
len += r;
|
||||
}
|
||||
pos += len;
|
||||
since_ack += len;
|
||||
end = len < BLOCK ? ZCRCE : since_ack >= WINDOW ? ZCRCQ : ZCRCG;
|
||||
send_data(z, buf, len, end);
|
||||
} else {
|
||||
/* a chunk of the file, as one frame */
|
||||
int fn = 0;
|
||||
while (len < z->chunk_size) {
|
||||
int r = xfer_src_read(x, z->chunk + len, z->chunk_size - len);
|
||||
if (r <= 0)
|
||||
break;
|
||||
len += r;
|
||||
}
|
||||
free(z->frame);
|
||||
z->frame = NULL;
|
||||
if (len > 0) {
|
||||
z->frame = zc_encode(z->chunk, len, &fn);
|
||||
if (!z->frame)
|
||||
xfer_fail(x, "out of memory");
|
||||
}
|
||||
since_ack += fn;
|
||||
end = len < z->chunk_size ? ZCRCE : since_ack >= WINDOW ? ZCRCQ : ZCRCG;
|
||||
int o = 0;
|
||||
do {
|
||||
int k = fn - o < BLOCK ? fn - o : BLOCK;
|
||||
send_data(z, z->frame + o, k, o + k < fn ? ZCRCG : end);
|
||||
o += k;
|
||||
/* (a ZRPOS in the middle of the frame: the
|
||||
rest of it not sent) */
|
||||
if (o < fn && heard(z, &h)) {
|
||||
if (h.type == ZRPOS) {
|
||||
BACK(hpos(&h));
|
||||
restart = 1;
|
||||
break;
|
||||
}
|
||||
if (h.type == ZCAN || h.type == ZABORT)
|
||||
xfer_cancelled(x, "cancelled by the receiver");
|
||||
if (h.type == ZSKIP)
|
||||
return;
|
||||
}
|
||||
} while (o < fn);
|
||||
if (restart)
|
||||
break;
|
||||
len += r;
|
||||
pos += len;
|
||||
}
|
||||
pos += len;
|
||||
since_ack += len;
|
||||
int end = len < BLOCK ? ZCRCE : since_ack >= WINDOW ? ZCRCQ : ZCRCG;
|
||||
send_data(z, buf, len, end);
|
||||
xfer_bytes(x, pos);
|
||||
if (end == ZCRCE)
|
||||
break;
|
||||
@@ -798,6 +1022,8 @@ static void stream(struct z *z, long long start)
|
||||
restart = 1;
|
||||
break;
|
||||
}
|
||||
if (z->chunk_size * 2 <= ZSTD_CHUNK)
|
||||
z->chunk_size *= 2;
|
||||
}
|
||||
}
|
||||
if (restart)
|
||||
@@ -844,8 +1070,13 @@ static int send_file(struct z *z, const struct xfer_file *f, int files_left,
|
||||
long long start = -1;
|
||||
int tries = 0;
|
||||
struct header h;
|
||||
/* zstd: where the receiver takes it, for a file that gets smaller */
|
||||
xfer_open(z->x, f);
|
||||
int zstd = z->zstd && f->size > 0 && worth_compressing(f->name) &&
|
||||
sample_compresses(z);
|
||||
while (start < 0) {
|
||||
send_bin(z, hdr_flags(ZFILE, z->x->opt.resume ? ZCRECOV : ZCBIN));
|
||||
send_bin(z, hdr_flags(ZFILE, z->x->opt.resume ? ZCRECOV : ZCBIN, 0,
|
||||
zstd ? ZTZSTD : 0));
|
||||
send_data(z, info, n, ZCRCW);
|
||||
for (;;) {
|
||||
int got = header_or_null(z, &h);
|
||||
@@ -853,8 +1084,10 @@ static int send_file(struct z *z, const struct xfer_file *f, int files_left,
|
||||
start = hpos(&h);
|
||||
break;
|
||||
}
|
||||
if (got && h.type == ZSKIP)
|
||||
if (got && h.type == ZSKIP) {
|
||||
xfer_close(z->x);
|
||||
return 1;
|
||||
}
|
||||
if (got && h.type == ZCRC) {
|
||||
send_hex(z, hdr_pos(ZCRC, 0));
|
||||
continue;
|
||||
@@ -872,8 +1105,9 @@ static int send_file(struct z *z, const struct xfer_file *f, int files_left,
|
||||
break;
|
||||
}
|
||||
}
|
||||
xfer_open(z->x, f);
|
||||
stream(z, start);
|
||||
xfer_zstd(z->x, zstd);
|
||||
stream(z, start, zstd);
|
||||
xfer_zstd(z->x, 0);
|
||||
xfer_close(z->x);
|
||||
return 0;
|
||||
}
|
||||
@@ -883,6 +1117,7 @@ void z_send(dmc_xfer *x)
|
||||
struct z *z = z_new(x);
|
||||
struct header r = wait_for_zrinit(z);
|
||||
z->crc32 = (r.p[3] & CANFC32) != 0;
|
||||
z->zstd = !x->opt.no_zstd && (r.p[2] & CANZSTD) != 0;
|
||||
long long left = 0;
|
||||
for (int i = 0; i < x->nfiles; i++)
|
||||
left += x->files[i].size > 0 ? x->files[i].size : 0;
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 D-Modem Authors
|
||||
*
|
||||
* zstd_codec.h - Zstandard (RFC 8878), whole frames, in plain C: the
|
||||
* encoder makes one frame of a chunk (single segment, its
|
||||
* content size and checksum in it, no dictionary), as
|
||||
* ZMODEM with zstd sends a file (docs/zmodem-zstd.md of
|
||||
* D-Modem); the decoder takes any frame up to its limit.
|
||||
* A port of D-Modem's link/zstd_codec.cpp (and of the
|
||||
* app's Zstd.kt): a hash chain over the chunk with a lazy
|
||||
* match, the repeat offsets, literals raw, RLE or Huffman
|
||||
* coded, each sequence table the predefined or a new one.
|
||||
* Named so that it does not hide the system's <zstd.h>.
|
||||
*/
|
||||
#ifndef DMC_ZSTD_CODEC_H
|
||||
#define DMC_ZSTD_CODEC_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define ZC_MAGIC 0xFD2FB528u
|
||||
|
||||
/* data[0..n) as one frame (malloc'd, its length in *out_n); NULL when
|
||||
memory ran out */
|
||||
unsigned char *zc_encode(const unsigned char *data, int n, int *out_n);
|
||||
|
||||
/* the content of the frame frame[0..n) (all of it, no more), malloc'd,
|
||||
its length in *out_n (a frame of no content gives a block of 1 byte
|
||||
that is not part of it); NULL if it is not a frame, has a dictionary,
|
||||
a window or content over max, bad data or a bad checksum (why, if
|
||||
not NULL, says which) */
|
||||
unsigned char *zc_decode(const unsigned char *frame, int n, int max, int *out_n,
|
||||
const char **why);
|
||||
|
||||
/* the length of the frame at the start of b[0..n) from its headers (RFC
|
||||
8878 3.1.1.1 and 3.1.1.2): 0 while not all of it is there, -1 if it is
|
||||
not a frame that ZMODEM with zstd takes (a frame of no more than max
|
||||
bytes of content, without a dictionary, with its content size and
|
||||
checksum) */
|
||||
int zc_frame_len(const unsigned char *b, int n, int max);
|
||||
|
||||
/* XXH64 (seed 0), Zstandard's content checksum */
|
||||
uint64_t zc_xxh64(const unsigned char *b, size_t n);
|
||||
|
||||
#endif
|
||||
@@ -28,6 +28,7 @@
|
||||
|
||||
#include "dmodemctl.h"
|
||||
#include "xfer.h"
|
||||
#include "zstd_codec.h"
|
||||
|
||||
static int failures;
|
||||
|
||||
@@ -85,9 +86,12 @@ static void empty_dir(const char *dir)
|
||||
|
||||
/* the two transfers' line, until both end: every noise-th byte from a
|
||||
damaged (0 none) */
|
||||
static long long line_bytes; /* what a sent in the last link2 */
|
||||
|
||||
static void link2(dmc_xfer *a, dmc_xfer *b, int noise)
|
||||
{
|
||||
char buf[4096];
|
||||
line_bytes = 0;
|
||||
long long count = 0, end = xfer_now() + 180000;
|
||||
for (;;) {
|
||||
dmc_xfer_status sa, sb;
|
||||
@@ -106,6 +110,7 @@ static void link2(dmc_xfer *a, dmc_xfer *b, int noise)
|
||||
if (++count % noise == 0)
|
||||
buf[i] ^= 0x55;
|
||||
dmc_xfer_feed(b, buf, n);
|
||||
line_bytes += n;
|
||||
moved = 1;
|
||||
}
|
||||
while ((n = dmc_xfer_output(b, buf, sizeof(buf))) > 0) {
|
||||
@@ -375,6 +380,206 @@ static void resume(void)
|
||||
dmc_xfer_free(r);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* ZMODEM with zstd */
|
||||
|
||||
static int have(const char *prog);
|
||||
|
||||
/* n bytes of text that compresses, as a BBS's text files do */
|
||||
static unsigned char *text(size_t n, unsigned seed)
|
||||
{
|
||||
static const char *const words[] = {
|
||||
"modem", "carrier", "the", "BBS", "file", "area", "sysop",
|
||||
"message", "ZMODEM", "of", "and", "a", "upload", "download",
|
||||
"baud", "line", "\r\n"
|
||||
};
|
||||
unsigned char *d = (unsigned char *)malloc(n + 16);
|
||||
size_t at = 0;
|
||||
unsigned r = seed * 2654435761u + 1;
|
||||
while (at < n) {
|
||||
r = r * 1103515245u + 12345u;
|
||||
const char *w = words[(r >> 16) % (sizeof(words) / sizeof(words[0]))];
|
||||
size_t l = strlen(w);
|
||||
for (size_t i = 0; i < l && at < n; i++)
|
||||
d[at++] = (unsigned char)w[i];
|
||||
if (at < n)
|
||||
d[at++] = ' ';
|
||||
}
|
||||
return d;
|
||||
}
|
||||
|
||||
/* the codec: the specification's frame, there and back, against the
|
||||
reference zstd (where it is installed) */
|
||||
static void zstd_codec(void)
|
||||
{
|
||||
static const unsigned char vec[55] = {
|
||||
0x28, 0xb5, 0x2f, 0xfd, 0x24, 0x66, 0x55, 0x01, 0x00, 0x24, 0x02, 0x44, 0x2d, 0x4d,
|
||||
0x6f, 0x64, 0x65, 0x6d, 0x20, 0x5a, 0x4d, 0x4f, 0x44, 0x45, 0x4d, 0x20, 0x7a, 0x73,
|
||||
0x74, 0x64, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x76, 0x65, 0x63, 0x74, 0x6f, 0x72,
|
||||
0x2e, 0x0d, 0x0a, 0x01, 0x00, 0x8a, 0x82, 0xaa, 0x27, 0x32, 0x21, 0x69, 0x9d
|
||||
};
|
||||
const char *line = "D-Modem ZMODEM zstd test vector.\r\n";
|
||||
char want[103];
|
||||
snprintf(want, sizeof(want), "%s%s%s", line, line, line);
|
||||
int n;
|
||||
const char *why;
|
||||
unsigned char *d = zc_decode(vec, 55, 1024, &n, &why);
|
||||
CHECK(d && n == 102 && !memcmp(d, want, 102), "zstd: the vector: %s", why ? why : "differs");
|
||||
free(d);
|
||||
CHECK(zc_frame_len(vec, 55, 1024) == 55 && zc_frame_len(vec, 54, 1024) == 0,
|
||||
"zstd: the vector's length");
|
||||
unsigned char bad[55];
|
||||
memcpy(bad, vec, 55);
|
||||
bad[20] ^= 1;
|
||||
d = zc_decode(bad, 55, 1024, &n, &why);
|
||||
CHECK(!d, "zstd: a byte wrong, taken");
|
||||
free(d);
|
||||
d = zc_decode(vec, 55, 100, &n, &why);
|
||||
CHECK(!d, "zstd: over the limit, taken");
|
||||
free(d);
|
||||
CHECK(zc_xxh64(NULL, 0) == 0xEF46DB3751D8E999ULL &&
|
||||
zc_xxh64((const unsigned char *)"a", 1) == 0xD24EC4F1A98C6E5BULL &&
|
||||
zc_xxh64((const unsigned char *)"Nobody inspects the spammish repetition", 39) ==
|
||||
0xFBCEA83C8A378BF1ULL, "zstd: XXH64");
|
||||
|
||||
int ref = have("zstd");
|
||||
size_t sizes[] = { 0, 1, 255, 256, 65535, 65791, 65792, 200000 };
|
||||
for (size_t i = 0; i < sizeof(sizes) / sizeof(sizes[0]); i++) {
|
||||
for (int kind = 0; kind < 3; kind++) {
|
||||
size_t sz = sizes[i];
|
||||
unsigned char *t = kind == 0 ? text(sz, (unsigned)i) : (unsigned char *)malloc(sz + 1);
|
||||
for (size_t k = 0; kind > 0 && k < sz; k++)
|
||||
t[k] = kind == 1 ? big[k % sizeof(big)] : (unsigned char)(k / 1000);
|
||||
int fn = 0, back = 0;
|
||||
unsigned char *f = zc_encode(t, (int)sz, &fn);
|
||||
d = f ? zc_decode(f, fn, 256 * 1024, &back, &why) : NULL;
|
||||
CHECK(d && (size_t)back == sz && !memcmp(d, t, sz),
|
||||
"zstd: %zu bytes (%d) there and back: %s", sz, kind, why ? why : "differs");
|
||||
CHECK(f && zc_frame_len(f, fn, 256 * 1024) == fn, "zstd: %zu bytes: its length", sz);
|
||||
if (kind == 0 && sz == 65535)
|
||||
CHECK(fn * 3 < (int)sz, "zstd: 64 KB of text in %d bytes", fn);
|
||||
/* the reference zstd: ours decoded, and its frame decoded */
|
||||
if (sz > 0 && ref && f) {
|
||||
char a[700], b[700], cmd[1500];
|
||||
snprintf(a, sizeof(a), "%s/z.raw", tmp);
|
||||
snprintf(b, sizeof(b), "%s/z.zst", tmp);
|
||||
write_file(b, f, (size_t)fn);
|
||||
snprintf(cmd, sizeof(cmd), "zstd -q -d -f -o '%s' '%s'", a, b);
|
||||
CHECK(system(cmd) == 0, "zstd -d: our frame of %zu bytes", sz);
|
||||
check_same(a, t, sz, 0, "zstd -d");
|
||||
write_file(a, t, sz);
|
||||
snprintf(cmd, sizeof(cmd), "zstd -q -19 --check -f -o '%s' '%s'", b, a);
|
||||
CHECK(system(cmd) == 0, "zstd -19: %zu bytes", sz);
|
||||
size_t rn;
|
||||
unsigned char *r = read_file(b, &rn);
|
||||
unsigned char *rd = r ? zc_decode(r, (int)rn, 1 << 23, &back, &why) : NULL;
|
||||
CHECK(rd && (size_t)back == sz && !memcmp(rd, t, sz),
|
||||
"zstd: the reference's frame of %zu bytes: %s", sz, why ? why : "differs");
|
||||
free(r);
|
||||
free(rd);
|
||||
}
|
||||
free(d);
|
||||
free(f);
|
||||
free(t);
|
||||
}
|
||||
}
|
||||
if (ref)
|
||||
printf("zstd: against the reference zstd\n");
|
||||
}
|
||||
|
||||
/* one ZMODEM transfer of the files (paths) to dst with the two sides'
|
||||
options: both ends' results checked */
|
||||
static void zstd_link(const char *const *paths, int n, const char *dst,
|
||||
const dmc_xfer_options *so, const dmc_xfer_options *ro,
|
||||
int noise, const char *what)
|
||||
{
|
||||
dmc_xfer *r = dmc_xfer_receive_opt(DMC_XFER_ZMODEM, dst, NULL, ro);
|
||||
dmc_xfer *s = dmc_xfer_send_opt(DMC_XFER_ZMODEM, paths, n, so);
|
||||
link2(s, r, noise);
|
||||
dmc_xfer_status ss, rs;
|
||||
dmc_xfer_get_status(s, &ss);
|
||||
dmc_xfer_get_status(r, &rs);
|
||||
CHECK(!ss.result[0], "%s: the sender: %s", what, ss.result);
|
||||
CHECK(!rs.result[0], "%s: the receiver: %s", what, rs.result);
|
||||
if (noise)
|
||||
CHECK(rs.errors + ss.errors > 0, "%s: no errors on a noisy line", what);
|
||||
dmc_xfer_free(s);
|
||||
dmc_xfer_free(r);
|
||||
}
|
||||
|
||||
/* ZMODEM with zstd: smaller on the line, whole after errors, resumed,
|
||||
plain when one side doesn't take it or the file is compressed */
|
||||
static void zstd_transfers(void)
|
||||
{
|
||||
char src[600], dst[600], p[5][700], got[700];
|
||||
const char *paths[5];
|
||||
size_t sizes[5] = { 0, 1, 65536, 65537, 300001 };
|
||||
unsigned char *d[5];
|
||||
long long total = 0;
|
||||
dmc_xfer_options on, off;
|
||||
memset(&on, 0, sizeof(on));
|
||||
memset(&off, 0, sizeof(off));
|
||||
off.no_zstd = 1;
|
||||
snprintf(src, sizeof(src), "%s/src", tmp);
|
||||
snprintf(dst, sizeof(dst), "%s/dst", tmp);
|
||||
empty_dir(src);
|
||||
empty_dir(dst);
|
||||
for (int i = 0; i < 5; i++) {
|
||||
d[i] = text(sizes[i], (unsigned)i + 1);
|
||||
snprintf(p[i], sizeof(p[i]), "%s/f%d.txt", src, i);
|
||||
write_file(p[i], d[i], sizes[i]);
|
||||
paths[i] = p[i];
|
||||
total += (long long)sizes[i];
|
||||
}
|
||||
zstd_link(paths, 5, dst, &on, &on, 0, "zstd");
|
||||
for (int i = 0; i < 5; i++) {
|
||||
snprintf(got, sizeof(got), "%s/f%d.txt", dst, i);
|
||||
check_same(got, d[i], sizes[i], 0, "zstd");
|
||||
}
|
||||
CHECK(line_bytes * 2 < total, "zstd: %lld bytes on the line for %lld", line_bytes, total);
|
||||
printf("ZMODEM with zstd: %lld bytes on the line for %lld\n", line_bytes, total);
|
||||
|
||||
/* on a noisy line */
|
||||
empty_dir(dst);
|
||||
zstd_link(paths + 4, 1, dst, &on, &on, 3001, "zstd (noisy)");
|
||||
snprintf(got, sizeof(got), "%s/f4.txt", dst);
|
||||
check_same(got, d[4], sizes[4], 0, "zstd (noisy)");
|
||||
|
||||
/* zstd stopped on either side, or a compressed file: plain */
|
||||
for (int k = 0; k < 3; k++) {
|
||||
char zip[700];
|
||||
const char *one[1] = { paths[4] };
|
||||
empty_dir(dst);
|
||||
if (k == 2) {
|
||||
snprintf(zip, sizeof(zip), "%s/f4.zip", src);
|
||||
write_file(zip, d[4], sizes[4]);
|
||||
one[0] = zip;
|
||||
}
|
||||
zstd_link(one, 1, dst, k == 0 ? &off : &on, k == 1 ? &off : &on, 0, "zstd off");
|
||||
snprintf(got, sizeof(got), "%s/f4.%s", dst, k == 2 ? "zip" : "txt");
|
||||
check_same(got, d[4], sizes[4], 0, "zstd off");
|
||||
CHECK(line_bytes >= (long long)sizes[4],
|
||||
"zstd off (%d): %lld bytes on the line: compressed", k, line_bytes);
|
||||
}
|
||||
|
||||
/* crash recovery with zstd: from the end of the part (fewer bytes on
|
||||
the line than for the whole file) */
|
||||
dmc_xfer_options res = on;
|
||||
res.resume = 1;
|
||||
empty_dir(dst);
|
||||
zstd_link(paths + 4, 1, dst, &on, &on, 0, "zstd (whole)");
|
||||
long long whole = line_bytes;
|
||||
empty_dir(dst);
|
||||
snprintf(got, sizeof(got), "%s/f4.txt", dst);
|
||||
write_file(got, d[4], 123457);
|
||||
zstd_link(paths + 4, 1, dst, &on, &res, 0, "zstd (resume)");
|
||||
check_same(got, d[4], sizes[4], 0, "zstd (resume)");
|
||||
CHECK(line_bytes * 4 < whole * 3,
|
||||
"zstd (resume): %lld bytes on the line, %lld for all: not resumed", line_bytes, whole);
|
||||
for (int i = 0; i < 5; i++)
|
||||
free(d[i]);
|
||||
}
|
||||
|
||||
/* plain text with the waits of ASCII Setup */
|
||||
static void text_delays(void)
|
||||
{
|
||||
@@ -719,6 +924,9 @@ int main(void)
|
||||
printf("skip file\n");
|
||||
resume();
|
||||
printf("ZMODEM crash recovery\n");
|
||||
zstd_codec();
|
||||
printf("zstd\n");
|
||||
zstd_transfers();
|
||||
cancel();
|
||||
detect();
|
||||
interop();
|
||||
|
||||
@@ -170,6 +170,23 @@ protocols are:
|
||||
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.
|
||||
- ZMODEM with zstd (the specification is `docs/zmodem-zstd.md` in the
|
||||
D-Modem repository). The D-Modem BBS, GUI and app use this mode. The
|
||||
receiver sets `CANZSTD` (0x40) in ZF1 of its ZRINIT. The sender sets
|
||||
`ZTZSTD` (8) in ZF2 of the ZFILE for each file that it compresses. Then
|
||||
the sender sends the file as independent zstd frames, one frame for each
|
||||
chunk of 64 KB. After a ZRPOS, the chunk is half as large (minimum 1 KB).
|
||||
After an ACK, the chunk is two times as large (maximum 64 KB). The
|
||||
receiver writes only complete frames. All positions are file offsets,
|
||||
thus ZRPOS and crash recovery operate as without zstd. The sender does
|
||||
not compress a file of 0 bytes, a file with a compressed extension (for
|
||||
example `.zip`, `.gz` or `.jpg`), or a file whose first 64 KB do not
|
||||
become 5% smaller. When the other side does not use this mode, the
|
||||
transfer is normal ZMODEM. To stop zstd, set `dmc_xfer_options.no_zstd`
|
||||
(the Windows app: Transfer, ZMODEM Compression (zstd)).
|
||||
`dmc_xfer_zstd` tells if the file now goes with zstd. The zstd encoder
|
||||
and decoder are in `core/src/zstd_codec.c`. They are a C port of the
|
||||
codec of the D-Modem app (`Zstd.kt`), and they do not use libzstd.
|
||||
- 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).
|
||||
|
||||
@@ -37,6 +37,7 @@ BEGIN
|
||||
MENUITEM "&Receive File...", IDM_RECEIVE
|
||||
MENUITEM SEPARATOR
|
||||
MENUITEM "&Automatic Receive (ZMODEM, Kermit)", IDM_AUTORECV
|
||||
MENUITEM "ZMODEM Compression (&zstd)", IDM_XZSTD
|
||||
MENUITEM SEPARATOR
|
||||
MENUITEM "&Cancel Transfer", IDM_XCANCEL
|
||||
MENUITEM SEPARATOR
|
||||
|
||||
@@ -227,6 +227,10 @@ bool xfer_running();
|
||||
bool xfer_busy(XferLine *l);
|
||||
bool xfer_auto();
|
||||
void xfer_set_auto(bool on);
|
||||
/* ZMODEM with zstd (where the other side has it too: D-Modem's BBS, GUI
|
||||
and app) */
|
||||
bool xfer_zstd();
|
||||
void xfer_set_zstd(bool on);
|
||||
/* the transfer's dialog's keys (IsDialogMessage) */
|
||||
bool xfer_dialog_key(MSG *m);
|
||||
|
||||
|
||||
@@ -335,6 +335,9 @@ static LRESULT CALLBACK main_proc(HWND w, UINT m, WPARAM wp, LPARAM lp)
|
||||
case IDM_AUTORECV:
|
||||
xfer_set_auto(!xfer_auto());
|
||||
return 0;
|
||||
case IDM_XZSTD:
|
||||
xfer_set_zstd(!xfer_zstd());
|
||||
return 0;
|
||||
case IDM_XCANCEL:
|
||||
xfer_cancel();
|
||||
return 0;
|
||||
@@ -381,6 +384,8 @@ static LRESULT CALLBACK main_proc(HWND w, UINT m, WPARAM wp, LPARAM lp)
|
||||
EnableMenuItem(m, IDM_XCANCEL, MF_BYCOMMAND | (busy ? MF_ENABLED : MF_GRAYED));
|
||||
CheckMenuItem(m, IDM_AUTORECV, MF_BYCOMMAND |
|
||||
(xfer_auto() ? MF_CHECKED : MF_UNCHECKED));
|
||||
CheckMenuItem(m, IDM_XZSTD, MF_BYCOMMAND |
|
||||
(xfer_zstd() ? MF_CHECKED : MF_UNCHECKED));
|
||||
HWND v = view_now();
|
||||
UINT has_view = v ? MF_ENABLED : MF_GRAYED;
|
||||
EnableMenuItem(m, IDM_EDCOPY, MF_BYCOMMAND |
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
#define IDM_CAPSTOP 214
|
||||
#define IDM_CAPPRINT 215
|
||||
#define IDM_PRNPAUSE 216
|
||||
#define IDM_XZSTD 217
|
||||
|
||||
#define IDC_STATUS 300
|
||||
#define IDC_TABS 301
|
||||
|
||||
@@ -149,6 +149,16 @@ void xfer_set_auto(bool on)
|
||||
ini_set_int("auto", on ? 1 : 0);
|
||||
}
|
||||
|
||||
bool xfer_zstd()
|
||||
{
|
||||
return ini_int("zmodem_zstd", 1) != 0;
|
||||
}
|
||||
|
||||
void xfer_set_zstd(bool on)
|
||||
{
|
||||
ini_set_int("zmodem_zstd", on ? 1 : 0);
|
||||
}
|
||||
|
||||
bool xfer_running()
|
||||
{
|
||||
return cur != NULL;
|
||||
@@ -198,6 +208,12 @@ static void show_status(const dmc_xfer_status *st, bool force)
|
||||
char a[300], b[64], c[64];
|
||||
to_ansi(st->file[0] ? st->file : "(waiting for the other side)", a,
|
||||
sizeof(a));
|
||||
/* (a file that goes compressed: ZMODEM with zstd) */
|
||||
if (st->running && cur && dmc_xfer_zstd(cur)) {
|
||||
size_t al = strlen(a);
|
||||
_snprintf(a + al, sizeof(a) - al, " (zstd)");
|
||||
a[sizeof(a) - 1] = '\0';
|
||||
}
|
||||
set_text(IDC_X_FILE, a);
|
||||
if (st->files > last_files) {
|
||||
done_bytes += st->size > 0 ? st->size : st->bytes;
|
||||
@@ -451,6 +467,7 @@ static bool start_receive(XferLine *l, int protocol, const char *name, bool resu
|
||||
dmc_xfer_options o;
|
||||
memset(&o, 0, sizeof(o));
|
||||
o.resume = resume && protocol == DMC_XFER_ZMODEM;
|
||||
o.no_zstd = !xfer_zstd();
|
||||
return start(l, dmc_xfer_receive_opt(protocol, where, name, &o), protocol,
|
||||
false, 0, o.resume != 0);
|
||||
}
|
||||
@@ -799,6 +816,7 @@ void xfer_send_dialog(XferLine *l, int protocol)
|
||||
dmc_xfer_options o;
|
||||
memset(&o, 0, sizeof(o));
|
||||
o.resume = a.resume;
|
||||
o.no_zstd = !xfer_zstd();
|
||||
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,
|
||||
|
||||
Reference in new issue
Block a user