blob: 47752ae78008a33764b657b7e84a15d0537c2c5a [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
Yingdi Yu27b0e392014-05-05 16:27:02 -070081BlockPrinter(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 }
Yingdi Yu27b0e392014-05-05 16:27:02 -070091 catch (ndn::Tlv::Error& e) {
Alexander Afanasyev303b3502014-01-07 12:02:59 -080092 // 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 Afanasyev9c578182014-05-14 17:28:28 -0700100 ndn::name::Component(block.value(), block.value_size()).toUri(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
Yingdi Yu27b0e392014-05-05 16:27:02 -0700105 for (ndn::Block::element_const_iterator i = block.elements_begin();
106 i != block.elements_end();
107 ++i)
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800108 {
109 BlockPrinter(*i, indent+" ");
110 }
111}
112
113void
Yingdi Yu27b0e392014-05-05 16:27:02 -0700114HexPrinter(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 {
Yingdi Yu27b0e392014-05-05 16:27:02 -0700119 std::cout << "0x";
120 std::cout << std::noshowbase << std::hex << std::setw(2) <<
121 std::setfill('0') << static_cast<int>(*i);
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800122 std::cout << ", ";
123 }
124 std::cout << "\n";
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -0700125
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800126 if (block.elements_size() == 0 && block.value_size() > 0)
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800127 {
128 std::cout << indent << " ";
129 for (ndn::Buffer::const_iterator i = block.value_begin (); i != block.value_end(); ++i)
130 {
Yingdi Yu27b0e392014-05-05 16:27:02 -0700131 std::cout << "0x";
132 std::cout << std::noshowbase << std::hex << std::setw(2) <<
133 std::setfill('0') << static_cast<int>(*i);
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800134 std::cout << ", ";
135 }
136 std::cout << "\n";
137 }
138 else
139 {
Yingdi Yu27b0e392014-05-05 16:27:02 -0700140 for (ndn::Block::element_const_iterator i = block.elements_begin();
141 i != block.elements_end();
142 ++i)
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800143 {
144 HexPrinter(*i, indent+" ");
145 }
146 }
147}
148
Yingdi Yu27b0e392014-05-05 16:27:02 -0700149void
150parseBlocksFromStream(std::istream& is)
151{
152 while (is.peek() != std::char_traits<char>::eof()) {
153 try {
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700154 ndn::Block block = ndn::Block::fromStream(is);
Yingdi Yu27b0e392014-05-05 16:27:02 -0700155 BlockPrinter(block, "");
156 // HexPrinter(block, "");
157 }
158 catch (std::exception& e) {
159 std::cerr << "ERROR: " << e.what() << std::endl;
160 }
161 }
162
163}
164
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800165int main(int argc, const char *argv[])
166{
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800167 if (argc == 1 ||
168 (argc == 2 && std::string(argv[1]) == "-"))
169 {
Yingdi Yu27b0e392014-05-05 16:27:02 -0700170 parseBlocksFromStream(std::cin);
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800171 }
172 else
173 {
174 std::ifstream file(argv[1]);
Yingdi Yu27b0e392014-05-05 16:27:02 -0700175 parseBlocksFromStream(file);
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800176 }
177
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800178 return 0;
179}