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
extern crate std;

use std::{vec, vec::Vec};

use super::{
    contracts::{Failure, InputError, OperationError, Status, TerminalError},
    incremental_decoder::DecoderState,
    rfc4648_oracle::{self as oracle, ErrorClass, Profile},
    specifications::{CodecBuilder, STRICT_STANDARD_PADDED, STRICT_URL_SAFE_PADDED},
};

const RFC_VECTORS: &[(&[u8], &[u8])] = &[
    (b"", b""),
    (b"f", b"Zg=="),
    (b"fo", b"Zm8="),
    (b"foo", b"Zm9v"),
    (b"foob", b"Zm9vYg=="),
    (b"fooba", b"Zm9vYmE="),
    (b"foobar", b"Zm9vYmFy"),
];

#[test]
fn rfc4648_vectors_pass_every_input_and_output_partition() {
    for &(expected, encoded) in RFC_VECTORS {
        for profile in profiles() {
            assert_every_valid_partition(profile, encoded, expected);
        }
    }
}

#[test]
fn bounded_binary_inputs_match_independent_oracle_for_every_partition() {
    let mut plain = [0_u8; 6];
    for len in 0..=plain.len() {
        for (index, byte) in plain[..len].iter_mut().enumerate() {
            *byte = u8::try_from((index * 67 + len * 37) % 256).unwrap();
        }
        for profile in profiles() {
            let encoded = oracle::encode(profile, &plain[..len]);
            assert_every_valid_partition(profile, &encoded, &plain[..len]);
        }
    }
}

#[test]
fn malformed_inputs_keep_exact_indexes_across_every_chunk_boundary() {
    let cases = [
        malformed(Profile::StandardPadded, b"=AAA", invalid_padding(0)),
        malformed(Profile::StandardPadded, b"A=AA", invalid_padding(1)),
        malformed(Profile::StandardPadded, b"AA=A", invalid_padding(2)),
        malformed(Profile::StandardPadded, b"Zm!v", invalid_byte(2, b'!')),
        malformed(Profile::StandardPadded, b"AAAAZm!v", invalid_byte(6, b'!')),
        malformed(Profile::StandardPadded, b"AA-A", invalid_byte(2, b'-')),
        malformed(Profile::UrlSafePadded, b"AA+A", invalid_byte(2, b'+')),
        malformed(Profile::StandardPadded, b"AB==", noncanonical(1)),
        malformed(Profile::StandardPadded, b"AAB=", noncanonical(2)),
        malformed(Profile::StandardPadded, b"AA==A", trailing(4)),
        malformed(Profile::StandardPadded, b"AAA=A", trailing(4)),
    ];

    for case in cases {
        for mask in partition_masks(case.input.len()) {
            assert_malformed_partition(case, mask);
        }
    }
}

#[test]
fn every_non_alphabet_byte_is_rejected_at_every_quantum_position() {
    for profile in profiles() {
        for value in 0_u16..=u16::from(u8::MAX) {
            let byte = u8::try_from(value).unwrap();
            if byte == b'=' || profile_alphabet(profile).contains(&byte) {
                continue;
            }
            for position in 0..4 {
                let mut input = *b"AAAA";
                input[position] = byte;
                let case = malformed(profile, &input, invalid_byte(position, byte));
                for mask in partition_masks(input.len()) {
                    assert_malformed_partition(case, mask);
                }
            }
        }
    }
}

#[test]
fn every_ascii_whitespace_byte_is_rejected_at_every_quantum_position() {
    for profile in profiles() {
        for byte in *b" \t\r\n" {
            for position in 0..4 {
                let mut input = *b"AAAA";
                input[position] = byte;
                let case = malformed(profile, &input, invalid_byte(position, byte));
                for mask in partition_masks(input.len()) {
                    assert_malformed_partition(case, mask);
                }
            }
        }
    }
}

