Jeff Thompson | e37aa21 | 2013-10-17 17:53:23 -0700 | [diff] [blame] | 1 | /* -*- 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 | |
| 14 | using namespace std; |
| 15 | |
| 16 | namespace ndn { |
| 17 | |
| 18 | namespace der { |
| 19 | |
| 20 | void |
| 21 | PrintVisitor::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 | |
| 29 | void |
| 30 | PrintVisitor::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 | |
| 38 | void |
| 39 | PrintVisitor::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 | |
| 47 | void |
| 48 | PrintVisitor::visit(DerBitString& derBStr, ndnboost::any param) |
| 49 | { |
| 50 | const string& indent = ndnboost::any_cast<const string&>(param); |
| 51 | |
| 52 | printData(derBStr.getHeader(), indent); |
Jeff Thompson | 4affbf5 | 2013-10-18 14:36:46 -0700 | [diff] [blame] | 53 | const vector<uint8_t>& payload = derBStr.getPayload(); |
Jeff Thompson | e37aa21 | 2013-10-17 17:53:23 -0700 | [diff] [blame] | 54 | cout << indent << " " << " " << hex << setw(2) << setfill('0') << (int)(uint8_t)payload[0] << endl; |
| 55 | printData(payload, indent + " ", 1); |
| 56 | } |
| 57 | |
| 58 | void |
| 59 | PrintVisitor::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 | |
| 68 | void |
| 69 | PrintVisitor::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 | |
| 77 | void |
| 78 | PrintVisitor::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 | |
| 87 | void |
| 88 | PrintVisitor::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 | |
| 96 | void |
| 97 | PrintVisitor::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 | |
| 110 | void |
| 111 | PrintVisitor::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 | } |