base122-fast 0.1.3

High-performance Base122 codex (6+ Gbps) with lower overhead (~14%) than Base64.
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
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
//! # Base122-Fast
//!
//! A high-performance Base122 implementation achieving throughput up to **6.5 Gbps (encoding)**
//! and **6.2 Gbps (decoding)** on modern hardware (e.g., AMD Ryzen 5 5600, single-threaded).
//!
//! Base122 is a binary-to-text encoding scheme that is significantly more space-efficient
//! than Base64, offering approximately **14% overhead** compared to Base64's 33%, while
//! remaining valid UTF-8.
//!
//! ## Key Features
//!
//! *   **High Throughput**: Optimized for Gbps-level processing.
//! *   **no_std**: Suitable for embedded systems and WASM (requires `alloc`).
//! *   **SIMD Within A Register**: Uses SWAR techniques to process multiple bytes in 64-bit registers.
//! *   **Safety**: While leveraging `unsafe` for performance, it is rigorously tested for round-trip integrity.
//!
//! ## Quick Start
//!
//! ```rust
//! let data = b"hello world";
//! let encoded = base122_fast::encode(data);
//! let decoded = base122_fast::decode(&encoded).expect("Failed to decode");
//!
//! assert_eq!(data, decoded.as_slice());
//! ```

#![no_std]

extern crate alloc;

use alloc::string::String;
use alloc::vec::Vec;

const ILLEGALS: [u8; 6] = [0, 10, 13, 34, 38, 92];
const SHORTENED: u8 = 0b111;
const ASCII_MASK_8: u64 = 0x8080_8080_8080_8080;
const LO7_8: u64 = 0x7F7F_7F7F_7F7F_7F7F;
const Z0_MASK: u64 = 0x0000_0000_0000_0000;
const Z10_MASK: u64 = 0x0A0A_0A0A_0A0A_0A0A;
const Z13_MASK: u64 = 0x0D0D_0D0D_0D0D_0D0D;
const Z34_MASK: u64 = 0x2222_2222_2222_2222;
const Z38_MASK: u64 = 0x2626_2626_2626_2626;
const Z92_MASK: u64 = 0x5C5C_5C5C_5C5C_5C5C;

const CLASS: [u8; 128] = {
    let mut arr = [0u8; 128];
    arr[0] = 1;
    arr[10] = 2;
    arr[13] = 3;
    arr[34] = 4;
    arr[38] = 5;
    arr[92] = 6;
    arr
};

const ESCAPE_TABLE: [[u16; 128]; 7] = {
    let mut table = [[0u16; 128]; 7];
    let mut idx = 0usize;
    while idx < 6 {
        let mut next = 0usize;
        while next < 128 {
            let n = next as u8;
            let b1 = 0b1100_0010 | (idx as u8) << 2 | (n >> 6);
            let b2 = 0x80 | (n & 0x3F);
            table[idx][next] = u16::from_le_bytes([b1, b2]);
            next += 1;
        }
        idx += 1;
    }
    let mut next = 0usize;
    while next < 128 {
        let n = next as u8;
        let b1 = 0b1100_0010 | (SHORTENED << 2) | (n >> 6);
        let b2 = 0x80 | (n & 0x3F);
        table[6][next] = u16::from_le_bytes([b1, b2]);
        next += 1;
    }
    table
};

const LEAD_DECODE: [u8; 256] = {
    let mut arr = [0xFFu8; 256];

    let mut idx = 0usize;
    while idx < 6 {
        let mut bit = 0usize;
        while bit < 2 {
            let lead = 0b1100_0010 | ((idx as u8) << 2) | (bit as u8);
            arr[lead as usize] = ((idx as u8) << 1) | (bit as u8);
            bit += 1;
        }
        idx += 1;
    }

    let mut bit = 0usize;
    while bit < 2 {
        let lead = 0b1100_0010 | (SHORTENED << 2) | (bit as u8);
        arr[lead as usize] = (SHORTENED << 1) | (bit as u8);
        bit += 1;
    }

    arr
};

