Fix emulator build error
[platform/framework/web/chromium-efl.git] / base / base64.cc
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.
4
5 #include "base/base64.h"
6
7 #include <stddef.h>
8
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"
13
14 namespace base {
15
16 namespace {
17
18 ModpDecodePolicy GetModpPolicy(Base64DecodePolicy policy) {
19   switch (policy) {
20     case Base64DecodePolicy::kStrict:
21       return ModpDecodePolicy::kStrict;
22     case Base64DecodePolicy::kForgiving:
23       return ModpDecodePolicy::kForgiving;
24   }
25 }
26
27 }  // namespace
28
29 std::string Base64Encode(span<const uint8_t> input) {
30   std::string output;
31   Base64EncodeAppend(input, &output);
32   return output;
33 }
34
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());
39
40   size_t prefix_len = output->size();
41   output->resize(base::CheckAdd(encode_data_len, prefix_len).ValueOrDie());
42
43   const size_t output_size = modp_b64_encode_data(
44       output->data() + prefix_len, reinterpret_cast<const char*>(input.data()),
45       input.size());
46   CHECK_EQ(output->size(), prefix_len + output_size);
47 }
48
49 void Base64Encode(StringPiece input, std::string* output) {
50   *output = Base64Encode(base::as_bytes(base::make_span(input)));
51 }
52
53 std::string Base64Encode(StringPiece input) {
54   return Base64Encode(base::as_bytes(base::make_span(input)));;
55 }
56
57 bool Base64Decode(StringPiece input,
58                   std::string* output,
59                   Base64DecodePolicy policy) {
60   std::string temp;
61   temp.resize(modp_b64_decode_len(input.size()));
62
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));
67
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
78     // on success.
79     std::string input_without_whitespace;
80     RemoveChars(input, kInfraAsciiWhitespace, &input_without_whitespace);
81     output_size =
82         modp_b64_decode(&(temp[0]), input_without_whitespace.data(),
83                         input_without_whitespace.size(), GetModpPolicy(policy));
84   }
85
86   if (output_size == MODP_B64_ERROR)
87     return false;
88
89   temp.resize(output_size);
90   output->swap(temp);
91   return true;
92 }
93
94 absl::optional<std::vector<uint8_t>> Base64Decode(StringPiece input) {
95   std::vector<uint8_t> ret(modp_b64_decode_len(input.size()));
96
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;
102
103   ret.resize(output_size);
104   return ret;
105 }
106
107 }  // namespace base