blob: 8ed1249257d6fd7de3472a5cc3e87a69edbe3e28 [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
initial.commit586acc5fe2008-07-26 22:42:527#include "third_party/modp_b64/modp_b64.h"
initial.commit586acc5fe2008-07-26 22:42:528
[email protected]978df342009-11-24 06:21:539namespace base {
[email protected]a9bb6f692008-07-30 16:40:1010
[email protected]33fca122013-12-11 01:48:5011void Base64Encode(const StringPiece& input, std::string* output) {
initial.commit586acc5fe2008-07-26 22:42:5212 std::string temp;
13 temp.resize(modp_b64_encode_len(input.size())); // makes room for null byte
14
[email protected]c8838bf2012-05-17 18:34:5915 // modp_b64_encode_len() returns at least 1, so temp[0] is safe to use.
[email protected]33fca122013-12-11 01:48:5016 size_t output_size = modp_b64_encode(&(temp[0]), input.data(), input.size());
initial.commit586acc5fe2008-07-26 22:42:5217
18 temp.resize(output_size); // strips off null byte
19 output->swap(temp);
initial.commit586acc5fe2008-07-26 22:42:5220}
21
[email protected]1dda9772011-07-22 13:22:2322bool Base64Decode(const StringPiece& input, std::string* output) {
initial.commit586acc5fe2008-07-26 22:42:5223 std::string temp;
24 temp.resize(modp_b64_decode_len(input.size()));
25
26 // does not null terminate result since result is binary data!
[email protected]c5253122012-12-27 22:01:5827 size_t input_size = input.size();
28 size_t output_size = modp_b64_decode(&(temp[0]), input.data(), input_size);
29 if (output_size == MODP_B64_ERROR)
initial.commit586acc5fe2008-07-26 22:42:5230 return false;
31
32 temp.resize(output_size);
33 output->swap(temp);
34 return true;
35}
[email protected]a9bb6f692008-07-30 16:40:1036
[email protected]978df342009-11-24 06:21:5337} // namespace base