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