xref: /freebsd/usr.sbin/ypldap/ber.h (revision f39bffc62c1395bde25d152c7f68fdf7cbaab414)
1 /*	$OpenBSD: ber.h,v 1.2 2008/12/29 15:48:13 aschrijver Exp $ */
2 /*	$FreeBSD$ */
3 
4 /*
5  * Copyright (c) 2007 Reyk Floeter <reyk@vantronix.net>
6  * Copyright (c) 2006, 2007 Claudio Jeker <claudio@openbsd.org>
7  *
8  * Permission to use, copy, modify, and distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  */
20 
21 struct ber_element {
22 	struct ber_element	*be_next;
23 	unsigned long		 be_type;
24 	unsigned long		 be_encoding;
25 	size_t			 be_len;
26 	int			 be_free;
27 	u_int8_t		 be_class;
28 	union {
29 		struct ber_element	*bv_sub;
30 		void			*bv_val;
31 		long long		 bv_numeric;
32 	} be_union;
33 #define be_sub		be_union.bv_sub
34 #define be_val		be_union.bv_val
35 #define be_numeric	be_union.bv_numeric
36 };
37 
38 struct ber {
39 	int	 fd;
40 	u_char	*br_wbuf;
41 	u_char	*br_wptr;
42 	u_char	*br_wend;
43 	u_char	*br_rbuf;
44 	u_char	*br_rptr;
45 	u_char	*br_rend;
46 
47 	unsigned long	(*br_application)(struct ber_element *);
48 };
49 
50 /* well-known ber_element types */
51 #define BER_TYPE_DEFAULT	((unsigned long)-1)
52 #define BER_TYPE_EOC		0
53 #define BER_TYPE_BOOLEAN	1
54 #define BER_TYPE_INTEGER	2
55 #define BER_TYPE_BITSTRING	3
56 #define BER_TYPE_OCTETSTRING	4
57 #define BER_TYPE_NULL		5
58 #define BER_TYPE_OBJECT		6
59 #define BER_TYPE_ENUMERATED	10
60 #define BER_TYPE_SEQUENCE	16
61 #define BER_TYPE_SET		17
62 
63 /* ber classes */
64 #define BER_CLASS_UNIVERSAL	0x0
65 #define BER_CLASS_UNIV		BER_CLASS_UNIVERSAL
66 #define BER_CLASS_APPLICATION	0x1
67 #define BER_CLASS_APP		BER_CLASS_APPLICATION
68 #define BER_CLASS_CONTEXT	0x2
69 #define BER_CLASS_PRIVATE	0x3
70 #define BER_CLASS_MASK		0x3
71 
72 /* common definitions */
73 #define BER_MIN_OID_LEN		2	/* OBJECT */
74 #define BER_MAX_OID_LEN		32	/* OBJECT */
75 
76 struct ber_oid {
77 	u_int32_t	bo_id[BER_MAX_OID_LEN + 1];
78 	size_t		bo_n;
79 };
80 
81 __BEGIN_DECLS
82 struct ber_element	*ber_get_element(unsigned long);
83 void			 ber_set_header(struct ber_element *, int,
84 			    unsigned long);
85 void			 ber_link_elements(struct ber_element *,
86 			    struct ber_element *);
87 struct ber_element	*ber_unlink_elements(struct ber_element *);
88 void			 ber_replace_elements(struct ber_element *,
89 			    struct ber_element *);
90 struct ber_element	*ber_add_sequence(struct ber_element *);
91 struct ber_element	*ber_add_set(struct ber_element *);
92 struct ber_element	*ber_add_integer(struct ber_element *, long long);
93 int			 ber_get_integer(struct ber_element *, long long *);
94 struct ber_element	*ber_add_enumerated(struct ber_element *, long long);
95 int			 ber_get_enumerated(struct ber_element *, long long *);
96 struct ber_element	*ber_add_boolean(struct ber_element *, int);
97 int			 ber_get_boolean(struct ber_element *, int *);
98 struct ber_element	*ber_add_string(struct ber_element *, const char *);
99 struct ber_element	*ber_add_nstring(struct ber_element *, const char *,
100 			    size_t);
101 int			 ber_get_string(struct ber_element *, char **);
102 int			 ber_get_nstring(struct ber_element *, void **,
103 			    size_t *);
104 struct ber_element	*ber_add_bitstring(struct ber_element *, const void *,
105 			    size_t);
106 int			 ber_get_bitstring(struct ber_element *, void **,
107 			    size_t *);
108 struct ber_element	*ber_add_null(struct ber_element *);
109 int			 ber_get_null(struct ber_element *);
110 struct ber_element	*ber_add_eoc(struct ber_element *);
111 int			 ber_get_eoc(struct ber_element *);
112 struct ber_element	*ber_add_oid(struct ber_element *, struct ber_oid *);
113 struct ber_element	*ber_add_noid(struct ber_element *, struct ber_oid *, int);
114 struct ber_element	*ber_add_oidstring(struct ber_element *, const char *);
115 int			 ber_get_oid(struct ber_element *, struct ber_oid *);
116 size_t			 ber_oid2ber(struct ber_oid *, u_int8_t *, size_t);
117 int			 ber_string2oid(const char *, struct ber_oid *);
118 struct ber_element	*ber_printf_elements(struct ber_element *, char *, ...);
119 int			 ber_scanf_elements(struct ber_element *, char *, ...);
120 ssize_t			 ber_get_writebuf(struct ber *, void **);
121 int			 ber_write_elements(struct ber *, struct ber_element *);
122 void			 ber_set_readbuf(struct ber *, void *, size_t);
123 struct ber_element	*ber_read_elements(struct ber *, struct ber_element *);
124 void			 ber_free_elements(struct ber_element *);
125 size_t			 ber_calc_len(struct ber_element *);
126 void			 ber_set_application(struct ber *,
127 			    unsigned long (*)(struct ber_element *));
128 void			 ber_free(struct ber *);
129 __END_DECLS
130