baseh 2.0.3

baseH codec: fixed-length, checksummed, optionally permuted human-readable identifiers
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
//! Full encode/decode flow (spec sections 3, 8, 9, 10, 11, 12 and 18).

use std::collections::HashSet;

use num_bigint::BigUint;

use crate::basen::{decode_base_n, encode_base_n, pow_biguint};
use crate::checksum::calculate_checksum;
use crate::error::{BasehError, ErrorCode};
use crate::feistel::{inverse_permute, permute};
use crate::profile::{
    effective_checksum_length, prepare_profile, Mode, Permutation, PreparedProfile, Profile,
};

/// Built-in spoken-confusion candidate maps (spec 3.3).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ConfusionProfile {
    /// No candidates: correction always fails with INVALID_CHECKSUM.
    /// This is the default, matching the reference decoder.
    #[default]
    None,
    /// B/D and P/T.
    Light,
    /// Light plus M/N and V/W.
    Medium,
    /// Medium plus F/S and C/G.
    Heavy,
}

impl ConfusionProfile {
    fn map(self) -> &'static [(char, &'static [char])] {
        match self {
            ConfusionProfile::None => &[],
            ConfusionProfile::Light => {
                &[('B', &['D']), ('D', &['B']), ('P', &['T']), ('T', &['P'])]
            }
            ConfusionProfile::Medium => &[
                ('B', &['D']),
                ('D', &['B']),
                ('P', &['T']),
                ('T', &['P']),
                ('M', &['N']),
                ('N', &['M']),
                ('V', &['W']),
                ('W', &['V']),
            ],
            ConfusionProfile::Heavy => &[
                ('B', &['D']),
                ('D', &['B']),
                ('P', &['T']),
                ('T', &['P']),
                ('M', &['N']),
                ('N', &['M']),
                ('V', &['W']),
                ('W', &['V']),
                ('F', &['S']),
                ('S', &['F']),
                ('C', &['G']),
                ('G', &['C']),
            ],
        }
    }
}

/// Decode options (spec 12.2). Defaults: strict input, no correction.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DecodeOptions {
    /// Remove ASCII spaces during normalization in addition to separators.
    pub accept_spaces: bool,
    /// Attempt single-symbol spoken-confusion correction after a checksum failure.
    pub try_correction: bool,
    pub confusion_profile: ConfusionProfile,
    /// 0 or 1 in version 1. Anything above 1 is treated as 1. Defaults to 1,
    /// matching the other ports; set it to 0 to disable correction
    /// explicitly when try_correction is on.
    pub max_corrections: u32,
}

impl Default for DecodeOptions {
    fn default() -> Self {
        DecodeOptions {
            accept_spaces: false,
            try_correction: false,
            confusion_profile: ConfusionProfile::None,
            max_corrections: 1,
        }
    }
}

impl DecodeOptions {
    pub fn strict() -> Self {
        DecodeOptions::default()
    }
}

/// Successful decode result (spec 12.2).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DecodeResult {
    pub id: BigUint,
    pub canonical_code: String,
    /// True when the normalized input differed from the canonical code.
    pub corrected: bool,
}

/// Non-throwing validation result (spec 12.4). Never carries an internal ID.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ValidateOutcome {
    pub valid: bool,
    pub canonical_code: Option<String>,
    pub reason: Option<ErrorCode>,
}

/// Live as-you-type inspection result (spec 12.5). `inspect` never panics on
/// user input and never reports [`InspectResult::Valid`] for an incomplete
/// code. There is deliberately no `suggest` variant: the frozen profiles
/// alias confusable characters during normalization.
#[derive(Debug, Clone, PartialEq)]
pub enum InspectResult {
    /// Nothing typed yet, after separator and whitespace removal.
    Empty,
    /// Incomplete input, every symbol valid so far. `typed` carries the
    /// normalized symbols with separators inserted as far as the groups go;
    /// `progress` is the fraction toward a complete code, in `(0, 1)`.
    Typing { typed: String, progress: f64 },
    /// A symbol outside the union of the body and checksum alphabets.
    BadChar,
    /// More symbols than the profile accepts (fixed: `bodyLength +
    /// checksumLength`; expandable: 32).
    TooLong,
    /// A complete code that failed validation; `reason` is the error code
    /// reported by validate.
    Invalid { reason: ErrorCode },
    /// A complete, valid code with its decoded id and canonical rendering.
    Valid { id: BigUint, canonical_code: String },
}

