Files
gui/core/src/zstd_codec.h
T
Claude ce1fa43f5c 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
2026-11-20 22:38:20 +00:00

47 lines
1.9 KiB
C

/*
* 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