base64-ng 2.0.1

no_std-first Base64 encoding and decoding with strict RFC 4648 APIs and optional SIMD
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
//! Heapless strict incremental ordinary decoding.

use core::num::NonZeroUsize;

use super::{
    contracts::{
        BackendFault, Failure, InputError, Lifecycle, OperationError, Progress, SourceSpan, Step,
    },
    decode_primitives::{
        is_legacy_ascii_whitespace, one_byte_tail_is_canonical, pack_full_quantum,
        two_byte_tail_is_canonical,
    },
    specifications::{Base64, Codec, CodecSettings, DecodePadding, TrailingBits},
};

const INPUT_QUANTUM: usize = 4;
const OUTPUT_QUANTUM: usize = 3;

/// Heapless strict Base64 decoder state.
///
/// The current input quantum and pending output quantum are mutually
/// exclusive. This keeps retry state bounded while allowing one-byte output
/// destinations without asking the caller to replay accepted input.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct DecoderState {
    settings: CodecSettings,
    input_mode: InputMode,
    quantum: [u8; INPUT_QUANTUM],
    quantum_indexes: [usize; INPUT_QUANTUM],
    quantum_len: usize,
    pending: [u8; OUTPUT_QUANTUM],
    pending_start: usize,
    pending_len: usize,
    terminal_padding: bool,
    lifecycle: Lifecycle,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum InputMode {
    Strict,
    IgnoreLegacyAsciiWhitespace,
}

impl DecoderState {
    /// Constructs a decoder whose caller selected canonical padded decoding.
    pub(crate) const fn new_padded(settings: CodecSettings) -> Self {
        Self {
            settings,
            input_mode: InputMode::Strict,
            quantum: [0; INPUT_QUANTUM],
            quantum_indexes: [0; INPUT_QUANTUM],
            quantum_len: 0,
            pending: [0; OUTPUT_QUANTUM],
            pending_start: 0,
            pending_len: 0,
            terminal_padding: false,
            lifecycle: Lifecycle::new(),
        }
    }

    /// Constructs a decoder whose caller selected strict unpadded decoding.
    pub(crate) const fn new_unpadded(settings: CodecSettings) -> Self {
        Self::new_padded(settings)
    }

    pub(crate) const fn new_legacy_ascii_whitespace(settings: CodecSettings) -> Self {
        let mut state = Self::new_padded(settings);
        state.input_mode = InputMode::IgnoreLegacyAsciiWhitespace;
        state
    }

    /// Accepts a strict padded input prefix and writes decoded output that fits.
    pub fn update(&mut self, input: &[u8], output: &mut [u8]) -> Result<Step, OperationError> {
        let span = self.lifecycle.reserve_input(input.len())?;
        let consumed = match self.plan_update(input, output.len(), span) {
            Ok(consumed) => consumed,
            Err(failure) => return Err(self.lifecycle.fail(failure)),
        };
        self.lifecycle.commit_input(span, consumed)?;

        let mut produced = self.drain_pending(output);
        let source_start = self.lifecycle.source_position() - consumed;
        let mut input_offset = 0;
        while input_offset < consumed {
            if self.ignores(input[input_offset]) {
                input_offset += 1;
                continue;
            }
            self.quantum[self.quantum_len] = input[input_offset];
            self.quantum_indexes[self.quantum_len] = source_start + input_offset;
            self.quantum_len += 1;
            input_offset += 1;

            if self.quantum_len == INPUT_QUANTUM {
                let Ok(decoded) = decode_quantum(self.settings, self.quantum, self.quantum_indexes)
                else {
                    return Err(self
                        .lifecycle
                        .fail(Failure::Backend(BackendFault::ImpossibleState)));
                };
                self.pending = decoded.bytes;
                self.pending_start = 0;
                self.pending_len = decoded.len;
                self.terminal_padding = decoded.terminal_padding;
                self.quantum_len = 0;
                produced += self.drain_pending(&mut output[produced..]);
            }
        }

        let progress = Progress::new(consumed, produced);
        if self.pending_len != 0 || consumed != input.len() {
            self.lifecycle.output_full(progress, NonZeroUsize::MIN)
        } else {
            self.lifecycle.need_input(progress)
        }
    }