#[inline(always)]
unsafe fn store_u64_le(dst: *mut u8, value: u64) {
    unsafe { (dst as *mut u64).write_unaligned(value.to_le()) };
}

#[inline(always)]
unsafe fn store_u16_le(dst: *mut u8, value: u16) {
    unsafe { (dst as *mut u16).write_unaligned(value.to_le()) };
}

#[inline(always)]
unsafe fn store_be_partial(dst: *mut u8, value: u64, len: usize) {
    debug_assert!(len > 0 && len <= 8);
    let shifted = value << ((8 - len) * 8);
    unsafe { (dst as *mut u64).write_unaligned(shifted.to_be()) };
}

#[inline(always)]
unsafe fn load_u64_le(ptr: *const u8) -> u64 {
    u64::from_le(unsafe { (ptr as *const u64).read_unaligned() })
}

#[inline(always)]
unsafe fn load56_be_overread1(ptr: *const u8) -> u64 {
    u64::from_be(unsafe { (ptr as *const u64).read_unaligned() }) >> 8
}

#[inline(always)]
unsafe fn load56_be_exact(ptr: *const u8) -> u64 {
    #[cfg(miri)]
    {
        let mut tmp = 0u64;
        unsafe { core::ptr::copy_nonoverlapping(ptr, &mut tmp as *mut u64 as *mut u8, 7) };
        u64::from_be(tmp) >> 8
    }
    #[cfg(not(miri))]
    {
        unsafe {
            let val = (ptr as *const u64).read_unaligned();
            val.swap_bytes() >> 8
        }
    }
}

#[inline(always)]
fn split56_to_groups_le(bits56: u64) -> u64 {
    #[cfg(all(
        any(target_arch = "x86_64", target_arch = "x86"),
        target_feature = "bmi2"
    ))]
    {
        let scattered = unsafe { core::arch::x86_64::_pdep_u64(bits56, LO7_8) };
        return scattered.swap_bytes();
    }
    #[allow(unreachable_code)]
    {
        ((bits56 >> 49) & 0x7F)
            | (((bits56 >> 42) & 0x7F) << 8)
            | (((bits56 >> 35) & 0x7F) << 16)
            | (((bits56 >> 28) & 0x7F) << 24)
            | (((bits56 >> 21) & 0x7F) << 32)
            | (((bits56 >> 14) & 0x7F) << 40)
            | (((bits56 >> 7) & 0x7F) << 48)
            | ((bits56 & 0x7F) << 56)
    }
}

#[inline(always)]
fn gather_groups_to_bits56(chunk_le: u64) -> u64 {
    #[cfg(all(
        any(target_arch = "x86_64", target_arch = "x86"),
        target_feature = "bmi2"
    ))]
    {
        return unsafe { core::arch::x86_64::_pext_u64(chunk_le.swap_bytes(), LO7_8) };
    }
    #[allow(unreachable_code)]
    {
        let g0 = (chunk_le & 0xFF) as u64;
        let g1 = ((chunk_le >> 8) & 0xFF) as u64;
        let g2 = ((chunk_le >> 16) & 0xFF) as u64;
        let g3 = ((chunk_le >> 24) & 0xFF) as u64;
        let g4 = ((chunk_le >> 32) & 0xFF) as u64;
        let g5 = ((chunk_le >> 40) & 0xFF) as u64;
        let g6 = ((chunk_le >> 48) & 0xFF) as u64;
        let g7 = ((chunk_le >> 56) & 0xFF) as u64;

        (g0 << 49) | (g1 << 42) | (g2 << 35) | (g3 << 28) | (g4 << 21) | (g5 << 14) | (g6 << 7) | g7
    }
}

