1
/* $OpenLDAP: pkg/ldap/include/lber.h,v 1.99.2.4 2008/02/11 23:26:40 kurt Exp $ */
2
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4
* Copyright 1998-2008 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
142
/* Only meaningful ifdef LDAP_PF_LOCAL_SENDMSG */
143
#define LBER_SB_OPT_UNGET_BUF 15
145
/* Largest option used by the library */
146
#define LBER_SB_OPT_OPT_MAX 15
148
/* LBER IO operations stacking levels */
149
#define LBER_SBIOD_LEVEL_PROVIDER 10
150
#define LBER_SBIOD_LEVEL_TRANSPORT 20
151
#define LBER_SBIOD_LEVEL_APPLICATION 30
153
/* get/set options for Sockbuf */
154
#define LBER_OPT_SOCKBUF_DESC 0x1000
155
#define LBER_OPT_SOCKBUF_OPTIONS 0x1001
156
#define LBER_OPT_SOCKBUF_DEBUG 0x1002
159
LBER_V( char ) ber_pvt_opt_on;
160
#define LBER_OPT_ON ((void *) &ber_pvt_opt_on)
161
#define LBER_OPT_OFF ((void *) 0)
163
#define LBER_OPT_SUCCESS (0)
164
#define LBER_OPT_ERROR (-1)
166
typedef struct berelement BerElement;
167
typedef struct sockbuf Sockbuf;
168
typedef struct seqorset Seqorset;
170
typedef struct sockbuf_io Sockbuf_IO;
172
/* Structure for LBER IO operarion descriptor */
173
typedef struct sockbuf_io_desc {
176
Sockbuf_IO *sbiod_io;
178
struct sockbuf_io_desc *sbiod_next;
181
/* Structure for LBER IO operation functions */
183
int (*sbi_setup)( Sockbuf_IO_Desc *sbiod, void *arg );
184
int (*sbi_remove)( Sockbuf_IO_Desc *sbiod );
185
int (*sbi_ctrl)( Sockbuf_IO_Desc *sbiod, int opt, void *arg);
187
ber_slen_t (*sbi_read)( Sockbuf_IO_Desc *sbiod, void *buf,
189
ber_slen_t (*sbi_write)( Sockbuf_IO_Desc *sbiod, void *buf,
192
int (*sbi_close)( Sockbuf_IO_Desc *sbiod );
195
/* Helper macros for LBER IO functions */
196
#define LBER_SBIOD_READ_NEXT( sbiod, buf, len ) \
197
( (sbiod)->sbiod_next->sbiod_io->sbi_read( (sbiod)->sbiod_next, \
199
#define LBER_SBIOD_WRITE_NEXT( sbiod, buf, len ) \
200
( (sbiod)->sbiod_next->sbiod_io->sbi_write( (sbiod)->sbiod_next, \
202
#define LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg ) \
203
( (sbiod)->sbiod_next ? \
204
( (sbiod)->sbiod_next->sbiod_io->sbi_ctrl( \
205
(sbiod)->sbiod_next, opt, arg ) ) : 0 )
207
/* structure for returning a sequence of octet strings + length */
208
typedef struct berval {
213
typedef BerValue *BerVarray; /* To distinguish from a single bv */
215
/* this should be moved to lber-int.h */
221
ber_error_print LDAP_P((
222
LDAP_CONST char *data ));
226
LDAP_CONST char *data, ber_len_t len ));
230
BerElement *ber, int inout ));
233
ber_sos_dump LDAP_P((
239
typedef int (*BERDecodeCallback) LDAP_P((
249
ber_skip_tag LDAP_P((
254
ber_peek_tag LDAP_P((
264
ber_get_enum LDAP_P((
269
ber_get_stringb LDAP_P((
274
#define LBER_BV_ALLOC 0x01 /* allocate/copy result, otherwise in-place */
275
#define LBER_BV_NOTERM 0x02 /* omit NUL-terminator if parsing in-place */
278
ber_get_stringbv LDAP_P((
284
ber_get_stringa LDAP_P((
289
ber_get_stringal LDAP_P((
291
struct berval **bv ));
294
ber_get_bitstringa LDAP_P((
300
ber_get_null LDAP_P((
304
ber_get_boolean LDAP_P((
306
ber_int_t *boolval ));
309
ber_first_element LDAP_P((
315
ber_next_element LDAP_P((
318
LDAP_CONST char *last ));
323
LDAP_CONST char *fmt,
327
ber_decode_oid LDAP_P((
329
struct berval *out ));
335
ber_encode_oid LDAP_P((
337
struct berval *out ));
339
typedef int (*BEREncodeCallback) LDAP_P((
344
ber_put_enum LDAP_P((
356
ber_put_ostring LDAP_P((
358
LDAP_CONST char *str,
363
ber_put_berval LDAP_P((
369
ber_put_string LDAP_P((
371
LDAP_CONST char *str,
375
ber_put_bitstring LDAP_P((
377
LDAP_CONST char *str,
382
ber_put_null LDAP_P((
387
ber_put_boolean LDAP_P((
393
ber_start_seq LDAP_P((
398
ber_start_set LDAP_P((
413
LDAP_CONST char *fmt,
422
ber_skip_data LDAP_P((
435
LDAP_CONST char *buf,
445
ber_free_buf LDAP_P(( BerElement *ber ));
452
#define LBER_FLUSH_FREE_NEVER (0x0) /* traditional behavior */
453
#define LBER_FLUSH_FREE_ON_SUCCESS (0x1) /* traditional behavior */
454
#define LBER_FLUSH_FREE_ON_ERROR (0x2)
455
#define LBER_FLUSH_FREE_ALWAYS (LBER_FLUSH_FREE_ON_SUCCESS|LBER_FLUSH_FREE_ON_ERROR)
461
int freeit )); /* DEPRECATED */
463
LBER_F( BerElement * )
464
ber_alloc LDAP_P(( void )); /* DEPRECATED */
466
LBER_F( BerElement * )
467
der_alloc LDAP_P(( void )); /* DEPRECATED */
469
LBER_F( BerElement * )
473
LBER_F( BerElement * )
478
ber_get_next LDAP_P((
490
ber_init_w_nullc LDAP_P(( /* DEPRECATED */
499
LBER_F( BerElement * )
501
struct berval *bv ));
506
struct berval **bvPtr ));
509
ber_flatten2 LDAP_P((
515
ber_remaining LDAP_P((
519
* LBER ber accessor functions
523
ber_get_option LDAP_P((
529
ber_set_option LDAP_P((
532
LDAP_CONST void *invalue));
539
ber_sockbuf_alloc LDAP_P((
543
ber_sockbuf_free LDAP_P((
547
ber_sockbuf_add_io LDAP_P((
554
ber_sockbuf_remove_io LDAP_P((
560
ber_sockbuf_ctrl LDAP_P((
565
LBER_V( Sockbuf_IO ) ber_sockbuf_io_tcp;
566
LBER_V( Sockbuf_IO ) ber_sockbuf_io_readahead;
567
LBER_V( Sockbuf_IO ) ber_sockbuf_io_fd;
568
LBER_V( Sockbuf_IO ) ber_sockbuf_io_debug;
569
LBER_V( Sockbuf_IO ) ber_sockbuf_io_udp;
575
ber_memalloc LDAP_P((
579
ber_memrealloc LDAP_P((
584
ber_memcalloc LDAP_P((
593
ber_memvfree LDAP_P((
598
struct berval *bv ));
601
ber_bvecfree LDAP_P((
602
struct berval **bv ));
606
struct berval ***bvec,
607
struct berval *bv ));
609
LBER_F( struct berval * )
611
struct berval *dst, struct berval *src ));
613
LBER_F( struct berval * )
615
struct berval *src ));
617
LBER_F( struct berval * )
619
LDAP_CONST char *, ber_len_t len, int duplicate, struct berval *bv));
621
LBER_F( struct berval * )
623
LDAP_CONST char *, ber_len_t len, int duplicate, struct berval *bv));
625
#define ber_bvstr(a) ((ber_str2bv)((a), 0, 0, NULL))
626
#define ber_bvstrdup(a) ((ber_str2bv)((a), 0, 1, NULL))
630
LDAP_CONST char * ));
632
LBER_F( struct berval * )
633
ber_bvreplace LDAP_P((
634
struct berval *dst, LDAP_CONST struct berval *src ));
637
ber_bvarray_free LDAP_P(( BerVarray p ));
640
ber_bvarray_add LDAP_P(( BerVarray *p, BerValue *bv ));
642
#define ber_bvcmp(v1,v2) \
643
((v1)->bv_len < (v2)->bv_len \
644
? -1 : ((v1)->bv_len > (v2)->bv_len \
645
? 1 : memcmp((v1)->bv_val, (v2)->bv_val, (v1)->bv_len) ))
650
LBER_F( int * ) ber_errno_addr LDAP_P((void));
651
#define ber_errno (*(ber_errno_addr)())
653
#define LBER_ERROR_NONE 0
654
#define LBER_ERROR_PARAM 0x1
655
#define LBER_ERROR_MEMORY 0x2