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