PTLib  Version 2.18.8
 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 
27 #ifndef PTLIB_ASNER_H
28 #define PTLIB_ASNER_H
29 
30 #ifdef P_USE_PRAGMA
31 #pragma interface
32 #endif
33 
34 #if P_ASN
35 
36 #if !PTRACING && !defined(PASN_NOPRINTON)
37  #define PASN_NOPRINTON 1
38 #endif
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 m_extendable; }
76  void SetExtendable(PBoolean ext = true) { m_extendable = ext; }
77 
78  enum TagClass {
79  UniversalTagClass,
80  ApplicationTagClass,
81  ContextSpecificTagClass,
82  PrivateTagClass,
83  DefaultTagClass
84  };
85  TagClass GetTagClass() const { return m_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 m_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 
152 
153  PBoolean m_extendable;
154  TagClass m_tagClass;
155  unsigned m_tag;
156 };
157 
158 
161 class PASN_ConstrainedObject : public PASN_Object
162 {
163  PCLASSINFO(PASN_ConstrainedObject, PASN_Object);
164  public:
165  PBoolean IsConstrained() const { return constraint != Unconstrained; }
166  int GetLowerLimit() const { return lowerLimit; }
167  unsigned GetUpperLimit() const { return upperLimit; }
168 
169  PBoolean ConstrainedLengthDecode(PPER_Stream & strm, unsigned & length);
170  void ConstrainedLengthEncode(PPER_Stream & strm, unsigned length) const;
171 
172  PBoolean ConstraintEncode(PPER_Stream & strm, unsigned value) const;
173 
174  protected:
175  virtual void SetConstraintBounds(ConstraintType type, int lower, unsigned upper);
176  PASN_ConstrainedObject(unsigned tag, TagClass tagClass);
177 
178  ConstraintType constraint;
179  int lowerLimit;
180  unsigned upperLimit;
181 };
182 
183 
186 class PASN_Null : public PASN_Object
187 {
188  PCLASSINFO(PASN_Null, PASN_Object);
189  public:
190  PASN_Null(unsigned tag = UniversalNull,
191  TagClass tagClass = UniversalTagClass);
192 
193  virtual Comparison Compare(const PObject & obj) const;
194  virtual PObject * Clone() const;
195  virtual void PrintOn(ostream & strm) const;
196 
197  virtual PString GetTypeAsString() const;
198  virtual PINDEX GetDataLength() const;
199  virtual PBoolean Decode(PASN_Stream &);
200  virtual void Encode(PASN_Stream &) const;
201 };
202 
203 
206 class PASN_Boolean : public PASN_Object
207 {
208  PCLASSINFO(PASN_Boolean, PASN_Object);
209  public:
210  PASN_Boolean(PBoolean val = false);
211  PASN_Boolean(unsigned tag, TagClass tagClass, PBoolean val = false);
212 
213  PASN_Boolean & operator=(PBoolean v) { value = v; return *this; }
214  operator PBoolean() const { return value; }
215  PBoolean GetValue() const { return value; }
216  void SetValue(PBoolean v) { value = v; }
217 
218  virtual Comparison Compare(const PObject & obj) const;
219  virtual PObject * Clone() const;
220  virtual void PrintOn(ostream & strm) const;
221 
222  virtual PString GetTypeAsString() const;
223  virtual PINDEX GetDataLength() const;
224  virtual PBoolean Decode(PASN_Stream &);
225  virtual void Encode(PASN_Stream &) const;
226 
227  protected:
228  PBoolean value;
229 };
230 
231 
234 class PASN_Integer : public PASN_ConstrainedObject
235 {
236  PCLASSINFO(PASN_Integer, PASN_ConstrainedObject);
237  public:
238  PASN_Integer(unsigned val = 0);
239  PASN_Integer(unsigned tag, TagClass tagClass, unsigned val = 0);
240 
241  PASN_Integer & operator=(unsigned value);
242  operator unsigned() const { return value; }
243  unsigned GetValue() const { return value; }
244  void SetValue(unsigned v) { operator=(v); }
245 
246  virtual Comparison Compare(const PObject & obj) const;
247  virtual PObject * Clone() const;
248  virtual void PrintOn(ostream & strm) const;
249 
250  virtual void SetConstraintBounds(ConstraintType type, int lower, unsigned upper);
251  virtual PString GetTypeAsString() const;
252  virtual PINDEX GetDataLength() const;
253  virtual PBoolean Decode(PASN_Stream &);
254  virtual void Encode(PASN_Stream &) const;
255 
256 #ifdef P_INCLUDE_PER
257  PBoolean DecodePER(PPER_Stream & strm);
258  void EncodePER(PPER_Stream & strm) const;
259 #endif
260 
261  PBoolean IsUnsigned() const;
262 
263  protected:
264  unsigned value;
265 };
266 
267 struct PASN_Names{
268  const char * name;
269  unsigned value;
270 };
271 
274 class PASN_Enumeration : public PASN_Object
275 {
276  PCLASSINFO(PASN_Enumeration, PASN_Object);
277  public:
278  PASN_Enumeration(unsigned val = 0);
279  PASN_Enumeration(unsigned tag,
280  TagClass tagClass,
281  unsigned nEnums = UINT_MAX,
282  PBoolean extendable = false,
283  unsigned val = 0);
284  PASN_Enumeration(unsigned tag,
285  TagClass tagClass,
286  unsigned nEnums,
287  PBoolean extendable,
288  const PASN_Names * nameSpec,
289  unsigned namesCnt,
290  unsigned val = 0);
291 
292  PASN_Enumeration & operator=(unsigned v) { value = v; return *this; }
293  operator unsigned() const { return value; }
294  unsigned GetValue() const { return value; }
295  void SetValue(unsigned v) { value = v; }
296 
297  unsigned GetMaximum() const { return maxEnumValue; }
298 
299  virtual Comparison Compare(const PObject & obj) const;
300  virtual PObject * Clone() const;
301  virtual void PrintOn(ostream & strm) const;
302 
303  virtual PString GetTypeAsString() const;
304  virtual PINDEX GetDataLength() const;
305  virtual PBoolean Decode(PASN_Stream &);
306  virtual void Encode(PASN_Stream &) const;
307 
308 #ifdef P_INCLUDE_PER
309  PBoolean DecodePER(PPER_Stream & strm);
310  void EncodePER(PPER_Stream & strm) const;
311 #endif
312 
313 #ifdef P_INCLUDE_XER
314  virtual PBoolean DecodeXER(PXER_Stream & strm);
315  virtual void EncodeXER(PXER_Stream & strm) const;
316 #endif
317 
318  PINDEX GetValueByName(PString name) const;
319  protected:
320  unsigned maxEnumValue;
321  unsigned value;
322  const PASN_Names *names;
323  unsigned namesCount;
324 };
325 
326 
329 class PASN_Real : public PASN_Object
330 {
331  PCLASSINFO(PASN_Real, PASN_Object);
332  public:
333  PASN_Real(double val = 0);
334  PASN_Real(unsigned tag, TagClass tagClass, double val = 0);
335 
336  PASN_Real & operator=(double val) { value = val; return *this; }
337  operator double() const { return value; }
338  double GetValue() const { return value; }
339  void SetValue(double v) { value = v; }
340 
341  virtual Comparison Compare(const PObject & obj) const;
342  virtual PObject * Clone() const;
343  virtual void PrintOn(ostream & strm) const;
344 
345  virtual PString GetTypeAsString() const;
346  virtual PINDEX GetDataLength() const;
347  virtual PBoolean Decode(PASN_Stream &);
348  virtual void Encode(PASN_Stream &) const;
349 
350  protected:
351  double value;
352 };
353 
354 
357 class PASN_ObjectId : public PASN_Object
358 {
359  PCLASSINFO(PASN_ObjectId, PASN_Object);
360  public:
361  PASN_ObjectId(const char * dotstr = NULL);
362  PASN_ObjectId(unsigned tag, TagClass tagClass);
363 
364  PASN_ObjectId(const PASN_ObjectId & other);
365  PASN_ObjectId & operator=(const PASN_ObjectId & other);
366 
367  PASN_ObjectId & operator=(const char * dotstr);
368  PASN_ObjectId & operator=(const PString & dotstr);
369  PASN_ObjectId & operator=(const PUnsignedArray & numbers);
370  void SetValue(const PString & dotstr);
371  void SetValue(const PUnsignedArray & numbers) { value = numbers; }
372  void SetValue(const unsigned * numbers, PINDEX size);
373 
374  bool operator==(const char * dotstr) const;
375  bool operator!=(const char * dotstr) const { return !operator==(dotstr); }
376  bool operator==(const PString & dotstr) const { return operator==((const char *)dotstr); }
377  bool operator!=(const PString & dotstr) const { return !operator==((const char *)dotstr); }
378  bool operator==(const PASN_ObjectId & id) const { return value == id.value; }
379 
380  PINDEX GetSize() const { return value.GetSize(); }
381  unsigned operator[](PINDEX idx) const { return value[idx]; }
382  const PUnsignedArray & GetValue() const { return value; }
383  PString AsString() const;
384 
385  virtual Comparison Compare(const PObject & obj) const;
386  virtual PObject * Clone() const;
387  virtual void PrintOn(ostream & strm) const;
388 
389  virtual PString GetTypeAsString() const;
390  virtual PINDEX GetDataLength() const;
391  virtual PBoolean Decode(PASN_Stream &);
392  virtual void Encode(PASN_Stream &) const;
393 
394  PBoolean CommonDecode(PASN_Stream & strm, unsigned dataLen);
395  void CommonEncode(PBYTEArray & eObjId) const;
396 
397  protected:
398  PUnsignedArray value;
399 };
400 
401 
404 class PASN_BitString : public PASN_ConstrainedObject
405 {
406  PCLASSINFO(PASN_BitString, PASN_ConstrainedObject);
407  public:
408  PASN_BitString(unsigned nBits = 0, const BYTE * buf = NULL);
409  PASN_BitString(unsigned tag, TagClass tagClass, unsigned nBits = 0);
410 
411  PASN_BitString(const PASN_BitString & other);
412  PASN_BitString & operator=(const PASN_BitString & other);
413 
414  void SetData(const PBYTEArray & bytes) { SetData(bytes.GetSize()*8, bytes); }
415  void SetData(const BYTE * buf, PINDEX size) { SetData(size*8, buf, size); }
416  void SetData(unsigned nBits, const PBYTEArray & bytes);
417  void SetData(unsigned nBits, const BYTE * buf, PINDEX size = 0);
418 
419  const PBYTEArray & GetData() const { return bitData; }
420  const BYTE * GetDataPointer() const { return bitData; }
421 
422  unsigned GetSize() const { return totalBits; }
423  PBoolean SetSize(unsigned nBits);
424 
425  bool operator[](PINDEX bit) const;
426  void Set(unsigned bit);
427  void Clear(unsigned bit);
428  void Invert(unsigned bit);
429 
430  virtual Comparison Compare(const PObject & obj) const;
431  virtual PObject * Clone() const;
432  virtual void PrintOn(ostream & strm) const;
433 
434  virtual void SetConstraintBounds(ConstraintType type, int lower, unsigned upper);
435  virtual PString GetTypeAsString() const;
436  virtual PINDEX GetDataLength() const;
437  virtual PBoolean Decode(PASN_Stream &);
438  virtual void Encode(PASN_Stream &) const;
439 
440 #ifdef P_INCLUDE_BER
441  PBoolean DecodeBER(PBER_Stream & strm, unsigned len);
442  void EncodeBER(PBER_Stream & strm) const;
443 #endif
444 
445 #ifdef P_INCLUDE_PER
446  PBoolean DecodePER(PPER_Stream & strm);
447  void EncodePER(PPER_Stream & strm) const;
448 #endif
449 
450  PBoolean DecodeSequenceExtensionBitmap(PPER_Stream & strm);
451  void EncodeSequenceExtensionBitmap(PPER_Stream & strm) const;
452 
453  protected:
454  unsigned totalBits;
455  PBYTEArray bitData;
456 };
457 
458 
461 class PASN_OctetString : public PASN_ConstrainedObject
462 {
463  PCLASSINFO(PASN_OctetString, PASN_ConstrainedObject);
464  public:
465  PASN_OctetString(const char * str = NULL, PINDEX size = 0);
466  PASN_OctetString(unsigned tag, TagClass tagClass);
467 
468  PASN_OctetString(const PASN_OctetString & other);
469  PASN_OctetString & operator=(const PASN_OctetString & other);
470 
471  PASN_OctetString & operator=(const char * str);
472  PASN_OctetString & operator=(const PString & str);
473  PASN_OctetString & operator=(const PBYTEArray & arr);
474  void SetValue(const char * str) { operator=(str); }
475  void SetValue(const PString & str) { operator=(str); }
476  void SetValue(const PBYTEArray & arr) { operator=(arr); }
477  void SetValue(const BYTE * data, PINDEX len);
478  const PBYTEArray & GetValue() const { return value; }
479  PBYTEArray & GetWritableValue() { return value; }
480  operator const PBYTEArray &() const { return value; }
481  operator const BYTE *() const { return value; }
482  PString AsString() const;
483  BYTE operator[](PINDEX i) const { return value[i]; }
484  BYTE & operator[](PINDEX i) { return value[i]; }
485  BYTE * GetPointer(PINDEX sz = 0) { return value.GetPointer(sz); }
486  PINDEX GetSize() const { return value.GetSize(); }
487  PBoolean SetSize(PINDEX newSize);
488 
489  virtual Comparison Compare(const PObject & obj) const;
490  virtual PObject * Clone() const;
491  virtual void PrintOn(ostream & strm) const;
492 
493  virtual void SetConstraintBounds(ConstraintType type, int lower, unsigned upper);
494  virtual PString GetTypeAsString() const;
495  virtual PINDEX GetDataLength() const;
496  virtual PBoolean Decode(PASN_Stream &);
497  virtual void Encode(PASN_Stream &) const;
498 
499 #ifdef P_INCLUDE_PER
500  PBoolean DecodePER(PPER_Stream & strm);
501  void EncodePER(PPER_Stream & strm) const;
502 #endif
503 
504  PBoolean DecodeSubType(PASN_Object &) const;
505  void EncodeSubType(const PASN_Object &);
506 
507  protected:
508  PBYTEArray value;
509 };
510 
511 
514 class PASN_ConstrainedString : public PASN_ConstrainedObject
515 {
516  PCLASSINFO(PASN_ConstrainedString, PASN_ConstrainedObject);
517  public:
518  PASN_ConstrainedString & operator=(const char * str);
519  PASN_ConstrainedString & operator=(const PString & str) { return operator=((const char *)str); }
520  operator const PString &() const { return value; }
521  const PString & GetValue() const { return value; }
522  void SetValue(const char * v) { operator=(v); }
523  void SetValue(const PString & v) { operator=(v); }
524  char operator[](PINDEX idx) const { return value[idx]; }
525 
526  void SetCharacterSet(ConstraintType ctype, const char * charSet);
527  void SetCharacterSet(ConstraintType ctype, unsigned firstChar = 0, unsigned lastChar = 255);
528  void SetCharacterSet(const char * charSet, PINDEX size, ConstraintType ctype);
529 
530  virtual Comparison Compare(const PObject & obj) const;
531  virtual void PrintOn(ostream & strm) const;
532 
533  virtual void SetConstraintBounds(ConstraintType type, int lower, unsigned upper);
534  virtual PINDEX GetDataLength() const;
535  virtual PBoolean Decode(PASN_Stream &);
536  virtual void Encode(PASN_Stream &) const;
537 
538 #ifdef P_INCLUDE_BER
539  PBoolean DecodeBER(PBER_Stream & strm, unsigned len);
540  void EncodeBER(PBER_Stream & strm) const;
541 #endif
542 
543 #ifdef P_INCLUDE_PER
544  PBoolean DecodePER(PPER_Stream & strm);
545  void EncodePER(PPER_Stream & strm) const;
546 #endif
547 
548  protected:
549  PASN_ConstrainedString(const char * canonicalSet, PINDEX setSize,
550  unsigned tag, TagClass tagClass);
551 
552  PString value;
553  PCharArray characterSet;
554  const char * canonicalSet;
555  PINDEX canonicalSetSize;
556  unsigned canonicalSetBits;
557  unsigned charSetUnalignedBits;
558  unsigned charSetAlignedBits;
559 };
560 
561 
562 #define DECLARE_STRING_CLASS(name) \
563  class PASN_##name##String : public PASN_ConstrainedString { \
564  PCLASSINFO(PASN_##name##String, PASN_ConstrainedString); \
565  public: \
566  PASN_##name##String(const char * str = NULL); \
567  PASN_##name##String(unsigned tag, TagClass tagClass); \
568  PASN_##name##String & operator=(const char * str); \
569  PASN_##name##String & operator=(const PString & str); \
570  virtual PObject * Clone() const; \
571  virtual PString GetTypeAsString() const; \
572  }
573 
574 DECLARE_STRING_CLASS(Numeric);
575 DECLARE_STRING_CLASS(Printable);
576 DECLARE_STRING_CLASS(Visible);
577 DECLARE_STRING_CLASS(IA5);
578 DECLARE_STRING_CLASS(General);
579 
580 
583 class PASN_BMPString : public PASN_ConstrainedObject
584 {
585  PCLASSINFO(PASN_BMPString, PASN_ConstrainedObject);
586  public:
587  PASN_BMPString(const char * str = NULL);
588  PASN_BMPString(const PWCharArray & wstr);
589  PASN_BMPString(unsigned tag, TagClass tagClass);
590 
591  PASN_BMPString(const PASN_BMPString & other);
592  PASN_BMPString & operator=(const PASN_BMPString & other);
593 
594  PASN_BMPString & operator=(const char * v) { return operator=(PString(v).AsWide()); }
595  PASN_BMPString & operator=(const PString & v) { return operator=(v.AsWide()); }
596  PASN_BMPString & operator=(const PWCharArray & v);
597  operator PString() const { return GetValue(); }
598  operator PWCharArray() const { return value; }
599  PString GetValue() const { return value; }
600  void GetValue(PWCharArray & v) const { v = value; }
601  void SetValue(const char * v) { operator=(PString(v).AsWide()); }
602  void SetValue(const PString & v) { operator=(v.AsWide()); }
603  void SetValue(const PWCharArray & v) { operator=(v); }
604  void SetValue(const PASN_BMPString & v) { operator=(v.value); }
605  void SetValueRaw(const PWCharArray & v) { SetValueRaw(v, v.GetSize()); }
606  void SetValueRaw(const wchar_t * val, PINDEX len);
607 
608  void SetCharacterSet(ConstraintType ctype, const char * charSet);
609  void SetCharacterSet(ConstraintType ctype, const PWCharArray & charSet);
610  void SetCharacterSet(ConstraintType ctype, unsigned firstChar, unsigned lastChar);
611 
612  virtual Comparison Compare(const PObject & obj) const;
613  virtual PObject * Clone() const;
614  virtual void PrintOn(ostream & strm) const;
615 
616  virtual PString GetTypeAsString() const;
617  virtual PINDEX GetDataLength() const;
618  virtual PBoolean Decode(PASN_Stream &);
619  virtual void Encode(PASN_Stream &) const;
620 
621 #ifdef P_INCLUDE_BER
622  PBoolean DecodeBER(PBER_Stream & strm, unsigned len);
623  void EncodeBER(PBER_Stream & strm) const;
624 #endif
625 
626 #ifdef P_INCLUDE_PER
627  PBoolean DecodePER(PPER_Stream & strm);
628  void EncodePER(PPER_Stream & strm) const;
629 #endif
630 
631  protected:
632  void Construct();
633  PBoolean IsLegalCharacter(WORD ch);
634 
635  PWCharArray value;
636  PWCharArray characterSet;
637  wchar_t firstChar, lastChar;
638  unsigned charSetUnalignedBits;
639  unsigned charSetAlignedBits;
640 };
641 
642 
643 class PASN_GeneralisedTime : public PASN_VisibleString
644 {
645  PCLASSINFO(PASN_GeneralisedTime, PASN_VisibleString);
646  public:
647  PASN_GeneralisedTime()
648  : PASN_VisibleString(UniversalGeneralisedTime, UniversalTagClass) { }
649  PASN_GeneralisedTime(const PTime & time)
650  : PASN_VisibleString(UniversalGeneralisedTime, UniversalTagClass) { SetValue(time); }
651  PASN_GeneralisedTime(unsigned theTag, TagClass theTagClass)
652  : PASN_VisibleString(theTag, theTagClass) { }
653 
654  PASN_GeneralisedTime & operator=(const PTime & time);
655  void SetValue(const PTime & time) { operator=(time); }
656  PTime GetValue() const;
657 };
658 
659 
660 class PASN_UniversalTime : public PASN_VisibleString
661 {
662  PCLASSINFO(PASN_UniversalTime, PASN_VisibleString);
663  public:
664  PASN_UniversalTime()
665  : PASN_VisibleString(UniversalUTCTime, UniversalTagClass) { }
666  PASN_UniversalTime(const PTime & time)
667  : PASN_VisibleString(UniversalUTCTime, UniversalTagClass) { SetValue(time); }
668  PASN_UniversalTime(unsigned theTag, TagClass theTagClass)
669  : PASN_VisibleString(theTag, theTagClass) { }
670 
671  PASN_UniversalTime & operator=(const PTime & time);
672  void SetValue(const PTime & time) { operator=(time); }
673  PTime GetValue() const;
674 };
675 
676 
677 class PASN_Sequence;
678 
681 class PASN_Choice : public PASN_Object
682 {
683  PCLASSINFO(PASN_Choice, PASN_Object);
684  public:
685  ~PASN_Choice();
686 
687  virtual void SetTag(unsigned newTag, TagClass tagClass = DefaultTagClass);
688  PString GetTagName() const;
689  PASN_Object & GetObject() const;
690  PBoolean IsValid() const { return choice != NULL; }
691 
692 #if defined(__GNUC__) && __GNUC__ <= 2 && __GNUC_MINOR__ < 9
693 
694  operator PASN_Null &() const;
695  operator PASN_Boolean &() const;
696  operator PASN_Integer &() const;
697  operator PASN_Enumeration &() const;
698  operator PASN_Real &() const;
699  operator PASN_ObjectId &() const;
700  operator PASN_BitString &() const;
701  operator PASN_OctetString &() const;
702  operator PASN_NumericString &() const;
703  operator PASN_PrintableString &() const;
704  operator PASN_VisibleString &() const;
705  operator PASN_IA5String &() const;
706  operator PASN_GeneralString &() const;
707  operator PASN_BMPString &() const;
708  operator PASN_Sequence &() const;
709 
710 #else
711 
712  operator PASN_Null &();
713  operator PASN_Boolean &();
714  operator PASN_Integer &();
715  operator PASN_Enumeration &();
716  operator PASN_Real &();
717  operator PASN_ObjectId &();
718  operator PASN_BitString &();
719  operator PASN_OctetString &();
720  operator PASN_NumericString &();
721  operator PASN_PrintableString &();
722  operator PASN_VisibleString &();
723  operator PASN_IA5String &();
724  operator PASN_GeneralString &();
725  operator PASN_BMPString &();
726  operator PASN_Sequence &();
727 
728  operator const PASN_Null &() const;
729  operator const PASN_Boolean &() const;
730  operator const PASN_Integer &() const;
731  operator const PASN_Enumeration &() const;
732  operator const PASN_Real &() const;
733  operator const PASN_ObjectId &() const;
734  operator const PASN_BitString &() const;
735  operator const PASN_OctetString &() const;
736  operator const PASN_NumericString &() const;
737  operator const PASN_PrintableString &() const;
738  operator const PASN_VisibleString &() const;
739  operator const PASN_IA5String &() const;
740  operator const PASN_GeneralString &() const;
741  operator const PASN_BMPString &() const;
742  operator const PASN_Sequence &() const;
743 
744 #endif
745 
746  virtual PBoolean CreateObject() = 0;
747 
748  virtual Comparison Compare(const PObject & obj) const;
749  virtual void PrintOn(ostream & strm) const;
750 
751  virtual PString GetTypeAsString() const;
752  virtual PINDEX GetDataLength() const;
753  virtual PBoolean IsPrimitive() const;
754  virtual PBoolean Decode(PASN_Stream &);
755  virtual void Encode(PASN_Stream &) const;
756 
757 #ifdef P_INCLUDE_PER
758  virtual PBoolean DecodePER(PPER_Stream &);
759  virtual void EncodePER(PPER_Stream &) const;
760 #endif
761 
762 #ifdef P_INCLUDE_XER
763  PBoolean DecodeXER(PXER_Stream &);
764  void EncodeXER(PXER_Stream &) const;
765 #endif
766 
767  PASN_Choice & operator=(const PASN_Choice & other);
768 
769  PINDEX GetValueByName(PString name) const;
770  protected:
771  PASN_Choice(unsigned nChoices = 0, PBoolean extend = false);
772  PASN_Choice(unsigned tag, TagClass tagClass, unsigned nChoices, PBoolean extend);
773  PASN_Choice(unsigned tag, TagClass tagClass, unsigned nChoices, PBoolean extend, const PASN_Names * nameSpec,unsigned namesCnt);
774 
775  PASN_Choice(const PASN_Choice & other);
776 
777  PBoolean CheckCreate() const;
778 
779  unsigned numChoices;
780  PASN_Object * choice;
781  const PASN_Names *names;
782  unsigned namesCount;
783 };
784 
785 
786 PARRAY(PASN_ObjectArray, PASN_Object);
787 
788 
791 class PASN_Sequence : public PASN_Object
792 {
793  PCLASSINFO(PASN_Sequence, PASN_Object);
794  public:
795  PASN_Sequence(unsigned tag = UniversalSequence,
796  TagClass tagClass = UniversalTagClass,
797  unsigned nOpts = 0, PBoolean extend = false, unsigned nExtend = 0);
798 
799  PASN_Sequence(const PASN_Sequence & other);
800  PASN_Sequence & operator=(const PASN_Sequence & other);
801 
802  PINDEX GetSize() const { return fields.GetSize(); }
803  PBoolean SetSize(PINDEX newSize);
804  PASN_Object & operator[](PINDEX i) const { return fields[i]; }
805 
806  PBoolean HasOptionalField(PINDEX opt) const;
807  void IncludeOptionalField(PINDEX opt);
808  void RemoveOptionalField(PINDEX opt);
809 
810  virtual Comparison Compare(const PObject & obj) const;
811  virtual PObject * Clone() const;
812  virtual void PrintOn(ostream & strm) const;
813 
814  virtual PString GetTypeAsString() const;
815  virtual PINDEX GetDataLength() const;
816  virtual PBoolean IsPrimitive() const;
817  virtual PBoolean Decode(PASN_Stream &);
818  virtual void Encode(PASN_Stream &) const;
819 
820  PBoolean PreambleDecode(PASN_Stream & strm);
821  void PreambleEncode(PASN_Stream & strm) const;
822  PBoolean KnownExtensionDecode(PASN_Stream & strm, PINDEX fld, PASN_Object & field);
823  void KnownExtensionEncode(PASN_Stream & strm, PINDEX fld, const PASN_Object & field) const;
824  PBoolean UnknownExtensionsDecode(PASN_Stream & strm);
825  void UnknownExtensionsEncode(PASN_Stream & strm) const;
826 
827 #ifdef P_INCLUDE_BER
828  PBoolean PreambleDecodeBER(PBER_Stream & strm);
829  void PreambleEncodeBER(PBER_Stream & strm) const;
830  PBoolean KnownExtensionDecodeBER(PBER_Stream & strm, PINDEX fld, PASN_Object & field);
831  void KnownExtensionEncodeBER(PBER_Stream & strm, PINDEX fld, const PASN_Object & field) const;
832  PBoolean UnknownExtensionsDecodeBER(PBER_Stream & strm);
833  void UnknownExtensionsEncodeBER(PBER_Stream & strm) const;
834 #endif
835 
836 #ifdef P_INCLUDE_PER
837  PBoolean PreambleDecodePER(PPER_Stream & strm);
838  void PreambleEncodePER(PPER_Stream & strm) const;
839  PBoolean KnownExtensionDecodePER(PPER_Stream & strm, PINDEX fld, PASN_Object & field);
840  void KnownExtensionEncodePER(PPER_Stream & strm, PINDEX fld, const PASN_Object & field) const;
841  PBoolean UnknownExtensionsDecodePER(PPER_Stream & strm);
842  void UnknownExtensionsEncodePER(PPER_Stream & strm) const;
843 #endif
844 
845 #ifdef P_INCLUDE_XER
846  virtual PBoolean PreambleDecodeXER(PXER_Stream & strm);
847  virtual void PreambleEncodeXER(PXER_Stream & strm) const;
848  virtual PBoolean KnownExtensionDecodeXER(PXER_Stream & strm, PINDEX fld, PASN_Object & field);
849  virtual void KnownExtensionEncodeXER(PXER_Stream & strm, PINDEX fld, const PASN_Object & field) const;
850  virtual PBoolean UnknownExtensionsDecodeXER(PXER_Stream & strm);
851  virtual void UnknownExtensionsEncodeXER(PXER_Stream & strm) const;
852 #endif
853 
854  protected:
855  PBoolean NoExtensionsToDecode(PPER_Stream & strm);
856  PBoolean NoExtensionsToEncode(PPER_Stream & strm);
857 
858  PASN_ObjectArray fields;
859  PASN_BitString optionMap;
860  int knownExtensions;
861  int totalExtensions;
862  PASN_BitString extensionMap;
863  PINDEX endBasicEncoding;
864 };
865 
866 
869 class PASN_Set : public PASN_Sequence
870 {
871  PCLASSINFO(PASN_Set, PASN_Sequence);
872  public:
873  PASN_Set(unsigned tag = UniversalSet,
874  TagClass tagClass = UniversalTagClass,
875  unsigned nOpts = 0, PBoolean extend = false, unsigned nExtend = 0);
876 
877  virtual PObject * Clone() const;
878  virtual PString GetTypeAsString() const;
879 };
880 
881 
884 class PASN_Array : public PASN_ConstrainedObject
885 {
886  PCLASSINFO(PASN_Array, PASN_ConstrainedObject);
887  public:
888  PINDEX GetSize() const { return array.GetSize(); }
889  PBoolean SetSize(PINDEX newSize);
890  PASN_Object & operator[](PINDEX i) const { return array[i]; }
891  void Append(PASN_Object * obj) { array.SetAt(array.GetSize(), obj); }
892  void RemoveAt(PINDEX i) { array.RemoveAt(i); }
893  void RemoveAll() { array.RemoveAll(); }
894 
895  virtual Comparison Compare(const PObject & obj) const;
896  virtual void PrintOn(ostream & strm) const;
897 
898  virtual void SetConstraintBounds(ConstraintType type, int lower, unsigned upper);
899  virtual PString GetTypeAsString() const;
900  virtual PINDEX GetDataLength() const;
901  virtual PBoolean IsPrimitive() const;
902  virtual PBoolean Decode(PASN_Stream &);
903  virtual void Encode(PASN_Stream &) const;
904 
905  virtual PASN_Object * CreateObject() const = 0;
906 
907  PASN_Array & operator=(const PASN_Array & other);
908 
909  protected:
910  PASN_Array(unsigned tag = UniversalSequence,
911  TagClass tagClass = UniversalTagClass);
912 
913  PASN_Array(const PASN_Array & other);
914 
915  PASN_ObjectArray array;
916 };
917 
918 
920 
923 class PASN_Stream : public PBYTEArray
924 {
925  PCLASSINFO(PASN_Stream, PBYTEArray);
926  public:
927  PASN_Stream();
928  PASN_Stream(const PBYTEArray & bytes);
929  PASN_Stream(const BYTE * buf, PINDEX size);
930 
931  void PrintOn(ostream & strm) const;
932 
933  PINDEX GetPosition() const { return byteOffset; }
934  void SetPosition(PINDEX newPos);
935  PBoolean IsAtEnd() { return byteOffset >= GetSize(); }
936  void ResetDecoder();
937  void BeginEncoding();
938  void CompleteEncoding();
939 
940  virtual PBoolean Read(PChannel & chan) = 0;
941  virtual PBoolean Write(PChannel & chan) = 0;
942 
943  virtual PBoolean NullDecode(PASN_Null &) = 0;
944  virtual void NullEncode(const PASN_Null &) = 0;
945  virtual PBoolean BooleanDecode(PASN_Boolean &) = 0;
946  virtual void BooleanEncode(const PASN_Boolean &) = 0;
947  virtual PBoolean IntegerDecode(PASN_Integer &) = 0;
948  virtual void IntegerEncode(const PASN_Integer &) = 0;
949  virtual PBoolean EnumerationDecode(PASN_Enumeration &) = 0;
950  virtual void EnumerationEncode(const PASN_Enumeration &) = 0;
951  virtual PBoolean RealDecode(PASN_Real &) = 0;
952  virtual void RealEncode(const PASN_Real &) = 0;
953  virtual PBoolean ObjectIdDecode(PASN_ObjectId &) = 0;
954  virtual void ObjectIdEncode(const PASN_ObjectId &) = 0;
955  virtual PBoolean BitStringDecode(PASN_BitString &) = 0;
956  virtual void BitStringEncode(const PASN_BitString &) = 0;
957  virtual PBoolean OctetStringDecode(PASN_OctetString &) = 0;
958  virtual void OctetStringEncode(const PASN_OctetString &) = 0;
959  virtual PBoolean ConstrainedStringDecode(PASN_ConstrainedString &) = 0;
960  virtual void ConstrainedStringEncode(const PASN_ConstrainedString &) = 0;
961  virtual PBoolean BMPStringDecode(PASN_BMPString &) = 0;
962  virtual void BMPStringEncode(const PASN_BMPString &) = 0;
963  virtual PBoolean ChoiceDecode(PASN_Choice &) = 0;
964  virtual void ChoiceEncode(const PASN_Choice &) = 0;
965  virtual PBoolean ArrayDecode(PASN_Array &) = 0;
966  virtual void ArrayEncode(const PASN_Array &) = 0;
967  virtual PBoolean SequencePreambleDecode(PASN_Sequence &) = 0;
968  virtual void SequencePreambleEncode(const PASN_Sequence &) = 0;
969  virtual PBoolean SequenceKnownDecode(PASN_Sequence &, PINDEX, PASN_Object &) = 0;
970  virtual void SequenceKnownEncode(const PASN_Sequence &, PINDEX, const PASN_Object &) = 0;
971  virtual PBoolean SequenceUnknownDecode(PASN_Sequence &) = 0;
972  virtual void SequenceUnknownEncode(const PASN_Sequence &) = 0;
973 
974  BYTE ByteDecode();
975  void ByteEncode(unsigned value);
976 
977  unsigned BlockDecode(BYTE * bufptr, unsigned nBytes);
978  void BlockEncode(const BYTE * bufptr, PINDEX nBytes);
979 
980  void ByteAlign();
981 
982  protected:
983  PINDEX byteOffset;
984  unsigned bitOffset;
985 
986  private:
987  void Construct();
988 };
989 
990 #ifdef P_INCLUDE_PER
991 #include "asnper.h"
992 #endif
993 
994 #ifdef P_INCLUDE_BER
995 #include "asnber.h"
996 #endif
997 
998 #ifdef P_INCLUDE_XER
999 #include "asnxer.h"
1000 #endif
1001 
1002 #endif // P_ASN
1003 
1004 #endif // PTLIB_ASNER_H
1005 
1006 
1007 // End Of File ///////////////////////////////////////////////////////////////
Array of characters.
Definition: array.h:552
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:2164
This class defines an absolute time and date.
Definition: ptime.h:49
Array of unsigned characters.
Definition: array.h:605
Abstract class defining I/O channel semantics.
Definition: channel.h:103
virtual PINDEX GetSize() const
Get the current size of the container.
bool PBoolean
Definition: object.h:174
#define PARRAY(cls, T)
Declare an array to a specific type of object.
Definition: array.h:985
The character string class.
Definition: pstring.h:108
virtual void PrintOn(ostream &strm) const
Print the array.
Definition: pxml.h:398
Ultimate parent class for all objects in the class library.
Definition: object.h:2204
void PrintOn(ostream &strm) const