Fixed build on MacOSX
[pwlib.git] / include / ptclib / asner.h
blobe1ea6870252eb0480d31ce5036b7c7317d7596be
1 /*
2 * asner.h
4 * Abstract Syntax Notation Encoding Rules classes
6 * Portable Windows Library
8 * Copyright (c) 1993-2002 Equivalence Pty. Ltd.
10 * The contents of this file are subject to the Mozilla Public License
11 * Version 1.0 (the "License"); you may not use this file except in
12 * compliance with the License. You may obtain a copy of the License at
13 * http://www.mozilla.org/MPL/
15 * Software distributed under the License is distributed on an "AS IS"
16 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17 * the License for the specific language governing rights and limitations
18 * under the License.
20 * The Original Code is Portable Windows Library.
22 * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
24 * Contributor(s): ______________________________________.
26 * $Log$
27 * Revision 1.48 2007/05/16 09:11:44 csoutheren
28 * Fix warnings on gcc 4.2.0
30 * Revision 1.47 2007/04/18 23:49:50 csoutheren
31 * Add usage of precompiled headers
33 * Revision 1.46 2005/11/25 01:01:13 csoutheren
34 * Applied patch #1351168
35 * PWlib various fixes
37 * Revision 1.45 2005/06/07 06:25:53 csoutheren
38 * Applied patch 1199897 to increase speed of ASN parser debugging output
39 * Thanks to Dmitriy <ddv@abinet.com>
41 * Revision 1.44 2004/11/11 07:34:50 csoutheren
42 * Added #include <ptlib.h>
44 * Revision 1.43 2004/04/18 04:33:35 rjongbloed
45 * Changed all operators that return BOOL to return standard type bool. This is primarily
46 * for improved compatibility with std STL usage removing many warnings.
48 * Revision 1.42 2003/12/14 10:21:29 rjongbloed
49 * Fixed bug in length incorrectlty decoded from ASN and (apparently) rare circumstances. Thanks pangxg@hotmail.com.
50 * Cleaned up return values to be BOOL rather than int for some functions.
52 * Revision 1.41 2003/08/18 23:32:22 rjongbloed
53 * Micro optimisation suggested by Chih-Wei Huang
55 * Revision 1.40 2003/08/01 16:00:51 csoutheren
56 * Changed #if to #ifdef to (maybe) avoid compiler problems with gcc 2.95.2
58 * Revision 1.39 2003/08/01 02:12:34 csoutheren
59 * Changed to allow easy isolation of PER, BER and XER encoding/decoding routines
61 * Revision 1.38 2003/04/22 23:39:09 craigs
62 * Changed some functions from protected to public for MacOSX. Thanks to Hugo Santos
64 * Revision 1.37 2003/04/17 14:44:44 craigs
65 * Removed MacOS specific defines to make some attributes public
66 * Thanks to Hugo Santos and apologies to Roger Hardiman
68 * Revision 1.36 2003/02/26 01:57:44 robertj
69 * Added XML encoding rules to ASN system, thanks Federico Pinna
71 * Revision 1.35 2003/02/01 13:25:52 robertj
72 * Added function to add new elements directly to ASN array.
74 * Revision 1.34 2003/01/24 23:43:43 robertj
75 * Fixed subtle problems with the use of MAX keyword for unsigned numbers,
76 * should beUINT_MAX not INT_MAX, thanks Stevie Gray for pointing it out.
78 * Revision 1.33 2002/11/26 23:29:18 robertj
79 * Added missing const to DecodeSubType() function.
81 * Revision 1.32 2002/11/06 22:47:23 robertj
82 * Fixed header comment (copyright etc)
84 * Revision 1.31 2002/10/31 05:50:49 robertj
85 * Changed to use new UTF-8/UCS-2 conversion functions on PString.
87 * Revision 1.30 2002/10/10 14:37:40 rogerh
88 * In two of the PASN classes make the protected members public. This
89 * makes OpenH323 compile in Mac OS X 10.2.1
91 * Revision 1.29 2002/09/16 01:08:59 robertj
92 * Added #define so can select if #pragma interface/implementation is used on
93 * platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan.
95 * Revision 1.28 2002/05/29 01:22:35 robertj
96 * Added ability to set object id from unsigned integer arrays.
98 * Revision 1.27 2002/05/21 04:23:40 robertj
99 * Fixed problem with ASN encoding/decoding unsconstrained negative numbers,
101 * Revision 1.26 2002/05/14 06:59:31 robertj
102 * Added more bullet proofing so a malformed PDU cannot cause teh decoder
103 * to try and allocate huge arrays and consume all CPU and memory on a
104 * system. A configurable limit of 100 is set for things like SEQUENCE OF.
106 * Revision 1.25 2001/12/13 09:13:28 robertj
107 * Added function get get oid as a string.
108 * Added functions to compare oid to PString.
110 * Revision 1.24 2001/09/14 01:59:59 robertj
111 * Fixed problem with incorrectly initialised PASN_Choice sub-object.
113 * Revision 1.23 2001/08/06 01:39:02 robertj
114 * Added assignement operator with RHS of PASN_BMPString to classes
115 * descended from PASN_BMPString.
117 * Revision 1.22 2001/06/14 02:14:12 robertj
118 * Added functions to encode and decode another ASN type that is inside
119 * an octet string, useful for ANY or EXTERNAL types etc.
121 * Revision 1.21 2001/04/30 06:47:04 robertj
122 * Fixed problem with en/decoding more than 16 extension fields in a sequence.
124 * Revision 1.20 2001/04/26 08:15:58 robertj
125 * Fixed problem with ASN compile of single constraints on enumerations.
127 * Revision 1.19 2001/04/23 04:40:14 robertj
128 * Added ASN standard types GeneralizedTime and UTCTime
130 * Revision 1.18 2001/04/12 03:25:22 robertj
131 * Fixed PASN_Boolean cosntructor to be compatible with usage in ASN parser.
132 * Changed all PASN_xxx types so constructor can take real type as only
133 * parameter. eg PASN_OctetString s = "fred";
135 * Revision 1.17 2001/03/21 03:32:35 robertj
136 * Aded ability to get at the data bits buffer in a PASN_BitString
138 * Revision 1.16 2001/01/24 04:36:56 robertj
139 * Added more bulletproofing to ASN structures to obey constraints.
141 * Revision 1.15 2000/10/26 11:09:07 robertj
142 * More bullet proofing of PER decoder, changed bit type to be unsigned.
144 * Revision 1.14 2000/10/25 04:05:44 robertj
145 * More bullet proofing of PER decoder.
147 * Revision 1.13 2000/07/11 18:23:03 robertj
148 * Added ability to set/get BMP string data as PWORDArray.
150 * Revision 1.12 2000/04/10 17:30:42 robertj
151 * Added [] operator for char access on ASN string classes.
153 * Revision 1.11 2000/02/29 06:32:12 robertj
154 * Added ability to remove optional field in sequence, thanks Dave Harvey.
156 * Revision 1.10 1999/08/09 13:02:45 robertj
157 * dded ASN compiler #defines for backward support of pre GCC 2.9 compilers.
158 * Added ASN compiler #defines to reduce its memory footprint.
160 * Revision 1.9 1999/07/22 06:48:51 robertj
161 * Added comparison operation to base ASN classes and compiled ASN code.
162 * Added support for ANY type in ASN parser.
164 * Revision 1.8 1999/03/09 09:34:05 robertj
165 * Fixed typo's.
167 * Revision 1.7 1999/03/09 08:01:46 robertj
168 * Changed comments for doc++ support (more to come).
170 * Revision 1.6 1998/09/23 06:19:21 robertj
171 * Added open source copyright license.
173 * Revision 1.5 1998/05/21 04:26:53 robertj
174 * Fixed numerous PER problems.
176 * Revision 1.4 1998/05/07 05:19:28 robertj
177 * Fixed problems with using copy constructor/assignment oeprator on PASN_Objects.
179 * Revision 1.3 1997/12/18 05:08:13 robertj
180 * Added function to get choice discriminat`or name.
182 * Revision 1.2 1997/12/11 10:35:42 robertj
183 * Support for new ASN file parser.
187 #ifndef _ASNER_H
188 #define _ASNER_H
190 #ifdef P_USE_PRAGMA
191 #pragma interface
192 #endif
194 // provide options to omit vertain encodings, if needed
195 #define P_INCLUDE_PER
196 #define P_INCLUDE_BER
197 #define P_INCLUDE_XER
199 class PASN_Stream;
200 class PBER_Stream;
201 class PPER_Stream;
203 #if P_EXPAT
204 class PXER_Stream;
205 class PXMLElement;
206 #else
207 #undef P_INCLUDE_XER
208 #endif
210 #ifndef _PTLIB_H
211 #include <ptlib.h>
212 #endif
214 /////////////////////////////////////////////////////////////////////////////
216 /** Base class for ASN encoding/decoding.
218 class PASN_Object : public PObject
220 PCLASSINFO(PASN_Object, PObject);
221 public:
222 /** Return a string giving the type of the object */
223 virtual PString GetTypeAsString() const = 0;
225 PINDEX GetObjectLength() const;
226 virtual PINDEX GetDataLength() const = 0;
227 virtual BOOL IsPrimitive() const { return TRUE; }
229 virtual BOOL Decode(PASN_Stream &) = 0;
230 virtual void Encode(PASN_Stream &) const = 0;
232 BOOL IsExtendable() const { return extendable; }
233 void SetExtendable(BOOL ext = TRUE) { extendable = ext; }
235 enum TagClass {
236 UniversalTagClass,
237 ApplicationTagClass,
238 ContextSpecificTagClass,
239 PrivateTagClass,
240 DefaultTagClass
242 TagClass GetTagClass() const { return tagClass; }
244 enum UniversalTags {
245 InvalidUniversalTag,
246 UniversalBoolean,
247 UniversalInteger,
248 UniversalBitString,
249 UniversalOctetString,
250 UniversalNull,
251 UniversalObjectId,
252 UniversalObjectDescriptor,
253 UniversalExternalType,
254 UniversalReal,
255 UniversalEnumeration,
256 UniversalEmbeddedPDV,
257 UniversalSequence = 16,
258 UniversalSet,
259 UniversalNumericString,
260 UniversalPrintableString,
261 UniversalTeletexString,
262 UniversalVideotexString,
263 UniversalIA5String,
264 UniversalUTCTime,
265 UniversalGeneralisedTime,
266 UniversalGeneralizedTime = UniversalGeneralisedTime,
267 UniversalGraphicString,
268 UniversalVisibleString,
269 UniversalGeneralString,
270 UniversalUniversalString,
271 UniversalBMPString = 30
274 unsigned GetTag() const { return tag; }
275 virtual void SetTag(unsigned newTag, TagClass tagClass = DefaultTagClass);
277 enum ConstraintType {
278 Unconstrained,
279 PartiallyConstrained,
280 FixedConstraint,
281 ExtendableConstraint
284 enum MinimumValueTag { MinimumValue = INT_MIN };
285 enum MaximumValueTag { MaximumValue = INT_MAX };
286 void SetConstraints(ConstraintType type, int value)
287 { SetConstraintBounds(type, value, value); }
288 void SetConstraints(ConstraintType, int lower, MaximumValueTag /*upper*/)
289 { SetConstraintBounds(PartiallyConstrained, (int)lower, lower < 0 ? INT_MAX : UINT_MAX); }
290 void SetConstraints(ConstraintType, MinimumValueTag lower, unsigned upper)
291 { SetConstraintBounds(PartiallyConstrained, (int)lower, (unsigned)upper); }
292 void SetConstraints(ConstraintType, MinimumValueTag lower, MaximumValueTag upper)
293 { SetConstraintBounds(PartiallyConstrained, (int)lower, (unsigned)upper); }
294 void SetConstraints(ConstraintType type, int lower, unsigned upper)
295 { SetConstraintBounds(type, lower, upper); }
297 virtual void SetConstraintBounds(ConstraintType type, int lower, unsigned upper);
298 virtual void SetCharacterSet(ConstraintType ctype, const char * charSet);
299 virtual void SetCharacterSet(ConstraintType ctype, unsigned firstChar, unsigned lastChar);
301 static PINDEX GetMaximumArraySize();
302 static void SetMaximumArraySize(PINDEX sz);
303 static PINDEX GetMaximumStringSize();
304 static void SetMaximumStringSize(PINDEX sz);
306 protected:
307 PASN_Object(unsigned tag, TagClass tagClass, BOOL extend = FALSE);
309 /// PER extension capability
310 BOOL extendable;
311 /// BER tag class
312 TagClass tagClass;
313 /// ASN object tag
314 unsigned tag;
318 /** Base class for constrained ASN encoding/decoding.
320 class PASN_ConstrainedObject : public PASN_Object
322 PCLASSINFO(PASN_ConstrainedObject, PASN_Object);
323 public:
324 BOOL IsConstrained() const { return constraint != Unconstrained; }
325 int GetLowerLimit() const { return lowerLimit; }
326 unsigned GetUpperLimit() const { return upperLimit; }
328 BOOL ConstrainedLengthDecode(PPER_Stream & strm, unsigned & length);
329 void ConstrainedLengthEncode(PPER_Stream & strm, unsigned length) const;
331 BOOL ConstraintEncode(PPER_Stream & strm, unsigned value) const;
333 protected:
334 virtual void SetConstraintBounds(ConstraintType type, int lower, unsigned upper);
335 PASN_ConstrainedObject(unsigned tag, TagClass tagClass);
337 ConstraintType constraint;
338 int lowerLimit;
339 unsigned upperLimit;
343 /** Class for ASN Null type.
345 class PASN_Null : public PASN_Object
347 PCLASSINFO(PASN_Null, PASN_Object);
348 public:
349 PASN_Null(unsigned tag = UniversalNull,
350 TagClass tagClass = UniversalTagClass);
352 virtual Comparison Compare(const PObject & obj) const;
353 virtual PObject * Clone() const;
354 virtual void PrintOn(ostream & strm) const;
356 virtual PString GetTypeAsString() const;
357 virtual PINDEX GetDataLength() const;
358 virtual BOOL Decode(PASN_Stream &);
359 virtual void Encode(PASN_Stream &) const;
363 /** Class for ASN Boolean type.
365 class PASN_Boolean : public PASN_Object
367 PCLASSINFO(PASN_Boolean, PASN_Object);
368 public:
369 PASN_Boolean(BOOL val = FALSE);
370 PASN_Boolean(unsigned tag, TagClass tagClass, BOOL val = FALSE);
372 PASN_Boolean & operator=(BOOL v) { value = v; return *this; }
373 operator BOOL() const { return value; }
374 BOOL GetValue() const { return value; }
375 void SetValue(BOOL v) { value = v; }
377 virtual Comparison Compare(const PObject & obj) const;
378 virtual PObject * Clone() const;
379 virtual void PrintOn(ostream & strm) const;
381 virtual PString GetTypeAsString() const;
382 virtual PINDEX GetDataLength() const;
383 virtual BOOL Decode(PASN_Stream &);
384 virtual void Encode(PASN_Stream &) const;
386 protected:
387 BOOL value;
391 /** Class for ASN Integer type.
393 class PASN_Integer : public PASN_ConstrainedObject
395 PCLASSINFO(PASN_Integer, PASN_ConstrainedObject);
396 public:
397 PASN_Integer(unsigned val = 0);
398 PASN_Integer(unsigned tag, TagClass tagClass, unsigned val = 0);
400 PASN_Integer & operator=(unsigned value);
401 operator unsigned() const { return value; }
402 unsigned GetValue() const { return value; }
403 void SetValue(unsigned v) { operator=(v); }
405 virtual Comparison Compare(const PObject & obj) const;
406 virtual PObject * Clone() const;
407 virtual void PrintOn(ostream & strm) const;
409 virtual void SetConstraintBounds(ConstraintType type, int lower, unsigned upper);
410 virtual PString GetTypeAsString() const;
411 virtual PINDEX GetDataLength() const;
412 virtual BOOL Decode(PASN_Stream &);
413 virtual void Encode(PASN_Stream &) const;
415 #ifdef P_INCLUDE_PER
416 BOOL DecodePER(PPER_Stream & strm);
417 void EncodePER(PPER_Stream & strm) const;
418 #endif
420 BOOL IsUnsigned() const;
422 protected:
423 unsigned value;
426 struct PASN_Names{
427 const char * name;
428 PINDEX value;
431 /** Class for ASN Enumerated type.
433 class PASN_Enumeration : public PASN_Object
435 PCLASSINFO(PASN_Enumeration, PASN_Object);
436 public:
437 PASN_Enumeration(unsigned val = 0);
438 PASN_Enumeration(unsigned tag,
439 TagClass tagClass,
440 unsigned nEnums = P_MAX_INDEX,
441 BOOL extendable = FALSE,
442 unsigned val = 0);
443 PASN_Enumeration(unsigned tag,
444 TagClass tagClass,
445 unsigned nEnums,
446 BOOL extendable,
447 const PASN_Names * nameSpec,
448 unsigned namesCnt,
449 unsigned val = 0);
451 PASN_Enumeration & operator=(unsigned v) { value = v; return *this; }
452 operator unsigned() const { return value; }
453 unsigned GetValue() const { return value; }
454 void SetValue(unsigned v) { value = v; }
456 unsigned GetMaximum() const { return maxEnumValue; }
458 virtual Comparison Compare(const PObject & obj) const;
459 virtual PObject * Clone() const;
460 virtual void PrintOn(ostream & strm) const;
462 virtual PString GetTypeAsString() const;
463 virtual PINDEX GetDataLength() const;
464 virtual BOOL Decode(PASN_Stream &);
465 virtual void Encode(PASN_Stream &) const;
467 #ifdef P_INCLUDE_PER
468 BOOL DecodePER(PPER_Stream & strm);
469 void EncodePER(PPER_Stream & strm) const;
470 #endif
472 #ifdef P_INCLUDE_XER
473 virtual BOOL DecodeXER(PXER_Stream & strm);
474 virtual void EncodeXER(PXER_Stream & strm) const;
475 #endif
477 PINDEX GetValueByName(PString name) const;
478 protected:
479 unsigned maxEnumValue;
480 unsigned value;
481 const PASN_Names *names;
482 unsigned namesCount;
486 /** Class for ASN floating point type.
488 class PASN_Real : public PASN_Object
490 PCLASSINFO(PASN_Real, PASN_Object);
491 public:
492 PASN_Real(double val = 0);
493 PASN_Real(unsigned tag, TagClass tagClass, double val = 0);
495 PASN_Real & operator=(double val) { value = val; return *this; }
496 operator double() const { return value; }
497 double GetValue() const { return value; }
498 void SetValue(double v) { value = v; }
500 virtual Comparison Compare(const PObject & obj) const;
501 virtual PObject * Clone() const;
502 virtual void PrintOn(ostream & strm) const;
504 virtual PString GetTypeAsString() const;
505 virtual PINDEX GetDataLength() const;
506 virtual BOOL Decode(PASN_Stream &);
507 virtual void Encode(PASN_Stream &) const;
509 protected:
510 double value;
514 /** Class for ASN Object Identifier type.
516 class PASN_ObjectId : public PASN_Object
518 PCLASSINFO(PASN_ObjectId, PASN_Object);
519 public:
520 PASN_ObjectId(const char * dotstr = NULL);
521 PASN_ObjectId(unsigned tag, TagClass tagClass);
523 PASN_ObjectId(const PASN_ObjectId & other);
524 PASN_ObjectId & operator=(const PASN_ObjectId & other);
526 PASN_ObjectId & operator=(const char * dotstr);
527 PASN_ObjectId & operator=(const PString & dotstr);
528 PASN_ObjectId & operator=(const PUnsignedArray & numbers);
529 void SetValue(const PString & dotstr);
530 void SetValue(const PUnsignedArray & numbers) { value = numbers; }
531 void SetValue(const unsigned * numbers, PINDEX size);
533 bool operator==(const char * dotstr) const;
534 bool operator!=(const char * dotstr) const { return !operator==(dotstr); }
535 bool operator==(const PString & dotstr) const { return operator==((const char *)dotstr); }
536 bool operator!=(const PString & dotstr) const { return !operator==((const char *)dotstr); }
537 bool operator==(const PASN_ObjectId & id) const { return value == id.value; }
539 PINDEX GetSize() const { return value.GetSize(); }
540 unsigned operator[](PINDEX idx) const { return value[idx]; }
541 const PUnsignedArray & GetValue() const { return value; }
542 PString AsString() const;
544 virtual Comparison Compare(const PObject & obj) const;
545 virtual PObject * Clone() const;
546 virtual void PrintOn(ostream & strm) const;
548 virtual PString GetTypeAsString() const;
549 virtual PINDEX GetDataLength() const;
550 virtual BOOL Decode(PASN_Stream &);
551 virtual void Encode(PASN_Stream &) const;
553 BOOL CommonDecode(PASN_Stream & strm, unsigned dataLen);
554 void CommonEncode(PBYTEArray & eObjId) const;
556 protected:
557 PUnsignedArray value;
561 /** Class for ASN Bit String type.
563 class PASN_BitString : public PASN_ConstrainedObject
565 PCLASSINFO(PASN_BitString, PASN_ConstrainedObject);
566 public:
567 PASN_BitString(unsigned nBits = 0, const BYTE * buf = NULL);
568 PASN_BitString(unsigned tag, TagClass tagClass, unsigned nBits = 0);
570 PASN_BitString(const PASN_BitString & other);
571 PASN_BitString & operator=(const PASN_BitString & other);
573 void SetData(unsigned nBits, const PBYTEArray & bytes);
574 void SetData(unsigned nBits, const BYTE * buf, PINDEX size = 0);
576 const BYTE * GetDataPointer() const { return bitData; }
578 unsigned GetSize() const { return totalBits; }
579 BOOL SetSize(unsigned nBits);
581 bool operator[](PINDEX bit) const;
582 void Set(unsigned bit);
583 void Clear(unsigned bit);
584 void Invert(unsigned bit);
586 virtual Comparison Compare(const PObject & obj) const;
587 virtual PObject * Clone() const;
588 virtual void PrintOn(ostream & strm) const;
590 virtual void SetConstraintBounds(ConstraintType type, int lower, unsigned upper);
591 virtual PString GetTypeAsString() const;
592 virtual PINDEX GetDataLength() const;
593 virtual BOOL Decode(PASN_Stream &);
594 virtual void Encode(PASN_Stream &) const;
596 #ifdef P_INCLUDE_BER
597 BOOL DecodeBER(PBER_Stream & strm, unsigned len);
598 void EncodeBER(PBER_Stream & strm) const;
599 #endif
601 #ifdef P_INCLUDE_PER
602 BOOL DecodePER(PPER_Stream & strm);
603 void EncodePER(PPER_Stream & strm) const;
604 #endif
606 BOOL DecodeSequenceExtensionBitmap(PPER_Stream & strm);
607 void EncodeSequenceExtensionBitmap(PPER_Stream & strm) const;
609 protected:
610 unsigned totalBits;
611 PBYTEArray bitData;
615 /** Class for ASN Octet String type.
617 class PASN_OctetString : public PASN_ConstrainedObject
619 PCLASSINFO(PASN_OctetString, PASN_ConstrainedObject);
620 public:
621 PASN_OctetString(const char * str = NULL, PINDEX size = 0);
622 PASN_OctetString(unsigned tag, TagClass tagClass);
624 PASN_OctetString(const PASN_OctetString & other);
625 PASN_OctetString & operator=(const PASN_OctetString & other);
627 PASN_OctetString & operator=(const char * str);
628 PASN_OctetString & operator=(const PString & str);
629 PASN_OctetString & operator=(const PBYTEArray & arr);
630 void SetValue(const char * str) { operator=(str); }
631 void SetValue(const PString & str) { operator=(str); }
632 void SetValue(const PBYTEArray & arr) { operator=(arr); }
633 void SetValue(const BYTE * data, PINDEX len);
634 const PBYTEArray & GetValue() const { return value; }
635 operator const PBYTEArray &() const { return value; }
636 operator const BYTE *() const { return value; }
637 PString AsString() const;
638 BYTE operator[](PINDEX i) const { return value[i]; }
639 BYTE & operator[](PINDEX i) { return value[i]; }
640 BYTE * GetPointer(PINDEX sz = 0) { return value.GetPointer(sz); }
641 PINDEX GetSize() const { return value.GetSize(); }
642 BOOL SetSize(PINDEX newSize);
644 virtual Comparison Compare(const PObject & obj) const;
645 virtual PObject * Clone() const;
646 virtual void PrintOn(ostream & strm) const;
648 virtual void SetConstraintBounds(ConstraintType type, int lower, unsigned upper);
649 virtual PString GetTypeAsString() const;
650 virtual PINDEX GetDataLength() const;
651 virtual BOOL Decode(PASN_Stream &);
652 virtual void Encode(PASN_Stream &) const;
654 #ifdef P_INCLUDE_PER
655 BOOL DecodePER(PPER_Stream & strm);
656 void EncodePER(PPER_Stream & strm) const;
657 #endif
659 BOOL DecodeSubType(PASN_Object &) const;
660 void EncodeSubType(const PASN_Object &);
662 protected:
663 PBYTEArray value;
667 /** Base class for ASN String types.
669 class PASN_ConstrainedString : public PASN_ConstrainedObject
671 PCLASSINFO(PASN_ConstrainedString, PASN_ConstrainedObject);
672 public:
673 PASN_ConstrainedString & operator=(const char * str);
674 PASN_ConstrainedString & operator=(const PString & str) { return operator=((const char *)str); }
675 operator const PString &() const { return value; }
676 const PString & GetValue() const { return value; }
677 void SetValue(const char * v) { operator=(v); }
678 void SetValue(const PString & v) { operator=(v); }
679 char operator[](PINDEX idx) const { return value[idx]; }
681 void SetCharacterSet(ConstraintType ctype, const char * charSet);
682 void SetCharacterSet(ConstraintType ctype, unsigned firstChar = 0, unsigned lastChar = 255);
683 void SetCharacterSet(const char * charSet, PINDEX size, ConstraintType ctype);
685 virtual Comparison Compare(const PObject & obj) const;
686 virtual void PrintOn(ostream & strm) const;
688 virtual void SetConstraintBounds(ConstraintType type, int lower, unsigned upper);
689 virtual PINDEX GetDataLength() const;
690 virtual BOOL Decode(PASN_Stream &);
691 virtual void Encode(PASN_Stream &) const;
693 #ifdef P_INCLUDE_BER
694 BOOL DecodeBER(PBER_Stream & strm, unsigned len);
695 void EncodeBER(PBER_Stream & strm) const;
696 #endif
698 #ifdef P_INCLUDE_PER
699 BOOL DecodePER(PPER_Stream & strm);
700 void EncodePER(PPER_Stream & strm) const;
701 #endif
703 protected:
704 PASN_ConstrainedString(const char * canonicalSet, PINDEX setSize,
705 unsigned tag, TagClass tagClass);
707 PString value;
708 PCharArray characterSet;
709 const char * canonicalSet;
710 PINDEX canonicalSetSize;
711 unsigned canonicalSetBits;
712 unsigned charSetUnalignedBits;
713 unsigned charSetAlignedBits;
717 #define DECLARE_STRING_CLASS(name) \
718 class PASN_##name##String : public PASN_ConstrainedString { \
719 PCLASSINFO(PASN_##name##String, PASN_ConstrainedString); \
720 public: \
721 PASN_##name##String(const char * str = NULL); \
722 PASN_##name##String(unsigned tag, TagClass tagClass); \
723 PASN_##name##String & operator=(const char * str); \
724 PASN_##name##String & operator=(const PString & str); \
725 virtual PObject * Clone() const; \
726 virtual PString GetTypeAsString() const; \
729 DECLARE_STRING_CLASS(Numeric);
730 DECLARE_STRING_CLASS(Printable);
731 DECLARE_STRING_CLASS(Visible);
732 DECLARE_STRING_CLASS(IA5);
733 DECLARE_STRING_CLASS(General);
736 /** Class for ASN BMP (16 bit) String type.
738 class PASN_BMPString : public PASN_ConstrainedObject
740 PCLASSINFO(PASN_BMPString, PASN_ConstrainedObject);
741 public:
742 PASN_BMPString(const char * str = NULL);
743 PASN_BMPString(const PWORDArray & wstr);
744 PASN_BMPString(unsigned tag, TagClass tagClass);
746 PASN_BMPString(const PASN_BMPString & other);
747 PASN_BMPString & operator=(const PASN_BMPString & other);
749 PASN_BMPString & operator=(const char * v) { return operator=(PString(v).AsUCS2()); }
750 PASN_BMPString & operator=(const PString & v) { return operator=(v.AsUCS2()); }
751 PASN_BMPString & operator=(const PWORDArray & v);
752 operator PString() const { return GetValue(); }
753 operator PWORDArray() const { return value; }
754 PString GetValue() const { return value; }
755 void GetValue(PWORDArray & v) const { v = value; }
756 void SetValue(const char * v) { operator=(PString(v).AsUCS2()); }
757 void SetValue(const PString & v) { operator=(v.AsUCS2()); }
758 void SetValue(const PWORDArray & v) { operator=(v); }
759 void SetValue(const PASN_BMPString & v) { operator=(v.value); }
761 void SetCharacterSet(ConstraintType ctype, const char * charSet);
762 void SetCharacterSet(ConstraintType ctype, const PWORDArray & charSet);
763 void SetCharacterSet(ConstraintType ctype, unsigned firstChar, unsigned lastChar);
765 virtual Comparison Compare(const PObject & obj) const;
766 virtual PObject * Clone() const;
767 virtual void PrintOn(ostream & strm) const;
769 virtual PString GetTypeAsString() const;
770 virtual PINDEX GetDataLength() const;
771 virtual BOOL Decode(PASN_Stream &);
772 virtual void Encode(PASN_Stream &) const;
774 #ifdef P_INCLUDE_BER
775 BOOL DecodeBER(PBER_Stream & strm, unsigned len);
776 void EncodeBER(PBER_Stream & strm) const;
777 #endif
779 #ifdef P_INCLUDE_PER
780 BOOL DecodePER(PPER_Stream & strm);
781 void EncodePER(PPER_Stream & strm) const;
782 #endif
784 protected:
785 void Construct();
786 BOOL IsLegalCharacter(WORD ch);
788 PWORDArray value;
789 PWORDArray characterSet;
790 WORD firstChar, lastChar;
791 unsigned charSetUnalignedBits;
792 unsigned charSetAlignedBits;
796 class PASN_GeneralisedTime : public PASN_VisibleString
798 PCLASSINFO(PASN_GeneralisedTime, PASN_VisibleString);
799 public:
800 PASN_GeneralisedTime()
801 : PASN_VisibleString(UniversalGeneralisedTime, UniversalTagClass) { }
802 PASN_GeneralisedTime(const PTime & time)
803 : PASN_VisibleString(UniversalGeneralisedTime, UniversalTagClass) { SetValue(time); }
804 PASN_GeneralisedTime(unsigned tag, TagClass tagClass)
805 : PASN_VisibleString(tag, tagClass) { }
807 PASN_GeneralisedTime & operator=(const PTime & time);
808 void SetValue(const PTime & time) { operator=(time); }
809 PTime GetValue() const;
813 class PASN_UniversalTime : public PASN_VisibleString
815 PCLASSINFO(PASN_UniversalTime, PASN_VisibleString);
816 public:
817 PASN_UniversalTime()
818 : PASN_VisibleString(UniversalUTCTime, UniversalTagClass) { }
819 PASN_UniversalTime(const PTime & time)
820 : PASN_VisibleString(UniversalUTCTime, UniversalTagClass) { SetValue(time); }
821 PASN_UniversalTime(unsigned tag, TagClass tagClass)
822 : PASN_VisibleString(tag, tagClass) { }
824 PASN_UniversalTime & operator=(const PTime & time);
825 void SetValue(const PTime & time) { operator=(time); }
826 PTime GetValue() const;
830 class PASN_Sequence;
832 /** Class for ASN Choice type.
834 class PASN_Choice : public PASN_Object
836 PCLASSINFO(PASN_Choice, PASN_Object);
837 public:
838 ~PASN_Choice();
840 virtual void SetTag(unsigned newTag, TagClass tagClass = DefaultTagClass);
841 PString GetTagName() const;
842 PASN_Object & GetObject() const;
843 BOOL IsValid() const { return choice != NULL; }
845 #if defined(__GNUC__) && __GNUC__ <= 2 && __GNUC_MINOR__ < 9
847 operator PASN_Null &() const;
848 operator PASN_Boolean &() const;
849 operator PASN_Integer &() const;
850 operator PASN_Enumeration &() const;
851 operator PASN_Real &() const;
852 operator PASN_ObjectId &() const;
853 operator PASN_BitString &() const;
854 operator PASN_OctetString &() const;
855 operator PASN_NumericString &() const;
856 operator PASN_PrintableString &() const;
857 operator PASN_VisibleString &() const;
858 operator PASN_IA5String &() const;
859 operator PASN_GeneralString &() const;
860 operator PASN_BMPString &() const;
861 operator PASN_Sequence &() const;
863 #else
865 operator PASN_Null &();
866 operator PASN_Boolean &();
867 operator PASN_Integer &();
868 operator PASN_Enumeration &();
869 operator PASN_Real &();
870 operator PASN_ObjectId &();
871 operator PASN_BitString &();
872 operator PASN_OctetString &();
873 operator PASN_NumericString &();
874 operator PASN_PrintableString &();
875 operator PASN_VisibleString &();
876 operator PASN_IA5String &();
877 operator PASN_GeneralString &();
878 operator PASN_BMPString &();
879 operator PASN_Sequence &();
881 operator const PASN_Null &() const;
882 operator const PASN_Boolean &() const;
883 operator const PASN_Integer &() const;
884 operator const PASN_Enumeration &() const;
885 operator const PASN_Real &() const;
886 operator const PASN_ObjectId &() const;
887 operator const PASN_BitString &() const;
888 operator const PASN_OctetString &() const;
889 operator const PASN_NumericString &() const;
890 operator const PASN_PrintableString &() const;
891 operator const PASN_VisibleString &() const;
892 operator const PASN_IA5String &() const;
893 operator const PASN_GeneralString &() const;
894 operator const PASN_BMPString &() const;
895 operator const PASN_Sequence &() const;
897 #endif
899 virtual BOOL CreateObject() = 0;
901 virtual Comparison Compare(const PObject & obj) const;
902 virtual void PrintOn(ostream & strm) const;
904 virtual PString GetTypeAsString() const;
905 virtual PINDEX GetDataLength() const;
906 virtual BOOL IsPrimitive() const;
907 virtual BOOL Decode(PASN_Stream &);
908 virtual void Encode(PASN_Stream &) const;
910 #ifdef P_INCLUDE_PER
911 virtual BOOL DecodePER(PPER_Stream &);
912 virtual void EncodePER(PPER_Stream &) const;
913 #endif
915 #ifdef P_INCLUDE_XER
916 BOOL DecodeXER(PXER_Stream &);
917 void EncodeXER(PXER_Stream &) const;
918 #endif
920 PASN_Choice & operator=(const PASN_Choice & other);
922 PINDEX GetValueByName(PString name) const;
923 protected:
924 PASN_Choice(unsigned nChoices = 0, BOOL extend = FALSE);
925 PASN_Choice(unsigned tag, TagClass tagClass, unsigned nChoices, BOOL extend);
926 PASN_Choice(unsigned tag, TagClass tagClass, unsigned nChoices, BOOL extend, const PASN_Names * nameSpec,unsigned namesCnt);
928 PASN_Choice(const PASN_Choice & other);
930 BOOL CheckCreate() const;
932 unsigned numChoices;
933 PASN_Object * choice;
934 const PASN_Names *names;
935 unsigned namesCount;
939 PARRAY(PASN_ObjectArray, PASN_Object);
942 /** Class for ASN Sequence type.
944 class PASN_Sequence : public PASN_Object
946 PCLASSINFO(PASN_Sequence, PASN_Object);
947 public:
948 PASN_Sequence(unsigned tag = UniversalSequence,
949 TagClass tagClass = UniversalTagClass,
950 unsigned nOpts = 0, BOOL extend = FALSE, unsigned nExtend = 0);
952 PASN_Sequence(const PASN_Sequence & other);
953 PASN_Sequence & operator=(const PASN_Sequence & other);
955 PINDEX GetSize() const { return fields.GetSize(); }
956 BOOL SetSize(PINDEX newSize);
957 PASN_Object & operator[](PINDEX i) const { return fields[i]; }
959 BOOL HasOptionalField(PINDEX opt) const;
960 void IncludeOptionalField(PINDEX opt);
961 void RemoveOptionalField(PINDEX opt);
963 virtual Comparison Compare(const PObject & obj) const;
964 virtual PObject * Clone() const;
965 virtual void PrintOn(ostream & strm) const;
967 virtual PString GetTypeAsString() const;
968 virtual PINDEX GetDataLength() const;
969 virtual BOOL IsPrimitive() const;
970 virtual BOOL Decode(PASN_Stream &);
971 virtual void Encode(PASN_Stream &) const;
973 BOOL PreambleDecode(PASN_Stream & strm);
974 void PreambleEncode(PASN_Stream & strm) const;
975 BOOL KnownExtensionDecode(PASN_Stream & strm, PINDEX fld, PASN_Object & field);
976 void KnownExtensionEncode(PASN_Stream & strm, PINDEX fld, const PASN_Object & field) const;
977 BOOL UnknownExtensionsDecode(PASN_Stream & strm);
978 void UnknownExtensionsEncode(PASN_Stream & strm) const;
980 #ifdef P_INCLUDE_BER
981 BOOL PreambleDecodeBER(PBER_Stream & strm);
982 void PreambleEncodeBER(PBER_Stream & strm) const;
983 BOOL KnownExtensionDecodeBER(PBER_Stream & strm, PINDEX fld, PASN_Object & field);
984 void KnownExtensionEncodeBER(PBER_Stream & strm, PINDEX fld, const PASN_Object & field) const;
985 BOOL UnknownExtensionsDecodeBER(PBER_Stream & strm);
986 void UnknownExtensionsEncodeBER(PBER_Stream & strm) const;
987 #endif
989 #ifdef P_INCLUDE_PER
990 BOOL PreambleDecodePER(PPER_Stream & strm);
991 void PreambleEncodePER(PPER_Stream & strm) const;
992 BOOL KnownExtensionDecodePER(PPER_Stream & strm, PINDEX fld, PASN_Object & field);
993 void KnownExtensionEncodePER(PPER_Stream & strm, PINDEX fld, const PASN_Object & field) const;
994 BOOL UnknownExtensionsDecodePER(PPER_Stream & strm);
995 void UnknownExtensionsEncodePER(PPER_Stream & strm) const;
996 #endif
998 #ifdef P_INCLUDE_XER
999 virtual BOOL PreambleDecodeXER(PXER_Stream & strm);
1000 virtual void PreambleEncodeXER(PXER_Stream & strm) const;
1001 virtual BOOL KnownExtensionDecodeXER(PXER_Stream & strm, PINDEX fld, PASN_Object & field);
1002 virtual void KnownExtensionEncodeXER(PXER_Stream & strm, PINDEX fld, const PASN_Object & field) const;
1003 virtual BOOL UnknownExtensionsDecodeXER(PXER_Stream & strm);
1004 virtual void UnknownExtensionsEncodeXER(PXER_Stream & strm) const;
1005 #endif
1007 protected:
1008 BOOL NoExtensionsToDecode(PPER_Stream & strm);
1009 BOOL NoExtensionsToEncode(PPER_Stream & strm);
1011 PASN_ObjectArray fields;
1012 PASN_BitString optionMap;
1013 int knownExtensions;
1014 int totalExtensions;
1015 PASN_BitString extensionMap;
1016 PINDEX endBasicEncoding;
1020 /** Class for ASN Set type.
1022 class PASN_Set : public PASN_Sequence
1024 PCLASSINFO(PASN_Set, PASN_Sequence);
1025 public:
1026 PASN_Set(unsigned tag = UniversalSet,
1027 TagClass tagClass = UniversalTagClass,
1028 unsigned nOpts = 0, BOOL extend = FALSE, unsigned nExtend = 0);
1030 virtual PObject * Clone() const;
1031 virtual PString GetTypeAsString() const;
1035 /** Class for ASN Array type.
1037 class PASN_Array : public PASN_ConstrainedObject
1039 PCLASSINFO(PASN_Array, PASN_ConstrainedObject);
1040 public:
1041 PINDEX GetSize() const { return array.GetSize(); }
1042 BOOL SetSize(PINDEX newSize);
1043 PASN_Object & operator[](PINDEX i) const { return array[i]; }
1044 void Append(PASN_Object * obj) { array.SetAt(array.GetSize(), obj); }
1045 void RemoveAt(PINDEX i) { array.RemoveAt(i); }
1046 void RemoveAll() { array.RemoveAll(); }
1048 virtual Comparison Compare(const PObject & obj) const;
1049 virtual void PrintOn(ostream & strm) const;
1051 virtual void SetConstraintBounds(ConstraintType type, int lower, unsigned upper);
1052 virtual PString GetTypeAsString() const;
1053 virtual PINDEX GetDataLength() const;
1054 virtual BOOL IsPrimitive() const;
1055 virtual BOOL Decode(PASN_Stream &);
1056 virtual void Encode(PASN_Stream &) const;
1058 virtual PASN_Object * CreateObject() const = 0;
1060 PASN_Array & operator=(const PASN_Array & other);
1062 protected:
1063 PASN_Array(unsigned tag = UniversalSequence,
1064 TagClass tagClass = UniversalTagClass);
1066 PASN_Array(const PASN_Array & other);
1068 PASN_ObjectArray array;
1072 /////////////////////////////////////////////////////////////////////////////
1074 /** Base class for ASN decoder/encoder stream.
1076 class PASN_Stream : public PBYTEArray
1078 PCLASSINFO(PASN_Stream, PBYTEArray);
1079 public:
1080 PASN_Stream();
1081 PASN_Stream(const PBYTEArray & bytes);
1082 PASN_Stream(const BYTE * buf, PINDEX size);
1084 void PrintOn(ostream & strm) const;
1086 PINDEX GetPosition() const { return byteOffset; }
1087 void SetPosition(PINDEX newPos);
1088 BOOL IsAtEnd() { return byteOffset >= GetSize(); }
1089 void ResetDecoder();
1090 void BeginEncoding();
1091 void CompleteEncoding();
1093 virtual BOOL Read(PChannel & chan) = 0;
1094 virtual BOOL Write(PChannel & chan) = 0;
1096 virtual BOOL NullDecode(PASN_Null &) = 0;
1097 virtual void NullEncode(const PASN_Null &) = 0;
1098 virtual BOOL BooleanDecode(PASN_Boolean &) = 0;
1099 virtual void BooleanEncode(const PASN_Boolean &) = 0;
1100 virtual BOOL IntegerDecode(PASN_Integer &) = 0;
1101 virtual void IntegerEncode(const PASN_Integer &) = 0;
1102 virtual BOOL EnumerationDecode(PASN_Enumeration &) = 0;
1103 virtual void EnumerationEncode(const PASN_Enumeration &) = 0;
1104 virtual BOOL RealDecode(PASN_Real &) = 0;
1105 virtual void RealEncode(const PASN_Real &) = 0;
1106 virtual BOOL ObjectIdDecode(PASN_ObjectId &) = 0;
1107 virtual void ObjectIdEncode(const PASN_ObjectId &) = 0;
1108 virtual BOOL BitStringDecode(PASN_BitString &) = 0;
1109 virtual void BitStringEncode(const PASN_BitString &) = 0;
1110 virtual BOOL OctetStringDecode(PASN_OctetString &) = 0;
1111 virtual void OctetStringEncode(const PASN_OctetString &) = 0;
1112 virtual BOOL ConstrainedStringDecode(PASN_ConstrainedString &) = 0;
1113 virtual void ConstrainedStringEncode(const PASN_ConstrainedString &) = 0;
1114 virtual BOOL BMPStringDecode(PASN_BMPString &) = 0;
1115 virtual void BMPStringEncode(const PASN_BMPString &) = 0;
1116 virtual BOOL ChoiceDecode(PASN_Choice &) = 0;
1117 virtual void ChoiceEncode(const PASN_Choice &) = 0;
1118 virtual BOOL ArrayDecode(PASN_Array &) = 0;
1119 virtual void ArrayEncode(const PASN_Array &) = 0;
1120 virtual BOOL SequencePreambleDecode(PASN_Sequence &) = 0;
1121 virtual void SequencePreambleEncode(const PASN_Sequence &) = 0;
1122 virtual BOOL SequenceKnownDecode(PASN_Sequence &, PINDEX, PASN_Object &) = 0;
1123 virtual void SequenceKnownEncode(const PASN_Sequence &, PINDEX, const PASN_Object &) = 0;
1124 virtual BOOL SequenceUnknownDecode(PASN_Sequence &) = 0;
1125 virtual void SequenceUnknownEncode(const PASN_Sequence &) = 0;
1127 BYTE ByteDecode();
1128 void ByteEncode(unsigned value);
1130 unsigned BlockDecode(BYTE * bufptr, unsigned nBytes);
1131 void BlockEncode(const BYTE * bufptr, PINDEX nBytes);
1133 void ByteAlign();
1135 protected:
1136 PINDEX byteOffset;
1137 unsigned bitOffset;
1139 private:
1140 void Construct();
1143 #ifdef P_INCLUDE_PER
1144 #include "asnper.h"
1145 #endif
1147 #ifdef P_INCLUDE_BER
1148 #include "asnber.h"
1149 #endif
1151 #ifdef P_INCLUDE_XER
1152 #include "asnxer.h"
1153 #endif
1155 #endif // _ASNER_H