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 | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 30 | namespace ndn { |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 31 | |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 32 | const std::vector<std::string> TLV_DICT = { |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 33 | "RESERVED", // = 0 |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 34 | "ImplicitSha256DigestComponent", // = 1 |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 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, |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 62 | "KeyDigest" // = 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 | |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 70 | if (type < TLV_DICT.size()) { |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 71 | std::cout << TLV_DICT[type]; |
| 72 | } |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 73 | else if (TLV_DICT.size() <= type && type < tlv::AppPrivateBlock1) { |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 74 | std::cout << "RESERVED_1"; |
| 75 | } |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 76 | else if (tlv::AppPrivateBlock1 <= type && type < 253) { |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 77 | std::cout << "APP_TAG_1"; |
| 78 | } |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 79 | else if (253 <= type && type < tlv::AppPrivateBlock2) { |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 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 |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 90 | BlockPrinter(const 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 { |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 97 | // if (block.type() != tlv::Content && block.type() != tlv::SignatureValue) |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 98 | block.parse(); |
| 99 | } |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 100 | catch (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 | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 109 | 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 | |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 114 | for (Block::element_const_iterator i = block.elements_begin(); |
Yingdi Yu | 27b0e39 | 2014-05-05 16:27:02 -0700 | [diff] [blame] | 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 |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 123 | HexPrinter(const Block& block, const std::string& indent = "") |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 124 | { |
| 125 | std::cout << indent; |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 126 | for (Buffer::const_iterator i = block.begin (); i != block.value_begin(); ++i) |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 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 << " "; |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 138 | 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] | 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 | { |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 149 | for (Block::element_const_iterator i = block.elements_begin(); |
Yingdi Yu | 27b0e39 | 2014-05-05 16:27:02 -0700 | [diff] [blame] | 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 | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 163 | Block block = 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 | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 174 | } // namespace ndn |
| 175 | |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 176 | int main(int argc, const char *argv[]) |
| 177 | { |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 178 | if (argc == 1 || |
| 179 | (argc == 2 && std::string(argv[1]) == "-")) |
| 180 | { |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 181 | ndn::parseBlocksFromStream(std::cin); |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 182 | } |
| 183 | else |
| 184 | { |
| 185 | std::ifstream file(argv[1]); |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 186 | ndn::parseBlocksFromStream(file); |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 187 | } |
| 188 | |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 189 | return 0; |
| 190 | } |