#[inline(always)]
fn illegal_high_mask(x: u64) -> u64 {
    let z0 = x ^ Z0_MASK;
    let z10 = x ^ Z10_MASK;
    let z13 = x ^ Z13_MASK;
    let z34 = x ^ Z34_MASK;
    let z38 = x ^ Z38_MASK;
    let z92 = x ^ Z92_MASK;

    let t0 = !z0.wrapping_add(LO7_8);
    let t10 = !z10.wrapping_add(LO7_8);
    let t13 = !z13.wrapping_add(LO7_8);
    let t34 = !z34.wrapping_add(LO7_8);
    let t38 = !z38.wrapping_add(LO7_8);
    let t92 = !z92.wrapping_add(LO7_8);

    (t0 | t10 | t13 | t34 | t38 | t92) & ASCII_MASK_8
}

#[inline(always)]
unsafe fn emit_escape_pair(out_ptr: *mut u8, out_pos: &mut usize, first: u8, next: u8) {
    let class = unsafe { *CLASS.get_unchecked(first as usize) };
    debug_assert!(class >= 1 && class <= 6);
    let idx = (class - 1) as usize;
    let pair = unsafe { *ESCAPE_TABLE.get_unchecked(idx).get_unchecked(next as usize) };
    unsafe { store_u16_le(out_ptr.add(*out_pos), pair) };
    *out_pos += 2;
}

#[inline(always)]
unsafe fn emit_shortened(out_ptr: *mut u8, out_pos: &mut usize, bits: u8) {
    let pair = unsafe { *ESCAPE_TABLE.get_unchecked(6).get_unchecked(bits as usize) };
    unsafe { store_u16_le(out_ptr.add(*out_pos), pair) };
    *out_pos += 2;
}

#[inline(always)]
fn pull7_tail(tail: &[u8], pos: &mut usize, acc: &mut u64, acc_bits: &mut u32) -> Option<u8> {
    while *acc_bits < 7 && *pos < tail.len() {
        *acc = (*acc << 8) | tail[*pos] as u64;
        *pos += 1;
        *acc_bits += 8;
    }

    if *acc_bits >= 7 {
        *acc_bits -= 7;
        let bits = ((*acc >> *acc_bits) & 0x7F) as u8;
        if *acc_bits == 0 {
            *acc = 0;
        } else {
            *acc &= (1_u64 << *acc_bits) - 1;
        }
        Some(bits)
    } else if *acc_bits > 0 {
        let bits = ((*acc << (7 - *acc_bits)) & 0x7F) as u8;
        *acc = 0;
        *acc_bits = 0;
        Some(bits)
    } else {
        None
    }
}

#[inline(always)]
fn group_count(input_len: usize) -> usize {
    if input_len == 0 {
        0
    } else {
        input_len.saturating_mul(8).saturating_add(6) / 7
    }
}

#[inline(always)]
fn encoded_capacity(input_len: usize) -> usize {
    group_count(input_len).saturating_mul(2).saturating_add(8)
}

#[inline(always)]
fn decoded_capacity(encoded_len: usize) -> usize {
    encoded_len
        .saturating_mul(7)
        .saturating_add(7)
        .saturating_div(8)
        .saturating_add(8)
}

