@amritk/nish-x86_64-linux 0.42.0 → 0.44.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.
@@ -15,7 +15,9 @@
15
15
  * const digest: u8[] = sha1(message); // 20 bytes
16
16
  *
17
17
  * One shot only, because a handshake key is a few dozen bytes: there is no
18
- * streaming hasher to keep in step with `Sha256`'s.
18
+ * streaming hasher to keep in step with `Sha256`'s. A caller that hashes again
19
+ * and again keeps a `Sha1` and calls `digestInto`, which writes into its
20
+ * buffer and allocates nothing.
19
21
  *
20
22
  * Every word is a `u32`, whose arithmetic wraps, so the additions modulo 2^32
21
23
  * are plain `+`. The only branches are on lengths, loop counters and the round
@@ -39,8 +41,8 @@ const sha1LoadWord = (buf: u8[], at: i32): u32 =>
39
41
 
40
42
  /**
41
43
  * Folds the 64-byte block at `src[at .. at + 64)` into `h`: FIPS 180-4
42
- * §6.1.2, steps 1 to 4. `w` is the 80-word schedule, allocated once per call
43
- * to `sha1` rather than once per block.
44
+ * §6.1.2, steps 1 to 4. `w` is the 80-word schedule, held by the `Sha1`
45
+ * hasher rather than allocated once per block.
44
46
  */
