Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 2 | /** |
Junxiao Shi | 2d4be25 | 2016-08-17 04:29:00 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 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. |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #ifndef NDN_UTIL_IO_HPP |
| 23 | #define NDN_UTIL_IO_HPP |
| 24 | |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 25 | #include "../encoding/block.hpp" |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 26 | #include "../encoding/buffer-stream.hpp" |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame^] | 27 | #include "../security/transform.hpp" |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 28 | |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 29 | #include <iostream> |
| 30 | #include <fstream> |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 31 | |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 32 | namespace ndn { |
| 33 | namespace io { |
| 34 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 35 | class Error : public std::runtime_error |
| 36 | { |
| 37 | public: |
| 38 | explicit |
| 39 | Error(const std::string& what) |
| 40 | : std::runtime_error(what) |
| 41 | { |
| 42 | } |
| 43 | }; |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 44 | |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame^] | 45 | /** \brief indicates how a file or stream is encoded |
| 46 | */ |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 47 | enum IoEncoding { |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame^] | 48 | NO_ENCODING, ///< binary without encoding |
| 49 | BASE_64, ///< base64 encoding |
| 50 | HEX ///< uppercase hexadecimal encoding |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 51 | }; |
| 52 | |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame^] | 53 | /** \brief loads a TLV element from a stream |
| 54 | * \tparam T type of TLV element; T must be WireDecodable, |
| 55 | * and must have a Error nested type |
| 56 | * \return the TLV element, or nullptr if an error occurs |
| 57 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 58 | template<typename T> |
| 59 | shared_ptr<T> |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 60 | load(std::istream& is, IoEncoding encoding = BASE_64) |
| 61 | { |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame^] | 62 | OBufferStream os; |
Junxiao Shi | 2d4be25 | 2016-08-17 04:29:00 +0000 | [diff] [blame] | 63 | try { |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame^] | 64 | namespace t = ndn::security::transform; |
Junxiao Shi | 2d4be25 | 2016-08-17 04:29:00 +0000 | [diff] [blame] | 65 | switch (encoding) { |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame^] | 66 | case NO_ENCODING: |
| 67 | t::streamSource(is) >> t::streamSink(os); |
Junxiao Shi | 2d4be25 | 2016-08-17 04:29:00 +0000 | [diff] [blame] | 68 | break; |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame^] | 69 | case BASE_64: |
| 70 | t::streamSource(is) >> t::base64Decode() >> t::streamSink(os); |
Junxiao Shi | 2d4be25 | 2016-08-17 04:29:00 +0000 | [diff] [blame] | 71 | break; |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame^] | 72 | case HEX: |
| 73 | t::streamSource(is) >> t::hexDecode() >> t::streamSink(os); |
Junxiao Shi | 2d4be25 | 2016-08-17 04:29:00 +0000 | [diff] [blame] | 74 | break; |
Junxiao Shi | 2d4be25 | 2016-08-17 04:29:00 +0000 | [diff] [blame] | 75 | default: |
| 76 | return nullptr; |
| 77 | } |
Junxiao Shi | 2d4be25 | 2016-08-17 04:29:00 +0000 | [diff] [blame] | 78 | } |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame^] | 79 | catch (const ndn::security::transform::Error&) { |
Junxiao Shi | 2d4be25 | 2016-08-17 04:29:00 +0000 | [diff] [blame] | 80 | return nullptr; |
| 81 | } |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame^] | 82 | |
| 83 | try { |
| 84 | auto obj = make_shared<T>(); |
| 85 | obj->wireDecode(Block(os.buf())); |
| 86 | return obj; |
| 87 | } |
| 88 | catch (const typename T::Error& e) { |
Junxiao Shi | 2d4be25 | 2016-08-17 04:29:00 +0000 | [diff] [blame] | 89 | return nullptr; |
| 90 | } |
| 91 | catch (const tlv::Error& e) { |
| 92 | return nullptr; |
| 93 | } |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame^] | 96 | /** \brief loads a TLV element from a file |
| 97 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 98 | template<typename T> |
| 99 | shared_ptr<T> |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame^] | 100 | load(const std::string& filename, IoEncoding encoding = BASE_64) |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 101 | { |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame^] | 102 | std::ifstream is(filename); |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 103 | return load<T>(is, encoding); |
| 104 | } |
| 105 | |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame^] | 106 | /** \brief saves a TLV element to a stream |
| 107 | * \tparam T type of TLV element; T must be WireEncodable, |
| 108 | * and must have a Error nested type |
| 109 | * \throw Error error during encoding or saving |
| 110 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 111 | template<typename T> |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 112 | void |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame^] | 113 | save(const T& obj, std::ostream& os, IoEncoding encoding = BASE_64) |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 114 | { |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame^] | 115 | Block block; |
Junxiao Shi | 2d4be25 | 2016-08-17 04:29:00 +0000 | [diff] [blame] | 116 | try { |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame^] | 117 | block = obj.wireEncode(); |
Junxiao Shi | 2d4be25 | 2016-08-17 04:29:00 +0000 | [diff] [blame] | 118 | } |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame^] | 119 | catch (const typename T::Error& e) { |
Junxiao Shi | 2d4be25 | 2016-08-17 04:29:00 +0000 | [diff] [blame] | 120 | BOOST_THROW_EXCEPTION(Error(e.what())); |
| 121 | } |
| 122 | catch (const tlv::Error& e) { |
| 123 | BOOST_THROW_EXCEPTION(Error(e.what())); |
| 124 | } |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame^] | 125 | |
| 126 | try { |
| 127 | namespace t = ndn::security::transform; |
| 128 | switch (encoding) { |
| 129 | case NO_ENCODING: |
| 130 | t::bufferSource(block.wire(), block.size()) >> t::streamSink(os); |
| 131 | break; |
| 132 | case BASE_64: |
| 133 | t::bufferSource(block.wire(), block.size()) >> t::base64Encode() >> t::streamSink(os); |
| 134 | break; |
| 135 | case HEX: |
| 136 | t::bufferSource(block.wire(), block.size()) >> t::hexEncode(true) >> t::streamSink(os); |
| 137 | break; |
| 138 | default: |
| 139 | BOOST_THROW_EXCEPTION(Error("unrecognized IoEncoding")); |
| 140 | } |
| 141 | } |
| 142 | catch (const ndn::security::transform::Error& e) { |
| 143 | BOOST_THROW_EXCEPTION(Error(e.what())); |
| 144 | } |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame^] | 147 | /** \brief saves a TLV element to a file |
| 148 | */ |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 149 | template<typename T> |
| 150 | void |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame^] | 151 | save(const T& obj, const std::string& filename, IoEncoding encoding = BASE_64) |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 152 | { |
Junxiao Shi | f2515a7 | 2016-08-19 02:21:59 +0000 | [diff] [blame^] | 153 | std::ofstream os(filename); |
| 154 | save(obj, os, encoding); |
Yingdi Yu | f50098d | 2014-02-26 14:26:29 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | } // namespace io |
| 158 | } // namespace ndn |
| 159 | |
| 160 | #endif // NDN_UTIL_IO_HPP |