    /// Declares end of input and resolves the selected padding policy.
    pub fn finish(&mut self, output: &mut [u8]) -> Result<Step, OperationError> {
        if self.lifecycle.begin_finish()? {
            return self.lifecycle.finish(Progress::ZERO);
        }

        let mut produced = self.drain_pending(output);
        if self.pending_len != 0 {
            return self
                .lifecycle
                .output_full(Progress::new(0, produced), NonZeroUsize::MIN);
        }
        if self.quantum_len != 0 {
            let decoded = match self.settings.decode_padding() {
                DecodePadding::RequireCanonical => {
                    let failure = Failure::Input(InputError::TruncatedInput {
                        index: self.lifecycle.source_position(),
                    });
                    return Err(self.lifecycle.fail(failure));
                }
                DecodePadding::Forbid | DecodePadding::Indifferent => {
                    match decode_final_tail(
                        self.settings,
                        &self.quantum[..self.quantum_len],
                        &self.quantum_indexes[..self.quantum_len],
                    ) {
                        Ok(decoded) => decoded,
                        Err(error) => return Err(self.lifecycle.fail(Failure::Input(error))),
                    }
                }
            };
            self.pending = decoded.bytes;
            self.pending_start = 0;
            self.pending_len = decoded.len;
            self.quantum_len = 0;
            produced += self.drain_pending(&mut output[produced..]);
            if self.pending_len != 0 {
                return self
                    .lifecycle
                    .output_full(Progress::new(0, produced), NonZeroUsize::MIN);
            }
        }
        self.lifecycle.finish(Progress::new(0, produced))
    }

    /// Resets the state for one unrelated ordinary message.
    pub fn reset(&mut self) {
        self.quantum = [0; INPUT_QUANTUM];
        self.quantum_indexes = [0; INPUT_QUANTUM];
        self.quantum_len = 0;
        self.pending = [0; OUTPUT_QUANTUM];
        self.pending_start = 0;
        self.pending_len = 0;
        self.terminal_padding = false;
        self.lifecycle.reset();
    }

    /// Clears retained ordinary input and output through the reviewed wipe
    /// boundary, then resets the state for reuse.
    ///
    /// This is explicit best-effort cleanup. It cannot retract plaintext
    /// already returned to a caller or clear copies outside this state.
    pub fn clear(&mut self) {
        self.wipe();
    }

    /// Returns the absolute number of input bytes accepted since reset.
    #[must_use]
    pub const fn source_position(&self) -> usize {
        self.lifecycle.source_position()
    }

    /// Returns encoded input bytes retained until the next complete quantum.
    #[must_use]
    pub const fn buffered_input_len(&self) -> usize {
        self.quantum_len
    }

    /// Returns whether a terminal padded quantum has been accepted.
    #[must_use]
    pub const fn has_terminal_padding(&self) -> bool {
        self.terminal_padding
    }

    /// Clears retained input and output through the reviewed wipe boundary.
    pub(crate) fn wipe(&mut self) {
        crate::wipe_bytes(&mut self.quantum);
        crate::wipe_bytes(&mut self.pending);
        self.quantum_indexes = [0; INPUT_QUANTUM];
        self.quantum_len = 0;
        self.pending_start = 0;
        self.pending_len = 0;
        self.terminal_padding = false;
        self.lifecycle.reset();
    }

    fn plan_update(
        &self,
        input: &[u8],
        output_len: usize,
        span: SourceSpan,
    ) -> Result<usize, Failure> {
        let pending_written = self.pending_len.min(output_len);
        let mut pending = self.pending_len - pending_written;
        if pending != 0 {
            return Ok(0);
        }

        let mut available_output = output_len - pending_written;
        let mut quantum = self.quantum;
        let mut indexes = self.quantum_indexes;
        let mut quantum_len = self.quantum_len;
        let mut terminal_padding = self.terminal_padding;
        let mut consumed = 0;

        while consumed < input.len() {
            let index = span
                .index(consumed)
                .ok_or(Failure::Backend(BackendFault::ImpossibleState))?;
            if self.ignores(input[consumed]) {
                consumed += 1;
                continue;
            }
            if terminal_padding {
                return Err(Failure::Input(InputError::TrailingData { index }));
            }

            validate_partial_symbol(
                self.settings,
                quantum,
                &indexes,
                quantum_len,
                input[consumed],
                index,
            )
            .map_err(Failure::Input)?;
            quantum[quantum_len] = input[consumed];
            indexes[quantum_len] = index;
            quantum_len += 1;
            consumed += 1;

            if quantum_len == INPUT_QUANTUM {
                let decoded =
                    decode_quantum(self.settings, quantum, indexes).map_err(Failure::Input)?;
                quantum_len = 0;
                terminal_padding = decoded.terminal_padding;
                let written = decoded.len.min(available_output);
                available_output -= written;
                pending = decoded.len - written;
                if pending != 0 {
                    break;
                }
            }
        }
        Ok(consumed)
    }

