1 // Copyright 2023 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.
4
5 #include <openssl/base64.h>
6
7 #include "fillins_base64.h"
8
9 #include <vector>
10
11 namespace bssl {
12
13 namespace fillins {
14
Base64Encode(const std::string_view & input,std::string * output)15 bool Base64Encode(const std::string_view &input, std::string *output) {
16 size_t len;
17 if (!EVP_EncodedLength(&len, input.size())) {
18 return false;
19 }
20 std::vector<char> encoded(len);
21 len = EVP_EncodeBlock(reinterpret_cast<uint8_t *>(encoded.data()),
22 reinterpret_cast<const uint8_t *>(input.data()),
23 input.size());
24 if (!len) {
25 return false;
26 }
27 output->assign(encoded.data(), len);
28 return true;
29 }
30
Base64Decode(const std::string_view & input,std::string * output)31 bool Base64Decode(const std::string_view &input, std::string *output) {
32 size_t len;
33 if (!EVP_DecodedLength(&len, input.size())) {
34 return false;
35 }
36 std::vector<char> decoded(len);
37 if (!EVP_DecodeBase64(reinterpret_cast<uint8_t *>(decoded.data()), &len, len,
38 reinterpret_cast<const uint8_t *>(input.data()),
39 input.size())) {
40 return false;
41 }
42 output->assign(decoded.data(), len);
43 return true;
44 }
45
46 } // namespace fillins
47
48 } // namespace bssl
49