xref: /freebsd/sbin/hastd/nv.h (revision 7aa383846770374466b1dcb2cefd71bde9acf463)
1 /*-
2  * Copyright (c) 2009-2010 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Pawel Jakub Dawidek under sponsorship from
6  * the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 #ifndef	_NV_H_
33 #define	_NV_H_
34 
35 #include <sys/cdefs.h>
36 
37 #include <stdarg.h>
38 #include <stdbool.h>
39 #include <stdint.h>
40 #include <stdio.h>
41 
42 #include <ebuf.h>
43 
44 #define	NV_TYPE_INT8		1
45 #define	NV_TYPE_UINT8		2
46 #define	NV_TYPE_INT16		3
47 #define	NV_TYPE_UINT16		4
48 #define	NV_TYPE_INT32		5
49 #define	NV_TYPE_UINT32		6
50 #define	NV_TYPE_INT64		7
51 #define	NV_TYPE_UINT64		8
52 #define	NV_TYPE_INT8_ARRAY	9
53 #define	NV_TYPE_UINT8_ARRAY	10
54 #define	NV_TYPE_INT16_ARRAY	11
55 #define	NV_TYPE_UINT16_ARRAY	12
56 #define	NV_TYPE_INT32_ARRAY	13
57 #define	NV_TYPE_UINT32_ARRAY	14
58 #define	NV_TYPE_INT64_ARRAY	15
59 #define	NV_TYPE_UINT64_ARRAY	16
60 #define	NV_TYPE_STRING		17
61 
62 #define	NV_TYPE_MASK		0x7f
63 #define	NV_TYPE_FIRST		NV_TYPE_INT8
64 #define	NV_TYPE_LAST		NV_TYPE_STRING
65 
66 #define	NV_ORDER_NETWORK	0x00
67 #define	NV_ORDER_HOST		0x80
68 
69 #define	NV_ORDER_MASK		0x80
70 
71 struct nv;
72 
73 struct nv *nv_alloc(void);
74 void nv_free(struct nv *nv);
75 int nv_error(const struct nv *nv);
76 int nv_set_error(struct nv *nv, int error);
77 int nv_validate(struct nv *nv, size_t *extrap);
78 
79 struct ebuf *nv_hton(struct nv *nv);
80 struct nv *nv_ntoh(struct ebuf *eb);
81 
82 void nv_add_int8(struct nv *nv, int8_t value, const char *namefmt, ...)
83     __printflike(3, 4);
84 void nv_add_uint8(struct nv *nv, uint8_t value, const char *namefmt, ...)
85     __printflike(3, 4);
86 void nv_add_int16(struct nv *nv, int16_t value, const char *namefmt, ...)
87     __printflike(3, 4);
88 void nv_add_uint16(struct nv *nv, uint16_t value, const char *namefmt, ...)
89     __printflike(3, 4);
90 void nv_add_int32(struct nv *nv, int32_t value, const char *namefmt, ...)
91     __printflike(3, 4);
92 void nv_add_uint32(struct nv *nv, uint32_t value, const char *namefmt, ...)
93     __printflike(3, 4);
94 void nv_add_int64(struct nv *nv, int64_t value, const char *namefmt, ...)
95     __printflike(3, 4);
96 void nv_add_uint64(struct nv *nv, uint64_t value, const char *namefmt, ...)
97     __printflike(3, 4);
98 void nv_add_int8_array(struct nv *nv, const int8_t *value, size_t size,
99     const char *namefmt, ...) __printflike(4, 5);
100 void nv_add_uint8_array(struct nv *nv, const uint8_t *value, size_t size,
101     const char *namefmt, ...) __printflike(4, 5);
102 void nv_add_int16_array(struct nv *nv, const int16_t *value, size_t size,
103     const char *namefmt, ...) __printflike(4, 5);
104 void nv_add_uint16_array(struct nv *nv, const uint16_t *value, size_t size,
105     const char *namefmt, ...) __printflike(4, 5);
106 void nv_add_int32_array(struct nv *nv, const int32_t *value, size_t size,
107     const char *namefmt, ...) __printflike(4, 5);
108 void nv_add_uint32_array(struct nv *nv, const uint32_t *value, size_t size,
109     const char *namefmt, ...) __printflike(4, 5);
110 void nv_add_int64_array(struct nv *nv, const int64_t *value, size_t size,
111     const char *namefmt, ...) __printflike(4, 5);
112 void nv_add_uint64_array(struct nv *nv, const uint64_t *value, size_t size,
113     const char *namefmt, ...) __printflike(4, 5);
114 void nv_add_string(struct nv *nv, const char *value, const char *namefmt, ...)
115     __printflike(3, 4);
116 void nv_add_stringf(struct nv *nv, const char *name, const char *valuefmt, ...)
117     __printflike(3, 4);
118 void nv_add_stringv(struct nv *nv, const char *name, const char *valuefmt,
119     va_list valueap) __printflike(3, 0);
120 
121 int8_t nv_get_int8(struct nv *nv, const char *namefmt, ...)
122     __printflike(2, 3);
123 uint8_t nv_get_uint8(struct nv *nv, const char *namefmt, ...)
124     __printflike(2, 3);
125 int16_t nv_get_int16(struct nv *nv, const char *namefmt, ...)
126     __printflike(2, 3);
127 uint16_t nv_get_uint16(struct nv *nv, const char *namefmt, ...)
128     __printflike(2, 3);
129 int32_t nv_get_int32(struct nv *nv, const char *namefmt, ...)
130     __printflike(2, 3);
131 uint32_t nv_get_uint32(struct nv *nv, const char *namefmt, ...)
132     __printflike(2, 3);
133 int64_t nv_get_int64(struct nv *nv, const char *namefmt, ...)
134     __printflike(2, 3);
135 uint64_t nv_get_uint64(struct nv *nv, const char *namefmt, ...)
136     __printflike(2, 3);
137 const int8_t *nv_get_int8_array(struct nv *nv, size_t *sizep,
138     const char *namefmt, ...) __printflike(3, 4);
139 const uint8_t *nv_get_uint8_array(struct nv *nv, size_t *sizep,
140     const char *namefmt, ...) __printflike(3, 4);
141 const int16_t *nv_get_int16_array(struct nv *nv, size_t *sizep,
142     const char *namefmt, ...) __printflike(3, 4);
143 const uint16_t *nv_get_uint16_array(struct nv *nv, size_t *sizep,
144     const char *namefmt, ...) __printflike(3, 4);
145 const int32_t *nv_get_int32_array(struct nv *nv, size_t *sizep,
146     const char *namefmt, ...) __printflike(3, 4);
147 const uint32_t *nv_get_uint32_array(struct nv *nv, size_t *sizep,
148     const char *namefmt, ...) __printflike(3, 4);
149 const int64_t *nv_get_int64_array(struct nv *nv, size_t *sizep,
150     const char *namefmt, ...) __printflike(3, 4);
151 const uint64_t *nv_get_uint64_array(struct nv *nv, size_t *sizep,
152     const char *namefmt, ...) __printflike(3, 4);
153 const char *nv_get_string(struct nv *nv, const char *namefmt, ...)
154     __printflike(2, 3);
155 
156 void nv_dump(struct nv *nv);
157 
158 #endif	/* !_NV_H_ */
159