authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-28 17:11:07-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-03-28 17:11:07-07:00
logbb5cfbcb722b7e69b343c525803b15eb39d760f8
treea83899b9652ada4596b934db48f89337af61ab4a
parentbbe6a0dddde0e19b595dddf7e029b3e950ac14a0
parent6993087edceb0d80b120f8dd3927d77564f86cb3
signature Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #8305 from jedisct1/base64

std/base64: cleanups & support url-safe and other non-padded variants

5 files changed, 334 insertions(+), 336 deletions(-)

doc/langref.html.in+3-3
...@@ -9952,9 +9952,9 @@ export fn decode_base_64(...@@ -9952,9 +9952,9 @@ export fn decode_base_64(
9952) usize {9952) usize {
9953 const src = source_ptr[0..source_len];9953 const src = source_ptr[0..source_len];
9954 const dest = dest_ptr[0..dest_len];9954 const dest = dest_ptr[0..dest_len];
9955 const base64_decoder = base64.standard_decoder_unsafe;9955 const base64_decoder = base64.standard.Decoder;
9956 const decoded_size = base64_decoder.calcSize(src);9956 const decoded_size = base64_decoder.calcSizeForSlice(src) catch unreachable;
9957 base64_decoder.decode(dest[0..decoded_size], src);9957 base64_decoder.decode(dest[0..decoded_size], src) catch unreachable;
9958 return decoded_size;9958 return decoded_size;
9959}9959}
9960 {#code_end#}9960 {#code_end#}
lib/std/base64.zig+322-324
...@@ -8,454 +8,452 @@ const assert = std.debug.assert;...@@ -8,454 +8,452 @@ const assert = std.debug.assert;
8const testing = std.testing;8const testing = std.testing;
9const mem = std.mem;9const mem = std.mem;
1010
11pub const standard_alphabet_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";11pub const Error = error{
12pub const standard_pad_char = '=';12 InvalidCharacter,
13pub const standard_encoder = Base64Encoder.init(standard_alphabet_chars, standard_pad_char);13 InvalidPadding,
14 NoSpaceLeft,
15};
16
17/// Base64 codecs
18pub const Codecs = struct {
19 alphabet_chars: [64]u8,
20 pad_char: ?u8,
21 decoderWithIgnore: fn (ignore: []const u8) Base64DecoderWithIgnore,
22 Encoder: Base64Encoder,
23 Decoder: Base64Decoder,
24};
25
26pub const standard_alphabet_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".*;
27fn standardBase64DecoderWithIgnore(ignore: []const u8) Base64DecoderWithIgnore {
28 return Base64DecoderWithIgnore.init(standard_alphabet_chars, '=', ignore);
29}
30
31/// Standard Base64 codecs, with padding
32pub const standard = Codecs{
33 .alphabet_chars = standard_alphabet_chars,
34 .pad_char = '=',
35 .decoderWithIgnore = standardBase64DecoderWithIgnore,
36 .Encoder = Base64Encoder.init(standard_alphabet_chars, '='),
37 .Decoder = Base64Decoder.init(standard_alphabet_chars, '='),
38};
39
40/// Standard Base64 codecs, without padding
41pub const standard_no_pad = Codecs{
42 .alphabet_chars = standard_alphabet_chars,
43 .pad_char = null,
44 .decoderWithIgnore = standardBase64DecoderWithIgnore,
45 .Encoder = Base64Encoder.init(standard_alphabet_chars, null),
46 .Decoder = Base64Decoder.init(standard_alphabet_chars, null),
47};
48
49pub const url_safe_alphabet_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_".*;
50fn urlSafeBase64DecoderWithIgnore(ignore: []const u8) Base64DecoderWithIgnore {
51 return Base64DecoderWithIgnore.init(url_safe_alphabet_chars, null, ignore);
52}
53
54/// URL-safe Base64 codecs, with padding
55pub const url_safe = Codecs{
56 .alphabet_chars = url_safe_alphabet_chars,
57 .pad_char = '=',
58 .decoderWithIgnore = urlSafeBase64DecoderWithIgnore,
59 .Encoder = Base64Encoder.init(url_safe_alphabet_chars, '='),
60 .Decoder = Base64Decoder.init(url_safe_alphabet_chars, '='),
61};
62
63/// URL-safe Base64 codecs, without padding
64pub const url_safe_no_pad = Codecs{
65 .alphabet_chars = url_safe_alphabet_chars,
66 .pad_char = null,
67 .decoderWithIgnore = urlSafeBase64DecoderWithIgnore,
68 .Encoder = Base64Encoder.init(url_safe_alphabet_chars, null),
69 .Decoder = Base64Decoder.init(url_safe_alphabet_chars, null),
70};
71
72// Backwards compatibility
73
74/// Deprecated - Use `standard.pad_char`
75pub const standard_pad_char = standard.pad_char;
76/// Deprecated - Use `standard.Encoder`
77pub const standard_encoder = standard.Encoder;
78/// Deprecated - Use `standard.Decoder`
79pub const standard_decoder = standard.Decoder;
1480
15pub const Base64Encoder = struct {81pub const Base64Encoder = struct {
16 alphabet_chars: []const u8,82 alphabet_chars: [64]u8,
17 pad_char: u8,83 pad_char: ?u8,
1884
19 /// a bunch of assertions, then simply pass the data right through.85 /// A bunch of assertions, then simply pass the data right through.
20 pub fn init(alphabet_chars: []const u8, pad_char: u8) Base64Encoder {86 pub fn init(alphabet_chars: [64]u8, pad_char: ?u8) Base64Encoder {
21 assert(alphabet_chars.len == 64);87 assert(alphabet_chars.len == 64);
22 var char_in_alphabet = [_]bool{false} ** 256;88 var char_in_alphabet = [_]bool{false} ** 256;
23 for (alphabet_chars) |c| {89 for (alphabet_chars) |c| {
24 assert(!char_in_alphabet[c]);90 assert(!char_in_alphabet[c]);
25 assert(c != pad_char);91 assert(pad_char == null or c != pad_char.?);
26 char_in_alphabet[c] = true;92 char_in_alphabet[c] = true;
27 }93 }
28
29 return Base64Encoder{94 return Base64Encoder{
30 .alphabet_chars = alphabet_chars,95 .alphabet_chars = alphabet_chars,
31 .pad_char = pad_char,96 .pad_char = pad_char,
32 };97 };
33 }98 }
3499
35 /// ceil(source_len * 4/3)100 /// Compute the encoded length
36 pub fn calcSize(source_len: usize) usize {101 pub fn calcSize(encoder: *const Base64Encoder, source_len: usize) usize {
37 return @divTrunc(source_len + 2, 3) * 4;102 if (encoder.pad_char != null) {
103 return @divTrunc(source_len + 2, 3) * 4;
104 } else {
105 const leftover = source_len % 3;
106 return @divTrunc(source_len, 3) * 4 + @divTrunc(leftover * 4 + 2, 3);
107 }
38 }108 }
39109
40 /// dest.len must be what you get from ::calcSize.110 /// dest.len must at least be what you get from ::calcSize.
41 pub fn encode(encoder: *const Base64Encoder, dest: []u8, source: []const u8) []const u8 {111 pub fn encode(encoder: *const Base64Encoder, dest: []u8, source: []const u8) []const u8 {
42 assert(dest.len >= Base64Encoder.calcSize(source.len));112 const out_len = encoder.calcSize(source.len);
43113 assert(dest.len >= out_len);
44 var i: usize = 0;114
45 var out_index: usize = 0;115 const nibbles = source.len / 3;
46 while (i + 2 < source.len) : (i += 3) {116 const leftover = source.len - 3 * nibbles;
47 dest[out_index] = encoder.alphabet_chars[(source[i] >> 2) & 0x3f];117
48 out_index += 1;118 var acc: u12 = 0;
49119 var acc_len: u4 = 0;
50 dest[out_index] = encoder.alphabet_chars[((source[i] & 0x3) << 4) | ((source[i + 1] & 0xf0) >> 4)];120 var out_idx: usize = 0;
51 out_index += 1;121 for (source) |v| {
52122 acc = (acc << 8) + v;
53 dest[out_index] = encoder.alphabet_chars[((source[i + 1] & 0xf) << 2) | ((source[i + 2] & 0xc0) >> 6)];123 acc_len += 8;
54 out_index += 1;124 while (acc_len >= 6) {
55125 acc_len -= 6;
56 dest[out_index] = encoder.alphabet_chars[source[i + 2] & 0x3f];126 dest[out_idx] = encoder.alphabet_chars[@truncate(u6, (acc >> acc_len))];
57 out_index += 1;127 out_idx += 1;
128 }
58 }129 }
59130 if (acc_len > 0) {
60 if (i < source.len) {131 dest[out_idx] = encoder.alphabet_chars[@truncate(u6, (acc << 6 - acc_len))];
61 dest[out_index] = encoder.alphabet_chars[(source[i] >> 2) & 0x3f];132 out_idx += 1;
62 out_index += 1;133 }
63134 if (encoder.pad_char) |pad_char| {
64 if (i + 1 == source.len) {135 for (dest[out_idx..]) |*pad| {
65 dest[out_index] = encoder.alphabet_chars[(source[i] & 0x3) << 4];136 pad.* = pad_char;
66 out_index += 1;
67
68 dest[out_index] = encoder.pad_char;
69 out_index += 1;
70 } else {
71 dest[out_index] = encoder.alphabet_chars[((source[i] & 0x3) << 4) | ((source[i + 1] & 0xf0) >> 4)];
72 out_index += 1;
73
74 dest[out_index] = encoder.alphabet_chars[(source[i + 1] & 0xf) << 2];
75 out_index += 1;
76 }137 }
77
78 dest[out_index] = encoder.pad_char;
79 out_index += 1;
80 }138 }
81 return dest[0..out_index];139 return dest[0..out_len];
82 }140 }
83};141};
84142
85pub const standard_decoder = Base64Decoder.init(standard_alphabet_chars, standard_pad_char);
86
87pub const Base64Decoder = struct {143pub const Base64Decoder = struct {
144 const invalid_char: u8 = 0xff;
145
88 /// e.g. 'A' => 0.146 /// e.g. 'A' => 0.
89 /// undefined for any value not in the 64 alphabet chars.147 /// `invalid_char` for any value not in the 64 alphabet chars.
90 char_to_index: [256]u8,148 char_to_index: [256]u8,
149 pad_char: ?u8,
91150
92 /// true only for the 64 chars in the alphabet, not the pad char.151 pub fn init(alphabet_chars: [64]u8, pad_char: ?u8) Base64Decoder {
93 char_in_alphabet: [256]bool,
94 pad_char: u8,
95
96 pub fn init(alphabet_chars: []const u8, pad_char: u8) Base64Decoder {
97 assert(alphabet_chars.len == 64);
98
99 var result = Base64Decoder{152 var result = Base64Decoder{
100 .char_to_index = undefined,153 .char_to_index = [_]u8{invalid_char} ** 256,
101 .char_in_alphabet = [_]bool{false} ** 256,
102 .pad_char = pad_char,154 .pad_char = pad_char,
103 };155 };
104156
157 var char_in_alphabet = [_]bool{false} ** 256;
105 for (alphabet_chars) |c, i| {158 for (alphabet_chars) |c, i| {
106 assert(!result.char_in_alphabet[c]);159 assert(!char_in_alphabet[c]);
107 assert(c != pad_char);160 assert(pad_char == null or c != pad_char.?);
108161
109 result.char_to_index[c] = @intCast(u8, i);162 result.char_to_index[c] = @intCast(u8, i);
110 result.char_in_alphabet[c] = true;163 char_in_alphabet[c] = true;
111 }164 }
165 return result;
166 }
112167
168 /// Return the maximum possible decoded size for a given input length - The actual length may be less if the input includes padding.
169 /// `InvalidPadding` is returned if the input length is not valid.
170 pub fn calcSizeUpperBound(decoder: *const Base64Decoder, source_len: usize) Error!usize {
171 var result = source_len / 4 * 3;
172 const leftover = source_len % 4;
173 if (decoder.pad_char != null) {
174 if (leftover % 4 != 0) return error.InvalidPadding;
175 } else {
176 if (leftover % 4 == 1) return error.InvalidPadding;
177 result += leftover * 3 / 4;
178 }
113 return result;179 return result;
114 }180 }
115181
116 /// If the encoded buffer is detected to be invalid, returns error.InvalidPadding.182 /// Return the exact decoded size for a slice.
117 pub fn calcSize(decoder: *const Base64Decoder, source: []const u8) !usize {183 /// `InvalidPadding` is returned if the input length is not valid.
118 if (source.len % 4 != 0) return error.InvalidPadding;184 pub fn calcSizeForSlice(decoder: *const Base64Decoder, source: []const u8) Error!usize {
119 return calcDecodedSizeExactUnsafe(source, decoder.pad_char);185 const source_len = source.len;
186 var result = try decoder.calcSizeUpperBound(source_len);
187 if (decoder.pad_char) |pad_char| {
188 if (source_len >= 1 and source[source_len - 1] == pad_char) result -= 1;
189 if (source_len >= 2 and source[source_len - 2] == pad_char) result -= 1;
190 }
191 return result;
120 }192 }
121193
122 /// dest.len must be what you get from ::calcSize.194 /// dest.len must be what you get from ::calcSize.
123 /// invalid characters result in error.InvalidCharacter.195 /// invalid characters result in error.InvalidCharacter.
124 /// invalid padding results in error.InvalidPadding.196 /// invalid padding results in error.InvalidPadding.
125 pub fn decode(decoder: *const Base64Decoder, dest: []u8, source: []const u8) !void {197 pub fn decode(decoder: *const Base64Decoder, dest: []u8, source: []const u8) Error!void {
126 assert(dest.len == (decoder.calcSize(source) catch unreachable));198 if (decoder.pad_char != null and source.len % 4 != 0) return error.InvalidPadding;
127 assert(source.len % 4 == 0);199 var acc: u12 = 0;
128200 var acc_len: u4 = 0;
129 var src_cursor: usize = 0;201 var dest_idx: usize = 0;
130 var dest_cursor: usize = 0;202 var leftover_idx: ?usize = null;
131203 for (source) |c, src_idx| {
132 while (src_cursor < source.len) : (src_cursor += 4) {204 const d = decoder.char_to_index[c];
133 if (!decoder.char_in_alphabet[source[src_cursor + 0]]) return error.InvalidCharacter;205 if (d == invalid_char) {
134 if (!decoder.char_in_alphabet[source[src_cursor + 1]]) return error.InvalidCharacter;206 if (decoder.pad_char == null or c != decoder.pad_char.?) return error.InvalidCharacter;
135 if (src_cursor < source.len - 4 or source[src_cursor + 3] != decoder.pad_char) {207 leftover_idx = src_idx;
136 // common case208 break;
137 if (!decoder.char_in_alphabet[source[src_cursor + 2]]) return error.InvalidCharacter;209 }
138 if (!decoder.char_in_alphabet[source[src_cursor + 3]]) return error.InvalidCharacter;210 acc = (acc << 6) + d;
139 dest[dest_cursor + 0] = decoder.char_to_index[source[src_cursor + 0]] << 2 | decoder.char_to_index[source[src_cursor + 1]] >> 4;211 acc_len += 6;
140 dest[dest_cursor + 1] = decoder.char_to_index[source[src_cursor + 1]] << 4 | decoder.char_to_index[source[src_cursor + 2]] >> 2;212 if (acc_len >= 8) {
141 dest[dest_cursor + 2] = decoder.char_to_index[source[src_cursor + 2]] << 6 | decoder.char_to_index[source[src_cursor + 3]];213 acc_len -= 8;
142 dest_cursor += 3;214 dest[dest_idx] = @truncate(u8, acc >> acc_len);
143 } else if (source[src_cursor + 2] != decoder.pad_char) {215 dest_idx += 1;
144 // one pad char
145 if (!decoder.char_in_alphabet[source[src_cursor + 2]]) return error.InvalidCharacter;
146 dest[dest_cursor + 0] = decoder.char_to_index[source[src_cursor + 0]] << 2 | decoder.char_to_index[source[src_cursor + 1]] >> 4;
147 dest[dest_cursor + 1] = decoder.char_to_index[source[src_cursor + 1]] << 4 | decoder.char_to_index[source[src_cursor + 2]] >> 2;
148 if (decoder.char_to_index[source[src_cursor + 2]] << 6 != 0) return error.InvalidPadding;
149 dest_cursor += 2;
150 } else {
151 // two pad chars
152 dest[dest_cursor + 0] = decoder.char_to_index[source[src_cursor + 0]] << 2 | decoder.char_to_index[source[src_cursor + 1]] >> 4;
153 if (decoder.char_to_index[source[src_cursor + 1]] << 4 != 0) return error.InvalidPadding;
154 dest_cursor += 1;
155 }216 }
156 }217 }
157218 if (acc_len > 4 or (acc & (@as(u12, 1) << acc_len) - 1) != 0) {
158 assert(src_cursor == source.len);219 return error.InvalidPadding;
159 assert(dest_cursor == dest.len);220 }
221 if (leftover_idx == null) return;
222 var leftover = source[leftover_idx.?..];
223 if (decoder.pad_char) |pad_char| {
224 const padding_len = acc_len / 2;
225 var padding_chars: usize = 0;
226 var i: usize = 0;
227 for (leftover) |c| {
228 if (c != pad_char) {
229 return if (c == Base64Decoder.invalid_char) error.InvalidCharacter else error.InvalidPadding;
230 }
231 padding_chars += 1;
232 }
233 if (padding_chars != padding_len) return error.InvalidPadding;
234 }
160 }235 }
161};236};
162237
163pub const Base64DecoderWithIgnore = struct {238pub const Base64DecoderWithIgnore = struct {
164 decoder: Base64Decoder,239 decoder: Base64Decoder,
165 char_is_ignored: [256]bool,240 char_is_ignored: [256]bool,
166 pub fn init(alphabet_chars: []const u8, pad_char: u8, ignore_chars: []const u8) Base64DecoderWithIgnore {241
242 pub fn init(alphabet_chars: [64]u8, pad_char: ?u8, ignore_chars: []const u8) Base64DecoderWithIgnore {
167 var result = Base64DecoderWithIgnore{243 var result = Base64DecoderWithIgnore{
168 .decoder = Base64Decoder.init(alphabet_chars, pad_char),244 .decoder = Base64Decoder.init(alphabet_chars, pad_char),
169 .char_is_ignored = [_]bool{false} ** 256,245 .char_is_ignored = [_]bool{false} ** 256,
170 };246 };
171
172 for (ignore_chars) |c| {247 for (ignore_chars) |c| {
173 assert(!result.decoder.char_in_alphabet[c]);248 assert(result.decoder.char_to_index[c] == Base64Decoder.invalid_char);
174 assert(!result.char_is_ignored[c]);249 assert(!result.char_is_ignored[c]);
175 assert(result.decoder.pad_char != c);250 assert(result.decoder.pad_char != c);
176 result.char_is_ignored[c] = true;251 result.char_is_ignored[c] = true;
177 }252 }
178
179 return result;253 return result;
180 }254 }
181255
182 /// If no characters end up being ignored or padding, this will be the exact decoded size.256 /// Return the maximum possible decoded size for a given input length - The actual length may be less if the input includes padding
183 pub fn calcSizeUpperBound(encoded_len: usize) usize {257 /// `InvalidPadding` is returned if the input length is not valid.
184 return @divTrunc(encoded_len, 4) * 3;258 pub fn calcSizeUpperBound(decoder_with_ignore: *const Base64DecoderWithIgnore, source_len: usize) Error!usize {
259 var result = source_len / 4 * 3;
260 if (decoder_with_ignore.decoder.pad_char == null) {
261 const leftover = source_len % 4;
262 result += leftover * 3 / 4;
263 }
264 return result;
185 }265 }
186266
187 /// Invalid characters that are not ignored result in error.InvalidCharacter.267 /// Invalid characters that are not ignored result in error.InvalidCharacter.
188 /// Invalid padding results in error.InvalidPadding.268 /// Invalid padding results in error.InvalidPadding.
189 /// Decoding more data than can fit in dest results in error.OutputTooSmall. See also ::calcSizeUpperBound.269 /// Decoding more data than can fit in dest results in error.NoSpaceLeft. See also ::calcSizeUpperBound.
190 /// Returns the number of bytes written to dest.270 /// Returns the number of bytes written to dest.
191 pub fn decode(decoder_with_ignore: *const Base64DecoderWithIgnore, dest: []u8, source: []const u8) !usize {271 pub fn decode(decoder_with_ignore: *const Base64DecoderWithIgnore, dest: []u8, source: []const u8) Error!usize {
192 const decoder = &decoder_with_ignore.decoder;272 const decoder = &decoder_with_ignore.decoder;
193273 var acc: u12 = 0;
194 var src_cursor: usize = 0;274 var acc_len: u4 = 0;
195 var dest_cursor: usize = 0;275 var dest_idx: usize = 0;
196276 var leftover_idx: ?usize = null;
197 while (true) {277 for (source) |c, src_idx| {
198 // get the next 4 chars, if available278 if (decoder_with_ignore.char_is_ignored[c]) continue;
199 var next_4_chars: [4]u8 = undefined;279 const d = decoder.char_to_index[c];
200 var available_chars: usize = 0;280 if (d == Base64Decoder.invalid_char) {
201 var pad_char_count: usize = 0;281 if (decoder.pad_char == null or c != decoder.pad_char.?) return error.InvalidCharacter;
202 while (available_chars < 4 and src_cursor < source.len) {282 leftover_idx = src_idx;
203 var c = source[src_cursor];283 break;
204 src_cursor += 1;
205
206 if (decoder.char_in_alphabet[c]) {
207 // normal char
208 next_4_chars[available_chars] = c;
209 available_chars += 1;
210 } else if (decoder_with_ignore.char_is_ignored[c]) {
211 // we're told to skip this one
212 continue;
213 } else if (c == decoder.pad_char) {
214 // the padding has begun. count the pad chars.
215 pad_char_count += 1;
216 while (src_cursor < source.len) {
217 c = source[src_cursor];
218 src_cursor += 1;
219 if (c == decoder.pad_char) {
220 pad_char_count += 1;
221 if (pad_char_count > 2) return error.InvalidCharacter;
222 } else if (decoder_with_ignore.char_is_ignored[c]) {
223 // we can even ignore chars during the padding
224 continue;
225 } else return error.InvalidCharacter;
226 }
227 break;
228 } else return error.InvalidCharacter;
229 }284 }
230285 acc = (acc << 6) + d;
231 switch (available_chars) {286 acc_len += 6;
232 4 => {287 if (acc_len >= 8) {
233 // common case288 if (dest_idx == dest.len) return error.NoSpaceLeft;
234 if (dest_cursor + 3 > dest.len) return error.OutputTooSmall;289 acc_len -= 8;
235 assert(pad_char_count == 0);290 dest[dest_idx] = @truncate(u8, acc >> acc_len);
236 dest[dest_cursor + 0] = decoder.char_to_index[next_4_chars[0]] << 2 | decoder.char_to_index[next_4_chars[1]] >> 4;291 dest_idx += 1;
237 dest[dest_cursor + 1] = decoder.char_to_index[next_4_chars[1]] << 4 | decoder.char_to_index[next_4_chars[2]] >> 2;
238 dest[dest_cursor + 2] = decoder.char_to_index[next_4_chars[2]] << 6 | decoder.char_to_index[next_4_chars[3]];
239 dest_cursor += 3;
240 continue;
241 },
242 3 => {
243 if (dest_cursor + 2 > dest.len) return error.OutputTooSmall;
244 if (pad_char_count != 1) return error.InvalidPadding;
245 dest[dest_cursor + 0] = decoder.char_to_index[next_4_chars[0]] << 2 | decoder.char_to_index[next_4_chars[1]] >> 4;
246 dest[dest_cursor + 1] = decoder.char_to_index[next_4_chars[1]] << 4 | decoder.char_to_index[next_4_chars[2]] >> 2;
247 if (decoder.char_to_index[next_4_chars[2]] << 6 != 0) return error.InvalidPadding;
248 dest_cursor += 2;
249 break;
250 },
251 2 => {
252 if (dest_cursor + 1 > dest.len) return error.OutputTooSmall;
253 if (pad_char_count != 2) return error.InvalidPadding;
254 dest[dest_cursor + 0] = decoder.char_to_index[next_4_chars[0]] << 2 | decoder.char_to_index[next_4_chars[1]] >> 4;
255 if (decoder.char_to_index[next_4_chars[1]] << 4 != 0) return error.InvalidPadding;
256 dest_cursor += 1;
257 break;
258 },
259 1 => {
260 return error.InvalidPadding;
261 },
262 0 => {
263 if (pad_char_count != 0) return error.InvalidPadding;
264 break;
265 },
266 else => unreachable,
267 }292 }
268 }293 }
269294 if (acc_len > 4 or (acc & (@as(u12, 1) << acc_len) - 1) != 0) {
270 assert(src_cursor == source.len);295 return error.InvalidPadding;
271
272 return dest_cursor;
273 }
274};
275
276pub const standard_decoder_unsafe = Base64DecoderUnsafe.init(standard_alphabet_chars, standard_pad_char);
277
278pub const Base64DecoderUnsafe = struct {
279 /// e.g. 'A' => 0.
280 /// undefined for any value not in the 64 alphabet chars.
281 char_to_index: [256]u8,
282 pad_char: u8,
283
284 pub fn init(alphabet_chars: []const u8, pad_char: u8) Base64DecoderUnsafe {
285 assert(alphabet_chars.len == 64);
286 var result = Base64DecoderUnsafe{
287 .char_to_index = undefined,
288 .pad_char = pad_char,
289 };
290 for (alphabet_chars) |c, i| {
291 assert(c != pad_char);
292 result.char_to_index[c] = @intCast(u8, i);
293 }296 }
294 return result;297 const padding_len = acc_len / 2;
295 }298 if (leftover_idx == null) {
296299 if (decoder.pad_char != null and padding_len != 0) return error.InvalidPadding;
297 /// The source buffer must be valid.300 return dest_idx;
298 pub fn calcSize(decoder: *const Base64DecoderUnsafe, source: []const u8) usize {
299 return calcDecodedSizeExactUnsafe(source, decoder.pad_char);
300 }
301
302 /// dest.len must be what you get from ::calcDecodedSizeExactUnsafe.
303 /// invalid characters or padding will result in undefined values.
304 pub fn decode(decoder: *const Base64DecoderUnsafe, dest: []u8, source: []const u8) void {
305 assert(dest.len == decoder.calcSize(source));
306
307 var src_index: usize = 0;
308 var dest_index: usize = 0;
309 var in_buf_len: usize = source.len;
310
311 while (in_buf_len > 0 and source[in_buf_len - 1] == decoder.pad_char) {
312 in_buf_len -= 1;
313 }301 }
314302 var leftover = source[leftover_idx.?..];
315 while (in_buf_len > 4) {303 if (decoder.pad_char) |pad_char| {
316 dest[dest_index] = decoder.char_to_index[source[src_index + 0]] << 2 | decoder.char_to_index[source[src_index + 1]] >> 4;304 var padding_chars: usize = 0;
317 dest_index += 1;305 var i: usize = 0;
318306 for (leftover) |c| {
319 dest[dest_index] = decoder.char_to_index[source[src_index + 1]] << 4 | decoder.char_to_index[source[src_index + 2]] >> 2;307 if (decoder_with_ignore.char_is_ignored[c]) continue;
320 dest_index += 1;308 if (c != pad_char) {
321309 return if (c == Base64Decoder.invalid_char) error.InvalidCharacter else error.InvalidPadding;
322 dest[dest_index] = decoder.char_to_index[source[src_index + 2]] << 6 | decoder.char_to_index[source[src_index + 3]];310 }
323 dest_index += 1;311 padding_chars += 1;
324312 }
325 src_index += 4;313 if (padding_chars != padding_len) return error.InvalidPadding;
326 in_buf_len -= 4;
327 }
328
329 if (in_buf_len > 1) {
330 dest[dest_index] = decoder.char_to_index[source[src_index + 0]] << 2 | decoder.char_to_index[source[src_index + 1]] >> 4;
331 dest_index += 1;
332 }
333 if (in_buf_len > 2) {
334 dest[dest_index] = decoder.char_to_index[source[src_index + 1]] << 4 | decoder.char_to_index[source[src_index + 2]] >> 2;
335 dest_index += 1;
336 }
337 if (in_buf_len > 3) {
338 dest[dest_index] = decoder.char_to_index[source[src_index + 2]] << 6 | decoder.char_to_index[source[src_index + 3]];
339 dest_index += 1;
340 }314 }
315 return dest_idx;
341 }316 }
342};317};
343318
344fn calcDecodedSizeExactUnsafe(source: []const u8, pad_char: u8) usize {
345 if (source.len == 0) return 0;
346 var result = @divExact(source.len, 4) * 3;
347 if (source[source.len - 1] == pad_char) {
348 result -= 1;
349 if (source[source.len - 2] == pad_char) {
350 result -= 1;
351 }
352 }
353 return result;
354}
355
356test "base64" {319test "base64" {
357 @setEvalBranchQuota(8000);320 @setEvalBranchQuota(8000);
358 testBase64() catch unreachable;321 testBase64() catch unreachable;
359 comptime (testBase64() catch unreachable);322 comptime testAllApis(standard, "comptime", "Y29tcHRpbWU=") catch unreachable;
323}
324
325test "base64 url_safe_no_pad" {
326 @setEvalBranchQuota(8000);
327 testBase64UrlSafeNoPad() catch unreachable;
328 comptime testAllApis(url_safe_no_pad, "comptime", "Y29tcHRpbWU") catch unreachable;
360}329}
361330
362fn testBase64() !void {331fn testBase64() !void {
363 try testAllApis("", "");332 const codecs = standard;
364 try testAllApis("f", "Zg==");333
365 try testAllApis("fo", "Zm8=");334 try testAllApis(codecs, "", "");
366 try testAllApis("foo", "Zm9v");335 try testAllApis(codecs, "f", "Zg==");
367 try testAllApis("foob", "Zm9vYg==");336 try testAllApis(codecs, "fo", "Zm8=");
368 try testAllApis("fooba", "Zm9vYmE=");337 try testAllApis(codecs, "foo", "Zm9v");
369 try testAllApis("foobar", "Zm9vYmFy");338 try testAllApis(codecs, "foob", "Zm9vYg==");
370339 try testAllApis(codecs, "fooba", "Zm9vYmE=");
371 try testDecodeIgnoreSpace("", " ");340 try testAllApis(codecs, "foobar", "Zm9vYmFy");
372 try testDecodeIgnoreSpace("f", "Z g= =");341
373 try testDecodeIgnoreSpace("fo", " Zm8=");342 try testDecodeIgnoreSpace(codecs, "", " ");
374 try testDecodeIgnoreSpace("foo", "Zm9v ");343 try testDecodeIgnoreSpace(codecs, "f", "Z g= =");
375 try testDecodeIgnoreSpace("foob", "Zm9vYg = = ");344 try testDecodeIgnoreSpace(codecs, "fo", " Zm8=");
376 try testDecodeIgnoreSpace("fooba", "Zm9v YmE=");345 try testDecodeIgnoreSpace(codecs, "foo", "Zm9v ");
377 try testDecodeIgnoreSpace("foobar", " Z m 9 v Y m F y ");346 try testDecodeIgnoreSpace(codecs, "foob", "Zm9vYg = = ");
347 try testDecodeIgnoreSpace(codecs, "fooba", "Zm9v YmE=");
348 try testDecodeIgnoreSpace(codecs, "foobar", " Z m 9 v Y m F y ");
349
350 // test getting some api errors
351 try testError(codecs, "A", error.InvalidPadding);
352 try testError(codecs, "AA", error.InvalidPadding);
353 try testError(codecs, "AAA", error.InvalidPadding);
354 try testError(codecs, "A..A", error.InvalidCharacter);
355 try testError(codecs, "AA=A", error.InvalidPadding);
356 try testError(codecs, "AA/=", error.InvalidPadding);
357 try testError(codecs, "A/==", error.InvalidPadding);
358 try testError(codecs, "A===", error.InvalidPadding);
359 try testError(codecs, "====", error.InvalidPadding);
360
361 try testNoSpaceLeftError(codecs, "AA==");
362 try testNoSpaceLeftError(codecs, "AAA=");
363 try testNoSpaceLeftError(codecs, "AAAA");
364 try testNoSpaceLeftError(codecs, "AAAAAA==");
365}
366
367fn testBase64UrlSafeNoPad() !void {
368 const codecs = url_safe_no_pad;
369
370 try testAllApis(codecs, "", "");
371 try testAllApis(codecs, "f", "Zg");
372 try testAllApis(codecs, "fo", "Zm8");
373 try testAllApis(codecs, "foo", "Zm9v");
374 try testAllApis(codecs, "foob", "Zm9vYg");
375 try testAllApis(codecs, "fooba", "Zm9vYmE");
376 try testAllApis(codecs, "foobar", "Zm9vYmFy");
377
378 try testDecodeIgnoreSpace(codecs, "", " ");
379 try testDecodeIgnoreSpace(codecs, "f", "Z g ");
380 try testDecodeIgnoreSpace(codecs, "fo", " Zm8");
381 try testDecodeIgnoreSpace(codecs, "foo", "Zm9v ");
382 try testDecodeIgnoreSpace(codecs, "foob", "Zm9vYg ");
383 try testDecodeIgnoreSpace(codecs, "fooba", "Zm9v YmE");
384 try testDecodeIgnoreSpace(codecs, "foobar", " Z m 9 v Y m F y ");
378385
379 // test getting some api errors386 // test getting some api errors
380 try testError("A", error.InvalidPadding);387 try testError(codecs, "A", error.InvalidPadding);
381 try testError("AA", error.InvalidPadding);388 try testError(codecs, "AAA=", error.InvalidCharacter);
382 try testError("AAA", error.InvalidPadding);389 try testError(codecs, "A..A", error.InvalidCharacter);
383 try testError("A..A", error.InvalidCharacter);390 try testError(codecs, "AA=A", error.InvalidCharacter);
384 try testError("AA=A", error.InvalidCharacter);391 try testError(codecs, "AA/=", error.InvalidCharacter);
385 try testError("AA/=", error.InvalidPadding);392 try testError(codecs, "A/==", error.InvalidCharacter);
386 try testError("A/==", error.InvalidPadding);393 try testError(codecs, "A===", error.InvalidCharacter);
387 try testError("A===", error.InvalidCharacter);394 try testError(codecs, "====", error.InvalidCharacter);
388 try testError("====", error.InvalidCharacter);395
389396 try testNoSpaceLeftError(codecs, "AA");
390 try testOutputTooSmallError("AA==");397 try testNoSpaceLeftError(codecs, "AAA");
391 try testOutputTooSmallError("AAA=");398 try testNoSpaceLeftError(codecs, "AAAA");
392 try testOutputTooSmallError("AAAA");399 try testNoSpaceLeftError(codecs, "AAAAAA");
393 try testOutputTooSmallError("AAAAAA==");
394}400}
395401
396fn testAllApis(expected_decoded: []const u8, expected_encoded: []const u8) !void {402fn testAllApis(codecs: Codecs, expected_decoded: []const u8, expected_encoded: []const u8) !void {
397 // Base64Encoder403 // Base64Encoder
398 {404 {
399 var buffer: [0x100]u8 = undefined;405 var buffer: [0x100]u8 = undefined;
400 const encoded = standard_encoder.encode(&buffer, expected_decoded);406 const encoded = codecs.Encoder.encode(&buffer, expected_decoded);
401 testing.expectEqualSlices(u8, expected_encoded, encoded);407 testing.expectEqualSlices(u8, expected_encoded, encoded);
402 }408 }
403409
404 // Base64Decoder410 // Base64Decoder
405 {411 {
406 var buffer: [0x100]u8 = undefined;412 var buffer: [0x100]u8 = undefined;
407 var decoded = buffer[0..try standard_decoder.calcSize(expected_encoded)];413 var decoded = buffer[0..try codecs.Decoder.calcSizeForSlice(expected_encoded)];
408 try standard_decoder.decode(decoded, expected_encoded);414 try codecs.Decoder.decode(decoded, expected_encoded);
409 testing.expectEqualSlices(u8, expected_decoded, decoded);415 testing.expectEqualSlices(u8, expected_decoded, decoded);
410 }416 }
411417
412 // Base64DecoderWithIgnore418 // Base64DecoderWithIgnore
413 {419 {
414 const standard_decoder_ignore_nothing = Base64DecoderWithIgnore.init(standard_alphabet_chars, standard_pad_char, "");420 const decoder_ignore_nothing = codecs.decoderWithIgnore("");
415 var buffer: [0x100]u8 = undefined;421 var buffer: [0x100]u8 = undefined;
416 var decoded = buffer[0..Base64DecoderWithIgnore.calcSizeUpperBound(expected_encoded.len)];422 var decoded = buffer[0..try decoder_ignore_nothing.calcSizeUpperBound(expected_encoded.len)];
417 var written = try standard_decoder_ignore_nothing.decode(decoded, expected_encoded);423 var written = try decoder_ignore_nothing.decode(decoded, expected_encoded);
418 testing.expect(written <= decoded.len);424 testing.expect(written <= decoded.len);
419 testing.expectEqualSlices(u8, expected_decoded, decoded[0..written]);425 testing.expectEqualSlices(u8, expected_decoded, decoded[0..written]);
420 }426 }
421
422 // Base64DecoderUnsafe
423 {
424 var buffer: [0x100]u8 = undefined;
425 var decoded = buffer[0..standard_decoder_unsafe.calcSize(expected_encoded)];
426 standard_decoder_unsafe.decode(decoded, expected_encoded);
427 testing.expectEqualSlices(u8, expected_decoded, decoded);
428 }
429}427}
430428
431fn testDecodeIgnoreSpace(expected_decoded: []const u8, encoded: []const u8) !void {429fn testDecodeIgnoreSpace(codecs: Codecs, expected_decoded: []const u8, encoded: []const u8) !void {
432 const standard_decoder_ignore_space = Base64DecoderWithIgnore.init(standard_alphabet_chars, standard_pad_char, " ");430 const decoder_ignore_space = codecs.decoderWithIgnore(" ");
433 var buffer: [0x100]u8 = undefined;431 var buffer: [0x100]u8 = undefined;
434 var decoded = buffer[0..Base64DecoderWithIgnore.calcSizeUpperBound(encoded.len)];432 var decoded = buffer[0..try decoder_ignore_space.calcSizeUpperBound(encoded.len)];
435 var written = try standard_decoder_ignore_space.decode(decoded, encoded);433 var written = try decoder_ignore_space.decode(decoded, encoded);
436 testing.expectEqualSlices(u8, expected_decoded, decoded[0..written]);434 testing.expectEqualSlices(u8, expected_decoded, decoded[0..written]);
437}435}
438436
439fn testError(encoded: []const u8, expected_err: anyerror) !void {437fn testError(codecs: Codecs, encoded: []const u8, expected_err: anyerror) !void {
440 const standard_decoder_ignore_space = Base64DecoderWithIgnore.init(standard_alphabet_chars, standard_pad_char, " ");438 const decoder_ignore_space = codecs.decoderWithIgnore(" ");
441 var buffer: [0x100]u8 = undefined;439 var buffer: [0x100]u8 = undefined;
442 if (standard_decoder.calcSize(encoded)) |decoded_size| {440 if (codecs.Decoder.calcSizeForSlice(encoded)) |decoded_size| {
443 var decoded = buffer[0..decoded_size];441 var decoded = buffer[0..decoded_size];
444 if (standard_decoder.decode(decoded, encoded)) |_| {442 if (codecs.Decoder.decode(decoded, encoded)) |_| {
445 return error.ExpectedError;443 return error.ExpectedError;
446 } else |err| if (err != expected_err) return err;444 } else |err| if (err != expected_err) return err;
447 } else |err| if (err != expected_err) return err;445 } else |err| if (err != expected_err) return err;
448446
449 if (standard_decoder_ignore_space.decode(buffer[0..], encoded)) |_| {447 if (decoder_ignore_space.decode(buffer[0..], encoded)) |_| {
450 return error.ExpectedError;448 return error.ExpectedError;
451 } else |err| if (err != expected_err) return err;449 } else |err| if (err != expected_err) return err;
452}450}
453451
454fn testOutputTooSmallError(encoded: []const u8) !void {452fn testNoSpaceLeftError(codecs: Codecs, encoded: []const u8) !void {
455 const standard_decoder_ignore_space = Base64DecoderWithIgnore.init(standard_alphabet_chars, standard_pad_char, " ");453 const decoder_ignore_space = codecs.decoderWithIgnore(" ");
456 var buffer: [0x100]u8 = undefined;454 var buffer: [0x100]u8 = undefined;
457 var decoded = buffer[0 .. calcDecodedSizeExactUnsafe(encoded, standard_pad_char) - 1];455 var decoded = buffer[0 .. (try codecs.Decoder.calcSizeForSlice(encoded)) - 1];
458 if (standard_decoder_ignore_space.decode(decoded, encoded)) |_| {456 if (decoder_ignore_space.decode(decoded, encoded)) |_| {
459 return error.ExpectedError;457 return error.ExpectedError;
460 } else |err| if (err != error.OutputTooSmall) return err;458 } else |err| if (err != error.NoSpaceLeft) return err;
461}459}
lib/std/fs.zig+5-5
...@@ -50,13 +50,13 @@ pub const MAX_PATH_BYTES = switch (builtin.os.tag) {...@@ -50,13 +50,13 @@ pub const MAX_PATH_BYTES = switch (builtin.os.tag) {
50 else => @compileError("Unsupported OS"),50 else => @compileError("Unsupported OS"),
51};51};
5252
53pub const base64_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";53pub const base64_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_".*;
5454
55/// Base64 encoder, replacing the standard `+/` with `-_` so that it can be used in a file name on any filesystem.55/// Base64 encoder, replacing the standard `+/` with `-_` so that it can be used in a file name on any filesystem.
56pub const base64_encoder = base64.Base64Encoder.init(base64_alphabet, base64.standard_pad_char);56pub const base64_encoder = base64.Base64Encoder.init(base64_alphabet, null);
5757
58/// Base64 decoder, replacing the standard `+/` with `-_` so that it can be used in a file name on any filesystem.58/// Base64 decoder, replacing the standard `+/` with `-_` so that it can be used in a file name on any filesystem.
59pub const base64_decoder = base64.Base64Decoder.init(base64_alphabet, base64.standard_pad_char);59pub const base64_decoder = base64.Base64Decoder.init(base64_alphabet, null);
6060
61/// Whether or not async file system syscalls need a dedicated thread because the operating61/// Whether or not async file system syscalls need a dedicated thread because the operating
62/// system does not support non-blocking I/O on the file system.62/// system does not support non-blocking I/O on the file system.
...@@ -77,7 +77,7 @@ pub fn atomicSymLink(allocator: *Allocator, existing_path: []const u8, new_path:...@@ -77,7 +77,7 @@ pub fn atomicSymLink(allocator: *Allocator, existing_path: []const u8, new_path:
77 const dirname = path.dirname(new_path) orelse ".";77 const dirname = path.dirname(new_path) orelse ".";
7878
79 var rand_buf: [AtomicFile.RANDOM_BYTES]u8 = undefined;79 var rand_buf: [AtomicFile.RANDOM_BYTES]u8 = undefined;
80 const tmp_path = try allocator.alloc(u8, dirname.len + 1 + base64.Base64Encoder.calcSize(rand_buf.len));80 const tmp_path = try allocator.alloc(u8, dirname.len + 1 + base64_encoder.calcSize(rand_buf.len));
81 defer allocator.free(tmp_path);81 defer allocator.free(tmp_path);
82 mem.copy(u8, tmp_path[0..], dirname);82 mem.copy(u8, tmp_path[0..], dirname);
83 tmp_path[dirname.len] = path.sep;83 tmp_path[dirname.len] = path.sep;
...@@ -142,7 +142,7 @@ pub const AtomicFile = struct {...@@ -142,7 +142,7 @@ pub const AtomicFile = struct {
142 const InitError = File.OpenError;142 const InitError = File.OpenError;
143143
144 const RANDOM_BYTES = 12;144 const RANDOM_BYTES = 12;
145 const TMP_PATH_LEN = base64.Base64Encoder.calcSize(RANDOM_BYTES);145 const TMP_PATH_LEN = base64_encoder.calcSize(RANDOM_BYTES);
146146
147 /// Note that the `Dir.atomicFile` API may be more handy than this lower-level function.147 /// Note that the `Dir.atomicFile` API may be more handy than this lower-level function.
148 pub fn init(148 pub fn init(
lib/std/testing.zig+1-1
...@@ -298,7 +298,7 @@ pub const TmpDir = struct {...@@ -298,7 +298,7 @@ pub const TmpDir = struct {
298 sub_path: [sub_path_len]u8,298 sub_path: [sub_path_len]u8,
299299
300 const random_bytes_count = 12;300 const random_bytes_count = 12;
301 const sub_path_len = std.base64.Base64Encoder.calcSize(random_bytes_count);301 const sub_path_len = std.fs.base64_encoder.calcSize(random_bytes_count);
302302
303 pub fn cleanup(self: *TmpDir) void {303 pub fn cleanup(self: *TmpDir) void {
304 self.dir.close();304 self.dir.close();
test/standalone/mix_o_files/base64.zig+3-3
...@@ -3,9 +3,9 @@ const base64 = @import("std").base64;...@@ -3,9 +3,9 @@ const base64 = @import("std").base64;
3export fn decode_base_64(dest_ptr: [*]u8, dest_len: usize, source_ptr: [*]const u8, source_len: usize) usize {3export fn decode_base_64(dest_ptr: [*]u8, dest_len: usize, source_ptr: [*]const u8, source_len: usize) usize {
4 const src = source_ptr[0..source_len];4 const src = source_ptr[0..source_len];
5 const dest = dest_ptr[0..dest_len];5 const dest = dest_ptr[0..dest_len];
6 const base64_decoder = base64.standard_decoder_unsafe;6 const base64_decoder = base64.standard.Decoder;
7 const decoded_size = base64_decoder.calcSize(src);7 const decoded_size = base64_decoder.calcSizeForSlice(src) catch unreachable;
8 base64_decoder.decode(dest[0..decoded_size], src);8 base64_decoder.decode(dest[0..decoded_size], src) catch unreachable;
9 return decoded_size;9 return decoded_size;
10}10}
1111