Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -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 | * Yingdi Yu |
| 5 | * |
| 6 | * BSD license, See the LICENSE file for more information |
| 7 | * |
| 8 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 9 | */ |
| 10 | |
| 11 | #include "profile.h" |
| 12 | #include <ndn.cxx/helpers/der/der.h> |
Yingdi Yu | 68aced9 | 2013-10-17 21:13:03 -0700 | [diff] [blame^] | 13 | #include <ndn.cxx/helpers/der/visitor/print-visitor.h> |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 14 | #include <ndn.cxx/helpers/der/visitor/simple-visitor.h> |
Yingdi Yu | 68aced9 | 2013-10-17 21:13:03 -0700 | [diff] [blame^] | 15 | #include "logging.h" |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 16 | |
| 17 | using namespace std; |
| 18 | using namespace ndn; |
| 19 | |
Yingdi Yu | 68aced9 | 2013-10-17 21:13:03 -0700 | [diff] [blame^] | 20 | INIT_LOGGER("Profile"); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 21 | Profile::Profile(const Name& identityName) |
| 22 | : m_identityName(identityName) |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 23 | { |
| 24 | const string& nameString = identityName.toUri(); |
| 25 | Blob identityBlob (nameString.c_str(), nameString.size()); |
| 26 | m_entries[string("IDENTITY")] = identityBlob; |
| 27 | } |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 28 | |
| 29 | Profile::Profile(const Name& identityName, |
| 30 | const string& name, |
| 31 | const string& institution) |
| 32 | : m_identityName(identityName) |
| 33 | { |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 34 | const string& nameString = identityName.toUri(); |
| 35 | Blob identityBlob (nameString.c_str(), nameString.size()); |
| 36 | m_entries[string("IDENTITY")] = identityBlob; |
| 37 | |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 38 | Blob nameBlob (name.c_str(), name.size()); |
| 39 | Blob institutionBlob (institution.c_str(), institution.size()); |
| 40 | |
| 41 | m_entries[string("name")] = nameBlob; |
| 42 | m_entries[string("institution")] = institutionBlob; |
| 43 | } |
| 44 | |
| 45 | Profile::Profile(const Profile& profile) |
| 46 | : m_identityName(profile.m_identityName) |
| 47 | , m_entries(profile.m_entries) |
| 48 | {} |
| 49 | |
| 50 | void |
| 51 | Profile::setProfileEntry(const string& profileType, |
| 52 | const Blob& profileValue) |
| 53 | { m_entries[profileType] = profileValue; } |
| 54 | |
| 55 | Ptr<const Blob> |
Yingdi Yu | 53b8a9c | 2013-10-14 09:36:31 -0700 | [diff] [blame] | 56 | Profile::getProfileEntry(const string& profileType) const |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 57 | { |
| 58 | if(m_entries.find(profileType) != m_entries.end()) |
Yingdi Yu | 53b8a9c | 2013-10-14 09:36:31 -0700 | [diff] [blame] | 59 | return Ptr<Blob>(new Blob(m_entries.at(profileType).buf(), m_entries.at(profileType).size())); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 60 | |
| 61 | return NULL; |
| 62 | } |
| 63 | |
| 64 | Ptr<Blob> |
| 65 | Profile::toDerBlob() const |
| 66 | { |
| 67 | Ptr<der::DerSequence> root = Ptr<der::DerSequence>::Create(); |
| 68 | |
| 69 | Ptr<der::DerPrintableString> identityName = Ptr<der::DerPrintableString>(new der::DerPrintableString(m_identityName.toUri())); |
| 70 | root->addChild(identityName); |
| 71 | |
| 72 | map<string, Blob>::const_iterator it = m_entries.begin(); |
| 73 | for(; it != m_entries.end(); it++) |
| 74 | { |
| 75 | Ptr<der::DerSequence> entry = Ptr<der::DerSequence>::Create(); |
| 76 | Ptr<der::DerPrintableString> type = Ptr<der::DerPrintableString>(new der::DerPrintableString(it->first)); |
| 77 | Ptr<der::DerOctetString> value = Ptr<der::DerOctetString>(new der::DerOctetString(it->second)); |
| 78 | entry->addChild(type); |
| 79 | entry->addChild(value); |
| 80 | root->addChild(entry); |
| 81 | } |
| 82 | |
| 83 | blob_stream blobStream; |
| 84 | OutputIterator & start = reinterpret_cast<OutputIterator &> (blobStream); |
| 85 | root->encode(start); |
| 86 | |
| 87 | return blobStream.buf (); |
| 88 | } |
| 89 | |
| 90 | Ptr<Profile> |
| 91 | Profile::fromDerBlob(const Blob& derBlob) |
| 92 | { |
| 93 | boost::iostreams::stream |
| 94 | <boost::iostreams::array_source> is (derBlob.buf(), derBlob.size()); |
| 95 | |
| 96 | Ptr<der::DerSequence> root = DynamicCast<der::DerSequence>(der::DerNode::parse(reinterpret_cast<InputIterator &>(is))); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 97 | const der::DerNodePtrList & children = root->getChildren(); |
| 98 | der::SimpleVisitor simpleVisitor; |
| 99 | string identityName = boost::any_cast<string>(children[0]->accept(simpleVisitor)); |
| 100 | Ptr<Profile> profile = Ptr<Profile>(new Profile(identityName)); |
| 101 | |
| 102 | for(int i = 1; i < children.size(); i++) |
| 103 | { |
| 104 | Ptr<der::DerSequence> entry = DynamicCast<der::DerSequence>(children[i]); |
Yingdi Yu | 68aced9 | 2013-10-17 21:13:03 -0700 | [diff] [blame^] | 105 | const der::DerNodePtrList & tuple = entry->getChildren(); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 106 | string type = boost::any_cast<string>(tuple[0]->accept(simpleVisitor)); |
| 107 | Ptr<Blob> value = boost::any_cast<Ptr<Blob> >(tuple[1]->accept(simpleVisitor)); |
| 108 | profile->setProfileEntry(type, *value); |
| 109 | } |
| 110 | |
| 111 | return profile; |
| 112 | } |