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