| Commit | Line | Data |
|---|---|---|
| 984263bc MD |
1 | |
| 2 | /* | |
| 3 | * ng_parse.c | |
| 4 | * | |
| 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. | |
| 18 | * | |
| 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. | |
| 36 | * | |
| 37 | * Author: Archie Cobbs <archie@freebsd.org> | |
| 38 | * | |
| 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 $ | |
| e7b4468c | 41 | * $DragonFly: src/sys/netgraph/netgraph/ng_parse.c,v 1.10 2008/01/05 14:02:39 swildner Exp $ |
| 984263bc MD |
42 | */ |
| 43 | ||
| 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> | |
| 50 | ||
| 51 | #include <netinet/in.h> | |
| 52 | ||
| 53 | #include <netgraph/ng_message.h> | |
| 54 | #include <netgraph/netgraph.h> | |
| d2438d69 | 55 | #include <netgraph/ng_parse.h> |
| 984263bc MD |
56 | |
| 57 | /* Compute alignment for primitive integral types */ | |
| 58 | struct int16_temp { | |
| 59 | char x; | |
| 60 | int16_t y; | |
| 61 | }; | |
| 62 | ||
| 63 | struct int32_temp { | |
| 64 | char x; | |
| 65 | int32_t y; | |
| 66 | }; | |
| 67 | ||
| 68 | struct int64_temp { | |
| 69 | char x; | |
| 70 | int64_t y; | |
| 71 | }; | |
| 72 | ||
| 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) | |
| 77 | ||
| 78 | /* Output format for integral types */ | |
| 79 | #define INT_UNSIGNED 0 | |
| 80 | #define INT_SIGNED 1 | |
| 81 | #define INT_HEX 2 | |
| 82 | ||
| 83 | /* Type of composite object: struct, array, or fixedarray */ | |
| 84 | enum comptype { | |
| 85 | CT_STRUCT, | |
| 86 | CT_ARRAY, | |
| 87 | CT_FIXEDARRAY, | |
| 88 | }; | |
| 89 | ||
| 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); | |
| 107 | ||
| 108 | /* Parsing helper functions */ | |
| 109 | static int ng_parse_skip_value(const char *s, int off, int *lenp); | |
| 110 | ||
| 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)) | |
| 114 | ||
| 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); | |
| 120 | ||
| 121 | #define ALIGNMENT(t) (METHOD(t, getAlign) == NULL ? \ | |
| 122 | 0 : INVOKE(t, getAlign)(t)) | |
| 123 | ||
| 124 | /* For converting binary to string */ | |
| 125 | #define NG_PARSE_APPEND(fmt, args...) \ | |
| 126 | do { \ | |
| 127 | int len; \ | |
| 128 | \ | |
| f8c7a42d | 129 | len = ksnprintf((cbuf), (cbuflen), \ |
| 984263bc MD |
130 | fmt , ## args); \ |
| 131 | if (len >= (cbuflen)) \ | |
| 132 | return (ERANGE); \ | |
| 133 | (cbuf) += len; \ | |
| 134 | (cbuflen) -= len; \ | |
| 135 | } while (0) | |
| 136 | ||
| 137 | /************************************************************************ | |
| 138 | PUBLIC FUNCTIONS | |
| 139 | ************************************************************************/ | |
| 140 | ||
| 141 | /* | |
| 142 | * Convert an ASCII string to binary according to the supplied type descriptor | |
| 143 | */ | |
| 144 | int | |
| 145 | ng_parse(const struct ng_parse_type *type, | |
| 146 | const char *string, int *off, u_char *buf, int *buflen) | |
| 147 | { | |
| 148 | return INVOKE(type, parse)(type, string, off, buf, buf, buflen); | |
| 149 | } | |
| 150 | ||
| 151 | /* | |
| 152 | * Convert binary to an ASCII string according to the supplied type descriptor | |
| 153 | */ | |
| 154 | int | |
| 155 | ng_unparse(const struct ng_parse_type *type, | |
| 156 | const u_char *data, char *cbuf, int cbuflen) | |
| 157 | { | |
| 158 | int off = 0; | |
| 159 | ||
| 160 | return INVOKE(type, unparse)(type, data, &off, cbuf, cbuflen); | |
| 161 | } | |
| 162 | ||
| 163 | /* | |
| 164 | * Fill in the default value according to the supplied type descriptor | |
| 165 | */ | |
| 166 | int | |
| 167 | ng_parse_getDefault(const struct ng_parse_type *type, u_char *buf, int *buflen) | |
| 168 | { | |
| 169 | ng_getDefault_t *const func = METHOD(type, getDefault); | |
| 170 | ||
| 171 | if (func == NULL) | |
| 172 | return (EOPNOTSUPP); | |
| 173 | return (*func)(type, buf, buf, buflen); | |
| 174 | } | |
| 175 | ||
| 176 | ||
| 177 | /************************************************************************ | |
| 178 | STRUCTURE TYPE | |
| 179 | ************************************************************************/ | |
| 180 | ||
| 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) | |
| 185 | { | |
| 186 | return ng_parse_composite(type, s, off, start, buf, buflen, CT_STRUCT); | |
| 187 | } | |
| 188 | ||
| 189 | static int | |
| 190 | ng_struct_unparse(const struct ng_parse_type *type, | |
| 191 | const u_char *data, int *off, char *cbuf, int cbuflen) | |
| 192 | { | |
| 193 | return ng_unparse_composite(type, data, off, cbuf, cbuflen, CT_STRUCT); | |
| 194 | } | |
| 195 | ||
| 196 | static int | |
| 197 | ng_struct_getDefault(const struct ng_parse_type *type, | |
| 198 | const u_char *const start, u_char *buf, int *buflen) | |
| 199 | { | |
| 200 | int off = 0; | |
| 201 | ||
| 202 | return ng_parse_composite(type, | |
| 203 | "{}", &off, start, buf, buflen, CT_STRUCT); | |
| 204 | } | |
| 205 | ||
| 206 | static int | |
| 207 | ng_struct_getAlign(const struct ng_parse_type *type) | |
| 208 | { | |
| 209 | const struct ng_parse_struct_field *field; | |
| 210 | int align = 0; | |
| 211 | ||
| 212 | for (field = type->info; field->name != NULL; field++) { | |
| 213 | int falign = ALIGNMENT(field->type); | |
| 214 | ||
| 215 | if (falign > align) | |
| 216 | align = falign; | |
| 217 | } | |
| 218 | return align; | |
| 219 | } | |
| 220 | ||
| 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 | |
| 229 | }; | |
| 230 | ||
| 231 | /************************************************************************ | |
| 232 | FIXED LENGTH ARRAY TYPE | |
| 233 | ************************************************************************/ | |
| 234 | ||
| 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) | |
| 239 | { | |
| 240 | return ng_parse_composite(type, | |
| 241 | s, off, start, buf, buflen, CT_FIXEDARRAY); | |
| 242 | } | |
| 243 | ||
| 244 | static int | |
| 245 | ng_fixedarray_unparse(const struct ng_parse_type *type, | |
| 246 | const u_char *data, int *off, char *cbuf, int cbuflen) | |
| 247 | { | |
| 248 | return ng_unparse_composite(type, | |
| 249 | data, off, cbuf, cbuflen, CT_FIXEDARRAY); | |
| 250 | } | |
| 251 | ||
| 252 | static int | |
| 253 | ng_fixedarray_getDefault(const struct ng_parse_type *type, | |
| 254 | const u_char *const start, u_char *buf, int *buflen) | |
| 255 | { | |
| 256 | int off = 0; | |
| 257 | ||
| 258 | return ng_parse_composite(type, | |
| 259 | "[]", &off, start, buf, buflen, CT_FIXEDARRAY); | |
| 260 | } | |
| 261 | ||
| 262 | static int | |
| 263 | ng_fixedarray_getAlign(const struct ng_parse_type *type) | |
| 264 | { | |
| 265 | const struct ng_parse_fixedarray_info *fi = type->info; | |
| 266 | ||
| 267 | return ALIGNMENT(fi->elementType); | |
| 268 | } | |
| 269 | ||
| 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 | |
| 278 | }; | |
| 279 | ||
| 280 | /************************************************************************ | |
| 281 | VARIABLE LENGTH ARRAY TYPE | |
| 282 | ************************************************************************/ | |
| 283 | ||
| 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) | |
| 288 | { | |
| 289 | return ng_parse_composite(type, s, off, start, buf, buflen, CT_ARRAY); | |
| 290 | } | |
| 291 | ||
| 292 | static int | |
| 293 | ng_array_unparse(const struct ng_parse_type *type, | |
| 294 | const u_char *data, int *off, char *cbuf, int cbuflen) | |
| 295 | { | |
| 296 | return ng_unparse_composite(type, data, off, cbuf, cbuflen, CT_ARRAY); | |
| 297 | } | |
| 298 | ||
| 299 | static int | |
| 300 | ng_array_getDefault(const struct ng_parse_type *type, | |
| 301 | const u_char *const start, u_char *buf, int *buflen) | |
| 302 | { | |
| 303 | int off = 0; | |
| 304 | ||
| 305 | return ng_parse_composite(type, | |
| 306 | "[]", &off, start, buf, buflen, CT_ARRAY); | |
| 307 | } | |
| 308 | ||
| 309 | static int | |
| 310 | ng_array_getAlign(const struct ng_parse_type *type) | |
| 311 | { | |
| 312 | const struct ng_parse_array_info *ai = type->info; | |
| 313 | ||
| 314 | return ALIGNMENT(ai->elementType); | |
| 315 | } | |
| 316 | ||
| 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 | |
| 325 | }; | |
| 326 | ||
| 327 | /************************************************************************ | |
| 328 | INT8 TYPE | |
| 329 | ************************************************************************/ | |
| 330 | ||
| 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) | |
| 335 | { | |
| 336 | long val; | |
| 337 | int8_t val8; | |
| 338 | char *eptr; | |
| 339 | ||
| 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); | |
| 348 | } | |
| 349 | ||
| 350 | static int | |
| 351 | ng_int8_unparse(const struct ng_parse_type *type, | |
| 352 | const u_char *data, int *off, char *cbuf, int cbuflen) | |
| 353 | { | |
| 354 | const char *fmt; | |
| 355 | int fval; | |
| 356 | int8_t val; | |
| 357 | ||
| 358 | bcopy(data + *off, &val, sizeof(int8_t)); | |
| 973c11b9 | 359 | switch ((int)(intptr_t)type->info) { |
| 984263bc MD |
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: | |
| 9eb57b71 | 373 | panic("%s: unknown type", __func__); |
| 984263bc MD |
374 | } |
| 375 | NG_PARSE_APPEND(fmt, fval); | |
| 376 | *off += sizeof(int8_t); | |
| 377 | return (0); | |
| 378 | } | |
| 379 | ||
| 380 | static int | |
| 381 | ng_int8_getDefault(const struct ng_parse_type *type, | |
| 382 | const u_char *const start, u_char *buf, int *buflen) | |
| 383 | { | |
| 384 | int8_t val; | |
| 385 | ||
| 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); | |
| 392 | } | |
| 393 | ||
| 394 | static int | |
| 395 | ng_int8_getAlign(const struct ng_parse_type *type) | |
| 396 | { | |
| 397 | return INT8_ALIGNMENT; | |
| 398 | } | |
| 399 | ||
| 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 | |
| 408 | }; | |
| 409 | ||
| 410 | const struct ng_parse_type ng_parse_uint8_type = { | |
| 411 | &ng_parse_int8_type, | |
| 412 | (void *)INT_UNSIGNED | |
| 413 | }; | |
| 414 | ||
| 415 | const struct ng_parse_type ng_parse_hint8_type = { | |
| 416 | &ng_parse_int8_type, | |
| 417 | (void *)INT_HEX | |
| 418 | }; | |
| 419 | ||
| 420 | /************************************************************************ | |
| 421 | INT16 TYPE | |
| 422 | ************************************************************************/ | |
| 423 | ||
| 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) | |
| 428 | { | |
| 429 | long val; | |
| 430 | int16_t val16; | |
| 431 | char *eptr; | |
| 432 | ||
| 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); | |
| 442 | } | |
| 443 | ||
| 444 | static int | |
| 445 | ng_int16_unparse(const struct ng_parse_type *type, | |
| 446 | const u_char *data, int *off, char *cbuf, int cbuflen) | |
| 447 | { | |
| 448 | const char *fmt; | |
| 449 | int fval; | |
| 450 | int16_t val; | |
| 451 | ||
| 452 | bcopy(data + *off, &val, sizeof(int16_t)); | |
| 973c11b9 | 453 | switch ((int)(intptr_t)type->info) { |
| 984263bc MD |
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: | |
| 9eb57b71 | 467 | panic("%s: unknown type", __func__); |
| 984263bc MD |
468 | } |
| 469 | NG_PARSE_APPEND(fmt, fval); | |
| 470 | *off += sizeof(int16_t); | |
| 471 | return (0); | |
| 472 | } | |
| 473 | ||
| 474 | static int | |
| 475 | ng_int16_getDefault(const struct ng_parse_type *type, | |
| 476 | const u_char *const start, u_char *buf, int *buflen) | |
| 477 | { | |
| 478 | int16_t val; | |
| 479 | ||
| 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); | |
| 486 | } | |
| 487 | ||
| 488 | static int | |
| 489 | ng_int16_getAlign(const struct ng_parse_type *type) | |
| 490 | { | |
| 491 | return INT16_ALIGNMENT; | |
| 492 | } | |
| 493 | ||
| 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 | |
| 502 | }; | |
| 503 | ||
| 504 | const struct ng_parse_type ng_parse_uint16_type = { | |
| 505 | &ng_parse_int16_type, | |
| 506 | (void *)INT_UNSIGNED | |
| 507 | }; | |
| 508 | ||
| 509 | const struct ng_parse_type ng_parse_hint16_type = { | |
| 510 | &ng_parse_int16_type, | |
| 511 | (void *)INT_HEX | |
| 512 | }; | |
| 513 | ||
| 514 | /************************************************************************ | |
| 515 | INT32 TYPE | |
| 516 | ************************************************************************/ | |
| 517 | ||
| 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) | |
| 522 | { | |
| 523 | long val; /* assumes long is at least 32 bits */ | |
| 524 | int32_t val32; | |
| 525 | char *eptr; | |
| 526 | ||
| 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); | |
| 536 | } | |
| 537 | ||
| 538 | static int | |
| 539 | ng_int32_unparse(const struct ng_parse_type *type, | |
| 540 | const u_char *data, int *off, char *cbuf, int cbuflen) | |
| 541 | { | |
| 542 | const char *fmt; | |
| 543 | long fval; | |
| 544 | int32_t val; | |
| 545 | ||
| 546 | bcopy(data + *off, &val, sizeof(int32_t)); | |
| 973c11b9 | 547 | switch ((int)(intptr_t)type->info) { |
| 984263bc MD |
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: | |
| 9eb57b71 | 561 | panic("%s: unknown type", __func__); |
| 984263bc MD |
562 | } |
| 563 | NG_PARSE_APPEND(fmt, fval); | |
| 564 | *off += sizeof(int32_t); | |
| 565 | return (0); | |
| 566 | } | |
| 567 | ||
| 568 | static int | |
| 569 | ng_int32_getDefault(const struct ng_parse_type *type, | |
| 570 | const u_char *const start, u_char *buf, int *buflen) | |
| 571 | { | |
| 572 | int32_t val; | |
| 573 | ||
| 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); | |
| 580 | } | |
| 581 | ||
| 582 | static int | |
| 583 | ng_int32_getAlign(const struct ng_parse_type *type) | |
| 584 | { | |
| 585 | return INT32_ALIGNMENT; | |
| 586 | } | |
| 587 | ||
| 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 | |
| 596 | }; | |
| 597 | ||
| 598 | const struct ng_parse_type ng_parse_uint32_type = { | |
| 599 | &ng_parse_int32_type, | |
| 600 | (void *)INT_UNSIGNED | |
| 601 | }; | |
| 602 | ||
| 603 | const struct ng_parse_type ng_parse_hint32_type = { | |
| 604 | &ng_parse_int32_type, | |
| 605 | (void *)INT_HEX | |
| 606 | }; | |
| 607 | ||
| 608 | /************************************************************************ | |
| 609 | INT64 TYPE | |
| 610 | ************************************************************************/ | |
| 611 | ||
| 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) | |
| 616 | { | |
| 617 | quad_t val; | |
| 618 | int64_t val64; | |
| 619 | char *eptr; | |
| 620 | ||
| 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); | |
| 629 | } | |
| 630 | ||
| 631 | static int | |
| 632 | ng_int64_unparse(const struct ng_parse_type *type, | |
| 633 | const u_char *data, int *off, char *cbuf, int cbuflen) | |
| 634 | { | |
| 635 | const char *fmt; | |
| 636 | long long fval; | |
| 637 | int64_t val; | |
| 638 | ||
| 639 | bcopy(data + *off, &val, sizeof(int64_t)); | |
| 973c11b9 | 640 | switch ((int)(intptr_t)type->info) { |
| 984263bc MD |
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: | |
| 9eb57b71 | 654 | panic("%s: unknown type", __func__); |
| 984263bc MD |
655 | } |
| 656 | NG_PARSE_APPEND(fmt, fval); | |
| 657 | *off += sizeof(int64_t); | |
| 658 | return (0); | |
| 659 | } | |
| 660 | ||
| 661 | static int | |
| 662 | ng_int64_getDefault(const struct ng_parse_type *type, | |
| 663 | const u_char *const start, u_char *buf, int *buflen) | |
| 664 | { | |
| 665 | int64_t val; | |
| 666 | ||
| 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); | |
| 673 | } | |
| 674 | ||
| 675 | static int | |
| 676 | ng_int64_getAlign(const struct ng_parse_type *type) | |
| 677 | { | |
| 678 | return INT64_ALIGNMENT; | |
| 679 | } | |
| 680 | ||
| 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 | |
| 689 | }; | |
| 690 | ||
| 691 | const struct ng_parse_type ng_parse_uint64_type = { | |
| 692 | &ng_parse_int64_type, | |
| 693 | (void *)INT_UNSIGNED | |
| 694 | }; | |
| 695 | ||
| 696 | const struct ng_parse_type ng_parse_hint64_type = { | |
| 697 | &ng_parse_int64_type, | |
| 698 | (void *)INT_HEX | |
| 699 | }; | |
| 700 | ||
| 701 | /************************************************************************ | |
| 702 | STRING TYPE | |
| 703 | ************************************************************************/ | |
| 704 | ||
| 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) | |
| 709 | { | |
| 710 | char *sval; | |
| 711 | int len; | |
| 712 | ||
| 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); | |
| 721 | } | |
| 722 | ||
| 723 | static int | |
| 724 | ng_string_unparse(const struct ng_parse_type *type, | |
| 725 | const u_char *data, int *off, char *cbuf, int cbuflen) | |
| 726 | { | |
| 727 | const char *const raw = (const char *)data + *off; | |
| 728 | char *const s = ng_encode_string(raw); | |
| 729 | ||
| 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); | |
| 736 | } | |
| 737 | ||
| 738 | static int | |
| 739 | ng_string_getDefault(const struct ng_parse_type *type, | |
| 740 | const u_char *const start, u_char *buf, int *buflen) | |
| 741 | { | |
| 742 | ||
| 743 | if (*buflen < 1) | |
| 744 | return (ERANGE); | |
| 745 | buf[0] = (u_char)'\0'; | |
| 746 | *buflen = 1; | |
| 747 | return (0); | |
| 748 | } | |
| 749 | ||
| 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 | |
| 758 | }; | |
| 759 | ||
| 760 | /************************************************************************ | |
| 761 | FIXED BUFFER STRING TYPE | |
| 762 | ************************************************************************/ | |
| 763 | ||
| 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) | |
| 768 | { | |
| 769 | const struct ng_parse_fixedstring_info *const fi = type->info; | |
| 770 | char *sval; | |
| 771 | int len; | |
| 772 | ||
| 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); | |
| 784 | } | |
| 785 | ||
| 786 | static int | |
| 787 | ng_fixedstring_unparse(const struct ng_parse_type *type, | |
| 788 | const u_char *data, int *off, char *cbuf, int cbuflen) | |
| 789 | { | |
| 790 | const struct ng_parse_fixedstring_info *const fi = type->info; | |
| 791 | int error, temp = *off; | |
| 792 | ||
| 793 | if ((error = ng_string_unparse(type, data, &temp, cbuf, cbuflen)) != 0) | |
| 794 | return (error); | |
| 795 | *off += fi->bufSize; | |
| 796 | return (0); | |
| 797 | } | |
| 798 | ||
| 799 | static int | |
| 800 | ng_fixedstring_getDefault(const struct ng_parse_type *type, | |
| 801 | const u_char *const start, u_char *buf, int *buflen) | |
| 802 | { | |
| 803 | const struct ng_parse_fixedstring_info *const fi = type->info; | |
| 804 | ||
| 805 | if (*buflen < fi->bufSize) | |
| 806 | return (ERANGE); | |
| 807 | bzero(buf, fi->bufSize); | |
| 808 | *buflen = fi->bufSize; | |
| 809 | return (0); | |
| 810 | } | |
| 811 | ||
| 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 | |
| 820 | }; | |
| 821 | ||
| 822 | const struct ng_parse_fixedstring_info ng_parse_nodebuf_info = { | |
| f15ba1a8 | 823 | NG_NODESIZ |
| 984263bc MD |
824 | }; |
| 825 | const struct ng_parse_type ng_parse_nodebuf_type = { | |
| 826 | &ng_parse_fixedstring_type, | |
| 827 | &ng_parse_nodebuf_info | |
| 828 | }; | |
| 829 | ||
| 830 | const struct ng_parse_fixedstring_info ng_parse_hookbuf_info = { | |
| f15ba1a8 | 831 | NG_HOOKSIZ |
| 984263bc MD |
832 | }; |
| 833 | const struct ng_parse_type ng_parse_hookbuf_type = { | |
| 834 | &ng_parse_fixedstring_type, | |
| 835 | &ng_parse_hookbuf_info | |
| 836 | }; | |
| 837 | ||
| 838 | const struct ng_parse_fixedstring_info ng_parse_pathbuf_info = { | |
| f15ba1a8 | 839 | NG_PATHSIZ |
| 984263bc MD |
840 | }; |
| 841 | const struct ng_parse_type ng_parse_pathbuf_type = { | |
| 842 | &ng_parse_fixedstring_type, | |
| 843 | &ng_parse_pathbuf_info | |
| 844 | }; | |
| 845 | ||
| 846 | const struct ng_parse_fixedstring_info ng_parse_typebuf_info = { | |
| f15ba1a8 | 847 | NG_TYPESIZ |
| 984263bc MD |
848 | }; |
| 849 | const struct ng_parse_type ng_parse_typebuf_type = { | |
| 850 | &ng_parse_fixedstring_type, | |
| 851 | &ng_parse_typebuf_info | |
| 852 | }; | |
| 853 | ||
| 854 | const struct ng_parse_fixedstring_info ng_parse_cmdbuf_info = { | |
| f15ba1a8 | 855 | NG_CMDSTRSIZ |
| 984263bc MD |
856 | }; |
| 857 | const struct ng_parse_type ng_parse_cmdbuf_type = { | |
| 858 | &ng_parse_fixedstring_type, | |
| 859 | &ng_parse_cmdbuf_info | |
| 860 | }; | |
| 861 | ||
| 862 | /************************************************************************ | |
| 863 | IP ADDRESS TYPE | |
| 864 | ************************************************************************/ | |
| 865 | ||
| 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) | |
| 870 | { | |
| 871 | int i, error; | |
| 872 | ||
| 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)++; | |
| 880 | } | |
| 881 | *buflen = 4; | |
| 882 | return (0); | |
| 883 | } | |
| 884 | ||
| 885 | static int | |
| 886 | ng_ipaddr_unparse(const struct ng_parse_type *type, | |
| 887 | const u_char *data, int *off, char *cbuf, int cbuflen) | |
| 888 | { | |
| 889 | struct in_addr ip; | |
| 890 | ||
| 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); | |
| 896 | } | |
| 897 | ||
| 898 | static int | |
| 899 | ng_ipaddr_getDefault(const struct ng_parse_type *type, | |
| 900 | const u_char *const start, u_char *buf, int *buflen) | |
| 901 | { | |
| 902 | struct in_addr ip = { 0 }; | |
| 903 | ||
| 904 | if (*buflen < sizeof(ip)) | |
| 905 | return (ERANGE); | |
| 906 | bcopy(&ip, buf, sizeof(ip)); | |
| 907 | *buflen = sizeof(ip); | |
| 908 | return (0); | |
| 909 | } | |
| 910 | ||
| 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 | |
| 919 | }; | |
| 920 | ||
| 921 | /************************************************************************ | |
| 922 | BYTE ARRAY TYPE | |
| 923 | ************************************************************************/ | |
| 924 | ||
| 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) | |
| 929 | { | |
| 930 | ng_parse_array_getLength_t *const getLength = type->private; | |
| 931 | ||
| 932 | return (*getLength)(type, start, buf); | |
| 933 | } | |
| 934 | ||
| 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 | |
| 940 | }; | |
| 941 | static const struct ng_parse_type ng_parse_bytearray_subtype = { | |
| 942 | &ng_parse_array_type, | |
| 943 | &ng_parse_bytearray_subtype_info | |
| 944 | }; | |
| 945 | ||
| 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) | |
| 950 | { | |
| 951 | char *str; | |
| 952 | int toklen; | |
| 953 | ||
| 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; | |
| 958 | ||
| 959 | arraylen = (*getLength)(type, start, buf); | |
| 960 | if (arraylen > *buflen) { | |
| 961 | FREE(str, M_NETGRAPH); | |
| 962 | return (ERANGE); | |
| 963 | } | |
| 964 | slen = strlen(str) + 1; | |
| 965 | if (slen > arraylen) { | |
| 966 | FREE(str, M_NETGRAPH); | |
| 967 | return (E2BIG); | |
| 968 | } | |
| 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; | |
| 977 | ||
| 978 | subtype = ng_parse_bytearray_subtype; | |
| 55f2596a | 979 | *(const void **)&subtype.private = type->info; |
| 984263bc MD |
980 | return ng_array_parse(&subtype, s, off, start, buf, buflen); |
| 981 | } | |
| 982 | } | |
| 983 | ||
| 984 | static int | |
| 985 | ng_bytearray_unparse(const struct ng_parse_type *type, | |
| 986 | const u_char *data, int *off, char *cbuf, int cbuflen) | |
| 987 | { | |
| 988 | struct ng_parse_type subtype; | |
| 989 | ||
| 990 | subtype = ng_parse_bytearray_subtype; | |
| 55f2596a | 991 | *(const void **)&subtype.private = type->info; |
| 984263bc MD |
992 | return ng_array_unparse(&subtype, data, off, cbuf, cbuflen); |
| 993 | } | |
| 994 | ||
| 995 | static int | |
| 996 | ng_bytearray_getDefault(const struct ng_parse_type *type, | |
| 997 | const u_char *const start, u_char *buf, int *buflen) | |
| 998 | { | |
| 999 | struct ng_parse_type subtype; | |
| 1000 | ||
| 1001 | subtype = ng_parse_bytearray_subtype; | |
| 55f2596a | 1002 | *(const void **)&subtype.private = type->info; |
| 984263bc MD |
1003 | return ng_array_getDefault(&subtype, start, buf, buflen); |
| 1004 | } | |
| 1005 | ||
| 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 | |
| 1014 | }; | |
| 1015 | ||
| 1016 | /************************************************************************ | |
| 1017 | STRUCT NG_MESG TYPE | |
| 1018 | ************************************************************************/ | |
| 1019 | ||
| 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) | |
| 1024 | { | |
| 1025 | const struct ng_mesg *msg; | |
| 1026 | ||
| 1027 | msg = (const struct ng_mesg *)(buf - sizeof(*msg)); | |
| 1028 | return msg->header.arglen; | |
| 1029 | } | |
| 1030 | ||
| 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 | |
| 1035 | }; | |
| 1036 | ||
| 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, | |
| 1043 | }; | |
| 1044 | ||
| 1045 | /************************************************************************ | |
| 1046 | COMPOSITE HELPER ROUTINES | |
| 1047 | ************************************************************************/ | |
| 1048 | ||
| 1049 | /* | |
| 1050 | * Convert a structure or array from ASCII to binary | |
| 1051 | */ | |
| 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) | |
| 1056 | { | |
| 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; | |
| 1062 | ||
| 1063 | /* Initialize */ | |
| e7b4468c | 1064 | MALLOC(foff, int *, num * sizeof(*foff), M_NETGRAPH, M_NOWAIT | M_ZERO); |
| 984263bc MD |
1065 | if (foff == NULL) { |
| 1066 | error = ENOMEM; | |
| 1067 | goto done; | |
| 1068 | } | |
| 984263bc MD |
1069 | |
| 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; | |
| 1075 | } | |
| 1076 | *off += len; | |
| 1077 | ||
| 1078 | /* Get individual element value positions in the string */ | |
| 1079 | for (;;) { | |
| 1080 | enum ng_parse_token tok; | |
| 1081 | ||
| 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; | |
| 1087 | } | |
| 1088 | ||
| 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; | |
| 1095 | ||
| 1096 | /* If an opening brace/bracket, index is implied */ | |
| 1097 | if (tok == T_LBRACE || tok == T_LBRACKET) { | |
| 1098 | index = nextIndex++; | |
| 1099 | goto gotIndex; | |
| 1100 | } | |
| 1101 | ||
| 1102 | /* Might be an index, might be a value, either way... */ | |
| 1103 | if (tok != T_WORD) { | |
| 1104 | error = EINVAL; | |
| 1105 | goto done; | |
| 1106 | } | |
| 1107 | ||
| 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; | |
| 1113 | } | |
| 1114 | ||
| 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; | |
| 1120 | } | |
| 1121 | nextIndex = index + 1; | |
| 1122 | *off += len + len2; | |
| 1123 | gotIndex: | |
| 6b08710e | 1124 | ; /* avoid gcc3.x warning */ |
| 984263bc MD |
1125 | } else { /* a structure field */ |
| 1126 | const struct ng_parse_struct_field *const | |
| 1127 | fields = type->info; | |
| 1128 | ||
| 1129 | /* Find the field by name (required) in field list */ | |
| 1130 | if (tok != T_WORD) { | |
| 1131 | error = EINVAL; | |
| 1132 | goto done; | |
| 1133 | } | |
| 1134 | for (index = 0; index < num; index++) { | |
| 1135 | const struct ng_parse_struct_field *const | |
| 1136 | field = &fields[index]; | |
| 1137 | ||
| 1138 | if (strncmp(&s[*off], field->name, len) == 0 | |
| 1139 | && field->name[len] == '\0') | |
| 1140 | break; | |
| 1141 | } | |
| 1142 | if (index == num) { | |
| 1143 | error = ENOENT; | |
| 1144 | goto done; | |
| 1145 | } | |
| 1146 | *off += len; | |
| 1147 | ||
| 1148 | /* Get equals sign */ | |
| 1149 | if (ng_parse_get_token(s, off, &len) != T_EQUALS) { | |
| 1150 | error = EINVAL; | |
| 1151 | goto done; | |
| 1152 | } | |
| 1153 | *off += len; | |
| 1154 | } | |
| 1155 | ||
| 1156 | /* Check array index */ | |
| 1157 | if (index >= num) { | |
| 1158 | error = E2BIG; | |
| 1159 | goto done; | |
| 1160 | } | |
| 1161 | ||
| 1162 | /* Save value's position and skip over it for now */ | |
| 1163 | if (foff[index] != 0) { | |
| 1164 | error = EALREADY; /* duplicate */ | |
| 1165 | goto done; | |
| 1166 | } | |
| 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; | |
| 1173 | } | |
| 1174 | ||
| 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; | |
| 1180 | ||
| 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; | |
| 1187 | } | |
| 1188 | buf[blen++] = 0; | |
| 1189 | } | |
| 1190 | ||
| 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); | |
| 1200 | } | |
| 1201 | if (error != 0) | |
| 1202 | goto done; | |
| 1203 | blen += vlen; | |
| 1204 | } | |
| 1205 | ||
| 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; | |
| 1212 | } | |
| 1213 | buf[blen++] = 0; | |
| 1214 | } | |
| 1215 | } | |
| 1216 | ||
| 1217 | /* Done */ | |
| 1218 | *buflen = blen; | |
| 1219 | done: | |
| 1220 | if (foff != NULL) | |
| 1221 | FREE(foff, M_NETGRAPH); | |
| 1222 | return (error); | |
| 1223 | } | |
| 1224 | ||
| 1225 | /* | |
| 1226 | * Convert an array or structure from binary to ASCII | |
| 1227 | */ | |
| 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) | |
| 1231 | { | |
| 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; | |
| 1239 | ||
| 1240 | /* Get workspace for checking default values */ | |
| 1241 | MALLOC(workBuf, u_char *, workSize, M_NETGRAPH, M_NOWAIT); | |
| 1242 | if (workBuf == NULL) | |
| 1243 | return (ENOMEM); | |
| 1244 | ||
| 1245 | /* Opening brace/bracket */ | |
| 1246 | NG_PARSE_APPEND("%c", (ctype == CT_STRUCT) ? '{' : '['); | |
| 1247 | ||
| 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); | |
| 1252 | ||
| 1253 | /* Skip any alignment pad bytes */ | |
| 1254 | *off += ng_parse_get_elem_pad(type, index, ctype, *off); | |
| 1255 | ||
| 1256 | /* | |
| 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. | |
| 1259 | */ | |
| 1260 | if (sizeof(*hdr) + *off < workSize) { | |
| 1261 | int tempsize = workSize - sizeof(*hdr) - *off; | |
| 1262 | ||
| 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; | |
| 1271 | } | |
| 1272 | } | |
| 1273 | ||
| 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); | |
| 1280 | } | |
| 1281 | nextIndex++; | |
| 1282 | } else { | |
| 1283 | const struct ng_parse_struct_field *const | |
| 1284 | fields = type->info; | |
| 1285 | ||
| 1286 | NG_PARSE_APPEND("%s=", fields[index].name); | |
| 1287 | } | |
| 1288 | ||
| 1289 | /* Print value */ | |
| 1290 | if ((error = INVOKE(etype, unparse) | |
| 1291 | (etype, data, off, cbuf, cbuflen)) != 0) { | |
| 1292 | FREE(workBuf, M_NETGRAPH); | |
| 1293 | return (error); | |
| 1294 | } | |
| 1295 | cbuflen -= strlen(cbuf); | |
| 1296 | cbuf += strlen(cbuf); | |
| 1297 | didOne = 1; | |
| 1298 | } | |
| 1299 | FREE(workBuf, M_NETGRAPH); | |
| 1300 | ||
| 1301 | /* Closing brace/bracket */ | |
| 1302 | NG_PARSE_APPEND("%s%c", | |
| 1303 | didOne ? " " : "", (ctype == CT_STRUCT) ? '}' : ']'); | |
| 1304 | return (0); | |
| 1305 | } | |
| 1306 | ||
| 1307 | /* | |
| 1308 | * Generate the default value for an element of an array or structure | |
| 1309 | * Returns EOPNOTSUPP if default value is unspecified. | |
| 1310 | */ | |
| 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) | |
| 1315 | { | |
| 1316 | const struct ng_parse_type *etype; | |
| 1317 | ng_getDefault_t *func; | |
| 1318 | ||
| 1319 | switch (ctype) { | |
| 1320 | case CT_STRUCT: | |
| 1321 | break; | |
| 1322 | case CT_ARRAY: | |
| 1323 | { | |
| 1324 | const struct ng_parse_array_info *const ai = type->info; | |
| 1325 | ||
| 1326 | if (ai->getDefault != NULL) { | |
| 1327 | return (*ai->getDefault)(type, | |
| 1328 | index, start, buf, buflen); | |
| 1329 | } | |
| 1330 | break; | |
| 1331 | } | |
| 1332 | case CT_FIXEDARRAY: | |
| 1333 | { | |
| 1334 | const struct ng_parse_fixedarray_info *const fi = type->info; | |
| 1335 | ||
| 1336 | if (*fi->getDefault != NULL) { | |
| 1337 | return (*fi->getDefault)(type, | |
| 1338 | index, start, buf, buflen); | |
| 1339 | } | |
| 1340 | break; | |
| 1341 | } | |
| 1342 | default: | |
| 9eb57b71 | 1343 | panic("%s", __func__); |
| 984263bc MD |
1344 | } |
| 1345 | ||
| 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); | |
| 1352 | } | |
| 1353 | ||
| 1354 | /* | |
| 1355 | * Get the number of elements in a struct, variable or fixed array. | |
| 1356 | */ | |
| 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) | |
| 1361 | { | |
| 1362 | switch (ctype) { | |
| 1363 | case CT_STRUCT: | |
| 1364 | { | |
| 1365 | const struct ng_parse_struct_field *const fields = type->info; | |
| 1366 | int numFields = 0; | |
| 1367 | ||
| 1368 | for (numFields = 0; ; numFields++) { | |
| 1369 | const struct ng_parse_struct_field *const | |
| 1370 | fi = &fields[numFields]; | |
| 1371 | ||
| 1372 | if (fi->name == NULL) | |
| 1373 | break; | |
| 1374 | } | |
| 1375 | return (numFields); | |
| 1376 | } | |
| 1377 | case CT_ARRAY: | |
| 1378 | { | |
| 1379 | const struct ng_parse_array_info *const ai = type->info; | |
| 1380 | ||
| 1381 | return (*ai->getLength)(type, start, buf); | |
| 1382 | } | |
| 1383 | case CT_FIXEDARRAY: | |
| 1384 | { | |
| 1385 | const struct ng_parse_fixedarray_info *const fi = type->info; | |
| 1386 | ||
| 1387 | return fi->length; | |
| 1388 | } | |
| 1389 | default: | |
| 9eb57b71 | 1390 | panic("%s", __func__); |
| 984263bc MD |
1391 | } |
| 1392 | return (0); | |
| 1393 | } | |
| 1394 | ||
| 1395 | /* | |
| 1396 | * Return the type of the index'th element of a composite structure | |
| 1397 | */ | |
| 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) | |
| 1401 | { | |
| 1402 | const struct ng_parse_type *etype = NULL; | |
| 1403 | ||
| 1404 | switch (ctype) { | |
| 1405 | case CT_STRUCT: | |
| 1406 | { | |
| 1407 | const struct ng_parse_struct_field *const fields = type->info; | |
| 1408 | ||
| 1409 | etype = fields[index].type; | |
| 1410 | break; | |
| 1411 | } | |
| 1412 | case CT_ARRAY: | |
| 1413 | { | |
| 1414 | const struct ng_parse_array_info *const ai = type->info; | |
| 1415 | ||
| 1416 | etype = ai->elementType; | |
| 1417 | break; | |
| 1418 | } | |
| 1419 | case CT_FIXEDARRAY: | |
| 1420 | { | |
| 1421 | const struct ng_parse_fixedarray_info *const fi = type->info; | |
| 1422 | ||
| 1423 | etype = fi->elementType; | |
| 1424 | break; | |
| 1425 | } | |
| 1426 | default: | |
| 9eb57b71 | 1427 | panic("%s", __func__); |
| 984263bc MD |
1428 | } |
| 1429 | return (etype); | |
| 1430 | } | |
| 1431 | ||
| 1432 | /* | |
| 1433 | * Get the number of bytes to skip to align for the next | |
| 1434 | * element in a composite structure. | |
| 1435 | */ | |
| 1436 | static int | |
| 1437 | ng_parse_get_elem_pad(const struct ng_parse_type *type, | |
| 1438 | int index, enum comptype ctype, int posn) | |
| 1439 | { | |
| 1440 | const struct ng_parse_type *const | |
| 1441 | etype = ng_get_composite_etype(type, index, ctype); | |
| 1442 | int align; | |
| 1443 | ||
| 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; | |
| 1448 | ||
| 1449 | if (fields[index].alignment != 0) | |
| 1450 | align = fields[index].alignment; | |
| 1451 | } | |
| 1452 | ||
| 1453 | /* Return number of bytes to skip to align */ | |
| 1454 | return (align ? (align - (posn % align)) % align : 0); | |
| 1455 | } | |
| 1456 | ||
| 1457 | /************************************************************************ | |
| 1458 | PARSING HELPER ROUTINES | |
| 1459 | ************************************************************************/ | |
| 1460 | ||
| 1461 | /* | |
| 1462 | * Skip over a value | |
| 1463 | */ | |
| 1464 | static int | |
| 1465 | ng_parse_skip_value(const char *s, int off0, int *lenp) | |
| 1466 | { | |
| 1467 | int len, nbracket, nbrace; | |
| 1468 | int off = off0; | |
| 1469 | ||
| 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; | |
| 1491 | } | |
| 1492 | off += len; | |
| 1493 | } while (nbracket > 0 || nbrace > 0); | |
| 1494 | *lenp = off - off0; | |
| 1495 | return (0); | |
| 1496 | } | |
| 1497 | ||
| 1498 | /* | |
| 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. | |
| 1502 | */ | |
| 1503 | enum ng_parse_token | |
| 1504 | ng_parse_get_token(const char *s, int *startp, int *lenp) | |
| 1505 | { | |
| 1506 | char *t; | |
| 1507 | int i; | |
| 1508 | ||
| 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++) | |
| 1539 | ; | |
| 1540 | *lenp = i - *startp; | |
| 1541 | return T_WORD; | |
| 1542 | } | |
| 1543 | } | |
| 1544 | ||
| 1545 | /* | |
| 1546 | * Get a string token, which must be enclosed in double quotes. | |
| 1547 | * The normal C backslash escapes are recognized. | |
| 1548 | */ | |
| 1549 | char * | |
| 1550 | ng_get_string_token(const char *s, int *startp, int *lenp) | |
| 1551 | { | |
| 1552 | char *cbuf, *p; | |
| 1553 | int start, off; | |
| 1554 | ||
| 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; | |
| 1572 | ||
| 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++; | |
| 1606 | } | |
| 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++; | |
| 1616 | } | |
| 1617 | *--v = (char)x; | |
| 1618 | break; | |
| 1619 | default: | |
| 1620 | continue; | |
| 1621 | } | |
| 1622 | strcpy(p, v); | |
| 1623 | } | |
| 1624 | } | |
| 1625 | return (NULL); /* no closing quote */ | |
| 1626 | } | |
| 1627 | ||
| 1628 | /* | |
| 1629 | * Encode a string so it can be safely put in double quotes. | |
| 1630 | * Caller must free the result. | |
| 1631 | */ | |
| 1632 | char * | |
| 1633 | ng_encode_string(const char *raw) | |
| 1634 | { | |
| 1635 | char *cbuf; | |
| 1636 | int off = 0; | |
| 1637 | ||
| 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) { | |
| f8c7a42d | 1671 | off += ksprintf(cbuf + off, |
| 984263bc MD |
1672 | "\\x%02x", (u_char)*raw); |
| 1673 | break; | |
| 1674 | } | |
| 1675 | cbuf[off++] = *raw; | |
| 1676 | break; | |
| 1677 | } | |
| 1678 | } | |
| 1679 | cbuf[off++] = '"'; | |
| 1680 | cbuf[off] = '\0'; | |
| 1681 | return (cbuf); | |
| 1682 | } | |
| 1683 | ||
| 1684 | /************************************************************************ | |
| 1685 | VIRTUAL METHOD LOOKUP | |
| 1686 | ************************************************************************/ | |
| 1687 | ||
| 1688 | static ng_parse_t * | |
| 1689 | ng_get_parse_method(const struct ng_parse_type *t) | |
| 1690 | { | |
| 1691 | while (t != NULL && t->parse == NULL) | |
| 1692 | t = t->supertype; | |
| 1693 | return (t ? t->parse : NULL); | |
| 1694 | } | |
| 1695 | ||
| 1696 | static ng_unparse_t * | |
| 1697 | ng_get_unparse_method(const struct ng_parse_type *t) | |
| 1698 | { | |
| 1699 | while (t != NULL && t->unparse == NULL) | |
| 1700 | t = t->supertype; | |
| 1701 | return (t ? t->unparse : NULL); | |
| 1702 | } | |
| 1703 | ||
| 1704 | static ng_getDefault_t * | |
| 1705 | ng_get_getDefault_method(const struct ng_parse_type *t) | |
| 1706 | { | |
| 1707 | while (t != NULL && t->getDefault == NULL) | |
| 1708 | t = t->supertype; | |
| 1709 | return (t ? t->getDefault : NULL); | |
| 1710 | } | |
| 1711 | ||
| 1712 | static ng_getAlign_t * | |
| 1713 | ng_get_getAlign_method(const struct ng_parse_type *t) | |
| 1714 | { | |
| 1715 | while (t != NULL && t->getAlign == NULL) | |
| 1716 | t = t->supertype; | |
| 1717 | return (t ? t->getAlign : NULL); | |
| 1718 | } | |
| 1719 |