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

Generated on Fri Oct 20 11:56:45 2006 for PWLib by  doxygen 1.4.7