Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | 73e3004 | 2015-09-17 17:09:51 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2015 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. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
Alexander Afanasyev | 09c613f | 2014-01-29 00:23:58 -0800 | [diff] [blame] | 24 | #include "face.hpp" |
| 25 | #include "encoding/block.hpp" |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 26 | |
| 27 | #include <iomanip> |
| 28 | #include <fstream> |
Alexander Afanasyev | 4cd8d73 | 2014-12-30 13:16:03 -0800 | [diff] [blame] | 29 | #include <map> |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 30 | |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 31 | namespace ndn { |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 32 | |
Alexander Afanasyev | 4cd8d73 | 2014-12-30 13:16:03 -0800 | [diff] [blame] | 33 | std::map<uint32_t, std::string> TLV_DICT = { |
| 34 | {tlv::Interest , "Interest"}, |
| 35 | {tlv::Data , "Data"}, |
| 36 | {tlv::Name , "Name"}, |
| 37 | {tlv::NameComponent , "NameComponent"}, |
| 38 | {tlv::ImplicitSha256DigestComponent, "ImplicitSha256DigestComponent"}, |
| 39 | {tlv::Selectors , "Selectors"}, |
| 40 | {tlv::Nonce , "Nonce"}, |
Alexander Afanasyev | 4cd8d73 | 2014-12-30 13:16:03 -0800 | [diff] [blame] | 41 | {tlv::InterestLifetime , "InterestLifetime"}, |
| 42 | {tlv::MinSuffixComponents , "MinSuffixComponents"}, |
| 43 | {tlv::MaxSuffixComponents , "MaxSuffixComponents"}, |
| 44 | {tlv::PublisherPublicKeyLocator , "PublisherPublicKeyLocator"}, |
| 45 | {tlv::Exclude , "Exclude"}, |
| 46 | {tlv::ChildSelector , "ChildSelector"}, |
| 47 | {tlv::MustBeFresh , "MustBeFresh"}, |
| 48 | {tlv::Any , "Any"}, |
| 49 | {tlv::MetaInfo , "MetaInfo"}, |
| 50 | {tlv::Content , "Content"}, |
| 51 | {tlv::SignatureInfo , "SignatureInfo"}, |
| 52 | {tlv::SignatureValue , "SignatureValue"}, |
| 53 | {tlv::ContentType , "ContentType"}, |
| 54 | {tlv::FreshnessPeriod , "FreshnessPeriod"}, |
| 55 | {tlv::FinalBlockId , "FinalBlockId"}, |
| 56 | {tlv::SignatureType , "SignatureType"}, |
| 57 | {tlv::KeyLocator , "KeyLocator"}, |
| 58 | {tlv::KeyDigest , "KeyDigest"}, |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | void |
| 62 | printTypeInfo(uint32_t type) |
| 63 | { |
| 64 | std::cout << type << " ("; |
| 65 | |
Alexander Afanasyev | 4cd8d73 | 2014-12-30 13:16:03 -0800 | [diff] [blame] | 66 | if (TLV_DICT.count(type) != 0) { |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 67 | std::cout << TLV_DICT[type]; |
| 68 | } |
Alexander Afanasyev | 4cd8d73 | 2014-12-30 13:16:03 -0800 | [diff] [blame] | 69 | else if (type < tlv::AppPrivateBlock1) { |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 70 | std::cout << "RESERVED_1"; |
| 71 | } |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 72 | else if (tlv::AppPrivateBlock1 <= type && type < 253) { |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 73 | std::cout << "APP_TAG_1"; |
| 74 | } |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 75 | else if (253 <= type && type < tlv::AppPrivateBlock2) { |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 76 | std::cout << "RESERVED_3"; |
| 77 | } |
| 78 | else { |
| 79 | std::cout << "APP_TAG_3"; |
| 80 | } |
| 81 | std::cout << ")"; |
| 82 | } |
| 83 | |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 84 | |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 85 | void |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 86 | BlockPrinter(const Block& block, const std::string& indent = "") |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 87 | { |
| 88 | std::cout << indent; |
| 89 | printTypeInfo(block.type()); |
| 90 | std::cout << " (size: " << block.value_size() << ")"; |
| 91 | |
| 92 | try { |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 93 | // if (block.type() != tlv::Content && block.type() != tlv::SignatureValue) |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 94 | block.parse(); |
| 95 | } |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 96 | catch (tlv::Error& e) { |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 97 | // pass (e.g., leaf block reached) |
| 98 | |
| 99 | // @todo: Figure how to deterministically figure out that value is not recursive TLV block |
| 100 | } |
| 101 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 102 | if (block.elements().empty()) |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 103 | { |
| 104 | std::cout << " [["; |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 105 | name::Component(block.value(), block.value_size()).toUri(std::cout); |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 106 | std::cout<< "]]"; |
| 107 | } |
| 108 | std::cout << std::endl; |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 109 | |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 110 | for (Block::element_const_iterator i = block.elements_begin(); |
Yingdi Yu | 27b0e39 | 2014-05-05 16:27:02 -0700 | [diff] [blame] | 111 | i != block.elements_end(); |
| 112 | ++i) |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 113 | { |
| 114 | BlockPrinter(*i, indent+" "); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | void |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 119 | HexPrinter(const Block& block, const std::string& indent = "") |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 120 | { |
| 121 | std::cout << indent; |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 122 | for (Buffer::const_iterator i = block.begin (); i != block.value_begin(); ++i) |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 123 | { |
Yingdi Yu | 27b0e39 | 2014-05-05 16:27:02 -0700 | [diff] [blame] | 124 | std::cout << "0x"; |
| 125 | std::cout << std::noshowbase << std::hex << std::setw(2) << |
| 126 | std::setfill('0') << static_cast<int>(*i); |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 127 | std::cout << ", "; |
| 128 | } |
| 129 | std::cout << "\n"; |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 130 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 131 | if (block.elements_size() == 0 && block.value_size() > 0) |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 132 | { |
| 133 | std::cout << indent << " "; |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 134 | for (Buffer::const_iterator i = block.value_begin (); i != block.value_end(); ++i) |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 135 | { |
Yingdi Yu | 27b0e39 | 2014-05-05 16:27:02 -0700 | [diff] [blame] | 136 | std::cout << "0x"; |
| 137 | std::cout << std::noshowbase << std::hex << std::setw(2) << |
| 138 | std::setfill('0') << static_cast<int>(*i); |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 139 | std::cout << ", "; |
| 140 | } |
| 141 | std::cout << "\n"; |
| 142 | } |
| 143 | else |
| 144 | { |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 145 | for (Block::element_const_iterator i = block.elements_begin(); |
Yingdi Yu | 27b0e39 | 2014-05-05 16:27:02 -0700 | [diff] [blame] | 146 | i != block.elements_end(); |
| 147 | ++i) |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 148 | { |
| 149 | HexPrinter(*i, indent+" "); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
Yingdi Yu | 27b0e39 | 2014-05-05 16:27:02 -0700 | [diff] [blame] | 154 | void |
| 155 | parseBlocksFromStream(std::istream& is) |
| 156 | { |
| 157 | while (is.peek() != std::char_traits<char>::eof()) { |
| 158 | try { |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 159 | Block block = Block::fromStream(is); |
Yingdi Yu | 27b0e39 | 2014-05-05 16:27:02 -0700 | [diff] [blame] | 160 | BlockPrinter(block, ""); |
| 161 | // HexPrinter(block, ""); |
| 162 | } |
| 163 | catch (std::exception& e) { |
| 164 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | } |
| 169 | |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 170 | } // namespace ndn |
| 171 | |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 172 | int main(int argc, const char *argv[]) |
| 173 | { |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 174 | if (argc == 1 || |
| 175 | (argc == 2 && std::string(argv[1]) == "-")) |
| 176 | { |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 177 | ndn::parseBlocksFromStream(std::cin); |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 178 | } |
| 179 | else |
| 180 | { |
| 181 | std::ifstream file(argv[1]); |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 182 | ndn::parseBlocksFromStream(file); |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 185 | return 0; |
| 186 | } |