| ... | @@ -1,4 +1,5 @@ | ... | @@ -1,4 +1,5 @@ |
| 1 | //! Base64 encoding/decoding. | 1 | //! Base64 encoding/decoding as specified by |
| | 2 | //! [RFC 4648](https://datatracker.ietf.org/doc/html/rfc4648). |
| 2 | | 3 | |
| 3 | const std = @import("std.zig"); | 4 | const std = @import("std.zig"); |
| 4 | const assert = std.debug.assert; | 5 | const assert = std.debug.assert; |
| ... | @@ -24,12 +25,15 @@ pub const Codecs = struct { | ... | @@ -24,12 +25,15 @@ pub const Codecs = struct { |
| 24 | Decoder: Base64Decoder, | 25 | Decoder: Base64Decoder, |
| 25 | }; | 26 | }; |
| 26 | | 27 | |
| | 28 | /// The Base64 alphabet defined in |
| | 29 | /// [RFC 4648 section 4](https://datatracker.ietf.org/doc/html/rfc4648#section-4). |
| 27 | pub const standard_alphabet_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".*; | 30 | pub const standard_alphabet_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".*; |
| 28 | fn standardBase64DecoderWithIgnore(ignore: []const u8) Base64DecoderWithIgnore { | 31 | fn standardBase64DecoderWithIgnore(ignore: []const u8) Base64DecoderWithIgnore { |
| 29 | return Base64DecoderWithIgnore.init(standard_alphabet_chars, '=', ignore); | 32 | return Base64DecoderWithIgnore.init(standard_alphabet_chars, '=', ignore); |
| 30 | } | 33 | } |
| 31 | | 34 | |
| 32 | /// Standard Base64 codecs, with padding | 35 | /// Standard Base64 codecs, with padding, as defined in |
| | 36 | /// [RFC 4648 section 4](https://datatracker.ietf.org/doc/html/rfc4648#section-4). |
| 33 | pub const standard = Codecs{ | 37 | pub const standard = Codecs{ |
| 34 | .alphabet_chars = standard_alphabet_chars, | 38 | .alphabet_chars = standard_alphabet_chars, |
| 35 | .pad_char = '=', | 39 | .pad_char = '=', |
| ... | @@ -38,7 +42,8 @@ pub const standard = Codecs{ | ... | @@ -38,7 +42,8 @@ pub const standard = Codecs{ |
| 38 | .Decoder = Base64Decoder.init(standard_alphabet_chars, '='), | 42 | .Decoder = Base64Decoder.init(standard_alphabet_chars, '='), |
| 39 | }; | 43 | }; |
| 40 | | 44 | |
| 41 | /// Standard Base64 codecs, without padding | 45 | /// Standard Base64 codecs, without padding, as defined in |
| | 46 | /// [RFC 4648 section 3.2](https://datatracker.ietf.org/doc/html/rfc4648#section-3.2). |
| 42 | pub const standard_no_pad = Codecs{ | 47 | pub const standard_no_pad = Codecs{ |
| 43 | .alphabet_chars = standard_alphabet_chars, | 48 | .alphabet_chars = standard_alphabet_chars, |
| 44 | .pad_char = null, | 49 | .pad_char = null, |
| ... | @@ -47,12 +52,15 @@ pub const standard_no_pad = Codecs{ | ... | @@ -47,12 +52,15 @@ pub const standard_no_pad = Codecs{ |
| 47 | .Decoder = Base64Decoder.init(standard_alphabet_chars, null), | 52 | .Decoder = Base64Decoder.init(standard_alphabet_chars, null), |
| 48 | }; | 53 | }; |
| 49 | | 54 | |
| | 55 | /// The URL-safe Base64 alphabet defined in |
| | 56 | /// [RFC 4648 section 5](https://datatracker.ietf.org/doc/html/rfc4648#section-5). |
| 50 | pub const url_safe_alphabet_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_".*; | 57 | pub const url_safe_alphabet_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_".*; |
| 51 | fn urlSafeBase64DecoderWithIgnore(ignore: []const u8) Base64DecoderWithIgnore { | 58 | fn urlSafeBase64DecoderWithIgnore(ignore: []const u8) Base64DecoderWithIgnore { |
| 52 | return Base64DecoderWithIgnore.init(url_safe_alphabet_chars, null, ignore); | 59 | return Base64DecoderWithIgnore.init(url_safe_alphabet_chars, null, ignore); |
| 53 | } | 60 | } |
| 54 | | 61 | |
| 55 | /// URL-safe Base64 codecs, with padding | 62 | /// URL-safe Base64 codecs, with padding, as defined in |
| | 63 | /// [RFC 4648 section 5](https://datatracker.ietf.org/doc/html/rfc4648#section-5). |
| 56 | pub const url_safe = Codecs{ | 64 | pub const url_safe = Codecs{ |
| 57 | .alphabet_chars = url_safe_alphabet_chars, | 65 | .alphabet_chars = url_safe_alphabet_chars, |
| 58 | .pad_char = '=', | 66 | .pad_char = '=', |
| ... | @@ -61,7 +69,8 @@ pub const url_safe = Codecs{ | ... | @@ -61,7 +69,8 @@ pub const url_safe = Codecs{ |
| 61 | .Decoder = Base64Decoder.init(url_safe_alphabet_chars, '='), | 69 | .Decoder = Base64Decoder.init(url_safe_alphabet_chars, '='), |
| 62 | }; | 70 | }; |
| 63 | | 71 | |
| 64 | /// URL-safe Base64 codecs, without padding | 72 | /// URL-safe Base64 codecs, without padding, as defined in |
| | 73 | /// [RFC 4648 section 3.2](https://datatracker.ietf.org/doc/html/rfc4648#section-3.2). |
| 65 | pub const url_safe_no_pad = Codecs{ | 74 | pub const url_safe_no_pad = Codecs{ |
| 66 | .alphabet_chars = url_safe_alphabet_chars, | 75 | .alphabet_chars = url_safe_alphabet_chars, |
| 67 | .pad_char = null, | 76 | .pad_char = null, |