| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/base64.h" | 5 #include "base/base64.h" |
| 6 | 6 |
| 7 #include "third_party/modp_b64/modp_b64.h" | 7 #include "third_party/modp_b64/modp_b64.h" |
| 8 | 8 |
| 9 namespace base { | 9 namespace base { |
| 10 | 10 |
| 11 void Base64Encode(const StringPiece& input, std::string* output) { | 11 bool Base64Encode(const StringPiece& input, std::string* output) { |
| 12 std::string temp; | 12 std::string temp; |
| 13 temp.resize(modp_b64_encode_len(input.size())); // makes room for null byte | 13 temp.resize(modp_b64_encode_len(input.size())); // makes room for null byte |
| 14 | 14 |
| 15 // null terminates result since result is base64 text! |
| 16 int input_size = static_cast<int>(input.size()); |
| 17 |
| 15 // modp_b64_encode_len() returns at least 1, so temp[0] is safe to use. | 18 // modp_b64_encode_len() returns at least 1, so temp[0] is safe to use. |
| 16 size_t output_size = modp_b64_encode(&(temp[0]), input.data(), input.size()); | 19 size_t output_size = modp_b64_encode(&(temp[0]), input.data(), input_size); |
| 20 if (output_size == MODP_B64_ERROR) |
| 21 return false; |
| 17 | 22 |
| 18 temp.resize(output_size); // strips off null byte | 23 temp.resize(output_size); // strips off null byte |
| 19 output->swap(temp); | 24 output->swap(temp); |
| 25 return true; |
| 20 } | 26 } |
| 21 | 27 |
| 22 bool Base64Decode(const StringPiece& input, std::string* output) { | 28 bool Base64Decode(const StringPiece& input, std::string* output) { |
| 23 std::string temp; | 29 std::string temp; |
| 24 temp.resize(modp_b64_decode_len(input.size())); | 30 temp.resize(modp_b64_decode_len(input.size())); |
| 25 | 31 |
| 26 // does not null terminate result since result is binary data! | 32 // does not null terminate result since result is binary data! |
| 27 size_t input_size = input.size(); | 33 size_t input_size = input.size(); |
| 28 size_t output_size = modp_b64_decode(&(temp[0]), input.data(), input_size); | 34 size_t output_size = modp_b64_decode(&(temp[0]), input.data(), input_size); |
| 29 if (output_size == MODP_B64_ERROR) | 35 if (output_size == MODP_B64_ERROR) |
| 30 return false; | 36 return false; |
| 31 | 37 |
| 32 temp.resize(output_size); | 38 temp.resize(output_size); |
| 33 output->swap(temp); | 39 output->swap(temp); |
| 34 return true; | 40 return true; |
| 35 } | 41 } |
| 36 | 42 |
| 37 } // namespace base | 43 } // namespace base |
| OLD | NEW |