#[inline(always)]
unsafe fn process_groups8_masked(
    mut groups_le: u64,
    mut illegal_mask: u64,
    out_ptr: *mut u8,
    out_pos: &mut usize,
    pending_illegal_bits: &mut u8,
    has_pending_illegal: &mut bool,
) {
    let mut remaining = 8usize;

    if *has_pending_illegal {
        let next = groups_le as u8;
        unsafe { emit_escape_pair(out_ptr, out_pos, *pending_illegal_bits, next) };
        *has_pending_illegal = false;
        groups_le >>= 8;
        illegal_mask >>= 8;
        remaining -= 1;
    }

    if illegal_mask == 0 {
        unsafe { store_u64_le(out_ptr.add(*out_pos), groups_le) };
        *out_pos += remaining;
        return;
    }

    if (illegal_mask & (illegal_mask - 1)) == 0 {
        let prefix = (illegal_mask.trailing_zeros() >> 3) as usize;
        if prefix != 0 {
            unsafe { store_u64_le(out_ptr.add(*out_pos), groups_le) };
            *out_pos += prefix;
            groups_le >>= prefix * 8;
            remaining -= prefix;
        }

        let cur = groups_le as u8;
        if remaining > 1 {
            let next = (groups_le >> 8) as u8;
            unsafe { emit_escape_pair(out_ptr, out_pos, cur, next) };
            groups_le >>= 16;
            remaining -= 2;

            if remaining != 0 {
                unsafe { store_u64_le(out_ptr.add(*out_pos), groups_le) };
                *out_pos += remaining;
            }
        } else {
            *pending_illegal_bits = cur;
            *has_pending_illegal = true;
        }
        return;
    }

    while remaining != 0 {
        if illegal_mask == 0 {
            unsafe { store_u64_le(out_ptr.add(*out_pos), groups_le) };
            *out_pos += remaining;
            break;
        }

        let prefix = (illegal_mask.trailing_zeros() >> 3) as usize;
        if prefix != 0 {
            unsafe { store_u64_le(out_ptr.add(*out_pos), groups_le) };
            *out_pos += prefix;
            groups_le >>= prefix * 8;
            illegal_mask >>= prefix * 8;
            remaining -= prefix;
        }

        let cur = groups_le as u8;
        if remaining > 1 {
            let next = (groups_le >> 8) as u8;
            unsafe { emit_escape_pair(out_ptr, out_pos, cur, next) };
            groups_le >>= 16;
            illegal_mask >>= 16;
            remaining -= 2;
        } else {
            *pending_illegal_bits = cur;
            *has_pending_illegal = true;
            break;
        }
    }
}