#[test]
fn every_noncanonical_terminal_value_is_rejected_across_every_boundary() {
    for profile in profiles() {
        let alphabet = profile_alphabet(profile);
        for (value, byte) in alphabet.iter().copied().enumerate() {
            if value & 0x0f != 0 {
                let input = [b'A', byte, b'=', b'='];
                let case = malformed(profile, &input, noncanonical(1));
                for mask in partition_masks(input.len()) {
                    assert_malformed_partition(case, mask);
                }
            }
            if value & 0x03 != 0 {
                let input = [b'A', b'A', byte, b'='];
                let case = malformed(profile, &input, noncanonical(2));
                for mask in partition_masks(input.len()) {
                    assert_malformed_partition(case, mask);
                }
            }
        }
    }
}

#[test]
fn truncated_padded_inputs_fail_at_the_absolute_end_position() {
    for input in [&b"A"[..], &b"AA"[..], &b"AAA"[..], &b"Zg="[..]] {
        let case = malformed(
            Profile::StandardPadded,
            input,
            InputError::TruncatedInput { index: input.len() },
        );
        for mask in partition_masks(input.len()) {
            assert_malformed_partition(case, mask);
        }
    }
}

#[test]
fn one_byte_destinations_drain_pending_output_without_reconsuming_input() {
    let mut state = STRICT_STANDARD_PADDED.decoder();
    let mut no_output = [];
    let first = state.update(b"Zm9v", &mut no_output).unwrap();
    assert_eq!(first.progress().input_consumed(), 4);
    assert_eq!(first.progress().output_produced(), 0);
    assert_output_full(first.status());
    assert_eq!(state.source_position(), 4);

    for _ in 0..3 {
        let stalled = state.update(b"", &mut no_output).unwrap();
        assert_eq!(stalled.progress().input_consumed(), 0);
        assert_eq!(stalled.progress().output_produced(), 0);
        assert_output_full(stalled.status());
    }

    let mut decoded = [0_u8; 3];
    for byte in &mut decoded {
        let step = state.update(b"", core::slice::from_mut(byte)).unwrap();
        assert_eq!(step.progress().input_consumed(), 0);
        assert_eq!(step.progress().output_produced(), 1);
    }
    assert_eq!(&decoded, b"foo");
    assert_eq!(
        state.finish(&mut no_output).unwrap().status(),
        Status::Complete
    );
    assert_eq!(
        state.finish(&mut no_output).unwrap().status(),
        Status::Complete
    );
    assert_eq!(
        state.update(b"A", &mut no_output),
        Err(OperationError::Terminal(TerminalError::InputAfterComplete))
    );
}

#[test]
fn finish_closes_padded_decoder_input_before_pending_output_drains() {
    let mut state = STRICT_STANDARD_PADDED.decoder();
    state.update(b"Zm9v", &mut []).unwrap();
    let finishing = state.finish(&mut []).unwrap();
    assert_output_full(finishing.status());
    assert_eq!(
        state.update(b"A", &mut []),
        Err(OperationError::Terminal(TerminalError::InputAfterFinish))
    );
    let mut output = [0_u8; 3];
    assert_eq!(
        state.finish(&mut output).unwrap().status(),
        Status::Complete
    );
    assert_eq!(&output, b"foo");
}

#[test]
fn validated_runtime_alphabet_decodes_strict_padded_input() {
    const CUSTOM: [u8; 64] = *b"./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    let codec = CodecBuilder::from_table(CUSTOM).unwrap().build().unwrap();
    let encoded = oracle::encode(Profile::StandardPadded, b"custom");
    let mut translated = Vec::with_capacity(encoded.len());
    for byte in encoded {
        translated.push(if byte == b'=' {
            b'='
        } else {
            let value = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
                .iter()
                .position(|candidate| *candidate == byte)
                .unwrap();
            CUSTOM[value]
        });
    }

    let mut state = DecoderState::new_padded(codec.settings());
    let actual = drive_valid(&mut state, &translated, &[1; 8], &[1; 6]);
    assert_eq!(actual, b"custom");
}

