rs-rich-micro 0.0.10

Micro assets for rs-rich: emoji-sized inline images, packages, a layered registry and :micro: markup
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
//! `.richmicro` packages and packs (#580), read-only.
//!
//! A **package** is one asset: a zip archive or a directory holding a
//! `manifest.json` (`schema_version: 1`), a static image, and optionally an
//! animation (GIF, APNG or WebP) and a frame sequence:
//!
//! ```json
//! {
//!   "schema_version": 1,
//!   "name": "status/success",
//!   "alt": "green check mark",
//!   "size": "2x1",
//!   "fallback": { "emoji": "✅", "text": "OK" },
//!   "static": "static.png",
//!   "animation": "animation.gif",
//!   "aliases": ["ok"],
//!   "version": "1.0.0",
//!   "license": "CC0-1.0",
//!   "author": "rs-rich contributors"
//! }
//! ```
//!
//! A **pack** is a directory or zip archive with a `pack.json` listing its
//! packages (folders or, in a pack directory, `.richmicro` archives):
//!
//! ```json
//! { "schema_version": 1, "name": "status", "version": "1.0.0",
//!   "packages": ["success", "warning.richmicro"] }
//! ```
//!
//! Everything is untrusted input. Hard [`Limits`] bound the archive, every
//! file, the manifest, the entry count, pixel dimensions, frame counts and
//! the bytes an asset takes decoded; images are checked by their headers,
//! never decoded here. Paths must stay inside the package: absolute paths,
//! `..`, backslashes, and symbolic links (in an archive, any; in a
//! directory, any that leads out of it) are refused.

use std::cell::Cell;
use std::fs::File;
use std::io::Read;
use std::path::{Path, PathBuf};

use serde_json::{Map, Value};

use crate::error::MicroError;
use crate::image::sniff;
use crate::model::{
    AssetKind, CellSize, ImageFormat, ImageRef, Layer, MicroAsset, Origin, PackageLocation,
    Variants,
};
use crate::name::check_name;

/// The manifest's file name.
pub const MANIFEST: &str = "manifest.json";
/// A pack's index file name.
pub const PACK_INDEX: &str = "pack.json";
/// The only manifest and pack schema this release reads.
pub const SCHEMA_VERSION: u64 = 1;

/// Hard limits on untrusted packages. The defaults suit emoji-sized assets
/// with generous headroom; anything beyond them is refused, not truncated.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Limits {
    /// An archive's size on disk.
    pub max_archive_bytes: u64,
    /// Any one file, uncompressed.
    pub max_file_bytes: u64,
    /// `manifest.json` and `pack.json`.
    pub max_manifest_bytes: u64,
    /// Entries in one archive.
    pub max_entries: usize,
    /// Bytes read, uncompressed, from one archive in total.
    pub max_total_bytes: u64,
    /// Width or height of any image, in pixels.
    pub max_dimension: u32,
    /// Frames in one asset: its animation's plus its frame sequence's.
    pub max_frames: u32,
    /// What one asset takes decoded (RGBA, every frame at canvas size).
    pub max_decoded_bytes: u64,
    /// Packages in one pack.
    pub max_packages: usize,
}

impl Default for Limits {
    fn default() -> Self {
        Limits {
            max_archive_bytes: 8 << 20,
            max_file_bytes: 2 << 20,
            max_manifest_bytes: 64 << 10,
            max_entries: 512,
            max_total_bytes: 16 << 20,
            max_dimension: 256,
            max_frames: 240,
            max_decoded_bytes: 32 << 20,
            max_packages: 1024,
        }
    }
}

/// A loaded pack: its assets, and the packages in it that failed, each with
/// its reason. One bad package does not reject the pack.
#[derive(Clone, Debug)]
pub struct Pack {
    pub name: String,
    pub version: Option<String>,
    pub assets: Vec<MicroAsset>,
    /// `(package path within the pack, why it was rejected)`.
    pub rejected: Vec<(String, MicroError)>,
}

/// What a path held: one package or a pack.
#[derive(Clone, Debug)]
pub enum Loaded {
    Package(Box<MicroAsset>),
    Pack(Pack),
}

