blob: e66482b5603ca930cefe53d5fd518afb1f2242ac [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev303b3502014-01-07 12:02:59 -08002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
Alexander Afanasyev303b3502014-01-07 12:02:59 -080022 */
23
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080024#include "face.hpp"
25#include "encoding/block.hpp"
Alexander Afanasyev303b3502014-01-07 12:02:59 -080026
27#include <iomanip>
28#include <fstream>
29
Alexander Afanasyev4b456282014-02-13 00:34:34 -080030const uint32_t TLV_DICT_SIZE = 30;
Alexander Afanasyev303b3502014-01-07 12:02:59 -080031
32const std::string TLV_DICT[TLV_DICT_SIZE] = {
33 "RESERVED", // = 0
Alexander Afanasyev4b456282014-02-13 00:34:34 -080034 "RESERVED", // = 1
35 "RESERVED", // = 2
36 "RESERVED", // = 3
37 "RESERVED", // = 4
38 "Interest", // = 5,
39 "Data", // = 6,
40 "Name", // = 7,
41 "NameComponent", // = 8,
42 "Selectors", // = 9,
43 "Nonce", // = 10,
44 "Scope", // = 11,
45 "InterestLifetime", // = 12,
46 "MinSuffixComponents", // = 13,
47 "MaxSuffixComponents", // = 14,
48 "PublisherPublicKeyLocator", // = 15,
49 "Exclude", // = 16,
50 "ChildSelector", // = 17,
51 "MustBeFresh", // = 18,
52 "Any", // = 19,
53 "MetaInfo", // = 20,
54 "Content", // = 21,
55 "SignatureInfo", // = 22,
56 "SignatureValue", // = 23,
57 "ContentType", // = 24,
58 "FreshnessPeriod", // = 25,
59 "FinalBlockId" // = 26
60 "SignatureType", // = 27,
61 "KeyLocator", // = 28,
62 "KeyLocatorDigest", // = 29
Alexander Afanasyev303b3502014-01-07 12:02:59 -080063};
64
65void
66printTypeInfo(uint32_t type)
67{
68 std::cout << type << " (";
69
70 if (type < TLV_DICT_SIZE) {
71 std::cout << TLV_DICT[type];
72 }
73 else if (TLV_DICT_SIZE <= type && type < 128) {
74 std::cout << "RESERVED_1";
75 }
76 else if (128 <= type && type < 253) {
77 std::cout << "APP_TAG_1";
78 }
79 else if (253 <= type && type < 32767) {
80 std::cout << "RESERVED_3";
81 }
82 else {
83 std::cout << "APP_TAG_3";
84 }
85 std::cout << ")";
86}
87
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070088
Alexander Afanasyev303b3502014-01-07 12:02:59 -080089void
Yingdi Yu27b0e392014-05-05 16:27:02 -070090BlockPrinter(const ndn::Block& block, const std::string& indent = "")
Alexander Afanasyev303b3502014-01-07 12:02:59 -080091{
92 std::cout << indent;
93 printTypeInfo(block.type());
94 std::cout << " (size: " << block.value_size() << ")";
95
96 try {
97 // if (block.type() != ndn::Tlv::Content && block.type() != ndn::Tlv::SignatureValue)
98 block.parse();
99 }
Yingdi Yu27b0e392014-05-05 16:27:02 -0700100 catch (ndn::Tlv::Error& e) {
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800101 // pass (e.g., leaf block reached)
102
103 // @todo: Figure how to deterministically figure out that value is not recursive TLV block
104 }
105
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800106 if (block.elements().empty())
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800107 {
108 std::cout << " [[";
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700109 ndn::name::Component(block.value(), block.value_size()).toUri(std::cout);
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800110 std::cout<< "]]";
111 }
112 std::cout << std::endl;
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -0700113
Yingdi Yu27b0e392014-05-05 16:27:02 -0700114 for (ndn::Block::element_const_iterator i = block.elements_begin();
115 i != block.elements_end();
116 ++i)
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800117 {
118 BlockPrinter(*i, indent+" ");
119 }
120}
121
122void
Yingdi Yu27b0e392014-05-05 16:27:02 -0700123HexPrinter(const ndn::Block& block, const std::string& indent = "")
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800124{
125 std::cout << indent;
126 for (ndn::Buffer::const_iterator i = block.begin (); i != block.value_begin(); ++i)
127 {
Yingdi Yu27b0e392014-05-05 16:27:02 -0700128 std::cout << "0x";
129 std::cout << std::noshowbase << std::hex << std::setw(2) <<
130 std::setfill('0') << static_cast<int>(*i);
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800131 std::cout << ", ";
132 }
133 std::cout << "\n";
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -0700134
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800135 if (block.elements_size() == 0 && block.value_size() > 0)
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800136 {
137 std::cout << indent << " ";
138 for (ndn::Buffer::const_iterator i = block.value_begin (); i != block.value_end(); ++i)
139 {
Yingdi Yu27b0e392014-05-05 16:27:02 -0700140 std::cout << "0x";
141 std::cout << std::noshowbase << std::hex << std::setw(2) <<
142 std::setfill('0') << static_cast<int>(*i);
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800143 std::cout << ", ";
144 }
145 std::cout << "\n";
146 }
147 else
148 {
Yingdi Yu27b0e392014-05-05 16:27:02 -0700149 for (ndn::Block::element_const_iterator i = block.elements_begin();
150 i != block.elements_end();
151 ++i)
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800152 {
153 HexPrinter(*i, indent+" ");
154 }
155 }
156}
157
Yingdi Yu27b0e392014-05-05 16:27:02 -0700158void
159parseBlocksFromStream(std::istream& is)
160{
161 while (is.peek() != std::char_traits<char>::eof()) {
162 try {
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700163 ndn::Block block = ndn::Block::fromStream(is);
Yingdi Yu27b0e392014-05-05 16:27:02 -0700164 BlockPrinter(block, "");
165 // HexPrinter(block, "");
166 }
167 catch (std::exception& e) {
168 std::cerr << "ERROR: " << e.what() << std::endl;
169 }
170 }
171
172}
173
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800174int main(int argc, const char *argv[])
175{
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800176 if (argc == 1 ||
177 (argc == 2 && std::string(argv[1]) == "-"))
178 {
Yingdi Yu27b0e392014-05-05 16:27:02 -0700179 parseBlocksFromStream(std::cin);
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800180 }
181 else
182 {
183 std::ifstream file(argv[1]);
Yingdi Yu27b0e392014-05-05 16:27:02 -0700184 parseBlocksFromStream(file);
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800185 }
186
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800187 return 0;
188}