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-2022 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 {
31 namespace security {
32 namespace transform {
33 
49 class Error : public std::runtime_error
50 {
51 public:
52  Error(size_t index, const std::string& what);
53 
54  size_t
55  getIndex() const
56  {
57  return m_index;
58  }
59 
60 private:
61  size_t m_index;
62 };
63 
69 class Downstream : noncopyable
70 {
71 public:
72  virtual
73  ~Downstream() = default;
74 
91  size_t
92  write(span<const uint8_t> buf);
93 
102  void
103  end();
104 
108  bool
109  isEnd() const
110  {
111  return m_isEnd;
112  }
113 
117  void
118  setIndex(size_t index)
119  {
120  m_index = index;
121  }
122 
126  size_t
127  getIndex() const
128  {
129  return m_index;
130  }
131 
132 protected:
133  Downstream() = default;
134 
135 private:
136  virtual size_t
137  doWrite(span<const uint8_t> buf) = 0;
138 
139  virtual void
140  doEnd() = 0;
141 
142 private:
143  size_t m_index = 0;
144  bool m_isEnd = false;
145 };
146 
152 class Upstream
153 {
154 public:
155  virtual
156  ~Upstream() = default;
157 
158 protected:
159  Upstream() = default;
160 
164  void
165  appendChain(unique_ptr<Downstream> tail);
166 
167  Downstream*
169  {
170  return m_next.get();
171  }
172 
173 protected:
174  unique_ptr<Downstream> m_next;
175 };
176 
180 class Transform : public Upstream,
181  public Downstream
182 {
183 protected:
184  using OBuffer = std::vector<uint8_t>;
185 
186  Transform() = default;
187 
193  void
195 
200  void
201  flushAllOutput();
202 
206  void
207  setOutputBuffer(unique_ptr<OBuffer> buffer);
208 
212  bool
213  isOutputBufferEmpty() const;
214 
215 private:
216  size_t
217  doWrite(span<const uint8_t> data) final;
218 
224  void
225  doEnd() final;
226 
238  virtual void
239  preTransform();
240 
246  virtual size_t
247  convert(span<const uint8_t> data) = 0;
248 
256  virtual void
257  finalize();
258 
259 private:
260  unique_ptr<OBuffer> m_oBuffer;
261  size_t m_outputOffset = 0;
262 };
263 
269 class Sink : public Downstream
270 {
271 };
272 
278 class Source : public Upstream
279 {
280 public:
284  Source&
285  operator>>(unique_ptr<Transform> transform);
286 
292  void
293  operator>>(unique_ptr<Sink> sink);
294 
295 protected:
296  Source() = default;
297 
301  void
302  pump();
303 
307  size_t
308  getIndex() const
309  {
310  return 0;
311  }
312 
313 private:
314  virtual void
315  doPump() = 0;
316 
317 private:
318  size_t m_nModules = 1; // count of modules in the chain starting from (and including) this Source
319 };
320 
321 } // namespace transform
322 } // namespace security
323 } // namespace ndn
324 
325 #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.
Definition: data.cpp:25
std::istream & operator>>(std::istream &is, Name &name)
Parse URI from stream as Name.
Definition: name.cpp:371