base64-encode.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2022 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 
23 
24 #include <openssl/bio.h>
25 #include <openssl/evp.h>
26 
27 namespace ndn {
28 namespace security {
29 namespace transform {
30 
31 class Base64Encode::Impl : boost::noncopyable
32 {
33 public:
34  Impl()
35  : m_base64(BIO_new(BIO_f_base64()))
36  , m_sink(BIO_new(BIO_s_mem()))
37  {
38  // connect base64 transform to the data sink.
39  BIO_push(m_base64, m_sink);
40  }
41 
42  ~Impl()
43  {
44  BIO_free_all(m_base64);
45  }
46 
47 public:
48  BIO* m_base64;
49  BIO* m_sink; // BIO_f_base64 alone does not work without a sink
50 };
51 
52 
54  : m_impl(make_unique<Impl>())
55 {
56  if (!needBreak)
57  BIO_set_flags(m_impl->m_base64, BIO_FLAGS_BASE64_NO_NL);
58 }
59 
60 Base64Encode::~Base64Encode() = default;
61 
62 void
63 Base64Encode::preTransform()
64 {
65  fillOutputBuffer();
66 }
67 
68 size_t
69 Base64Encode::convert(span<const uint8_t> data)
70 {
71  if (data.empty())
72  return 0;
73 
74  int wLen = BIO_write(m_impl->m_base64, data.data(), data.size());
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  NDN_THROW(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 
89 void
90 Base64Encode::finalize()
91 {
92  if (BIO_flush(m_impl->m_base64) != 1)
93  NDN_THROW(Error(getIndex(), "Failed to flush"));
94 
95  while (!isConverterEmpty()) {
96  fillOutputBuffer();
97  while (!isOutputBufferEmpty()) {
99  }
100  }
101 }
102 
103 void
104 Base64Encode::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->data(), nRead);
113  if (rLen < 0)
114  return;
115 
116  if (rLen < nRead)
117  buffer->erase(buffer->begin() + rLen, buffer->end());
118 
119  setOutputBuffer(std::move(buffer));
120 }
121 
122 bool
123 Base64Encode::isConverterEmpty()
124 {
125  return BIO_pending(m_impl->m_sink) <= 0;
126 }
127 
128 unique_ptr<Transform>
129 base64Encode(bool needBreak)
130 {
131  return make_unique<Base64Encode>(needBreak);
132 }
133 
134 } // namespace transform
135 } // namespace security
136 } // namespace ndn
Base64Encode(bool needBreak=true)
Create a base64 encoding module.
size_t getIndex() const
Get the module index.
void setOutputBuffer(unique_ptr< OBuffer > buffer)
Set output buffer to buffer.
bool isOutputBufferEmpty() const
Check if output buffer is empty.
void flushOutputBuffer()
Read the content from output buffer and write it into next module.
#define NDN_THROW(e)
Definition: exception.hpp:61
unique_ptr< Transform > base64Encode(bool needBreak)
Definition: data.cpp:25