blob: 0a27bedab20dc156afb263f4c8b1800bb9ab54d2 [file] [log] [blame]
Yingdi Yu38317e52015-07-22 13:58:02 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#include "base64-encode.hpp"
23#include "../../encoding/buffer.hpp"
24#include "../detail/openssl.hpp"
25
26namespace ndn {
27namespace security {
28namespace transform {
29
30/**
31 * @brief The implementation class which contains the internal state of the filter
32 * which includes openssl specific structures.
33 */
34class Base64Encode::Impl
35{
36public:
37 Impl()
38 : m_base64(BIO_new(BIO_f_base64()))
39 , m_sink(BIO_new(BIO_s_mem()))
40 {
41 // connect base64 transform to the data sink.
42 BIO_push(m_base64, m_sink);
43 }
44
45 ~Impl()
46 {
47 BIO_free_all(m_sink);
48 }
49
50public:
51 BIO* m_base64;
52 BIO* m_sink; // BIO_f_base64 alone does not work without a sink
53};
54
55Base64Encode::Base64Encode(bool needBreak)
56 : m_impl(new Impl)
57{
58 if (!needBreak)
59 BIO_set_flags(m_impl->m_base64, BIO_FLAGS_BASE64_NO_NL);
60}
61
62void
63Base64Encode::preTransform()
64{
65 fillOutputBuffer();
66}
67
68size_t
69Base64Encode::convert(const uint8_t* data, size_t dataLen)
70{
71 if (dataLen == 0)
72 return 0;
73
74 int wLen = BIO_write(m_impl->m_base64, data, dataLen);
75
76 if (wLen <= 0) { // fail to write data
77 if (!BIO_should_retry(m_impl->m_base64)) {
78 // we haven't written everything but some error happens, and we cannot retry
79 BOOST_THROW_EXCEPTION(Error(getIndex(), "Failed to accept more input"));
80 }
81 return 0;
82 }
83 else { // update number of bytes written
84 fillOutputBuffer();
85 return wLen;
86 }
87}
88
89void
90Base64Encode::finalize()
91{
92 if (BIO_flush(m_impl->m_base64) != 1)
93 BOOST_THROW_EXCEPTION(Error(getIndex(), "Failed to flush"));
94
95 while (!isConverterEmpty()) {
96 fillOutputBuffer();
97 while (!isOutputBufferEmpty()) {
98 flushOutputBuffer();
99 }
100 }
101}
102
103void
104Base64Encode::fillOutputBuffer()
105{
106 int nRead = BIO_pending(m_impl->m_sink);
107 if (nRead <= 0)
108 return;
109
110 // there is something to read from BIO
111 auto buffer = make_unique<OBuffer>(nRead);
112 int rLen = BIO_read(m_impl->m_sink, &(*buffer)[0], nRead);
113 if (rLen < 0)
114 return;
115
116 if (rLen < nRead)
117 buffer->erase(buffer->begin() + rLen, buffer->end());
118 setOutputBuffer(std::move(buffer));
119}
120
121bool
122Base64Encode::isConverterEmpty()
123{
124 return (BIO_pending(m_impl->m_sink) <= 0);
125}
126
127unique_ptr<Transform>
128base64Encode(bool needBreak)
129{
130 return make_unique<Base64Encode>(needBreak);
131}
132
133} // namespace transform
134} // namespace security
135} // namespace ndn