# pasn.h

Go to the documentation of this file.

    00001 /*
    00002  * pasn.h
    00003  *
    00004  * Abstract Syntax Notation 1 classes for support of SNMP only.
    00005  *
    00006  * Portable Windows Library
    00007  *
    00008  * Copyright (c) 1993-2002 Equivalence Pty. Ltd.
    00009  *
    00010  * The contents of this file are subject to the Mozilla Public License
    00011  * Version 1.0 (the "License"); you may not use this file except in
    00012  * compliance with the License. You may obtain a copy of the License at
    00013  * http://www.mozilla.org/MPL/
    00014  *
    00015  * Software distributed under the License is distributed on an "AS IS"
    00016  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
    00017  * the License for the specific language governing rights and limitations
    00018  * under the License.
    00019  *
    00020  * The Original Code is Portable Windows Library.
    00021  *
    00022  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
    00023  *
    00024  * Contributor(s): ______________________________________.
    00025  *
    00026  * $Revision: 21788 $
    00027  * $Author: rjongbloed $
    00028  * $Date: 2008-12-11 23:42:13 -0600 (Thu, 11 Dec 2008) $
    00029  */
    00030
    00031 #ifndef PTLIB_PASN_H
    00032 #define PTLIB_PASN_H
    00033
    00034 #ifdef P_USE_PRAGMA
    00035 #pragma interface
    00036 #endif
    00037
    00038 #include <ptlib/sockets.h>
    00039
    00040 //
    00041 // define some types used by the ASN classes
    00042 //
    00043 typedef PInt32  PASNInt;
    00044 typedef DWORD   PASNUnsigned;
    00045 typedef DWORD           PASNOid;
    00046
    00047 class PASNObject;
    00048 class PASNSequence;
    00049
    00050 PARRAY(PASNObjectArray, PASNObject);
    00051
    00052
    00054
    00064 class PASNObject : public PObject
    00065 {
    00066   PCLASSINFO(PASNObject, PObject)
    00067
    00068   public:
    00072     enum ASNType {
    00073       Integer,
    00074       String,
    00075       ObjectID,
    00076       Sequence,
    00077       Choice,
    00078       IPAddress,
    00079       Counter,
    00080       Gauge,
    00081       TimeTicks,
    00082       Opaque,
    00083       NsapAddress,
    00084       Counter64,
    00085       UInteger32,
    00086       Null,
    00087       Unknown,
    00088       ASNTypeMax
    00089     };
    00090
    00094     virtual ASNType GetType() const;
    00095
    00096
    00099     int GetChoice() const;
    00100
    00102     virtual PString GetTypeAsString() const;
    00103
    00109     virtual PASNInt GetInteger () const;
    00110
    00116     virtual PASNUnsigned GetUnsigned () const;
    00117
    00121     virtual PString GetString () const;
    00122
    00128     virtual const PASNSequence & GetSequence() const;
    00129
    00135     virtual PIPSocket::Address GetIPAddress () const;
    00136
    00140     virtual void PrintOn(
    00141       ostream & strm
    00142     ) const;
    00143
    00145     virtual void Encode(
    00146       PBYTEArray & buffer
    00147     );
    00148
    00152     virtual WORD GetEncodedLength();
    00153
    00155     virtual PObject * Clone() const;
    00156
    00158     static void EncodeASNLength (
    00159       PBYTEArray & buffer,
    00160       WORD length
    00161     );
    00162
    00164     static WORD GetASNLengthLength (
    00165       WORD length
    00166     );
    00167
    00171     static PBoolean DecodeASNLength (
    00172       const PBYTEArray & buffer,
    00173       PINDEX & ptr,
    00174       WORD & len
    00175     );
    00176
    00178     static void EncodeASNSequenceStart (
    00179       PBYTEArray & buffer,
    00180       BYTE type,
    00181       WORD length
    00182     );
    00183
    00185     static WORD GetASNSequenceStartLength (
    00186       WORD length
    00187     );
    00188
    00190     static void EncodeASNHeader(
    00191       PBYTEArray & buffer,
    00192       PASNObject::ASNType type,
    00193       WORD length
    00194     );
    00195
    00197     static WORD GetASNHeaderLength (
    00198       WORD length
    00199     );
    00200
    00201     static void EncodeASNInteger    (
    00202       PBYTEArray & buffer,
    00203       PASNInt data,
    00204       PASNObject::ASNType type
    00205     );
    00206     // Encode an ASN integer value into the specified buffer */
    00207
    00208     static void EncodeASNUnsigned (
    00209       PBYTEArray & buffer,
    00210       PASNUnsigned data,
    00211       PASNObject::ASNType type
    00212     );
    00213     // Encode an ASN integer value into the specified buffer */
    00214
    00215     static WORD GetASNIntegerLength (
    00216       PASNInt data
    00217     );
    00218     // Return the length of an encoded ASN integer with the specified value
    00219
    00220     static WORD GetASNUnsignedLength (
    00221       PASNUnsigned data
    00222     );
    00223     // Return the length of an encoded ASN integer with the specified value
    00224
    00225     static PBoolean DecodeASNInteger (
    00226       const PBYTEArray & buffer,
    00227       PINDEX & ptr,
    00228       PASNInt & value,
    00229       ASNType type = Integer
    00230     );
    00231     // Decode an ASN integer value in the specified buffer
    00232
    00233     static PBoolean DecodeASNUnsigned (
    00234       const PBYTEArray & buffer,
    00235       PINDEX & ptr,
    00236       PASNUnsigned & value,
    00237       ASNType type = TimeTicks
    00238     );
    00239     // Decode an ASN integer value in the specified buffer
    00240
    00241   protected:
    00243     PASNObject();
    00244
    00246     static BYTE ASNTypeToType[ASNTypeMax];
    00247
    00248 };
    00249
    00250
    00252
    00255 class PASNInteger : public PASNObject
    00256 {
    00257   PCLASSINFO(PASNInteger, PASNObject)
    00258   public:
    00259     PASNInteger(PASNInt val);
    00260     PASNInteger(const PBYTEArray & buffer, PINDEX & ptr);
    00261
    00262     void PrintOn(ostream & strm) const;
    00263     void Encode(PBYTEArray & buffer);
    00264     WORD GetEncodedLength();
    00265     PObject * Clone() const;
    00266
    00267     PASNInt GetInteger() const;
    00268     PString GetString () const;
    00269     ASNType GetType() const;
    00270     PString GetTypeAsString() const;
    00271
    00272   private:
    00273     PASNInt value;
    00274 };
    00275
    00276
    00278
    00281 class PASNString : public PASNObject
    00282 {
    00283   PCLASSINFO(PASNString, PASNObject)
    00284   public:
    00285     PASNString(const PString & str);
    00286     PASNString(const BYTE * ptr, int len);
    00287     PASNString(const PBYTEArray & buffer,               PASNObject::ASNType = String);
    00288     PASNString(const PBYTEArray & buffer, PINDEX & ptr, PASNObject::ASNType = String);
    00289
    00290     void PrintOn(ostream & strm) const;
    00291
    00292     void Encode(PBYTEArray & buffer)
    00293       { Encode(buffer, String); }
    00294
    00295     WORD GetEncodedLength();
    00296     PObject * Clone() const;
    00297
    00298     PString GetString() const;
    00299     ASNType GetType() const;
    00300     PString GetTypeAsString() const;
    00301
    00302   protected:
    00303     PBoolean Decode(const PBYTEArray & buffer, PINDEX & i, PASNObject::ASNType type);
    00304     void Encode(PBYTEArray & buffer,             PASNObject::ASNType type);
    00305
    00306     PString value;
    00307     WORD    valueLen;
    00308 };
    00309
    00310
    00312
    00315 class PASNIPAddress : public PASNString
    00316 {
    00317   PCLASSINFO(PASNIPAddress, PASNString)
    00318   public:
    00319     PASNIPAddress(const PIPSocket::Address & addr)
    00320       : PASNString((const BYTE *)addr.GetPointer(), addr.GetSize()) { }
    00321
    00322     PASNIPAddress(const PString & str);
    00323
    00324     PASNIPAddress(const PBYTEArray & buffer)
    00325       : PASNString(buffer, IPAddress) { }
    00326
    00327     PASNIPAddress(const PBYTEArray & buffer, PINDEX & ptr)
    00328       : PASNString(buffer, ptr, IPAddress) { }
    00329
    00330     PASNObject::ASNType GetType() const
    00331       { return IPAddress; }
    00332
    00333     void Encode(PBYTEArray & buffer)
    00334       { PASNString::Encode(buffer, IPAddress); }
    00335
    00336     PString GetString() const;
    00337
    00338     PString GetTypeAsString() const;
    00339
    00340     PObject * Clone() const
    00341       { return PNEW PASNIPAddress(*this); }
    00342
    00343     PIPSocket::Address GetIPAddress () const;
    00344 };
    00345
    00346
    00348
    00351 class PASNUnsignedInteger : public PASNObject
    00352 {
    00353   PCLASSINFO(PASNUnsignedInteger, PASNObject)
    00354   public:
    00355     PASNUnsignedInteger(PASNUnsigned val)
    00356       { value = val; }
    00357
    00358     PASNUnsignedInteger(const PBYTEArray & buffer, PINDEX & ptr);
    00359
    00360     void PrintOn(ostream & strm) const;
    00361     WORD GetEncodedLength();
    00362     PString GetString () const;
    00363     PASNUnsigned GetUnsigned() const;
    00364
    00365   protected:
    00366     PASNUnsignedInteger()
    00367       { value = 0; }
    00368
    00369     PBoolean Decode(const PBYTEArray & buffer, PINDEX & i, PASNObject::ASNType theType);
    00370     void Encode(PBYTEArray & buffer, PASNObject::ASNType theType);
    00371
    00372   private:
    00373     PASNUnsigned value;
    00374 };
    00375
    00376
    00378
    00381 class PASNTimeTicks : public PASNUnsignedInteger
    00382 {
    00383   PCLASSINFO(PASNTimeTicks, PASNUnsignedInteger)
    00384   public:
    00385     PASNTimeTicks(PASNUnsigned val)
    00386       : PASNUnsignedInteger(val) { }
    00387
    00388     PASNTimeTicks(const PBYTEArray & buffer, PINDEX & ptr)
    00389       { PASNUnsignedInteger::Decode(buffer, ptr, TimeTicks); }
    00390
    00391     void Encode(PBYTEArray & buffer)
    00392       { PASNUnsignedInteger::Encode(buffer, TimeTicks); }
    00393
    00394     PObject * Clone() const
    00395       { return PNEW PASNTimeTicks(*this); }
    00396
    00397     PASNObject::ASNType GetType() const
    00398       { return TimeTicks; }
    00399
    00400     PString GetTypeAsString() const;
    00401 };
    00402
    00403
    00405
    00408 class PASNCounter : public PASNUnsignedInteger
    00409 {
    00410   PCLASSINFO(PASNCounter, PASNUnsignedInteger)
    00411   public:
    00412     PASNCounter(PASNUnsigned val)
    00413       : PASNUnsignedInteger(val) { }
    00414
    00415     PASNCounter(const PBYTEArray & buffer, PINDEX & ptr)
    00416       {  PASNUnsignedInteger::Decode(buffer, ptr, Counter); }
    00417
    00418     void Encode(PBYTEArray & buffer)
    00419       { PASNUnsignedInteger::Encode(buffer, Counter); }
    00420
    00421     PObject * Clone() const
    00422       { return PNEW PASNCounter(*this); }
    00423
    00424     PASNObject::ASNType GetType() const
    00425       { return Counter; }
    00426
    00427     PString GetTypeAsString() const;
    00428 };
    00429
    00430
    00432
    00435 class PASNGauge : public PASNUnsignedInteger
    00436 {
    00437   PCLASSINFO(PASNGauge, PASNUnsignedInteger)
    00438   public:
    00439     PASNGauge(PASNUnsigned val)
    00440       : PASNUnsignedInteger(val) { }
    00441
    00442     PASNGauge(const PBYTEArray & buffer, PINDEX & ptr)
    00443       { Decode(buffer, ptr); }
    00444
    00445     PBoolean Decode(const PBYTEArray & buffer, PINDEX & i)
    00446       { return PASNUnsignedInteger::Decode(buffer, i, Gauge); }
    00447
    00448     void Encode(PBYTEArray & buffer)
    00449       { PASNUnsignedInteger::Encode(buffer, Gauge); }
    00450
    00451     PObject * Clone() const
    00452       { return PNEW PASNGauge(*this); }
    00453
    00454     PASNObject::ASNType GetType() const
    00455       { return Gauge; }
    00456
    00457     PString GetTypeAsString() const;
    00458 };
    00459
    00460
    00461
    00463
    00466 class PASNObjectID : public PASNObject
    00467 {
    00468   PCLASSINFO(PASNObjectID, PASNObject)
    00469   public:
    00470     PASNObjectID(const PString & str);
    00471     PASNObjectID(PASNOid * val, BYTE theLen);
    00472     PASNObjectID(const PBYTEArray & buffer);
    00473     PASNObjectID(const PBYTEArray & buffer, PINDEX & ptr);
    00474
    00475     void PrintOn(ostream & strm) const;
    00476     void Encode(PBYTEArray & buffer);
    00477     WORD GetEncodedLength();
    00478     PObject * Clone() const;
    00479
    00480     ASNType GetType() const;
    00481     PString GetString () const;
    00482     PString GetTypeAsString() const;
    00483
    00484   protected:
    00485     PBoolean Decode(const PBYTEArray & buffer, PINDEX & i);
    00486
    00487   private:
    00488     PDWORDArray value;
    00489 };
    00490
    00491
    00493
    00496 class PASNNull : public PASNObject
    00497 {
    00498   PCLASSINFO(PASNNull, PASNObject)
    00499   public:
    00500     PASNNull();
    00501     PASNNull(const PBYTEArray & buffer, PINDEX & ptr);
    00502
    00503     void PrintOn(ostream & strm) const;
    00504
    00505     void Encode(PBYTEArray & buffer);
    00506     WORD GetEncodedLength();
    00507
    00508     PObject * Clone() const;
    00509
    00510     ASNType GetType() const;
    00511     PString GetString () const;
    00512     PString GetTypeAsString() const;
    00513 };
    00514
    00515
    00517
    00520 class PASNSequence : public PASNObject
    00521 {
    00522   PCLASSINFO(PASNSequence, PASNObject)
    00523   public:
    00524     PASNSequence();
    00525     PASNSequence(BYTE selector);
    00526     PASNSequence(const PBYTEArray & buffer);
    00527     PASNSequence(const PBYTEArray & buffer, PINDEX & i);
    00528
    00529     void Append(PASNObject * obj);
    00530     PINDEX GetSize() const;
    00531     PASNObject & operator [] (PINDEX idx) const;
    00532     const PASNSequence & GetSequence() const;
    00533
    00534     void AppendInteger (PASNInt value);
    00535     void AppendString  (const PString & str);
    00536     void AppendObjectID(const PString & str);
    00537     void AppendObjectID(PASNOid * val, BYTE len);
    00538
    00539     int GetChoice() const;
    00540
    00541 //    PASNInt GetInteger (PINDEX idx) const;
    00542 //    PString GetString  (PINDEX idx) const;
    00543
    00544     void PrintOn(ostream & strm) const;
    00545     void Encode(PBYTEArray & buffer);
    00546     PBoolean Decode(const PBYTEArray & buffer, PINDEX & i);
    00547     WORD GetEncodedLength();
    00548     ASNType GetType() const;
    00549     PString GetTypeAsString() const;
    00550
    00551     PBoolean Encode(PBYTEArray & buffer, PINDEX maxLen) ;
    00552
    00553   private:
    00554     PASNObjectArray sequence;
    00555     BYTE     type;
    00556     ASNType  asnType;
    00557     WORD     encodedLen;
    00558     WORD     seqLen;
    00559 };
    00560
    00561
    00562 #endif // PTLIB_PASN_H
    00563
    00564
    00565 // End Of File ///////////////////////////////////////////////////////////////