2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2004 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
15 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
16 * All rights reserved.
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.
29 #include <lber_types.h>
34 /* Overview of LBER tag construction
41 * 1 0 = CONTEXT-SPECIFIC
48 * | 5 ... 1 | TAG-NUMBER
51 /* BER classes and mask */
52 #define LBER_CLASS_UNIVERSAL ((ber_tag_t) 0x00U)
53 #define LBER_CLASS_APPLICATION ((ber_tag_t) 0x40U)
54 #define LBER_CLASS_CONTEXT ((ber_tag_t) 0x80U)
55 #define LBER_CLASS_PRIVATE ((ber_tag_t) 0xc0U)
56 #define LBER_CLASS_MASK ((ber_tag_t) 0xc0U)
58 /* BER encoding type and mask */
59 #define LBER_PRIMITIVE ((ber_tag_t) 0x00U)
60 #define LBER_CONSTRUCTED ((ber_tag_t) 0x20U)
61 #define LBER_ENCODING_MASK ((ber_tag_t) 0x20U)
63 #define LBER_BIG_TAG_MASK ((ber_tag_t) 0x1fU)
64 #define LBER_MORE_TAG_MASK ((ber_tag_t) 0x80U)
67 * Note that LBER_ERROR and LBER_DEFAULT are values that can never appear
68 * as valid BER tags, and so it is safe to use them to report errors. In
69 * fact, any tag for which the following is true is invalid:
71 #define LBER_INVALID(t) (((t) & (ber_tag_t) 0x080UL) \
72 && (((t) & (ber_tag_t) ~ 0x0FF))
74 #define LBER_ERROR ((ber_tag_t) -1)
75 #define LBER_DEFAULT ((ber_tag_t) -1)
77 /* general BER types we know about */
78 #define LBER_BOOLEAN ((ber_tag_t) 0x01UL)
79 #define LBER_INTEGER ((ber_tag_t) 0x02UL)
80 #define LBER_BITSTRING ((ber_tag_t) 0x03UL)
81 #define LBER_OCTETSTRING ((ber_tag_t) 0x04UL)
82 #define LBER_NULL ((ber_tag_t) 0x05UL)
83 #define LBER_ENUMERATED ((ber_tag_t) 0x0aUL)
84 #define LBER_SEQUENCE ((ber_tag_t) 0x30UL) /* constructed */
85 #define LBER_SET ((ber_tag_t) 0x31UL) /* constructed */
87 /* LBER BerElement options */
88 #define LBER_USE_DER 0x01
90 /* get/set options for BerElement */
91 #define LBER_OPT_BER_OPTIONS 0x01
92 #define LBER_OPT_BER_DEBUG 0x02
93 #define LBER_OPT_BER_REMAINING_BYTES 0x03
94 #define LBER_OPT_BER_TOTAL_BYTES 0x04
95 #define LBER_OPT_BER_BYTES_TO_WRITE 0x05
96 #define LBER_OPT_BER_MEMCTX 0x06
98 #define LBER_OPT_DEBUG_LEVEL LBER_OPT_BER_DEBUG
99 #define LBER_OPT_REMAINING_BYTES LBER_OPT_BER_REMAINING_BYTES
100 #define LBER_OPT_TOTAL_BYTES LBER_OPT_BER_TOTAL_BYTES
101 #define LBER_OPT_BYTES_TO_WRITE LBER_OPT_BER_BYTES_TO_WRITE
103 #define LBER_OPT_LOG_PRINT_FN 0x8001
104 #define LBER_OPT_MEMORY_FNS 0x8002
105 #define LBER_OPT_ERROR_FN 0x8003
106 #define LBER_OPT_LOG_PRINT_FILE 0x8004
108 /* get/set Memory Debug options */
109 #define LBER_OPT_MEMORY_INUSE 0x8005 /* for memory debugging */
110 #define LBER_OPT_LOG_PROC 0x8006 /* for external logging function */
112 typedef int* (*BER_ERRNO_FN) LDAP_P(( void ));
114 typedef void (*BER_LOG_PRINT_FN) LDAP_P(( LDAP_CONST char *buf ));
116 typedef void* (BER_MEMALLOC_FN) LDAP_P(( ber_len_t size, void *ctx ));
117 typedef void* (BER_MEMCALLOC_FN) LDAP_P(( ber_len_t n, ber_len_t size, void *ctx ));
118 typedef void* (BER_MEMREALLOC_FN) LDAP_P(( void *p, ber_len_t size, void *ctx ));
119 typedef void (BER_MEMFREE_FN) LDAP_P(( void *p, void *ctx ));
121 typedef struct lber_memory_fns {
122 BER_MEMALLOC_FN *bmf_malloc;
123 BER_MEMCALLOC_FN *bmf_calloc;
124 BER_MEMREALLOC_FN *bmf_realloc;
125 BER_MEMFREE_FN *bmf_free;
126 } BerMemoryFunctions;
128 /* LBER Sockbuf_IO options */
129 #define LBER_SB_OPT_GET_FD 1
130 #define LBER_SB_OPT_SET_FD 2
131 #define LBER_SB_OPT_HAS_IO 3
132 #define LBER_SB_OPT_SET_NONBLOCK 4
133 #define LBER_SB_OPT_GET_SSL 7
134 #define LBER_SB_OPT_DATA_READY 8
135 #define LBER_SB_OPT_SET_READAHEAD 9
136 #define LBER_SB_OPT_DRAIN 10
137 #define LBER_SB_OPT_NEEDS_READ 11
138 #define LBER_SB_OPT_NEEDS_WRITE 12
139 #define LBER_SB_OPT_GET_MAX_INCOMING 13
140 #define LBER_SB_OPT_SET_MAX_INCOMING 14
141 /* Largest option used by the library */
142 #define LBER_SB_OPT_OPT_MAX 14
144 /* LBER IO operations stacking levels */
145 #define LBER_SBIOD_LEVEL_PROVIDER 10
146 #define LBER_SBIOD_LEVEL_TRANSPORT 20
147 #define LBER_SBIOD_LEVEL_APPLICATION 30
149 /* get/set options for Sockbuf */
150 #define LBER_OPT_SOCKBUF_DESC 0x1000
151 #define LBER_OPT_SOCKBUF_OPTIONS 0x1001
152 #define LBER_OPT_SOCKBUF_DEBUG 0x1002
155 LBER_V( char ) ber_pvt_opt_on;
156 #define LBER_OPT_ON ((void *) &ber_pvt_opt_on)
157 #define LBER_OPT_OFF ((void *) 0)
159 #define LBER_OPT_SUCCESS (0)
160 #define LBER_OPT_ERROR (-1)
162 typedef struct berelement BerElement;
163 typedef struct sockbuf Sockbuf;
164 typedef struct seqorset Seqorset;
166 typedef struct sockbuf_io Sockbuf_IO;
168 /* Structure for LBER IO operarion descriptor */
169 typedef struct sockbuf_io_desc {
172 Sockbuf_IO *sbiod_io;
174 struct sockbuf_io_desc *sbiod_next;
177 /* Structure for LBER IO operation functions */
179 int (*sbi_setup)( Sockbuf_IO_Desc *sbiod, void *arg );
180 int (*sbi_remove)( Sockbuf_IO_Desc *sbiod );
181 int (*sbi_ctrl)( Sockbuf_IO_Desc *sbiod, int opt, void *arg);
183 ber_slen_t (*sbi_read)( Sockbuf_IO_Desc *sbiod, void *buf,
185 ber_slen_t (*sbi_write)( Sockbuf_IO_Desc *sbiod, void *buf,
188 int (*sbi_close)( Sockbuf_IO_Desc *sbiod );
191 /* Helper macros for LBER IO functions */
192 #define LBER_SBIOD_READ_NEXT( sbiod, buf, len ) \
193 ( (sbiod)->sbiod_next->sbiod_io->sbi_read( (sbiod)->sbiod_next, \
195 #define LBER_SBIOD_WRITE_NEXT( sbiod, buf, len ) \
196 ( (sbiod)->sbiod_next->sbiod_io->sbi_write( (sbiod)->sbiod_next, \
198 #define LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg ) \
199 ( (sbiod)->sbiod_next ? \
200 ( (sbiod)->sbiod_next->sbiod_io->sbi_ctrl( \
201 (sbiod)->sbiod_next, opt, arg ) ) : 0 )
203 /* structure for returning a sequence of octet strings + length */
204 typedef struct berval {
209 typedef BerValue *BerVarray; /* To distinguish from a single bv */
211 /* this should be moved to lber-int.h */
217 ber_error_print LDAP_P((
218 LDAP_CONST char *data ));
222 LDAP_CONST char *data, ber_len_t len ));
226 BerElement *ber, int inout ));
229 ber_sos_dump LDAP_P((
235 typedef int (*BERDecodeCallback) LDAP_P((
245 ber_skip_tag LDAP_P((
250 ber_peek_tag LDAP_P((
260 ber_get_enum LDAP_P((
265 ber_get_stringb LDAP_P((
271 ber_get_stringbv LDAP_P((
277 ber_get_stringa LDAP_P((
282 ber_get_stringal LDAP_P((
284 struct berval **bv ));
287 ber_get_bitstringa LDAP_P((
293 ber_get_null LDAP_P((
297 ber_get_boolean LDAP_P((
299 ber_int_t *boolval ));
302 ber_first_element LDAP_P((
308 ber_next_element LDAP_P((
311 LDAP_CONST char *last ));
316 LDAP_CONST char *fmt,
322 typedef int (*BEREncodeCallback) LDAP_P((
327 ber_put_enum LDAP_P((
339 ber_put_ostring LDAP_P((
341 LDAP_CONST char *str,
346 ber_put_berval LDAP_P((
352 ber_put_string LDAP_P((
354 LDAP_CONST char *str,
358 ber_put_bitstring LDAP_P((
360 LDAP_CONST char *str,
365 ber_put_null LDAP_P((
370 ber_put_boolean LDAP_P((
376 ber_start_seq LDAP_P((
381 ber_start_set LDAP_P((
396 LDAP_CONST char *fmt,
413 LDAP_CONST char *buf,
423 ber_free_buf LDAP_P(( BerElement *ber ));
431 LBER_F( BerElement * )
432 ber_alloc LDAP_P(( void )); /* DEPRECATED */
434 LBER_F( BerElement * )
435 der_alloc LDAP_P(( void )); /* DEPRECATED */
437 LBER_F( BerElement * )
441 LBER_F( BerElement * )
446 ber_get_next LDAP_P((
458 ber_init_w_nullc LDAP_P(( /* DEPRECATED */
467 LBER_F( BerElement * )
469 struct berval *bv ));
474 struct berval **bvPtr ));
477 ber_flatten2 LDAP_P((
483 ber_remaining LDAP_P((
487 * LBER ber accessor functions
491 ber_get_option LDAP_P((
497 ber_set_option LDAP_P((
500 LDAP_CONST void *invalue));
507 ber_sockbuf_alloc LDAP_P((
511 ber_sockbuf_free LDAP_P((
515 ber_sockbuf_add_io LDAP_P((
522 ber_sockbuf_remove_io LDAP_P((
528 ber_sockbuf_ctrl LDAP_P((
533 LBER_V( Sockbuf_IO ) ber_sockbuf_io_tcp;
534 LBER_V( Sockbuf_IO ) ber_sockbuf_io_readahead;
535 LBER_V( Sockbuf_IO ) ber_sockbuf_io_fd;
536 LBER_V( Sockbuf_IO ) ber_sockbuf_io_debug;
537 LBER_V( Sockbuf_IO ) ber_sockbuf_io_udp;
543 ber_memalloc LDAP_P((
547 ber_memrealloc LDAP_P((
552 ber_memcalloc LDAP_P((
561 ber_memvfree LDAP_P((
566 struct berval *bv ));
569 ber_bvecfree LDAP_P((
570 struct berval **bv ));
574 struct berval ***bvec,
575 struct berval *bv ));
577 LBER_F( struct berval * )
579 struct berval *dst, struct berval *src ));
581 LBER_F( struct berval * )
583 struct berval *src ));
585 LBER_F( struct berval * )
587 LDAP_CONST char *, ber_len_t len, int dup, struct berval *bv));
589 LBER_F( struct berval * )
591 LDAP_CONST char *, ber_len_t len, int dup, struct berval *bv));
593 #define ber_bvstr(a) ((ber_str2bv)((a), 0, 0, NULL))
594 #define ber_bvstrdup(a) ((ber_str2bv)((a), 0, 1, NULL))
598 LDAP_CONST char * ));
601 ber_bvarray_free LDAP_P(( BerVarray p ));
604 ber_bvarray_add LDAP_P(( BerVarray *p, BerValue *bv ));
606 #define ber_bvcmp(v1,v2) \
607 ((v1)->bv_len < (v2)->bv_len \
608 ? -1 : ((v1)->bv_len > (v2)->bv_len \
609 ? 1 : memcmp((v1)->bv_val, (v2)->bv_val, (v1)->bv_len) ))
614 LBER_F( int * ) ber_errno_addr LDAP_P((void));
615 #define ber_errno (*(ber_errno_addr)())
617 #define LBER_ERROR_NONE 0
618 #define LBER_ERROR_PARAM 0x1
619 #define LBER_ERROR_MEMORY 0x2