xref: /freebsd/sys/netgraph/ng_parse.c (revision 222c1141ea17780afc6acae54d3a7d9985112a8b)
1f8307e12SArchie Cobbs /*
2f8307e12SArchie Cobbs  * ng_parse.c
3c398230bSWarner Losh  */
4c398230bSWarner Losh 
5c398230bSWarner Losh /*-
6f8307e12SArchie Cobbs  * Copyright (c) 1999 Whistle Communications, Inc.
7f8307e12SArchie Cobbs  * All rights reserved.
8f8307e12SArchie Cobbs  *
9f8307e12SArchie Cobbs  * Subject to the following obligations and disclaimer of warranty, use and
10f8307e12SArchie Cobbs  * redistribution of this software, in source or object code forms, with or
11f8307e12SArchie Cobbs  * without modifications are expressly permitted by Whistle Communications;
12f8307e12SArchie Cobbs  * provided, however, that:
13f8307e12SArchie Cobbs  * 1. Any and all reproductions of the source or object code must include the
14f8307e12SArchie Cobbs  *    copyright notice above and the following disclaimer of warranties; and
15f8307e12SArchie Cobbs  * 2. No rights are granted, in any manner or form, to use Whistle
16f8307e12SArchie Cobbs  *    Communications, Inc. trademarks, including the mark "WHISTLE
17f8307e12SArchie Cobbs  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
18f8307e12SArchie Cobbs  *    such appears in the above copyright notice or in the software.
19f8307e12SArchie Cobbs  *
20f8307e12SArchie Cobbs  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
21f8307e12SArchie Cobbs  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
22f8307e12SArchie Cobbs  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
23f8307e12SArchie Cobbs  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
24f8307e12SArchie Cobbs  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
25f8307e12SArchie Cobbs  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
26f8307e12SArchie Cobbs  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
27f8307e12SArchie Cobbs  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
28f8307e12SArchie Cobbs  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
29f8307e12SArchie Cobbs  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30f8307e12SArchie Cobbs  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31f8307e12SArchie Cobbs  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32f8307e12SArchie Cobbs  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33f8307e12SArchie Cobbs  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34f8307e12SArchie Cobbs  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35f8307e12SArchie Cobbs  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
36f8307e12SArchie Cobbs  * OF SUCH DAMAGE.
37f8307e12SArchie Cobbs  *
38cc3bbd68SJulian Elischer  * Author: Archie Cobbs <archie@freebsd.org>
39f8307e12SArchie Cobbs  *
40f8307e12SArchie Cobbs  * $Whistle: ng_parse.c,v 1.3 1999/11/29 01:43:48 archie Exp $
41f8307e12SArchie Cobbs  * $FreeBSD$
42f8307e12SArchie Cobbs  */
43f8307e12SArchie Cobbs 
44f8307e12SArchie Cobbs #include <sys/types.h>
45f8307e12SArchie Cobbs #include <sys/param.h>
46f8307e12SArchie Cobbs #include <sys/systm.h>
479c8c302fSJulian Elischer #include <sys/kernel.h>
48f8307e12SArchie Cobbs #include <sys/errno.h>
49222c1141SGleb Smirnoff #include <sys/limits.h>
50f8307e12SArchie Cobbs #include <sys/malloc.h>
51327b288eSJulian Elischer #include <sys/mbuf.h>
52f8307e12SArchie Cobbs #include <sys/ctype.h>
53f8307e12SArchie Cobbs 
54f627a379SGleb Smirnoff #include <machine/stdarg.h>
55f627a379SGleb Smirnoff 
568c7e4101SRuslan Ermilov #include <net/ethernet.h>
578c7e4101SRuslan Ermilov 
58f8307e12SArchie Cobbs #include <netinet/in.h>
59f8307e12SArchie Cobbs 
60f8307e12SArchie Cobbs #include <netgraph/ng_message.h>
61f8307e12SArchie Cobbs #include <netgraph/netgraph.h>
62f8307e12SArchie Cobbs #include <netgraph/ng_parse.h>
63f8307e12SArchie Cobbs 
649c8c302fSJulian Elischer #ifdef NG_SEPARATE_MALLOC
659c8c302fSJulian Elischer MALLOC_DEFINE(M_NETGRAPH_PARSE, "netgraph_parse", "netgraph parse info");
669c8c302fSJulian Elischer #else
679c8c302fSJulian Elischer #define M_NETGRAPH_PARSE M_NETGRAPH
689c8c302fSJulian Elischer #endif
699c8c302fSJulian Elischer 
70f8307e12SArchie Cobbs /* Compute alignment for primitive integral types */
71f8307e12SArchie Cobbs struct int16_temp {
72f8307e12SArchie Cobbs 	char	x;
73f8307e12SArchie Cobbs 	int16_t	y;
74f8307e12SArchie Cobbs };
75f8307e12SArchie Cobbs 
76f8307e12SArchie Cobbs struct int32_temp {
77f8307e12SArchie Cobbs 	char	x;
78f8307e12SArchie Cobbs 	int32_t	y;
79f8307e12SArchie Cobbs };
80f8307e12SArchie Cobbs 
81f8307e12SArchie Cobbs struct int64_temp {
82f8307e12SArchie Cobbs 	char	x;
83f8307e12SArchie Cobbs 	int64_t	y;
84f8307e12SArchie Cobbs };
85f8307e12SArchie Cobbs 
86f8307e12SArchie Cobbs #define INT8_ALIGNMENT		1
87f8307e12SArchie Cobbs #define INT16_ALIGNMENT		((int)&((struct int16_temp *)0)->y)
88f8307e12SArchie Cobbs #define INT32_ALIGNMENT		((int)&((struct int32_temp *)0)->y)
89f8307e12SArchie Cobbs #define INT64_ALIGNMENT		((int)&((struct int64_temp *)0)->y)
90f8307e12SArchie Cobbs 
918db3c6cdSArchie Cobbs /* Output format for integral types */
928db3c6cdSArchie Cobbs #define INT_UNSIGNED		0
938db3c6cdSArchie Cobbs #define INT_SIGNED		1
948db3c6cdSArchie Cobbs #define INT_HEX			2
958db3c6cdSArchie Cobbs 
96f8307e12SArchie Cobbs /* Type of composite object: struct, array, or fixedarray */
97f8307e12SArchie Cobbs enum comptype {
98f8307e12SArchie Cobbs 	CT_STRUCT,
99f8307e12SArchie Cobbs 	CT_ARRAY,
100f8307e12SArchie Cobbs 	CT_FIXEDARRAY,
101f8307e12SArchie Cobbs };
102f8307e12SArchie Cobbs 
103f8307e12SArchie Cobbs /* Composite types helper functions */
104f8307e12SArchie Cobbs static int	ng_parse_composite(const struct ng_parse_type *type,
105f8307e12SArchie Cobbs 			const char *s, int *off, const u_char *start,
106f8307e12SArchie Cobbs 			u_char *const buf, int *buflen, enum comptype ctype);
107f8307e12SArchie Cobbs static int	ng_unparse_composite(const struct ng_parse_type *type,
108f8307e12SArchie Cobbs 			const u_char *data, int *off, char *cbuf, int cbuflen,
109f8307e12SArchie Cobbs 			enum comptype ctype);
110f8307e12SArchie Cobbs static int	ng_get_composite_elem_default(const struct ng_parse_type *type,
111f8307e12SArchie Cobbs 			int index, const u_char *start, u_char *buf,
112f8307e12SArchie Cobbs 			int *buflen, enum comptype ctype);
113f8307e12SArchie Cobbs static int	ng_get_composite_len(const struct ng_parse_type *type,
114f8307e12SArchie Cobbs 			const u_char *start, const u_char *buf,
115f8307e12SArchie Cobbs 			enum comptype ctype);
116f8307e12SArchie Cobbs static const	struct ng_parse_type *ng_get_composite_etype(const struct
117f8307e12SArchie Cobbs 			ng_parse_type *type, int index, enum comptype ctype);
118f8307e12SArchie Cobbs static int	ng_parse_get_elem_pad(const struct ng_parse_type *type,
119f8307e12SArchie Cobbs 			int index, enum comptype ctype, int posn);
120f8307e12SArchie Cobbs 
121f8307e12SArchie Cobbs /* Parsing helper functions */
122f8307e12SArchie Cobbs static int	ng_parse_skip_value(const char *s, int off, int *lenp);
123f627a379SGleb Smirnoff static int	ng_parse_append(char **cbufp, int *cbuflenp,
124f627a379SGleb Smirnoff 			const char *fmt, ...);
125f8307e12SArchie Cobbs 
126f8307e12SArchie Cobbs /* Poor man's virtual method calls */
127f8307e12SArchie Cobbs #define METHOD(t,m)	(ng_get_ ## m ## _method(t))
128f8307e12SArchie Cobbs #define INVOKE(t,m)	(*METHOD(t,m))
129f8307e12SArchie Cobbs 
130f8307e12SArchie Cobbs static ng_parse_t	*ng_get_parse_method(const struct ng_parse_type *t);
131f8307e12SArchie Cobbs static ng_unparse_t	*ng_get_unparse_method(const struct ng_parse_type *t);
132f8307e12SArchie Cobbs static ng_getDefault_t	*ng_get_getDefault_method(const
133f8307e12SArchie Cobbs 				struct ng_parse_type *t);
134f8307e12SArchie Cobbs static ng_getAlign_t	*ng_get_getAlign_method(const struct ng_parse_type *t);
135f8307e12SArchie Cobbs 
136f8307e12SArchie Cobbs #define ALIGNMENT(t)	(METHOD(t, getAlign) == NULL ? \
137f8307e12SArchie Cobbs 				0 : INVOKE(t, getAlign)(t))
138f8307e12SArchie Cobbs 
139f8307e12SArchie Cobbs /************************************************************************
140f8307e12SArchie Cobbs 			PUBLIC FUNCTIONS
141f8307e12SArchie Cobbs  ************************************************************************/
142f8307e12SArchie Cobbs 
143f8307e12SArchie Cobbs /*
144f8307e12SArchie Cobbs  * Convert an ASCII string to binary according to the supplied type descriptor
145f8307e12SArchie Cobbs  */
146f8307e12SArchie Cobbs int
147f8307e12SArchie Cobbs ng_parse(const struct ng_parse_type *type,
148f8307e12SArchie Cobbs 	const char *string, int *off, u_char *buf, int *buflen)
149f8307e12SArchie Cobbs {
150f8307e12SArchie Cobbs 	return INVOKE(type, parse)(type, string, off, buf, buf, buflen);
151f8307e12SArchie Cobbs }
152f8307e12SArchie Cobbs 
153f8307e12SArchie Cobbs /*
154f8307e12SArchie Cobbs  * Convert binary to an ASCII string according to the supplied type descriptor
155f8307e12SArchie Cobbs  */
156f8307e12SArchie Cobbs int
157f8307e12SArchie Cobbs ng_unparse(const struct ng_parse_type *type,
158f8307e12SArchie Cobbs 	const u_char *data, char *cbuf, int cbuflen)
159f8307e12SArchie Cobbs {
160f8307e12SArchie Cobbs 	int off = 0;
161f8307e12SArchie Cobbs 
162f8307e12SArchie Cobbs 	return INVOKE(type, unparse)(type, data, &off, cbuf, cbuflen);
163f8307e12SArchie Cobbs }
164f8307e12SArchie Cobbs 
165f8307e12SArchie Cobbs /*
166f8307e12SArchie Cobbs  * Fill in the default value according to the supplied type descriptor
167f8307e12SArchie Cobbs  */
168f8307e12SArchie Cobbs int
169f8307e12SArchie Cobbs ng_parse_getDefault(const struct ng_parse_type *type, u_char *buf, int *buflen)
170f8307e12SArchie Cobbs {
171f8307e12SArchie Cobbs 	ng_getDefault_t *const func = METHOD(type, getDefault);
172f8307e12SArchie Cobbs 
173f8307e12SArchie Cobbs 	if (func == NULL)
174f8307e12SArchie Cobbs 		return (EOPNOTSUPP);
175f8307e12SArchie Cobbs 	return (*func)(type, buf, buf, buflen);
176f8307e12SArchie Cobbs }
177f8307e12SArchie Cobbs 
178f8307e12SArchie Cobbs 
179f8307e12SArchie Cobbs /************************************************************************
180f8307e12SArchie Cobbs 			STRUCTURE TYPE
181f8307e12SArchie Cobbs  ************************************************************************/
182f8307e12SArchie Cobbs 
183f8307e12SArchie Cobbs static int
184f8307e12SArchie Cobbs ng_struct_parse(const struct ng_parse_type *type,
185f8307e12SArchie Cobbs 	const char *s, int *off, const u_char *const start,
186f8307e12SArchie Cobbs 	u_char *const buf, int *buflen)
187f8307e12SArchie Cobbs {
188f8307e12SArchie Cobbs 	return ng_parse_composite(type, s, off, start, buf, buflen, CT_STRUCT);
189f8307e12SArchie Cobbs }
190f8307e12SArchie Cobbs 
191f8307e12SArchie Cobbs static int
192f8307e12SArchie Cobbs ng_struct_unparse(const struct ng_parse_type *type,
193f8307e12SArchie Cobbs 	const u_char *data, int *off, char *cbuf, int cbuflen)
194f8307e12SArchie Cobbs {
195f8307e12SArchie Cobbs 	return ng_unparse_composite(type, data, off, cbuf, cbuflen, CT_STRUCT);
196f8307e12SArchie Cobbs }
197f8307e12SArchie Cobbs 
198f8307e12SArchie Cobbs static int
199f8307e12SArchie Cobbs ng_struct_getDefault(const struct ng_parse_type *type,
200f8307e12SArchie Cobbs 	const u_char *const start, u_char *buf, int *buflen)
201f8307e12SArchie Cobbs {
202f8307e12SArchie Cobbs 	int off = 0;
203f8307e12SArchie Cobbs 
204f8307e12SArchie Cobbs 	return ng_parse_composite(type,
205f8307e12SArchie Cobbs 	    "{}", &off, start, buf, buflen, CT_STRUCT);
206f8307e12SArchie Cobbs }
207f8307e12SArchie Cobbs 
208f8307e12SArchie Cobbs static int
209f8307e12SArchie Cobbs ng_struct_getAlign(const struct ng_parse_type *type)
210f8307e12SArchie Cobbs {
211f8307e12SArchie Cobbs 	const struct ng_parse_struct_field *field;
212f8307e12SArchie Cobbs 	int align = 0;
213f8307e12SArchie Cobbs 
214f0184ff8SArchie Cobbs 	for (field = type->info; field->name != NULL; field++) {
215f8307e12SArchie Cobbs 		int falign = ALIGNMENT(field->type);
216f8307e12SArchie Cobbs 
217f8307e12SArchie Cobbs 		if (falign > align)
218f8307e12SArchie Cobbs 			align = falign;
219f8307e12SArchie Cobbs 	}
220f8307e12SArchie Cobbs 	return align;
221f8307e12SArchie Cobbs }
222f8307e12SArchie Cobbs 
223f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_struct_type = {
224f8307e12SArchie Cobbs 	NULL,
225f8307e12SArchie Cobbs 	NULL,
226f8307e12SArchie Cobbs 	NULL,
227f8307e12SArchie Cobbs 	ng_struct_parse,
228f8307e12SArchie Cobbs 	ng_struct_unparse,
229f8307e12SArchie Cobbs 	ng_struct_getDefault,
230f8307e12SArchie Cobbs 	ng_struct_getAlign
231f8307e12SArchie Cobbs };
232f8307e12SArchie Cobbs 
233f8307e12SArchie Cobbs /************************************************************************
234f8307e12SArchie Cobbs 			FIXED LENGTH ARRAY TYPE
235f8307e12SArchie Cobbs  ************************************************************************/
236f8307e12SArchie Cobbs 
237f8307e12SArchie Cobbs static int
238f8307e12SArchie Cobbs ng_fixedarray_parse(const struct ng_parse_type *type,
239f8307e12SArchie Cobbs 	const char *s, int *off, const u_char *const start,
240f8307e12SArchie Cobbs 	u_char *const buf, int *buflen)
241f8307e12SArchie Cobbs {
242f8307e12SArchie Cobbs 	return ng_parse_composite(type,
243f8307e12SArchie Cobbs 	    s, off, start, buf, buflen, CT_FIXEDARRAY);
244f8307e12SArchie Cobbs }
245f8307e12SArchie Cobbs 
246f8307e12SArchie Cobbs static int
247f8307e12SArchie Cobbs ng_fixedarray_unparse(const struct ng_parse_type *type,
248f8307e12SArchie Cobbs 	const u_char *data, int *off, char *cbuf, int cbuflen)
249f8307e12SArchie Cobbs {
250f8307e12SArchie Cobbs 	return ng_unparse_composite(type,
251f8307e12SArchie Cobbs 		data, off, cbuf, cbuflen, CT_FIXEDARRAY);
252f8307e12SArchie Cobbs }
253f8307e12SArchie Cobbs 
254f8307e12SArchie Cobbs static int
255f8307e12SArchie Cobbs ng_fixedarray_getDefault(const struct ng_parse_type *type,
256f8307e12SArchie Cobbs 	const u_char *const start, u_char *buf, int *buflen)
257f8307e12SArchie Cobbs {
258f8307e12SArchie Cobbs 	int off = 0;
259f8307e12SArchie Cobbs 
260f8307e12SArchie Cobbs 	return ng_parse_composite(type,
261f8307e12SArchie Cobbs 	    "[]", &off, start, buf, buflen, CT_FIXEDARRAY);
262f8307e12SArchie Cobbs }
263f8307e12SArchie Cobbs 
264f8307e12SArchie Cobbs static int
265f8307e12SArchie Cobbs ng_fixedarray_getAlign(const struct ng_parse_type *type)
266f8307e12SArchie Cobbs {
267f8307e12SArchie Cobbs 	const struct ng_parse_fixedarray_info *fi = type->info;
268f8307e12SArchie Cobbs 
269f8307e12SArchie Cobbs 	return ALIGNMENT(fi->elementType);
270f8307e12SArchie Cobbs }
271f8307e12SArchie Cobbs 
272f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_fixedarray_type = {
273f8307e12SArchie Cobbs 	NULL,
274f8307e12SArchie Cobbs 	NULL,
275f8307e12SArchie Cobbs 	NULL,
276f8307e12SArchie Cobbs 	ng_fixedarray_parse,
277f8307e12SArchie Cobbs 	ng_fixedarray_unparse,
278f8307e12SArchie Cobbs 	ng_fixedarray_getDefault,
279f8307e12SArchie Cobbs 	ng_fixedarray_getAlign
280f8307e12SArchie Cobbs };
281f8307e12SArchie Cobbs 
282f8307e12SArchie Cobbs /************************************************************************
283f8307e12SArchie Cobbs 			VARIABLE LENGTH ARRAY TYPE
284f8307e12SArchie Cobbs  ************************************************************************/
285f8307e12SArchie Cobbs 
286f8307e12SArchie Cobbs static int
287f8307e12SArchie Cobbs ng_array_parse(const struct ng_parse_type *type,
288f8307e12SArchie Cobbs 	const char *s, int *off, const u_char *const start,
289f8307e12SArchie Cobbs 	u_char *const buf, int *buflen)
290f8307e12SArchie Cobbs {
291f8307e12SArchie Cobbs 	return ng_parse_composite(type, s, off, start, buf, buflen, CT_ARRAY);
292f8307e12SArchie Cobbs }
293f8307e12SArchie Cobbs 
294f8307e12SArchie Cobbs static int
295f8307e12SArchie Cobbs ng_array_unparse(const struct ng_parse_type *type,
296f8307e12SArchie Cobbs 	const u_char *data, int *off, char *cbuf, int cbuflen)
297f8307e12SArchie Cobbs {
298f8307e12SArchie Cobbs 	return ng_unparse_composite(type, data, off, cbuf, cbuflen, CT_ARRAY);
299f8307e12SArchie Cobbs }
300f8307e12SArchie Cobbs 
301f8307e12SArchie Cobbs static int
302f8307e12SArchie Cobbs ng_array_getDefault(const struct ng_parse_type *type,
303f8307e12SArchie Cobbs 	const u_char *const start, u_char *buf, int *buflen)
304f8307e12SArchie Cobbs {
305f8307e12SArchie Cobbs 	int off = 0;
306f8307e12SArchie Cobbs 
307f8307e12SArchie Cobbs 	return ng_parse_composite(type,
308f8307e12SArchie Cobbs 	    "[]", &off, start, buf, buflen, CT_ARRAY);
309f8307e12SArchie Cobbs }
310f8307e12SArchie Cobbs 
311f8307e12SArchie Cobbs static int
312f8307e12SArchie Cobbs ng_array_getAlign(const struct ng_parse_type *type)
313f8307e12SArchie Cobbs {
314f8307e12SArchie Cobbs 	const struct ng_parse_array_info *ai = type->info;
315f8307e12SArchie Cobbs 
316f8307e12SArchie Cobbs 	return ALIGNMENT(ai->elementType);
317f8307e12SArchie Cobbs }
318f8307e12SArchie Cobbs 
319f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_array_type = {
320f8307e12SArchie Cobbs 	NULL,
321f8307e12SArchie Cobbs 	NULL,
322f8307e12SArchie Cobbs 	NULL,
323f8307e12SArchie Cobbs 	ng_array_parse,
324f8307e12SArchie Cobbs 	ng_array_unparse,
325f8307e12SArchie Cobbs 	ng_array_getDefault,
326f8307e12SArchie Cobbs 	ng_array_getAlign
327f8307e12SArchie Cobbs };
328f8307e12SArchie Cobbs 
329f8307e12SArchie Cobbs /************************************************************************
330f8307e12SArchie Cobbs 				INT8 TYPE
331f8307e12SArchie Cobbs  ************************************************************************/
332f8307e12SArchie Cobbs 
333f8307e12SArchie Cobbs static int
334f8307e12SArchie Cobbs ng_int8_parse(const struct ng_parse_type *type,
335f8307e12SArchie Cobbs 	const char *s, int *off, const u_char *const start,
336f8307e12SArchie Cobbs 	u_char *const buf, int *buflen)
337f8307e12SArchie Cobbs {
338f8307e12SArchie Cobbs 	long val;
339f8307e12SArchie Cobbs 	int8_t val8;
340f8307e12SArchie Cobbs 	char *eptr;
341f8307e12SArchie Cobbs 
342f8307e12SArchie Cobbs 	val = strtol(s + *off, &eptr, 0);
343a514569eSJohn Polstra 	if (val < (int8_t)0x80 || val > (u_int8_t)0xff || eptr == s + *off)
344f8307e12SArchie Cobbs 		return (EINVAL);
345f8307e12SArchie Cobbs 	*off = eptr - s;
346f8307e12SArchie Cobbs 	val8 = (int8_t)val;
347f8307e12SArchie Cobbs 	bcopy(&val8, buf, sizeof(int8_t));
348f8307e12SArchie Cobbs 	*buflen = sizeof(int8_t);
349f8307e12SArchie Cobbs 	return (0);
350f8307e12SArchie Cobbs }
351f8307e12SArchie Cobbs 
352f8307e12SArchie Cobbs static int
353f8307e12SArchie Cobbs ng_int8_unparse(const struct ng_parse_type *type,
354f8307e12SArchie Cobbs 	const u_char *data, int *off, char *cbuf, int cbuflen)
355f8307e12SArchie Cobbs {
3568db3c6cdSArchie Cobbs 	const char *fmt;
3578db3c6cdSArchie Cobbs 	int fval;
358f627a379SGleb Smirnoff 	int error;
359f8307e12SArchie Cobbs 	int8_t val;
360f8307e12SArchie Cobbs 
361f8307e12SArchie Cobbs 	bcopy(data + *off, &val, sizeof(int8_t));
3628214d60eSJohn Baldwin 	switch ((intptr_t)type->info) {
3638db3c6cdSArchie Cobbs 	case INT_SIGNED:
3648db3c6cdSArchie Cobbs 		fmt = "%d";
3658db3c6cdSArchie Cobbs 		fval = val;
3668db3c6cdSArchie Cobbs 		break;
3678db3c6cdSArchie Cobbs 	case INT_UNSIGNED:
3688db3c6cdSArchie Cobbs 		fmt = "%u";
3698db3c6cdSArchie Cobbs 		fval = (u_int8_t)val;
3708db3c6cdSArchie Cobbs 		break;
3718db3c6cdSArchie Cobbs 	case INT_HEX:
3728db3c6cdSArchie Cobbs 		fmt = "0x%x";
3738db3c6cdSArchie Cobbs 		fval = (u_int8_t)val;
3748db3c6cdSArchie Cobbs 		break;
3758db3c6cdSArchie Cobbs 	default:
3766e551fb6SDavid E. O'Brien 		panic("%s: unknown type", __func__);
377b40ce416SJulian Elischer #ifdef	RESTARTABLE_PANICS
378b40ce416SJulian Elischer 		return(0);
379b40ce416SJulian Elischer #endif
3808db3c6cdSArchie Cobbs 	}
381f627a379SGleb Smirnoff 	if ((error = ng_parse_append(&cbuf, &cbuflen, fmt, fval)) != 0)
382f627a379SGleb Smirnoff 		return (error);
383f8307e12SArchie Cobbs 	*off += sizeof(int8_t);
384f8307e12SArchie Cobbs 	return (0);
385f8307e12SArchie Cobbs }
386f8307e12SArchie Cobbs 
387f8307e12SArchie Cobbs static int
388f8307e12SArchie Cobbs ng_int8_getDefault(const struct ng_parse_type *type,
389f8307e12SArchie Cobbs 	const u_char *const start, u_char *buf, int *buflen)
390f8307e12SArchie Cobbs {
391f8307e12SArchie Cobbs 	int8_t val;
392f8307e12SArchie Cobbs 
393f8307e12SArchie Cobbs 	if (*buflen < sizeof(int8_t))
394f8307e12SArchie Cobbs 		return (ERANGE);
395f8307e12SArchie Cobbs 	val = 0;
396f8307e12SArchie Cobbs 	bcopy(&val, buf, sizeof(int8_t));
397f8307e12SArchie Cobbs 	*buflen = sizeof(int8_t);
398f8307e12SArchie Cobbs 	return (0);
399f8307e12SArchie Cobbs }
400f8307e12SArchie Cobbs 
401f8307e12SArchie Cobbs static int
402f8307e12SArchie Cobbs ng_int8_getAlign(const struct ng_parse_type *type)
403f8307e12SArchie Cobbs {
404f8307e12SArchie Cobbs 	return INT8_ALIGNMENT;
405f8307e12SArchie Cobbs }
406f8307e12SArchie Cobbs 
407f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_int8_type = {
408f8307e12SArchie Cobbs 	NULL,
4098db3c6cdSArchie Cobbs 	(void *)INT_SIGNED,
410f8307e12SArchie Cobbs 	NULL,
411f8307e12SArchie Cobbs 	ng_int8_parse,
412f8307e12SArchie Cobbs 	ng_int8_unparse,
413f8307e12SArchie Cobbs 	ng_int8_getDefault,
414f8307e12SArchie Cobbs 	ng_int8_getAlign
415f8307e12SArchie Cobbs };
416f8307e12SArchie Cobbs 
4178db3c6cdSArchie Cobbs const struct ng_parse_type ng_parse_uint8_type = {
4188db3c6cdSArchie Cobbs 	&ng_parse_int8_type,
4198db3c6cdSArchie Cobbs 	(void *)INT_UNSIGNED
4208db3c6cdSArchie Cobbs };
4218db3c6cdSArchie Cobbs 
4228db3c6cdSArchie Cobbs const struct ng_parse_type ng_parse_hint8_type = {
4238db3c6cdSArchie Cobbs 	&ng_parse_int8_type,
4248db3c6cdSArchie Cobbs 	(void *)INT_HEX
4258db3c6cdSArchie Cobbs };
4268db3c6cdSArchie Cobbs 
427f8307e12SArchie Cobbs /************************************************************************
428f8307e12SArchie Cobbs 				INT16 TYPE
429f8307e12SArchie Cobbs  ************************************************************************/
430f8307e12SArchie Cobbs 
431f8307e12SArchie Cobbs static int
432f8307e12SArchie Cobbs ng_int16_parse(const struct ng_parse_type *type,
433f8307e12SArchie Cobbs 	const char *s, int *off, const u_char *const start,
434f8307e12SArchie Cobbs 	u_char *const buf, int *buflen)
435f8307e12SArchie Cobbs {
436f8307e12SArchie Cobbs 	long val;
437f8307e12SArchie Cobbs 	int16_t val16;
438f8307e12SArchie Cobbs 	char *eptr;
439f8307e12SArchie Cobbs 
440f8307e12SArchie Cobbs 	val = strtol(s + *off, &eptr, 0);
441a514569eSJohn Polstra 	if (val < (int16_t)0x8000
442a514569eSJohn Polstra 	    || val > (u_int16_t)0xffff || eptr == s + *off)
443f8307e12SArchie Cobbs 		return (EINVAL);
444f8307e12SArchie Cobbs 	*off = eptr - s;
445f8307e12SArchie Cobbs 	val16 = (int16_t)val;
446f8307e12SArchie Cobbs 	bcopy(&val16, buf, sizeof(int16_t));
447f8307e12SArchie Cobbs 	*buflen = sizeof(int16_t);
448f8307e12SArchie Cobbs 	return (0);
449f8307e12SArchie Cobbs }
450f8307e12SArchie Cobbs 
451f8307e12SArchie Cobbs static int
452f8307e12SArchie Cobbs ng_int16_unparse(const struct ng_parse_type *type,
453f8307e12SArchie Cobbs 	const u_char *data, int *off, char *cbuf, int cbuflen)
454f8307e12SArchie Cobbs {
4558db3c6cdSArchie Cobbs 	const char *fmt;
4568db3c6cdSArchie Cobbs 	int fval;
457f627a379SGleb Smirnoff 	int error;
458f8307e12SArchie Cobbs 	int16_t val;
459f8307e12SArchie Cobbs 
460f8307e12SArchie Cobbs 	bcopy(data + *off, &val, sizeof(int16_t));
4618214d60eSJohn Baldwin 	switch ((intptr_t)type->info) {
4628db3c6cdSArchie Cobbs 	case INT_SIGNED:
4638db3c6cdSArchie Cobbs 		fmt = "%d";
4648db3c6cdSArchie Cobbs 		fval = val;
4658db3c6cdSArchie Cobbs 		break;
4668db3c6cdSArchie Cobbs 	case INT_UNSIGNED:
4678db3c6cdSArchie Cobbs 		fmt = "%u";
4688db3c6cdSArchie Cobbs 		fval = (u_int16_t)val;
4698db3c6cdSArchie Cobbs 		break;
4708db3c6cdSArchie Cobbs 	case INT_HEX:
4718db3c6cdSArchie Cobbs 		fmt = "0x%x";
4728db3c6cdSArchie Cobbs 		fval = (u_int16_t)val;
4738db3c6cdSArchie Cobbs 		break;
4748db3c6cdSArchie Cobbs 	default:
4756e551fb6SDavid E. O'Brien 		panic("%s: unknown type", __func__);
476b40ce416SJulian Elischer #ifdef	RESTARTABLE_PANICS
477b40ce416SJulian Elischer 		return(0);
478b40ce416SJulian Elischer #endif
4798db3c6cdSArchie Cobbs 	}
480f627a379SGleb Smirnoff 	if ((error = ng_parse_append(&cbuf, &cbuflen, fmt, fval)) != 0)
481f627a379SGleb Smirnoff 		return (error);
482f8307e12SArchie Cobbs 	*off += sizeof(int16_t);
483f8307e12SArchie Cobbs 	return (0);
484f8307e12SArchie Cobbs }
485f8307e12SArchie Cobbs 
486f8307e12SArchie Cobbs static int
487f8307e12SArchie Cobbs ng_int16_getDefault(const struct ng_parse_type *type,
488f8307e12SArchie Cobbs 	const u_char *const start, u_char *buf, int *buflen)
489f8307e12SArchie Cobbs {
490f8307e12SArchie Cobbs 	int16_t val;
491f8307e12SArchie Cobbs 
492f8307e12SArchie Cobbs 	if (*buflen < sizeof(int16_t))
493f8307e12SArchie Cobbs 		return (ERANGE);
494f8307e12SArchie Cobbs 	val = 0;
495f8307e12SArchie Cobbs 	bcopy(&val, buf, sizeof(int16_t));
496f8307e12SArchie Cobbs 	*buflen = sizeof(int16_t);
497f8307e12SArchie Cobbs 	return (0);
498f8307e12SArchie Cobbs }
499f8307e12SArchie Cobbs 
500f8307e12SArchie Cobbs static int
501f8307e12SArchie Cobbs ng_int16_getAlign(const struct ng_parse_type *type)
502f8307e12SArchie Cobbs {
503f8307e12SArchie Cobbs 	return INT16_ALIGNMENT;
504f8307e12SArchie Cobbs }
505f8307e12SArchie Cobbs 
506f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_int16_type = {
507f8307e12SArchie Cobbs 	NULL,
5088db3c6cdSArchie Cobbs 	(void *)INT_SIGNED,
509f8307e12SArchie Cobbs 	NULL,
510f8307e12SArchie Cobbs 	ng_int16_parse,
511f8307e12SArchie Cobbs 	ng_int16_unparse,
512f8307e12SArchie Cobbs 	ng_int16_getDefault,
513f8307e12SArchie Cobbs 	ng_int16_getAlign
514f8307e12SArchie Cobbs };
515f8307e12SArchie Cobbs 
5168db3c6cdSArchie Cobbs const struct ng_parse_type ng_parse_uint16_type = {
5178db3c6cdSArchie Cobbs 	&ng_parse_int16_type,
5188db3c6cdSArchie Cobbs 	(void *)INT_UNSIGNED
5198db3c6cdSArchie Cobbs };
5208db3c6cdSArchie Cobbs 
5218db3c6cdSArchie Cobbs const struct ng_parse_type ng_parse_hint16_type = {
5228db3c6cdSArchie Cobbs 	&ng_parse_int16_type,
5238db3c6cdSArchie Cobbs 	(void *)INT_HEX
5248db3c6cdSArchie Cobbs };
5258db3c6cdSArchie Cobbs 
526f8307e12SArchie Cobbs /************************************************************************
527f8307e12SArchie Cobbs 				INT32 TYPE
528f8307e12SArchie Cobbs  ************************************************************************/
529f8307e12SArchie Cobbs 
530f8307e12SArchie Cobbs static int
531f8307e12SArchie Cobbs ng_int32_parse(const struct ng_parse_type *type,
532f8307e12SArchie Cobbs 	const char *s, int *off, const u_char *const start,
533f8307e12SArchie Cobbs 	u_char *const buf, int *buflen)
534f8307e12SArchie Cobbs {
535f8307e12SArchie Cobbs 	long val;			/* assumes long is at least 32 bits */
536f8307e12SArchie Cobbs 	int32_t val32;
537f8307e12SArchie Cobbs 	char *eptr;
538f8307e12SArchie Cobbs 
53991b756f4SRuslan Ermilov 	if ((intptr_t)type->info == INT_SIGNED)
540f8307e12SArchie Cobbs 		val = strtol(s + *off, &eptr, 0);
54191b756f4SRuslan Ermilov 	else
54291b756f4SRuslan Ermilov 		val = strtoul(s + *off, &eptr, 0);
543a514569eSJohn Polstra 	if (val < (int32_t)0x80000000
544a514569eSJohn Polstra 	    || val > (u_int32_t)0xffffffff || eptr == s + *off)
545f8307e12SArchie Cobbs 		return (EINVAL);
546f8307e12SArchie Cobbs 	*off = eptr - s;
547f8307e12SArchie Cobbs 	val32 = (int32_t)val;
548f8307e12SArchie Cobbs 	bcopy(&val32, buf, sizeof(int32_t));
549f8307e12SArchie Cobbs 	*buflen = sizeof(int32_t);
550f8307e12SArchie Cobbs 	return (0);
551f8307e12SArchie Cobbs }
552f8307e12SArchie Cobbs 
553f8307e12SArchie Cobbs static int
554f8307e12SArchie Cobbs ng_int32_unparse(const struct ng_parse_type *type,
555f8307e12SArchie Cobbs 	const u_char *data, int *off, char *cbuf, int cbuflen)
556f8307e12SArchie Cobbs {
5578db3c6cdSArchie Cobbs 	const char *fmt;
5588db3c6cdSArchie Cobbs 	long fval;
559f627a379SGleb Smirnoff 	int error;
560f8307e12SArchie Cobbs 	int32_t val;
561f8307e12SArchie Cobbs 
562f8307e12SArchie Cobbs 	bcopy(data + *off, &val, sizeof(int32_t));
5638214d60eSJohn Baldwin 	switch ((intptr_t)type->info) {
5648db3c6cdSArchie Cobbs 	case INT_SIGNED:
5658db3c6cdSArchie Cobbs 		fmt = "%ld";
5668db3c6cdSArchie Cobbs 		fval = val;
5678db3c6cdSArchie Cobbs 		break;
5688db3c6cdSArchie Cobbs 	case INT_UNSIGNED:
5698db3c6cdSArchie Cobbs 		fmt = "%lu";
5708db3c6cdSArchie Cobbs 		fval = (u_int32_t)val;
5718db3c6cdSArchie Cobbs 		break;
5728db3c6cdSArchie Cobbs 	case INT_HEX:
5738db3c6cdSArchie Cobbs 		fmt = "0x%lx";
5748db3c6cdSArchie Cobbs 		fval = (u_int32_t)val;
5758db3c6cdSArchie Cobbs 		break;
5768db3c6cdSArchie Cobbs 	default:
5776e551fb6SDavid E. O'Brien 		panic("%s: unknown type", __func__);
578b40ce416SJulian Elischer #ifdef	RESTARTABLE_PANICS
579b40ce416SJulian Elischer 		return(0);
580b40ce416SJulian Elischer #endif
5818db3c6cdSArchie Cobbs 	}
582f627a379SGleb Smirnoff 	if ((error = ng_parse_append(&cbuf, &cbuflen, fmt, fval)) != 0)
583f627a379SGleb Smirnoff 		return (error);
584f8307e12SArchie Cobbs 	*off += sizeof(int32_t);
585f8307e12SArchie Cobbs 	return (0);
586f8307e12SArchie Cobbs }
587f8307e12SArchie Cobbs 
588f8307e12SArchie Cobbs static int
589f8307e12SArchie Cobbs ng_int32_getDefault(const struct ng_parse_type *type,
590f8307e12SArchie Cobbs 	const u_char *const start, u_char *buf, int *buflen)
591f8307e12SArchie Cobbs {
592f8307e12SArchie Cobbs 	int32_t val;
593f8307e12SArchie Cobbs 
594f8307e12SArchie Cobbs 	if (*buflen < sizeof(int32_t))
595f8307e12SArchie Cobbs 		return (ERANGE);
596f8307e12SArchie Cobbs 	val = 0;
597f8307e12SArchie Cobbs 	bcopy(&val, buf, sizeof(int32_t));
598f8307e12SArchie Cobbs 	*buflen = sizeof(int32_t);
599f8307e12SArchie Cobbs 	return (0);
600f8307e12SArchie Cobbs }
601f8307e12SArchie Cobbs 
602f8307e12SArchie Cobbs static int
603f8307e12SArchie Cobbs ng_int32_getAlign(const struct ng_parse_type *type)
604f8307e12SArchie Cobbs {
605f8307e12SArchie Cobbs 	return INT32_ALIGNMENT;
606f8307e12SArchie Cobbs }
607f8307e12SArchie Cobbs 
608f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_int32_type = {
609f8307e12SArchie Cobbs 	NULL,
6108db3c6cdSArchie Cobbs 	(void *)INT_SIGNED,
611f8307e12SArchie Cobbs 	NULL,
612f8307e12SArchie Cobbs 	ng_int32_parse,
613f8307e12SArchie Cobbs 	ng_int32_unparse,
614f8307e12SArchie Cobbs 	ng_int32_getDefault,
615f8307e12SArchie Cobbs 	ng_int32_getAlign
616f8307e12SArchie Cobbs };
617f8307e12SArchie Cobbs 
6188db3c6cdSArchie Cobbs const struct ng_parse_type ng_parse_uint32_type = {
6198db3c6cdSArchie Cobbs 	&ng_parse_int32_type,
6208db3c6cdSArchie Cobbs 	(void *)INT_UNSIGNED
6218db3c6cdSArchie Cobbs };
6228db3c6cdSArchie Cobbs 
6238db3c6cdSArchie Cobbs const struct ng_parse_type ng_parse_hint32_type = {
6248db3c6cdSArchie Cobbs 	&ng_parse_int32_type,
6258db3c6cdSArchie Cobbs 	(void *)INT_HEX
6268db3c6cdSArchie Cobbs };
6278db3c6cdSArchie Cobbs 
628f8307e12SArchie Cobbs /************************************************************************
629f8307e12SArchie Cobbs 				INT64 TYPE
630f8307e12SArchie Cobbs  ************************************************************************/
631f8307e12SArchie Cobbs 
632f8307e12SArchie Cobbs static int
633f8307e12SArchie Cobbs ng_int64_parse(const struct ng_parse_type *type,
634f8307e12SArchie Cobbs 	const char *s, int *off, const u_char *const start,
635f8307e12SArchie Cobbs 	u_char *const buf, int *buflen)
636f8307e12SArchie Cobbs {
637f8307e12SArchie Cobbs 	quad_t val;
638f8307e12SArchie Cobbs 	int64_t val64;
639f8307e12SArchie Cobbs 	char *eptr;
640f8307e12SArchie Cobbs 
641f8307e12SArchie Cobbs 	val = strtoq(s + *off, &eptr, 0);
642f8307e12SArchie Cobbs 	if (eptr == s + *off)
643f8307e12SArchie Cobbs 		return (EINVAL);
644f8307e12SArchie Cobbs 	*off = eptr - s;
645f8307e12SArchie Cobbs 	val64 = (int64_t)val;
646f8307e12SArchie Cobbs 	bcopy(&val64, buf, sizeof(int64_t));
647f8307e12SArchie Cobbs 	*buflen = sizeof(int64_t);
648f8307e12SArchie Cobbs 	return (0);
649f8307e12SArchie Cobbs }
650f8307e12SArchie Cobbs 
651f8307e12SArchie Cobbs static int
652f8307e12SArchie Cobbs ng_int64_unparse(const struct ng_parse_type *type,
653f8307e12SArchie Cobbs 	const u_char *data, int *off, char *cbuf, int cbuflen)
654f8307e12SArchie Cobbs {
6558db3c6cdSArchie Cobbs 	const char *fmt;
6568db3c6cdSArchie Cobbs 	long long fval;
657f8307e12SArchie Cobbs 	int64_t val;
658f627a379SGleb Smirnoff 	int error;
659f8307e12SArchie Cobbs 
660f8307e12SArchie Cobbs 	bcopy(data + *off, &val, sizeof(int64_t));
6618214d60eSJohn Baldwin 	switch ((intptr_t)type->info) {
6628db3c6cdSArchie Cobbs 	case INT_SIGNED:
6638db3c6cdSArchie Cobbs 		fmt = "%lld";
6648db3c6cdSArchie Cobbs 		fval = val;
6658db3c6cdSArchie Cobbs 		break;
6668db3c6cdSArchie Cobbs 	case INT_UNSIGNED:
6678db3c6cdSArchie Cobbs 		fmt = "%llu";
6688db3c6cdSArchie Cobbs 		fval = (u_int64_t)val;
6698db3c6cdSArchie Cobbs 		break;
6708db3c6cdSArchie Cobbs 	case INT_HEX:
6718db3c6cdSArchie Cobbs 		fmt = "0x%llx";
6728db3c6cdSArchie Cobbs 		fval = (u_int64_t)val;
6738db3c6cdSArchie Cobbs 		break;
6748db3c6cdSArchie Cobbs 	default:
6756e551fb6SDavid E. O'Brien 		panic("%s: unknown type", __func__);
676b40ce416SJulian Elischer #ifdef	RESTARTABLE_PANICS
677b40ce416SJulian Elischer 		return(0);
678b40ce416SJulian Elischer #endif
6798db3c6cdSArchie Cobbs 	}
680f627a379SGleb Smirnoff 	if ((error = ng_parse_append(&cbuf, &cbuflen, fmt, fval)) != 0)
681f627a379SGleb Smirnoff 		return (error);
682f8307e12SArchie Cobbs 	*off += sizeof(int64_t);
683f8307e12SArchie Cobbs 	return (0);
684f8307e12SArchie Cobbs }
685f8307e12SArchie Cobbs 
686f8307e12SArchie Cobbs static int
687f8307e12SArchie Cobbs ng_int64_getDefault(const struct ng_parse_type *type,
688f8307e12SArchie Cobbs 	const u_char *const start, u_char *buf, int *buflen)
689f8307e12SArchie Cobbs {
690f8307e12SArchie Cobbs 	int64_t val;
691f8307e12SArchie Cobbs 
692f8307e12SArchie Cobbs 	if (*buflen < sizeof(int64_t))
693f8307e12SArchie Cobbs 		return (ERANGE);
694f8307e12SArchie Cobbs 	val = 0;
695f8307e12SArchie Cobbs 	bcopy(&val, buf, sizeof(int64_t));
696f8307e12SArchie Cobbs 	*buflen = sizeof(int64_t);
697f8307e12SArchie Cobbs 	return (0);
698f8307e12SArchie Cobbs }
699f8307e12SArchie Cobbs 
700f8307e12SArchie Cobbs static int
701f8307e12SArchie Cobbs ng_int64_getAlign(const struct ng_parse_type *type)
702f8307e12SArchie Cobbs {
703f8307e12SArchie Cobbs 	return INT64_ALIGNMENT;
704f8307e12SArchie Cobbs }
705f8307e12SArchie Cobbs 
706f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_int64_type = {
707f8307e12SArchie Cobbs 	NULL,
7088db3c6cdSArchie Cobbs 	(void *)INT_SIGNED,
709f8307e12SArchie Cobbs 	NULL,
710f8307e12SArchie Cobbs 	ng_int64_parse,
711f8307e12SArchie Cobbs 	ng_int64_unparse,
712f8307e12SArchie Cobbs 	ng_int64_getDefault,
713f8307e12SArchie Cobbs 	ng_int64_getAlign
714f8307e12SArchie Cobbs };
715f8307e12SArchie Cobbs 
7168db3c6cdSArchie Cobbs const struct ng_parse_type ng_parse_uint64_type = {
7178db3c6cdSArchie Cobbs 	&ng_parse_int64_type,
7188db3c6cdSArchie Cobbs 	(void *)INT_UNSIGNED
7198db3c6cdSArchie Cobbs };
7208db3c6cdSArchie Cobbs 
7218db3c6cdSArchie Cobbs const struct ng_parse_type ng_parse_hint64_type = {
7228db3c6cdSArchie Cobbs 	&ng_parse_int64_type,
7238db3c6cdSArchie Cobbs 	(void *)INT_HEX
7248db3c6cdSArchie Cobbs };
7258db3c6cdSArchie Cobbs 
726f8307e12SArchie Cobbs /************************************************************************
727f8307e12SArchie Cobbs 				STRING TYPE
728f8307e12SArchie Cobbs  ************************************************************************/
729f8307e12SArchie Cobbs 
730f8307e12SArchie Cobbs static int
731f8307e12SArchie Cobbs ng_string_parse(const struct ng_parse_type *type,
732f8307e12SArchie Cobbs 	const char *s, int *off, const u_char *const start,
733f8307e12SArchie Cobbs 	u_char *const buf, int *buflen)
734f8307e12SArchie Cobbs {
735f8307e12SArchie Cobbs 	char *sval;
736f8307e12SArchie Cobbs 	int len;
73727121ab1SBrian Somers 	int slen;
738f8307e12SArchie Cobbs 
73927121ab1SBrian Somers 	if ((sval = ng_get_string_token(s, off, &len, &slen)) == NULL)
740f8307e12SArchie Cobbs 		return (EINVAL);
741f8307e12SArchie Cobbs 	*off += len;
74227121ab1SBrian Somers 	bcopy(sval, buf, slen + 1);
7439c8c302fSJulian Elischer 	FREE(sval, M_NETGRAPH_PARSE);
74427121ab1SBrian Somers 	*buflen = slen + 1;
745f8307e12SArchie Cobbs 	return (0);
746f8307e12SArchie Cobbs }
747f8307e12SArchie Cobbs 
748f8307e12SArchie Cobbs static int
749f8307e12SArchie Cobbs ng_string_unparse(const struct ng_parse_type *type,
750f8307e12SArchie Cobbs 	const u_char *data, int *off, char *cbuf, int cbuflen)
751f8307e12SArchie Cobbs {
752f8307e12SArchie Cobbs 	const char *const raw = (const char *)data + *off;
75327121ab1SBrian Somers 	char *const s = ng_encode_string(raw, strlen(raw));
754f627a379SGleb Smirnoff 	int error;
755f8307e12SArchie Cobbs 
756f8307e12SArchie Cobbs 	if (s == NULL)
757f8307e12SArchie Cobbs 		return (ENOMEM);
758f627a379SGleb Smirnoff 	if ((error = ng_parse_append(&cbuf, &cbuflen, "%s", s)) != 0) {
759f627a379SGleb Smirnoff 		FREE(s, M_NETGRAPH_PARSE);
760f627a379SGleb Smirnoff 		return (error);
761f627a379SGleb Smirnoff 	}
762f8307e12SArchie Cobbs 	*off += strlen(raw) + 1;
7639c8c302fSJulian Elischer 	FREE(s, M_NETGRAPH_PARSE);
764f8307e12SArchie Cobbs 	return (0);
765f8307e12SArchie Cobbs }
766f8307e12SArchie Cobbs 
767f8307e12SArchie Cobbs static int
768f8307e12SArchie Cobbs ng_string_getDefault(const struct ng_parse_type *type,
769f8307e12SArchie Cobbs 	const u_char *const start, u_char *buf, int *buflen)
770f8307e12SArchie Cobbs {
771f8307e12SArchie Cobbs 
772f8307e12SArchie Cobbs 	if (*buflen < 1)
773f8307e12SArchie Cobbs 		return (ERANGE);
774f8307e12SArchie Cobbs 	buf[0] = (u_char)'\0';
775f8307e12SArchie Cobbs 	*buflen = 1;
776f8307e12SArchie Cobbs 	return (0);
777f8307e12SArchie Cobbs }
778f8307e12SArchie Cobbs 
779f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_string_type = {
780f8307e12SArchie Cobbs 	NULL,
781f8307e12SArchie Cobbs 	NULL,
782f8307e12SArchie Cobbs 	NULL,
783f8307e12SArchie Cobbs 	ng_string_parse,
784f8307e12SArchie Cobbs 	ng_string_unparse,
785f8307e12SArchie Cobbs 	ng_string_getDefault,
786f8307e12SArchie Cobbs 	NULL
787f8307e12SArchie Cobbs };
788f8307e12SArchie Cobbs 
789f8307e12SArchie Cobbs /************************************************************************
790f8307e12SArchie Cobbs 			FIXED BUFFER STRING TYPE
791f8307e12SArchie Cobbs  ************************************************************************/
792f8307e12SArchie Cobbs 
793f8307e12SArchie Cobbs static int
794f8307e12SArchie Cobbs ng_fixedstring_parse(const struct ng_parse_type *type,
795f8307e12SArchie Cobbs 	const char *s, int *off, const u_char *const start,
796f8307e12SArchie Cobbs 	u_char *const buf, int *buflen)
797f8307e12SArchie Cobbs {
798c1b9e5f2SArchie Cobbs 	const struct ng_parse_fixedstring_info *const fi = type->info;
799f8307e12SArchie Cobbs 	char *sval;
800f8307e12SArchie Cobbs 	int len;
80127121ab1SBrian Somers 	int slen;
802f8307e12SArchie Cobbs 
80327121ab1SBrian Somers 	if (slen + 1 > fi->bufSize)
804f8307e12SArchie Cobbs 		return (E2BIG);
805222c1141SGleb Smirnoff 	if ((sval = ng_get_string_token(s, off, &len, &slen)) == NULL)
806222c1141SGleb Smirnoff 		return (EINVAL);
807f8307e12SArchie Cobbs 	*off += len;
80827121ab1SBrian Somers 	bcopy(sval, buf, slen);
8099c8c302fSJulian Elischer 	FREE(sval, M_NETGRAPH_PARSE);
81027121ab1SBrian Somers 	bzero(buf + slen, fi->bufSize - slen);
811f8307e12SArchie Cobbs 	*buflen = fi->bufSize;
812f8307e12SArchie Cobbs 	return (0);
813f8307e12SArchie Cobbs }
814f8307e12SArchie Cobbs 
815f8307e12SArchie Cobbs static int
816f8307e12SArchie Cobbs ng_fixedstring_unparse(const struct ng_parse_type *type,
817f8307e12SArchie Cobbs 	const u_char *data, int *off, char *cbuf, int cbuflen)
818f8307e12SArchie Cobbs {
819c1b9e5f2SArchie Cobbs 	const struct ng_parse_fixedstring_info *const fi = type->info;
820f8307e12SArchie Cobbs 	int error, temp = *off;
821f8307e12SArchie Cobbs 
822f8307e12SArchie Cobbs 	if ((error = ng_string_unparse(type, data, &temp, cbuf, cbuflen)) != 0)
823f8307e12SArchie Cobbs 		return (error);
824f8307e12SArchie Cobbs 	*off += fi->bufSize;
825f8307e12SArchie Cobbs 	return (0);
826f8307e12SArchie Cobbs }
827f8307e12SArchie Cobbs 
828f8307e12SArchie Cobbs static int
829f8307e12SArchie Cobbs ng_fixedstring_getDefault(const struct ng_parse_type *type,
830f8307e12SArchie Cobbs 	const u_char *const start, u_char *buf, int *buflen)
831f8307e12SArchie Cobbs {
832c1b9e5f2SArchie Cobbs 	const struct ng_parse_fixedstring_info *const fi = type->info;
833f8307e12SArchie Cobbs 
834f8307e12SArchie Cobbs 	if (*buflen < fi->bufSize)
835f8307e12SArchie Cobbs 		return (ERANGE);
836f8307e12SArchie Cobbs 	bzero(buf, fi->bufSize);
837f8307e12SArchie Cobbs 	*buflen = fi->bufSize;
838f8307e12SArchie Cobbs 	return (0);
839f8307e12SArchie Cobbs }
840f8307e12SArchie Cobbs 
841f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_fixedstring_type = {
842f8307e12SArchie Cobbs 	NULL,
843f8307e12SArchie Cobbs 	NULL,
844f8307e12SArchie Cobbs 	NULL,
845f8307e12SArchie Cobbs 	ng_fixedstring_parse,
846f8307e12SArchie Cobbs 	ng_fixedstring_unparse,
847f8307e12SArchie Cobbs 	ng_fixedstring_getDefault,
848f8307e12SArchie Cobbs 	NULL
849f8307e12SArchie Cobbs };
850f8307e12SArchie Cobbs 
851c1b9e5f2SArchie Cobbs const struct ng_parse_fixedstring_info ng_parse_nodebuf_info = {
85287e2c66aSHartmut Brandt 	NG_NODESIZ
853f8307e12SArchie Cobbs };
854f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_nodebuf_type = {
855f8307e12SArchie Cobbs 	&ng_parse_fixedstring_type,
856f8307e12SArchie Cobbs 	&ng_parse_nodebuf_info
857f8307e12SArchie Cobbs };
858f8307e12SArchie Cobbs 
859c1b9e5f2SArchie Cobbs const struct ng_parse_fixedstring_info ng_parse_hookbuf_info = {
86087e2c66aSHartmut Brandt 	NG_HOOKSIZ
861f8307e12SArchie Cobbs };
862f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_hookbuf_type = {
863f8307e12SArchie Cobbs 	&ng_parse_fixedstring_type,
864f8307e12SArchie Cobbs 	&ng_parse_hookbuf_info
865f8307e12SArchie Cobbs };
866f8307e12SArchie Cobbs 
867c1b9e5f2SArchie Cobbs const struct ng_parse_fixedstring_info ng_parse_pathbuf_info = {
86887e2c66aSHartmut Brandt 	NG_PATHSIZ
869f8307e12SArchie Cobbs };
870f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_pathbuf_type = {
871f8307e12SArchie Cobbs 	&ng_parse_fixedstring_type,
872f8307e12SArchie Cobbs 	&ng_parse_pathbuf_info
873f8307e12SArchie Cobbs };
874f8307e12SArchie Cobbs 
875c1b9e5f2SArchie Cobbs const struct ng_parse_fixedstring_info ng_parse_typebuf_info = {
87687e2c66aSHartmut Brandt 	NG_TYPESIZ
877f8307e12SArchie Cobbs };
878f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_typebuf_type = {
879f8307e12SArchie Cobbs 	&ng_parse_fixedstring_type,
880f8307e12SArchie Cobbs 	&ng_parse_typebuf_info
881f8307e12SArchie Cobbs };
882f8307e12SArchie Cobbs 
883c1b9e5f2SArchie Cobbs const struct ng_parse_fixedstring_info ng_parse_cmdbuf_info = {
88487e2c66aSHartmut Brandt 	NG_CMDSTRSIZ
885f8307e12SArchie Cobbs };
886f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_cmdbuf_type = {
887f8307e12SArchie Cobbs 	&ng_parse_fixedstring_type,
888f8307e12SArchie Cobbs 	&ng_parse_cmdbuf_info
889f8307e12SArchie Cobbs };
890f8307e12SArchie Cobbs 
891f8307e12SArchie Cobbs /************************************************************************
89227121ab1SBrian Somers 			EXPLICITLY SIZED STRING TYPE
89327121ab1SBrian Somers  ************************************************************************/
89427121ab1SBrian Somers 
89527121ab1SBrian Somers static int
89627121ab1SBrian Somers ng_sizedstring_parse(const struct ng_parse_type *type,
89727121ab1SBrian Somers 	const char *s, int *off, const u_char *const start,
89827121ab1SBrian Somers 	u_char *const buf, int *buflen)
89927121ab1SBrian Somers {
90027121ab1SBrian Somers 	char *sval;
90127121ab1SBrian Somers 	int len;
90227121ab1SBrian Somers 	int slen;
90327121ab1SBrian Somers 
904222c1141SGleb Smirnoff 	if (slen > USHRT_MAX)
90527121ab1SBrian Somers 		return (EINVAL);
906222c1141SGleb Smirnoff 	if ((sval = ng_get_string_token(s, off, &len, &slen)) == NULL)
90727121ab1SBrian Somers 		return (EINVAL);
90827121ab1SBrian Somers 	*off += len;
90927121ab1SBrian Somers 	*((u_int16_t *)buf) = (u_int16_t)slen;
91027121ab1SBrian Somers 	bcopy(sval, buf + 2, slen);
9119c8c302fSJulian Elischer 	FREE(sval, M_NETGRAPH_PARSE);
91227121ab1SBrian Somers 	*buflen = 2 + slen;
91327121ab1SBrian Somers 	return (0);
91427121ab1SBrian Somers }
91527121ab1SBrian Somers 
91627121ab1SBrian Somers static int
91727121ab1SBrian Somers ng_sizedstring_unparse(const struct ng_parse_type *type,
91827121ab1SBrian Somers 	const u_char *data, int *off, char *cbuf, int cbuflen)
91927121ab1SBrian Somers {
92027121ab1SBrian Somers 	const char *const raw = (const char *)data + *off + 2;
92127121ab1SBrian Somers 	const int slen = *((const u_int16_t *)(data + *off));
92227121ab1SBrian Somers 	char *const s = ng_encode_string(raw, slen);
923f627a379SGleb Smirnoff 	int error;
92427121ab1SBrian Somers 
92527121ab1SBrian Somers 	if (s == NULL)
92627121ab1SBrian Somers 		return (ENOMEM);
927f627a379SGleb Smirnoff 	if ((error = ng_parse_append(&cbuf, &cbuflen, "%s", s)) != 0) {
928f627a379SGleb Smirnoff 		FREE(s, M_NETGRAPH_PARSE);
929f627a379SGleb Smirnoff 		return (error);
930f627a379SGleb Smirnoff 	}
9319c8c302fSJulian Elischer 	FREE(s, M_NETGRAPH_PARSE);
93227121ab1SBrian Somers 	*off += slen + 2;
93327121ab1SBrian Somers 	return (0);
93427121ab1SBrian Somers }
93527121ab1SBrian Somers 
93627121ab1SBrian Somers static int
93727121ab1SBrian Somers ng_sizedstring_getDefault(const struct ng_parse_type *type,
93827121ab1SBrian Somers 	const u_char *const start, u_char *buf, int *buflen)
93927121ab1SBrian Somers {
94027121ab1SBrian Somers 	if (*buflen < 2)
94127121ab1SBrian Somers 		return (ERANGE);
94227121ab1SBrian Somers 	bzero(buf, 2);
94327121ab1SBrian Somers 	*buflen = 2;
94427121ab1SBrian Somers 	return (0);
94527121ab1SBrian Somers }
94627121ab1SBrian Somers 
94727121ab1SBrian Somers const struct ng_parse_type ng_parse_sizedstring_type = {
94827121ab1SBrian Somers 	NULL,
94927121ab1SBrian Somers 	NULL,
95027121ab1SBrian Somers 	NULL,
95127121ab1SBrian Somers 	ng_sizedstring_parse,
95227121ab1SBrian Somers 	ng_sizedstring_unparse,
95327121ab1SBrian Somers 	ng_sizedstring_getDefault,
95427121ab1SBrian Somers 	NULL
95527121ab1SBrian Somers };
95627121ab1SBrian Somers 
95727121ab1SBrian Somers /************************************************************************
958f8307e12SArchie Cobbs 			IP ADDRESS TYPE
959f8307e12SArchie Cobbs  ************************************************************************/
960f8307e12SArchie Cobbs 
961f8307e12SArchie Cobbs static int
962f8307e12SArchie Cobbs ng_ipaddr_parse(const struct ng_parse_type *type,
963f8307e12SArchie Cobbs 	const char *s, int *off, const u_char *const start,
964f8307e12SArchie Cobbs 	u_char *const buf, int *buflen)
965f8307e12SArchie Cobbs {
966f8307e12SArchie Cobbs 	int i, error;
967f8307e12SArchie Cobbs 
968f8307e12SArchie Cobbs 	for (i = 0; i < 4; i++) {
969f8307e12SArchie Cobbs 		if ((error = ng_int8_parse(&ng_parse_int8_type,
970f8307e12SArchie Cobbs 		    s, off, start, buf + i, buflen)) != 0)
971f8307e12SArchie Cobbs 			return (error);
972f8307e12SArchie Cobbs 		if (i < 3 && s[*off] != '.')
973f8307e12SArchie Cobbs 			return (EINVAL);
974f8307e12SArchie Cobbs 		(*off)++;
975f8307e12SArchie Cobbs 	}
976f8307e12SArchie Cobbs 	*buflen = 4;
977f8307e12SArchie Cobbs 	return (0);
978f8307e12SArchie Cobbs }
979f8307e12SArchie Cobbs 
980f8307e12SArchie Cobbs static int
981f8307e12SArchie Cobbs ng_ipaddr_unparse(const struct ng_parse_type *type,
982f8307e12SArchie Cobbs 	const u_char *data, int *off, char *cbuf, int cbuflen)
983f8307e12SArchie Cobbs {
984f8307e12SArchie Cobbs 	struct in_addr ip;
985f627a379SGleb Smirnoff 	int error;
986f8307e12SArchie Cobbs 
987f8307e12SArchie Cobbs 	bcopy(data + *off, &ip, sizeof(ip));
988f627a379SGleb Smirnoff 	if ((error = ng_parse_append(&cbuf, &cbuflen, "%d.%d.%d.%d",
989f627a379SGleb Smirnoff 	    ((u_char *)&ip)[0], ((u_char *)&ip)[1],
990f627a379SGleb Smirnoff 	    ((u_char *)&ip)[2], ((u_char *)&ip)[3])) != 0)
991f627a379SGleb Smirnoff 		return (error);
992f8307e12SArchie Cobbs 	*off += sizeof(ip);
993f8307e12SArchie Cobbs 	return (0);
994f8307e12SArchie Cobbs }
995f8307e12SArchie Cobbs 
996f8307e12SArchie Cobbs static int
997f8307e12SArchie Cobbs ng_ipaddr_getDefault(const struct ng_parse_type *type,
998f8307e12SArchie Cobbs 	const u_char *const start, u_char *buf, int *buflen)
999f8307e12SArchie Cobbs {
1000f8307e12SArchie Cobbs 	struct in_addr ip = { 0 };
1001f8307e12SArchie Cobbs 
1002f8307e12SArchie Cobbs 	if (*buflen < sizeof(ip))
1003f8307e12SArchie Cobbs 		return (ERANGE);
1004f8307e12SArchie Cobbs 	bcopy(&ip, buf, sizeof(ip));
1005f8307e12SArchie Cobbs 	*buflen = sizeof(ip);
1006f8307e12SArchie Cobbs 	return (0);
1007f8307e12SArchie Cobbs }
1008f8307e12SArchie Cobbs 
1009f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_ipaddr_type = {
1010f8307e12SArchie Cobbs 	NULL,
1011f8307e12SArchie Cobbs 	NULL,
1012f8307e12SArchie Cobbs 	NULL,
1013f8307e12SArchie Cobbs 	ng_ipaddr_parse,
1014f8307e12SArchie Cobbs 	ng_ipaddr_unparse,
1015f8307e12SArchie Cobbs 	ng_ipaddr_getDefault,
1016f8307e12SArchie Cobbs 	ng_int32_getAlign
1017f8307e12SArchie Cobbs };
1018f8307e12SArchie Cobbs 
1019f8307e12SArchie Cobbs /************************************************************************
10208c7e4101SRuslan Ermilov 			ETHERNET ADDRESS TYPE
10218c7e4101SRuslan Ermilov  ************************************************************************/
10228c7e4101SRuslan Ermilov 
10238c7e4101SRuslan Ermilov static int
10248c7e4101SRuslan Ermilov ng_enaddr_parse(const struct ng_parse_type *type,
10258c7e4101SRuslan Ermilov 	const char *s, int *const off, const u_char *const start,
10268c7e4101SRuslan Ermilov 	u_char *const buf, int *const buflen)
10278c7e4101SRuslan Ermilov {
10288c7e4101SRuslan Ermilov 	char *eptr;
10298c7e4101SRuslan Ermilov 	u_long val;
10308c7e4101SRuslan Ermilov 	int i;
10318c7e4101SRuslan Ermilov 
10328c7e4101SRuslan Ermilov 	if (*buflen < ETHER_ADDR_LEN)
10338c7e4101SRuslan Ermilov 		return (ERANGE);
10348c7e4101SRuslan Ermilov 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
10358c7e4101SRuslan Ermilov 		val = strtoul(s + *off, &eptr, 16);
10368c7e4101SRuslan Ermilov 		if (val > 0xff || eptr == s + *off)
10378c7e4101SRuslan Ermilov 			return (EINVAL);
10388c7e4101SRuslan Ermilov 		buf[i] = (u_char)val;
10398c7e4101SRuslan Ermilov 		*off = (eptr - s);
10408c7e4101SRuslan Ermilov 		if (i < ETHER_ADDR_LEN - 1) {
10418c7e4101SRuslan Ermilov 			if (*eptr != ':')
10428c7e4101SRuslan Ermilov 				return (EINVAL);
10438c7e4101SRuslan Ermilov 			(*off)++;
10448c7e4101SRuslan Ermilov 		}
10458c7e4101SRuslan Ermilov 	}
10468c7e4101SRuslan Ermilov 	*buflen = ETHER_ADDR_LEN;
10478c7e4101SRuslan Ermilov 	return (0);
10488c7e4101SRuslan Ermilov }
10498c7e4101SRuslan Ermilov 
10508c7e4101SRuslan Ermilov static int
10518c7e4101SRuslan Ermilov ng_enaddr_unparse(const struct ng_parse_type *type,
10528c7e4101SRuslan Ermilov 	const u_char *data, int *off, char *cbuf, int cbuflen)
10538c7e4101SRuslan Ermilov {
10548c7e4101SRuslan Ermilov 	int len;
10558c7e4101SRuslan Ermilov 
10568c7e4101SRuslan Ermilov 	len = snprintf(cbuf, cbuflen, "%02x:%02x:%02x:%02x:%02x:%02x",
10578c7e4101SRuslan Ermilov 	    data[*off], data[*off + 1], data[*off + 2],
10588c7e4101SRuslan Ermilov 	    data[*off + 3], data[*off + 4], data[*off + 5]);
10598c7e4101SRuslan Ermilov 	if (len >= cbuflen)
10608c7e4101SRuslan Ermilov 		return (ERANGE);
10618c7e4101SRuslan Ermilov 	*off += ETHER_ADDR_LEN;
10628c7e4101SRuslan Ermilov 	return (0);
10638c7e4101SRuslan Ermilov }
10648c7e4101SRuslan Ermilov 
10658c7e4101SRuslan Ermilov const struct ng_parse_type ng_parse_enaddr_type = {
10668c7e4101SRuslan Ermilov 	NULL,
10678c7e4101SRuslan Ermilov 	NULL,
10688c7e4101SRuslan Ermilov 	NULL,
10698c7e4101SRuslan Ermilov 	ng_enaddr_parse,
10708c7e4101SRuslan Ermilov 	ng_enaddr_unparse,
10718c7e4101SRuslan Ermilov 	NULL,
10728c7e4101SRuslan Ermilov 	0
10738c7e4101SRuslan Ermilov };
10748c7e4101SRuslan Ermilov 
10758c7e4101SRuslan Ermilov /************************************************************************
1076f8307e12SArchie Cobbs 			BYTE ARRAY TYPE
1077f8307e12SArchie Cobbs  ************************************************************************/
1078f8307e12SArchie Cobbs 
1079f8307e12SArchie Cobbs /* Get the length of a byte array */
1080f8307e12SArchie Cobbs static int
1081f8307e12SArchie Cobbs ng_parse_bytearray_subtype_getLength(const struct ng_parse_type *type,
1082f8307e12SArchie Cobbs 	const u_char *start, const u_char *buf)
1083f8307e12SArchie Cobbs {
1084f8307e12SArchie Cobbs 	ng_parse_array_getLength_t *const getLength = type->private;
1085f8307e12SArchie Cobbs 
1086f8307e12SArchie Cobbs 	return (*getLength)(type, start, buf);
1087f8307e12SArchie Cobbs }
1088f8307e12SArchie Cobbs 
10898db3c6cdSArchie Cobbs /* Byte array element type is hex int8 */
1090f8307e12SArchie Cobbs static const struct ng_parse_array_info ng_parse_bytearray_subtype_info = {
10918db3c6cdSArchie Cobbs 	&ng_parse_hint8_type,
1092f8307e12SArchie Cobbs 	&ng_parse_bytearray_subtype_getLength,
1093f8307e12SArchie Cobbs 	NULL
1094f8307e12SArchie Cobbs };
1095f8307e12SArchie Cobbs static const struct ng_parse_type ng_parse_bytearray_subtype = {
1096f8307e12SArchie Cobbs 	&ng_parse_array_type,
1097f8307e12SArchie Cobbs 	&ng_parse_bytearray_subtype_info
1098f8307e12SArchie Cobbs };
1099f8307e12SArchie Cobbs 
1100f8307e12SArchie Cobbs static int
1101f8307e12SArchie Cobbs ng_bytearray_parse(const struct ng_parse_type *type,
1102f8307e12SArchie Cobbs 	const char *s, int *off, const u_char *const start,
1103f8307e12SArchie Cobbs 	u_char *const buf, int *buflen)
1104f8307e12SArchie Cobbs {
1105f8307e12SArchie Cobbs 	char *str;
1106f8307e12SArchie Cobbs 	int toklen;
110727121ab1SBrian Somers 	int slen;
1108f8307e12SArchie Cobbs 
1109f8307e12SArchie Cobbs 	/* We accept either an array of bytes or a string constant */
111027121ab1SBrian Somers 	if ((str = ng_get_string_token(s, off, &toklen, &slen)) != NULL) {
1111f8307e12SArchie Cobbs 		ng_parse_array_getLength_t *const getLength = type->info;
111227121ab1SBrian Somers 		int arraylen;
1113f8307e12SArchie Cobbs 
1114f8307e12SArchie Cobbs 		arraylen = (*getLength)(type, start, buf);
1115f8307e12SArchie Cobbs 		if (arraylen > *buflen) {
11169c8c302fSJulian Elischer 			FREE(str, M_NETGRAPH_PARSE);
1117f8307e12SArchie Cobbs 			return (ERANGE);
1118f8307e12SArchie Cobbs 		}
1119f8307e12SArchie Cobbs 		if (slen > arraylen) {
11209c8c302fSJulian Elischer 			FREE(str, M_NETGRAPH_PARSE);
1121f8307e12SArchie Cobbs 			return (E2BIG);
1122f8307e12SArchie Cobbs 		}
1123f8307e12SArchie Cobbs 		bcopy(str, buf, slen);
1124f8307e12SArchie Cobbs 		bzero(buf + slen, arraylen - slen);
11259c8c302fSJulian Elischer 		FREE(str, M_NETGRAPH_PARSE);
1126f8307e12SArchie Cobbs 		*off += toklen;
1127f8307e12SArchie Cobbs 		*buflen = arraylen;
1128f8307e12SArchie Cobbs 		return (0);
1129f8307e12SArchie Cobbs 	} else {
1130f8307e12SArchie Cobbs 		struct ng_parse_type subtype;
1131f8307e12SArchie Cobbs 
1132f8307e12SArchie Cobbs 		subtype = ng_parse_bytearray_subtype;
1133445e045bSAlexander Kabaev 		*(const void **)&subtype.private = type->info;
1134f8307e12SArchie Cobbs 		return ng_array_parse(&subtype, s, off, start, buf, buflen);
1135f8307e12SArchie Cobbs 	}
1136f8307e12SArchie Cobbs }
1137f8307e12SArchie Cobbs 
1138f8307e12SArchie Cobbs static int
1139f8307e12SArchie Cobbs ng_bytearray_unparse(const struct ng_parse_type *type,
1140f8307e12SArchie Cobbs 	const u_char *data, int *off, char *cbuf, int cbuflen)
1141f8307e12SArchie Cobbs {
1142f8307e12SArchie Cobbs 	struct ng_parse_type subtype;
1143f8307e12SArchie Cobbs 
1144f8307e12SArchie Cobbs 	subtype = ng_parse_bytearray_subtype;
1145445e045bSAlexander Kabaev 	*(const void **)&subtype.private = type->info;
1146f8307e12SArchie Cobbs 	return ng_array_unparse(&subtype, data, off, cbuf, cbuflen);
1147f8307e12SArchie Cobbs }
1148f8307e12SArchie Cobbs 
1149f8307e12SArchie Cobbs static int
1150f8307e12SArchie Cobbs ng_bytearray_getDefault(const struct ng_parse_type *type,
1151f8307e12SArchie Cobbs 	const u_char *const start, u_char *buf, int *buflen)
1152f8307e12SArchie Cobbs {
1153f8307e12SArchie Cobbs 	struct ng_parse_type subtype;
1154f8307e12SArchie Cobbs 
1155f8307e12SArchie Cobbs 	subtype = ng_parse_bytearray_subtype;
1156445e045bSAlexander Kabaev 	*(const void **)&subtype.private = type->info;
1157f8307e12SArchie Cobbs 	return ng_array_getDefault(&subtype, start, buf, buflen);
1158f8307e12SArchie Cobbs }
1159f8307e12SArchie Cobbs 
1160f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_bytearray_type = {
1161f8307e12SArchie Cobbs 	NULL,
1162f8307e12SArchie Cobbs 	NULL,
1163f8307e12SArchie Cobbs 	NULL,
1164f8307e12SArchie Cobbs 	ng_bytearray_parse,
1165f8307e12SArchie Cobbs 	ng_bytearray_unparse,
1166f8307e12SArchie Cobbs 	ng_bytearray_getDefault,
1167f8307e12SArchie Cobbs 	NULL
1168f8307e12SArchie Cobbs };
1169f8307e12SArchie Cobbs 
1170f8307e12SArchie Cobbs /************************************************************************
1171f8307e12SArchie Cobbs 			STRUCT NG_MESG TYPE
1172f8307e12SArchie Cobbs  ************************************************************************/
1173f8307e12SArchie Cobbs 
1174f8307e12SArchie Cobbs /* Get msg->header.arglen when "buf" is pointing to msg->data */
1175f8307e12SArchie Cobbs static int
1176f8307e12SArchie Cobbs ng_parse_ng_mesg_getLength(const struct ng_parse_type *type,
1177f8307e12SArchie Cobbs 	const u_char *start, const u_char *buf)
1178f8307e12SArchie Cobbs {
1179f8307e12SArchie Cobbs 	const struct ng_mesg *msg;
1180f8307e12SArchie Cobbs 
1181f8307e12SArchie Cobbs 	msg = (const struct ng_mesg *)(buf - sizeof(*msg));
1182f8307e12SArchie Cobbs 	return msg->header.arglen;
1183f8307e12SArchie Cobbs }
1184f8307e12SArchie Cobbs 
1185f8307e12SArchie Cobbs /* Type for the variable length data portion of a struct ng_mesg */
1186f8307e12SArchie Cobbs static const struct ng_parse_type ng_msg_data_type = {
1187f8307e12SArchie Cobbs 	&ng_parse_bytearray_type,
1188f8307e12SArchie Cobbs 	&ng_parse_ng_mesg_getLength
1189f8307e12SArchie Cobbs };
1190f8307e12SArchie Cobbs 
1191f8307e12SArchie Cobbs /* Type for the entire struct ng_mesg header with data section */
1192f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_parse_ng_mesg_type_fields[]
1193f0184ff8SArchie Cobbs 	= NG_GENERIC_NG_MESG_INFO(&ng_msg_data_type);
1194f8307e12SArchie Cobbs const struct ng_parse_type ng_parse_ng_mesg_type = {
1195f8307e12SArchie Cobbs 	&ng_parse_struct_type,
1196f0184ff8SArchie Cobbs 	&ng_parse_ng_mesg_type_fields,
1197f8307e12SArchie Cobbs };
1198f8307e12SArchie Cobbs 
1199f8307e12SArchie Cobbs /************************************************************************
1200f8307e12SArchie Cobbs 			COMPOSITE HELPER ROUTINES
1201f8307e12SArchie Cobbs  ************************************************************************/
1202f8307e12SArchie Cobbs 
1203f8307e12SArchie Cobbs /*
1204f8307e12SArchie Cobbs  * Convert a structure or array from ASCII to binary
1205f8307e12SArchie Cobbs  */
1206f8307e12SArchie Cobbs static int
1207f8307e12SArchie Cobbs ng_parse_composite(const struct ng_parse_type *type, const char *s,
1208f8307e12SArchie Cobbs 	int *off, const u_char *const start, u_char *const buf, int *buflen,
1209f8307e12SArchie Cobbs 	const enum comptype ctype)
1210f8307e12SArchie Cobbs {
1211f8307e12SArchie Cobbs 	const int num = ng_get_composite_len(type, start, buf, ctype);
1212f8307e12SArchie Cobbs 	int nextIndex = 0;		/* next implicit array index */
1213f8307e12SArchie Cobbs 	u_int index;			/* field or element index */
1214f8307e12SArchie Cobbs 	int *foff;			/* field value offsets in string */
1215f8307e12SArchie Cobbs 	int align, len, blen, error = 0;
1216f8307e12SArchie Cobbs 
1217f8307e12SArchie Cobbs 	/* Initialize */
12189c8c302fSJulian Elischer 	MALLOC(foff, int *, num * sizeof(*foff), M_NETGRAPH_PARSE, M_NOWAIT | M_ZERO);
1219f8307e12SArchie Cobbs 	if (foff == NULL) {
1220f8307e12SArchie Cobbs 		error = ENOMEM;
1221f8307e12SArchie Cobbs 		goto done;
1222f8307e12SArchie Cobbs 	}
1223f8307e12SArchie Cobbs 
1224f8307e12SArchie Cobbs 	/* Get opening brace/bracket */
1225f8307e12SArchie Cobbs 	if (ng_parse_get_token(s, off, &len)
1226f8307e12SArchie Cobbs 	    != (ctype == CT_STRUCT ? T_LBRACE : T_LBRACKET)) {
1227f8307e12SArchie Cobbs 		error = EINVAL;
1228f8307e12SArchie Cobbs 		goto done;
1229f8307e12SArchie Cobbs 	}
1230f8307e12SArchie Cobbs 	*off += len;
1231f8307e12SArchie Cobbs 
1232f8307e12SArchie Cobbs 	/* Get individual element value positions in the string */
1233f8307e12SArchie Cobbs 	for (;;) {
1234f8307e12SArchie Cobbs 		enum ng_parse_token tok;
1235f8307e12SArchie Cobbs 
1236f8307e12SArchie Cobbs 		/* Check for closing brace/bracket */
1237f8307e12SArchie Cobbs 		tok = ng_parse_get_token(s, off, &len);
1238f8307e12SArchie Cobbs 		if (tok == (ctype == CT_STRUCT ? T_RBRACE : T_RBRACKET)) {
1239f8307e12SArchie Cobbs 			*off += len;
1240f8307e12SArchie Cobbs 			break;
1241f8307e12SArchie Cobbs 		}
1242f8307e12SArchie Cobbs 
1243f8307e12SArchie Cobbs 		/* For arrays, the 'name' (ie, index) is optional, so
1244f8307e12SArchie Cobbs 		   distinguish name from values by seeing if the next
1245f8307e12SArchie Cobbs 		   token is an equals sign */
1246f8307e12SArchie Cobbs 		if (ctype != CT_STRUCT) {
1247f8307e12SArchie Cobbs 			int len2, off2;
1248f8307e12SArchie Cobbs 			char *eptr;
1249f8307e12SArchie Cobbs 
1250f8307e12SArchie Cobbs 			/* If an opening brace/bracket, index is implied */
1251f8307e12SArchie Cobbs 			if (tok == T_LBRACE || tok == T_LBRACKET) {
1252f8307e12SArchie Cobbs 				index = nextIndex++;
1253f8307e12SArchie Cobbs 				goto gotIndex;
1254f8307e12SArchie Cobbs 			}
1255f8307e12SArchie Cobbs 
1256f8307e12SArchie Cobbs 			/* Might be an index, might be a value, either way... */
1257f8307e12SArchie Cobbs 			if (tok != T_WORD) {
1258f8307e12SArchie Cobbs 				error = EINVAL;
1259f8307e12SArchie Cobbs 				goto done;
1260f8307e12SArchie Cobbs 			}
1261f8307e12SArchie Cobbs 
1262f8307e12SArchie Cobbs 			/* If no equals sign follows, index is implied */
1263f8307e12SArchie Cobbs 			off2 = *off + len;
1264f8307e12SArchie Cobbs 			if (ng_parse_get_token(s, &off2, &len2) != T_EQUALS) {
1265f8307e12SArchie Cobbs 				index = nextIndex++;
1266f8307e12SArchie Cobbs 				goto gotIndex;
1267f8307e12SArchie Cobbs 			}
1268f8307e12SArchie Cobbs 
1269f8307e12SArchie Cobbs 			/* Index was specified explicitly; parse it */
1270f8307e12SArchie Cobbs 			index = (u_int)strtoul(s + *off, &eptr, 0);
1271f8307e12SArchie Cobbs 			if (index < 0 || eptr - (s + *off) != len) {
1272f8307e12SArchie Cobbs 				error = EINVAL;
1273f8307e12SArchie Cobbs 				goto done;
1274f8307e12SArchie Cobbs 			}
1275f8307e12SArchie Cobbs 			nextIndex = index + 1;
1276f8307e12SArchie Cobbs 			*off += len + len2;
1277f8307e12SArchie Cobbs 		} else {			/* a structure field */
1278f0184ff8SArchie Cobbs 			const struct ng_parse_struct_field *const
1279f0184ff8SArchie Cobbs 			    fields = type->info;
1280f8307e12SArchie Cobbs 
1281f8307e12SArchie Cobbs 			/* Find the field by name (required) in field list */
1282f8307e12SArchie Cobbs 			if (tok != T_WORD) {
1283f8307e12SArchie Cobbs 				error = EINVAL;
1284f8307e12SArchie Cobbs 				goto done;
1285f8307e12SArchie Cobbs 			}
1286f8307e12SArchie Cobbs 			for (index = 0; index < num; index++) {
1287f0184ff8SArchie Cobbs 				const struct ng_parse_struct_field *const
1288f0184ff8SArchie Cobbs 				    field = &fields[index];
1289f0184ff8SArchie Cobbs 
1290f8307e12SArchie Cobbs 				if (strncmp(&s[*off], field->name, len) == 0
1291f8307e12SArchie Cobbs 				    && field->name[len] == '\0')
1292f8307e12SArchie Cobbs 					break;
1293f8307e12SArchie Cobbs 			}
1294f8307e12SArchie Cobbs 			if (index == num) {
1295f8307e12SArchie Cobbs 				error = ENOENT;
1296f8307e12SArchie Cobbs 				goto done;
1297f8307e12SArchie Cobbs 			}
1298f8307e12SArchie Cobbs 			*off += len;
1299f8307e12SArchie Cobbs 
1300f8307e12SArchie Cobbs 			/* Get equals sign */
1301f8307e12SArchie Cobbs 			if (ng_parse_get_token(s, off, &len) != T_EQUALS) {
1302f8307e12SArchie Cobbs 				error = EINVAL;
1303f8307e12SArchie Cobbs 				goto done;
1304f8307e12SArchie Cobbs 			}
1305f8307e12SArchie Cobbs 			*off += len;
1306f8307e12SArchie Cobbs 		}
130755be04abSPeter Wemm gotIndex:
1308f8307e12SArchie Cobbs 
1309f8307e12SArchie Cobbs 		/* Check array index */
1310f8307e12SArchie Cobbs 		if (index >= num) {
1311f8307e12SArchie Cobbs 			error = E2BIG;
1312f8307e12SArchie Cobbs 			goto done;
1313f8307e12SArchie Cobbs 		}
1314f8307e12SArchie Cobbs 
1315f8307e12SArchie Cobbs 		/* Save value's position and skip over it for now */
1316f8307e12SArchie Cobbs 		if (foff[index] != 0) {
1317f8307e12SArchie Cobbs 			error = EALREADY;		/* duplicate */
1318f8307e12SArchie Cobbs 			goto done;
1319f8307e12SArchie Cobbs 		}
1320f8307e12SArchie Cobbs 		while (isspace(s[*off]))
1321f8307e12SArchie Cobbs 			(*off)++;
1322f8307e12SArchie Cobbs 		foff[index] = *off;
1323f8307e12SArchie Cobbs 		if ((error = ng_parse_skip_value(s, *off, &len)) != 0)
1324f8307e12SArchie Cobbs 			goto done;
1325f8307e12SArchie Cobbs 		*off += len;
1326f8307e12SArchie Cobbs 	}
1327f8307e12SArchie Cobbs 
1328f8307e12SArchie Cobbs 	/* Now build binary structure from supplied values and defaults */
1329f8307e12SArchie Cobbs 	for (blen = index = 0; index < num; index++) {
1330f8307e12SArchie Cobbs 		const struct ng_parse_type *const
1331f8307e12SArchie Cobbs 		    etype = ng_get_composite_etype(type, index, ctype);
1332f8307e12SArchie Cobbs 		int k, pad, vlen;
1333f8307e12SArchie Cobbs 
1334f8307e12SArchie Cobbs 		/* Zero-pad any alignment bytes */
1335f8307e12SArchie Cobbs 		pad = ng_parse_get_elem_pad(type, index, ctype, blen);
1336f8307e12SArchie Cobbs 		for (k = 0; k < pad; k++) {
1337f8307e12SArchie Cobbs 			if (blen >= *buflen) {
1338f8307e12SArchie Cobbs 				error = ERANGE;
1339f8307e12SArchie Cobbs 				goto done;
1340f8307e12SArchie Cobbs 			}
1341f8307e12SArchie Cobbs 			buf[blen++] = 0;
1342f8307e12SArchie Cobbs 		}
1343f8307e12SArchie Cobbs 
1344f8307e12SArchie Cobbs 		/* Get value */
1345f8307e12SArchie Cobbs 		vlen = *buflen - blen;
1346f8307e12SArchie Cobbs 		if (foff[index] == 0) {		/* use default value */
1347f8307e12SArchie Cobbs 			error = ng_get_composite_elem_default(type, index,
1348f8307e12SArchie Cobbs 			    start, buf + blen, &vlen, ctype);
1349f8307e12SArchie Cobbs 		} else {			/* parse given value */
1350f8307e12SArchie Cobbs 			*off = foff[index];
1351f8307e12SArchie Cobbs 			error = INVOKE(etype, parse)(etype,
1352f8307e12SArchie Cobbs 			    s, off, start, buf + blen, &vlen);
1353f8307e12SArchie Cobbs 		}
1354f8307e12SArchie Cobbs 		if (error != 0)
1355f8307e12SArchie Cobbs 			goto done;
1356f8307e12SArchie Cobbs 		blen += vlen;
1357f8307e12SArchie Cobbs 	}
1358f8307e12SArchie Cobbs 
1359f8307e12SArchie Cobbs 	/* Make total composite structure size a multiple of its alignment */
1360f8307e12SArchie Cobbs 	if ((align = ALIGNMENT(type)) != 0) {
1361f8307e12SArchie Cobbs 		while (blen % align != 0) {
1362f8307e12SArchie Cobbs 			if (blen >= *buflen) {
1363f8307e12SArchie Cobbs 				error = ERANGE;
1364f8307e12SArchie Cobbs 				goto done;
1365f8307e12SArchie Cobbs 			}
1366f8307e12SArchie Cobbs 			buf[blen++] = 0;
1367f8307e12SArchie Cobbs 		}
1368f8307e12SArchie Cobbs 	}
1369f8307e12SArchie Cobbs 
1370f8307e12SArchie Cobbs 	/* Done */
1371f8307e12SArchie Cobbs 	*buflen = blen;
1372f8307e12SArchie Cobbs done:
1373d99b0733SArchie Cobbs 	if (foff != NULL)
13749c8c302fSJulian Elischer 		FREE(foff, M_NETGRAPH_PARSE);
1375f8307e12SArchie Cobbs 	return (error);
1376f8307e12SArchie Cobbs }
1377f8307e12SArchie Cobbs 
1378f8307e12SArchie Cobbs /*
1379f8307e12SArchie Cobbs  * Convert an array or structure from binary to ASCII
1380f8307e12SArchie Cobbs  */
1381f8307e12SArchie Cobbs static int
1382f8307e12SArchie Cobbs ng_unparse_composite(const struct ng_parse_type *type, const u_char *data,
1383f8307e12SArchie Cobbs 	int *off, char *cbuf, int cbuflen, const enum comptype ctype)
1384f8307e12SArchie Cobbs {
1385d9bfecabSArchie Cobbs 	const struct ng_mesg *const hdr
1386d9bfecabSArchie Cobbs 	    = (const struct ng_mesg *)(data - sizeof(*hdr));
1387f8307e12SArchie Cobbs 	const int num = ng_get_composite_len(type, data, data + *off, ctype);
13888db3c6cdSArchie Cobbs 	const int workSize = 20 * 1024;		/* XXX hard coded constant */
1389f8307e12SArchie Cobbs 	int nextIndex = 0, didOne = 0;
1390f8307e12SArchie Cobbs 	int error, index;
13918db3c6cdSArchie Cobbs 	u_char *workBuf;
13928db3c6cdSArchie Cobbs 
13938db3c6cdSArchie Cobbs 	/* Get workspace for checking default values */
13949c8c302fSJulian Elischer 	MALLOC(workBuf, u_char *, workSize, M_NETGRAPH_PARSE, M_NOWAIT);
13958db3c6cdSArchie Cobbs 	if (workBuf == NULL)
13968db3c6cdSArchie Cobbs 		return (ENOMEM);
1397f8307e12SArchie Cobbs 
1398f8307e12SArchie Cobbs 	/* Opening brace/bracket */
1399f627a379SGleb Smirnoff 	if ((error = ng_parse_append(&cbuf, &cbuflen, "%c",
1400f627a379SGleb Smirnoff 	    (ctype == CT_STRUCT) ? '{' : '[')) != 0)
1401f627a379SGleb Smirnoff 		goto fail;
1402f8307e12SArchie Cobbs 
1403f8307e12SArchie Cobbs 	/* Do each item */
1404f8307e12SArchie Cobbs 	for (index = 0; index < num; index++) {
1405f8307e12SArchie Cobbs 		const struct ng_parse_type *const
1406f8307e12SArchie Cobbs 		    etype = ng_get_composite_etype(type, index, ctype);
1407f8307e12SArchie Cobbs 
1408f8307e12SArchie Cobbs 		/* Skip any alignment pad bytes */
1409f8307e12SArchie Cobbs 		*off += ng_parse_get_elem_pad(type, index, ctype, *off);
1410f8307e12SArchie Cobbs 
1411d9bfecabSArchie Cobbs 		/*
1412d9bfecabSArchie Cobbs 		 * See if element is equal to its default value; skip if so.
1413d9bfecabSArchie Cobbs 		 * Copy struct ng_mesg header for types that peek into it.
1414d9bfecabSArchie Cobbs 		 */
1415d9bfecabSArchie Cobbs 		if (sizeof(*hdr) + *off < workSize) {
1416d9bfecabSArchie Cobbs 			int tempsize = workSize - sizeof(*hdr) - *off;
1417f8307e12SArchie Cobbs 
14183cbeb975SArchie Cobbs 			bcopy(hdr, workBuf, sizeof(*hdr) + *off);
1419d9bfecabSArchie Cobbs 			if (ng_get_composite_elem_default(type, index, workBuf
1420d9bfecabSArchie Cobbs 			      + sizeof(*hdr), workBuf + sizeof(*hdr) + *off,
1421d9bfecabSArchie Cobbs 			      &tempsize, ctype) == 0
1422d9bfecabSArchie Cobbs 			    && bcmp(workBuf + sizeof(*hdr) + *off,
14238db3c6cdSArchie Cobbs 			      data + *off, tempsize) == 0) {
1424f8307e12SArchie Cobbs 				*off += tempsize;
1425f8307e12SArchie Cobbs 				continue;
1426f8307e12SArchie Cobbs 			}
1427f8307e12SArchie Cobbs 		}
1428f8307e12SArchie Cobbs 
1429f8307e12SArchie Cobbs 		/* Print name= */
1430f627a379SGleb Smirnoff 		if ((error = ng_parse_append(&cbuf, &cbuflen, " ")) != 0)
1431f627a379SGleb Smirnoff 			goto fail;
1432f8307e12SArchie Cobbs 		if (ctype != CT_STRUCT) {
1433f8307e12SArchie Cobbs 			if (index != nextIndex) {
1434f8307e12SArchie Cobbs 				nextIndex = index;
1435f627a379SGleb Smirnoff 				if ((error = ng_parse_append(&cbuf,
1436f627a379SGleb Smirnoff 				    &cbuflen, "%d=", index)) != 0)
1437f627a379SGleb Smirnoff 					goto fail;
1438f8307e12SArchie Cobbs 			}
1439f8307e12SArchie Cobbs 			nextIndex++;
1440f8307e12SArchie Cobbs 		} else {
1441f0184ff8SArchie Cobbs 			const struct ng_parse_struct_field *const
1442f0184ff8SArchie Cobbs 			    fields = type->info;
1443f8307e12SArchie Cobbs 
1444f627a379SGleb Smirnoff 			if ((error = ng_parse_append(&cbuf,
1445f627a379SGleb Smirnoff 			    &cbuflen, "%s=", fields[index].name)) != 0)
1446f627a379SGleb Smirnoff 				goto fail;
1447f8307e12SArchie Cobbs 		}
1448f8307e12SArchie Cobbs 
1449f8307e12SArchie Cobbs 		/* Print value */
1450f8307e12SArchie Cobbs 		if ((error = INVOKE(etype, unparse)
14518db3c6cdSArchie Cobbs 		    (etype, data, off, cbuf, cbuflen)) != 0) {
14529c8c302fSJulian Elischer 			FREE(workBuf, M_NETGRAPH_PARSE);
1453f8307e12SArchie Cobbs 			return (error);
14548db3c6cdSArchie Cobbs 		}
1455f8307e12SArchie Cobbs 		cbuflen -= strlen(cbuf);
1456f8307e12SArchie Cobbs 		cbuf += strlen(cbuf);
1457f8307e12SArchie Cobbs 		didOne = 1;
1458f8307e12SArchie Cobbs 	}
14599c8c302fSJulian Elischer 	FREE(workBuf, M_NETGRAPH_PARSE);
1460f8307e12SArchie Cobbs 
1461f8307e12SArchie Cobbs 	/* Closing brace/bracket */
1462f627a379SGleb Smirnoff 	if ((error = ng_parse_append(&cbuf, &cbuflen, "%s%c",
1463f627a379SGleb Smirnoff 	    didOne ? " " : "", (ctype == CT_STRUCT) ? '}' : ']')) != 0)
1464f627a379SGleb Smirnoff 		goto fail;
1465f8307e12SArchie Cobbs 	return (0);
1466f627a379SGleb Smirnoff 
1467f627a379SGleb Smirnoff fail:
1468f627a379SGleb Smirnoff 	/* Clean up after failure */
1469f627a379SGleb Smirnoff 	FREE(workBuf, M_NETGRAPH_PARSE);
1470f627a379SGleb Smirnoff 	return (error);
1471f8307e12SArchie Cobbs }
1472f8307e12SArchie Cobbs 
1473f8307e12SArchie Cobbs /*
1474f8307e12SArchie Cobbs  * Generate the default value for an element of an array or structure
1475f8307e12SArchie Cobbs  * Returns EOPNOTSUPP if default value is unspecified.
1476f8307e12SArchie Cobbs  */
1477f8307e12SArchie Cobbs static int
1478f8307e12SArchie Cobbs ng_get_composite_elem_default(const struct ng_parse_type *type,
1479f8307e12SArchie Cobbs 	int index, const u_char *const start, u_char *buf, int *buflen,
1480f8307e12SArchie Cobbs 	const enum comptype ctype)
1481f8307e12SArchie Cobbs {
1482f8307e12SArchie Cobbs 	const struct ng_parse_type *etype;
1483f8307e12SArchie Cobbs 	ng_getDefault_t *func;
1484f8307e12SArchie Cobbs 
1485f8307e12SArchie Cobbs 	switch (ctype) {
1486f8307e12SArchie Cobbs 	case CT_STRUCT:
1487f8307e12SArchie Cobbs 		break;
1488f8307e12SArchie Cobbs 	case CT_ARRAY:
1489f8307e12SArchie Cobbs 	    {
1490f8307e12SArchie Cobbs 		const struct ng_parse_array_info *const ai = type->info;
1491f8307e12SArchie Cobbs 
1492f8307e12SArchie Cobbs 		if (ai->getDefault != NULL) {
1493f8307e12SArchie Cobbs 			return (*ai->getDefault)(type,
1494f8307e12SArchie Cobbs 			    index, start, buf, buflen);
1495f8307e12SArchie Cobbs 		}
1496f8307e12SArchie Cobbs 		break;
1497f8307e12SArchie Cobbs 	    }
1498f8307e12SArchie Cobbs 	case CT_FIXEDARRAY:
1499f8307e12SArchie Cobbs 	    {
1500f8307e12SArchie Cobbs 		const struct ng_parse_fixedarray_info *const fi = type->info;
1501f8307e12SArchie Cobbs 
1502f8307e12SArchie Cobbs 		if (*fi->getDefault != NULL) {
1503f8307e12SArchie Cobbs 			return (*fi->getDefault)(type,
1504f8307e12SArchie Cobbs 			    index, start, buf, buflen);
1505f8307e12SArchie Cobbs 		}
1506f8307e12SArchie Cobbs 		break;
1507f8307e12SArchie Cobbs 	    }
1508f8307e12SArchie Cobbs 	default:
15096e551fb6SDavid E. O'Brien 	    panic("%s", __func__);
1510f8307e12SArchie Cobbs 	}
1511f8307e12SArchie Cobbs 
1512f8307e12SArchie Cobbs 	/* Default to element type default */
1513f8307e12SArchie Cobbs 	etype = ng_get_composite_etype(type, index, ctype);
1514f8307e12SArchie Cobbs 	func = METHOD(etype, getDefault);
1515f8307e12SArchie Cobbs 	if (func == NULL)
1516f8307e12SArchie Cobbs 		return (EOPNOTSUPP);
1517f8307e12SArchie Cobbs 	return (*func)(etype, start, buf, buflen);
1518f8307e12SArchie Cobbs }
1519f8307e12SArchie Cobbs 
1520f8307e12SArchie Cobbs /*
1521f8307e12SArchie Cobbs  * Get the number of elements in a struct, variable or fixed array.
1522f8307e12SArchie Cobbs  */
1523f8307e12SArchie Cobbs static int
1524f8307e12SArchie Cobbs ng_get_composite_len(const struct ng_parse_type *type,
1525f8307e12SArchie Cobbs 	const u_char *const start, const u_char *buf,
1526f8307e12SArchie Cobbs 	const enum comptype ctype)
1527f8307e12SArchie Cobbs {
1528f8307e12SArchie Cobbs 	switch (ctype) {
1529f8307e12SArchie Cobbs 	case CT_STRUCT:
1530f8307e12SArchie Cobbs 	    {
1531f0184ff8SArchie Cobbs 		const struct ng_parse_struct_field *const fields = type->info;
1532f8307e12SArchie Cobbs 		int numFields = 0;
1533f8307e12SArchie Cobbs 
1534f8307e12SArchie Cobbs 		for (numFields = 0; ; numFields++) {
1535f8307e12SArchie Cobbs 			const struct ng_parse_struct_field *const
1536f0184ff8SArchie Cobbs 				fi = &fields[numFields];
1537f8307e12SArchie Cobbs 
1538f8307e12SArchie Cobbs 			if (fi->name == NULL)
1539f8307e12SArchie Cobbs 				break;
1540f8307e12SArchie Cobbs 		}
1541f8307e12SArchie Cobbs 		return (numFields);
1542f8307e12SArchie Cobbs 	    }
1543f8307e12SArchie Cobbs 	case CT_ARRAY:
1544f8307e12SArchie Cobbs 	    {
1545f8307e12SArchie Cobbs 		const struct ng_parse_array_info *const ai = type->info;
1546f8307e12SArchie Cobbs 
1547f8307e12SArchie Cobbs 		return (*ai->getLength)(type, start, buf);
1548f8307e12SArchie Cobbs 	    }
1549f8307e12SArchie Cobbs 	case CT_FIXEDARRAY:
1550f8307e12SArchie Cobbs 	    {
1551f8307e12SArchie Cobbs 		const struct ng_parse_fixedarray_info *const fi = type->info;
1552f8307e12SArchie Cobbs 
1553f8307e12SArchie Cobbs 		return fi->length;
1554f8307e12SArchie Cobbs 	    }
1555f8307e12SArchie Cobbs 	default:
15566e551fb6SDavid E. O'Brien 	    panic("%s", __func__);
1557f8307e12SArchie Cobbs 	}
1558f8307e12SArchie Cobbs 	return (0);
1559f8307e12SArchie Cobbs }
1560f8307e12SArchie Cobbs 
1561f8307e12SArchie Cobbs /*
1562f8307e12SArchie Cobbs  * Return the type of the index'th element of a composite structure
1563f8307e12SArchie Cobbs  */
1564f8307e12SArchie Cobbs static const struct ng_parse_type *
1565f8307e12SArchie Cobbs ng_get_composite_etype(const struct ng_parse_type *type,
1566f8307e12SArchie Cobbs 	int index, const enum comptype ctype)
1567f8307e12SArchie Cobbs {
1568f8307e12SArchie Cobbs 	const struct ng_parse_type *etype = NULL;
1569f8307e12SArchie Cobbs 
1570f8307e12SArchie Cobbs 	switch (ctype) {
1571f8307e12SArchie Cobbs 	case CT_STRUCT:
1572f8307e12SArchie Cobbs 	    {
1573f0184ff8SArchie Cobbs 		const struct ng_parse_struct_field *const fields = type->info;
1574f8307e12SArchie Cobbs 
1575f0184ff8SArchie Cobbs 		etype = fields[index].type;
1576f8307e12SArchie Cobbs 		break;
1577f8307e12SArchie Cobbs 	    }
1578f8307e12SArchie Cobbs 	case CT_ARRAY:
1579f8307e12SArchie Cobbs 	    {
1580f8307e12SArchie Cobbs 		const struct ng_parse_array_info *const ai = type->info;
1581f8307e12SArchie Cobbs 
1582f8307e12SArchie Cobbs 		etype = ai->elementType;
1583f8307e12SArchie Cobbs 		break;
1584f8307e12SArchie Cobbs 	    }
1585f8307e12SArchie Cobbs 	case CT_FIXEDARRAY:
1586f8307e12SArchie Cobbs 	    {
1587f8307e12SArchie Cobbs 		const struct ng_parse_fixedarray_info *const fi = type->info;
1588f8307e12SArchie Cobbs 
1589f8307e12SArchie Cobbs 		etype = fi->elementType;
1590f8307e12SArchie Cobbs 		break;
1591f8307e12SArchie Cobbs 	    }
1592f8307e12SArchie Cobbs 	default:
15936e551fb6SDavid E. O'Brien 	    panic("%s", __func__);
1594f8307e12SArchie Cobbs 	}
1595f8307e12SArchie Cobbs 	return (etype);
1596f8307e12SArchie Cobbs }
1597f8307e12SArchie Cobbs 
1598f8307e12SArchie Cobbs /*
1599f8307e12SArchie Cobbs  * Get the number of bytes to skip to align for the next
1600f8307e12SArchie Cobbs  * element in a composite structure.
1601f8307e12SArchie Cobbs  */
1602f8307e12SArchie Cobbs static int
1603f8307e12SArchie Cobbs ng_parse_get_elem_pad(const struct ng_parse_type *type,
1604f8307e12SArchie Cobbs 	int index, enum comptype ctype, int posn)
1605f8307e12SArchie Cobbs {
1606f8307e12SArchie Cobbs 	const struct ng_parse_type *const
1607f8307e12SArchie Cobbs 	    etype = ng_get_composite_etype(type, index, ctype);
1608f8307e12SArchie Cobbs 	int align;
1609f8307e12SArchie Cobbs 
1610f8307e12SArchie Cobbs 	/* Get element's alignment, and possibly override */
1611f8307e12SArchie Cobbs 	align = ALIGNMENT(etype);
1612f8307e12SArchie Cobbs 	if (ctype == CT_STRUCT) {
1613f0184ff8SArchie Cobbs 		const struct ng_parse_struct_field *const fields = type->info;
1614f8307e12SArchie Cobbs 
1615f0184ff8SArchie Cobbs 		if (fields[index].alignment != 0)
1616f0184ff8SArchie Cobbs 			align = fields[index].alignment;
1617f8307e12SArchie Cobbs 	}
1618f8307e12SArchie Cobbs 
1619f8307e12SArchie Cobbs 	/* Return number of bytes to skip to align */
1620f8307e12SArchie Cobbs 	return (align ? (align - (posn % align)) % align : 0);
1621f8307e12SArchie Cobbs }
1622f8307e12SArchie Cobbs 
1623f8307e12SArchie Cobbs /************************************************************************
1624f8307e12SArchie Cobbs 			PARSING HELPER ROUTINES
1625f8307e12SArchie Cobbs  ************************************************************************/
1626f8307e12SArchie Cobbs 
1627f8307e12SArchie Cobbs /*
1628f627a379SGleb Smirnoff  * Append to a fixed length string buffer.
1629f627a379SGleb Smirnoff  */
1630f627a379SGleb Smirnoff static int
1631f627a379SGleb Smirnoff ng_parse_append(char **cbufp, int *cbuflenp, const char *fmt, ...)
1632f627a379SGleb Smirnoff {
1633f627a379SGleb Smirnoff 	va_list args;
1634f627a379SGleb Smirnoff 	int len;
1635f627a379SGleb Smirnoff 
1636f627a379SGleb Smirnoff 	va_start(args, fmt);
1637f627a379SGleb Smirnoff 	len = vsnprintf(*cbufp, *cbuflenp, fmt, args);
1638f627a379SGleb Smirnoff 	va_end(args);
1639f627a379SGleb Smirnoff 	if (len >= *cbuflenp)
1640f627a379SGleb Smirnoff 		return ERANGE;
1641f627a379SGleb Smirnoff 	*cbufp += len;
1642f627a379SGleb Smirnoff 	*cbuflenp -= len;
1643f627a379SGleb Smirnoff 
1644f627a379SGleb Smirnoff 	return (0);
1645f627a379SGleb Smirnoff }
1646f627a379SGleb Smirnoff 
1647f627a379SGleb Smirnoff /*
1648f8307e12SArchie Cobbs  * Skip over a value
1649f8307e12SArchie Cobbs  */
1650f8307e12SArchie Cobbs static int
1651f8307e12SArchie Cobbs ng_parse_skip_value(const char *s, int off0, int *lenp)
1652f8307e12SArchie Cobbs {
1653f8307e12SArchie Cobbs 	int len, nbracket, nbrace;
1654f8307e12SArchie Cobbs 	int off = off0;
1655f8307e12SArchie Cobbs 
1656f8307e12SArchie Cobbs 	len = nbracket = nbrace = 0;
1657f8307e12SArchie Cobbs 	do {
1658f8307e12SArchie Cobbs 		switch (ng_parse_get_token(s, &off, &len)) {
1659f8307e12SArchie Cobbs 		case T_LBRACKET:
1660f8307e12SArchie Cobbs 			nbracket++;
1661f8307e12SArchie Cobbs 			break;
1662f8307e12SArchie Cobbs 		case T_LBRACE:
1663f8307e12SArchie Cobbs 			nbrace++;
1664f8307e12SArchie Cobbs 			break;
1665f8307e12SArchie Cobbs 		case T_RBRACKET:
1666f8307e12SArchie Cobbs 			if (nbracket-- == 0)
1667f8307e12SArchie Cobbs 				return (EINVAL);
1668f8307e12SArchie Cobbs 			break;
1669f8307e12SArchie Cobbs 		case T_RBRACE:
1670f8307e12SArchie Cobbs 			if (nbrace-- == 0)
1671f8307e12SArchie Cobbs 				return (EINVAL);
1672f8307e12SArchie Cobbs 			break;
1673f8307e12SArchie Cobbs 		case T_EOF:
1674f8307e12SArchie Cobbs 			return (EINVAL);
1675f8307e12SArchie Cobbs 		default:
1676f8307e12SArchie Cobbs 			break;
1677f8307e12SArchie Cobbs 		}
1678f8307e12SArchie Cobbs 		off += len;
1679f8307e12SArchie Cobbs 	} while (nbracket > 0 || nbrace > 0);
1680f8307e12SArchie Cobbs 	*lenp = off - off0;
1681f8307e12SArchie Cobbs 	return (0);
1682f8307e12SArchie Cobbs }
1683f8307e12SArchie Cobbs 
1684f8307e12SArchie Cobbs /*
1685f8307e12SArchie Cobbs  * Find the next token in the string, starting at offset *startp.
1686f8307e12SArchie Cobbs  * Returns the token type, with *startp pointing to the first char
1687f8307e12SArchie Cobbs  * and *lenp the length.
1688f8307e12SArchie Cobbs  */
1689f8307e12SArchie Cobbs enum ng_parse_token
1690f8307e12SArchie Cobbs ng_parse_get_token(const char *s, int *startp, int *lenp)
1691f8307e12SArchie Cobbs {
1692f8307e12SArchie Cobbs 	char *t;
1693f8307e12SArchie Cobbs 	int i;
1694f8307e12SArchie Cobbs 
1695f8307e12SArchie Cobbs 	while (isspace(s[*startp]))
1696f8307e12SArchie Cobbs 		(*startp)++;
1697f8307e12SArchie Cobbs 	switch (s[*startp]) {
1698f8307e12SArchie Cobbs 	case '\0':
1699f8307e12SArchie Cobbs 		*lenp = 0;
1700f8307e12SArchie Cobbs 		return T_EOF;
1701f8307e12SArchie Cobbs 	case '{':
1702f8307e12SArchie Cobbs 		*lenp = 1;
1703f8307e12SArchie Cobbs 		return T_LBRACE;
1704f8307e12SArchie Cobbs 	case '}':
1705f8307e12SArchie Cobbs 		*lenp = 1;
1706f8307e12SArchie Cobbs 		return T_RBRACE;
1707f8307e12SArchie Cobbs 	case '[':
1708f8307e12SArchie Cobbs 		*lenp = 1;
1709f8307e12SArchie Cobbs 		return T_LBRACKET;
1710f8307e12SArchie Cobbs 	case ']':
1711f8307e12SArchie Cobbs 		*lenp = 1;
1712f8307e12SArchie Cobbs 		return T_RBRACKET;
1713f8307e12SArchie Cobbs 	case '=':
1714f8307e12SArchie Cobbs 		*lenp = 1;
1715f8307e12SArchie Cobbs 		return T_EQUALS;
1716f8307e12SArchie Cobbs 	case '"':
171727121ab1SBrian Somers 		if ((t = ng_get_string_token(s, startp, lenp, NULL)) == NULL)
1718f8307e12SArchie Cobbs 			return T_ERROR;
17199c8c302fSJulian Elischer 		FREE(t, M_NETGRAPH_PARSE);
1720f8307e12SArchie Cobbs 		return T_STRING;
1721f8307e12SArchie Cobbs 	default:
1722f8307e12SArchie Cobbs 		for (i = *startp + 1; s[i] != '\0' && !isspace(s[i])
1723f8307e12SArchie Cobbs 		    && s[i] != '{' && s[i] != '}' && s[i] != '['
1724f8307e12SArchie Cobbs 		    && s[i] != ']' && s[i] != '=' && s[i] != '"'; i++)
1725f8307e12SArchie Cobbs 			;
1726f8307e12SArchie Cobbs 		*lenp = i - *startp;
1727f8307e12SArchie Cobbs 		return T_WORD;
1728f8307e12SArchie Cobbs 	}
1729f8307e12SArchie Cobbs }
1730f8307e12SArchie Cobbs 
1731f8307e12SArchie Cobbs /*
1732f8307e12SArchie Cobbs  * Get a string token, which must be enclosed in double quotes.
1733f8307e12SArchie Cobbs  * The normal C backslash escapes are recognized.
1734f8307e12SArchie Cobbs  */
1735f8307e12SArchie Cobbs char *
173627121ab1SBrian Somers ng_get_string_token(const char *s, int *startp, int *lenp, int *slenp)
1737f8307e12SArchie Cobbs {
1738f8307e12SArchie Cobbs 	char *cbuf, *p;
1739f8307e12SArchie Cobbs 	int start, off;
174027121ab1SBrian Somers 	int slen;
1741f8307e12SArchie Cobbs 
1742f8307e12SArchie Cobbs 	while (isspace(s[*startp]))
1743f8307e12SArchie Cobbs 		(*startp)++;
1744f8307e12SArchie Cobbs 	start = *startp;
1745f8307e12SArchie Cobbs 	if (s[*startp] != '"')
1746f8307e12SArchie Cobbs 		return (NULL);
17479c8c302fSJulian Elischer 	MALLOC(cbuf, char *, strlen(s + start), M_NETGRAPH_PARSE, M_NOWAIT);
1748f8307e12SArchie Cobbs 	if (cbuf == NULL)
1749f8307e12SArchie Cobbs 		return (NULL);
1750f8307e12SArchie Cobbs 	strcpy(cbuf, s + start + 1);
175127121ab1SBrian Somers 	for (slen = 0, off = 1, p = cbuf; *p != '\0'; slen++, off++, p++) {
1752f8307e12SArchie Cobbs 		if (*p == '"') {
1753f8307e12SArchie Cobbs 			*p = '\0';
1754f8307e12SArchie Cobbs 			*lenp = off + 1;
175527121ab1SBrian Somers 			if (slenp != NULL)
175627121ab1SBrian Somers 				*slenp = slen;
1757f8307e12SArchie Cobbs 			return (cbuf);
1758f8307e12SArchie Cobbs 		} else if (p[0] == '\\' && p[1] != '\0') {
1759f8307e12SArchie Cobbs 			int x, k;
1760f8307e12SArchie Cobbs 			char *v;
1761f8307e12SArchie Cobbs 
1762f8307e12SArchie Cobbs 			strcpy(p, p + 1);
1763f8307e12SArchie Cobbs 			v = p;
1764f8307e12SArchie Cobbs 			switch (*p) {
1765f8307e12SArchie Cobbs 			case 't':
1766f8307e12SArchie Cobbs 				*v = '\t';
1767f8307e12SArchie Cobbs 				off++;
1768f8307e12SArchie Cobbs 				continue;
1769f8307e12SArchie Cobbs 			case 'n':
1770f8307e12SArchie Cobbs 				*v = '\n';
1771f8307e12SArchie Cobbs 				off++;
1772f8307e12SArchie Cobbs 				continue;
1773f8307e12SArchie Cobbs 			case 'r':
1774f8307e12SArchie Cobbs 				*v = '\r';
1775f8307e12SArchie Cobbs 				off++;
1776f8307e12SArchie Cobbs 				continue;
1777f8307e12SArchie Cobbs 			case 'v':
1778f8307e12SArchie Cobbs 				*v =  '\v';
1779f8307e12SArchie Cobbs 				off++;
1780f8307e12SArchie Cobbs 				continue;
1781f8307e12SArchie Cobbs 			case 'f':
1782f8307e12SArchie Cobbs 				*v =  '\f';
1783f8307e12SArchie Cobbs 				off++;
1784f8307e12SArchie Cobbs 				continue;
1785f8307e12SArchie Cobbs 			case '"':
1786f8307e12SArchie Cobbs 				*v =  '"';
1787f8307e12SArchie Cobbs 				off++;
1788f8307e12SArchie Cobbs 				continue;
1789f8307e12SArchie Cobbs 			case '0': case '1': case '2': case '3':
1790f8307e12SArchie Cobbs 			case '4': case '5': case '6': case '7':
1791f8307e12SArchie Cobbs 				for (x = k = 0;
1792f8307e12SArchie Cobbs 				    k < 3 && *v >= '0' && *v <= '7'; v++) {
1793f8307e12SArchie Cobbs 					x = (x << 3) + (*v - '0');
1794f8307e12SArchie Cobbs 					off++;
1795f8307e12SArchie Cobbs 				}
1796f8307e12SArchie Cobbs 				*--v = (char)x;
1797f8307e12SArchie Cobbs 				break;
1798f8307e12SArchie Cobbs 			case 'x':
1799f8307e12SArchie Cobbs 				for (v++, x = k = 0;
1800f8307e12SArchie Cobbs 				    k < 2 && isxdigit(*v); v++) {
1801f8307e12SArchie Cobbs 					x = (x << 4) + (isdigit(*v) ?
1802f8307e12SArchie Cobbs 					      (*v - '0') :
1803f8307e12SArchie Cobbs 					      (tolower(*v) - 'a' + 10));
1804f8307e12SArchie Cobbs 					off++;
1805f8307e12SArchie Cobbs 				}
1806f8307e12SArchie Cobbs 				*--v = (char)x;
1807f8307e12SArchie Cobbs 				break;
1808f8307e12SArchie Cobbs 			default:
1809f8307e12SArchie Cobbs 				continue;
1810f8307e12SArchie Cobbs 			}
1811f8307e12SArchie Cobbs 			strcpy(p, v);
1812f8307e12SArchie Cobbs 		}
1813f8307e12SArchie Cobbs 	}
18140a4a8041SJohn Polstra 	FREE(cbuf, M_NETGRAPH_PARSE);
1815f8307e12SArchie Cobbs 	return (NULL);		/* no closing quote */
1816f8307e12SArchie Cobbs }
1817f8307e12SArchie Cobbs 
1818f8307e12SArchie Cobbs /*
1819f8307e12SArchie Cobbs  * Encode a string so it can be safely put in double quotes.
182027121ab1SBrian Somers  * Caller must free the result. Exactly "slen" characters
182127121ab1SBrian Somers  * are encoded.
1822f8307e12SArchie Cobbs  */
1823f8307e12SArchie Cobbs char *
182427121ab1SBrian Somers ng_encode_string(const char *raw, int slen)
1825f8307e12SArchie Cobbs {
1826f8307e12SArchie Cobbs 	char *cbuf;
1827f8307e12SArchie Cobbs 	int off = 0;
182827121ab1SBrian Somers 	int i;
1829f8307e12SArchie Cobbs 
18309c8c302fSJulian Elischer 	MALLOC(cbuf, char *, strlen(raw) * 4 + 3, M_NETGRAPH_PARSE, M_NOWAIT);
1831f8307e12SArchie Cobbs 	if (cbuf == NULL)
1832f8307e12SArchie Cobbs 		return (NULL);
1833f8307e12SArchie Cobbs 	cbuf[off++] = '"';
183427121ab1SBrian Somers 	for (i = 0; i < slen; i++, raw++) {
1835f8307e12SArchie Cobbs 		switch (*raw) {
1836f8307e12SArchie Cobbs 		case '\t':
1837f8307e12SArchie Cobbs 			cbuf[off++] = '\\';
1838f8307e12SArchie Cobbs 			cbuf[off++] = 't';
1839f8307e12SArchie Cobbs 			break;
1840f8307e12SArchie Cobbs 		case '\f':
1841f8307e12SArchie Cobbs 			cbuf[off++] = '\\';
1842f8307e12SArchie Cobbs 			cbuf[off++] = 'f';
1843f8307e12SArchie Cobbs 			break;
1844f8307e12SArchie Cobbs 		case '\n':
1845f8307e12SArchie Cobbs 			cbuf[off++] = '\\';
1846f8307e12SArchie Cobbs 			cbuf[off++] = 'n';
1847f8307e12SArchie Cobbs 			break;
1848f8307e12SArchie Cobbs 		case '\r':
1849f8307e12SArchie Cobbs 			cbuf[off++] = '\\';
1850f8307e12SArchie Cobbs 			cbuf[off++] = 'r';
1851f8307e12SArchie Cobbs 			break;
1852f8307e12SArchie Cobbs 		case '\v':
1853f8307e12SArchie Cobbs 			cbuf[off++] = '\\';
1854f8307e12SArchie Cobbs 			cbuf[off++] = 'v';
1855f8307e12SArchie Cobbs 			break;
1856f8307e12SArchie Cobbs 		case '"':
1857f8307e12SArchie Cobbs 		case '\\':
1858f8307e12SArchie Cobbs 			cbuf[off++] = '\\';
1859f8307e12SArchie Cobbs 			cbuf[off++] = *raw;
1860f8307e12SArchie Cobbs 			break;
1861f8307e12SArchie Cobbs 		default:
1862f8307e12SArchie Cobbs 			if (*raw < 0x20 || *raw > 0x7e) {
1863f8307e12SArchie Cobbs 				off += sprintf(cbuf + off,
1864f8307e12SArchie Cobbs 				    "\\x%02x", (u_char)*raw);
1865f8307e12SArchie Cobbs 				break;
1866f8307e12SArchie Cobbs 			}
1867f8307e12SArchie Cobbs 			cbuf[off++] = *raw;
1868f8307e12SArchie Cobbs 			break;
1869f8307e12SArchie Cobbs 		}
1870f8307e12SArchie Cobbs 	}
1871f8307e12SArchie Cobbs 	cbuf[off++] = '"';
1872f8307e12SArchie Cobbs 	cbuf[off] = '\0';
1873f8307e12SArchie Cobbs 	return (cbuf);
1874f8307e12SArchie Cobbs }
1875f8307e12SArchie Cobbs 
1876f8307e12SArchie Cobbs /************************************************************************
1877f8307e12SArchie Cobbs 			VIRTUAL METHOD LOOKUP
1878f8307e12SArchie Cobbs  ************************************************************************/
1879f8307e12SArchie Cobbs 
1880f8307e12SArchie Cobbs static ng_parse_t *
1881f8307e12SArchie Cobbs ng_get_parse_method(const struct ng_parse_type *t)
1882f8307e12SArchie Cobbs {
1883f8307e12SArchie Cobbs 	while (t != NULL && t->parse == NULL)
1884f8307e12SArchie Cobbs 		t = t->supertype;
1885f8307e12SArchie Cobbs 	return (t ? t->parse : NULL);
1886f8307e12SArchie Cobbs }
1887f8307e12SArchie Cobbs 
1888f8307e12SArchie Cobbs static ng_unparse_t *
1889f8307e12SArchie Cobbs ng_get_unparse_method(const struct ng_parse_type *t)
1890f8307e12SArchie Cobbs {
1891f8307e12SArchie Cobbs 	while (t != NULL && t->unparse == NULL)
1892f8307e12SArchie Cobbs 		t = t->supertype;
1893f8307e12SArchie Cobbs 	return (t ? t->unparse : NULL);
1894f8307e12SArchie Cobbs }
1895f8307e12SArchie Cobbs 
1896f8307e12SArchie Cobbs static ng_getDefault_t *
1897f8307e12SArchie Cobbs ng_get_getDefault_method(const struct ng_parse_type *t)
1898f8307e12SArchie Cobbs {
1899f8307e12SArchie Cobbs 	while (t != NULL && t->getDefault == NULL)
1900f8307e12SArchie Cobbs 		t = t->supertype;
1901f8307e12SArchie Cobbs 	return (t ? t->getDefault : NULL);
1902f8307e12SArchie Cobbs }
1903f8307e12SArchie Cobbs 
1904f8307e12SArchie Cobbs static ng_getAlign_t *
1905f8307e12SArchie Cobbs ng_get_getAlign_method(const struct ng_parse_type *t)
1906f8307e12SArchie Cobbs {
1907f8307e12SArchie Cobbs 	while (t != NULL && t->getAlign == NULL)
1908f8307e12SArchie Cobbs 		t = t->supertype;
1909f8307e12SArchie Cobbs 	return (t ? t->getAlign : NULL);
1910f8307e12SArchie Cobbs }
1911f8307e12SArchie Cobbs 
1912