Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <sstream>
00022 #include "oid.hpp"
00023 #include "exceptions.hpp"
00024
00025
00026 using namespace agentxcpp;
00027 using namespace std;
00028
00029
00030 void oid::parse_string(std::string s)
00031 {
00032
00033 if(s.empty()) return;
00034
00035
00036 std::istringstream ss(s);
00037 uint32_t subid;
00038 char ch;
00039 while(ss)
00040 {
00041
00042 ss >> subid;
00043 if(!ss)
00044 {
00045
00046
00047 throw( inval_param() );
00048 }
00049 push_back(subid);
00050
00051
00052 ss >> ch;
00053 if(!ss)
00054 {
00055
00056 break;
00057 }
00058 if(ch != '.')
00059 {
00060
00061 throw( inval_param() );
00062 }
00063 }
00064 }
00065
00066
00067
00068 oid::oid(std::string s)
00069 {
00070 include = false;
00071
00072
00073 parse_string(s);
00074 }
00075
00076
00077 oid::oid(const oid& o, std::string id)
00078 {
00079
00080 *this = o;
00081
00082
00083 parse_string(id);
00084 }
00085
00086
00087 std::ostream& agentxcpp::operator<<(std::ostream& out, const oid& o)
00088 {
00089
00090 out << ".";
00091
00092
00093 if(o.size() == 0)
00094 {
00095 return out;
00096 }
00097
00098
00099 oid::const_iterator it = o.begin();
00100
00101
00102 out << *it;
00103 it++;
00104
00105
00106 while(it != o.end())
00107 {
00108 out << "." << *it;
00109 it++;
00110 }
00111
00112
00113 return out;
00114 }
00115
00116
00117
00118 data_t oid::serialize() const
00119 {
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134 const int n_subid_idx = 0;
00135 const int prefix_idx = 1;
00136 const int include_idx = 2;
00137 const int reserved_idx = 3;
00138
00139
00140 data_t serialized;
00141 serialized.resize(4);
00142
00143
00144 serialized[reserved_idx] = 0;
00145
00146
00147 serialized[include_idx] = include ? 1 : 0;
00148
00149
00150 oid::const_iterator subid = this->begin();
00151
00152
00153 if( this->size() >= 5 &&
00154 (*this)[0] == 1 &&
00155 (*this)[1] == 3 &&
00156 (*this)[2] == 6 &&
00157 (*this)[3] == 1 &&
00158 (*this)[4] <= 0xff)
00159 {
00160
00161 serialized[prefix_idx] = (*this)[4];
00162 subid += 5;
00163
00164
00165 serialized[n_subid_idx] = this->size() - 5;
00166 }
00167 else
00168 {
00169
00170 serialized[prefix_idx] = 0;
00171
00172
00173 serialized[n_subid_idx] = this->size();
00174 }
00175
00176
00177 while( subid != this->end() )
00178 {
00179 serialized.push_back( (*subid) << 24 & 0xff );
00180 serialized.push_back( (*subid) << 16 & 0xff );
00181 serialized.push_back( (*subid) << 8 & 0xff );
00182 serialized.push_back( (*subid) << 0 & 0xff );
00183 subid++;
00184 }
00185
00186 return serialized;
00187 }
00188
00189 oid::oid(data_t::const_iterator& pos,
00190 const data_t::const_iterator& end,
00191 bool big_endian)
00192 {
00193 if(end - pos < 4)
00194 {
00195 throw(parse_error());
00196 }
00197
00198
00199 int n_subid = *pos++;
00200
00201
00202 int prefix = *pos++;
00203 if( prefix != 0 )
00204 {
00205 this->push_back(1);
00206 this->push_back(3);
00207 this->push_back(6);
00208 this->push_back(1);
00209 this->push_back(prefix);
00210 }
00211
00212
00213 switch( *pos++ )
00214 {
00215 case 0:
00216 include = false;
00217 break;
00218 case 1:
00219 include = true;
00220 break;
00221 default:
00222
00223 throw parse_error();
00224 }
00225
00226
00227 *pos++;
00228
00229
00230 if(end - pos < n_subid * 4)
00231 {
00232 throw(parse_error());
00233 }
00234 uint32_t subid;
00235 for( int i = 0; i < n_subid; i++)
00236 {
00237 if(big_endian)
00238 {
00239
00240 subid = *pos++ << 24;
00241 subid |= *pos++ << 16;
00242 subid |= *pos++ << 8;
00243 subid |= *pos++ << 0;
00244 }
00245 else
00246 {
00247
00248 subid = *pos++ << 0;
00249 subid |= *pos++ << 8;
00250 subid |= *pos++ << 16;
00251 subid |= *pos++ << 24;
00252 }
00253 this->push_back(subid);
00254 }
00255 }
00256
00257
00258 bool oid::operator<(const oid& o) const
00259 {
00260 oid::const_iterator mine, yours;
00261 mine = this->begin();
00262 yours = o.begin();
00263
00264
00265 while( mine != this->end()
00266 && yours != o.end() )
00267 {
00268 if( *mine < *yours )
00269 {
00270
00271 return true;
00272 }
00273 if( *mine > *yours )
00274 {
00275
00276 return false;
00277 }
00278
00279
00280 mine++;
00281 yours++;
00282 }
00283
00284
00285
00286
00287 if( this->size() < o.size() )
00288 {
00289
00290 return true;
00291 }
00292 else
00293 {
00294
00295 return false;
00296 }
00297 }
00298
00299
00300
00301 bool oid::operator==(const oid& o) const
00302 {
00303
00304
00305 if( this->size() != o.size() )
00306 {
00307 return false;
00308 }
00309
00310
00311 oid::const_iterator mine, yours;
00312 mine = this->begin();
00313 yours = o.begin();
00314
00315 while( mine != this->end()
00316 && yours != o.end() )
00317 {
00318 if( *mine != *yours )
00319 {
00320
00321 return false;
00322 }
00323
00324
00325 mine++;
00326 yours++;
00327 }
00328
00329
00330
00331 return true;
00332 }
00333
00334
00335 oid& oid::operator=(const oid& other)
00336 {
00337
00338 this->include = other.include;
00339
00340
00341 vector<uint32_t>::operator=(other);
00342 variable::operator=(other);
00343
00344
00345 return *this;
00346 }
00347
00348
00349 bool oid::contains(const oid& id)
00350 {
00351
00352 if(this->size() > id.size())
00353 {
00354
00355 return false;
00356 }
00357
00358
00359 for(size_type i = 0; i < this->size(); i++)
00360 {
00361 if( (*this)[i] != id[i] )
00362 {
00363
00364 return false;
00365 }
00366 }
00367
00368
00369
00370
00371 return true;
00372 }
00373
00374
00375 bool oid::is_null() const
00376 {
00377 if( this->size() == 0 &&
00378 ! this->include)
00379 {
00380
00381 return true;
00382 }
00383 else
00384 {
00385 return false;
00386 }
00387 }