blob: a385eea26a99e902de484334580ac975310d36c5 [file] [log] [blame]
Junxiao Shi160701a2016-08-30 11:35:25 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawandee84d1eb2018-01-04 20:46:44 -06002/*
Davide Pesavento923ba442019-02-12 22:00:38 -05003 * Copyright (c) 2013-2019 Regents of the University of California.
Junxiao Shi160701a2016-08-30 11:35:25 +00004 *
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
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/util/io.hpp"
23#include "ndn-cxx/encoding/buffer-stream.hpp"
Davide Pesavento6c6e3852019-08-05 20:20:35 -040024#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"
Junxiao Shi160701a2016-08-30 11:35:25 +000032
33namespace ndn {
34namespace io {
35
Davide Pesavento6c6e3852019-08-05 20:20:35 -040036shared_ptr<Buffer>
37loadBuffer(std::istream& is, IoEncoding encoding)
Junxiao Shi160701a2016-08-30 11:35:25 +000038{
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);
Davide Pesavento6c6e3852019-08-05 20:20:35 -040046 return os.buf();
Junxiao Shi4ce0bcf2016-09-03 07:09:03 +000047 case BASE64:
48 t::streamSource(is) >> t::stripSpace("\n") >> t::base64Decode(false) >> t::streamSink(os);
Davide Pesavento6c6e3852019-08-05 20:20:35 -040049 return os.buf();
Junxiao Shi160701a2016-08-30 11:35:25 +000050 case HEX:
51 t::streamSource(is) >> t::hexDecode() >> t::streamSink(os);
Davide Pesavento6c6e3852019-08-05 20:20:35 -040052 return os.buf();
Junxiao Shi160701a2016-08-30 11:35:25 +000053 }
54 }
Davide Pesavento6c6e3852019-08-05 20:20:35 -040055 catch (const t::Error& e) {
56 NDN_THROW_NESTED(Error(e.what()));
Junxiao Shi160701a2016-08-30 11:35:25 +000057 }
58
Davide Pesavento6c6e3852019-08-05 20:20:35 -040059 NDN_THROW(std::invalid_argument("Unknown IoEncoding " + to_string(encoding)));
60}
61
62optional<Block>
63loadBlock(std::istream& is, IoEncoding encoding)
64{
Junxiao Shi160701a2016-08-30 11:35:25 +000065 try {
Davide Pesavento6c6e3852019-08-05 20:20:35 -040066 return make_optional<Block>(loadBuffer(is, encoding));
Junxiao Shi160701a2016-08-30 11:35:25 +000067 }
Ashlesh Gawandee84d1eb2018-01-04 20:46:44 -060068 catch (const std::invalid_argument&) {
69 return nullopt;
70 }
Davide Pesavento6c6e3852019-08-05 20:20:35 -040071 catch (const std::runtime_error&) {
72 return nullopt;
73 }
Junxiao Shi160701a2016-08-30 11:35:25 +000074}
75
76void
Davide Pesavento6c6e3852019-08-05 20:20:35 -040077saveBuffer(const uint8_t* buf, size_t size, std::ostream& os, IoEncoding encoding)
Junxiao Shi160701a2016-08-30 11:35:25 +000078{
79 namespace t = ndn::security::transform;
80
81 try {
82 switch (encoding) {
83 case NO_ENCODING:
Davide Pesavento6c6e3852019-08-05 20:20:35 -040084 t::bufferSource(buf, size) >> t::streamSink(os);
85 return;
Junxiao Shi4ce0bcf2016-09-03 07:09:03 +000086 case BASE64:
Davide Pesavento6c6e3852019-08-05 20:20:35 -040087 t::bufferSource(buf, size) >> t::base64Encode() >> t::streamSink(os);
88 return;
Junxiao Shi160701a2016-08-30 11:35:25 +000089 case HEX:
Davide Pesavento6c6e3852019-08-05 20:20:35 -040090 t::bufferSource(buf, size) >> t::hexEncode(true) >> t::streamSink(os);
91 return;
Junxiao Shi160701a2016-08-30 11:35:25 +000092 }
93 }
Davide Pesavento6c6e3852019-08-05 20:20:35 -040094 catch (const t::Error& e) {
95 NDN_THROW_NESTED(Error(e.what()));
Junxiao Shi160701a2016-08-30 11:35:25 +000096 }
Davide Pesavento6c6e3852019-08-05 20:20:35 -040097
98 NDN_THROW(std::invalid_argument("Unknown IoEncoding " + to_string(encoding)));
99}
100
101void
102saveBlock(const Block& block, std::ostream& os, IoEncoding encoding)
103{
104 saveBuffer(block.wire(), block.size(), os, encoding);
Junxiao Shi160701a2016-08-30 11:35:25 +0000105}
106
107} // namespace io
108} // namespace ndn