nsh-core 0.8.0

Platform-independent core: shadow tree, styles, layout and mount instructions.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//! What platforms answer clipboard reads with: the root's
//! [`EventKind::Clipboard`](crate::EventKind::Clipboard) payloads for images and files
//! ([`Command::ReadClipboardImage`](crate::Command::ReadClipboardImage),
//! [`Command::ReadClipboardFiles`](crate::Command::ReadClipboardFiles)), and
//! the `data:` and `file:` URIs they carry, so every backend writes them
//! the same way.

use crate::{EventPayload, PayloadValue};

/// The largest image a clipboard read gives, in encoded bytes (48 MiB).
/// Its `data:` URI (a third larger) becomes a JS string the VM's heap holds
/// (256 MB for an embedded app), with whatever the app makes of it, such as
/// a `Blob`; running out of heap stops the VM, so a larger image is left
/// out of the answer, which carries its size instead (`tooLarge`; `has`
/// stays true): `Clipboard.readImage` rejects with
/// `ERR_NSH_QUOTA_EXCEEDED`, the Rust API reads it as none.
pub const MAX_IMAGE_BYTES: usize = 48 << 20;

/// The number of bytes base64 `data:` URI `uri` encodes, from its length
/// (nothing is decoded).
pub(crate) fn data_uri_len(uri: &str) -> usize {
    let data = uri.split_once(',').map_or("", |(_, data)| data);
    let padding = data.bytes().rev().take_while(|&b| b == b'=').count();
    (data.len() / 4 * 3 + data.len() % 4 * 3 / 4).saturating_sub(padding)
}

/// An image read from the clipboard, as the platform found it.
#[derive(Debug, Clone, Copy)]
pub struct Image<'a> {
    /// The encoded bytes (PNG, JPEG, ...).
    pub bytes: &'a [u8],
    /// Their MIME type (`image/png`).
    pub mime: &'a str,
    /// Width in pixels.
    pub width: u32,
    /// Height in pixels.
    pub height: u32,
}

