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

Generated on Fri Sep 21 14:40:10 2007 for PWLib by  doxygen 1.5.3