| 1 | //! Base64 encoding/decoding as specified by |
| 2 | //! [RFC 4648](https://datatracker.ietf.org/doc/html/rfc4648). |
| 3 | |
| 4 | const std = @import("std.zig"); |
| 5 | const assert = std.debug.assert; |
| 6 | const builtin = @import("builtin"); |
| 7 | const testing = std.testing; |
| 8 | const mem = std.mem; |
| 9 | const window = mem.window; |
| 10 | |
| 11 | pub const Error = error{ |
| 12 | InvalidCharacter, |
| 13 | InvalidPadding, |
| 14 | NoSpaceLeft, |
| 15 | }; |
| 16 | |
| 17 | const decoderWithIgnoreProto = *const fn (ignore: []const u8) Base64DecoderWithIgnore; |
| 18 | |
| 19 | /// Base64 codecs |
| 20 | pub const Codecs = struct { |
| 21 | alphabet_chars: [64]u8, |
| 22 | pad_char: ?u8, |
| 23 | decoderWithIgnore: decoderWithIgnoreProto, |
| 24 | Encoder: Base64Encoder, |
| 25 | Decoder: Base64Decoder, |
| 26 | }; |
| 27 | |
| 28 | /// The Base64 alphabet defined in |
| 29 | /// [RFC 4648 section 4](https://datatracker.ietf.org/doc/html/rfc4648#section-4). |
| 30 | pub const standard_alphabet_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".*; |
| 31 | fn standardBase64DecoderWithIgnore(ignore: []const u8) Base64DecoderWithIgnore { |
| 32 | return Base64DecoderWithIgnore.init(standard_alphabet_chars, '=', ignore); |
| 33 | } |
| 34 | |
| 35 | /// Standard Base64 codecs, with padding, as defined in |
| 36 | /// [RFC 4648 section 4](https://datatracker.ietf.org/doc/html/rfc4648#section-4). |
| 37 | pub const standard = Codecs{ |
| 38 | .alphabet_chars = standard_alphabet_chars, |
| 39 | .pad_char = '=', |
| 40 | .decoderWithIgnore = standardBase64DecoderWithIgnore, |
| 41 | .Encoder = Base64Encoder.init(standard_alphabet_chars, '='), |
| 42 | .Decoder = Base64Decoder.init(standard_alphabet_chars, '='), |
| 43 | }; |
| 44 | |
| 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). |
| 47 | pub const standard_no_pad = Codecs{ |
| 48 | .alphabet_chars = standard_alphabet_chars, |
| 49 | .pad_char = null, |
| 50 | .decoderWithIgnore = standardBase64DecoderWithIgnore, |
| 51 | .Encoder = Base64Encoder.init(standard_alphabet_chars, null), |
| 52 | .Decoder = Base64Decoder.init(standard_alphabet_chars, null), |
| 53 | }; |
| 54 | |
| 55 | /// The URL-safe Base64 alphabet defined in |
| 56 | /// [RFC 4648 section 5](https://datatracker.ietf.org/doc/html/rfc4648#section-5). |
| 57 | pub const url_safe_alphabet_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_".*; |
| 58 | fn urlSafeBase64DecoderWithIgnore(ignore: []const u8) Base64DecoderWithIgnore { |
| 59 | return Base64DecoderWithIgnore.init(url_safe_alphabet_chars, null, ignore); |
| 60 | } |
| 61 | |
| 62 | /// URL-safe Base64 codecs, with padding, as defined in |
| 63 | /// [RFC 4648 section 5](https://datatracker.ietf.org/doc/html/rfc4648#section-5). |
| 64 | pub const url_safe = Codecs{ |
| 65 | .alphabet_chars = url_safe_alphabet_chars, |
| 66 | .pad_char = '=', |
| 67 | .decoderWithIgnore = urlSafeBase64DecoderWithIgnore, |
| 68 | .Encoder = Base64Encoder.init(url_safe_alphabet_chars, '='), |
| 69 | .Decoder = Base64Decoder.init(url_safe_alphabet_chars, '='), |
| 70 | }; |
| 71 | |
| 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). |
| 74 | pub const url_safe_no_pad = Codecs{ |
| 75 | .alphabet_chars = url_safe_alphabet_chars, |
| 76 | .pad_char = null, |
| 77 | .decoderWithIgnore = urlSafeBase64DecoderWithIgnore, |
| 78 | .Encoder = Base64Encoder.init(url_safe_alphabet_chars, null), |
| 79 | .Decoder = Base64Decoder.init(url_safe_alphabet_chars, null), |
| 80 | }; |
| 81 | |
| 82 | pub const Base64Encoder = struct { |
| 83 | alphabet_chars: [64]u8, |
| 84 | pad_char: ?u8, |
| 85 | |
| 86 | /// A bunch of assertions, then simply pass the data right through. |
| 87 | pub fn init(alphabet_chars: [64]u8, pad_char: ?u8) Base64Encoder { |
| 88 | assert(alphabet_chars.len == 64); |
| 89 | var char_in_alphabet: [256]bool = @splat(false); |
| 90 | for (alphabet_chars) |c| { |
| 91 | assert(!char_in_alphabet[c]); |
| 92 | assert(pad_char == null or c != pad_char.?); |
| 93 | char_in_alphabet[c] = true; |
| 94 | } |
| 95 | return Base64Encoder{ |
| 96 | .alphabet_chars = alphabet_chars, |
| 97 | .pad_char = pad_char, |
| 98 | }; |
| 99 | } |
| 100 | |
| 101 | /// Compute the encoded length |
| 102 | pub fn calcSize(encoder: *const Base64Encoder, source_len: usize) usize { |
| 103 | if (encoder.pad_char != null) { |
| 104 | return @divTrunc(source_len + 2, 3) * 4; |
| 105 | } else { |
| 106 | const leftover = source_len % 3; |
| 107 | return @divTrunc(source_len, 3) * 4 + @divTrunc(leftover * 4 + 2, 3); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | pub fn encodeWriter(encoder: *const Base64Encoder, dest: *std.Io.Writer, source: []const u8) !void { |
| 112 | var chunker = window(u8, source, 3, 3); |
| 113 | while (chunker.next()) |chunk| { |
| 114 | var temp: [5]u8 = undefined; |
| 115 | const s = encoder.encode(&temp, chunk); |
| 116 | try dest.writeAll(s); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /// dest.len must at least be what you get from ::calcSize. |
| 121 | pub fn encode(encoder: *const Base64Encoder, dest: []u8, source: []const u8) []const u8 { |
| 122 | const out_len = encoder.calcSize(source.len); |
| 123 | assert(dest.len >= out_len); |
| 124 | |
| 125 | var idx: usize = 0; |
| 126 | var out_idx: usize = 0; |
| 127 | while (idx + 15 < source.len) : (idx += 12) { |
| 128 | const bits = std.mem.readInt(u128, source[idx..][0..16], .big); |
| 129 | inline for (0..16) |i| { |
| 130 | dest[out_idx + i] = encoder.alphabet_chars[@truncate((bits >> (122 - i * 6)) & 0x3f)]; |
| 131 | } |
| 132 | out_idx += 16; |
| 133 | } |
| 134 | while (idx + 3 < source.len) : (idx += 3) { |
| 135 | const bits = std.mem.readInt(u32, source[idx..][0..4], .big); |
| 136 | dest[out_idx] = encoder.alphabet_chars[(bits >> 26) & 0x3f]; |
| 137 | dest[out_idx + 1] = encoder.alphabet_chars[(bits >> 20) & 0x3f]; |
| 138 | dest[out_idx + 2] = encoder.alphabet_chars[(bits >> 14) & 0x3f]; |
| 139 | dest[out_idx + 3] = encoder.alphabet_chars[(bits >> 8) & 0x3f]; |
| 140 | out_idx += 4; |
| 141 | } |
| 142 | if (idx + 2 < source.len) { |
| 143 | dest[out_idx] = encoder.alphabet_chars[source[idx] >> 2]; |
| 144 | dest[out_idx + 1] = encoder.alphabet_chars[((source[idx] & 0x3) << 4) | (source[idx + 1] >> 4)]; |
| 145 | dest[out_idx + 2] = encoder.alphabet_chars[(source[idx + 1] & 0xf) << 2 | (source[idx + 2] >> 6)]; |
| 146 | dest[out_idx + 3] = encoder.alphabet_chars[source[idx + 2] & 0x3f]; |
| 147 | out_idx += 4; |
| 148 | } else if (idx + 1 < source.len) { |
| 149 | dest[out_idx] = encoder.alphabet_chars[source[idx] >> 2]; |
| 150 | dest[out_idx + 1] = encoder.alphabet_chars[((source[idx] & 0x3) << 4) | (source[idx + 1] >> 4)]; |
| 151 | dest[out_idx + 2] = encoder.alphabet_chars[(source[idx + 1] & 0xf) << 2]; |
| 152 | out_idx += 3; |
| 153 | } else if (idx < source.len) { |
| 154 | dest[out_idx] = encoder.alphabet_chars[source[idx] >> 2]; |
| 155 | dest[out_idx + 1] = encoder.alphabet_chars[(source[idx] & 0x3) << 4]; |
| 156 | out_idx += 2; |
| 157 | } |
| 158 | if (encoder.pad_char) |pad_char| { |
| 159 | for (dest[out_idx..out_len]) |*pad| { |
| 160 | pad.* = pad_char; |
| 161 | } |
| 162 | } |
| 163 | return dest[0..out_len]; |
| 164 | } |
| 165 | }; |
| 166 | |
| 167 | pub const Base64Decoder = struct { |
| 168 | const invalid_char: u8 = 0xff; |
| 169 | const invalid_char_tst: u32 = 0xff000000; |
| 170 | |
| 171 | /// e.g. 'A' => 0. |
| 172 | /// `invalid_char` for any value not in the 64 alphabet chars. |
| 173 | char_to_index: [256]u8, |
| 174 | fast_char_to_index: [4][256]u32, |
| 175 | pad_char: ?u8, |
| 176 | |
| 177 | pub fn init(alphabet_chars: [64]u8, pad_char: ?u8) Base64Decoder { |
| 178 | var result = Base64Decoder{ |
| 179 | .char_to_index = @splat(invalid_char), |
| 180 | .fast_char_to_index = @splat(@splat(invalid_char_tst)), |
| 181 | .pad_char = pad_char, |
| 182 | }; |
| 183 | |
| 184 | var char_in_alphabet: [256]bool = @splat(false); |
| 185 | for (alphabet_chars, 0..) |c, i| { |
| 186 | assert(!char_in_alphabet[c]); |
| 187 | assert(pad_char == null or c != pad_char.?); |
| 188 | |
| 189 | const ci = @as(u32, @intCast(i)); |
| 190 | result.fast_char_to_index[0][c] = ci << 2; |
| 191 | result.fast_char_to_index[1][c] = (ci >> 4) | ((ci & 0x0f) << 12); |
| 192 | result.fast_char_to_index[2][c] = ((ci & 0x3) << 22) | ((ci & 0x3c) << 6); |
| 193 | result.fast_char_to_index[3][c] = ci << 16; |
| 194 | |
| 195 | result.char_to_index[c] = @as(u8, @intCast(i)); |
| 196 | char_in_alphabet[c] = true; |
| 197 | } |
| 198 | return result; |
| 199 | } |
| 200 | |
| 201 | /// Return the maximum possible decoded size for a given input length - The actual length may be less if the input includes padding. |
| 202 | /// `InvalidPadding` is returned if the input length is not valid. |
| 203 | pub fn calcSizeUpperBound(decoder: *const Base64Decoder, source_len: usize) Error!usize { |
| 204 | var result = source_len / 4 * 3; |
| 205 | const leftover = source_len % 4; |
| 206 | if (decoder.pad_char != null) { |
| 207 | if (leftover % 4 != 0) return error.InvalidPadding; |
| 208 | } else { |
| 209 | if (leftover % 4 == 1) return error.InvalidPadding; |
| 210 | result += leftover * 3 / 4; |
| 211 | } |
| 212 | return result; |
| 213 | } |
| 214 | |
| 215 | /// Return the exact decoded size for a slice. |
| 216 | /// `InvalidPadding` is returned if the input length is not valid. |
| 217 | pub fn calcSizeForSlice(decoder: *const Base64Decoder, source: []const u8) Error!usize { |
| 218 | const source_len = source.len; |
| 219 | var result = try decoder.calcSizeUpperBound(source_len); |
| 220 | if (decoder.pad_char) |pad_char| { |
| 221 | if (source_len >= 1 and source[source_len - 1] == pad_char) result -= 1; |
| 222 | if (source_len >= 2 and source[source_len - 2] == pad_char) result -= 1; |
| 223 | } |
| 224 | return result; |
| 225 | } |
| 226 | |
| 227 | /// dest.len must be what you get from ::calcSize. |
| 228 | /// Invalid characters result in `error.InvalidCharacter`. |
| 229 | /// Invalid padding results in `error.InvalidPadding`. |
| 230 | pub fn decode(decoder: *const Base64Decoder, dest: []u8, source: []const u8) Error!void { |
| 231 | if (decoder.pad_char != null and source.len % 4 != 0) return error.InvalidPadding; |
| 232 | var dest_idx: usize = 0; |
| 233 | var fast_src_idx: usize = 0; |
| 234 | var acc: u12 = 0; |
| 235 | var acc_len: u4 = 0; |
| 236 | var leftover_idx: ?usize = null; |
| 237 | while (fast_src_idx + 16 < source.len and dest_idx + 15 < dest.len) : ({ |
| 238 | fast_src_idx += 16; |
| 239 | dest_idx += 12; |
| 240 | }) { |
| 241 | var bits: u128 = 0; |
| 242 | inline for (0..4) |i| { |
| 243 | var new_bits: u128 = decoder.fast_char_to_index[0][source[fast_src_idx + i * 4]]; |
| 244 | new_bits |= decoder.fast_char_to_index[1][source[fast_src_idx + 1 + i * 4]]; |
| 245 | new_bits |= decoder.fast_char_to_index[2][source[fast_src_idx + 2 + i * 4]]; |
| 246 | new_bits |= decoder.fast_char_to_index[3][source[fast_src_idx + 3 + i * 4]]; |
| 247 | if ((new_bits & invalid_char_tst) != 0) return error.InvalidCharacter; |
| 248 | bits |= (new_bits << (24 * i)); |
| 249 | } |
| 250 | std.mem.writeInt(u128, dest[dest_idx..][0..16], bits, .little); |
| 251 | } |
| 252 | while (fast_src_idx + 4 < source.len and dest_idx + 3 < dest.len) : ({ |
| 253 | fast_src_idx += 4; |
| 254 | dest_idx += 3; |
| 255 | }) { |
| 256 | var bits = decoder.fast_char_to_index[0][source[fast_src_idx]]; |
| 257 | bits |= decoder.fast_char_to_index[1][source[fast_src_idx + 1]]; |
| 258 | bits |= decoder.fast_char_to_index[2][source[fast_src_idx + 2]]; |
| 259 | bits |= decoder.fast_char_to_index[3][source[fast_src_idx + 3]]; |
| 260 | if ((bits & invalid_char_tst) != 0) return error.InvalidCharacter; |
| 261 | std.mem.writeInt(u32, dest[dest_idx..][0..4], bits, .little); |
| 262 | } |
| 263 | const remaining = source[fast_src_idx..]; |
| 264 | for (remaining, fast_src_idx..) |c, src_idx| { |
| 265 | const d = decoder.char_to_index[c]; |
| 266 | if (d == invalid_char) { |
| 267 | if (decoder.pad_char == null or c != decoder.pad_char.?) return error.InvalidCharacter; |
| 268 | leftover_idx = src_idx; |
| 269 | break; |
| 270 | } |
| 271 | acc = (acc << 6) + d; |
| 272 | acc_len += 6; |
| 273 | if (acc_len >= 8) { |
| 274 | acc_len -= 8; |
| 275 | dest[dest_idx] = @as(u8, @truncate(acc >> acc_len)); |
| 276 | dest_idx += 1; |
| 277 | } |
| 278 | } |
| 279 | if (acc_len > 4 or (acc & (@as(u12, 1) << acc_len) - 1) != 0) { |
| 280 | return error.InvalidPadding; |
| 281 | } |
| 282 | if (leftover_idx == null) return; |
| 283 | const leftover = source[leftover_idx.?..]; |
| 284 | if (decoder.pad_char) |pad_char| { |
| 285 | const padding_len = acc_len / 2; |
| 286 | var padding_chars: usize = 0; |
| 287 | for (leftover) |c| { |
| 288 | if (c != pad_char) { |
| 289 | return if (c == Base64Decoder.invalid_char) error.InvalidCharacter else error.InvalidPadding; |
| 290 | } |
| 291 | padding_chars += 1; |
| 292 | } |
| 293 | if (padding_chars != padding_len) return error.InvalidPadding; |
| 294 | } |
| 295 | } |
| 296 | }; |
| 297 | |
| 298 | pub const Base64DecoderWithIgnore = struct { |
| 299 | decoder: Base64Decoder, |
| 300 | char_is_ignored: [256]bool, |
| 301 | |
| 302 | pub fn init(alphabet_chars: [64]u8, pad_char: ?u8, ignore_chars: []const u8) Base64DecoderWithIgnore { |
| 303 | var result = Base64DecoderWithIgnore{ |
| 304 | .decoder = Base64Decoder.init(alphabet_chars, pad_char), |
| 305 | .char_is_ignored = @splat(false), |
| 306 | }; |
| 307 | for (ignore_chars) |c| { |
| 308 | assert(result.decoder.char_to_index[c] == Base64Decoder.invalid_char); |
| 309 | assert(!result.char_is_ignored[c]); |
| 310 | assert(result.decoder.pad_char != c); |
| 311 | result.char_is_ignored[c] = true; |
| 312 | } |
| 313 | return result; |
| 314 | } |
| 315 | |
| 316 | /// Return the maximum possible decoded size for a given input length - The actual length may be |
| 317 | /// less if the input includes padding or ignored characters. |
| 318 | pub fn calcSizeUpperBound(decoder_with_ignore: *const Base64DecoderWithIgnore, source_len: usize) usize { |
| 319 | var result = source_len / 4 * 3; |
| 320 | if (decoder_with_ignore.decoder.pad_char == null) { |
| 321 | const leftover = source_len % 4; |
| 322 | result += leftover * 3 / 4; |
| 323 | } |
| 324 | return result; |
| 325 | } |
| 326 | |
| 327 | /// Invalid characters that are not ignored result in error.InvalidCharacter. |
| 328 | /// Invalid padding results in error.InvalidPadding. |
| 329 | /// Decoding more data than can fit in dest results in error.NoSpaceLeft. See also ::calcSizeUpperBound. |
| 330 | /// Returns the number of bytes written to dest. |
| 331 | pub fn decode(decoder_with_ignore: *const Base64DecoderWithIgnore, dest: []u8, source: []const u8) Error!usize { |
| 332 | const decoder = &decoder_with_ignore.decoder; |
| 333 | var acc: u12 = 0; |
| 334 | var acc_len: u4 = 0; |
| 335 | var dest_idx: usize = 0; |
| 336 | var leftover_idx: ?usize = null; |
| 337 | for (source, 0..) |c, src_idx| { |
| 338 | if (decoder_with_ignore.char_is_ignored[c]) continue; |
| 339 | const d = decoder.char_to_index[c]; |
| 340 | if (d == Base64Decoder.invalid_char) { |
| 341 | if (decoder.pad_char == null or c != decoder.pad_char.?) return error.InvalidCharacter; |
| 342 | leftover_idx = src_idx; |
| 343 | break; |
| 344 | } |
| 345 | acc = (acc << 6) + d; |
| 346 | acc_len += 6; |
| 347 | if (acc_len >= 8) { |
| 348 | if (dest_idx == dest.len) return error.NoSpaceLeft; |
| 349 | acc_len -= 8; |
| 350 | dest[dest_idx] = @as(u8, @truncate(acc >> acc_len)); |
| 351 | dest_idx += 1; |
| 352 | } |
| 353 | } |
| 354 | if (acc_len > 4 or (acc & (@as(u12, 1) << acc_len) - 1) != 0) { |
| 355 | return error.InvalidPadding; |
| 356 | } |
| 357 | const padding_len = acc_len / 2; |
| 358 | if (leftover_idx == null) { |
| 359 | if (decoder.pad_char != null and padding_len != 0) return error.InvalidPadding; |
| 360 | return dest_idx; |
| 361 | } |
| 362 | const leftover = source[leftover_idx.?..]; |
| 363 | if (decoder.pad_char) |pad_char| { |
| 364 | var padding_chars: usize = 0; |
| 365 | for (leftover) |c| { |
| 366 | if (decoder_with_ignore.char_is_ignored[c]) continue; |
| 367 | if (c != pad_char) { |
| 368 | return if (c == Base64Decoder.invalid_char) error.InvalidCharacter else error.InvalidPadding; |
| 369 | } |
| 370 | padding_chars += 1; |
| 371 | } |
| 372 | if (padding_chars != padding_len) return error.InvalidPadding; |
| 373 | } |
| 374 | return dest_idx; |
| 375 | } |
| 376 | }; |
| 377 | |
| 378 | test "base64" { |
| 379 | @setEvalBranchQuota(8000); |
| 380 | try testBase64(); |
| 381 | try comptime testAllApis(standard, "comptime", "Y29tcHRpbWU="); |
| 382 | } |
| 383 | |
| 384 | test "base64 padding dest overflow" { |
| 385 | const input = "foo"; |
| 386 | |
| 387 | var expect: [128]u8 = undefined; |
| 388 | @memset(&expect, 0); |
| 389 | _ = url_safe.Encoder.encode(expect[0..url_safe.Encoder.calcSize(input.len)], input); |
| 390 | |
| 391 | var got: [128]u8 = undefined; |
| 392 | @memset(&got, 0); |
| 393 | _ = url_safe.Encoder.encode(&got, input); |
| 394 | |
| 395 | try std.testing.expectEqualSlices(u8, &expect, &got); |
| 396 | } |
| 397 | |
| 398 | test "base64 url_safe_no_pad" { |
| 399 | @setEvalBranchQuota(8000); |
| 400 | try testBase64UrlSafeNoPad(); |
| 401 | try comptime testAllApis(url_safe_no_pad, "comptime", "Y29tcHRpbWU"); |
| 402 | } |
| 403 | |
| 404 | fn testBase64() !void { |
| 405 | const codecs = standard; |
| 406 | |
| 407 | try testAllApis(codecs, "", ""); |
| 408 | try testAllApis(codecs, "f", "Zg=="); |
| 409 | try testAllApis(codecs, "fo", "Zm8="); |
| 410 | try testAllApis(codecs, "foo", "Zm9v"); |
| 411 | try testAllApis(codecs, "foob", "Zm9vYg=="); |
| 412 | try testAllApis(codecs, "fooba", "Zm9vYmE="); |
| 413 | try testAllApis(codecs, "foobar", "Zm9vYmFy"); |
| 414 | try testAllApis(codecs, "foobarfoobarfoo", "Zm9vYmFyZm9vYmFyZm9v"); |
| 415 | try testAllApis(codecs, "foobarfoobarfoob", "Zm9vYmFyZm9vYmFyZm9vYg=="); |
| 416 | try testAllApis(codecs, "foobarfoobarfooba", "Zm9vYmFyZm9vYmFyZm9vYmE="); |
| 417 | try testAllApis(codecs, "foobarfoobarfoobar", "Zm9vYmFyZm9vYmFyZm9vYmFy"); |
| 418 | |
| 419 | try testDecodeIgnoreSpace(codecs, "", " "); |
| 420 | try testDecodeIgnoreSpace(codecs, "f", "Z g= ="); |
| 421 | try testDecodeIgnoreSpace(codecs, "fo", " Zm8="); |
| 422 | try testDecodeIgnoreSpace(codecs, "foo", "Zm9v "); |
| 423 | try testDecodeIgnoreSpace(codecs, "foob", "Zm9vYg = = "); |
| 424 | try testDecodeIgnoreSpace(codecs, "fooba", "Zm9v YmE="); |
| 425 | try testDecodeIgnoreSpace(codecs, "foobar", " Z m 9 v Y m F y "); |
| 426 | |
| 427 | // test getting some api errors |
| 428 | try testError(codecs, "A", error.InvalidPadding); |
| 429 | try testError(codecs, "AA", error.InvalidPadding); |
| 430 | try testError(codecs, "AAA", error.InvalidPadding); |
| 431 | try testError(codecs, "A..A", error.InvalidCharacter); |
| 432 | try testError(codecs, "AA=A", error.InvalidPadding); |
| 433 | try testError(codecs, "AA/=", error.InvalidPadding); |
| 434 | try testError(codecs, "A/==", error.InvalidPadding); |
| 435 | try testError(codecs, "A===", error.InvalidPadding); |
| 436 | try testError(codecs, "====", error.InvalidPadding); |
| 437 | try testError(codecs, "Zm9vYmFyZm9vYmFyA..A", error.InvalidCharacter); |
| 438 | try testError(codecs, "Zm9vYmFyZm9vYmFyAA=A", error.InvalidPadding); |
| 439 | try testError(codecs, "Zm9vYmFyZm9vYmFyAA/=", error.InvalidPadding); |
| 440 | try testError(codecs, "Zm9vYmFyZm9vYmFyA/==", error.InvalidPadding); |
| 441 | try testError(codecs, "Zm9vYmFyZm9vYmFyA===", error.InvalidPadding); |
| 442 | try testError(codecs, "A..AZm9vYmFyZm9vYmFy", error.InvalidCharacter); |
| 443 | try testError(codecs, "Zm9vYmFyZm9vAA=A", error.InvalidPadding); |
| 444 | try testError(codecs, "Zm9vYmFyZm9vAA/=", error.InvalidPadding); |
| 445 | try testError(codecs, "Zm9vYmFyZm9vA/==", error.InvalidPadding); |
| 446 | try testError(codecs, "Zm9vYmFyZm9vA===", error.InvalidPadding); |
| 447 | |
| 448 | try testNoSpaceLeftError(codecs, "AA=="); |
| 449 | try testNoSpaceLeftError(codecs, "AAA="); |
| 450 | try testNoSpaceLeftError(codecs, "AAAA"); |
| 451 | try testNoSpaceLeftError(codecs, "AAAAAA=="); |
| 452 | |
| 453 | try testFourBytesDestNoSpaceLeftError(codecs, "AAAAAAAAAAAAAAAA"); |
| 454 | } |
| 455 | |
| 456 | fn testBase64UrlSafeNoPad() !void { |
| 457 | const codecs = url_safe_no_pad; |
| 458 | |
| 459 | try testAllApis(codecs, "", ""); |
| 460 | try testAllApis(codecs, "f", "Zg"); |
| 461 | try testAllApis(codecs, "fo", "Zm8"); |
| 462 | try testAllApis(codecs, "foo", "Zm9v"); |
| 463 | try testAllApis(codecs, "foob", "Zm9vYg"); |
| 464 | try testAllApis(codecs, "fooba", "Zm9vYmE"); |
| 465 | try testAllApis(codecs, "foobar", "Zm9vYmFy"); |
| 466 | try testAllApis(codecs, "foobarfoobarfoobar", "Zm9vYmFyZm9vYmFyZm9vYmFy"); |
| 467 | |
| 468 | try testDecodeIgnoreSpace(codecs, "", " "); |
| 469 | try testDecodeIgnoreSpace(codecs, "f", "Z g "); |
| 470 | try testDecodeIgnoreSpace(codecs, "fo", " Zm8"); |
| 471 | try testDecodeIgnoreSpace(codecs, "foo", "Zm9v "); |
| 472 | try testDecodeIgnoreSpace(codecs, "foob", "Zm9vYg "); |
| 473 | try testDecodeIgnoreSpace(codecs, "fooba", "Zm9v YmE"); |
| 474 | try testDecodeIgnoreSpace(codecs, "foobar", " Z m 9 v Y m F y "); |
| 475 | |
| 476 | // test getting some api errors |
| 477 | try testError(codecs, "A", error.InvalidPadding); |
| 478 | try testError(codecs, "AAA=", error.InvalidCharacter); |
| 479 | try testError(codecs, "A..A", error.InvalidCharacter); |
| 480 | try testError(codecs, "AA=A", error.InvalidCharacter); |
| 481 | try testError(codecs, "AA/=", error.InvalidCharacter); |
| 482 | try testError(codecs, "A/==", error.InvalidCharacter); |
| 483 | try testError(codecs, "A===", error.InvalidCharacter); |
| 484 | try testError(codecs, "====", error.InvalidCharacter); |
| 485 | try testError(codecs, "Zm9vYmFyZm9vYmFyA..A", error.InvalidCharacter); |
| 486 | try testError(codecs, "A..AZm9vYmFyZm9vYmFy", error.InvalidCharacter); |
| 487 | |
| 488 | try testNoSpaceLeftError(codecs, "AA"); |
| 489 | try testNoSpaceLeftError(codecs, "AAA"); |
| 490 | try testNoSpaceLeftError(codecs, "AAAA"); |
| 491 | try testNoSpaceLeftError(codecs, "AAAAAA"); |
| 492 | |
| 493 | try testFourBytesDestNoSpaceLeftError(codecs, "AAAAAAAAAAAAAAAA"); |
| 494 | } |
| 495 | |
| 496 | fn testAllApis(codecs: Codecs, expected_decoded: []const u8, expected_encoded: []const u8) !void { |
| 497 | // Base64Encoder |
| 498 | { |
| 499 | // raw encode |
| 500 | var buffer: [0x100]u8 = undefined; |
| 501 | const encoded = codecs.Encoder.encode(&buffer, expected_decoded); |
| 502 | try testing.expectEqualSlices(u8, expected_encoded, encoded); |
| 503 | } |
| 504 | { |
| 505 | // stream encode |
| 506 | var buffer: [0x100]u8 = undefined; |
| 507 | var writer: std.Io.Writer = .fixed(&buffer); |
| 508 | try codecs.Encoder.encodeWriter(&writer, expected_decoded); |
| 509 | try testing.expectEqualSlices(u8, expected_encoded, writer.buffered()); |
| 510 | } |
| 511 | |
| 512 | // Base64Decoder |
| 513 | { |
| 514 | var buffer: [0x100]u8 = undefined; |
| 515 | const decoded = buffer[0..try codecs.Decoder.calcSizeForSlice(expected_encoded)]; |
| 516 | try codecs.Decoder.decode(decoded, expected_encoded); |
| 517 | try testing.expectEqualSlices(u8, expected_decoded, decoded); |
| 518 | } |
| 519 | |
| 520 | // Base64DecoderWithIgnore |
| 521 | { |
| 522 | const decoder_ignore_nothing = codecs.decoderWithIgnore(""); |
| 523 | var buffer: [0x100]u8 = undefined; |
| 524 | const decoded = buffer[0..decoder_ignore_nothing.calcSizeUpperBound(expected_encoded.len)]; |
| 525 | const written = try decoder_ignore_nothing.decode(decoded, expected_encoded); |
| 526 | try testing.expect(written <= decoded.len); |
| 527 | try testing.expectEqualSlices(u8, expected_decoded, decoded[0..written]); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | fn testDecodeIgnoreSpace(codecs: Codecs, expected_decoded: []const u8, encoded: []const u8) !void { |
| 532 | const decoder_ignore_space = codecs.decoderWithIgnore(" "); |
| 533 | var buffer: [0x100]u8 = undefined; |
| 534 | const decoded = buffer[0..decoder_ignore_space.calcSizeUpperBound(encoded.len)]; |
| 535 | const written = try decoder_ignore_space.decode(decoded, encoded); |
| 536 | try testing.expectEqualSlices(u8, expected_decoded, decoded[0..written]); |
| 537 | } |
| 538 | |
| 539 | fn testError(codecs: Codecs, encoded: []const u8, expected_err: anyerror) !void { |
| 540 | const decoder_ignore_space = codecs.decoderWithIgnore(" "); |
| 541 | var buffer: [0x100]u8 = undefined; |
| 542 | if (codecs.Decoder.calcSizeForSlice(encoded)) |decoded_size| { |
| 543 | const decoded = buffer[0..decoded_size]; |
| 544 | if (codecs.Decoder.decode(decoded, encoded)) |_| { |
| 545 | return error.ExpectedError; |
| 546 | } else |err| if (err != expected_err) return err; |
| 547 | } else |err| if (err != expected_err) return err; |
| 548 | |
| 549 | if (decoder_ignore_space.decode(buffer[0..], encoded)) |_| { |
| 550 | return error.ExpectedError; |
| 551 | } else |err| if (err != expected_err) return err; |
| 552 | } |
| 553 | |
| 554 | fn testNoSpaceLeftError(codecs: Codecs, encoded: []const u8) !void { |
| 555 | const decoder_ignore_space = codecs.decoderWithIgnore(" "); |
| 556 | var buffer: [0x100]u8 = undefined; |
| 557 | const decoded = buffer[0 .. (try codecs.Decoder.calcSizeForSlice(encoded)) - 1]; |
| 558 | if (decoder_ignore_space.decode(decoded, encoded)) |_| { |
| 559 | return error.ExpectedError; |
| 560 | } else |err| if (err != error.NoSpaceLeft) return err; |
| 561 | } |
| 562 | |
| 563 | fn testFourBytesDestNoSpaceLeftError(codecs: Codecs, encoded: []const u8) !void { |
| 564 | const decoder_ignore_space = codecs.decoderWithIgnore(" "); |
| 565 | var buffer: [0x100]u8 = undefined; |
| 566 | const decoded = buffer[0..4]; |
| 567 | if (decoder_ignore_space.decode(decoded, encoded)) |_| { |
| 568 | return error.ExpectedError; |
| 569 | } else |err| if (err != error.NoSpaceLeft) return err; |
| 570 | } |