All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
oid.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
8 #include "common.hpp"
9 
10 #include "oid.hpp"
11 
12 #include "../security/cryptopp.hpp"
13 
14 using namespace std;
15 using namespace CryptoPP;
16 
17 namespace ndn {
18 
19 OID::OID(const char* oid)
20 {
21  construct(oid);
22 }
23 
24 OID::OID(const string& oid)
25 {
26  construct(oid);
27 }
28 
29 void
30 OID::construct(const std::string& oid)
31 {
32  string str = oid + ".";
33 
34  size_t pos = 0;
35  size_t ppos = 0;
36 
37  while (string::npos != pos) {
38  ppos = pos;
39 
40  pos = str.find_first_of('.', pos);
41  if (pos == string::npos)
42  break;
43 
44  m_oid.push_back(atoi(str.substr(ppos, pos - ppos).c_str()));
45 
46  pos++;
47  }
48 }
49 
50 string OID::toString() const
51 {
52  ostringstream convert;
53 
54  for (vector<int>::const_iterator it = m_oid.begin(); it != m_oid.end(); ++it) {
55  if (it != m_oid.begin())
56  convert << ".";
57  convert << *it;
58  }
59 
60  return convert.str();
61 }
62 
63 bool
64 OID::equal(const OID& oid) const
65 {
66  vector<int>::const_iterator i = m_oid.begin();
67  vector<int>::const_iterator j = oid.m_oid.begin();
68 
69  for (; i != m_oid.end () && j != oid.m_oid.end (); i++, j++) {
70  if (*i != *j)
71  return false;
72  }
73 
74  if (i == m_oid.end () && j == oid.m_oid.end ())
75  return true;
76  else
77  return false;
78 }
79 
80 inline void
81 EncodeValue(BufferedTransformation& bt, word32 v)
82 {
83  for (unsigned int i = RoundUpToMultipleOf(STDMAX(7U,BitPrecision(v)), 7U) - 7; i != 0; i -= 7)
84  bt.Put((byte)(0x80 | ((v >> i) & 0x7f)));
85  bt.Put((byte)(v & 0x7f));
86 }
87 
88 inline size_t
89 DecodeValue(BufferedTransformation& bt, word32& v)
90 {
91  v = 0;
92  size_t i = 0;
93  while (true)
94  {
95  byte b;
96  if (!bt.Get(b))
97  BERDecodeError();
98  i++;
99  if (v >> (8*sizeof(v) - 7)) // v about to overflow
100  BERDecodeError();
101  v <<= 7;
102  v += b & 0x7f;
103  if (!(b & 0x80))
104  return i;
105  }
106 }
107 
108 void
109 OID::encode(CryptoPP::BufferedTransformation& out) const
110 {
111  BOOST_ASSERT(m_oid.size() >= 2);
112 
113  ByteQueue temp;
114  temp.Put(byte(m_oid[0] * 40 + m_oid[1]));
115  for (size_t i = 2; i < m_oid.size(); i++)
116  EncodeValue(temp, m_oid[i]);
117 
118  out.Put(OBJECT_IDENTIFIER);
119  DERLengthEncode(out, temp.CurrentSize());
120  temp.TransferTo(out);
121 }
122 
123 void
124 OID::decode(CryptoPP::BufferedTransformation& in)
125 {
126  byte b;
127  if (!in.Get(b) || b != OBJECT_IDENTIFIER)
128  BERDecodeError();
129 
130  size_t length;
131  if (!BERLengthDecode(in, length) || length < 1)
132  BERDecodeError();
133 
134  if (!in.Get(b))
135  BERDecodeError();
136 
137  length--;
138  m_oid.resize(2);
139  m_oid[0] = b / 40;
140  m_oid[1] = b % 40;
141 
142  while (length > 0)
143  {
144  word32 v;
145  size_t valueLen = DecodeValue(in, v);
146  if (valueLen > length)
147  BERDecodeError();
148  m_oid.push_back(v);
149  length -= valueLen;
150  }
151 }
152 
153 } // namespace ndn
size_t DecodeValue(BufferedTransformation &bt, word32 &v)
Definition: oid.cpp:89
std::string toString(const system_clock::TimePoint &timePoint, const std::string &format="%Y-%m-%d %H:%M:%S", const std::locale &locale=std::locale("C"))
Convert time point to string with specified format.
Definition: time.hpp:190
Definition: oid.hpp:20
void EncodeValue(BufferedTransformation &bt, word32 v)
Definition: oid.cpp:81