/// Check a path from a manifest or an archive: relative, `/`-separated, no
/// empty, `.` or `..` segments, no backslashes, drive letters, NULs or other
/// control characters.
/// Returns it normalised (without a trailing `/`).
pub fn safe_relative(path: &str) -> Result<String, MicroError> {
    let unsafe_path = |why: &str| MicroError::UnsafePath(format!("unsafe path {path:?}: {why}"));
    if path.is_empty() {
        return Err(unsafe_path("empty"));
    }
    if path.starts_with('/') {
        return Err(unsafe_path("absolute"));
    }
    if path.contains('\\') || path.contains('\0') || path.contains(':') {
        return Err(unsafe_path("backslashes, colons and NULs are not allowed"));
    }
    // Paths are quoted in messages and `rich micro show`: no C0 or C1
    // control or DEL may reach the terminal through one.
    if path.chars().any(char::is_control) {
        return Err(unsafe_path("control characters are not allowed"));
    }
    let trimmed = path.strip_suffix('/').unwrap_or(path);
    for segment in trimmed.split('/') {
        match segment {
            "" | "." => return Err(unsafe_path("empty or `.` segment")),
            ".." => return Err(unsafe_path("`..` leads out of the package")),
            _ => {}
        }
    }
    Ok(trimmed.to_string())
}

fn io_error(path: &Path, error: impl std::fmt::Display) -> MicroError {
    MicroError::Io(format!("{}: {error}", path.display()))
}

/// A FIFO, socket or device where a package or archive was expected.
fn not_a_file(path: &Path) -> MicroError {
    MicroError::Io(format!(
        "{} is not a regular file or a directory",
        path.display()
    ))
}

/// Read at most `limit` bytes; more is an error, whatever a header claimed.
fn read_bounded(mut reader: impl Read, limit: u64, what: &str) -> Result<Vec<u8>, MicroError> {
    let mut out = Vec::new();
    reader
        .by_ref()
        .take(limit + 1)
        .read_to_end(&mut out)
        .map_err(|e| MicroError::Io(format!("{what}: {e}")))?;
    if out.len() as u64 > limit {
        return Err(MicroError::Limit(format!(
            "{what} is larger than {limit} bytes"
        )));
    }
    Ok(out)
}

/// Where files are read from: a directory, or a checked zip archive.
enum Source {
    Directory {
        root: PathBuf,
        canonical: PathBuf,
    },
    Archive {
        path: PathBuf,
        archive: Box<zip::ZipArchive<File>>,
        read_total: Cell<u64>,
    },
    /// The compiled-in built-in set ([`crate::builtin`]), under `root`.
    Embedded {
        root: String,
    },
}

impl Source {
    fn directory(root: &Path) -> Result<Self, MicroError> {
        let canonical = root.canonicalize().map_err(|e| io_error(root, e))?;
        Ok(Source::Directory {
            root: root.to_path_buf(),
            canonical,
        })
    }

    /// Open an archive and check every entry before reading any: the size,
    /// the count, each name, and no links, encryption or duplicates.
    fn archive(path: &Path, limits: &Limits) -> Result<Self, MicroError> {
        let metadata = std::fs::metadata(path).map_err(|e| io_error(path, e))?;
        if !metadata.is_file() {
            return Err(not_a_file(path));
        }
        if metadata.len() > limits.max_archive_bytes {
            return Err(MicroError::Limit(format!(
                "{} is larger than {} bytes",
                path.display(),
                limits.max_archive_bytes
            )));
        }
        let file = File::open(path).map_err(|e| io_error(path, e))?;
        // What was opened, in case the path changed since it was checked.
        if !file.metadata().is_ok_and(|m| m.is_file()) {
            return Err(not_a_file(path));
        }
        let mut archive = zip::ZipArchive::new(file).map_err(|e| io_error(path, e))?;
        if archive.len() > limits.max_entries {
            return Err(MicroError::Limit(format!(
                "{} has more than {} entries",
                path.display(),
                limits.max_entries
            )));
        }
        let mut seen = std::collections::HashSet::new();
        for index in 0..archive.len() {
            let entry = archive.by_index_raw(index).map_err(|e| io_error(path, e))?;
            let name = entry.name().map_err(|e| io_error(path, e))?.into_owned();
            let normal = safe_relative(&name)
                .map_err(|e| MicroError::UnsafePath(format!("{}: {e}", path.display())))?;
            if entry.is_symlink() {
                return Err(MicroError::UnsafePath(format!(
                    "{}: {name:?} is a symbolic link",
                    path.display()
                )));
            }
            if entry.encrypted() {
                return Err(MicroError::Manifest(format!(
                    "{}: {name:?} is encrypted",
                    path.display()
                )));
            }
            if !entry.is_dir() && entry.size() > limits.max_file_bytes {
                return Err(MicroError::Limit(format!(
                    "{}: {name:?} is larger than {} bytes",
                    path.display(),
                    limits.max_file_bytes
                )));
            }
            if !seen.insert(normal) {
                return Err(MicroError::Manifest(format!(
                    "{}: {name:?} appears twice",
                    path.display()
                )));
            }
        }
        Ok(Source::Archive {
            path: path.to_path_buf(),
            archive: Box::new(archive),
            read_total: Cell::new(0),
        })
    }

