PTLib  Version 2.18.8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
pasn.h
Go to the documentation of this file.
1 /*
2  * pasn.h
3  *
4  * Abstract Syntax Notation 1 classes for support of SNMP only.
5  *
6  * Portable Windows Library
7  *
8  * Copyright (c) 1993-2002 Equivalence Pty. Ltd.
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is Portable Windows Library.
21  *
22  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
23  *
24  * Contributor(s): ______________________________________.
25  */
26 
27 #ifndef PTLIB_PASN_H
28 #define PTLIB_PASN_H
29 
30 #ifdef P_USE_PRAGMA
31 #pragma interface
32 #endif
33 
34 #include <ptlib/sockets.h>
35 
36 //
37 // define some types used by the ASN classes
38 //
39 typedef PInt32 PASNInt;
40 typedef DWORD PASNUnsigned;
41 typedef DWORD PASNOid;
42 
43 class PASNObject;
44 class PASNSequence;
45 
46 PARRAY(PASNObjectArray, PASNObject);
47 
48 
50 
60 class PASNObject : public PObject
61 {
63 
64  public:
68  enum ASNType {
82  Null,
85  };
86 
90  virtual ASNType GetType() const;
91 
92 
95  int GetChoice() const;
96 
98  virtual PString GetTypeAsString() const;
99 
105  virtual PASNInt GetInteger () const;
106 
112  virtual PASNUnsigned GetUnsigned () const;
113 
117  virtual PString GetString () const;
118 
124  virtual const PASNSequence & GetSequence() const;
125 
131  virtual PIPSocket::Address GetIPAddress () const;
132 
136  virtual void PrintOn(
137  ostream & strm
138  ) const;
139 
141  virtual void Encode(
142  PBYTEArray & buffer
143  );
144 
148  virtual WORD GetEncodedLength();
149 
151  virtual PObject * Clone() const;
152 
154  static void EncodeASNLength (
155  PBYTEArray & buffer,
156  WORD length
157  );
158 
160  static WORD GetASNLengthLength (
161  WORD length
162  );
163 
167  static PBoolean DecodeASNLength (
168  const PBYTEArray & buffer,
169  PINDEX & ptr,
170  WORD & len
171  );
172 
174  static void EncodeASNSequenceStart (
175  PBYTEArray & buffer,
176  BYTE type,
177  WORD length
178  );
179 
181  static WORD GetASNSequenceStartLength (
182  WORD length
183  );
184 
186  static void EncodeASNHeader(
187  PBYTEArray & buffer,
188  PASNObject::ASNType type,
189  WORD length
190  );
191 
193  static WORD GetASNHeaderLength (
194  WORD length
195  );
196 
197  static void EncodeASNInteger (
198  PBYTEArray & buffer,
199  PASNInt data,
200  PASNObject::ASNType type
201  );
202  // Encode an ASN integer value into the specified buffer */
203 
204  static void EncodeASNUnsigned (
205  PBYTEArray & buffer,
206  PASNUnsigned data,
207  PASNObject::ASNType type
208  );
209  // Encode an ASN integer value into the specified buffer */
210 
211  static WORD GetASNIntegerLength (
212  PASNInt data
213  );
214  // Return the length of an encoded ASN integer with the specified value
215 
216  static WORD GetASNUnsignedLength (
217  PASNUnsigned data
218  );
219  // Return the length of an encoded ASN integer with the specified value
220 
221  static PBoolean DecodeASNInteger (
222  const PBYTEArray & buffer,
223  PINDEX & ptr,
224  PASNInt & value,
225  ASNType type = Integer
226  );
227  // Decode an ASN integer value in the specified buffer
228 
229  static PBoolean DecodeASNUnsigned (
230  const PBYTEArray & buffer,
231  PINDEX & ptr,
232  PASNUnsigned & value,
233  ASNType type = TimeTicks
234  );
235  // Decode an ASN integer value in the specified buffer
236 
237  protected:
239  PASNObject();
240 
242  static BYTE ASNTypeToType[ASNTypeMax];
243 
244 };
245 
246 
248 
251 class PASNInteger : public PASNObject
252 {
254  public:
255  PASNInteger(PASNInt val);
256  PASNInteger(const PBYTEArray & buffer, PINDEX & ptr);
257 
258  void PrintOn(ostream & strm) const;
259  void Encode(PBYTEArray & buffer);
260  WORD GetEncodedLength();
261  PObject * Clone() const;
262 
263  PASNInt GetInteger() const;
264  PString GetString () const;
265  ASNType GetType() const;
266  PString GetTypeAsString() const;
267 
268  private:
269  PASNInt value;
270 };
271 
272 
274 
277 class PASNString : public PASNObject
278 {
280  public:
281  PASNString(const PString & str);
282  PASNString(const BYTE * ptr, int len);
283  PASNString(const PBYTEArray & buffer, PASNObject::ASNType = String);
284  PASNString(const PBYTEArray & buffer, PINDEX & ptr, PASNObject::ASNType = String);
285 
286  void PrintOn(ostream & strm) const;
287 
288  void Encode(PBYTEArray & buffer)
289  { Encode(buffer, String); }
290 
291  WORD GetEncodedLength();
292  PObject * Clone() const;
293 
294  PString GetString() const;
295  ASNType GetType() const;
296  PString GetTypeAsString() const;
297 
298  protected:
299  PBoolean Decode(const PBYTEArray & buffer, PINDEX & i, PASNObject::ASNType type);
300  void Encode(PBYTEArray & buffer, PASNObject::ASNType type);
301 
303  WORD valueLen;
304 };
305 
306 
308 
311 class PASNIPAddress : public PASNString
312 {
314  public:
316  : PASNString((const BYTE *)addr.GetPointer(), addr.GetSize()) { }
317 
318  PASNIPAddress(const PString & str);
319 
320  PASNIPAddress(const PBYTEArray & buffer)
321  : PASNString(buffer, IPAddress) { }
322 
323  PASNIPAddress(const PBYTEArray & buffer, PINDEX & ptr)
324  : PASNString(buffer, ptr, IPAddress) { }
325 
327  { return IPAddress; }
328 
329  void Encode(PBYTEArray & buffer)
330  { PASNString::Encode(buffer, IPAddress); }
331 
332  PString GetString() const;
333 
334  PString GetTypeAsString() const;
335 
336  PObject * Clone() const
337  { return PNEW PASNIPAddress(*this); }
338 
340 };
341 
342 
344 
348 {
350  public:
352  { value = val; }
353 
354  PASNUnsignedInteger(const PBYTEArray & buffer, PINDEX & ptr);
355 
356  void PrintOn(ostream & strm) const;
357  WORD GetEncodedLength();
358  PString GetString () const;
359  PASNUnsigned GetUnsigned() const;
360 
361  protected:
363  { value = 0; }
364 
365  PBoolean Decode(const PBYTEArray & buffer, PINDEX & i, PASNObject::ASNType theType);
366  void Encode(PBYTEArray & buffer, PASNObject::ASNType theType);
367 
368  private:
369  PASNUnsigned value;
370 };
371 
372 
374 
378 {
380  public:
382  : PASNUnsignedInteger(val) { }
383 
384  PASNTimeTicks(const PBYTEArray & buffer, PINDEX & ptr)
385  { PASNUnsignedInteger::Decode(buffer, ptr, TimeTicks); }
386 
387  void Encode(PBYTEArray & buffer)
389 
390  PObject * Clone() const
391  { return PNEW PASNTimeTicks(*this); }
392 
394  { return TimeTicks; }
395 
396  PString GetTypeAsString() const;
397 };
398 
399 
401 
405 {
407  public:
409  : PASNUnsignedInteger(val) { }
410 
411  PASNCounter(const PBYTEArray & buffer, PINDEX & ptr)
412  { PASNUnsignedInteger::Decode(buffer, ptr, Counter); }
413 
414  void Encode(PBYTEArray & buffer)
416 
417  PObject * Clone() const
418  { return PNEW PASNCounter(*this); }
419 
421  { return Counter; }
422 
423  PString GetTypeAsString() const;
424 };
425 
426 
428 
432 {
434  public:
436  : PASNUnsignedInteger(val) { }
437 
438  PASNGauge(const PBYTEArray & buffer, PINDEX & ptr)
439  { Decode(buffer, ptr); }
440 
441  PBoolean Decode(const PBYTEArray & buffer, PINDEX & i)
442  { return PASNUnsignedInteger::Decode(buffer, i, Gauge); }
443 
444  void Encode(PBYTEArray & buffer)
445  { PASNUnsignedInteger::Encode(buffer, Gauge); }
446 
447  PObject * Clone() const
448  { return PNEW PASNGauge(*this); }
449 
451  { return Gauge; }
452 
453  PString GetTypeAsString() const;
454 };
455 
456 
457 
459 
462 class PASNObjectID : public PASNObject
463 {
465  public:
466  PASNObjectID(const PString & str);
467  PASNObjectID(PASNOid * val, BYTE theLen);
468  PASNObjectID(const PBYTEArray & buffer);
469  PASNObjectID(const PBYTEArray & buffer, PINDEX & ptr);
470 
471  void PrintOn(ostream & strm) const;
472  void Encode(PBYTEArray & buffer);
473  WORD GetEncodedLength();
474  PObject * Clone() const;
475 
476  ASNType GetType() const;
477  PString GetString () const;
478  PString GetTypeAsString() const;
479 
480  protected:
481  PBoolean Decode(const PBYTEArray & buffer, PINDEX & i);
482 
483  private:
484  PDWORDArray value;
485 };
486 
487 
489 
492 class PASNNull : public PASNObject
493 {
495  public:
496  PASNNull();
497  PASNNull(const PBYTEArray & buffer, PINDEX & ptr);
498 
499  void PrintOn(ostream & strm) const;
500 
501  void Encode(PBYTEArray & buffer);
502  WORD GetEncodedLength();
503 
504  PObject * Clone() const;
505 
506  ASNType GetType() const;
507  PString GetString () const;
508  PString GetTypeAsString() const;
509 };
510 
511 
513 
516 class PASNSequence : public PASNObject
517 {
519  public:
520  PASNSequence();
521  PASNSequence(BYTE selector);
522  PASNSequence(const PBYTEArray & buffer);
523  PASNSequence(const PBYTEArray & buffer, PINDEX & i);
524 
525  void Append(PASNObject * obj);
526  PINDEX GetSize() const;
527  PASNObject & operator [] (PINDEX idx) const;
528  const PASNSequence & GetSequence() const;
529 
530  void AppendInteger (PASNInt value);
531  void AppendString (const PString & str);
532  void AppendObjectID(const PString & str);
533  void AppendObjectID(PASNOid * val, BYTE len);
534 
535  int GetChoice() const;
536 
537 // PASNInt GetInteger (PINDEX idx) const;
538 // PString GetString (PINDEX idx) const;
539 
540  void PrintOn(ostream & strm) const;
541  void Encode(PBYTEArray & buffer);
542  PBoolean Decode(const PBYTEArray & buffer, PINDEX & i);
543  WORD GetEncodedLength();
544  ASNType GetType() const;
545  PString GetTypeAsString() const;
546 
547  PBoolean Encode(PBYTEArray & buffer, PINDEX maxLen) ;
548 
549  private:
550  PASNObjectArray sequence;
551  BYTE type;
552  ASNType asnType;
553  WORD encodedLen;
554  WORD seqLen;
555 };
556 
557 
558 #endif // PTLIB_PASN_H
559 
560 
561 // End Of File ///////////////////////////////////////////////////////////////
PObject * Clone() const
Virtual function used to duplicate objects.
Definition: pasn.h:390
PString GetString() const
Return the value of the object as a PString.
void PrintOn(ostream &strm) const
Virtual functions used by the PObject::operator&lt;&lt; function to print the value of the object...
PString GetString() const
Return the value of the object as a PString.
A descendant of PASNObject which is an unsigned ASN integer type.
Definition: pasn.h:347
PObject * Clone() const
Virtual function used to duplicate objects.
PString GetString() const
Return the value of the object as a PString.
PASNInteger(PASNInt val)
PASNIPAddress(const PBYTEArray &buffer, PINDEX &ptr)
Definition: pasn.h:323
PASNTimeTicks(const PBYTEArray &buffer, PINDEX &ptr)
Definition: pasn.h:384
static PBoolean DecodeASNUnsigned(const PBYTEArray &buffer, PINDEX &ptr, PASNUnsigned &value, ASNType type=TimeTicks)
A descendant of PASNObject which is a simple ASN OctetStr type.
Definition: pasn.h:277
PString GetTypeAsString() const
Return a string giving the type of the object.
PBoolean Decode(const PBYTEArray &buffer, PINDEX &i)
WORD GetEncodedLength()
Virtual function used to get the length of object when encoded into ASN format.
PObject * Clone() const
Virtual function used to duplicate objects.
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:2164
void Encode(PBYTEArray &buffer)
Virtual function used to encode the object into ASN format.
Definition: pasn.h:329
PASNObject::ASNType GetType() const
Return a value of type enum ASNType which indicates the type of the object.
Definition: pasn.h:450
void Encode(PBYTEArray &buffer)
Virtual function used to encode the object into ASN format.
virtual PString GetTypeAsString() const
Return a string giving the type of the object.
WORD GetEncodedLength()
Virtual function used to get the length of object when encoded into ASN format.
PString GetTypeAsString() const
Return a string giving the type of the object.
void Encode(PBYTEArray &buffer)
Virtual function used to encode the object into ASN format.
ASNType
Value returned by the GetType() function to indicate the type of an ASN object.
Definition: pasn.h:68
ASN Counter64.
Definition: pasn.h:80
PString GetTypeAsString() const
Return a string giving the type of the object.
ASN Octet String object.
Definition: pasn.h:70
PASNIPAddress(const PBYTEArray &buffer)
Definition: pasn.h:320
void Encode(PBYTEArray &buffer, PASNObject::ASNType theType)
virtual PIPSocket::Address GetIPAddress() const
Return the value of the object as an IPAddress.
PObject * Clone() const
Virtual function used to duplicate objects.
ASN Sequence with discriminator.
Definition: pasn.h:73
ASNType GetType() const
Return a value of type enum ASNType which indicates the type of the object.
PASNCounter(PASNUnsigned val)
Definition: pasn.h:408
WORD GetEncodedLength()
Virtual function used to get the length of object when encoded into ASN format.
PString GetTypeAsString() const
Return a string giving the type of the object.
PASNInt GetInteger() const
Return the value of the ASN object as a PASNInt.
ASN Gauge object.
Definition: pasn.h:76
ASN IPAddress object.
Definition: pasn.h:74
PString GetTypeAsString() const
Return a string giving the type of the object.
PASNGauge(PASNUnsigned val)
Definition: pasn.h:435
virtual PASNInt GetInteger() const
Return the value of the ASN object as a PASNInt.
WORD GetEncodedLength()
Virtual function used to get the length of object when encoded into ASN format.
PString GetTypeAsString() const
Return a string giving the type of the object.
PASNTimeTicks(PASNUnsigned val)
Definition: pasn.h:381
PASNUnsigned GetUnsigned() const
Return the value of the object as a PASNUnsigned.
static WORD GetASNSequenceStartLength(WORD length)
Return the encoded length of a sequence if it has the specified length.
ASN Object ID object.
Definition: pasn.h:71
PASNCounter(const PBYTEArray &buffer, PINDEX &ptr)
Definition: pasn.h:411
void AppendInteger(PASNInt value)
PInt32 PASNInt
Definition: pasn.h:39
const PASNSequence & GetSequence() const
Return the value of the object as a PString.
This class defines the common behviour of all ASN objects.
Definition: pasn.h:60
virtual PString GetString() const
Return the value of the object as a PString.
PString GetString() const
Return the value of the object as a PString.
ASN Opaque object.
Definition: pasn.h:78
PObject * Clone() const
Virtual function used to duplicate objects.
Definition: pasn.h:447
maximum of number of ASN object types
Definition: pasn.h:84
void Encode(PBYTEArray &buffer)
Virtual function used to encode the object into ASN format.
Definition: pasn.h:387
PBoolean Decode(const PBYTEArray &buffer, PINDEX &i)
Array of unsigned characters.
Definition: array.h:605
A descendant of PASNObject which is an unsigned ASN ObjID type.
Definition: pasn.h:462
ASN Counter object.
Definition: pasn.h:75
ASNType GetType() const
Return a value of type enum ASNType which indicates the type of the object.
void PrintOn(ostream &strm) const
Virtual functions used by the PObject::operator&lt;&lt; function to print the value of the object...
void PrintOn(ostream &strm) const
Virtual functions used by the PObject::operator&lt;&lt; function to print the value of the object...
WORD GetEncodedLength()
Virtual function used to get the length of object when encoded into ASN format.
static WORD GetASNIntegerLength(PASNInt data)
PString value
Definition: pasn.h:302
static void EncodeASNSequenceStart(PBYTEArray &buffer, BYTE type, WORD length)
Encode a sequence header into the buffer at the specified offset.
static void EncodeASNInteger(PBYTEArray &buffer, PASNInt data, PASNObject::ASNType type)
A descendant of PASNObject which is a simple ASN integer type.
Definition: pasn.h:251
static BYTE ASNTypeToType[ASNTypeMax]
Table to map enum ASNType values to ASN identifiers.
Definition: pasn.h:242
PString GetString() const
Return the value of the object as a PString.
PObject * Clone() const
Virtual function used to duplicate objects.
void PrintOn(ostream &strm) const
Virtual functions used by the PObject::operator&lt;&lt; function to print the value of the object...
virtual const PASNSequence & GetSequence() const
Return the value of the object as a PString.
virtual WORD GetEncodedLength()
Virtual function used to get the length of object when encoded into ASN format.
PObject * Clone() const
Virtual function used to duplicate objects.
Definition: pasn.h:417
ASNType GetType() const
Return a value of type enum ASNType which indicates the type of the object.
A descendant of PASNObject which is an unsigned ASN guage type.
Definition: pasn.h:431
PIPSocket::Address GetIPAddress() const
Return the value of the object as an IPAddress.
PString GetTypeAsString() const
Return a string giving the type of the object.
bool PBoolean
Definition: object.h:174
virtual void Encode(PBYTEArray &buffer)
Virtual function used to encode the object into ASN format.
PASNObjectID(const PString &str)
#define PARRAY(cls, T)
Declare an array to a specific type of object.
Definition: array.h:985
A descendant of PASNObject which is an unsigned ASN time tick type.
Definition: pasn.h:377
virtual PASNUnsigned GetUnsigned() const
Return the value of the object as a PASNUnsigned.
ASN Null.
Definition: pasn.h:82
The character string class.
Definition: pstring.h:108
PINDEX GetSize() const
A descendant of PASNObject which is an unsigned ASN counter type.
Definition: pasn.h:404
static void EncodeASNLength(PBYTEArray &buffer, WORD length)
Encode an ASN length value.
WORD valueLen
Definition: pasn.h:303
static PBoolean DecodeASNInteger(const PBYTEArray &buffer, PINDEX &ptr, PASNInt &value, ASNType type=Integer)
void Encode(PBYTEArray &buffer)
Virtual function used to encode the object into ASN format.
Definition: pasn.h:414
ASNType GetType() const
Return a value of type enum ASNType which indicates the type of the object.
PASNObject::ASNType GetType() const
Return a value of type enum ASNType which indicates the type of the object.
Definition: pasn.h:393
PASNObject::ASNType GetType() const
Return a value of type enum ASNType which indicates the type of the object.
Definition: pasn.h:420
void PrintOn(ostream &strm) const
Virtual functions used by the PObject::operator&lt;&lt; function to print the value of the object...
PASNString(const PString &str)
PBoolean Decode(const PBYTEArray &buffer, PINDEX &i, PASNObject::ASNType theType)
PASNUnsignedInteger(PASNUnsigned val)
Definition: pasn.h:351
PASNIPAddress(const PIPSocket::Address &addr)
Definition: pasn.h:315
PASNUnsignedInteger()
Definition: pasn.h:362
static void EncodeASNHeader(PBYTEArray &buffer, PASNObject::ASNType type, WORD length)
Encode an ASN object header into the buffer.
void Encode(PBYTEArray &buffer)
Virtual function used to encode the object into ASN format.
Definition: pasn.h:444
A class describing an IP address.
Definition: ipsock.h:59
DWORD PASNOid
Definition: pasn.h:41
PASNGauge(const PBYTEArray &buffer, PINDEX &ptr)
Definition: pasn.h:438
ASN Unsigned integer 32.
Definition: pasn.h:81
void AppendObjectID(const PString &str)
PString GetString() const
Return the value of the object as a PString.
ASN Sequence object.
Definition: pasn.h:72
PASNObject::ASNType GetType() const
Return a value of type enum ASNType which indicates the type of the object.
Definition: pasn.h:326
PString GetTypeAsString() const
Return a string giving the type of the object.
void Encode(PBYTEArray &buffer)
Virtual function used to encode the object into ASN format.
void AppendString(const PString &str)
static PBoolean DecodeASNLength(const PBYTEArray &buffer, PINDEX &ptr, WORD &len)
Decode an ASN length in the buffer at the given ptr.
PBoolean Decode(const PBYTEArray &buffer, PINDEX &i, PASNObject::ASNType type)
int GetChoice() const
Return the descriminator for Choice sequences.
virtual void PrintOn(ostream &strm) const
Virtual functions used by the PObject::operator&lt;&lt; function to print the value of the object...
void Encode(PBYTEArray &buffer)
Virtual function used to encode the object into ASN format.
static WORD GetASNUnsignedLength(PASNUnsigned data)
void Encode(PBYTEArray &buffer)
Virtual function used to encode the object into ASN format.
Definition: pasn.h:288
PASNObject & operator[](PINDEX idx) const
void Append(PASNObject *obj)
void PrintOn(ostream &strm) const
Virtual functions used by the PObject::operator&lt;&lt; function to print the value of the object...
ASNType GetType() const
Return a value of type enum ASNType which indicates the type of the object.
A descendant of PASNObject which is the complex sequence type.
Definition: pasn.h:516
static WORD GetASNLengthLength(WORD length)
Return the length of an encoded ASN length value.
PBoolean Decode(const PBYTEArray &buffer, PINDEX &i)
Definition: pasn.h:441
static WORD GetASNHeaderLength(WORD length)
Return the length of an ASN object header if the object is the specified length.
ASN Integer object.
Definition: pasn.h:69
unknown ASN object type
Definition: pasn.h:83
Ultimate parent class for all objects in the class library.
Definition: object.h:2204
WORD GetEncodedLength()
Virtual function used to get the length of object when encoded into ASN format.
static void EncodeASNUnsigned(PBYTEArray &buffer, PASNUnsigned data, PASNObject::ASNType type)
virtual PObject * Clone() const
Virtual function used to duplicate objects.
virtual ASNType GetType() const
Return a value of type enum ASNType which indicates the type of the object.
DWORD PASNUnsigned
Definition: pasn.h:40
ASN NsapAddress.
Definition: pasn.h:79
#define PNEW
Macro for overriding system default new operator.
Definition: object.h:1896
PObject * Clone() const
Virtual function used to duplicate objects.
Definition: pasn.h:336
A descendant of PASNObject which is the NULL type.
Definition: pasn.h:492
PASNObject()
Create an empty ASN object.
A descendant of PASNObject which is an IP address type.
Definition: pasn.h:311
ASN TimeTicks object.
Definition: pasn.h:77
int GetChoice() const
PString GetTypeAsString() const
Return a string giving the type of the object.