xref: /illumos-gate/usr/src/head/lber.h (revision 012e6ce759c490003aed29439cc47d3d73a99ad3)
1 /*
2  * Copyright (c) 2001 by Sun Microsystems, Inc.
3  * All rights reserved.
4  */
5 
6 /*
7  * Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4
8  *
9  * The contents of this file are subject to the Netscape Public License
10  * Version 1.0(the "NPL"); you may not use this file except in
11  * compliance with the NPL.  You may obtain a copy of the NPL at
12  * http:/ /www.mozilla.org/NPL/
13  *
14  * Software distributed under the NPL is distributed on an "AS IS" basis,
15  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
16  * for the specific language governing rights and limitations under the
17  * NPL.
18  *
19  * The Initial Developer of this code under the NPL is Netscape
20  * Communications Corporation.  Portions created by Netscape are
21  * Copyright(C) 1998 Netscape Communications Corporation.  All Rights
22  * Reserved.
23  */
24 
25 #ifndef	_LBER_H
26 #define	_LBER_H
27 
28 #ifdef	__cplusplus
29 extern "C" {
30 #endif
31 
32 #ifndef	_SOLARIS_SDK
33 #define	_SOLARIS_SDK
34 #endif
35 
36 #include <stdlib.h>	/* to pick up size_t typedef */
37 
38 #ifdef	_SOLARIS_SDK
39 #ifdef	sunos4
40 #define	SAFEMEMCPY(d, s, n)   bcopy(s, d, n)
41 #else /* sunos4 */
42 #define	SAFEMEMCPY(d, s, n)   memmove(d, s, n)
43 #endif /* sunos4 */
44 #endif /* _SOLARIS_SDK */
45 /*
46  * Note that LBER_ERROR and LBER_DEFAULT are values that can never appear
47  * as valid BER tags, and so it is safe to use them to report errors.  In
48  * fact, any tag for which the following is true is invalid:
49  *     (( tag & 0x00000080 ) != 0 ) && (( tag & 0xFFFFFF00 ) != 0 )
50  */
51 #define	LBER_ERROR		0xffffffffU
52 #define	LBER_DEFAULT		0xffffffffU
53 #define	LBER_END_OF_SEQORSET	0xfffffffeU
54 /* BER classes and mask */
55 #define	LBER_CLASS_UNIVERSAL    0x00
56 #define	LBER_CLASS_APPLICATION  0x40
57 #define	LBER_CLASS_CONTEXT	0x80
58 #define	LBER_CLASS_PRIVATE	0xc0
59 #define	LBER_CLASS_MASK		0xc0
60 
61 /* BER encoding type and mask */
62 #define	LBER_PRIMITIVE		0x00
63 #define	LBER_CONSTRUCTED	0x20
64 #define	LBER_ENCODING_MASK	0x20
65 
66 #define	LBER_BIG_TAG_MASK	0x1f
67 #define	LBER_MORE_TAG_MASK	0x80
68 
69 /* general BER types we know about */
70 #define	LBER_BOOLEAN		0x01
71 #define	LBER_INTEGER		0x02
72 #define	LBER_BITSTRING		0x03
73 #define	LBER_OCTETSTRING	0x04
74 #define	LBER_NULL		0x05
75 #define	LBER_ENUMERATED		0x0a
76 #define	LBER_SEQUENCE		0x30
77 #define	LBER_SET		0x31
78 
79 
80 typedef unsigned int	ber_len_t;   /* for BER len */
81 typedef unsigned int	ber_tag_t;   /* for BER tags */
82 typedef int		ber_int_t;   /* for BER ints, enums, and Booleans */
83 typedef unsigned int	ber_uint_t; /* unsigned equivalent of ber_int_t */
84 typedef int		ber_slen_t; /* signed equivalent of ber_len_t */
85 
86 typedef struct berval {
87 	ber_len_t	bv_len;
88 	char		*bv_val;
89 } BerValue;
90 
91 typedef struct berelement BerElement;
92 
93 #ifdef	_SOLARIS_SDK
94 #define	NULLBER ((BerElement *)NULL)
95 #endif
96 
97 typedef int (*BERTranslateProc)(char **bufp, ber_uint_t *buflenp,
98 	int free_input);
99 #ifndef	macintosh
100 #if defined(_WINDOWS) || defined(_WIN32) || defined(_CONSOLE)
101 #include <winsock.h> /* for SOCKET */
102 typedef SOCKET LBER_SOCKET;
103 #else
104 typedef int LBER_SOCKET;
105 #endif /* _WINDOWS */
106 #else /* macintosh */
107 typedef void *LBER_SOCKET;
108 #endif /* macintosh */
109 
110 /* calling conventions used by library */
111 #ifndef	LDAP_CALL
112 #if defined(_WINDOWS) || defined(_WIN32)
113 #define	LDAP_C __cdecl
114 #ifndef	_WIN32
115 #define	__stdcall _far _pascal
116 #define	LDAP_CALLBACK _loadds
117 #else
118 #define	LDAP_CALLBACK
119 #endif /* _WIN32 */
120 #define	LDAP_PASCAL __stdcall
121 #define	LDAP_CALL LDAP_PASCAL
122 #else /* _WINDOWS */
123 #define	LDAP_C
124 #define	LDAP_CALLBACK
125 #define	LDAP_PASCAL
126 #define	LDAP_CALL
127 #endif /* _WINDOWS */
128 #endif /* LDAP_CALL */
129 
130 /*
131  * function prototypes for lber library
132  */
133 #ifndef	LDAP_API
134 #if defined(_WINDOWS) || defined(_WIN32)
135 #define	LDAP_API(rt) rt
136 #else /* _WINDOWS */
137 #define	LDAP_API(rt) rt
138 #endif /* _WINDOWS */
139 #endif /* LDAP_API */
140 
141 /*
142  * decode routines
143  */
144 ber_tag_t LDAP_CALL ber_get_tag(BerElement *ber);
145 ber_tag_t LDAP_CALL ber_skip_tag(BerElement *ber,
146 	ber_len_t *len);
147 ber_tag_t LDAP_CALL ber_peek_tag(BerElement *ber,
148 	ber_len_t *len);
149 ber_tag_t LDAP_CALL ber_get_int(BerElement *ber, ber_int_t *num);
150 ber_tag_t LDAP_CALL ber_get_stringb(BerElement *ber, char *buf,
151 	ber_len_t *len);
152 ber_tag_t LDAP_CALL ber_get_stringa(BerElement *ber,
153 	char **buf);
154 ber_tag_t LDAP_CALL ber_get_stringal(BerElement *ber,
155 	struct berval **bv);
156 ber_tag_t ber_get_bitstringa(BerElement *ber,
157 	char **buf, ber_len_t *len);
158 ber_tag_t LDAP_CALL ber_get_null(BerElement *ber);
159 ber_tag_t LDAP_CALL ber_get_boolean(BerElement *ber,
160 	int *boolval);
161 ber_tag_t LDAP_CALL ber_first_element(BerElement *ber,
162 	ber_len_t *len, char **last);
163 ber_tag_t LDAP_CALL ber_next_element(BerElement *ber,
164 	ber_len_t *len, char *last);
165 ber_tag_t LDAP_C ber_scanf(BerElement *ber, const char *fmt,
166 	...);
167 LDAP_API(void) LDAP_CALL ber_bvfree(struct berval *bv);
168 LDAP_API(void) LDAP_CALL ber_bvecfree(struct berval **bv);
169 struct berval *LDAP_CALL ber_bvdup(const struct berval *bv);
170 LDAP_API(void) LDAP_CALL ber_set_string_translators(BerElement *ber,
171 	BERTranslateProc encode_proc, BERTranslateProc decode_proc);
172 LDAP_API(BerElement *) LDAP_CALL ber_init(const struct berval *bv);
173 
174 /*
175  * encoding routines
176  */
177 int LDAP_CALL ber_put_enum(BerElement *ber, ber_int_t num,
178 	ber_tag_t tag);
179 int LDAP_CALL ber_put_int(BerElement *ber, ber_int_t num,
180 	ber_tag_t tag);
181 int LDAP_CALL ber_put_ostring(BerElement *ber, char *str,
182 	ber_len_t len, ber_tag_t tag);
183 int LDAP_CALL ber_put_string(BerElement *ber, char *str,
184 	ber_tag_t tag);
185 int LDAP_CALL ber_put_bitstring(BerElement *ber, char *str,
186 	ber_len_t bitlen, ber_tag_t tag);
187 int LDAP_CALL ber_put_null(BerElement *ber, ber_tag_t tag);
188 int LDAP_CALL ber_put_boolean(BerElement *ber, int boolval,
189 	ber_tag_t tag);
190 int LDAP_CALL ber_start_seq(BerElement *ber, ber_tag_t tag);
191 int LDAP_CALL ber_start_set(BerElement *ber, ber_tag_t tag);
192 int LDAP_CALL ber_put_seq(BerElement *ber);
193 int LDAP_CALL ber_put_set(BerElement *ber);
194 int LDAP_C ber_printf(BerElement *ber, const char *fmt, ...);
195 int LDAP_CALL ber_flatten(BerElement *ber,
196 	struct berval **bvPtr);
197 
198 /*
199  * miscellaneous routines
200  */
201 LDAP_API(void) LDAP_CALL ber_free(BerElement *ber, int freebuf);
202 LDAP_API(BerElement*) LDAP_CALL ber_alloc(void);
203 LDAP_API(BerElement*) LDAP_CALL der_alloc(void);
204 LDAP_API(BerElement*) LDAP_CALL ber_alloc_t(int options);
205 LDAP_API(BerElement*) LDAP_CALL ber_dup(BerElement *ber);
206 ber_int_t LDAP_CALL ber_read(BerElement *ber, char *buf,
207 	ber_len_t len);
208 ber_int_t LDAP_CALL ber_write(BerElement *ber, char *buf,
209 	ber_len_t len, int nosos);
210 LDAP_API(void) LDAP_CALL ber_reset(BerElement *ber, int was_writing);
211 
212 #ifdef	__cplusplus
213 }
214 #endif
215 
216 #endif /* _LBER_H */
217