45
47
  const sha1Compress = (h: u32[], w: u32[], src: u8[], at: i32): void => {
46
48
  // Written as the condition the body runs under, the shape that proves the
@@ -95,7 +97,98 @@ const sha1Compress = (h: u32[], w: u32[], src: u8[], at: i32): void => {
95
97
  }
96
98
 
97
99
  /**
98
- * The SHA-1 digest of all of `data`, as a fresh 20-byte array.
100
+ * A SHA-1 hasher that keeps its working state, so that a caller hashing again
101
+ * and again (a server answering WebSocket handshakes) allocates once, when it
102
+ * builds one, and never after: `digestInto` writes into the caller's buffer.
103
+ * It holds no secret, so nothing in it is wiped.
104
+ *
105
+ * const hasher = new Sha1();
106
+ * hasher.digestInto(message, 0, message.length, digest, 0); // digest[0 .. 20)
107
+ */
108
+ export class Sha1 {
109
+ /** The chaining value H of FIPS 180-4 §6.1.2, five words. */
110
+ h: u32[]
111
+ /** The 80-word message schedule, reused for every block. */
112
+ w: u32[]
113
+ /** The last one or two blocks: the message's tail and its padding (§5.1.1). */
114
+ last: u8[]
115
+
116
+ /** A hasher with its buffers allocated, ready for `digestInto`. */
117
+ constructor() {
118
+ this.h = new Array<u32>(5)
119
+ this.w = new Array<u32>(80)
120
+ this.last = new Array<u8>(2 * SHA1_BLOCK)
121
+ }
122
+
123
+ /**
124
+ * Writes the SHA-1 digest of `data[start .. start + length)` into
125
+ * `out[at .. at + 20)`, allocating nothing. A range outside `data` or `out`
126
+ * panics, as does a message longer than 2^31 - 1 bytes.
127
+ *
128
+ * @param data The buffer holding the message; it is read, not changed.
129
+ * @param start Where in `data` the message starts.
130
+ * @param length How many bytes of `data` the message is.
131
+ * @param out The buffer the 20-byte digest is written into.
132
+ * @param at Where in `out` the digest starts.
133
+ */
134
+ digestInto(data: u8[], start: i32, length: i32, out: u8[], at: i32): void {
135
+ if (data.length > 2147483647 || out.length > 2147483647) {
136
+ panic("sha1: a buffer longer than 2^31 - 1 bytes")
137
+ }
138
+ if (start < 0 || length < 0 || start > toI32(data.length) - length) {
139
+ panic("sha1: a message outside its buffer")
140
+ }
141
+ if (at < 0 || at > toI32(out.length) - SHA1_SIZE) {
142
+ panic("sha1: a digest outside its buffer")
143
+ }
144
+ const h: u32[] = this.h
145
+ const w: u32[] = this.w
146
+ const last: u8[] = this.last
147
+ if (toI32(h.length) < 5) {
148
+ panic("sha1: a hasher without its state")
149
+ }
150
+ // H^(0) of FIPS 180-4 §5.3.1.
151
+ h[0] = 0x67452301
152
+ h[1] = 0xefcdab89
153
+ h[2] = 0x98badcfe
154
+ h[3] = 0x10325476
155
+ h[4] = 0xc3d2e1f0
156
+ // Whole blocks straight from `data`, then the tail and its padding (§5.1.1)
157
+ // in one or two blocks of `last`.
158
+ const whole: i32 = length - (length % SHA1_BLOCK)
159
+ for (let off: i32 = 0; off < whole; off += SHA1_BLOCK) {
160
+ sha1Compress(h, w, data, start + off)
161
+ }
162
+ const tail: i32 = length - whole
163
+ const lastLength: i32 = tail < SHA1_BLOCK - 8 ? SHA1_BLOCK : 2 * SHA1_BLOCK
164
+ const room: i32 = toI32(last.length)
165
+ if (lastLength > room) {
166
+ panic("sha1: a hasher without its state")
167
+ }
168
+ for (let k: i32 = 0; k < lastLength && k < room; k += 1) {
169
+ last[k] = k < tail ? data[start + whole + k] : 0
170
+ }
171
+ last[tail] = 0x80
172
+ const bits: u64 = toU64(toI64(length)) << 3
173
+ for (let i: i32 = 0; i < 8; i += 1) {
174
+ last[lastLength - 1 - i] = toU8(bits >>> toU64(8 * i))
175
+ }
176
+ for (let off: i32 = 0; off < lastLength; off += SHA1_BLOCK) {
177
+ sha1Compress(h, w, last, off)
178
+ }
179
+ for (let i: i32 = 0; i < 5; i += 1) {
180
+ const word: u32 = h[i]
181
+ out[at + 4 * i] = toU8(word >>> 24)
182
+ out[at + 4 * i + 1] = toU8(word >>> 16)
183
+ out[at + 4 * i + 2] = toU8(word >>> 8)
184
+ out[at + 4 * i + 3] = toU8(word)
185
+ }
186
+ }
187
+ }
188
+
189
+ /**
190
+ * The SHA-1 digest of all of `data`, as a fresh 20-byte array: `Sha1`'s
191
+ * `digestInto` over the whole of it, with a hasher of its own.
99
192
  *
100
193
  * An array longer than 2^31 - 1 bytes panics, for the reason `sha256` gives:
101
194
  * under `--number-mode f64`, `toI32` of a longer length saturates, and the
@@ -108,43 +201,8 @@ export const sha1 = (data: u8[]): u8[] => {
108
201
  if (data.length > 2147483647) {
109
202
  panic("sha1: a message longer than 2^31 - 1 bytes")
110
203
  }
111
- const n: i32 = toI32(data.length)
112
- // H^(0) of FIPS 180-4 §5.3.1.
113
- const h: u32[] = new Array<u32>(5)
114
- h[0] = 0x67452301
115
- h[1] = 0xefcdab89
116
- h[2] = 0x98badcfe
117
- h[3] = 0x10325476
118
- h[4] = 0xc3d2e1f0
119
- const w: u32[] = new Array<u32>(80)
120
- // Whole blocks straight from `data`, then the tail and its padding (§5.1.1)
121
- // in one or two blocks of their own.
122
- const whole: i32 = n - (n % SHA1_BLOCK)
123
- for (let at: i32 = 0; at < whole; at += SHA1_BLOCK) {
124
- sha1Compress(h, w, data, at)
125
- }
126
- const tail: i32 = n - whole
127
- const tailBlocks: i32 = tail < SHA1_BLOCK - 8 ? 1 : 2
128
- const last: u8[] = new Array<u8>(tailBlocks * SHA1_BLOCK)
129
- const lastLength: i32 = toI32(last.length)
130
- for (let k: i32 = 0; k < tail && k < lastLength; k += 1) {
131
- last[k] = data[whole + k]
132
- }
133
- last[tail] = 0x80
134
- const bits: u64 = toU64(toI64(n)) << 3
135
- for (let i: i32 = 0; i < 8; i += 1) {
136
- last[lastLength - 1 - i] = toU8(bits >>> toU64(8 * i))
137
- }
138
- for (let at: i32 = 0; at < lastLength; at += SHA1_BLOCK) {
139
- sha1Compress(h, w, last, at)
140
- }
141
204
  const out: u8[] = new Array<u8>(SHA1_SIZE)
142
- for (let i: i32 = 0; i < 5; i += 1) {
143
- const word: u32 = h[i]
144
- out[4 * i] = toU8(word >>> 24)
145
- out[4 * i + 1] = toU8(word >>> 16)
146
- out[4 * i + 2] = toU8(word >>> 8)
147
- out[4 * i + 3] = toU8(word)
148
- }
205
+ const zero: i32 = 0
206
+ new Sha1().digestInto(data, zero, toI32(data.length), out, zero)
149
207
  return out
150
208
  }
package/std/host/ring.ts CHANGED
@@ -10,7 +10,7 @@
10
10
  *
11
11
  * const INPUT = new Int32Array(2 + 64); // head, tail, then 64 slots
12
12
  *
13
- * export function tick(): void {
13
+ * export const tick = (): void => {
14
14
  * while (ringSize(INPUT) > 0) {
15
15
  * steer(ringShift(INPUT));
16
16
  * }
package/std/host/text.ts CHANGED
@@ -13,7 +13,7 @@
13
13
  * export const OUTBOX = new Array<u8>(256);
14
14
  *
15
15
  * // The page: n = new TextEncoder().encodeInto(text, views.INBOX).written
16
- * export function echo(n: i32): i32 {
16
+ * export const echo = (n: i32): i32 => {
17
17
  * const text = textRead(INBOX, n);
18
18
  * if (text === null) return -1;
19
19
  * return textWriteInt(OUTBOX, textWrite(OUTBOX, 0, text), toI64(n));
@@ -770,7 +770,7 @@ export class HttpHandler {
770
770
  this.open[s] &&
771
771
  this.server.holds(s) &&
772
772
  this.current[s] === -1 &&
773
- !this.server.connection(s).wantsWrite()
773
+ (this.server.resting(s) || !this.server.connection(s).wantsWrite())
774
774
  ) {
775
775
  this.closeSlot(s)
776
776
  } else if (this.open[s] && this.server.holds(s)) {
@@ -1271,11 +1271,10 @@ export class HttpServer {
1271
1271
  if (h1 !== null) {
1272
1272
  for (let s: i32 = 0; s < toI32(this.h1Open.length); s++) {
1273
1273
  if (this.h1Open[s] && h1.holds(s)) {
1274
- const conn: Http1Connection = h1.connection(s)
1275
- if (this.h1Current[s] === -1 && !conn.wantsWrite()) {
1274
+ if (this.h1Current[s] === -1 && (h1.resting(s) || !h1.connection(s).wantsWrite())) {
1276
1275
  this.h1Close(h1, s)
1277
1276
  } else {
1278
- conn.closeAfter = true
1277
+ h1.connection(s).closeAfter = true
1279
1278
  }
1280
1279
  }
1281
1280
  }