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