    fn display(&self, relative: &str) -> String {
        match self {
            Source::Directory { root, .. } => root.join(relative).display().to_string(),
            Source::Archive { path, .. } => format!("{}!{relative}", path.display()),
            Source::Embedded { root } => format!("builtin:{}", join(root, relative)),
        }
    }

    fn exists(&mut self, relative: &str) -> bool {
        match self {
            Source::Directory { root, .. } => root.join(relative).is_file(),
            Source::Archive { archive, .. } => archive.index_for_name(relative).is_some(),
            Source::Embedded { root } => crate::builtin::file(&join(root, relative)).is_some(),
        }
    }

    fn is_dir(&mut self, relative: &str) -> bool {
        match self {
            Source::Directory { root, .. } => root.join(relative).is_dir(),
            Source::Archive { archive, .. } => {
                let prefix = format!("{relative}/");
                archive
                    .file_names()
                    .any(|name| name.is_ok_and(|name| name.starts_with(&prefix)))
            }
            Source::Embedded { root } => crate::builtin::is_dir(&join(root, relative)),
        }
    }

    /// Read `relative` (already [`safe_relative`]), at most `limit` bytes.
    fn read(&mut self, relative: &str, limit: u64, limits: &Limits) -> Result<Vec<u8>, MicroError> {
        let shown = self.display(relative);
        match self {
            Source::Directory { root, canonical } => {
                let path = root.join(relative);
                let resolved = path.canonicalize().map_err(|e| io_error(&path, e))?;
                if !resolved.starts_with(&*canonical) {
                    return Err(MicroError::UnsafePath(format!(
                        "{shown} links out of the package"
                    )));
                }
                let metadata = std::fs::metadata(&resolved).map_err(|e| io_error(&path, e))?;
                if !metadata.is_file() {
                    return Err(MicroError::Io(format!("{shown} is not a file")));
                }
                if metadata.len() > limit {
                    return Err(MicroError::Limit(format!(
                        "{shown} is larger than {limit} bytes"
                    )));
                }
                let file = File::open(&resolved).map_err(|e| io_error(&path, e))?;
                read_bounded(file, limit, &shown)
            }
            Source::Archive {
                archive,
                read_total,
                ..
            } => {
                let entry = archive
                    .by_name(relative)
                    .map_err(|e| MicroError::Io(format!("{shown}: {e}")))?;
                if !entry.is_file() {
                    return Err(MicroError::Io(format!("{shown} is not a file")));
                }
                let left = limits.max_total_bytes.saturating_sub(read_total.get());
                let bytes = read_bounded(entry, limit.min(left), &shown).map_err(|e| {
                    if limit > left {
                        MicroError::Limit(format!(
                            "{shown}: the archive holds more than {} bytes uncompressed",
                            limits.max_total_bytes
                        ))
                    } else {
                        e
                    }
                })?;
                read_total.set(read_total.get() + bytes.len() as u64);
                Ok(bytes)
            }
            Source::Embedded { root } => {
                let bytes = crate::builtin::file(&join(root, relative))
                    .ok_or_else(|| MicroError::Io(format!("{shown}: no such file")))?;
                if bytes.len() as u64 > limit {
                    return Err(MicroError::Limit(format!(
                        "{shown} is larger than {limit} bytes"
                    )));
                }
                Ok(bytes.to_vec())
            }
        }
    }

