hayato@chromium.org | 268ac3a | 2009-11-24 15:21:53 +0900 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 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 "third_party/modp_b64/modp_b64.h" |
| 8 | |
| 9 | namespace base { |
| 10 | |
joth@chromium.org | 42544c8 | 2011-07-22 22:22:23 +0900 | [diff] [blame] | 11 | bool Base64Encode(const StringPiece& input, std::string* output) { |
hayato@chromium.org | 268ac3a | 2009-11-24 15:21:53 +0900 | [diff] [blame] | 12 | std::string temp; |
| 13 | temp.resize(modp_b64_encode_len(input.size())); // makes room for null byte |
| 14 | |
| 15 | // null terminates result since result is base64 text! |
| 16 | int input_size = static_cast<int>(input.size()); |
| 17 | int output_size= modp_b64_encode(&(temp[0]), input.data(), input_size); |
| 18 | if (output_size < 0) |
| 19 | return false; |
| 20 | |
| 21 | temp.resize(output_size); // strips off null byte |
| 22 | output->swap(temp); |
| 23 | return true; |
| 24 | } |
| 25 | |
joth@chromium.org | 42544c8 | 2011-07-22 22:22:23 +0900 | [diff] [blame] | 26 | bool Base64Decode(const StringPiece& input, std::string* output) { |
hayato@chromium.org | 268ac3a | 2009-11-24 15:21:53 +0900 | [diff] [blame] | 27 | std::string temp; |
| 28 | temp.resize(modp_b64_decode_len(input.size())); |
| 29 | |
| 30 | // does not null terminate result since result is binary data! |
| 31 | int input_size = static_cast<int>(input.size()); |
| 32 | int output_size = modp_b64_decode(&(temp[0]), input.data(), input_size); |
| 33 | if (output_size < 0) |
| 34 | return false; |
| 35 | |
| 36 | temp.resize(output_size); |
| 37 | output->swap(temp); |
| 38 | return true; |
| 39 | } |
| 40 | |
| 41 | } // namespace base |