blob: 1b94630f384c97a2807b2bb9994a01fac880ed97 [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 Afanasyev6835ad82014-02-12 10:07:20 -080014const uint32_t TLV_DICT_SIZE = 26;
Alexander Afanasyev303b3502014-01-07 12:02:59 -080015
16const std::string TLV_DICT[TLV_DICT_SIZE] = {
17 "RESERVED", // = 0
Alexander Afanasyev6835ad82014-02-12 10:07:20 -080018 "Name", // = 1,
19 "NameComponent", // = 2,
20 "Interest", // = 3,
21 "Data", // = 4,
Alexander Afanasyev303b3502014-01-07 12:02:59 -080022 "Selectors", // = 5,
23 "Nonce", // = 6,
24 "Scope", // = 7,
25 "InterestLifetime", // = 8,
26 "MinSuffixComponents", // = 9,
27 "MaxSuffixComponents", // = 10,
28 "PublisherPublicKeyLocator", // = 11,
29 "Exclude", // = 12,
30 "ChildSelector", // = 13,
31 "MustBeFresh", // = 14,
32 "Any", // = 15,
33 "MetaInfo", // = 16,
34 "Content", // = 17,
35 "SignatureInfo", // = 18,
36 "SignatureValue", // = 19,
37 "ContentType", // = 20,
38 "FreshnessPeriod", // = 21,
39 "SignatureType", // = 22,
40 "KeyLocator", // = 23,
41 "KeyLocatorDigest", // = 24
Alexander Afanasyev6835ad82014-02-12 10:07:20 -080042 "FinalBlockId" // = 25
Alexander Afanasyev303b3502014-01-07 12:02:59 -080043};
44
45void
46printTypeInfo(uint32_t type)
47{
48 std::cout << type << " (";
49
50 if (type < TLV_DICT_SIZE) {
51 std::cout << TLV_DICT[type];
52 }
53 else if (TLV_DICT_SIZE <= type && type < 128) {
54 std::cout << "RESERVED_1";
55 }
56 else if (128 <= type && type < 253) {
57 std::cout << "APP_TAG_1";
58 }
59 else if (253 <= type && type < 32767) {
60 std::cout << "RESERVED_3";
61 }
62 else {
63 std::cout << "APP_TAG_3";
64 }
65 std::cout << ")";
66}
67
68
69void
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -080070BlockPrinter(const ndn::Block& block, const std::string& indent="")
Alexander Afanasyev303b3502014-01-07 12:02:59 -080071{
72 std::cout << indent;
73 printTypeInfo(block.type());
74 std::cout << " (size: " << block.value_size() << ")";
75
76 try {
77 // if (block.type() != ndn::Tlv::Content && block.type() != ndn::Tlv::SignatureValue)
78 block.parse();
79 }
80 catch(ndn::Tlv::Error &e) {
81 // pass (e.g., leaf block reached)
82
83 // @todo: Figure how to deterministically figure out that value is not recursive TLV block
84 }
85
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -080086 if (block.elements().empty())
Alexander Afanasyev303b3502014-01-07 12:02:59 -080087 {
88 std::cout << " [[";
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -080089 ndn::name::Component(block.value(), block.value_size()).toEscapedString(std::cout);
Alexander Afanasyev303b3502014-01-07 12:02:59 -080090 std::cout<< "]]";
91 }
92 std::cout << std::endl;
93
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -080094 for(ndn::Block::element_const_iterator i = block.elements_begin();
95 i != block.elements_end();
Alexander Afanasyev303b3502014-01-07 12:02:59 -080096 ++i)
97 {
98 BlockPrinter(*i, indent+" ");
99 }
100}
101
102void
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800103HexPrinter(const ndn::Block& block, const std::string &indent="")
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800104{
105 std::cout << indent;
106 for (ndn::Buffer::const_iterator i = block.begin (); i != block.value_begin(); ++i)
107 {
108 std::cout << "0x" << std::noshowbase << std::hex << std::setw(2) << std::setfill('0') << (int)*i;
109 std::cout << ", ";
110 }
111 std::cout << "\n";
112
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800113 if (block.elements_size() == 0 && block.value_size() > 0)
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800114 {
115 std::cout << indent << " ";
116 for (ndn::Buffer::const_iterator i = block.value_begin (); i != block.value_end(); ++i)
117 {
118 std::cout << "0x" << std::noshowbase << std::hex << std::setw(2) << std::setfill('0') << (int)*i;
119 std::cout << ", ";
120 }
121 std::cout << "\n";
122 }
123 else
124 {
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800125 for(ndn::Block::element_const_iterator i = block.elements_begin();
126 i != block.elements_end();
Alexander Afanasyev303b3502014-01-07 12:02:59 -0800127 ++i)
128 {
129 HexPrinter(*i, indent+" ");
130 }
131 }
132}
133
134int main(int argc, const char *argv[])
135{
136 unsigned char buf[9000];
137 std::streamsize s = 0;
138 if (argc == 1 ||
139 (argc == 2 && std::string(argv[1]) == "-"))
140 {
141 std::cin.read(reinterpret_cast<char*>(buf), 9000);
142 s = std::cin.gcount();
143 }
144 else
145 {
146 std::ifstream file(argv[1]);
147 file.read(reinterpret_cast<char*>(buf), 9000);
148 s = file.gcount();
149 }
150
151 try {
152 ndn::Block block(buf, s);
153 BlockPrinter(block, "");
154 // HexPrinter(block, "");
155 }
156 catch(std::exception &e) {
157 std::cerr << "ERROR: "<< e.what() << std::endl;
158 }
159
160 return 0;
161}