blob: be808999597039c08f51df91f0a3e0c5b6b65f85 [file] [log] [blame]
Alexander Afanasyev303b3502014-01-07 12:02:59 -08001/* -*- 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
Yingdi Yu61ec2722014-01-20 14:22:32 -08008#include <ndn-cpp-dev/face.hpp>
9#include <ndn-cpp-dev/encoding/block.hpp>
Alexander Afanasyev303b3502014-01-07 12:02:59 -080010
11#include <iomanip>
12#include <fstream>
13
14const int TLV_DICT_SIZE = 25;
15
16const std::string TLV_DICT[TLV_DICT_SIZE] = {
17 "RESERVED", // = 0
18 "Interest", // = 1,
19 "Data", // = 2,
20 "Name", // = 3,
21 "NameComponent", // = 4,
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
42};
43
44void
45printTypeInfo(uint32_t type)
46{
47 std::cout << type << " (";
48
49 if (type < TLV_DICT_SIZE) {
50 std::cout << TLV_DICT[type];
51 }
52 else if (TLV_DICT_SIZE <= type && type < 128) {
53 std::cout << "RESERVED_1";
54 }
55 else if (128 <= type && type < 253) {
56 std::cout << "APP_TAG_1";
57 }
58 else if (253 <= type && type < 32767) {
59 std::cout << "RESERVED_3";
60 }
61 else {
62 std::cout << "APP_TAG_3";
63 }
64 std::cout << ")";
65}
66
67
68void
69BlockPrinter(ndn::Block &block, const std::string &indent="")
70{
71 std::cout << indent;
72 printTypeInfo(block.type());
73 std::cout << " (size: " << block.value_size() << ")";
74
75 try {
76 // if (block.type() != ndn::Tlv::Content && block.type() != ndn::Tlv::SignatureValue)
77 block.parse();
78 }
79 catch(ndn::Tlv::Error &e) {
80 // pass (e.g., leaf block reached)
81
82 // @todo: Figure how to deterministically figure out that value is not recursive TLV block
83 }
84
85 if (block.getAll().empty())
86 {
87 std::cout << " [[";
88 ndn::Name::toEscapedString(block.value(), block.value_size(), std::cout);
89 std::cout<< "]]";
90 }
91 std::cout << std::endl;
92
93 for(ndn::Block::element_iterator i = block.getAll().begin();
94 i != block.getAll().end();
95 ++i)
96 {
97 BlockPrinter(*i, indent+" ");
98 }
99}
100
101void
102HexPrinter(ndn::Block &block, const std::string &indent="")
103{
104 std::cout << indent;
105 for (ndn::Buffer::const_iterator i = block.begin (); i != block.value_begin(); ++i)
106 {
107 std::cout << "0x" << std::noshowbase << std::hex << std::setw(2) << std::setfill('0') << (int)*i;
108 std::cout << ", ";
109 }
110 std::cout << "\n";
111
112 if (block.getAll().size() == 0 && block.value_size() > 0)
113 {
114 std::cout << indent << " ";
115 for (ndn::Buffer::const_iterator i = block.value_begin (); i != block.value_end(); ++i)
116 {
117 std::cout << "0x" << std::noshowbase << std::hex << std::setw(2) << std::setfill('0') << (int)*i;
118 std::cout << ", ";
119 }
120 std::cout << "\n";
121 }
122 else
123 {
124 for(ndn::Block::element_iterator i = block.getAll().begin();
125 i != block.getAll().end();
126 ++i)
127 {
128 HexPrinter(*i, indent+" ");
129 }
130 }
131}
132
133int main(int argc, const char *argv[])
134{
135 unsigned char buf[9000];
136 std::streamsize s = 0;
137 if (argc == 1 ||
138 (argc == 2 && std::string(argv[1]) == "-"))
139 {
140 std::cin.read(reinterpret_cast<char*>(buf), 9000);
141 s = std::cin.gcount();
142 }
143 else
144 {
145 std::ifstream file(argv[1]);
146 file.read(reinterpret_cast<char*>(buf), 9000);
147 s = file.gcount();
148 }
149
150 try {
151 ndn::Block block(buf, s);
152 BlockPrinter(block, "");
153 // HexPrinter(block, "");
154 }
155 catch(std::exception &e) {
156 std::cerr << "ERROR: "<< e.what() << std::endl;
157 }
158
159 return 0;
160}