Junxiao Shi | 160701a | 2016-08-30 11:35:25 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2016 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 "io.hpp" |
| 23 | #include "../encoding/buffer-stream.hpp" |
| 24 | #include "../security/transform.hpp" |
| 25 | |
| 26 | namespace ndn { |
| 27 | namespace io { |
| 28 | |
| 29 | optional<Block> |
| 30 | loadBlock(std::istream& is, IoEncoding encoding) |
| 31 | { |
| 32 | namespace t = ndn::security::transform; |
| 33 | |
| 34 | OBufferStream os; |
| 35 | try { |
| 36 | switch (encoding) { |
| 37 | case NO_ENCODING: |
| 38 | t::streamSource(is) >> t::streamSink(os); |
| 39 | break; |
Junxiao Shi | 4ce0bcf | 2016-09-03 07:09:03 +0000 | [diff] [blame^] | 40 | case BASE64: |
| 41 | t::streamSource(is) >> t::stripSpace("\n") >> t::base64Decode(false) >> t::streamSink(os); |
Junxiao Shi | 160701a | 2016-08-30 11:35:25 +0000 | [diff] [blame] | 42 | break; |
| 43 | case HEX: |
| 44 | t::streamSource(is) >> t::hexDecode() >> t::streamSink(os); |
| 45 | break; |
| 46 | default: |
| 47 | return nullopt; |
| 48 | } |
| 49 | } |
| 50 | catch (const t::Error&) { |
| 51 | return nullopt; |
| 52 | } |
| 53 | |
| 54 | try { |
| 55 | return make_optional<Block>(os.buf()); |
| 56 | } |
| 57 | catch (const tlv::Error&) { |
| 58 | return nullopt; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void |
| 63 | saveBlock(const Block& block, std::ostream& os, IoEncoding encoding) |
| 64 | { |
| 65 | namespace t = ndn::security::transform; |
| 66 | |
| 67 | try { |
| 68 | switch (encoding) { |
| 69 | case NO_ENCODING: |
| 70 | t::bufferSource(block.wire(), block.size()) >> t::streamSink(os); |
| 71 | break; |
Junxiao Shi | 4ce0bcf | 2016-09-03 07:09:03 +0000 | [diff] [blame^] | 72 | case BASE64: |
Junxiao Shi | 160701a | 2016-08-30 11:35:25 +0000 | [diff] [blame] | 73 | t::bufferSource(block.wire(), block.size()) >> t::base64Encode() >> t::streamSink(os); |
| 74 | break; |
| 75 | case HEX: |
| 76 | t::bufferSource(block.wire(), block.size()) >> t::hexEncode(true) >> t::streamSink(os); |
| 77 | break; |
| 78 | default: |
| 79 | BOOST_THROW_EXCEPTION(Error("unrecognized IoEncoding")); |
| 80 | } |
| 81 | } |
| 82 | catch (const t::Error& e) { |
| 83 | BOOST_THROW_EXCEPTION(Error(e.what())); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | } // namespace io |
| 88 | } // namespace ndn |