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 |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [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 | } |
| 91 | catch(ndn::Tlv::Error &e) { |
| 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 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 105 | for(ndn::Block::element_const_iterator i = block.elements_begin(); |
| 106 | i != block.elements_end(); |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 107 | ++i) |
| 108 | { |
| 109 | BlockPrinter(*i, indent+" "); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [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 | { |
| 119 | std::cout << "0x" << std::noshowbase << std::hex << std::setw(2) << std::setfill('0') << (int)*i; |
| 120 | std::cout << ", "; |
| 121 | } |
| 122 | std::cout << "\n"; |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 123 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 124 | if (block.elements_size() == 0 && block.value_size() > 0) |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 125 | { |
| 126 | std::cout << indent << " "; |
| 127 | for (ndn::Buffer::const_iterator i = block.value_begin (); i != block.value_end(); ++i) |
| 128 | { |
| 129 | std::cout << "0x" << std::noshowbase << std::hex << std::setw(2) << std::setfill('0') << (int)*i; |
| 130 | std::cout << ", "; |
| 131 | } |
| 132 | std::cout << "\n"; |
| 133 | } |
| 134 | else |
| 135 | { |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 136 | for(ndn::Block::element_const_iterator i = block.elements_begin(); |
| 137 | i != block.elements_end(); |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 138 | ++i) |
| 139 | { |
| 140 | HexPrinter(*i, indent+" "); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | int main(int argc, const char *argv[]) |
| 146 | { |
| 147 | unsigned char buf[9000]; |
| 148 | std::streamsize s = 0; |
| 149 | if (argc == 1 || |
| 150 | (argc == 2 && std::string(argv[1]) == "-")) |
| 151 | { |
| 152 | std::cin.read(reinterpret_cast<char*>(buf), 9000); |
| 153 | s = std::cin.gcount(); |
| 154 | } |
| 155 | else |
| 156 | { |
| 157 | std::ifstream file(argv[1]); |
| 158 | file.read(reinterpret_cast<char*>(buf), 9000); |
| 159 | s = file.gcount(); |
| 160 | } |
| 161 | |
| 162 | try { |
| 163 | ndn::Block block(buf, s); |
| 164 | BlockPrinter(block, ""); |
| 165 | // HexPrinter(block, ""); |
| 166 | } |
| 167 | catch(std::exception &e) { |
| 168 | std::cerr << "ERROR: "<< e.what() << std::endl; |
| 169 | } |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 170 | |
Alexander Afanasyev | 303b350 | 2014-01-07 12:02:59 -0800 | [diff] [blame] | 171 | return 0; |
| 172 | } |