blob: 80a4f5d502ab644d3d59f3ef50d4ae07cac42386 [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);
Jeff Thompson43a57b12013-10-22 16:25:38 -070081
Jeff Thompsone37aa212013-10-17 17:53:23 -070082 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);
Jeff Thompson43a57b12013-10-22 16:25:38 -070091
Jeff Thompsone37aa212013-10-17 17:53:23 -070092 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
Jeff Thompson3d5096c2013-12-11 16:40:05 -0800103 DerNodePtrList& children = derSequence.getChildren();
104 DerNodePtrList::iterator it = children.begin();
Jeff Thompsone37aa212013-10-17 17:53:23 -0700105 for(; it != children.end(); it++)
Jeff Thompson43a57b12013-10-22 16:25:38 -0700106 (*it)->accept(*this, indent + " | ");
Jeff Thompsone37aa212013-10-17 17:53:23 -0700107}
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}