/// Encodes binary data into a Base122 string.
///
/// The resulting string is guaranteed to be valid UTF-8 and is more compact than
/// Base64, typically resulting in only ~14% size overhead.
///
/// # Performance
///
/// This function utilizes a fast-path execution model that scans 8-byte chunks
/// using SWAR logic. It is optimized for long buffers where ASCII-compatible
/// sequences are common.
///
/// # Examples
///
/// ```rust
/// let data = b"data";
/// let encoded = base122_fast::encode(data);
/// assert!(!encoded.is_empty());
/// ```
pub fn encode(data: &[u8]) -> String {
    if data.is_empty() {
        return String::new();
    }

    let mut out = Vec::<u8>::with_capacity(encoded_capacity(data.len()));
    let out_ptr = out.as_mut_ptr();

    let len = data.len();
    let ptr = data.as_ptr();

    let mut out_pos = 0usize;
    let mut i = 0usize;
    let mut pending_illegal_bits = 0u8;
    let mut has_pending_illegal = false;

    while i + 29 <= len {
        let bits56_a = unsafe { load56_be_overread1(ptr.add(i)) };
        let bits56_b = unsafe { load56_be_overread1(ptr.add(i + 7)) };
        let bits56_c = unsafe { load56_be_overread1(ptr.add(i + 14)) };
        let bits56_d = unsafe { load56_be_overread1(ptr.add(i + 21)) };

        let groups_a = split56_to_groups_le(bits56_a);
        let groups_b = split56_to_groups_le(bits56_b);
        let groups_c = split56_to_groups_le(bits56_c);
        let groups_d = split56_to_groups_le(bits56_d);

        let mask_a = illegal_high_mask(groups_a);
        let mask_b = illegal_high_mask(groups_b);
        let mask_c = illegal_high_mask(groups_c);
        let mask_d = illegal_high_mask(groups_d);

        if !has_pending_illegal && (mask_a | mask_b | mask_c | mask_d) == 0 {
            unsafe {
                store_u64_le(out_ptr.add(out_pos), groups_a);
                store_u64_le(out_ptr.add(out_pos + 8), groups_b);
                store_u64_le(out_ptr.add(out_pos + 16), groups_c);
                store_u64_le(out_ptr.add(out_pos + 24), groups_d);
            }
            out_pos += 32;
            i += 28;
            continue;
        }

        unsafe {
            process_groups8_masked(
                groups_a,
                mask_a,
                out_ptr,
                &mut out_pos,
                &mut pending_illegal_bits,
                &mut has_pending_illegal,
            );
            process_groups8_masked(
                groups_b,
                mask_b,
                out_ptr,
                &mut out_pos,
                &mut pending_illegal_bits,
                &mut has_pending_illegal,
            );
            process_groups8_masked(
                groups_c,
                mask_c,
                out_ptr,
                &mut out_pos,
                &mut pending_illegal_bits,
                &mut has_pending_illegal,
            );
            process_groups8_masked(
                groups_d,
                mask_d,
                out_ptr,
                &mut out_pos,
                &mut pending_illegal_bits,
                &mut has_pending_illegal,
            );
        }
        i += 28;
    }

    while i + 15 <= len {
        let bits56_a = unsafe { load56_be_overread1(ptr.add(i)) };
        let bits56_b = unsafe { load56_be_overread1(ptr.add(i + 7)) };

        let groups_a = split56_to_groups_le(bits56_a);
        let groups_b = split56_to_groups_le(bits56_b);

        let mask_a = illegal_high_mask(groups_a);
        let mask_b = illegal_high_mask(groups_b);

        if !has_pending_illegal && (mask_a | mask_b) == 0 {
            unsafe {
                store_u64_le(out_ptr.add(out_pos), groups_a);
                store_u64_le(out_ptr.add(out_pos + 8), groups_b);
            }
            out_pos += 16;
            i += 14;
            continue;
        }

        unsafe {
            process_groups8_masked(
                groups_a,
                mask_a,
                out_ptr,
                &mut out_pos,
                &mut pending_illegal_bits,
                &mut has_pending_illegal,
            );
            process_groups8_masked(
                groups_b,
                mask_b,
                out_ptr,
                &mut out_pos,
                &mut pending_illegal_bits,
                &mut has_pending_illegal,
            );
        }
        i += 14;
    }

    while i + 8 <= len {
        let bits56 = unsafe { load56_be_overread1(ptr.add(i)) };
        let groups_le = split56_to_groups_le(bits56);
        let mask = illegal_high_mask(groups_le);

        if !has_pending_illegal && mask == 0 {
            unsafe { store_u64_le(out_ptr.add(out_pos), groups_le) };
            out_pos += 8;
            i += 7;
            continue;
        }

        unsafe {
            process_groups8_masked(
                groups_le,
                mask,
                out_ptr,
                &mut out_pos,
                &mut pending_illegal_bits,
                &mut has_pending_illegal,
            );
        }
        i += 7;
    }

    if i + 7 <= len {
        let bits56 = unsafe { load56_be_exact(ptr.add(i)) };
        let groups_le = split56_to_groups_le(bits56);
        let mask = illegal_high_mask(groups_le);

        if !has_pending_illegal && mask == 0 {
            unsafe { store_u64_le(out_ptr.add(out_pos), groups_le) };
            out_pos += 8;
            i += 7;
        } else {
            unsafe {
                process_groups8_masked(
                    groups_le,
                    mask,
                    out_ptr,
                    &mut out_pos,
                    &mut pending_illegal_bits,
                    &mut has_pending_illegal,
                );
            }
            i += 7;
        }
    }

    let tail = &data[i..];
    let mut tail_pos = 0usize;
    let mut acc = 0u64;
    let mut acc_bits = 0u32;

    if has_pending_illegal {
        if let Some(nb) = pull7_tail(tail, &mut tail_pos, &mut acc, &mut acc_bits) {
            unsafe { emit_escape_pair(out_ptr, &mut out_pos, pending_illegal_bits, nb) };
        } else {
            unsafe { emit_shortened(out_ptr, &mut out_pos, pending_illegal_bits) };
        }
    }

    while let Some(cur) = pull7_tail(tail, &mut tail_pos, &mut acc, &mut acc_bits) {
        let class = unsafe { *CLASS.get_unchecked(cur as usize) };
        if class == 0 {
            unsafe { *out_ptr.add(out_pos) = cur };
            out_pos += 1;
        } else if let Some(nb) = pull7_tail(tail, &mut tail_pos, &mut acc, &mut acc_bits) {
            unsafe { emit_escape_pair(out_ptr, &mut out_pos, cur, nb) };
        } else {
            unsafe { emit_shortened(out_ptr, &mut out_pos, cur) };
            break;
        }
    }

    unsafe {
        out.set_len(out_pos);
        String::from_utf8_unchecked(out)
    }
}

