Yate
yateasn.h
1
21
22#ifndef __YATEASN_H
23#define __YATEASN_H
24
25#include <yateclass.h>
26
27#ifdef _WINDOWS
28
29#ifdef LIBYASN_EXPORTS
30#define YASN_API __declspec(dllexport)
31#else
32#ifndef LIBYASN_STATIC
33#define YASN_API __declspec(dllimport)
34#endif
35#endif
36
37#endif /* _WINDOWS */
38
39#ifndef YASN_API
40#define YASN_API
41#endif
42
43namespace TelEngine {
44
45#define ASN_LONG_LENGTH 0x80
46#define ASN_BIT8 0x80
47#define ASN_EXTENSION_ID 31
48#define IS_EXTENSION_ID(byte) (((byte) & ASN_EXTENSION_ID) == ASN_EXTENSION_ID)
49
50class AsnObject;
51class AsnValue;
52class ASNObjId;
53class ASNLib;
54class ASNError;
55
60class YASN_API OctetString : public DataBlock
61{
62public:
68 {
69 String str((const char*)data(),length());
70 return str;
71 }
72 inline DataBlock& operator=(const String& value)
73 {
74 clear();
75 append(value);
76 return *this;
77 }
78 inline DataBlock& operator=(const DataBlock& value)
79 {
80 clear();
81 append(value);
82 return *this;
83 }
88 inline const String toHexString() const
89 {
90 String str;
91 str = str.hexify(data(),length());
92 return str;
93 }
94
98 inline DataBlock& fromHexString(const String& value)
99 {
100 unHexify(value);
101 return *this;
102 }
103};
104
109class YASN_API AsnObject : public GenObject {
110 YCLASS(AsnObject, GenObject)
111public:
115 inline AsnObject()
116 {}
117
122 inline AsnObject(const AsnObject& original)
123 {}
124
128 virtual inline ~AsnObject()
129 {}
130
135 virtual int decode(DataBlock& data) = 0;
136
141 virtual int encode(DataBlock& data) = 0;
142
147 virtual void getParams(NamedList* params) = 0;
148
153 virtual void setParams(NamedList* params) = 0;
154};
155
160class YASN_API AsnValue : public GenObject {
161 YCLASS(AsnValue, GenObject)
162public:
166 enum ValType {
167 INTEGER = 1,
168 STRING = 2,
169 OBJECT_ID = 3,
170 IPADDRESS = 4,
171 COUNTER = 5,
172 TIMETICKS = 6,
173 ARBITRARY = 7,
174 BIG_COUNTER = 8,
175 UNSIGNED_INTEGER = 9
176 };
177
181 inline AsnValue()
182 : m_type(0), m_data("")
183 {}
184
189 inline AsnValue(const AsnValue& original)
190 : m_type(original.m_type), m_data(original.m_data)
191 { }
192
198 inline AsnValue(const String& value, int type = STRING)
199 : m_type(type), m_data(value)
200 { }
201
205 virtual inline ~AsnValue()
206 {}
207
212 inline const String& getValue()
213 { return m_data; }
214
219 inline int type()
220 { return m_type;}
221
226 {
227 if (!val)
228 return *this;
229 m_data = val->getValue();
230 m_type = val->type();
231 return *this;
232 }
233
238 {
239 m_data = val.getValue();
240 m_type = val.type();
241 return *this;
242 }
243
248 inline void setValue(const String& data)
249 { m_data.clear();m_data = data; }
250
255 inline void setType(int type)
256 { m_type = type; }
257
258private:
259 int m_type;
260 String m_data;
261};
262
266class YASN_API AsnMib : public GenObject {
267 YCLASS(AsnMib, GenObject)
268public:
272 enum Access {
273 notAccessible = 0,
274 accessibleForNotify = 1,
275 readOnly = 2,
276 readWrite = 3,
277 readCreate = 4
278 };
279
282 inline AsnMib()
283 : m_access(""), m_accessVal(0), m_index(0)
284 {}
285
291
295 inline ~AsnMib()
296 {}
297
304 { return m_access;}
305
311 inline int getAccessValue()
312 { return m_accessVal;}
313
318 inline String& getName()
319 { return m_name;}
320
325 inline String getOID()
326 { String str = ".";
327 str += m_index;
328 return m_oid + str;}
329
334 inline String& getType()
335 { return m_type;}
336
342 { return m_revision; }
343
348 inline const String& toString() const
349 { return m_oid;}
350
355 inline void setIndex(unsigned int ind)
356 { m_index = ind;}
357
362 inline unsigned int index()
363 { return m_index;}
364
370 int compareTo(AsnMib* mib);
371
377 {
378 int pos = m_oid.rfind('.');
379 return m_oid.substr(0,pos);
380 }
381
382private:
383 String m_name;
384 String m_oid;
385 String m_access;
386 int m_accessVal;
387 String m_type;
388 String m_revision;
389 int size;
390 int maxVal;
391 int minVal;
392 unsigned int m_index;
393
394 static TokenDict s_access[];
395};
396
400class YASN_API ASNObjId : public GenObject {
401 YCLASS(ASNObjId, GenObject)
402public:
407
412 inline ASNObjId(const ASNObjId& original)
413 : m_value(original.m_value), m_name(original.m_name)
414 { }
415
420 ASNObjId(const String& val);
421
427 ASNObjId(const String& name, const String& val);
428
434
439
443 inline ASNObjId& operator=(const ASNObjId& original)
444 { m_value = original.toString(); return *this; }
445
449 inline ASNObjId& operator=(const String& val)
450 { m_value = val; return *this; }
451
455 inline ASNObjId& operator=(const char* val)
456 { m_value = val; return *this; }
457
462
468
472 inline const String& toString() const
473 { return m_value; }
474
479 inline const String& getName() const
480 { return m_name; }
481
486 inline void setValue(const String& value)
487 { m_value = value; toDataBlock();}
488
489private:
490 String m_value;
491 String m_name;
492 DataBlock m_ids;
493};
494
499class AsnTag {
500public:
504 enum Class {
505 Universal = 0x00,
506 Application = 0x40,
507 Context = 0x80,
508 Private = 0xc0,
509 };
510
514 enum Type {
515 Primitive = 0x00,
516 Constructor = 0x20,
517 };
518
522 inline AsnTag()
523 : m_class(Universal), m_type(Primitive), m_code(0)
524 { }
525
532 inline AsnTag(Class clas, Type type, unsigned int code)
533 : m_class(clas), m_type(type), m_code(code)
534 {
535 encode();
536 }
537
541 inline ~AsnTag()
542 {}
543
549 static void decode(AsnTag& tag, DataBlock& data);
550
558 static void encode(Class clas, Type type, unsigned int code, DataBlock& data);
559
563 inline void encode()
564 { AsnTag::encode(m_class,m_type,m_code,m_coding); }
565
569 inline bool operator==(const AsnTag& tag) const
570 {
571 return (m_class == tag.classType() && m_type == tag.type() && m_code == tag.code());
572 }
573
577 inline bool operator!=(const AsnTag& tag) const
578 {
579 return !(m_class == tag.classType() && m_type == tag.type() && m_code == tag.code());
580 }
581
585 inline AsnTag& operator=(const AsnTag& value)
586 {
587 m_class = value.classType();
588 m_type = value.type();
589 m_code = value.code();
590 encode();
591 return *this;
592 }
593
598 inline const Class classType() const
599 { return m_class; }
600
605 inline void classType(Class clas)
606 { m_class = clas; }
607
612 inline const Type type() const
613 { return m_type; }
614
619 inline void type(Type type)
620 { m_type = type; }
621
626 inline const unsigned int code() const
627 { return m_code; }
628
633 inline void code(unsigned int code)
634 { m_code = code; }
635
640 inline const DataBlock& coding() const
641 { return m_coding; }
642
643private:
644 Class m_class;
645 Type m_type;
646 unsigned int m_code;
647 DataBlock m_coding;
648};
649
654class YASN_API ASNLib {
655public:
659 enum TypeTag {
660 UNIVERSAL = 0x00,
661 BOOLEAN = 0x01,
662 INTEGER = 0x02,
663 BIT_STRING = 0x03,
664 OCTET_STRING = 0x04,
665 NULL_ID = 0x05,
666 OBJECT_ID = 0x06,
667 REAL = 0x09, //not implemented
668 UTF8_STR = 0x0c,
669 SEQUENCE = 0x30,
670 SET = 0x31,
671 NUMERIC_STR = 0x12,
672 PRINTABLE_STR = 0x13,
673 IA5_STR = 0x16,
674 UTC_TIME = 0x17,
675 GENERALIZED_TIME = 0x18,
676 VISIBLE_STR = 0x1a,
677 GENERAL_STR = 0x1b, // not implemented
678 UNIVERSAL_STR = 0x1c, // not implemented
679 CHARACTER_STR = 0x1d, // not implemented
680 BMP_STR = 0x1e, // not implemented
681 CHOICE = 0x1f, // does not have a value
682 DEFINED = 0x2d
683 };
684 // values not implemented
685 // 10 ENUMERATED
686 // 11 EMBEDDED PDV
687 // 13 RELATIVE-OID
688 // 20 TeletexString, T61String
689 // 21 VideotexString
690 // 25 GraphicString
691 // 27 GeneralString
692 // 28 UniversalString
693 // 29 CHARACTER STRING
694 // 30 BMPString
698 enum Error {
699 InvalidLengthOrTag = -1,
700 ConstraintBreakError = -2,
701 ParseError = -3,
702 InvalidContentsError = -4,
703 IndefiniteForm = -5,
704 };
705
710
715
721 static int decodeLength(DataBlock& data);
722
730 static int decodeBoolean(DataBlock& data, bool* val, bool tagCheck);
731
740 static int decodeInteger(DataBlock& data, u_int64_t& intVal, unsigned int bytes, bool tagCheck);
741
749 static int decodeUINT8(DataBlock& data, u_int8_t* intVal, bool tagCheck);
750
758 static int decodeUINT16(DataBlock& data, u_int16_t* intVal, bool tagCheck);
759
767 static int decodeUINT32(DataBlock& data, u_int32_t* intVal, bool tagCheck);
768
776 static int decodeUINT64(DataBlock& data, u_int64_t* intVal, bool tagCheck);
777
785 static int decodeINT8(DataBlock& data, int8_t* intVal, bool tagCheck);
786
794 static int decodeINT16(DataBlock& data, int16_t* intVal, bool tagCheck);
795
803 static int decodeINT32(DataBlock& data, int32_t* intVal, bool tagCheck);
804
812 static int decodeINT64(DataBlock& data, int64_t* intVal, bool tagCheck);
813
821 static int decodeBitString(DataBlock& data, String* val, bool tagCheck);
822
830 static int decodeOctetString(DataBlock& data, OctetString* strVal, bool tagCheck);
831
838 static int decodeNull(DataBlock& data, bool tagCheck);
839
847 static int decodeOID(DataBlock& data, ASNObjId* obj, bool tagCheck);
848
856 static int decodeReal(DataBlock& data, float* realVal, bool tagCheck);
857
866 static int decodeString(DataBlock& data, String* str, int* type, bool tagCheck);
867
875 static int decodeUtf8(DataBlock& data, String* str, bool tagCheck);
876
886 static int decodeGenTime(DataBlock& data, unsigned int* time, unsigned int* fractions, bool* utc, bool tagCheck);
887
895 static int decodeUTCTime(DataBlock& data, unsigned int* time, bool tagCheck);
896
904 static int decodeAny(DataBlock data, DataBlock* val, bool tagCheck);
905
912 static int decodeSequence(DataBlock& data, bool tagCheck);
913
920 static int decodeSet(DataBlock& data, bool tagCheck);
921
928
935 static DataBlock encodeBoolean(bool val, bool tagCheck);
936
943 static DataBlock encodeInteger(u_int64_t intVal, bool tagCheck);
944
951 static DataBlock encodeOctetString(OctetString strVal, bool tagCheck);
952
958 static DataBlock encodeNull(bool tagCheck);
959
966 static DataBlock encodeBitString(String val, bool tagCheck);
967
974 static DataBlock encodeOID(ASNObjId obj, bool tagCheck);
975
982 static DataBlock encodeReal(float val, bool tagCheck);
983
991 static DataBlock encodeString(String str, int type, bool tagCheck);
992
999 static DataBlock encodeUtf8(String str, bool tagCheck);
1000
1008 static DataBlock encodeGenTime(unsigned int time, unsigned int fractions, bool tagCheck);
1009
1016 static DataBlock encodeUTCTime(unsigned int time, bool tagCheck);
1017
1024 static DataBlock encodeAny(DataBlock data, bool tagCheck);
1025
1032 static int encodeSequence(DataBlock& data, bool tagCheck);
1033
1040 static int encodeSet(DataBlock& data, bool tagCheck);
1041
1047 static int matchEOC(DataBlock& data);
1048
1055 static int parseUntilEoC(DataBlock& data, int length = 0);
1056};
1057
1058}
1059
1060#endif /* __YATEASN_H */
1061
1062/* vi: set ts=8 sw=4 sts=4 noet: */
static int decodeINT8(DataBlock &data, int8_t *intVal, bool tagCheck)
static int decodeGenTime(DataBlock &data, unsigned int *time, unsigned int *fractions, bool *utc, bool tagCheck)
static int decodeUINT16(DataBlock &data, u_int16_t *intVal, bool tagCheck)
static int decodeLength(DataBlock &data)
static DataBlock encodeInteger(u_int64_t intVal, bool tagCheck)
static int decodeOctetString(DataBlock &data, OctetString *strVal, bool tagCheck)
static int decodeAny(DataBlock data, DataBlock *val, bool tagCheck)
static int decodeUTCTime(DataBlock &data, unsigned int *time, bool tagCheck)
static int matchEOC(DataBlock &data)
static int decodeOID(DataBlock &data, ASNObjId *obj, bool tagCheck)
static DataBlock encodeReal(float val, bool tagCheck)
Error
Definition yateasn.h:698
static DataBlock buildLength(DataBlock &data)
static DataBlock encodeBitString(String val, bool tagCheck)
static int decodeUINT32(DataBlock &data, u_int32_t *intVal, bool tagCheck)
static DataBlock encodeAny(DataBlock data, bool tagCheck)
TypeTag
Definition yateasn.h:659
static int decodeNull(DataBlock &data, bool tagCheck)
static int decodeBitString(DataBlock &data, String *val, bool tagCheck)
static int decodeUtf8(DataBlock &data, String *str, bool tagCheck)
static int decodeBoolean(DataBlock &data, bool *val, bool tagCheck)
static int decodeString(DataBlock &data, String *str, int *type, bool tagCheck)
static int decodeReal(DataBlock &data, float *realVal, bool tagCheck)
static DataBlock encodeBoolean(bool val, bool tagCheck)
static int encodeSequence(DataBlock &data, bool tagCheck)
static int decodeInteger(DataBlock &data, u_int64_t &intVal, unsigned int bytes, bool tagCheck)
static int parseUntilEoC(DataBlock &data, int length=0)
static int decodeINT32(DataBlock &data, int32_t *intVal, bool tagCheck)
static int decodeINT16(DataBlock &data, int16_t *intVal, bool tagCheck)
static int decodeINT64(DataBlock &data, int64_t *intVal, bool tagCheck)
static DataBlock encodeNull(bool tagCheck)
static int decodeUINT64(DataBlock &data, u_int64_t *intVal, bool tagCheck)
static DataBlock encodeOctetString(OctetString strVal, bool tagCheck)
static DataBlock encodeOID(ASNObjId obj, bool tagCheck)
static DataBlock encodeString(String str, int type, bool tagCheck)
static DataBlock encodeGenTime(unsigned int time, unsigned int fractions, bool tagCheck)
static DataBlock encodeUTCTime(unsigned int time, bool tagCheck)
static int decodeSequence(DataBlock &data, bool tagCheck)
static int decodeUINT8(DataBlock &data, u_int8_t *intVal, bool tagCheck)
static int encodeSet(DataBlock &data, bool tagCheck)
static DataBlock encodeUtf8(String str, bool tagCheck)
static int decodeSet(DataBlock &data, bool tagCheck)
Definition yateasn.h:400
ASNObjId & operator=(const char *val)
Definition yateasn.h:455
const String & toString() const
Definition yateasn.h:472
void toDataBlock()
ASNObjId(const String &name, const String &val)
ASNObjId & operator=(const ASNObjId &original)
Definition yateasn.h:443
const String & getName() const
Definition yateasn.h:479
ASNObjId(const String &val)
ASNObjId & operator=(const String &val)
Definition yateasn.h:449
DataBlock getIds()
ASNObjId(AsnMib *mib)
void setValue(const String &value)
Definition yateasn.h:486
ASNObjId(const ASNObjId &original)
Definition yateasn.h:412
Definition yateasn.h:266
String & getName()
Definition yateasn.h:318
String getParent()
Definition yateasn.h:376
const String & toString() const
Definition yateasn.h:348
String & getAccess()
Definition yateasn.h:303
String & getType()
Definition yateasn.h:334
void setIndex(unsigned int ind)
Definition yateasn.h:355
int compareTo(AsnMib *mib)
Access
Definition yateasn.h:272
unsigned int index()
Definition yateasn.h:362
String & getRevision()
Definition yateasn.h:341
~AsnMib()
Definition yateasn.h:295
String getOID()
Definition yateasn.h:325
int getAccessValue()
Definition yateasn.h:311
AsnMib(NamedList &params)
AsnMib()
Definition yateasn.h:282
virtual ~AsnObject()
Definition yateasn.h:128
virtual int decode(DataBlock &data)=0
AsnObject()
Definition yateasn.h:115
virtual void setParams(NamedList *params)=0
virtual int encode(DataBlock &data)=0
AsnObject(const AsnObject &original)
Definition yateasn.h:122
virtual void getParams(NamedList *params)=0
static void decode(AsnTag &tag, DataBlock &data)
Type
Definition yateasn.h:514
AsnTag()
Definition yateasn.h:522
bool operator!=(const AsnTag &tag) const
Definition yateasn.h:577
void code(unsigned int code)
Definition yateasn.h:633
AsnTag & operator=(const AsnTag &value)
Definition yateasn.h:585
void classType(Class clas)
Definition yateasn.h:605
const DataBlock & coding() const
Definition yateasn.h:640
bool operator==(const AsnTag &tag) const
Definition yateasn.h:569
~AsnTag()
Definition yateasn.h:541
void encode()
Definition yateasn.h:563
const Class classType() const
Definition yateasn.h:598
void type(Type type)
Definition yateasn.h:619
static void encode(Class clas, Type type, unsigned int code, DataBlock &data)
const Type type() const
Definition yateasn.h:612
const unsigned int code() const
Definition yateasn.h:626
AsnTag(Class clas, Type type, unsigned int code)
Definition yateasn.h:532
Class
Definition yateasn.h:504
const String & getValue()
Definition yateasn.h:212
void setValue(const String &data)
Definition yateasn.h:248
AsnValue()
Definition yateasn.h:181
AsnValue & operator=(AsnValue val)
Definition yateasn.h:237
AsnValue & operator=(AsnValue *val)
Definition yateasn.h:225
virtual ~AsnValue()
Definition yateasn.h:205
AsnValue(const AsnValue &original)
Definition yateasn.h:189
ValType
Definition yateasn.h:166
int type()
Definition yateasn.h:219
AsnValue(const String &value, int type=STRING)
Definition yateasn.h:198
void setType(int type)
Definition yateasn.h:255
A class that holds just a block of raw data.
Definition yateclass.h:4237
void * data() const
Definition yateclass.h:4289
bool unHexify(const char *data, unsigned int len, char sep)
unsigned int length() const
Definition yateclass.h:4321
DataBlock(unsigned int overAlloc=0)
A named string container class.
Definition yateclass.h:5016
Helper class for operations with octet strings.
Definition yateasn.h:61
const String toHexString() const
Definition yateasn.h:88
String getString()
Definition yateasn.h:67
DataBlock & fromHexString(const String &value)
Definition yateasn.h:98
A C-style string handling class.
Definition yateclass.h:2131
String & hexify(void *data, unsigned int len, char sep=0, bool upCase=false)
int rfind(char what) const
Definition yatemime.h:34
Definition yateclass.h:848