"Fossies" - the Fresh Open Source Software Archive 
Member "npadmin-0.14/ber.h" (19 Apr 2001, 5598 Bytes) of package /linux/misc/old/npadmin-0.14.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 /* Copyright (c) 2000 Ben Woodard
2 * All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public Licence
6 * as published by the Free Software Foundation; either version 2
7 * of the Licence, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRENTY; without even the implied warrenty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public Licence in the COPYING file for more
13 * details.
14 *
15 * You should have received a copy of the GNU Library General
16 * Public License along with the GNU C Library; see the file
17 * COPYING.LIB. If not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21 #ifndef __BER_H__
22 #define __BER_H__
23
24 #include <stdlib.h>
25
26 #include "compat.h"
27
28
29 enum Tags {INT_TAG=0x02,STRING_TAG=0x04,NULL_TAG=0x05,OID_TAG=0x06,
30 CONSTRUCTOR_TAG=0x20,SEQUENCE_TAG=0x30,IPADDR_TAG=0x40,
31 COUNTER_TAG=0x41,TIME_TICK_TAG=0x43,GET_REQ_TAG=0xa0,
32 GET_NEXT_TAG=0xa1,GET_RESP_TAG=0xa2,SET_REQ_TAG=0xa3};
33
34 class BerEncodedData {
35 unsigned char *data;
36 unsigned long length; // this is the length of the data as it appears in the
37 // packet not just the length of the data part of the
38 // packet. In other words this includes the header
39 // length.
40 public:
41 BerEncodedData *next;
42
43 BerEncodedData(unsigned char *dat, unsigned long len);
44 ~BerEncodedData(){delete data;}
45 inline unsigned char *Data(){return data;}
46 inline void Data(unsigned char* dat){data=dat;}
47 inline unsigned int Length(){return length;}
48 };
49
50 class BerBase {
51 protected:
52 // used for decoding the wire format
53 BerBase(unsigned char* data);
54
55 public:
56 // head of the linked list that holds private copy of the data
57 BerEncodedData *edatacache;
58
59 // simple linkage field
60 BerBase *next;
61
62 inline BerBase():edatacache(NULL),next(NULL){}
63 virtual ~BerBase(){if(edatacache!=NULL) delete edatacache;}
64
65 // returns a char* of malloced data that the application can use
66 virtual BerEncodedData *encode();
67
68 /* returns the head to the list of the data still owned by
69 datastructure. */
70 virtual BerEncodedData *getdata();
71
72 /* return the full length basically to overcome the problems
73 with seqences where their edatacache->Length() is just the
74 length of the header not the length of the whole sequence. */
75 unsigned long fulllen();
76
77 // makes an ascii representation of the string and then puts it in buf
78 // then returns the lenth it used up. -1 if it won't fit.
79 virtual int print(char *buf, unsigned int len)=0;
80
81 Tags type(){ return (Tags) *(edatacache->Data()); }
82 };
83
84 class BerNull: public BerBase{
85 public:
86 BerNull(unsigned char *);
87 inline BerNull(){}
88 virtual ~BerNull(){}
89 virtual BerEncodedData *encode();
90 virtual int print(char *buf, unsigned int len);
91 };
92
93 class BerInt: public BerBase {
94 long val;
95 public:
96 BerInt(unsigned char *str);
97 inline BerInt(long valu): val(valu){}
98 virtual ~BerInt(){}
99 virtual BerEncodedData *encode();
100 virtual int print(char *buf, unsigned int len);
101
102 inline long value(){ return val;}
103 };
104
105 class BerTimeTick: public BerBase {
106 unsigned long val;
107 public:
108 BerTimeTick(unsigned char *str);
109 virtual ~BerTimeTick(){}
110 virtual int print(char *buf, unsigned int len);
111 virtual BerEncodedData *encode();
112
113 inline unsigned long value(){ return val;}
114 };
115
116 class BerString: public BerBase {
117 char *str;
118 unsigned int stringlen;
119 public:
120 // this one is for decoding the wire format of the string only.
121 // It is not for a typical initialization of the class.
122 BerString(unsigned char *strng);
123
124 // this one is for normal use
125 BerString(CONST char *strng,unsigned int length);
126 virtual ~BerString(){}
127
128 virtual BerEncodedData *encode();
129 virtual int print(char *buf, unsigned int len);
130 int copy(char *buf, unsigned int len);
131 inline const char *Str(){ return str; } // Warning: no terminator!
132 inline unsigned int strlen(){ return stringlen; }
133 };
134
135 class BerIPAddr: public BerBase {
136 unsigned char *ipaddr;
137 unsigned int len;
138 public:
139 BerIPAddr(unsigned char *strng);
140 BerIPAddr(CONST char *addr,int len);
141 virtual ~BerIPAddr(){}
142 virtual BerEncodedData *encode();
143 virtual int print(char *buf, unsigned int len);
144 inline unsigned char *IPaddr(){return ipaddr;}
145 inline unsigned int Len(){return len;}
146 };
147
148 class BerOid: public BerBase {
149 unsigned char *eoid;
150 unsigned int oidbytes;
151 public:
152 BerOid(unsigned char *str); //decoding wire protocol only
153 BerOid(CONST char *oidstr,unsigned int len);
154 virtual ~BerOid(){}
155
156 int operator==(BerOid & other);
157
158 virtual BerEncodedData *encode();
159 virtual int print(char *buf, unsigned int len);
160 };
161
162 class BerSequence: public BerBase{
163 BerBase *head,*tail;
164 Tags tag;
165 unsigned long seqlen;
166
167 public:
168 BerSequence(unsigned char *str);
169 BerSequence(Tags tag,unsigned int entries ...);
170 virtual ~BerSequence();
171 BerEncodedData *encode();
172 BerEncodedData *getdata();
173
174 // void prepend(unsigned int num ...);
175 void append(unsigned int num ...);
176 BerBase *peek(unsigned int num); // look at the num'd entry in the list
177 BerBase *extract(unsigned int num); // remove the num'd entry in the list
178 virtual int print(char *buf, unsigned int len);
179 };
180
181 unsigned char *start_data(Tags type,unsigned int len, unsigned char &headlen);
182 unsigned long unpack_len(unsigned char *start,unsigned char &headerlen);
183
184 #endif //__BER_H__
185