1 /*        $NetBSD: lber_pvt.h,v 1.3 2021/08/14 16:14:55 christos Exp $          */
2 
3 /* $OpenLDAP$ */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5  *
6  * Copyright 1998-2021 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 file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 
18 /*
19  * lber_pvt.h - Header for ber_pvt_ functions.
20  * These are meant to be internal to OpenLDAP Software.
21  */
22 
23 #ifndef _LBER_PVT_H
24 #define _LBER_PVT_H 1
25 
26 #include <lber.h>
27 
28 LDAP_BEGIN_DECL
29 
30 /* for allocating aligned buffers (on the stack) */
31 #define LBER_ALIGNED_BUFFER(uname,size) \
32           union uname { \
33                     char buffer[size]; \
34                     /* force alignment */ \
35                     int ialign; \
36                     long lalign; \
37                     float falign; \
38                     double dalign; \
39                     char* palign; \
40           }
41 
42 #define LBER_ELEMENT_SIZEOF (256) /* must be >= sizeof(BerElement) */
43 typedef LBER_ALIGNED_BUFFER(lber_berelement_u,LBER_ELEMENT_SIZEOF)
44           BerElementBuffer;
45 
46 typedef struct sockbuf_buf {
47           ber_len_t           buf_size;
48           ber_len_t           buf_ptr;
49           ber_len_t           buf_end;
50           char                          *buf_base;
51 } Sockbuf_Buf;
52 
53 /*
54  * bprint.c
55  */
56 LBER_V( BER_LOG_PRINT_FN ) ber_pvt_log_print;
57 
58 LBER_F( int )
59 ber_pvt_log_printf LDAP_P((
60           int errlvl,
61           int loglvl,
62           const char *fmt,
63           ... )) LDAP_GCCATTR((format(printf, 3, 4)));
64 
65 /*
66  * sockbuf.c
67  */
68 LBER_F( ber_slen_t )
69 ber_pvt_sb_do_write LDAP_P(( Sockbuf_IO_Desc *sbiod, Sockbuf_Buf *buf_out ));
70 
71 LBER_F( void )
72 ber_pvt_sb_buf_init LDAP_P(( Sockbuf_Buf *buf ));
73 
74 LBER_F( void )
75 ber_pvt_sb_buf_destroy LDAP_P(( Sockbuf_Buf *buf ));
76 
77 LBER_F( int )
78 ber_pvt_sb_grow_buffer LDAP_P(( Sockbuf_Buf *buf, ber_len_t minsize ));
79 
80 LBER_F( ber_len_t )
81 ber_pvt_sb_copy_out LDAP_P(( Sockbuf_Buf *sbb, char *buf, ber_len_t len ));
82 
83 LBER_F( int )
84 ber_pvt_socket_set_nonblock LDAP_P(( ber_socket_t sd, int nb ));
85 
86 /*
87  * memory.c
88  */
89 LBER_F( void * )
90 ber_memalloc_x LDAP_P((
91           ber_len_t s, void *ctx));
92 
93 LBER_F( void * )
94 ber_memrealloc_x LDAP_P((
95           void* p,
96           ber_len_t s, void *ctx ));
97 
98 LBER_F( void * )
99 ber_memcalloc_x LDAP_P((
100           ber_len_t n,
101           ber_len_t s, void *ctx ));
102 
103 LBER_F( void )
104 ber_memfree_x LDAP_P((
105           void* p, void *ctx ));
106 
107 LBER_F( void )
108 ber_memvfree_x LDAP_P((
109           void** vector, void *ctx ));
110 
111 LBER_F( void )
112 ber_bvfree_x LDAP_P((
113           struct berval *bv, void *ctx ));
114 
115 LBER_F( void )
116 ber_bvecfree_x LDAP_P((
117           struct berval **bv, void *ctx ));
118 
119 LBER_F( int )
120 ber_bvecadd_x LDAP_P((
121           struct berval ***bvec,
122           struct berval *bv, void *ctx ));
123 
124 LBER_F( struct berval * )
125 ber_dupbv_x LDAP_P((
126           struct berval *dst, struct berval *src, void *ctx ));
127 
128 LBER_F( struct berval * )
129 ber_str2bv_x LDAP_P((
130           LDAP_CONST char *, ber_len_t len, int dup, struct berval *bv, void *ctx));
131 
132 LBER_F( struct berval * )
133 ber_mem2bv_x LDAP_P((
134           LDAP_CONST char *, ber_len_t len, int dup, struct berval *bv, void *ctx));
135 
136 LBER_F( char * )
137 ber_strdup_x LDAP_P((
138           LDAP_CONST char *, void *ctx ));
139 
140 LBER_F( struct berval * )
141 ber_bvreplace_x LDAP_P((
142           struct berval *dst, LDAP_CONST struct berval *src, void *ctx ));
143 
144 LBER_F( void )
145 ber_bvarray_free_x LDAP_P(( BerVarray p, void *ctx ));
146 
147 LBER_F( int )
148 ber_bvarray_add_x LDAP_P(( BerVarray *p, BerValue *bv, void *ctx ));
149 
150 LBER_F( int )
151 ber_bvarray_dup_x LDAP_P(( BerVarray *dst, BerVarray src, void *ctx ));
152 
153 #if 0
154 #define ber_bvstrcmp(v1,v2) \
155           ((v1)->bv_len < (v2)->bv_len \
156                     ? -1 : ((v1)->bv_len > (v2)->bv_len \
157                               ? 1 : strncmp((v1)->bv_val, (v2)->bv_val, (v1)->bv_len) ))
158 #else
159           /* avoid strncmp() */
160 #define ber_bvstrcmp(v1,v2)   ber_bvcmp((v1),(v2))
161 #endif
162 
163 #define ber_bvstrcasecmp(v1,v2) \
164           ((v1)->bv_len < (v2)->bv_len \
165                     ? -1 : ((v1)->bv_len > (v2)->bv_len \
166                               ? 1 : strncasecmp((v1)->bv_val, (v2)->bv_val, (v1)->bv_len) ))
167 
168 #define ber_bvccmp(v1,c) \
169           ( (v1)->bv_len == 1 && (v1)->bv_val[0] == (c) )
170 
171 #define ber_strccmp(s,c) \
172           ( (s)[0] == (c) && (s)[1] == '\0' )
173 
174 #define ber_bvchr(bv,c) \
175           ((char *) memchr( (bv)->bv_val, (c), (bv)->bv_len ))
176 
177 #define ber_bvrchr(bv,c) \
178           ((char *) lutil_memrchr( (bv)->bv_val, (c), (bv)->bv_len ))
179 
180 #define ber_bvchr_post(dst,bv,c) \
181           do { \
182                     (dst)->bv_val = memchr( (bv)->bv_val, (c), (bv)->bv_len ); \
183                     (dst)->bv_len = (dst)->bv_val ? (bv)->bv_len - ((dst)->bv_val - (bv)->bv_val) : 0; \
184           } while (0)
185 
186 #define ber_bvchr_pre(dst,bv,c) \
187           do { \
188                     (dst)->bv_val = memchr( (bv)->bv_val, (c), (bv)->bv_len ); \
189                     (dst)->bv_len = (dst)->bv_val ? ((dst)->bv_val - (bv)->bv_val) : (bv)->bv_len; \
190                     (dst)->bv_val = (bv)->bv_val; \
191           } while (0)
192 
193 #define ber_bvrchr_post(dst,bv,c) \
194           do { \
195                     (dst)->bv_val = lutil_memrchr( (bv)->bv_val, (c), (bv)->bv_len ); \
196                     (dst)->bv_len = (dst)->bv_val ? (bv)->bv_len - ((dst)->bv_val - (bv)->bv_val) : 0; \
197           } while (0)
198 
199 #define ber_bvrchr_pre(dst,bv,c) \
200           do { \
201                     (dst)->bv_val = lutil_memrchr( (bv)->bv_val, (c), (bv)->bv_len ); \
202                     (dst)->bv_len = (dst)->bv_val ? ((dst)->bv_val - (bv)->bv_val) : (bv)->bv_len; \
203                     (dst)->bv_val = (bv)->bv_val; \
204           } while (0)
205 
206 #define BER_STRLENOF(s)       (sizeof(s)-1)
207 #define BER_BVC(s)            { BER_STRLENOF(s), (char *)(s) }
208 #define BER_BVNULL            { 0L, NULL }
209 #define BER_BVZERO(bv) \
210           do { \
211                     (bv)->bv_len = 0; \
212                     (bv)->bv_val = NULL; \
213           } while (0)
214 #define BER_BVSTR(bv,s)       \
215           do { \
216                     (bv)->bv_len = BER_STRLENOF(s); \
217                     (bv)->bv_val = (s); \
218           } while (0)
219 #define BER_BVISNULL(bv)      ((bv)->bv_val == NULL)
220 #define BER_BVISEMPTY(bv)     ((bv)->bv_len == 0)
221 
222 LDAP_END_DECL
223 
224 #endif
225 
226