PTLib  Version 2.12.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
asner.h
Go to the documentation of this file.
1 /*
2  * asner.h
3  *
4  * Abstract Syntax Notation Encoding Rules classes
5  *
6  * Portable Windows Library
7  *
8  * Copyright (c) 1993-2002 Equivalence Pty. Ltd.
9  *
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/
14  *
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.
19  *
20  * The Original Code is Portable Windows Library.
21  *
22  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
23  *
24  * Contributor(s): ______________________________________.
25  *
26  * $Revision: 28207 $
27  * $Author: rjongbloed $
28  * $Date: 2012-08-17 10:09:54 +1000 (Fri, 17 Aug 2012) $
29  */
30 
31 #ifndef PTLIB_ASNER_H
32 #define PTLIB_ASNER_H
33 
34 #ifdef P_USE_PRAGMA
35 #pragma interface
36 #endif
37 
38 #if P_ASN
39 
40 // provide options to omit vertain encodings, if needed
41 #define P_INCLUDE_PER
42 #define P_INCLUDE_BER
43 #define P_INCLUDE_XER
44 
45 class PASN_Stream;
46 class PBER_Stream;
47 class PPER_Stream;
48 
49 #ifdef P_EXPAT
50 class PXER_Stream;
51 class PXMLElement;
52 #else
53 #undef P_INCLUDE_XER
54 #endif
55 
56 
58 
61 class PASN_Object : public PObject
62 {
63  PCLASSINFO(PASN_Object, PObject);
64  public:
66  virtual PString GetTypeAsString() const = 0;
67 
68  PINDEX GetObjectLength() const;
69  virtual PINDEX GetDataLength() const = 0;
70  virtual PBoolean IsPrimitive() const { return true; }
71 
72  virtual PBoolean Decode(PASN_Stream &) = 0;
73  virtual void Encode(PASN_Stream &) const = 0;
74 
75  PBoolean IsExtendable() const { return extendable; }
76  void SetExtendable(PBoolean ext = true) { extendable = ext; }
77 
78  enum TagClass {
79  UniversalTagClass,
80  ApplicationTagClass,
81  ContextSpecificTagClass,
82  PrivateTagClass,
83  DefaultTagClass
84  };
85  TagClass GetTagClass() const { return tagClass; }
86 
87  enum UniversalTags {
88  InvalidUniversalTag,
89  UniversalBoolean,
90  UniversalInteger,
91  UniversalBitString,
92  UniversalOctetString,
93  UniversalNull,
94  UniversalObjectId,
95  UniversalObjectDescriptor,
96  UniversalExternalType,
97  UniversalReal,
98  UniversalEnumeration,
99  UniversalEmbeddedPDV,
100  UniversalSequence = 16,
101  UniversalSet,
102  UniversalNumericString,
103  UniversalPrintableString,
104  UniversalTeletexString,
105  UniversalVideotexString,
106  UniversalIA5String,
107  UniversalUTCTime,
108  UniversalGeneralisedTime,
109  UniversalGeneralizedTime = UniversalGeneralisedTime,
110  UniversalGraphicString,
111  UniversalVisibleString,
112  UniversalGeneralString,
113  UniversalUniversalString,
114  UniversalBMPString = 30
115  };
116 
117  unsigned GetTag() const { return tag; }
118  virtual void SetTag(unsigned newTag, TagClass tagClass = DefaultTagClass);
119 
120  enum ConstraintType {
121  Unconstrained,
122  PartiallyConstrained,
123  FixedConstraint,
124  ExtendableConstraint
125  };
126 
127  enum MinimumValueTag { MinimumValue = INT_MIN };
128  enum MaximumValueTag { MaximumValue = INT_MAX };
129  void SetConstraints(ConstraintType type, int value)
130  { SetConstraintBounds(type, value, value); }
131  void SetConstraints(ConstraintType, int lower, MaximumValueTag /*upper*/)
132  { SetConstraintBounds(PartiallyConstrained, (int)lower, lower < 0 ? INT_MAX : UINT_MAX); }
133  void SetConstraints(ConstraintType, MinimumValueTag lower, unsigned upper)
134  { SetConstraintBounds(PartiallyConstrained, (int)lower, (unsigned)upper); }
135  void SetConstraints(ConstraintType, MinimumValueTag lower, MaximumValueTag upper)
136  { SetConstraintBounds(PartiallyConstrained, (int)lower, (unsigned)upper); }
137  void SetConstraints(ConstraintType type, int lower, unsigned upper)
138  { SetConstraintBounds(type, lower, upper); }
139 
140  virtual void SetConstraintBounds(ConstraintType type, int lower, unsigned upper);
141  virtual void SetCharacterSet(ConstraintType ctype, const char * charSet);
142  virtual void SetCharacterSet(ConstraintType ctype, unsigned firstChar, unsigned lastChar);
143 
144  static PINDEX GetMaximumArraySize();
145  static void SetMaximumArraySize(PINDEX sz);
146  static PINDEX GetMaximumStringSize();
147  static void SetMaximumStringSize(PINDEX sz);
148 
149  protected:
150  PASN_Object(unsigned tag, TagClass tagClass, PBoolean extend = false);
151 
153  PBoolean extendable;
155  TagClass tagClass;
157  unsigned tag;
158 };
159 
160 
163 class PASN_ConstrainedObject : public PASN_Object
164 {
165  PCLASSINFO(PASN_ConstrainedObject, PASN_Object);
166  public:
167  PBoolean IsConstrained() const { return constraint != Unconstrained; }
168  int GetLowerLimit() const { return lowerLimit; }
169  unsigned GetUpperLimit() const { return upperLimit; }
170 
171  PBoolean ConstrainedLengthDecode(PPER_Stream & strm, unsigned & length);
172  void ConstrainedLengthEncode(PPER_Stream & strm, unsigned length) const;
173 
174  PBoolean ConstraintEncode(PPER_Stream & strm, unsigned value) const;
175 
176  protected:
177  virtual void SetConstraintBounds(ConstraintType type, int lower, unsigned upper);
178  PASN_ConstrainedObject(unsigned tag, TagClass tagClass);
179 
180  ConstraintType constraint;
181  int lowerLimit;
182  unsigned upperLimit;
183 };
184 
185 
188 class PASN_Null : public PASN_Object
189 {
190  PCLASSINFO(PASN_Null, PASN_Object);
191  public:
192  PASN_Null(unsigned tag = UniversalNull,
193  TagClass tagClass = UniversalTagClass);
194 
195  virtual Comparison Compare(const PObject & obj) const;
196  virtual PObject * Clone() const;
197  virtual void PrintOn(ostream & strm) const;
198 
199  virtual PString GetTypeAsString() const;
200  virtual PINDEX GetDataLength() const;
201  virtual PBoolean Decode(PASN_Stream &);
202  virtual void Encode(PASN_Stream &) const;
203 };
204 
205 
208 class PASN_Boolean : public PASN_Object
209 {
210  PCLASSINFO(PASN_Boolean, PASN_Object);
211  public:
212  PASN_Boolean(PBoolean val = false);
213  PASN_Boolean(unsigned tag, TagClass tagClass, PBoolean val = false);
214 
215  PASN_Boolean & operator=(PBoolean v) { value = v; return *this; }
216  operator PBoolean() const { return value; }
217  PBoolean GetValue() const { return value; }
218  void SetValue(PBoolean v) { value = v; }
219 
220  virtual Comparison Compare(const PObject & obj) const;
221  virtual PObject * Clone() const;
222  virtual void PrintOn(ostream & strm) const;
223 
224  virtual PString GetTypeAsString() const;
225  virtual PINDEX GetDataLength() const;
226  virtual PBoolean Decode(PASN_Stream &);
227  virtual void Encode(PASN_Stream &) const;
228 
229  protected:
230  PBoolean value;
231 };
232 
233 
236 class PASN_Integer : public PASN_ConstrainedObject
237 {
238  PCLASSINFO(PASN_Integer, PASN_ConstrainedObject);
239  public:
240  PASN_Integer(unsigned val = 0);
241  PASN_Integer(unsigned tag, TagClass tagClass, unsigned val = 0);
242 
243  PASN_Integer & operator=(unsigned value);
244  operator unsigned() const { return value; }
245  unsigned GetValue() const { return value; }
246  void SetValue(unsigned v) { operator=(v); }
247 
248  virtual Comparison Compare(const PObject & obj) const;
249  virtual PObject * Clone() const;
250  virtual void PrintOn(ostream & strm) const;
251 
252  virtual void SetConstraintBounds(ConstraintType type, int lower, unsigned upper);
253  virtual PString GetTypeAsString() const;
254  virtual PINDEX GetDataLength() const;
255  virtual PBoolean Decode(PASN_Stream &);
256  virtual void Encode(PASN_Stream &) const;
257 
258 #ifdef P_INCLUDE_PER
259  PBoolean DecodePER(PPER_Stream & strm);
260  void EncodePER(PPER_Stream & strm) const;
261 #endif
262 
263  PBoolean IsUnsigned() const;
264 
265  protected:
266  unsigned value;
267 };
268 
269 struct PASN_Names{
270  const char * name;
271  unsigned value;
272 };
273 
276 class PASN_Enumeration : public PASN_Object
277 {
278  PCLASSINFO(PASN_Enumeration, PASN_Object);
279  public:
280  PASN_Enumeration(unsigned val = 0);
281  PASN_Enumeration(unsigned tag,
282  TagClass tagClass,
283  unsigned nEnums = UINT_MAX,
284  PBoolean extendable = false,
285  unsigned val = 0);
286  PASN_Enumeration(unsigned tag,
287  TagClass tagClass,
288  unsigned nEnums,
289  PBoolean extendable,
290  const PASN_Names * nameSpec,
291  unsigned namesCnt,
292  unsigned val = 0);
293 
294  PASN_Enumeration & operator=(unsigned v) { value = v; return *this; }
295  operator unsigned() const { return value; }
296  unsigned GetValue() const { return value; }
297  void SetValue(unsigned v) { value = v; }
298 
299  unsigned GetMaximum() const { return maxEnumValue; }
300 
301  virtual Comparison Compare(const PObject & obj) const;
302  virtual PObject * Clone() const;
303  virtual void PrintOn(ostream & strm) const;
304 
305  virtual PString GetTypeAsString() const;
306  virtual PINDEX GetDataLength() const;
307  virtual PBoolean Decode(PASN_Stream &);
308  virtual void Encode(PASN_Stream &) const;
309 
310 #ifdef P_INCLUDE_PER
311  PBoolean DecodePER(PPER_Stream & strm);
312  void EncodePER(PPER_Stream & strm) const;
313 #endif
314 
315 #ifdef P_INCLUDE_XER
316  virtual PBoolean DecodeXER(PXER_Stream & strm);
317  virtual void EncodeXER(PXER_Stream & strm) const;
318 #endif
319 
320  PINDEX GetValueByName(PString name) const;
321  protected:
322  unsigned maxEnumValue;
323  unsigned value;
324  const PASN_Names *names;
325  unsigned namesCount;
326 };
327 
328 
331 class PASN_Real : public PASN_Object
332 {
333  PCLASSINFO(PASN_Real, PASN_Object);
334  public:
335  PASN_Real(double val = 0);
336  PASN_Real(unsigned tag, TagClass tagClass, double val = 0);
337 
338  PASN_Real & operator=(double val) { value = val; return *this; }
339  operator double() const { return value; }
340  double GetValue() const { return value; }
341  void SetValue(double v) { value = v; }
342 
343  virtual Comparison Compare(const PObject & obj) const;
344  virtual PObject * Clone() const;
345  virtual void PrintOn(ostream & strm) const;
346 
347  virtual PString GetTypeAsString() const;
348  virtual PINDEX GetDataLength() const;
349  virtual PBoolean Decode(PASN_Stream &);
350  virtual void Encode(PASN_Stream &) const;
351 
352  protected:
353  double value;
354 };
355 
356 
359 class PASN_ObjectId : public PASN_Object
360 {
361  PCLASSINFO(PASN_ObjectId, PASN_Object);
362  public:
363  PASN_ObjectId(const char * dotstr = NULL);
364  PASN_ObjectId(unsigned tag, TagClass tagClass);
365 
366  PASN_ObjectId(const PASN_ObjectId & other);
367  PASN_ObjectId & operator=(const PASN_ObjectId & other);
368 
369  PASN_ObjectId & operator=(const char * dotstr);
370  PASN_ObjectId & operator=(const PString & dotstr);
371  PASN_ObjectId & operator=(const PUnsignedArray & numbers);
372  void SetValue(const PString & dotstr);
373  void SetValue(const PUnsignedArray & numbers) { value = numbers; }
374  void SetValue(const unsigned * numbers, PINDEX size);
375 
376  bool operator==(const char * dotstr) const;
377  bool operator!=(const char * dotstr) const { return !operator==(dotstr); }
378  bool operator==(const PString & dotstr) const { return operator==((const char *)dotstr); }
379  bool operator!=(const PString & dotstr) const { return !operator==((const char *)dotstr); }
380  bool operator==(const PASN_ObjectId & id) const { return value == id.value; }
381 
382  PINDEX GetSize() const { return value.GetSize(); }
383  unsigned operator[](PINDEX idx) const { return value[idx]; }
384  const PUnsignedArray & GetValue() const { return value; }
385  PString AsString() const;
386 
387  virtual Comparison Compare(const PObject & obj) const;
388  virtual PObject * Clone() const;
389  virtual void PrintOn(ostream & strm) const;
390 
391  virtual PString GetTypeAsString() const;
392  virtual PINDEX GetDataLength() const;
393  virtual PBoolean Decode(PASN_Stream &);
394  virtual void Encode(PASN_Stream &) const;
395 
396  PBoolean CommonDecode(PASN_Stream & strm, unsigned dataLen);
397  void CommonEncode(PBYTEArray & eObjId) const;
398 
399  protected:
400  PUnsignedArray value;
401 };
402 
403 
406 class PASN_BitString : public PASN_ConstrainedObject
407 {
408  PCLASSINFO(PASN_BitString, PASN_ConstrainedObject);
409  public:
410  PASN_BitString(unsigned nBits = 0, const BYTE * buf = NULL);
411  PASN_BitString(unsigned tag, TagClass tagClass, unsigned nBits = 0);
412 
413  PASN_BitString(const PASN_BitString & other);
414  PASN_BitString & operator=(const PASN_BitString & other);
415 
416  void SetData(unsigned nBits, const PBYTEArray & bytes);
417  void SetData(unsigned nBits, const BYTE * buf, PINDEX size = 0);
418 
419  const BYTE * GetDataPointer() const { return bitData; }
420 
421  unsigned GetSize() const { return totalBits; }
422  PBoolean SetSize(unsigned nBits);
423 
424  bool operator[](PINDEX bit) const;
425  void Set(unsigned bit);
426  void Clear(unsigned bit);
427  void Invert(unsigned bit);
428 
429  virtual Comparison Compare(const PObject & obj) const;
430  virtual PObject * Clone() const;
431  virtual void PrintOn(ostream & strm) const;
432 
433  virtual void SetConstraintBounds(ConstraintType type, int lower, unsigned upper);
434  virtual PString GetTypeAsString() const;
435  virtual PINDEX GetDataLength() const;
436  virtual PBoolean Decode(PASN_Stream &);
437  virtual void Encode(PASN_Stream &) const;
438 
439 #ifdef P_INCLUDE_BER
440  PBoolean DecodeBER(PBER_Stream & strm, unsigned len);
441  void EncodeBER(PBER_Stream & strm) const;
442 #endif
443 
444 #ifdef P_INCLUDE_PER
445  PBoolean DecodePER(PPER_Stream & strm);
446  void EncodePER(PPER_Stream & strm) const;
447 #endif
448 
449  PBoolean DecodeSequenceExtensionBitmap(PPER_Stream & strm);
450  void EncodeSequenceExtensionBitmap(PPER_Stream & strm) const;
451 
452  protected:
453  unsigned totalBits;
454  PBYTEArray bitData;
455 };
456 
457 
460 class PASN_OctetString : public PASN_ConstrainedObject
461 {
462  PCLASSINFO(PASN_OctetString, PASN_ConstrainedObject);
463  public:
464  PASN_OctetString(const char * str = NULL, PINDEX size = 0);
465  PASN_OctetString(unsigned tag, TagClass tagClass);
466 
467  PASN_OctetString(const PASN_OctetString & other);
468  PASN_OctetString & operator=(const PASN_OctetString & other);
469 
470  PASN_OctetString & operator=(const char * str);
471  PASN_OctetString & operator=(const PString & str);
472  PASN_OctetString & operator=(const PBYTEArray & arr);
473  void SetValue(const char * str) { operator=(str); }
474  void SetValue(const PString & str) { operator=(str); }
475  void SetValue(const PBYTEArray & arr) { operator=(arr); }
476  void SetValue(const BYTE * data, PINDEX len);
477  const PBYTEArray & GetValue() const { return value; }
478  operator const PBYTEArray &() const { return value; }
479  operator const BYTE *() const { return value; }
480  PString AsString() const;
481  BYTE operator[](PINDEX i) const { return value[i]; }
482  BYTE & operator[](PINDEX i) { return value[i]; }
483  BYTE * GetPointer(PINDEX sz = 0) { return value.GetPointer(sz); }
484  PINDEX GetSize() const { return value.GetSize(); }
485  PBoolean SetSize(PINDEX newSize);
486 
487  virtual Comparison Compare(const PObject & obj) const;
488  virtual PObject * Clone() const;
489  virtual void PrintOn(ostream & strm) const;
490 
491  virtual void SetConstraintBounds(ConstraintType type, int lower, unsigned upper);
492  virtual PString GetTypeAsString() const;
493  virtual PINDEX GetDataLength() const;
494  virtual PBoolean Decode(PASN_Stream &);
495  virtual void Encode(PASN_Stream &) const;
496 
497 #ifdef P_INCLUDE_PER
498  PBoolean DecodePER(PPER_Stream & strm);
499  void EncodePER(PPER_Stream & strm) const;
500 #endif
501 
502  PBoolean DecodeSubType(PASN_Object &) const;
503  void EncodeSubType(const PASN_Object &);
504 
505  protected:
506  PBYTEArray value;
507 };
508 
509 
512 class PASN_ConstrainedString : public PASN_ConstrainedObject
513 {
514  PCLASSINFO(PASN_ConstrainedString, PASN_ConstrainedObject);
515  public:
516  PASN_ConstrainedString & operator=(const char * str);
517  PASN_ConstrainedString & operator=(const PString & str) { return operator=((const char *)str); }
518  operator const PString &() const { return value; }
519  const PString & GetValue() const { return value; }
520  void SetValue(const char * v) { operator=(v); }
521  void SetValue(const PString & v) { operator=(v); }
522  char operator[](PINDEX idx) const { return value[idx]; }
523 
524  void SetCharacterSet(ConstraintType ctype, const char * charSet);
525  void SetCharacterSet(ConstraintType ctype, unsigned firstChar = 0, unsigned lastChar = 255);
526  void SetCharacterSet(const char * charSet, PINDEX size, ConstraintType ctype);
527 
528  virtual Comparison Compare(const PObject & obj) const;
529  virtual void PrintOn(ostream & strm) const;
530 
531  virtual void SetConstraintBounds(ConstraintType type, int lower, unsigned upper);
532  virtual PINDEX GetDataLength() const;
533  virtual PBoolean Decode(PASN_Stream &);
534  virtual void Encode(PASN_Stream &) const;
535 
536 #ifdef P_INCLUDE_BER
537  PBoolean DecodeBER(PBER_Stream & strm, unsigned len);
538  void EncodeBER(PBER_Stream & strm) const;
539 #endif
540 
541 #ifdef P_INCLUDE_PER
542  PBoolean DecodePER(PPER_Stream & strm);
543  void EncodePER(PPER_Stream & strm) const;
544 #endif
545 
546  protected:
547  PASN_ConstrainedString(const char * canonicalSet, PINDEX setSize,
548  unsigned tag, TagClass tagClass);
549 
550  PString value;
551  PCharArray characterSet;
552  const char * canonicalSet;
553  PINDEX canonicalSetSize;
554  unsigned canonicalSetBits;
555  unsigned charSetUnalignedBits;
556  unsigned charSetAlignedBits;
557 };
558 
559 
560 #define DECLARE_STRING_CLASS(name) \
561  class PASN_##name##String : public PASN_ConstrainedString { \
562  PCLASSINFO(PASN_##name##String, PASN_ConstrainedString); \
563  public: \
564  PASN_##name##String(const char * str = NULL); \
565  PASN_##name##String(unsigned tag, TagClass tagClass); \
566  PASN_##name##String & operator=(const char * str); \
567  PASN_##name##String & operator=(const PString & str); \
568  virtual PObject * Clone() const; \
569  virtual PString GetTypeAsString() const; \
570  }
571 
572 DECLARE_STRING_CLASS(Numeric);
573 DECLARE_STRING_CLASS(Printable);
574 DECLARE_STRING_CLASS(Visible);
575 DECLARE_STRING_CLASS(IA5);
576 DECLARE_STRING_CLASS(General);
577 
578 
581 class PASN_BMPString : public PASN_ConstrainedObject
582 {
583  PCLASSINFO(PASN_BMPString, PASN_ConstrainedObject);
584  public:
585  PASN_BMPString(const char * str = NULL);
586  PASN_BMPString(const PWCharArray & wstr);
587  PASN_BMPString(unsigned tag, TagClass tagClass);
588 
589  PASN_BMPString(const PASN_BMPString & other);
590  PASN_BMPString & operator=(const PASN_BMPString & other);
591 
592  PASN_BMPString & operator=(const char * v) { return operator=(PString(v).AsUCS2()); }
593  PASN_BMPString & operator=(const PString & v) { return operator=(v.AsUCS2()); }
594  PASN_BMPString & operator=(const PWCharArray & v);
595  operator PString() const { return GetValue(); }
596  operator PWCharArray() const { return value; }
597  PString GetValue() const { return value; }
598  void GetValue(PWCharArray & v) const { v = value; }
599  void SetValue(const char * v) { operator=(PString(v).AsUCS2()); }
600  void SetValue(const PString & v) { operator=(v.AsUCS2()); }
601  void SetValue(const PWCharArray & v) { operator=(v); }
602  void SetValue(const PASN_BMPString & v) { operator=(v.value); }
603  void SetValueRaw(const PWCharArray & v) { SetValueRaw(v, v.GetSize()); }
604  void SetValueRaw(const wchar_t * val, PINDEX len);
605 
606  void SetCharacterSet(ConstraintType ctype, const char * charSet);
607  void SetCharacterSet(ConstraintType ctype, const PWCharArray & charSet);
608  void SetCharacterSet(ConstraintType ctype, unsigned firstChar, unsigned lastChar);
609 
610  virtual Comparison Compare(const PObject & obj) const;
611  virtual PObject * Clone() const;
612  virtual void PrintOn(ostream & strm) const;
613 
614  virtual PString GetTypeAsString() const;
615  virtual PINDEX GetDataLength() const;
616  virtual PBoolean Decode(PASN_Stream &);
617  virtual void Encode(PASN_Stream &) const;
618 
619 #ifdef P_INCLUDE_BER
620  PBoolean DecodeBER(PBER_Stream & strm, unsigned len);
621  void EncodeBER(PBER_Stream & strm) const;
622 #endif
623 
624 #ifdef P_INCLUDE_PER
625  PBoolean DecodePER(PPER_Stream & strm);
626  void EncodePER(PPER_Stream & strm) const;
627 #endif
628 
629  protected:
630  void Construct();
631  PBoolean IsLegalCharacter(WORD ch);
632 
633  PWCharArray value;
634  PWCharArray characterSet;
635  wchar_t firstChar, lastChar;
636  unsigned charSetUnalignedBits;
637  unsigned charSetAlignedBits;
638 };
639 
640 
641 class PASN_GeneralisedTime : public PASN_VisibleString
642 {
643  PCLASSINFO(PASN_GeneralisedTime, PASN_VisibleString);
644  public:
645  PASN_GeneralisedTime()
646  : PASN_VisibleString(UniversalGeneralisedTime, UniversalTagClass) { }
647  PASN_GeneralisedTime(const PTime & time)
648  : PASN_VisibleString(UniversalGeneralisedTime, UniversalTagClass) { SetValue(time); }
649  PASN_GeneralisedTime(unsigned theTag, TagClass theTagClass)
650  : PASN_VisibleString(theTag, theTagClass) { }
651 
652  PASN_GeneralisedTime & operator=(const PTime & time);
653  void SetValue(const PTime & time) { operator=(time); }
654  PTime GetValue() const;
655 };
656 
657 
658 class PASN_UniversalTime : public PASN_VisibleString
659 {
660  PCLASSINFO(PASN_UniversalTime, PASN_VisibleString);
661  public:
662  PASN_UniversalTime()
663  : PASN_VisibleString(UniversalUTCTime, UniversalTagClass) { }
664  PASN_UniversalTime(const PTime & time)
665  : PASN_VisibleString(UniversalUTCTime, UniversalTagClass) { SetValue(time); }
666  PASN_UniversalTime(unsigned theTag, TagClass theTagClass)
667  : PASN_VisibleString(theTag, theTagClass) { }
668 
669  PASN_UniversalTime & operator=(const PTime & time);
670  void SetValue(const PTime & time) { operator=(time); }
671  PTime GetValue() const;
672 };
673 
674 
675 class PASN_Sequence;
676 
679 class PASN_Choice : public PASN_Object
680 {
681  PCLASSINFO(PASN_Choice, PASN_Object);
682  public:
683  ~PASN_Choice();
684 
685  virtual void SetTag(unsigned newTag, TagClass tagClass = DefaultTagClass);
686  PString GetTagName() const;
687  PASN_Object & GetObject() const;
688  PBoolean IsValid() const { return choice != NULL; }
689 
690 #if defined(__GNUC__) && __GNUC__ <= 2 && __GNUC_MINOR__ < 9
691 
692  operator PASN_Null &() const;
693  operator PASN_Boolean &() const;
694  operator PASN_Integer &() const;
695  operator PASN_Enumeration &() const;
696  operator PASN_Real &() const;
697  operator PASN_ObjectId &() const;
698  operator PASN_BitString &() const;
699  operator PASN_OctetString &() const;
700  operator PASN_NumericString &() const;
701  operator PASN_PrintableString &() const;
702  operator PASN_VisibleString &() const;
703  operator PASN_IA5String &() const;
704  operator PASN_GeneralString &() const;
705  operator PASN_BMPString &() const;
706  operator PASN_Sequence &() const;
707 
708 #else
709 
710  operator PASN_Null &();
711  operator PASN_Boolean &();
712  operator PASN_Integer &();
713  operator PASN_Enumeration &();
714  operator PASN_Real &();
715  operator PASN_ObjectId &();
716  operator PASN_BitString &();
717  operator PASN_OctetString &();
718  operator PASN_NumericString &();
719  operator PASN_PrintableString &();
720  operator PASN_VisibleString &();
721  operator PASN_IA5String &();
722  operator PASN_GeneralString &();
723  operator PASN_BMPString &();
724  operator PASN_Sequence &();
725 
726  operator const PASN_Null &() const;
727  operator const PASN_Boolean &() const;
728  operator const PASN_Integer &() const;
729  operator const PASN_Enumeration &() const;
730  operator const PASN_Real &() const;
731  operator const PASN_ObjectId &() const;
732  operator const PASN_BitString &() const;
733  operator const PASN_OctetString &() const;
734  operator const PASN_NumericString &() const;
735  operator const PASN_PrintableString &() const;
736  operator const PASN_VisibleString &() const;
737  operator const PASN_IA5String &() const;
738  operator const PASN_GeneralString &() const;
739  operator const PASN_BMPString &() const;
740  operator const PASN_Sequence &() const;
741 
742 #endif
743 
744  virtual PBoolean CreateObject() = 0;
745 
746  virtual Comparison Compare(const PObject & obj) const;
747  virtual void PrintOn(ostream & strm) const;
748 
749  virtual PString GetTypeAsString() const;
750  virtual PINDEX GetDataLength() const;
751  virtual PBoolean IsPrimitive() const;
752  virtual PBoolean Decode(PASN_Stream &);
753  virtual void Encode(PASN_Stream &) const;
754 
755 #ifdef P_INCLUDE_PER
756  virtual PBoolean DecodePER(PPER_Stream &);
757  virtual void EncodePER(PPER_Stream &) const;
758 #endif
759 
760 #ifdef P_INCLUDE_XER
761  PBoolean DecodeXER(PXER_Stream &);
762  void EncodeXER(PXER_Stream &) const;
763 #endif
764 
765  PASN_Choice & operator=(const PASN_Choice & other);
766 
767  PINDEX GetValueByName(PString name) const;
768  protected:
769  PASN_Choice(unsigned nChoices = 0, PBoolean extend = false);
770  PASN_Choice(unsigned tag, TagClass tagClass, unsigned nChoices, PBoolean extend);
771  PASN_Choice(unsigned tag, TagClass tagClass, unsigned nChoices, PBoolean extend, const PASN_Names * nameSpec,unsigned namesCnt);
772 
773  PASN_Choice(const PASN_Choice & other);
774 
775  PBoolean CheckCreate() const;
776 
777  unsigned numChoices;
778  PASN_Object * choice;
779  const PASN_Names *names;
780  unsigned namesCount;
781 };
782 
783 
784 PARRAY(PASN_ObjectArray, PASN_Object);
785 
786 
789 class PASN_Sequence : public PASN_Object
790 {
791  PCLASSINFO(PASN_Sequence, PASN_Object);
792  public:
793  PASN_Sequence(unsigned tag = UniversalSequence,
794  TagClass tagClass = UniversalTagClass,
795  unsigned nOpts = 0, PBoolean extend = false, unsigned nExtend = 0);
796 
797  PASN_Sequence(const PASN_Sequence & other);
798  PASN_Sequence & operator=(const PASN_Sequence & other);
799 
800  PINDEX GetSize() const { return fields.GetSize(); }
801  PBoolean SetSize(PINDEX newSize);
802  PASN_Object & operator[](PINDEX i) const { return fields[i]; }
803 
804  PBoolean HasOptionalField(PINDEX opt) const;
805  void IncludeOptionalField(PINDEX opt);
806  void RemoveOptionalField(PINDEX opt);
807 
808  virtual Comparison Compare(const PObject & obj) const;
809  virtual PObject * Clone() const;
810  virtual void PrintOn(ostream & strm) const;
811 
812  virtual PString GetTypeAsString() const;
813  virtual PINDEX GetDataLength() const;
814  virtual PBoolean IsPrimitive() const;
815  virtual PBoolean Decode(PASN_Stream &);
816  virtual void Encode(PASN_Stream &) const;
817 
818  PBoolean PreambleDecode(PASN_Stream & strm);
819  void PreambleEncode(PASN_Stream & strm) const;
820  PBoolean KnownExtensionDecode(PASN_Stream & strm, PINDEX fld, PASN_Object & field);
821  void KnownExtensionEncode(PASN_Stream & strm, PINDEX fld, const PASN_Object & field) const;
822  PBoolean UnknownExtensionsDecode(PASN_Stream & strm);
823  void UnknownExtensionsEncode(PASN_Stream & strm) const;
824 
825 #ifdef P_INCLUDE_BER
826  PBoolean PreambleDecodeBER(PBER_Stream & strm);
827  void PreambleEncodeBER(PBER_Stream & strm) const;
828  PBoolean KnownExtensionDecodeBER(PBER_Stream & strm, PINDEX fld, PASN_Object & field);
829  void KnownExtensionEncodeBER(PBER_Stream & strm, PINDEX fld, const PASN_Object & field) const;
830  PBoolean UnknownExtensionsDecodeBER(PBER_Stream & strm);
831  void UnknownExtensionsEncodeBER(PBER_Stream & strm) const;
832 #endif
833 
834 #ifdef P_INCLUDE_PER
835  PBoolean PreambleDecodePER(PPER_Stream & strm);
836  void PreambleEncodePER(PPER_Stream & strm) const;
837  PBoolean KnownExtensionDecodePER(PPER_Stream & strm, PINDEX fld, PASN_Object & field);
838  void KnownExtensionEncodePER(PPER_Stream & strm, PINDEX fld, const PASN_Object & field) const;
839  PBoolean UnknownExtensionsDecodePER(PPER_Stream & strm);
840  void UnknownExtensionsEncodePER(PPER_Stream & strm) const;
841 #endif
842 
843 #ifdef P_INCLUDE_XER
844  virtual PBoolean PreambleDecodeXER(PXER_Stream & strm);
845  virtual void PreambleEncodeXER(PXER_Stream & strm) const;
846  virtual PBoolean KnownExtensionDecodeXER(PXER_Stream & strm, PINDEX fld, PASN_Object & field);
847  virtual void KnownExtensionEncodeXER(PXER_Stream & strm, PINDEX fld, const PASN_Object & field) const;
848  virtual PBoolean UnknownExtensionsDecodeXER(PXER_Stream & strm);
849  virtual void UnknownExtensionsEncodeXER(PXER_Stream & strm) const;
850 #endif
851 
852  protected:
853  PBoolean NoExtensionsToDecode(PPER_Stream & strm);
854  PBoolean NoExtensionsToEncode(PPER_Stream & strm);
855 
856  PASN_ObjectArray fields;
857  PASN_BitString optionMap;
858  int knownExtensions;
859  int totalExtensions;
860  PASN_BitString extensionMap;
861  PINDEX endBasicEncoding;
862 };
863 
864 
867 class PASN_Set : public PASN_Sequence
868 {
869  PCLASSINFO(PASN_Set, PASN_Sequence);
870  public:
871  PASN_Set(unsigned tag = UniversalSet,
872  TagClass tagClass = UniversalTagClass,
873  unsigned nOpts = 0, PBoolean extend = false, unsigned nExtend = 0);
874 
875  virtual PObject * Clone() const;
876  virtual PString GetTypeAsString() const;
877 };
878 
879 
882 class PASN_Array : public PASN_ConstrainedObject
883 {
884  PCLASSINFO(PASN_Array, PASN_ConstrainedObject);
885  public:
886  PINDEX GetSize() const { return array.GetSize(); }
887  PBoolean SetSize(PINDEX newSize);
888  PASN_Object & operator[](PINDEX i) const { return array[i]; }
889  void Append(PASN_Object * obj) { array.SetAt(array.GetSize(), obj); }
890  void RemoveAt(PINDEX i) { array.RemoveAt(i); }
891  void RemoveAll() { array.RemoveAll(); }
892 
893  virtual Comparison Compare(const PObject & obj) const;
894  virtual void PrintOn(ostream & strm) const;
895 
896  virtual void SetConstraintBounds(ConstraintType type, int lower, unsigned upper);
897  virtual PString GetTypeAsString() const;
898  virtual PINDEX GetDataLength() const;
899  virtual PBoolean IsPrimitive() const;
900  virtual PBoolean Decode(PASN_Stream &);
901  virtual void Encode(PASN_Stream &) const;
902 
903  virtual PASN_Object * CreateObject() const = 0;
904 
905  PASN_Array & operator=(const PASN_Array & other);
906 
907  protected:
908  PASN_Array(unsigned tag = UniversalSequence,
909  TagClass tagClass = UniversalTagClass);
910 
911  PASN_Array(const PASN_Array & other);
912 
913  PASN_ObjectArray array;
914 };
915 
916 
918 
921 class PASN_Stream : public PBYTEArray
922 {
923  PCLASSINFO(PASN_Stream, PBYTEArray);
924  public:
925  PASN_Stream();
926  PASN_Stream(const PBYTEArray & bytes);
927  PASN_Stream(const BYTE * buf, PINDEX size);
928 
929  void PrintOn(ostream & strm) const;
930 
931  PINDEX GetPosition() const { return byteOffset; }
932  void SetPosition(PINDEX newPos);
933  PBoolean IsAtEnd() { return byteOffset >= GetSize(); }
934  void ResetDecoder();
935  void BeginEncoding();
936  void CompleteEncoding();
937 
938  virtual PBoolean Read(PChannel & chan) = 0;
939  virtual PBoolean Write(PChannel & chan) = 0;
940 
941  virtual PBoolean NullDecode(PASN_Null &) = 0;
942  virtual void NullEncode(const PASN_Null &) = 0;
943  virtual PBoolean BooleanDecode(PASN_Boolean &) = 0;
944  virtual void BooleanEncode(const PASN_Boolean &) = 0;
945  virtual PBoolean IntegerDecode(PASN_Integer &) = 0;
946  virtual void IntegerEncode(const PASN_Integer &) = 0;
947  virtual PBoolean EnumerationDecode(PASN_Enumeration &) = 0;
948  virtual void EnumerationEncode(const PASN_Enumeration &) = 0;
949  virtual PBoolean RealDecode(PASN_Real &) = 0;
950  virtual void RealEncode(const PASN_Real &) = 0;
951  virtual PBoolean ObjectIdDecode(PASN_ObjectId &) = 0;
952  virtual void ObjectIdEncode(const PASN_ObjectId &) = 0;
953  virtual PBoolean BitStringDecode(PASN_BitString &) = 0;
954  virtual void BitStringEncode(const PASN_BitString &) = 0;
955  virtual PBoolean OctetStringDecode(PASN_OctetString &) = 0;
956  virtual void OctetStringEncode(const PASN_OctetString &) = 0;
957  virtual PBoolean ConstrainedStringDecode(PASN_ConstrainedString &) = 0;
958  virtual void ConstrainedStringEncode(const PASN_ConstrainedString &) = 0;
959  virtual PBoolean BMPStringDecode(PASN_BMPString &) = 0;
960  virtual void BMPStringEncode(const PASN_BMPString &) = 0;
961  virtual PBoolean ChoiceDecode(PASN_Choice &) = 0;
962  virtual void ChoiceEncode(const PASN_Choice &) = 0;
963  virtual PBoolean ArrayDecode(PASN_Array &) = 0;
964  virtual void ArrayEncode(const PASN_Array &) = 0;
965  virtual PBoolean SequencePreambleDecode(PASN_Sequence &) = 0;
966  virtual void SequencePreambleEncode(const PASN_Sequence &) = 0;
967  virtual PBoolean SequenceKnownDecode(PASN_Sequence &, PINDEX, PASN_Object &) = 0;
968  virtual void SequenceKnownEncode(const PASN_Sequence &, PINDEX, const PASN_Object &) = 0;
969  virtual PBoolean SequenceUnknownDecode(PASN_Sequence &) = 0;
970  virtual void SequenceUnknownEncode(const PASN_Sequence &) = 0;
971 
972  BYTE ByteDecode();
973  void ByteEncode(unsigned value);
974 
975  unsigned BlockDecode(BYTE * bufptr, unsigned nBytes);
976  void BlockEncode(const BYTE * bufptr, PINDEX nBytes);
977 
978  void ByteAlign();
979 
980  protected:
981  PINDEX byteOffset;
982  unsigned bitOffset;
983 
984  private:
985  void Construct();
986 };
987 
988 #ifdef P_INCLUDE_PER
989 #include "asnper.h"
990 #endif
991 
992 #ifdef P_INCLUDE_BER
993 #include "asnber.h"
994 #endif
995 
996 #ifdef P_INCLUDE_XER
997 #include "asnxer.h"
998 #endif
999 
1000 #endif // P_ASN
1001 
1002 #endif // PTLIB_ASNER_H
1003 
1004 
1005 // End Of File ///////////////////////////////////////////////////////////////