| 194 | } |
| 195 | |
| 196 | bool Base64DecodeBufLen(const char* in, int64_t in_len, int64_t* out_max) { |
| 197 | // Base64 decoding turns every 4 characters into 3 bytes. If the last character of the |
| 198 | // encoded string is '=', that character (which represents 6 bits) and the last two bits |
| 199 | // of the previous character is ignored, for a total of 8 ignored bits, therefore |
| 200 | // producing one fewer byte of output. This is repeated if the second-to-last character |
| 201 | // is '='. One more byte must be allocated to account for Base64Decode's null-padding |
| 202 | // of its output. |
| 203 | if (UNLIKELY(in_len == 0)) { |
| 204 | *out_max = 0; |
| 205 | return true; |
| 206 | } |
| 207 | if (UNLIKELY((in_len & 3) != 0)) return false; |
| 208 | *out_max = 1 + 3 * (in_len / 4); |
| 209 | DCHECK_GE(in_len, 1); |
| 210 | if (in[in_len - 1] == '=') { |
| 211 | --(*out_max); |
| 212 | DCHECK_GE(in_len, 2); |
| 213 | if (in[in_len - 2] == '=') { |
| 214 | --(*out_max); |
| 215 | } |
| 216 | } |
| 217 | return true; |
| 218 | } |
| 219 | |
| 220 | bool Base64Decode(const char* in, int64_t in_len, int64_t out_max, char* out, |
| 221 | unsigned* out_len) { |
no outgoing calls