NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
oid.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
23 #include "common.hpp"
24 
25 #include "oid.hpp"
26 
27 #include "../security/cryptopp.hpp"
28 
29 #include <sstream>
30 
31 namespace ndn {
32 
33 using std::string;
34 using std::vector;
35 
36 static const int OID_MAGIC_NUMBER = 40;
37 
38 OID::OID(const char* oid)
39 {
40  construct(oid);
41 }
42 
43 OID::OID(const string& oid)
44 {
45  construct(oid);
46 }
47 
48 void
49 OID::construct(const std::string& oid)
50 {
51  string str = oid + ".";
52 
53  size_t pos = 0;
54  size_t ppos = 0;
55 
56  while (string::npos != pos) {
57  ppos = pos;
58 
59  pos = str.find_first_of('.', pos);
60  if (pos == string::npos)
61  break;
62 
63  m_oid.push_back(atoi(str.substr(ppos, pos - ppos).c_str()));
64 
65  pos++;
66  }
67 }
68 
69 string
71 {
72  std::ostringstream convert;
73 
74  for (vector<int>::const_iterator it = m_oid.begin(); it != m_oid.end(); ++it) {
75  if (it != m_oid.begin())
76  convert << ".";
77  convert << *it;
78  }
79 
80  return convert.str();
81 }
82 
83 bool
84 OID::equal(const OID& oid) const
85 {
86  vector<int>::const_iterator i = m_oid.begin();
87  vector<int>::const_iterator j = oid.m_oid.begin();
88 
89  for (; i != m_oid.end() && j != oid.m_oid.end(); i++, j++) {
90  if (*i != *j)
91  return false;
92  }
93 
94  return (i == m_oid.end() && j == oid.m_oid.end()); // keep parenthesis for readability.
95 }
96 
97 inline void
98 encodeValue(CryptoPP::BufferedTransformation& bt, CryptoPP::word32 v)
99 {
100  using namespace CryptoPP;
101 
102  for (unsigned int i = RoundUpToMultipleOf(STDMAX(7U, BitPrecision(v)), 7U) - 7; i != 0; i -= 7)
103  bt.Put(static_cast<byte>(0x80 | ((v >> i) & 0x7f)));
104  bt.Put(static_cast<byte>(v & 0x7f));
105 }
106 
107 inline size_t
108 decodeValue(CryptoPP::BufferedTransformation& bt, CryptoPP::word32& v)
109 {
110  using namespace CryptoPP;
111 
112  v = 0;
113  size_t i = 0;
114  while (true)
115  {
116  byte b;
117  if (!bt.Get(b))
118  BERDecodeError();
119  i++;
120  if (v >> (8 * sizeof(v) - 7)) // v about to overflow
121  BERDecodeError();
122  v <<= 7;
123  v += b & 0x7f;
124  if ((b & 0x80) == 0)
125  return i;
126  }
127 }
128 
129 void
130 OID::encode(CryptoPP::BufferedTransformation& out) const
131 {
132  using namespace CryptoPP;
133 
134  BOOST_ASSERT(m_oid.size() >= 2);
135 
136  ByteQueue temp;
137  temp.Put(byte(m_oid[0] * OID_MAGIC_NUMBER + m_oid[1]));
138  for (size_t i = 2; i < m_oid.size(); i++)
139  encodeValue(temp, m_oid[i]);
140 
141  out.Put(OBJECT_IDENTIFIER);
142  DERLengthEncode(out, temp.CurrentSize());
143  temp.TransferTo(out);
144 }
145 
146 void
147 OID::decode(CryptoPP::BufferedTransformation& in)
148 {
149  using namespace CryptoPP;
150 
151  byte b;
152  if (!in.Get(b) || b != OBJECT_IDENTIFIER)
153  BERDecodeError();
154 
155  size_t length;
156  if (!BERLengthDecode(in, length) || length < 1)
157  BERDecodeError();
158 
159  if (!in.Get(b))
160  BERDecodeError();
161 
162  length--;
163  m_oid.resize(2);
164  m_oid[0] = b / OID_MAGIC_NUMBER;
165  m_oid[1] = b % OID_MAGIC_NUMBER;
166 
167  while (length > 0)
168  {
169  word32 v;
170  size_t valueLen = decodeValue(in, v);
171  if (valueLen > length)
172  BERDecodeError();
173  m_oid.push_back(v);
174  length -= valueLen;
175  }
176 }
177 
178 namespace oid {
179 const OID RSA("1.2.840.113549.1.1.1");
180 const OID ECDSA("1.2.840.10045.2.1");
181 
182 const OID ATTRIBUTE_NAME("2.5.4.41");
183 }
184 
185 } // namespace ndn
void encodeValue(CryptoPP::BufferedTransformation &bt, CryptoPP::word32 v)
Definition: oid.cpp:98
std::string toString() const
Definition: oid.cpp:70
Copyright (c) 2011-2015 Regents of the University of California.
OID()
Definition: oid.hpp:38
Copyright (c) 2013-2014 Regents of the University of California.
Definition: oid.hpp:29
const OID ATTRIBUTE_NAME("2.5.4.41")
Definition: oid.hpp:105
void decode(CryptoPP::BufferedTransformation &in)
Definition: oid.cpp:147
Definition: oid.hpp:35
size_t decodeValue(CryptoPP::BufferedTransformation &bt, CryptoPP::word32 &v)
Definition: oid.cpp:108
const OID ECDSA("1.2.840.10045.2.1")
Definition: oid.hpp:102
void encode(CryptoPP::BufferedTransformation &out) const
Definition: oid.cpp:130
const OID RSA("1.2.840.113549.1.1.1")
Definition: oid.hpp:101
static const int OID_MAGIC_NUMBER
Definition: oid.cpp:36