impl InspectResult {
    /// The spec 12.5 state name, identical across implementations.
    pub fn state(&self) -> &'static str {
        match self {
            InspectResult::Empty => "empty",
            InspectResult::Typing { .. } => "typing",
            InspectResult::BadChar => "bad-char",
            InspectResult::TooLong => "too-long",
            InspectResult::Invalid { .. } => "invalid",
            InspectResult::Valid { .. } => "valid",
        }
    }
}

const MAX_CANDIDATES: usize = 64;

fn is_ascii_ws(c: char) -> bool {
    matches!(c, '\t' | '\n' | '\x0B' | '\x0C' | '\r' | ' ')
}

/// Spec 3.1 normalization, steps 1-7. Returns the raw unformatted chars.
fn normalize(
    input: &str,
    profile: &PreparedProfile,
    accept_spaces: bool,
) -> Result<Vec<char>, BasehError> {
    let trimmed = input.trim_matches(is_ascii_ws);
    let had_separator =
        !profile.profile.separator.is_empty() && trimmed.contains(&profile.profile.separator);
    let mut s: String = if profile.profile.separator.is_empty() {
        trimmed.to_string()
    } else {
        trimmed.replace(&profile.profile.separator, "")
    };
    if accept_spaces {
        s = s.replace(' ', "");
    }
    let p = &profile.profile;
    let allowed: HashSet<char> = profile
        .body_alphabet_norm
        .iter()
        .chain(profile.checksum_alphabet_norm.iter())
        .copied()
        .collect();
    // Spec 3.2: an alias never maps two distinct canonical symbols into one
    // value, so a symbol that is already canonical stays as-is and only
    // non-canonical symbols are aliased. (In fixed tiers alias sources are
    // never canonical, so this changes nothing there.)
    let normalized: Vec<char> = s
        .chars()
        .map(|c| {
            let c = if p.case_sensitive {
                c
            } else {
                c.to_ascii_uppercase()
            };
            if allowed.contains(&c) {
                c
            } else {
                *profile.aliases_norm.get(&c).unwrap_or(&c)
            }
        })
        .collect();
    // Spec 3.1 step 6 runs before spec 3.4 re-padding, so padded zero
    // symbols never raise INVALID_CHARACTER.
    for ch in &normalized {
        if !allowed.contains(ch) {
            return Err(BasehError::customer(
                ErrorCode::InvalidCharacter,
                format!("Symbol {ch:?} is not accepted"),
            ));
        }
    }
    if p.mode == Mode::Expandable {
        // Spec 19.2/19.7: no left-padding and no stripped-zero leniency.
        // Input shorter than minLength or longer than 32 fails
        // INVALID_LENGTH, and a separator below separatorMinLength is
        // rejected (spec 19.5: the decoder expects no separators there).
        if normalized.len() < profile.min_length {
            return Err(BasehError::customer(
                ErrorCode::InvalidLength,
                format!(
                    "Expected at least {} symbols, got {}",
                    profile.min_length,
                    normalized.len()
                ),
            ));
        }
        if normalized.len() > 32 {
            return Err(BasehError::customer(
                ErrorCode::InvalidLength,
                format!("Expected at most 32 symbols, got {}", normalized.len()),
            ));
        }
        if had_separator && normalized.len() < profile.separator_min_length {
            return Err(BasehError::customer(
                ErrorCode::InvalidCharacter,
                format!(
                    "Separators do not appear below {} symbols",
                    profile.separator_min_length
                ),
            ));
        }
        return Ok(normalized);
    }
    let expected = p.body_length + p.checksum_length;
    // Spec 3.4: a code that lost leading zero body symbols is re-padded with
    // the body zero symbol. The checksum symbols always remain, so the split
    // point is unambiguous. A fully stripped no-checksum code would be empty
    // and stays a length error.
    let mut normalized = normalized;
    if normalized.len() < expected && normalized.len() >= p.checksum_length.max(1) {
        let zero = profile.body_alphabet_norm[0];
        let mut padded = vec![zero; expected - normalized.len()];
        padded.extend(normalized);
        normalized = padded;
    }
    if normalized.len() != expected {
        return Err(BasehError::customer(
            ErrorCode::InvalidLength,
            format!("Expected {} symbols, got {}", expected, normalized.len()),
        ));
    }
    Ok(normalized)
}

