AMD64 - Fix many compile-time warnings. int/ptr type mismatches, %llx, etc.
[dragonfly.git] / sys / netgraph / netgraph / ng_parse.c
blob4869b837b3cc034906ea55b2e6f9f845e4b50ad1
2 /*
3 * ng_parse.c
5 * Copyright (c) 1999 Whistle Communications, Inc.
6 * All rights reserved.
7 *
8 * Subject to the following obligations and disclaimer of warranty, use and
9 * redistribution of this software, in source or object code forms, with or
10 * without modifications are expressly permitted by Whistle Communications;
11 * provided, however, that:
12 * 1. Any and all reproductions of the source or object code must include the
13 * copyright notice above and the following disclaimer of warranties; and
14 * 2. No rights are granted, in any manner or form, to use Whistle
15 * Communications, Inc. trademarks, including the mark "WHISTLE
16 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17 * such appears in the above copyright notice or in the software.
19 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
35 * OF SUCH DAMAGE.
37 * Author: Archie Cobbs <archie@freebsd.org>
39 * $Whistle: ng_parse.c,v 1.3 1999/11/29 01:43:48 archie Exp $
40 * $FreeBSD: src/sys/netgraph/ng_parse.c,v 1.3.2.8 2002/07/02 23:44:02 archie Exp $
41 * $DragonFly: src/sys/netgraph/netgraph/ng_parse.c,v 1.10 2008/01/05 14:02:39 swildner Exp $
44 #include <sys/types.h>
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/errno.h>
48 #include <sys/malloc.h>
49 #include <sys/ctype.h>
51 #include <netinet/in.h>
53 #include <netgraph/ng_message.h>
54 #include <netgraph/netgraph.h>
55 #include <netgraph/ng_parse.h>
57 /* Compute alignment for primitive integral types */
58 struct int16_temp {
59 char x;
60 int16_t y;
63 struct int32_temp {
64 char x;
65 int32_t y;
68 struct int64_temp {
69 char x;
70 int64_t y;
73 #define INT8_ALIGNMENT 1
74 #define INT16_ALIGNMENT ((int)&((struct int16_temp *)0)->y)
75 #define INT32_ALIGNMENT ((int)&((struct int32_temp *)0)->y)
76 #define INT64_ALIGNMENT ((int)&((struct int64_temp *)0)->y)
78 /* Output format for integral types */
79 #define INT_UNSIGNED 0
80 #define INT_SIGNED 1
81 #define INT_HEX 2
83 /* Type of composite object: struct, array, or fixedarray */
84 enum comptype {
85 CT_STRUCT,
86 CT_ARRAY,
87 CT_FIXEDARRAY,
90 /* Composite types helper functions */
91 static int ng_parse_composite(const struct ng_parse_type *type,
92 const char *s, int *off, const u_char *start,
93 u_char *const buf, int *buflen, enum comptype ctype);
94 static int ng_unparse_composite(const struct ng_parse_type *type,
95 const u_char *data, int *off, char *cbuf, int cbuflen,
96 enum comptype ctype);
97 static int ng_get_composite_elem_default(const struct ng_parse_type *type,
98 int index, const u_char *start, u_char *buf,
99 int *buflen, enum comptype ctype);
100 static int ng_get_composite_len(const struct ng_parse_type *type,
101 const u_char *start, const u_char *buf,
102 enum comptype ctype);
103 static const struct ng_parse_type *ng_get_composite_etype(const struct
104 ng_parse_type *type, int index, enum comptype ctype);
105 static int ng_parse_get_elem_pad(const struct ng_parse_type *type,
106 int index, enum comptype ctype, int posn);
108 /* Parsing helper functions */
109 static int ng_parse_skip_value(const char *s, int off, int *lenp);
111 /* Poor man's virtual method calls */
112 #define METHOD(t,m) (ng_get_ ## m ## _method(t))
113 #define INVOKE(t,m) (*METHOD(t,m))
115 static ng_parse_t *ng_get_parse_method(const struct ng_parse_type *t);
116 static ng_unparse_t *ng_get_unparse_method(const struct ng_parse_type *t);
117 static ng_getDefault_t *ng_get_getDefault_method(const
118 struct ng_parse_type *t);
119 static ng_getAlign_t *ng_get_getAlign_method(const struct ng_parse_type *t);
121 #define ALIGNMENT(t) (METHOD(t, getAlign) == NULL ? \
122 0 : INVOKE(t, getAlign)(t))
124 /* For converting binary to string */
125 #define NG_PARSE_APPEND(fmt, args...) \
126 do { \
127 int len; \
129 len = ksnprintf((cbuf), (cbuflen), \
130 fmt , ## args); \
131 if (len >= (cbuflen)) \
132 return (ERANGE); \
133 (cbuf) += len; \
134 (cbuflen) -= len; \
135 } while (0)
137 /************************************************************************
138 PUBLIC FUNCTIONS
139 ************************************************************************/
142 * Convert an ASCII string to binary according to the supplied type descriptor
145 ng_parse(const struct ng_parse_type *type,
146 const char *string, int *off, u_char *buf, int *buflen)
148 return INVOKE(type, parse)(type, string, off, buf, buf, buflen);
152 * Convert binary to an ASCII string according to the supplied type descriptor
155 ng_unparse(const struct ng_parse_type *type,
156 const u_char *data, char *cbuf, int cbuflen)
158 int off = 0;
160 return INVOKE(type, unparse)(type, data, &off, cbuf, cbuflen);
164 * Fill in the default value according to the supplied type descriptor
167 ng_parse_getDefault(const struct ng_parse_type *type, u_char *buf, int *buflen)
169 ng_getDefault_t *const func = METHOD(type, getDefault);
171 if (func == NULL)
172 return (EOPNOTSUPP);
173 return (*func)(type, buf, buf, buflen);
177 /************************************************************************
178 STRUCTURE TYPE
179 ************************************************************************/
181 static int
182 ng_struct_parse(const struct ng_parse_type *type,
183 const char *s, int *off, const u_char *const start,
184 u_char *const buf, int *buflen)
186 return ng_parse_composite(type, s, off, start, buf, buflen, CT_STRUCT);
189 static int
190 ng_struct_unparse(const struct ng_parse_type *type,
191 const u_char *data, int *off, char *cbuf, int cbuflen)
193 return ng_unparse_composite(type, data, off, cbuf, cbuflen, CT_STRUCT);
196 static int
197 ng_struct_getDefault(const struct ng_parse_type *type,
198 const u_char *const start, u_char *buf, int *buflen)
200 int off = 0;
202 return ng_parse_composite(type,
203 "{}", &off, start, buf, buflen, CT_STRUCT);
206 static int
207 ng_struct_getAlign(const struct ng_parse_type *type)
209 const struct ng_parse_struct_field *field;
210 int align = 0;
212 for (field = type->info; field->name != NULL; field++) {
213 int falign = ALIGNMENT(field->type);
215 if (falign > align)
216 align = falign;
218 return align;
221 const struct ng_parse_type ng_parse_struct_type = {
222 NULL,
223 NULL,
224 NULL,
225 ng_struct_parse,
226 ng_struct_unparse,
227 ng_struct_getDefault,
228 ng_struct_getAlign
231 /************************************************************************
232 FIXED LENGTH ARRAY TYPE
233 ************************************************************************/
235 static int
236 ng_fixedarray_parse(const struct ng_parse_type *type,
237 const char *s, int *off, const u_char *const start,
238 u_char *const buf, int *buflen)
240 return ng_parse_composite(type,
241 s, off, start, buf, buflen, CT_FIXEDARRAY);
244 static int
245 ng_fixedarray_unparse(const struct ng_parse_type *type,
246 const u_char *data, int *off, char *cbuf, int cbuflen)
248 return ng_unparse_composite(type,
249 data, off, cbuf, cbuflen, CT_FIXEDARRAY);
252 static int
253 ng_fixedarray_getDefault(const struct ng_parse_type *type,
254 const u_char *const start, u_char *buf, int *buflen)
256 int off = 0;
258 return ng_parse_composite(type,
259 "[]", &off, start, buf, buflen, CT_FIXEDARRAY);
262 static int
263 ng_fixedarray_getAlign(const struct ng_parse_type *type)
265 const struct ng_parse_fixedarray_info *fi = type->info;
267 return ALIGNMENT(fi->elementType);
270 const struct ng_parse_type ng_parse_fixedarray_type = {
271 NULL,
272 NULL,
273 NULL,
274 ng_fixedarray_parse,
275 ng_fixedarray_unparse,
276 ng_fixedarray_getDefault,
277 ng_fixedarray_getAlign
280 /************************************************************************
281 VARIABLE LENGTH ARRAY TYPE
282 ************************************************************************/
284 static int
285 ng_array_parse(const struct ng_parse_type *type,
286 const char *s, int *off, const u_char *const start,
287 u_char *const buf, int *buflen)
289 return ng_parse_composite(type, s, off, start, buf, buflen, CT_ARRAY);
292 static int
293 ng_array_unparse(const struct ng_parse_type *type,
294 const u_char *data, int *off, char *cbuf, int cbuflen)
296 return ng_unparse_composite(type, data, off, cbuf, cbuflen, CT_ARRAY);
299 static int
300 ng_array_getDefault(const struct ng_parse_type *type,
301 const u_char *const start, u_char *buf, int *buflen)
303 int off = 0;
305 return ng_parse_composite(type,
306 "[]", &off, start, buf, buflen, CT_ARRAY);
309 static int
310 ng_array_getAlign(const struct ng_parse_type *type)
312 const struct ng_parse_array_info *ai = type->info;
314 return ALIGNMENT(ai->elementType);
317 const struct ng_parse_type ng_parse_array_type = {
318 NULL,
319 NULL,
320 NULL,
321 ng_array_parse,
322 ng_array_unparse,
323 ng_array_getDefault,
324 ng_array_getAlign
327 /************************************************************************
328 INT8 TYPE
329 ************************************************************************/
331 static int
332 ng_int8_parse(const struct ng_parse_type *type,
333 const char *s, int *off, const u_char *const start,
334 u_char *const buf, int *buflen)
336 long val;
337 int8_t val8;
338 char *eptr;
340 val = strtol(s + *off, &eptr, 0);
341 if (val < (int8_t)0x80 || val > (u_int8_t)0xff || eptr == s + *off)
342 return (EINVAL);
343 *off = eptr - s;
344 val8 = (int8_t)val;
345 bcopy(&val8, buf, sizeof(int8_t));
346 *buflen = sizeof(int8_t);
347 return (0);
350 static int
351 ng_int8_unparse(const struct ng_parse_type *type,
352 const u_char *data, int *off, char *cbuf, int cbuflen)
354 const char *fmt;
355 int fval;
356 int8_t val;
358 bcopy(data + *off, &val, sizeof(int8_t));
359 switch ((int)(intptr_t)type->info) {
360 case INT_SIGNED:
361 fmt = "%d";
362 fval = val;
363 break;
364 case INT_UNSIGNED:
365 fmt = "%u";
366 fval = (u_int8_t)val;
367 break;
368 case INT_HEX:
369 fmt = "0x%x";
370 fval = (u_int8_t)val;
371 break;
372 default:
373 panic("%s: unknown type", __func__);
375 NG_PARSE_APPEND(fmt, fval);
376 *off += sizeof(int8_t);
377 return (0);
380 static int
381 ng_int8_getDefault(const struct ng_parse_type *type,
382 const u_char *const start, u_char *buf, int *buflen)
384 int8_t val;
386 if (*buflen < sizeof(int8_t))
387 return (ERANGE);
388 val = 0;
389 bcopy(&val, buf, sizeof(int8_t));
390 *buflen = sizeof(int8_t);
391 return (0);
394 static int
395 ng_int8_getAlign(const struct ng_parse_type *type)
397 return INT8_ALIGNMENT;
400 const struct ng_parse_type ng_parse_int8_type = {
401 NULL,
402 (void *)INT_SIGNED,
403 NULL,
404 ng_int8_parse,
405 ng_int8_unparse,
406 ng_int8_getDefault,
407 ng_int8_getAlign
410 const struct ng_parse_type ng_parse_uint8_type = {
411 &ng_parse_int8_type,
412 (void *)INT_UNSIGNED
415 const struct ng_parse_type ng_parse_hint8_type = {
416 &ng_parse_int8_type,
417 (void *)INT_HEX
420 /************************************************************************
421 INT16 TYPE
422 ************************************************************************/
424 static int
425 ng_int16_parse(const struct ng_parse_type *type,
426 const char *s, int *off, const u_char *const start,
427 u_char *const buf, int *buflen)
429 long val;
430 int16_t val16;
431 char *eptr;
433 val = strtol(s + *off, &eptr, 0);
434 if (val < (int16_t)0x8000
435 || val > (u_int16_t)0xffff || eptr == s + *off)
436 return (EINVAL);
437 *off = eptr - s;
438 val16 = (int16_t)val;
439 bcopy(&val16, buf, sizeof(int16_t));
440 *buflen = sizeof(int16_t);
441 return (0);
444 static int
445 ng_int16_unparse(const struct ng_parse_type *type,
446 const u_char *data, int *off, char *cbuf, int cbuflen)
448 const char *fmt;
449 int fval;
450 int16_t val;
452 bcopy(data + *off, &val, sizeof(int16_t));
453 switch ((int)(intptr_t)type->info) {
454 case INT_SIGNED:
455 fmt = "%d";
456 fval = val;
457 break;
458 case INT_UNSIGNED:
459 fmt = "%u";
460 fval = (u_int16_t)val;
461 break;
462 case INT_HEX:
463 fmt = "0x%x";
464 fval = (u_int16_t)val;
465 break;
466 default:
467 panic("%s: unknown type", __func__);
469 NG_PARSE_APPEND(fmt, fval);
470 *off += sizeof(int16_t);
471 return (0);
474 static int
475 ng_int16_getDefault(const struct ng_parse_type *type,
476 const u_char *const start, u_char *buf, int *buflen)
478 int16_t val;
480 if (*buflen < sizeof(int16_t))
481 return (ERANGE);
482 val = 0;
483 bcopy(&val, buf, sizeof(int16_t));
484 *buflen = sizeof(int16_t);
485 return (0);
488 static int
489 ng_int16_getAlign(const struct ng_parse_type *type)
491 return INT16_ALIGNMENT;
494 const struct ng_parse_type ng_parse_int16_type = {
495 NULL,
496 (void *)INT_SIGNED,
497 NULL,
498 ng_int16_parse,
499 ng_int16_unparse,
500 ng_int16_getDefault,
501 ng_int16_getAlign
504 const struct ng_parse_type ng_parse_uint16_type = {
505 &ng_parse_int16_type,
506 (void *)INT_UNSIGNED
509 const struct ng_parse_type ng_parse_hint16_type = {
510 &ng_parse_int16_type,
511 (void *)INT_HEX
514 /************************************************************************
515 INT32 TYPE
516 ************************************************************************/
518 static int
519 ng_int32_parse(const struct ng_parse_type *type,
520 const char *s, int *off, const u_char *const start,
521 u_char *const buf, int *buflen)
523 long val; /* assumes long is at least 32 bits */
524 int32_t val32;
525 char *eptr;
527 val = strtol(s + *off, &eptr, 0);
528 if (val < (int32_t)0x80000000
529 || val > (u_int32_t)0xffffffff || eptr == s + *off)
530 return (EINVAL);
531 *off = eptr - s;
532 val32 = (int32_t)val;
533 bcopy(&val32, buf, sizeof(int32_t));
534 *buflen = sizeof(int32_t);
535 return (0);
538 static int
539 ng_int32_unparse(const struct ng_parse_type *type,
540 const u_char *data, int *off, char *cbuf, int cbuflen)
542 const char *fmt;
543 long fval;
544 int32_t val;
546 bcopy(data + *off, &val, sizeof(int32_t));
547 switch ((int)(intptr_t)type->info) {
548 case INT_SIGNED:
549 fmt = "%ld";
550 fval = val;
551 break;
552 case INT_UNSIGNED:
553 fmt = "%lu";
554 fval = (u_int32_t)val;
555 break;
556 case INT_HEX:
557 fmt = "0x%lx";
558 fval = (u_int32_t)val;
559 break;
560 default:
561 panic("%s: unknown type", __func__);
563 NG_PARSE_APPEND(fmt, fval);
564 *off += sizeof(int32_t);
565 return (0);
568 static int
569 ng_int32_getDefault(const struct ng_parse_type *type,
570 const u_char *const start, u_char *buf, int *buflen)
572 int32_t val;
574 if (*buflen < sizeof(int32_t))
575 return (ERANGE);
576 val = 0;
577 bcopy(&val, buf, sizeof(int32_t));
578 *buflen = sizeof(int32_t);
579 return (0);
582 static int
583 ng_int32_getAlign(const struct ng_parse_type *type)
585 return INT32_ALIGNMENT;
588 const struct ng_parse_type ng_parse_int32_type = {
589 NULL,
590 (void *)INT_SIGNED,
591 NULL,
592 ng_int32_parse,
593 ng_int32_unparse,
594 ng_int32_getDefault,
595 ng_int32_getAlign
598 const struct ng_parse_type ng_parse_uint32_type = {
599 &ng_parse_int32_type,
600 (void *)INT_UNSIGNED
603 const struct ng_parse_type ng_parse_hint32_type = {
604 &ng_parse_int32_type,
605 (void *)INT_HEX
608 /************************************************************************
609 INT64 TYPE
610 ************************************************************************/
612 static int
613 ng_int64_parse(const struct ng_parse_type *type,
614 const char *s, int *off, const u_char *const start,
615 u_char *const buf, int *buflen)
617 quad_t val;
618 int64_t val64;
619 char *eptr;
621 val = strtoq(s + *off, &eptr, 0);
622 if (eptr == s + *off)
623 return (EINVAL);
624 *off = eptr - s;
625 val64 = (int64_t)val;
626 bcopy(&val64, buf, sizeof(int64_t));
627 *buflen = sizeof(int64_t);
628 return (0);
631 static int
632 ng_int64_unparse(const struct ng_parse_type *type,
633 const u_char *data, int *off, char *cbuf, int cbuflen)
635 const char *fmt;
636 long long fval;
637 int64_t val;
639 bcopy(data + *off, &val, sizeof(int64_t));
640 switch ((int)(intptr_t)type->info) {
641 case INT_SIGNED:
642 fmt = "%lld";
643 fval = val;
644 break;
645 case INT_UNSIGNED:
646 fmt = "%llu";
647 fval = (u_int64_t)val;
648 break;
649 case INT_HEX:
650 fmt = "0x%llx";
651 fval = (u_int64_t)val;
652 break;
653 default:
654 panic("%s: unknown type", __func__);
656 NG_PARSE_APPEND(fmt, fval);
657 *off += sizeof(int64_t);
658 return (0);
661 static int
662 ng_int64_getDefault(const struct ng_parse_type *type,
663 const u_char *const start, u_char *buf, int *buflen)
665 int64_t val;
667 if (*buflen < sizeof(int64_t))
668 return (ERANGE);
669 val = 0;
670 bcopy(&val, buf, sizeof(int64_t));
671 *buflen = sizeof(int64_t);
672 return (0);
675 static int
676 ng_int64_getAlign(const struct ng_parse_type *type)
678 return INT64_ALIGNMENT;
681 const struct ng_parse_type ng_parse_int64_type = {
682 NULL,
683 (void *)INT_SIGNED,
684 NULL,
685 ng_int64_parse,
686 ng_int64_unparse,
687 ng_int64_getDefault,
688 ng_int64_getAlign
691 const struct ng_parse_type ng_parse_uint64_type = {
692 &ng_parse_int64_type,
693 (void *)INT_UNSIGNED
696 const struct ng_parse_type ng_parse_hint64_type = {
697 &ng_parse_int64_type,
698 (void *)INT_HEX
701 /************************************************************************
702 STRING TYPE
703 ************************************************************************/
705 static int
706 ng_string_parse(const struct ng_parse_type *type,
707 const char *s, int *off, const u_char *const start,
708 u_char *const buf, int *buflen)
710 char *sval;
711 int len;
713 if ((sval = ng_get_string_token(s, off, &len)) == NULL)
714 return (EINVAL);
715 *off += len;
716 len = strlen(sval) + 1;
717 bcopy(sval, buf, len);
718 FREE(sval, M_NETGRAPH);
719 *buflen = len;
720 return (0);
723 static int
724 ng_string_unparse(const struct ng_parse_type *type,
725 const u_char *data, int *off, char *cbuf, int cbuflen)
727 const char *const raw = (const char *)data + *off;
728 char *const s = ng_encode_string(raw);
730 if (s == NULL)
731 return (ENOMEM);
732 NG_PARSE_APPEND("%s", s);
733 *off += strlen(raw) + 1;
734 FREE(s, M_NETGRAPH);
735 return (0);
738 static int
739 ng_string_getDefault(const struct ng_parse_type *type,
740 const u_char *const start, u_char *buf, int *buflen)
743 if (*buflen < 1)
744 return (ERANGE);
745 buf[0] = (u_char)'\0';
746 *buflen = 1;
747 return (0);
750 const struct ng_parse_type ng_parse_string_type = {
751 NULL,
752 NULL,
753 NULL,
754 ng_string_parse,
755 ng_string_unparse,
756 ng_string_getDefault,
757 NULL
760 /************************************************************************
761 FIXED BUFFER STRING TYPE
762 ************************************************************************/
764 static int
765 ng_fixedstring_parse(const struct ng_parse_type *type,
766 const char *s, int *off, const u_char *const start,
767 u_char *const buf, int *buflen)
769 const struct ng_parse_fixedstring_info *const fi = type->info;
770 char *sval;
771 int len;
773 if ((sval = ng_get_string_token(s, off, &len)) == NULL)
774 return (EINVAL);
775 if (strlen(sval) + 1 > fi->bufSize)
776 return (E2BIG);
777 *off += len;
778 len = strlen(sval) + 1;
779 bcopy(sval, buf, len);
780 FREE(sval, M_NETGRAPH);
781 bzero(buf + len, fi->bufSize - len);
782 *buflen = fi->bufSize;
783 return (0);
786 static int
787 ng_fixedstring_unparse(const struct ng_parse_type *type,
788 const u_char *data, int *off, char *cbuf, int cbuflen)
790 const struct ng_parse_fixedstring_info *const fi = type->info;
791 int error, temp = *off;
793 if ((error = ng_string_unparse(type, data, &temp, cbuf, cbuflen)) != 0)
794 return (error);
795 *off += fi->bufSize;
796 return (0);
799 static int
800 ng_fixedstring_getDefault(const struct ng_parse_type *type,
801 const u_char *const start, u_char *buf, int *buflen)
803 const struct ng_parse_fixedstring_info *const fi = type->info;
805 if (*buflen < fi->bufSize)
806 return (ERANGE);
807 bzero(buf, fi->bufSize);
808 *buflen = fi->bufSize;
809 return (0);
812 const struct ng_parse_type ng_parse_fixedstring_type = {
813 NULL,
814 NULL,
815 NULL,
816 ng_fixedstring_parse,
817 ng_fixedstring_unparse,
818 ng_fixedstring_getDefault,
819 NULL
822 const struct ng_parse_fixedstring_info ng_parse_nodebuf_info = {
823 NG_NODESIZ
825 const struct ng_parse_type ng_parse_nodebuf_type = {
826 &ng_parse_fixedstring_type,
827 &ng_parse_nodebuf_info
830 const struct ng_parse_fixedstring_info ng_parse_hookbuf_info = {
831 NG_HOOKSIZ
833 const struct ng_parse_type ng_parse_hookbuf_type = {
834 &ng_parse_fixedstring_type,
835 &ng_parse_hookbuf_info
838 const struct ng_parse_fixedstring_info ng_parse_pathbuf_info = {
839 NG_PATHSIZ
841 const struct ng_parse_type ng_parse_pathbuf_type = {
842 &ng_parse_fixedstring_type,
843 &ng_parse_pathbuf_info
846 const struct ng_parse_fixedstring_info ng_parse_typebuf_info = {
847 NG_TYPESIZ
849 const struct ng_parse_type ng_parse_typebuf_type = {
850 &ng_parse_fixedstring_type,
851 &ng_parse_typebuf_info
854 const struct ng_parse_fixedstring_info ng_parse_cmdbuf_info = {
855 NG_CMDSTRSIZ
857 const struct ng_parse_type ng_parse_cmdbuf_type = {
858 &ng_parse_fixedstring_type,
859 &ng_parse_cmdbuf_info
862 /************************************************************************
863 IP ADDRESS TYPE
864 ************************************************************************/
866 static int
867 ng_ipaddr_parse(const struct ng_parse_type *type,
868 const char *s, int *off, const u_char *const start,
869 u_char *const buf, int *buflen)
871 int i, error;
873 for (i = 0; i < 4; i++) {
874 if ((error = ng_int8_parse(&ng_parse_int8_type,
875 s, off, start, buf + i, buflen)) != 0)
876 return (error);
877 if (i < 3 && s[*off] != '.')
878 return (EINVAL);
879 (*off)++;
881 *buflen = 4;
882 return (0);
885 static int
886 ng_ipaddr_unparse(const struct ng_parse_type *type,
887 const u_char *data, int *off, char *cbuf, int cbuflen)
889 struct in_addr ip;
891 bcopy(data + *off, &ip, sizeof(ip));
892 NG_PARSE_APPEND("%d.%d.%d.%d", ((u_char *)&ip)[0],
893 ((u_char *)&ip)[1], ((u_char *)&ip)[2], ((u_char *)&ip)[3]);
894 *off += sizeof(ip);
895 return (0);
898 static int
899 ng_ipaddr_getDefault(const struct ng_parse_type *type,
900 const u_char *const start, u_char *buf, int *buflen)
902 struct in_addr ip = { 0 };
904 if (*buflen < sizeof(ip))
905 return (ERANGE);
906 bcopy(&ip, buf, sizeof(ip));
907 *buflen = sizeof(ip);
908 return (0);
911 const struct ng_parse_type ng_parse_ipaddr_type = {
912 NULL,
913 NULL,
914 NULL,
915 ng_ipaddr_parse,
916 ng_ipaddr_unparse,
917 ng_ipaddr_getDefault,
918 ng_int32_getAlign
921 /************************************************************************
922 BYTE ARRAY TYPE
923 ************************************************************************/
925 /* Get the length of a byte array */
926 static int
927 ng_parse_bytearray_subtype_getLength(const struct ng_parse_type *type,
928 const u_char *start, const u_char *buf)
930 ng_parse_array_getLength_t *const getLength = type->private;
932 return (*getLength)(type, start, buf);
935 /* Byte array element type is hex int8 */
936 static const struct ng_parse_array_info ng_parse_bytearray_subtype_info = {
937 &ng_parse_hint8_type,
938 &ng_parse_bytearray_subtype_getLength,
939 NULL
941 static const struct ng_parse_type ng_parse_bytearray_subtype = {
942 &ng_parse_array_type,
943 &ng_parse_bytearray_subtype_info
946 static int
947 ng_bytearray_parse(const struct ng_parse_type *type,
948 const char *s, int *off, const u_char *const start,
949 u_char *const buf, int *buflen)
951 char *str;
952 int toklen;
954 /* We accept either an array of bytes or a string constant */
955 if ((str = ng_get_string_token(s, off, &toklen)) != NULL) {
956 ng_parse_array_getLength_t *const getLength = type->info;
957 int arraylen, slen;
959 arraylen = (*getLength)(type, start, buf);
960 if (arraylen > *buflen) {
961 FREE(str, M_NETGRAPH);
962 return (ERANGE);
964 slen = strlen(str) + 1;
965 if (slen > arraylen) {
966 FREE(str, M_NETGRAPH);
967 return (E2BIG);
969 bcopy(str, buf, slen);
970 bzero(buf + slen, arraylen - slen);
971 FREE(str, M_NETGRAPH);
972 *off += toklen;
973 *buflen = arraylen;
974 return (0);
975 } else {
976 struct ng_parse_type subtype;
978 subtype = ng_parse_bytearray_subtype;
979 *(const void **)&subtype.private = type->info;
980 return ng_array_parse(&subtype, s, off, start, buf, buflen);
984 static int
985 ng_bytearray_unparse(const struct ng_parse_type *type,
986 const u_char *data, int *off, char *cbuf, int cbuflen)
988 struct ng_parse_type subtype;
990 subtype = ng_parse_bytearray_subtype;
991 *(const void **)&subtype.private = type->info;
992 return ng_array_unparse(&subtype, data, off, cbuf, cbuflen);
995 static int
996 ng_bytearray_getDefault(const struct ng_parse_type *type,
997 const u_char *const start, u_char *buf, int *buflen)
999 struct ng_parse_type subtype;
1001 subtype = ng_parse_bytearray_subtype;
1002 *(const void **)&subtype.private = type->info;
1003 return ng_array_getDefault(&subtype, start, buf, buflen);
1006 const struct ng_parse_type ng_parse_bytearray_type = {
1007 NULL,
1008 NULL,
1009 NULL,
1010 ng_bytearray_parse,
1011 ng_bytearray_unparse,
1012 ng_bytearray_getDefault,
1013 NULL
1016 /************************************************************************
1017 STRUCT NG_MESG TYPE
1018 ************************************************************************/
1020 /* Get msg->header.arglen when "buf" is pointing to msg->data */
1021 static int
1022 ng_parse_ng_mesg_getLength(const struct ng_parse_type *type,
1023 const u_char *start, const u_char *buf)
1025 const struct ng_mesg *msg;
1027 msg = (const struct ng_mesg *)(buf - sizeof(*msg));
1028 return msg->header.arglen;
1031 /* Type for the variable length data portion of a struct ng_mesg */
1032 static const struct ng_parse_type ng_msg_data_type = {
1033 &ng_parse_bytearray_type,
1034 &ng_parse_ng_mesg_getLength
1037 /* Type for the entire struct ng_mesg header with data section */
1038 static const struct ng_parse_struct_field ng_parse_ng_mesg_type_fields[]
1039 = NG_GENERIC_NG_MESG_INFO(&ng_msg_data_type);
1040 const struct ng_parse_type ng_parse_ng_mesg_type = {
1041 &ng_parse_struct_type,
1042 &ng_parse_ng_mesg_type_fields,
1045 /************************************************************************
1046 COMPOSITE HELPER ROUTINES
1047 ************************************************************************/
1050 * Convert a structure or array from ASCII to binary
1052 static int
1053 ng_parse_composite(const struct ng_parse_type *type, const char *s,
1054 int *off, const u_char *const start, u_char *const buf, int *buflen,
1055 const enum comptype ctype)
1057 const int num = ng_get_composite_len(type, start, buf, ctype);
1058 int nextIndex = 0; /* next implicit array index */
1059 u_int index; /* field or element index */
1060 int *foff; /* field value offsets in string */
1061 int align, len, blen, error = 0;
1063 /* Initialize */
1064 MALLOC(foff, int *, num * sizeof(*foff), M_NETGRAPH, M_NOWAIT | M_ZERO);
1065 if (foff == NULL) {
1066 error = ENOMEM;
1067 goto done;
1070 /* Get opening brace/bracket */
1071 if (ng_parse_get_token(s, off, &len)
1072 != (ctype == CT_STRUCT ? T_LBRACE : T_LBRACKET)) {
1073 error = EINVAL;
1074 goto done;
1076 *off += len;
1078 /* Get individual element value positions in the string */
1079 for (;;) {
1080 enum ng_parse_token tok;
1082 /* Check for closing brace/bracket */
1083 tok = ng_parse_get_token(s, off, &len);
1084 if (tok == (ctype == CT_STRUCT ? T_RBRACE : T_RBRACKET)) {
1085 *off += len;
1086 break;
1089 /* For arrays, the 'name' (ie, index) is optional, so
1090 distinguish name from values by seeing if the next
1091 token is an equals sign */
1092 if (ctype != CT_STRUCT) {
1093 int len2, off2;
1094 char *eptr;
1096 /* If an opening brace/bracket, index is implied */
1097 if (tok == T_LBRACE || tok == T_LBRACKET) {
1098 index = nextIndex++;
1099 goto gotIndex;
1102 /* Might be an index, might be a value, either way... */
1103 if (tok != T_WORD) {
1104 error = EINVAL;
1105 goto done;
1108 /* If no equals sign follows, index is implied */
1109 off2 = *off + len;
1110 if (ng_parse_get_token(s, &off2, &len2) != T_EQUALS) {
1111 index = nextIndex++;
1112 goto gotIndex;
1115 /* Index was specified explicitly; parse it */
1116 index = (u_int)strtoul(s + *off, &eptr, 0);
1117 if (index < 0 || eptr - (s + *off) != len) {
1118 error = EINVAL;
1119 goto done;
1121 nextIndex = index + 1;
1122 *off += len + len2;
1123 gotIndex:
1124 ; /* avoid gcc3.x warning */
1125 } else { /* a structure field */
1126 const struct ng_parse_struct_field *const
1127 fields = type->info;
1129 /* Find the field by name (required) in field list */
1130 if (tok != T_WORD) {
1131 error = EINVAL;
1132 goto done;
1134 for (index = 0; index < num; index++) {
1135 const struct ng_parse_struct_field *const
1136 field = &fields[index];
1138 if (strncmp(&s[*off], field->name, len) == 0
1139 && field->name[len] == '\0')
1140 break;
1142 if (index == num) {
1143 error = ENOENT;
1144 goto done;
1146 *off += len;
1148 /* Get equals sign */
1149 if (ng_parse_get_token(s, off, &len) != T_EQUALS) {
1150 error = EINVAL;
1151 goto done;
1153 *off += len;
1156 /* Check array index */
1157 if (index >= num) {
1158 error = E2BIG;
1159 goto done;
1162 /* Save value's position and skip over it for now */
1163 if (foff[index] != 0) {
1164 error = EALREADY; /* duplicate */
1165 goto done;
1167 while (isspace(s[*off]))
1168 (*off)++;
1169 foff[index] = *off;
1170 if ((error = ng_parse_skip_value(s, *off, &len)) != 0)
1171 goto done;
1172 *off += len;
1175 /* Now build binary structure from supplied values and defaults */
1176 for (blen = index = 0; index < num; index++) {
1177 const struct ng_parse_type *const
1178 etype = ng_get_composite_etype(type, index, ctype);
1179 int k, pad, vlen;
1181 /* Zero-pad any alignment bytes */
1182 pad = ng_parse_get_elem_pad(type, index, ctype, blen);
1183 for (k = 0; k < pad; k++) {
1184 if (blen >= *buflen) {
1185 error = ERANGE;
1186 goto done;
1188 buf[blen++] = 0;
1191 /* Get value */
1192 vlen = *buflen - blen;
1193 if (foff[index] == 0) { /* use default value */
1194 error = ng_get_composite_elem_default(type, index,
1195 start, buf + blen, &vlen, ctype);
1196 } else { /* parse given value */
1197 *off = foff[index];
1198 error = INVOKE(etype, parse)(etype,
1199 s, off, start, buf + blen, &vlen);
1201 if (error != 0)
1202 goto done;
1203 blen += vlen;
1206 /* Make total composite structure size a multiple of its alignment */
1207 if ((align = ALIGNMENT(type)) != 0) {
1208 while (blen % align != 0) {
1209 if (blen >= *buflen) {
1210 error = ERANGE;
1211 goto done;
1213 buf[blen++] = 0;
1217 /* Done */
1218 *buflen = blen;
1219 done:
1220 if (foff != NULL)
1221 FREE(foff, M_NETGRAPH);
1222 return (error);
1226 * Convert an array or structure from binary to ASCII
1228 static int
1229 ng_unparse_composite(const struct ng_parse_type *type, const u_char *data,
1230 int *off, char *cbuf, int cbuflen, const enum comptype ctype)
1232 const struct ng_mesg *const hdr
1233 = (const struct ng_mesg *)(data - sizeof(*hdr));
1234 const int num = ng_get_composite_len(type, data, data + *off, ctype);
1235 const int workSize = 20 * 1024; /* XXX hard coded constant */
1236 int nextIndex = 0, didOne = 0;
1237 int error, index;
1238 u_char *workBuf;
1240 /* Get workspace for checking default values */
1241 MALLOC(workBuf, u_char *, workSize, M_NETGRAPH, M_NOWAIT);
1242 if (workBuf == NULL)
1243 return (ENOMEM);
1245 /* Opening brace/bracket */
1246 NG_PARSE_APPEND("%c", (ctype == CT_STRUCT) ? '{' : '[');
1248 /* Do each item */
1249 for (index = 0; index < num; index++) {
1250 const struct ng_parse_type *const
1251 etype = ng_get_composite_etype(type, index, ctype);
1253 /* Skip any alignment pad bytes */
1254 *off += ng_parse_get_elem_pad(type, index, ctype, *off);
1257 * See if element is equal to its default value; skip if so.
1258 * Copy struct ng_mesg header for types that peek into it.
1260 if (sizeof(*hdr) + *off < workSize) {
1261 int tempsize = workSize - sizeof(*hdr) - *off;
1263 bcopy(hdr, workBuf, sizeof(*hdr) + *off);
1264 if (ng_get_composite_elem_default(type, index, workBuf
1265 + sizeof(*hdr), workBuf + sizeof(*hdr) + *off,
1266 &tempsize, ctype) == 0
1267 && bcmp(workBuf + sizeof(*hdr) + *off,
1268 data + *off, tempsize) == 0) {
1269 *off += tempsize;
1270 continue;
1274 /* Print name= */
1275 NG_PARSE_APPEND(" ");
1276 if (ctype != CT_STRUCT) {
1277 if (index != nextIndex) {
1278 nextIndex = index;
1279 NG_PARSE_APPEND("%d=", index);
1281 nextIndex++;
1282 } else {
1283 const struct ng_parse_struct_field *const
1284 fields = type->info;
1286 NG_PARSE_APPEND("%s=", fields[index].name);
1289 /* Print value */
1290 if ((error = INVOKE(etype, unparse)
1291 (etype, data, off, cbuf, cbuflen)) != 0) {
1292 FREE(workBuf, M_NETGRAPH);
1293 return (error);
1295 cbuflen -= strlen(cbuf);
1296 cbuf += strlen(cbuf);
1297 didOne = 1;
1299 FREE(workBuf, M_NETGRAPH);
1301 /* Closing brace/bracket */
1302 NG_PARSE_APPEND("%s%c",
1303 didOne ? " " : "", (ctype == CT_STRUCT) ? '}' : ']');
1304 return (0);
1308 * Generate the default value for an element of an array or structure
1309 * Returns EOPNOTSUPP if default value is unspecified.
1311 static int
1312 ng_get_composite_elem_default(const struct ng_parse_type *type,
1313 int index, const u_char *const start, u_char *buf, int *buflen,
1314 const enum comptype ctype)
1316 const struct ng_parse_type *etype;
1317 ng_getDefault_t *func;
1319 switch (ctype) {
1320 case CT_STRUCT:
1321 break;
1322 case CT_ARRAY:
1324 const struct ng_parse_array_info *const ai = type->info;
1326 if (ai->getDefault != NULL) {
1327 return (*ai->getDefault)(type,
1328 index, start, buf, buflen);
1330 break;
1332 case CT_FIXEDARRAY:
1334 const struct ng_parse_fixedarray_info *const fi = type->info;
1336 if (*fi->getDefault != NULL) {
1337 return (*fi->getDefault)(type,
1338 index, start, buf, buflen);
1340 break;
1342 default:
1343 panic("%s", __func__);
1346 /* Default to element type default */
1347 etype = ng_get_composite_etype(type, index, ctype);
1348 func = METHOD(etype, getDefault);
1349 if (func == NULL)
1350 return (EOPNOTSUPP);
1351 return (*func)(etype, start, buf, buflen);
1355 * Get the number of elements in a struct, variable or fixed array.
1357 static int
1358 ng_get_composite_len(const struct ng_parse_type *type,
1359 const u_char *const start, const u_char *buf,
1360 const enum comptype ctype)
1362 switch (ctype) {
1363 case CT_STRUCT:
1365 const struct ng_parse_struct_field *const fields = type->info;
1366 int numFields = 0;
1368 for (numFields = 0; ; numFields++) {
1369 const struct ng_parse_struct_field *const
1370 fi = &fields[numFields];
1372 if (fi->name == NULL)
1373 break;
1375 return (numFields);
1377 case CT_ARRAY:
1379 const struct ng_parse_array_info *const ai = type->info;
1381 return (*ai->getLength)(type, start, buf);
1383 case CT_FIXEDARRAY:
1385 const struct ng_parse_fixedarray_info *const fi = type->info;
1387 return fi->length;
1389 default:
1390 panic("%s", __func__);
1392 return (0);
1396 * Return the type of the index'th element of a composite structure
1398 static const struct ng_parse_type *
1399 ng_get_composite_etype(const struct ng_parse_type *type,
1400 int index, const enum comptype ctype)
1402 const struct ng_parse_type *etype = NULL;
1404 switch (ctype) {
1405 case CT_STRUCT:
1407 const struct ng_parse_struct_field *const fields = type->info;
1409 etype = fields[index].type;
1410 break;
1412 case CT_ARRAY:
1414 const struct ng_parse_array_info *const ai = type->info;
1416 etype = ai->elementType;
1417 break;
1419 case CT_FIXEDARRAY:
1421 const struct ng_parse_fixedarray_info *const fi = type->info;
1423 etype = fi->elementType;
1424 break;
1426 default:
1427 panic("%s", __func__);
1429 return (etype);
1433 * Get the number of bytes to skip to align for the next
1434 * element in a composite structure.
1436 static int
1437 ng_parse_get_elem_pad(const struct ng_parse_type *type,
1438 int index, enum comptype ctype, int posn)
1440 const struct ng_parse_type *const
1441 etype = ng_get_composite_etype(type, index, ctype);
1442 int align;
1444 /* Get element's alignment, and possibly override */
1445 align = ALIGNMENT(etype);
1446 if (ctype == CT_STRUCT) {
1447 const struct ng_parse_struct_field *const fields = type->info;
1449 if (fields[index].alignment != 0)
1450 align = fields[index].alignment;
1453 /* Return number of bytes to skip to align */
1454 return (align ? (align - (posn % align)) % align : 0);
1457 /************************************************************************
1458 PARSING HELPER ROUTINES
1459 ************************************************************************/
1462 * Skip over a value
1464 static int
1465 ng_parse_skip_value(const char *s, int off0, int *lenp)
1467 int len, nbracket, nbrace;
1468 int off = off0;
1470 len = nbracket = nbrace = 0;
1471 do {
1472 switch (ng_parse_get_token(s, &off, &len)) {
1473 case T_LBRACKET:
1474 nbracket++;
1475 break;
1476 case T_LBRACE:
1477 nbrace++;
1478 break;
1479 case T_RBRACKET:
1480 if (nbracket-- == 0)
1481 return (EINVAL);
1482 break;
1483 case T_RBRACE:
1484 if (nbrace-- == 0)
1485 return (EINVAL);
1486 break;
1487 case T_EOF:
1488 return (EINVAL);
1489 default:
1490 break;
1492 off += len;
1493 } while (nbracket > 0 || nbrace > 0);
1494 *lenp = off - off0;
1495 return (0);
1499 * Find the next token in the string, starting at offset *startp.
1500 * Returns the token type, with *startp pointing to the first char
1501 * and *lenp the length.
1503 enum ng_parse_token
1504 ng_parse_get_token(const char *s, int *startp, int *lenp)
1506 char *t;
1507 int i;
1509 while (isspace(s[*startp]))
1510 (*startp)++;
1511 switch (s[*startp]) {
1512 case '\0':
1513 *lenp = 0;
1514 return T_EOF;
1515 case '{':
1516 *lenp = 1;
1517 return T_LBRACE;
1518 case '}':
1519 *lenp = 1;
1520 return T_RBRACE;
1521 case '[':
1522 *lenp = 1;
1523 return T_LBRACKET;
1524 case ']':
1525 *lenp = 1;
1526 return T_RBRACKET;
1527 case '=':
1528 *lenp = 1;
1529 return T_EQUALS;
1530 case '"':
1531 if ((t = ng_get_string_token(s, startp, lenp)) == NULL)
1532 return T_ERROR;
1533 FREE(t, M_NETGRAPH);
1534 return T_STRING;
1535 default:
1536 for (i = *startp + 1; s[i] != '\0' && !isspace(s[i])
1537 && s[i] != '{' && s[i] != '}' && s[i] != '['
1538 && s[i] != ']' && s[i] != '=' && s[i] != '"'; i++)
1540 *lenp = i - *startp;
1541 return T_WORD;
1546 * Get a string token, which must be enclosed in double quotes.
1547 * The normal C backslash escapes are recognized.
1549 char *
1550 ng_get_string_token(const char *s, int *startp, int *lenp)
1552 char *cbuf, *p;
1553 int start, off;
1555 while (isspace(s[*startp]))
1556 (*startp)++;
1557 start = *startp;
1558 if (s[*startp] != '"')
1559 return (NULL);
1560 MALLOC(cbuf, char *, strlen(s + start), M_NETGRAPH, M_NOWAIT);
1561 if (cbuf == NULL)
1562 return (NULL);
1563 strcpy(cbuf, s + start + 1);
1564 for (off = 1, p = cbuf; *p != '\0'; off++, p++) {
1565 if (*p == '"') {
1566 *p = '\0';
1567 *lenp = off + 1;
1568 return (cbuf);
1569 } else if (p[0] == '\\' && p[1] != '\0') {
1570 int x, k;
1571 char *v;
1573 strcpy(p, p + 1);
1574 v = p;
1575 switch (*p) {
1576 case 't':
1577 *v = '\t';
1578 off++;
1579 continue;
1580 case 'n':
1581 *v = '\n';
1582 off++;
1583 continue;
1584 case 'r':
1585 *v = '\r';
1586 off++;
1587 continue;
1588 case 'v':
1589 *v = '\v';
1590 off++;
1591 continue;
1592 case 'f':
1593 *v = '\f';
1594 off++;
1595 continue;
1596 case '"':
1597 *v = '"';
1598 off++;
1599 continue;
1600 case '0': case '1': case '2': case '3':
1601 case '4': case '5': case '6': case '7':
1602 for (x = k = 0;
1603 k < 3 && *v >= '0' && *v <= '7'; v++) {
1604 x = (x << 3) + (*v - '0');
1605 off++;
1607 *--v = (char)x;
1608 break;
1609 case 'x':
1610 for (v++, x = k = 0;
1611 k < 2 && isxdigit(*v); v++) {
1612 x = (x << 4) + (isdigit(*v) ?
1613 (*v - '0') :
1614 (tolower(*v) - 'a' + 10));
1615 off++;
1617 *--v = (char)x;
1618 break;
1619 default:
1620 continue;
1622 strcpy(p, v);
1625 return (NULL); /* no closing quote */
1629 * Encode a string so it can be safely put in double quotes.
1630 * Caller must free the result.
1632 char *
1633 ng_encode_string(const char *raw)
1635 char *cbuf;
1636 int off = 0;
1638 MALLOC(cbuf, char *, strlen(raw) * 4 + 3, M_NETGRAPH, M_NOWAIT);
1639 if (cbuf == NULL)
1640 return (NULL);
1641 cbuf[off++] = '"';
1642 for ( ; *raw != '\0'; raw++) {
1643 switch (*raw) {
1644 case '\t':
1645 cbuf[off++] = '\\';
1646 cbuf[off++] = 't';
1647 break;
1648 case '\f':
1649 cbuf[off++] = '\\';
1650 cbuf[off++] = 'f';
1651 break;
1652 case '\n':
1653 cbuf[off++] = '\\';
1654 cbuf[off++] = 'n';
1655 break;
1656 case '\r':
1657 cbuf[off++] = '\\';
1658 cbuf[off++] = 'r';
1659 break;
1660 case '\v':
1661 cbuf[off++] = '\\';
1662 cbuf[off++] = 'v';
1663 break;
1664 case '"':
1665 case '\\':
1666 cbuf[off++] = '\\';
1667 cbuf[off++] = *raw;
1668 break;
1669 default:
1670 if (*raw < 0x20 || *raw > 0x7e) {
1671 off += ksprintf(cbuf + off,
1672 "\\x%02x", (u_char)*raw);
1673 break;
1675 cbuf[off++] = *raw;
1676 break;
1679 cbuf[off++] = '"';
1680 cbuf[off] = '\0';
1681 return (cbuf);
1684 /************************************************************************
1685 VIRTUAL METHOD LOOKUP
1686 ************************************************************************/
1688 static ng_parse_t *
1689 ng_get_parse_method(const struct ng_parse_type *t)
1691 while (t != NULL && t->parse == NULL)
1692 t = t->supertype;
1693 return (t ? t->parse : NULL);
1696 static ng_unparse_t *
1697 ng_get_unparse_method(const struct ng_parse_type *t)
1699 while (t != NULL && t->unparse == NULL)
1700 t = t->supertype;
1701 return (t ? t->unparse : NULL);
1704 static ng_getDefault_t *
1705 ng_get_getDefault_method(const struct ng_parse_type *t)
1707 while (t != NULL && t->getDefault == NULL)
1708 t = t->supertype;
1709 return (t ? t->getDefault : NULL);
1712 static ng_getAlign_t *
1713 ng_get_getAlign_method(const struct ng_parse_type *t)
1715 while (t != NULL && t->getAlign == NULL)
1716 t = t->supertype;
1717 return (t ? t->getAlign : NULL);