UPS: apcupsd clean sources
[tomato.git] / release / src / router / apcupsd / src / drivers / snmplite / asn.h
blobc70461645ab79f1d6c4aa5d20878d7737438edf9
1 /*
2 * asn.h
4 * ASN.1 type classes
5 */
7 /*
8 * Copyright (C) 2009 Adam Kropelin
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General
12 * Public License as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public
20 * License along with this program; if not, write to the Free
21 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * MA 02111-1307, USA.
25 #ifndef __ASN_H
26 #define __ASN_H
28 #include "astring.h"
29 #include "alist.h"
31 namespace Asn
33 // Class field
34 static const unsigned char UNIVERSAL = 0x00;
35 static const unsigned char APPLICATION = 0x40;
36 static const unsigned char CONTEXT = 0x80;
37 static const unsigned char PRIVATE = 0xC0;
39 // Primitive/Constructed field
40 static const unsigned char CONSTRUCTED = 0x20;
41 static const unsigned char PRIMITIVE = 0x00;
43 // Built-in types from ASN.1
44 typedef unsigned char Identifier;
45 static const Identifier INTEGER = UNIVERSAL | PRIMITIVE | 0x02;
46 static const Identifier BITSTRING = UNIVERSAL | PRIMITIVE | 0x03;
47 static const Identifier OCTETSTRING = UNIVERSAL | PRIMITIVE | 0x04;
48 static const Identifier NULLL = UNIVERSAL | PRIMITIVE | 0x05;
49 static const Identifier OBJECTID = UNIVERSAL | PRIMITIVE | 0x06;
50 static const Identifier SEQUENCE = UNIVERSAL | CONSTRUCTED | 0x10;
52 // SNMP-specific types
53 static const Identifier IPADDRESS = APPLICATION | PRIMITIVE | 0x00;
54 static const Identifier COUNTER = APPLICATION | PRIMITIVE | 0x01;
55 static const Identifier GAUGE = APPLICATION | PRIMITIVE | 0x02;
56 static const Identifier TIMETICKS = APPLICATION | PRIMITIVE | 0x03;
57 static const Identifier GET_REQ_PDU = CONTEXT | CONSTRUCTED | 0x00;
58 static const Identifier GETNEXT_REQ_PDU = CONTEXT | CONSTRUCTED | 0x01;
59 static const Identifier GET_RSP_PDU = CONTEXT | CONSTRUCTED | 0x02;
60 static const Identifier SET_REQ_PDU = CONTEXT | CONSTRUCTED | 0x03;
61 static const Identifier TRAP_PDU = CONTEXT | CONSTRUCTED | 0x04;
63 // **************************************************************************
64 // Forward declarations
65 // **************************************************************************
66 class Integer;
67 class ObjectId;
68 class OctetString;
69 class Sequence;
71 // **************************************************************************
72 // Object
73 // **************************************************************************
74 class Object
76 public:
78 Object(Identifier type): _type(type) {}
79 virtual ~Object() {}
81 static Object *Demarshal(unsigned char *&buffer, unsigned int &buflen);
83 Identifier Type() const { return _type; }
85 Integer *AsInteger() { return (Integer*) this; }
86 ObjectId *AsObjectId() { return (ObjectId*) this; }
87 OctetString *AsOctetString() { return (OctetString*)this; }
88 Sequence *AsSequence() { return (Sequence*) this; }
90 virtual bool IsInteger() { return false; }
91 virtual bool IsObjectId() { return false; }
92 virtual bool IsOctetString() { return false; }
93 virtual bool IsSequence() { return false; }
95 virtual bool Marshal(
96 unsigned char *&buffer,
97 unsigned int &buflen) const = 0;
99 virtual Object *copy() const = 0;
101 protected:
103 virtual bool demarshal(unsigned char *&buffer, unsigned int &buflen) = 0;
104 bool marshalType(unsigned char *&buffer, unsigned int &buflen) const;
106 bool marshalLength(
107 unsigned int len,
108 unsigned char *&buffer,
109 unsigned int &buflen) const;
111 bool demarshalLength(
112 unsigned char *&buffer,
113 unsigned int &buflen,
114 unsigned int &vallen) const;
116 int numbits(unsigned int num) const;
118 Identifier _type;
121 // **************************************************************************
122 // Integer
123 // **************************************************************************
124 class Integer: public Object
126 public:
128 Integer(Identifier id = INTEGER) :
129 Object(id), _value(0), _signed(false) {}
131 Integer(int value) :
132 Object(INTEGER), _value(value), _signed(value < 0) {}
134 virtual ~Integer() {}
136 unsigned int UintValue() const { return _value; }
137 int IntValue() const { return (int)_value; }
139 Integer &operator=(int value)
140 { _value = value; _signed = value < 0; return *this; }
141 Integer &operator=(unsigned int value)
142 { _value = value; _signed = false; return *this; }
144 virtual Object *copy() const { return new Integer(*this); }
145 virtual bool Marshal(unsigned char *&buffer, unsigned int &buflen) const;
146 virtual bool IsInteger() { return true; }
148 protected:
150 virtual bool demarshal(unsigned char *&buffer, unsigned int &buflen);
152 unsigned int _value;
153 bool _signed;
156 // **************************************************************************
157 // OctetString
158 // **************************************************************************
159 class OctetString: public Object
161 public:
163 OctetString() : Object(OCTETSTRING), _data(NULL), _len(0) {}
164 OctetString(const char *value);
165 OctetString(const unsigned char *value, unsigned int len);
166 virtual ~OctetString() { delete [] _data; }
168 OctetString(const OctetString &rhs);
169 OctetString &operator=(const OctetString &rhs);
171 const unsigned char *Value() const { return _data; }
172 const unsigned int Length() const { return _len; }
174 operator const char *() const { return (const char *)_data; }
176 virtual Object *copy() const { return new OctetString(*this); }
177 virtual bool Marshal(unsigned char *&buffer, unsigned int &buflen) const;
178 virtual bool IsOctetString() { return true; }
180 protected:
182 virtual bool demarshal(unsigned char *&buffer, unsigned int &buflen);
184 void assign(const unsigned char *data, unsigned int len);
186 unsigned char *_data;
187 unsigned int _len;
190 // **************************************************************************
191 // ObjectId
192 // **************************************************************************
193 class ObjectId: public Object
195 public:
197 ObjectId() : Object(OBJECTID), _value(NULL), _count(0) {}
198 ObjectId(const int oid[]);
199 virtual ~ObjectId() { delete [] _value; }
201 ObjectId(const ObjectId &rhs);
202 ObjectId &operator=(const ObjectId &rhs);
203 ObjectId &operator=(const int oid[]);
205 bool operator==(const ObjectId &rhs) const;
206 bool operator==(const int oid[]) const;
207 bool operator!=(const ObjectId &rhs) const { return !(*this == rhs); }
208 bool operator!=(const int oid[]) const { return !(*this == oid); }
210 bool IsChildOf(const int oid[]);
212 virtual Object *copy() const { return new ObjectId(*this); }
213 virtual bool Marshal(unsigned char *&buffer, unsigned int &buflen) const;
214 virtual bool IsObjectId() { return true; }
216 protected:
218 virtual bool demarshal(unsigned char *&buffer, unsigned int &buflen);
219 void assign(const int oid[]);
220 void assign(const int oid[], unsigned int count);
222 int *_value;
223 unsigned int _count;
226 // **************************************************************************
227 // Null
228 // **************************************************************************
229 class Null: public Object
231 public:
233 Null() : Object(NULLL) {}
234 virtual ~Null() {}
236 virtual Object *copy() const { return new Null(*this); }
237 virtual bool Marshal(unsigned char *&buffer, unsigned int &buflen) const;
239 protected:
241 virtual bool demarshal(unsigned char *&buffer, unsigned int &buflen);
244 // **************************************************************************
245 // Sequence
246 // **************************************************************************
247 class Sequence: public Object
249 public:
251 Sequence(Identifier type = SEQUENCE);
252 virtual ~Sequence();
254 Sequence(const Sequence &rhs);
255 Sequence &operator=(const Sequence &rhs);
257 unsigned int Size() { return _size; }
258 Object *operator[](unsigned int idx);
259 void Append(Object *obj);
261 virtual Object *copy() const { return new Sequence(*this); }
262 virtual bool Marshal(unsigned char *&buffer, unsigned int &buflen) const;
263 virtual bool IsSequence() { return true; }
265 protected:
267 virtual bool demarshal(unsigned char *&buffer, unsigned int &buflen);
268 void assign(const Sequence &rhs);
269 void clear();
271 Object **_data;
272 unsigned int _size;
275 #endif