    fn location(&self, prefix: &str) -> PackageLocation {
        match self {
            Source::Directory { root, .. } => PackageLocation::Directory(if prefix.is_empty() {
                root.clone()
            } else {
                root.join(prefix)
            }),
            Source::Archive { path, .. } => PackageLocation::Archive {
                path: path.clone(),
                prefix: prefix.to_string(),
            },
            Source::Embedded { root } => PackageLocation::BuiltIn(join(root, prefix)),
        }
    }
}

fn join(prefix: &str, relative: &str) -> String {
    if prefix.is_empty() {
        relative.to_string()
    } else {
        format!("{prefix}/{relative}")
    }
}

/// Read one file of a loaded package, under `limits`. Used by
/// [`MicroAsset::read_variant`].
pub(crate) fn read_file(
    location: &PackageLocation,
    relative: &str,
    limits: &Limits,
) -> Result<Vec<u8>, MicroError> {
    let relative = safe_relative(relative)?;
    match location {
        PackageLocation::Directory(root) => {
            Source::directory(root)?.read(&relative, limits.max_file_bytes, limits)
        }
        PackageLocation::Archive { path, prefix } => Source::archive(path, limits)?.read(
            &join(prefix, &relative),
            limits.max_file_bytes,
            limits,
        ),
        PackageLocation::BuiltIn(prefix) => Source::Embedded {
            root: prefix.clone(),
        }
        .read(&relative, limits.max_file_bytes, limits),
    }
}

/// Load the compiled-in pack at `root` (`status`) into `layer`.
pub(crate) fn load_embedded_pack(
    root: &str,
    layer: Layer,
    limits: &Limits,
) -> Result<Pack, MicroError> {
    let mut source = Source::Embedded {
        root: root.to_string(),
    };
    read_pack(&mut source, None, layer, limits)
}

fn parse_json(bytes: &[u8], what: &str) -> Result<Map<String, Value>, MicroError> {
    match serde_json::from_slice::<Value>(bytes) {
        Ok(Value::Object(map)) => Ok(map),
        Ok(_) => Err(MicroError::Manifest(format!("{what}: not a JSON object"))),
        Err(e) => Err(MicroError::Manifest(format!("{what}: invalid JSON: {e}"))),
    }
}

fn check_schema(map: &Map<String, Value>, what: &str) -> Result<(), MicroError> {
    match map.get("schema_version") {
        Some(Value::Number(n)) if n.as_u64() == Some(SCHEMA_VERSION) => Ok(()),
        Some(other) => Err(MicroError::Manifest(format!(
            "{what}: unsupported schema_version {other}; this release reads {SCHEMA_VERSION}"
        ))),
        None => Err(MicroError::Manifest(format!(
            "{what}: missing schema_version"
        ))),
    }
}

fn check_keys(map: &Map<String, Value>, allowed: &[&str], what: &str) -> Result<(), MicroError> {
    match map.keys().find(|key| !allowed.contains(&key.as_str())) {
        Some(key) => Err(MicroError::Manifest(format!(
            "{what}: unknown field {key:?}"
        ))),
        None => Ok(()),
    }
}

fn string<'a>(
    map: &'a Map<String, Value>,
    key: &str,
    what: &str,
) -> Result<Option<&'a str>, MicroError> {
    match map.get(key) {
        None | Some(Value::Null) => Ok(None),
        Some(Value::String(s)) => Ok(Some(s)),
        Some(_) => Err(MicroError::Manifest(format!(
            "{what}: {key:?} must be a string"
        ))),
    }
}

/// [`string`] for free text shown to people (`version`, `license`,
/// `author`, `description`): no C0 or C1 control or DEL, which a terminal
/// would run rather than show.
fn plain<'a>(
    map: &'a Map<String, Value>,
    key: &str,
    what: &str,
) -> Result<Option<&'a str>, MicroError> {
    let value = string(map, key, what)?;
    if value.is_some_and(|value| value.chars().any(char::is_control)) {
        return Err(MicroError::Manifest(format!(
            "{what}: {key:?} holds control characters"
        )));
    }
    Ok(value)
}

fn required<'a>(map: &'a Map<String, Value>, key: &str, what: &str) -> Result<&'a str, MicroError> {
    string(map, key, what)?.ok_or_else(|| MicroError::Manifest(format!("{what}: missing {key:?}")))
}

