blob: ca8ee93907972bdd4476a55462c2ac216dcb9bd2 [file] [log] [blame]
[email protected]c8838bf2012-05-17 18:34:591// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
[email protected]978df342009-11-24 06:21:535#include "base/base64.h"
initial.commit586acc5fe2008-07-26 22:42:526
avi9b6f42932015-12-26 22:15:147#include <stddef.h>
8
initial.commit586acc5fe2008-07-26 22:42:529#include "third_party/modp_b64/modp_b64.h"
initial.commit586acc5fe2008-07-26 22:42:5210
[email protected]978df342009-11-24 06:21:5311namespace base {
[email protected]a9bb6f692008-07-30 16:40:1012
[email protected]33fca122013-12-11 01:48:5013void Base64Encode(const StringPiece& input, std::string* output) {
initial.commit586acc5fe2008-07-26 22:42:5214 std::string temp;
15 temp.resize(modp_b64_encode_len(input.size())); // makes room for null byte
16
[email protected]c8838bf2012-05-17 18:34:5917 // modp_b64_encode_len() returns at least 1, so temp[0] is safe to use.
[email protected]33fca122013-12-11 01:48:5018 size_t output_size = modp_b64_encode(&(temp[0]), input.data(), input.size());
initial.commit586acc5fe2008-07-26 22:42:5219
20 temp.resize(output_size); // strips off null byte
21 output->swap(temp);
initial.commit586acc5fe2008-07-26 22:42:5222}
23
[email protected]1dda9772011-07-22 13:22:2324bool Base64Decode(const StringPiece& input, std::string* output) {
initial.commit586acc5fe2008-07-26 22:42:5225 std::string temp;
26 temp.resize(modp_b64_decode_len(input.size()));
27
28 // does not null terminate result since result is binary data!
[email protected]c5253122012-12-27 22:01:5829 size_t input_size = input.size();
30 size_t output_size = modp_b64_decode(&(temp[0]), input.data(), input_size);
31 if (output_size == MODP_B64_ERROR)
initial.commit586acc5fe2008-07-26 22:42:5232 return false;
33
34 temp.resize(output_size);
35 output->swap(temp);
36 return true;
37}
[email protected]a9bb6f692008-07-30 16:40:1038
[email protected]978df342009-11-24 06:21:5339} // namespace base