blob: 8ed1249257d6fd7de3472a5cc3e87a69edbe3e28 [file] [log] [blame]
lambroslambrou@chromium.org0d2c4aa2012-05-18 03:34:59 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
hayato@chromium.org268ac3a2009-11-24 15:21:53 +09002// 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
9namespace base {
10
vadimt@chromium.org528238b2013-12-11 10:48:50 +090011void Base64Encode(const StringPiece& input, std::string* output) {
hayato@chromium.org268ac3a2009-11-24 15:21:53 +090012 std::string temp;
13 temp.resize(modp_b64_encode_len(input.size())); // makes room for null byte
14
lambroslambrou@chromium.org0d2c4aa2012-05-18 03:34:59 +090015 // modp_b64_encode_len() returns at least 1, so temp[0] is safe to use.
vadimt@chromium.org528238b2013-12-11 10:48:50 +090016 size_t output_size = modp_b64_encode(&(temp[0]), input.data(), input.size());
hayato@chromium.org268ac3a2009-11-24 15:21:53 +090017
18 temp.resize(output_size); // strips off null byte
19 output->swap(temp);
hayato@chromium.org268ac3a2009-11-24 15:21:53 +090020}
21
joth@chromium.org42544c82011-07-22 22:22:23 +090022bool Base64Decode(const StringPiece& input, std::string* output) {
hayato@chromium.org268ac3a2009-11-24 15:21:53 +090023 std::string temp;
24 temp.resize(modp_b64_decode_len(input.size()));
25
26 // does not null terminate result since result is binary data!
jschuh@chromium.org98976a62012-12-28 07:01:58 +090027 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)
hayato@chromium.org268ac3a2009-11-24 15:21:53 +090030 return false;
31
32 temp.resize(output_size);
33 output->swap(temp);
34 return true;
35}
36
37} // namespace base