fn strings(map: &Map<String, Value>, key: &str, what: &str) -> Result<Vec<String>, MicroError> {
    match map.get(key) {
        None | Some(Value::Null) => Ok(Vec::new()),
        Some(Value::Array(items)) => items
            .iter()
            .map(|item| match item {
                Value::String(s) => Ok(s.clone()),
                _ => Err(MicroError::Manifest(format!(
                    "{what}: {key:?} must be a list of strings"
                ))),
            })
            .collect(),
        Some(_) => Err(MicroError::Manifest(format!(
            "{what}: {key:?} must be a list of strings"
        ))),
    }
}

fn size(map: &Map<String, Value>, what: &str) -> Result<CellSize, MicroError> {
    let in_manifest = |e: MicroError| MicroError::InvalidSize(format!("{what}: {e}"));
    match map.get("size") {
        None | Some(Value::Null) => Ok(CellSize::default()),
        Some(Value::String(s)) => s.parse().map_err(in_manifest),
        Some(Value::Object(size)) => {
            let get = |key: &str| match size.get(key) {
                Some(Value::Number(n)) => n
                    .as_u64()
                    .and_then(|n| u16::try_from(n).ok())
                    .ok_or_else(|| {
                        MicroError::InvalidSize(format!("{what}: size.{key} is out of range"))
                    }),
                None if key == "rows" => Ok(1),
                _ => Err(MicroError::InvalidSize(format!(
                    "{what}: size.{key} must be a number"
                ))),
            };
            CellSize::new(get("cols")?, get("rows")?).map_err(in_manifest)
        }
        Some(_) => Err(MicroError::InvalidSize(format!(
            "{what}: size must be \"COLSxROWS\" or {{\"cols\": …, \"rows\": …}}"
        ))),
    }
}

const MANIFEST_KEYS: &[&str] = &[
    "schema_version",
    "name",
    "kind",
    "size",
    "alt",
    "fallback",
    "static",
    "animation",
    "frames",
    "aliases",
    "version",
    "license",
    "author",
];

