PTLib  Version 2.10.10
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  * $Revision: 21788 $
27  * $Author: rjongbloed $
28  * $Date: 2008-12-11 23:42:13 -0600 (Thu, 11 Dec 2008) $
29  */
30 
31 #ifndef PTLIB_PASN_H
32 #define PTLIB_PASN_H
33 
34 #ifdef P_USE_PRAGMA
35 #pragma interface
36 #endif
37 
38 #include <ptlib/sockets.h>
39 
40 //
41 // define some types used by the ASN classes
42 //
43 typedef PInt32 PASNInt;
44 typedef DWORD PASNUnsigned;
45 typedef DWORD PASNOid;
46 
47 class PASNObject;
48 class PASNSequence;
49 
50 PARRAY(PASNObjectArray, PASNObject);
51 
52 
54 
64 class PASNObject : public PObject
65 {
67 
68  public:
72  enum ASNType {
86  Null,
89  };
90 
94  virtual ASNType GetType() const;
95 
96 
99  int GetChoice() const;
100 
102  virtual PString GetTypeAsString() const;
103 
109  virtual PASNInt GetInteger () const;
110 
116  virtual PASNUnsigned GetUnsigned () const;
117 
121  virtual PString GetString () const;
122 
128  virtual const PASNSequence & GetSequence() const;
129 
135  virtual PIPSocket::Address GetIPAddress () const;
136 
140  virtual void PrintOn(
141  ostream & strm
142  ) const;
143 
145  virtual void Encode(
146  PBYTEArray & buffer
147  );
148 
152  virtual WORD GetEncodedLength();
153 
155  virtual PObject * Clone() const;
156 
158  static void EncodeASNLength (
159  PBYTEArray & buffer,
160  WORD length
161  );
162 
164  static WORD GetASNLengthLength (
165  WORD length
166  );
167 
171  static PBoolean DecodeASNLength (
172  const PBYTEArray & buffer,
173  PINDEX & ptr,
174  WORD & len
175  );
176 
178  static void EncodeASNSequenceStart (
179  PBYTEArray & buffer,
180  BYTE type,
181  WORD length
182  );
183 
185  static WORD GetASNSequenceStartLength (
186  WORD length
187  );
188 
190  static void EncodeASNHeader(
191  PBYTEArray & buffer,
192  PASNObject::ASNType type,
193  WORD length
194  );
195 
197  static WORD GetASNHeaderLength (
198  WORD length
199  );
200 
201  static void EncodeASNInteger (
202  PBYTEArray & buffer,
203  PASNInt data,
204  PASNObject::ASNType type
205  );
206  // Encode an ASN integer value into the specified buffer */
207 
208  static void EncodeASNUnsigned (
209  PBYTEArray & buffer,
210  PASNUnsigned data,
211  PASNObject::ASNType type
212  );
213  // Encode an ASN integer value into the specified buffer */
214 
215  static WORD GetASNIntegerLength (
216  PASNInt data
217  );
218  // Return the length of an encoded ASN integer with the specified value
219 
220  static WORD GetASNUnsignedLength (
221  PASNUnsigned data
222  );
223  // Return the length of an encoded ASN integer with the specified value
224 
225  static PBoolean DecodeASNInteger (
226  const PBYTEArray & buffer,
227  PINDEX & ptr,
228  PASNInt & value,
229  ASNType type = Integer
230  );
231  // Decode an ASN integer value in the specified buffer
232 
233  static PBoolean DecodeASNUnsigned (
234  const PBYTEArray & buffer,
235  PINDEX & ptr,
236  PASNUnsigned & value,
237  ASNType type = TimeTicks
238  );
239  // Decode an ASN integer value in the specified buffer
240 
241  protected:
243  PASNObject();
244 
246  static BYTE ASNTypeToType[ASNTypeMax];
247 
248 };
249 
250 
252 
255 class PASNInteger : public PASNObject
256 {
258  public:
259  PASNInteger(PASNInt val);
260  PASNInteger(const PBYTEArray & buffer, PINDEX & ptr);
261 
262  void PrintOn(ostream & strm) const;
263  void Encode(PBYTEArray & buffer);
264  WORD GetEncodedLength();
265  PObject * Clone() const;
266 
267  PASNInt GetInteger() const;
268  PString GetString () const;
269  ASNType GetType() const;
270  PString GetTypeAsString() const;
271 
272  private:
273  PASNInt value;
274 };
275 
276 
278 
281 class PASNString : public PASNObject
282 {
284  public:
285  PASNString(const PString & str);
286  PASNString(const BYTE * ptr, int len);
287  PASNString(const PBYTEArray & buffer, PASNObject::ASNType = String);
288  PASNString(const PBYTEArray & buffer, PINDEX & ptr, PASNObject::ASNType = String);
289 
290  void PrintOn(ostream & strm) const;
291 
292  void Encode(PBYTEArray & buffer)
293  { Encode(buffer, String); }
294 
295  WORD GetEncodedLength();
296  PObject * Clone() const;
297 
298  PString GetString() const;
299  ASNType GetType() const;
300  PString GetTypeAsString() const;
301 
302  protected:
303  PBoolean Decode(const PBYTEArray & buffer, PINDEX & i, PASNObject::ASNType type);
304  void Encode(PBYTEArray & buffer, PASNObject::ASNType type);
305 
307  WORD valueLen;
308 };
309 
310 
312 
315 class PASNIPAddress : public PASNString
316 {
318  public:
320  : PASNString((const BYTE *)addr.GetPointer(), addr.GetSize()) { }
321 
322  PASNIPAddress(const PString & str);
323 
324  PASNIPAddress(const PBYTEArray & buffer)
325  : PASNString(buffer, IPAddress) { }
326 
327  PASNIPAddress(const PBYTEArray & buffer, PINDEX & ptr)
328  : PASNString(buffer, ptr, IPAddress) { }
329 
331  { return IPAddress; }
332 
333  void Encode(PBYTEArray & buffer)
334  { PASNString::Encode(buffer, IPAddress); }
335 
336  PString GetString() const;
337 
338  PString GetTypeAsString() const;
339 
340  PObject * Clone() const
341  { return PNEW PASNIPAddress(*this); }
342 
344 };
345 
346 
348 
352 {
354  public:
356  { value = val; }
357 
358  PASNUnsignedInteger(const PBYTEArray & buffer, PINDEX & ptr);
359 
360  void PrintOn(ostream & strm) const;
361  WORD GetEncodedLength();
362  PString GetString () const;
363  PASNUnsigned GetUnsigned() const;
364 
365  protected:
367  { value = 0; }
368 
369  PBoolean Decode(const PBYTEArray & buffer, PINDEX & i, PASNObject::ASNType theType);
370  void Encode(PBYTEArray & buffer, PASNObject::ASNType theType);
371 
372  private:
373  PASNUnsigned value;
374 };
375 
376 
378 
382 {
384  public:
386  : PASNUnsignedInteger(val) { }
387 
388  PASNTimeTicks(const PBYTEArray & buffer, PINDEX & ptr)
389  { PASNUnsignedInteger::Decode(buffer, ptr, TimeTicks); }
390 
391  void Encode(PBYTEArray & buffer)
393 
394  PObject * Clone() const
395  { return PNEW PASNTimeTicks(*this); }
396 
398  { return TimeTicks; }
399 
400  PString GetTypeAsString() const;
401 };
402 
403 
405 
409 {
411  public:
413  : PASNUnsignedInteger(val) { }
414 
415  PASNCounter(const PBYTEArray & buffer, PINDEX & ptr)
416  { PASNUnsignedInteger::Decode(buffer, ptr, Counter); }
417 
418  void Encode(PBYTEArray & buffer)
420 
421  PObject * Clone() const
422  { return PNEW PASNCounter(*this); }
423 
425  { return Counter; }
426 
427  PString GetTypeAsString() const;
428 };
429 
430 
432 
436 {
438  public:
440  : PASNUnsignedInteger(val) { }
441 
442  PASNGauge(const PBYTEArray & buffer, PINDEX & ptr)
443  { Decode(buffer, ptr); }
444 
445  PBoolean Decode(const PBYTEArray & buffer, PINDEX & i)
446  { return PASNUnsignedInteger::Decode(buffer, i, Gauge); }
447 
448  void Encode(PBYTEArray & buffer)
449  { PASNUnsignedInteger::Encode(buffer, Gauge); }
450 
451  PObject * Clone() const
452  { return PNEW PASNGauge(*this); }
453 
455  { return Gauge; }
456 
457  PString GetTypeAsString() const;
458 };
459 
460 
461 
463 
466 class PASNObjectID : public PASNObject
467 {
469  public:
470  PASNObjectID(const PString & str);
471  PASNObjectID(PASNOid * val, BYTE theLen);
472  PASNObjectID(const PBYTEArray & buffer);
473  PASNObjectID(const PBYTEArray & buffer, PINDEX & ptr);
474 
475  void PrintOn(ostream & strm) const;
476  void Encode(PBYTEArray & buffer);
477  WORD GetEncodedLength();
478  PObject * Clone() const;
479 
480  ASNType GetType() const;
481  PString GetString () const;
482  PString GetTypeAsString() const;
483 
484  protected:
485  PBoolean Decode(const PBYTEArray & buffer, PINDEX & i);
486 
487  private:
488  PDWORDArray value;
489 };
490 
491 
493 
496 class PASNNull : public PASNObject
497 {
499  public:
500  PASNNull();
501  PASNNull(const PBYTEArray & buffer, PINDEX & ptr);
502 
503  void PrintOn(ostream & strm) const;
504 
505  void Encode(PBYTEArray & buffer);
506  WORD GetEncodedLength();
507 
508  PObject * Clone() const;
509 
510  ASNType GetType() const;
511  PString GetString () const;
512  PString GetTypeAsString() const;
513 };
514 
515 
517 
520 class PASNSequence : public PASNObject
521 {
523  public:
524  PASNSequence();
525  PASNSequence(BYTE selector);
526  PASNSequence(const PBYTEArray & buffer);
527  PASNSequence(const PBYTEArray & buffer, PINDEX & i);
528 
529  void Append(PASNObject * obj);
530  PINDEX GetSize() const;
531  PASNObject & operator [] (PINDEX idx) const;
532  const PASNSequence & GetSequence() const;
533 
534  void AppendInteger (PASNInt value);
535  void AppendString (const PString & str);
536  void AppendObjectID(const PString & str);
537  void AppendObjectID(PASNOid * val, BYTE len);
538 
539  int GetChoice() const;
540 
541 // PASNInt GetInteger (PINDEX idx) const;
542 // PString GetString (PINDEX idx) const;
543 
544  void PrintOn(ostream & strm) const;
545  void Encode(PBYTEArray & buffer);
546  PBoolean Decode(const PBYTEArray & buffer, PINDEX & i);
547  WORD GetEncodedLength();
548  ASNType GetType() const;
549  PString GetTypeAsString() const;
550 
551  PBoolean Encode(PBYTEArray & buffer, PINDEX maxLen) ;
552 
553  private:
554  PASNObjectArray sequence;
555  BYTE type;
556  ASNType asnType;
557  WORD encodedLen;
558  WORD seqLen;
559 };
560 
561 
562 #endif // PTLIB_PASN_H
563 
564 
565 // End Of File ///////////////////////////////////////////////////////////////
PObject * Clone() const
Virtual function used to duplicate objects.
Definition: pasn.h:394
PString GetString() const
Return the value of the object as a PString.
void PrintOn(ostream &strm) const
Virtual functions used by the PObject::operator<< 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:351
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:327
PASNTimeTicks(const PBYTEArray &buffer, PINDEX &ptr)
Definition: pasn.h:388
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:281
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:1049
void Encode(PBYTEArray &buffer)
Virtual function used to encode the object into ASN format.
Definition: pasn.h:333
PASNObject::ASNType GetType() const
Return a value of type enum ASNType which indicates the type of the object.
Definition: pasn.h:454
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:72
ASN Counter64.
Definition: pasn.h:84
PString GetTypeAsString() const
Return a string giving the type of the object.
ASN Octet String object.
Definition: pasn.h:74
PASNIPAddress(const PBYTEArray &buffer)
Definition: pasn.h:324
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:77
ASNType GetType() const
Return a value of type enum ASNType which indicates the type of the object.
PASNCounter(PASNUnsigned val)
Definition: pasn.h:412
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:80
ASN IPAddress object.
Definition: pasn.h:78
PString GetTypeAsString() const
Return a string giving the type of the object.
PASNGauge(PASNUnsigned val)
Definition: pasn.h:439
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:385
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:75
Array of unsigned long integers.
Definition: array.h:764
PASNCounter(const PBYTEArray &buffer, PINDEX &ptr)
Definition: pasn.h:415
void AppendInteger(PASNInt value)
PInt32 PASNInt
Definition: pasn.h:43
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:64
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:82
BOOL PBoolean
Definition: object.h:102
PObject * Clone() const
Virtual function used to duplicate objects.
Definition: pasn.h:451
maximum of number of ASN object types
Definition: pasn.h:88
void Encode(PBYTEArray &buffer)
Virtual function used to encode the object into ASN format.
Definition: pasn.h:391
PBoolean Decode(const PBYTEArray &buffer, PINDEX &i)
Array of unsigned characters.
Definition: array.h:670
A descendant of PASNObject which is an unsigned ASN ObjID type.
Definition: pasn.h:466
ASN Counter object.
Definition: pasn.h:79
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<< function to print the value of the object...
void PrintOn(ostream &strm) const
Virtual functions used by the PObject::operator<< 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:306
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:255
static BYTE ASNTypeToType[ASNTypeMax]
Table to map enum ASNType values to ASN identifiers.
Definition: pasn.h:246
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<< 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:421
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:435
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.
virtual void Encode(PBYTEArray &buffer)
Virtual function used to encode the object into ASN format.
PASNObjectID(const PString &str)
A descendant of PASNObject which is an unsigned ASN time tick type.
Definition: pasn.h:381
virtual PASNUnsigned GetUnsigned() const
Return the value of the object as a PASNUnsigned.
ASN Null.
Definition: pasn.h:86
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:408
static void EncodeASNLength(PBYTEArray &buffer, WORD length)
Encode an ASN length value.
WORD valueLen
Definition: pasn.h:307
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:418
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:397
PASNObject::ASNType GetType() const
Return a value of type enum ASNType which indicates the type of the object.
Definition: pasn.h:424
void PrintOn(ostream &strm) const
Virtual functions used by the PObject::operator<< 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:355
PASNIPAddress(const PIPSocket::Address &addr)
Definition: pasn.h:319
PASNUnsignedInteger()
Definition: pasn.h:366
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:448
A class describing an IP address.
Definition: ipsock.h:75
DWORD PASNOid
Definition: pasn.h:45
PASNGauge(const PBYTEArray &buffer, PINDEX &ptr)
Definition: pasn.h:442
ASN Unsigned integer 32.
Definition: pasn.h:85
void AppendObjectID(const PString &str)
PString GetString() const
Return the value of the object as a PString.
ASN Sequence object.
Definition: pasn.h:76
PASNObject::ASNType GetType() const
Return a value of type enum ASNType which indicates the type of the object.
Definition: pasn.h:330
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<< 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:292
PASNObject & operator[](PINDEX idx) const
void Append(PASNObject *obj)
PARRAY(PASNObjectArray, PASNObject)
void PrintOn(ostream &strm) const
Virtual functions used by the PObject::operator<< 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:520
static WORD GetASNLengthLength(WORD length)
Return the length of an encoded ASN length value.
PBoolean Decode(const PBYTEArray &buffer, PINDEX &i)
Definition: pasn.h:445
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:73
unknown ASN object type
Definition: pasn.h:87
Ultimate parent class for all objects in the class library.
Definition: object.h:1118
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:44
ASN NsapAddress.
Definition: pasn.h:83
#define PNEW
Macro for overriding system default new operator.
Definition: object.h:890
PObject * Clone() const
Virtual function used to duplicate objects.
Definition: pasn.h:340
A descendant of PASNObject which is the NULL type.
Definition: pasn.h:496
PASNObject()
Create an empty ASN object.
A descendant of PASNObject which is an IP address type.
Definition: pasn.h:315
ASN TimeTicks object.
Definition: pasn.h:81
int GetChoice() const
PString GetTypeAsString() const
Return a string giving the type of the object.