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