qubit_codec_misc/
base64_quantum_codec.rs1use core::num::NonZeroUsize;
11
12use qubit_codec::Codec;
13use qubit_codec::DecodeFailure;
14use qubit_utils::nonzero;
15
16use crate::MiscCodecError;
17use crate::MiscCodecResult;
18use crate::misc_codec_error::map_misc_decode_failure_with_consumed;
19
20#[must_use]
26#[derive(Debug, Clone, Copy, PartialEq, Eq)]
27pub struct Base64QuantumCodec {
28 url_safe: bool,
29}
30
31impl Base64QuantumCodec {
32 #[inline]
37 pub fn standard() -> Self {
38 Self { url_safe: false }
39 }
40
41 #[inline]
46 pub fn url_safe() -> Self {
47 Self { url_safe: true }
48 }
49
50 #[inline(always)]
55 fn alphabet(&self) -> &'static [u8; 64] {
56 if self.url_safe {
57 b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
58 } else {
59 b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
60 }
61 }
62
63 fn decode_unit(&self, unit: u8, index: usize) -> MiscCodecResult<u8> {
76 match unit {
77 b'A'..=b'Z' => Ok(unit - b'A'),
78 b'a'..=b'z' => Ok(unit - b'a' + 26),
79 b'0'..=b'9' => Ok(unit - b'0' + 52),
80 b'+' if !self.url_safe => Ok(62),
81 b'/' if !self.url_safe => Ok(63),
82 b'-' if self.url_safe => Ok(62),
83 b'_' if self.url_safe => Ok(63),
84 _ => Err(MiscCodecError::InvalidInput {
85 codec: "base64-quantum",
86 reason: format!("invalid Base64 unit '{}' at index {}", char::from(unit), index),
87 }),
88 }
89 }
90}
91
92impl Default for Base64QuantumCodec {
93 #[inline]
95 fn default() -> Self {
96 Self::standard()
97 }
98}
99
100impl Codec for Base64QuantumCodec {
101 type Value = [u8; 3];
102 type Unit = u8;
103 type DecodeError = MiscCodecError;
104 type EncodeError = MiscCodecError;
105
106 const MIN_UNITS_PER_VALUE: usize = 4;
107 const MAX_ENCODE_UNITS_PER_VALUE: usize = 4;
108
109 const MAX_DECODE_UNITS_PER_VALUE: usize = 4;
110
111 unsafe fn decode(
116 &mut self,
117 input: &[u8],
118 input_index: usize,
119 ) -> Result<([u8; 3], NonZeroUsize), DecodeFailure<Self::DecodeError>> {
120 debug_assert!(input_index + 4 <= input.len());
121
122 let first = self
123 .decode_unit(input[input_index], input_index)
124 .map_err(|error| map_misc_decode_failure_with_consumed(error, nonzero(4)))?;
125 let second = self
126 .decode_unit(input[input_index + 1], input_index + 1)
127 .map_err(|error| map_misc_decode_failure_with_consumed(error, nonzero(4)))?;
128 let third = self
129 .decode_unit(input[input_index + 2], input_index + 2)
130 .map_err(|error| map_misc_decode_failure_with_consumed(error, nonzero(4)))?;
131 let fourth = self
132 .decode_unit(input[input_index + 3], input_index + 3)
133 .map_err(|error| map_misc_decode_failure_with_consumed(error, nonzero(4)))?;
134 Ok((
135 [
136 (first << 2) | (second >> 4),
137 (second << 4) | (third >> 2),
138 (third << 6) | fourth,
139 ],
140 nonzero(4),
141 ))
142 }
143
144 unsafe fn encode(
149 &mut self,
150 value: &[u8; 3],
151 output: &mut [u8],
152 output_index: usize,
153 ) -> Result<usize, Self::EncodeError> {
154 debug_assert!(output_index + 4 <= output.len());
155
156 let alphabet = self.alphabet();
157 output[output_index] = alphabet[(value[0] >> 2) as usize];
158 output[output_index + 1] = alphabet[(((value[0] & 0x03) << 4) | (value[1] >> 4)) as usize];
159 output[output_index + 2] = alphabet[(((value[1] & 0x0f) << 2) | (value[2] >> 6)) as usize];
160 output[output_index + 3] = alphabet[(value[2] & 0x3f) as usize];
161 Ok(4)
162 }
163}