@replikativ/zstd 0.0.0-stage → 0.1.1
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/LICENSE +22 -0
- package/README.md +92 -2
- package/package.json +44 -4
- package/zstd.d.ts +20 -0
- package/zstd.mjs +762 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Arjun Barrett
|
|
4
|
+
Copyright (c) 2026 replikativ contributors
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,93 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @replikativ/zstd
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Zstandard decompression in plain JavaScript that verifies frame checksums. It is
|
|
4
|
+
one ES module with no dependencies, about 14 KB minified (6 KB gzipped). It needs
|
|
5
|
+
only typed arrays, so the same code runs in browsers and server runtimes. It is
|
|
6
|
+
tested on V8 (Node), JavaScriptCore (Bun) and SpiderMonkey (gjs).
|
|
7
|
+
|
|
8
|
+
```js
|
|
9
|
+
import { decompress } from '@replikativ/zstd';
|
|
10
|
+
|
|
11
|
+
const bytes = decompress(compressed); // Uint8Array in, Uint8Array out
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
It started as [fzstd](https://github.com/101arrowz/fzstd) and rewrites its
|
|
15
|
+
decoding loops. Compared with fzstd:
|
|
16
|
+
|
|
17
|
+
- **Checksums are verified.** A frame that carries an XXH64 checksum is checked,
|
|
18
|
+
so corrupted data raises an error instead of decoding to wrong bytes. fzstd skips
|
|
19
|
+
the check.
|
|
20
|
+
- **2 to 2.4 times faster** on V8, JavaScriptCore and SpiderMonkey.
|
|
21
|
+
- **Errors are explicit.** Invalid input, including truncated or damaged frames,
|
|
22
|
+
throws `ZstdError`. It never hangs, and it never fails with another kind of
|
|
23
|
+
error from inside the decoder.
|
|
24
|
+
|
|
25
|
+
## API
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
decompress(data: Uint8Array, out?: Uint8Array, verify = true): Uint8Array
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
- **Input:** one or more frames; skippable frames are ignored.
|
|
32
|
+
- **`out`:** if given, the result is a view of it, and it must be large enough.
|
|
33
|
+
Otherwise the output is allocated, sized exactly when frames declare their size.
|
|
34
|
+
- **`verify = false`:** skips checksum verification.
|
|
35
|
+
|
|
36
|
+
`ZstdError` is the error class. `xxh64Backend()` reports which checksum
|
|
37
|
+
implementation is in use.
|
|
38
|
+
|
|
39
|
+
Not supported: dictionaries, which throw an error, and streaming input. The
|
|
40
|
+
module only decompresses.
|
|
41
|
+
|
|
42
|
+
## Checksums and WebAssembly
|
|
43
|
+
|
|
44
|
+
XXH64 needs 64-bit multiplication, which JavaScript can only emulate, at about
|
|
45
|
+
350 MB/s. So the module embeds an 837-byte WebAssembly build of XXH64, which
|
|
46
|
+
runs at native speed. Its source is in `xxh64/`, and `xxh64/build.sh`
|
|
47
|
+
regenerates it.
|
|
48
|
+
|
|
49
|
+
The module is compiled on first use. If WebAssembly is unavailable (an older
|
|
50
|
+
runtime, or a content security policy without `wasm-unsafe-eval`), checksums use
|
|
51
|
+
the JavaScript implementation instead. Both give the same results, and the tests
|
|
52
|
+
run both.
|
|
53
|
+
|
|
54
|
+
## Performance
|
|
55
|
+
|
|
56
|
+
Throughput in MB/s on database index nodes of about 6 KB, with checksums, on a
|
|
57
|
+
busy machine:
|
|
58
|
+
|
|
59
|
+
| engine | fzstd | @replikativ/zstd | native zstd |
|
|
60
|
+
|---|---|---|---|
|
|
61
|
+
| V8 (Node 24) | 106 | 215 | 382 (node:zlib) |
|
|
62
|
+
| JavaScriptCore (Bun 1.3) | 126 | 241 | 483 (Bun) |
|
|
63
|
+
| SpiderMonkey 140 (gjs) | 64 | 128 | – |
|
|
64
|
+
|
|
65
|
+
Run `npm run bench` to measure on the test fixtures, or
|
|
66
|
+
`node bench/bench.mjs <dir>` for a directory of `.zst` files.
|
|
67
|
+
|
|
68
|
+
## Testing
|
|
69
|
+
|
|
70
|
+
```sh
|
|
71
|
+
npm test
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
This needs Node 22.15 or newer, because the reference zstd library in
|
|
75
|
+
`node:zlib` is the oracle. The tests check:
|
|
76
|
+
|
|
77
|
+
- frames written by [zstd-java](https://github.com/replikativ/zstd-java),
|
|
78
|
+
generated reproducibly with `test/gen-fixtures.clj`;
|
|
79
|
+
- the reference encoder at levels 1 to 19, with and without checksums and
|
|
80
|
+
content sizes;
|
|
81
|
+
- concatenated and skippable frames;
|
|
82
|
+
- that every bit flip in a checksummed frame is rejected;
|
|
83
|
+
- fuzzed and truncated input;
|
|
84
|
+
- XXH64 against a BigInt implementation of the specification.
|
|
85
|
+
|
|
86
|
+
The suite runs once with WebAssembly and once without (`--jitless`). CI also
|
|
87
|
+
runs it on Bun.
|
|
88
|
+
|
|
89
|
+
## License
|
|
90
|
+
|
|
91
|
+
MIT. Copyright (c) 2020 Arjun Barrett (fzstd); copyright (c) 2026 replikativ
|
|
92
|
+
contributors. XXH64 is Yann Collet's algorithm, implemented here from its
|
|
93
|
+
specification.
|
package/package.json
CHANGED
|
@@ -1,6 +1,46 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@replikativ/zstd",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Zstandard decompression in plain JavaScript that verifies frame checksums",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./zstd.mjs",
|
|
7
|
+
"types": "./zstd.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./zstd.d.ts",
|
|
11
|
+
"default": "./zstd.mjs"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"zstd.mjs",
|
|
16
|
+
"zstd.d.ts",
|
|
17
|
+
"LICENSE",
|
|
18
|
+
"README.md"
|
|
19
|
+
],
|
|
20
|
+
"sideEffects": false,
|
|
21
|
+
"scripts": {
|
|
22
|
+
"test": "node test/test.mjs && node --jitless test/test.mjs && node test/xxh64-reference.mjs",
|
|
23
|
+
"bench": "node bench/bench.mjs"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"zstd",
|
|
27
|
+
"zstandard",
|
|
28
|
+
"decompression",
|
|
29
|
+
"browser",
|
|
30
|
+
"pure-javascript"
|
|
31
|
+
],
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "https://github.com/replikativ/zstd-js.git"
|
|
35
|
+
},
|
|
36
|
+
"homepage": "https://github.com/replikativ/zstd-js",
|
|
37
|
+
"bugs": {
|
|
38
|
+
"url": "https://github.com/replikativ/zstd-js/issues"
|
|
39
|
+
},
|
|
40
|
+
"author": "replikativ",
|
|
41
|
+
"license": "MIT",
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public",
|
|
44
|
+
"registry": "https://registry.npmjs.org/"
|
|
45
|
+
}
|
|
46
|
+
}
|
package/zstd.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** Thrown for any input that is not valid zstd data, including checksum mismatches. */
|
|
2
|
+
export class ZstdError extends Error {}
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Decompresses zstd data: one or more frames; skippable frames are ignored.
|
|
6
|
+
* @param data the compressed bytes
|
|
7
|
+
* @param out where to decompress to; must be large enough. The result is a view of it.
|
|
8
|
+
* @param verify check frame checksums where frames carry them (default true)
|
|
9
|
+
* @returns the decompressed bytes
|
|
10
|
+
*/
|
|
11
|
+
export function decompress(data: Uint8Array, out?: Uint8Array, verify?: boolean): Uint8Array;
|
|
12
|
+
|
|
13
|
+
/** Low 32 bits of XXH64(bytes[start, end)) with seed 0, as zstd frame checksums store it. */
|
|
14
|
+
export function xxh64lo(bytes: Uint8Array, start: number, end: number): number;
|
|
15
|
+
|
|
16
|
+
/** The same hash, always in JavaScript (the fallback when WebAssembly is unavailable). */
|
|
17
|
+
export function xxh64js(bytes: Uint8Array, start: number, end: number): number;
|
|
18
|
+
|
|
19
|
+
/** Which XXH64 implementation checksums use. */
|
|
20
|
+
export function xxh64Backend(): 'wasm' | 'js';
|
package/zstd.mjs
ADDED
|
@@ -0,0 +1,762 @@
|
|
|
1
|
+
/*! Zstandard decoder in plain JavaScript.
|
|
2
|
+
* Derived from fzstd 0.1.1 by Arjun Barrett (MIT License, Copyright (c) 2020
|
|
3
|
+
* Arjun Barrett, https://github.com/101arrowz/fzstd); the decoding loops,
|
|
4
|
+
* tables and bit reading are rewritten, and frame checksums are verified. */
|
|
5
|
+
|
|
6
|
+
const u8 = Uint8Array, i32 = Int32Array;
|
|
7
|
+
|
|
8
|
+
export class ZstdError extends Error {}
|
|
9
|
+
function err(msg) { throw new ZstdError(msg || 'invalid zstd data'); }
|
|
10
|
+
|
|
11
|
+
// index of the highest set bit, v > 0
|
|
12
|
+
const hib = (v) => 31 - Math.clz32(v);
|
|
13
|
+
|
|
14
|
+
// ---------------------------------------------------------------- code tables
|
|
15
|
+
|
|
16
|
+
const LL_BASE = new i32([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 20, 22, 24, 28, 32,
|
|
17
|
+
40, 48, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536]);
|
|
18
|
+
const LL_BITS = new u8([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 4, 6, 7, 8, 9,
|
|
19
|
+
10, 11, 12, 13, 14, 15, 16]);
|
|
20
|
+
const ML_BASE = new i32(53), ML_BITS = new u8(53);
|
|
21
|
+
{
|
|
22
|
+
const b = [35, 37, 39, 41, 43, 47, 51, 59, 67, 83, 99, 131, 259, 515, 1027, 2051, 4099, 8195, 16387, 32771, 65539];
|
|
23
|
+
const n = [1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
|
|
24
|
+
for (let i = 0; i < 32; ++i) ML_BASE[i] = i + 3;
|
|
25
|
+
for (let i = 0; i < 21; ++i) ML_BASE[32 + i] = b[i], ML_BITS[32 + i] = n[i];
|
|
26
|
+
}
|
|
27
|
+
// offset codes above 30 cannot address a window the format allows
|
|
28
|
+
const OF_BASE = new i32(31), OF_BITS = new u8(31);
|
|
29
|
+
for (let i = 0; i < 31; ++i) OF_BASE[i] = 1 << i, OF_BITS[i] = i;
|
|
30
|
+
|
|
31
|
+
// ------------------------------------------------------------------ FSE tables
|
|
32
|
+
|
|
33
|
+
// scratch: normalized counts, then the decoding table they spread into
|
|
34
|
+
const NORM = new Int16Array(256 + 8);
|
|
35
|
+
const NEXT = new Uint16Array(256 + 8);
|
|
36
|
+
const FSYM = new u8(512), FNB = new u8(512), FNS = new Uint16Array(512);
|
|
37
|
+
let ncLog = 0, ncSyms = 0;
|
|
38
|
+
|
|
39
|
+
// Reads an FSE table description at byte bt into NORM; returns the next byte.
|
|
40
|
+
function readNCount(dat, bt, maxLog, maxSym) {
|
|
41
|
+
const al = (dat[bt] & 15) + 5;
|
|
42
|
+
if (al > maxLog) err('FSE accuracy too high');
|
|
43
|
+
const sz = 1 << al;
|
|
44
|
+
let tpos = (bt << 3) + 4, probs = sz, sym = -1;
|
|
45
|
+
while (sym < 255 && probs > 0) {
|
|
46
|
+
const bits = hib(probs + 1), cbt = tpos >> 3, msk = (1 << (bits + 1)) - 1;
|
|
47
|
+
let val = ((dat[cbt] | (dat[cbt + 1] << 8) | (dat[cbt + 2] << 16)) >> (tpos & 7)) & msk;
|
|
48
|
+
const msk1fb = (1 << bits) - 1, msv = msk - probs - 1, sval = val & msk1fb;
|
|
49
|
+
if (sval < msv) tpos += bits, val = sval;
|
|
50
|
+
else {
|
|
51
|
+
tpos += bits + 1;
|
|
52
|
+
if (val > msk1fb) val -= msv;
|
|
53
|
+
}
|
|
54
|
+
NORM[++sym] = --val;
|
|
55
|
+
if (val == -1) --probs;
|
|
56
|
+
else probs -= val;
|
|
57
|
+
if (!val) {
|
|
58
|
+
let re;
|
|
59
|
+
do {
|
|
60
|
+
const rbt = tpos >> 3;
|
|
61
|
+
re = ((dat[rbt] | (dat[rbt + 1] << 8)) >> (tpos & 7)) & 3;
|
|
62
|
+
tpos += 2;
|
|
63
|
+
for (let k = 0; k < re && sym < 255; ++k) NORM[++sym] = 0;
|
|
64
|
+
} while (re == 3);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (sym > maxSym || probs) err();
|
|
68
|
+
ncLog = al, ncSyms = sym + 1;
|
|
69
|
+
return (tpos + 7) >> 3;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Spreads NORM into FSYM/FNB/FNS: symbol, state bits and next-state base.
|
|
73
|
+
function buildFSE(al, nsyms) {
|
|
74
|
+
const sz = 1 << al;
|
|
75
|
+
let high = sz - 1;
|
|
76
|
+
for (let s = 0; s < nsyms; ++s) {
|
|
77
|
+
const c = NORM[s];
|
|
78
|
+
if (c == -1) FSYM[high--] = s, NEXT[s] = 1;
|
|
79
|
+
else NEXT[s] = c;
|
|
80
|
+
}
|
|
81
|
+
const step = (sz >> 1) + (sz >> 3) + 3, mask = sz - 1;
|
|
82
|
+
let pos = 0;
|
|
83
|
+
for (let s = 0; s < nsyms; ++s) {
|
|
84
|
+
const c = NORM[s];
|
|
85
|
+
for (let i = 0; i < c; ++i) {
|
|
86
|
+
FSYM[pos] = s;
|
|
87
|
+
do pos = (pos + step) & mask; while (pos > high);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
if (pos) err();
|
|
91
|
+
for (let u = 0; u < sz; ++u) {
|
|
92
|
+
const ns = NEXT[FSYM[u]]++, nb = al - hib(ns);
|
|
93
|
+
FNB[u] = nb, FNS[u] = (ns << nb) - sz;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// A sequence decoding table, two ints per state:
|
|
98
|
+
// [2u] baseline value, [2u+1] extra bits | state bits << 8 | next state << 16
|
|
99
|
+
function seqTable(T, al, base, bits) {
|
|
100
|
+
const sz = 1 << al, nb = base.length;
|
|
101
|
+
for (let u = 0; u < sz; ++u) {
|
|
102
|
+
const s = FSYM[u];
|
|
103
|
+
if (s >= nb) err();
|
|
104
|
+
T[u << 1] = base[s];
|
|
105
|
+
T[(u << 1) | 1] = bits[s] | (FNB[u] << 8) | (FNS[u] << 16);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function predefined(norm, al, base, bits) {
|
|
110
|
+
for (let i = 0; i < norm.length; ++i) NORM[i] = norm[i];
|
|
111
|
+
buildFSE(al, norm.length);
|
|
112
|
+
const T = new i32(2 << al);
|
|
113
|
+
seqTable(T, al, base, bits);
|
|
114
|
+
return T;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const PRE_LL = predefined([4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 1, 1,
|
|
118
|
+
1, 1, 1, -1, -1, -1, -1], 6, LL_BASE, LL_BITS);
|
|
119
|
+
const PRE_ML = predefined([1, 4, 3, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
120
|
+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1], 6, ML_BASE, ML_BITS);
|
|
121
|
+
const PRE_OF = predefined([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,
|
|
122
|
+
-1], 5, OF_BASE, OF_BITS);
|
|
123
|
+
|
|
124
|
+
// --------------------------------------------------------------- decoder state
|
|
125
|
+
// One decoder per module: JavaScript runs a call to completion, and a frame's
|
|
126
|
+
// blocks share these (repeat offsets, repeated tables, the Huffman table).
|
|
127
|
+
|
|
128
|
+
const LLW = new i32(1024), MLW = new i32(1024), OFW = new i32(512);
|
|
129
|
+
const LLR = new i32(2), MLR = new i32(2), OFR = new i32(2);
|
|
130
|
+
let LLt = PRE_LL, MLt = PRE_ML, OFt = PRE_OF, LLl = 6, MLl = 6, OFl = 5, seqOk = false;
|
|
131
|
+
// Huffman table: symbol | bits << 8, indexed by the next hufLog bits
|
|
132
|
+
const HT = new Uint16Array(2048), HT32 = new Uint32Array(HT.buffer);
|
|
133
|
+
let hufLog = 0, hufOk = false;
|
|
134
|
+
const W = new u8(257), RC = new Uint32Array(16), RI = new Uint32Array(16);
|
|
135
|
+
let r0 = 1, r1 = 4, r2 = 8, fs0 = 0;
|
|
136
|
+
// literals of the current block; 32 spare bytes keep 16-byte over-reads inside
|
|
137
|
+
const LB = new u8(131072 + 32), LBV = new DataView(LB.buffer);
|
|
138
|
+
|
|
139
|
+
// Reads a Huffman tree description at bt into HT; returns the next byte.
|
|
140
|
+
function readHuffman(dat, bt) {
|
|
141
|
+
const hb0 = dat[bt];
|
|
142
|
+
let wc;
|
|
143
|
+
if (hb0 < 128) {
|
|
144
|
+
// weights are FSE coded, two interleaved states, read backwards
|
|
145
|
+
const ebt = readNCount(dat, bt + 1, 6, 255);
|
|
146
|
+
buildFSE(ncLog, ncSyms);
|
|
147
|
+
const last = bt + hb0, epos = ebt << 3, lb = dat[last];
|
|
148
|
+
if (!lb || last <= ebt - 1) err();
|
|
149
|
+
let st1 = 0, st2 = 0, b1 = ncLog, b2 = ncLog;
|
|
150
|
+
let fpos = (last << 3) + hib(lb);
|
|
151
|
+
wc = -1;
|
|
152
|
+
for (;;) {
|
|
153
|
+
fpos -= b1;
|
|
154
|
+
if (fpos < epos) break;
|
|
155
|
+
let c = fpos >> 3;
|
|
156
|
+
st1 += ((dat[c] | (dat[c + 1] << 8)) >> (fpos & 7)) & ((1 << b1) - 1);
|
|
157
|
+
W[++wc] = FSYM[st1];
|
|
158
|
+
fpos -= b2;
|
|
159
|
+
if (fpos < epos) break;
|
|
160
|
+
c = fpos >> 3;
|
|
161
|
+
st2 += ((dat[c] | (dat[c + 1] << 8)) >> (fpos & 7)) & ((1 << b2) - 1);
|
|
162
|
+
W[++wc] = FSYM[st2];
|
|
163
|
+
b1 = FNB[st1], st1 = FNS[st1], b2 = FNB[st2], st2 = FNS[st2];
|
|
164
|
+
}
|
|
165
|
+
if (++wc > 255) err();
|
|
166
|
+
bt = last + 1;
|
|
167
|
+
} else {
|
|
168
|
+
wc = hb0 - 127;
|
|
169
|
+
for (let i = 0; i < wc; i += 2) {
|
|
170
|
+
const b = dat[++bt];
|
|
171
|
+
W[i] = b >> 4, W[i + 1] = b & 15;
|
|
172
|
+
}
|
|
173
|
+
++bt;
|
|
174
|
+
}
|
|
175
|
+
// counts per weight and the weight sum in one pass
|
|
176
|
+
RC.fill(0);
|
|
177
|
+
let wes = 0;
|
|
178
|
+
for (let i = 0; i < wc; ++i) {
|
|
179
|
+
const w = W[i];
|
|
180
|
+
if (w > 11) err();
|
|
181
|
+
++RC[w];
|
|
182
|
+
wes += (1 << w) >> 1;
|
|
183
|
+
}
|
|
184
|
+
if (!wes) err();
|
|
185
|
+
const mb = hib(wes) + 1;
|
|
186
|
+
if (mb > 11) err();
|
|
187
|
+
const ts = 1 << mb, rem = ts - wes;
|
|
188
|
+
if (rem & (rem - 1)) err();
|
|
189
|
+
const lw = hib(rem) + 1;
|
|
190
|
+
W[wc] = lw, ++RC[lw];
|
|
191
|
+
// A symbol of weight w takes 2^(w-1) entries; weight 1 (the longest
|
|
192
|
+
// code) comes first. In a complete code each run starts at a multiple of
|
|
193
|
+
// its length, so runs of 2 or more fill whole 32-bit pairs.
|
|
194
|
+
let at = 0;
|
|
195
|
+
for (let w = 1; w <= mb; ++w) RI[w] = at, at += RC[w] << (w - 1);
|
|
196
|
+
if (at != ts) err();
|
|
197
|
+
for (let i = 0, ns = wc + 1; i < ns; ++i) {
|
|
198
|
+
const w = W[i];
|
|
199
|
+
if (!w) continue;
|
|
200
|
+
const c = RI[w], v = i | ((mb + 1 - w) << 8);
|
|
201
|
+
if (w == 1) {
|
|
202
|
+
HT[c] = v;
|
|
203
|
+
RI[1] = c + 1;
|
|
204
|
+
} else {
|
|
205
|
+
const k = 1 << (w - 1), vv = v | (v << 16);
|
|
206
|
+
RI[w] = c + k;
|
|
207
|
+
for (let j = c >> 1, e = (c + k) >> 1; j < e; ++j) HT32[j] = vv;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
hufLog = mb, hufOk = true;
|
|
211
|
+
return bt;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// ------------------------------------------------------------------- Huffman
|
|
215
|
+
|
|
216
|
+
// Unconsumed bits of a backward stream [s, e): everything below its end mark.
|
|
217
|
+
function streamBits(dat, s, e) {
|
|
218
|
+
if (e <= s) err();
|
|
219
|
+
const lb = dat[e - 1];
|
|
220
|
+
if (!lb) err();
|
|
221
|
+
return ((e - 1 - s) << 3) + hib(lb);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// Decodes symbols into out[o, oe) from the stream at byte s with p bits left.
|
|
225
|
+
// Window refills read 4 bytes at s + lo/8 with p - lo in [25, 32]; since
|
|
226
|
+
// p <= 8 * len - 1, those stay inside the stream.
|
|
227
|
+
function hufTail(dat, dv, s, p, out, o, oe, mb) {
|
|
228
|
+
const T = HT, mask = (1 << mb) - 1;
|
|
229
|
+
let lo = 0x7FFFFFFF, w = 0;
|
|
230
|
+
while (o < oe && p >= 25) {
|
|
231
|
+
if (p - mb < lo) lo = (p - 25) & -8, w = dv.getInt32(s + (lo >> 3), true);
|
|
232
|
+
const x = T[(w >>> (p - mb - lo)) & mask];
|
|
233
|
+
out[o++] = x;
|
|
234
|
+
p -= x >> 8;
|
|
235
|
+
}
|
|
236
|
+
if (o < oe) {
|
|
237
|
+
// the last bits: what lies below the stream reads as zeros
|
|
238
|
+
const v = dat[s] | (dat[s + 1] << 8) | (dat[s + 2] << 16) | (dat[s + 3] << 24);
|
|
239
|
+
while (o < oe) {
|
|
240
|
+
if (p <= 0) err();
|
|
241
|
+
const x = T[(p >= mb ? v >>> (p - mb) : v << (mb - p)) & mask];
|
|
242
|
+
out[o++] = x;
|
|
243
|
+
p -= x >> 8;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
if (p) err();
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function huf4(dat, dv, bt, se, out, o1, oe, mb) {
|
|
250
|
+
const s1 = bt + 6, s2 = s1 + (dat[bt] | (dat[bt + 1] << 8)), s3 = s2 + (dat[bt + 2] | (dat[bt + 3] << 8));
|
|
251
|
+
const s4 = s3 + (dat[bt + 4] | (dat[bt + 5] << 8));
|
|
252
|
+
if (s4 >= se) err();
|
|
253
|
+
const seg = (oe - o1 + 3) >> 2, e1 = o1 + seg, e2 = e1 + seg, e3 = e2 + seg;
|
|
254
|
+
if (e3 > oe) err();
|
|
255
|
+
let p1 = streamBits(dat, s1, s2), p2 = streamBits(dat, s2, s3), p3 = streamBits(dat, s3, s4), p4 = streamBits(dat, s4, se);
|
|
256
|
+
let o2 = e1, o3 = e2, o4 = e3;
|
|
257
|
+
const T = HT, mask = (1 << mb) - 1;
|
|
258
|
+
// Each refill leaves at least 25 bits: two symbols of at most 11 bits.
|
|
259
|
+
// Stream 4 is the shortest, so its output bounds all four.
|
|
260
|
+
while (o4 + 2 <= oe && p1 >= 25 && p2 >= 25 && p3 >= 25 && p4 >= 25) {
|
|
261
|
+
const l1 = (p1 - 25) & -8, l2 = (p2 - 25) & -8, l3 = (p3 - 25) & -8, l4 = (p4 - 25) & -8;
|
|
262
|
+
const w1 = dv.getInt32(s1 + (l1 >> 3), true), w2 = dv.getInt32(s2 + (l2 >> 3), true);
|
|
263
|
+
const w3 = dv.getInt32(s3 + (l3 >> 3), true), w4 = dv.getInt32(s4 + (l4 >> 3), true);
|
|
264
|
+
let x1 = T[(w1 >>> (p1 - mb - l1)) & mask], x2 = T[(w2 >>> (p2 - mb - l2)) & mask];
|
|
265
|
+
let x3 = T[(w3 >>> (p3 - mb - l3)) & mask], x4 = T[(w4 >>> (p4 - mb - l4)) & mask];
|
|
266
|
+
out[o1] = x1, out[o2] = x2, out[o3] = x3, out[o4] = x4;
|
|
267
|
+
p1 -= x1 >> 8, p2 -= x2 >> 8, p3 -= x3 >> 8, p4 -= x4 >> 8;
|
|
268
|
+
x1 = T[(w1 >>> (p1 - mb - l1)) & mask], x2 = T[(w2 >>> (p2 - mb - l2)) & mask];
|
|
269
|
+
x3 = T[(w3 >>> (p3 - mb - l3)) & mask], x4 = T[(w4 >>> (p4 - mb - l4)) & mask];
|
|
270
|
+
out[o1 + 1] = x1, out[o2 + 1] = x2, out[o3 + 1] = x3, out[o4 + 1] = x4;
|
|
271
|
+
p1 -= x1 >> 8, p2 -= x2 >> 8, p3 -= x3 >> 8, p4 -= x4 >> 8;
|
|
272
|
+
o1 += 2, o2 += 2, o3 += 2, o4 += 2;
|
|
273
|
+
}
|
|
274
|
+
hufTail(dat, dv, s1, p1, out, o1, e1, mb);
|
|
275
|
+
hufTail(dat, dv, s2, p2, out, o2, e2, mb);
|
|
276
|
+
hufTail(dat, dv, s3, p3, out, o3, e3, mb);
|
|
277
|
+
hufTail(dat, dv, s4, p4, out, o4, oe, mb);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// ------------------------------------------------------------------- blocks
|
|
281
|
+
|
|
282
|
+
// Copies n literal bytes; lit is the input (raw literals) or out (decoded).
|
|
283
|
+
function copyLit(lit, lp, out, op, n) {
|
|
284
|
+
out.set(lit.subarray(lp, lp + n), op);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// Decodes the compressed block dat[bt, ebt) to out (ov its DataView) at op,
|
|
288
|
+
// not past re. Short copies move 4 bytes at a time and may write up to 15
|
|
289
|
+
// bytes past their end, which later output overwrites; they run only where
|
|
290
|
+
// that stays inside out.
|
|
291
|
+
function decodeBlock(dat, dv, bt, ebt, out, ov, op, re) {
|
|
292
|
+
const b0 = dat[bt], lbt = b0 & 3, sf = (b0 >> 2) & 3;
|
|
293
|
+
let lss = b0 >> 4, lcs = 0;
|
|
294
|
+
if (lbt < 2) {
|
|
295
|
+
if (sf & 1) {
|
|
296
|
+
lss |= dat[++bt] << 4;
|
|
297
|
+
if (sf & 2) lss |= dat[++bt] << 12;
|
|
298
|
+
} else lss = b0 >> 3;
|
|
299
|
+
} else if (sf < 2) {
|
|
300
|
+
lss |= (dat[++bt] & 63) << 4;
|
|
301
|
+
lcs = (dat[bt] >> 6) | (dat[++bt] << 2);
|
|
302
|
+
} else if (sf == 2) {
|
|
303
|
+
lss |= (dat[++bt] << 4) | ((dat[++bt] & 3) << 12);
|
|
304
|
+
lcs = (dat[bt] >> 2) | (dat[++bt] << 6);
|
|
305
|
+
} else {
|
|
306
|
+
lss |= (dat[++bt] << 4) | ((dat[++bt] & 63) << 12);
|
|
307
|
+
lcs = (dat[bt] >> 6) | (dat[++bt] << 2) | (dat[++bt] << 10);
|
|
308
|
+
}
|
|
309
|
+
++bt;
|
|
310
|
+
let lit = LB, lv = LBV, lp = 0, le = lss;
|
|
311
|
+
if (lbt == 0) {
|
|
312
|
+
lit = dat, lv = dv, lp = bt, le = bt += lss;
|
|
313
|
+
if (le > ebt) err();
|
|
314
|
+
} else {
|
|
315
|
+
if (lss > 131072) err();
|
|
316
|
+
if (lbt == 1) LB.fill(dat[bt++], 0, lss);
|
|
317
|
+
else {
|
|
318
|
+
if (lbt == 2) {
|
|
319
|
+
const b = bt;
|
|
320
|
+
bt = readHuffman(dat, bt);
|
|
321
|
+
lcs -= bt - b;
|
|
322
|
+
} else if (!hufOk) err();
|
|
323
|
+
const se = bt + lcs;
|
|
324
|
+
if (lcs < 1 || se > ebt) err();
|
|
325
|
+
if (sf) huf4(dat, dv, bt, se, LB, 0, lss, hufLog);
|
|
326
|
+
else hufTail(dat, dv, bt, streamBits(dat, bt, se), LB, 0, lss, hufLog);
|
|
327
|
+
bt = se;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
if (bt >= ebt) err();
|
|
331
|
+
let ns = dat[bt++];
|
|
332
|
+
if (!ns) {
|
|
333
|
+
copyLit(lit, lp, out, op, lss);
|
|
334
|
+
return op + lss;
|
|
335
|
+
}
|
|
336
|
+
if (ns > 127) {
|
|
337
|
+
if (ns == 255) ns = (dat[bt] | (dat[bt + 1] << 8)) + 0x7F00, bt += 2;
|
|
338
|
+
else ns = ((ns - 128) << 8) | dat[bt++];
|
|
339
|
+
}
|
|
340
|
+
const scm = dat[bt++];
|
|
341
|
+
if (scm & 3) err();
|
|
342
|
+
let m = scm >> 6;
|
|
343
|
+
if (m == 0) LLt = PRE_LL, LLl = 6;
|
|
344
|
+
else if (m == 1) {
|
|
345
|
+
const s = dat[bt++];
|
|
346
|
+
if (s > 35) err();
|
|
347
|
+
LLR[0] = LL_BASE[s], LLR[1] = LL_BITS[s], LLt = LLR, LLl = 0;
|
|
348
|
+
} else if (m == 2) {
|
|
349
|
+
bt = readNCount(dat, bt, 9, 35);
|
|
350
|
+
buildFSE(ncLog, ncSyms);
|
|
351
|
+
seqTable(LLW, ncLog, LL_BASE, LL_BITS);
|
|
352
|
+
LLt = LLW, LLl = ncLog;
|
|
353
|
+
} else if (!seqOk) err();
|
|
354
|
+
m = (scm >> 4) & 3;
|
|
355
|
+
if (m == 0) OFt = PRE_OF, OFl = 5;
|
|
356
|
+
else if (m == 1) {
|
|
357
|
+
const s = dat[bt++];
|
|
358
|
+
if (s > 30) err();
|
|
359
|
+
OFR[0] = OF_BASE[s], OFR[1] = OF_BITS[s], OFt = OFR, OFl = 0;
|
|
360
|
+
} else if (m == 2) {
|
|
361
|
+
bt = readNCount(dat, bt, 8, 31);
|
|
362
|
+
buildFSE(ncLog, ncSyms);
|
|
363
|
+
seqTable(OFW, ncLog, OF_BASE, OF_BITS);
|
|
364
|
+
OFt = OFW, OFl = ncLog;
|
|
365
|
+
} else if (!seqOk) err();
|
|
366
|
+
m = (scm >> 2) & 3;
|
|
367
|
+
if (m == 0) MLt = PRE_ML, MLl = 6;
|
|
368
|
+
else if (m == 1) {
|
|
369
|
+
const s = dat[bt++];
|
|
370
|
+
if (s > 52) err();
|
|
371
|
+
MLR[0] = ML_BASE[s], MLR[1] = ML_BITS[s], MLt = MLR, MLl = 0;
|
|
372
|
+
} else if (m == 2) {
|
|
373
|
+
bt = readNCount(dat, bt, 9, 52);
|
|
374
|
+
buildFSE(ncLog, ncSyms);
|
|
375
|
+
seqTable(MLW, ncLog, ML_BASE, ML_BITS);
|
|
376
|
+
MLt = MLW, MLl = ncLog;
|
|
377
|
+
} else if (!seqOk) err();
|
|
378
|
+
seqOk = true;
|
|
379
|
+
|
|
380
|
+
// The sequence bitstream [bt, ebt), read backwards. Bits [p0, pos) are
|
|
381
|
+
// unread; win holds the 32 bits from lo, a byte boundary. A refill leaves
|
|
382
|
+
// pos - lo in [25, 32] (and reads at most up to ebt), so any field of at
|
|
383
|
+
// most 25 bits needs one refill check.
|
|
384
|
+
if (bt >= ebt) err();
|
|
385
|
+
const lb = dat[ebt - 1];
|
|
386
|
+
if (!lb) err();
|
|
387
|
+
const LT = LLt, MT = MLt, OT = OFt, p0 = bt << 3, f0 = fs0, olim = out.length - 16, llim = lit.length - 16;
|
|
388
|
+
let pos = ((ebt - 1) << 3) + hib(lb), lo = (pos - 25) & -8;
|
|
389
|
+
if (lo < 0) lo = 0;
|
|
390
|
+
let win = dv.getInt32(lo >> 3, true), n = LLl;
|
|
391
|
+
if (pos - n < lo) { lo = (pos - 25) & -8; if (lo < 0) lo = 0; win = dv.getInt32(lo >> 3, true); }
|
|
392
|
+
pos -= n;
|
|
393
|
+
let ls = (win >>> (pos - lo)) & ((1 << n) - 1);
|
|
394
|
+
n = OFl;
|
|
395
|
+
if (pos - n < lo) { lo = (pos - 25) & -8; if (lo < 0) lo = 0; win = dv.getInt32(lo >> 3, true); }
|
|
396
|
+
pos -= n;
|
|
397
|
+
let os = (win >>> (pos - lo)) & ((1 << n) - 1);
|
|
398
|
+
n = MLl;
|
|
399
|
+
if (pos - n < lo) { lo = (pos - 25) & -8; if (lo < 0) lo = 0; win = dv.getInt32(lo >> 3, true); }
|
|
400
|
+
pos -= n;
|
|
401
|
+
let ms = (win >>> (pos - lo)) & ((1 << n) - 1);
|
|
402
|
+
let a = r0, b = r1, c = r2;
|
|
403
|
+
for (;;) {
|
|
404
|
+
const li = LT[(ls << 1) | 1], mi = MT[(ms << 1) | 1], oi = OT[(os << 1) | 1];
|
|
405
|
+
let off;
|
|
406
|
+
n = oi & 255;
|
|
407
|
+
if (n > 25) {
|
|
408
|
+
const hn = n - 25;
|
|
409
|
+
if (pos - hn < lo) { lo = (pos - 25) & -8; if (lo < 0) lo = 0; win = dv.getInt32(lo >> 3, true); }
|
|
410
|
+
pos -= hn;
|
|
411
|
+
const hv = (win >>> (pos - lo)) & ((1 << hn) - 1);
|
|
412
|
+
if (pos - 25 < lo) { lo = (pos - 25) & -8; if (lo < 0) lo = 0; win = dv.getInt32(lo >> 3, true); }
|
|
413
|
+
pos -= 25;
|
|
414
|
+
off = OT[os << 1] + hv * 33554432 + ((win >>> (pos - lo)) & 33554431);
|
|
415
|
+
} else {
|
|
416
|
+
if (pos - n < lo) { lo = (pos - 25) & -8; if (lo < 0) lo = 0; win = dv.getInt32(lo >> 3, true); }
|
|
417
|
+
pos -= n;
|
|
418
|
+
off = OT[os << 1] + ((win >>> (pos - lo)) & ((1 << n) - 1));
|
|
419
|
+
}
|
|
420
|
+
n = mi & 255;
|
|
421
|
+
if (pos - n < lo) { lo = (pos - 25) & -8; if (lo < 0) lo = 0; win = dv.getInt32(lo >> 3, true); }
|
|
422
|
+
pos -= n;
|
|
423
|
+
let ml = MT[ms << 1] + ((win >>> (pos - lo)) & ((1 << n) - 1));
|
|
424
|
+
n = li & 255;
|
|
425
|
+
if (pos - n < lo) { lo = (pos - 25) & -8; if (lo < 0) lo = 0; win = dv.getInt32(lo >> 3, true); }
|
|
426
|
+
pos -= n;
|
|
427
|
+
const ll = LT[ls << 1] + ((win >>> (pos - lo)) & ((1 << n) - 1));
|
|
428
|
+
if (--ns) {
|
|
429
|
+
n = (li >> 8) & 255;
|
|
430
|
+
if (pos - n < lo) { lo = (pos - 25) & -8; if (lo < 0) lo = 0; win = dv.getInt32(lo >> 3, true); }
|
|
431
|
+
pos -= n;
|
|
432
|
+
ls = (li >>> 16) + ((win >>> (pos - lo)) & ((1 << n) - 1));
|
|
433
|
+
n = (mi >> 8) & 255;
|
|
434
|
+
if (pos - n < lo) { lo = (pos - 25) & -8; if (lo < 0) lo = 0; win = dv.getInt32(lo >> 3, true); }
|
|
435
|
+
pos -= n;
|
|
436
|
+
ms = (mi >>> 16) + ((win >>> (pos - lo)) & ((1 << n) - 1));
|
|
437
|
+
n = (oi >> 8) & 255;
|
|
438
|
+
if (pos - n < lo) { lo = (pos - 25) & -8; if (lo < 0) lo = 0; win = dv.getInt32(lo >> 3, true); }
|
|
439
|
+
pos -= n;
|
|
440
|
+
os = (oi >>> 16) + ((win >>> (pos - lo)) & ((1 << n) - 1));
|
|
441
|
+
}
|
|
442
|
+
if (off > 3) c = b, b = a, a = off -= 3;
|
|
443
|
+
else {
|
|
444
|
+
const idx = ll ? off - 1 : off;
|
|
445
|
+
if (idx) {
|
|
446
|
+
off = idx == 3 ? a - 1 : idx == 1 ? b : c;
|
|
447
|
+
if (idx > 1) c = b;
|
|
448
|
+
b = a, a = off;
|
|
449
|
+
} else off = a;
|
|
450
|
+
}
|
|
451
|
+
if (ll <= 16 && op <= olim && lp <= llim) {
|
|
452
|
+
if (ll) {
|
|
453
|
+
ov.setInt32(op, lv.getInt32(lp, true), true);
|
|
454
|
+
ov.setInt32(op + 4, lv.getInt32(lp + 4, true), true);
|
|
455
|
+
if (ll > 8) {
|
|
456
|
+
ov.setInt32(op + 8, lv.getInt32(lp + 8, true), true);
|
|
457
|
+
ov.setInt32(op + 12, lv.getInt32(lp + 12, true), true);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
} else if (ll < 16) {
|
|
461
|
+
for (let i = 0; i < ll; ++i) out[op + i] = lit[lp + i];
|
|
462
|
+
} else copyLit(lit, lp, out, op, ll);
|
|
463
|
+
op += ll, lp += ll;
|
|
464
|
+
if ((off - 1) >>> 0 >= op - f0) err('match distance too far back');
|
|
465
|
+
const src = op - off;
|
|
466
|
+
if (off >= 4 && ml <= 64 && op + ml <= olim) {
|
|
467
|
+
// 4-byte steps read only bytes written before them when off >= 4
|
|
468
|
+
ov.setInt32(op, ov.getInt32(src, true), true);
|
|
469
|
+
ov.setInt32(op + 4, ov.getInt32(src + 4, true), true);
|
|
470
|
+
for (let i = 8; i < ml; i += 4) ov.setInt32(op + i, ov.getInt32(src + i, true), true);
|
|
471
|
+
} else if (ml < 16) {
|
|
472
|
+
for (let i = 0; i < ml; ++i) out[op + i] = out[src + i];
|
|
473
|
+
} else if (off >= ml) out.copyWithin(op, src, src + ml);
|
|
474
|
+
else {
|
|
475
|
+
// overlapping: repeat the period, each copy reading only written bytes
|
|
476
|
+
let d = op, r = ml;
|
|
477
|
+
while (r > 0) {
|
|
478
|
+
const k = r < d - src ? r : d - src;
|
|
479
|
+
out.copyWithin(d, src, src + k);
|
|
480
|
+
d += k, r -= k;
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
op += ml;
|
|
484
|
+
if (!ns) break;
|
|
485
|
+
}
|
|
486
|
+
if (pos != p0) err();
|
|
487
|
+
r0 = a, r1 = b, r2 = c;
|
|
488
|
+
const rl = le - lp;
|
|
489
|
+
if (rl < 0) err();
|
|
490
|
+
copyLit(lit, lp, out, op, rl);
|
|
491
|
+
op += rl;
|
|
492
|
+
if (op > re) err();
|
|
493
|
+
return op;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
// --------------------------------------------------------------------- XXH64
|
|
497
|
+
// 64-bit arithmetic on (high, low) int32 halves; results in H, L.
|
|
498
|
+
|
|
499
|
+
let H = 0, L = 0;
|
|
500
|
+
const STRIPE = new Int32Array(8);
|
|
501
|
+
const P1H = 0x9E3779B1 | 0, P1L = 0x85EBCA87 | 0, P2H = 0xC2B2AE3D | 0, P2L = 0x27D4EB4F | 0;
|
|
502
|
+
const P3H = 0x165667B1 | 0, P3L = 0x9E3779F9 | 0, P4H = 0x85EBCA77 | 0, P4L = 0xC2B2AE63 | 0;
|
|
503
|
+
const P5H = 0x27D4EB2F | 0, P5L = 0x165667C5 | 0;
|
|
504
|
+
|
|
505
|
+
function mul(ah, al, bh, bl) {
|
|
506
|
+
const a0 = al & 0xFFFF, a1 = al >>> 16, b0 = bl & 0xFFFF, b1 = bl >>> 16;
|
|
507
|
+
const p01 = Math.imul(a0, b1), p10 = Math.imul(a1, b0);
|
|
508
|
+
const mid = (Math.imul(a0, b0) >>> 16) + (p01 & 0xFFFF) + (p10 & 0xFFFF);
|
|
509
|
+
H = (Math.imul(a1, b1) + (p01 >>> 16) + (p10 >>> 16) + (mid >>> 16) + Math.imul(ah, bl) + Math.imul(al, bh)) | 0;
|
|
510
|
+
L = Math.imul(al, bl);
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
function add(ah, al, bh, bl) {
|
|
514
|
+
const l = (al + bl) | 0;
|
|
515
|
+
L = l;
|
|
516
|
+
H = (ah + bh + ((l >>> 0) < (al >>> 0) ? 1 : 0)) | 0;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
function rotl(h, l, r) {
|
|
520
|
+
H = (h << r) | (l >>> (32 - r));
|
|
521
|
+
L = (l << r) | (h >>> (32 - r));
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
// acc = rotl(acc + input * P2, 31) * P1
|
|
525
|
+
function round(ah, al, ih, il) {
|
|
526
|
+
mul(ih, il, P2H, P2L);
|
|
527
|
+
add(ah, al, H, L);
|
|
528
|
+
rotl(H, L, 31);
|
|
529
|
+
mul(H, L, P1H, P1L);
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
// h = (h ^ round(0, v)) * P1 + P4
|
|
533
|
+
function merge(hh, hl, vh, vl) {
|
|
534
|
+
round(0, 0, vh, vl);
|
|
535
|
+
mul(hh ^ H, hl ^ L, P1H, P1L);
|
|
536
|
+
add(H, L, P4H, P4L);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
// Low 32 bits of XXH64(b[s, e), seed 0), as the zstd frame checksum stores it.
|
|
540
|
+
// Uses the WebAssembly version when the environment allows it.
|
|
541
|
+
export function xxh64lo(b, s, e) {
|
|
542
|
+
const X = xxWasm();
|
|
543
|
+
if (!X) return xxh64js(b, s, e);
|
|
544
|
+
const n = e - s;
|
|
545
|
+
if (n > xxMem.length) X['memory'].grow(Math.ceil((n - xxMem.length) / 65536)), xxMem = new u8(X['memory'].buffer);
|
|
546
|
+
xxMem.set(b.subarray(s, e));
|
|
547
|
+
return X['h'](0, n) >>> 0;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
// XXH64 in plain JavaScript, for environments without WebAssembly.
|
|
551
|
+
export function xxh64js(b, s, e) {
|
|
552
|
+
const dv = new DataView(b.buffer, b.byteOffset, b.byteLength), len = e - s;
|
|
553
|
+
let hh, hl, p = s;
|
|
554
|
+
if (len >= 32) {
|
|
555
|
+
add(P1H, P1L, P2H, P2L);
|
|
556
|
+
let v1h = H, v1l = L, v2h = P2H, v2l = P2L, v3h = 0, v3l = 0;
|
|
557
|
+
add(~P1H, ~P1L, 0, 1);
|
|
558
|
+
let v4h = H, v4l = L;
|
|
559
|
+
// the stripe loop, rounds inlined: acc = rotl(acc + lane * P2, 31) * P1
|
|
560
|
+
const P2a = P2L & 0xFFFF, P2b = P2L >>> 16, P1a = P1L & 0xFFFF, P1b = P1L >>> 16;
|
|
561
|
+
const V = STRIPE;
|
|
562
|
+
V[0] = v1h, V[1] = v1l, V[2] = v2h, V[3] = v2l, V[4] = v3h, V[5] = v3l, V[6] = v4h, V[7] = v4l;
|
|
563
|
+
for (const lim = e - 32; p <= lim; p += 32) {
|
|
564
|
+
for (let j = 0; j < 8; j += 2) {
|
|
565
|
+
const il = dv.getInt32(p + (j << 2), true), ih = dv.getInt32(p + (j << 2) + 4, true);
|
|
566
|
+
// m = lane * P2
|
|
567
|
+
let a0 = il & 0xFFFF, a1 = il >>> 16, x = Math.imul(a0, P2b), y = Math.imul(a1, P2a);
|
|
568
|
+
let mid = (Math.imul(a0, P2a) >>> 16) + (x & 0xFFFF) + (y & 0xFFFF);
|
|
569
|
+
const mh = (Math.imul(a1, P2b) + (x >>> 16) + (y >>> 16) + (mid >>> 16) + Math.imul(ih, P2L) + Math.imul(il, P2H)) | 0;
|
|
570
|
+
const ml = Math.imul(il, P2L);
|
|
571
|
+
// acc += m
|
|
572
|
+
const ol = V[j + 1];
|
|
573
|
+
let vl = (ol + ml) | 0, vh = (V[j] + mh + ((vl >>> 0) < (ol >>> 0) ? 1 : 0)) | 0;
|
|
574
|
+
// rotl 31
|
|
575
|
+
const t = vh;
|
|
576
|
+
vh = (vh << 31) | (vl >>> 1), vl = (vl << 31) | (t >>> 1);
|
|
577
|
+
// acc *= P1
|
|
578
|
+
a0 = vl & 0xFFFF, a1 = vl >>> 16, x = Math.imul(a0, P1b), y = Math.imul(a1, P1a);
|
|
579
|
+
mid = (Math.imul(a0, P1a) >>> 16) + (x & 0xFFFF) + (y & 0xFFFF);
|
|
580
|
+
V[j] = (Math.imul(a1, P1b) + (x >>> 16) + (y >>> 16) + (mid >>> 16) + Math.imul(vh, P1L) + Math.imul(vl, P1H)) | 0;
|
|
581
|
+
V[j + 1] = Math.imul(vl, P1L);
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
v1h = V[0], v1l = V[1], v2h = V[2], v2l = V[3], v3h = V[4], v3l = V[5], v4h = V[6], v4l = V[7];
|
|
585
|
+
rotl(v1h, v1l, 1); hh = H, hl = L;
|
|
586
|
+
rotl(v2h, v2l, 7); add(hh, hl, H, L); hh = H, hl = L;
|
|
587
|
+
rotl(v3h, v3l, 12); add(hh, hl, H, L); hh = H, hl = L;
|
|
588
|
+
rotl(v4h, v4l, 18); add(hh, hl, H, L); hh = H, hl = L;
|
|
589
|
+
merge(hh, hl, v1h, v1l); hh = H, hl = L;
|
|
590
|
+
merge(hh, hl, v2h, v2l); hh = H, hl = L;
|
|
591
|
+
merge(hh, hl, v3h, v3l); hh = H, hl = L;
|
|
592
|
+
merge(hh, hl, v4h, v4l); hh = H, hl = L;
|
|
593
|
+
} else hh = P5H, hl = P5L;
|
|
594
|
+
add(hh, hl, Math.floor(len / 4294967296) | 0, len | 0); hh = H, hl = L;
|
|
595
|
+
for (; p + 8 <= e; p += 8) {
|
|
596
|
+
round(0, 0, dv.getInt32(p + 4, true), dv.getInt32(p, true));
|
|
597
|
+
rotl(hh ^ H, hl ^ L, 27);
|
|
598
|
+
mul(H, L, P1H, P1L);
|
|
599
|
+
add(H, L, P4H, P4L); hh = H, hl = L;
|
|
600
|
+
}
|
|
601
|
+
if (p + 4 <= e) {
|
|
602
|
+
mul(0, dv.getInt32(p, true), P1H, P1L);
|
|
603
|
+
rotl(hh ^ H, hl ^ L, 23);
|
|
604
|
+
mul(H, L, P2H, P2L);
|
|
605
|
+
add(H, L, P3H, P3L); hh = H, hl = L;
|
|
606
|
+
p += 4;
|
|
607
|
+
}
|
|
608
|
+
for (; p < e; ++p) {
|
|
609
|
+
mul(0, b[p], P5H, P5L);
|
|
610
|
+
rotl(hh ^ H, hl ^ L, 11);
|
|
611
|
+
mul(H, L, P1H, P1L); hh = H, hl = L;
|
|
612
|
+
}
|
|
613
|
+
hl ^= hh >>> 1;
|
|
614
|
+
mul(hh, hl, P2H, P2L); hh = H, hl = L;
|
|
615
|
+
hl ^= (hl >>> 29) | (hh << 3), hh ^= hh >>> 29;
|
|
616
|
+
mul(hh, hl, P3H, P3L); hh = H, hl = L;
|
|
617
|
+
return (hl ^ hh) >>> 0;
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
// XXH64 compiled to WebAssembly (837 bytes; source and build in xxh64/): 64-bit integer
|
|
621
|
+
// arithmetic at native speed. Compiled on first use; if WebAssembly is absent
|
|
622
|
+
// or forbidden (a content security policy), xxWasm returns null for good.
|
|
623
|
+
const XXH64_WASM = 'AGFzbQEAAAABBwFgAn9/AX8DAgEABAUBcAEBAQUDAQABBgcBfwFBgAgLBw4CBm1lbW9yeQIAAWgAAAqIBgGFBgQBfwF+AX8DfiAAIAFqIQJCxc/ZsvHluuonIQMCQCABQSBJDQAgAkFgaiEEQvnq0NDnyaHk4QAhA0IAIQVC1uuC7ur9ifXgACEGQs/W077Sx6vZQiEHA0ACQCAAIARNDQAgB0IHiSAGQgGJfCAFQgyJfCADQhKJfCAGQs/W077Sx6vZQn5CH4lCh5Wvr5i23puef36FQoeVr6+Ytt6bnn9+QuPcypX8zvL1hX98IAdCz9bTvtLHq9lCfkIfiUKHla+vmLbem55/foVCh5Wvr5i23puef35C49zKlfzO8vWFf3wgBULP1tO+0ser2UJ+Qh+JQoeVr6+Ytt6bnn9+hUKHla+vmLbem55/fkLj3MqV/M7y9YV/fCADQs/W077Sx6vZQn5CH4lCh5Wvr5i23puef36FQoeVr6+Ytt6bnn9+QuPcypX8zvL1hX98IQMMAgsgACkAAELP1tO+0ser2UJ+IAZ8Qh+JQoeVr6+Ytt6bnn9+IQYgAEEYaikAAELP1tO+0ser2UJ+IAN8Qh+JQoeVr6+Ytt6bnn9+IQMgAEEQaikAAELP1tO+0ser2UJ+IAV8Qh+JQoeVr6+Ytt6bnn9+IQUgAEEIaikAAELP1tO+0ser2UJ+IAd8Qh+JQoeVr6+Ytt6bnn9+IQcgAEEgaiEADAALCyADIAGtfCEDAkADQCAAQQhqIgQgAksNASAAKQAAQs/W077Sx6vZQn5CH4lCh5Wvr5i23puef34gA4VCG4lCh5Wvr5i23puef35C49zKlfzO8vWFf3whAyAEIQAMAAsLAkACQCAAQQRqIgQgAk0NACAAIQQMAQsgADUAAEKHla+vmLbem55/fiADhUIXiULP1tO+0ser2UJ+Qvnz3fGZ9pmrFnwhAwsCQANAIAQgAk8NASAEMQAAQsXP2bLx5brqJ34gA4VCC4lCh5Wvr5i23puef34hAyAEQQFqIQQMAAsLIANCIYggA4VCz9bTvtLHq9lCfiIDQh2IIAOFQvnz3fGZ9pmrFn4iA0IgiCADhacL';
|
|
624
|
+
let xxState = 0, xxExports = null, xxMem = null;
|
|
625
|
+
function xxWasm() {
|
|
626
|
+
if (xxState) return xxExports;
|
|
627
|
+
xxState = 1;
|
|
628
|
+
try {
|
|
629
|
+
const t = new u8(128), a = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/', s = XXH64_WASM;
|
|
630
|
+
for (let i = 0; i < 64; ++i) t[a.charCodeAt(i)] = i;
|
|
631
|
+
const n = s.length - (s.endsWith('==') ? 2 : s.endsWith('=') ? 1 : 0), w = new u8((n * 3) >> 2);
|
|
632
|
+
for (let i = 0, o = 0; i < n; ++i) {
|
|
633
|
+
const v = t[s.charCodeAt(i)], k = i & 3;
|
|
634
|
+
if (k == 1) w[o++] |= v >> 4, w[o] = v << 4;
|
|
635
|
+
else if (k == 2) w[o++] |= v >> 2, w[o] = v << 6;
|
|
636
|
+
else if (k == 3) w[o++] |= v;
|
|
637
|
+
else w[o] = v << 2;
|
|
638
|
+
}
|
|
639
|
+
const X = new WebAssembly.Instance(new WebAssembly.Module(w), {}).exports;
|
|
640
|
+
// export names quoted: a minifier must not rename them
|
|
641
|
+
if ((X['h'](0, 0) >>> 0) != 0x51D8E999) return null;
|
|
642
|
+
xxExports = X, xxMem = new u8(X['memory'].buffer);
|
|
643
|
+
} catch (e) {
|
|
644
|
+
xxExports = null;
|
|
645
|
+
}
|
|
646
|
+
return xxExports;
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
/** Which XXH64 implementation checksums use: 'wasm' or 'js'. */
|
|
650
|
+
export function xxh64Backend() {
|
|
651
|
+
return xxWasm() ? 'wasm' : 'js';
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
// --------------------------------------------------------------------- frames
|
|
655
|
+
|
|
656
|
+
const b4 = (d, b) => (d[b] | (d[b + 1] << 8) | (d[b + 2] << 16) | (d[b + 3] << 24)) >>> 0;
|
|
657
|
+
|
|
658
|
+
function grow(out, need) {
|
|
659
|
+
const n = new u8(Math.max(need, out.length * 2));
|
|
660
|
+
n.set(out);
|
|
661
|
+
return n;
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
/**
|
|
665
|
+
* Decompresses zstd data: one or more frames, skippable frames ignored.
|
|
666
|
+
* @param {Uint8Array} dat compressed bytes
|
|
667
|
+
* @param {Uint8Array} [buf] where to decompress to; must be large enough
|
|
668
|
+
* @param {boolean} [verify=true] check frame checksums where present
|
|
669
|
+
* @returns {Uint8Array} the decompressed bytes (a view of buf if given)
|
|
670
|
+
*/
|
|
671
|
+
export function decompress(dat, buf, verify = true) {
|
|
672
|
+
try {
|
|
673
|
+
return decompressFrames(dat, buf, verify);
|
|
674
|
+
} catch (e) {
|
|
675
|
+
// a corrupt input can still index past a buffer
|
|
676
|
+
if (e instanceof RangeError) err();
|
|
677
|
+
throw e;
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
function decompressFrames(dat, buf, verify) {
|
|
682
|
+
const len = dat.length, dv = new DataView(dat.buffer, dat.byteOffset, len);
|
|
683
|
+
let out = buf || null, ov = null, op = 0, bt = 0;
|
|
684
|
+
while (bt < len) {
|
|
685
|
+
if (len - bt < 8) err('unexpected EOF');
|
|
686
|
+
const magic = b4(dat, bt);
|
|
687
|
+
if ((magic >>> 4) == 0x184D2A5) {
|
|
688
|
+
bt += 8 + b4(dat, bt + 4);
|
|
689
|
+
if (bt > len) err('unexpected EOF');
|
|
690
|
+
continue;
|
|
691
|
+
}
|
|
692
|
+
if (magic != 0xFD2FB528) err();
|
|
693
|
+
const fhd = dat[bt + 4];
|
|
694
|
+
bt += 5;
|
|
695
|
+
if (fhd & 8) err();
|
|
696
|
+
const ss = (fhd >> 5) & 1, cc = (fhd >> 2) & 1, dif = fhd & 3, fcf = fhd >> 6;
|
|
697
|
+
let ws = 0;
|
|
698
|
+
if (!ss) {
|
|
699
|
+
const wd = dat[bt++], e = 10 + (wd >> 3);
|
|
700
|
+
if (e > 30) err('window size too large');
|
|
701
|
+
ws = (1 << e) + (1 << (e - 3)) * (wd & 7);
|
|
702
|
+
}
|
|
703
|
+
if (dif) {
|
|
704
|
+
const n = dif == 3 ? 4 : dif;
|
|
705
|
+
let id = 0;
|
|
706
|
+
for (let k = 0; k < n; ++k) id |= dat[bt + k] << (k << 3);
|
|
707
|
+
bt += n;
|
|
708
|
+
if (id) err('dictionaries are not supported');
|
|
709
|
+
}
|
|
710
|
+
let fcs = -1;
|
|
711
|
+
if (fcf == 0) { if (ss) fcs = dat[bt++]; }
|
|
712
|
+
else if (fcf == 1) fcs = (dat[bt] | (dat[bt + 1] << 8)) + 256, bt += 2;
|
|
713
|
+
else if (fcf == 2) fcs = b4(dat, bt), bt += 4;
|
|
714
|
+
else fcs = b4(dat, bt) + b4(dat, bt + 4) * 4294967296, bt += 8;
|
|
715
|
+
if (ss) ws = fcs;
|
|
716
|
+
const bmax = Math.min(ws, 131072);
|
|
717
|
+
fs0 = op, r0 = 1, r1 = 4, r2 = 8, hufOk = false, seqOk = false;
|
|
718
|
+
let fend = -1;
|
|
719
|
+
if (fcs >= 0) {
|
|
720
|
+
fend = op + fcs;
|
|
721
|
+
if (!out) out = new u8(fcs);
|
|
722
|
+
else if (fend > out.length) {
|
|
723
|
+
if (buf) err('output buffer too small');
|
|
724
|
+
out = grow(out, fend);
|
|
725
|
+
}
|
|
726
|
+
} else if (!out) out = new u8(Math.max(bmax, 1 << 16));
|
|
727
|
+
for (;;) {
|
|
728
|
+
if (bt + 3 > len) err('unexpected EOF');
|
|
729
|
+
const h = dat[bt] | (dat[bt + 1] << 8) | (dat[bt + 2] << 16), bs = h >>> 3, type = (h >> 1) & 3;
|
|
730
|
+
bt += 3;
|
|
731
|
+
if (type == 3) err('invalid block type');
|
|
732
|
+
const csz = type == 1 ? 1 : bs;
|
|
733
|
+
if (bt + csz > len) err('unexpected EOF');
|
|
734
|
+
let re = op + bmax;
|
|
735
|
+
if (fend >= 0) { if (re > fend) re = fend; }
|
|
736
|
+
else if (re > out.length) {
|
|
737
|
+
if (buf) re = out.length;
|
|
738
|
+
else out = grow(out, re);
|
|
739
|
+
}
|
|
740
|
+
if (type == 2) {
|
|
741
|
+
if (!ov || ov.buffer !== out.buffer) ov = new DataView(out.buffer, out.byteOffset, out.length);
|
|
742
|
+
op = decodeBlock(dat, dv, bt, bt + bs, out, ov, op, re);
|
|
743
|
+
}
|
|
744
|
+
else {
|
|
745
|
+
if (op + bs > re) err();
|
|
746
|
+
if (type == 0) out.set(dat.subarray(bt, bt + bs), op);
|
|
747
|
+
else out.fill(dat[bt], op, op + bs);
|
|
748
|
+
op += bs;
|
|
749
|
+
}
|
|
750
|
+
bt += csz;
|
|
751
|
+
if (h & 1) break;
|
|
752
|
+
}
|
|
753
|
+
if (fend >= 0 && op != fend) err('content size mismatch');
|
|
754
|
+
if (cc) {
|
|
755
|
+
if (bt + 4 > len) err('unexpected EOF');
|
|
756
|
+
if (verify && xxh64lo(out, fs0, op) != b4(dat, bt)) err('checksum mismatch');
|
|
757
|
+
bt += 4;
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
if (!out) return new u8(0);
|
|
761
|
+
return buf ? out.subarray(0, op) : op == out.length ? out : out.slice(0, op);
|
|
762
|
+
}
|