Yate
yateasn.h
00001 
00025 #ifndef __YATEASN_H
00026 #define __YATEASN_H
00027 
00028 #include <yatengine.h>
00029 
00030 #ifdef _WINDOWS
00031 
00032 #ifdef LIBYASN_EXPORTS
00033 #define YASN_API __declspec(dllexport)
00034 #else
00035 #ifndef LIBYASN_STATIC
00036 #define YASN_API __declspec(dllimport)
00037 #endif
00038 #endif
00039 
00040 #endif /* _WINDOWS */
00041 
00042 #ifndef YASN_API
00043 #define YASN_API
00044 #endif
00045 
00046 namespace TelEngine {
00047 
00048 #define ASN_LONG_LENGTH         0x80
00049 #define ASN_BIT8                0x80
00050 #define ASN_EXTENSION_ID        31
00051 #define IS_EXTENSION_ID(byte) (((byte) & ASN_EXTENSION_ID) == ASN_EXTENSION_ID)
00052 
00053 class AsnObject;
00054 class AsnValue;
00055 class AsnMibTree;
00056 class ASNObjId;
00057 class ASNLib;
00058 class ASNError;
00059 
00064 class YASN_API OctetString : public DataBlock
00065 {
00066 public:
00071     inline String getString()
00072     {
00073         String str((const char*)data(),length());
00074         return str;
00075     }
00076     inline DataBlock& operator=(const String& value)
00077     {
00078         clear();
00079         append(value);
00080         return *this;
00081     }
00082     inline DataBlock& operator=(const DataBlock& value)
00083     {
00084         clear();
00085         append(value);
00086         return *this;
00087     }
00092     inline const String toHexString() const
00093     {
00094         String str;
00095         str = str.hexify(data(),length());
00096         return str;
00097     }
00102     inline DataBlock& fromHexString(const String& value)
00103     {
00104         unHexify(value,value.length());
00105         return *this;
00106     }
00107 };
00108 
00113 class YASN_API AsnObject : public GenObject {
00114     YCLASS(AsnObject, GenObject)
00115 public:
00119     inline AsnObject()
00120         {}
00126     AsnObject(void* data, int len)
00127         {}
00131     virtual inline ~AsnObject()
00132         {}
00133 
00138     virtual int decode(DataBlock& data) = 0;
00139 
00144     virtual int encode(DataBlock& data) = 0;
00145 
00150     virtual void getParams(NamedList* params) = 0;
00151 
00156     virtual void setParams(NamedList* params) = 0;
00157 };
00158 
00163 class YASN_API AsnValue : public GenObject {
00164     YCLASS(AsnValue, GenObject)
00165 public:
00169     enum ValType {
00170         INTEGER                 = 1,
00171         STRING                  = 2,
00172         OBJECT_ID               = 3,
00173         IPADDRESS               = 4,
00174         COUNTER                 = 5,
00175         TIMETICKS               = 6,
00176         ARBITRARY               = 7,
00177         BIG_COUNTER             = 8,
00178         UNSIGNED_INTEGER        = 9
00179     };
00180 
00184     inline AsnValue()
00185         : m_type(0), m_data("")
00186         {}
00187 
00193     inline AsnValue(const String& value, int type = STRING)
00194         : m_type(type), m_data(value)
00195         { }
00196 
00200     virtual inline ~AsnValue()
00201         {}
00202 
00207     inline String getValue()
00208         { return m_data;}
00209 
00214     inline int type()
00215         { return m_type;}
00216 
00220     inline AsnValue& operator=( AsnValue* val)
00221     { 
00222         if (!val) 
00223             return *this;
00224         m_data = val->getValue();
00225         m_type = val->type(); 
00226         return *this;
00227     }
00228 
00232     inline AsnValue& operator=( AsnValue val)
00233     {  
00234         m_data = val.getValue();
00235         m_type = val.type();
00236         return *this;
00237     }
00238 
00243     inline void setValue(const String& data)
00244         { m_data.clear();m_data = data; }
00245 
00250     inline void setType(int type)
00251         { m_type = type; }
00252 
00253 private:
00254     int m_type;
00255     String m_data;
00256 };
00257 
00261 class YASN_API AsnMib : public GenObject {
00262     YCLASS(AsnMib, GenObject)
00263 public:
00267     enum Access {
00268         notAccessible = 0,
00269         accessibleForNotify = 1,
00270         readOnly = 2,
00271         readWrite = 3,
00272         readCreate = 4
00273     };
00277     inline AsnMib()
00278         : m_access(""), m_accessVal(0), m_index(0)
00279         {}
00280 
00285     AsnMib(NamedList& params);
00286 
00290     inline ~AsnMib()
00291         {}
00292 
00298     inline String& getAccess()
00299         { return m_access;}
00300 
00306     inline int getAccessValue()
00307         { return m_accessVal;}
00308 
00313     inline String& getName()
00314         { return m_name;}
00315 
00320     inline String getOID()
00321         { String str = ".";
00322           str += m_index;
00323           return m_oid + str;}
00324 
00329     inline String& getType()
00330         { return m_type;}
00331 
00336     inline String& getRevision()
00337         { return m_revision; }
00338 
00343     inline const String& toString() const
00344         { return m_oid;}
00345 
00350     inline void setIndex(unsigned int ind)
00351         { m_index = ind;}
00352 
00357     inline unsigned int index()
00358         { return m_index;}
00359 
00365     int compareTo(AsnMib* mib);
00366 
00371     inline String getParent()
00372     {
00373         int pos = m_oid.rfind('.');
00374         return m_oid.substr(0,pos);
00375     }
00376 
00377 private:
00378     String m_name;
00379     String m_oid;
00380     String m_access;
00381     int m_accessVal;
00382     String m_type;
00383     String m_revision;
00384     int size;
00385     int maxVal;
00386     int minVal;
00387     unsigned int m_index;
00388     
00389     static TokenDict s_access[];
00390 };
00391 
00395 class YASN_API AsnMibTree : public GenObject {
00396     YCLASS(AsnMibTree, GenObject)
00397 public:
00401     inline AsnMibTree()
00402         {}
00403 
00408     AsnMibTree(const String& fileName);
00409 
00413     virtual ~AsnMibTree();
00414 
00420     AsnMib* find(const ASNObjId& id);
00421 
00427     AsnMib* find(const String& name);
00428 
00434     AsnMib* findNext(const ASNObjId& id);
00435 
00441     int getAccess(const ASNObjId& oid);
00442 
00446     void buildTree();
00447 
00453     String findRevision(const String& name);
00454 
00455 private:
00456     String m_treeConf;
00457     ObjList m_mibs;
00458 };
00459 
00463 class YASN_API ASNObjId : public GenObject {
00464     YCLASS(ASNObjId, GenObject)
00465 public:
00469     ASNObjId();
00470 
00475     ASNObjId(const String& val);
00476 
00482     ASNObjId(const String& name, const String& val);
00483 
00488     ASNObjId(AsnMib* mib);
00489 
00493     ~ASNObjId();
00494 
00498     ASNObjId& operator=(const String& val);
00499 
00503     ASNObjId& operator=(const char* val);
00504 
00508     void toDataBlock();
00509 
00514     DataBlock getIds();
00515 
00519     inline const String& toString() const
00520         { return m_value; }
00521 
00526     inline const String& getName() const
00527         { return m_name; }
00528 
00533     inline void setValue(const String& value)
00534         { m_value = value; toDataBlock();}
00535 
00536 private:
00537     String m_value;
00538     String m_name;
00539     DataBlock m_ids;
00540 };
00541 
00546 class AsnTag {
00547 public:
00551     enum Class {
00552         Universal   = 0x00,
00553         Application = 0x40,
00554         Context     = 0x80,
00555         Private     = 0xc0,
00556     };
00557 
00561     enum Type {
00562         Primitive   = 0x00,
00563         Constructor = 0x20,
00564     };
00565 
00569     inline AsnTag()
00570         : m_class(Universal), m_type(Primitive), m_code(0)
00571     { }
00572 
00579     inline AsnTag(Class clas, Type type, unsigned int code)
00580         : m_class(clas), m_type(type), m_code(code)
00581     {
00582         encode();
00583     }
00584 
00588     inline ~AsnTag()
00589     {}
00590 
00596     static void decode(AsnTag& tag, DataBlock& data);
00597 
00605     static void encode(Class clas, Type type, unsigned int code, DataBlock& data);
00606 
00610     inline void encode()
00611         { AsnTag::encode(m_class,m_type,m_code,m_coding); }
00612 
00616     inline bool operator==(const AsnTag& tag) const
00617     {
00618         return (m_class == tag.classType() && m_type == tag.type() && m_code == tag.code());
00619     }
00620 
00624     inline bool operator!=(const AsnTag& tag) const
00625     {
00626         return !(m_class == tag.classType() && m_type == tag.type() && m_code == tag.code());
00627     }
00628 
00632     inline AsnTag& operator=(const AsnTag& value)
00633     {
00634         m_class = value.classType();
00635         m_type = value.type();
00636         m_code = value.code();
00637         encode();
00638         return *this;
00639     }
00640 
00645     inline const Class classType() const
00646         { return m_class; }
00647 
00652     inline void classType(Class clas)
00653         { m_class = clas; }
00654 
00659     inline const Type type() const
00660         { return m_type; }
00661 
00666     inline void type(Type type)
00667         { m_type = type; }
00668 
00673     inline const unsigned int code() const
00674         { return m_code; }
00675 
00680     inline void code(unsigned int code)
00681         { m_code = code; }
00682 
00687     inline const DataBlock& coding() const
00688         { return m_coding; }
00689 
00690 private:
00691     Class m_class;
00692     Type m_type;
00693     unsigned int m_code;
00694     DataBlock m_coding;
00695 };
00696 
00701 class YASN_API ASNLib {
00702 public:
00706     enum TypeTag {
00707         UNIVERSAL       = 0x00,
00708         BOOLEAN         = 0x01,
00709         INTEGER         = 0x02,
00710         BIT_STRING      = 0x03,
00711         OCTET_STRING    = 0x04,
00712         NULL_ID         = 0x05,
00713         OBJECT_ID       = 0x06,
00714         REAL            = 0x09, //not implemented
00715         UTF8_STR        = 0x0c,
00716         SEQUENCE        = 0x30,
00717         SET             = 0x31,
00718         NUMERIC_STR     = 0x12,
00719         PRINTABLE_STR   = 0x13,
00720         IA5_STR         = 0x16,
00721         UTC_TIME        = 0x17,
00722         GENERALIZED_TIME = 0x18,
00723         VISIBLE_STR     = 0x1a,
00724         GENERAL_STR     = 0x1b, // not implemented
00725         UNIVERSAL_STR   = 0x1c, // not implemented
00726         CHARACTER_STR   = 0x1d, // not implemented
00727         BMP_STR         = 0x1e, // not implemented
00728         CHOICE          = 0x1f, // does not have a value
00729         DEFINED         = 0x2d
00730     };
00731         // values not implemented
00732         // 10   ENUMERATED
00733         // 11   EMBEDDED PDV
00734         // 13   RELATIVE-OID
00735         // 20   TeletexString, T61String
00736         // 21   VideotexString
00737         // 25   GraphicString
00738         // 27   GeneralString
00739         // 28   UniversalString
00740         // 29   CHARACTER STRING
00741         // 30   BMPString
00745     enum Error {
00746         InvalidLengthOrTag = -1,
00747         ConstraintBreakError = -2,
00748         ParseError,
00749         InvalidContentsError
00750     };
00751 
00755     ASNLib();
00756 
00760     ~ASNLib();
00761 
00767     static int decodeLength(DataBlock& data);
00768 
00776     static int decodeBoolean(DataBlock& data, bool* val, bool tagCheck);
00777 
00786     static int decodeInteger(DataBlock& data, u_int64_t& intVal, unsigned int bytes, bool tagCheck);
00787 
00795     static int decodeUINT8(DataBlock& data, u_int8_t* intVal, bool tagCheck);
00796 
00804     static int decodeUINT16(DataBlock& data, u_int16_t* intVal, bool tagCheck);
00805 
00813     static int decodeUINT32(DataBlock& data, u_int32_t* intVal, bool tagCheck);
00814 
00822     static int decodeUINT64(DataBlock& data, u_int64_t* intVal, bool tagCheck);
00823 
00831     static int decodeINT8(DataBlock& data, int8_t* intVal, bool tagCheck);
00832 
00840     static int decodeINT16(DataBlock& data, int16_t* intVal, bool tagCheck);
00841 
00849     static int decodeINT32(DataBlock& data, int32_t* intVal, bool tagCheck);
00850 
00858     static int decodeINT64(DataBlock& data, int64_t* intVal, bool tagCheck);
00859 
00867     static int decodeBitString(DataBlock& data, String* val, bool tagCheck);
00868 
00876     static int decodeOctetString(DataBlock& data, OctetString* strVal, bool tagCheck);
00877 
00884     static int decodeNull(DataBlock& data, bool tagCheck);
00885 
00893     static int decodeOID(DataBlock& data, ASNObjId* obj, bool tagCheck);
00894 
00902     static int decodeReal(DataBlock& data, float* realVal, bool tagCheck);
00903 
00912     static int decodeString(DataBlock& data, String* str, int* type, bool tagCheck);
00913 
00921     static int decodeUtf8(DataBlock& data, String* str, bool tagCheck);
00922 
00932     static int decodeGenTime(DataBlock& data, unsigned int* time, unsigned int* fractions, bool* utc, bool tagCheck);
00933 
00941     static int decodeUTCTime(DataBlock& data, unsigned int* time, bool tagCheck);
00942 
00950     static int decodeAny(DataBlock data, DataBlock* val, bool tagCheck);
00951 
00958     static int decodeSequence(DataBlock& data, bool tagCheck);
00959 
00966     static int decodeSet(DataBlock& data, bool tagCheck);
00967 
00973     static DataBlock buildLength(DataBlock& data);
00974 
00981     static DataBlock encodeBoolean(bool val, bool tagCheck);
00982 
00989     static DataBlock encodeInteger(u_int64_t intVal, bool tagCheck);
00990 
00997     static DataBlock encodeOctetString(OctetString strVal, bool tagCheck);
00998 
01004     static DataBlock encodeNull(bool tagCheck);
01005 
01012     static DataBlock encodeBitString(String val, bool tagCheck);
01013 
01020     static DataBlock encodeOID(ASNObjId obj, bool tagCheck);
01021 
01028     static DataBlock encodeReal(float val, bool tagCheck);
01029 
01037     static DataBlock encodeString(String str, int type, bool tagCheck);
01038 
01045     static DataBlock encodeUtf8(String str, bool tagCheck);
01046 
01054     static DataBlock encodeGenTime(unsigned int time, unsigned int fractions, bool tagCheck);
01055 
01062     static DataBlock encodeUTCTime(unsigned int time, bool tagCheck);
01063 
01070     static DataBlock encodeAny(DataBlock data, bool tagCheck);
01071 
01078     static int encodeSequence(DataBlock& data, bool tagCheck);
01079 
01086     static int encodeSet(DataBlock& data, bool tagCheck);
01087 };
01088 
01089 }
01090 
01091 #endif /* __YATEASN_H */
01092 
01093 /* vi: set ts=8 sw=4 sts=4 noet: */