#[test]
fn reset_discards_ordinary_decoder_state_without_drop_overhead() {
    assert!(!core::mem::needs_drop::<DecoderState>());
    let mut state = STRICT_STANDARD_PADDED.decoder();
    let mut no_output = [];
    state.update(b"Zm9v", &mut no_output).unwrap();
    state.reset();
    assert_eq!(state.source_position(), 0);
    assert_eq!(drive_valid(&mut state, b"Zg==", &[1; 4], &[1]), b"f");
}

fn assert_every_valid_partition(profile: Profile, input: &[u8], expected: &[u8]) {
    for input_mask in partition_masks(input.len()) {
        let input_chunks = partition_lengths(input.len(), input_mask);
        for output_mask in partition_masks(expected.len()) {
            let output_chunks = partition_lengths(expected.len(), output_mask);
            let mut state = decoder(profile);
            let actual = drive_valid(&mut state, input, &input_chunks, &output_chunks);
            assert_eq!(actual, expected);
        }
    }
}

fn drive_valid(
    state: &mut DecoderState,
    input: &[u8],
    input_chunks: &[usize],
    output_chunks: &[usize],
) -> Vec<u8> {
    let mut output = Vec::new();
    let mut input_start = 0;
    let mut output_chunk = 0;

    for &input_len in input_chunks {
        let input_end = input_start + input_len;
        let mut accepted = input_start;
        loop {
            let capacity = output_chunks.get(output_chunk).copied().unwrap_or(1);
            output_chunk += 1;
            let mut destination = [0_u8; 16];
            let step = state
                .update(&input[accepted..input_end], &mut destination[..capacity])
                .unwrap();
            accepted += step.progress().input_consumed();
            output.extend_from_slice(&destination[..step.progress().output_produced()]);
            match step.status() {
                Status::NeedInput => {
                    assert_eq!(accepted, input_end);
                    break;
                }
                Status::OutputFull(requirement) => {
                    assert_eq!(requirement.minimum_output().get(), 1);
                }
                Status::Complete => panic!("update completed before finish"),
            }
        }
        input_start = input_end;
    }

    loop {
        let capacity = output_chunks.get(output_chunk).copied().unwrap_or(1);
        output_chunk += 1;
        let mut destination = [0_u8; 16];
        let step = state.finish(&mut destination[..capacity]).unwrap();
        output.extend_from_slice(&destination[..step.progress().output_produced()]);
        match step.status() {
            Status::Complete => break,
            Status::OutputFull(requirement) => {
                assert_eq!(requirement.minimum_output().get(), 1);
            }
            Status::NeedInput => panic!("finish requested more input"),
        }
    }
    output
}

fn assert_malformed_partition(case: MalformedCase<'_>, mask: usize) {
    let chunks = partition_lengths(case.input.len(), mask);
    let mut state = decoder(case.profile);
    let mut input_start = 0;
    let mut call = 0;

    for input_len in chunks {
        let input_end = input_start + input_len;
        let mut accepted = input_start;
        loop {
            let capacity = call % 4;
            call += 1;
            let mut destination = [0xa5_u8; 3];
            let before = destination;
            match state.update(
                &case.input[accepted..input_end],
                &mut destination[..capacity],
            ) {
                Ok(step) => {
                    accepted += step.progress().input_consumed();
                    match step.status() {
                        Status::NeedInput => break,
                        Status::OutputFull(_) => {}
                        Status::Complete => panic!("update completed before finish"),
                    }
                }
                Err(error) => {
                    assert_eq!(destination, before);
                    assert_absorbing(&mut state, error, case.expected);
                    assert_oracle_index_when_comparable(case);
                    return;
                }
            }
        }
        input_start = input_end;
    }

    let mut destination = [0xa5_u8; 3];
    let before = destination;
    let error = state.finish(&mut destination).unwrap_err();
    assert_eq!(destination, before);
    assert_absorbing(&mut state, error, case.expected);
}

fn assert_absorbing(state: &mut DecoderState, error: OperationError, expected: InputError) {
    let expected = OperationError::Failed(Failure::Input(expected));
    assert_eq!(error, expected);
    assert_eq!(state.update(b"", &mut []), Err(expected));
    assert_eq!(state.finish(&mut []), Err(expected));
}

