Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Class Members | File Members

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  * $Log: pasn.h,v $
00027  * Revision 1.13  2002/11/06 22:47:24  robertj
00028  * Fixed header comment (copyright etc)
00029  *
00030  * Revision 1.12  2002/09/16 01:08:59  robertj
00031  * Added #define so can select if #pragma interface/implementation is used on
00032  *   platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan.
00033  *
00034  * Revision 1.11  1999/05/01 03:52:20  robertj
00035  * Fixed various egcs warnings.
00036  *
00037  * Revision 1.10  1999/03/09 08:01:46  robertj
00038  * Changed comments for doc++ support (more to come).
00039  *
00040  * Revision 1.9  1999/02/16 08:07:10  robertj
00041  * MSVC 6.0 compatibility changes.
00042  *
00043  * Revision 1.8  1998/11/30 02:50:54  robertj
00044  * New directory structure
00045  *
00046  * Revision 1.7  1998/09/23 06:19:44  robertj
00047  * Added open source copyright license.
00048  *
00049  * Revision 1.6  1997/08/20 08:48:56  craigs
00050  * Added GetString() to PASNNull
00051  *
00052  * Revision 1.5  1997/07/20 08:34:14  craigs
00053  * Added ASN NULL type
00054  *
00055  * Revision 1.4  1997/07/16 05:51:08  craigs
00056  * Added PASNString constructor with ptr and length
00057  *
00058  * Revision 1.3  1996/11/04 09:45:08  robertj
00059  * Fixed bug in IP number ASN type, should be binary not dot format string.
00060  *
00061  * Revision 1.2  1996/11/04 03:56:00  robertj
00062  * Added ASN types to class.
00063  *
00064  * Revision 1.1  1996/09/14 12:58:57  robertj
00065  * Initial revision
00066  *
00067  */
00068 
00069 #ifndef _PASN_H
00070 #define _PASN_H
00071 
00072 #ifdef P_USE_PRAGMA
00073 #pragma interface
00074 #endif
00075 
00076 #include <ptlib/sockets.h>
00077 
00078 //
00079 // define some types used by the ASN classes
00080 //
00081 typedef PInt32          PASNInt;
00082 typedef DWORD           PASNUnsigned;
00083 typedef DWORD           PASNOid;
00084 
00085 class PASNObject;
00086 class PASNSequence;
00087 
00088 PLIST(PASNObjectList, PASNObject);
00089 
00090 
00092 
00102 class PASNObject : public PObject
00103 {
00104   PCLASSINFO(PASNObject, PObject)
00105 
00106   public:
00110     enum ASNType {
00111       Integer,          // ASN Integer object
00112       String,       // ASN Octet String object
00113       ObjectID,         // ASN Object ID object
00114       Sequence,         // ASN Sequence object
00115       Choice,           // ASN Sequence with discriminator
00116       IPAddress,    // ASN IPAddress object
00117       Counter,      // ASN Counter object
00118       Gauge,        // ASN Gauge object
00119       TimeTicks,    // ASN TimeTicks object
00120       Opaque,       // ASN Opaque object
00121       NsapAddress,  // ASN NsapAddress
00122       Counter64,    // ASN Counter64
00123       UInteger32,   // ASN Unsigned integer 32
00124           Null,         // ASN Null
00125       Unknown,          // unknown ASN object type
00126       ASNTypeMax        // maximum of number of ASN object types
00127     };
00128 
00132     virtual ASNType GetType() const;
00133 
00134 
00137     int GetChoice() const;
00138 
00140     virtual PString GetTypeAsString() const;
00141 
00147     virtual PASNInt GetInteger () const;
00148 
00154     virtual PASNUnsigned GetUnsigned () const;
00155 
00159     virtual PString GetString () const;
00160 
00166     virtual const PASNSequence & GetSequence() const;
00167 
00173     virtual PIPSocket::Address GetIPAddress () const;
00174 
00178     virtual void PrintOn(
00179       ostream & strm            // stream to print on
00180     ) const;
00181 
00183     virtual void Encode(
00184       PBYTEArray & buffer       // buffer to encode into
00185     );
00186 
00190     virtual WORD GetEncodedLength();
00191 
00193     virtual PObject * Clone() const;
00194 
00196     static void EncodeASNLength (
00197       PBYTEArray & buffer,              // buffer to encode into
00198       WORD length                       // ASN length to encode
00199     );
00200 
00202     static WORD GetASNLengthLength (
00203       WORD length                       // length to find length of
00204     );
00205 
00209     static BOOL DecodeASNLength (
00210       const PBYTEArray & buffer,                // buffer to decode data from
00211       PINDEX & ptr,                     // ptr to decode from
00212       WORD & len                        // returned length
00213     );
00214 
00216     static void EncodeASNSequenceStart (
00217       PBYTEArray & buffer,              // buffer to encode data into
00218       BYTE type,                        // sequence type
00219       WORD length                       // length of sequence data
00220     );
00221 
00223     static WORD GetASNSequenceStartLength (
00224       WORD length                       // length of sequence data
00225     );
00226 
00228     static void EncodeASNHeader(
00229       PBYTEArray & buffer,              // buffer to encode into
00230       PASNObject::ASNType type,         // ASN type of the object
00231       WORD length                       // length of the object
00232     );
00233 
00235     static WORD GetASNHeaderLength (
00236       WORD length                       // length of object
00237     );
00238 
00239     static void EncodeASNInteger    (
00240       PBYTEArray & buffer,              // buffer to encode into
00241       PASNInt data,                     // value to encode
00242       PASNObject::ASNType type          // actual integer type
00243     );
00244     // Encode an ASN integer value into the specified buffer */
00245 
00246     static void EncodeASNUnsigned (
00247       PBYTEArray & buffer,              // buffer to encode into
00248       PASNUnsigned data,                // value to encode
00249       PASNObject::ASNType type          // actual integer type
00250     );
00251     // Encode an ASN integer value into the specified buffer */
00252 
00253     static WORD GetASNIntegerLength (
00254       PASNInt data                      // value to get length of
00255     );
00256     // Return the length of an encoded ASN integer with the specified value 
00257 
00258     static WORD GetASNUnsignedLength (
00259       PASNUnsigned data                 // value to get length of
00260     );
00261     // Return the length of an encoded ASN integer with the specified value 
00262 
00263     static BOOL DecodeASNInteger (
00264       const PBYTEArray & buffer,                // buffer to decode from
00265       PINDEX & ptr,                     // ptr to data in buffer
00266       PASNInt & value,                  // returned value
00267       ASNType type = Integer            // actual integer type
00268     );
00269     // Decode an ASN integer value in the specified buffer 
00270 
00271     static BOOL DecodeASNUnsigned (
00272       const PBYTEArray & buffer,                // buffer to decode from
00273       PINDEX & ptr,                     // ptr to data in buffer
00274       PASNUnsigned & value,             // returned value
00275       ASNType type = TimeTicks          // actual integer type
00276     );
00277     // Decode an ASN integer value in the specified buffer 
00278 
00279   protected:
00281     PASNObject();
00282 
00284     static BYTE ASNTypeToType[ASNTypeMax];
00285 
00286 };
00287 
00288 
00290 
00293 class PASNInteger : public PASNObject
00294 {
00295   PCLASSINFO(PASNInteger, PASNObject)
00296   public:
00297     PASNInteger(PASNInt val);
00298     PASNInteger(const PBYTEArray & buffer, PINDEX & ptr);
00299 
00300     void PrintOn(ostream & strm) const;
00301     void Encode(PBYTEArray & buffer);
00302     WORD GetEncodedLength();
00303     PObject * Clone() const;
00304 
00305     PASNInt GetInteger() const;
00306     PString GetString () const;
00307     ASNType GetType() const;
00308     PString GetTypeAsString() const;
00309 
00310   private:
00311     PASNInt value;
00312 };
00313 
00314 
00316 
00319 class PASNString : public PASNObject
00320 {
00321   PCLASSINFO(PASNString, PASNObject)
00322   public:
00323     PASNString(const PString & str);
00324     PASNString(const BYTE * ptr, int len);
00325     PASNString(const PBYTEArray & buffer,               PASNObject::ASNType = String);
00326     PASNString(const PBYTEArray & buffer, PINDEX & ptr, PASNObject::ASNType = String);
00327 
00328     void PrintOn(ostream & strm) const;
00329 
00330     void Encode(PBYTEArray & buffer)
00331       { Encode(buffer, String); }
00332 
00333     WORD GetEncodedLength();
00334     PObject * Clone() const;
00335 
00336     PString GetString() const;
00337     ASNType GetType() const;
00338     PString GetTypeAsString() const;
00339 
00340   protected:
00341     BOOL Decode(const PBYTEArray & buffer, PINDEX & i, PASNObject::ASNType type);
00342     void Encode(PBYTEArray & buffer,             PASNObject::ASNType type);
00343 
00344     PString value;
00345     WORD    valueLen;
00346 };
00347 
00348 
00350 
00353 class PASNIPAddress : public PASNString
00354 {
00355   PCLASSINFO(PASNIPAddress, PASNString)
00356   public:
00357     PASNIPAddress(const PIPSocket::Address & addr)
00358       : PASNString(PString((const char *)&addr, 4)) { }
00359 
00360     PASNIPAddress(const PString & str);
00361 
00362     PASNIPAddress(const PBYTEArray & buffer)
00363       : PASNString(buffer, IPAddress) { }
00364 
00365     PASNIPAddress(const PBYTEArray & buffer, PINDEX & ptr)
00366       : PASNString(buffer, ptr, IPAddress) { }
00367 
00368     PASNObject::ASNType GetType() const
00369       { return IPAddress; }
00370 
00371     void Encode(PBYTEArray & buffer)
00372       { PASNString::Encode(buffer, IPAddress); }
00373 
00374     PString GetString() const;
00375 
00376     PString GetTypeAsString() const;
00377 
00378     PObject * Clone() const
00379       { return PNEW PASNIPAddress(*this); }
00380 
00381     PIPSocket::Address GetIPAddress () const;
00382 };
00383 
00384 
00386 
00389 class PASNUnsignedInteger : public PASNObject
00390 {
00391   PCLASSINFO(PASNUnsignedInteger, PASNObject)
00392   public:
00393     PASNUnsignedInteger(PASNUnsigned val)
00394       { value = val; }
00395 
00396     PASNUnsignedInteger(const PBYTEArray & buffer, PINDEX & ptr);
00397 
00398     void PrintOn(ostream & strm) const;
00399     WORD GetEncodedLength();
00400     PString GetString () const;
00401     PASNUnsigned GetUnsigned() const;
00402 
00403   protected:
00404     PASNUnsignedInteger()
00405       { value = 0; }
00406 
00407     BOOL Decode(const PBYTEArray & buffer, PINDEX & i, PASNObject::ASNType theType);
00408     void Encode(PBYTEArray & buffer, PASNObject::ASNType theType);
00409 
00410   private:
00411     PASNUnsigned value;
00412 };
00413 
00414 
00416 
00419 class PASNTimeTicks : public PASNUnsignedInteger
00420 {
00421   PCLASSINFO(PASNTimeTicks, PASNUnsignedInteger)
00422   public:
00423     PASNTimeTicks(PASNUnsigned val) 
00424       : PASNUnsignedInteger(val) { }
00425 
00426     PASNTimeTicks(const PBYTEArray & buffer, PINDEX & ptr)
00427       { PASNUnsignedInteger::Decode(buffer, ptr, TimeTicks); }
00428 
00429     void Encode(PBYTEArray & buffer)
00430       { PASNUnsignedInteger::Encode(buffer, TimeTicks); }
00431 
00432     PObject * Clone() const
00433       { return PNEW PASNTimeTicks(*this); }
00434 
00435     PASNObject::ASNType GetType() const
00436       { return TimeTicks; }
00437 
00438     PString GetTypeAsString() const;
00439 };
00440 
00441 
00443 
00446 class PASNCounter : public PASNUnsignedInteger
00447 {
00448   PCLASSINFO(PASNCounter, PASNUnsignedInteger)
00449   public:
00450     PASNCounter(PASNUnsigned val) 
00451       : PASNUnsignedInteger(val) { }
00452 
00453     PASNCounter(const PBYTEArray & buffer, PINDEX & ptr)
00454       {  PASNUnsignedInteger::Decode(buffer, ptr, Counter); }
00455 
00456     void Encode(PBYTEArray & buffer)
00457       { PASNUnsignedInteger::Encode(buffer, Counter); }
00458 
00459     PObject * Clone() const
00460       { return PNEW PASNCounter(*this); }
00461 
00462     PASNObject::ASNType GetType() const
00463       { return Counter; }
00464 
00465     PString GetTypeAsString() const;
00466 };
00467 
00468 
00470 
00473 class PASNGauge : public PASNUnsignedInteger
00474 {
00475   PCLASSINFO(PASNGauge, PASNUnsignedInteger)
00476   public:
00477     PASNGauge(PASNUnsigned val) 
00478       : PASNUnsignedInteger(val) { }
00479 
00480     PASNGauge(const PBYTEArray & buffer, PINDEX & ptr)
00481       { Decode(buffer, ptr); }
00482 
00483     BOOL Decode(const PBYTEArray & buffer, PINDEX & i)
00484       { return PASNUnsignedInteger::Decode(buffer, i, Gauge); }
00485 
00486     void Encode(PBYTEArray & buffer)
00487       { PASNUnsignedInteger::Encode(buffer, Gauge); }
00488 
00489     PObject * Clone() const
00490       { return PNEW PASNGauge(*this); }
00491 
00492     PASNObject::ASNType GetType() const
00493       { return Gauge; }
00494 
00495     PString GetTypeAsString() const;
00496 };
00497 
00498 
00499 
00501 
00504 class PASNObjectID : public PASNObject
00505 {
00506   PCLASSINFO(PASNObjectID, PASNObject)
00507   public:
00508     PASNObjectID(const PString & str);
00509     PASNObjectID(PASNOid * val, BYTE theLen);
00510     PASNObjectID(const PBYTEArray & buffer);
00511     PASNObjectID(const PBYTEArray & buffer, PINDEX & ptr);
00512 
00513     void PrintOn(ostream & strm) const;
00514     void Encode(PBYTEArray & buffer);
00515     WORD GetEncodedLength();
00516     PObject * Clone() const;
00517 
00518     ASNType GetType() const;
00519     PString GetString () const;
00520     PString GetTypeAsString() const;
00521 
00522   protected:
00523     BOOL Decode(const PBYTEArray & buffer, PINDEX & i);
00524 
00525   private:
00526     PDWORDArray value;
00527 };
00528 
00529 
00531 
00534 class PASNNull : public PASNObject
00535 {
00536   PCLASSINFO(PASNNull, PASNObject)
00537   public:
00538     PASNNull();
00539     PASNNull(const PBYTEArray & buffer, PINDEX & ptr);
00540 
00541     void PrintOn(ostream & strm) const;
00542 
00543     void Encode(PBYTEArray & buffer);
00544     WORD GetEncodedLength();
00545 
00546     PObject * Clone() const;
00547 
00548     ASNType GetType() const;
00549     PString GetString () const;
00550     PString GetTypeAsString() const;
00551 };
00552 
00553 
00555 
00558 class PASNSequence : public PASNObject
00559 {
00560   PCLASSINFO(PASNSequence, PASNObject)
00561   public:
00562     PASNSequence();
00563     PASNSequence(BYTE selector);
00564     PASNSequence(const PBYTEArray & buffer);
00565     PASNSequence(const PBYTEArray & buffer, PINDEX & i);
00566 
00567     void Append(PASNObject * obj);
00568     PINDEX GetSize() const;
00569     PASNObject & operator [] (PINDEX idx) const;
00570     const PASNSequence & GetSequence() const;
00571 
00572     void AppendInteger (PASNInt value);
00573     void AppendString  (const PString & str);
00574     void AppendObjectID(const PString & str);
00575     void AppendObjectID(PASNOid * val, BYTE len);
00576 
00577     int GetChoice() const;
00578 
00579 //    PASNInt GetInteger (PINDEX idx) const;
00580 //    PString GetString  (PINDEX idx) const;
00581 
00582     void PrintOn(ostream & strm) const;
00583     void Encode(PBYTEArray & buffer);
00584     BOOL Decode(const PBYTEArray & buffer, PINDEX & i);
00585     WORD GetEncodedLength();
00586     ASNType GetType() const;
00587     PString GetTypeAsString() const;
00588 
00589     BOOL Encode(PBYTEArray & buffer, PINDEX maxLen) ;
00590 
00591   private:
00592     PASNObjectList sequence;
00593     BYTE     type;
00594     ASNType  asnType;
00595     WORD     encodedLen;
00596     WORD     seqLen;
00597 };
00598 
00599 #endif
00600 
00601 
00602 // End of File.
00603 

Generated on Mon Feb 21 20:43:09 2005 for PWLib by  doxygen 1.4.1