Yingdi Yu | 38317e5 | 2015-07-22 13:58:02 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 8aad372 | 2017-09-16 20:57:28 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Yingdi Yu | 38317e5 | 2015-07-22 13:58:02 -0700 | [diff] [blame] | 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-decode.hpp" |
Yingdi Yu | 38317e5 | 2015-07-22 13:58:02 -0700 | [diff] [blame] | 23 | #include "../detail/openssl.hpp" |
| 24 | |
| 25 | namespace ndn { |
| 26 | namespace security { |
| 27 | namespace transform { |
| 28 | |
| 29 | /** |
| 30 | * @brief The implementation class which contains the internal state of the filter |
| 31 | * which includes openssl specific structures. |
| 32 | */ |
| 33 | class Base64Decode::Impl |
| 34 | { |
| 35 | public: |
| 36 | Impl() |
| 37 | : m_base64(BIO_new(BIO_f_base64())) |
| 38 | , m_source(BIO_new(BIO_s_mem())) |
| 39 | { |
| 40 | // Input may not be written in a single time. |
| 41 | // Do not return EOF when source is empty unless explicitly requested |
| 42 | BIO_set_mem_eof_return(m_source, -1); |
| 43 | |
| 44 | // connect base64 transform to the data source. |
| 45 | BIO_push(m_base64, m_source); |
| 46 | } |
| 47 | |
| 48 | ~Impl() |
| 49 | { |
Junxiao Shi | 1b0f486 | 2016-09-10 17:45:04 +0000 | [diff] [blame] | 50 | BIO_free_all(m_base64); |
Yingdi Yu | 38317e5 | 2015-07-22 13:58:02 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | public: |
| 54 | BIO* m_base64; |
| 55 | BIO* m_source; // BIO_f_base64 alone does not work without a source |
| 56 | }; |
| 57 | |
| 58 | static const size_t BUFFER_LENGTH = 1024; |
| 59 | |
| 60 | Base64Decode::Base64Decode(bool expectNewlineEvery64Bytes) |
Davide Pesavento | 8aad372 | 2017-09-16 20:57:28 -0400 | [diff] [blame] | 61 | : m_impl(make_unique<Impl>()) |
Yingdi Yu | 38317e5 | 2015-07-22 13:58:02 -0700 | [diff] [blame] | 62 | { |
| 63 | if (!expectNewlineEvery64Bytes) |
| 64 | BIO_set_flags(m_impl->m_base64, BIO_FLAGS_BASE64_NO_NL); |
| 65 | } |
| 66 | |
Davide Pesavento | 8aad372 | 2017-09-16 20:57:28 -0400 | [diff] [blame] | 67 | Base64Decode::~Base64Decode() = default; |
| 68 | |
Yingdi Yu | 38317e5 | 2015-07-22 13:58:02 -0700 | [diff] [blame] | 69 | void |
| 70 | Base64Decode::preTransform() |
| 71 | { |
| 72 | while (isOutputBufferEmpty()) { |
| 73 | fillOutputBuffer(); |
| 74 | if (isOutputBufferEmpty()) // nothing to read from BIO, return |
| 75 | return; |
| 76 | |
| 77 | flushOutputBuffer(); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | size_t |
| 82 | Base64Decode::convert(const uint8_t* buf, size_t size) |
| 83 | { |
| 84 | int wLen = BIO_write(m_impl->m_source, buf, size); |
| 85 | |
| 86 | if (wLen <= 0) { // fail to write data |
| 87 | if (!BIO_should_retry(m_impl->m_source)) { |
| 88 | // we haven't written everything but some error happens, and we cannot retry |
| 89 | BOOST_THROW_EXCEPTION(Error(getIndex(), "Failed to accept more input")); |
| 90 | } |
| 91 | return 0; |
| 92 | } |
| 93 | else { // update number of bytes written |
| 94 | return wLen; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | void |
| 99 | Base64Decode::finalize() |
| 100 | { |
| 101 | BIO_set_mem_eof_return(m_impl->m_source, 0); |
| 102 | |
| 103 | fillOutputBuffer(); |
| 104 | |
| 105 | while (!isOutputBufferEmpty()) { |
| 106 | flushOutputBuffer(); |
| 107 | if (isOutputBufferEmpty()) |
| 108 | fillOutputBuffer(); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | Base64Decode::fillOutputBuffer() |
| 114 | { |
| 115 | // OpenSSL base64 BIO cannot give us the number bytes of partial decoded result, |
| 116 | // so we just try to read a chunk. |
| 117 | auto buffer = make_unique<OBuffer>(BUFFER_LENGTH); |
Davide Pesavento | 8aad372 | 2017-09-16 20:57:28 -0400 | [diff] [blame] | 118 | int rLen = BIO_read(m_impl->m_base64, buffer->data(), buffer->size()); |
Yingdi Yu | 38317e5 | 2015-07-22 13:58:02 -0700 | [diff] [blame] | 119 | if (rLen <= 0) |
| 120 | return; |
| 121 | |
| 122 | if (static_cast<size_t>(rLen) < buffer->size()) |
| 123 | buffer->erase(buffer->begin() + rLen, buffer->end()); |
| 124 | |
| 125 | setOutputBuffer(std::move(buffer)); |
| 126 | } |
| 127 | |
| 128 | unique_ptr<Transform> |
| 129 | base64Decode(bool expectNewlineEvery64Bytes) |
| 130 | { |
| 131 | return make_unique<Base64Decode>(expectNewlineEvery64Bytes); |
| 132 | } |
| 133 | |
| 134 | } // namespace transform |
| 135 | } // namespace security |
| 136 | } // namespace ndn |