nv.h revision 204076
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: head/sbin/hastd/nv.h 204076 2010-02-18 23:16:19Z pjd $
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
71struct nv;
72
73struct nv *nv_alloc(void);
74void nv_free(struct nv *nv);
75int nv_error(const struct nv *nv);
76int nv_set_error(struct nv *nv, int error);
77int nv_validate(struct nv *nv, size_t *extrap);
78
79struct ebuf *nv_hton(struct nv *nv);
80struct nv *nv_ntoh(struct ebuf *eb);
81
82void nv_add_int8(struct nv *nv, int8_t value, const char *namefmt, ...)
83    __printflike(3, 4);
84void nv_add_uint8(struct nv *nv, uint8_t value, const char *namefmt, ...)
85    __printflike(3, 4);
86void nv_add_int16(struct nv *nv, int16_t value, const char *namefmt, ...)
87    __printflike(3, 4);
88void nv_add_uint16(struct nv *nv, uint16_t value, const char *namefmt, ...)
89    __printflike(3, 4);
90void nv_add_int32(struct nv *nv, int32_t value, const char *namefmt, ...)
91    __printflike(3, 4);
92void nv_add_uint32(struct nv *nv, uint32_t value, const char *namefmt, ...)
93    __printflike(3, 4);
94void nv_add_int64(struct nv *nv, int64_t value, const char *namefmt, ...)
95    __printflike(3, 4);
96void nv_add_uint64(struct nv *nv, uint64_t value, const char *namefmt, ...)
97    __printflike(3, 4);
98void nv_add_int8_array(struct nv *nv, const int8_t *value, size_t size,
99    const char *namefmt, ...) __printflike(4, 5);
100void nv_add_uint8_array(struct nv *nv, const uint8_t *value, size_t size,
101    const char *namefmt, ...) __printflike(4, 5);
102void nv_add_int16_array(struct nv *nv, const int16_t *value, size_t size,
103    const char *namefmt, ...) __printflike(4, 5);
104void nv_add_uint16_array(struct nv *nv, const uint16_t *value, size_t size,
105    const char *namefmt, ...) __printflike(4, 5);
106void nv_add_int32_array(struct nv *nv, const int32_t *value, size_t size,
107    const char *namefmt, ...) __printflike(4, 5);
108void nv_add_uint32_array(struct nv *nv, const uint32_t *value, size_t size,
109    const char *namefmt, ...) __printflike(4, 5);
110void nv_add_int64_array(struct nv *nv, const int64_t *value, size_t size,
111    const char *namefmt, ...) __printflike(4, 5);
112void nv_add_uint64_array(struct nv *nv, const uint64_t *value, size_t size,
113    const char *namefmt, ...) __printflike(4, 5);
114void nv_add_string(struct nv *nv, const char *value, const char *namefmt, ...)
115    __printflike(3, 4);
116void nv_add_stringf(struct nv *nv, const char *name, const char *valuefmt, ...)
117    __printflike(3, 4);
118void nv_add_stringv(struct nv *nv, const char *name, const char *valuefmt,
119    va_list valueap) __printflike(3, 0);
120
121int8_t nv_get_int8(struct nv *nv, const char *namefmt, ...)
122    __printflike(2, 3);
123uint8_t nv_get_uint8(struct nv *nv, const char *namefmt, ...)
124    __printflike(2, 3);
125int16_t nv_get_int16(struct nv *nv, const char *namefmt, ...)
126    __printflike(2, 3);
127uint16_t nv_get_uint16(struct nv *nv, const char *namefmt, ...)
128    __printflike(2, 3);
129int32_t nv_get_int32(struct nv *nv, const char *namefmt, ...)
130    __printflike(2, 3);
131uint32_t nv_get_uint32(struct nv *nv, const char *namefmt, ...)
132    __printflike(2, 3);
133int64_t nv_get_int64(struct nv *nv, const char *namefmt, ...)
134    __printflike(2, 3);
135uint64_t nv_get_uint64(struct nv *nv, const char *namefmt, ...)
136    __printflike(2, 3);
137const int8_t *nv_get_int8_array(struct nv *nv, size_t *sizep,
138    const char *namefmt, ...) __printflike(3, 4);
139const uint8_t *nv_get_uint8_array(struct nv *nv, size_t *sizep,
140    const char *namefmt, ...) __printflike(3, 4);
141const int16_t *nv_get_int16_array(struct nv *nv, size_t *sizep,
142    const char *namefmt, ...) __printflike(3, 4);
143const uint16_t *nv_get_uint16_array(struct nv *nv, size_t *sizep,
144    const char *namefmt, ...) __printflike(3, 4);
145const int32_t *nv_get_int32_array(struct nv *nv, size_t *sizep,
146    const char *namefmt, ...) __printflike(3, 4);
147const uint32_t *nv_get_uint32_array(struct nv *nv, size_t *sizep,
148    const char *namefmt, ...) __printflike(3, 4);
149const int64_t *nv_get_int64_array(struct nv *nv, size_t *sizep,
150    const char *namefmt, ...) __printflike(3, 4);
151const uint64_t *nv_get_uint64_array(struct nv *nv, size_t *sizep,
152    const char *namefmt, ...) __printflike(3, 4);
153const char *nv_get_string(struct nv *nv, const char *namefmt, ...)
154    __printflike(2, 3);
155
156void nv_dump(struct nv *nv);
157
158#endif	/* !_NV_H_ */
159