/// Read the package at `prefix` in `source`.
fn read_package(
    source: &mut Source,
    prefix: &str,
    origin: Origin,
    limits: &Limits,
) -> Result<MicroAsset, MicroError> {
    let manifest_path = join(prefix, MANIFEST);
    let what = source.display(&manifest_path);
    let bytes = source.read(&manifest_path, limits.max_manifest_bytes, limits)?;
    let map = parse_json(&bytes, &what)?;
    check_schema(&map, &what)?;
    check_keys(&map, MANIFEST_KEYS, &what)?;

    let name = required(&map, "name", &what)?;
    let alt = required(&map, "alt", &what)?;
    let in_manifest = |e: MicroError| match e {
        MicroError::InvalidName(_) => e,
        other => MicroError::Manifest(format!("{what}: {other}")),
    };
    let mut asset = MicroAsset::new(name, alt)
        .map_err(in_manifest)?
        .with_size(size(&map, &what)?)
        .map_err(in_manifest)?;
    match map.get("fallback") {
        None | Some(Value::Null) => {}
        Some(Value::Object(fallback)) => {
            check_keys(fallback, &["emoji", "text"], &format!("{what}: fallback"))?;
            if let Some(emoji) = string(fallback, "emoji", &what)? {
                asset = asset.with_emoji(emoji).map_err(in_manifest)?;
            }
            if let Some(text) = string(fallback, "text", &what)? {
                asset = asset.with_text(text).map_err(in_manifest)?;
            }
        }
        Some(_) => {
            return Err(MicroError::Manifest(format!(
                "{what}: fallback must be an object with \"emoji\" and/or \"text\""
            )));
        }
    }
    for alias in strings(&map, "aliases", &what)? {
        asset = asset.with_alias(&alias).map_err(in_manifest)?;
    }
    if let Some(version) = plain(&map, "version", &what)? {
        asset = asset.with_version(version);
    }
    if let Some(license) = plain(&map, "license", &what)? {
        asset = asset.with_license(license);
    }
    if let Some(author) = plain(&map, "author", &what)? {
        asset = asset.with_author(author);
    }

    // Images: header-checked against the limits, never decoded.
    let mut load = |relative: &str, animation: bool| -> Result<ImageRef, MicroError> {
        let relative = safe_relative(relative)?;
        let full = join(prefix, &relative);
        let bytes = source.read(&full, limits.max_file_bytes, limits)?;
        let shown = source.display(&full);
        let info = sniff(&bytes, limits.max_frames)
            .map_err(|e| MicroError::Image(format!("{shown}: {e}")))?;
        if info.width > limits.max_dimension || info.height > limits.max_dimension {
            return Err(MicroError::Limit(format!(
                "{shown} is {}x{} pixels; the limit is {} on each side",
                info.width, info.height, limits.max_dimension
            )));
        }
        if info.frames > limits.max_frames {
            return Err(MicroError::Limit(format!(
                "{shown} has more than {} frames",
                limits.max_frames
            )));
        }
        if !animation && info.frames > 1 {
            return Err(MicroError::Image(format!(
                "{shown} is animated; the static image and frames must be still images"
            )));
        }
        if animation && info.format == ImageFormat::Png {
            return Err(MicroError::Image(format!(
                "{shown} is a still PNG; an animation is a GIF, APNG or WebP"
            )));
        }
        Ok(ImageRef {
            path: relative,
            info,
            bytes: bytes.len() as u64,
        })
    };
    let static_path = required(&map, "static", &what)?;
    let static_image = load(static_path, false)?;
    let animation = string(&map, "animation", &what)?
        .map(|path| load(path, true))
        .transpose()?;
    let frame_paths = strings(&map, "frames", &what)?;
    if frame_paths.len() as u64 > limits.max_frames as u64 {
        return Err(MicroError::Limit(format!(
            "{what}: more than {} frames",
            limits.max_frames
        )));
    }
    let frames = frame_paths
        .iter()
        .map(|path| load(path, false))
        .collect::<Result<Vec<_>, _>>()?;
    let variants = Variants {
        static_image: Some(static_image),
        animation,
        frames,
    };
    let total_frames: u64 = variants
        .iter()
        .map(|image| image.info.frames as u64)
        .sum::<u64>()
        - 1;
    if total_frames > limits.max_frames as u64 {
        return Err(MicroError::Limit(format!(
            "{what}: more than {} frames in all",
            limits.max_frames
        )));
    }
    let decoded: u64 = variants
        .iter()
        .map(|image| image.info.decoded_bytes())
        .sum();
    if decoded > limits.max_decoded_bytes {
        return Err(MicroError::Limit(format!(
            "{what}: its images take {decoded} bytes decoded; the limit is {}",
            limits.max_decoded_bytes
        )));
    }

    let kind = match string(&map, "kind", &what)? {
        Some(kind) => kind.parse().map_err(in_manifest)?,
        None if variants.animation.is_some() || !variants.frames.is_empty() => AssetKind::Animated,
        None => AssetKind::Static,
    };
    let asset = asset
        .with_kind(kind)
        .with_variants(variants)
        .with_origin(Origin {
            location: Some(source.location(prefix)),
            ..origin
        });
    asset.validate().map_err(in_manifest)?;
    Ok(asset)
}

const PACK_KEYS: &[&str] = &[
    "schema_version",
    "name",
    "version",
    "description",
    "license",
    "author",
    "packages",
];

