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