blob: 06282af2646bd2fd83bd4b2da44538caf538dc43 [file] [log] [blame]
Yingdi Yub4be64a2013-10-13 17:24:50 -07001/* -*- 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
15using namespace std;
16using namespace ndn;
17
18Profile::Profile(const Name& identityName)
19 : m_identityName(identityName)
20{}
21
22Profile::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
34Profile::Profile(const Profile& profile)
35 : m_identityName(profile.m_identityName)
36 , m_entries(profile.m_entries)
37{}
38
39void
40Profile::setProfileEntry(const string& profileType,
41 const Blob& profileValue)
42{ m_entries[profileType] = profileValue; }
43
44Ptr<const Blob>
Yingdi Yudbeb8e22013-10-14 09:36:31 -070045Profile::getProfileEntry(const string& profileType) const
Yingdi Yub4be64a2013-10-13 17:24:50 -070046{
47 if(m_entries.find(profileType) != m_entries.end())
Yingdi Yudbeb8e22013-10-14 09:36:31 -070048 return Ptr<Blob>(new Blob(m_entries.at(profileType).buf(), m_entries.at(profileType).size()));
Yingdi Yub4be64a2013-10-13 17:24:50 -070049
50 return NULL;
51}
52
53Ptr<Blob>
54Profile::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
79Ptr<Profile>
80Profile::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}