blob: 6aad8c5014e348c239cfcbc3a8f39e39fc309f76 [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
Alexander Afanasyev09c613f2014-01-29 00:23:58 -08008#include "face.hpp"
9#include "encoding/block.hpp"
Alexander Afanasyev303b3502014-01-07 12:02:59 -080010
11#include <iomanip>
12#include <fstream>
13
Alexander Afanasyev4b456282014-02-13 00:34:34 -080014const uint32_t TLV_DICT_SIZE = 30;
Alexander Afanasyev303b3502014-01-07 12:02:59 -080015
16const std::string TLV_DICT[TLV_DICT_SIZE] = {
17 "RESERVED", // = 0
Alexander Afanasyev4b456282014-02-13 00:34:34 -080018 "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 Afanasyev303b3502014-01-07 12:02:59 -080047};
48
49void
50printTypeInfo(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
73void
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -080074BlockPrinter(const ndn::Block& block, const std::string& indent="")
Alexander Afanasyev303b3502014-01-07 12:02:59 -080075{
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 Afanasyev29e5c3d2014-02-11 00:01:10 -080090 if (block.elements().empty())
Alexander Afanasyev303b3502014-01-07 12:02:59 -080091 {
92 std::cout << " [[";
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -080093 ndn::name::Component(block.value(), block.value_size()).toEscapedString(std::cout);
Alexander Afanasyev303b3502014-01-07 12:02:59 -080094 std::cout<< "]]";
95 }
96 std::cout << std::endl;
97
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -080098 for(ndn::Block::element_const_iterator i = block.elements_begin();
99 i != block.elements_end();
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800100 ++i)
101 {
102 BlockPrinter(*i, indent+" ");
103 }
104}
105
106void
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800107HexPrinter(const ndn::Block& block, const std::string &indent="")
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800108{
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 Afanasyev29e5c3d2014-02-11 00:01:10 -0800117 if (block.elements_size() == 0 && block.value_size() > 0)
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800118 {
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 Afanasyev29e5c3d2014-02-11 00:01:10 -0800129 for(ndn::Block::element_const_iterator i = block.elements_begin();
130 i != block.elements_end();
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800131 ++i)
132 {
133 HexPrinter(*i, indent+" ");
134 }
135 }
136}
137
138int 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}