I should drink more coffee before starting committing. Revert the last change
[dragonfly.git] / sys / netgraph7 / ng_parse.c
blob2e6e1ae49e803fc72acc01b8cff77e7c9bbc4ed7
1 /*
2 * ng_parse.c
3 */
5 /*-
6 * Copyright (c) 1999 Whistle Communications, Inc.
7 * All rights reserved.
8 *
9 * Subject to the following obligations and disclaimer of warranty, use and
10 * redistribution of this software, in source or object code forms, with or
11 * without modifications are expressly permitted by Whistle Communications;
12 * provided, however, that:
13 * 1. Any and all reproductions of the source or object code must include the
14 * copyright notice above and the following disclaimer of warranties; and
15 * 2. No rights are granted, in any manner or form, to use Whistle
16 * Communications, Inc. trademarks, including the mark "WHISTLE
17 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
18 * such appears in the above copyright notice or in the software.
20 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
21 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
22 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
23 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
25 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
26 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
27 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
28 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
29 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
36 * OF SUCH DAMAGE.
38 * Author: Archie Cobbs <archie@freebsd.org>
40 * $Whistle: ng_parse.c,v 1.3 1999/11/29 01:43:48 archie Exp $
41 * $FreeBSD: src/sys/netgraph/ng_parse.c,v 1.30 2007/06/23 00:02:20 mjacob Exp $
42 * $DragonFly: src/sys/netgraph7/ng_parse.c,v 1.2 2008/06/26 23:05:35 dillon Exp $
45 #include <sys/types.h>
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/errno.h>
50 #include <sys/limits.h>
51 #include <sys/malloc.h>
52 #include <sys/mbuf.h>
53 #include <sys/ctype.h>
55 #include <machine/stdarg.h>
57 #include <net/ethernet.h>
59 #include <netinet/in.h>
61 #include "ng_message.h"
62 #include "netgraph.h"
63 #include "ng_parse.h"
65 #ifdef NG_SEPARATE_MALLOC
66 MALLOC_DEFINE(M_NETGRAPH_PARSE, "netgraph_parse", "netgraph parse info");
67 #else
68 #define M_NETGRAPH_PARSE M_NETGRAPH
69 #endif
71 /* Compute alignment for primitive integral types */
72 struct int16_temp {
73 char x;
74 int16_t y;
77 struct int32_temp {
78 char x;
79 int32_t y;
82 struct int64_temp {
83 char x;
84 int64_t y;
87 #define INT8_ALIGNMENT 1
88 #define INT16_ALIGNMENT ((size_t)&((struct int16_temp *)0)->y)
89 #define INT32_ALIGNMENT ((size_t)&((struct int32_temp *)0)->y)
90 #define INT64_ALIGNMENT ((size_t)&((struct int64_temp *)0)->y)
92 /* Output format for integral types */
93 #define INT_UNSIGNED 0
94 #define INT_SIGNED 1
95 #define INT_HEX 2
97 /* Type of composite object: struct, array, or fixedarray */
98 enum comptype {
99 CT_STRUCT,
100 CT_ARRAY,
101 CT_FIXEDARRAY,
104 /* Composite types helper functions */
105 static int ng_parse_composite(const struct ng_parse_type *type,
106 const char *s, int *off, const u_char *start,
107 u_char *const buf, int *buflen, enum comptype ctype);
108 static int ng_unparse_composite(const struct ng_parse_type *type,
109 const u_char *data, int *off, char *cbuf, int cbuflen,
110 enum comptype ctype);
111 static int ng_get_composite_elem_default(const struct ng_parse_type *type,
112 int index, const u_char *start, u_char *buf,
113 int *buflen, enum comptype ctype);
114 static int ng_get_composite_len(const struct ng_parse_type *type,
115 const u_char *start, const u_char *buf,
116 enum comptype ctype);
117 static const struct ng_parse_type *ng_get_composite_etype(const struct
118 ng_parse_type *type, int index, enum comptype ctype);
119 static int ng_parse_get_elem_pad(const struct ng_parse_type *type,
120 int index, enum comptype ctype, int posn);
122 /* Parsing helper functions */
123 static int ng_parse_skip_value(const char *s, int off, int *lenp);
124 static int ng_parse_append(char **cbufp, int *cbuflenp,
125 const char *fmt, ...);
127 /* Poor man's virtual method calls */
128 #define METHOD(t,m) (ng_get_ ## m ## _method(t))
129 #define INVOKE(t,m) (*METHOD(t,m))
131 static ng_parse_t *ng_get_parse_method(const struct ng_parse_type *t);
132 static ng_unparse_t *ng_get_unparse_method(const struct ng_parse_type *t);
133 static ng_getDefault_t *ng_get_getDefault_method(const
134 struct ng_parse_type *t);
135 static ng_getAlign_t *ng_get_getAlign_method(const struct ng_parse_type *t);
137 #define ALIGNMENT(t) (METHOD(t, getAlign) == NULL ? \
138 0 : INVOKE(t, getAlign)(t))
140 /************************************************************************
141 PUBLIC FUNCTIONS
142 ************************************************************************/
145 * Convert an ASCII string to binary according to the supplied type descriptor
148 ng_parse(const struct ng_parse_type *type,
149 const char *string, int *off, u_char *buf, int *buflen)
151 return INVOKE(type, parse)(type, string, off, buf, buf, buflen);
155 * Convert binary to an ASCII string according to the supplied type descriptor
158 ng_unparse(const struct ng_parse_type *type,
159 const u_char *data, char *cbuf, int cbuflen)
161 int off = 0;
163 return INVOKE(type, unparse)(type, data, &off, cbuf, cbuflen);
167 * Fill in the default value according to the supplied type descriptor
170 ng_parse_getDefault(const struct ng_parse_type *type, u_char *buf, int *buflen)
172 ng_getDefault_t *const func = METHOD(type, getDefault);
174 if (func == NULL)
175 return (EOPNOTSUPP);
176 return (*func)(type, buf, buf, buflen);
180 /************************************************************************
181 STRUCTURE TYPE
182 ************************************************************************/
184 static int
185 ng_struct_parse(const struct ng_parse_type *type,
186 const char *s, int *off, const u_char *const start,
187 u_char *const buf, int *buflen)
189 return ng_parse_composite(type, s, off, start, buf, buflen, CT_STRUCT);
192 static int
193 ng_struct_unparse(const struct ng_parse_type *type,
194 const u_char *data, int *off, char *cbuf, int cbuflen)
196 return ng_unparse_composite(type, data, off, cbuf, cbuflen, CT_STRUCT);
199 static int
200 ng_struct_getDefault(const struct ng_parse_type *type,
201 const u_char *const start, u_char *buf, int *buflen)
203 int off = 0;
205 return ng_parse_composite(type,
206 "{}", &off, start, buf, buflen, CT_STRUCT);
209 static int
210 ng_struct_getAlign(const struct ng_parse_type *type)
212 const struct ng_parse_struct_field *field;
213 int align = 0;
215 for (field = type->info; field->name != NULL; field++) {
216 int falign = ALIGNMENT(field->type);
218 if (falign > align)
219 align = falign;
221 return align;
224 const struct ng_parse_type ng_parse_struct_type = {
225 NULL,
226 NULL,
227 NULL,
228 ng_struct_parse,
229 ng_struct_unparse,
230 ng_struct_getDefault,
231 ng_struct_getAlign
234 /************************************************************************
235 FIXED LENGTH ARRAY TYPE
236 ************************************************************************/
238 static int
239 ng_fixedarray_parse(const struct ng_parse_type *type,
240 const char *s, int *off, const u_char *const start,
241 u_char *const buf, int *buflen)
243 return ng_parse_composite(type,
244 s, off, start, buf, buflen, CT_FIXEDARRAY);
247 static int
248 ng_fixedarray_unparse(const struct ng_parse_type *type,
249 const u_char *data, int *off, char *cbuf, int cbuflen)
251 return ng_unparse_composite(type,
252 data, off, cbuf, cbuflen, CT_FIXEDARRAY);
255 static int
256 ng_fixedarray_getDefault(const struct ng_parse_type *type,
257 const u_char *const start, u_char *buf, int *buflen)
259 int off = 0;
261 return ng_parse_composite(type,
262 "[]", &off, start, buf, buflen, CT_FIXEDARRAY);
265 static int
266 ng_fixedarray_getAlign(const struct ng_parse_type *type)
268 const struct ng_parse_fixedarray_info *fi = type->info;
270 return ALIGNMENT(fi->elementType);
273 const struct ng_parse_type ng_parse_fixedarray_type = {
274 NULL,
275 NULL,
276 NULL,
277 ng_fixedarray_parse,
278 ng_fixedarray_unparse,
279 ng_fixedarray_getDefault,
280 ng_fixedarray_getAlign
283 /************************************************************************
284 VARIABLE LENGTH ARRAY TYPE
285 ************************************************************************/
287 static int
288 ng_array_parse(const struct ng_parse_type *type,
289 const char *s, int *off, const u_char *const start,
290 u_char *const buf, int *buflen)
292 return ng_parse_composite(type, s, off, start, buf, buflen, CT_ARRAY);
295 static int
296 ng_array_unparse(const struct ng_parse_type *type,
297 const u_char *data, int *off, char *cbuf, int cbuflen)
299 return ng_unparse_composite(type, data, off, cbuf, cbuflen, CT_ARRAY);
302 static int
303 ng_array_getDefault(const struct ng_parse_type *type,
304 const u_char *const start, u_char *buf, int *buflen)
306 int off = 0;
308 return ng_parse_composite(type,
309 "[]", &off, start, buf, buflen, CT_ARRAY);
312 static int
313 ng_array_getAlign(const struct ng_parse_type *type)
315 const struct ng_parse_array_info *ai = type->info;
317 return ALIGNMENT(ai->elementType);
320 const struct ng_parse_type ng_parse_array_type = {
321 NULL,
322 NULL,
323 NULL,
324 ng_array_parse,
325 ng_array_unparse,
326 ng_array_getDefault,
327 ng_array_getAlign
330 /************************************************************************
331 INT8 TYPE
332 ************************************************************************/
334 static int
335 ng_int8_parse(const struct ng_parse_type *type,
336 const char *s, int *off, const u_char *const start,
337 u_char *const buf, int *buflen)
339 long val;
340 int8_t val8;
341 char *eptr;
343 val = strtol(s + *off, &eptr, 0);
344 if (val < (int8_t)0x80 || val > (u_int8_t)0xff || eptr == s + *off)
345 return (EINVAL);
346 *off = eptr - s;
347 val8 = (int8_t)val;
348 bcopy(&val8, buf, sizeof(int8_t));
349 *buflen = sizeof(int8_t);
350 return (0);
353 static int
354 ng_int8_unparse(const struct ng_parse_type *type,
355 const u_char *data, int *off, char *cbuf, int cbuflen)
357 const char *fmt;
358 int fval;
359 int error;
360 int8_t val;
362 bcopy(data + *off, &val, sizeof(int8_t));
363 switch ((intptr_t)type->info) {
364 case INT_SIGNED:
365 fmt = "%d";
366 fval = val;
367 break;
368 case INT_UNSIGNED:
369 fmt = "%u";
370 fval = (u_int8_t)val;
371 break;
372 case INT_HEX:
373 fmt = "0x%x";
374 fval = (u_int8_t)val;
375 break;
376 default:
377 panic("%s: unknown type", __func__);
378 #ifdef RESTARTABLE_PANICS
379 return(0);
380 #endif
382 if ((error = ng_parse_append(&cbuf, &cbuflen, fmt, fval)) != 0)
383 return (error);
384 *off += sizeof(int8_t);
385 return (0);
388 static int
389 ng_int8_getDefault(const struct ng_parse_type *type,
390 const u_char *const start, u_char *buf, int *buflen)
392 int8_t val;
394 if (*buflen < sizeof(int8_t))
395 return (ERANGE);
396 val = 0;
397 bcopy(&val, buf, sizeof(int8_t));
398 *buflen = sizeof(int8_t);
399 return (0);
402 static int
403 ng_int8_getAlign(const struct ng_parse_type *type)
405 return INT8_ALIGNMENT;
408 const struct ng_parse_type ng_parse_int8_type = {
409 NULL,
410 (void *)INT_SIGNED,
411 NULL,
412 ng_int8_parse,
413 ng_int8_unparse,
414 ng_int8_getDefault,
415 ng_int8_getAlign
418 const struct ng_parse_type ng_parse_uint8_type = {
419 &ng_parse_int8_type,
420 (void *)INT_UNSIGNED
423 const struct ng_parse_type ng_parse_hint8_type = {
424 &ng_parse_int8_type,
425 (void *)INT_HEX
428 /************************************************************************
429 INT16 TYPE
430 ************************************************************************/
432 static int
433 ng_int16_parse(const struct ng_parse_type *type,
434 const char *s, int *off, const u_char *const start,
435 u_char *const buf, int *buflen)
437 long val;
438 int16_t val16;
439 char *eptr;
441 val = strtol(s + *off, &eptr, 0);
442 if (val < (int16_t)0x8000
443 || val > (u_int16_t)0xffff || eptr == s + *off)
444 return (EINVAL);
445 *off = eptr - s;
446 val16 = (int16_t)val;
447 bcopy(&val16, buf, sizeof(int16_t));
448 *buflen = sizeof(int16_t);
449 return (0);
452 static int
453 ng_int16_unparse(const struct ng_parse_type *type,
454 const u_char *data, int *off, char *cbuf, int cbuflen)
456 const char *fmt;
457 int fval;
458 int error;
459 int16_t val;
461 bcopy(data + *off, &val, sizeof(int16_t));
462 switch ((intptr_t)type->info) {
463 case INT_SIGNED:
464 fmt = "%d";
465 fval = val;
466 break;
467 case INT_UNSIGNED:
468 fmt = "%u";
469 fval = (u_int16_t)val;
470 break;
471 case INT_HEX:
472 fmt = "0x%x";
473 fval = (u_int16_t)val;
474 break;
475 default:
476 panic("%s: unknown type", __func__);
477 #ifdef RESTARTABLE_PANICS
478 return(0);
479 #endif
481 if ((error = ng_parse_append(&cbuf, &cbuflen, fmt, fval)) != 0)
482 return (error);
483 *off += sizeof(int16_t);
484 return (0);
487 static int
488 ng_int16_getDefault(const struct ng_parse_type *type,
489 const u_char *const start, u_char *buf, int *buflen)
491 int16_t val;
493 if (*buflen < sizeof(int16_t))
494 return (ERANGE);
495 val = 0;
496 bcopy(&val, buf, sizeof(int16_t));
497 *buflen = sizeof(int16_t);
498 return (0);
501 static int
502 ng_int16_getAlign(const struct ng_parse_type *type)
504 return INT16_ALIGNMENT;
507 const struct ng_parse_type ng_parse_int16_type = {
508 NULL,
509 (void *)INT_SIGNED,
510 NULL,
511 ng_int16_parse,
512 ng_int16_unparse,
513 ng_int16_getDefault,
514 ng_int16_getAlign
517 const struct ng_parse_type ng_parse_uint16_type = {
518 &ng_parse_int16_type,
519 (void *)INT_UNSIGNED
522 const struct ng_parse_type ng_parse_hint16_type = {
523 &ng_parse_int16_type,
524 (void *)INT_HEX
527 /************************************************************************
528 INT32 TYPE
529 ************************************************************************/
531 static int
532 ng_int32_parse(const struct ng_parse_type *type,
533 const char *s, int *off, const u_char *const start,
534 u_char *const buf, int *buflen)
536 long val; /* assumes long is at least 32 bits */
537 int32_t val32;
538 char *eptr;
540 if ((intptr_t)type->info == INT_SIGNED)
541 val = strtol(s + *off, &eptr, 0);
542 else
543 val = strtoul(s + *off, &eptr, 0);
544 if (val < (int32_t)0x80000000
545 || val > (u_int32_t)0xffffffff || eptr == s + *off)
546 return (EINVAL);
547 *off = eptr - s;
548 val32 = (int32_t)val;
549 bcopy(&val32, buf, sizeof(int32_t));
550 *buflen = sizeof(int32_t);
551 return (0);
554 static int
555 ng_int32_unparse(const struct ng_parse_type *type,
556 const u_char *data, int *off, char *cbuf, int cbuflen)
558 const char *fmt;
559 long fval;
560 int error;
561 int32_t val;
563 bcopy(data + *off, &val, sizeof(int32_t));
564 switch ((intptr_t)type->info) {
565 case INT_SIGNED:
566 fmt = "%ld";
567 fval = val;
568 break;
569 case INT_UNSIGNED:
570 fmt = "%lu";
571 fval = (u_int32_t)val;
572 break;
573 case INT_HEX:
574 fmt = "0x%lx";
575 fval = (u_int32_t)val;
576 break;
577 default:
578 panic("%s: unknown type", __func__);
579 #ifdef RESTARTABLE_PANICS
580 return(0);
581 #endif
583 if ((error = ng_parse_append(&cbuf, &cbuflen, fmt, fval)) != 0)
584 return (error);
585 *off += sizeof(int32_t);
586 return (0);
589 static int
590 ng_int32_getDefault(const struct ng_parse_type *type,
591 const u_char *const start, u_char *buf, int *buflen)
593 int32_t val;
595 if (*buflen < sizeof(int32_t))
596 return (ERANGE);
597 val = 0;
598 bcopy(&val, buf, sizeof(int32_t));
599 *buflen = sizeof(int32_t);
600 return (0);
603 static int
604 ng_int32_getAlign(const struct ng_parse_type *type)
606 return INT32_ALIGNMENT;
609 const struct ng_parse_type ng_parse_int32_type = {
610 NULL,
611 (void *)INT_SIGNED,
612 NULL,
613 ng_int32_parse,
614 ng_int32_unparse,
615 ng_int32_getDefault,
616 ng_int32_getAlign
619 const struct ng_parse_type ng_parse_uint32_type = {
620 &ng_parse_int32_type,
621 (void *)INT_UNSIGNED
624 const struct ng_parse_type ng_parse_hint32_type = {
625 &ng_parse_int32_type,
626 (void *)INT_HEX
629 /************************************************************************
630 INT64 TYPE
631 ************************************************************************/
633 static int
634 ng_int64_parse(const struct ng_parse_type *type,
635 const char *s, int *off, const u_char *const start,
636 u_char *const buf, int *buflen)
638 quad_t val;
639 int64_t val64;
640 char *eptr;
642 val = strtoq(s + *off, &eptr, 0);
643 if (eptr == s + *off)
644 return (EINVAL);
645 *off = eptr - s;
646 val64 = (int64_t)val;
647 bcopy(&val64, buf, sizeof(int64_t));
648 *buflen = sizeof(int64_t);
649 return (0);
652 static int
653 ng_int64_unparse(const struct ng_parse_type *type,
654 const u_char *data, int *off, char *cbuf, int cbuflen)
656 const char *fmt;
657 long long fval;
658 int64_t val;
659 int error;
661 bcopy(data + *off, &val, sizeof(int64_t));
662 switch ((intptr_t)type->info) {
663 case INT_SIGNED:
664 fmt = "%lld";
665 fval = val;
666 break;
667 case INT_UNSIGNED:
668 fmt = "%llu";
669 fval = (u_int64_t)val;
670 break;
671 case INT_HEX:
672 fmt = "0x%llx";
673 fval = (u_int64_t)val;
674 break;
675 default:
676 panic("%s: unknown type", __func__);
677 #ifdef RESTARTABLE_PANICS
678 return(0);
679 #endif
681 if ((error = ng_parse_append(&cbuf, &cbuflen, fmt, fval)) != 0)
682 return (error);
683 *off += sizeof(int64_t);
684 return (0);
687 static int
688 ng_int64_getDefault(const struct ng_parse_type *type,
689 const u_char *const start, u_char *buf, int *buflen)
691 int64_t val;
693 if (*buflen < sizeof(int64_t))
694 return (ERANGE);
695 val = 0;
696 bcopy(&val, buf, sizeof(int64_t));
697 *buflen = sizeof(int64_t);
698 return (0);
701 static int
702 ng_int64_getAlign(const struct ng_parse_type *type)
704 return INT64_ALIGNMENT;
707 const struct ng_parse_type ng_parse_int64_type = {
708 NULL,
709 (void *)INT_SIGNED,
710 NULL,
711 ng_int64_parse,
712 ng_int64_unparse,
713 ng_int64_getDefault,
714 ng_int64_getAlign
717 const struct ng_parse_type ng_parse_uint64_type = {
718 &ng_parse_int64_type,
719 (void *)INT_UNSIGNED
722 const struct ng_parse_type ng_parse_hint64_type = {
723 &ng_parse_int64_type,
724 (void *)INT_HEX
727 /************************************************************************
728 STRING TYPE
729 ************************************************************************/
731 static int
732 ng_string_parse(const struct ng_parse_type *type,
733 const char *s, int *off, const u_char *const start,
734 u_char *const buf, int *buflen)
736 char *sval;
737 int len;
738 int slen;
740 if ((sval = ng_get_string_token(s, off, &len, &slen)) == NULL)
741 return (EINVAL);
742 *off += len;
743 bcopy(sval, buf, slen + 1);
744 FREE(sval, M_NETGRAPH_PARSE);
745 *buflen = slen + 1;
746 return (0);
749 static int
750 ng_string_unparse(const struct ng_parse_type *type,
751 const u_char *data, int *off, char *cbuf, int cbuflen)
753 const char *const raw = (const char *)data + *off;
754 char *const s = ng_encode_string(raw, strlen(raw));
755 int error;
757 if (s == NULL)
758 return (ENOMEM);
759 if ((error = ng_parse_append(&cbuf, &cbuflen, "%s", s)) != 0) {
760 FREE(s, M_NETGRAPH_PARSE);
761 return (error);
763 *off += strlen(raw) + 1;
764 FREE(s, M_NETGRAPH_PARSE);
765 return (0);
768 static int
769 ng_string_getDefault(const struct ng_parse_type *type,
770 const u_char *const start, u_char *buf, int *buflen)
773 if (*buflen < 1)
774 return (ERANGE);
775 buf[0] = (u_char)'\0';
776 *buflen = 1;
777 return (0);
780 const struct ng_parse_type ng_parse_string_type = {
781 NULL,
782 NULL,
783 NULL,
784 ng_string_parse,
785 ng_string_unparse,
786 ng_string_getDefault,
787 NULL
790 /************************************************************************
791 FIXED BUFFER STRING TYPE
792 ************************************************************************/
794 static int
795 ng_fixedstring_parse(const struct ng_parse_type *type,
796 const char *s, int *off, const u_char *const start,
797 u_char *const buf, int *buflen)
799 const struct ng_parse_fixedstring_info *const fi = type->info;
800 char *sval;
801 int len;
802 int slen;
804 if ((sval = ng_get_string_token(s, off, &len, &slen)) == NULL)
805 return (EINVAL);
806 if (slen + 1 > fi->bufSize) {
807 FREE(sval, M_NETGRAPH_PARSE);
808 return (E2BIG);
810 *off += len;
811 bcopy(sval, buf, slen);
812 FREE(sval, M_NETGRAPH_PARSE);
813 bzero(buf + slen, fi->bufSize - slen);
814 *buflen = fi->bufSize;
815 return (0);
818 static int
819 ng_fixedstring_unparse(const struct ng_parse_type *type,
820 const u_char *data, int *off, char *cbuf, int cbuflen)
822 const struct ng_parse_fixedstring_info *const fi = type->info;
823 int error, temp = *off;
825 if ((error = ng_string_unparse(type, data, &temp, cbuf, cbuflen)) != 0)
826 return (error);
827 *off += fi->bufSize;
828 return (0);
831 static int
832 ng_fixedstring_getDefault(const struct ng_parse_type *type,
833 const u_char *const start, u_char *buf, int *buflen)
835 const struct ng_parse_fixedstring_info *const fi = type->info;
837 if (*buflen < fi->bufSize)
838 return (ERANGE);
839 bzero(buf, fi->bufSize);
840 *buflen = fi->bufSize;
841 return (0);
844 const struct ng_parse_type ng_parse_fixedstring_type = {
845 NULL,
846 NULL,
847 NULL,
848 ng_fixedstring_parse,
849 ng_fixedstring_unparse,
850 ng_fixedstring_getDefault,
851 NULL
854 const struct ng_parse_fixedstring_info ng_parse_nodebuf_info = {
855 NG_NODESIZ
857 const struct ng_parse_type ng_parse_nodebuf_type = {
858 &ng_parse_fixedstring_type,
859 &ng_parse_nodebuf_info
862 const struct ng_parse_fixedstring_info ng_parse_hookbuf_info = {
863 NG_HOOKSIZ
865 const struct ng_parse_type ng_parse_hookbuf_type = {
866 &ng_parse_fixedstring_type,
867 &ng_parse_hookbuf_info
870 const struct ng_parse_fixedstring_info ng_parse_pathbuf_info = {
871 NG_PATHSIZ
873 const struct ng_parse_type ng_parse_pathbuf_type = {
874 &ng_parse_fixedstring_type,
875 &ng_parse_pathbuf_info
878 const struct ng_parse_fixedstring_info ng_parse_typebuf_info = {
879 NG_TYPESIZ
881 const struct ng_parse_type ng_parse_typebuf_type = {
882 &ng_parse_fixedstring_type,
883 &ng_parse_typebuf_info
886 const struct ng_parse_fixedstring_info ng_parse_cmdbuf_info = {
887 NG_CMDSTRSIZ
889 const struct ng_parse_type ng_parse_cmdbuf_type = {
890 &ng_parse_fixedstring_type,
891 &ng_parse_cmdbuf_info
894 /************************************************************************
895 EXPLICITLY SIZED STRING TYPE
896 ************************************************************************/
898 static int
899 ng_sizedstring_parse(const struct ng_parse_type *type,
900 const char *s, int *off, const u_char *const start,
901 u_char *const buf, int *buflen)
903 char *sval;
904 int len;
905 int slen;
907 if ((sval = ng_get_string_token(s, off, &len, &slen)) == NULL)
908 return (EINVAL);
909 if (slen > USHRT_MAX) {
910 FREE(sval, M_NETGRAPH_PARSE);
911 return (EINVAL);
913 *off += len;
914 *((u_int16_t *)buf) = (u_int16_t)slen;
915 bcopy(sval, buf + 2, slen);
916 FREE(sval, M_NETGRAPH_PARSE);
917 *buflen = 2 + slen;
918 return (0);
921 static int
922 ng_sizedstring_unparse(const struct ng_parse_type *type,
923 const u_char *data, int *off, char *cbuf, int cbuflen)
925 const char *const raw = (const char *)data + *off + 2;
926 const int slen = *((const u_int16_t *)(data + *off));
927 char *const s = ng_encode_string(raw, slen);
928 int error;
930 if (s == NULL)
931 return (ENOMEM);
932 if ((error = ng_parse_append(&cbuf, &cbuflen, "%s", s)) != 0) {
933 FREE(s, M_NETGRAPH_PARSE);
934 return (error);
936 FREE(s, M_NETGRAPH_PARSE);
937 *off += slen + 2;
938 return (0);
941 static int
942 ng_sizedstring_getDefault(const struct ng_parse_type *type,
943 const u_char *const start, u_char *buf, int *buflen)
945 if (*buflen < 2)
946 return (ERANGE);
947 bzero(buf, 2);
948 *buflen = 2;
949 return (0);
952 const struct ng_parse_type ng_parse_sizedstring_type = {
953 NULL,
954 NULL,
955 NULL,
956 ng_sizedstring_parse,
957 ng_sizedstring_unparse,
958 ng_sizedstring_getDefault,
959 NULL
962 /************************************************************************
963 IP ADDRESS TYPE
964 ************************************************************************/
966 static int
967 ng_ipaddr_parse(const struct ng_parse_type *type,
968 const char *s, int *off, const u_char *const start,
969 u_char *const buf, int *buflen)
971 int i, error;
973 for (i = 0; i < 4; i++) {
974 if ((error = ng_int8_parse(&ng_parse_int8_type,
975 s, off, start, buf + i, buflen)) != 0)
976 return (error);
977 if (i < 3 && s[*off] != '.')
978 return (EINVAL);
979 (*off)++;
981 *buflen = 4;
982 return (0);
985 static int
986 ng_ipaddr_unparse(const struct ng_parse_type *type,
987 const u_char *data, int *off, char *cbuf, int cbuflen)
989 struct in_addr ip;
990 int error;
992 bcopy(data + *off, &ip, sizeof(ip));
993 if ((error = ng_parse_append(&cbuf, &cbuflen, "%d.%d.%d.%d",
994 ((u_char *)&ip)[0], ((u_char *)&ip)[1],
995 ((u_char *)&ip)[2], ((u_char *)&ip)[3])) != 0)
996 return (error);
997 *off += sizeof(ip);
998 return (0);
1001 static int
1002 ng_ipaddr_getDefault(const struct ng_parse_type *type,
1003 const u_char *const start, u_char *buf, int *buflen)
1005 struct in_addr ip = { 0 };
1007 if (*buflen < sizeof(ip))
1008 return (ERANGE);
1009 bcopy(&ip, buf, sizeof(ip));
1010 *buflen = sizeof(ip);
1011 return (0);
1014 const struct ng_parse_type ng_parse_ipaddr_type = {
1015 NULL,
1016 NULL,
1017 NULL,
1018 ng_ipaddr_parse,
1019 ng_ipaddr_unparse,
1020 ng_ipaddr_getDefault,
1021 ng_int32_getAlign
1024 /************************************************************************
1025 ETHERNET ADDRESS TYPE
1026 ************************************************************************/
1028 static int
1029 ng_enaddr_parse(const struct ng_parse_type *type,
1030 const char *s, int *const off, const u_char *const start,
1031 u_char *const buf, int *const buflen)
1033 char *eptr;
1034 u_long val;
1035 int i;
1037 if (*buflen < ETHER_ADDR_LEN)
1038 return (ERANGE);
1039 for (i = 0; i < ETHER_ADDR_LEN; i++) {
1040 val = strtoul(s + *off, &eptr, 16);
1041 if (val > 0xff || eptr == s + *off)
1042 return (EINVAL);
1043 buf[i] = (u_char)val;
1044 *off = (eptr - s);
1045 if (i < ETHER_ADDR_LEN - 1) {
1046 if (*eptr != ':')
1047 return (EINVAL);
1048 (*off)++;
1051 *buflen = ETHER_ADDR_LEN;
1052 return (0);
1055 static int
1056 ng_enaddr_unparse(const struct ng_parse_type *type,
1057 const u_char *data, int *off, char *cbuf, int cbuflen)
1059 int len;
1061 len = snprintf(cbuf, cbuflen, "%02x:%02x:%02x:%02x:%02x:%02x",
1062 data[*off], data[*off + 1], data[*off + 2],
1063 data[*off + 3], data[*off + 4], data[*off + 5]);
1064 if (len >= cbuflen)
1065 return (ERANGE);
1066 *off += ETHER_ADDR_LEN;
1067 return (0);
1070 const struct ng_parse_type ng_parse_enaddr_type = {
1071 NULL,
1072 NULL,
1073 NULL,
1074 ng_enaddr_parse,
1075 ng_enaddr_unparse,
1076 NULL,
1080 /************************************************************************
1081 BYTE ARRAY TYPE
1082 ************************************************************************/
1084 /* Get the length of a byte array */
1085 static int
1086 ng_parse_bytearray_subtype_getLength(const struct ng_parse_type *type,
1087 const u_char *start, const u_char *buf)
1089 ng_parse_array_getLength_t *const getLength = type->private;
1091 return (*getLength)(type, start, buf);
1094 /* Byte array element type is hex int8 */
1095 static const struct ng_parse_array_info ng_parse_bytearray_subtype_info = {
1096 &ng_parse_hint8_type,
1097 &ng_parse_bytearray_subtype_getLength,
1098 NULL
1100 static const struct ng_parse_type ng_parse_bytearray_subtype = {
1101 &ng_parse_array_type,
1102 &ng_parse_bytearray_subtype_info
1105 static int
1106 ng_bytearray_parse(const struct ng_parse_type *type,
1107 const char *s, int *off, const u_char *const start,
1108 u_char *const buf, int *buflen)
1110 char *str;
1111 int toklen;
1112 int slen;
1114 /* We accept either an array of bytes or a string constant */
1115 if ((str = ng_get_string_token(s, off, &toklen, &slen)) != NULL) {
1116 ng_parse_array_getLength_t *const getLength = type->info;
1117 int arraylen;
1119 arraylen = (*getLength)(type, start, buf);
1120 if (arraylen > *buflen) {
1121 FREE(str, M_NETGRAPH_PARSE);
1122 return (ERANGE);
1124 if (slen > arraylen) {
1125 FREE(str, M_NETGRAPH_PARSE);
1126 return (E2BIG);
1128 bcopy(str, buf, slen);
1129 bzero(buf + slen, arraylen - slen);
1130 FREE(str, M_NETGRAPH_PARSE);
1131 *off += toklen;
1132 *buflen = arraylen;
1133 return (0);
1134 } else {
1135 struct ng_parse_type subtype;
1137 subtype = ng_parse_bytearray_subtype;
1138 *(const void **)&subtype.private = type->info;
1139 return ng_array_parse(&subtype, s, off, start, buf, buflen);
1143 static int
1144 ng_bytearray_unparse(const struct ng_parse_type *type,
1145 const u_char *data, int *off, char *cbuf, int cbuflen)
1147 struct ng_parse_type subtype;
1149 subtype = ng_parse_bytearray_subtype;
1150 *(const void **)&subtype.private = type->info;
1151 return ng_array_unparse(&subtype, data, off, cbuf, cbuflen);
1154 static int
1155 ng_bytearray_getDefault(const struct ng_parse_type *type,
1156 const u_char *const start, u_char *buf, int *buflen)
1158 struct ng_parse_type subtype;
1160 subtype = ng_parse_bytearray_subtype;
1161 *(const void **)&subtype.private = type->info;
1162 return ng_array_getDefault(&subtype, start, buf, buflen);
1165 const struct ng_parse_type ng_parse_bytearray_type = {
1166 NULL,
1167 NULL,
1168 NULL,
1169 ng_bytearray_parse,
1170 ng_bytearray_unparse,
1171 ng_bytearray_getDefault,
1172 NULL
1175 /************************************************************************
1176 STRUCT NG_MESG TYPE
1177 ************************************************************************/
1179 /* Get msg->header.arglen when "buf" is pointing to msg->data */
1180 static int
1181 ng_parse_ng_mesg_getLength(const struct ng_parse_type *type,
1182 const u_char *start, const u_char *buf)
1184 const struct ng_mesg *msg;
1186 msg = (const struct ng_mesg *)(buf - sizeof(*msg));
1187 return msg->header.arglen;
1190 /* Type for the variable length data portion of a struct ng_mesg */
1191 static const struct ng_parse_type ng_msg_data_type = {
1192 &ng_parse_bytearray_type,
1193 &ng_parse_ng_mesg_getLength
1196 /* Type for the entire struct ng_mesg header with data section */
1197 static const struct ng_parse_struct_field ng_parse_ng_mesg_type_fields[]
1198 = NG_GENERIC_NG_MESG_INFO(&ng_msg_data_type);
1199 const struct ng_parse_type ng_parse_ng_mesg_type = {
1200 &ng_parse_struct_type,
1201 &ng_parse_ng_mesg_type_fields,
1204 /************************************************************************
1205 COMPOSITE HELPER ROUTINES
1206 ************************************************************************/
1209 * Convert a structure or array from ASCII to binary
1211 static int
1212 ng_parse_composite(const struct ng_parse_type *type, const char *s,
1213 int *off, const u_char *const start, u_char *const buf, int *buflen,
1214 const enum comptype ctype)
1216 const int num = ng_get_composite_len(type, start, buf, ctype);
1217 int nextIndex = 0; /* next implicit array index */
1218 u_int index; /* field or element index */
1219 int *foff; /* field value offsets in string */
1220 int align, len, blen, error = 0;
1222 /* Initialize */
1223 MALLOC(foff, int *, num * sizeof(*foff), M_NETGRAPH_PARSE, M_WAITOK | M_NULLOK | M_ZERO);
1224 if (foff == NULL) {
1225 error = ENOMEM;
1226 goto done;
1229 /* Get opening brace/bracket */
1230 if (ng_parse_get_token(s, off, &len)
1231 != (ctype == CT_STRUCT ? T_LBRACE : T_LBRACKET)) {
1232 error = EINVAL;
1233 goto done;
1235 *off += len;
1237 /* Get individual element value positions in the string */
1238 for (;;) {
1239 enum ng_parse_token tok;
1241 /* Check for closing brace/bracket */
1242 tok = ng_parse_get_token(s, off, &len);
1243 if (tok == (ctype == CT_STRUCT ? T_RBRACE : T_RBRACKET)) {
1244 *off += len;
1245 break;
1248 /* For arrays, the 'name' (ie, index) is optional, so
1249 distinguish name from values by seeing if the next
1250 token is an equals sign */
1251 if (ctype != CT_STRUCT) {
1252 int len2, off2;
1253 char *eptr;
1255 /* If an opening brace/bracket, index is implied */
1256 if (tok == T_LBRACE || tok == T_LBRACKET) {
1257 index = nextIndex++;
1258 goto gotIndex;
1261 /* Might be an index, might be a value, either way... */
1262 if (tok != T_WORD) {
1263 error = EINVAL;
1264 goto done;
1267 /* If no equals sign follows, index is implied */
1268 off2 = *off + len;
1269 if (ng_parse_get_token(s, &off2, &len2) != T_EQUALS) {
1270 index = nextIndex++;
1271 goto gotIndex;
1274 /* Index was specified explicitly; parse it */
1275 index = (u_int)strtoul(s + *off, &eptr, 0);
1276 if (index < 0 || eptr - (s + *off) != len) {
1277 error = EINVAL;
1278 goto done;
1280 nextIndex = index + 1;
1281 *off += len + len2;
1282 } else { /* a structure field */
1283 const struct ng_parse_struct_field *const
1284 fields = type->info;
1286 /* Find the field by name (required) in field list */
1287 if (tok != T_WORD) {
1288 error = EINVAL;
1289 goto done;
1291 for (index = 0; index < num; index++) {
1292 const struct ng_parse_struct_field *const
1293 field = &fields[index];
1295 if (strncmp(&s[*off], field->name, len) == 0
1296 && field->name[len] == '\0')
1297 break;
1299 if (index == num) {
1300 error = ENOENT;
1301 goto done;
1303 *off += len;
1305 /* Get equals sign */
1306 if (ng_parse_get_token(s, off, &len) != T_EQUALS) {
1307 error = EINVAL;
1308 goto done;
1310 *off += len;
1312 gotIndex:
1314 /* Check array index */
1315 if (index >= num) {
1316 error = E2BIG;
1317 goto done;
1320 /* Save value's position and skip over it for now */
1321 if (foff[index] != 0) {
1322 error = EALREADY; /* duplicate */
1323 goto done;
1325 while (isspace(s[*off]))
1326 (*off)++;
1327 foff[index] = *off;
1328 if ((error = ng_parse_skip_value(s, *off, &len)) != 0)
1329 goto done;
1330 *off += len;
1333 /* Now build binary structure from supplied values and defaults */
1334 for (blen = index = 0; index < num; index++) {
1335 const struct ng_parse_type *const
1336 etype = ng_get_composite_etype(type, index, ctype);
1337 int k, pad, vlen;
1339 /* Zero-pad any alignment bytes */
1340 pad = ng_parse_get_elem_pad(type, index, ctype, blen);
1341 for (k = 0; k < pad; k++) {
1342 if (blen >= *buflen) {
1343 error = ERANGE;
1344 goto done;
1346 buf[blen++] = 0;
1349 /* Get value */
1350 vlen = *buflen - blen;
1351 if (foff[index] == 0) { /* use default value */
1352 error = ng_get_composite_elem_default(type, index,
1353 start, buf + blen, &vlen, ctype);
1354 } else { /* parse given value */
1355 *off = foff[index];
1356 error = INVOKE(etype, parse)(etype,
1357 s, off, start, buf + blen, &vlen);
1359 if (error != 0)
1360 goto done;
1361 blen += vlen;
1364 /* Make total composite structure size a multiple of its alignment */
1365 if ((align = ALIGNMENT(type)) != 0) {
1366 while (blen % align != 0) {
1367 if (blen >= *buflen) {
1368 error = ERANGE;
1369 goto done;
1371 buf[blen++] = 0;
1375 /* Done */
1376 *buflen = blen;
1377 done:
1378 if (foff != NULL)
1379 FREE(foff, M_NETGRAPH_PARSE);
1380 return (error);
1384 * Convert an array or structure from binary to ASCII
1386 static int
1387 ng_unparse_composite(const struct ng_parse_type *type, const u_char *data,
1388 int *off, char *cbuf, int cbuflen, const enum comptype ctype)
1390 const struct ng_mesg *const hdr
1391 = (const struct ng_mesg *)(data - sizeof(*hdr));
1392 const int num = ng_get_composite_len(type, data, data + *off, ctype);
1393 const int workSize = 20 * 1024; /* XXX hard coded constant */
1394 int nextIndex = 0, didOne = 0;
1395 int error, index;
1396 u_char *workBuf;
1398 /* Get workspace for checking default values */
1399 MALLOC(workBuf, u_char *, workSize, M_NETGRAPH_PARSE, M_WAITOK | M_NULLOK);
1400 if (workBuf == NULL)
1401 return (ENOMEM);
1403 /* Opening brace/bracket */
1404 if ((error = ng_parse_append(&cbuf, &cbuflen, "%c",
1405 (ctype == CT_STRUCT) ? '{' : '[')) != 0)
1406 goto fail;
1408 /* Do each item */
1409 for (index = 0; index < num; index++) {
1410 const struct ng_parse_type *const
1411 etype = ng_get_composite_etype(type, index, ctype);
1413 /* Skip any alignment pad bytes */
1414 *off += ng_parse_get_elem_pad(type, index, ctype, *off);
1417 * See if element is equal to its default value; skip if so.
1418 * Copy struct ng_mesg header for types that peek into it.
1420 if (sizeof(*hdr) + *off < workSize) {
1421 int tempsize = workSize - sizeof(*hdr) - *off;
1423 bcopy(hdr, workBuf, sizeof(*hdr) + *off);
1424 if (ng_get_composite_elem_default(type, index, workBuf
1425 + sizeof(*hdr), workBuf + sizeof(*hdr) + *off,
1426 &tempsize, ctype) == 0
1427 && bcmp(workBuf + sizeof(*hdr) + *off,
1428 data + *off, tempsize) == 0) {
1429 *off += tempsize;
1430 continue;
1434 /* Print name= */
1435 if ((error = ng_parse_append(&cbuf, &cbuflen, " ")) != 0)
1436 goto fail;
1437 if (ctype != CT_STRUCT) {
1438 if (index != nextIndex) {
1439 nextIndex = index;
1440 if ((error = ng_parse_append(&cbuf,
1441 &cbuflen, "%d=", index)) != 0)
1442 goto fail;
1444 nextIndex++;
1445 } else {
1446 const struct ng_parse_struct_field *const
1447 fields = type->info;
1449 if ((error = ng_parse_append(&cbuf,
1450 &cbuflen, "%s=", fields[index].name)) != 0)
1451 goto fail;
1454 /* Print value */
1455 if ((error = INVOKE(etype, unparse)
1456 (etype, data, off, cbuf, cbuflen)) != 0) {
1457 FREE(workBuf, M_NETGRAPH_PARSE);
1458 return (error);
1460 cbuflen -= strlen(cbuf);
1461 cbuf += strlen(cbuf);
1462 didOne = 1;
1465 /* Closing brace/bracket */
1466 error = ng_parse_append(&cbuf, &cbuflen, "%s%c",
1467 didOne ? " " : "", (ctype == CT_STRUCT) ? '}' : ']');
1469 fail:
1470 /* Clean up after failure */
1471 FREE(workBuf, M_NETGRAPH_PARSE);
1472 return (error);
1476 * Generate the default value for an element of an array or structure
1477 * Returns EOPNOTSUPP if default value is unspecified.
1479 static int
1480 ng_get_composite_elem_default(const struct ng_parse_type *type,
1481 int index, const u_char *const start, u_char *buf, int *buflen,
1482 const enum comptype ctype)
1484 const struct ng_parse_type *etype;
1485 ng_getDefault_t *func;
1487 switch (ctype) {
1488 case CT_STRUCT:
1489 break;
1490 case CT_ARRAY:
1492 const struct ng_parse_array_info *const ai = type->info;
1494 if (ai->getDefault != NULL) {
1495 return (*ai->getDefault)(type,
1496 index, start, buf, buflen);
1498 break;
1500 case CT_FIXEDARRAY:
1502 const struct ng_parse_fixedarray_info *const fi = type->info;
1504 if (*fi->getDefault != NULL) {
1505 return (*fi->getDefault)(type,
1506 index, start, buf, buflen);
1508 break;
1510 default:
1511 panic("%s", __func__);
1514 /* Default to element type default */
1515 etype = ng_get_composite_etype(type, index, ctype);
1516 func = METHOD(etype, getDefault);
1517 if (func == NULL)
1518 return (EOPNOTSUPP);
1519 return (*func)(etype, start, buf, buflen);
1523 * Get the number of elements in a struct, variable or fixed array.
1525 static int
1526 ng_get_composite_len(const struct ng_parse_type *type,
1527 const u_char *const start, const u_char *buf,
1528 const enum comptype ctype)
1530 switch (ctype) {
1531 case CT_STRUCT:
1533 const struct ng_parse_struct_field *const fields = type->info;
1534 int numFields = 0;
1536 for (numFields = 0; ; numFields++) {
1537 const struct ng_parse_struct_field *const
1538 fi = &fields[numFields];
1540 if (fi->name == NULL)
1541 break;
1543 return (numFields);
1545 case CT_ARRAY:
1547 const struct ng_parse_array_info *const ai = type->info;
1549 return (*ai->getLength)(type, start, buf);
1551 case CT_FIXEDARRAY:
1553 const struct ng_parse_fixedarray_info *const fi = type->info;
1555 return fi->length;
1557 default:
1558 panic("%s", __func__);
1560 return (0);
1564 * Return the type of the index'th element of a composite structure
1566 static const struct ng_parse_type *
1567 ng_get_composite_etype(const struct ng_parse_type *type,
1568 int index, const enum comptype ctype)
1570 const struct ng_parse_type *etype = NULL;
1572 switch (ctype) {
1573 case CT_STRUCT:
1575 const struct ng_parse_struct_field *const fields = type->info;
1577 etype = fields[index].type;
1578 break;
1580 case CT_ARRAY:
1582 const struct ng_parse_array_info *const ai = type->info;
1584 etype = ai->elementType;
1585 break;
1587 case CT_FIXEDARRAY:
1589 const struct ng_parse_fixedarray_info *const fi = type->info;
1591 etype = fi->elementType;
1592 break;
1594 default:
1595 panic("%s", __func__);
1597 return (etype);
1601 * Get the number of bytes to skip to align for the next
1602 * element in a composite structure.
1604 static int
1605 ng_parse_get_elem_pad(const struct ng_parse_type *type,
1606 int index, enum comptype ctype, int posn)
1608 const struct ng_parse_type *const
1609 etype = ng_get_composite_etype(type, index, ctype);
1610 int align;
1612 /* Get element's alignment, and possibly override */
1613 align = ALIGNMENT(etype);
1614 if (ctype == CT_STRUCT) {
1615 const struct ng_parse_struct_field *const fields = type->info;
1617 if (fields[index].alignment != 0)
1618 align = fields[index].alignment;
1621 /* Return number of bytes to skip to align */
1622 return (align ? (align - (posn % align)) % align : 0);
1625 /************************************************************************
1626 PARSING HELPER ROUTINES
1627 ************************************************************************/
1630 * Append to a fixed length string buffer.
1632 static int
1633 ng_parse_append(char **cbufp, int *cbuflenp, const char *fmt, ...)
1635 va_list args;
1636 int len;
1638 va_start(args, fmt);
1639 len = vsnprintf(*cbufp, *cbuflenp, fmt, args);
1640 va_end(args);
1641 if (len >= *cbuflenp)
1642 return ERANGE;
1643 *cbufp += len;
1644 *cbuflenp -= len;
1646 return (0);
1650 * Skip over a value
1652 static int
1653 ng_parse_skip_value(const char *s, int off0, int *lenp)
1655 int len, nbracket, nbrace;
1656 int off = off0;
1658 len = nbracket = nbrace = 0;
1659 do {
1660 switch (ng_parse_get_token(s, &off, &len)) {
1661 case T_LBRACKET:
1662 nbracket++;
1663 break;
1664 case T_LBRACE:
1665 nbrace++;
1666 break;
1667 case T_RBRACKET:
1668 if (nbracket-- == 0)
1669 return (EINVAL);
1670 break;
1671 case T_RBRACE:
1672 if (nbrace-- == 0)
1673 return (EINVAL);
1674 break;
1675 case T_EOF:
1676 return (EINVAL);
1677 default:
1678 break;
1680 off += len;
1681 } while (nbracket > 0 || nbrace > 0);
1682 *lenp = off - off0;
1683 return (0);
1687 * Find the next token in the string, starting at offset *startp.
1688 * Returns the token type, with *startp pointing to the first char
1689 * and *lenp the length.
1691 enum ng_parse_token
1692 ng_parse_get_token(const char *s, int *startp, int *lenp)
1694 char *t;
1695 int i;
1697 while (isspace(s[*startp]))
1698 (*startp)++;
1699 switch (s[*startp]) {
1700 case '\0':
1701 *lenp = 0;
1702 return T_EOF;
1703 case '{':
1704 *lenp = 1;
1705 return T_LBRACE;
1706 case '}':
1707 *lenp = 1;
1708 return T_RBRACE;
1709 case '[':
1710 *lenp = 1;
1711 return T_LBRACKET;
1712 case ']':
1713 *lenp = 1;
1714 return T_RBRACKET;
1715 case '=':
1716 *lenp = 1;
1717 return T_EQUALS;
1718 case '"':
1719 if ((t = ng_get_string_token(s, startp, lenp, NULL)) == NULL)
1720 return T_ERROR;
1721 FREE(t, M_NETGRAPH_PARSE);
1722 return T_STRING;
1723 default:
1724 for (i = *startp + 1; s[i] != '\0' && !isspace(s[i])
1725 && s[i] != '{' && s[i] != '}' && s[i] != '['
1726 && s[i] != ']' && s[i] != '=' && s[i] != '"'; i++)
1728 *lenp = i - *startp;
1729 return T_WORD;
1734 * Get a string token, which must be enclosed in double quotes.
1735 * The normal C backslash escapes are recognized.
1737 char *
1738 ng_get_string_token(const char *s, int *startp, int *lenp, int *slenp)
1740 char *cbuf, *p;
1741 int start, off;
1742 int slen;
1744 while (isspace(s[*startp]))
1745 (*startp)++;
1746 start = *startp;
1747 if (s[*startp] != '"')
1748 return (NULL);
1749 MALLOC(cbuf, char *, strlen(s + start), M_NETGRAPH_PARSE, M_WAITOK | M_NULLOK);
1750 if (cbuf == NULL)
1751 return (NULL);
1752 strcpy(cbuf, s + start + 1);
1753 for (slen = 0, off = 1, p = cbuf; *p != '\0'; slen++, off++, p++) {
1754 if (*p == '"') {
1755 *p = '\0';
1756 *lenp = off + 1;
1757 if (slenp != NULL)
1758 *slenp = slen;
1759 return (cbuf);
1760 } else if (p[0] == '\\' && p[1] != '\0') {
1761 int x, k;
1762 char *v;
1764 strcpy(p, p + 1);
1765 v = p;
1766 switch (*p) {
1767 case 't':
1768 *v = '\t';
1769 off++;
1770 continue;
1771 case 'n':
1772 *v = '\n';
1773 off++;
1774 continue;
1775 case 'r':
1776 *v = '\r';
1777 off++;
1778 continue;
1779 case 'v':
1780 *v = '\v';
1781 off++;
1782 continue;
1783 case 'f':
1784 *v = '\f';
1785 off++;
1786 continue;
1787 case '"':
1788 *v = '"';
1789 off++;
1790 continue;
1791 case '0': case '1': case '2': case '3':
1792 case '4': case '5': case '6': case '7':
1793 for (x = k = 0;
1794 k < 3 && *v >= '0' && *v <= '7'; v++) {
1795 x = (x << 3) + (*v - '0');
1796 off++;
1798 *--v = (char)x;
1799 break;
1800 case 'x':
1801 for (v++, x = k = 0;
1802 k < 2 && isxdigit(*v); v++) {
1803 x = (x << 4) + (isdigit(*v) ?
1804 (*v - '0') :
1805 (tolower(*v) - 'a' + 10));
1806 off++;
1808 *--v = (char)x;
1809 break;
1810 default:
1811 continue;
1813 strcpy(p, v);
1816 FREE(cbuf, M_NETGRAPH_PARSE);
1817 return (NULL); /* no closing quote */
1821 * Encode a string so it can be safely put in double quotes.
1822 * Caller must free the result. Exactly "slen" characters
1823 * are encoded.
1825 char *
1826 ng_encode_string(const char *raw, int slen)
1828 char *cbuf;
1829 int off = 0;
1830 int i;
1832 MALLOC(cbuf, char *, strlen(raw) * 4 + 3, M_NETGRAPH_PARSE, M_WAITOK | M_NULLOK);
1833 if (cbuf == NULL)
1834 return (NULL);
1835 cbuf[off++] = '"';
1836 for (i = 0; i < slen; i++, raw++) {
1837 switch (*raw) {
1838 case '\t':
1839 cbuf[off++] = '\\';
1840 cbuf[off++] = 't';
1841 break;
1842 case '\f':
1843 cbuf[off++] = '\\';
1844 cbuf[off++] = 'f';
1845 break;
1846 case '\n':
1847 cbuf[off++] = '\\';
1848 cbuf[off++] = 'n';
1849 break;
1850 case '\r':
1851 cbuf[off++] = '\\';
1852 cbuf[off++] = 'r';
1853 break;
1854 case '\v':
1855 cbuf[off++] = '\\';
1856 cbuf[off++] = 'v';
1857 break;
1858 case '"':
1859 case '\\':
1860 cbuf[off++] = '\\';
1861 cbuf[off++] = *raw;
1862 break;
1863 default:
1864 if (*raw < 0x20 || *raw > 0x7e) {
1865 off += sprintf(cbuf + off,
1866 "\\x%02x", (u_char)*raw);
1867 break;
1869 cbuf[off++] = *raw;
1870 break;
1873 cbuf[off++] = '"';
1874 cbuf[off] = '\0';
1875 return (cbuf);
1878 /************************************************************************
1879 VIRTUAL METHOD LOOKUP
1880 ************************************************************************/
1882 static ng_parse_t *
1883 ng_get_parse_method(const struct ng_parse_type *t)
1885 while (t != NULL && t->parse == NULL)
1886 t = t->supertype;
1887 return (t ? t->parse : NULL);
1890 static ng_unparse_t *
1891 ng_get_unparse_method(const struct ng_parse_type *t)
1893 while (t != NULL && t->unparse == NULL)
1894 t = t->supertype;
1895 return (t ? t->unparse : NULL);
1898 static ng_getDefault_t *
1899 ng_get_getDefault_method(const struct ng_parse_type *t)
1901 while (t != NULL && t->getDefault == NULL)
1902 t = t->supertype;
1903 return (t ? t->getDefault : NULL);
1906 static ng_getAlign_t *
1907 ng_get_getAlign_method(const struct ng_parse_type *t)
1909 while (t != NULL && t->getAlign == NULL)
1910 t = t->supertype;
1911 return (t ? t->getAlign : NULL);