]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/smux/types.cpp
Other comparsion functions for OID added
[stg.git] / projects / stargazer / plugins / other / smux / types.cpp
1 #include <stdexcept>
2 #include <algorithm>
3 #include <sstream>
4
5 #include "types.h"
6
7 bool StringToArcs(const char * str, size_t length, std::vector<unsigned> & arcs)
8 {
9 unsigned a[1024];
10 if (length == 0)
11     return false;
12 const char * left = str;
13 if (*left == '.')
14     ++left;
15 size_t arcPos = 0;
16 while ((left - str) < length)
17     {
18     char * pos = NULL;
19     unsigned arc = strtoul(left, &pos, 10);
20     if (pos == left)
21         return false;
22     a[arcPos++] = arc;
23     if (arcPos >= 1024)
24         return false;
25     left = pos + 1;
26     }
27
28 std::vector<unsigned> newArcs(a, a + arcPos);
29 arcs.swap(newArcs);
30 return true;
31 }
32
33 OID::OID(const std::string & str)
34 {
35 if (!StringToArcs(str.c_str(), str.length(), arcs))
36     throw std::runtime_error("Invalid oid");
37 }
38
39 OID::OID(const char * str, size_t length)
40 {
41 if (!StringToArcs(str, length, arcs))
42     throw std::runtime_error("Invalid oid");
43 }
44
45 OID::OID(const std::vector<unsigned> & a)
46     : arcs(a)
47 {
48 }
49
50 OID::OID(const unsigned * a, size_t length)
51 {
52 std::vector<unsigned> newArcs(a, a + length);
53 arcs.swap(newArcs);
54 }
55
56 OID::OID(OBJECT_IDENTIFIER_t * oid)
57 {
58 unsigned a[1024];
59 int count = OBJECT_IDENTIFIER_get_arcs(oid, a, sizeof(a[0]), 1024);
60
61 if (count > 1024)
62     throw std::runtime_error("OID is too long");
63
64 std::vector<unsigned> newArcs(a, a + count);
65 arcs.swap(newArcs);
66 }
67
68 OID::OID(const OID & rvalue)
69     : arcs(rvalue.arcs)
70 {
71 }
72
73 OID::~OID()
74 {
75 }
76
77 std::string OID::ToString() const
78 {
79 std::stringstream stream;
80 for (size_t i = 0; i < arcs.size(); ++i)
81     stream << "." << arcs[i];
82 return stream.str();
83 }
84
85 void OID::ToOID(OBJECT_IDENTIFIER_t * oid) const
86 {
87 OBJECT_IDENTIFIER_set_arcs(oid, &arcs.front(), sizeof(unsigned), arcs.size());
88 }
89
90 OID & OID::operator=(const OID & rvalue)
91 {
92 arcs = rvalue.arcs;
93 return *this;
94 }
95
96 bool OID::operator==(const OID & rvalue) const
97 {
98 if (arcs.size() != rvalue.arcs.size())
99     return false;
100 for (size_t i = 0; i < arcs.size(); ++i)
101     if (arcs[i] != rvalue.arcs[i])
102         return false;
103 return true;
104 }
105
106 bool OID::operator<(const OID & rvalue) const
107 {
108 for (size_t i = 0; i < std::min(arcs.size(), rvalue.arcs.size()); ++i)
109     if (arcs[i] > rvalue.arcs[i])
110         return false;
111 if (rvalue.arcs.size() < arcs.size())
112     return false;
113 return true;
114 }
115
116 std::ostream & operator<<(std::ostream & stream, const OID & oid)
117 {
118 for (size_t i = 0; i < oid.arcs.size(); ++i)
119     stream << "." << oid.arcs[i];
120 return stream;
121 }