/// Spec 11/19.5. Grouping is presentation only; skipped when the separator
/// is empty or (expandable) below separatorMinLength.
fn format_raw(raw: &[char], profile: &PreparedProfile) -> String {
    let p = &profile.profile;
    if p.separator.is_empty() {
        return raw.iter().collect();
    }
    if p.mode == Mode::Expandable {
        if raw.len() < profile.separator_min_length {
            return raw.iter().collect();
        }
        return format_with(raw, &expandable_grouping(raw.len()), &p.separator);
    }
    format_with(raw, &p.grouping, &p.separator)
}

fn format_with(raw: &[char], sizes: &[usize], separator: &str) -> String {
    let mut parts: Vec<String> = Vec::with_capacity(sizes.len());
    let mut offset = 0;
    for size in sizes {
        parts.push(raw[offset..offset + size].iter().collect());
        offset += size;
    }
    parts.join(separator)
}

/// Spec 12.5. Separators inserted into a partially typed code, as far as the
/// groups go. Fixed mode walks the configured grouping, emitting a partial
/// final group as-is and no separator for a group the symbols do not reach;
/// expandable mode uses the balanced grouping rule of spec 19.5 for the typed
/// length, bare below separatorMinLength.
fn format_partial(raw: &[char], profile: &PreparedProfile) -> String {
    let p = &profile.profile;
    if p.separator.is_empty() {
        return raw.iter().collect();
    }
    if p.mode == Mode::Expandable {
        if raw.len() < profile.separator_min_length {
            return raw.iter().collect();
        }
        return format_with(raw, &expandable_grouping(raw.len()), &p.separator);
    }
    let mut parts: Vec<String> = Vec::new();
    let mut offset = 0;
    for size in &p.grouping {
        if offset >= raw.len() {
            break;
        }
        let end = (offset + size).min(raw.len());
        parts.push(raw[offset..end].iter().collect());
        offset = end;
    }
    parts.join(&p.separator)
}

/// Spec 19.5. Group sizes for a total length under the balanced rule:
/// `g = max(2, ceil(L / 5))` groups whose sizes differ by at most one, the
/// larger groups on the left.
pub fn expandable_grouping(length: usize) -> Vec<usize> {
    let g = length.div_ceil(5);
    let g = g.max(2);
    let base = length / g;
    if base < 1 {
        return vec![length];
    }
    let rem = length % g;
    let mut sizes = vec![base + 1; rem];
    sizes.extend(std::iter::repeat_n(base, g - rem));
    sizes
}

/// Spec 19.1/22.3. First id of generation L: the sum of each generation's
/// capacity A^(k - effectiveK(k)) for k from minLength through L-1. The
/// effective checksum length is per-generation (spec 22), so the sum is not
/// a single geometric series when the short checksum is on.
pub(crate) fn generation_base(profile: &PreparedProfile, length: usize) -> BigUint {
    let mut base = BigUint::from(0u64);
    for l in profile.min_length..length {
        base += generation_capacity(profile, l);
    }
    base
}

/// Spec 19.1/22.3. Ids held by generation L: A^(L - effectiveK(L)).
pub(crate) fn generation_capacity(profile: &PreparedProfile, length: usize) -> BigUint {
    pow_biguint(
        BigUint::from(profile.body_alphabet_norm.len() as u64),
        length - effective_checksum_length(profile, length),
    )
}

