00001 // OSB library ************************************************ -*- C++ -*- 00016 /* 00017 AUTHOR(S): Stephan Broennimann (vb) 00018 00019 RCS information 00020 $Name: OSB_060808 $ 00021 $Revision: 1.2 $ 00022 00023 License 00024 OSB rating and billing library for communication networks 00025 Copyright (C) 2004, 2005, 2006 OSB systems 00026 00027 This file may be distributed and/or modify under the terms of the 00028 GNU General Public License (GPL) as published by the Free Software 00029 Foundation which is provided in the file LICENSE.GPL included in the 00030 packaging of this file. 00031 00032 The file is distributed in the hope that it will be useful, but WITHOUT 00033 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00034 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 00035 for more details. 00036 00037 Holders of a OSB Commercial License may use this file under the terms 00038 and conditions of this commercial license. 00039 */ 00040 #ifndef _ASN1_H_ 00041 #define _ASN1_H_ 00042 00043 // ************************************************************************ 00044 // included header files 00045 // + standard includes 00046 #include <cstddef> // for size_t 00047 #include <cstdio> // for FILE* 00048 00049 // + local headers 00050 #include "asn1fwd.h" 00051 00052 // + class declarations 00053 namespace OSB_LIB { 00054 class OsbErrors; 00055 } 00056 00057 // ************************************************************************ 00058 // namespace extensions 00059 namespace OSB_LIB { 00060 00061 // ************************************************************************ 00062 // class definitions 00063 00090 class Asn1Tag { 00091 public: 00098 enum TypeClass { 00099 tcUniversal = 0x00, 00100 tcApplication = 0x40, 00101 tcContext = 0x80, 00102 tcPrivate = 0xc0 00103 }; 00104 00106 static const size_t tagNotSet = size_t(-1); 00107 00114 explicit Asn1Tag(size_t val = tagNotSet); 00115 00126 Asn1Tag(TypeClass typeClass, 00127 size_t tagNum, 00128 bool constructed); 00129 00131 size_t value() const; 00133 size_t tagNumber() const; 00135 bool isSet() const; 00137 TypeClass typeClass() const; 00139 bool isConstructed() const; 00141 size_t size() const; 00143 Asn1Byte operator[](size_t i) const; 00144 00146 bool operator==(const Asn1Tag& rhs) const; 00148 bool operator!=(const Asn1Tag& rhs) const; 00150 bool operator<(const Asn1Tag& rhs) const; 00151 00161 size_t read(FILE* in); 00162 00171 size_t get( 00172 const Asn1Byte* data, 00173 size_t dataSize 00174 ); 00175 00181 void encode(Asn1Data& dest) const; 00182 00191 std::string str() const; 00192 00193 private: 00195 size_t value_; 00197 size_t size_; 00198 00199 private: 00201 inline void mkSize(); 00202 }; // class Asn1Tag 00203 00224 class Asn1Len { 00225 public: 00227 static const size_t infinite = static_cast<size_t>(-1); 00228 00229 public: 00231 explicit Asn1Len(size_t len = 0); 00233 const Asn1Len& operator=(size_t len); 00234 00235 public: 00237 void setInfinite(); 00238 00239 public: 00241 operator size_t() const; 00243 bool isInfinite() const; 00245 size_t size() const; 00247 Asn1Byte operator[](size_t i) const; 00248 00258 size_t read(FILE* in); 00259 00268 size_t get( 00269 const Asn1Byte* data, 00270 size_t dataSize 00271 ); 00272 00278 void encode(Asn1Data& dest) const; 00279 00286 std::string str() const; 00287 00288 private: 00290 size_t len_; 00291 }; 00292 } // namespace OSB_LIB 00293 00294 // ************************************************************************ 00295 // inline definitions 00296 namespace OSB_LIB { 00297 // ******************************************************************** 00298 // class Asn1Tag 00299 inline size_t Asn1Tag::value() const 00300 { 00301 return value_; 00302 } 00303 00304 inline bool Asn1Tag::isSet() const 00305 { 00306 return tagNotSet != value_; 00307 } 00308 00309 inline size_t Asn1Tag::size() const 00310 { 00311 return size_; 00312 } 00313 00314 inline Asn1Tag::TypeClass Asn1Tag::typeClass() const 00315 { 00316 Asn1Byte f = (*this)[0]; 00317 return TypeClass(tcPrivate & f); 00318 } 00319 00320 inline bool Asn1Tag::isConstructed() const 00321 { 00322 Asn1Byte f = (*this)[0]; 00323 return 0x20 == (0x20 & f); 00324 } 00325 00326 inline bool Asn1Tag::operator==(const Asn1Tag& rhs) const 00327 { 00328 return value_ == rhs.value_; 00329 } 00330 00331 inline bool Asn1Tag::operator!=(const Asn1Tag& rhs) const 00332 { 00333 return value_ != rhs.value_; 00334 } 00335 00336 inline bool Asn1Tag::operator<(const Asn1Tag& rhs) const 00337 { 00338 return value_ < rhs.value_; 00339 } 00340 00341 // ******************************************************************** 00342 // class Asn1Len 00343 inline Asn1Len::Asn1Len(size_t len) 00344 : len_(len) 00345 { 00346 } 00347 00348 inline const Asn1Len& Asn1Len::operator=(size_t len) 00349 { 00350 len_ = len; 00351 return *this; 00352 } 00353 00354 inline void Asn1Len::setInfinite() 00355 { 00356 len_ = infinite; 00357 } 00358 00359 inline Asn1Len::operator size_t() const 00360 { 00361 return isInfinite() ? 0 : len_; 00362 } 00363 00364 inline bool Asn1Len::isInfinite() const 00365 { 00366 return len_ == infinite; 00367 } 00368 } // namespace OSB_LIB 00369 #endif // #ifndef _ASN1_H_