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

asner.h

Go to the documentation of this file.
00001 /*
00002  * asner.h
00003  *
00004  * Abstract Syntax Notation Encoding Rules classes
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: asner.h,v $
00027  * Revision 1.44  2004/11/11 07:34:50  csoutheren
00028  * Added #include <ptlib.h>
00029  *
00030  * Revision 1.43  2004/04/18 04:33:35  rjongbloed
00031  * Changed all operators that return BOOL to return standard type bool. This is primarily
00032  *   for improved compatibility with std STL usage removing many warnings.
00033  *
00034  * Revision 1.42  2003/12/14 10:21:29  rjongbloed
00035  * Fixed bug in length incorrectlty decoded from ASN and (apparently) rare circumstances. Thanks pangxg@hotmail.com.
00036  * Cleaned up return values to be BOOL rather than int for some functions.
00037  *
00038  * Revision 1.41  2003/08/18 23:32:22  rjongbloed
00039  * Micro optimisation suggested by Chih-Wei Huang
00040  *
00041  * Revision 1.40  2003/08/01 16:00:51  csoutheren
00042  * Changed #if to #ifdef to (maybe) avoid compiler problems with gcc 2.95.2
00043  *
00044  * Revision 1.39  2003/08/01 02:12:34  csoutheren
00045  * Changed to allow easy isolation of PER, BER and XER encoding/decoding routines
00046  *
00047  * Revision 1.38  2003/04/22 23:39:09  craigs
00048  * Changed some functions from protected to public for MacOSX. Thanks to Hugo Santos
00049  *
00050  * Revision 1.37  2003/04/17 14:44:44  craigs
00051  * Removed MacOS specific defines to make some attributes public
00052  * Thanks to Hugo Santos and apologies to Roger Hardiman
00053  *
00054  * Revision 1.36  2003/02/26 01:57:44  robertj
00055  * Added XML encoding rules to ASN system, thanks Federico Pinna
00056  *
00057  * Revision 1.35  2003/02/01 13:25:52  robertj
00058  * Added function to add new elements directly to ASN array.
00059  *
00060  * Revision 1.34  2003/01/24 23:43:43  robertj
00061  * Fixed subtle problems with the use of MAX keyword for unsigned numbers,
00062  *   should beUINT_MAX not INT_MAX, thanks Stevie Gray for pointing it out.
00063  *
00064  * Revision 1.33  2002/11/26 23:29:18  robertj
00065  * Added missing const to DecodeSubType() function.
00066  *
00067  * Revision 1.32  2002/11/06 22:47:23  robertj
00068  * Fixed header comment (copyright etc)
00069  *
00070  * Revision 1.31  2002/10/31 05:50:49  robertj
00071  * Changed to use new UTF-8/UCS-2 conversion functions on PString.
00072  *
00073  * Revision 1.30  2002/10/10 14:37:40  rogerh
00074  * In two of the PASN classes make the protected members public. This
00075  * makes OpenH323 compile in Mac OS X 10.2.1
00076  *
00077  * Revision 1.29  2002/09/16 01:08:59  robertj
00078  * Added #define so can select if #pragma interface/implementation is used on
00079  *   platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan.
00080  *
00081  * Revision 1.28  2002/05/29 01:22:35  robertj
00082  * Added ability to set object id from unsigned integer arrays.
00083  *
00084  * Revision 1.27  2002/05/21 04:23:40  robertj
00085  * Fixed problem with ASN encoding/decoding unsconstrained negative numbers,
00086  *
00087  * Revision 1.26  2002/05/14 06:59:31  robertj
00088  * Added more bullet proofing so a malformed PDU cannot cause teh decoder
00089  *   to try and allocate huge arrays and consume all CPU and memory on a
00090  *   system. A configurable limit of 100 is set for things like SEQUENCE OF.
00091  *
00092  * Revision 1.25  2001/12/13 09:13:28  robertj
00093  * Added function get get oid as a string.
00094  * Added functions to compare oid to PString.
00095  *
00096  * Revision 1.24  2001/09/14 01:59:59  robertj
00097  * Fixed problem with incorrectly initialised PASN_Choice sub-object.
00098  *
00099  * Revision 1.23  2001/08/06 01:39:02  robertj
00100  * Added assignement operator with RHS of PASN_BMPString to classes
00101  *   descended from PASN_BMPString.
00102  *
00103  * Revision 1.22  2001/06/14 02:14:12  robertj
00104  * Added functions to encode and decode another ASN type that is inside
00105  *   an octet string, useful for ANY or EXTERNAL types etc.
00106  *
00107  * Revision 1.21  2001/04/30 06:47:04  robertj
00108  * Fixed problem with en/decoding more than 16 extension fields in a sequence.
00109  *
00110  * Revision 1.20  2001/04/26 08:15:58  robertj
00111  * Fixed problem with ASN compile of single constraints on enumerations.
00112  *
00113  * Revision 1.19  2001/04/23 04:40:14  robertj
00114  * Added ASN standard types GeneralizedTime and UTCTime
00115  *
00116  * Revision 1.18  2001/04/12 03:25:22  robertj
00117  * Fixed PASN_Boolean cosntructor to be compatible with usage in ASN parser.
00118  * Changed all PASN_xxx types so constructor can take real type as only
00119  *   parameter. eg PASN_OctetString s = "fred";
00120  *
00121  * Revision 1.17  2001/03/21 03:32:35  robertj
00122  * Aded ability to get at the data bits buffer in a PASN_BitString
00123  *
00124  * Revision 1.16  2001/01/24 04:36:56  robertj
00125  * Added more bulletproofing to ASN structures to obey constraints.
00126  *
00127  * Revision 1.15  2000/10/26 11:09:07  robertj
00128  * More bullet proofing of PER decoder, changed bit type to be unsigned.
00129  *
00130  * Revision 1.14  2000/10/25 04:05:44  robertj
00131  * More bullet proofing of PER decoder.
00132  *
00133  * Revision 1.13  2000/07/11 18:23:03  robertj
00134  * Added ability to set/get BMP string data as PWORDArray.
00135  *
00136  * Revision 1.12  2000/04/10 17:30:42  robertj
00137  * Added [] operator for char access on ASN string classes.
00138  *
00139  * Revision 1.11  2000/02/29 06:32:12  robertj
00140  * Added ability to remove optional field in sequence, thanks Dave Harvey.
00141  *
00142  * Revision 1.10  1999/08/09 13:02:45  robertj
00143  * dded ASN compiler #defines for backward support of pre GCC 2.9 compilers.
00144  * Added ASN compiler #defines to reduce its memory footprint.
00145  *
00146  * Revision 1.9  1999/07/22 06:48:51  robertj
00147  * Added comparison operation to base ASN classes and compiled ASN code.
00148  * Added support for ANY type in ASN parser.
00149  *
00150  * Revision 1.8  1999/03/09 09:34:05  robertj
00151  * Fixed typo's.
00152  *
00153  * Revision 1.7  1999/03/09 08:01:46  robertj
00154  * Changed comments for doc++ support (more to come).
00155  *
00156  * Revision 1.6  1998/09/23 06:19:21  robertj
00157  * Added open source copyright license.
00158  *
00159  * Revision 1.5  1998/05/21 04:26:53  robertj
00160  * Fixed numerous PER problems.
00161  *
00162  * Revision 1.4  1998/05/07 05:19:28  robertj
00163  * Fixed problems with using copy constructor/assignment oeprator on PASN_Objects.
00164  *
00165  * Revision 1.3  1997/12/18 05:08:13  robertj
00166  * Added function to get choice discriminat`or name.
00167  *
00168  * Revision 1.2  1997/12/11 10:35:42  robertj
00169  * Support for new ASN file parser.
00170  *
00171  */
00172 
00173 #ifndef _ASNER_H
00174 #define _ASNER_H
00175 
00176 #ifdef P_USE_PRAGMA
00177 #pragma interface
00178 #endif
00179 
00180 // provide options to omit vertain encodings, if needed
00181 #define     P_INCLUDE_PER
00182 #define     P_INCLUDE_BER
00183 #define     P_INCLUDE_XER
00184 
00185 class PASN_Stream;
00186 class PBER_Stream;
00187 class PPER_Stream;
00188 
00189 #if P_EXPAT
00190 class PXER_Stream;
00191 class PXMLElement;
00192 #else
00193 #undef      P_INCLUDE_XER
00194 #endif
00195 
00196 #include <ptlib.h>
00197 
00199 
00202 class PASN_Object : public PObject
00203 {
00204     PCLASSINFO(PASN_Object, PObject);
00205   public:
00207     virtual PString GetTypeAsString() const = 0;
00208 
00209     PINDEX GetObjectLength() const;
00210     virtual PINDEX GetDataLength() const = 0;
00211     virtual BOOL IsPrimitive() const { return TRUE; }
00212 
00213     virtual BOOL Decode(PASN_Stream &) = 0;
00214     virtual void Encode(PASN_Stream &) const = 0;
00215 
00216     BOOL IsExtendable() const { return extendable; }
00217     void SetExtendable(BOOL ext = TRUE) { extendable = ext; }
00218 
00219     enum TagClass {
00220       UniversalTagClass,
00221       ApplicationTagClass,
00222       ContextSpecificTagClass,
00223       PrivateTagClass,
00224       DefaultTagClass
00225     };
00226     TagClass GetTagClass() const { return tagClass; }
00227 
00228     enum UniversalTags {
00229       InvalidUniversalTag,
00230       UniversalBoolean,
00231       UniversalInteger,
00232       UniversalBitString,
00233       UniversalOctetString,
00234       UniversalNull,
00235       UniversalObjectId,
00236       UniversalObjectDescriptor,
00237       UniversalExternalType,
00238       UniversalReal,
00239       UniversalEnumeration,
00240       UniversalEmbeddedPDV,
00241       UniversalSequence = 16,
00242       UniversalSet,
00243       UniversalNumericString,
00244       UniversalPrintableString,
00245       UniversalTeletexString,
00246       UniversalVideotexString,
00247       UniversalIA5String,
00248       UniversalUTCTime,
00249       UniversalGeneralisedTime,
00250       UniversalGeneralizedTime = UniversalGeneralisedTime,
00251       UniversalGraphicString,
00252       UniversalVisibleString,
00253       UniversalGeneralString,
00254       UniversalUniversalString,
00255       UniversalBMPString = 30
00256     };
00257 
00258     unsigned GetTag() const  { return tag; }
00259     virtual void SetTag(unsigned newTag, TagClass tagClass = DefaultTagClass);
00260 
00261     enum ConstraintType {
00262       Unconstrained,
00263       PartiallyConstrained,
00264       FixedConstraint,
00265       ExtendableConstraint
00266     };
00267 
00268     enum MinimumValueTag { MinimumValue = INT_MIN };
00269     enum MaximumValueTag { MaximumValue = INT_MAX };
00270     void SetConstraints(ConstraintType type, int value)
00271       { SetConstraintBounds(type, value, value); }
00272     void SetConstraints(ConstraintType, int lower, MaximumValueTag /*upper*/)
00273       { SetConstraintBounds(PartiallyConstrained, (int)lower, lower < 0 ? INT_MAX : UINT_MAX); }
00274     void SetConstraints(ConstraintType, MinimumValueTag lower, unsigned upper)
00275       { SetConstraintBounds(PartiallyConstrained, (int)lower, (unsigned)upper); }
00276     void SetConstraints(ConstraintType, MinimumValueTag lower, MaximumValueTag upper)
00277       { SetConstraintBounds(PartiallyConstrained, (int)lower, (unsigned)upper); }
00278     void SetConstraints(ConstraintType type, int lower, unsigned upper)
00279       { SetConstraintBounds(type, lower, upper); }
00280 
00281     virtual void SetConstraintBounds(ConstraintType type, int lower, unsigned upper);
00282     virtual void SetCharacterSet(ConstraintType ctype, const char * charSet);
00283     virtual void SetCharacterSet(ConstraintType ctype, unsigned firstChar, unsigned lastChar);
00284 
00285     static PINDEX GetMaximumArraySize();
00286     static void SetMaximumArraySize(PINDEX sz);
00287     static PINDEX GetMaximumStringSize();
00288     static void SetMaximumStringSize(PINDEX sz);
00289 
00290   protected:
00291     PASN_Object(unsigned tag, TagClass tagClass, BOOL extend = FALSE);
00292 
00294     BOOL extendable;
00296     TagClass tagClass;
00298     unsigned tag;
00299 };
00300 
00301 
00304 class PASN_ConstrainedObject : public PASN_Object
00305 {
00306     PCLASSINFO(PASN_ConstrainedObject, PASN_Object);
00307   public:
00308     BOOL IsConstrained() const { return constraint != Unconstrained; }
00309     int GetLowerLimit() const { return lowerLimit; }
00310     unsigned GetUpperLimit() const { return upperLimit; }
00311 
00312     BOOL ConstrainedLengthDecode(PPER_Stream & strm, unsigned & length);
00313     void ConstrainedLengthEncode(PPER_Stream & strm, unsigned length) const;
00314 
00315     BOOL ConstraintEncode(PPER_Stream & strm, unsigned value) const;
00316 
00317   protected:
00318     virtual void SetConstraintBounds(ConstraintType type, int lower, unsigned upper);
00319     PASN_ConstrainedObject(unsigned tag, TagClass tagClass);
00320 
00321     ConstraintType constraint;
00322     int lowerLimit;
00323     unsigned upperLimit;
00324 };
00325 
00326 
00329 class PASN_Null : public PASN_Object
00330 {
00331     PCLASSINFO(PASN_Null, PASN_Object);
00332   public:
00333     PASN_Null(unsigned tag = UniversalNull,
00334               TagClass tagClass = UniversalTagClass);
00335 
00336     virtual Comparison Compare(const PObject & obj) const;
00337     virtual PObject * Clone() const;
00338     virtual void PrintOn(ostream & strm) const;
00339 
00340     virtual PString GetTypeAsString() const;
00341     virtual PINDEX GetDataLength() const;
00342     virtual BOOL Decode(PASN_Stream &);
00343     virtual void Encode(PASN_Stream &) const;
00344 };
00345 
00346 
00349 class PASN_Boolean : public PASN_Object
00350 {
00351     PCLASSINFO(PASN_Boolean, PASN_Object);
00352   public:
00353     PASN_Boolean(BOOL val = FALSE);
00354     PASN_Boolean(unsigned tag, TagClass tagClass, BOOL val = FALSE);
00355 
00356     PASN_Boolean & operator=(BOOL v) { value = v; return *this; }
00357     operator BOOL() const { return value; }
00358     BOOL GetValue() const { return value; }
00359     void SetValue(BOOL v) { value = v; }
00360 
00361     virtual Comparison Compare(const PObject & obj) const;
00362     virtual PObject * Clone() const;
00363     virtual void PrintOn(ostream & strm) const;
00364 
00365     virtual PString GetTypeAsString() const;
00366     virtual PINDEX GetDataLength() const;
00367     virtual BOOL Decode(PASN_Stream &);
00368     virtual void Encode(PASN_Stream &) const;
00369 
00370   protected:
00371     BOOL value;
00372 };
00373 
00374 
00377 class PASN_Integer : public PASN_ConstrainedObject
00378 {
00379     PCLASSINFO(PASN_Integer, PASN_ConstrainedObject);
00380   public:
00381     PASN_Integer(unsigned val = 0);
00382     PASN_Integer(unsigned tag, TagClass tagClass, unsigned val = 0);
00383 
00384     PASN_Integer & operator=(unsigned value);
00385     operator unsigned() const { return value; }
00386     unsigned GetValue() const { return value; }
00387     void SetValue(unsigned v) { operator=(v); }
00388 
00389     virtual Comparison Compare(const PObject & obj) const;
00390     virtual PObject * Clone() const;
00391     virtual void PrintOn(ostream & strm) const;
00392 
00393     virtual void SetConstraintBounds(ConstraintType type, int lower, unsigned upper);
00394     virtual PString GetTypeAsString() const;
00395     virtual PINDEX GetDataLength() const;
00396     virtual BOOL Decode(PASN_Stream &);
00397     virtual void Encode(PASN_Stream &) const;
00398 
00399 #ifdef P_INCLUDE_PER
00400     BOOL DecodePER(PPER_Stream & strm);
00401     void EncodePER(PPER_Stream & strm) const;
00402 #endif
00403 
00404     BOOL IsUnsigned() const;
00405 
00406   protected:
00407     unsigned value;
00408 };
00409 
00410 
00413 class PASN_Enumeration : public PASN_Object
00414 {
00415     PCLASSINFO(PASN_Enumeration, PASN_Object);
00416   public:
00417     PASN_Enumeration(unsigned val = 0);
00418     PASN_Enumeration(unsigned tag,
00419                      TagClass tagClass,
00420                      unsigned nEnums = P_MAX_INDEX,
00421                      BOOL extendable = FALSE,
00422                      unsigned val = 0);
00423     PASN_Enumeration(unsigned tag,
00424                      TagClass tagClass,
00425                      unsigned nEnums,
00426                      BOOL extendable,
00427                      const PString & nameSpec,
00428                      unsigned val = 0);
00429 
00430     PASN_Enumeration & operator=(unsigned v) { value = v; return *this; }
00431     operator unsigned() const { return value; }
00432     unsigned GetValue() const { return value; }
00433     void SetValue(unsigned v) { value = v; }
00434 
00435     unsigned GetMaximum() const { return maxEnumValue; }
00436 
00437     virtual Comparison Compare(const PObject & obj) const;
00438     virtual PObject * Clone() const;
00439     virtual void PrintOn(ostream & strm) const;
00440 
00441     virtual PString GetTypeAsString() const;
00442     virtual PINDEX GetDataLength() const;
00443     virtual BOOL Decode(PASN_Stream &);
00444     virtual void Encode(PASN_Stream &) const;
00445 
00446 #ifdef P_INCLUDE_PER
00447     BOOL DecodePER(PPER_Stream & strm);
00448     void EncodePER(PPER_Stream & strm) const;
00449 #endif
00450 
00451 #ifdef P_INCLUDE_XER
00452     virtual BOOL DecodeXER(PXER_Stream & strm);
00453     virtual void EncodeXER(PXER_Stream & strm) const;
00454 #endif
00455 
00456   protected:
00457     unsigned maxEnumValue;
00458     unsigned value;
00459     POrdinalToString names;
00460 };
00461 
00462 
00465 class PASN_Real : public PASN_Object
00466 {
00467     PCLASSINFO(PASN_Real, PASN_Object);
00468   public:
00469     PASN_Real(double val = 0);
00470     PASN_Real(unsigned tag, TagClass tagClass, double val = 0);
00471 
00472     PASN_Real & operator=(double val) { value = val; return *this; }
00473     operator double() const { return value; }
00474     double GetValue() const { return value; }
00475     void SetValue(double v) { value = v; }
00476 
00477     virtual Comparison Compare(const PObject & obj) const;
00478     virtual PObject * Clone() const;
00479     virtual void PrintOn(ostream & strm) const;
00480 
00481     virtual PString GetTypeAsString() const;
00482     virtual PINDEX GetDataLength() const;
00483     virtual BOOL Decode(PASN_Stream &);
00484     virtual void Encode(PASN_Stream &) const;
00485 
00486   protected:
00487     double value;
00488 };
00489 
00490 
00493 class PASN_ObjectId : public PASN_Object
00494 {
00495     PCLASSINFO(PASN_ObjectId, PASN_Object);
00496   public:
00497     PASN_ObjectId(const char * dotstr = NULL);
00498     PASN_ObjectId(unsigned tag, TagClass tagClass);
00499 
00500     PASN_ObjectId(const PASN_ObjectId & other);
00501     PASN_ObjectId & operator=(const PASN_ObjectId & other);
00502 
00503     PASN_ObjectId & operator=(const char * dotstr);
00504     PASN_ObjectId & operator=(const PString & dotstr);
00505     PASN_ObjectId & operator=(const PUnsignedArray & numbers);
00506     void SetValue(const PString & dotstr);
00507     void SetValue(const PUnsignedArray & numbers) { value = numbers; }
00508     void SetValue(const unsigned * numbers, PINDEX size);
00509 
00510     bool operator==(const char * dotstr) const;
00511     bool operator!=(const char * dotstr) const      { return !operator==(dotstr); }
00512     bool operator==(const PString & dotstr) const   { return  operator==((const char *)dotstr); }
00513     bool operator!=(const PString & dotstr) const   { return !operator==((const char *)dotstr); }
00514     bool operator==(const PASN_ObjectId & id) const { return value == id.value; }
00515 
00516     PINDEX GetSize() const { return value.GetSize(); }
00517     unsigned operator[](PINDEX idx) const { return value[idx]; }
00518     const PUnsignedArray & GetValue() const { return value; }
00519     PString AsString() const;
00520 
00521     virtual Comparison Compare(const PObject & obj) const;
00522     virtual PObject * Clone() const;
00523     virtual void PrintOn(ostream & strm) const;
00524 
00525     virtual PString GetTypeAsString() const;
00526     virtual PINDEX GetDataLength() const;
00527     virtual BOOL Decode(PASN_Stream &);
00528     virtual void Encode(PASN_Stream &) const;
00529 
00530     BOOL CommonDecode(PASN_Stream & strm, unsigned dataLen);
00531     void CommonEncode(PBYTEArray & eObjId) const;
00532 
00533   protected:
00534     PUnsignedArray value;
00535 };
00536 
00537 
00540 class PASN_BitString : public PASN_ConstrainedObject
00541 {
00542     PCLASSINFO(PASN_BitString, PASN_ConstrainedObject);
00543   public:
00544     PASN_BitString(unsigned nBits = 0, const BYTE * buf = NULL);
00545     PASN_BitString(unsigned tag, TagClass tagClass, unsigned nBits = 0);
00546 
00547     PASN_BitString(const PASN_BitString & other);
00548     PASN_BitString & operator=(const PASN_BitString & other);
00549 
00550     void SetData(unsigned nBits, const PBYTEArray & bytes);
00551     void SetData(unsigned nBits, const BYTE * buf, PINDEX size = 0);
00552 
00553     const BYTE * GetDataPointer() const { return bitData; }
00554 
00555     unsigned GetSize() const { return totalBits; }
00556     BOOL SetSize(unsigned nBits);
00557 
00558     bool operator[](PINDEX bit) const;
00559     void Set(unsigned bit);
00560     void Clear(unsigned bit);
00561     void Invert(unsigned bit);
00562 
00563     virtual Comparison Compare(const PObject & obj) const;
00564     virtual PObject * Clone() const;
00565     virtual void PrintOn(ostream & strm) const;
00566 
00567     virtual void SetConstraintBounds(ConstraintType type, int lower, unsigned upper);
00568     virtual PString GetTypeAsString() const;
00569     virtual PINDEX GetDataLength() const;
00570     virtual BOOL Decode(PASN_Stream &);
00571     virtual void Encode(PASN_Stream &) const;
00572 
00573 #ifdef P_INCLUDE_BER
00574     BOOL DecodeBER(PBER_Stream & strm, unsigned len);
00575     void EncodeBER(PBER_Stream & strm) const;
00576 #endif
00577 
00578 #ifdef P_INCLUDE_PER
00579     BOOL DecodePER(PPER_Stream & strm);
00580     void EncodePER(PPER_Stream & strm) const;
00581 #endif
00582 
00583     BOOL DecodeSequenceExtensionBitmap(PPER_Stream & strm);
00584     void EncodeSequenceExtensionBitmap(PPER_Stream & strm) const;
00585 
00586   protected:
00587     unsigned totalBits;
00588     PBYTEArray bitData;
00589 };
00590 
00591 
00594 class PASN_OctetString : public PASN_ConstrainedObject
00595 {
00596     PCLASSINFO(PASN_OctetString, PASN_ConstrainedObject);
00597   public:
00598     PASN_OctetString(const char * str = NULL, PINDEX size = 0);
00599     PASN_OctetString(unsigned tag, TagClass tagClass);
00600 
00601     PASN_OctetString(const PASN_OctetString & other);
00602     PASN_OctetString & operator=(const PASN_OctetString & other);
00603 
00604     PASN_OctetString & operator=(const char * str);
00605     PASN_OctetString & operator=(const PString & str);
00606     PASN_OctetString & operator=(const PBYTEArray & arr);
00607     void SetValue(const char * str) { operator=(str); }
00608     void SetValue(const PString & str) { operator=(str); }
00609     void SetValue(const PBYTEArray & arr) { operator=(arr); }
00610     void SetValue(const BYTE * data, PINDEX len);
00611     const PBYTEArray & GetValue() const { return value; }
00612     operator const PBYTEArray &() const { return value; }
00613     operator const BYTE *() const { return value; }
00614     PString AsString() const;
00615     BYTE operator[](PINDEX i) const { return value[i]; }
00616     BYTE & operator[](PINDEX i) { return value[i]; }
00617     BYTE * GetPointer(PINDEX sz = 0) { return value.GetPointer(sz); }
00618     PINDEX GetSize() const { return value.GetSize(); }
00619     BOOL SetSize(PINDEX newSize);
00620 
00621     virtual Comparison Compare(const PObject & obj) const;
00622     virtual PObject * Clone() const;
00623     virtual void PrintOn(ostream & strm) const;
00624 
00625     virtual void SetConstraintBounds(ConstraintType type, int lower, unsigned upper);
00626     virtual PString GetTypeAsString() const;
00627     virtual PINDEX GetDataLength() const;
00628     virtual BOOL Decode(PASN_Stream &);
00629     virtual void Encode(PASN_Stream &) const;
00630 
00631 #ifdef P_INCLUDE_PER
00632     BOOL DecodePER(PPER_Stream & strm);
00633     void EncodePER(PPER_Stream & strm) const;
00634 #endif
00635 
00636     BOOL DecodeSubType(PASN_Object &) const;
00637     void EncodeSubType(const PASN_Object &);
00638 
00639   protected:
00640     PBYTEArray value;
00641 };
00642 
00643 
00646 class PASN_ConstrainedString : public PASN_ConstrainedObject
00647 {
00648     PCLASSINFO(PASN_ConstrainedString, PASN_ConstrainedObject);
00649   public:
00650     PASN_ConstrainedString & operator=(const char * str);
00651     PASN_ConstrainedString & operator=(const PString & str) { return operator=((const char *)str); }
00652     operator const PString &() const { return value; }
00653     const PString & GetValue() const { return value; }
00654     void SetValue(const char * v) { operator=(v); }
00655     void SetValue(const PString & v) { operator=(v); }
00656     char operator[](PINDEX idx) const { return value[idx]; }
00657 
00658     void SetCharacterSet(ConstraintType ctype, const char * charSet);
00659     void SetCharacterSet(ConstraintType ctype, unsigned firstChar = 0, unsigned lastChar = 255);
00660     void SetCharacterSet(const char * charSet, PINDEX size, ConstraintType ctype);
00661 
00662     virtual Comparison Compare(const PObject & obj) const;
00663     virtual void PrintOn(ostream & strm) const;
00664 
00665     virtual void SetConstraintBounds(ConstraintType type, int lower, unsigned upper);
00666     virtual PINDEX GetDataLength() const;
00667     virtual BOOL Decode(PASN_Stream &);
00668     virtual void Encode(PASN_Stream &) const;
00669 
00670 #ifdef P_INCLUDE_BER
00671     BOOL DecodeBER(PBER_Stream & strm, unsigned len);
00672     void EncodeBER(PBER_Stream & strm) const;
00673 #endif
00674 
00675 #ifdef P_INCLUDE_PER
00676     BOOL DecodePER(PPER_Stream & strm);
00677     void EncodePER(PPER_Stream & strm) const;
00678 #endif
00679 
00680   protected:
00681     PASN_ConstrainedString(const char * canonicalSet, PINDEX setSize,
00682                            unsigned tag, TagClass tagClass);
00683 
00684     PString value;
00685     PCharArray characterSet;
00686     const char * canonicalSet;
00687     PINDEX canonicalSetSize;
00688     unsigned canonicalSetBits;
00689     unsigned charSetUnalignedBits;
00690     unsigned charSetAlignedBits;
00691 };
00692 
00693 
00694 #define DECLARE_STRING_CLASS(name) \
00695   class PASN_##name##String : public PASN_ConstrainedString { \
00696     PCLASSINFO(PASN_##name##String, PASN_ConstrainedString); \
00697     public: \
00698       PASN_##name##String(const char * str = NULL); \
00699       PASN_##name##String(unsigned tag, TagClass tagClass); \
00700       PASN_##name##String & operator=(const char * str); \
00701       PASN_##name##String & operator=(const PString & str); \
00702       virtual PObject * Clone() const; \
00703       virtual PString GetTypeAsString() const; \
00704   }
00705 
00706 DECLARE_STRING_CLASS(Numeric);
00707 DECLARE_STRING_CLASS(Printable);
00708 DECLARE_STRING_CLASS(Visible);
00709 DECLARE_STRING_CLASS(IA5);
00710 DECLARE_STRING_CLASS(General);
00711 
00712 
00715 class PASN_BMPString : public PASN_ConstrainedObject
00716 {
00717     PCLASSINFO(PASN_BMPString, PASN_ConstrainedObject);
00718   public:
00719     PASN_BMPString(const char * str = NULL);
00720     PASN_BMPString(const PWORDArray & wstr);
00721     PASN_BMPString(unsigned tag, TagClass tagClass);
00722 
00723     PASN_BMPString(const PASN_BMPString & other);
00724     PASN_BMPString & operator=(const PASN_BMPString & other);
00725 
00726     PASN_BMPString & operator=(const char * v) { return operator=(PString(v).AsUCS2()); }
00727     PASN_BMPString & operator=(const PString & v) { return operator=(v.AsUCS2()); }
00728     PASN_BMPString & operator=(const PWORDArray & v);
00729     operator PString() const { return GetValue(); }
00730     operator PWORDArray() const { return value; }
00731     PString GetValue() const { return value; }
00732     void GetValue(PWORDArray & v) const { v = value; }
00733     void SetValue(const char * v) { operator=(PString(v).AsUCS2()); }
00734     void SetValue(const PString & v) { operator=(v.AsUCS2()); }
00735     void SetValue(const PWORDArray & v) { operator=(v); }
00736     void SetValue(const PASN_BMPString & v) { operator=(v.value); }
00737 
00738     void SetCharacterSet(ConstraintType ctype, const char * charSet);
00739     void SetCharacterSet(ConstraintType ctype, const PWORDArray & charSet);
00740     void SetCharacterSet(ConstraintType ctype, unsigned firstChar, unsigned lastChar);
00741 
00742     virtual Comparison Compare(const PObject & obj) const;
00743     virtual PObject * Clone() const;
00744     virtual void PrintOn(ostream & strm) const;
00745 
00746     virtual PString GetTypeAsString() const;
00747     virtual PINDEX GetDataLength() const;
00748     virtual BOOL Decode(PASN_Stream &);
00749     virtual void Encode(PASN_Stream &) const;
00750 
00751 #ifdef P_INCLUDE_BER
00752     BOOL DecodeBER(PBER_Stream & strm, unsigned len);
00753     void EncodeBER(PBER_Stream & strm) const;
00754 #endif
00755 
00756 #ifdef P_INCLUDE_PER
00757     BOOL DecodePER(PPER_Stream & strm);
00758     void EncodePER(PPER_Stream & strm) const;
00759 #endif
00760 
00761   protected:
00762     void Construct();
00763     BOOL IsLegalCharacter(WORD ch);
00764 
00765     PWORDArray value;
00766     PWORDArray characterSet;
00767     WORD firstChar, lastChar;
00768     unsigned charSetUnalignedBits;
00769     unsigned charSetAlignedBits;
00770 };
00771 
00772 
00773 class PASN_GeneralisedTime : public PASN_VisibleString
00774 {
00775     PCLASSINFO(PASN_GeneralisedTime, PASN_VisibleString);
00776   public:
00777     PASN_GeneralisedTime()
00778       : PASN_VisibleString(UniversalGeneralisedTime, UniversalTagClass) { }
00779     PASN_GeneralisedTime(const PTime & time)
00780       : PASN_VisibleString(UniversalGeneralisedTime, UniversalTagClass) { SetValue(time); }
00781     PASN_GeneralisedTime(unsigned tag, TagClass tagClass)
00782       : PASN_VisibleString(tag, tagClass) { }
00783 
00784     PASN_GeneralisedTime & operator=(const PTime & time);
00785     void SetValue(const PTime & time) { operator=(time); }
00786     PTime GetValue() const;
00787 };
00788 
00789 
00790 class PASN_UniversalTime : public PASN_VisibleString
00791 {
00792     PCLASSINFO(PASN_UniversalTime, PASN_VisibleString);
00793   public:
00794     PASN_UniversalTime()
00795       : PASN_VisibleString(UniversalUTCTime, UniversalTagClass) { }
00796     PASN_UniversalTime(const PTime & time)
00797       : PASN_VisibleString(UniversalUTCTime, UniversalTagClass) { SetValue(time); }
00798     PASN_UniversalTime(unsigned tag, TagClass tagClass)
00799       : PASN_VisibleString(tag, tagClass) { }
00800 
00801     PASN_UniversalTime & operator=(const PTime & time);
00802     void SetValue(const PTime & time) { operator=(time); }
00803     PTime GetValue() const;
00804 };
00805 
00806 
00807 class PASN_Sequence;
00808 
00811 class PASN_Choice : public PASN_Object
00812 {
00813     PCLASSINFO(PASN_Choice, PASN_Object);
00814   public:
00815     ~PASN_Choice();
00816 
00817     virtual void SetTag(unsigned newTag, TagClass tagClass = DefaultTagClass);
00818     PString GetTagName() const;
00819     PASN_Object & GetObject() const;
00820     BOOL IsValid() const { return choice != NULL; }
00821 
00822 #if defined(__GNUC__) && __GNUC__ <= 2 && __GNUC_MINOR__ < 9
00823 
00824     operator PASN_Null &() const;
00825     operator PASN_Boolean &() const;
00826     operator PASN_Integer &() const;
00827     operator PASN_Enumeration &() const;
00828     operator PASN_Real &() const;
00829     operator PASN_ObjectId &() const;
00830     operator PASN_BitString &() const;
00831     operator PASN_OctetString &() const;
00832     operator PASN_NumericString &() const;
00833     operator PASN_PrintableString &() const;
00834     operator PASN_VisibleString &() const;
00835     operator PASN_IA5String &() const;
00836     operator PASN_GeneralString &() const;
00837     operator PASN_BMPString &() const;
00838     operator PASN_Sequence &() const;
00839 
00840 #else
00841 
00842     operator PASN_Null &();
00843     operator PASN_Boolean &();
00844     operator PASN_Integer &();
00845     operator PASN_Enumeration &();
00846     operator PASN_Real &();
00847     operator PASN_ObjectId &();
00848     operator PASN_BitString &();
00849     operator PASN_OctetString &();
00850     operator PASN_NumericString &();
00851     operator PASN_PrintableString &();
00852     operator PASN_VisibleString &();
00853     operator PASN_IA5String &();
00854     operator PASN_GeneralString &();
00855     operator PASN_BMPString &();
00856     operator PASN_Sequence &();
00857 
00858     operator const PASN_Null &() const;
00859     operator const PASN_Boolean &() const;
00860     operator const PASN_Integer &() const;
00861     operator const PASN_Enumeration &() const;
00862     operator const PASN_Real &() const;
00863     operator const PASN_ObjectId &() const;
00864     operator const PASN_BitString &() const;
00865     operator const PASN_OctetString &() const;
00866     operator const PASN_NumericString &() const;
00867     operator const PASN_PrintableString &() const;
00868     operator const PASN_VisibleString &() const;
00869     operator const PASN_IA5String &() const;
00870     operator const PASN_GeneralString &() const;
00871     operator const PASN_BMPString &() const;
00872     operator const PASN_Sequence &() const;
00873 
00874 #endif
00875 
00876     virtual BOOL CreateObject() = 0;
00877 
00878     virtual Comparison Compare(const PObject & obj) const;
00879     virtual void PrintOn(ostream & strm) const;
00880 
00881     virtual PString GetTypeAsString() const;
00882     virtual PINDEX GetDataLength() const;
00883     virtual BOOL IsPrimitive() const;
00884     virtual BOOL Decode(PASN_Stream &);
00885     virtual void Encode(PASN_Stream &) const;
00886 
00887 #ifdef P_INCLUDE_PER
00888     virtual BOOL DecodePER(PPER_Stream &);
00889     virtual void EncodePER(PPER_Stream &) const;
00890 #endif
00891 
00892 #ifdef P_INCLUDE_XER
00893     BOOL DecodeXER(PXER_Stream &);
00894     void EncodeXER(PXER_Stream &) const;
00895 #endif
00896 
00897     PASN_Choice & operator=(const PASN_Choice & other);
00898 
00899   protected:
00900     PASN_Choice(unsigned nChoices = 0, BOOL extend = FALSE);
00901     PASN_Choice(unsigned tag, TagClass tagClass, unsigned nChoices, BOOL extend);
00902     PASN_Choice(unsigned tag, TagClass tagClass, unsigned nChoices, BOOL extend, const PString & nameSpec);
00903 
00904     PASN_Choice(const PASN_Choice & other);
00905 
00906     BOOL CheckCreate() const;
00907 
00908     unsigned numChoices;
00909     PASN_Object * choice;
00910     POrdinalToString names;
00911 };
00912 
00913 
00914 PARRAY(PASN_ObjectArray, PASN_Object);
00915 
00916 
00919 class PASN_Sequence : public PASN_Object
00920 {
00921     PCLASSINFO(PASN_Sequence, PASN_Object);
00922   public:
00923     PASN_Sequence(unsigned tag = UniversalSequence,
00924                   TagClass tagClass = UniversalTagClass,
00925                   unsigned nOpts = 0, BOOL extend = FALSE, unsigned nExtend = 0);
00926 
00927     PASN_Sequence(const PASN_Sequence & other);
00928     PASN_Sequence & operator=(const PASN_Sequence & other);
00929 
00930     PINDEX GetSize() const { return fields.GetSize(); }
00931     BOOL SetSize(PINDEX newSize);
00932     PASN_Object & operator[](PINDEX i) const { return fields[i]; }
00933 
00934     BOOL HasOptionalField(PINDEX opt) const;
00935     void IncludeOptionalField(PINDEX opt);
00936     void RemoveOptionalField(PINDEX opt);
00937 
00938     virtual Comparison Compare(const PObject & obj) const;
00939     virtual PObject * Clone() const;
00940     virtual void PrintOn(ostream & strm) const;
00941 
00942     virtual PString GetTypeAsString() const;
00943     virtual PINDEX GetDataLength() const;
00944     virtual BOOL IsPrimitive() const;
00945     virtual BOOL Decode(PASN_Stream &);
00946     virtual void Encode(PASN_Stream &) const;
00947 
00948     BOOL PreambleDecode(PASN_Stream & strm);
00949     void PreambleEncode(PASN_Stream & strm) const;
00950     BOOL KnownExtensionDecode(PASN_Stream & strm, PINDEX fld, PASN_Object & field);
00951     void KnownExtensionEncode(PASN_Stream & strm, PINDEX fld, const PASN_Object & field) const;
00952     BOOL UnknownExtensionsDecode(PASN_Stream & strm);
00953     void UnknownExtensionsEncode(PASN_Stream & strm) const;
00954 
00955 #ifdef P_INCLUDE_BER
00956     BOOL PreambleDecodeBER(PBER_Stream & strm);
00957     void PreambleEncodeBER(PBER_Stream & strm) const;
00958     BOOL KnownExtensionDecodeBER(PBER_Stream & strm, PINDEX fld, PASN_Object & field);
00959     void KnownExtensionEncodeBER(PBER_Stream & strm, PINDEX fld, const PASN_Object & field) const;
00960     BOOL UnknownExtensionsDecodeBER(PBER_Stream & strm);
00961     void UnknownExtensionsEncodeBER(PBER_Stream & strm) const;
00962 #endif
00963 
00964 #ifdef P_INCLUDE_PER
00965     BOOL PreambleDecodePER(PPER_Stream & strm);
00966     void PreambleEncodePER(PPER_Stream & strm) const;
00967     BOOL KnownExtensionDecodePER(PPER_Stream & strm, PINDEX fld, PASN_Object & field);
00968     void KnownExtensionEncodePER(PPER_Stream & strm, PINDEX fld, const PASN_Object & field) const;
00969     BOOL UnknownExtensionsDecodePER(PPER_Stream & strm);
00970     void UnknownExtensionsEncodePER(PPER_Stream & strm) const;
00971 #endif
00972 
00973 #ifdef P_INCLUDE_XER
00974     virtual BOOL PreambleDecodeXER(PXER_Stream & strm);
00975     virtual void PreambleEncodeXER(PXER_Stream & strm) const;
00976     virtual BOOL KnownExtensionDecodeXER(PXER_Stream & strm, PINDEX fld, PASN_Object & field);
00977     virtual void KnownExtensionEncodeXER(PXER_Stream & strm, PINDEX fld, const PASN_Object & field) const;
00978     virtual BOOL UnknownExtensionsDecodeXER(PXER_Stream & strm);
00979     virtual void UnknownExtensionsEncodeXER(PXER_Stream & strm) const;
00980 #endif
00981 
00982   protected:
00983     BOOL NoExtensionsToDecode(PPER_Stream & strm);
00984     BOOL NoExtensionsToEncode(PPER_Stream & strm);
00985 
00986     PASN_ObjectArray fields;
00987     PASN_BitString optionMap;
00988     int knownExtensions;
00989     int totalExtensions;
00990     PASN_BitString extensionMap;
00991     PINDEX endBasicEncoding;
00992 };
00993 
00994 
00997 class PASN_Set : public PASN_Sequence
00998 {
00999     PCLASSINFO(PASN_Set, PASN_Sequence);
01000   public:
01001     PASN_Set(unsigned tag = UniversalSet,
01002              TagClass tagClass = UniversalTagClass,
01003              unsigned nOpts = 0, BOOL extend = FALSE, unsigned nExtend = 0);
01004 
01005     virtual PObject * Clone() const;
01006     virtual PString GetTypeAsString() const;
01007 };
01008 
01009 
01012 class PASN_Array : public PASN_ConstrainedObject
01013 {
01014     PCLASSINFO(PASN_Array, PASN_ConstrainedObject);
01015   public:
01016     PINDEX GetSize() const { return array.GetSize(); }
01017     BOOL SetSize(PINDEX newSize);
01018     PASN_Object & operator[](PINDEX i) const { return array[i]; }
01019     void Append(PASN_Object * obj) { array.SetAt(array.GetSize(), obj); }
01020     void RemoveAt(PINDEX i) { array.RemoveAt(i); }
01021     void RemoveAll() { array.RemoveAll(); }
01022 
01023     virtual Comparison Compare(const PObject & obj) const;
01024     virtual void PrintOn(ostream & strm) const;
01025 
01026     virtual void SetConstraintBounds(ConstraintType type, int lower, unsigned upper);
01027     virtual PString GetTypeAsString() const;
01028     virtual PINDEX GetDataLength() const;
01029     virtual BOOL IsPrimitive() const;
01030     virtual BOOL Decode(PASN_Stream &);
01031     virtual void Encode(PASN_Stream &) const;
01032 
01033     virtual PASN_Object * CreateObject() const = 0;
01034 
01035     PASN_Array & operator=(const PASN_Array & other);
01036 
01037   protected:
01038     PASN_Array(unsigned tag = UniversalSequence,
01039                TagClass tagClass = UniversalTagClass);
01040 
01041     PASN_Array(const PASN_Array & other);
01042 
01043     PASN_ObjectArray array;
01044 };
01045 
01046 
01048 
01051 class PASN_Stream : public PBYTEArray
01052 {
01053     PCLASSINFO(PASN_Stream, PBYTEArray);
01054   public:
01055     PASN_Stream();
01056     PASN_Stream(const PBYTEArray & bytes);
01057     PASN_Stream(const BYTE * buf, PINDEX size);
01058 
01059     void PrintOn(ostream & strm) const;
01060 
01061     PINDEX GetPosition() const { return byteOffset; }
01062     void SetPosition(PINDEX newPos);
01063     BOOL IsAtEnd() { return byteOffset >= GetSize(); }
01064     void ResetDecoder();
01065     void BeginEncoding();
01066     void CompleteEncoding();
01067 
01068     virtual BOOL Read(PChannel & chan) = 0;
01069     virtual BOOL Write(PChannel & chan) = 0;
01070 
01071     virtual BOOL NullDecode(PASN_Null &) = 0;
01072     virtual void NullEncode(const PASN_Null &) = 0;
01073     virtual BOOL BooleanDecode(PASN_Boolean &) = 0;
01074     virtual void BooleanEncode(const PASN_Boolean &) = 0;
01075     virtual BOOL IntegerDecode(PASN_Integer &) = 0;
01076     virtual void IntegerEncode(const PASN_Integer &) = 0;
01077     virtual BOOL EnumerationDecode(PASN_Enumeration &) = 0;
01078     virtual void EnumerationEncode(const PASN_Enumeration &) = 0;
01079     virtual BOOL RealDecode(PASN_Real &) = 0;
01080     virtual void RealEncode(const PASN_Real &) = 0;
01081     virtual BOOL ObjectIdDecode(PASN_ObjectId &) = 0;
01082     virtual void ObjectIdEncode(const PASN_ObjectId &) = 0;
01083     virtual BOOL BitStringDecode(PASN_BitString &) = 0;
01084     virtual void BitStringEncode(const PASN_BitString &) = 0;
01085     virtual BOOL OctetStringDecode(PASN_OctetString &) = 0;
01086     virtual void OctetStringEncode(const PASN_OctetString &) = 0;
01087     virtual BOOL ConstrainedStringDecode(PASN_ConstrainedString &) = 0;
01088     virtual void ConstrainedStringEncode(const PASN_ConstrainedString &) = 0;
01089     virtual BOOL BMPStringDecode(PASN_BMPString &) = 0;
01090     virtual void BMPStringEncode(const PASN_BMPString &) = 0;
01091     virtual BOOL ChoiceDecode(PASN_Choice &) = 0;
01092     virtual void ChoiceEncode(const PASN_Choice &) = 0;
01093     virtual BOOL ArrayDecode(PASN_Array &) = 0;
01094     virtual void ArrayEncode(const PASN_Array &) = 0;
01095     virtual BOOL SequencePreambleDecode(PASN_Sequence &) = 0;
01096     virtual void SequencePreambleEncode(const PASN_Sequence &) = 0;
01097     virtual BOOL SequenceKnownDecode(PASN_Sequence &, PINDEX, PASN_Object &) = 0;
01098     virtual void SequenceKnownEncode(const PASN_Sequence &, PINDEX, const PASN_Object &) = 0;
01099     virtual BOOL SequenceUnknownDecode(PASN_Sequence &) = 0;
01100     virtual void SequenceUnknownEncode(const PASN_Sequence &) = 0;
01101 
01102     BYTE ByteDecode();
01103     void ByteEncode(unsigned value);
01104 
01105     unsigned BlockDecode(BYTE * bufptr, unsigned nBytes);
01106     void BlockEncode(const BYTE * bufptr, PINDEX nBytes);
01107 
01108     void ByteAlign();
01109 
01110   protected:
01111     PINDEX byteOffset;
01112     unsigned bitOffset;
01113 
01114   private:
01115     void Construct();
01116 };
01117 
01118 #ifdef  P_INCLUDE_PER
01119 #include "asnper.h"
01120 #endif
01121 
01122 #ifdef  P_INCLUDE_BER
01123 #include "asnber.h"
01124 #endif
01125 
01126 #ifdef  P_INCLUDE_XER
01127 #include "asnxer.h"
01128 #endif
01129 
01130 #endif // _ASNER_H

Generated on Tue Mar 15 10:44:51 2005 for PWLib by  doxygen 1.4.0