base64-decode.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 Base64Decode::Impl : boost::noncopyable
32 {
33 public:
34  Impl()
35  : m_base64(BIO_new(BIO_f_base64()))
36  , m_source(BIO_new(BIO_s_mem()))
37  {
38  // Input may not be written in a single time.
39  // Do not return EOF when source is empty unless explicitly requested
40  BIO_set_mem_eof_return(m_source, -1);
41 
42  // connect base64 transform to the data source.
43  BIO_push(m_base64, m_source);
44  }
45 
46  ~Impl()
47  {
48  BIO_free_all(m_base64);
49  }
50 
51 public:
52  BIO* m_base64;
53  BIO* m_source; // BIO_f_base64 alone does not work without a source
54 };
55 
56 
57 Base64Decode::Base64Decode(bool expectNewlineEvery64Bytes)
58  : m_impl(make_unique<Impl>())
59 {
60  if (!expectNewlineEvery64Bytes)
61  BIO_set_flags(m_impl->m_base64, BIO_FLAGS_BASE64_NO_NL);
62 }
63 
64 Base64Decode::~Base64Decode() = default;
65 
66 void
67 Base64Decode::preTransform()
68 {
69  while (isOutputBufferEmpty()) {
70  fillOutputBuffer();
71  if (isOutputBufferEmpty()) // nothing to read from BIO, return
72  return;
73 
75  }
76 }
77 
78 size_t
79 Base64Decode::convert(span<const uint8_t> buf)
80 {
81  int wLen = BIO_write(m_impl->m_source, buf.data(), buf.size());
82 
83  if (wLen <= 0) { // fail to write data
84  if (!BIO_should_retry(m_impl->m_source)) {
85  // we haven't written everything but some error happens, and we cannot retry
86  NDN_THROW(Error(getIndex(), "Failed to accept more input"));
87  }
88  return 0;
89  }
90  else { // update number of bytes written
91  return wLen;
92  }
93 }
94 
95 void
96 Base64Decode::finalize()
97 {
98  BIO_set_mem_eof_return(m_impl->m_source, 0);
99 
100  fillOutputBuffer();
101 
102  while (!isOutputBufferEmpty()) {
104  if (isOutputBufferEmpty())
105  fillOutputBuffer();
106  }
107 }
108 
109 void
110 Base64Decode::fillOutputBuffer()
111 {
112  // OpenSSL base64 BIO cannot give us the number bytes of partial decoded result,
113  // so we just try to read a chunk.
114  auto buffer = make_unique<OBuffer>(1024);
115  int rLen = BIO_read(m_impl->m_base64, buffer->data(), buffer->size());
116  if (rLen <= 0)
117  return;
118 
119  if (static_cast<size_t>(rLen) < buffer->size())
120  buffer->erase(buffer->begin() + rLen, buffer->end());
121 
122  setOutputBuffer(std::move(buffer));
123 }
124 
125 unique_ptr<Transform>
126 base64Decode(bool expectNewlineEvery64Bytes)
127 {
128  return make_unique<Base64Decode>(expectNewlineEvery64Bytes);
129 }
130 
131 } // namespace transform
132 } // namespace security
133 } // namespace ndn
Base64Decode(bool expectNewlineEvery64Bytes=true)
Create a base64 decoding 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 > base64Decode(bool expectNewlineEvery64Bytes)
Definition: data.cpp:25