Encode method and error message inside LevelDB::Status message
[chromium-blink-merge.git] / base / base64.cc
blob9514b0a5c2ca7d44d5d24b692cb7ab8a004aeeeb
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.
5 #include "base/base64.h"
7 #include "third_party/modp_b64/modp_b64.h"
9 namespace base {
11 bool Base64Encode(const StringPiece& input, std::string* output) {
12 std::string temp;
13 temp.resize(modp_b64_encode_len(input.size())); // makes room for null byte
15 // null terminates result since result is base64 text!
16 int input_size = static_cast<int>(input.size());
18 // modp_b64_encode_len() returns at least 1, so temp[0] is safe to use.
19 size_t output_size = modp_b64_encode(&(temp[0]), input.data(), input_size);
20 if (output_size == MODP_B64_ERROR)
21 return false;
23 temp.resize(output_size); // strips off null byte
24 output->swap(temp);
25 return true;
28 bool Base64Decode(const StringPiece& input, std::string* output) {
29 std::string temp;
30 temp.resize(modp_b64_decode_len(input.size()));
32 // does not null terminate result since result is binary data!
33 size_t input_size = input.size();
34 size_t output_size = modp_b64_decode(&(temp[0]), input.data(), input_size);
35 if (output_size == MODP_B64_ERROR)
36 return false;
38 temp.resize(output_size);
39 output->swap(temp);
40 return true;
43 } // namespace base