blob: 2a65281a8d224df22eb6e3e4947e94965f626a22 [file] [log] [blame]
Yingdi Yu3168c302015-07-04 16:45:40 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev130076f2017-12-05 11:09:33 -05002/*
3 * Copyright (c) 2013-2017 Regents of the University of California.
Yingdi Yu3168c302015-07-04 16:45:40 -07004 *
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)
Alexander Afanasyev130076f2017-12-05 11:09:33 -050029 : std::runtime_error("Error in module " + to_string(index) + ": " + what)
Yingdi Yu3168c302015-07-04 16:45:40 -070030 , m_index(index)
31{
32}
33
34Downstream::Downstream()
35 : m_isEnd(false)
Junxiao Shib8deee02016-09-12 21:00:06 +000036 , m_index(0)
Yingdi Yu3168c302015-07-04 16:45:40 -070037{
38}
39
40size_t
41Downstream::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
51void
52Downstream::end()
53{
54 if (m_isEnd)
55 return;
56
57 m_isEnd = true;
58 return doEnd();
59}
60
61Upstream::Upstream()
62 : m_next(nullptr)
63{
64}
65
66void
67Upstream::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 Yu38317e52015-07-22 13:58:02 -070078Transform::Transform()
79 : m_oBuffer(nullptr)
80 , m_outputOffset(0)
81{
82}
83
84void
85Transform::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
95void
Yingdi Yuae734272015-07-04 17:38:48 -070096Transform::flushAllOutput()
97{
98 while (!isOutputBufferEmpty()) {
99 flushOutputBuffer();
100 }
101}
102
103void
Yingdi Yu38317e52015-07-22 13:58:02 -0700104Transform::setOutputBuffer(unique_ptr<OBuffer> buffer)
105{
106 BOOST_ASSERT(isOutputBufferEmpty());
107 m_oBuffer = std::move(buffer);
108 m_outputOffset = 0;
109}
110
111bool
112Transform::isOutputBufferEmpty() const
113{
114 return (m_oBuffer == nullptr || m_oBuffer->size() == m_outputOffset);
115}
116
117size_t
118Transform::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
136void
137Transform::doEnd()
138{
139 finalize();
140 m_next->end();
141}
142
143void
144Transform::preTransform()
145{
146}
147
148void
149Transform::finalize()
150{
Yingdi Yuae734272015-07-04 17:38:48 -0700151 flushAllOutput();
Yingdi Yu38317e52015-07-22 13:58:02 -0700152}
153
Yingdi Yu3168c302015-07-04 16:45:40 -0700154Source::Source()
155 : m_nModules(1) // source per se is counted as one module
156{
157}
158
159void
160Source::pump()
161{
162 doPump();
163}
164
165Source&
166Source::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
175void
176Source::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