1/*	$NetBSD: lber.h,v 1.1.1.3 2010/12/12 15:21:22 adam Exp $	*/
2
3/* OpenLDAP: pkg/ldap/include/lber.h,v 1.99.2.12 2010/04/13 20:22:47 kurt Exp */
4/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 *
6 * Copyright 1998-2010 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/* Portions Copyright (c) 1990 Regents of the University of Michigan.
18 * All rights reserved.
19 *
20 * Redistribution and use in source and binary forms are permitted
21 * provided that this notice is preserved and that due credit is given
22 * to the University of Michigan at Ann Arbor. The name of the University
23 * may not be used to endorse or promote products derived from this
24 * software without specific prior written permission. This software
25 * is provided ``as is'' without express or implied warranty.
26 */
27
28#ifndef _LBER_H
29#define _LBER_H
30
31#include <lber_types.h>
32#include <string.h>
33
34LDAP_BEGIN_DECL
35
36/*
37 * ber_tag_t represents the identifier octets at the beginning of BER
38 * elements.  OpenLDAP treats them as mere big-endian unsigned integers.
39 *
40 * Actually the BER identifier octets look like this:
41 *
42 *	Bits of 1st octet:
43 *	______
44 *	8 7 | CLASS
45 *	0 0 = UNIVERSAL
46 *	0 1 = APPLICATION
47 *	1 0 = CONTEXT-SPECIFIC
48 *	1 1 = PRIVATE
49 *		_____
50 *		| 6 | DATA-TYPE
51 *		  0 = PRIMITIVE
52 *		  1 = CONSTRUCTED
53 *			___________
54 *			| 5 ... 1 | TAG-NUMBER
55 *
56 *  For ASN.1 tag numbers >= 0x1F, TAG-NUMBER above is 0x1F and the next
57 *  BER octets contain the actual ASN.1 tag number:  Big-endian, base
58 *  128, 8.bit = 1 in all but the last octet, minimum number of octets.
59 */
60
61/* BER classes and mask (in 1st identifier octet) */
62#define LBER_CLASS_UNIVERSAL	((ber_tag_t) 0x00U)
63#define LBER_CLASS_APPLICATION	((ber_tag_t) 0x40U)
64#define LBER_CLASS_CONTEXT		((ber_tag_t) 0x80U)
65#define LBER_CLASS_PRIVATE		((ber_tag_t) 0xc0U)
66#define LBER_CLASS_MASK			((ber_tag_t) 0xc0U)
67
68/* BER encoding type and mask (in 1st identifier octet) */
69#define LBER_PRIMITIVE			((ber_tag_t) 0x00U)
70#define LBER_CONSTRUCTED		((ber_tag_t) 0x20U)
71#define LBER_ENCODING_MASK		((ber_tag_t) 0x20U)
72
73#define LBER_BIG_TAG_MASK		((ber_tag_t) 0x1fU)
74#define LBER_MORE_TAG_MASK		((ber_tag_t) 0x80U)
75
76/*
77 * LBER_ERROR and LBER_DEFAULT are values that can never appear
78 * as valid BER tags, so it is safe to use them to report errors.
79 * Valid tags have (tag & (ber_tag_t) 0xFF) != 0xFF.
80 */
81#define LBER_ERROR			((ber_tag_t) -1)
82#define LBER_DEFAULT		((ber_tag_t) -1)
83
84/* general BER types we know about */
85#define LBER_BOOLEAN		((ber_tag_t) 0x01UL)
86#define LBER_INTEGER		((ber_tag_t) 0x02UL)
87#define LBER_BITSTRING		((ber_tag_t) 0x03UL)
88#define LBER_OCTETSTRING	((ber_tag_t) 0x04UL)
89#define LBER_NULL			((ber_tag_t) 0x05UL)
90#define LBER_ENUMERATED		((ber_tag_t) 0x0aUL)
91#define LBER_SEQUENCE		((ber_tag_t) 0x30UL)	/* constructed */
92#define LBER_SET			((ber_tag_t) 0x31UL)	/* constructed */
93
94/* LBER BerElement options */
95#define LBER_USE_DER		0x01
96
97/* get/set options for BerElement */
98#define LBER_OPT_BER_OPTIONS			0x01
99#define LBER_OPT_BER_DEBUG				0x02
100#define LBER_OPT_BER_REMAINING_BYTES	0x03
101#define LBER_OPT_BER_TOTAL_BYTES		0x04
102#define LBER_OPT_BER_BYTES_TO_WRITE		0x05
103#define LBER_OPT_BER_MEMCTX				0x06
104
105#define LBER_OPT_DEBUG_LEVEL	LBER_OPT_BER_DEBUG
106#define LBER_OPT_REMAINING_BYTES	LBER_OPT_BER_REMAINING_BYTES
107#define LBER_OPT_TOTAL_BYTES		LBER_OPT_BER_TOTAL_BYTES
108#define LBER_OPT_BYTES_TO_WRITE		LBER_OPT_BER_BYTES_TO_WRITE
109
110#define LBER_OPT_LOG_PRINT_FN	0x8001
111#define LBER_OPT_MEMORY_FNS		0x8002
112#define LBER_OPT_ERROR_FN		0x8003
113#define LBER_OPT_LOG_PRINT_FILE		0x8004
114
115/* get/set Memory Debug options */
116#define LBER_OPT_MEMORY_INUSE		0x8005	/* for memory debugging */
117#define LBER_OPT_LOG_PROC           0x8006  /* for external logging function */
118
119typedef int* (*BER_ERRNO_FN) LDAP_P(( void ));
120
121typedef void (*BER_LOG_PRINT_FN) LDAP_P(( LDAP_CONST char *buf ));
122
123typedef void* (BER_MEMALLOC_FN)	LDAP_P(( ber_len_t size, void *ctx ));
124typedef void* (BER_MEMCALLOC_FN)	LDAP_P(( ber_len_t n, ber_len_t size, void *ctx ));
125typedef void* (BER_MEMREALLOC_FN)	LDAP_P(( void *p, ber_len_t size, void *ctx ));
126typedef void  (BER_MEMFREE_FN)		LDAP_P(( void *p, void *ctx ));
127
128typedef struct lber_memory_fns {
129	BER_MEMALLOC_FN	*bmf_malloc;
130	BER_MEMCALLOC_FN *bmf_calloc;
131	BER_MEMREALLOC_FN *bmf_realloc;
132	BER_MEMFREE_FN *bmf_free;
133} BerMemoryFunctions;
134
135/* LBER Sockbuf_IO options */
136#define LBER_SB_OPT_GET_FD		1
137#define LBER_SB_OPT_SET_FD		2
138#define LBER_SB_OPT_HAS_IO		3
139#define LBER_SB_OPT_SET_NONBLOCK	4
140#define LBER_SB_OPT_GET_SSL		7
141#define LBER_SB_OPT_DATA_READY		8
142#define LBER_SB_OPT_SET_READAHEAD	9
143#define LBER_SB_OPT_DRAIN		10
144#define LBER_SB_OPT_NEEDS_READ		11
145#define LBER_SB_OPT_NEEDS_WRITE		12
146#define LBER_SB_OPT_GET_MAX_INCOMING	13
147#define LBER_SB_OPT_SET_MAX_INCOMING	14
148
149/* Only meaningful ifdef LDAP_PF_LOCAL_SENDMSG */
150#define LBER_SB_OPT_UNGET_BUF	15
151
152/* Largest option used by the library */
153#define LBER_SB_OPT_OPT_MAX		15
154
155/* LBER IO operations stacking levels */
156#define LBER_SBIOD_LEVEL_PROVIDER	10
157#define LBER_SBIOD_LEVEL_TRANSPORT	20
158#define LBER_SBIOD_LEVEL_APPLICATION	30
159
160/* get/set options for Sockbuf */
161#define LBER_OPT_SOCKBUF_DESC		0x1000
162#define LBER_OPT_SOCKBUF_OPTIONS	0x1001
163#define LBER_OPT_SOCKBUF_DEBUG		0x1002
164
165/* on/off values */
166LBER_V( char ) ber_pvt_opt_on;
167#define LBER_OPT_ON		((void *) &ber_pvt_opt_on)
168#define LBER_OPT_OFF	((void *) 0)
169
170#define LBER_OPT_SUCCESS	(0)
171#define LBER_OPT_ERROR		(-1)
172
173typedef struct berelement BerElement;
174typedef struct sockbuf Sockbuf;
175
176typedef struct sockbuf_io Sockbuf_IO;
177
178/* Structure for LBER IO operarion descriptor */
179typedef struct sockbuf_io_desc {
180	int			sbiod_level;
181	Sockbuf			*sbiod_sb;
182	Sockbuf_IO		*sbiod_io;
183	void 			*sbiod_pvt;
184	struct sockbuf_io_desc	*sbiod_next;
185} Sockbuf_IO_Desc;
186
187/* Structure for LBER IO operation functions */
188struct sockbuf_io {
189	int (*sbi_setup)( Sockbuf_IO_Desc *sbiod, void *arg );
190	int (*sbi_remove)( Sockbuf_IO_Desc *sbiod );
191	int (*sbi_ctrl)( Sockbuf_IO_Desc *sbiod, int opt, void *arg);
192
193	ber_slen_t (*sbi_read)( Sockbuf_IO_Desc *sbiod, void *buf,
194		ber_len_t len );
195	ber_slen_t (*sbi_write)( Sockbuf_IO_Desc *sbiod, void *buf,
196		ber_len_t len );
197
198	int (*sbi_close)( Sockbuf_IO_Desc *sbiod );
199};
200
201/* Helper macros for LBER IO functions */
202#define LBER_SBIOD_READ_NEXT( sbiod, buf, len ) \
203	( (sbiod)->sbiod_next->sbiod_io->sbi_read( (sbiod)->sbiod_next, \
204		buf, len ) )
205#define LBER_SBIOD_WRITE_NEXT( sbiod, buf, len ) \
206	( (sbiod)->sbiod_next->sbiod_io->sbi_write( (sbiod)->sbiod_next, \
207		buf, len ) )
208#define LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg ) \
209	( (sbiod)->sbiod_next ? \
210		( (sbiod)->sbiod_next->sbiod_io->sbi_ctrl( \
211		(sbiod)->sbiod_next, opt, arg ) ) : 0 )
212
213/* structure for returning a sequence of octet strings + length */
214typedef struct berval {
215	ber_len_t	bv_len;
216	char		*bv_val;
217} BerValue;
218
219typedef BerValue *BerVarray;	/* To distinguish from a single bv */
220
221/* this should be moved to lber-int.h */
222
223/*
224 * in bprint.c:
225 */
226LBER_F( void )
227ber_error_print LDAP_P((
228	LDAP_CONST char *data ));
229
230LBER_F( void )
231ber_bprint LDAP_P((
232	LDAP_CONST char *data, ber_len_t len ));
233
234LBER_F( void )
235ber_dump LDAP_P((
236	BerElement *ber, int inout ));
237
238/*
239 * in decode.c:
240 */
241typedef int (*BERDecodeCallback) LDAP_P((
242	BerElement *ber,
243	void *data,
244	int mode ));
245
246LBER_F( ber_tag_t )
247ber_get_tag LDAP_P((
248	BerElement *ber ));
249
250LBER_F( ber_tag_t )
251ber_skip_tag LDAP_P((
252	BerElement *ber,
253	ber_len_t *len ));
254
255LBER_F( ber_tag_t )
256ber_peek_tag LDAP_P((
257	BerElement *ber,
258	ber_len_t *len ));
259
260LBER_F( ber_tag_t )
261ber_skip_element LDAP_P((
262	BerElement *ber,
263	struct berval *bv ));
264
265LBER_F( ber_tag_t )
266ber_peek_element LDAP_P((
267	LDAP_CONST BerElement *ber,
268	struct berval *bv ));
269
270LBER_F( ber_tag_t )
271ber_get_int LDAP_P((
272	BerElement *ber,
273	ber_int_t *num ));
274
275LBER_F( ber_tag_t )
276ber_get_enum LDAP_P((
277	BerElement *ber,
278	ber_int_t *num ));
279
280LBER_F( ber_tag_t )
281ber_get_stringb LDAP_P((
282	BerElement *ber,
283	char *buf,
284	ber_len_t *len ));
285
286#define	LBER_BV_ALLOC	0x01	/* allocate/copy result, otherwise in-place */
287#define	LBER_BV_NOTERM	0x02	/* omit NUL-terminator if parsing in-place */
288#define	LBER_BV_STRING	0x04	/* fail if berval contains embedded \0 */
289/* LBER_BV_STRING currently accepts a terminating \0 in the berval, because
290 * Active Directory sends that in at least the diagonsticMessage field.
291 */
292
293LBER_F( ber_tag_t )
294ber_get_stringbv LDAP_P((
295	BerElement *ber,
296	struct berval *bv,
297	int options ));
298
299LBER_F( ber_tag_t )
300ber_get_stringa LDAP_P((
301	BerElement *ber,
302	char **buf ));
303
304LBER_F( ber_tag_t )
305ber_get_stringal LDAP_P((
306	BerElement *ber,
307	struct berval **bv ));
308
309LBER_F( ber_tag_t )
310ber_get_bitstringa LDAP_P((
311	BerElement *ber,
312	char **buf,
313	ber_len_t *len ));
314
315LBER_F( ber_tag_t )
316ber_get_null LDAP_P((
317	BerElement *ber ));
318
319LBER_F( ber_tag_t )
320ber_get_boolean LDAP_P((
321	BerElement *ber,
322	ber_int_t *boolval ));
323
324LBER_F( ber_tag_t )
325ber_first_element LDAP_P((
326	BerElement *ber,
327	ber_len_t *len,
328	char **last ));
329
330LBER_F( ber_tag_t )
331ber_next_element LDAP_P((
332	BerElement *ber,
333	ber_len_t *len,
334	LDAP_CONST char *last ));
335
336LBER_F( ber_tag_t )
337ber_scanf LDAP_P((
338	BerElement *ber,
339	LDAP_CONST char *fmt,
340	... ));
341
342LBER_F( int )
343ber_decode_oid LDAP_P((
344	struct berval *in,
345	struct berval *out ));
346
347/*
348 * in encode.c
349 */
350LBER_F( int )
351ber_encode_oid LDAP_P((
352	struct berval *in,
353	struct berval *out ));
354
355typedef int (*BEREncodeCallback) LDAP_P((
356	BerElement *ber,
357	void *data ));
358
359LBER_F( int )
360ber_put_enum LDAP_P((
361	BerElement *ber,
362	ber_int_t num,
363	ber_tag_t tag ));
364
365LBER_F( int )
366ber_put_int LDAP_P((
367	BerElement *ber,
368	ber_int_t num,
369	ber_tag_t tag ));
370
371LBER_F( int )
372ber_put_ostring LDAP_P((
373	BerElement *ber,
374	LDAP_CONST char *str,
375	ber_len_t len,
376	ber_tag_t tag ));
377
378LBER_F( int )
379ber_put_berval LDAP_P((
380	BerElement *ber,
381	struct berval *bv,
382	ber_tag_t tag ));
383
384LBER_F( int )
385ber_put_string LDAP_P((
386	BerElement *ber,
387	LDAP_CONST char *str,
388	ber_tag_t tag ));
389
390LBER_F( int )
391ber_put_bitstring LDAP_P((
392	BerElement *ber,
393	LDAP_CONST char *str,
394	ber_len_t bitlen,
395	ber_tag_t tag ));
396
397LBER_F( int )
398ber_put_null LDAP_P((
399	BerElement *ber,
400	ber_tag_t tag ));
401
402LBER_F( int )
403ber_put_boolean LDAP_P((
404	BerElement *ber,
405	ber_int_t boolval,
406	ber_tag_t tag ));
407
408LBER_F( int )
409ber_start_seq LDAP_P((
410	BerElement *ber,
411	ber_tag_t tag ));
412
413LBER_F( int )
414ber_start_set LDAP_P((
415	BerElement *ber,
416	ber_tag_t tag ));
417
418LBER_F( int )
419ber_put_seq LDAP_P((
420	BerElement *ber ));
421
422LBER_F( int )
423ber_put_set LDAP_P((
424	BerElement *ber ));
425
426LBER_F( int )
427ber_printf LDAP_P((
428	BerElement *ber,
429	LDAP_CONST char *fmt,
430	... ));
431
432
433/*
434 * in io.c:
435 */
436
437LBER_F( ber_slen_t )
438ber_skip_data LDAP_P((
439	BerElement *ber,
440	ber_len_t len ));
441
442LBER_F( ber_slen_t )
443ber_read LDAP_P((
444	BerElement *ber,
445	char *buf,
446	ber_len_t len ));
447
448LBER_F( ber_slen_t )
449ber_write LDAP_P((
450	BerElement *ber,
451	LDAP_CONST char *buf,
452	ber_len_t len,
453	int zero ));	/* nonzero is unsupported from OpenLDAP 2.4.18 */
454
455LBER_F( void )
456ber_free LDAP_P((
457	BerElement *ber,
458	int freebuf ));
459
460LBER_F( void )
461ber_free_buf LDAP_P(( BerElement *ber ));
462
463LBER_F( int )
464ber_flush2 LDAP_P((
465	Sockbuf *sb,
466	BerElement *ber,
467	int freeit ));
468#define LBER_FLUSH_FREE_NEVER		(0x0)	/* traditional behavior */
469#define LBER_FLUSH_FREE_ON_SUCCESS	(0x1)	/* traditional behavior */
470#define LBER_FLUSH_FREE_ON_ERROR	(0x2)
471#define LBER_FLUSH_FREE_ALWAYS		(LBER_FLUSH_FREE_ON_SUCCESS|LBER_FLUSH_FREE_ON_ERROR)
472
473LBER_F( int )
474ber_flush LDAP_P((
475	Sockbuf *sb,
476	BerElement *ber,
477	int freeit )); /* DEPRECATED */
478
479LBER_F( BerElement * )
480ber_alloc LDAP_P(( void )); /* DEPRECATED */
481
482LBER_F( BerElement * )
483der_alloc LDAP_P(( void )); /* DEPRECATED */
484
485LBER_F( BerElement * )
486ber_alloc_t LDAP_P((
487	int beroptions ));
488
489LBER_F( BerElement * )
490ber_dup LDAP_P((
491	BerElement *ber ));
492
493LBER_F( ber_tag_t )
494ber_get_next LDAP_P((
495	Sockbuf *sb,
496	ber_len_t *len,
497	BerElement *ber ));
498
499LBER_F( void )
500ber_init2 LDAP_P((
501	BerElement *ber,
502	struct berval *bv,
503	int options ));
504
505LBER_F( void )
506ber_init_w_nullc LDAP_P((	/* DEPRECATED */
507	BerElement *ber,
508	int options ));
509
510LBER_F( void )
511ber_reset LDAP_P((
512	BerElement *ber,
513	int was_writing ));
514
515LBER_F( BerElement * )
516ber_init LDAP_P((
517	struct berval *bv ));
518
519LBER_F( int )
520ber_flatten LDAP_P((
521	BerElement *ber,
522	struct berval **bvPtr ));
523
524LBER_F( int )
525ber_flatten2 LDAP_P((
526	BerElement *ber,
527	struct berval *bv,
528	int alloc ));
529
530LBER_F( int )
531ber_remaining LDAP_P((
532	BerElement *ber ));
533
534/*
535 * LBER ber accessor functions
536 */
537
538LBER_F( int )
539ber_get_option LDAP_P((
540	void *item,
541	int option,
542	void *outvalue));
543
544LBER_F( int )
545ber_set_option LDAP_P((
546	void *item,
547	int option,
548	LDAP_CONST void *invalue));
549
550/*
551 * LBER sockbuf.c
552 */
553
554LBER_F( Sockbuf *  )
555ber_sockbuf_alloc LDAP_P((
556	void ));
557
558LBER_F( void )
559ber_sockbuf_free LDAP_P((
560	Sockbuf *sb ));
561
562LBER_F( int )
563ber_sockbuf_add_io LDAP_P((
564	Sockbuf *sb,
565	Sockbuf_IO *sbio,
566	int layer,
567	void *arg ));
568
569LBER_F( int )
570ber_sockbuf_remove_io LDAP_P((
571	Sockbuf *sb,
572	Sockbuf_IO *sbio,
573	int layer ));
574
575LBER_F( int )
576ber_sockbuf_ctrl LDAP_P((
577	Sockbuf *sb,
578	int opt,
579	void *arg ));
580
581LBER_V( Sockbuf_IO ) ber_sockbuf_io_tcp;
582LBER_V( Sockbuf_IO ) ber_sockbuf_io_readahead;
583LBER_V( Sockbuf_IO ) ber_sockbuf_io_fd;
584LBER_V( Sockbuf_IO ) ber_sockbuf_io_debug;
585LBER_V( Sockbuf_IO ) ber_sockbuf_io_udp;
586
587/*
588 * LBER memory.c
589 */
590LBER_F( void * )
591ber_memalloc LDAP_P((
592	ber_len_t s ));
593
594LBER_F( void * )
595ber_memrealloc LDAP_P((
596	void* p,
597	ber_len_t s ));
598
599LBER_F( void * )
600ber_memcalloc LDAP_P((
601	ber_len_t n,
602	ber_len_t s ));
603
604LBER_F( void )
605ber_memfree LDAP_P((
606	void* p ));
607
608LBER_F( void )
609ber_memvfree LDAP_P((
610	void** vector ));
611
612LBER_F( void )
613ber_bvfree LDAP_P((
614	struct berval *bv ));
615
616LBER_F( void )
617ber_bvecfree LDAP_P((
618	struct berval **bv ));
619
620LBER_F( int )
621ber_bvecadd LDAP_P((
622	struct berval ***bvec,
623	struct berval *bv ));
624
625LBER_F( struct berval * )
626ber_dupbv LDAP_P((
627	struct berval *dst, struct berval *src ));
628
629LBER_F( struct berval * )
630ber_bvdup LDAP_P((
631	struct berval *src ));
632
633LBER_F( struct berval * )
634ber_mem2bv LDAP_P((
635	LDAP_CONST char *, ber_len_t len, int duplicate, struct berval *bv));
636
637LBER_F( struct berval * )
638ber_str2bv LDAP_P((
639	LDAP_CONST char *, ber_len_t len, int duplicate, struct berval *bv));
640
641#define	ber_bvstr(a)	((ber_str2bv)((a), 0, 0, NULL))
642#define	ber_bvstrdup(a)	((ber_str2bv)((a), 0, 1, NULL))
643
644LBER_F( char * )
645ber_strdup LDAP_P((
646	LDAP_CONST char * ));
647
648LBER_F( ber_len_t )
649ber_strnlen LDAP_P((
650	LDAP_CONST char *s, ber_len_t len ));
651
652LBER_F( char * )
653ber_strndup LDAP_P((
654	LDAP_CONST char *s, ber_len_t l ));
655
656LBER_F( struct berval * )
657ber_bvreplace LDAP_P((
658	struct berval *dst, LDAP_CONST struct berval *src ));
659
660LBER_F( void )
661ber_bvarray_free LDAP_P(( BerVarray p ));
662
663LBER_F( int )
664ber_bvarray_add LDAP_P(( BerVarray *p, BerValue *bv ));
665
666#define ber_bvcmp(v1,v2) \
667	((v1)->bv_len < (v2)->bv_len \
668		? -1 : ((v1)->bv_len > (v2)->bv_len \
669			? 1 : memcmp((v1)->bv_val, (v2)->bv_val, (v1)->bv_len) ))
670
671/*
672 * error.c
673 */
674LBER_F( int * ) ber_errno_addr LDAP_P((void));
675#define ber_errno (*(ber_errno_addr)())
676
677#define LBER_ERROR_NONE		0
678#define LBER_ERROR_PARAM	0x1
679#define LBER_ERROR_MEMORY	0x2
680
681LDAP_END_DECL
682
683#endif /* _LBER_H */
684