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-2021 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 
97  [[deprecated("use the overload that takes a span<>")]]
98  size_t
99  write(const uint8_t* buf, size_t size)
100  {
101  return write({buf, size});
102  }
103 
112  void
113  end();
114 
118  bool
119  isEnd() const
120  {
121  return m_isEnd;
122  }
123 
127  void
128  setIndex(size_t index)
129  {
130  m_index = index;
131  }
132 
136  size_t
137  getIndex() const
138  {
139  return m_index;
140  }
141 
142 protected:
143  Downstream() = default;
144 
145 private:
146  virtual size_t
147  doWrite(span<const uint8_t> buf) = 0;
148 
149  virtual void
150  doEnd() = 0;
151 
152 private:
153  size_t m_index = 0;
154  bool m_isEnd = false;
155 };
156 
162 class Upstream
163 {
164 public:
165  virtual
166  ~Upstream() = default;
167 
168 protected:
169  Upstream() = default;
170 
171 protected:
175  void
176  appendChain(unique_ptr<Downstream> tail);
177 
178  Downstream*
180  {
181  return m_next.get();
182  }
183 
184 protected:
185  unique_ptr<Downstream> m_next;
186 };
187 
191 class Transform : public Upstream,
192  public Downstream
193 {
194 protected:
195  using OBuffer = std::vector<uint8_t>;
196 
197  Transform() = default;
198 
204  void
206 
211  void
212  flushAllOutput();
213 
217  void
218  setOutputBuffer(unique_ptr<OBuffer> buffer);
219 
223  bool
224  isOutputBufferEmpty() const;
225 
226 private:
227  size_t
228  doWrite(span<const uint8_t> data) final;
229 
235  void
236  doEnd() final;
237 
249  virtual void
250  preTransform();
251 
257  virtual size_t
258  convert(span<const uint8_t> data) = 0;
259 
267  virtual void
268  finalize();
269 
270 private:
271  unique_ptr<OBuffer> m_oBuffer;
272  size_t m_outputOffset = 0;
273 };
274 
280 class Sink : public Downstream
281 {
282 };
283 
289 class Source : public Upstream
290 {
291 public:
295  Source&
296  operator>>(unique_ptr<Transform> transform);
297 
303  void
304  operator>>(unique_ptr<Sink> sink);
305 
306 protected:
307  Source() = default;
308 
312  void
313  pump();
314 
318  size_t
319  getIndex() const
320  {
321  return 0;
322  }
323 
324 private:
325  virtual void
326  doPump() = 0;
327 
328 private:
329  size_t m_nModules = 1; // count of modules in the chain starting from (and including) this Source
330 };
331 
332 } // namespace transform
333 } // namespace security
334 } // namespace ndn
335 
336 #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.
size_t write(const uint8_t *buf, size_t size)
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:370