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