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