From 256bd814d307b3346c09da700ad3bca1d9bfebff Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 20 Nov 2026 22:41:47 +0000 Subject: [PATCH] Document every function of the zstd code (doc_check) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01B82eRR1WNeCEtXUcjE8RgY --- .../kotlin/org/dmodem/app/transfer/ZModem.kt | 1 + .../kotlin/org/dmodem/app/transfer/Zstd.kt | 37 +++++++++++++++++++ .../org/dmodem/app/transfer/ZstdTest.kt | 11 ++++++ .../dmodem/app/transfer/ZstdReferenceTest.kt | 2 + 4 files changed, 51 insertions(+) diff --git a/shared/src/commonMain/kotlin/org/dmodem/app/transfer/ZModem.kt b/shared/src/commonMain/kotlin/org/dmodem/app/transfer/ZModem.kt index b0d9da2..113eca5 100644 --- a/shared/src/commonMain/kotlin/org/dmodem/app/transfer/ZModem.kt +++ b/shared/src/commonMain/kotlin/org/dmodem/app/transfer/ZModem.kt @@ -495,6 +495,7 @@ class ZModem( private fun zstdFrameLen(b: ByteArray, at: Int): Int { val left = b.size - at if (left < 5) return 0 + // the byte at i of the frame, unsigned fun u(i: Int) = b[at + i].toInt() and 0xff if ((u(0) or (u(1) shl 8) or (u(2) shl 16) or (u(3) shl 24)) != Zstd.MAGIC) throw Zstd.FormatException("not a zstd frame") val fhd = u(4) diff --git a/shared/src/commonMain/kotlin/org/dmodem/app/transfer/Zstd.kt b/shared/src/commonMain/kotlin/org/dmodem/app/transfer/Zstd.kt index c60c0ba..817a40d 100644 --- a/shared/src/commonMain/kotlin/org/dmodem/app/transfer/Zstd.kt +++ b/shared/src/commonMain/kotlin/org/dmodem/app/transfer/Zstd.kt @@ -22,6 +22,7 @@ object Zstd { /** a frame that is not Zstandard, or that breaks one of [decode]'s limits */ class FormatException(msg: String) : IOException(msg) + // the index of v's highest set bit (-1 for 0) private fun hbit(v: Int): Int = 31 - v.countLeadingZeroBits() private val LL_BASE = intArrayOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, @@ -40,6 +41,7 @@ object Zstd { private val OF_DEF = intArrayOf(1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1) + // b[i] unsigned private fun u8(b: ByteArray, i: Int) = b[i].toInt() and 0xff // ------------------------------------------------------------------ @@ -49,6 +51,7 @@ object Zstd { private class FwdBits(val p: ByteArray, val start: Int, val n: Int) { var pos = 0L var bad = false + // the next nb bits, not taken (0 past the end) fun peek(nb: Int): Int { var v = 0 for (k in 0 until nb) { @@ -57,23 +60,27 @@ object Zstd { } return v } + // the next nb bits, taken; bad set past the end fun read(nb: Int): Int { val v = peek(nb) pos += nb if (pos > 8L * n) bad = true return v } + // the bytes read so far, a partial last one counted fun bytes(): Int = ((pos + 7) / 8).toInt() } /** a backward bit stream (Huffman and FSE coded data): from under the last byte's marker toward the start */ private class BackBits(val p: ByteArray, val start: Int, val n: Int) { var off = 0L + // at the last byte's marker bit; false if there is none fun init(): Boolean { if (n <= 0 || p[start + n - 1].toInt() == 0) return false off = 8L * (n - 1) + hbit(u8(p, start + n - 1)) return true } + // the next nb bits, from the high end down (0 past the start) fun read(nb: Int): Long { var v = 0L off -= nb @@ -83,6 +90,7 @@ object Zstd { } return v } + // whether more bits were read than there are fun over() = off < 0 } @@ -91,6 +99,7 @@ object Zstd { val b = Bytes() var acc = 0 var n = 0 + // the low nb bits of v fun put(v: Long, nb: Int) { for (k in 0 until nb) { acc = acc or (((v shr k) and 1L).toInt() shl n) @@ -101,7 +110,9 @@ object Zstd { } } } + // the low nb bits of v (an Int) fun put(v: Int, nb: Int) = put(v.toLong() and 0xffffffffL, nb) + // the end marker bit, and zeros to the byte's end fun close() { put(1, 1) if (n != 0) put(0, 8 - n) @@ -288,6 +299,7 @@ object Zstd { return inb.off + h.maxBits == 0L } + // the code of length v: the last base not over it private fun lenCode(v: Int, base: IntArray): Int { var c = 0 while (c + 1 < base.size && base[c + 1] <= v) c++ @@ -323,7 +335,9 @@ object Zstd { for (i in 0 until (1 shl al)) lists[t.sym[i]].add(i) states = Array(c.size) { lists[it].toIntArray() } } + // a symbol's first state, where the stream starts fun start(s: Int) = states[s][0] + // the state after symbol s from st: its low bits written fun step(st: Int, s: Int, w: BitW): Int { val c = if (cnt[s] < 0) 1 else cnt[s] val x = st + (1 shl al) @@ -397,6 +411,7 @@ object Zstd { return code } + // lit[from..to) Huffman coded, backward, as one stream private fun hufEncode(lit: ByteArray, from: Int, to: Int, bits: IntArray, code: IntArray): ByteArray { val w = BitW() for (k in to - 1 downTo from) { @@ -407,6 +422,7 @@ object Zstd { return w.b.toByteArray() } + // a literals section header: its type, sizes and streams private fun litHeader(o: Bytes, type: Int, regen: Int, comp: Int, oneStream: Boolean) { if (type < 2) { when { @@ -445,6 +461,7 @@ object Zstd { return o.toByteArray() } + // counts f made an FSE table's of accuracy log al: summing to 1 shl al, none of a used symbol 0 private fun fseNormalize(f: IntArray, nsym: Int, al: Int): IntArray { var total = 0L for (s in 0 until nsym) total += f[s] @@ -471,6 +488,7 @@ object Zstd { return c } + // an FSE table description of counts c (4.1.1), into o private fun fseDescWrite(c: IntArray, al: Int, o: Bytes) { val w = BitW() w.put(al - 5, 4) @@ -501,6 +519,7 @@ object Zstd { o.write(w.b.toByteArray()) } + // the bits sym costs with the table of counts cnt; -1 if one can't be coded private fun fseCost(cnt: IntArray, al: Int, sym: IntArray): Double { var bits = 0.0 for (x in sym) { @@ -528,9 +547,11 @@ object Zstd { var encBits: IntArray? = null val out = Bytes() + // the hash of the 3 bytes at i (14 bits) fun hash3(i: Int): Int = (((u8(hist, i) shl 16) or (u8(hist, i + 1) shl 8) or u8(hist, i + 2)) * -1640531535) ushr 18 + // position i put in the hash chain fun insert(i: Int) { if (i + 2 >= n) return val h = hash3(i) @@ -540,9 +561,11 @@ object Zstd { var matchOff = 0 + // the longest match at i before end (matchOff its offset); 0 if under 3 fun bestMatch(i: Int, end: Int): Int { val lim = minOf(end - i, 65536) var best = 0 + // the match length at i with offset d fun lenAt(d: Int): Int { var l = 0 while (l < lim && hist[i - d + l] == hist[i + l]) l++ @@ -573,6 +596,7 @@ object Zstd { return if (best >= 3) best else 0 } + // the whole frame: header, blocks and checksum fun frame(): ByteArray { // the header: single segment, the checksum, the content size val fcsFlag = when { @@ -599,12 +623,14 @@ object Zstd { return out.toByteArray() } + // a block header and its body fun putBlock(body: ByteArray, type: Int, size: Int, last: Boolean) { val h = (if (last) 1 else 0) or (type shl 1) or (size shl 3) out.write(h and 0xff); out.write((h shr 8) and 0xff); out.write((h shr 16) and 0xff) out.write(body) } + // hist[start..end) as one block: compressed, RLE or raw, whichever is smallest fun block(start: Int, end: Int, last: Boolean) { val nb = end - start val repSaved = rep.copyOf() @@ -614,6 +640,7 @@ object Zstd { val lit = Bytes() var i = start var litAt = start + // the hash chain up to upto fun catchUp(upto: Int) { while (ins < upto) insert(ins++) } if (ins < start) ins = start while (i + 3 <= end) { @@ -739,6 +766,7 @@ object Zstd { val fo = enc[1]!! val fm = enc[2]!! val w = BitW() + // sequence k's extra bits: literal length, match length, offset fun extras(k: Int) { w.put(seqLl[k] - LL_BASE[llc[k]], LL_BITS[llc[k]]) w.put(seqMl[k] - ML_BASE[mlc[k]], ML_BITS[mlc[k]]) @@ -801,14 +829,17 @@ object Zstd { val have = BooleanArray(3) var window = 0 + // refused, with why fun bad(why: String): Nothing = throw FormatException("zstd: $why") + // one byte of content fun emit(c: Byte) { if (hn >= maxSize) bad("more than $maxSize bytes") if (hn == hist.size) hist = hist.copyOf(minOf(maxSize, hist.size * 2)) hist[hn++] = c } + // the frame decoded: its content fun run(): ByteArray { val n = p.size if (n < 6) bad("too short") @@ -872,6 +903,7 @@ object Zstd { return hist.copyOf(hn) } + // the literals section at p[at..at+n) into lit: its bytes fun literals(at: Int, n: Int, lit: Bytes): Int { if (n < 1) bad("no literals") val b0 = u8(p, at) @@ -937,6 +969,7 @@ object Zstd { return hs + comp } + // the sequences section at p[start..start+n), with lit, into the content fun sequences(start: Int, n: Int, lit: ByteArray) { if (n < 1) bad("no sequences") var at = start @@ -1016,20 +1049,24 @@ object XxHash64 { private const val P4 = -8796714831421723037L // 0x85EBCA77C2B2AE63 private const val P5 = 2870177450012600261L // 0x27D4EB2F165667C5 + // 8 bytes at i, little-endian private fun le64(b: ByteArray, i: Int): Long { var v = 0L for (k in 0 until 8) v = v or ((b[i + k].toLong() and 0xff) shl (8 * k)) return v } + // 4 bytes at i, little-endian private fun le32(b: ByteArray, i: Int): Long { var v = 0L for (k in 0 until 4) v = v or ((b[i + k].toLong() and 0xff) shl (8 * k)) return v } + // one XXH64 round private fun round(acc: Long, input: Long): Long = (acc + input * P2).rotateLeft(31) * P1 + // an accumulator merged into the hash private fun merge(acc: Long, v: Long): Long = (acc xor round(0, v)) * P1 + P4 /** XXH64 of b[off..off+len) */ diff --git a/shared/src/commonTest/kotlin/org/dmodem/app/transfer/ZstdTest.kt b/shared/src/commonTest/kotlin/org/dmodem/app/transfer/ZstdTest.kt index 428f9e2..9192862 100644 --- a/shared/src/commonTest/kotlin/org/dmodem/app/transfer/ZstdTest.kt +++ b/shared/src/commonTest/kotlin/org/dmodem/app/transfer/ZstdTest.kt @@ -35,6 +35,7 @@ class ZstdTest { return b.toByteArray().copyOf(n) } + // XXH64's known values @Test fun xxHash64() { assertEquals(0xEF46DB3751D8E999uL.toLong(), XxHash64.hash(ByteArray(0), 0, 0)) @@ -59,6 +60,7 @@ class ZstdTest { assertFailsWith { Zstd.decode(frame, 100) } } + // there and back: empty, short, random, text, around the header's size limits @Test fun roundTrip() { val r = Random(3) @@ -82,8 +84,11 @@ class ZstdTest { private class Memory : Incoming { val files = LinkedHashMap() var had: ByteArray? = null + // a new file, in memory override fun create(name: String, size: Long): Pair = Bytes().let { b -> files[name] = b; b to name } + // a file dropped override fun abandon(name: String) { files.remove(name) } + // the part in had, if any, to continue override fun resume(name: String, size: Long): Resumed? { val h = had ?: return null val b = Bytes().also { it.write(h) } @@ -92,9 +97,11 @@ class ZstdTest { } } + // data, offered as the file name private fun outgoing(name: String, data: ByteArray) = Outgoing(name, data.size.toLong(), 1_700_000_000) { object : Source { var at = 0 + // up to len bytes of data override fun read(b: ByteArray, off: Int, len: Int): Int { if (at >= data.size) return -1 val n = minOf(len, data.size - at) @@ -125,6 +132,7 @@ class ZstdTest { r.await() } + // files of text, compressed: whole, and less than half on the line @Test fun zmodemZstd() { val m = Memory() @@ -135,6 +143,7 @@ class ZstdTest { assertTrue(sent * 2 < total, "$sent bytes on the line for $total") } + // plain where the receiver doesn't take zstd, or the file is a .zip @Test fun zmodemZstdOff() { val d = text(100_000, 6) @@ -146,6 +155,7 @@ class ZstdTest { } } + // whole on a line that damages bytes @Test fun zmodemZstdNoisy() { val m = Memory() @@ -154,6 +164,7 @@ class ZstdTest { assertContentEquals(d, m.files.getValue("noisy.txt").toByteArray()) } + // a part continued, with and without zstd @Test fun zmodemResume() { for (name in listOf("resume.txt", "resume.zip")) { diff --git a/shared/src/jvmTest/kotlin/org/dmodem/app/transfer/ZstdReferenceTest.kt b/shared/src/jvmTest/kotlin/org/dmodem/app/transfer/ZstdReferenceTest.kt index 09da82f..f616ac9 100644 --- a/shared/src/jvmTest/kotlin/org/dmodem/app/transfer/ZstdReferenceTest.kt +++ b/shared/src/jvmTest/kotlin/org/dmodem/app/transfer/ZstdReferenceTest.kt @@ -12,6 +12,7 @@ import kotlin.test.assertContentEquals * installed): what it makes we decode, what we make it decodes. */ class ZstdReferenceTest { + // the zstd command's output for input; null if it isn't there or fails private fun zstd(vararg args: String, input: ByteArray): ByteArray? { val p = try { ProcessBuilder(listOf("zstd") + args).redirectError(ProcessBuilder.Redirect.DISCARD).start() @@ -24,6 +25,7 @@ class ZstdReferenceTest { return if (p.waitFor() == 0) result else null } + // its frames we decode, ours it decodes @Test fun bothWays() { val r = Random(11)