aboutsummaryrefslogtreecommitdiff
path: root/src/base64/base64.zig
blob: bddc4456455e939dc63394850ead9fc30ab19c96 (plain) (blame)
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
const std = @import("std");

pub const DecodeResult = struct {
    written: usize,
    fail: bool = false,
};

const mixed_decoder = brk: {
    var decoder = zig_base64.standard.decoderWithIgnore("\xff \t\r\n" ++ [_]u8{
        std.ascii.control_code.vt,
        std.ascii.control_code.ff,
    });

    for (zig_base64.url_safe_alphabet_chars[62..], 62..) |c, i| {
        decoder.decoder.char_to_index[c] = @intCast(u8, i);
    }

    break :brk decoder;
};

pub fn decode(destination: []u8, source: []const u8) DecodeResult {
    var wrote: usize = 0;
    mixed_decoder.decode(destination, source, &wrote) catch {
        return .{
            .written = wrote,
            .fail = true,
        };
    };
    return .{ .written = wrote, .fail = false };
}

pub fn encode(destination: []u8, source: []const u8) usize {
    return zig_base64.standard.Encoder.encode(destination, source).len;
}

pub fn decodeLenUpperBound(len: usize) usize {
    return zig_base64.standard.Decoder.calcSizeUpperBound(len) catch {
        //fallback
        return len / 4 * 3;
    };
}

pub fn decodeLen(source: anytype) usize {
    return zig_base64.standard.Decoder.calcSizeForSlice(source) catch {

        //fallback; add 2 to allow for potentially missing padding
        return source.len / 4 * 3 + 2;
    };
}

pub fn encodeLen(source: anytype) usize {
    return zig_base64.standard.Encoder.calcSize(source.len);
}

