io.cpp
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 #include "ndn-cxx/util/io.hpp"
32 
33 namespace ndn::io {
34 
35 shared_ptr<Buffer>
36 loadBuffer(std::istream& is, IoEncoding encoding)
37 {
38  namespace t = ndn::security::transform;
39 
40  OBufferStream os;
41  try {
42  switch (encoding) {
43  case NO_ENCODING:
44  t::streamSource(is) >> t::streamSink(os);
45  return os.buf();
46  case BASE64:
47  t::streamSource(is) >> t::stripSpace("\n") >> t::base64Decode(false) >> t::streamSink(os);
48  return os.buf();
49  case HEX:
51  return os.buf();
52  }
53  }
54  catch (const std::runtime_error& e) {
55  NDN_THROW_NESTED(Error(e.what()));
56  }
57 
58  NDN_THROW(std::invalid_argument("Unknown IoEncoding " + to_string(encoding)));
59 }
60 
61 void
62 saveBuffer(span<const uint8_t> buf, std::ostream& os, IoEncoding encoding)
63 {
64  namespace t = ndn::security::transform;
65 
66  try {
67  switch (encoding) {
68  case NO_ENCODING:
69  t::bufferSource(buf) >> t::streamSink(os);
70  return;
71  case BASE64:
73  return;
74  case HEX:
75  t::bufferSource(buf) >> t::hexEncode(true) >> t::streamSink(os);
76  return;
77  }
78  }
79  catch (const std::runtime_error& e) {
80  NDN_THROW_NESTED(Error(e.what()));
81  }
82 
83  NDN_THROW(std::invalid_argument("Unknown IoEncoding " + to_string(encoding)));
84 }
85 
86 } // namespace ndn::io
An output stream that writes to a Buffer.
std::shared_ptr< Buffer > buf()
Return a shared pointer to the underlying buffer.
#define NDN_THROW_NESTED(e)
Definition: exception.hpp:65
#define NDN_THROW(e)
Definition: exception.hpp:56
std::string to_string(const errinfo_stacktrace &x)
Definition: exception.cpp:30
Definition: io.cpp:33
void saveBuffer(span< const uint8_t > buf, std::ostream &os, IoEncoding encoding)
Writes a sequence of bytes to a stream.
Definition: io.cpp:62
IoEncoding
Indicates how a file or stream of bytes is encoded.
Definition: io.hpp:40
@ NO_ENCODING
Raw binary, without encoding.
Definition: io.hpp:43
@ HEX
Hexadecimal encoding.
Definition: io.hpp:56
@ BASE64
Base64 encoding.
Definition: io.hpp:50
shared_ptr< Buffer > loadBuffer(std::istream &is, IoEncoding encoding)
Reads bytes from a stream until EOF.
Definition: io.cpp:36
unique_ptr< Sink > streamSink(std::ostream &os)
Definition: stream-sink.cpp:51
unique_ptr< Transform > hexDecode()
Definition: hex-decode.cpp:106
unique_ptr< Transform > stripSpace(const char *whitespaces)
Constructs a StripSpace transform.
Definition: strip-space.cpp:52
unique_ptr< Transform > base64Encode(bool needBreak)
unique_ptr< Transform > hexEncode(bool useUpperCase)
Definition: hex-encode.cpp:62
unique_ptr< Transform > base64Decode(bool expectNewlineEvery64Bytes)