impl<'a> Image<'a> {
    /// The image of encoded `bytes`, with the type and size read from their
    /// header (PNG, JPEG, GIF, BMP, WebP); `None` for other formats.
    pub fn sniff(bytes: &'a [u8]) -> Option<Image<'a>> {
        let mime = image_mime(bytes)?;
        let (width, height) = image_size(bytes)?;
        Some(Image {
            bytes,
            mime,
            width,
            height,
        })
    }
}

/// The answer to image read `id`: whether the clipboard holds an image and,
/// unless only that was asked (`image` is `None` then), the image as a
/// `data:` URI and its size. An image over [`MAX_IMAGE_BYTES`] is left
/// out, unencoded: `tooLarge` carries its size in bytes instead.
pub fn image_answer(id: u32, has: bool, image: Option<Image<'_>>) -> EventPayload {
    let mut fields = vec![
        ("id", PayloadValue::Number(id as f64)),
        ("has", PayloadValue::Bool(has || image.is_some())),
    ];
    match image {
        Some(image) if image.bytes.len() > MAX_IMAGE_BYTES => {
            fields.push(("tooLarge", PayloadValue::Number(image.bytes.len() as f64)));
        }
        Some(image) => {
            fields.push(("uri", PayloadValue::Str(data_uri(image.mime, image.bytes))));
            fields.push(("width", PayloadValue::Number(image.width as f64)));
            fields.push(("height", PayloadValue::Number(image.height as f64)));
        }
        None => {}
    }
    EventPayload::Map(fields)
}

/// The answer to files read `id`: the files' `file:` URLs (see
/// [`file_url`]), in the clipboard's order.
pub fn files_answer(id: u32, files: &[String]) -> EventPayload {
    EventPayload::Map(vec![
        ("id", PayloadValue::Number(id as f64)),
        ("has", PayloadValue::Bool(!files.is_empty())),
        ("files", PayloadValue::Str(files.join("\n"))),
    ])
}

/// A `data:` URI of `bytes` of type `mime`, base64 encoded.
pub fn data_uri(mime: &str, bytes: &[u8]) -> String {
    const TABLE: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    let mut out = String::with_capacity(13 + mime.len() + bytes.len().div_ceil(3) * 4);
    out.push_str("data:");
    out.push_str(mime);
    out.push_str(";base64,");
    for chunk in bytes.chunks(3) {
        let n = (u32::from(chunk[0]) << 16)
            | (u32::from(*chunk.get(1).unwrap_or(&0)) << 8)
            | u32::from(*chunk.get(2).unwrap_or(&0));
        let sextets = [n >> 18, n >> 12, n >> 6, n];
        for (i, s) in sextets.iter().enumerate() {
            if i <= chunk.len() {
                out.push(TABLE[(*s & 63) as usize] as char);
            } else {
                out.push('=');
            }
        }
    }
    out
}

/// The type and bytes of a base64 `data:` URI (`Clipboard.writeImage`'s
/// argument); `None` when it is not one.
pub fn decode_data_uri(uri: &str) -> Option<(&str, Vec<u8>)> {
    let rest = uri.strip_prefix("data:")?;
    let (meta, data) = rest.split_once(',')?;
    let mime = meta.strip_suffix(";base64")?;
    let value = |c: u8| match c {
        b'A'..=b'Z' => Some(c - b'A'),
        b'a'..=b'z' => Some(c - b'a' + 26),
        b'0'..=b'9' => Some(c - b'0' + 52),
        b'+' | b'-' => Some(62),
        b'/' | b'_' => Some(63),
        _ => None,
    };
    let mut out = Vec::with_capacity(data.len() / 4 * 3);
    let (mut acc, mut bits) = (0u32, 0);
    for c in data.bytes() {
        if c == b'=' || c.is_ascii_whitespace() {
            continue;
        }
        acc = (acc << 6) | u32::from(value(c)?);
        bits += 6;
        if bits >= 8 {
            bits -= 8;
            out.push((acc >> bits) as u8);
        }
    }
    Some((mime, out))
}

/// The `file:` URL of an absolute path (`/tmp/a b.png` is
/// `file:///tmp/a%20b.png`; on Windows `C:\a.png` is `file:///C:/a.png` and
/// `\\server\share\a` is `file://server/share/a`), its bytes outside the
/// unreserved ones and `/` percent-encoded as UTF-8.
pub fn file_url(path: &str) -> String {
    let path = path.replace('\\', "/");
    let (host, path) = match path.strip_prefix("//") {
        Some(unc) => unc.split_once('/').unwrap_or((unc, "")),
        None => ("", path.as_str()),
    };
    let mut out = String::from("file://");
    out.push_str(host);
    if !path.starts_with('/') {
        out.push('/');
    }
    for b in path.bytes() {
        match b {
            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'.' | b'_' | b'~' | b'/' | b':' => {
                out.push(b as char)
            }
            _ => out.push_str(&format!("%{b:02X}")),
        }
    }
    out
}

/// The MIME type of encoded image `bytes`, from their signature.
pub fn image_mime(bytes: &[u8]) -> Option<&'static str> {
    Some(match bytes {
        [0x89, b'P', b'N', b'G', ..] => "image/png",
        [0xFF, 0xD8, 0xFF, ..] => "image/jpeg",
        [b'G', b'I', b'F', b'8', ..] => "image/gif",
        [b'B', b'M', ..] => "image/bmp",
        [
            b'R',
            b'I',
            b'F',
            b'F',
            _,
            _,
            _,
            _,
            b'W',
            b'E',
            b'B',
            b'P',
            ..,
        ] => "image/webp",
        [b'I', b'I', 42, 0, ..] | [b'M', b'M', 0, 42, ..] => "image/tiff",
        _ => return None,
    })
}