// This is just std.base64 copy-pasted
// with support for returning how many bytes were decoded
const zig_base64 = struct {
    const assert = std.debug.assert;
    const testing = std.testing;
    const mem = std.mem;

    pub const Error = error{
        InvalidCharacter,
        InvalidPadding,
        NoSpaceLeft,
    };

    const decoderWithIgnoreProto = switch (@import("builtin").zig_backend) {
        .stage1 => fn (ignore: []const u8) Base64DecoderWithIgnore,
        else => *const fn (ignore: []const u8) Base64DecoderWithIgnore,
    };

    /// Base64 codecs
    pub const Codecs = struct {
        alphabet_chars: [64]u8,
        pad_char: ?u8,
        decoderWithIgnore: decoderWithIgnoreProto,
        Encoder: Base64Encoder,
        Decoder: Base64Decoder,
    };

    pub const standard_alphabet_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".*;
    fn standardBase64DecoderWithIgnore(ignore: []const u8) Base64DecoderWithIgnore {
        return Base64DecoderWithIgnore.init(standard_alphabet_chars, '=', ignore);
    }

    /// Standard Base64 codecs, with padding
    pub const standard = Codecs{
        .alphabet_chars = standard_alphabet_chars,
        .pad_char = '=',
        .decoderWithIgnore = standardBase64DecoderWithIgnore,
        .Encoder = Base64Encoder.init(standard_alphabet_chars, '='),
        .Decoder = Base64Decoder.init(standard_alphabet_chars, '='),
    };

    /// Standard Base64 codecs, without padding
    pub const standard_no_pad = Codecs{
        .alphabet_chars = standard_alphabet_chars,
        .pad_char = null,
        .decoderWithIgnore = standardBase64DecoderWithIgnore,
        .Encoder = Base64Encoder.init(standard_alphabet_chars, null),
        .Decoder = Base64Decoder.init(standard_alphabet_chars, null),
    };

    pub const url_safe_alphabet_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_".*;
    fn urlSafeBase64DecoderWithIgnore(ignore: []const u8) Base64DecoderWithIgnore {
        return Base64DecoderWithIgnore.init(url_safe_alphabet_chars, '=', ignore);
    }

    /// URL-safe Base64 codecs, with padding
    pub const url_safe = Codecs{
        .alphabet_chars = url_safe_alphabet_chars,
        .pad_char = '=',
        .decoderWithIgnore = urlSafeBase64DecoderWithIgnore,
        .Encoder = Base64Encoder.init(url_safe_alphabet_chars, '='),
        .Decoder = Base64Decoder.init(url_safe_alphabet_chars, '='),
    };

    /// URL-safe Base64 codecs, without padding
    pub const url_safe_no_pad = Codecs{
        .alphabet_chars = url_safe_alphabet_chars,
        .pad_char = null,
        .decoderWithIgnore = urlSafeBase64DecoderWithIgnore,
        .Encoder = Base64Encoder.init(url_safe_alphabet_chars, null),
        .Decoder = Base64Decoder.init(url_safe_alphabet_chars, null),
    };

    pub const standard_pad_char = @compileError("deprecated; use standard.pad_char");
    pub const standard_encoder = @compileError("deprecated; use standard.Encoder");
    pub const standard_decoder = @compileError("deprecated; use standard.Decoder");

    pub const Base64Encoder = struct {
        alphabet_chars: [64]u8,
        pad_char: ?u8,

        /// A bunch of assertions, then simply pass the data right through.
        pub fn init(alphabet_chars: [64]u8, pad_char: ?u8) Base64Encoder {
            assert(alphabet_chars.len == 64);
            var char_in_alphabet = [_]bool{false} ** 256;
            for (alphabet_chars) |c| {
                assert(!char_in_alphabet[c]);
                assert(pad_char == null or c != pad_char.?);
                char_in_alphabet[c] = true;
            }
            return Base64Encoder{
                .alphabet_chars = alphabet_chars,
                .pad_char = pad_char,
            };
        }

        /// Compute the encoded length
        pub fn calcSize(encoder: *const Base64Encoder, source_len: usize) usize {
            if (encoder.pad_char != null) {
                return @divTrunc(source_len + 2, 3) * 4;
            } else {
                const leftover = source_len % 3;
                return @divTrunc(source_len, 3) * 4 + @divTrunc(leftover * 4 + 2, 3);
            }
        }

        /// dest.len must at least be what you get from ::calcSize.
        pub fn encode(encoder: *const Base64Encoder, dest: []u8, source: []const u8) []const u8 {
            const out_len = encoder.calcSize(source.len);
            assert(dest.len >= out_len);

            var acc: u12 = 0;
            var acc_len: u4 = 0;
            var out_idx: usize = 0;
            for (source) |v| {
                acc = (acc << 8) + v;
                acc_len += 8;
                while (acc_len >= 6) {
                    acc_len -= 6;
                    dest[out_idx] = encoder.alphabet_chars[@truncate(u6, (acc >> acc_len))];
                    out_idx += 1;
                }
            }
            if (acc_len > 0) {
                dest[out_idx] = encoder.alphabet_chars[@truncate(u6, (acc << 6 - acc_len))];
                out_idx += 1;
            }
            if (encoder.pad_char) |pad_char| {
                for (dest[out_idx..out_len]) |*pad| {
                    pad.* = pad_char;
                }
            }
            return dest[0..out_len];
        }
    };

    pub const Base64Decoder = struct {
        const invalid_char: u8 = 0xff;

        /// e.g. 'A' => 0.
        /// `invalid_char` for any value not in the 64 alphabet chars.
        char_to_index: [256]u8,
        pad_char: ?u8,

        pub fn init(alphabet_chars: [64]u8, pad_char: ?u8) Base64Decoder {
            var result = Base64Decoder{
                .char_to_index = [_]u8{invalid_char} ** 256,
                .pad_char = pad_char,
            };

            var char_in_alphabet = [_]bool{false} ** 256;
            for (alphabet_chars, 0..) |c, i| {
                assert(!char_in_alphabet[c]);
                assert(pad_char == null or c != pad_char.?);

                result.char_to_index[c] = @intCast(u8, i);
                char_in_alphabet[c] = true;
            }
            return result;
        }

        /// Return the maximum possible decoded size for a given input length - The actual length may be less if the input includes padding.
        /// `InvalidPadding` is returned if the input length is not valid.
        pub fn calcSizeUpperBound(decoder: *const Base64Decoder, source_len: usize) Error!usize {
            var result = source_len / 4 * 3;
            const leftover = source_len % 4;
            if (decoder.pad_char != null) {
                if (leftover % 4 != 0) return error.InvalidPadding;
            } else {
                if (leftover % 4 == 1) return error.InvalidPadding;
                result += leftover * 3 / 4;
            }
            return result;
        }

        /// Return the exact decoded size for a slice.
        /// `InvalidPadding` is returned if the input length is not valid.
        pub fn calcSizeForSlice(decoder: *const Base64Decoder, source: []const u8) Error!usize {
            const source_len = source.len;
            var result = try decoder.calcSizeUpperBound(source_len);
            if (decoder.pad_char) |pad_char| {
                if (source_len >= 1 and source[source_len - 1] == pad_char) result -= 1;
                if (source_len >= 2 and source[source_len - 2] == pad_char) result -= 1;
            }
            return result;
        }

        /// dest.len must be what you get from ::calcSize.
        /// invalid characters result in error.InvalidCharacter.
        /// invalid padding results in error.InvalidPadding.
        pub fn decode(decoder: *const Base64Decoder, dest: []u8, source: []const u8) Error!void {
            if (decoder.pad_char != null and source.len % 4 != 0) return error.InvalidPadding;
            var acc: u12 = 0;
            var acc_len: u4 = 0;
            var dest_idx: usize = 0;
            var leftover_idx: ?usize = null;
            for (source, 0..) |c, src_idx| {
                const d = decoder.char_to_index[c];
                if (d == invalid_char) {
                    if (decoder.pad_char == null or c != decoder.pad_char.?) return error.InvalidCharacter;
                    leftover_idx = src_idx;
                    break;
                }
                acc = (acc << 6) + d;
                acc_len += 6;
                if (acc_len >= 8) {
                    acc_len -= 8;
                    dest[dest_idx] = @truncate(u8, acc >> acc_len);
                    dest_idx += 1;
                }
            }
            if (acc_len > 4 or (acc & (@as(u12, 1) << acc_len) - 1) != 0) {
                return error.InvalidPadding;
            }
            if (leftover_idx == null) return;
            var leftover = source[leftover_idx.?..];
            if (decoder.pad_char) |pad_char| {
                const padding_len = acc_len / 2;
                var padding_chars: usize = 0;
                for (leftover) |c| {
                    if (c != pad_char) {
                        return if (c == Base64Decoder.invalid_char) error.InvalidCharacter else error.InvalidPadding;
                    }
                    padding_chars += 1;
                }
                if (padding_chars != padding_len) return error.InvalidPadding;
            }
        }
    };

    pub const Base64DecoderWithIgnore = struct {
        decoder: Base64Decoder,
        char_is_ignored: [256]bool,

        pub fn init(alphabet_chars: [64]u8, pad_char: ?u8, ignore_chars: []const u8) Base64DecoderWithIgnore {
            var result = Base64DecoderWithIgnore{
                .decoder = Base64Decoder.init(alphabet_chars, pad_char),
                .char_is_ignored = [_]bool{false} ** 256,
            };
            for (ignore_chars) |c| {
                assert(result.decoder.char_to_index[c] == Base64Decoder.invalid_char);
                assert(!result.char_is_ignored[c]);
                assert(result.decoder.pad_char != c);
                result.char_is_ignored[c] = true;
            }
            return result;
        }

        /// Return the maximum possible decoded size for a given input length - The actual length may be less if the input includes padding
        /// `InvalidPadding` is returned if the input length is not valid.
        pub fn calcSizeUpperBound(decoder_with_ignore: *const Base64DecoderWithIgnore, source_len: usize) usize {
            var result = source_len / 4 * 3;
            if (decoder_with_ignore.decoder.pad_char == null) {
                const leftover = source_len % 4;
                result += leftover * 3 / 4;
            }
            return result;
        }

        /// Invalid characters that are not ignored result in error.InvalidCharacter.
        /// Invalid padding results in error.InvalidPadding.
        /// Decoding more data than can fit in dest results in error.NoSpaceLeft. See also ::calcSizeUpperBound.
        /// Returns the number of bytes written to dest.
        pub fn decode(decoder_with_ignore: *const Base64DecoderWithIgnore, dest: []u8, source: []const u8, wrote: *usize) Error!void {
            const decoder = &decoder_with_ignore.decoder;
            var acc: u12 = 0;
            var acc_len: u4 = 0;
            var dest_idx: usize = 0;
            var leftover_idx: ?usize = null;

            defer {
                wrote.* = dest_idx;
            }

            for (source, 0..) |c, src_idx| {
                if (decoder_with_ignore.char_is_ignored[c]) continue;
                const d = decoder.char_to_index[c];
                if (d == Base64Decoder.invalid_char) {
                    if (decoder.pad_char) |pad_char| {
                        if (c == pad_char) {
                            leftover_idx = src_idx;
                            break;
                        }
                    }
                    if (decoder_with_ignore.char_is_ignored[Base64Decoder.invalid_char]) continue;
                    return error.InvalidCharacter;
                }
                acc = (acc << 6) + d;
                acc_len += 6;
                if (acc_len >= 8) {
                    if (dest_idx == dest.len) return error.NoSpaceLeft;
                    acc_len -= 8;
                    dest[dest_idx] = @truncate(u8, acc >> acc_len);
                    dest_idx += 1;
                }
            }
            if (acc_len > 4 or (acc & (@as(u12, 1) << acc_len) - 1) != 0) return error.InvalidPadding;

            if (decoder.pad_char) |pad_char| {
                const padding_len = acc_len / 2;

                if (leftover_idx) |idx| {
                    var leftover = source[idx..];
                    var padding_chars: usize = 0;
                    for (leftover) |c| {
                        if (decoder_with_ignore.char_is_ignored[c]) continue;
                        if (c != pad_char) {
                            return if (c == Base64Decoder.invalid_char) error.InvalidCharacter else error.InvalidPadding;
                        }
                        padding_chars += 1;
                    }
                    if (padding_chars != padding_len) return error.InvalidPadding;
                } else if (padding_len > 0) {
                    return error.InvalidPadding;
                }
            }
        }
    };

    test "base64" {
        @setEvalBranchQuota(8000);
        try testBase64();
        comptime try testAllApis(standard, "comptime", "Y29tcHRpbWU=");
    }

    test "base64 url_safe_no_pad" {
        @setEvalBranchQuota(8000);
        try testBase64UrlSafeNoPad();
        comptime try testAllApis(url_safe_no_pad, "comptime", "Y29tcHRpbWU");
    }

    fn testBase64() !void {
        const codecs = standard;

        try testAllApis(codecs, "", "");
        try testAllApis(codecs, "f", "Zg==");
        try testAllApis(codecs, "fo", "Zm8=");
        try testAllApis(codecs, "foo", "Zm9v");
        try testAllApis(codecs, "foob", "Zm9vYg==");
        try testAllApis(codecs, "fooba", "Zm9vYmE=");
        try testAllApis(codecs, "foobar", "Zm9vYmFy");

        try testDecodeIgnoreSpace(codecs, "", " ");
        try testDecodeIgnoreSpace(codecs, "f", "Z g= =");
        try testDecodeIgnoreSpace(codecs, "fo", "    Zm8=");
        try testDecodeIgnoreSpace(codecs, "foo", "Zm9v    ");
        try testDecodeIgnoreSpace(codecs, "foob", "Zm9vYg = = ");
        try testDecodeIgnoreSpace(codecs, "fooba", "Zm9v YmE=");
        try testDecodeIgnoreSpace(codecs, "foobar", " Z m 9 v Y m F y ");

        // test getting some api errors
        try testError(codecs, "A", error.InvalidPadding);
        try testError(codecs, "AA", error.InvalidPadding);
        try testError(codecs, "AAA", error.InvalidPadding);
        try testError(codecs, "A..A", error.InvalidCharacter);
        try testError(codecs, "AA=A", error.InvalidPadding);
        try testError(codecs, "AA/=", error.InvalidPadding);
        try testError(codecs, "A/==", error.InvalidPadding);
        try testError(codecs, "A===", error.InvalidPadding);
        try testError(codecs, "====", error.InvalidPadding);

        try testNoSpaceLeftError(codecs, "AA==");
        try testNoSpaceLeftError(codecs, "AAA=");
        try testNoSpaceLeftError(codecs, "AAAA");
        try testNoSpaceLeftError(codecs, "AAAAAA==");
    }

    fn testBase64UrlSafeNoPad() !void {
        const codecs = url_safe_no_pad;

        try testAllApis(codecs, "", "");
        try testAllApis(codecs, "f", "Zg");
        try testAllApis(codecs, "fo", "Zm8");
        try testAllApis(codecs, "foo", "Zm9v");
        try testAllApis(codecs, "foob", "Zm9vYg");
        try testAllApis(codecs, "fooba", "Zm9vYmE");
        try testAllApis(codecs, "foobar", "Zm9vYmFy");

        try testDecodeIgnoreSpace(codecs, "", " ");
        try testDecodeIgnoreSpace(codecs, "f", "Z g ");
        try testDecodeIgnoreSpace(codecs, "fo", "    Zm8");
        try testDecodeIgnoreSpace(codecs, "foo", "Zm9v    ");
        try testDecodeIgnoreSpace(codecs, "foob", "Zm9vYg   ");
        try testDecodeIgnoreSpace(codecs, "fooba", "Zm9v YmE");
        try testDecodeIgnoreSpace(codecs, "foobar", " Z m 9 v Y m F y ");

        // test getting some api errors
        try testError(codecs, "A", error.InvalidPadding);
        try testError(codecs, "AAA=", error.InvalidCharacter);
        try testError(codecs, "A..A", error.InvalidCharacter);
        try testError(codecs, "AA=A", error.InvalidCharacter);
        try testError(codecs, "AA/=", error.InvalidCharacter);
        try testError(codecs, "A/==", error.InvalidCharacter);
        try testError(codecs, "A===", error.InvalidCharacter);
        try testError(codecs, "====", error.InvalidCharacter);

        try testNoSpaceLeftError(codecs, "AA");
        try testNoSpaceLeftError(codecs, "AAA");
        try testNoSpaceLeftError(codecs, "AAAA");
        try testNoSpaceLeftError(codecs, "AAAAAA");
    }

    fn testAllApis(codecs: Codecs, expected_decoded: []const u8, expected_encoded: []const u8) !void {
        // Base64Encoder
        {
            var buffer: [0x100]u8 = undefined;
            const encoded = codecs.Encoder.encode(&buffer, expected_decoded);
            try testing.expectEqualSlices(u8, expected_encoded, encoded);
        }

        // Base64Decoder
        {
            var buffer: [0x100]u8 = undefined;
            var decoded = buffer[0..try codecs.Decoder.calcSizeForSlice(expected_encoded)];
            try codecs.Decoder.decode(decoded, expected_encoded);
            try testing.expectEqualSlices(u8, expected_decoded, decoded);
        }

        // Base64DecoderWithIgnore
        {
            const decoder_ignore_nothing = codecs.decoderWithIgnore("");
            var buffer: [0x100]u8 = undefined;
            var decoded = buffer[0..try decoder_ignore_nothing.calcSizeUpperBound(expected_encoded.len)];
            var written = try decoder_ignore_nothing.decode(decoded, expected_encoded);
            try testing.expect(written <= decoded.len);
            try testing.expectEqualSlices(u8, expected_decoded, decoded[0..written]);
        }
    }

    fn testDecodeIgnoreSpace(codecs: Codecs, expected_decoded: []const u8, encoded: []const u8) !void {
        const decoder_ignore_space = codecs.decoderWithIgnore(" ");
        var buffer: [0x100]u8 = undefined;
        var decoded = buffer[0..try decoder_ignore_space.calcSizeUpperBound(encoded.len)];
        var written = try decoder_ignore_space.decode(decoded, encoded);
        try testing.expectEqualSlices(u8, expected_decoded, decoded[0..written]);
    }

    fn testError(codecs: Codecs, encoded: []const u8, expected_err: anyerror) !void {
        const decoder_ignore_space = codecs.decoderWithIgnore(" ");
        var buffer: [0x100]u8 = undefined;
        if (codecs.Decoder.calcSizeForSlice(encoded)) |decoded_size| {
            var decoded = buffer[0..decoded_size];
            if (codecs.Decoder.decode(decoded, encoded)) |_| {
                return error.ExpectedError;
            } else |err| if (err != expected_err) return err;
        } else |err| if (err != expected_err) return err;

        if (decoder_ignore_space.decode(buffer[0..], encoded)) |_| {
            return error.ExpectedError;
        } else |err| if (err != expected_err) return err;
    }

    fn testNoSpaceLeftError(codecs: Codecs, encoded: []const u8) !void {
        const decoder_ignore_space = codecs.decoderWithIgnore(" ");
        var buffer: [0x100]u8 = undefined;
        var decoded = buffer[0 .. (try codecs.Decoder.calcSizeForSlice(encoded)) - 1];
        if (decoder_ignore_space.decode(decoded, encoded)) |_| {
            return error.ExpectedError;
        } else |err| if (err != error.NoSpaceLeft) return err;
    }
};