blob: 7904f10fdaf67737f34b813831a11cdba95c06e3 [file] [log] [blame]
Alexander Afanasyev303b3502014-01-07 12:02:59 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * 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 Afanasyev303b3502014-01-07 12:02:59 -080013 */
14
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080015#include "face.hpp"
16#include "encoding/block.hpp"
Alexander Afanasyev303b3502014-01-07 12:02:59 -080017
18#include <iomanip>
19#include <fstream>
20
Alexander Afanasyev4b456282014-02-13 00:34:34 -080021const uint32_t TLV_DICT_SIZE = 30;
Alexander Afanasyev303b3502014-01-07 12:02:59 -080022
23const std::string TLV_DICT[TLV_DICT_SIZE] = {
24 "RESERVED", // = 0
Alexander Afanasyev4b456282014-02-13 00:34:34 -080025 "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 Afanasyev303b3502014-01-07 12:02:59 -080054};
55
56void
57printTypeInfo(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 Afanasyevdfa52c42014-04-24 21:10:11 -070079
Alexander Afanasyev303b3502014-01-07 12:02:59 -080080void
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -080081BlockPrinter(const ndn::Block& block, const std::string& indent="")
Alexander Afanasyev303b3502014-01-07 12:02:59 -080082{
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 Afanasyev29e5c3d2014-02-11 00:01:10 -080097 if (block.elements().empty())
Alexander Afanasyev303b3502014-01-07 12:02:59 -080098 {
99 std::cout << " [[";
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800100 ndn::name::Component(block.value(), block.value_size()).toEscapedString(std::cout);
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800101 std::cout<< "]]";
102 }
103 std::cout << std::endl;
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -0700104
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800105 for(ndn::Block::element_const_iterator i = block.elements_begin();
106 i != block.elements_end();
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800107 ++i)
108 {
109 BlockPrinter(*i, indent+" ");
110 }
111}
112
113void
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800114HexPrinter(const ndn::Block& block, const std::string &indent="")
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800115{
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 Afanasyevdfa52c42014-04-24 21:10:11 -0700123
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800124 if (block.elements_size() == 0 && block.value_size() > 0)
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800125 {
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 Afanasyev29e5c3d2014-02-11 00:01:10 -0800136 for(ndn::Block::element_const_iterator i = block.elements_begin();
137 i != block.elements_end();
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800138 ++i)
139 {
140 HexPrinter(*i, indent+" ");
141 }
142 }
143}
144
145int 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 Afanasyevdfa52c42014-04-24 21:10:11 -0700170
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800171 return 0;
172}