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