]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/base64.cc
Merge branch 'validate-hostname' of https://github.com/hlindqvist/pdns into hlindqvis...
[thirdparty/pdns.git] / pdns / base64.cc
CommitLineData
12471842
PL
1/*
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
870a0fe4
AT
22#ifdef HAVE_CONFIG_H
23#include "config.h"
24#endif
64c7e76f 25#include "base64.hh"
bc712818 26#include <stdexcept>
5fdd91db 27#include <boost/scoped_array.hpp>
f89adb9d
CH
28#include <openssl/bio.h>
29#include <openssl/evp.h>
64c7e76f 30
48836055 31int B64Decode(const std::string& src, std::string& dst)
64c7e76f 32{
48836055
KM
33 if (src.empty() ) {
34 dst.clear();
35 return 0;
36 }
bc712818
CH
37 int dlen = ( src.length() * 6 + 7 ) / 8 ;
38 ssize_t olen = 0;
5fdd91db 39 boost::scoped_array<unsigned char> d( new unsigned char[dlen] );
f89adb9d
CH
40 BIO *bio, *b64;
41 bio = BIO_new(BIO_s_mem());
42 BIO_write(bio, src.c_str(), src.length());
43 b64 = BIO_new(BIO_f_base64());
44 bio = BIO_push(b64, bio);
45 BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
46 olen = BIO_read(b64, d.get(), dlen);
bc712818
CH
47 if ((olen == 0 || olen == -1) && BIO_should_retry(bio)) {
48 BIO_free_all(bio);
49 throw std::runtime_error("BIO_read failed to read all data from memory buffer");
50 }
f89adb9d
CH
51 BIO_free_all(bio);
52 if (olen > 0) {
bc712818 53 dst = std::string( reinterpret_cast<const char*>(d.get()), olen );
48836055
KM
54 return 0;
55 }
56 return -1;
64c7e76f
BH
57}
58
bc712818 59std::string Base64Encode(const std::string& src)
64c7e76f 60{
48836055 61 if (!src.empty()) {
3dd164ab 62 size_t olen = 0;
f89adb9d
CH
63 BIO *bio, *b64;
64 b64 = BIO_new(BIO_f_base64());
65 bio = BIO_new(BIO_s_mem());
66 bio = BIO_push(b64, bio);
67 BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
bcc3fcf9
PL
68 int bioWriteRet = BIO_write(bio, src.c_str(), src.length());
69 if (bioWriteRet < 0 || (size_t) bioWriteRet != src.length()) {
bc712818
CH
70 BIO_free_all(bio);
71 throw std::runtime_error("BIO_write failed to write all data to memory buffer");
72 }
73 (void)BIO_flush(bio);
f89adb9d
CH
74 char* pp;
75 std::string out;
76 olen = BIO_get_mem_data(bio, &pp);
77 if (olen > 0) {
78 out = std::string(pp, olen);
79 }
80 BIO_free_all(bio);
81 return out;
48836055
KM
82 }
83 return "";
64c7e76f 84}
f89adb9d
CH
85
86#if 0
87#include <iostream>
88int main() {
89 std::string in = "PowerDNS Test String 1";
90 std::string out = Base64Encode(in);
91 std::cout << out << std::endl;
92 if (out != "UG93ZXJETlMgVGVzdCBTdHJpbmcgMQ==") {
93 std::cerr << "output did not match expected data" << std::endl;
94 }
95 std::string roundtrip;
96 B64Decode(out, roundtrip);
97 std::cout << roundtrip << std::endl;
98 if (roundtrip != in) {
99 std::cerr << "roundtripped data did not match input data" << std::endl;
100 }
101 return 0;
102}
103#endif