xref: /netbsd-src/external/bsd/openldap/dist/libraries/liblber/lber-int.h (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: lber-int.h,v 1.1.1.4 2014/05/28 09:58:41 tron Exp $	*/
2 
3 /* $OpenLDAP$ */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5  *
6  * Copyright 1998-2014 The OpenLDAP Foundation.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
18  * All rights reserved.
19  *
20  * Redistribution and use in source and binary forms are permitted
21  * provided that this notice is preserved and that due credit is given
22  * to the University of Michigan at Ann Arbor. The name of the University
23  * may not be used to endorse or promote products derived from this
24  * software without specific prior written permission. This software
25  * is provided ``as is'' without express or implied warranty.
26  */
27 
28 #ifndef _LBER_INT_H
29 #define _LBER_INT_H
30 
31 #include "lber.h"
32 #include "ldap_log.h"
33 #include "lber_pvt.h"
34 #include "ldap_queue.h"
35 
36 LDAP_BEGIN_DECL
37 
38 typedef void (*BER_LOG_FN)(FILE *file,
39 	const char *subsys, int level, const char *fmt, ... );
40 
41 LBER_V (BER_ERRNO_FN) ber_int_errno_fn;
42 
43 #ifdef LDAP_MEMORY_TRACE
44 # ifndef LDAP_MEMORY_DEBUG
45 #  define LDAP_MEMORY_DEBUG 1
46 # endif
47 #endif
48 
49 #ifdef LDAP_MEMORY_DEBUG
50 LBER_V (long)	ber_int_meminuse;
51 #endif
52 #if defined(LDAP_MEMORY_DEBUG) && ((LDAP_MEMORY_DEBUG +0) & 2)
53 # define LDAP_MEMORY_DEBUG_ASSERT assert
54 #else
55 # define LDAP_MEMORY_DEBUG_ASSERT(expr) ((void) 0)
56 #endif
57 
58 struct lber_options {
59 	short lbo_valid;
60 	unsigned short		lbo_options;
61 	int			lbo_debug;
62 };
63 
64 LBER_F( int ) ber_pvt_log_output(
65 	const char *subsystem,
66 	int level,
67 	const char *fmt, ... );
68 
69 #define LBER_UNINITIALIZED		0x0
70 #define LBER_INITIALIZED		0x1
71 #define LBER_VALID_BERELEMENT	0x2
72 #define LBER_VALID_SOCKBUF		0x3
73 
74 LBER_V (struct lber_options) ber_int_options;
75 #define ber_int_debug ber_int_options.lbo_debug
76 
77 /* Data encoded in ASN.1 BER format */
78 struct berelement {
79 	struct		lber_options ber_opts;
80 #define ber_valid		ber_opts.lbo_valid
81 #define ber_options		ber_opts.lbo_options
82 #define ber_debug		ber_opts.lbo_debug
83 
84 	/*
85 	 * The members below, when not NULL/LBER_DEFAULT/etc, are:
86 	 *   ber_buf       Data buffer.  Other pointers normally point into it.
87 	 *   ber_rwptr     Read/write cursor for Sockbuf I/O.
88 	 *   ber_memctx    Context passed to ber_memalloc() & co.
89 	 * When decoding data (reading it from the BerElement):
90 	 *   ber_end       End of BER data.
91 	 *   ber_ptr       Read cursor, except for 1st octet of tags.
92 	 *   ber_tag       1st octet of next tag, saved from *ber_ptr when
93 	 *                 ber_ptr may be pointing at a tag and is >ber_buf.
94 	 *                 The octet *ber_ptr itself may get overwritten with
95 	 *                 a \0, to terminate the preceding element.
96 	 * When encoding data (writing it to the BerElement):
97 	 *   ber_end       End of allocated buffer - 1 (allowing a final \0).
98 	 *   ber_ptr       Last complete BER element (normally write cursor).
99 	 *   ber_sos_ptr   NULL or write cursor for incomplete sequence or set.
100 	 *   ber_sos_inner offset(seq/set length octets) if ber_sos_ptr!=NULL.
101 	 *   ber_tag       Default tag for next ber_printf() element.
102 	 *   ber_usertag   Boolean set by ber_printf "!" if it sets ber_tag.
103 	 *   ber_len       Reused for ber_sos_inner.
104 	 * When output to a Sockbuf:
105 	 *   ber_ptr       End of encoded data to write.
106 	 * When input from a Sockbuf:
107 	 *   See ber_get_next().
108 	 */
109 
110 	/* Do not change the order of these 3 fields! see ber_get_next */
111 	ber_tag_t	ber_tag;
112 	ber_len_t	ber_len;
113 	ber_tag_t	ber_usertag;
114 
115 	char		*ber_buf;
116 	char		*ber_ptr;
117 	char		*ber_end;
118 
119 	char		*ber_sos_ptr;
120 #	define		ber_sos_inner	ber_len /* reused for binary compat */
121 
122 	char		*ber_rwptr;
123 	void		*ber_memctx;
124 };
125 #define LBER_VALID(ber)	((ber)->ber_valid==LBER_VALID_BERELEMENT)
126 
127 #define ber_pvt_ber_remaining(ber)	((ber)->ber_end - (ber)->ber_ptr)
128 #define ber_pvt_ber_total(ber)		((ber)->ber_end - (ber)->ber_buf)
129 #define ber_pvt_ber_write(ber)		((ber)->ber_ptr - (ber)->ber_buf)
130 
131 struct sockbuf {
132 	struct lber_options sb_opts;
133 	Sockbuf_IO_Desc		*sb_iod;		/* I/O functions */
134 #define	sb_valid		sb_opts.lbo_valid
135 #define	sb_options		sb_opts.lbo_options
136 #define	sb_debug		sb_opts.lbo_debug
137 	ber_socket_t		sb_fd;
138 	ber_len_t			sb_max_incoming;
139    	unsigned int		sb_trans_needs_read:1;
140    	unsigned int		sb_trans_needs_write:1;
141 #ifdef LDAP_PF_LOCAL_SENDMSG
142 	char				sb_ungetlen;
143 	char				sb_ungetbuf[8];
144 #endif
145 };
146 
147 #define SOCKBUF_VALID( sb )	( (sb)->sb_valid == LBER_VALID_SOCKBUF )
148 
149 
150 /*
151  * decode.c, encode.c
152  */
153 
154 /* Simplest OID max-DER-component to implement in both decode and encode */
155 #define LBER_OID_COMPONENT_MAX ((unsigned long)-1 - 128)
156 
157 
158 /*
159  * io.c
160  */
161 LBER_F( int )
162 ber_realloc LDAP_P((
163 	BerElement *ber,
164 	ber_len_t len ));
165 
166 LBER_F (char *) ber_start LDAP_P(( BerElement * ));
167 LBER_F (int) ber_len LDAP_P(( BerElement * ));
168 LBER_F (int) ber_ptrlen LDAP_P(( BerElement * ));
169 LBER_F (void) ber_rewind LDAP_P(( BerElement * ));
170 
171 /*
172  * bprint.c
173  */
174 #define ber_log_printf ber_pvt_log_printf
175 
176 LBER_F( int )
177 ber_log_bprint LDAP_P((
178 	int errlvl,
179 	int loglvl,
180 	const char *data,
181 	ber_len_t len ));
182 
183 LBER_F( int )
184 ber_log_dump LDAP_P((
185 	int errlvl,
186 	int loglvl,
187 	BerElement *ber,
188 	int inout ));
189 
190 LBER_V (BER_LOG_FN) ber_int_log_proc;
191 LBER_V (FILE *) ber_pvt_err_file;
192 
193 /* memory.c */
194 	/* simple macros to realloc for now */
195 LBER_V (BerMemoryFunctions *)	ber_int_memory_fns;
196 LBER_F (char *)	ber_strndup( LDAP_CONST char *, ber_len_t );
197 LBER_F (char *)	ber_strndup_x( LDAP_CONST char *, ber_len_t, void *ctx );
198 
199 #define LBER_MALLOC(s)		ber_memalloc((s))
200 #define LBER_CALLOC(n,s)	ber_memcalloc((n),(s))
201 #define LBER_REALLOC(p,s)	ber_memrealloc((p),(s))
202 #define LBER_FREE(p)		ber_memfree((p))
203 #define LBER_VFREE(v)		ber_memvfree((void**)(v))
204 #define LBER_STRDUP(s)		ber_strdup((s))
205 #define LBER_STRNDUP(s,l)	ber_strndup((s),(l))
206 
207 /* sockbuf.c */
208 
209 LBER_F(	int )
210 ber_int_sb_init LDAP_P(( Sockbuf *sb ));
211 
212 LBER_F( int )
213 ber_int_sb_close LDAP_P(( Sockbuf *sb ));
214 
215 LBER_F(	int )
216 ber_int_sb_destroy LDAP_P(( Sockbuf *sb ));
217 
218 LBER_F( ber_slen_t )
219 ber_int_sb_read LDAP_P(( Sockbuf *sb, void *buf, ber_len_t len ));
220 
221 LBER_F( ber_slen_t )
222 ber_int_sb_write LDAP_P(( Sockbuf *sb, void *buf, ber_len_t len ));
223 
224 LDAP_END_DECL
225 
226 #endif /* _LBER_INT_H */
227