fn assert_oracle_index_when_comparable(case: MalformedCase<'_>) {
    if !case.input.len().is_multiple_of(4) {
        return;
    }
    let oracle_error = oracle::decode(case.profile, case.input).unwrap_err();
    assert_eq!(oracle_error.offset, input_error_index(case.expected));
    match case.expected {
        InputError::InvalidByte { .. } => assert_eq!(oracle_error.class, ErrorClass::Byte),
        InputError::InvalidPadding { .. } | InputError::NonCanonicalTrailingBits { .. } => {
            assert_eq!(oracle_error.class, ErrorClass::Padding);
        }
        _ => {}
    }
}

#[derive(Clone, Copy)]
struct MalformedCase<'a> {
    profile: Profile,
    input: &'a [u8],
    expected: InputError,
}

const fn malformed(profile: Profile, input: &[u8], expected: InputError) -> MalformedCase<'_> {
    MalformedCase {
        profile,
        input,
        expected,
    }
}

const fn invalid_byte(index: usize, byte: u8) -> InputError {
    InputError::InvalidByte { index, byte }
}

const fn invalid_padding(index: usize) -> InputError {
    InputError::InvalidPadding { index }
}

const fn noncanonical(index: usize) -> InputError {
    InputError::NonCanonicalTrailingBits { index }
}

const fn trailing(index: usize) -> InputError {
    InputError::TrailingData { index }
}

const fn input_error_index(error: InputError) -> Option<usize> {
    match error {
        InputError::InvalidByte { index, .. }
        | InputError::InvalidPadding { index }
        | InputError::NonCanonicalTrailingBits { index }
        | InputError::TruncatedInput { index }
        | InputError::TrailingData { index }
        | InputError::InvalidLineWrap { index } => Some(index),
        InputError::InvalidLength => None,
    }
}

fn decoder(profile: Profile) -> DecoderState {
    match profile {
        Profile::StandardPadded => STRICT_STANDARD_PADDED.decoder(),
        Profile::UrlSafePadded => STRICT_URL_SAFE_PADDED.decoder(),
        Profile::StandardUnpadded | Profile::UrlSafeUnpadded => {
            panic!("Commit 10 covers padded decoding only")
        }
    }
}

const fn profiles() -> [Profile; 2] {
    [Profile::StandardPadded, Profile::UrlSafePadded]
}

const fn profile_alphabet(profile: Profile) -> &'static [u8; 64] {
    match profile {
        Profile::StandardPadded => {
            b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
        }
        Profile::UrlSafePadded => {
            b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
        }
        Profile::StandardUnpadded | Profile::UrlSafeUnpadded => {
            panic!("Commit 10 covers padded decoding only")
        }
    }
}

fn assert_output_full(status: Status) {
    let Status::OutputFull(requirement) = status else {
        panic!("expected output-full status");
    };
    assert_eq!(requirement.minimum_output().get(), 1);
}

fn partition_masks(len: usize) -> Vec<usize> {
    if len <= 1 {
        return vec![0];
    }
    (0..(1_usize << (len - 1))).collect()
}

fn partition_lengths(len: usize, mask: usize) -> Vec<usize> {
    if len == 0 {
        return vec![0];
    }
    let mut lengths = Vec::new();
    let mut start = 0;
    for boundary in 1..len {
        if mask & (1 << (boundary - 1)) != 0 {
            lengths.push(boundary - start);
            start = boundary;
        }
    }
    lengths.push(len - start);
    lengths
}

#[test]
fn explicit_clear_resets_retained_decoder_state_for_reuse() {
    let mut state = STRICT_STANDARD_PADDED.decoder();
    let mut output = [0_u8; 3];
    let step = state.update(b"c2", &mut output).unwrap();
    assert_eq!(step.progress().input_consumed(), 2);
    assert_eq!(state.buffered_input_len(), 2);

    state.clear();

    assert_eq!(state, STRICT_STANDARD_PADDED.decoder());
    assert_eq!(state.source_position(), 0);
}