blob: 7f2ba6e8b2470ce49dca8fd4cebcc2ebe574856e [file] [log] [blame]
Yingdi Yu5ff62102013-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)
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070020{
21 const string& nameString = identityName.toUri();
22 Blob identityBlob (nameString.c_str(), nameString.size());
23 m_entries[string("IDENTITY")] = identityBlob;
24}
Yingdi Yu5ff62102013-10-13 17:24:50 -070025
26Profile::Profile(const Name& identityName,
27 const string& name,
28 const string& institution)
29 : m_identityName(identityName)
30{
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070031 const string& nameString = identityName.toUri();
32 Blob identityBlob (nameString.c_str(), nameString.size());
33 m_entries[string("IDENTITY")] = identityBlob;
34
Yingdi Yu5ff62102013-10-13 17:24:50 -070035 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
42Profile::Profile(const Profile& profile)
43 : m_identityName(profile.m_identityName)
44 , m_entries(profile.m_entries)
45{}
46
47void
48Profile::setProfileEntry(const string& profileType,
49 const Blob& profileValue)
50{ m_entries[profileType] = profileValue; }
51
52Ptr<const Blob>
Yingdi Yu53b8a9c2013-10-14 09:36:31 -070053Profile::getProfileEntry(const string& profileType) const
Yingdi Yu5ff62102013-10-13 17:24:50 -070054{
55 if(m_entries.find(profileType) != m_entries.end())
Yingdi Yu53b8a9c2013-10-14 09:36:31 -070056 return Ptr<Blob>(new Blob(m_entries.at(profileType).buf(), m_entries.at(profileType).size()));
Yingdi Yu5ff62102013-10-13 17:24:50 -070057
58 return NULL;
59}
60
61Ptr<Blob>
62Profile::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
87Ptr<Profile>
88Profile::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}