[M85 Dev][EFL] Fix crashes at webview launch
[platform/framework/web/chromium-efl.git] / base / base64.cc
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
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 "base/base64.h"
6
7 #include <stddef.h>
8
9 #include "third_party/modp_b64/modp_b64.h"
10
11 namespace base {
12
13 std::string Base64Encode(span<const uint8_t> input) {
14   std::string output;
15   output.resize(modp_b64_encode_len(input.size()));  // makes room for null byte
16
17   // modp_b64_encode_len() returns at least 1, so output[0] is safe to use.
18   const size_t output_size = modp_b64_encode(
19       &(output[0]), reinterpret_cast<const char*>(input.data()), input.size());
20
21   output.resize(output_size);
22   return output;
23 }
24
25 void Base64Encode(const StringPiece& input, std::string* output) {
26   *output = Base64Encode(base::as_bytes(base::make_span(input)));
27 }
28
29 bool Base64Decode(const StringPiece& input, std::string* output) {
30   std::string temp;
31   temp.resize(modp_b64_decode_len(input.size()));
32
33   // does not null terminate result since result is binary data!
34   size_t input_size = input.size();
35   size_t output_size = modp_b64_decode(&(temp[0]), input.data(), input_size);
36   if (output_size == MODP_B64_ERROR)
37     return false;
38
39   temp.resize(output_size);
40   output->swap(temp);
41   return true;
42 }
43
44 }  // namespace base