/// Smallest generation whose range holds id, per spec 19.6. The scan is
/// capped at generation 32 (spec 19.7's maximum code length), so at most
/// `33 - min_length` iterations run; an id beyond that fails OUT_OF_RANGE
/// here instead of looping on ever-larger big integers.
pub(crate) fn generation_for_id(
    profile: &PreparedProfile,
    id: &BigUint,
) -> Result<usize, BasehError> {
    let mut l = profile.min_length;
    let mut base = BigUint::from(0u64);
    let mut cap = generation_capacity(profile, l);
    while *id >= &base + &cap {
        if l >= 32 {
            return Err(BasehError::customer(
                ErrorCode::OutOfRange,
                format!("ID {id} requires a code longer than 32 symbols"),
            ));
        }
        base += &cap;
        l += 1;
        cap = generation_capacity(profile, l);
    }
    Ok(l)
}

/// Spec 10. Substitution-only candidate generation, capped and deduplicated.
fn generate_candidates(
    body: &[char],
    confusion: &[(char, Vec<char>)],
    max_edits: u32,
) -> Result<Vec<Vec<char>>, BasehError> {
    if max_edits == 0 {
        return Ok(Vec::new());
    }
    let mut results: Vec<Vec<char>> = Vec::new();
    let mut seen: HashSet<Vec<char>> = HashSet::new();
    for pos in 0..body.len() {
        let source = body[pos];
        for (map_source, replacements) in confusion {
            if *map_source != source {
                continue;
            }
            for replacement in replacements.iter() {
                let mut candidate = body.to_vec();
                candidate[pos] = *replacement;
                if seen.insert(candidate.clone()) {
                    results.push(candidate);
                    if results.len() > MAX_CANDIDATES {
                        return Err(BasehError::new(
                            ErrorCode::TooManyCandidates,
                            "Candidate generation exceeded 64 entries",
                            false,
                        ));
                    }
                }
            }
        }
    }
    Ok(results)
}

/// A baseH codec bound to a validated profile. Pure and stateless; safe to
/// share across threads.
pub struct Baseh {
    profile: PreparedProfile,
}

impl Baseh {
    /// Validate the profile per spec 2.2 and 18 and bind it. Invalid profiles
    /// are rejected here (at startup), never on the first customer request.
    pub fn new(profile: Profile) -> Result<Baseh, BasehError> {
        Ok(Baseh {
            profile: prepare_profile(profile)?,
        })
    }

    /// Spec 4: `body_alphabet_len ^ body_length` (after any vowel stripping).
    ///
    /// Spec 12.3: fixed mode only. Expandable profiles have no single
    /// capacity; use the per-generation formulas of spec 19.1
    /// ([`Baseh::generation_base`], [`Baseh::generation_capacity`]).
    /// Calling this on an expandable profile fails INVALID_PROFILE.
    pub fn capacity(&self) -> Result<&BigUint, BasehError> {
        if self.profile.profile.mode != Mode::Fixed {
            return Err(BasehError::new(
                ErrorCode::InvalidProfile,
                "capacity() is only defined for fixed-mode profiles",
                false,
            ));
        }
        Ok(&self.profile.capacity)
    }

    /// Spec 19.1. First id held by generation `length` (expandable mode).
    pub fn generation_base(&self, length: usize) -> BigUint {
        generation_base(&self.profile, length)
    }

    /// Spec 19.1. Ids held by generation `length` (expandable mode).
    pub fn generation_capacity(&self, length: usize) -> BigUint {
        generation_capacity(&self.profile, length)
    }

    /// Smallest generation whose range holds `id`, per spec 19.6. Fails
    /// OUT_OF_RANGE when `id` needs a code longer than 32 symbols.
    pub fn generation_for_id(&self, id: &BigUint) -> Result<usize, BasehError> {
        generation_for_id(&self.profile, id)
    }

    /// Spec 22. The checksum length that applies to a generation of the
    /// given total length.
    pub fn effective_checksum_length(&self, length: usize) -> usize {
        effective_checksum_length(&self.profile, length)
    }

