| ... | ... | @@ -8,454 +8,452 @@ const assert = std.debug.assert; |
| 8 | 8 | const testing = std.testing; |
| 9 | 9 | const mem = std.mem; |
| 10 | 10 | |
| 11 | | pub const standard_alphabet_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 12 | | pub const standard_pad_char = '='; |
| 13 | | pub const standard_encoder = Base64Encoder.init(standard_alphabet_chars, standard_pad_char); |
| 11 | pub const Error = error{ |
| 12 | InvalidCharacter, |
| 13 | InvalidPadding, |
| 14 | NoSpaceLeft, |
| 15 | }; |
| 16 | |
| 17 | /// Base64 codecs |
| 18 | pub 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 | |
| 26 | pub const standard_alphabet_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".*; |
| 27 | fn standardBase64DecoderWithIgnore(ignore: []const u8) Base64DecoderWithIgnore { |
| 28 | return Base64DecoderWithIgnore.init(standard_alphabet_chars, '=', ignore); |
| 29 | } |
| 30 | |
| 31 | /// Standard Base64 codecs, with padding |
| 32 | pub 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 |
| 41 | pub 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 | |
| 49 | pub const url_safe_alphabet_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_".*; |
| 50 | fn urlSafeBase64DecoderWithIgnore(ignore: []const u8) Base64DecoderWithIgnore { |
| 51 | return Base64DecoderWithIgnore.init(url_safe_alphabet_chars, null, ignore); |
| 52 | } |
| 53 | |
| 54 | /// URL-safe Base64 codecs, with padding |
| 55 | pub 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 |
| 64 | pub 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` |
| 75 | pub const standard_pad_char = standard.pad_char; |
| 76 | /// Deprecated - Use `standard.Encoder` |
| 77 | pub const standard_encoder = standard.Encoder; |
| 78 | /// Deprecated - Use `standard.Decoder` |
| 79 | pub const standard_decoder = standard.Decoder; |
| 14 | 80 | |
| 15 | 81 | pub const Base64Encoder = struct { |
| 16 | | alphabet_chars: []const u8, |
| 17 | | pad_char: u8, |
| 82 | alphabet_chars: [64]u8, |
| 83 | pad_char: ?u8, |
| 18 | 84 | |
| 19 | | /// a bunch of assertions, then simply pass the data right through. |
| 20 | | pub fn init(alphabet_chars: []const u8, pad_char: u8) Base64Encoder { |
| 85 | /// A bunch of assertions, then simply pass the data right through. |
| 86 | pub fn init(alphabet_chars: [64]u8, pad_char: ?u8) Base64Encoder { |
| 21 | 87 | assert(alphabet_chars.len == 64); |
| 22 | 88 | var char_in_alphabet = [_]bool{false} ** 256; |
| 23 | 89 | for (alphabet_chars) |c| { |
| 24 | 90 | assert(!char_in_alphabet[c]); |
| 25 | | assert(c != pad_char); |
| 91 | assert(pad_char == null or c != pad_char.?); |
| 26 | 92 | char_in_alphabet[c] = true; |
| 27 | 93 | } |
| 28 | | |
| 29 | 94 | return Base64Encoder{ |
| 30 | 95 | .alphabet_chars = alphabet_chars, |
| 31 | 96 | .pad_char = pad_char, |
| 32 | 97 | }; |
| 33 | 98 | } |
| 34 | 99 | |
| 35 | | /// ceil(source_len * 4/3) |
| 36 | | pub fn calcSize(source_len: usize) usize { |
| 37 | | return @divTrunc(source_len + 2, 3) * 4; |
| 100 | /// Compute the encoded length |
| 101 | pub fn calcSize(encoder: *const Base64Encoder, source_len: usize) usize { |
| 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 | } |
| 39 | 109 | |
| 40 | | /// dest.len must be what you get from ::calcSize. |
| 110 | /// dest.len must at least be what you get from ::calcSize. |
| 41 | 111 | pub fn encode(encoder: *const Base64Encoder, dest: []u8, source: []const u8) []const u8 { |
| 42 | | assert(dest.len >= Base64Encoder.calcSize(source.len)); |
| 43 | | |
| 44 | | var i: usize = 0; |
| 45 | | var out_index: usize = 0; |
| 46 | | while (i + 2 < source.len) : (i += 3) { |
| 47 | | dest[out_index] = encoder.alphabet_chars[(source[i] >> 2) & 0x3f]; |
| 48 | | out_index += 1; |
| 49 | | |
| 50 | | dest[out_index] = encoder.alphabet_chars[((source[i] & 0x3) << 4) | ((source[i + 1] & 0xf0) >> 4)]; |
| 51 | | out_index += 1; |
| 52 | | |
| 53 | | dest[out_index] = encoder.alphabet_chars[((source[i + 1] & 0xf) << 2) | ((source[i + 2] & 0xc0) >> 6)]; |
| 54 | | out_index += 1; |
| 55 | | |
| 56 | | dest[out_index] = encoder.alphabet_chars[source[i + 2] & 0x3f]; |
| 57 | | out_index += 1; |
| 112 | const out_len = encoder.calcSize(source.len); |
| 113 | assert(dest.len >= out_len); |
| 114 | |
| 115 | const nibbles = source.len / 3; |
| 116 | const leftover = source.len - 3 * nibbles; |
| 117 | |
| 118 | var acc: u12 = 0; |
| 119 | var acc_len: u4 = 0; |
| 120 | var out_idx: usize = 0; |
| 121 | for (source) |v| { |
| 122 | acc = (acc << 8) + v; |
| 123 | acc_len += 8; |
| 124 | while (acc_len >= 6) { |
| 125 | acc_len -= 6; |
| 126 | dest[out_idx] = encoder.alphabet_chars[@truncate(u6, (acc >> acc_len))]; |
| 127 | out_idx += 1; |
| 128 | } |
| 58 | 129 | } |
| 59 | | |
| 60 | | if (i < source.len) { |
| 61 | | dest[out_index] = encoder.alphabet_chars[(source[i] >> 2) & 0x3f]; |
| 62 | | out_index += 1; |
| 63 | | |
| 64 | | if (i + 1 == source.len) { |
| 65 | | dest[out_index] = encoder.alphabet_chars[(source[i] & 0x3) << 4]; |
| 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; |
| 130 | if (acc_len > 0) { |
| 131 | dest[out_idx] = encoder.alphabet_chars[@truncate(u6, (acc << 6 - acc_len))]; |
| 132 | out_idx += 1; |
| 133 | } |
| 134 | if (encoder.pad_char) |pad_char| { |
| 135 | for (dest[out_idx..]) |*pad| { |
| 136 | pad.* = pad_char; |
| 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 | }; |
| 84 | 142 | |
| 85 | | pub const standard_decoder = Base64Decoder.init(standard_alphabet_chars, standard_pad_char); |
| 86 | | |
| 87 | 143 | pub const Base64Decoder = struct { |
| 144 | const invalid_char: u8 = 0xff; |
| 145 | |
| 88 | 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 | 148 | char_to_index: [256]u8, |
| 149 | pad_char: ?u8, |
| 91 | 150 | |
| 92 | | /// true only for the 64 chars in the alphabet, not the pad char. |
| 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 | | |
| 151 | pub fn init(alphabet_chars: [64]u8, pad_char: ?u8) Base64Decoder { |
| 99 | 152 | var result = Base64Decoder{ |
| 100 | | .char_to_index = undefined, |
| 101 | | .char_in_alphabet = [_]bool{false} ** 256, |
| 153 | .char_to_index = [_]u8{invalid_char} ** 256, |
| 102 | 154 | .pad_char = pad_char, |
| 103 | 155 | }; |
| 104 | 156 | |
| 157 | var char_in_alphabet = [_]bool{false} ** 256; |
| 105 | 158 | for (alphabet_chars) |c, i| { |
| 106 | | assert(!result.char_in_alphabet[c]); |
| 107 | | assert(c != pad_char); |
| 159 | assert(!char_in_alphabet[c]); |
| 160 | assert(pad_char == null or c != pad_char.?); |
| 108 | 161 | |
| 109 | 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 | } |
| 112 | 167 | |
| 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 | 179 | return result; |
| 114 | 180 | } |
| 115 | 181 | |
| 116 | | /// If the encoded buffer is detected to be invalid, returns error.InvalidPadding. |
| 117 | | pub fn calcSize(decoder: *const Base64Decoder, source: []const u8) !usize { |
| 118 | | if (source.len % 4 != 0) return error.InvalidPadding; |
| 119 | | return calcDecodedSizeExactUnsafe(source, decoder.pad_char); |
| 182 | /// Return the exact decoded size for a slice. |
| 183 | /// `InvalidPadding` is returned if the input length is not valid. |
| 184 | pub fn calcSizeForSlice(decoder: *const Base64Decoder, source: []const u8) Error!usize { |
| 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 | } |
| 121 | 193 | |
| 122 | 194 | /// dest.len must be what you get from ::calcSize. |
| 123 | 195 | /// invalid characters result in error.InvalidCharacter. |
| 124 | 196 | /// invalid padding results in error.InvalidPadding. |
| 125 | | pub fn decode(decoder: *const Base64Decoder, dest: []u8, source: []const u8) !void { |
| 126 | | assert(dest.len == (decoder.calcSize(source) catch unreachable)); |
| 127 | | assert(source.len % 4 == 0); |
| 128 | | |
| 129 | | var src_cursor: usize = 0; |
| 130 | | var dest_cursor: usize = 0; |
| 131 | | |
| 132 | | while (src_cursor < source.len) : (src_cursor += 4) { |
| 133 | | if (!decoder.char_in_alphabet[source[src_cursor + 0]]) return error.InvalidCharacter; |
| 134 | | if (!decoder.char_in_alphabet[source[src_cursor + 1]]) return error.InvalidCharacter; |
| 135 | | if (src_cursor < source.len - 4 or source[src_cursor + 3] != decoder.pad_char) { |
| 136 | | // common case |
| 137 | | if (!decoder.char_in_alphabet[source[src_cursor + 2]]) return error.InvalidCharacter; |
| 138 | | if (!decoder.char_in_alphabet[source[src_cursor + 3]]) return error.InvalidCharacter; |
| 139 | | dest[dest_cursor + 0] = decoder.char_to_index[source[src_cursor + 0]] << 2 | decoder.char_to_index[source[src_cursor + 1]] >> 4; |
| 140 | | dest[dest_cursor + 1] = decoder.char_to_index[source[src_cursor + 1]] << 4 | decoder.char_to_index[source[src_cursor + 2]] >> 2; |
| 141 | | dest[dest_cursor + 2] = decoder.char_to_index[source[src_cursor + 2]] << 6 | decoder.char_to_index[source[src_cursor + 3]]; |
| 142 | | dest_cursor += 3; |
| 143 | | } else if (source[src_cursor + 2] != decoder.pad_char) { |
| 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; |
| 197 | pub fn decode(decoder: *const Base64Decoder, dest: []u8, source: []const u8) Error!void { |
| 198 | if (decoder.pad_char != null and source.len % 4 != 0) return error.InvalidPadding; |
| 199 | var acc: u12 = 0; |
| 200 | var acc_len: u4 = 0; |
| 201 | var dest_idx: usize = 0; |
| 202 | var leftover_idx: ?usize = null; |
| 203 | for (source) |c, src_idx| { |
| 204 | const d = decoder.char_to_index[c]; |
| 205 | if (d == invalid_char) { |
| 206 | if (decoder.pad_char == null or c != decoder.pad_char.?) return error.InvalidCharacter; |
| 207 | leftover_idx = src_idx; |
| 208 | break; |
| 209 | } |
| 210 | acc = (acc << 6) + d; |
| 211 | acc_len += 6; |
| 212 | if (acc_len >= 8) { |
| 213 | acc_len -= 8; |
| 214 | dest[dest_idx] = @truncate(u8, acc >> acc_len); |
| 215 | dest_idx += 1; |
| 155 | 216 | } |
| 156 | 217 | } |
| 157 | | |
| 158 | | assert(src_cursor == source.len); |
| 159 | | assert(dest_cursor == dest.len); |
| 218 | if (acc_len > 4 or (acc & (@as(u12, 1) << acc_len) - 1) != 0) { |
| 219 | return error.InvalidPadding; |
| 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 | }; |
| 162 | 237 | |
| 163 | 238 | pub const Base64DecoderWithIgnore = struct { |
| 164 | 239 | decoder: Base64Decoder, |
| 165 | 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 | 243 | var result = Base64DecoderWithIgnore{ |
| 168 | 244 | .decoder = Base64Decoder.init(alphabet_chars, pad_char), |
| 169 | 245 | .char_is_ignored = [_]bool{false} ** 256, |
| 170 | 246 | }; |
| 171 | | |
| 172 | 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 | 249 | assert(!result.char_is_ignored[c]); |
| 175 | 250 | assert(result.decoder.pad_char != c); |
| 176 | 251 | result.char_is_ignored[c] = true; |
| 177 | 252 | } |
| 178 | | |
| 179 | 253 | return result; |
| 180 | 254 | } |
| 181 | 255 | |
| 182 | | /// If no characters end up being ignored or padding, this will be the exact decoded size. |
| 183 | | pub fn calcSizeUpperBound(encoded_len: usize) usize { |
| 184 | | return @divTrunc(encoded_len, 4) * 3; |
| 256 | /// Return the maximum possible decoded size for a given input length - The actual length may be less if the input includes padding |
| 257 | /// `InvalidPadding` is returned if the input length is not valid. |
| 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 | } |
| 186 | 266 | |
| 187 | 267 | /// Invalid characters that are not ignored result in error.InvalidCharacter. |
| 188 | 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 | 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 | 272 | const decoder = &decoder_with_ignore.decoder; |
| 193 | | |
| 194 | | var src_cursor: usize = 0; |
| 195 | | var dest_cursor: usize = 0; |
| 196 | | |
| 197 | | while (true) { |
| 198 | | // get the next 4 chars, if available |
| 199 | | var next_4_chars: [4]u8 = undefined; |
| 200 | | var available_chars: usize = 0; |
| 201 | | var pad_char_count: usize = 0; |
| 202 | | while (available_chars < 4 and src_cursor < source.len) { |
| 203 | | var c = source[src_cursor]; |
| 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; |
| 273 | var acc: u12 = 0; |
| 274 | var acc_len: u4 = 0; |
| 275 | var dest_idx: usize = 0; |
| 276 | var leftover_idx: ?usize = null; |
| 277 | for (source) |c, src_idx| { |
| 278 | if (decoder_with_ignore.char_is_ignored[c]) continue; |
| 279 | const d = decoder.char_to_index[c]; |
| 280 | if (d == Base64Decoder.invalid_char) { |
| 281 | if (decoder.pad_char == null or c != decoder.pad_char.?) return error.InvalidCharacter; |
| 282 | leftover_idx = src_idx; |
| 283 | break; |
| 229 | 284 | } |
| 230 | | |
| 231 | | switch (available_chars) { |
| 232 | | 4 => { |
| 233 | | // common case |
| 234 | | if (dest_cursor + 3 > dest.len) return error.OutputTooSmall; |
| 235 | | assert(pad_char_count == 0); |
| 236 | | dest[dest_cursor + 0] = decoder.char_to_index[next_4_chars[0]] << 2 | decoder.char_to_index[next_4_chars[1]] >> 4; |
| 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, |
| 285 | acc = (acc << 6) + d; |
| 286 | acc_len += 6; |
| 287 | if (acc_len >= 8) { |
| 288 | if (dest_idx == dest.len) return error.NoSpaceLeft; |
| 289 | acc_len -= 8; |
| 290 | dest[dest_idx] = @truncate(u8, acc >> acc_len); |
| 291 | dest_idx += 1; |
| 267 | 292 | } |
| 268 | 293 | } |
| 269 | | |
| 270 | | assert(src_cursor == source.len); |
| 271 | | |
| 272 | | return dest_cursor; |
| 273 | | } |
| 274 | | }; |
| 275 | | |
| 276 | | pub const standard_decoder_unsafe = Base64DecoderUnsafe.init(standard_alphabet_chars, standard_pad_char); |
| 277 | | |
| 278 | | pub 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); |
| 294 | if (acc_len > 4 or (acc & (@as(u12, 1) << acc_len) - 1) != 0) { |
| 295 | return error.InvalidPadding; |
| 293 | 296 | } |
| 294 | | return result; |
| 295 | | } |
| 296 | | |
| 297 | | /// The source buffer must be valid. |
| 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; |
| 297 | const padding_len = acc_len / 2; |
| 298 | if (leftover_idx == null) { |
| 299 | if (decoder.pad_char != null and padding_len != 0) return error.InvalidPadding; |
| 300 | return dest_idx; |
| 313 | 301 | } |
| 314 | | |
| 315 | | while (in_buf_len > 4) { |
| 316 | | dest[dest_index] = decoder.char_to_index[source[src_index + 0]] << 2 | decoder.char_to_index[source[src_index + 1]] >> 4; |
| 317 | | dest_index += 1; |
| 318 | | |
| 319 | | dest[dest_index] = decoder.char_to_index[source[src_index + 1]] << 4 | decoder.char_to_index[source[src_index + 2]] >> 2; |
| 320 | | dest_index += 1; |
| 321 | | |
| 322 | | dest[dest_index] = decoder.char_to_index[source[src_index + 2]] << 6 | decoder.char_to_index[source[src_index + 3]]; |
| 323 | | dest_index += 1; |
| 324 | | |
| 325 | | src_index += 4; |
| 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; |
| 302 | var leftover = source[leftover_idx.?..]; |
| 303 | if (decoder.pad_char) |pad_char| { |
| 304 | var padding_chars: usize = 0; |
| 305 | var i: usize = 0; |
| 306 | for (leftover) |c| { |
| 307 | if (decoder_with_ignore.char_is_ignored[c]) continue; |
| 308 | if (c != pad_char) { |
| 309 | return if (c == Base64Decoder.invalid_char) error.InvalidCharacter else error.InvalidPadding; |
| 310 | } |
| 311 | padding_chars += 1; |
| 312 | } |
| 313 | if (padding_chars != padding_len) return error.InvalidPadding; |
| 340 | 314 | } |
| 315 | return dest_idx; |
| 341 | 316 | } |
| 342 | 317 | }; |
| 343 | 318 | |
| 344 | | fn 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 | | |
| 356 | 319 | test "base64" { |
| 357 | 320 | @setEvalBranchQuota(8000); |
| 358 | 321 | testBase64() catch unreachable; |
| 359 | | comptime (testBase64() catch unreachable); |
| 322 | comptime testAllApis(standard, "comptime", "Y29tcHRpbWU=") catch unreachable; |
| 323 | } |
| 324 | |
| 325 | test "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 | } |
| 361 | 330 | |
| 362 | 331 | fn testBase64() !void { |
| 363 | | try testAllApis("", ""); |
| 364 | | try testAllApis("f", "Zg=="); |
| 365 | | try testAllApis("fo", "Zm8="); |
| 366 | | try testAllApis("foo", "Zm9v"); |
| 367 | | try testAllApis("foob", "Zm9vYg=="); |
| 368 | | try testAllApis("fooba", "Zm9vYmE="); |
| 369 | | try testAllApis("foobar", "Zm9vYmFy"); |
| 370 | | |
| 371 | | try testDecodeIgnoreSpace("", " "); |
| 372 | | try testDecodeIgnoreSpace("f", "Z g= ="); |
| 373 | | try testDecodeIgnoreSpace("fo", " Zm8="); |
| 374 | | try testDecodeIgnoreSpace("foo", "Zm9v "); |
| 375 | | try testDecodeIgnoreSpace("foob", "Zm9vYg = = "); |
| 376 | | try testDecodeIgnoreSpace("fooba", "Zm9v YmE="); |
| 377 | | try testDecodeIgnoreSpace("foobar", " Z m 9 v Y m F y "); |
| 332 | const codecs = standard; |
| 333 | |
| 334 | try testAllApis(codecs, "", ""); |
| 335 | try testAllApis(codecs, "f", "Zg=="); |
| 336 | try testAllApis(codecs, "fo", "Zm8="); |
| 337 | try testAllApis(codecs, "foo", "Zm9v"); |
| 338 | try testAllApis(codecs, "foob", "Zm9vYg=="); |
| 339 | try testAllApis(codecs, "fooba", "Zm9vYmE="); |
| 340 | try testAllApis(codecs, "foobar", "Zm9vYmFy"); |
| 341 | |
| 342 | try testDecodeIgnoreSpace(codecs, "", " "); |
| 343 | try testDecodeIgnoreSpace(codecs, "f", "Z g= ="); |
| 344 | try testDecodeIgnoreSpace(codecs, "fo", " Zm8="); |
| 345 | try testDecodeIgnoreSpace(codecs, "foo", "Zm9v "); |
| 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 | |
| 367 | fn 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 "); |
| 378 | 385 | |
| 379 | 386 | // test getting some api errors |
| 380 | | try testError("A", error.InvalidPadding); |
| 381 | | try testError("AA", error.InvalidPadding); |
| 382 | | try testError("AAA", error.InvalidPadding); |
| 383 | | try testError("A..A", error.InvalidCharacter); |
| 384 | | try testError("AA=A", error.InvalidCharacter); |
| 385 | | try testError("AA/=", error.InvalidPadding); |
| 386 | | try testError("A/==", error.InvalidPadding); |
| 387 | | try testError("A===", error.InvalidCharacter); |
| 388 | | try testError("====", error.InvalidCharacter); |
| 389 | | |
| 390 | | try testOutputTooSmallError("AA=="); |
| 391 | | try testOutputTooSmallError("AAA="); |
| 392 | | try testOutputTooSmallError("AAAA"); |
| 393 | | try testOutputTooSmallError("AAAAAA=="); |
| 387 | try testError(codecs, "A", error.InvalidPadding); |
| 388 | try testError(codecs, "AAA=", error.InvalidCharacter); |
| 389 | try testError(codecs, "A..A", error.InvalidCharacter); |
| 390 | try testError(codecs, "AA=A", error.InvalidCharacter); |
| 391 | try testError(codecs, "AA/=", error.InvalidCharacter); |
| 392 | try testError(codecs, "A/==", error.InvalidCharacter); |
| 393 | try testError(codecs, "A===", error.InvalidCharacter); |
| 394 | try testError(codecs, "====", error.InvalidCharacter); |
| 395 | |
| 396 | try testNoSpaceLeftError(codecs, "AA"); |
| 397 | try testNoSpaceLeftError(codecs, "AAA"); |
| 398 | try testNoSpaceLeftError(codecs, "AAAA"); |
| 399 | try testNoSpaceLeftError(codecs, "AAAAAA"); |
| 394 | 400 | } |
| 395 | 401 | |
| 396 | | fn testAllApis(expected_decoded: []const u8, expected_encoded: []const u8) !void { |
| 402 | fn testAllApis(codecs: Codecs, expected_decoded: []const u8, expected_encoded: []const u8) !void { |
| 397 | 403 | // Base64Encoder |
| 398 | 404 | { |
| 399 | 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 | 407 | testing.expectEqualSlices(u8, expected_encoded, encoded); |
| 402 | 408 | } |
| 403 | 409 | |
| 404 | 410 | // Base64Decoder |
| 405 | 411 | { |
| 406 | 412 | var buffer: [0x100]u8 = undefined; |
| 407 | | var decoded = buffer[0..try standard_decoder.calcSize(expected_encoded)]; |
| 408 | | try standard_decoder.decode(decoded, expected_encoded); |
| 413 | var decoded = buffer[0..try codecs.Decoder.calcSizeForSlice(expected_encoded)]; |
| 414 | try codecs.Decoder.decode(decoded, expected_encoded); |
| 409 | 415 | testing.expectEqualSlices(u8, expected_decoded, decoded); |
| 410 | 416 | } |
| 411 | 417 | |
| 412 | 418 | // 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 | 421 | var buffer: [0x100]u8 = undefined; |
| 416 | | var decoded = buffer[0..Base64DecoderWithIgnore.calcSizeUpperBound(expected_encoded.len)]; |
| 417 | | var written = try standard_decoder_ignore_nothing.decode(decoded, expected_encoded); |
| 422 | var decoded = buffer[0..try decoder_ignore_nothing.calcSizeUpperBound(expected_encoded.len)]; |
| 423 | var written = try decoder_ignore_nothing.decode(decoded, expected_encoded); |
| 418 | 424 | testing.expect(written <= decoded.len); |
| 419 | 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 | } |
| 430 | 428 | |
| 431 | | fn testDecodeIgnoreSpace(expected_decoded: []const u8, encoded: []const u8) !void { |
| 432 | | const standard_decoder_ignore_space = Base64DecoderWithIgnore.init(standard_alphabet_chars, standard_pad_char, " "); |
| 429 | fn testDecodeIgnoreSpace(codecs: Codecs, expected_decoded: []const u8, encoded: []const u8) !void { |
| 430 | const decoder_ignore_space = codecs.decoderWithIgnore(" "); |
| 433 | 431 | var buffer: [0x100]u8 = undefined; |
| 434 | | var decoded = buffer[0..Base64DecoderWithIgnore.calcSizeUpperBound(encoded.len)]; |
| 435 | | var written = try standard_decoder_ignore_space.decode(decoded, encoded); |
| 432 | var decoded = buffer[0..try decoder_ignore_space.calcSizeUpperBound(encoded.len)]; |
| 433 | var written = try decoder_ignore_space.decode(decoded, encoded); |
| 436 | 434 | testing.expectEqualSlices(u8, expected_decoded, decoded[0..written]); |
| 437 | 435 | } |
| 438 | 436 | |
| 439 | | fn testError(encoded: []const u8, expected_err: anyerror) !void { |
| 440 | | const standard_decoder_ignore_space = Base64DecoderWithIgnore.init(standard_alphabet_chars, standard_pad_char, " "); |
| 437 | fn testError(codecs: Codecs, encoded: []const u8, expected_err: anyerror) !void { |
| 438 | const decoder_ignore_space = codecs.decoderWithIgnore(" "); |
| 441 | 439 | var buffer: [0x100]u8 = undefined; |
| 442 | | if (standard_decoder.calcSize(encoded)) |decoded_size| { |
| 440 | if (codecs.Decoder.calcSizeForSlice(encoded)) |decoded_size| { |
| 443 | 441 | var decoded = buffer[0..decoded_size]; |
| 444 | | if (standard_decoder.decode(decoded, encoded)) |_| { |
| 442 | if (codecs.Decoder.decode(decoded, encoded)) |_| { |
| 445 | 443 | return error.ExpectedError; |
| 446 | 444 | } else |err| if (err != expected_err) return err; |
| 447 | 445 | } else |err| if (err != expected_err) return err; |
| 448 | 446 | |
| 449 | | if (standard_decoder_ignore_space.decode(buffer[0..], encoded)) |_| { |
| 447 | if (decoder_ignore_space.decode(buffer[0..], encoded)) |_| { |
| 450 | 448 | return error.ExpectedError; |
| 451 | 449 | } else |err| if (err != expected_err) return err; |
| 452 | 450 | } |
| 453 | 451 | |
| 454 | | fn testOutputTooSmallError(encoded: []const u8) !void { |
| 455 | | const standard_decoder_ignore_space = Base64DecoderWithIgnore.init(standard_alphabet_chars, standard_pad_char, " "); |
| 452 | fn testNoSpaceLeftError(codecs: Codecs, encoded: []const u8) !void { |
| 453 | const decoder_ignore_space = codecs.decoderWithIgnore(" "); |
| 456 | 454 | var buffer: [0x100]u8 = undefined; |
| 457 | | var decoded = buffer[0 .. calcDecodedSizeExactUnsafe(encoded, standard_pad_char) - 1]; |
| 458 | | if (standard_decoder_ignore_space.decode(decoded, encoded)) |_| { |
| 455 | var decoded = buffer[0 .. (try codecs.Decoder.calcSizeForSlice(encoded)) - 1]; |
| 456 | if (decoder_ignore_space.decode(decoded, encoded)) |_| { |
| 459 | 457 | return error.ExpectedError; |
| 460 | | } else |err| if (err != error.OutputTooSmall) return err; |
| 458 | } else |err| if (err != error.NoSpaceLeft) return err; |
| 461 | 459 | } |