    const fn ignores(&self, byte: u8) -> bool {
        matches!(self.input_mode, InputMode::IgnoreLegacyAsciiWhitespace)
            && is_legacy_ascii_whitespace(byte)
    }

    fn drain_pending(&mut self, output: &mut [u8]) -> usize {
        let written = self.pending_len.min(output.len());
        let pending_end = self.pending_start + written;
        output[..written].copy_from_slice(&self.pending[self.pending_start..pending_end]);
        self.pending_start = pending_end;
        self.pending_len -= written;
        if self.pending_len == 0 {
            self.pending_start = 0;
        }
        written
    }

    #[cfg(kani)]
    pub(crate) fn proof_invariants(&self) -> bool {
        self.quantum_len < INPUT_QUANTUM
            && self.pending_start <= OUTPUT_QUANTUM
            && self.pending_len <= OUTPUT_QUANTUM
            && self.pending_start + self.pending_len <= OUTPUT_QUANTUM
            && !(self.quantum_len != 0 && self.pending_len != 0)
            && matches!(
                self.settings.decode_padding(),
                DecodePadding::RequireCanonical
                    | DecodePadding::Forbid
                    | DecodePadding::Indifferent
            )
    }

    #[cfg(test)]
    pub(crate) fn set_source_position_for_test(&mut self, source_position: usize) {
        self.lifecycle = Lifecycle::at_source_position(source_position);
    }
}

impl<S: Codec> Base64<S> {
    /// Constructs a fresh heapless ordinary decoder.
    pub fn decoder(&self) -> DecoderState {
        match self.settings().decode_padding() {
            DecodePadding::RequireCanonical => DecoderState::new_padded(self.settings()),
            DecodePadding::Forbid | DecodePadding::Indifferent => {
                DecoderState::new_unpadded(self.settings())
            }
        }
    }
}

#[derive(Clone, Copy)]
struct DecodedQuantum {
    bytes: [u8; OUTPUT_QUANTUM],
    len: usize,
    terminal_padding: bool,
}

fn validate_partial_symbol(
    settings: CodecSettings,
    quantum: [u8; INPUT_QUANTUM],
    indexes: &[usize; INPUT_QUANTUM],
    position: usize,
    byte: u8,
    index: usize,
) -> Result<(), InputError> {
    if matches!(settings.decode_padding(), DecodePadding::Forbid) {
        return decode_symbol(settings, byte, index).map(|_| ());
    }
    match position {
        0 | 1 => decode_symbol(settings, byte, index).map(|_| ()),
        2 => {
            if byte == b'=' {
                Ok(())
            } else {
                decode_symbol(settings, byte, index).map(|_| ())
            }
        }
        3 if quantum[2] == b'=' && byte != b'=' => {
            Err(InputError::InvalidPadding { index: indexes[2] })
        }
        3 => {
            if byte == b'=' {
                Ok(())
            } else {
                decode_symbol(settings, byte, index).map(|_| ())
            }
        }
        _ => Err(InputError::InvalidLength),
    }
}

