nish 0.23.0 → 0.24.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +51 -40
- package/docs/AI.md +26 -19
- package/llms.txt +1 -1
- package/package.json +5 -5
- package/runtime/core/arrays.d.ts +0 -2
- package/runtime/core/arrays.mjs +0 -2
- package/runtime/core/numbers.mjs +2 -3
- package/runtime/nish.d.ts +0 -4
- package/runtime/nish.mjs +2 -5
- package/runtime/runtime-net.c +1 -1
- package/runtime/runtime-term.c +2 -2
- package/runtime/runtime.c +1 -1
- package/scripts/gen-diagnostic-codes.mjs +2 -1
- package/std/README.md +20 -0
- package/std/compress/checksum.ts +178 -0
- package/std/compress/deflate.ts +2400 -0
- package/std/compress/gzip.ts +747 -0
- package/std/compress/zlib.ts +168 -0
- package/std/crypto/sha256.ts +2 -3
- package/std/crypto/x25519.ts +3 -4
- package/std/net/http-compress.ts +393 -0
- package/std/net/http1-server.ts +115 -14
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `nish/compress/checksum` — the two checksums the compressed formats carry:
|
|
3
|
+
* CRC-32 (ISO 3309, the polynomial gzip's trailer uses, RFC 1952 §8) and
|
|
4
|
+
* Adler-32 (zlib's trailer, RFC 1950 §9).
|
|
5
|
+
*
|
|
6
|
+
* import { Crc32 } from "nish/compress/checksum";
|
|
7
|
+
*
|
|
8
|
+
* const crc = new Crc32(); // the tables, once
|
|
9
|
+
* let c: u32 = crc.update(toU32(0), body, 0, 100);
|
|
10
|
+
* c = crc.update(c, body, 100, toI32(body.length) - 100); // the same as one call
|
|
11
|
+
*
|
|
12
|
+
* A module has no global state, so CRC-32's tables live in a `Crc32` object:
|
|
13
|
+
* build one and keep it, because building it is 2,048 table entries (a few
|
|
14
|
+
* microseconds) and an update of a 5 KB body is about as long again. `crc32`
|
|
15
|
+
* is the one-off convenience that builds a table for one call.
|
|
16
|
+
*
|
|
17
|
+
* CRC-32 is slicing-by-8: eight 256-entry tables, eight bytes per step with
|
|
18
|
+
* one table read per byte and no dependency between the reads of a step
|
|
19
|
+
* (Kounavis and Berry, "A Systematic Approach to Building High Performance
|
|
20
|
+
* Software-based CRC Generators", 2005). Adler-32 defers its two modulo
|
|
21
|
+
* reductions to once every 5,552 bytes, the most that cannot overflow 32 bits
|
|
22
|
+
* (RFC 1950 §9 and zlib's `NMAX`).
|
|
23
|
+
*
|
|
24
|
+
* Neither is a cryptographic hash: both detect accidental corruption only.
|
|
25
|
+
* Written from the specifications, not ported from another implementation.
|
|
26
|
+
*
|
|
27
|
+
* The table reads in `update` are `nish:unsafe`'s `uncheckedGet`: every index
|
|
28
|
+
* is a byte plus a multiple of 256 below 2,048, and the table is built here at
|
|
29
|
+
* exactly that length and never replaced, which is the whole proof. The input
|
|
30
|
+
* window is checked once, before the loop.
|
|
31
|
+
*/
|
|
32
|
+
import { uncheckedGet, uncheckedSet } from "nish:unsafe"
|
|
33
|
+
|
|
34
|
+
/** The reflected CRC-32 polynomial, x^32 + x^26 + ... + 1 (RFC 1952 §8). */
|
|
35
|
+
const CRC32_POLY: i64 = 0xedb88320
|
|
36
|
+
|
|
37
|
+
/** The entries of the eight slicing tables, 256 each. */
|
|
38
|
+
const CRC32_TABLE_SIZE: i32 = 2048
|
|
39
|
+
|
|
40
|
+
/** zlib's `NMAX`: the most bytes Adler-32's sums take before they must be reduced. */
|
|
41
|
+
const ADLER32_NMAX: i32 = 5552
|
|
42
|
+
|
|
43
|
+
/** The Adler-32 modulus, the largest prime below 2^16 (RFC 1950 §9). */
|
|
44
|
+
const ADLER32_BASE: i32 = 65521
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The little-endian 64-bit word at `buf[p .. p + 8)`, which the caller has
|
|
48
|
+
* checked. `p`'s range makes each `p + k` an `add nsw`, which lets LLVM load
|
|
49
|
+
* the word at once.
|
|
50
|
+
*/
|
|
51
|
+
const checksumLoad64 = (buf: u8[], p: integer<0, 2147483639>): u64 =>
|
|
52
|
+
toU64(uncheckedGet(buf, p)) |
|
|
53
|
+
(toU64(uncheckedGet(buf, p + 1)) << toU64(8)) |
|
|
54
|
+
(toU64(uncheckedGet(buf, p + 2)) << toU64(16)) |
|
|
55
|
+
(toU64(uncheckedGet(buf, p + 3)) << toU64(24)) |
|
|
56
|
+
(toU64(uncheckedGet(buf, p + 4)) << toU64(32)) |
|
|
57
|
+
(toU64(uncheckedGet(buf, p + 5)) << toU64(40)) |
|
|
58
|
+
(toU64(uncheckedGet(buf, p + 6)) << toU64(48)) |
|
|
59
|
+
(toU64(uncheckedGet(buf, p + 7)) << toU64(56))
|
|
60
|
+
|
|
61
|
+
/** Panics unless `buf[off .. off + len)` lies inside `buf`. */
|
|
62
|
+
const checksumWindow = (what: string, buf: u8[], off: i32, len: i32): void => {
|
|
63
|
+
if (off < 0 || len < 0 || off > toI32(buf.length) - len) {
|
|
64
|
+
panic(`${what}: the window lies outside its array`)
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* CRC-32's lookup tables, built once and read by every `update`. The object
|
|
70
|
+
* holds nothing else, so one may be shared by every compressor and reader in
|
|
71
|
+
* a thread, and by threads that only read it.
|
|
72
|
+
*/
|
|
73
|
+
export class Crc32 {
|
|
74
|
+
/** `table[k * 256 + b]`: the CRC of byte `b` followed by `k` zero bytes. */
|
|
75
|
+
readonly table: u32[]
|
|
76
|
+
|
|
77
|
+
/** Builds the eight tables. */
|
|
78
|
+
constructor() {
|
|
79
|
+
const t: u32[] = new Array<u32>(CRC32_TABLE_SIZE)
|
|
80
|
+
for (let i: i32 = 0; i < 256; i += 1) {
|
|
81
|
+
let c: u32 = toU32(i)
|
|
82
|
+
for (let k: i32 = 0; k < 8; k += 1) {
|
|
83
|
+
c = (c & 1) !== 0 ? toU32(CRC32_POLY) ^ (c >>> 1) : c >>> 1
|
|
84
|
+
}
|
|
85
|
+
uncheckedSet(t, i, c)
|
|
86
|
+
}
|
|
87
|
+
for (let i: i32 = 256; i < CRC32_TABLE_SIZE; i += 1) {
|
|
88
|
+
const prev: u32 = uncheckedGet(t, i - 256)
|
|
89
|
+
uncheckedSet(t, i, (prev >>> 8) ^ uncheckedGet(t, toI32(prev & 255)))
|
|
90
|
+
}
|
|
91
|
+
this.table = t
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* The CRC-32 of the bytes `crc` was the CRC of, followed by
|
|
96
|
+
* `buf[off .. off + len)`. Start a new checksum from 0. A window outside
|
|
97
|
+
* `buf` panics.
|
|
98
|
+
*/
|
|
99
|
+
update(crc: u32, buf: u8[], off: i32, len: i32): u32 {
|
|
100
|
+
checksumWindow("Crc32.update", buf, off, len)
|
|
101
|
+
const t: u32[] = this.table
|
|
102
|
+
if (toI32(t.length) !== CRC32_TABLE_SIZE) {
|
|
103
|
+
panic("Crc32.update: the table is not its size")
|
|
104
|
+
}
|
|
105
|
+
let c: u32 = ~crc
|
|
106
|
+
let p: i32 = off
|
|
107
|
+
const end: i32 = off + len
|
|
108
|
+
const whole: i32 = off + (len - (len & 7))
|
|
109
|
+
while (p < whole && p >= 0 && p <= 2147483639) {
|
|
110
|
+
const w: u64 = checksumLoad64(buf, p)
|
|
111
|
+
const lo: u32 = c ^ toU32(w)
|
|
112
|
+
const hi: u32 = toU32(w >> toU64(32))
|
|
113
|
+
c =
|
|
114
|
+
uncheckedGet(t, 1792 + toI32(lo & 255)) ^
|
|
115
|
+
uncheckedGet(t, 1536 + toI32((lo >>> 8) & 255)) ^
|
|
116
|
+
uncheckedGet(t, 1280 + toI32((lo >>> 16) & 255)) ^
|
|
117
|
+
uncheckedGet(t, 1024 + toI32(lo >>> 24)) ^
|
|
118
|
+
uncheckedGet(t, 768 + toI32(hi & 255)) ^
|
|
119
|
+
uncheckedGet(t, 512 + toI32((hi >>> 8) & 255)) ^
|
|
120
|
+
uncheckedGet(t, 256 + toI32((hi >>> 16) & 255)) ^
|
|
121
|
+
uncheckedGet(t, toI32(hi >>> 24))
|
|
122
|
+
p += 8
|
|
123
|
+
}
|
|
124
|
+
while (p < end) {
|
|
125
|
+
c = uncheckedGet(t, toI32((c ^ toU32(uncheckedGet(buf, p))) & 255)) ^ (c >>> 8)
|
|
126
|
+
p += 1
|
|
127
|
+
}
|
|
128
|
+
return ~c
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* The CRC-32 of `buf[off .. off + len)`. It builds a `Crc32` for the one
|
|
134
|
+
* call, which costs about as much as checksumming 5 KB: keep a `Crc32` for
|
|
135
|
+
* anything repeated. A window outside `buf` panics.
|
|
136
|
+
*/
|
|
137
|
+
export const crc32 = (buf: u8[], off: i32, len: i32): u32 => new Crc32().update(toU32(0), buf, off, len)
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* The Adler-32 of the bytes `adler` was the Adler-32 of, followed by
|
|
141
|
+
* `buf[off .. off + len)` (RFC 1950 §9). Start a new checksum from 1. A
|
|
142
|
+
* window outside `buf` panics.
|
|
143
|
+
*/
|
|
144
|
+
export const adler32Update = (adler: u32, buf: u8[], off: i32, len: i32): u32 => {
|
|
145
|
+
checksumWindow("adler32Update", buf, off, len)
|
|
146
|
+
let s1: u32 = adler & 0xffff
|
|
147
|
+
let s2: u32 = adler >>> 16
|
|
148
|
+
let p: i32 = off
|
|
149
|
+
const end: i32 = off + len
|
|
150
|
+
while (p < end) {
|
|
151
|
+
let stop: i32 = p + ADLER32_NMAX
|
|
152
|
+
if (stop > end) {
|
|
153
|
+
stop = end
|
|
154
|
+
}
|
|
155
|
+
while (p + 4 <= stop) {
|
|
156
|
+
s1 += toU32(uncheckedGet(buf, p))
|
|
157
|
+
s2 += s1
|
|
158
|
+
s1 += toU32(uncheckedGet(buf, p + 1))
|
|
159
|
+
s2 += s1
|
|
160
|
+
s1 += toU32(uncheckedGet(buf, p + 2))
|
|
161
|
+
s2 += s1
|
|
162
|
+
s1 += toU32(uncheckedGet(buf, p + 3))
|
|
163
|
+
s2 += s1
|
|
164
|
+
p += 4
|
|
165
|
+
}
|
|
166
|
+
while (p < stop) {
|
|
167
|
+
s1 += toU32(uncheckedGet(buf, p))
|
|
168
|
+
s2 += s1
|
|
169
|
+
p += 1
|
|
170
|
+
}
|
|
171
|
+
s1 = s1 % toU32(ADLER32_BASE)
|
|
172
|
+
s2 = s2 % toU32(ADLER32_BASE)
|
|
173
|
+
}
|
|
174
|
+
return (s2 << 16) | s1
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** The Adler-32 of `buf[off .. off + len)`. A window outside `buf` panics. */
|
|
178
|
+
export const adler32 = (buf: u8[], off: i32, len: i32): u32 => adler32Update(toU32(1), buf, off, len)
|