    /// The validated profile this codec was built from.
    pub fn profile(&self) -> &Profile {
        &self.profile.profile
    }

    /// Spec 8 (fixed mode), including the spec 18 blocklist scan.
    fn encode_fixed(&self, id: &BigUint) -> Result<String, BasehError> {
        if *id >= self.profile.capacity {
            return Err(BasehError::customer(
                ErrorCode::OutOfRange,
                format!("ID {id} is outside the profile capacity"),
            ));
        }
        let p = &self.profile.profile;
        let mut value = id.clone();
        if let Permutation::FeistelV1 {
            key_bytes, rounds, ..
        } = &p.permutation
        {
            value = permute(
                &value,
                &self.profile.capacity,
                &p.profile_id,
                key_bytes,
                *rounds,
                None,
            )?;
        }
        let body = encode_base_n(&value, &self.profile.body_alphabet_norm, p.body_length);
        let raw = self.finish_encode(body, p.checksum_length)?;
        Ok(format_raw(&raw, &self.profile))
    }

    /// Spec 19.6.
    fn encode_expandable(&self, id: &BigUint) -> Result<String, BasehError> {
        let l = generation_for_id(&self.profile, id)?;
        let p = &self.profile.profile;
        let mut value = id - generation_base(&self.profile, l);
        let domain = generation_capacity(&self.profile, l);
        if let Permutation::FeistelV1 {
            key_bytes, rounds, ..
        } = &p.permutation
        {
            value = permute(
                &value,
                &domain,
                &p.profile_id,
                key_bytes,
                *rounds,
                Some(l as u32),
            )?;
        }
        let k = effective_checksum_length(&self.profile, l);
        let body = encode_base_n(&value, &self.profile.body_alphabet_norm, l - k);
        let raw = self.finish_encode(body, k)?;
        Ok(format_raw(&raw, &self.profile))
    }

    /// Checksum append plus the spec 18.2 blocklist scan over the raw code.
    fn finish_encode(&self, body: String, checksum_length: usize) -> Result<Vec<char>, BasehError> {
        let body_chars: Vec<char> = body.chars().collect();
        let checksum = calculate_checksum(&self.profile, &body_chars, checksum_length)?;
        let mut raw = body_chars;
        raw.extend(checksum.chars());
        // Spec 18.2: case-insensitive substring scan over the raw code.
        if !self.profile.blocklist.is_empty() {
            let upper: String = raw
                .iter()
                .map(|c| c.to_ascii_uppercase())
                .collect::<String>();
            if self.profile.blocklist.iter().any(|w| upper.contains(w)) {
                return Err(BasehError::new(
                    ErrorCode::BlockedCode,
                    "The generated reference contains a blocked substring",
                    false,
                ));
            }
        }
        // Spec 21.2: a run of the same symbol at or above max_repetition
        // blocks the code. Runs are measured on the raw string, so a
        // separator never breaks a run.
        let max = self.profile.profile.max_repetition;
        if max > 0 {
            let mut run = 1;
            for pair in raw.windows(2) {
                run = if pair[0] == pair[1] { run + 1 } else { 1 };
                if run >= max {
                    return Err(BasehError::new(
                        ErrorCode::BlockedCode,
                        "The generated reference repeats a symbol beyond the profile limit",
                        false,
                    ));
                }
            }
        }
        Ok(raw)
    }

    /// Spec 8/19.6, including the spec 18 blocklist scan.
    pub fn encode(&self, id: &BigUint) -> Result<String, BasehError> {
        match self.profile.profile.mode {
            Mode::Fixed => self.encode_fixed(id),
            Mode::Expandable => self.encode_expandable(id),
        }
    }

