blob: ab9fcd4f7d756b6414abbce5b6feff5d262d53a0 [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 Afanasyev6486d522014-10-23 14:14:11 -070030namespace ndn {
Alexander Afanasyev303b3502014-01-07 12:02:59 -080031
Alexander Afanasyev6486d522014-10-23 14:14:11 -070032const std::vector<std::string> TLV_DICT = {
Alexander Afanasyev303b3502014-01-07 12:02:59 -080033 "RESERVED", // = 0
Alexander Afanasyev6486d522014-10-23 14:14:11 -070034 "ImplicitSha256DigestComponent", // = 1
Alexander Afanasyev4b456282014-02-13 00:34:34 -080035 "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,
Alexander Afanasyev6486d522014-10-23 14:14:11 -070062 "KeyDigest" // = 29
Alexander Afanasyev303b3502014-01-07 12:02:59 -080063};
64
65void
66printTypeInfo(uint32_t type)
67{
68 std::cout << type << " (";
69
Alexander Afanasyev6486d522014-10-23 14:14:11 -070070 if (type < TLV_DICT.size()) {
Alexander Afanasyev303b3502014-01-07 12:02:59 -080071 std::cout << TLV_DICT[type];
72 }
Alexander Afanasyev6486d522014-10-23 14:14:11 -070073 else if (TLV_DICT.size() <= type && type < tlv::AppPrivateBlock1) {
Alexander Afanasyev303b3502014-01-07 12:02:59 -080074 std::cout << "RESERVED_1";
75 }
Alexander Afanasyev6486d522014-10-23 14:14:11 -070076 else if (tlv::AppPrivateBlock1 <= type && type < 253) {
Alexander Afanasyev303b3502014-01-07 12:02:59 -080077 std::cout << "APP_TAG_1";
78 }
Alexander Afanasyev6486d522014-10-23 14:14:11 -070079 else if (253 <= type && type < tlv::AppPrivateBlock2) {
Alexander Afanasyev303b3502014-01-07 12:02:59 -080080 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
Alexander Afanasyev6486d522014-10-23 14:14:11 -070090BlockPrinter(const 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 {
Alexander Afanasyev6486d522014-10-23 14:14:11 -070097 // if (block.type() != tlv::Content && block.type() != tlv::SignatureValue)
Alexander Afanasyev303b3502014-01-07 12:02:59 -080098 block.parse();
99 }
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700100 catch (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 Afanasyev6486d522014-10-23 14:14:11 -0700109 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
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700114 for (Block::element_const_iterator i = block.elements_begin();
Yingdi Yu27b0e392014-05-05 16:27:02 -0700115 i != block.elements_end();
116 ++i)
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800117 {
118 BlockPrinter(*i, indent+" ");
119 }
120}
121
122void
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700123HexPrinter(const Block& block, const std::string& indent = "")
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800124{
125 std::cout << indent;
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700126 for (Buffer::const_iterator i = block.begin (); i != block.value_begin(); ++i)
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800127 {
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 << " ";
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700138 for (Buffer::const_iterator i = block.value_begin (); i != block.value_end(); ++i)
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800139 {
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 {
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700149 for (Block::element_const_iterator i = block.elements_begin();
Yingdi Yu27b0e392014-05-05 16:27:02 -0700150 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 Afanasyev6486d522014-10-23 14:14:11 -0700163 Block block = 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 Afanasyev6486d522014-10-23 14:14:11 -0700174} // namespace ndn
175
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800176int main(int argc, const char *argv[])
177{
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800178 if (argc == 1 ||
179 (argc == 2 && std::string(argv[1]) == "-"))
180 {
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700181 ndn::parseBlocksFromStream(std::cin);
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800182 }
183 else
184 {
185 std::ifstream file(argv[1]);
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700186 ndn::parseBlocksFromStream(file);
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800187 }
188
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800189 return 0;
190}