xref: /onnv-gate/usr/src/lib/libldap5/sources/ldap/ber/lber-int.h (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 2001-2003 Sun Microsystems, Inc.  All rights reserved.
3*0Sstevel@tonic-gate  * Use is subject to license terms.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate 
7*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
8*0Sstevel@tonic-gate 
9*0Sstevel@tonic-gate 
10*0Sstevel@tonic-gate /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
11*0Sstevel@tonic-gate  *
12*0Sstevel@tonic-gate  * The contents of this file are subject to the Netscape Public License
13*0Sstevel@tonic-gate  * Version 1.0 (the "NPL"); you may not use this file except in
14*0Sstevel@tonic-gate  * compliance with the NPL.  You may obtain a copy of the NPL at
15*0Sstevel@tonic-gate  * http://www.mozilla.org/NPL/
16*0Sstevel@tonic-gate  *
17*0Sstevel@tonic-gate  * Software distributed under the NPL is distributed on an "AS IS" basis,
18*0Sstevel@tonic-gate  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
19*0Sstevel@tonic-gate  * for the specific language governing rights and limitations under the
20*0Sstevel@tonic-gate  * NPL.
21*0Sstevel@tonic-gate  *
22*0Sstevel@tonic-gate  * The Initial Developer of the Original Code is Netscape
23*0Sstevel@tonic-gate  * Communications Corporation. Portions created by Netscape are
24*0Sstevel@tonic-gate  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
25*0Sstevel@tonic-gate  * Rights Reserved.
26*0Sstevel@tonic-gate  */
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate /*
29*0Sstevel@tonic-gate  * Copyright (c) 1990 Regents of the University of Michigan.
30*0Sstevel@tonic-gate  * All rights reserved.
31*0Sstevel@tonic-gate  *
32*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted
33*0Sstevel@tonic-gate  * provided that this notice is preserved and that due credit is given
34*0Sstevel@tonic-gate  * to the University of Michigan at Ann Arbor. The name of the University
35*0Sstevel@tonic-gate  * may not be used to endorse or promote products derived from this
36*0Sstevel@tonic-gate  * software without specific prior written permission. This software
37*0Sstevel@tonic-gate  * is provided ``as is'' without express or implied warranty.
38*0Sstevel@tonic-gate  */
39*0Sstevel@tonic-gate /* lbet-int.h - internal header file for liblber */
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #ifndef _LBERINT_H
42*0Sstevel@tonic-gate #define _LBERINT_H
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate #ifdef __cplusplus
45*0Sstevel@tonic-gate extern "C" {
46*0Sstevel@tonic-gate #endif
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate #include <stdio.h>
49*0Sstevel@tonic-gate #include <ctype.h>
50*0Sstevel@tonic-gate #include <stdarg.h>
51*0Sstevel@tonic-gate #include <stdlib.h>
52*0Sstevel@tonic-gate #ifdef LDAP_SASLIO_HOOKS
53*0Sstevel@tonic-gate #include <sasl/sasl.h>
54*0Sstevel@tonic-gate #endif
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate #ifdef macintosh
57*0Sstevel@tonic-gate # include "ldap-macos.h"
58*0Sstevel@tonic-gate #else /* macintosh */
59*0Sstevel@tonic-gate #if !defined(BSDI)
60*0Sstevel@tonic-gate # include <malloc.h>
61*0Sstevel@tonic-gate #endif
62*0Sstevel@tonic-gate # include <errno.h>
63*0Sstevel@tonic-gate # include <sys/types.h>
64*0Sstevel@tonic-gate #if defined(SUNOS4) || defined(SCOOS)
65*0Sstevel@tonic-gate # include <sys/time.h>
66*0Sstevel@tonic-gate #endif
67*0Sstevel@tonic-gate #if defined( _WINDOWS )
68*0Sstevel@tonic-gate #  define WIN32_LEAN_AND_MEAN
69*0Sstevel@tonic-gate #  include <windows.h>
70*0Sstevel@tonic-gate #  include <time.h>
71*0Sstevel@tonic-gate /* No stderr in a 16-bit Windows DLL */
72*0Sstevel@tonic-gate #  if defined(_WINDLL) && !defined(_WIN32)
73*0Sstevel@tonic-gate #    define USE_DBG_WIN
74*0Sstevel@tonic-gate #  endif
75*0Sstevel@tonic-gate # else
76*0Sstevel@tonic-gate #if !defined(XP_OS2)
77*0Sstevel@tonic-gate /* #  include <sys/varargs.h> */
78*0Sstevel@tonic-gate #  include <sys/socket.h>
79*0Sstevel@tonic-gate #  include <netinet/in.h>
80*0Sstevel@tonic-gate #  include <unistd.h>
81*0Sstevel@tonic-gate #endif
82*0Sstevel@tonic-gate # endif /* defined( _WINDOWS ) */
83*0Sstevel@tonic-gate #endif /* macintosh */
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate #include <memory.h>
86*0Sstevel@tonic-gate #include <string.h>
87*0Sstevel@tonic-gate #include "portable.h"
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate #ifdef _WINDOWS
90*0Sstevel@tonic-gate #include <winsock.h>
91*0Sstevel@tonic-gate #include <io.h>
92*0Sstevel@tonic-gate #endif /* _WINDOWS */
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate #ifdef XP_OS2
95*0Sstevel@tonic-gate #include <os2sock.h>
96*0Sstevel@tonic-gate #include <io.h>
97*0Sstevel@tonic-gate #endif /* XP_OS2 */
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate /* No stderr in a 16-bit Windows DLL */
100*0Sstevel@tonic-gate #if defined(_WINDLL) && !defined(_WIN32)
101*0Sstevel@tonic-gate #define stderr NULL
102*0Sstevel@tonic-gate #endif
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate #include "lber.h"
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate #ifdef _SOLARIS_SDK
107*0Sstevel@tonic-gate #include <libintl.h>
108*0Sstevel@tonic-gate #include "solaris-int.h"
109*0Sstevel@tonic-gate #endif
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate #ifdef macintosh
112*0Sstevel@tonic-gate #define NSLDAPI_LBER_SOCKET_IS_PTR
113*0Sstevel@tonic-gate #endif
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate #define OLD_LBER_SEQUENCE	0x10	/* w/o constructed bit - broken */
116*0Sstevel@tonic-gate #define OLD_LBER_SET		0x11	/* w/o constructed bit - broken */
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate #ifndef _IFP
119*0Sstevel@tonic-gate #define _IFP
120*0Sstevel@tonic-gate typedef int (LDAP_C LDAP_CALLBACK *IFP)();
121*0Sstevel@tonic-gate #endif
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate typedef struct seqorset {
124*0Sstevel@tonic-gate 	ber_len_t	sos_clen;
125*0Sstevel@tonic-gate 	ber_tag_t	sos_tag;
126*0Sstevel@tonic-gate 	char		*sos_first;
127*0Sstevel@tonic-gate 	char		*sos_ptr;
128*0Sstevel@tonic-gate 	struct seqorset	*sos_next;
129*0Sstevel@tonic-gate } Seqorset;
130*0Sstevel@tonic-gate #define NULLSEQORSET	((Seqorset *) 0)
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate #define SOS_STACK_SIZE 8 /* depth of the pre-allocated sos structure stack */
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate struct berelement {
135*0Sstevel@tonic-gate 	char		*ber_buf;
136*0Sstevel@tonic-gate 	char		*ber_ptr;
137*0Sstevel@tonic-gate 	char		*ber_end;
138*0Sstevel@tonic-gate 	struct seqorset	*ber_sos;
139*0Sstevel@tonic-gate 	ber_tag_t	ber_tag;
140*0Sstevel@tonic-gate 	ber_len_t	ber_len;
141*0Sstevel@tonic-gate 	int		ber_usertag;
142*0Sstevel@tonic-gate 	char		ber_options;
143*0Sstevel@tonic-gate 	char		*ber_rwptr;
144*0Sstevel@tonic-gate 	BERTranslateProc ber_encode_translate_proc;
145*0Sstevel@tonic-gate 	BERTranslateProc ber_decode_translate_proc;
146*0Sstevel@tonic-gate 	int		ber_flags;
147*0Sstevel@tonic-gate #define LBER_FLAG_NO_FREE_BUFFER	1	/* don't free ber_buf */
148*0Sstevel@tonic-gate 	int		ber_sos_stack_posn;
149*0Sstevel@tonic-gate 	Seqorset	ber_sos_stack[SOS_STACK_SIZE];
150*0Sstevel@tonic-gate };
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate #ifndef _SOLARIS_SDK
153*0Sstevel@tonic-gate #define NULLBER ((BerElement *)NULL)
154*0Sstevel@tonic-gate #endif
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate #ifdef LDAP_DEBUG
157*0Sstevel@tonic-gate void ber_dump( BerElement *ber, int inout );
158*0Sstevel@tonic-gate #endif
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate /*
163*0Sstevel@tonic-gate  * structure for read/write I/O callback functions.
164*0Sstevel@tonic-gate  */
165*0Sstevel@tonic-gate struct nslberi_io_fns {
166*0Sstevel@tonic-gate     LDAP_IOF_READ_CALLBACK	*lbiof_read;
167*0Sstevel@tonic-gate     LDAP_IOF_WRITE_CALLBACK	*lbiof_write;
168*0Sstevel@tonic-gate };
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate struct sockbuf {
172*0Sstevel@tonic-gate 	LBER_SOCKET	sb_sd;
173*0Sstevel@tonic-gate 	BerElement	sb_ber;
174*0Sstevel@tonic-gate 	int		sb_naddr;	/* > 0 implies using CLDAP (UDP) */
175*0Sstevel@tonic-gate 	void		*sb_useaddr;	/* pointer to sockaddr to use next */
176*0Sstevel@tonic-gate 	void		*sb_fromaddr;	/* pointer to message source sockaddr */
177*0Sstevel@tonic-gate 	void		**sb_addrs;	/* actually an array of pointers to
178*0Sstevel@tonic-gate 					   sockaddrs */
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 	int		sb_options;	/* to support copying ber elements */
181*0Sstevel@tonic-gate 	LBER_SOCKET	sb_copyfd;	/* for LBER_SOCKBUF_OPT_TO_FILE* opts */
182*0Sstevel@tonic-gate 	ber_uint_t	sb_max_incoming;
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate 	struct nslberi_io_fns
185*0Sstevel@tonic-gate 			sb_io_fns;	/* classic I/O callback functions */
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate 	struct lber_x_ext_io_fns
188*0Sstevel@tonic-gate 			sb_ext_io_fns;	/* extended I/O callback functions */
189*0Sstevel@tonic-gate #ifdef LDAP_SASLIO_HOOKS
190*0Sstevel@tonic-gate         sasl_conn_t     *sb_sasl_ctx;   /* pointer to sasl context */
191*0Sstevel@tonic-gate         char            *sb_sasl_ibuf;  /* sasl decrypted input buffer */
192*0Sstevel@tonic-gate         char            *sb_sasl_iptr;  /* current location in buffer */
193*0Sstevel@tonic-gate         int             sb_sasl_bfsz;   /* Alloc'd size of input buffer */
194*0Sstevel@tonic-gate         int             sb_sasl_ilen;   /* remaining length to process */
195*0Sstevel@tonic-gate         struct lber_x_ext_io_fns
196*0Sstevel@tonic-gate                         sb_sasl_fns;    /* sasl redirect copy ext I/O funcs */
197*0Sstevel@tonic-gate         void            *sb_sasl_prld;  /* reverse ld pointer for callbacks */
198*0Sstevel@tonic-gate #endif
199*0Sstevel@tonic-gate };
200*0Sstevel@tonic-gate #define NULLSOCKBUF	((Sockbuf *)NULL)
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate #ifndef NSLBERI_LBER_INT_FRIEND
204*0Sstevel@tonic-gate /*
205*0Sstevel@tonic-gate  * Everything from this point on is excluded if NSLBERI_LBER_INT_FRIEND is
206*0Sstevel@tonic-gate  * defined.  The code under ../libraries/libldap defines this.
207*0Sstevel@tonic-gate  */
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate #define READBUFSIZ	8192
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate /*
212*0Sstevel@tonic-gate  * macros used to check validity of data structures and parameters
213*0Sstevel@tonic-gate  */
214*0Sstevel@tonic-gate #define NSLBERI_VALID_BERELEMENT_POINTER( ber ) \
215*0Sstevel@tonic-gate 	( (ber) != NULLBER )
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate #define NSLBERI_VALID_SOCKBUF_POINTER( sb ) \
218*0Sstevel@tonic-gate 	( (sb) != NULLSOCKBUF )
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate #if defined(_WIN32) && defined(_ALPHA)
222*0Sstevel@tonic-gate #define LBER_HTONL( _l ) \
223*0Sstevel@tonic-gate 	    ((((_l)&0xff)<<24) + (((_l)&0xff00)<<8) + \
224*0Sstevel@tonic-gate 	     (((_l)&0xff0000)>>8) + (((_l)&0xff000000)>>24))
225*0Sstevel@tonic-gate #define LBER_NTOHL(_l) LBER_HTONL(_l)
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate #elif !defined(__alpha) || defined(VMS)
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate #define LBER_HTONL( l )	htonl( l )
230*0Sstevel@tonic-gate #define LBER_NTOHL( l )	ntohl( l )
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate #else /* __alpha */
233*0Sstevel@tonic-gate /*
234*0Sstevel@tonic-gate  * htonl and ntohl on the DEC Alpha under OSF 1 seem to only swap the
235*0Sstevel@tonic-gate  * lower-order 32-bits of a (64-bit) long, so we define correct versions
236*0Sstevel@tonic-gate  * here.
237*0Sstevel@tonic-gate  */
238*0Sstevel@tonic-gate #define LBER_HTONL( l )	(((long)htonl( (l) & 0x00000000FFFFFFFF )) << 32 \
239*0Sstevel@tonic-gate     			| htonl( ( (l) & 0xFFFFFFFF00000000 ) >> 32 ))
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate #define LBER_NTOHL( l )	(((long)ntohl( (l) & 0x00000000FFFFFFFF )) << 32 \
242*0Sstevel@tonic-gate     			| ntohl( ( (l) & 0xFFFFFFFF00000000 ) >> 32 ))
243*0Sstevel@tonic-gate #endif /* __alpha */
244*0Sstevel@tonic-gate 
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate /* function prototypes */
247*0Sstevel@tonic-gate #ifdef LDAP_DEBUG
248*0Sstevel@tonic-gate void lber_bprint( char *data, int len );
249*0Sstevel@tonic-gate #endif
250*0Sstevel@tonic-gate void ber_err_print( char *data );
251*0Sstevel@tonic-gate void *nslberi_malloc( size_t size );
252*0Sstevel@tonic-gate void *nslberi_calloc( size_t nelem, size_t elsize );
253*0Sstevel@tonic-gate void *nslberi_realloc( void *ptr, size_t size );
254*0Sstevel@tonic-gate void nslberi_free( void *ptr );
255*0Sstevel@tonic-gate int nslberi_ber_realloc( BerElement *ber, ber_len_t len );
256*0Sstevel@tonic-gate 
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate 
259*0Sstevel@tonic-gate /* blame: dboreham
260*0Sstevel@tonic-gate  * slapd spends much of its time doing memcpy's for the ber code.
261*0Sstevel@tonic-gate  * Most of these are single-byte, so we special-case those and speed
262*0Sstevel@tonic-gate  * things up considerably.
263*0Sstevel@tonic-gate  */
264*0Sstevel@tonic-gate 
265*0Sstevel@tonic-gate #ifdef sunos4
266*0Sstevel@tonic-gate #define THEMEMCPY( d, s, n )	bcopy( s, d, n )
267*0Sstevel@tonic-gate #else /* sunos4 */
268*0Sstevel@tonic-gate #define THEMEMCPY( d, s, n )	memmove( d, s, n )
269*0Sstevel@tonic-gate #endif /* sunos4 */
270*0Sstevel@tonic-gate 
271*0Sstevel@tonic-gate #ifdef SAFEMEMCPY
272*0Sstevel@tonic-gate #undef SAFEMEMCPY
273*0Sstevel@tonic-gate #define SAFEMEMCPY(d,s,n) if (1 == n) *((char*)d) = *((char*)s); else THEMEMCPY(d,s,n);
274*0Sstevel@tonic-gate #endif
275*0Sstevel@tonic-gate 
276*0Sstevel@tonic-gate /*
277*0Sstevel@tonic-gate  * Memory allocation done in liblber should all go through one of the
278*0Sstevel@tonic-gate  * following macros. This is so we can plug-in alternative memory
279*0Sstevel@tonic-gate  * allocators, etc. as the need arises.
280*0Sstevel@tonic-gate  */
281*0Sstevel@tonic-gate #define NSLBERI_MALLOC( size )		nslberi_malloc( size )
282*0Sstevel@tonic-gate #define NSLBERI_CALLOC( nelem, elsize )	nslberi_calloc( nelem, elsize )
283*0Sstevel@tonic-gate #define NSLBERI_REALLOC( ptr, size )	nslberi_realloc( ptr, size )
284*0Sstevel@tonic-gate #define NSLBERI_FREE( ptr )		nslberi_free( ptr )
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate /* allow the library to access the debug variable */
287*0Sstevel@tonic-gate 
288*0Sstevel@tonic-gate extern int lber_debug;
289*0Sstevel@tonic-gate 
290*0Sstevel@tonic-gate #endif /* !NSLBERI_LBER_INT_FRIEND */
291*0Sstevel@tonic-gate 
292*0Sstevel@tonic-gate 
293*0Sstevel@tonic-gate #ifdef __cplusplus
294*0Sstevel@tonic-gate }
295*0Sstevel@tonic-gate #endif
296*0Sstevel@tonic-gate #endif /* _LBERINT_H */
297