    /// Spec 9/19.7.
    pub fn decode(&self, input: &str, options: &DecodeOptions) -> Result<DecodeResult, BasehError> {
        let raw = normalize(input, &self.profile, options.accept_spaces)?;
        let p = &self.profile.profile;
        // Spec 22: the generation is selected by the presented total length,
        // so the effective checksum length is a deterministic function of it.
        let effective_k = match p.mode {
            Mode::Expandable => effective_checksum_length(&self.profile, raw.len()),
            Mode::Fixed => p.checksum_length,
        };
        let body_length = match p.mode {
            Mode::Expandable => raw.len() - effective_k,
            Mode::Fixed => p.body_length,
        };
        let mut body: Vec<char> = raw[..body_length].to_vec();
        let supplied_checksum: Vec<char> = raw[body_length..].to_vec();

        // Spec 3.1 step 6 checks the union of both alphabets (in normalize)
        // and spec 9 adds no partition-specific checks. A body position
        // holding a checksum-only symbol fails as INVALID_CHARACTER inside
        // calculate_checksum; a checksum position holding a body-only symbol
        // simply mismatches below and fails as INVALID_CHECKSUM. This
        // ordering is pinned by the frozen error vectors.
        if calculate_checksum(&self.profile, &body, effective_k)?
            != supplied_checksum.iter().collect::<String>()
        {
            if !options.try_correction || options.max_corrections == 0 {
                return Err(BasehError::customer(
                    ErrorCode::InvalidChecksum,
                    "The reference code did not pass validation",
                ));
            }
            // Spec 10: replacements that are not body alphabet symbols are
            // dropped before candidate generation. A suggested symbol the
            // alphabet cannot contain (say a spoken drop on a stripped-
            // alphabet profile) could never validate; generating it anyway
            // would throw INVALID_CHARACTER from the checksum step instead of
            // reporting an honest INVALID_CHECKSUM.
            let body_set: HashSet<char> = self.profile.body_alphabet_norm.iter().copied().collect();
            let filtered_map: Vec<(char, Vec<char>)> = options
                .confusion_profile
                .map()
                .iter()
                .filter_map(|(source, replacements)| {
                    let kept: Vec<char> = replacements
                        .iter()
                        .copied()
                        .filter(|r| body_set.contains(r))
                        .collect();
                    if kept.is_empty() {
                        None
                    } else {
                        Some((*source, kept))
                    }
                })
                .collect();
            let candidates = generate_candidates(&body, &filtered_map, options.max_corrections)?;
            let mut valid: Option<Vec<char>> = None;
            for candidate in candidates {
                let candidate_checksum =
                    calculate_checksum(&self.profile, &candidate, effective_k)?;
                if candidate_checksum == supplied_checksum.iter().collect::<String>() {
                    if valid.is_some() {
                        return Err(BasehError::new(
                            ErrorCode::AmbiguousInput,
                            "The reference code matches more than one record",
                            false,
                        ));
                    }
                    valid = Some(candidate);
                }
            }
            match valid {
                Some(candidate) => body = candidate,
                None => {
                    return Err(BasehError::customer(
                        ErrorCode::InvalidChecksum,
                        "The reference code did not pass validation",
                    ));
                }
            }
        }

        let mut value = decode_base_n(
            &body,
            self.profile.body_alphabet_norm.len(),
            &self.profile.body_index,
        )?;
        if p.mode == Mode::Expandable {
            // Spec 19.7: the offset is de-permuted within the generation's own
            // domain, then the generation base is added back.
            let l = raw.len();
            if let Permutation::FeistelV1 {
                key_bytes, rounds, ..
            } = &p.permutation
            {
                value = inverse_permute(
                    &value,
                    &generation_capacity(&self.profile, l),
                    &p.profile_id,
                    key_bytes,
                    *rounds,
                    Some(l as u32),
                )?;
            }
            value = generation_base(&self.profile, l) + value;
        } else if let Permutation::FeistelV1 {
            key_bytes, rounds, ..
        } = &p.permutation
        {
            value = inverse_permute(
                &value,
                &self.profile.capacity,
                &p.profile_id,
                key_bytes,
                *rounds,
                None,
            )?;
        }
        // Spec 18.2: the canonical re-encode may raise BLOCKED_CODE here,
        // since a blocked string could never have been issued.
        let canonical_code = self.encode(&value)?;
        // Separator removal is literal-substring based (matches the JS
        // reference: split on the separator string, not per character).
        let canonical_raw: Vec<char> = if p.separator.is_empty() {
            canonical_code.chars().collect()
        } else {
            canonical_code.replace(&p.separator, "").chars().collect()
        };
        Ok(DecodeResult {
            id: value,
            canonical_code,
            corrected: raw != canonical_raw,
        })
    }