/// The size in pixels of encoded image `bytes`, from their header (PNG,
/// JPEG, GIF, BMP, WebP).
pub fn image_size(bytes: &[u8]) -> Option<(u32, u32)> {
    let be16 = |i: usize| {
        Some(u32::from(u16::from_be_bytes(
            bytes.get(i..i + 2)?.try_into().ok()?,
        )))
    };
    let le16 = |i: usize| {
        Some(u32::from(u16::from_le_bytes(
            bytes.get(i..i + 2)?.try_into().ok()?,
        )))
    };
    let be32 = |i: usize| Some(u32::from_be_bytes(bytes.get(i..i + 4)?.try_into().ok()?));
    let le32 = |i: usize| Some(i32::from_le_bytes(bytes.get(i..i + 4)?.try_into().ok()?));
    let le24 = |i: usize| {
        let b = bytes.get(i..i + 3)?;
        Some(u32::from(b[0]) | (u32::from(b[1]) << 8) | (u32::from(b[2]) << 16))
    };
    match image_mime(bytes)? {
        "image/png" => Some((be32(16)?, be32(20)?)),
        "image/gif" => Some((le16(6)?, le16(8)?)),
        "image/bmp" => Some((le32(18)?.unsigned_abs(), le32(22)?.unsigned_abs())),
        "image/webp" => match bytes.get(12..16)? {
            b"VP8 " => Some((le16(26)? & 0x3FFF, le16(28)? & 0x3FFF)),
            b"VP8L" => {
                let b = u32::from_le_bytes(bytes.get(21..25)?.try_into().ok()?);
                Some(((b & 0x3FFF) + 1, ((b >> 14) & 0x3FFF) + 1))
            }
            b"VP8X" => Some((le24(24)? + 1, le24(27)? + 1)),
            _ => None,
        },
        "image/jpeg" => {
            // The first start-of-frame marker holds the size.
            let mut i = 2;
            while i + 9 < bytes.len() {
                if bytes[i] != 0xFF {
                    return None;
                }
                let marker = bytes[i + 1];
                let len = be16(i + 2)? as usize;
                let sof = matches!(marker, 0xC0..=0xCF) && !matches!(marker, 0xC4 | 0xC8 | 0xCC);
                if sof {
                    return Some((be16(i + 7)?, be16(i + 5)?));
                }
                i += 2 + len;
            }
            None
        }
        _ => None,
    }
}

/// HTML fragment `html` in Windows' `HTML Format` (CF_HTML): a header of
/// byte offsets into the UTF-8 that follows, then a document holding the
/// fragment between `StartFragment` and `EndFragment` comments, as apps
/// that paste HTML on Windows read it.
pub fn cf_html(html: &str) -> String {
    const HEADER: &str = "Version:0.9\r\nStartHTML:{0}\r\nEndHTML:{1}\r\n\
                          StartFragment:{2}\r\nEndFragment:{3}\r\n";
    const BEFORE: &str = "<html><body>\r\n<!--StartFragment-->";
    const AFTER: &str = "<!--EndFragment-->\r\n</body></html>";
    // The offsets are ten digits each, so the header's length is known.
    let header_len = HEADER.len() - 4 * 3 + 4 * 10;
    let start_html = header_len;
    let start_fragment = start_html + BEFORE.len();
    let end_fragment = start_fragment + html.len();
    let end_html = end_fragment + AFTER.len();
    let header = HEADER
        .replace("{0}", &format!("{start_html:010}"))
        .replace("{1}", &format!("{end_html:010}"))
        .replace("{2}", &format!("{start_fragment:010}"))
        .replace("{3}", &format!("{end_fragment:010}"));
    format!("{header}{BEFORE}{html}{AFTER}")
}

