transform-base.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2023 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 #ifndef NDN_CXX_SECURITY_TRANSFORM_BASE_HPP
23 #define NDN_CXX_SECURITY_TRANSFORM_BASE_HPP
24 
26 #include "ndn-cxx/util/span.hpp"
27 
28 #include <vector>
29 
30 namespace ndn::security::transform {
31 
47 class Error : public std::runtime_error
48 {
49 public:
50  Error(size_t index, const std::string& what);
51 
52  size_t
53  getIndex() const
54  {
55  return m_index;
56  }
57 
58 private:
59  size_t m_index;
60 };
61 
67 class Downstream : noncopyable
68 {
69 public:
70  virtual
71  ~Downstream() = default;
72 
89  size_t
90  write(span<const uint8_t> buf);
91 
100  void
101  end();
102 
106  bool
107  isEnd() const
108  {
109  return m_isEnd;
110  }
111 
115  void
116  setIndex(size_t index)
117  {
118  m_index = index;
119  }
120 
124  size_t
125  getIndex() const
126  {
127  return m_index;
128  }
129 
130 protected:
131  Downstream() = default;
132 
133 private:
134  virtual size_t
135  doWrite(span<const uint8_t> buf) = 0;
136 
137  virtual void
138  doEnd() = 0;
139 
140 private:
141  size_t m_index = 0;
142  bool m_isEnd = false;
143 };
144 
150 class Upstream
151 {
152 public:
153  virtual
154  ~Upstream() = default;
155 
156 protected:
157  Upstream() = default;
158 
162  void
163  appendChain(unique_ptr<Downstream> tail);
164 
165  Downstream*
167  {
168  return m_next.get();
169  }
170 
171 protected:
172  unique_ptr<Downstream> m_next;
173 };
174 
178 class Transform : public Upstream,
179  public Downstream
180 {
181 protected:
182  using OBuffer = std::vector<uint8_t>;
183 
184  Transform() = default;
185 
191  void
193 
198  void
199  flushAllOutput();
200 
204  void
205  setOutputBuffer(unique_ptr<OBuffer> buffer);
206 
210  bool
211  isOutputBufferEmpty() const;
212 
213 private:
214  size_t
215  doWrite(span<const uint8_t> data) final;
216 
222  void
223  doEnd() final;
224 
236  virtual void
237  preTransform();
238 
244  virtual size_t
245  convert(span<const uint8_t> data) = 0;
246 
254  virtual void
255  finalize();
256 
257 private:
258  unique_ptr<OBuffer> m_oBuffer;
259  size_t m_outputOffset = 0;
260 };
261 
267 class Sink : public Downstream
268 {
269 };
270 
276 class Source : public Upstream
277 {
278 public:
282  Source&
283  operator>>(unique_ptr<Transform> transform);
284 
290  void
291  operator>>(unique_ptr<Sink> sink);
292 
293 protected:
294  Source() = default;
295 
299  void
300  pump();
301 
305  size_t
306  getIndex() const
307  {
308  return 0;
309  }
310 
311 private:
312  virtual void
313  doPump() = 0;
314 
315 private:
316  size_t m_nModules = 1; // count of modules in the chain starting from (and including) this Source
317 };
318 
319 } // namespace ndn::security::transform
320 
321 #endif // NDN_CXX_SECURITY_TRANSFORM_BASE_HPP
The downstream interface of a transformation module.
void setIndex(size_t index)
Set the module index.
size_t getIndex() const
Get the module index.
void end()
Close the input interface of a module.
bool isEnd() const
Check if the input interface of a module is closed.
size_t write(span< const uint8_t > buf)
Accept input data and perform transformation.
Base class of transformation error.
Error(size_t index, const std::string &what)
Abstraction of the transformation sink module.
Abstraction of the transformation source module.
size_t getIndex() const
Get the source module index (should always be 0).
Abstraction of an intermediate transformation module.
void setOutputBuffer(unique_ptr< OBuffer > buffer)
Set output buffer to buffer.
void flushAllOutput()
Read the all the content from output buffer and write it into next module.
bool isOutputBufferEmpty() const
Check if output buffer is empty.
void flushOutputBuffer()
Read the content from output buffer and write it into next module.
The upstream interface of a transformation module.
void appendChain(unique_ptr< Downstream > tail)
Connect to the next transformation module.
unique_ptr< Downstream > m_next
Common includes and macros used throughout the library.
std::istream & operator>>(std::istream &is, Name &name)
Parse URI from stream as Name.
Definition: name.cpp:346