Junxiao Shi | 160701a | 2016-08-30 11:35:25 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Ashlesh Gawande | e84d1eb | 2018-01-04 20:46:44 -0600 | [diff] [blame] | 2 | /* |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2019 Regents of the University of California. |
Junxiao Shi | 160701a | 2016-08-30 11:35:25 +0000 | [diff] [blame] | 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 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/util/io.hpp" |
| 23 | #include "ndn-cxx/encoding/buffer-stream.hpp" |
Davide Pesavento | 6c6e385 | 2019-08-05 20:20:35 -0400 | [diff] [blame] | 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" |
Junxiao Shi | 160701a | 2016-08-30 11:35:25 +0000 | [diff] [blame] | 32 | |
| 33 | namespace ndn { |
| 34 | namespace io { |
| 35 | |
Davide Pesavento | 6c6e385 | 2019-08-05 20:20:35 -0400 | [diff] [blame] | 36 | shared_ptr<Buffer> |
| 37 | loadBuffer(std::istream& is, IoEncoding encoding) |
Junxiao Shi | 160701a | 2016-08-30 11:35:25 +0000 | [diff] [blame] | 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); |
Davide Pesavento | 6c6e385 | 2019-08-05 20:20:35 -0400 | [diff] [blame] | 46 | return os.buf(); |
Junxiao Shi | 4ce0bcf | 2016-09-03 07:09:03 +0000 | [diff] [blame] | 47 | case BASE64: |
| 48 | t::streamSource(is) >> t::stripSpace("\n") >> t::base64Decode(false) >> t::streamSink(os); |
Davide Pesavento | 6c6e385 | 2019-08-05 20:20:35 -0400 | [diff] [blame] | 49 | return os.buf(); |
Junxiao Shi | 160701a | 2016-08-30 11:35:25 +0000 | [diff] [blame] | 50 | case HEX: |
| 51 | t::streamSource(is) >> t::hexDecode() >> t::streamSink(os); |
Davide Pesavento | 6c6e385 | 2019-08-05 20:20:35 -0400 | [diff] [blame] | 52 | return os.buf(); |
Junxiao Shi | 160701a | 2016-08-30 11:35:25 +0000 | [diff] [blame] | 53 | } |
| 54 | } |
Davide Pesavento | 6c6e385 | 2019-08-05 20:20:35 -0400 | [diff] [blame] | 55 | catch (const t::Error& e) { |
| 56 | NDN_THROW_NESTED(Error(e.what())); |
Junxiao Shi | 160701a | 2016-08-30 11:35:25 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Davide Pesavento | 6c6e385 | 2019-08-05 20:20:35 -0400 | [diff] [blame] | 59 | NDN_THROW(std::invalid_argument("Unknown IoEncoding " + to_string(encoding))); |
| 60 | } |
| 61 | |
| 62 | optional<Block> |
| 63 | loadBlock(std::istream& is, IoEncoding encoding) |
| 64 | { |
Junxiao Shi | 160701a | 2016-08-30 11:35:25 +0000 | [diff] [blame] | 65 | try { |
Davide Pesavento | 6c6e385 | 2019-08-05 20:20:35 -0400 | [diff] [blame] | 66 | return make_optional<Block>(loadBuffer(is, encoding)); |
Junxiao Shi | 160701a | 2016-08-30 11:35:25 +0000 | [diff] [blame] | 67 | } |
Ashlesh Gawande | e84d1eb | 2018-01-04 20:46:44 -0600 | [diff] [blame] | 68 | catch (const std::invalid_argument&) { |
| 69 | return nullopt; |
| 70 | } |
Davide Pesavento | 6c6e385 | 2019-08-05 20:20:35 -0400 | [diff] [blame] | 71 | catch (const std::runtime_error&) { |
| 72 | return nullopt; |
| 73 | } |
Junxiao Shi | 160701a | 2016-08-30 11:35:25 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void |
Davide Pesavento | 6c6e385 | 2019-08-05 20:20:35 -0400 | [diff] [blame] | 77 | saveBuffer(const uint8_t* buf, size_t size, std::ostream& os, IoEncoding encoding) |
Junxiao Shi | 160701a | 2016-08-30 11:35:25 +0000 | [diff] [blame] | 78 | { |
| 79 | namespace t = ndn::security::transform; |
| 80 | |
| 81 | try { |
| 82 | switch (encoding) { |
| 83 | case NO_ENCODING: |
Davide Pesavento | 6c6e385 | 2019-08-05 20:20:35 -0400 | [diff] [blame] | 84 | t::bufferSource(buf, size) >> t::streamSink(os); |
| 85 | return; |
Junxiao Shi | 4ce0bcf | 2016-09-03 07:09:03 +0000 | [diff] [blame] | 86 | case BASE64: |
Davide Pesavento | 6c6e385 | 2019-08-05 20:20:35 -0400 | [diff] [blame] | 87 | t::bufferSource(buf, size) >> t::base64Encode() >> t::streamSink(os); |
| 88 | return; |
Junxiao Shi | 160701a | 2016-08-30 11:35:25 +0000 | [diff] [blame] | 89 | case HEX: |
Davide Pesavento | 6c6e385 | 2019-08-05 20:20:35 -0400 | [diff] [blame] | 90 | t::bufferSource(buf, size) >> t::hexEncode(true) >> t::streamSink(os); |
| 91 | return; |
Junxiao Shi | 160701a | 2016-08-30 11:35:25 +0000 | [diff] [blame] | 92 | } |
| 93 | } |
Davide Pesavento | 6c6e385 | 2019-08-05 20:20:35 -0400 | [diff] [blame] | 94 | catch (const t::Error& e) { |
| 95 | NDN_THROW_NESTED(Error(e.what())); |
Junxiao Shi | 160701a | 2016-08-30 11:35:25 +0000 | [diff] [blame] | 96 | } |
Davide Pesavento | 6c6e385 | 2019-08-05 20:20:35 -0400 | [diff] [blame] | 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); |
Junxiao Shi | 160701a | 2016-08-30 11:35:25 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | } // namespace io |
| 108 | } // namespace ndn |