/// The fragment held in Windows' `HTML Format` bytes (`cf_html`'s
/// counterpart): what its `StartFragment` and `EndFragment` byte offsets
/// name, or the fragment comments when the offsets are unusable, or the
/// document when only its offsets or markup are usable. Bytes that carry no
/// CF_HTML header are not HTML in this format (`None`). The offsets are byte
/// offsets; malformed ones that split a UTF-8 sequence decode lossily, as
/// the clipboard's producer wrote them.
pub fn cf_html_fragment(bytes: &[u8]) -> Option<String> {
    let len = bytes.iter().position(|b| *b == 0).unwrap_or(bytes.len());
    let bytes = &bytes[..len];
    if bytes.is_empty() {
        return None;
    }
    let leading = bytes
        .iter()
        .position(|b| !b.is_ascii_whitespace())
        .unwrap_or(bytes.len());
    let decode = |bytes: &[u8]| String::from_utf8_lossy(bytes).into_owned();
    if !bytes[leading..].starts_with(b"Version:") {
        return None;
    }
    let header_end = bytes.iter().position(|b| *b == b'<').unwrap_or(bytes.len());
    let header = &bytes[..header_end];
    let field = |name: &str| {
        let prefix = format!("{name}:");
        for line in header.split(|b| *b == b'\n') {
            let line = line.strip_suffix(b"\r").unwrap_or(line);
            if let Some(value) = line.strip_prefix(prefix.as_bytes())
                && let Ok(value) = std::str::from_utf8(value)
                && let Ok(value) = value.trim().parse::<usize>()
            {
                return Some(value);
            }
        }
        None
    };
    let valid = |start: usize, end: usize| start <= end && end <= bytes.len();
    if let (Some(start), Some(end)) = (field("StartFragment"), field("EndFragment"))
        && valid(start, end)
    {
        return Some(decode(&bytes[start..end]));
    }
    let marker = |needle: &[u8]| bytes.windows(needle.len()).position(|w| w == needle);
    if let (Some(start), Some(end)) = (
        marker(b"<!--StartFragment-->").map(|i| i + "<!--StartFragment-->".len()),
        marker(b"<!--EndFragment-->"),
    ) && start <= end
    {
        return Some(decode(&bytes[start..end]));
    }
    if let (Some(start), Some(end)) = (field("StartHTML"), field("EndHTML"))
        && valid(start, end)
    {
        return Some(decode(&bytes[start..end]));
    }
    bytes
        .iter()
        .position(|b| *b == b'<')
        .map(|start| decode(&bytes[start..]))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn cf_html_offsets_point_at_the_document_and_the_fragment() {
        let fragment = "<p>a <b>b</b> \u{4e2d}</p>";
        let out = cf_html(fragment);
        let field = |name: &str| -> usize {
            let at = out.find(&format!("{name}:")).unwrap() + name.len() + 1;
            out[at..at + 10].parse().unwrap()
        };
        assert!(out.starts_with("Version:0.9\r\n"));
        assert!(out[field("StartHTML")..].starts_with("<html>"));
        assert_eq!(field("EndHTML"), out.len());
        assert_eq!(&out[field("StartFragment")..field("EndFragment")], fragment);
    }

    #[test]
    fn cf_html_fragments_round_trip() {
        let fragment = "<p>a <b>b</b> \u{4e2d}</p>";
        let mut bytes = cf_html(fragment).into_bytes();
        bytes.push(0);
        assert_eq!(cf_html_fragment(&bytes).as_deref(), Some(fragment));

        // A document without fragment offsets or comments is still HTML.
        let document = "Version:0.9\r\n<html><body><p>x</p></body></html>";
        assert_eq!(
            cf_html_fragment(document.as_bytes()).as_deref(),
            Some("<html><body><p>x</p></body></html>")
        );
        assert_eq!(cf_html_fragment(b"<p>plain html</p>"), None);
        assert_eq!(cf_html_fragment(b""), None);
    }

    /// A 3×2 PNG's signature and header.
    const PNG: &[u8] = &[
        0x89, b'P', b'N', b'G', 13, 10, 26, 10, 0, 0, 0, 13, b'I', b'H', b'D', b'R', 0, 0, 0, 3, 0,
        0, 0, 2, 8, 6, 0, 0, 0,
    ];

    #[test]
    fn data_uris_round_trip() {
        for bytes in [&b""[..], b"h", b"hi", b"hi!", b"hi!!", PNG] {
            let uri = data_uri("image/png", bytes);
            let (mime, back) = decode_data_uri(&uri).unwrap();
            assert_eq!((mime, &back[..]), ("image/png", bytes));
        }
        assert_eq!(data_uri("text/plain", b"hi"), "data:text/plain;base64,aGk=");
        assert!(decode_data_uri("data:text/plain,hi").is_none());
        assert!(decode_data_uri("data:;base64,a*b").is_none());
        assert!(decode_data_uri("https://example.com/a.png").is_none());
    }

    #[test]
    fn file_urls_escape_paths() {
        assert_eq!(file_url("/tmp/a b#1.png"), "file:///tmp/a%20b%231.png");
        assert_eq!(file_url("/tmp/中/"), "file:///tmp/%E4%B8%AD/");
        assert_eq!(file_url(r"C:\Users\a b.txt"), "file:///C:/Users/a%20b.txt");
        assert_eq!(
            file_url(r"\\server\share\a.txt"),
            "file://server/share/a.txt"
        );
    }

    #[test]
    fn image_headers_give_type_and_size() {
        let image = Image::sniff(PNG).unwrap();
        assert_eq!((image.mime, image.width, image.height), ("image/png", 3, 2));
        let gif = b"GIF89a\x05\x00\x04\x00";
        assert_eq!(image_size(gif), Some((5, 4)));
        let mut bmp = vec![b'B', b'M'];
        bmp.resize(18, 0);
        bmp.extend(7i32.to_le_bytes());
        bmp.extend((-6i32).to_le_bytes());
        assert_eq!(image_size(&bmp), Some((7, 6)));
        // SOI, an APP0 segment, then SOF0 (height 9, width 10).
        let jpeg = [
            0xFF, 0xD8, 0xFF, 0xE0, 0, 4, 0, 0, 0xFF, 0xC0, 0, 11, 8, 0, 9, 0, 10, 3, 0, 0, 0,
        ];
        assert_eq!(image_mime(&jpeg), Some("image/jpeg"));
        assert_eq!(image_size(&jpeg), Some((10, 9)));
        assert_eq!(image_mime(b"II*\0rest"), Some("image/tiff"));
        assert_eq!(image_size(b"II*\0rest"), None);
        assert!(Image::sniff(b"plain text").is_none());
    }

    #[test]
    fn images_over_the_limit_answer_their_size() {
        let mut bytes = PNG.to_vec();
        bytes.resize(MAX_IMAGE_BYTES + 1, 0);
        let image = Image::sniff(&bytes).unwrap();
        assert_eq!(
            image_answer(1, true, Some(image)),
            EventPayload::Map(vec![
                ("id", PayloadValue::Number(1.0)),
                ("has", PayloadValue::Bool(true)),
                (
                    "tooLarge",
                    PayloadValue::Number((MAX_IMAGE_BYTES + 1) as f64)
                ),
            ])
        );
        bytes.truncate(MAX_IMAGE_BYTES);
        let EventPayload::Map(fields) = image_answer(1, true, Image::sniff(&bytes)) else {
            panic!()
        };
        assert!(
            matches!(&fields[2], ("uri", PayloadValue::Str(u)) if data_uri_len(u) == MAX_IMAGE_BYTES)
        );
    }

    #[test]
    fn data_uri_lengths_count_the_bytes() {
        for len in [0, 1, 2, 3, 4, 5, 100, 101, 102] {
            assert_eq!(data_uri_len(&data_uri("image/png", &vec![7; len])), len);
        }
        // Without its padding.
        assert_eq!(data_uri_len("data:text/plain;base64,aGk"), 2);
        assert_eq!(data_uri_len("https://example.com/a.png"), 0);
    }

    #[test]
    fn answers_carry_images_and_files() {
        let image = Image::sniff(PNG).unwrap();
        let EventPayload::Map(fields) = image_answer(4, true, Some(image)) else {
            panic!()
        };
        assert_eq!(fields[0], ("id", PayloadValue::Number(4.0)));
        assert_eq!(fields[1], ("has", PayloadValue::Bool(true)));
        assert!(
            matches!(&fields[2], ("uri", PayloadValue::Str(u)) if u.starts_with("data:image/png;base64,") && data_uri_len(u) == PNG.len())
        );
        assert_eq!(
            fields[3..],
            [
                ("width", PayloadValue::Number(3.0)),
                ("height", PayloadValue::Number(2.0))
            ]
        );
        assert_eq!(
            files_answer(5, &["file:///a".into(), "file:///b/".into()]),
            EventPayload::Map(vec![
                ("id", PayloadValue::Number(5.0)),
                ("has", PayloadValue::Bool(true)),
                ("files", PayloadValue::Str("file:///a\nfile:///b/".into())),
            ])
        );
    }
}