/src/brpc/src/butil/base64.cc
Line | Count | Source |
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 "butil/base64.h" |
6 | | |
7 | | #include "third_party/modp_b64/modp_b64.h" |
8 | | |
9 | | namespace butil { |
10 | | |
11 | 173 | void Base64Encode(const StringPiece& input, std::string* output) { |
12 | 173 | std::string temp; |
13 | 173 | temp.resize(modp_b64_encode_len(input.size())); // makes room for null byte |
14 | | |
15 | | // modp_b64_encode_len() returns at least 1, so temp[0] is safe to use. |
16 | 173 | size_t output_size = modp_b64_encode(&(temp[0]), input.data(), input.size()); |
17 | | |
18 | 173 | temp.resize(output_size); // strips off null byte |
19 | 173 | output->swap(temp); |
20 | 173 | } |
21 | | |
22 | 941 | bool Base64Decode(const StringPiece& input, std::string* output) { |
23 | 941 | std::string temp; |
24 | 941 | temp.resize(modp_b64_decode_len(input.size())); |
25 | | |
26 | | // does not null terminate result since result is binary data! |
27 | 941 | size_t input_size = input.size(); |
28 | 941 | size_t output_size = modp_b64_decode(&(temp[0]), input.data(), input_size); |
29 | 941 | if (output_size == MODP_B64_ERROR) |
30 | 156 | return false; |
31 | | |
32 | 785 | temp.resize(output_size); |
33 | 785 | output->swap(temp); |
34 | 785 | return true; |
35 | 941 | } |
36 | | |
37 | | } // namespace butil |