driftnet 0.4.1 → 0.5.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/public/qr.js ADDED
@@ -0,0 +1,170 @@
1
+ // A small QR code encoder: byte mode, error correction M, versions 1-10 (up to 213 bytes).
2
+ // Enough for a link with a one-time code; no dependencies, nothing leaves the page.
3
+ // qrMatrix(text) -> array of rows of booleans (true = dark), without the quiet zone.
4
+ // qrSvg(text, { scale, margin }) -> an <svg> string.
5
+
6
+ // Per version 1-10: [ecc codewords per block, [blocks, data codewords per block], ...]
7
+ const BLOCKS = [
8
+ [10, [1, 16]], [16, [1, 28]], [26, [1, 44]], [18, [2, 32]], [24, [2, 43]],
9
+ [16, [4, 27]], [18, [4, 31]], [22, [2, 38], [2, 39]], [22, [3, 36], [2, 37]], [26, [4, 43], [1, 44]],
10
+ ];
11
+ const ALIGN = [[], [6, 18], [6, 22], [6, 26], [6, 30], [6, 34], [6, 22, 38], [6, 24, 42], [6, 26, 46], [6, 28, 50]];
12
+
13
+ const dataWords = (v) => BLOCKS[v - 1].slice(1).reduce((n, [b, d]) => n + b * d, 0);
14
+ export const capacity = (v) => dataWords(v) - (v < 10 ? 2 : 3);
15
+
16
+ // GF(256) with the polynomial 0x11D.
17
+ const EXP = new Uint8Array(512), LOG = new Uint8Array(256);
18
+ for (let i = 0, x = 1; i < 255; i++) { EXP[i] = x; LOG[x] = i; x <<= 1; if (x & 256) x ^= 0x11d; }
19
+ for (let i = 255; i < 512; i++) EXP[i] = EXP[i - 255];
20
+ const mul = (a, b) => (a && b ? EXP[LOG[a] + LOG[b]] : 0);
21
+
22
+ function eccOf(data, n) {
23
+ let gen = [1];
24
+ for (let i = 0; i < n; i++) {
25
+ const next = new Array(gen.length + 1).fill(0);
26
+ gen.forEach((c, j) => { next[j] ^= c; next[j + 1] ^= mul(c, EXP[i]); });
27
+ gen = next;
28
+ }
29
+ const rem = new Array(n).fill(0);
30
+ for (const d of data) {
31
+ const f = d ^ rem.shift();
32
+ rem.push(0);
33
+ if (f) gen.slice(1).forEach((g, j) => { rem[j] ^= mul(g, f); });
34
+ }
35
+ return rem;
36
+ }
37
+
38
+ function codewords(bytes, v) {
39
+ const bits = [];
40
+ const put = (val, len) => { for (let i = len - 1; i >= 0; i--) bits.push((val >>> i) & 1); };
41
+ put(0b0100, 4);
42
+ put(bytes.length, v < 10 ? 8 : 16);
43
+ bytes.forEach((b) => put(b, 8));
44
+ const total = dataWords(v) * 8;
45
+ put(0, Math.min(4, total - bits.length));
46
+ while (bits.length % 8) bits.push(0);
47
+ for (let pad = 0xec; bits.length < total; pad ^= 0xec ^ 0x11) put(pad, 8);
48
+ const data = [];
49
+ for (let i = 0; i < bits.length; i += 8) data.push(parseInt(bits.slice(i, i + 8).join(""), 2));
50
+ // Split into blocks, add ECC to each, interleave.
51
+ const [eccLen, ...groups] = BLOCKS[v - 1];
52
+ const blocks = [];
53
+ let at = 0;
54
+ for (const [count, len] of groups) for (let i = 0; i < count; i++) { const d = data.slice(at, at + len); at += len; blocks.push({ d, e: eccOf(d, eccLen) }); }
55
+ const out = [];
56
+ for (let i = 0; i < Math.max(...blocks.map((b) => b.d.length)); i++) blocks.forEach((b) => { if (i < b.d.length) out.push(b.d[i]); });
57
+ for (let i = 0; i < eccLen; i++) blocks.forEach((b) => out.push(b.e[i]));
58
+ return out;
59
+ }
60
+
61
+ const MASKS = [
62
+ (r, c) => (r + c) % 2 === 0,
63
+ (r) => r % 2 === 0,
64
+ (r, c) => c % 3 === 0,
65
+ (r, c) => (r + c) % 3 === 0,
66
+ (r, c) => (Math.floor(r / 2) + Math.floor(c / 3)) % 2 === 0,
67
+ (r, c) => ((r * c) % 2) + ((r * c) % 3) === 0,
68
+ (r, c) => (((r * c) % 2) + ((r * c) % 3)) % 2 === 0,
69
+ (r, c) => (((r + c) % 2) + ((r * c) % 3)) % 2 === 0,
70
+ ];
71
+
72
+ function bch(value, poly, bitsLen) {
73
+ let r = value << bitsLen;
74
+ for (let i = 31 - Math.clz32(r); i >= bitsLen; i--) if ((r >>> i) & 1) r ^= poly << (i - bitsLen);
75
+ return (value << bitsLen) | r;
76
+ }
77
+
78
+ function build(words, v, mask) {
79
+ const n = 17 + 4 * v;
80
+ const m = Array.from({ length: n }, () => new Array(n).fill(false));
81
+ const fn = Array.from({ length: n }, () => new Array(n).fill(false)); // function modules
82
+ const set = (r, c, dark) => { if (r >= 0 && c >= 0 && r < n && c < n) { m[r][c] = dark; fn[r][c] = true; } };
83
+ const finder = (r0, c0) => {
84
+ for (let dr = -1; dr <= 7; dr++) for (let dc = -1; dc <= 7; dc++) {
85
+ const d = Math.max(Math.abs(dr - 3), Math.abs(dc - 3));
86
+ set(r0 + dr, c0 + dc, d !== 2 && d !== 4);
87
+ }
88
+ };
89
+ finder(0, 0); finder(0, n - 7); finder(n - 7, 0);
90
+ for (let i = 8; i < n - 8; i++) { set(6, i, i % 2 === 0); set(i, 6, i % 2 === 0); }
91
+ const a = ALIGN[v - 1];
92
+ for (const r of a) for (const c of a) {
93
+ if ((r === 6 && c === 6) || (r === 6 && c === a[a.length - 1]) || (r === a[a.length - 1] && c === 6)) continue;
94
+ for (let dr = -2; dr <= 2; dr++) for (let dc = -2; dc <= 2; dc++) set(r + dr, c + dc, Math.max(Math.abs(dr), Math.abs(dc)) !== 1);
95
+ }
96
+ // Format (ECC M = 00) and version information, then the always-dark module.
97
+ const fmt = bch((0b00 << 3) | mask, 0x537, 10) ^ 0x5412;
98
+ const f = (i) => ((fmt >>> i) & 1) === 1;
99
+ for (let i = 0; i <= 5; i++) set(i, 8, f(i));
100
+ set(7, 8, f(6)); set(8, 8, f(7)); set(8, 7, f(8));
101
+ for (let i = 9; i < 15; i++) set(8, 14 - i, f(i));
102
+ for (let i = 0; i < 8; i++) set(8, n - 1 - i, f(i));
103
+ for (let i = 8; i < 15; i++) set(n - 15 + i, 8, f(i));
104
+ set(n - 8, 8, true);
105
+ if (v >= 7) {
106
+ const ver = bch(v, 0x1f25, 12);
107
+ for (let i = 0; i < 18; i++) {
108
+ const bit = ((ver >>> i) & 1) === 1, x = n - 11 + (i % 3), y = Math.floor(i / 3);
109
+ set(y, x, bit); set(x, y, bit);
110
+ }
111
+ }
112
+ // Data, zigzag from the bottom right, skipping the vertical timing column.
113
+ const bits = [];
114
+ words.forEach((w) => { for (let i = 7; i >= 0; i--) bits.push((w >>> i) & 1); });
115
+ let k = 0;
116
+ for (let right = n - 1; right >= 1; right -= 2) {
117
+ if (right === 6) right = 5;
118
+ for (let j = 0; j < n; j++) for (let x = 0; x < 2; x++) {
119
+ const c = right - x, up = ((right + 1) & 2) === 0, r = up ? n - 1 - j : j;
120
+ if (fn[r][c]) continue;
121
+ const bit = k < bits.length && bits[k++] === 1;
122
+ m[r][c] = bit !== MASKS[mask](r, c);
123
+ }
124
+ }
125
+ return m;
126
+ }
127
+
128
+ function penalty(m) {
129
+ const n = m.length;
130
+ let p = 0;
131
+ const line = (get) => {
132
+ for (let i = 0; i < n; i++) {
133
+ let run = 1;
134
+ for (let j = 1; j < n; j++) {
135
+ if (get(i, j) === get(i, j - 1)) { run++; if (j === n - 1 && run >= 5) p += run - 2; }
136
+ else { if (run >= 5) p += run - 2; run = 1; }
137
+ }
138
+ // Finder-like 1:1:3:1:1 pattern with four light modules on a side.
139
+ const s = Array.from({ length: n }, (_, j) => (get(i, j) ? 1 : 0)).join("");
140
+ for (const pat of ["10111010000", "00001011101"]) for (let at = s.indexOf(pat); at >= 0; at = s.indexOf(pat, at + 1)) p += 40;
141
+ }
142
+ };
143
+ line((r, c) => m[r][c]);
144
+ line((r, c) => m[c][r]);
145
+ for (let r = 0; r < n - 1; r++) for (let c = 0; c < n - 1; c++) if (m[r][c] === m[r][c + 1] && m[r][c] === m[r + 1][c] && m[r][c] === m[r + 1][c + 1]) p += 3;
146
+ const dark = m.flat().filter(Boolean).length;
147
+ p += Math.floor(Math.abs((dark * 20) / (n * n) - 10)) * 10;
148
+ return p;
149
+ }
150
+
151
+ /** Smallest version that holds the text; throws when it does not fit in version 10. */
152
+ export function qrMatrix(text) {
153
+ const bytes = [...new TextEncoder().encode(text)];
154
+ const v = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].find((x) => bytes.length <= capacity(x));
155
+ if (!v) throw new Error("text too long for the QR encoder");
156
+ const words = codewords(bytes, v);
157
+ let best = null, bestScore = Infinity;
158
+ for (let mask = 0; mask < 8; mask++) {
159
+ const m = build(words, v, mask), s = penalty(m);
160
+ if (s < bestScore) { best = m; bestScore = s; }
161
+ }
162
+ return best;
163
+ }
164
+
165
+ export function qrSvg(text, { scale = 6, margin = 4 } = {}) {
166
+ const m = qrMatrix(text), n = m.length + margin * 2;
167
+ let d = "";
168
+ m.forEach((row, r) => row.forEach((dark, c) => { if (dark) d += `M${c + margin} ${r + margin}h1v1h-1z`; }));
169
+ return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${n} ${n}" width="${n * scale}" height="${n * scale}" shape-rendering="crispEdges" role="img"><rect width="${n}" height="${n}" fill="#fff"/><path d="${d}" fill="#000"/></svg>`;
170
+ }