#[inline(always)]
unsafe fn unpack8groups_chunk_le(
    chunk_le: u64,
    out_ptr: *mut u8,
    out_pos: &mut usize,
    acc: &mut u64,
    acc_bits: &mut u32,
) {
    let bits56 = gather_groups_to_bits56(chunk_le);
    let k = *acc_bits;

    let combined = (*acc << 56) | bits56;
    unsafe { store_be_partial(out_ptr.add(*out_pos), combined >> k, 7) };
    *out_pos += 7;
    *acc = combined & ((1u64 << k).wrapping_sub(1));
}

#[inline(always)]
unsafe fn push_ascii_prefix_le(
    mut chunk_le: u64,
    count: usize,
    out_ptr: *mut u8,
    out_pos: &mut usize,
    acc: &mut u64,
    acc_bits: &mut u32,
) {
    debug_assert!(count <= 7);

    let mut packed = 0u64;
    let mut n = 0usize;
    while n < count {
        packed = (packed << 7) | ((chunk_le as u8) as u64);
        chunk_le >>= 8;
        n += 1;
    }
    let add_bits = (count as u32) * 7;
    let total_bits = *acc_bits + add_bits;
    let new_bits = total_bits & 7;
    let emitted = (total_bits >> 3) as usize;
    let combined = (*acc << add_bits) | packed;

    if emitted != 0 {
        unsafe { store_be_partial(out_ptr.add(*out_pos), combined >> new_bits, emitted) };
        *out_pos += emitted;
    }

    *acc_bits = new_bits;
    *acc = combined & ((1u64 << new_bits).wrapping_sub(1));
}

#[inline(always)]
unsafe fn push7_scalar(
    out_ptr: *mut u8,
    out_pos: &mut usize,
    acc: &mut u64,
    acc_bits: &mut u32,
    bits: u8,
) {
    let combined = (*acc << 7) | (bits as u64);
    let total_bits = *acc_bits + 7;

    if total_bits >= 8 {
        let new_bits = total_bits - 8;
        unsafe { *out_ptr.add(*out_pos) = (combined >> new_bits) as u8 };
        *out_pos += 1;
        *acc_bits = new_bits;
        *acc = combined & ((1u64 << new_bits).wrapping_sub(1));
    } else {
        *acc_bits = total_bits;
        *acc = combined;
    }
}

#[inline(always)]
unsafe fn push14_scalar(
    out_ptr: *mut u8,
    out_pos: &mut usize,
    acc: &mut u64,
    acc_bits: &mut u32,
    hi7: u8,
    lo7: u8,
) {
    let combined = (*acc << 14) | ((hi7 as u64) << 7) | (lo7 as u64);
    let total_bits = *acc_bits + 14;
    let new_bits = total_bits & 7;
    let emitted = total_bits >> 3;
    let out_bits = combined >> new_bits;

    if emitted == 2 {
        unsafe { (out_ptr.add(*out_pos) as *mut u16).write_unaligned((out_bits as u16).to_be()) };
        *out_pos += 2;
    } else {
        unsafe { *out_ptr.add(*out_pos) = out_bits as u8 };
        *out_pos += 1;
    }

    *acc_bits = new_bits;
    *acc = combined & ((1u64 << new_bits).wrapping_sub(1));
}

