blob: 60e967b580a5b8d6c4921a1b61511034c5cdaea3 [file] [log] [blame]
Yingdi Yu3168c302015-07-04 16:45:40 -07001/* -*- 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
24namespace ndn {
25namespace security {
26namespace transform {
27
28Error::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
34Downstream::Downstream()
35 : m_isEnd(false)
36{
37}
38
39size_t
40Downstream::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
50void
51Downstream::end()
52{
53 if (m_isEnd)
54 return;
55
56 m_isEnd = true;
57 return doEnd();
58}
59
60Upstream::Upstream()
61 : m_next(nullptr)
62{
63}
64
65void
66Upstream::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 Yu38317e52015-07-22 13:58:02 -070077Transform::Transform()
78 : m_oBuffer(nullptr)
79 , m_outputOffset(0)
80{
81}
82
83void
84Transform::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
94void
Yingdi Yuae734272015-07-04 17:38:48 -070095Transform::flushAllOutput()
96{
97 while (!isOutputBufferEmpty()) {
98 flushOutputBuffer();
99 }
100}
101
102void
Yingdi Yu38317e52015-07-22 13:58:02 -0700103Transform::setOutputBuffer(unique_ptr<OBuffer> buffer)
104{
105 BOOST_ASSERT(isOutputBufferEmpty());
106 m_oBuffer = std::move(buffer);
107 m_outputOffset = 0;
108}
109
110bool
111Transform::isOutputBufferEmpty() const
112{
113 return (m_oBuffer == nullptr || m_oBuffer->size() == m_outputOffset);
114}
115
116size_t
117Transform::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
135void
136Transform::doEnd()
137{
138 finalize();
139 m_next->end();
140}
141
142void
143Transform::preTransform()
144{
145}
146
147void
148Transform::finalize()
149{
Yingdi Yuae734272015-07-04 17:38:48 -0700150 flushAllOutput();
Yingdi Yu38317e52015-07-22 13:58:02 -0700151}
152
Yingdi Yu3168c302015-07-04 16:45:40 -0700153Source::Source()
154 : m_nModules(1) // source per se is counted as one module
155{
156}
157
158void
159Source::pump()
160{
161 doPump();
162}
163
164Source&
165Source::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
174void
175Source::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