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