#[cold]
#[inline(never)]
fn decode_err<T>(msg: &'static str) -> Result<T, &'static str> {
    Err(msg)
}

#[inline(always)]
unsafe fn break_to_scalar(
    ptr: *const u8,
    i: &mut usize,
    len: usize,
    out_ptr: *mut u8,
    out_pos: &mut usize,
    acc: &mut u64,
    acc_bits: &mut u32,
) -> Result<(), &'static str> {
    if *i + 2 > len {
        return decode_err("Unexpected end of input");
    }
    let b1 = unsafe { *ptr.add(*i) };
    let code = LEAD_DECODE[b1 as usize];
    if code == 0xFF {
        return decode_err("Invalid lead byte");
    }
    let b2 = unsafe { *ptr.add(*i + 1) };
    if (b2 & 0xC0) != 0x80 {
        return decode_err("Invalid continuation byte");
    }
    *i += 2;
    let illegal_index = code >> 1;
    let first_bit = code & 1;
    let next = (first_bit << 6) | (b2 & 0x3F);

    unsafe {
        if illegal_index < 6 {
            push14_scalar(
                out_ptr,
                out_pos,
                acc,
                acc_bits,
                ILLEGALS[illegal_index as usize],
                next,
            );
        } else {
            debug_assert_eq!(illegal_index, SHORTENED);
            push7_scalar(out_ptr, out_pos, acc, acc_bits, next);
        }
    }
    Ok(())
}

/// Decodes a Base122 encoded string back into its original binary form.
///
/// # Errors
///
/// Returns an `Err` if the input string is not a valid Base122 sequence.
/// This can happen if:
/// *   It contains invalid UTF-8 lead bytes not conforming to Base122 escape patterns.
/// *   An escape sequence is malformed or truncated.
/// *   The illegal character index is out of the defined 0-6 range.
///
/// # Performance
///
/// Decoding is optimized via unaligned 64-bit reads and a lookup-table-based
/// state machine to quickly process 7-bit groups.
///
/// # Examples
///
/// ```rust
/// let data = b"data";
/// let encoded = base122_fast::encode(data);
/// let decoded = base122_fast::decode(&encoded).unwrap();
///
/// assert_eq!(data, &decoded[..]);
/// ```
pub fn decode(encoded: &str) -> Result<Vec<u8>, &'static str> {
    if encoded.is_empty() {
        return Ok(Vec::new());
    }

    let bytes = encoded.as_bytes();
    let len = bytes.len();
    let mut out = Vec::<u8>::with_capacity(decoded_capacity(len));
    let out_ptr = out.as_mut_ptr();

    let ptr = bytes.as_ptr();
    let mut out_pos = 0usize;
    let mut acc = 0u64;
    let mut acc_bits = 0u32;
    let mut i = 0usize;

    while i + 8 <= len {
        let chunk = unsafe { load_u64_le(ptr.add(i)) };
        let high = chunk & ASCII_MASK_8;

        if high == 0 {
            unsafe {
                unpack8groups_chunk_le(chunk, out_ptr, &mut out_pos, &mut acc, &mut acc_bits);
            }
            i += 8;
            continue;
        }

        let ascii_prefix = (high.trailing_zeros() >> 3) as usize;
        if ascii_prefix != 0 {
            unsafe {
                push_ascii_prefix_le(
                    chunk,
                    ascii_prefix,
                    out_ptr,
                    &mut out_pos,
                    &mut acc,
                    &mut acc_bits,
                );
            }
            i += ascii_prefix;
            continue;
        }

        (unsafe {
            break_to_scalar(
                ptr,
                &mut i,
                len,
                out_ptr,
                &mut out_pos,
                &mut acc,
                &mut acc_bits,
            )
        })?;
    }

    while i < len {
        let b = unsafe { *ptr.add(i) };
        if b < 128 {
            i += 1;
            unsafe { push7_scalar(out_ptr, &mut out_pos, &mut acc, &mut acc_bits, b) };
            continue;
        }

        let code = LEAD_DECODE[b as usize];
        if code == 0xFF {
            return decode_err("Invalid lead byte");
        }

        if i + 1 >= len {
            return decode_err("Unexpected end of input");
        }

        let b2 = unsafe { *ptr.add(i + 1) };
        if (b2 & 0xC0) != 0x80 {
            return decode_err("Invalid continuation byte");
        }

        i += 2;

        let illegal_index = code >> 1;
        let first_bit = code & 1;
        let next = (first_bit << 6) | (b2 & 0x3F);

        unsafe {
            if illegal_index < 6 {
                push14_scalar(
                    out_ptr,
                    &mut out_pos,
                    &mut acc,
                    &mut acc_bits,
                    ILLEGALS[illegal_index as usize],
                    next,
                );
            } else {
                debug_assert_eq!(illegal_index, SHORTENED);
                push7_scalar(out_ptr, &mut out_pos, &mut acc, &mut acc_bits, next);
            }
        }
    }

    unsafe { out.set_len(out_pos) };
    Ok(out)
}