    /// Spec 12.5. Live as-you-type inspection. Gates on the typed length
    /// before validating, so spec 3.4 re-padding can never paint an
    /// incomplete fixed-mode code valid (or invalid): a short fixed input is
    /// typing, never checked. Never panics on user input.
    pub fn inspect(&self, input: &str) -> InspectResult {
        let p = &self.profile.profile;
        // Step 1: remove every occurrence of the separator string (literal
        // substring removal), then drop ASCII whitespace anywhere.
        let no_sep = if p.separator.is_empty() {
            input.to_string()
        } else {
            input.replace(&p.separator, "")
        };
        let cleaned: Vec<char> = no_sep.chars().filter(|c| !is_ascii_ws(*c)).collect();
        let typed_count = cleaned.len();
        if typed_count == 0 {
            return InspectResult::Empty;
        }

        // Step 3: completeness bounds per mode. Expandable mode is complete
        // for every length from minLength through 32 (the length selects the
        // generation), so the over-length bound is 32.
        let fixed = p.mode == Mode::Fixed;
        let expected = if fixed {
            p.body_length + p.checksum_length
        } else {
            32
        };
        if typed_count > expected {
            return InspectResult::TooLong;
        }

        // Step 4: spec 3.1 steps 4-6 without the length checks and without
        // re-padding — case normalization, aliases, then union membership. A
        // symbol outside both alphabets is bad-char; a symbol valid only in
        // the other region passes here and fails later under validate.
        let allowed: HashSet<char> = self
            .profile
            .body_alphabet_norm
            .iter()
            .chain(self.profile.checksum_alphabet_norm.iter())
            .copied()
            .collect();
        let normalized: Vec<char> = cleaned
            .iter()
            .map(|c| {
                let c = if p.case_sensitive {
                    *c
                } else {
                    c.to_ascii_uppercase()
                };
                if allowed.contains(&c) {
                    c
                } else {
                    *self.profile.aliases_norm.get(&c).unwrap_or(&c)
                }
            })
            .collect();
        if normalized.iter().any(|c| !allowed.contains(c)) {
            return InspectResult::BadChar;
        }

        // Step 5: incomplete inputs report typing. Fixed mode: complete means
        // exactly bodyLength + checksumLength symbols; expandable mode: every
        // length at or above minLength is complete.
        let complete = if fixed {
            typed_count == expected
        } else {
            typed_count >= self.profile.min_length
        };
        if !complete {
            let denominator = if fixed {
                expected
            } else {
                self.profile.min_length
            };
            return InspectResult::Typing {
                typed: format_partial(&normalized, &self.profile),
                progress: typed_count as f64 / denominator as f64,
            };
        }

        // Step 6: judge the normalized string (no separator, no whitespace,
        // case- and alias-normalized).
        let raw: String = normalized.iter().collect();
        let options = DecodeOptions::default();
        let outcome = self.validate(&raw, &options);
        if !outcome.valid {
            return InspectResult::Invalid {
                reason: outcome.reason.unwrap_or(ErrorCode::InvalidChecksum),
            };
        }
        let decoded = self
            .decode(&raw, &options)
            .expect("validate passed, so decode cannot fail on the same input");
        InspectResult::Valid {
            id: decoded.id,
            canonical_code: decoded.canonical_code,
        }
    }

    /// Spec 12.4. Never fails on user input; never exposes an internal ID.
    pub fn validate(&self, input: &str, options: &DecodeOptions) -> ValidateOutcome {
        match self.decode(input, options) {
            Ok(result) => ValidateOutcome {
                valid: true,
                canonical_code: Some(result.canonical_code),
                reason: None,
            },
            Err(err) => ValidateOutcome {
                valid: false,
                canonical_code: None,
                reason: Some(err.code),
            },
        }
    }
}