@acepaste/ingress 0.9.1 → 0.9.21
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 +1 -1
- package/dist/src/adapters.d.ts +9 -0
- package/dist/src/adapters.js +76 -6
- package/dist/src/agent-hooks.d.ts +112 -0
- package/dist/src/agent-hooks.js +533 -0
- package/dist/src/approvers.d.ts +16 -0
- package/dist/src/approvers.js +50 -0
- package/dist/src/audio.d.ts +70 -0
- package/dist/src/audio.js +224 -0
- package/dist/src/audit-changes.d.ts +46 -0
- package/dist/src/audit-changes.js +110 -0
- package/dist/src/audit-local.d.ts +57 -0
- package/dist/src/audit-local.js +231 -0
- package/dist/src/audit-parsers.d.ts +1 -1
- package/dist/src/audit.js +48 -0
- package/dist/src/broker.d.ts +92 -1
- package/dist/src/broker.js +222 -3
- package/dist/src/calendar.d.ts +36 -0
- package/dist/src/calendar.js +227 -0
- package/dist/src/captions.d.ts +44 -0
- package/dist/src/captions.js +209 -0
- package/dist/src/changes.d.ts +105 -0
- package/dist/src/changes.js +174 -0
- package/dist/src/character.js +19 -3
- package/dist/src/classifier-model.d.ts +53 -0
- package/dist/src/classifier-model.js +3 -0
- package/dist/src/classifier.d.ts +41 -0
- package/dist/src/classifier.js +213 -0
- package/dist/src/cli.js +501 -1
- package/dist/src/codepass.d.ts +8 -0
- package/dist/src/codepass.js +39 -1
- package/dist/src/egress.d.ts +71 -0
- package/dist/src/egress.js +242 -0
- package/dist/src/email.d.ts +42 -0
- package/dist/src/email.js +424 -0
- package/dist/src/engine.d.ts +35 -12
- package/dist/src/engine.js +172 -45
- package/dist/src/image-meta.d.ts +33 -0
- package/dist/src/image-meta.js +433 -0
- package/dist/src/image.d.ts +104 -0
- package/dist/src/image.js +347 -0
- package/dist/src/index.d.ts +50 -5
- package/dist/src/index.js +28 -3
- package/dist/src/localguard.d.ts +33 -0
- package/dist/src/localguard.js +61 -0
- package/dist/src/meaning.d.ts +2 -0
- package/dist/src/meaning.js +348 -25
- package/dist/src/messages.d.ts +79 -0
- package/dist/src/messages.js +431 -0
- package/dist/src/multilingual.d.ts +3 -0
- package/dist/src/multilingual.js +117 -5
- package/dist/src/nearby.d.ts +52 -0
- package/dist/src/nearby.js +186 -0
- package/dist/src/netpolicy.js +50 -19
- package/dist/src/normalize.d.ts +36 -1
- package/dist/src/normalize.js +186 -4
- package/dist/src/provenance.d.ts +56 -0
- package/dist/src/provenance.js +114 -0
- package/dist/src/qr.d.ts +25 -0
- package/dist/src/qr.js +113 -0
- package/dist/src/safeguards.d.ts +61 -0
- package/dist/src/safeguards.js +199 -0
- package/dist/src/served.d.ts +20 -0
- package/dist/src/served.js +53 -0
- package/dist/src/shell-commands.d.ts +39 -0
- package/dist/src/shell-commands.js +291 -0
- package/dist/src/shortcuts.d.ts +45 -0
- package/dist/src/shortcuts.js +220 -0
- package/dist/src/stream.d.ts +88 -0
- package/dist/src/stream.js +119 -0
- package/dist/src/terminal.d.ts +29 -0
- package/dist/src/terminal.js +313 -0
- package/dist/src/tooldefs.d.ts +84 -0
- package/dist/src/tooldefs.js +122 -0
- package/dist/src/types.d.ts +50 -6
- package/dist/src/types.js +15 -2
- package/dist/src/visibility.d.ts +23 -3
- package/dist/src/visibility.js +285 -78
- package/native/ingress-presence.swift +48 -0
- package/native/ingress-vision.swift +417 -0
- package/package.json +4 -2
|
@@ -0,0 +1,433 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What an image file carries besides its picture: text in metadata, data after the end of the image, extra
|
|
3
|
+
* frames. Pure parsing, no dependencies, any platform. The pixels themselves are read by the vision helper
|
|
4
|
+
* (image.ts); this module reads the container.
|
|
5
|
+
*
|
|
6
|
+
* Why it matters: a model, or the code in front of it, may be handed a file's description, comment or XMP
|
|
7
|
+
* packet along with the picture. A person looking at the picture is shown none of it.
|
|
8
|
+
*/
|
|
9
|
+
import { inflateSync } from "node:zlib";
|
|
10
|
+
const ascii = (b, from, to) => { let s = ""; for (let i = from; i < to && i < b.length; i++)
|
|
11
|
+
s += String.fromCharCode(b[i]); return s; };
|
|
12
|
+
const utf8 = (b) => new TextDecoder("utf-8", { fatal: false }).decode(b);
|
|
13
|
+
const latin1 = (b) => new TextDecoder("latin1").decode(b);
|
|
14
|
+
const MAX_TEXT = 20_000;
|
|
15
|
+
export function detectImageFormat(b) {
|
|
16
|
+
if (b.length >= 8 && b[0] === 0x89 && ascii(b, 1, 4) === "PNG")
|
|
17
|
+
return "png";
|
|
18
|
+
if (b.length >= 3 && b[0] === 0xff && b[1] === 0xd8 && b[2] === 0xff)
|
|
19
|
+
return "jpeg";
|
|
20
|
+
if (ascii(b, 0, 6) === "GIF87a" || ascii(b, 0, 6) === "GIF89a")
|
|
21
|
+
return "gif";
|
|
22
|
+
if (ascii(b, 0, 4) === "RIFF" && ascii(b, 8, 12) === "WEBP")
|
|
23
|
+
return "webp";
|
|
24
|
+
if (ascii(b, 0, 2) === "BM" && b.length > 26)
|
|
25
|
+
return "bmp";
|
|
26
|
+
if (ascii(b, 0, 4) === "II*\0" || ascii(b, 0, 4) === "MM\0*")
|
|
27
|
+
return "tiff";
|
|
28
|
+
if (ascii(b, 4, 8) === "ftyp") {
|
|
29
|
+
const brand = ascii(b, 8, 12);
|
|
30
|
+
return /avif|avis/.test(brand) ? "avif" : /heic|heix|hevc|mif1|msf1/.test(brand) ? "heic" : "unknown";
|
|
31
|
+
}
|
|
32
|
+
if (b.length >= 4 && b[0] === 0 && b[1] === 0 && b[2] === 1 && b[3] === 0)
|
|
33
|
+
return "ico";
|
|
34
|
+
if (ascii(b, 0, 5) === "%PDF-")
|
|
35
|
+
return "pdf";
|
|
36
|
+
const head = utf8(b.subarray(0, 1024)).replace(/^/, "").trimStart();
|
|
37
|
+
if (/^(?:<\?xml[^>]*>\s*)?(?:<!--[\s\S]*?-->\s*)*(?:<!DOCTYPE svg[^>]*>\s*)?<svg[\s>]/i.test(head))
|
|
38
|
+
return "svg";
|
|
39
|
+
return "unknown";
|
|
40
|
+
}
|
|
41
|
+
/** What a run of bytes looks like, when it looks like the start of some other kind of file. */
|
|
42
|
+
export function looksLike(b) {
|
|
43
|
+
if (b.length < 4)
|
|
44
|
+
return undefined;
|
|
45
|
+
const s = ascii(b, 0, Math.min(b.length, 64));
|
|
46
|
+
if (s.startsWith("PK\x03\x04") || s.startsWith("PK\x05\x06"))
|
|
47
|
+
return "a ZIP archive";
|
|
48
|
+
if (s.startsWith("%PDF-"))
|
|
49
|
+
return "a PDF document";
|
|
50
|
+
if (s.startsWith("\x7fELF"))
|
|
51
|
+
return "a Linux program";
|
|
52
|
+
if (s.startsWith("MZ"))
|
|
53
|
+
return "a Windows program";
|
|
54
|
+
if (b[0] === 0xcf && b[1] === 0xfa && b[2] === 0xed && b[3] === 0xfe)
|
|
55
|
+
return "a macOS program";
|
|
56
|
+
if (s.startsWith("#!"))
|
|
57
|
+
return "a script";
|
|
58
|
+
if (s.startsWith("Rar!"))
|
|
59
|
+
return "a RAR archive";
|
|
60
|
+
if (s.startsWith("7z\xbc\xaf"))
|
|
61
|
+
return "a 7-Zip archive";
|
|
62
|
+
if (b[0] === 0x1f && b[1] === 0x8b)
|
|
63
|
+
return "gzip-compressed data";
|
|
64
|
+
if (/^\s*<(?:!doctype html|html|script|\?php|svg)\b/i.test(s))
|
|
65
|
+
return "a web page or script";
|
|
66
|
+
if (b[0] === 0x89 && s.slice(1, 4) === "PNG")
|
|
67
|
+
return "another PNG image";
|
|
68
|
+
if (b[0] === 0xff && b[1] === 0xd8)
|
|
69
|
+
return "another JPEG image";
|
|
70
|
+
// mostly printable: text someone appended
|
|
71
|
+
let printable = 0;
|
|
72
|
+
const n = Math.min(b.length, 256);
|
|
73
|
+
for (let i = 0; i < n; i++)
|
|
74
|
+
if ((b[i] >= 32 && b[i] < 127) || b[i] === 10 || b[i] === 13 || b[i] === 9)
|
|
75
|
+
printable++;
|
|
76
|
+
if (n >= 24 && printable / n > 0.9)
|
|
77
|
+
return "text";
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
const clean = (s) => s.replace(/\0+$/g, "").replace(/[\0-\x08\x0b\x0c\x0e-\x1f]/g, " ").trim().slice(0, MAX_TEXT);
|
|
81
|
+
/** XMP is XML: the values a reader might be handed are element text and attribute values. */
|
|
82
|
+
function xmpTexts(xml) {
|
|
83
|
+
const out = [];
|
|
84
|
+
const seen = new Set();
|
|
85
|
+
const add = (field, text) => {
|
|
86
|
+
const t = clean(text.replace(/</g, "<").replace(/>/g, ">").replace(/"/g, '"').replace(/'/g, "'").replace(/&/g, "&"));
|
|
87
|
+
// identifiers, dates and numbers are not prose
|
|
88
|
+
if (t.length < 12 || !/[A-Za-zÀ-]{3,}\s+[A-Za-zÀ-]{2,}/.test(t) || seen.has(t))
|
|
89
|
+
return;
|
|
90
|
+
seen.add(t);
|
|
91
|
+
out.push({ field: `xmp:${field}`, text: t });
|
|
92
|
+
};
|
|
93
|
+
for (const m of xml.slice(0, 2_000_000).matchAll(/<([A-Za-z][\w.-]*:[\w.-]+)[^>]*>([^<]{12,})<\/\1>/g))
|
|
94
|
+
add(m[1], m[2]);
|
|
95
|
+
for (const m of xml.slice(0, 2_000_000).matchAll(/<rdf:li[^>]*>([^<]{12,})<\/rdf:li>/g))
|
|
96
|
+
add("li", m[1]);
|
|
97
|
+
for (const m of xml.slice(0, 2_000_000).matchAll(/\s([A-Za-z][\w.-]*:[\w.-]+)="([^"]{12,})"/g))
|
|
98
|
+
if (!/^xmlns:|^rdf:about$|^xml:/.test(m[1]))
|
|
99
|
+
add(m[1], m[2]);
|
|
100
|
+
return out.slice(0, 60);
|
|
101
|
+
}
|
|
102
|
+
// ---------- EXIF (a TIFF structure) ----------
|
|
103
|
+
const EXIF_TEXT_TAGS = {
|
|
104
|
+
0x010e: "ImageDescription", 0x010f: "Make", 0x0110: "Model", 0x0131: "Software", 0x013b: "Artist", 0x8298: "Copyright",
|
|
105
|
+
0x9286: "UserComment", 0x9c9b: "XPTitle", 0x9c9c: "XPComment", 0x9c9d: "XPAuthor", 0x9c9e: "XPKeywords", 0x9c9f: "XPSubject",
|
|
106
|
+
0x02bc: "XMP", 0x83bb: "IPTC", 0xa420: "ImageUniqueID", 0xc4a5: "PrintIM",
|
|
107
|
+
};
|
|
108
|
+
function exifTexts(t) {
|
|
109
|
+
const texts = [];
|
|
110
|
+
let hasThumbnail = false;
|
|
111
|
+
if (t.length < 8)
|
|
112
|
+
return { texts, hasThumbnail };
|
|
113
|
+
const le = ascii(t, 0, 2) === "II";
|
|
114
|
+
if (!le && ascii(t, 0, 2) !== "MM")
|
|
115
|
+
return { texts, hasThumbnail };
|
|
116
|
+
const dv = new DataView(t.buffer, t.byteOffset, t.byteLength);
|
|
117
|
+
const u16 = (o) => (o + 2 <= t.length ? dv.getUint16(o, le) : 0);
|
|
118
|
+
const u32 = (o) => (o + 4 <= t.length ? dv.getUint32(o, le) : 0);
|
|
119
|
+
const visited = new Set();
|
|
120
|
+
const walk = (ifd, depth, index) => {
|
|
121
|
+
if (!ifd || ifd + 2 > t.length || visited.has(ifd) || depth > 4 || visited.size > 16)
|
|
122
|
+
return;
|
|
123
|
+
visited.add(ifd);
|
|
124
|
+
const n = Math.min(u16(ifd), 400);
|
|
125
|
+
for (let i = 0; i < n; i++) {
|
|
126
|
+
const e = ifd + 2 + i * 12;
|
|
127
|
+
if (e + 12 > t.length)
|
|
128
|
+
break;
|
|
129
|
+
const tag = u16(e), type = u16(e + 2), count = u32(e + 4);
|
|
130
|
+
const size = { 1: 1, 2: 1, 7: 1, 3: 2, 4: 4 }[type];
|
|
131
|
+
if (tag === 0x8769 || tag === 0xa005 || tag === 0x8825) {
|
|
132
|
+
if (tag !== 0x8825)
|
|
133
|
+
walk(u32(e + 8), depth + 1, index);
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
if (tag === 0x0201 && index === 1)
|
|
137
|
+
hasThumbnail = true; // JPEGInterchangeFormat in the second directory
|
|
138
|
+
const name = EXIF_TEXT_TAGS[tag];
|
|
139
|
+
if (!name || !size || count === 0 || count > 2_000_000)
|
|
140
|
+
continue;
|
|
141
|
+
const bytes = count * size;
|
|
142
|
+
const at = bytes <= 4 ? e + 8 : u32(e + 8);
|
|
143
|
+
if (at + bytes > t.length)
|
|
144
|
+
continue;
|
|
145
|
+
const raw = t.subarray(at, at + bytes);
|
|
146
|
+
let text;
|
|
147
|
+
if (name === "XMP") {
|
|
148
|
+
texts.push(...xmpTexts(utf8(raw)));
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
if (name.startsWith("XP"))
|
|
152
|
+
text = new TextDecoder("utf-16le").decode(raw);
|
|
153
|
+
else if (name === "UserComment") {
|
|
154
|
+
const code = ascii(raw, 0, 8);
|
|
155
|
+
text = code.startsWith("UNICODE") ? new TextDecoder(le ? "utf-16le" : "utf-16be").decode(raw.subarray(8)) : utf8(raw.subarray(code.startsWith("ASCII") || code.startsWith("JIS") || /^\0{8}$/.test(code) ? 8 : 0));
|
|
156
|
+
}
|
|
157
|
+
else if (type === 2)
|
|
158
|
+
text = utf8(raw);
|
|
159
|
+
else
|
|
160
|
+
continue;
|
|
161
|
+
text = clean(text);
|
|
162
|
+
if (text.length >= 3)
|
|
163
|
+
texts.push({ field: `exif:${name}`, text });
|
|
164
|
+
}
|
|
165
|
+
const next = u32(ifd + 2 + n * 12);
|
|
166
|
+
if (next)
|
|
167
|
+
walk(next, depth, index + 1);
|
|
168
|
+
};
|
|
169
|
+
walk(u32(4), 0, 0);
|
|
170
|
+
return { texts, hasThumbnail };
|
|
171
|
+
}
|
|
172
|
+
// ---------- containers ----------
|
|
173
|
+
function parsePng(b, m) {
|
|
174
|
+
const dv = new DataView(b.buffer, b.byteOffset, b.byteLength);
|
|
175
|
+
let o = 8, end = -1, chunks = 0;
|
|
176
|
+
while (o + 12 <= b.length && chunks++ < 100_000) {
|
|
177
|
+
const len = dv.getUint32(o), type = ascii(b, o + 4, o + 8);
|
|
178
|
+
const data = b.subarray(o + 8, Math.min(b.length, o + 8 + len));
|
|
179
|
+
if (type === "IHDR" && len >= 8) {
|
|
180
|
+
m.width = dv.getUint32(o + 8);
|
|
181
|
+
m.height = dv.getUint32(o + 12);
|
|
182
|
+
}
|
|
183
|
+
else if (type === "acTL" && len >= 4)
|
|
184
|
+
m.frames = dv.getUint32(o + 8);
|
|
185
|
+
else if (type === "tEXt") {
|
|
186
|
+
const z = data.indexOf(0);
|
|
187
|
+
if (z > 0)
|
|
188
|
+
m.texts.push({ field: `png:${clean(latin1(data.subarray(0, z))).slice(0, 80)}`, text: clean(latin1(data.subarray(z + 1))) });
|
|
189
|
+
}
|
|
190
|
+
else if (type === "zTXt") {
|
|
191
|
+
const z = data.indexOf(0);
|
|
192
|
+
if (z > 0 && z + 2 <= data.length) {
|
|
193
|
+
try {
|
|
194
|
+
m.texts.push({ field: `png:${clean(latin1(data.subarray(0, z))).slice(0, 80)}`, text: clean(latin1(inflateSync(data.subarray(z + 2), { maxOutputLength: 4_000_000 }))) });
|
|
195
|
+
}
|
|
196
|
+
catch {
|
|
197
|
+
m.notes.push("a compressed text chunk could not be read");
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
else if (type === "iTXt") {
|
|
202
|
+
const z = data.indexOf(0);
|
|
203
|
+
if (z > 0 && z + 3 <= data.length) {
|
|
204
|
+
const key = clean(latin1(data.subarray(0, z))).slice(0, 80);
|
|
205
|
+
const compressed = data[z + 1] === 1;
|
|
206
|
+
let p = z + 3;
|
|
207
|
+
p = data.indexOf(0, p) + 1; // language tag
|
|
208
|
+
if (p > 0) {
|
|
209
|
+
const q = data.indexOf(0, p);
|
|
210
|
+
p = q >= 0 ? q + 1 : -1;
|
|
211
|
+
} // translated keyword
|
|
212
|
+
if (p > 0) {
|
|
213
|
+
try {
|
|
214
|
+
const body = compressed ? inflateSync(data.subarray(p), { maxOutputLength: 4_000_000 }) : data.subarray(p);
|
|
215
|
+
const text = utf8(body);
|
|
216
|
+
if (/XML:com\.adobe\.xmp/i.test(key) || /<x:xmpmeta|<rdf:RDF/.test(text.slice(0, 400)))
|
|
217
|
+
m.texts.push(...xmpTexts(text));
|
|
218
|
+
else
|
|
219
|
+
m.texts.push({ field: `png:${key}`, text: clean(text) });
|
|
220
|
+
}
|
|
221
|
+
catch {
|
|
222
|
+
m.notes.push("a compressed text chunk could not be read");
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
else if (type === "eXIf") {
|
|
228
|
+
const e = exifTexts(data);
|
|
229
|
+
m.texts.push(...e.texts);
|
|
230
|
+
m.hasThumbnail ||= e.hasThumbnail;
|
|
231
|
+
}
|
|
232
|
+
o += 12 + len;
|
|
233
|
+
if (type === "IEND") {
|
|
234
|
+
end = o;
|
|
235
|
+
break;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
if (end < 0)
|
|
239
|
+
m.notes.push("the PNG has no end marker");
|
|
240
|
+
else
|
|
241
|
+
setTrailing(b, end, m);
|
|
242
|
+
}
|
|
243
|
+
function parseJpeg(b, m) {
|
|
244
|
+
let o = 2, end = -1, guard = 0;
|
|
245
|
+
while (o + 4 <= b.length && guard++ < 100_000) {
|
|
246
|
+
if (b[o] !== 0xff) {
|
|
247
|
+
o++;
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
const marker = b[o + 1];
|
|
251
|
+
if (marker === 0xff) {
|
|
252
|
+
o++;
|
|
253
|
+
continue;
|
|
254
|
+
}
|
|
255
|
+
if (marker === 0xd9) {
|
|
256
|
+
end = o + 2;
|
|
257
|
+
break;
|
|
258
|
+
}
|
|
259
|
+
if (marker === 0x01 || (marker >= 0xd0 && marker <= 0xd8)) {
|
|
260
|
+
o += 2;
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
const len = (b[o + 2] << 8) | b[o + 3];
|
|
264
|
+
if (len < 2)
|
|
265
|
+
break;
|
|
266
|
+
const data = b.subarray(o + 4, Math.min(b.length, o + 2 + len));
|
|
267
|
+
if (marker === 0xfe) {
|
|
268
|
+
const t = clean(utf8(data));
|
|
269
|
+
if (t.length >= 3)
|
|
270
|
+
m.texts.push({ field: "jpeg:comment", text: t });
|
|
271
|
+
}
|
|
272
|
+
else if (marker === 0xe1 && ascii(data, 0, 6) === "Exif\0\0") {
|
|
273
|
+
const e = exifTexts(data.subarray(6));
|
|
274
|
+
m.texts.push(...e.texts);
|
|
275
|
+
m.hasThumbnail ||= e.hasThumbnail;
|
|
276
|
+
}
|
|
277
|
+
else if (marker === 0xe1 && ascii(data, 0, 28) === "http://ns.adobe.com/xap/1.0/")
|
|
278
|
+
m.texts.push(...xmpTexts(utf8(data.subarray(29))));
|
|
279
|
+
else if (marker === 0xed) { // Photoshop / IPTC: caption and keywords are plain strings inside
|
|
280
|
+
for (const t of utf8(data).match(/[\x20-\x7e -]{16,}/g) ?? [])
|
|
281
|
+
if (/[A-Za-z]{3,}\s+[A-Za-z]{2,}\s+[A-Za-z]{2,}/.test(t) && !/^Photoshop 3\.0/.test(t))
|
|
282
|
+
m.texts.push({ field: "iptc", text: clean(t) });
|
|
283
|
+
}
|
|
284
|
+
else if (marker >= 0xc0 && marker <= 0xcf && ![0xc4, 0xc8, 0xcc].includes(marker) && data.length >= 5) {
|
|
285
|
+
m.height = (data[1] << 8) | data[2];
|
|
286
|
+
m.width = (data[3] << 8) | data[4];
|
|
287
|
+
}
|
|
288
|
+
if (marker === 0xda) {
|
|
289
|
+
// entropy-coded data runs to the next real marker
|
|
290
|
+
let p = o + 2 + len;
|
|
291
|
+
while (p + 1 < b.length) {
|
|
292
|
+
if (b[p] === 0xff && b[p + 1] !== 0 && !(b[p + 1] >= 0xd0 && b[p + 1] <= 0xd7) && b[p + 1] !== 0xff)
|
|
293
|
+
break;
|
|
294
|
+
p++;
|
|
295
|
+
}
|
|
296
|
+
o = p;
|
|
297
|
+
continue;
|
|
298
|
+
}
|
|
299
|
+
o += 2 + len;
|
|
300
|
+
}
|
|
301
|
+
if (end < 0)
|
|
302
|
+
m.notes.push("the JPEG has no end marker");
|
|
303
|
+
else
|
|
304
|
+
setTrailing(b, end, m);
|
|
305
|
+
}
|
|
306
|
+
function parseGif(b, m) {
|
|
307
|
+
if (b.length < 13)
|
|
308
|
+
return;
|
|
309
|
+
m.width = b[6] | (b[7] << 8);
|
|
310
|
+
m.height = b[8] | (b[9] << 8);
|
|
311
|
+
let o = 13 + (b[10] & 0x80 ? 3 * (1 << ((b[10] & 7) + 1)) : 0);
|
|
312
|
+
let frames = 0, end = -1, guard = 0;
|
|
313
|
+
const skipBlocks = (p) => { while (p < b.length && b[p] !== 0)
|
|
314
|
+
p += b[p] + 1; return p + 1; };
|
|
315
|
+
while (o < b.length && guard++ < 1_000_000) {
|
|
316
|
+
const c = b[o];
|
|
317
|
+
if (c === 0x3b) {
|
|
318
|
+
end = o + 1;
|
|
319
|
+
break;
|
|
320
|
+
}
|
|
321
|
+
if (c === 0x2c) {
|
|
322
|
+
frames++;
|
|
323
|
+
const flags = b[o + 9] ?? 0;
|
|
324
|
+
o += 10 + (flags & 0x80 ? 3 * (1 << ((flags & 7) + 1)) : 0) + 1;
|
|
325
|
+
o = skipBlocks(o);
|
|
326
|
+
continue;
|
|
327
|
+
}
|
|
328
|
+
if (c === 0x21) {
|
|
329
|
+
const label = b[o + 1];
|
|
330
|
+
let p = o + 2;
|
|
331
|
+
if (label === 0xfe) {
|
|
332
|
+
const parts = [];
|
|
333
|
+
while (p < b.length && b[p] !== 0) {
|
|
334
|
+
parts.push(b.subarray(p + 1, p + 1 + b[p]));
|
|
335
|
+
p += b[p] + 1;
|
|
336
|
+
}
|
|
337
|
+
const t = clean(utf8(Buffer.concat(parts)));
|
|
338
|
+
if (t.length >= 3)
|
|
339
|
+
m.texts.push({ field: "gif:comment", text: t });
|
|
340
|
+
o = p + 1;
|
|
341
|
+
continue;
|
|
342
|
+
}
|
|
343
|
+
if (label === 0xff && ascii(b, o + 3, o + 14) === "XMP DataXMP") {
|
|
344
|
+
const start = o + 14;
|
|
345
|
+
let q = start;
|
|
346
|
+
while (q < b.length && !(b[q] === 0x01 && b[q + 1] === 0xff && b[q + 2] === 0xfe))
|
|
347
|
+
q++;
|
|
348
|
+
m.texts.push(...xmpTexts(utf8(b.subarray(start, q))));
|
|
349
|
+
}
|
|
350
|
+
o = skipBlocks(p);
|
|
351
|
+
continue;
|
|
352
|
+
}
|
|
353
|
+
break;
|
|
354
|
+
}
|
|
355
|
+
m.frames = frames || undefined;
|
|
356
|
+
if (end >= 0)
|
|
357
|
+
setTrailing(b, end, m);
|
|
358
|
+
}
|
|
359
|
+
function parseWebp(b, m) {
|
|
360
|
+
const dv = new DataView(b.buffer, b.byteOffset, b.byteLength);
|
|
361
|
+
const riffEnd = Math.min(b.length, 8 + dv.getUint32(4, true));
|
|
362
|
+
let o = 12, frames = 0, guard = 0;
|
|
363
|
+
while (o + 8 <= riffEnd && guard++ < 100_000) {
|
|
364
|
+
const type = ascii(b, o, o + 4), len = dv.getUint32(o + 4, true);
|
|
365
|
+
const data = b.subarray(o + 8, Math.min(riffEnd, o + 8 + len));
|
|
366
|
+
if (type === "EXIF") {
|
|
367
|
+
const e = exifTexts(ascii(data, 0, 6) === "Exif\0\0" ? data.subarray(6) : data);
|
|
368
|
+
m.texts.push(...e.texts);
|
|
369
|
+
m.hasThumbnail ||= e.hasThumbnail;
|
|
370
|
+
}
|
|
371
|
+
else if (type === "XMP ")
|
|
372
|
+
m.texts.push(...xmpTexts(utf8(data)));
|
|
373
|
+
else if (type === "ANMF")
|
|
374
|
+
frames++;
|
|
375
|
+
else if (type === "VP8X" && data.length >= 10) {
|
|
376
|
+
m.width = 1 + (data[4] | (data[5] << 8) | (data[6] << 16));
|
|
377
|
+
m.height = 1 + (data[7] | (data[8] << 8) | (data[9] << 16));
|
|
378
|
+
}
|
|
379
|
+
o += 8 + len + (len & 1);
|
|
380
|
+
}
|
|
381
|
+
if (frames)
|
|
382
|
+
m.frames = frames;
|
|
383
|
+
setTrailing(b, riffEnd + (riffEnd & 1 && riffEnd < b.length ? 1 : 0), m);
|
|
384
|
+
}
|
|
385
|
+
function setTrailing(b, end, m) {
|
|
386
|
+
// a few bytes of padding or a newline after the end marker are common and mean nothing
|
|
387
|
+
let from = end;
|
|
388
|
+
while (from < b.length && (b[from] === 0 || b[from] === 0x0a || b[from] === 0x0d || b[from] === 0x20))
|
|
389
|
+
from++;
|
|
390
|
+
m.trailingBytes = b.length - from;
|
|
391
|
+
if (m.trailingBytes >= 4)
|
|
392
|
+
m.trailingLooksLike = looksLike(b.subarray(from));
|
|
393
|
+
}
|
|
394
|
+
/** Read an image file's container. Never throws: a file that cannot be parsed is reported as such. */
|
|
395
|
+
export function readImageMeta(bytes) {
|
|
396
|
+
const m = { format: detectImageFormat(bytes), texts: [], trailingBytes: 0, hasThumbnail: false, notes: [] };
|
|
397
|
+
try {
|
|
398
|
+
switch (m.format) {
|
|
399
|
+
case "png":
|
|
400
|
+
parsePng(bytes, m);
|
|
401
|
+
break;
|
|
402
|
+
case "jpeg":
|
|
403
|
+
parseJpeg(bytes, m);
|
|
404
|
+
break;
|
|
405
|
+
case "gif":
|
|
406
|
+
parseGif(bytes, m);
|
|
407
|
+
break;
|
|
408
|
+
case "webp":
|
|
409
|
+
parseWebp(bytes, m);
|
|
410
|
+
break;
|
|
411
|
+
case "tiff": {
|
|
412
|
+
const e = exifTexts(bytes);
|
|
413
|
+
m.texts.push(...e.texts);
|
|
414
|
+
m.notes.push("TIFF: data after the image is not measured");
|
|
415
|
+
break;
|
|
416
|
+
}
|
|
417
|
+
case "heic":
|
|
418
|
+
case "avif":
|
|
419
|
+
case "bmp":
|
|
420
|
+
case "ico":
|
|
421
|
+
m.notes.push(`${m.format.toUpperCase()}: metadata inside the file is not read`);
|
|
422
|
+
break;
|
|
423
|
+
default: break;
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
catch (e) {
|
|
427
|
+
m.notes.push(`the file's structure could not be read to the end (${e.message?.slice(0, 80) ?? "error"})`);
|
|
428
|
+
}
|
|
429
|
+
// drop empty and duplicate texts, cap the count
|
|
430
|
+
const seen = new Set();
|
|
431
|
+
m.texts = m.texts.filter((t) => t.text.length >= 3 && !seen.has(t.text) && seen.add(t.text)).slice(0, 80);
|
|
432
|
+
return m;
|
|
433
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Images: what a picture says to a person, and what it says only to a machine.
|
|
3
|
+
*
|
|
4
|
+
* An image reaches a model after a pipeline has handled it: resized, flattened, one frame picked, its
|
|
5
|
+
* metadata sometimes passed along as text. Each of those steps is a place to put words a person looking at
|
|
6
|
+
* the picture never sees. This module turns an image into text segments the engine's ordinary passes read:
|
|
7
|
+
*
|
|
8
|
+
* visible text recognised in the picture as shown, at full size
|
|
9
|
+
* concealed text that exists only after resizing without smoothing (an image-scaling attack), only
|
|
10
|
+
* after the contrast is raised, only on another background or under transparent pixels, only
|
|
11
|
+
* in a later frame; text in the file's metadata; the payload of a QR code
|
|
12
|
+
*
|
|
13
|
+
* Reading pixels needs the vision helper (native/ingress-vision.swift, macOS). Without it the container is
|
|
14
|
+
* still read (metadata, appended data, frames), and the result says the pixels were not inspected.
|
|
15
|
+
*
|
|
16
|
+
* Limits, stated plainly: the resizing check tries the common target sizes and three kernels, not every
|
|
17
|
+
* pipeline; noise crafted against one model (adversarial perturbation) is not text and is not detected;
|
|
18
|
+
* steganography that needs a decoder is not detected.
|
|
19
|
+
*/
|
|
20
|
+
import { type ImageMeta } from "./image-meta.js";
|
|
21
|
+
import { type QrVerdict } from "./qr.js";
|
|
22
|
+
import type { Finding, TextSegment } from "./types.js";
|
|
23
|
+
export interface VisionLine {
|
|
24
|
+
text: string;
|
|
25
|
+
conf: number;
|
|
26
|
+
x: number;
|
|
27
|
+
y: number;
|
|
28
|
+
w: number;
|
|
29
|
+
h: number;
|
|
30
|
+
}
|
|
31
|
+
export interface VisionReport {
|
|
32
|
+
version?: number;
|
|
33
|
+
/** false when the picture could not be opened or drawn */
|
|
34
|
+
decoded?: boolean;
|
|
35
|
+
width: number;
|
|
36
|
+
height: number;
|
|
37
|
+
frames: number;
|
|
38
|
+
hasAlpha: boolean;
|
|
39
|
+
full: VisionLine[];
|
|
40
|
+
variants: Array<{
|
|
41
|
+
name: string;
|
|
42
|
+
kind: "scale" | "smooth" | "contrast" | "alpha" | "frame";
|
|
43
|
+
lines: VisionLine[];
|
|
44
|
+
}>;
|
|
45
|
+
barcodes: Array<{
|
|
46
|
+
symbology: string;
|
|
47
|
+
payload: string;
|
|
48
|
+
variant: string;
|
|
49
|
+
}>;
|
|
50
|
+
notes: string[];
|
|
51
|
+
ms?: number;
|
|
52
|
+
}
|
|
53
|
+
/** Reads an image's pixels. The default runs the vision helper; tests and other platforms supply their own. */
|
|
54
|
+
export type VisionRunner = (path: string) => Promise<VisionReport>;
|
|
55
|
+
export interface ImageCoverage {
|
|
56
|
+
format: string;
|
|
57
|
+
/** were the pixels read (text recognition, codes, the resizing, contrast and transparency checks)? */
|
|
58
|
+
pixels: boolean;
|
|
59
|
+
/** why not, when they were not */
|
|
60
|
+
pixelsReason?: string;
|
|
61
|
+
metadata: boolean;
|
|
62
|
+
notes: string[];
|
|
63
|
+
}
|
|
64
|
+
export interface ImageInspection {
|
|
65
|
+
meta: ImageMeta;
|
|
66
|
+
vision?: VisionReport;
|
|
67
|
+
codes: Array<{
|
|
68
|
+
symbology: string;
|
|
69
|
+
payload: string;
|
|
70
|
+
verdict: QrVerdict;
|
|
71
|
+
}>;
|
|
72
|
+
segments: TextSegment[];
|
|
73
|
+
findings: Finding[];
|
|
74
|
+
coverage: ImageCoverage;
|
|
75
|
+
/** a stable description of what was found, hashed into the receipt */
|
|
76
|
+
report: string;
|
|
77
|
+
}
|
|
78
|
+
/** Where the built helper is expected: INGRESS_VISION_HELPER, or <state>/bin/ingress-vision. */
|
|
79
|
+
export declare function visionHelperPath(): string;
|
|
80
|
+
/** Why the helper cannot be used, or undefined when it can. A helper in the state folder must match the hash recorded when it was built. */
|
|
81
|
+
export declare function visionHelperProblem(path?: string): string | undefined;
|
|
82
|
+
/** The helper's source, shipped with the package. */
|
|
83
|
+
export declare function visionHelperSource(): string;
|
|
84
|
+
/**
|
|
85
|
+
* Compile the helper on this machine (macOS with the Swift compiler: Xcode or its command line tools) and
|
|
86
|
+
* record its hash. Built here rather than shipped as a binary, so what runs is what the source says.
|
|
87
|
+
*/
|
|
88
|
+
export declare function buildVisionHelper(out?: string): Promise<{
|
|
89
|
+
path: string;
|
|
90
|
+
sha256: string;
|
|
91
|
+
}>;
|
|
92
|
+
/** Compile one of the package's Swift helpers (ingress-vision, ingress-presence) and record its hash beside it. */
|
|
93
|
+
export declare function buildNativeHelper(name: "ingress-vision" | "ingress-presence", out?: string): Promise<{
|
|
94
|
+
path: string;
|
|
95
|
+
sha256: string;
|
|
96
|
+
}>;
|
|
97
|
+
export declare function helperRunner(path?: string, timeoutMs?: number): VisionRunner;
|
|
98
|
+
export interface InspectOptions {
|
|
99
|
+
/** how to read the pixels; `false` skips them; default: the helper when it is built */
|
|
100
|
+
vision?: VisionRunner | false;
|
|
101
|
+
/** the words shown beside the image (a caption, alt text, a file name), for comparison with what it holds */
|
|
102
|
+
caption?: string;
|
|
103
|
+
}
|
|
104
|
+
export declare function inspectImage(bytes: Uint8Array, opts?: InspectOptions): Promise<ImageInspection>;
|