Yingdi Yu | 3168c30 | 2015-07-04 16:45:40 -0700 | [diff] [blame] | 1 | /* -*- 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 "transform-base.hpp" |
| 23 | |
| 24 | namespace ndn { |
| 25 | namespace security { |
| 26 | namespace transform { |
| 27 | |
| 28 | Error::Error(size_t index, const std::string& what) |
| 29 | : std::runtime_error("Error in module " + std::to_string(index) + ": " + what) |
| 30 | , m_index(index) |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | Downstream::Downstream() |
| 35 | : m_isEnd(false) |
Junxiao Shi | b8deee0 | 2016-09-12 21:00:06 +0000 | [diff] [blame^] | 36 | , m_index(0) |
Yingdi Yu | 3168c30 | 2015-07-04 16:45:40 -0700 | [diff] [blame] | 37 | { |
| 38 | } |
| 39 | |
| 40 | size_t |
| 41 | Downstream::write(const uint8_t* buf, size_t size) |
| 42 | { |
| 43 | if (m_isEnd) |
| 44 | BOOST_THROW_EXCEPTION(Error(getIndex(), "Module is closed, no more input")); |
| 45 | |
| 46 | size_t nBytesWritten = doWrite(buf, size); |
| 47 | BOOST_ASSERT(nBytesWritten <= size); |
| 48 | return nBytesWritten; |
| 49 | } |
| 50 | |
| 51 | void |
| 52 | Downstream::end() |
| 53 | { |
| 54 | if (m_isEnd) |
| 55 | return; |
| 56 | |
| 57 | m_isEnd = true; |
| 58 | return doEnd(); |
| 59 | } |
| 60 | |
| 61 | Upstream::Upstream() |
| 62 | : m_next(nullptr) |
| 63 | { |
| 64 | } |
| 65 | |
| 66 | void |
| 67 | Upstream::appendChain(unique_ptr<Downstream> tail) |
| 68 | { |
| 69 | if (m_next == nullptr) { |
| 70 | m_next = std::move(tail); |
| 71 | } |
| 72 | else { |
| 73 | BOOST_ASSERT(dynamic_cast<Transform*>(m_next.get()) != nullptr); |
| 74 | static_cast<Transform*>(m_next.get())->appendChain(std::move(tail)); |
| 75 | } |
| 76 | } |
| 77 | |
Yingdi Yu | 38317e5 | 2015-07-22 13:58:02 -0700 | [diff] [blame] | 78 | Transform::Transform() |
| 79 | : m_oBuffer(nullptr) |
| 80 | , m_outputOffset(0) |
| 81 | { |
| 82 | } |
| 83 | |
| 84 | void |
| 85 | Transform::flushOutputBuffer() |
| 86 | { |
| 87 | if (isOutputBufferEmpty()) |
| 88 | return; |
| 89 | |
| 90 | size_t nWritten = m_next->write(&(*m_oBuffer)[m_outputOffset], |
| 91 | m_oBuffer->size() - m_outputOffset); |
| 92 | m_outputOffset += nWritten; |
| 93 | } |
| 94 | |
| 95 | void |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 96 | Transform::flushAllOutput() |
| 97 | { |
| 98 | while (!isOutputBufferEmpty()) { |
| 99 | flushOutputBuffer(); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | void |
Yingdi Yu | 38317e5 | 2015-07-22 13:58:02 -0700 | [diff] [blame] | 104 | Transform::setOutputBuffer(unique_ptr<OBuffer> buffer) |
| 105 | { |
| 106 | BOOST_ASSERT(isOutputBufferEmpty()); |
| 107 | m_oBuffer = std::move(buffer); |
| 108 | m_outputOffset = 0; |
| 109 | } |
| 110 | |
| 111 | bool |
| 112 | Transform::isOutputBufferEmpty() const |
| 113 | { |
| 114 | return (m_oBuffer == nullptr || m_oBuffer->size() == m_outputOffset); |
| 115 | } |
| 116 | |
| 117 | size_t |
| 118 | Transform::doWrite(const uint8_t* data, size_t dataLen) |
| 119 | { |
| 120 | flushOutputBuffer(); |
| 121 | if (!isOutputBufferEmpty()) |
| 122 | return 0; |
| 123 | |
| 124 | preTransform(); |
| 125 | flushOutputBuffer(); |
| 126 | if (!isOutputBufferEmpty()) |
| 127 | return 0; |
| 128 | |
| 129 | size_t nConverted = convert(data, dataLen); |
| 130 | |
| 131 | flushOutputBuffer(); |
| 132 | |
| 133 | return nConverted; |
| 134 | } |
| 135 | |
| 136 | void |
| 137 | Transform::doEnd() |
| 138 | { |
| 139 | finalize(); |
| 140 | m_next->end(); |
| 141 | } |
| 142 | |
| 143 | void |
| 144 | Transform::preTransform() |
| 145 | { |
| 146 | } |
| 147 | |
| 148 | void |
| 149 | Transform::finalize() |
| 150 | { |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 151 | flushAllOutput(); |
Yingdi Yu | 38317e5 | 2015-07-22 13:58:02 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Yingdi Yu | 3168c30 | 2015-07-04 16:45:40 -0700 | [diff] [blame] | 154 | Source::Source() |
| 155 | : m_nModules(1) // source per se is counted as one module |
| 156 | { |
| 157 | } |
| 158 | |
| 159 | void |
| 160 | Source::pump() |
| 161 | { |
| 162 | doPump(); |
| 163 | } |
| 164 | |
| 165 | Source& |
| 166 | Source::operator>>(unique_ptr<Transform> transform) |
| 167 | { |
| 168 | transform->setIndex(m_nModules); |
| 169 | m_nModules++; |
| 170 | this->appendChain(std::move(transform)); |
| 171 | |
| 172 | return *this; |
| 173 | } |
| 174 | |
| 175 | void |
| 176 | Source::operator>>(unique_ptr<Sink> sink) |
| 177 | { |
| 178 | sink->setIndex(m_nModules); |
| 179 | m_nModules++; |
| 180 | this->appendChain(std::move(sink)); |
| 181 | |
| 182 | this->pump(); |
| 183 | } |
| 184 | |
| 185 | } // namespace transform |
| 186 | } // namespace security |
| 187 | } // namespace ndn |