oculon 0.1.7 → 0.2.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 +73 -13
- package/dist/cli/main.js +316 -1
- package/dist/core/der.d.ts +2 -0
- package/dist/core/der.js +17 -0
- package/dist/core/http.d.ts +19 -1
- package/dist/core/http.js +24 -0
- package/dist/core/qr.d.ts +23 -0
- package/dist/core/qr.js +577 -0
- package/dist/core/quota.js +5 -0
- package/dist/core/raster.d.ts +6 -0
- package/dist/core/raster.js +298 -0
- package/dist/core/safeurl.d.ts +5 -0
- package/dist/core/safeurl.js +26 -0
- package/dist/core/verdict.js +1 -1
- package/dist/core/zip.d.ts +9 -0
- package/dist/core/zip.js +46 -0
- package/dist/index.d.ts +61 -0
- package/dist/index.js +99 -0
- package/dist/mcp.js +131 -5
- package/dist/modules/address.d.ts +6 -2
- package/dist/modules/address.js +32 -5
- package/dist/modules/apk.d.ts +37 -0
- package/dist/modules/apk.js +191 -0
- package/dist/modules/approvals.d.ts +40 -0
- package/dist/modules/approvals.js +155 -0
- package/dist/modules/archive.d.ts +21 -0
- package/dist/modules/archive.js +94 -0
- package/dist/modules/checks.js +33 -0
- package/dist/modules/contract.d.ts +56 -0
- package/dist/modules/contract.js +167 -0
- package/dist/modules/deployer.d.ts +42 -0
- package/dist/modules/deployer.js +90 -0
- package/dist/modules/doc.d.ts +24 -2
- package/dist/modules/doc.js +133 -7
- package/dist/modules/eml.d.ts +46 -0
- package/dist/modules/eml.js +175 -0
- package/dist/modules/hashcheck.d.ts +19 -3
- package/dist/modules/hashcheck.js +28 -4
- package/dist/modules/image.d.ts +32 -0
- package/dist/modules/image.js +276 -0
- package/dist/modules/invite.d.ts +27 -0
- package/dist/modules/invite.js +69 -0
- package/dist/modules/job.d.ts +33 -0
- package/dist/modules/job.js +64 -0
- package/dist/modules/meerkat.d.ts +1 -1
- package/dist/modules/meerkat.js +35 -2
- package/dist/modules/monitor.js +14 -0
- package/dist/modules/poisoning.d.ts +43 -0
- package/dist/modules/poisoning.js +114 -0
- package/dist/modules/qrcheck.d.ts +24 -0
- package/dist/modules/qrcheck.js +136 -0
- package/dist/modules/repo.d.ts +61 -0
- package/dist/modules/repo.js +162 -0
- package/dist/modules/report.d.ts +32 -0
- package/dist/modules/report.js +108 -0
- package/dist/modules/signature.d.ts +35 -0
- package/dist/modules/signature.js +283 -0
- package/dist/modules/site.d.ts +62 -0
- package/dist/modules/site.js +191 -0
- package/dist/modules/stocktoken.d.ts +48 -0
- package/dist/modules/stocktoken.js +99 -0
- package/dist/modules/trace.d.ts +37 -0
- package/dist/modules/trace.js +150 -0
- package/dist/modules/txexplain.d.ts +40 -0
- package/dist/modules/txexplain.js +135 -0
- package/dist/modules/urlcheck.js +15 -1
- package/dist/modules/where.d.ts +63 -0
- package/dist/modules/where.js +165 -0
- package/dist/report-pdf.js +3 -0
- package/dist/sources/cfurlscan.d.ts +24 -0
- package/dist/sources/cfurlscan.js +46 -0
- package/dist/sources/chainabuse.d.ts +17 -0
- package/dist/sources/chainabuse.js +19 -0
- package/dist/sources/evm.d.ts +135 -0
- package/dist/sources/evm.js +260 -0
- package/dist/sources/phishlists.d.ts +20 -0
- package/dist/sources/phishlists.js +90 -0
- package/dist/telegram.d.ts +17 -0
- package/dist/telegram.js +151 -4
- package/dist/web/docs.html +1 -1
- package/dist/web/index.html +147 -11
- package/dist/web/landing.html +1 -1
- package/dist/web/legal.html +3 -1
- package/dist/web/mcp.html +1 -1
- package/dist/web/openapi.js +13 -0
- package/dist/web/pages.js +2 -2
- package/dist/web/public.js +14 -1
- package/dist/web/server.d.ts +13 -0
- package/dist/web/server.js +77 -12
- package/dist/web/token.html +24 -4
- package/dist/web/token.json +12 -1
- package/package.json +2 -2
package/dist/core/qr.js
ADDED
|
@@ -0,0 +1,577 @@
|
|
|
1
|
+
/* ---------- Galois field and Reed-Solomon (generator base 0, as QR uses) ---------- */
|
|
2
|
+
const EXP = new Uint8Array(512), LOG = new Uint8Array(256);
|
|
3
|
+
{
|
|
4
|
+
let x = 1;
|
|
5
|
+
for (let i = 0; i < 255; i++) {
|
|
6
|
+
EXP[i] = x;
|
|
7
|
+
LOG[x] = i;
|
|
8
|
+
x <<= 1;
|
|
9
|
+
if (x & 0x100)
|
|
10
|
+
x ^= 0x11d;
|
|
11
|
+
}
|
|
12
|
+
for (let i = 255; i < 512; i++)
|
|
13
|
+
EXP[i] = EXP[i - 255];
|
|
14
|
+
}
|
|
15
|
+
const mul = (a, b) => (a && b ? EXP[LOG[a] + LOG[b]] : 0);
|
|
16
|
+
const div = (a, b) => (a ? EXP[(LOG[a] + 255 - LOG[b]) % 255] : 0);
|
|
17
|
+
const pow = (x, p) => EXP[(((LOG[x] * p) % 255) + 255) % 255];
|
|
18
|
+
const inv = (x) => EXP[255 - LOG[x]];
|
|
19
|
+
const pEval = (p, x) => { let y = p[0]; for (let i = 1; i < p.length; i++)
|
|
20
|
+
y = mul(y, x) ^ p[i]; return y; };
|
|
21
|
+
const pScale = (p, x) => p.map((c) => mul(c, x));
|
|
22
|
+
const pAdd = (p, q) => { const r = new Array(Math.max(p.length, q.length)).fill(0); p.forEach((c, i) => { r[i + r.length - p.length] = c; }); q.forEach((c, i) => { r[i + r.length - q.length] ^= c; }); return r; };
|
|
23
|
+
const pMul = (p, q) => { const r = new Array(p.length + q.length - 1).fill(0); for (let j = 0; j < q.length; j++)
|
|
24
|
+
for (let i = 0; i < p.length; i++)
|
|
25
|
+
r[i + j] ^= mul(p[i], q[j]); return r; };
|
|
26
|
+
function pDivRem(a, d) {
|
|
27
|
+
const out = [...a];
|
|
28
|
+
for (let i = 0; i < a.length - (d.length - 1); i++) {
|
|
29
|
+
const c = out[i];
|
|
30
|
+
if (c)
|
|
31
|
+
for (let j = 1; j < d.length; j++)
|
|
32
|
+
if (d[j])
|
|
33
|
+
out[i + j] ^= mul(d[j], c);
|
|
34
|
+
}
|
|
35
|
+
return out.slice(-(d.length - 1));
|
|
36
|
+
}
|
|
37
|
+
/** Corrects a block in place (data then EC codewords). Returns false when there are too many errors. */
|
|
38
|
+
export function rsCorrect(msg, nsym) {
|
|
39
|
+
const synd = [0, ...Array.from({ length: nsym }, (_, i) => pEval(msg, pow(2, i)))];
|
|
40
|
+
if (synd.every((s) => s === 0))
|
|
41
|
+
return true;
|
|
42
|
+
// Berlekamp-Massey.
|
|
43
|
+
let loc = [1], old = [1];
|
|
44
|
+
for (let i = 0; i < nsym; i++) {
|
|
45
|
+
const K = i + 1;
|
|
46
|
+
let delta = synd[K];
|
|
47
|
+
for (let j = 1; j < loc.length; j++)
|
|
48
|
+
delta ^= mul(loc[loc.length - 1 - j], synd[K - j]);
|
|
49
|
+
old = [...old, 0];
|
|
50
|
+
if (delta) {
|
|
51
|
+
if (old.length > loc.length) {
|
|
52
|
+
const nl = pScale(old, delta);
|
|
53
|
+
old = pScale(loc, inv(delta));
|
|
54
|
+
loc = nl;
|
|
55
|
+
}
|
|
56
|
+
loc = pAdd(loc, pScale(old, delta));
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
while (loc.length && loc[0] === 0)
|
|
60
|
+
loc.shift();
|
|
61
|
+
const errs = loc.length - 1;
|
|
62
|
+
if (errs * 2 > nsym)
|
|
63
|
+
return false;
|
|
64
|
+
// Chien search.
|
|
65
|
+
const rev = [...loc].reverse();
|
|
66
|
+
const pos = [];
|
|
67
|
+
for (let i = 0; i < msg.length; i++)
|
|
68
|
+
if (pEval(rev, pow(2, i)) === 0)
|
|
69
|
+
pos.push(msg.length - 1 - i);
|
|
70
|
+
if (pos.length !== errs)
|
|
71
|
+
return false;
|
|
72
|
+
// Forney.
|
|
73
|
+
const coef = pos.map((p) => msg.length - 1 - p);
|
|
74
|
+
let eLoc = [1];
|
|
75
|
+
for (const c of coef)
|
|
76
|
+
eLoc = pMul(eLoc, pAdd([1], [pow(2, c), 0]));
|
|
77
|
+
const sr = [...synd].reverse();
|
|
78
|
+
const eEval = pDivRem(pMul(sr, eLoc), [1, ...new Array(eLoc.length).fill(0)]).reverse();
|
|
79
|
+
const X = coef.map((c) => pow(2, c));
|
|
80
|
+
for (let i = 0; i < X.length; i++) {
|
|
81
|
+
const xi = inv(X[i]);
|
|
82
|
+
let prime = 1;
|
|
83
|
+
for (let j = 0; j < X.length; j++)
|
|
84
|
+
if (j !== i)
|
|
85
|
+
prime = mul(prime, 1 ^ mul(xi, X[j]));
|
|
86
|
+
if (!prime)
|
|
87
|
+
return false;
|
|
88
|
+
const y = mul(X[i], pEval([...eEval].reverse(), xi));
|
|
89
|
+
msg[pos[i]] ^= div(y, prime);
|
|
90
|
+
}
|
|
91
|
+
return Array.from({ length: nsym }, (_, i) => pEval(msg, pow(2, i))).every((s) => s === 0);
|
|
92
|
+
}
|
|
93
|
+
/** Per version: EC codewords per block and (block count, data codewords) groups, for L, M, Q, H. */
|
|
94
|
+
export const EC = {
|
|
95
|
+
1: [[7, [[1, 19]]], [10, [[1, 16]]], [13, [[1, 13]]], [17, [[1, 9]]]],
|
|
96
|
+
2: [[10, [[1, 34]]], [16, [[1, 28]]], [22, [[1, 22]]], [28, [[1, 16]]]],
|
|
97
|
+
3: [[15, [[1, 55]]], [26, [[1, 44]]], [18, [[2, 17]]], [22, [[2, 13]]]],
|
|
98
|
+
4: [[20, [[1, 80]]], [18, [[2, 32]]], [26, [[2, 24]]], [16, [[4, 9]]]],
|
|
99
|
+
5: [[26, [[1, 108]]], [24, [[2, 43]]], [18, [[2, 15], [2, 16]]], [22, [[2, 11], [2, 12]]]],
|
|
100
|
+
6: [[18, [[2, 68]]], [16, [[4, 27]]], [24, [[4, 19]]], [28, [[4, 15]]]],
|
|
101
|
+
7: [[20, [[2, 78]]], [18, [[4, 31]]], [18, [[2, 14], [4, 15]]], [26, [[4, 13], [1, 14]]]],
|
|
102
|
+
8: [[24, [[2, 97]]], [22, [[2, 38], [2, 39]]], [22, [[4, 18], [2, 19]]], [26, [[4, 14], [2, 15]]]],
|
|
103
|
+
9: [[30, [[2, 116]]], [22, [[3, 36], [2, 37]]], [20, [[4, 16], [4, 17]]], [24, [[4, 12], [4, 13]]]],
|
|
104
|
+
10: [[18, [[2, 68], [2, 69]]], [26, [[4, 43], [1, 44]]], [24, [[6, 19], [2, 20]]], [28, [[6, 15], [2, 16]]]],
|
|
105
|
+
11: [[20, [[4, 81]]], [30, [[1, 50], [4, 51]]], [28, [[4, 22], [4, 23]]], [24, [[3, 12], [8, 13]]]],
|
|
106
|
+
12: [[24, [[2, 92], [2, 93]]], [22, [[6, 36], [2, 37]]], [26, [[4, 20], [6, 21]]], [28, [[7, 14], [4, 15]]]],
|
|
107
|
+
13: [[26, [[4, 107]]], [22, [[8, 37], [1, 38]]], [24, [[8, 20], [4, 21]]], [22, [[12, 11], [4, 12]]]],
|
|
108
|
+
14: [[30, [[3, 115], [1, 116]]], [24, [[4, 40], [5, 41]]], [20, [[11, 16], [5, 17]]], [24, [[11, 12], [5, 13]]]],
|
|
109
|
+
15: [[22, [[5, 87], [1, 88]]], [24, [[5, 41], [5, 42]]], [30, [[5, 24], [7, 25]]], [24, [[11, 12], [7, 13]]]],
|
|
110
|
+
};
|
|
111
|
+
export const ALIGN = { 1: [], 2: [6, 18], 3: [6, 22], 4: [6, 26], 5: [6, 30], 6: [6, 34], 7: [6, 22, 38], 8: [6, 24, 42], 9: [6, 26, 46], 10: [6, 28, 50], 11: [6, 30, 54], 12: [6, 32, 58], 13: [6, 34, 62], 14: [6, 26, 46, 66], 15: [6, 26, 48, 70] };
|
|
112
|
+
/** Modules that hold function patterns (not data) for a version. */
|
|
113
|
+
export function functionMask(version) {
|
|
114
|
+
const n = 17 + 4 * version, f = new Uint8Array(n * n);
|
|
115
|
+
const set = (x, y, w, h) => { for (let j = y; j < y + h; j++)
|
|
116
|
+
for (let i = x; i < x + w; i++)
|
|
117
|
+
if (i >= 0 && j >= 0 && i < n && j < n)
|
|
118
|
+
f[j * n + i] = 1; };
|
|
119
|
+
set(0, 0, 9, 9);
|
|
120
|
+
set(n - 8, 0, 8, 9);
|
|
121
|
+
set(0, n - 8, 9, 8);
|
|
122
|
+
set(6, 0, 1, n);
|
|
123
|
+
set(0, 6, n, 1);
|
|
124
|
+
const a = ALIGN[version];
|
|
125
|
+
for (const ay of a)
|
|
126
|
+
for (const ax of a) {
|
|
127
|
+
if ((ax === 6 && ay === 6) || (ax === 6 && ay === a[a.length - 1]) || (ay === 6 && ax === a[a.length - 1]))
|
|
128
|
+
continue;
|
|
129
|
+
set(ax - 2, ay - 2, 5, 5);
|
|
130
|
+
}
|
|
131
|
+
if (version >= 7) {
|
|
132
|
+
set(n - 11, 0, 3, 6);
|
|
133
|
+
set(0, n - 11, 6, 3);
|
|
134
|
+
}
|
|
135
|
+
return f;
|
|
136
|
+
}
|
|
137
|
+
const FORMAT = (() => {
|
|
138
|
+
const out = [];
|
|
139
|
+
for (let d = 0; d < 32; d++) {
|
|
140
|
+
let r = d << 10;
|
|
141
|
+
for (let i = 14; i >= 10; i--)
|
|
142
|
+
if (r & (1 << i))
|
|
143
|
+
r ^= 0x537 << (i - 10);
|
|
144
|
+
out.push(((d << 10) | r) ^ 0x5412);
|
|
145
|
+
}
|
|
146
|
+
return out;
|
|
147
|
+
})();
|
|
148
|
+
const MASKS = [
|
|
149
|
+
(i, j) => (i + j) % 2 === 0, (i) => i % 2 === 0, (_i, j) => j % 3 === 0, (i, j) => (i + j) % 3 === 0,
|
|
150
|
+
(i, j) => (Math.floor(i / 2) + Math.floor(j / 3)) % 2 === 0, (i, j) => ((i * j) % 2) + ((i * j) % 3) === 0,
|
|
151
|
+
(i, j) => (((i * j) % 2) + ((i * j) % 3)) % 2 === 0, (i, j) => (((i + j) % 2) + ((i * j) % 3)) % 2 === 0,
|
|
152
|
+
];
|
|
153
|
+
/** Decodes a sampled module grid (1 = dark), row-major. */
|
|
154
|
+
export function decodeGrid(grid, n) {
|
|
155
|
+
const version = (n - 17) / 4;
|
|
156
|
+
if (!EC[version])
|
|
157
|
+
return null;
|
|
158
|
+
const g = (x, y) => grid[y * n + x];
|
|
159
|
+
let a = 0, b = 0;
|
|
160
|
+
for (let i = 0; i <= 5; i++)
|
|
161
|
+
a = (a << 1) | g(i, 8);
|
|
162
|
+
a = (a << 1) | g(7, 8);
|
|
163
|
+
a = (a << 1) | g(8, 8);
|
|
164
|
+
a = (a << 1) | g(8, 7);
|
|
165
|
+
for (let j = 5; j >= 0; j--)
|
|
166
|
+
a = (a << 1) | g(8, j);
|
|
167
|
+
for (let j = n - 1; j >= n - 7; j--)
|
|
168
|
+
b = (b << 1) | g(8, j);
|
|
169
|
+
for (let i = n - 8; i < n; i++)
|
|
170
|
+
b = (b << 1) | g(i, 8);
|
|
171
|
+
let best = -1, bestD = 99;
|
|
172
|
+
for (let d = 0; d < 32; d++)
|
|
173
|
+
for (const v of [a, b]) {
|
|
174
|
+
let x = v ^ FORMAT[d], c = 0;
|
|
175
|
+
while (x) {
|
|
176
|
+
c += x & 1;
|
|
177
|
+
x >>= 1;
|
|
178
|
+
}
|
|
179
|
+
if (c < bestD) {
|
|
180
|
+
bestD = c;
|
|
181
|
+
best = d;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
if (bestD > 3)
|
|
185
|
+
return null;
|
|
186
|
+
const ecl = [1, 0, 3, 2][best >> 3]; // bits: 01 L, 00 M, 11 Q, 10 H -> index L0 M1 Q2 H3
|
|
187
|
+
const mask = MASKS[best & 7];
|
|
188
|
+
const fm = functionMask(version);
|
|
189
|
+
// Read codewords in the zigzag order.
|
|
190
|
+
const bits = [];
|
|
191
|
+
let up = true;
|
|
192
|
+
for (let x = n - 1; x > 0; x -= 2) {
|
|
193
|
+
if (x === 6)
|
|
194
|
+
x--;
|
|
195
|
+
for (let k = 0; k < n; k++) {
|
|
196
|
+
const y = up ? n - 1 - k : k;
|
|
197
|
+
for (const xx of [x, x - 1]) {
|
|
198
|
+
if (fm[y * n + xx])
|
|
199
|
+
continue;
|
|
200
|
+
bits.push(g(xx, y) ^ (mask(y, xx) ? 1 : 0));
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
up = !up;
|
|
204
|
+
}
|
|
205
|
+
const words = [];
|
|
206
|
+
for (let i = 0; i + 8 <= bits.length; i += 8)
|
|
207
|
+
words.push(bits.slice(i, i + 8).reduce((s, v) => (s << 1) | v, 0));
|
|
208
|
+
const [ecPer, groups] = EC[version][ecl];
|
|
209
|
+
const blocks = [];
|
|
210
|
+
for (const [count, data] of groups)
|
|
211
|
+
for (let k = 0; k < count; k++)
|
|
212
|
+
blocks.push({ data, words: [] });
|
|
213
|
+
const maxData = Math.max(...blocks.map((x) => x.data));
|
|
214
|
+
let p = 0;
|
|
215
|
+
for (let i = 0; i < maxData; i++)
|
|
216
|
+
for (const bl of blocks)
|
|
217
|
+
if (i < bl.data)
|
|
218
|
+
bl.words.push(words[p++] ?? 0);
|
|
219
|
+
for (let i = 0; i < ecPer; i++)
|
|
220
|
+
for (const bl of blocks)
|
|
221
|
+
bl.words.push(words[p++] ?? 0);
|
|
222
|
+
const data = [];
|
|
223
|
+
for (const bl of blocks) {
|
|
224
|
+
if (!rsCorrect(bl.words, ecPer))
|
|
225
|
+
return null;
|
|
226
|
+
data.push(...bl.words.slice(0, bl.data));
|
|
227
|
+
}
|
|
228
|
+
return segments(data, version);
|
|
229
|
+
}
|
|
230
|
+
const ALNUM = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:';
|
|
231
|
+
function segments(data, version) {
|
|
232
|
+
let bit = 0;
|
|
233
|
+
const read = (n) => { let v = 0; for (let k = 0; k < n; k++) {
|
|
234
|
+
const byte = data[(bit >> 3)] ?? 0;
|
|
235
|
+
v = (v << 1) | ((byte >> (7 - (bit & 7))) & 1);
|
|
236
|
+
bit++;
|
|
237
|
+
} return v; };
|
|
238
|
+
const total = data.length * 8;
|
|
239
|
+
const big = version >= 10;
|
|
240
|
+
let out = '';
|
|
241
|
+
const bytes = [];
|
|
242
|
+
const flush = () => { if (bytes.length) {
|
|
243
|
+
const buf = Buffer.from(bytes);
|
|
244
|
+
const u = buf.toString('utf8');
|
|
245
|
+
out += u.includes('�') ? buf.toString('latin1') : u;
|
|
246
|
+
bytes.length = 0;
|
|
247
|
+
} };
|
|
248
|
+
while (bit + 4 <= total) {
|
|
249
|
+
const mode = read(4);
|
|
250
|
+
if (mode === 0)
|
|
251
|
+
break;
|
|
252
|
+
if (mode === 1) {
|
|
253
|
+
flush();
|
|
254
|
+
let c = read(big ? 12 : 10);
|
|
255
|
+
while (c >= 3) {
|
|
256
|
+
out += String(read(10)).padStart(3, '0');
|
|
257
|
+
c -= 3;
|
|
258
|
+
}
|
|
259
|
+
if (c === 2)
|
|
260
|
+
out += String(read(7)).padStart(2, '0');
|
|
261
|
+
else if (c === 1)
|
|
262
|
+
out += String(read(4));
|
|
263
|
+
}
|
|
264
|
+
else if (mode === 2) {
|
|
265
|
+
flush();
|
|
266
|
+
let c = read(big ? 11 : 9);
|
|
267
|
+
while (c >= 2) {
|
|
268
|
+
const v = read(11);
|
|
269
|
+
out += ALNUM[Math.floor(v / 45)] + ALNUM[v % 45];
|
|
270
|
+
c -= 2;
|
|
271
|
+
}
|
|
272
|
+
if (c)
|
|
273
|
+
out += ALNUM[read(6)];
|
|
274
|
+
}
|
|
275
|
+
else if (mode === 4) {
|
|
276
|
+
const c = read(big ? 16 : 8);
|
|
277
|
+
for (let k = 0; k < c; k++)
|
|
278
|
+
bytes.push(read(8));
|
|
279
|
+
}
|
|
280
|
+
else if (mode === 7) {
|
|
281
|
+
const f = read(8);
|
|
282
|
+
if (f & 0x80)
|
|
283
|
+
read((f & 0x40) ? 16 : 8);
|
|
284
|
+
}
|
|
285
|
+
else if (mode === 8) {
|
|
286
|
+
flush();
|
|
287
|
+
const c = read(big ? 10 : 8);
|
|
288
|
+
bit += c * 13;
|
|
289
|
+
out += '?'.repeat(c);
|
|
290
|
+
}
|
|
291
|
+
else if (mode === 3)
|
|
292
|
+
read(16);
|
|
293
|
+
else if (mode === 5) { /* FNC1, first position: no data */ }
|
|
294
|
+
else if (mode === 9)
|
|
295
|
+
read(8);
|
|
296
|
+
else
|
|
297
|
+
return out || null;
|
|
298
|
+
if (bit > total)
|
|
299
|
+
break;
|
|
300
|
+
}
|
|
301
|
+
flush();
|
|
302
|
+
return out;
|
|
303
|
+
}
|
|
304
|
+
/** Dark/light per pixel: local mean thresholding over an integral image. */
|
|
305
|
+
export function binarize(img, invert = false) {
|
|
306
|
+
const { width: w, height: h, data } = img;
|
|
307
|
+
const integ = new Float64Array((w + 1) * (h + 1));
|
|
308
|
+
for (let y = 0; y < h; y++) {
|
|
309
|
+
let row = 0;
|
|
310
|
+
for (let x = 0; x < w; x++) {
|
|
311
|
+
row += data[y * w + x];
|
|
312
|
+
integ[(y + 1) * (w + 1) + x + 1] = integ[y * (w + 1) + x + 1] + row;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
const r = Math.max(8, Math.round(Math.min(w, h) / 16));
|
|
316
|
+
const out = new Uint8Array(w * h);
|
|
317
|
+
for (let y = 0; y < h; y++) {
|
|
318
|
+
const y0 = Math.max(0, y - r), y1 = Math.min(h, y + r + 1);
|
|
319
|
+
for (let x = 0; x < w; x++) {
|
|
320
|
+
const x0 = Math.max(0, x - r), x1 = Math.min(w, x + r + 1);
|
|
321
|
+
const sum = integ[y1 * (w + 1) + x1] - integ[y0 * (w + 1) + x1] - integ[y1 * (w + 1) + x0] + integ[y0 * (w + 1) + x0];
|
|
322
|
+
const mean = sum / ((x1 - x0) * (y1 - y0));
|
|
323
|
+
const v = data[y * w + x];
|
|
324
|
+
const dark = v < mean - 4 || (v < 60 && mean < 70);
|
|
325
|
+
out[y * w + x] = (dark ? 1 : 0) ^ (invert ? 1 : 0);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
return out;
|
|
329
|
+
}
|
|
330
|
+
const ratioOk = (s) => {
|
|
331
|
+
const t = s.reduce((a, b) => a + b, 0);
|
|
332
|
+
if (t < 7)
|
|
333
|
+
return false;
|
|
334
|
+
const m = t / 7, v = m / 1.6;
|
|
335
|
+
return Math.abs(m - s[0]) < v && Math.abs(m - s[1]) < v && Math.abs(3 * m - s[2]) < 3 * v && Math.abs(m - s[3]) < v && Math.abs(m - s[4]) < v;
|
|
336
|
+
};
|
|
337
|
+
function crossCheck(bin, w, h, cx, cy, vertical, maxCount) {
|
|
338
|
+
const at = (k) => (vertical ? (k >= 0 && k < h ? bin[k * w + cx] : -1) : (k >= 0 && k < w ? bin[cy * w + k] : -1));
|
|
339
|
+
const c = vertical ? cy : cx;
|
|
340
|
+
const s = [0, 0, 0, 0, 0];
|
|
341
|
+
let k = c;
|
|
342
|
+
while (at(k) === 1) {
|
|
343
|
+
s[2]++;
|
|
344
|
+
k--;
|
|
345
|
+
}
|
|
346
|
+
if (at(k) === -1)
|
|
347
|
+
return null;
|
|
348
|
+
while (at(k) === 0 && s[1] <= maxCount) {
|
|
349
|
+
s[1]++;
|
|
350
|
+
k--;
|
|
351
|
+
}
|
|
352
|
+
if (at(k) === -1 || s[1] > maxCount)
|
|
353
|
+
return null;
|
|
354
|
+
while (at(k) === 1 && s[0] <= maxCount) {
|
|
355
|
+
s[0]++;
|
|
356
|
+
k--;
|
|
357
|
+
}
|
|
358
|
+
if (s[0] > maxCount)
|
|
359
|
+
return null;
|
|
360
|
+
k = c + 1;
|
|
361
|
+
while (at(k) === 1) {
|
|
362
|
+
s[2]++;
|
|
363
|
+
k++;
|
|
364
|
+
}
|
|
365
|
+
if (at(k) === -1)
|
|
366
|
+
return null;
|
|
367
|
+
while (at(k) === 0 && s[3] < maxCount) {
|
|
368
|
+
s[3]++;
|
|
369
|
+
k++;
|
|
370
|
+
}
|
|
371
|
+
if (at(k) === -1 || s[3] >= maxCount)
|
|
372
|
+
return null;
|
|
373
|
+
while (at(k) === 1 && s[4] < maxCount) {
|
|
374
|
+
s[4]++;
|
|
375
|
+
k++;
|
|
376
|
+
}
|
|
377
|
+
if (s[4] >= maxCount || !ratioOk(s))
|
|
378
|
+
return null;
|
|
379
|
+
return k - s[4] - s[3] - s[2] / 2;
|
|
380
|
+
}
|
|
381
|
+
function findFinders(bin, w, h) {
|
|
382
|
+
const found = [];
|
|
383
|
+
const step = Math.max(1, Math.floor(h / 400));
|
|
384
|
+
for (let y = 0; y < h; y += step) {
|
|
385
|
+
const s = [0, 0, 0, 0, 0];
|
|
386
|
+
let state = 0;
|
|
387
|
+
for (let x = 0; x <= w; x++) {
|
|
388
|
+
const v = x < w ? bin[y * w + x] : 0;
|
|
389
|
+
if (v === 1) {
|
|
390
|
+
if (state & 1)
|
|
391
|
+
state++;
|
|
392
|
+
s[state]++;
|
|
393
|
+
}
|
|
394
|
+
else {
|
|
395
|
+
if (!(state & 1)) {
|
|
396
|
+
if (state === 4) {
|
|
397
|
+
if (ratioOk(s)) {
|
|
398
|
+
const total = s.reduce((a, b) => a + b, 0);
|
|
399
|
+
const cx = Math.floor(x - s[4] - s[3] - s[2] / 2);
|
|
400
|
+
const cy = crossCheck(bin, w, h, cx, y, true, s[2]);
|
|
401
|
+
if (cy !== null) {
|
|
402
|
+
const cx2 = crossCheck(bin, w, h, cx, Math.floor(cy), false, s[2]);
|
|
403
|
+
if (cx2 !== null) {
|
|
404
|
+
const size = total / 7;
|
|
405
|
+
const near = found.find((f) => Math.abs(f.x - cx2) <= size * 2 && Math.abs(f.y - cy) <= size * 2 && Math.abs(f.size - size) <= Math.max(1, f.size * 0.5));
|
|
406
|
+
if (near) {
|
|
407
|
+
near.x = (near.x * near.count + cx2) / (near.count + 1);
|
|
408
|
+
near.y = (near.y * near.count + cy) / (near.count + 1);
|
|
409
|
+
near.size = (near.size * near.count + size) / (near.count + 1);
|
|
410
|
+
near.count++;
|
|
411
|
+
}
|
|
412
|
+
else
|
|
413
|
+
found.push({ x: cx2, y: cy, size, count: 1 });
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
s[0] = s[2];
|
|
417
|
+
s[1] = s[3];
|
|
418
|
+
s[2] = s[4];
|
|
419
|
+
s[3] = 1;
|
|
420
|
+
s[4] = 0;
|
|
421
|
+
state = 3;
|
|
422
|
+
continue;
|
|
423
|
+
}
|
|
424
|
+
s[0] = s[2];
|
|
425
|
+
s[1] = s[3];
|
|
426
|
+
s[2] = s[4];
|
|
427
|
+
s[3] = 1;
|
|
428
|
+
s[4] = 0;
|
|
429
|
+
state = 3;
|
|
430
|
+
}
|
|
431
|
+
else {
|
|
432
|
+
state++;
|
|
433
|
+
s[state]++;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
else
|
|
437
|
+
s[state]++;
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
return found;
|
|
442
|
+
}
|
|
443
|
+
const dist = (a, b) => Math.hypot(a.x - b.x, a.y - b.y);
|
|
444
|
+
/** Best three finders: similar size, arranged as a right isosceles triangle. Returned as TL, TR, BL. */
|
|
445
|
+
function pickThree(fs) {
|
|
446
|
+
const c = fs.filter((f) => f.count >= 1).sort((a, b) => b.count - a.count).slice(0, 12);
|
|
447
|
+
const out = [];
|
|
448
|
+
for (let i = 0; i < c.length; i++)
|
|
449
|
+
for (let j = i + 1; j < c.length; j++)
|
|
450
|
+
for (let k = j + 1; k < c.length; k++) {
|
|
451
|
+
const p = [c[i], c[j], c[k]];
|
|
452
|
+
const sizes = p.map((f) => f.size);
|
|
453
|
+
if (Math.max(...sizes) > Math.min(...sizes) * 1.6)
|
|
454
|
+
continue;
|
|
455
|
+
const d = [[dist(p[0], p[1]), 2], [dist(p[1], p[2]), 0], [dist(p[0], p[2]), 1]];
|
|
456
|
+
d.sort((a, b) => b[0] - a[0]);
|
|
457
|
+
const tl = p[d[0][1]];
|
|
458
|
+
const others = p.filter((x) => x !== tl);
|
|
459
|
+
const [a, b] = [dist(tl, others[0]), dist(tl, others[1])];
|
|
460
|
+
const hyp = d[0][0];
|
|
461
|
+
const err = Math.abs(a - b) / Math.max(a, b) + Math.abs(hyp - Math.hypot(a, b)) / hyp;
|
|
462
|
+
if (err > 0.5 || Math.min(a, b) < tl.size * 6)
|
|
463
|
+
continue;
|
|
464
|
+
let tr = others[0], bl = others[1];
|
|
465
|
+
const cross = (tr.x - tl.x) * (bl.y - tl.y) - (tr.y - tl.y) * (bl.x - tl.x);
|
|
466
|
+
if (cross < 0)
|
|
467
|
+
[tr, bl] = [bl, tr];
|
|
468
|
+
out.push({ s: err - (tl.count + tr.count + bl.count) * 0.01, t: [tl, tr, bl] });
|
|
469
|
+
}
|
|
470
|
+
return out.sort((a, b) => a.s - b.s).slice(0, 3).map((x) => x.t);
|
|
471
|
+
}
|
|
472
|
+
/** Homography from four point pairs (solved directly), mapping grid coordinates to image pixels. */
|
|
473
|
+
function quadToQuad(from, to) {
|
|
474
|
+
const A = [];
|
|
475
|
+
for (let i = 0; i < 4; i++) {
|
|
476
|
+
const { x, y } = from[i], { x: u, y: v } = to[i];
|
|
477
|
+
A.push([x, y, 1, 0, 0, 0, -u * x, -u * y, u]);
|
|
478
|
+
A.push([0, 0, 0, x, y, 1, -v * x, -v * y, v]);
|
|
479
|
+
}
|
|
480
|
+
for (let c = 0; c < 8; c++) {
|
|
481
|
+
let p = c;
|
|
482
|
+
for (let r = c + 1; r < 8; r++)
|
|
483
|
+
if (Math.abs(A[r][c]) > Math.abs(A[p][c]))
|
|
484
|
+
p = r;
|
|
485
|
+
[A[c], A[p]] = [A[p], A[c]];
|
|
486
|
+
const d = A[c][c] || 1e-12;
|
|
487
|
+
for (let k = c; k < 9; k++)
|
|
488
|
+
A[c][k] /= d;
|
|
489
|
+
for (let r = 0; r < 8; r++)
|
|
490
|
+
if (r !== c) {
|
|
491
|
+
const m = A[r][c];
|
|
492
|
+
if (m)
|
|
493
|
+
for (let k = c; k < 9; k++)
|
|
494
|
+
A[r][k] -= m * A[c][k];
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
const h = A.map((r) => r[8]);
|
|
498
|
+
return (x, y) => { const den = h[6] * x + h[7] * y + 1; return { x: (h[0] * x + h[1] * y + h[2]) / den, y: (h[3] * x + h[4] * y + h[5]) / den }; };
|
|
499
|
+
}
|
|
500
|
+
function findAlignment(bin, w, h, est, module) {
|
|
501
|
+
const r = Math.round(module * 5);
|
|
502
|
+
let best = null, bestScore = 0;
|
|
503
|
+
const at = (x, y) => { const xi = Math.round(x), yi = Math.round(y); return xi >= 0 && yi >= 0 && xi < w && yi < h ? bin[yi * w + xi] : 0; };
|
|
504
|
+
for (let y = Math.round(est.y - r); y <= est.y + r; y++)
|
|
505
|
+
for (let x = Math.round(est.x - r); x <= est.x + r; x++) {
|
|
506
|
+
if (!at(x, y))
|
|
507
|
+
continue;
|
|
508
|
+
let s = 0;
|
|
509
|
+
for (let k = 0; k < 8; k++) {
|
|
510
|
+
const a = (k * Math.PI) / 4, cx = Math.cos(a), cy = Math.sin(a);
|
|
511
|
+
const sq = Math.max(Math.abs(cx), Math.abs(cy));
|
|
512
|
+
if (!at(x + (cx / sq) * module, y + (cy / sq) * module))
|
|
513
|
+
s++;
|
|
514
|
+
if (at(x + (cx / sq) * module * 2, y + (cy / sq) * module * 2))
|
|
515
|
+
s++;
|
|
516
|
+
}
|
|
517
|
+
const score = s - dist({ x, y }, est) / (module * 10);
|
|
518
|
+
if (score > bestScore) {
|
|
519
|
+
bestScore = score;
|
|
520
|
+
best = { x, y };
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
return bestScore >= 14 ? best : null;
|
|
524
|
+
}
|
|
525
|
+
function sample(bin, w, h, tl, tr, bl, br, n, brOffset) {
|
|
526
|
+
const map = quadToQuad([{ x: 3.5, y: 3.5 }, { x: n - 3.5, y: 3.5 }, { x: n - brOffset, y: n - brOffset }, { x: 3.5, y: n - 3.5 }], [tl, tr, br, bl]);
|
|
527
|
+
const grid = new Uint8Array(n * n);
|
|
528
|
+
for (let y = 0; y < n; y++)
|
|
529
|
+
for (let x = 0; x < n; x++) {
|
|
530
|
+
const p = map(x + 0.5, y + 0.5);
|
|
531
|
+
const xi = Math.round(p.x - 0.5), yi = Math.round(p.y - 0.5);
|
|
532
|
+
grid[y * n + x] = xi >= 0 && yi >= 0 && xi < w && yi < h ? bin[yi * w + xi] : 0;
|
|
533
|
+
}
|
|
534
|
+
return grid;
|
|
535
|
+
}
|
|
536
|
+
/** Finds and decodes QR codes in an image. Returns the decoded texts (usually one). */
|
|
537
|
+
export function readQr(img) {
|
|
538
|
+
const results = new Set();
|
|
539
|
+
for (const invert of [false, true]) {
|
|
540
|
+
const bin = binarize(img, invert);
|
|
541
|
+
const { width: w, height: h } = img;
|
|
542
|
+
for (const [tl, tr, bl] of pickThree(findFinders(bin, w, h))) {
|
|
543
|
+
// Finder sizes come from horizontal runs, which a rotated code stretches by 1/cos(angle).
|
|
544
|
+
const ang = Math.atan2(tr.y - tl.y, tr.x - tl.x);
|
|
545
|
+
const module = ((tl.size + tr.size + bl.size) / 3) * Math.max(Math.abs(Math.cos(ang)), Math.abs(Math.sin(ang)));
|
|
546
|
+
const est = Math.round(((dist(tl, tr) + dist(tl, bl)) / 2) / module) + 7;
|
|
547
|
+
for (const n of [...new Set([0, -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6].map((d) => est + d))].filter((d) => d >= 21 && (d - 17) % 4 === 0 && EC[(d - 17) / 4])) {
|
|
548
|
+
const version = (n - 17) / 4;
|
|
549
|
+
const brEst = { x: tr.x - tl.x + bl.x, y: tr.y - tl.y + bl.y };
|
|
550
|
+
const tries = [];
|
|
551
|
+
if (version >= 2) {
|
|
552
|
+
const k = 1 - 3 / (n - 7);
|
|
553
|
+
const ae = { x: tl.x + k * (brEst.x - tl.x), y: tl.y + k * (brEst.y - tl.y) };
|
|
554
|
+
const al = findAlignment(bin, w, h, ae, module);
|
|
555
|
+
if (al)
|
|
556
|
+
tries.push([al, 6.5]);
|
|
557
|
+
tries.push([ae, 6.5]);
|
|
558
|
+
}
|
|
559
|
+
tries.push([brEst, 3.5]);
|
|
560
|
+
for (const [br, off] of tries) {
|
|
561
|
+
const text = decodeGrid(sample(bin, w, h, tl, tr, bl, br, n, off), n);
|
|
562
|
+
if (text !== null) {
|
|
563
|
+
results.add(text);
|
|
564
|
+
break;
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
if (results.size)
|
|
568
|
+
break;
|
|
569
|
+
}
|
|
570
|
+
if (results.size)
|
|
571
|
+
break;
|
|
572
|
+
}
|
|
573
|
+
if (results.size)
|
|
574
|
+
break;
|
|
575
|
+
}
|
|
576
|
+
return [...results];
|
|
577
|
+
}
|
package/dist/core/quota.js
CHANGED
|
@@ -86,6 +86,11 @@ export const DAILY_BUDGET = {
|
|
|
86
86
|
gleif: 3000,
|
|
87
87
|
vies: 1000,
|
|
88
88
|
celestrak: 200, // asks for at most one fetch of the same data per two hours
|
|
89
|
+
chainabuse: 24, // 750 per month on the free key
|
|
90
|
+
'hybrid-analysis': 180, // free key: 200 per day
|
|
91
|
+
'wayback-save': 1000, // logged-in Save Page Now: 30,000 a day; kept well under
|
|
92
|
+
'cf-urlscan': 150,
|
|
93
|
+
alchemy: 20_000,
|
|
89
94
|
};
|
|
90
95
|
const envName = (source) => `OCULON_QUOTA_${source.toUpperCase().replace(/[^A-Z0-9]/g, '_')}`;
|
|
91
96
|
export function budgetFor(source) {
|