/// Read the pack whose `pack.json` is at `source`'s root.
fn read_pack(
    source: &mut Source,
    pack_dir: Option<&Path>,
    layer: Layer,
    limits: &Limits,
) -> Result<Pack, MicroError> {
    let what = source.display(PACK_INDEX);
    let bytes = source.read(PACK_INDEX, limits.max_manifest_bytes, limits)?;
    let map = parse_json(&bytes, &what)?;
    check_schema(&map, &what)?;
    check_keys(&map, PACK_KEYS, &what)?;
    let name = required(&map, "name", &what)?;
    check_name(name)?;
    for key in ["description", "license", "author"] {
        plain(&map, key, &what)?;
    }
    let packages = strings(&map, "packages", &what)?;
    if packages.len() > limits.max_packages {
        return Err(MicroError::Limit(format!(
            "{what}: more than {} packages",
            limits.max_packages
        )));
    }
    let mut pack = Pack {
        name: name.to_string(),
        version: plain(&map, "version", &what)?.map(str::to_string),
        assets: Vec::new(),
        rejected: Vec::new(),
    };
    for package in packages {
        let origin = Origin {
            layer,
            pack: Some(pack.name.clone()),
            location: None,
        };
        let result = safe_relative(&package).and_then(|relative| {
            if source.is_dir(&relative) {
                return read_package(source, &relative, origin, limits);
            }
            // A `.richmicro` archive inside a pack directory. (Inside a pack
            // archive, packages are folders: archives do not nest.)
            match (pack_dir, source.exists(&relative)) {
                (Some(dir), true) => {
                    let path = dir.join(&relative);
                    // `read` checks it does not link out of the pack.
                    let canonical_dir = dir.canonicalize().map_err(|e| io_error(dir, e))?;
                    let resolved = path.canonicalize().map_err(|e| io_error(&path, e))?;
                    if !resolved.starts_with(&canonical_dir) {
                        return Err(MicroError::UnsafePath(format!(
                            "{} links out of the pack",
                            path.display()
                        )));
                    }
                    let mut inner = Source::archive(&path, limits)?;
                    read_package(&mut inner, "", origin, limits)
                }
                _ => Err(MicroError::Io(format!(
                    "{}: no such package",
                    source.display(&relative)
                ))),
            }
        });
        match result {
            Ok(asset) => pack.assets.push(asset),
            Err(error) => pack.rejected.push((package, error)),
        }
    }
    Ok(pack)
}

/// Load a package from a directory or a zip archive.
pub fn load_package(path: &Path, layer: Layer, limits: &Limits) -> Result<MicroAsset, MicroError> {
    match load(path, layer, limits)? {
        Loaded::Package(asset) => Ok(*asset),
        Loaded::Pack(_) => Err(MicroError::Manifest(format!(
            "{} is a pack, not a package",
            path.display()
        ))),
    }
}

/// Load a pack from a directory or a zip archive.
pub fn load_pack(path: &Path, layer: Layer, limits: &Limits) -> Result<Pack, MicroError> {
    match load(path, layer, limits)? {
        Loaded::Pack(pack) => Ok(pack),
        Loaded::Package(_) => Err(MicroError::Manifest(format!(
            "{} is a package, not a pack",
            path.display()
        ))),
    }
}

/// Load whatever `path` holds: a package (`manifest.json`) or a pack
/// (`pack.json`), as a directory or a zip archive.
pub fn load(path: &Path, layer: Layer, limits: &Limits) -> Result<Loaded, MicroError> {
    let metadata = std::fs::symlink_metadata(path).map_err(|e| io_error(path, e))?;
    let metadata = if metadata.file_type().is_symlink() {
        std::fs::metadata(path).map_err(|e| io_error(path, e))?
    } else {
        metadata
    };
    let is_dir = metadata.is_dir();
    // Only a regular file is opened as an archive: opening a FIFO blocks
    // until something writes to it, and a device or socket is no archive.
    if !is_dir && !metadata.is_file() {
        return Err(not_a_file(path));
    }
    let mut source = if is_dir {
        Source::directory(path)?
    } else {
        Source::archive(path, limits)?
    };
    let origin = Origin {
        layer,
        pack: None,
        location: None,
    };
    if source.exists(MANIFEST) {
        read_package(&mut source, "", origin, limits).map(|asset| Loaded::Package(Box::new(asset)))
    } else if source.exists(PACK_INDEX) {
        read_pack(&mut source, is_dir.then_some(path), layer, limits).map(Loaded::Pack)
    } else {
        Err(MicroError::Manifest(format!(
            "{}: no {MANIFEST} or {PACK_INDEX}",
            path.display()
        )))
    }
}

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

    #[test]
    fn relative_paths() {
        assert_eq!(safe_relative("a/b.png").unwrap(), "a/b.png");
        assert_eq!(safe_relative("frames/").unwrap(), "frames");
        for bad in [
            "",
            "/etc/passwd",
            "../x",
            "a/../../x",
            "a/./b",
            "a//b",
            "C:/x",
            "a\\b",
            "a\0",
            "a\u{1b}]0;title\u{7}",
            "a\u{7f}",
            "a\u{9b}2J",
            "a\u{85}b",
            "a\tb",
        ] {
            assert!(
                matches!(safe_relative(bad), Err(MicroError::UnsafePath(_))),
                "{bad:?}"
            );
        }
    }
}