blob: cba5d2d7cbdd546b443a01e55446d7b8f1073baf [file] [log] [blame]
Jeff Thompsone37aa212013-10-17 17:53:23 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Yingdi Yu <yingdi@cs.ucla.edu>
5 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
6 * See COPYING for copyright and distribution information.
7 */
8
9#include "print-visitor.hpp"
10#include "../der.hpp"
11#include <iostream>
12#include <iomanip>
13
14using namespace std;
15
16namespace ndn {
17
18namespace der {
19
20void
21PrintVisitor::visit(DerBool& derBool, ndnboost::any param)
22{
23 const string& indent = ndnboost::any_cast<const string&>(param);
24
25 printData(derBool.getHeader(), indent);
26 printData(derBool.getPayload(), indent + " ");
27}
28
29void
30PrintVisitor::visit(DerInteger& derInteger, ndnboost::any param)
31{
32 const string& indent = ndnboost::any_cast<const string&>(param);
33
34 printData(derInteger.getHeader(), indent);
35 printData(derInteger.getPayload(), indent + " ");
36}
37
38void
39PrintVisitor::visit(DerPrintableString& derPStr, ndnboost::any param)
40{
41 const string& indent = ndnboost::any_cast<const string&>(param);
42
43 printData(derPStr.getHeader(), indent);
44 printData(derPStr.getPayload(), indent + " ");
45}
46
47void
48PrintVisitor::visit(DerBitString& derBStr, ndnboost::any param)
49{
50 const string& indent = ndnboost::any_cast<const string&>(param);
51
52 printData(derBStr.getHeader(), indent);
Jeff Thompson4affbf52013-10-18 14:36:46 -070053 const vector<uint8_t>& payload = derBStr.getPayload();
Jeff Thompsone37aa212013-10-17 17:53:23 -070054 cout << indent << " " << " " << hex << setw(2) << setfill('0') << (int)(uint8_t)payload[0] << endl;
55 printData(payload, indent + " ", 1);
56}
57
58void
59PrintVisitor::visit(DerNull& derNull, ndnboost::any param)
60{
61 const string& indent = ndnboost::any_cast<const string&>(param);
62
63 printData(derNull.getHeader(), indent);
64 printData(derNull.getPayload(), indent + " ");
65
66}
67
68void
69PrintVisitor::visit(DerOctetString& derOStr, ndnboost::any param)
70{
71 const string& indent = ndnboost::any_cast<const string&>(param);
72
73 printData(derOStr.getHeader(), indent);
74 printData(derOStr.getPayload(), indent + " ");
75}
76
77void
78PrintVisitor::visit(DerOid& derOid, ndnboost::any param)
79{
80 const string& indent = ndnboost::any_cast<const string&>(param);
81
82 printData(derOid.getHeader(), indent);
83 printData(derOid.getPayload(), indent + " ");
84
85}
86
87void
88PrintVisitor::visit(DerGtime& derGtime, ndnboost::any param)
89{
90 const string& indent = ndnboost::any_cast<const string&>(param);
91
92 printData(derGtime.getHeader(), indent);
93 printData(derGtime.getPayload(), indent + " ");
94}
95
96void
97PrintVisitor::visit(DerSequence& derSequence, ndnboost::any param)
98{
99 const string& indent = ndnboost::any_cast<const string&>(param);
100
101 printData(derSequence.getHeader(), indent);
102
103 const DerNodePtrList& children = derSequence.getChildren();
104 DerNodePtrList::const_iterator it = children.begin();
105 for(; it != children.end(); it++)
106 (*it)->accept(*this, indent + " | ");
107}
108
109
110void
111PrintVisitor::printData(const vector<uint8_t>& blob, const string& indent, int offset)
112{
113 cout << indent;
114
115 int count = 0;
116 for(int i = offset; i < blob.size(); i++)
117 {
118 cout << " " << hex << setw(2) << setfill('0') << (int)blob[i];
119 count++;
120 if(8 == count)
121 {
122 count = 0;
123 cout << "\n" << indent;
124 }
125 }
126 cout << endl;
127}
128
129} // der
130
131}