1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/base64.h"
9 #include "base/check.h"
10 #include "base/numerics/checked_math.h"
11 #include "base/strings/string_util.h"
12 #include "third_party/modp_b64/modp_b64.h"
18 ModpDecodePolicy GetModpPolicy(Base64DecodePolicy policy) {
20 case Base64DecodePolicy::kStrict:
21 return ModpDecodePolicy::kStrict;
22 case Base64DecodePolicy::kForgiving:
23 return ModpDecodePolicy::kForgiving;
29 std::string Base64Encode(span<const uint8_t> input) {
31 Base64EncodeAppend(input, &output);
35 void Base64EncodeAppend(span<const uint8_t> input, std::string* output) {
36 // Ensure `modp_b64_encode_data_len` will not overflow.
37 CHECK_LE(input.size(), MODP_B64_MAX_INPUT_LEN);
38 size_t encode_data_len = modp_b64_encode_data_len(input.size());
40 size_t prefix_len = output->size();
41 output->resize(base::CheckAdd(encode_data_len, prefix_len).ValueOrDie());
43 const size_t output_size = modp_b64_encode_data(
44 output->data() + prefix_len, reinterpret_cast<const char*>(input.data()),
46 CHECK_EQ(output->size(), prefix_len + output_size);
49 void Base64Encode(StringPiece input, std::string* output) {
50 *output = Base64Encode(base::as_bytes(base::make_span(input)));
53 std::string Base64Encode(StringPiece input) {
54 return Base64Encode(base::as_bytes(base::make_span(input)));;
57 bool Base64Decode(StringPiece input,
59 Base64DecodePolicy policy) {
61 temp.resize(modp_b64_decode_len(input.size()));
63 // does not null terminate result since result is binary data!
64 size_t input_size = input.size();
65 size_t output_size = modp_b64_decode(&(temp[0]), input.data(), input_size,
66 GetModpPolicy(policy));
68 // Forgiving mode requires whitespace to be stripped prior to decoding.
69 // We don't do that in the above code to ensure that the "happy path" of
70 // input without whitespace is as fast as possible. Since whitespace in input
71 // will always cause `modp_b64_decode` to fail, just handle whitespace
72 // stripping on failure. This is not much slower than just scanning for
73 // whitespace first, even for input with whitespace.
74 if (output_size == MODP_B64_ERROR &&
75 policy == Base64DecodePolicy::kForgiving) {
76 // We could use `output` here to avoid an allocation when decoding is done
77 // in-place, but it violates the API contract that `output` is only modified
79 std::string input_without_whitespace;
80 RemoveChars(input, kInfraAsciiWhitespace, &input_without_whitespace);
82 modp_b64_decode(&(temp[0]), input_without_whitespace.data(),
83 input_without_whitespace.size(), GetModpPolicy(policy));
86 if (output_size == MODP_B64_ERROR)
89 temp.resize(output_size);
94 absl::optional<std::vector<uint8_t>> Base64Decode(StringPiece input) {
95 std::vector<uint8_t> ret(modp_b64_decode_len(input.size()));
97 size_t input_size = input.size();
98 size_t output_size = modp_b64_decode(reinterpret_cast<char*>(ret.data()),
99 input.data(), input_size);
100 if (output_size == MODP_B64_ERROR)
101 return absl::nullopt;
103 ret.resize(output_size);