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