#[cfg(test)]
mod tests {
    use super::*;
    use alloc::{format, vec};

    #[test]
    fn test_empty() {
        assert_eq!(encode(b""), "");
        assert_eq!(decode("").unwrap(), b"");
    }

    #[test]
    fn test_hello_world() {
        let data = b"hello world";
        let enc = encode(data);
        let dec = decode(&enc).expect("decoding failed");
        assert_eq!(dec, data);
    }

    #[test]
    fn test_single_byte_values() {
        for b in 0..=255u8 {
            let data = vec![b];
            let enc = encode(&data);
            let dec = decode(&enc).expect(&format!("decoding failed for byte {}", b));
            assert_eq!(dec, data, "failed for byte {}", b);
        }
    }

    #[test]
    fn test_various_lengths_roundtrip() {
        for len in [
            0, 1, 2, 3, 6, 7, 8, 9, 14, 15, 16, 17, 31, 32, 33, 100, 255, 256, 511, 512,
        ] {
            let data: Vec<u8> = (0..len).map(|i| (i % 251) as u8).collect();
            let enc = encode(&data);
            let dec = decode(&enc).expect("decoding failed");
            assert_eq!(dec, data, "roundtrip failed for length {}", len);
        }
    }

    #[test]
    fn test_all_illegal_bytes_handling() {
        let data = b"\x00\x0A\x0D\x22\x26\x5C";
        let enc = encode(data);
        let dec = decode(&enc).expect("decoding failed");
        assert_eq!(dec, data.as_ref());
    }

    #[test]
    fn test_mixed_content() {
        let data: Vec<u8> = (0..=255).collect();
        let enc = encode(&data);
        let dec = decode(&enc).expect("decoding failed");
        assert_eq!(dec, data);
    }

    #[test]
    fn test_repeated_illegal_bytes() {
        let data = vec![0u8; 100];
        let enc = encode(&data);
        let dec = decode(&enc).expect("decoding failed");
        assert_eq!(dec, data);
    }

    #[test]
    fn test_decode_invalid_lead_byte() {
        let invalid = vec![0x80u8];
        let s = unsafe { String::from_utf8_unchecked(invalid) };
        assert!(decode(&s).is_err());
        let invalid2 = vec![0xFFu8];
        let s2 = unsafe { String::from_utf8_unchecked(invalid2) };
        assert!(decode(&s2).is_err());
    }

    #[test]
    fn test_decode_truncated_escape() {
        let mut data = vec![0xC0u8];
        let s = unsafe { String::from_utf8_unchecked(data.clone()) };
        assert!(decode(&s).is_err());
        data.push(0x40);
        let s2 = unsafe { String::from_utf8_unchecked(data) };
        assert!(decode(&s2).is_err());
    }

    #[test]
    fn test_decode_invalid_continuation_byte() {
        let data = vec![0xC2u8, 0xFF];
        let s = unsafe { String::from_utf8_unchecked(data) };
        assert!(decode(&s).is_err());
    }

    #[test]
    fn test_shortened_at_end() {
        let data = vec![0u8];
        let enc = encode(&data);
        let dec = decode(&enc).expect("decode failed");
        assert_eq!(dec, data);
    }

    #[test]
    fn test_very_long_input() {
        use rand::Rng;
        const DATA_SIZE: usize = 5_000_000;
        let mut data = vec![0u8; DATA_SIZE];
        let mut rng = rand::rng();
        rng.fill_bytes(&mut data);
        let enc = encode(&data);
        let dec = decode(&enc).expect("decode failed");
        assert_eq!(dec, data);
    }
}