NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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-2019 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
"
23
#include "
ndn-cxx/encoding/buffer-stream.hpp
"
24
#include "
ndn-cxx/security/transform/base64-decode.hpp
"
25
#include "
ndn-cxx/security/transform/base64-encode.hpp
"
26
#include "
ndn-cxx/security/transform/buffer-source.hpp
"
27
#include "
ndn-cxx/security/transform/hex-decode.hpp
"
28
#include "
ndn-cxx/security/transform/hex-encode.hpp
"
29
#include "
ndn-cxx/security/transform/stream-sink.hpp
"
30
#include "
ndn-cxx/security/transform/stream-source.hpp
"
31
#include "
ndn-cxx/security/transform/strip-space.hpp
"
32
33
namespace
ndn
{
34
namespace
io {
35
36
shared_ptr<Buffer>
37
loadBuffer
(std::istream& is,
IoEncoding
encoding)
38
{
39
namespace
t =
ndn::security::transform
;
40
41
OBufferStream
os;
42
try
{
43
switch
(encoding) {
44
case
NO_ENCODING
:
45
t::streamSource
(is) >>
t::streamSink
(os);
46
return
os.
buf
();
47
case
BASE64
:
48
t::streamSource
(is) >>
t::stripSpace
(
"\n"
) >>
t::base64Decode
(
false
) >>
t::streamSink
(os);
49
return
os.
buf
();
50
case
HEX
:
51
t::streamSource
(is) >>
t::hexDecode
() >>
t::streamSink
(os);
52
return
os.
buf
();
53
}
54
}
55
catch
(
const
t::Error& e) {
56
NDN_THROW_NESTED
(
Error
(e.what()));
57
}
58
59
NDN_THROW
(std::invalid_argument(
"Unknown IoEncoding "
+
to_string
(encoding)));
60
}
61
62
optional<Block>
63
loadBlock
(std::istream& is,
IoEncoding
encoding)
64
{
65
try
{
66
return
make_optional<Block>(
loadBuffer
(is, encoding));
67
}
68
catch
(
const
std::invalid_argument&) {
69
return
nullopt
;
70
}
71
catch
(
const
std::runtime_error&) {
72
return
nullopt
;
73
}
74
}
75
76
void
77
saveBuffer
(
const
uint8_t*
buf
,
size_t
size, std::ostream& os,
IoEncoding
encoding)
78
{
79
namespace
t =
ndn::security::transform
;
80
81
try
{
82
switch
(encoding) {
83
case
NO_ENCODING
:
84
t::bufferSource
(
buf
, size) >>
t::streamSink
(os);
85
return
;
86
case
BASE64
:
87
t::bufferSource
(
buf
, size) >>
t::base64Encode
() >>
t::streamSink
(os);
88
return
;
89
case
HEX
:
90
t::bufferSource
(
buf
, size) >>
t::hexEncode
(
true
) >>
t::streamSink
(os);
91
return
;
92
}
93
}
94
catch
(
const
t::Error& e) {
95
NDN_THROW_NESTED
(
Error
(e.what()));
96
}
97
98
NDN_THROW
(std::invalid_argument(
"Unknown IoEncoding "
+
to_string
(encoding)));
99
}
100
101
void
102
saveBlock
(
const
Block
& block, std::ostream& os,
IoEncoding
encoding)
103
{
104
saveBuffer
(block.
wire
(), block.
size
(), os, encoding);
105
}
106
107
}
// namespace io
108
}
// namespace ndn
buf
const uint8_t * buf
Definition:
verification-helpers.cpp:47
buffer-source.hpp
ndn::io::NO_ENCODING
@ NO_ENCODING
Raw binary, without encoding.
Definition:
io.hpp:44
ndn::security::transform
Definition:
back-end-file.hpp:30
ndn::security::transform::stripSpace
unique_ptr< Transform > stripSpace(const char *whitespaces)
constructs a StripSpace transform
Definition:
strip-space.cpp:55
ndn::security::transform::base64Decode
unique_ptr< Transform > base64Decode(bool expectNewlineEvery64Bytes)
Definition:
base64-decode.cpp:129
ndn::security::transform::bufferSource
BufferSource bufferSource
Definition:
buffer-source.hpp:73
hex-encode.hpp
base64-decode.hpp
ndn::io::BASE64
@ BASE64
Base64 encoding.
Definition:
io.hpp:51
NDN_THROW_NESTED
#define NDN_THROW_NESTED(e)
Definition:
exception.hpp:71
ndn::io::loadBlock
optional< Block > loadBlock(std::istream &is, IoEncoding encoding)
Reads a TLV block from a stream.
Definition:
io.cpp:63
io.hpp
ndn::io::loadBuffer
shared_ptr< Buffer > loadBuffer(std::istream &is, IoEncoding encoding)
Reads bytes from a stream until EOF.
Definition:
io.cpp:37
ndn::security::transform::hexEncode
unique_ptr< Transform > hexEncode(bool useUpperCase)
Definition:
hex-encode.cpp:70
hex-decode.hpp
ndn::io::IoEncoding
IoEncoding
Indicates how a file or stream of bytes is encoded.
Definition:
io.hpp:41
ndn::io::saveBuffer
void saveBuffer(const uint8_t *buf, size_t size, std::ostream &os, IoEncoding encoding)
Writes a byte buffer to a stream.
Definition:
io.cpp:77
NDN_THROW
#define NDN_THROW(e)
Definition:
exception.hpp:61
ndn::security::transform::streamSource
StreamSource streamSource
Definition:
stream-source.hpp:62
nonstd::optional_lite::nullopt
const nullopt_t nullopt((nullopt_t::init()))
stream-sink.hpp
ndn::security::transform::base64Encode
unique_ptr< Transform > base64Encode(bool needBreak)
Definition:
base64-encode.cpp:129
stream-source.hpp
ndn::security::transform::hexDecode
unique_ptr< Transform > hexDecode()
Definition:
hex-decode.cpp:114
ndn::OBufferStream
implements an output stream that constructs ndn::Buffer
Definition:
buffer-stream.hpp:71
ndn::io::Error
Definition:
io.hpp:34
ndn::Block
Represents a TLV element of NDN packet format.
Definition:
block.hpp:43
strip-space.hpp
ndn::security::transform::streamSink
unique_ptr< Sink > streamSink(std::ostream &os)
Definition:
stream-sink.cpp:53
ndn::Block::size
size_t size() const
Return the size of the encoded wire, i.e.
Definition:
block.cpp:290
ndn::to_string
std::string to_string(const T &val)
Definition:
backports.hpp:102
buffer-stream.hpp
ndn::io::HEX
@ HEX
Hexadecimal encoding.
Definition:
io.hpp:57
ndn::OBufferStream::buf
shared_ptr< Buffer > buf()
Flush written data to the stream and return shared pointer to the underlying buffer.
Definition:
buffer-stream.cpp:54
ndn::Block::wire
const uint8_t * wire() const
Return a raw pointer to the beginning of the encoded wire.
Definition:
block.cpp:281
ndn::io::saveBlock
void saveBlock(const Block &block, std::ostream &os, IoEncoding encoding)
Writes a TLV block to a stream.
Definition:
io.cpp:102
base64-encode.hpp
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition:
ndn-strategy-choice-helper.hpp:34
ndnSIM
ndn-cxx
ndn-cxx
util
io.cpp
Generated on Mon Jun 1 2020 22:32:15 for ndnSIM by
1.8.18