PTLib  Version 2.14.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
pasn.h
Go to the documentation of this file.
1 /*
2  * pasn.h
3  *
4  * Abstract Syntax Notation 1 classes for support of SNMP only.
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: 21788 $
27  * $Author: rjongbloed $
28  * $Date: 2008-12-12 16:42:13 +1100 (Fri, 12 Dec 2008) $
29  */
30 
31 #ifndef PTLIB_PASN_H
32 #define PTLIB_PASN_H
33 
34 #ifdef P_USE_PRAGMA
35 #pragma interface
36 #endif
37 
38 #include <ptlib/sockets.h>
39 
40 //
41 // define some types used by the ASN classes
42 //
43 typedef PInt32 PASNInt;
44 typedef DWORD PASNUnsigned;
45 typedef DWORD PASNOid;
46 
47 class PASNObject;
48 class PASNSequence;
49 
50 PARRAY(PASNObjectArray, PASNObject);
51 
52 
54 
64 class PASNObject : public PObject
65 {
67 
68  public:
72  enum ASNType {
86  Null,
89  };
90 
94  virtual ASNType GetType() const;
95 
96 
99  int GetChoice() const;
100 
102  virtual PString GetTypeAsString() const;
103 
109  virtual PASNInt GetInteger () const;
110 
116  virtual PASNUnsigned GetUnsigned () const;
117 
121  virtual PString GetString () const;
122 
128  virtual const PASNSequence & GetSequence() const;
129 
135  virtual PIPSocket::Address GetIPAddress () const;
136 
140  virtual void PrintOn(
141  ostream & strm
142  ) const;
143 
145  virtual void Encode(
146  PBYTEArray & buffer
147  );
148 
152  virtual WORD GetEncodedLength();
153 
155  virtual PObject * Clone() const;
156 
158  static void EncodeASNLength (
159  PBYTEArray & buffer,
160  WORD length
161  );
162 
164  static WORD GetASNLengthLength (
165  WORD length
166  );
167 
171  static PBoolean DecodeASNLength (
172  const PBYTEArray & buffer,
173  PINDEX & ptr,
174  WORD & len
175  );
176 
178  static void EncodeASNSequenceStart (
179  PBYTEArray & buffer,
180  BYTE type,
181  WORD length
182  );
183 
185  static WORD GetASNSequenceStartLength (
186  WORD length
187  );
188 
190  static void EncodeASNHeader(
191  PBYTEArray & buffer,
192  PASNObject::ASNType type,
193  WORD length
194  );
195 
197  static WORD GetASNHeaderLength (
198  WORD length
199  );
200 
201  static void EncodeASNInteger (
202  PBYTEArray & buffer,
203  PASNInt data,
204  PASNObject::ASNType type
205  );
206  // Encode an ASN integer value into the specified buffer */
207 
208  static void EncodeASNUnsigned (
209  PBYTEArray & buffer,
210  PASNUnsigned data,
211  PASNObject::ASNType type
212  );
213  // Encode an ASN integer value into the specified buffer */
214 
215  static WORD GetASNIntegerLength (
216  PASNInt data
217  );
218  // Return the length of an encoded ASN integer with the specified value
219 
220  static WORD GetASNUnsignedLength (
221  PASNUnsigned data
222  );
223  // Return the length of an encoded ASN integer with the specified value
224 
225  static PBoolean DecodeASNInteger (
226  const PBYTEArray & buffer,
227  PINDEX & ptr,
228  PASNInt & value,
229  ASNType type = Integer
230  );
231  // Decode an ASN integer value in the specified buffer
232 
233  static PBoolean DecodeASNUnsigned (
234  const PBYTEArray & buffer,
235  PINDEX & ptr,
236  PASNUnsigned & value,
237  ASNType type = TimeTicks
238  );
239  // Decode an ASN integer value in the specified buffer
240 
241  protected:
243  PASNObject();
244 
246  static BYTE ASNTypeToType[ASNTypeMax];
247 
248 };
249 
250 
252 
255 class PASNInteger : public PASNObject
256 {
258  public:
259  PASNInteger(PASNInt val);
260  PASNInteger(const PBYTEArray & buffer, PINDEX & ptr);
261 
262  void PrintOn(ostream & strm) const;
263  void Encode(PBYTEArray & buffer);
264  WORD GetEncodedLength();
265  PObject * Clone() const;
266 
267  PASNInt GetInteger() const;
268  PString GetString () const;
269  ASNType GetType() const;
270  PString GetTypeAsString() const;
271 
272  private:
273  PASNInt value;
274 };
275 
276 
278 
281 class PASNString : public PASNObject
282 {
284  public:
285  PASNString(const PString & str);
286  PASNString(const BYTE * ptr, int len);
287  PASNString(const PBYTEArray & buffer, PASNObject::ASNType = String);
288  PASNString(const PBYTEArray & buffer, PINDEX & ptr, PASNObject::ASNType = String);
289 
290  void PrintOn(ostream & strm) const;
291 
292  void Encode(PBYTEArray & buffer)
293  { Encode(buffer, String); }
294 
295  WORD GetEncodedLength();
296  PObject * Clone() const;
297 
298  PString GetString() const;
299  ASNType GetType() const;
300  PString GetTypeAsString() const;
301 
302  protected:
303  PBoolean Decode(const PBYTEArray & buffer, PINDEX & i, PASNObject::ASNType type);
304  void Encode(PBYTEArray & buffer, PASNObject::ASNType type);
305 
307  WORD valueLen;
308 };
309 
310 
312 
315 class PASNIPAddress : public PASNString
316 {
318  public:
320  : PASNString((const BYTE *)addr.GetPointer(), addr.GetSize()) { }
321 
322  PASNIPAddress(const PString & str);
323 
324  PASNIPAddress(const PBYTEArray & buffer)
325  : PASNString(buffer, IPAddress) { }
326 
327  PASNIPAddress(const PBYTEArray & buffer, PINDEX & ptr)
328  : PASNString(buffer, ptr, IPAddress) { }
329 
331  { return IPAddress; }
332 
333  void Encode(PBYTEArray & buffer)
334  { PASNString::Encode(buffer, IPAddress); }
335 
336  PString GetString() const;
337 
338  PString GetTypeAsString() const;
339 
340  PObject * Clone() const
341  { return PNEW PASNIPAddress(*this); }
342 
344 };
345 
346 
348 
352 {
354  public:
356  { value = val; }
357 
358  PASNUnsignedInteger(const PBYTEArray & buffer, PINDEX & ptr);
359 
360  void PrintOn(ostream & strm) const;
361  WORD GetEncodedLength();
362  PString GetString () const;
363  PASNUnsigned GetUnsigned() const;
364 
365  protected:
367  { value = 0; }
368 
369  PBoolean Decode(const PBYTEArray & buffer, PINDEX & i, PASNObject::ASNType theType);
370  void Encode(PBYTEArray & buffer, PASNObject::ASNType theType);
371 
372  private:
373  PASNUnsigned value;
374 };
375 
376 
378 
382 {
384  public:
386  : PASNUnsignedInteger(val) { }
387 
388  PASNTimeTicks(const PBYTEArray & buffer, PINDEX & ptr)
389  { PASNUnsignedInteger::Decode(buffer, ptr, TimeTicks); }
390 
391  void Encode(PBYTEArray & buffer)
393 
394  PObject * Clone() const
395  { return PNEW PASNTimeTicks(*this); }
396 
398  { return TimeTicks; }
399 
400  PString GetTypeAsString() const;
401 };
402 
403 
405 
409 {
411  public:
413  : PASNUnsignedInteger(val) { }
414 
415  PASNCounter(const PBYTEArray & buffer, PINDEX & ptr)
416  { PASNUnsignedInteger::Decode(buffer, ptr, Counter); }
417 
418  void Encode(PBYTEArray & buffer)
420 
421  PObject * Clone() const
422  { return PNEW PASNCounter(*this); }
423 
425  { return Counter; }
426 
427  PString GetTypeAsString() const;
428 };
429 
430 
432 
436 {
438  public:
440  : PASNUnsignedInteger(val) { }
441 
442  PASNGauge(const PBYTEArray & buffer, PINDEX & ptr)
443  { Decode(buffer, ptr); }
444 
445  PBoolean Decode(const PBYTEArray & buffer, PINDEX & i)
446  { return PASNUnsignedInteger::Decode(buffer, i, Gauge); }
447 
448  void Encode(PBYTEArray & buffer)
449  { PASNUnsignedInteger::Encode(buffer, Gauge); }
450 
451  PObject * Clone() const
452  { return PNEW PASNGauge(*this); }
453 
455  { return Gauge; }
456 
457  PString GetTypeAsString() const;
458 };
459 
460 
461 
463 
466 class PASNObjectID : public PASNObject
467 {
469  public:
470  PASNObjectID(const PString & str);
471  PASNObjectID(PASNOid * val, BYTE theLen);
472  PASNObjectID(const PBYTEArray & buffer);
473  PASNObjectID(const PBYTEArray & buffer, PINDEX & ptr);
474 
475  void PrintOn(ostream & strm) const;
476  void Encode(PBYTEArray & buffer);
477  WORD GetEncodedLength();
478  PObject * Clone() const;
479 
480  ASNType GetType() const;
481  PString GetString () const;
482  PString GetTypeAsString() const;
483 
484  protected:
485  PBoolean Decode(const PBYTEArray & buffer, PINDEX & i);
486 
487  private:
488  PDWORDArray value;
489 };
490 
491 
493 
496 class PASNNull : public PASNObject
497 {
499  public:
500  PASNNull();
501  PASNNull(const PBYTEArray & buffer, PINDEX & ptr);
502 
503  void PrintOn(ostream & strm) const;
504 
505  void Encode(PBYTEArray & buffer);
506  WORD GetEncodedLength();
507 
508  PObject * Clone() const;
509 
510  ASNType GetType() const;
511  PString GetString () const;
512  PString GetTypeAsString() const;
513 };
514 
515 
517 
520 class PASNSequence : public PASNObject
521 {
523  public:
524  PASNSequence();
525  PASNSequence(BYTE selector);
526  PASNSequence(const PBYTEArray & buffer);
527  PASNSequence(const PBYTEArray & buffer, PINDEX & i);
528 
529  void Append(PASNObject * obj);
530  PINDEX GetSize() const;
531  PASNObject & operator [] (PINDEX idx) const;
532  const PASNSequence & GetSequence() const;
533 
534  void AppendInteger (PASNInt value);
535  void AppendString (const PString & str);
536  void AppendObjectID(const PString & str);
537  void AppendObjectID(PASNOid * val, BYTE len);
538 
539  int GetChoice() const;
540 
541 // PASNInt GetInteger (PINDEX idx) const;
542 // PString GetString (PINDEX idx) const;
543 
544  void PrintOn(ostream & strm) const;
545  void Encode(PBYTEArray & buffer);
546  PBoolean Decode(const PBYTEArray & buffer, PINDEX & i);
547  WORD GetEncodedLength();
548  ASNType GetType() const;
549  PString GetTypeAsString() const;
550 
551  PBoolean Encode(PBYTEArray & buffer, PINDEX maxLen) ;
552 
553  private:
554  PASNObjectArray sequence;
555  BYTE type;
556  ASNType asnType;
557  WORD encodedLen;
558  WORD seqLen;
559 };
560 
561 
562 #endif // PTLIB_PASN_H
563 
564 
565 // End Of File ///////////////////////////////////////////////////////////////