fn decode_quantum(
    settings: CodecSettings,
    input: [u8; INPUT_QUANTUM],
    indexes: [usize; INPUT_QUANTUM],
) -> Result<DecodedQuantum, InputError> {
    let first = decode_symbol(settings, input[0], indexes[0])?;
    let second = decode_symbol(settings, input[1], indexes[1])?;

    if matches!(settings.decode_padding(), DecodePadding::Forbid) {
        let third = decode_symbol(settings, input[2], indexes[2])?;
        let fourth = decode_symbol(settings, input[3], indexes[3])?;
        return Ok(DecodedQuantum {
            bytes: pack_full_quantum(first, second, third, fourth),
            len: 3,
            terminal_padding: false,
        });
    }

    match (input[2], input[3]) {
        (b'=', b'=') => {
            if !one_byte_tail_is_canonical(second)
                && settings.trailing_bits() == TrailingBits::RequireCanonical
            {
                return Err(InputError::NonCanonicalTrailingBits { index: indexes[1] });
            }
            Ok(DecodedQuantum {
                bytes: [(first << 2) | (second >> 4), 0, 0],
                len: 1,
                terminal_padding: true,
            })
        }
        (b'=', _) => Err(InputError::InvalidPadding { index: indexes[2] }),
        (third, b'=') => {
            let third = decode_symbol(settings, third, indexes[2])?;
            if !two_byte_tail_is_canonical(third)
                && settings.trailing_bits() == TrailingBits::RequireCanonical
            {
                return Err(InputError::NonCanonicalTrailingBits { index: indexes[2] });
            }
            Ok(DecodedQuantum {
                bytes: [
                    (first << 2) | (second >> 4),
                    (second << 4) | (third >> 2),
                    0,
                ],
                len: 2,
                terminal_padding: true,
            })
        }
        (third, fourth) => {
            let third = decode_symbol(settings, third, indexes[2])?;
            let fourth = decode_symbol(settings, fourth, indexes[3])?;
            Ok(DecodedQuantum {
                bytes: pack_full_quantum(first, second, third, fourth),
                len: 3,
                terminal_padding: false,
            })
        }
    }
}

fn decode_unpadded_tail(
    settings: CodecSettings,
    input: &[u8],
    indexes: &[usize],
) -> Result<DecodedQuantum, InputError> {
    match input {
        [first, second] => {
            let first = decode_symbol(settings, *first, indexes[0])?;
            let second = decode_symbol(settings, *second, indexes[1])?;
            if !one_byte_tail_is_canonical(second)
                && settings.trailing_bits() == TrailingBits::RequireCanonical
            {
                return Err(InputError::NonCanonicalTrailingBits { index: indexes[1] });
            }
            Ok(DecodedQuantum {
                bytes: [(first << 2) | (second >> 4), 0, 0],
                len: 1,
                terminal_padding: false,
            })
        }
        [first, second, third] => {
            let first = decode_symbol(settings, *first, indexes[0])?;
            let second = decode_symbol(settings, *second, indexes[1])?;
            let third = decode_symbol(settings, *third, indexes[2])?;
            if !two_byte_tail_is_canonical(third)
                && settings.trailing_bits() == TrailingBits::RequireCanonical
            {
                return Err(InputError::NonCanonicalTrailingBits { index: indexes[2] });
            }
            Ok(DecodedQuantum {
                bytes: [
                    (first << 2) | (second >> 4),
                    (second << 4) | (third >> 2),
                    0,
                ],
                len: 2,
                terminal_padding: false,
            })
        }
        _ => Err(InputError::InvalidLength),
    }
}

fn decode_final_tail(
    settings: CodecSettings,
    input: &[u8],
    indexes: &[usize],
) -> Result<DecodedQuantum, InputError> {
    if settings.decode_padding() == DecodePadding::Indifferent
        && let [first, second, b'='] = input
    {
        let first = decode_symbol(settings, *first, indexes[0])?;
        let second = decode_symbol(settings, *second, indexes[1])?;
        if !one_byte_tail_is_canonical(second)
            && settings.trailing_bits() == TrailingBits::RequireCanonical
        {
            return Err(InputError::NonCanonicalTrailingBits { index: indexes[1] });
        }
        return Ok(DecodedQuantum {
            bytes: [(first << 2) | (second >> 4), 0, 0],
            len: 1,
            terminal_padding: true,
        });
    }
    decode_unpadded_tail(settings, input, indexes)
}

fn decode_symbol(settings: CodecSettings, byte: u8, index: usize) -> Result<u8, InputError> {
    match settings.alphabet().decode_byte(byte) {
        Some(value) => Ok(value),
        None if byte == b'=' => Err(InputError::InvalidPadding { index }),
        None => Err(InputError::InvalidByte { index, byte }),
    }
}