xref: /onnv-gate/usr/src/lib/libdhcputil/common/dhcp_symbol.c (revision 3431:9f2d277dcffa)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*3431Scarlsonj  * Common Development and Distribution License (the "License").
6*3431Scarlsonj  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*3431Scarlsonj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <stdlib.h>
290Sstevel@tonic-gate #include <ctype.h>
300Sstevel@tonic-gate #include <strings.h>
310Sstevel@tonic-gate #include <limits.h>
320Sstevel@tonic-gate #include <errno.h>
330Sstevel@tonic-gate #include <dhcp_impl.h>
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #include "dhcp_symbol.h"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate /*
380Sstevel@tonic-gate  * The following structure and table are used to define the attributes
390Sstevel@tonic-gate  * of a DHCP symbol category.
400Sstevel@tonic-gate  */
410Sstevel@tonic-gate typedef struct dsym_cat {
420Sstevel@tonic-gate 	char		*dc_string;	/* string value for the category */
430Sstevel@tonic-gate 	int		dc_minlen;	/* min. chars of dc_string to match */
440Sstevel@tonic-gate 	dsym_category_t	dc_id;		/* numerical value for the category */
450Sstevel@tonic-gate 	boolean_t	dc_dhcptab;	/* valid for dhcptab use? */
460Sstevel@tonic-gate 	ushort_t	dc_min;		/* minimum valid code */
470Sstevel@tonic-gate 	ushort_t	dc_max;		/* maximum valid code */
480Sstevel@tonic-gate } dsym_cat_t;
490Sstevel@tonic-gate 
50*3431Scarlsonj static dsym_cat_t cats[] = {
510Sstevel@tonic-gate 	{ "Extend", 6, DSYM_EXTEND, B_TRUE, DHCP_LAST_STD + 1,
520Sstevel@tonic-gate 		DHCP_SITE_OPT - 1 },
530Sstevel@tonic-gate 	{ "Vendor=", 6, DSYM_VENDOR, B_TRUE, DHCP_FIRST_OPT,
540Sstevel@tonic-gate 		DHCP_LAST_OPT },
550Sstevel@tonic-gate 	{ "Site", 4, DSYM_SITE, B_TRUE, DHCP_SITE_OPT, DHCP_LAST_OPT },
560Sstevel@tonic-gate 	{ "Standard", 8, DSYM_STANDARD, B_FALSE, DHCP_FIRST_OPT,
570Sstevel@tonic-gate 	    DHCP_LAST_STD },
580Sstevel@tonic-gate 	{ "Field", 5, DSYM_FIELD, B_FALSE, CD_PACKET_START,
590Sstevel@tonic-gate 		CD_PACKET_END },
600Sstevel@tonic-gate 	{ "Internal", 8, DSYM_INTERNAL, B_FALSE, CD_INTRNL_START,
610Sstevel@tonic-gate 	    CD_INTRNL_END }
620Sstevel@tonic-gate };
630Sstevel@tonic-gate 
640Sstevel@tonic-gate /*
650Sstevel@tonic-gate  * The following structure and table are used to define the attributes
660Sstevel@tonic-gate  * of a DHCP symbol type.
670Sstevel@tonic-gate  */
680Sstevel@tonic-gate typedef struct dsym_type {
690Sstevel@tonic-gate 	char		*dt_string;	/* string value for the type */
700Sstevel@tonic-gate 	dsym_cdtype_t	dt_id;		/* numerical value for the type */
710Sstevel@tonic-gate 	boolean_t	dt_dhcptab;	/* valid for dhcptab use? */
720Sstevel@tonic-gate } dsym_type_t;
730Sstevel@tonic-gate 
74*3431Scarlsonj static dsym_type_t types[] = {
750Sstevel@tonic-gate 	{ "ASCII", DSYM_ASCII, B_TRUE },
760Sstevel@tonic-gate 	{ "OCTET", DSYM_OCTET, B_TRUE },
770Sstevel@tonic-gate 	{ "IP", DSYM_IP, B_TRUE },
780Sstevel@tonic-gate 	{ "NUMBER", DSYM_NUMBER, B_TRUE },
790Sstevel@tonic-gate 	{ "BOOL", DSYM_BOOL, B_TRUE },
800Sstevel@tonic-gate 	{ "INCLUDE", DSYM_INCLUDE, B_FALSE },
810Sstevel@tonic-gate 	{ "UNUMBER8", DSYM_UNUMBER8, B_TRUE },
820Sstevel@tonic-gate 	{ "UNUMBER16", DSYM_UNUMBER16, B_TRUE },
83*3431Scarlsonj 	{ "UNUMBER24", DSYM_UNUMBER24, B_TRUE },
840Sstevel@tonic-gate 	{ "UNUMBER32", DSYM_UNUMBER32, B_TRUE },
850Sstevel@tonic-gate 	{ "UNUMBER64", DSYM_UNUMBER64, B_TRUE },
860Sstevel@tonic-gate 	{ "SNUMBER8", DSYM_SNUMBER8, B_TRUE },
870Sstevel@tonic-gate 	{ "SNUMBER16", DSYM_SNUMBER16, B_TRUE },
880Sstevel@tonic-gate 	{ "SNUMBER32", DSYM_SNUMBER32, B_TRUE },
89*3431Scarlsonj 	{ "SNUMBER64", DSYM_SNUMBER64, B_TRUE },
90*3431Scarlsonj 	{ "IPV6", DSYM_IPV6, B_TRUE },
91*3431Scarlsonj 	{ "DUID", DSYM_DUID, B_TRUE },
92*3431Scarlsonj 	{ "DOMAIN", DSYM_DOMAIN, B_TRUE }
930Sstevel@tonic-gate };
940Sstevel@tonic-gate 
950Sstevel@tonic-gate /*
960Sstevel@tonic-gate  * symbol delimiters and constants
970Sstevel@tonic-gate  */
980Sstevel@tonic-gate #define	DSYM_CLASS_DEL		" \t\n"
990Sstevel@tonic-gate #define	DSYM_FIELD_DEL		","
1000Sstevel@tonic-gate #define	DSYM_VENDOR_DEL		'='
1010Sstevel@tonic-gate #define	DSYM_QUOTE		'"'
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate /*
1040Sstevel@tonic-gate  * dsym_trim(): trims all whitespace from either side of a string
1050Sstevel@tonic-gate  *
1060Sstevel@tonic-gate  *  input: char **: a pointer to a string to trim of whitespace.
1070Sstevel@tonic-gate  * output: none
1080Sstevel@tonic-gate  */
109*3431Scarlsonj 
1100Sstevel@tonic-gate static void
dsym_trim(char ** str)1110Sstevel@tonic-gate dsym_trim(char **str)
1120Sstevel@tonic-gate {
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	char *tmpstr = *str;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	/*
1170Sstevel@tonic-gate 	 * Trim all whitespace from the front of the string.
1180Sstevel@tonic-gate 	 */
1190Sstevel@tonic-gate 	while (*tmpstr != '\0' && isspace(*tmpstr)) {
1200Sstevel@tonic-gate 		tmpstr++;
1210Sstevel@tonic-gate 	}
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	/*
1240Sstevel@tonic-gate 	 * Move the str pointer to first non-whitespace char.
1250Sstevel@tonic-gate 	 */
1260Sstevel@tonic-gate 	*str = tmpstr;
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	/*
1290Sstevel@tonic-gate 	 * Check case where the string is nothing but whitespace.
1300Sstevel@tonic-gate 	 */
1310Sstevel@tonic-gate 	if (*tmpstr == '\0') {
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 		/*
1340Sstevel@tonic-gate 		 * Trim all whitespace from the end of the string.
1350Sstevel@tonic-gate 		 */
1360Sstevel@tonic-gate 		tmpstr = *str + strlen(*str) - 1;
1370Sstevel@tonic-gate 		while (tmpstr >= *str && isspace(*tmpstr)) {
1380Sstevel@tonic-gate 			tmpstr--;
1390Sstevel@tonic-gate 		}
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 		/*
1420Sstevel@tonic-gate 		 * terminate after last non-whitespace char.
1430Sstevel@tonic-gate 		 */
1440Sstevel@tonic-gate 		*(tmpstr+1) = '\0';
1450Sstevel@tonic-gate 	}
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate /*
1490Sstevel@tonic-gate  * dsym_get_token(): strtok_r() like routine, except consecutive delimiters
1500Sstevel@tonic-gate  *                   result in an empty string
1510Sstevel@tonic-gate  *
1520Sstevel@tonic-gate  *   note: original string is modified
1530Sstevel@tonic-gate  *
1540Sstevel@tonic-gate  *  input: char *: string in which to search for tokens
1550Sstevel@tonic-gate  *         char *: list of possible token delimiter characters
1560Sstevel@tonic-gate  *         char **: location for next call to routine
1570Sstevel@tonic-gate  *         boolean_t: should delimiters be ignored if within quoted string?
1580Sstevel@tonic-gate  * output: char *: token, or NULL if no more tokens
1590Sstevel@tonic-gate  */
160*3431Scarlsonj 
1610Sstevel@tonic-gate static char *
dsym_get_token(char * str,char * dels,char ** lasts,boolean_t quote_support)1620Sstevel@tonic-gate dsym_get_token(char *str, char *dels, char **lasts, boolean_t quote_support)
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	char *ptr = str;
1660Sstevel@tonic-gate 	char *del;
1670Sstevel@tonic-gate 	boolean_t found = B_FALSE;
1680Sstevel@tonic-gate 	boolean_t in_quote = B_FALSE;
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	/*
1710Sstevel@tonic-gate 	 * If incoming string has no tokens return a NULL
1720Sstevel@tonic-gate 	 * pointer to signify no more tokens.
1730Sstevel@tonic-gate 	 */
1740Sstevel@tonic-gate 	if (*ptr == '\0') {
1750Sstevel@tonic-gate 		return (NULL);
1760Sstevel@tonic-gate 	}
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	/*
1790Sstevel@tonic-gate 	 * Loop until either a token has been identified or until end
1800Sstevel@tonic-gate 	 * of string has been reached.
1810Sstevel@tonic-gate 	 */
1820Sstevel@tonic-gate 	while (!found && *ptr != '\0') {
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 		/*
1850Sstevel@tonic-gate 		 * If pointer currently lies within a quoted string,
1860Sstevel@tonic-gate 		 * then do not check for the delimiter.
1870Sstevel@tonic-gate 		 */
1880Sstevel@tonic-gate 		if (!in_quote) {
1890Sstevel@tonic-gate 			for (del = dels; !found && *del != '\0'; del++) {
1900Sstevel@tonic-gate 				if (*del == *ptr) {
1910Sstevel@tonic-gate 					*ptr++ = '\0';
1920Sstevel@tonic-gate 					found = B_TRUE;
1930Sstevel@tonic-gate 				}
1940Sstevel@tonic-gate 			}
1950Sstevel@tonic-gate 		}
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 		/*
1980Sstevel@tonic-gate 		 * If the pointer is pointing at a delimiter, then
1990Sstevel@tonic-gate 		 * check to see if it points to at a quote and update
2000Sstevel@tonic-gate 		 * the state appropriately.
2010Sstevel@tonic-gate 		 */
2020Sstevel@tonic-gate 		if (!found) {
2030Sstevel@tonic-gate 			if (quote_support && *ptr == DSYM_QUOTE) {
2040Sstevel@tonic-gate 				in_quote = !in_quote;
2050Sstevel@tonic-gate 			}
2060Sstevel@tonic-gate 			ptr++;
2070Sstevel@tonic-gate 		}
2080Sstevel@tonic-gate 	}
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	*lasts = ptr;
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	return (str);
2130Sstevel@tonic-gate }
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate /*
2160Sstevel@tonic-gate  * dsym_get_long(): given a numeric string, returns its long value
2170Sstevel@tonic-gate  *
2180Sstevel@tonic-gate  *  input: const char *: the numeric string
2190Sstevel@tonic-gate  *         long *: the return location for the long value
2200Sstevel@tonic-gate  * output: DSYM_SUCCESS, DSYM_VALUE_OUT_OF_RANGE or DSYM_SYNTAX_ERROR
2210Sstevel@tonic-gate  */
222*3431Scarlsonj 
2230Sstevel@tonic-gate static dsym_errcode_t
dsym_get_long(const char * str,long * val)2240Sstevel@tonic-gate dsym_get_long(const char *str, long *val)
2250Sstevel@tonic-gate {
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	int ret = DSYM_SUCCESS;
2280Sstevel@tonic-gate 	int i;
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	for (i = 0; str[i] != '\0'; i++) {
2310Sstevel@tonic-gate 		if (!isdigit(str[i])) {
2320Sstevel@tonic-gate 			return (DSYM_SYNTAX_ERROR);
2330Sstevel@tonic-gate 		}
2340Sstevel@tonic-gate 	}
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	errno = 0;
2370Sstevel@tonic-gate 	*val = strtol(str, NULL, 10);
2380Sstevel@tonic-gate 	if (errno != 0) {
2390Sstevel@tonic-gate 		ret = DSYM_VALUE_OUT_OF_RANGE;
2400Sstevel@tonic-gate 	}
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	return (ret);
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate /*
2460Sstevel@tonic-gate  * dsym_free_classes(): frees the classes allocated by dsym_parse_classes()
2470Sstevel@tonic-gate  *
2480Sstevel@tonic-gate  *  input: dhcp_classes_t *: pointer to structure containing classes to free
2490Sstevel@tonic-gate  * output: none
2500Sstevel@tonic-gate  */
251*3431Scarlsonj 
2520Sstevel@tonic-gate void
dsym_free_classes(dhcp_classes_t * classes)2530Sstevel@tonic-gate dsym_free_classes(dhcp_classes_t *classes)
2540Sstevel@tonic-gate {
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	int i;
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	if (classes->dc_names == NULL) {
2590Sstevel@tonic-gate 		return;
2600Sstevel@tonic-gate 	}
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	for (i = 0; i < classes->dc_cnt; i++) {
2630Sstevel@tonic-gate 		free(classes->dc_names[i]);
2640Sstevel@tonic-gate 	}
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	free(classes->dc_names);
2670Sstevel@tonic-gate 	classes->dc_names = NULL;
2680Sstevel@tonic-gate 	classes->dc_cnt = 0;
2690Sstevel@tonic-gate }
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate /*
2720Sstevel@tonic-gate  * dsym_parse_classes(): given a "Vendor" class string, builds and returns
2730Sstevel@tonic-gate  *                     the list of vendor classes
2740Sstevel@tonic-gate  *
2750Sstevel@tonic-gate  *  input: char *: the "Vendor" class string
2760Sstevel@tonic-gate  *         dhcp_classes_t *: pointer to the classes structure
2770Sstevel@tonic-gate  * output: DSYM_SUCCESS, DSYM_INVALID_CAT, DSYM_EXCEEDS_MAX_CLASS_SIZE,
2780Sstevel@tonic-gate  *         DSYM_EXCEEDS_CLASS_SIZE, DSYM_SYNTAX_ERROR, or DSYM_NO_MEMORY
2790Sstevel@tonic-gate  */
280*3431Scarlsonj 
2810Sstevel@tonic-gate static dsym_errcode_t
dsym_parse_classes(char * ptr,dhcp_classes_t * classes_ret)2820Sstevel@tonic-gate dsym_parse_classes(char *ptr, dhcp_classes_t *classes_ret)
2830Sstevel@tonic-gate {
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	char **classes = NULL;
2860Sstevel@tonic-gate 	char *cp;
2870Sstevel@tonic-gate 	int len;
2880Sstevel@tonic-gate 	int ret = DSYM_SUCCESS;
2890Sstevel@tonic-gate 	int i;
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	while (*ptr != '\0') {
2920Sstevel@tonic-gate 		if (*ptr == DSYM_VENDOR_DEL) {
2930Sstevel@tonic-gate 			ptr++;
2940Sstevel@tonic-gate 			break;
2950Sstevel@tonic-gate 		}
2960Sstevel@tonic-gate 		ptr++;
2970Sstevel@tonic-gate 	}
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	if (*ptr == '\0') {
3000Sstevel@tonic-gate 	    return (DSYM_INVALID_CAT);
3010Sstevel@tonic-gate 	}
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	if (strlen(ptr) > DSYM_MAX_CLASS_SIZE) {
3040Sstevel@tonic-gate 		return (DSYM_EXCEEDS_MAX_CLASS_SIZE);
3050Sstevel@tonic-gate 	}
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 	dsym_trim(&ptr);
3080Sstevel@tonic-gate 	classes_ret->dc_cnt = 0;
3090Sstevel@tonic-gate 	for (i = 0; ret == DSYM_SUCCESS; i++) {
3100Sstevel@tonic-gate 		cp = dsym_get_token(ptr, DSYM_CLASS_DEL, &ptr, B_TRUE);
3110Sstevel@tonic-gate 		if (cp == NULL) {
3120Sstevel@tonic-gate 			break;
3130Sstevel@tonic-gate 		}
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 		len = strlen(cp);
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 		if (len == 0) {
3180Sstevel@tonic-gate 			continue;
3190Sstevel@tonic-gate 		} else if (len > DSYM_CLASS_SIZE) {
3200Sstevel@tonic-gate 			ret = DSYM_EXCEEDS_CLASS_SIZE;
3210Sstevel@tonic-gate 			continue;
3220Sstevel@tonic-gate 		}
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 		if (cp[0] == DSYM_QUOTE && cp[len-1] != DSYM_QUOTE) {
3250Sstevel@tonic-gate 			ret = DSYM_SYNTAX_ERROR;
3260Sstevel@tonic-gate 			continue;
3270Sstevel@tonic-gate 		}
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 		/* Strip off the quotes */
3300Sstevel@tonic-gate 		if (cp[0] == DSYM_QUOTE) {
3310Sstevel@tonic-gate 			cp[len-1] = '\0';
3320Sstevel@tonic-gate 			cp++;
3330Sstevel@tonic-gate 		}
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 		classes = realloc(classes_ret->dc_names,
3360Sstevel@tonic-gate 		    (sizeof (char **)) * (classes_ret->dc_cnt + 1));
3370Sstevel@tonic-gate 		if (classes == NULL ||
3380Sstevel@tonic-gate 		    (classes[classes_ret->dc_cnt] = strdup(cp))
3390Sstevel@tonic-gate 		    == NULL) {
3400Sstevel@tonic-gate 			ret = DSYM_NO_MEMORY;
3410Sstevel@tonic-gate 			continue;
3420Sstevel@tonic-gate 		}
3430Sstevel@tonic-gate 		classes_ret->dc_names = classes;
3440Sstevel@tonic-gate 		classes_ret->dc_cnt++;
3450Sstevel@tonic-gate 	}
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	if (ret != DSYM_SUCCESS) {
3480Sstevel@tonic-gate 		dsym_free_classes(classes_ret);
3490Sstevel@tonic-gate 	}
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	return (ret);
3520Sstevel@tonic-gate }
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate /*
3550Sstevel@tonic-gate  * dsym_get_cat_by_name(): given a category field, returns the pointer to its
3560Sstevel@tonic-gate  *                         entry in the internal category table.
3570Sstevel@tonic-gate  *
3580Sstevel@tonic-gate  *  input: const char *: the category name
3590Sstevel@tonic-gate  *         dsym_cat_t *: the return location for the pointer to the table entry
3600Sstevel@tonic-gate  *         boolean_t: case-sensitive name compare
3610Sstevel@tonic-gate  * output: int: DSYM_SUCCESS or DSYM_INVALID_CAT
3620Sstevel@tonic-gate  */
363*3431Scarlsonj 
3640Sstevel@tonic-gate static dsym_errcode_t
dsym_get_cat_by_name(const char * cat,dsym_cat_t ** entry,boolean_t cs)3650Sstevel@tonic-gate dsym_get_cat_by_name(const char *cat, dsym_cat_t **entry, boolean_t cs)
3660Sstevel@tonic-gate {
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	dsym_cat_t *entryp = NULL;
3690Sstevel@tonic-gate 	int ret = DSYM_SUCCESS;
3700Sstevel@tonic-gate 	int cnt = sizeof (cats) / sizeof (dsym_cat_t);
3710Sstevel@tonic-gate 	int result;
3720Sstevel@tonic-gate 	int len;
3730Sstevel@tonic-gate 	int i;
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	for (i = 0; i < cnt; i++) {
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 		len = cats[i].dc_minlen;
3780Sstevel@tonic-gate 		if (cs) {
3790Sstevel@tonic-gate 			result = strncmp(cat, cats[i].dc_string, len);
3800Sstevel@tonic-gate 		} else {
3810Sstevel@tonic-gate 			result = strncasecmp(cat, cats[i].dc_string, len);
3820Sstevel@tonic-gate 		}
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 		if (result == 0) {
3850Sstevel@tonic-gate 			entryp = &cats[i];
3860Sstevel@tonic-gate 			break;
3870Sstevel@tonic-gate 		}
3880Sstevel@tonic-gate 	}
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	if (entryp != NULL) {
3910Sstevel@tonic-gate 		/*
3920Sstevel@tonic-gate 		 * Special code required for the Vendor category, because we
3930Sstevel@tonic-gate 		 * allow whitespace between the keyword and the delimiter.
3940Sstevel@tonic-gate 		 * If there is no delimiter, then this is an illegal category.
3950Sstevel@tonic-gate 		 */
3960Sstevel@tonic-gate 		const char *ptr = cat + entryp->dc_minlen;
3970Sstevel@tonic-gate 		if (entryp->dc_id == DSYM_VENDOR) {
3980Sstevel@tonic-gate 			while (*ptr != '\0' && isspace(*ptr)) {
3990Sstevel@tonic-gate 				ptr++;
4000Sstevel@tonic-gate 			}
4010Sstevel@tonic-gate 			if (*ptr != DSYM_VENDOR_DEL) {
4020Sstevel@tonic-gate 				ret = DSYM_INVALID_CAT;
4030Sstevel@tonic-gate 			}
4040Sstevel@tonic-gate 		} else {
4050Sstevel@tonic-gate 			if (*ptr != '\0') {
4060Sstevel@tonic-gate 				ret = DSYM_INVALID_CAT;
4070Sstevel@tonic-gate 			}
4080Sstevel@tonic-gate 		}
4090Sstevel@tonic-gate 	} else {
4100Sstevel@tonic-gate 		ret = DSYM_INVALID_CAT;
4110Sstevel@tonic-gate 	}
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	if (ret == DSYM_SUCCESS) {
4140Sstevel@tonic-gate 		*entry = entryp;
4150Sstevel@tonic-gate 	}
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	return (ret);
4180Sstevel@tonic-gate }
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate /*
4210Sstevel@tonic-gate  * dsym_parse_cat(): given a category field, returns the category value
4220Sstevel@tonic-gate  *                 Note: The category must be a valid dhcptab category.
4230Sstevel@tonic-gate  *
4240Sstevel@tonic-gate  *  input: const char *: a category field
4250Sstevel@tonic-gate  *         dsym_category_t *: the return location for the category value
4260Sstevel@tonic-gate  * output: int: DSYM_SUCCESS or DSYM_INVALID_CAT
4270Sstevel@tonic-gate  */
428*3431Scarlsonj 
4290Sstevel@tonic-gate static dsym_errcode_t
dsym_parse_cat(const char * field,dsym_category_t * cat)4300Sstevel@tonic-gate dsym_parse_cat(const char *field, dsym_category_t *cat)
4310Sstevel@tonic-gate {
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	dsym_cat_t *entry;
4340Sstevel@tonic-gate 	int ret;
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	ret = dsym_get_cat_by_name(field, &entry, B_TRUE);
4370Sstevel@tonic-gate 	if (ret == DSYM_SUCCESS) {
4380Sstevel@tonic-gate 		/*
4390Sstevel@tonic-gate 		 * Since this routine is meant to be used to parse dhcptab
4400Sstevel@tonic-gate 		 * symbol definitions, only a subset of the DHCP categories
4410Sstevel@tonic-gate 		 * are valid in this context.
4420Sstevel@tonic-gate 		 */
4430Sstevel@tonic-gate 		if (entry->dc_dhcptab) {
4440Sstevel@tonic-gate 			*cat = entry->dc_id;
4450Sstevel@tonic-gate 		} else {
4460Sstevel@tonic-gate 			ret = DSYM_INVALID_CAT;
4470Sstevel@tonic-gate 		}
4480Sstevel@tonic-gate 	}
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	return (ret);
4510Sstevel@tonic-gate }
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate /*
4540Sstevel@tonic-gate  * dsym_parse_intrange(): given a DHCP integer field, returns the value
4550Sstevel@tonic-gate  *
4560Sstevel@tonic-gate  *  input: const char *: a DHCP code field
4570Sstevel@tonic-gate  *         int *: the return location for the value
4580Sstevel@tonic-gate  *         int: the minimum valid value
4590Sstevel@tonic-gate  *         int: the maximum valid value
4600Sstevel@tonic-gate  * output: int: DSYM_SUCCESS, DSYM_SYNTAX_ERROR, or DSYM_VALUE_OUT_OF_RANGE
4610Sstevel@tonic-gate  */
462*3431Scarlsonj 
4630Sstevel@tonic-gate static dsym_errcode_t
dsym_parse_intrange(const char * field,int * intval,int min,int max)4640Sstevel@tonic-gate dsym_parse_intrange(const char *field, int *intval, int min, int max)
4650Sstevel@tonic-gate {
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	int ret;
4680Sstevel@tonic-gate 	long longval;
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	ret = dsym_get_long(field, &longval);
4710Sstevel@tonic-gate 	if (ret == DSYM_SUCCESS) {
4720Sstevel@tonic-gate 		if (longval < min || longval > max) {
4730Sstevel@tonic-gate 			ret = DSYM_VALUE_OUT_OF_RANGE;
4740Sstevel@tonic-gate 		} else {
4750Sstevel@tonic-gate 			*intval = (int)longval;
4760Sstevel@tonic-gate 		}
4770Sstevel@tonic-gate 	}
4780Sstevel@tonic-gate 	return (ret);
4790Sstevel@tonic-gate }
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate /*
4820Sstevel@tonic-gate  * dsym_validate_code(): given a symbol category and code, validates
4830Sstevel@tonic-gate  *                       that the code is valid for the category
4840Sstevel@tonic-gate  *
4850Sstevel@tonic-gate  *  input: dsym_category_t: the symbol category
4860Sstevel@tonic-gate  *         uint16_t: the symbol code
4870Sstevel@tonic-gate  * output: DSYM_SUCCESS, DSYM_INVALID_CAT or DSYM_CODE_OUT_OF_RANGE
4880Sstevel@tonic-gate  */
489*3431Scarlsonj 
4900Sstevel@tonic-gate static dsym_errcode_t
dsym_validate_code(dsym_category_t cat,ushort_t code)4910Sstevel@tonic-gate dsym_validate_code(dsym_category_t cat, ushort_t code)
4920Sstevel@tonic-gate {
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	int cnt = sizeof (cats) / sizeof (dsym_cat_t);
4950Sstevel@tonic-gate 	int i;
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 	/*
4980Sstevel@tonic-gate 	 * Find the category entry from the internal table.
4990Sstevel@tonic-gate 	 */
5000Sstevel@tonic-gate 	for (i = 0; i < cnt; i++) {
5010Sstevel@tonic-gate 		dsym_cat_t *entry;
5020Sstevel@tonic-gate 		if (cat == cats[i].dc_id) {
5030Sstevel@tonic-gate 			entry = &cats[i];
5040Sstevel@tonic-gate 			if (code < entry->dc_min || code > entry->dc_max) {
5050Sstevel@tonic-gate 				return (DSYM_CODE_OUT_OF_RANGE);
5060Sstevel@tonic-gate 			}
5070Sstevel@tonic-gate 			return (DSYM_SUCCESS);
5080Sstevel@tonic-gate 		}
5090Sstevel@tonic-gate 	}
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	return (DSYM_INVALID_CAT);
5120Sstevel@tonic-gate }
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate /*
5150Sstevel@tonic-gate  * dsym_validate_granularity(): given a symbol type, validates
5160Sstevel@tonic-gate  *                       	that the granularity is valid for the type
5170Sstevel@tonic-gate  *
5180Sstevel@tonic-gate  *  input: dsym_cdtype_t: the symbol type
5190Sstevel@tonic-gate  *         uchar_t: the symbol granularity
5200Sstevel@tonic-gate  * output: DSYM_SUCCESS or DSYM_VALUE_OUT_OF_RANGE
5210Sstevel@tonic-gate  */
522*3431Scarlsonj 
5230Sstevel@tonic-gate static dsym_errcode_t
dsym_validate_granularity(dsym_cdtype_t type,uchar_t gran)5240Sstevel@tonic-gate dsym_validate_granularity(dsym_cdtype_t type, uchar_t gran)
5250Sstevel@tonic-gate {
5260Sstevel@tonic-gate 	/*
5270Sstevel@tonic-gate 	 * We only need to check for a 0 with non-boolean types, as
5280Sstevel@tonic-gate 	 * anything else is already validated by the ranges passed to
5290Sstevel@tonic-gate 	 * dsym_parse_intrange() in dsym_parse_field().
5300Sstevel@tonic-gate 	 */
5310Sstevel@tonic-gate 	if (gran == 0 && type != DSYM_BOOL) {
5320Sstevel@tonic-gate 		return (DSYM_VALUE_OUT_OF_RANGE);
5330Sstevel@tonic-gate 	}
5340Sstevel@tonic-gate 	return (DSYM_SUCCESS);
5350Sstevel@tonic-gate }
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate /*
5380Sstevel@tonic-gate  * dsym_get_type_by_name(): given a type field, returns the pointer to its
5390Sstevel@tonic-gate  *                          entry in the internal type table.
5400Sstevel@tonic-gate  *
5410Sstevel@tonic-gate  *  input: const char *: the type name
5420Sstevel@tonic-gate  *         dsym_type_t *: the return location for the pointer to the table entry
5430Sstevel@tonic-gate  *         boolean_t: case-sensitive name compare
5440Sstevel@tonic-gate  * output: int: DSYM_SUCCESS or DSYM_INVALID_TYPE
5450Sstevel@tonic-gate  */
546*3431Scarlsonj 
5470Sstevel@tonic-gate static dsym_errcode_t
dsym_get_type_by_name(const char * type,dsym_type_t ** entry,boolean_t cs)5480Sstevel@tonic-gate dsym_get_type_by_name(const char *type, dsym_type_t **entry, boolean_t cs)
5490Sstevel@tonic-gate {
5500Sstevel@tonic-gate 	int cnt = sizeof (types) / sizeof (dsym_type_t);
5510Sstevel@tonic-gate 	int result;
5520Sstevel@tonic-gate 	int i;
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 	for (i = 0; i < cnt; i++) {
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 		if (cs) {
5570Sstevel@tonic-gate 			result = strcmp(type, types[i].dt_string);
5580Sstevel@tonic-gate 		} else {
5590Sstevel@tonic-gate 			result = strcasecmp(type, types[i].dt_string);
5600Sstevel@tonic-gate 		}
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 		if (result == 0) {
5630Sstevel@tonic-gate 			*entry = &types[i];
5640Sstevel@tonic-gate 			return (DSYM_SUCCESS);
5650Sstevel@tonic-gate 		}
5660Sstevel@tonic-gate 	}
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	return (DSYM_INVALID_TYPE);
5690Sstevel@tonic-gate }
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate /*
5720Sstevel@tonic-gate  * dsym_parse_type(): given a DHCP type string, returns the type id
5730Sstevel@tonic-gate  *
5740Sstevel@tonic-gate  *  input: char *: a DHCP type string
5750Sstevel@tonic-gate  *         dsym_cdtype_t *: the return location for the type id
5760Sstevel@tonic-gate  * output: int: DSYM_SUCCESS or DSYM_INVALID_TYPE
5770Sstevel@tonic-gate  */
578*3431Scarlsonj 
5790Sstevel@tonic-gate static dsym_errcode_t
dsym_parse_type(char * field,dsym_cdtype_t * type)5800Sstevel@tonic-gate dsym_parse_type(char *field, dsym_cdtype_t *type)
5810Sstevel@tonic-gate {
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	dsym_type_t *entry;
5840Sstevel@tonic-gate 	int ret;
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 	ret = dsym_get_type_by_name(field, &entry, B_TRUE);
5870Sstevel@tonic-gate 	if (ret == DSYM_SUCCESS) {
5880Sstevel@tonic-gate 		/*
5890Sstevel@tonic-gate 		 * Since this routine is meant to be used to parse dhcptab
5900Sstevel@tonic-gate 		 * symbol definitions, only a subset of the DHCP type
5910Sstevel@tonic-gate 		 * are valid in this context.
5920Sstevel@tonic-gate 		 */
5930Sstevel@tonic-gate 		if (entry->dt_dhcptab) {
5940Sstevel@tonic-gate 			*type = entry->dt_id;
5950Sstevel@tonic-gate 		} else {
5960Sstevel@tonic-gate 			ret = DSYM_INVALID_TYPE;
5970Sstevel@tonic-gate 		}
5980Sstevel@tonic-gate 	}
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	return (ret);
6010Sstevel@tonic-gate }
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate /*
6040Sstevel@tonic-gate  * dsym_free_fields(): frees an array of fields allocated by
6050Sstevel@tonic-gate  *                     dsym_init_parser().
6060Sstevel@tonic-gate  *
6070Sstevel@tonic-gate  *  input: char **: array of fields to free
6080Sstevel@tonic-gate  * output: none
6090Sstevel@tonic-gate  */
610*3431Scarlsonj 
6110Sstevel@tonic-gate void
dsym_free_fields(char ** fields)6120Sstevel@tonic-gate dsym_free_fields(char **fields)
6130Sstevel@tonic-gate {
6140Sstevel@tonic-gate 	int i;
6150Sstevel@tonic-gate 	if (fields != NULL) {
6160Sstevel@tonic-gate 		for (i = 0; i < DSYM_NUM_FIELDS; i++) {
6170Sstevel@tonic-gate 			free(fields[i]);
6180Sstevel@tonic-gate 		}
6190Sstevel@tonic-gate 		free(fields);
6200Sstevel@tonic-gate 	}
6210Sstevel@tonic-gate }
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate /*
6240Sstevel@tonic-gate  * dsym_close_parser(): free up all resources associated with the parser
6250Sstevel@tonic-gate  *
6260Sstevel@tonic-gate  *  input: char **: the fields allocated by dsym_init_parser()
6270Sstevel@tonic-gate  *         dhcp_symbol_t *: the structure populated by dsym_init_parser()
6280Sstevel@tonic-gate  * output: none
6290Sstevel@tonic-gate  */
630*3431Scarlsonj 
6310Sstevel@tonic-gate void
dsym_close_parser(char ** fields,dhcp_symbol_t * sym)6320Sstevel@tonic-gate dsym_close_parser(char **fields, dhcp_symbol_t *sym)
6330Sstevel@tonic-gate {
6340Sstevel@tonic-gate 	dsym_free_fields(fields);
6350Sstevel@tonic-gate 	dsym_free_classes(&sym->ds_classes);
6360Sstevel@tonic-gate }
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate /*
6390Sstevel@tonic-gate  * dsym_init_parser(): initializes the structures used to parse a symbol
6400Sstevel@tonic-gate  *                     value.
6410Sstevel@tonic-gate  *
6420Sstevel@tonic-gate  *  input: const char *: the symbol name
6430Sstevel@tonic-gate  *         const char *: the symbol value in dhcptab format
6440Sstevel@tonic-gate  *         char ***: the return location for the symbol fields
6450Sstevel@tonic-gate  *         dhcp_symbol_t *: the structure which eventually will
6460Sstevel@tonic-gate  *                          be the repository for the parsed symbol data
6470Sstevel@tonic-gate  * output: int: DSYM_SUCCESS, DYSM_NO_MEMORY, DSYM_NULL_FIELD or
6480Sstevel@tonic-gate  *              DSYM_TOO_MANY_FIELDS
6490Sstevel@tonic-gate  */
650*3431Scarlsonj 
6510Sstevel@tonic-gate dsym_errcode_t
dsym_init_parser(const char * name,const char * value,char *** fields_ret,dhcp_symbol_t * sym)6520Sstevel@tonic-gate dsym_init_parser(const char *name, const char *value, char ***fields_ret,
6530Sstevel@tonic-gate     dhcp_symbol_t *sym)
6540Sstevel@tonic-gate {
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 	int ret = DSYM_SUCCESS;
6570Sstevel@tonic-gate 	char *cp;
6580Sstevel@tonic-gate 	char *next;
6590Sstevel@tonic-gate 	char *field;
6600Sstevel@tonic-gate 	char **fields;
6610Sstevel@tonic-gate 	int i;
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate 	/*
6640Sstevel@tonic-gate 	 * Initialize the symbol structure.
6650Sstevel@tonic-gate 	 */
6660Sstevel@tonic-gate 	sym->ds_category = 0;
6670Sstevel@tonic-gate 	sym->ds_code = 0;
6680Sstevel@tonic-gate 	(void) strlcpy(sym->ds_name, name, DSYM_MAX_SYM_LEN);
6690Sstevel@tonic-gate 	sym->ds_type = 0;
6700Sstevel@tonic-gate 	sym->ds_gran = 0;
6710Sstevel@tonic-gate 	sym->ds_max = 0;
6720Sstevel@tonic-gate 	sym->ds_classes.dc_names = NULL;
6730Sstevel@tonic-gate 	sym->ds_classes.dc_cnt = 0;
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 	if ((cp = strdup(value)) == NULL ||
6760Sstevel@tonic-gate 	    (fields = calloc(DSYM_NUM_FIELDS, sizeof (char *))) == NULL) {
6770Sstevel@tonic-gate 		ret = DSYM_NO_MEMORY;
6780Sstevel@tonic-gate 	}
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	next = cp;
6810Sstevel@tonic-gate 	for (i = 0; ret == DSYM_SUCCESS && i < DSYM_NUM_FIELDS; i++) {
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 		field = dsym_get_token(next, DSYM_FIELD_DEL, &next,
6840Sstevel@tonic-gate 			B_FALSE);
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 		if (field == NULL) {
6870Sstevel@tonic-gate 			ret = DSYM_NULL_FIELD;
6880Sstevel@tonic-gate 			continue;
6890Sstevel@tonic-gate 		}
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 		dsym_trim(&field);
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 		if (strlen(field) == 0) {
6940Sstevel@tonic-gate 			ret = DSYM_NULL_FIELD;
6950Sstevel@tonic-gate 			continue;
6960Sstevel@tonic-gate 		}
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 		if ((fields[i] = strdup(field)) == NULL) {
6990Sstevel@tonic-gate 			ret = DSYM_NO_MEMORY;
7000Sstevel@tonic-gate 			continue;
7010Sstevel@tonic-gate 		}
7020Sstevel@tonic-gate 	}
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 	if (ret == DSYM_SUCCESS &&
7050Sstevel@tonic-gate 	    dsym_get_token(next, DSYM_FIELD_DEL, &next, B_FALSE) != NULL) {
7060Sstevel@tonic-gate 		ret = DSYM_TOO_MANY_FIELDS;
7070Sstevel@tonic-gate 	}
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 	if (ret != DSYM_SUCCESS) {
7100Sstevel@tonic-gate 		dsym_free_fields(fields);
7110Sstevel@tonic-gate 	} else {
7120Sstevel@tonic-gate 		*fields_ret = fields;
7130Sstevel@tonic-gate 	}
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 	free(cp);
7160Sstevel@tonic-gate 	return (ret);
7170Sstevel@tonic-gate }
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate /*
7200Sstevel@tonic-gate  * dsym_parse_field(): parses the specified symbol field.
7210Sstevel@tonic-gate  *
7220Sstevel@tonic-gate  *  input: int: the field number to be parsed.
7230Sstevel@tonic-gate  *         char **: symbol fields initialized by dsym_init_parser()
7240Sstevel@tonic-gate  *         dhcp_symbol_t *: the structure which will be the repository
7250Sstevel@tonic-gate  *                          for the parsed field
7260Sstevel@tonic-gate  * output: int: DSYM_SUCCESS, DSYM_SYNTAX_ERROR, DSYM_CODE_OUT_OF_RANGE,
7270Sstevel@tonic-gate  *              DSYM_INVALID_CAT, DSYM_INVALID_TYPE, DSYM_EXCEEDS_CLASS_SIZE,
7280Sstevel@tonic-gate  *              DSYM_EXCEEDS_MAX_CLASS_SIZE, DSYM_NO_MEMORY,
7290Sstevel@tonic-gate  *              DSYM_INVALID_FIELD_NUM, DSYM_VALUE_OUT_OF_RANGE
7300Sstevel@tonic-gate  */
731*3431Scarlsonj 
7320Sstevel@tonic-gate dsym_errcode_t
dsym_parse_field(int field_num,char ** fields,dhcp_symbol_t * sym)7330Sstevel@tonic-gate dsym_parse_field(int field_num, char **fields, dhcp_symbol_t *sym)
7340Sstevel@tonic-gate {
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	int 	ret = DSYM_SUCCESS;
7370Sstevel@tonic-gate 	int	intval;
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 	switch (field_num) {
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate 	case DSYM_CAT_FIELD:
7420Sstevel@tonic-gate 		ret = dsym_parse_cat(fields[field_num], &sym->ds_category);
7430Sstevel@tonic-gate 		if (ret == DSYM_SUCCESS && sym->ds_category == DSYM_VENDOR) {
7440Sstevel@tonic-gate 			ret = dsym_parse_classes(fields[field_num],
7450Sstevel@tonic-gate 			    &sym->ds_classes);
7460Sstevel@tonic-gate 		}
7470Sstevel@tonic-gate 		break;
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 	case DSYM_CODE_FIELD:
7500Sstevel@tonic-gate 		ret = dsym_parse_intrange(fields[field_num], &intval, 0,
7510Sstevel@tonic-gate 		    USHRT_MAX);
7520Sstevel@tonic-gate 		if (ret == DSYM_SUCCESS) {
7530Sstevel@tonic-gate 			sym->ds_code = (ushort_t)intval;
7540Sstevel@tonic-gate 			ret = dsym_validate_code(sym->ds_category,
7550Sstevel@tonic-gate 			    sym->ds_code);
7560Sstevel@tonic-gate 		}
7570Sstevel@tonic-gate 		break;
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 	case DSYM_TYPE_FIELD:
7600Sstevel@tonic-gate 		ret = dsym_parse_type(fields[field_num], &sym->ds_type);
7610Sstevel@tonic-gate 		break;
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	case DSYM_GRAN_FIELD:
7640Sstevel@tonic-gate 		ret = dsym_parse_intrange(fields[field_num], &intval, 0,
7650Sstevel@tonic-gate 		    UCHAR_MAX);
7660Sstevel@tonic-gate 		if (ret == DSYM_SUCCESS) {
7670Sstevel@tonic-gate 			sym->ds_gran = (uchar_t)intval;
7680Sstevel@tonic-gate 			ret = dsym_validate_granularity(sym->ds_type,
7690Sstevel@tonic-gate 			    sym->ds_gran);
7700Sstevel@tonic-gate 		}
7710Sstevel@tonic-gate 		break;
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 	case DSYM_MAX_FIELD:
7740Sstevel@tonic-gate 		ret = dsym_parse_intrange(fields[field_num], &intval, 0,
7750Sstevel@tonic-gate 		    UCHAR_MAX);
7760Sstevel@tonic-gate 		if (ret == DSYM_SUCCESS) {
7770Sstevel@tonic-gate 			sym->ds_max = (uchar_t)intval;
7780Sstevel@tonic-gate 		}
7790Sstevel@tonic-gate 		break;
7800Sstevel@tonic-gate 	default:
7810Sstevel@tonic-gate 		ret = DSYM_INVALID_FIELD_NUM;
7820Sstevel@tonic-gate 	}
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 	return (ret);
7850Sstevel@tonic-gate }
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate /*
7880Sstevel@tonic-gate  * dsym_parser(): parses a DHCP symbol value
7890Sstevel@tonic-gate  *
7900Sstevel@tonic-gate  *  input: char **: symbol fields initialized by dsym_init_parser()
7910Sstevel@tonic-gate  *         dhcp_symbol_t *: the structure which will be the repository
7920Sstevel@tonic-gate  *                          for the parsed field
7930Sstevel@tonic-gate  *         int *: last field processed
7940Sstevel@tonic-gate  *         boolean_t: parse all fields even though errors occur?
7950Sstevel@tonic-gate  * output: int: DSYM_SUCCESS, DSYM_SYNTAX_ERROR, DSYM_CODE_OUT_OF_RANGE,
7960Sstevel@tonic-gate  *              DSYM_INVALID_CAT, DSYM_INVALID_TYPE, DSYM_EXCEEDS_CLASS_SIZE,
7970Sstevel@tonic-gate  *              DSYM_EXCEEDS_MAX_CLASS_SIZE, DSYM_NO_MEMORY
7980Sstevel@tonic-gate  *              DSYM_INVALID_FIELD_NUM, DSYM_VALUE_OUT_OF_RANGE
7990Sstevel@tonic-gate  */
800*3431Scarlsonj 
8010Sstevel@tonic-gate dsym_errcode_t
dsym_parser(char ** fields,dhcp_symbol_t * sym,int * lastField,boolean_t bestEffort)8020Sstevel@tonic-gate dsym_parser(char **fields, dhcp_symbol_t *sym, int *lastField,
8030Sstevel@tonic-gate     boolean_t bestEffort)
8040Sstevel@tonic-gate {
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	int ret = DSYM_SUCCESS;
8070Sstevel@tonic-gate 	int tret = DSYM_SUCCESS;
8080Sstevel@tonic-gate 	int i;
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 	*lastField = -1;
8110Sstevel@tonic-gate 	for (i = DSYM_FIRST_FIELD;
8120Sstevel@tonic-gate 	    tret == DSYM_SUCCESS && i < DSYM_NUM_FIELDS; i++) {
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate 		tret = dsym_parse_field(i, fields, sym);
8150Sstevel@tonic-gate 		if (tret != DSYM_SUCCESS) {
8160Sstevel@tonic-gate 			if (ret == DSYM_SUCCESS) {
8170Sstevel@tonic-gate 				ret = tret;
8180Sstevel@tonic-gate 			}
8190Sstevel@tonic-gate 			if (bestEffort) {
8200Sstevel@tonic-gate 				*lastField = i;
8210Sstevel@tonic-gate 				tret = DSYM_SUCCESS;
8220Sstevel@tonic-gate 			}
8230Sstevel@tonic-gate 		}
8240Sstevel@tonic-gate 	}
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 	if (*lastField == -1) {
8270Sstevel@tonic-gate 		*lastField = i - 1;
8280Sstevel@tonic-gate 	}
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 	return (ret);
8310Sstevel@tonic-gate }
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate /*
8340Sstevel@tonic-gate  * dsym_get_cat_id(): given a category string, return the associated id.
8350Sstevel@tonic-gate  *
8360Sstevel@tonic-gate  *  input: const char *: the category name
8370Sstevel@tonic-gate  *         dsym_category_t *: the return location for the id
8380Sstevel@tonic-gate  *         boolean_t: case-sensitive name compare
8390Sstevel@tonic-gate  * output: int: DSYM_SUCCESS or DSYM_INVALID_CAT
8400Sstevel@tonic-gate  */
841*3431Scarlsonj 
8420Sstevel@tonic-gate dsym_errcode_t
dsym_get_cat_id(const char * cat,dsym_category_t * id,boolean_t cs)8430Sstevel@tonic-gate dsym_get_cat_id(const char *cat, dsym_category_t *id, boolean_t cs)
8440Sstevel@tonic-gate {
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate 	dsym_cat_t *entry;
8470Sstevel@tonic-gate 	int ret;
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 	ret = dsym_get_cat_by_name(cat, &entry, cs);
8500Sstevel@tonic-gate 	if (ret == DSYM_SUCCESS) {
8510Sstevel@tonic-gate 		*id = entry->dc_id;
8520Sstevel@tonic-gate 	}
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate 	return (ret);
8550Sstevel@tonic-gate }
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate /*
8580Sstevel@tonic-gate  * dsym_get_code_ranges(): given a category field, returns its valid code
8590Sstevel@tonic-gate  *                         ranges.
8600Sstevel@tonic-gate  *
8610Sstevel@tonic-gate  *  input: const char *: the category name
8620Sstevel@tonic-gate  *         ushort *: return location for the minimum code value.
8630Sstevel@tonic-gate  *         ushort *: return location for the maximum code value.
8640Sstevel@tonic-gate  *         boolean_t: case-sensitive name compare
8650Sstevel@tonic-gate  * output: int: DSYM_SUCCESS or DSYM_INVALID_CAT
8660Sstevel@tonic-gate  */
867*3431Scarlsonj 
8680Sstevel@tonic-gate dsym_errcode_t
dsym_get_code_ranges(const char * cat,ushort_t * min,ushort_t * max,boolean_t cs)8690Sstevel@tonic-gate dsym_get_code_ranges(const char *cat, ushort_t *min, ushort_t *max,
8700Sstevel@tonic-gate     boolean_t cs)
8710Sstevel@tonic-gate {
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate 	dsym_cat_t *entry;
8740Sstevel@tonic-gate 	int ret;
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate 	ret = dsym_get_cat_by_name(cat, &entry, cs);
8770Sstevel@tonic-gate 	if (ret == DSYM_SUCCESS) {
8780Sstevel@tonic-gate 		*min = entry->dc_min;
8790Sstevel@tonic-gate 		*max = entry->dc_max;
8800Sstevel@tonic-gate 	}
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 	return (ret);
8830Sstevel@tonic-gate }
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate /*
8860Sstevel@tonic-gate  * dsym_get_type_id(): given a type string, return the associated type id.
8870Sstevel@tonic-gate  *
8880Sstevel@tonic-gate  *  input: const char *: the type name
8890Sstevel@tonic-gate  *         dsym_cdtype_t *: the return location for the id
8900Sstevel@tonic-gate  *         boolean_t: case-sensitive name compare
8910Sstevel@tonic-gate  * output: int: DSYM_SUCCESS or DSYM_INVALID_TYPE
8920Sstevel@tonic-gate  */
893*3431Scarlsonj 
8940Sstevel@tonic-gate dsym_errcode_t
dsym_get_type_id(const char * type,dsym_cdtype_t * id,boolean_t cs)8950Sstevel@tonic-gate dsym_get_type_id(const char *type, dsym_cdtype_t *id, boolean_t cs)
8960Sstevel@tonic-gate {
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 	dsym_type_t *entry;
8990Sstevel@tonic-gate 	int ret;
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 	ret = dsym_get_type_by_name(type, &entry, cs);
9020Sstevel@tonic-gate 	if (ret == DSYM_SUCCESS) {
9030Sstevel@tonic-gate 		*id = entry->dt_id;
9040Sstevel@tonic-gate 	}
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 	return (ret);
9070Sstevel@tonic-gate }
908