📄 asn1.h
字号:
/** * Format the AbstractData object using normal ASN.1 value notation into the string. */ std::string asValueNotation() const;#endif};/** Base class for constrained ASN encoding/decoding.*/class ConstrainedObject : public AbstractData{ public: ConstrainedObject(const void* info) : AbstractData(info){} bool extendable() const { return info()->type == ExtendableConstraint; } bool constrained() const { return info()->type != Unconstrained; } unsigned getConstraintType() const { return info()->type; } int getLowerLimit() const { return info()->lowerLimit; } unsigned getUpperLimit() const { return info()->upperLimit; } protected: struct InfoType { CreateFun create; unsigned tag; unsigned type; int lowerLimit; unsigned upperLimit; }; const InfoType* info() const { return static_cast<const InfoType*>(info_);}};/** Class for ASN Null type.*/class Null : public AbstractData , public detail::Allocator<Null>{ Null(const void* info) : AbstractData(info){} public: Null() : AbstractData(&theInfo){} bool isValid() const { return true;} bool isStrictlyValid() const { return true;} Null * clone() const { return static_cast<Null*>(do_clone()); } static AbstractData* create(const void* info); void swap(Null& that) { /* do nothing */} bool operator == (const Null& ) const { return true; } bool operator != (const Null& ) const { return false; } bool operator < (const Null& ) const { return false; } bool operator > (const Null& ) const { return false; } bool operator <= (const Null& ) const { return false; } bool operator >= (const Null& ) const { return false; } ASN1_EXPORT static const InfoType theInfo; static bool equal_type(const ASN1::AbstractData& type) {return type.info() == reinterpret_cast<const ASN1::AbstractData::InfoType*>(&theInfo);} private: virtual int do_compare(const AbstractData& other) const; virtual AbstractData* do_clone() const ; virtual bool do_accept(Visitor&); virtual bool do_accept(ConstVisitor&) const;};/** Class for ASN Boolean type.*/class BOOLEAN : public AbstractData, public detail::Allocator<BOOLEAN>{ protected: BOOLEAN(const void* info); public: typedef bool value_type; typedef bool& reference; typedef bool const_reference; BOOLEAN() : AbstractData(&theInfo), value(false) {} BOOLEAN(bool b , const void* info= &theInfo); BOOLEAN(const BOOLEAN& that); BOOLEAN& operator = (const BOOLEAN& that) { value = that.value; return *this; } BOOLEAN& operator = (bool b) { value = b; return *this; } operator bool() const { return value; } bool isValid() const { return true;} bool isStrictlyValid() const { return true;} BOOLEAN * clone() const { return static_cast<BOOLEAN*>(do_clone()); } static AbstractData* create(const void*); void swap(BOOLEAN& that) { std::swap(value, that.value); } bool operator == (const BOOLEAN& rhs) const { return value == rhs.value ; } bool operator != (const BOOLEAN& rhs) const { return value != rhs.value ; } bool operator < (const BOOLEAN& rhs) const { return value < rhs.value ; } bool operator > (const BOOLEAN& rhs) const { return value > rhs.value ; } bool operator <= (const BOOLEAN& rhs) const { return value <= rhs.value ; } bool operator >= (const BOOLEAN& rhs) const { return value >= rhs.value ; } #if __GNUC__> 2 || __GNUC_MINOR__ > 95 friend bool operator == (bool lhs, const BOOLEAN& rhs) { return lhs == rhs.value ; } friend bool operator != (bool lhs, const BOOLEAN& rhs) { return lhs != rhs.value ; } friend bool operator < (bool lhs, const BOOLEAN& rhs) { return lhs < rhs.value ; } friend bool operator > (bool lhs, const BOOLEAN& rhs) { return lhs > rhs.value ; } friend bool operator <= (bool lhs, const BOOLEAN& rhs) { return lhs <= rhs.value ; } friend bool operator >= (bool lhs, const BOOLEAN& rhs) { return lhs >= rhs.value ; } #endif bool operator == (bool rhs) const { return value == rhs ; } bool operator != (bool rhs) const { return value != rhs ; } bool operator < (bool rhs) const { return value < rhs ; } bool operator > (bool rhs) const { return value > rhs ; } bool operator <= (bool rhs) const { return value <= rhs ; } bool operator >= (bool rhs) const { return value >= rhs ; } ASN1_EXPORT static const InfoType theInfo; static bool equal_type(const ASN1::AbstractData& type) {return type.info() == reinterpret_cast<const ASN1::AbstractData::InfoType*>(&theInfo);} private: virtual int do_compare(const AbstractData& other) const; virtual AbstractData* do_clone() const ; virtual bool do_accept(Visitor&); virtual bool do_accept(ConstVisitor&) const; bool value;};/** Class for ASN Integer type.*/class INTEGER : public ConstrainedObject, public detail::Allocator<INTEGER>{protected: INTEGER(const void* info);public: typedef int int_type; typedef int value_type; typedef int_type& reference; typedef int_type const_reference; INTEGER() : ConstrainedObject(&theInfo), value(0){} INTEGER(int_type val, const void* info = &theInfo); INTEGER(const INTEGER& other); INTEGER& operator = (const INTEGER& val) { setValue(val.value) ; return *this;} INTEGER& operator = (int_type val) { setValue(val); return *this;} int_type getValue() const { return static_cast<int_type>(value);} bool isValid() const { return isStrictValid() || getConstraintType() == ExtendableConstraint; } bool isStrictValid() const; INTEGER* clone() const { return static_cast<INTEGER*>(do_clone()); } static AbstractData* create(const void*); void swap(INTEGER& that) { std::swap(value, that.value); } bool operator == (const INTEGER& rhs) const { return getValue() == rhs.getValue(); } bool operator != (const INTEGER& rhs) const { return getValue() != rhs.getValue(); } bool operator < (const INTEGER& rhs) const { return getValue() < rhs.getValue(); } bool operator > (const INTEGER& rhs) const { return getValue() > rhs.getValue(); } bool operator <= (const INTEGER& rhs) const { return getValue() <= rhs.getValue(); } bool operator >= (const INTEGER& rhs) const { return getValue() >= rhs.getValue(); } #if __GNUC__> 2 || __GNUC_MINOR__ > 95 friend bool operator == (int_type lhs, const INTEGER& rhs) { return lhs == rhs.getValue(); } friend bool operator != (int_type lhs, const INTEGER& rhs) { return lhs != rhs.getValue(); } friend bool operator < (int_type lhs, const INTEGER& rhs) { return lhs < rhs.getValue(); } friend bool operator > (int_type lhs, const INTEGER& rhs) { return lhs > rhs.getValue(); } friend bool operator <= (int_type lhs, const INTEGER& rhs) { return lhs <= rhs.getValue(); } friend bool operator >= (int_type lhs, const INTEGER& rhs) { return lhs >= rhs.getValue(); } #endif bool operator == (int_type rhs) const { return getValue() == rhs; } bool operator != (int_type rhs) const { return getValue() != rhs; } bool operator < (int_type rhs) const { return getValue() < rhs; } bool operator > (int_type rhs) const { return getValue() > rhs; } bool operator <= (int_type rhs) const { return getValue() <= rhs; } bool operator >= (int_type rhs) const { return getValue() >= rhs; } INTEGER& operator += (int_type val) { value += val; return *this; } INTEGER& operator -= (int_type val) { value -= val; return *this; } INTEGER& operator *= (int_type val) { value *= val; return *this; } INTEGER& operator /= (int_type val) { value /= val; return *this; } INTEGER& operator %= (int_type val) { value %= val; return *this; } INTEGER& operator += (const INTEGER& val) { value += val.getValue(); return *this; } INTEGER& operator -= (const INTEGER& val) { value -= val.getValue(); return *this; } INTEGER& operator *= (const INTEGER& val) { value *= val.getValue(); return *this; } INTEGER& operator /= (const INTEGER& val) { value /= val.getValue(); return *this; } INTEGER& operator %= (const INTEGER& val) { value %= val.getValue(); return *this; } INTEGER& operator ++ () { ++ value ; return *this;} INTEGER operator ++ (int) { INTEGER result(*this); ++(*this); return result;} INTEGER& operator -- () { -- value ; return *this;} INTEGER operator -- (int){ INTEGER result(*this); --(*this); return result;} int_type operator + (const INTEGER& rhs) const { int_type t(getValue()); return t+=rhs.getValue();} int_type operator - (const INTEGER& rhs) const { int_type t(getValue()); return t-=rhs.getValue();} int_type operator * (const INTEGER& rhs) const { int_type t(getValue()); return t*=rhs.getValue();} int_type operator / (const INTEGER& rhs) const { int_type t(getValue()); return t/=rhs.getValue();} friend int_type operator + (int_type lhs, const INTEGER& rhs) { int_type t(lhs); return t+=rhs.getValue();} friend int_type operator - (int_type lhs, const INTEGER& rhs) { int_type t(lhs); return t-=rhs.getValue();} friend int_type operator * (int_type lhs, const INTEGER& rhs) { int_type t(lhs); return t*=rhs.getValue();} friend int_type operator / (int_type lhs, const INTEGER& rhs) { int_type t(lhs); return t/=rhs.getValue();} int_type operator + (int_type rhs) { int_type t(getValue()); return t+=rhs;} int_type operator - (int_type rhs) { int_type t(getValue()); return t-=rhs;} int_type operator * (int_type rhs) { int_type t(getValue()); return t*=rhs;} int_type operator / (int_type rhs) { int_type t(getValue()); return t/=rhs;} ASN1_EXPORT static const InfoType theInfo; static bool equal_type(const ASN1::AbstractData& type) {return type.info() == reinterpret_cast<const ASN1::AbstractData::InfoType*>(&theInfo);} protected: void setValue(int_type val) { value = static_cast<unsigned>(val); } unsigned value; private: virtual int do_compare(const AbstractData& other) const; virtual AbstractData* do_clone() const ; virtual bool do_accept(Visitor&); virtual bool do_accept(ConstVisitor&) const;};class IntegerWithNamedNumber : public INTEGER{ protected: IntegerWithNamedNumber(const void* info); IntegerWithNamedNumber(int val, const void* info); static AbstractData* create(const void* info); struct NameEntry { int value; const char* name; }; struct NameEntryCmp { bool operator () (const NameEntry& lhs, const NameEntry& rhs) const { return lhs.value < rhs.value; } bool operator () (int lhs, const NameEntry& rhs) const { return lhs < rhs.value; } bool operator () (const NameEntry& lhs, int rhs) const { return lhs.value < rhs; } }; struct InfoType { CreateFun create; unsigned tag; unsigned type; int lowerLimit; unsigned upperLimit; AVN_ONLY(const NameEntry* nameEntries;) AVN_ONLY(unsigned entryCount;) }; const InfoType* info() const { return static_cast<const InfoType*>(info_); } private: virtual bool do_accept(Visitor&); virtual bool do_accept(ConstVisitor&) const;#ifdef ASN1_HAS_IOSTREAM public: bool getName(std::string&) const; bool setFromName(const std::string&);#endif};namespace detail { template <int i> struct is_negtive { enum { yes = (i<0) }; }; template <bool isNegtive> struct select_integer_type; template <> struct select_integer_type<false> { typedef unsigned value_type; }; template <> struct select_integer_type<true> { typedef int value_type; };}template <ConstraintType contraint, int lower, unsigned upper > class Constrained_INTEGER : public INTEGER{ typedef Constrained_INTEGER<contraint, lower, upper> ThisType; typedef INTEGER Inherited; protected: typedef Inherited::InfoType InfoType; public: enum { UpperLimit = upper, LowerLimit = lower }; // select value_type base on lower bound, if lower is negtive then value_type is int, otherwise value_type is unsigned. typedef typename detail::select_integer_type<detail::is_negtive<lower>::yes >::value_type value_type; typedef value_type& reference; typedef value_type const_reference; typedef value_type int_type; Constrained_INTEGER(value_type val =0) : INTEGER(&theInfo) { setValue(val); } // Constrained_INTEGER(const ThisType& other) ; //use default copy constructor ThisType& operator = (const INTEGER& val) { value = val.value ; assert(isValid()); return *this;} ThisType& operator = (int_type val) { value = val; assert(isValid()); return *this;} int_type getValue() const { return (value_type) value; } bool operator == (const ThisType& rhs) const { return getValue() == rhs.getValue(); } bool operator != (const ThisType& rhs) const { return getValue() != rhs.getValue(); } bool operator < (const ThisType& rhs) const { return getValue() < rhs.getValue(); } bool operator > (const ThisType& rhs) const { return getValue() > rhs.getValue(); } bool operator <= (const ThisType& rhs) const { return getValue() <= rhs.getValue(); } bool operator >= (const ThisType& rhs) const { return getValue() >= rhs.getValue(); } #if __GNUC__> 2 || __GNUC_MINOR__ > 95 friend bool operator == (int_type lhs, const ThisType& rhs) { return lhs == rhs.getValue(); } friend bool operator != (int_type lhs, const ThisType& rhs) { return lhs != rhs.getValue(); } friend bool operator < (int_type lhs, const ThisType& rhs) { return lhs < rhs.getValue(); } friend bool operator > (int_type lhs, const ThisType& rhs) { return lhs > rhs.getValue(); } friend bool operator <= (int_type lhs, const ThisType& rhs) { return lhs <= rhs.getValue(); } friend bool operator >= (int_type lhs, const ThisType& rhs) { return lhs >= rhs.getValue(); } #endif bool operator == (int_type rhs) const { return getValue() == rhs; } bool operator != (int_type rhs) const { return getValue() != rhs; } bool operator < (int_type rhs) const { return getValue() < rhs; } bool operator > (int_type rhs) const { return getValue() > rhs; } bool operator <= (int_type rhs) const { return getValue() <= rhs; } bool operator >= (int_type rhs) const { return getValue() >= rhs; } ThisType& operator += (int_type val) { value += val; assert(isValid()); return *this; } ThisType& operator -= (int_type val) { value -= val; assert(isValid()); return *this; } ThisType& operator *= (int_type val) { value *= val; assert(isValid()); return *this; } ThisType& operator /= (int_type val) { value /= val; assert(isValid()); return *this; } ThisType& operator %= (int_type val) { value %= val; assert(isValid()); return *this; } ThisType& operator += (const ThisType& val) { value += val.getValue(); assert(isValid()); return *this; } ThisType& operator -= (const ThisType& val) { value -= val.getValue(); assert(isValid()); return *this; } ThisType& operator *= (const ThisType& val) { value *= val.getValue(); assert(isValid()); return *this; } ThisType& operator /= (const ThisType& val) { value /= val.getValue(); assert(isValid()); return *this; } ThisType& operator %= (const ThisType& val) { value %= val.getValue(); assert(isValid()); return *this; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -