Path: blob/main/sys/contrib/openzfs/lib/libuutil/uu_ident.c
48375 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* Common Development and Distribution License, Version 1.0 only6* (the "License"). You may not use this file except in compliance7* with the License.8*9* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE10* or https://opensource.org/licenses/CDDL-1.0.11* See the License for the specific language governing permissions12* and limitations under the License.13*14* When distributing Covered Code, include this CDDL HEADER in each15* file and include the License file at usr/src/OPENSOLARIS.LICENSE.16* If applicable, add the following below this CDDL HEADER, with the17* fields enclosed by brackets "[]" replaced with your own identifying18* information: Portions Copyright [yyyy] [name of copyright owner]19*20* CDDL HEADER END21*/22/*23* Copyright 2004 Sun Microsystems, Inc. All rights reserved.24* Use is subject to license terms.25*/26272829#include "libuutil_common.h"3031#include <string.h>3233/*34* We require names of the form:35* [provider,]identifier[/[provider,]identifier]...36*37* Where provider is either a stock symbol (SUNW) or a java-style reversed38* domain name (com.sun).39*40* Both providers and identifiers must start with a letter, and may41* only contain alphanumerics, dashes, and underlines. Providers42* may also contain periods.43*44* Note that we do _not_ use the macros in <ctype.h>, since they are affected45* by the current locale settings.46*/4748#define IS_ALPHA(c) \49(((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))5051#define IS_DIGIT(c) \52((c) >= '0' && (c) <= '9')5354static int55is_valid_ident(const char *s, const char *e, int allowdot)56{57char c;5859if (s >= e)60return (0); /* name is empty */6162c = *s++;63if (!IS_ALPHA(c))64return (0); /* does not start with letter */6566while (s < e && (c = *s++) != 0) {67if (IS_ALPHA(c) || IS_DIGIT(c) || c == '-' || c == '_' ||68(allowdot && c == '.'))69continue;70return (0); /* invalid character */71}72return (1);73}7475static int76is_valid_component(const char *b, const char *e, uint_t flags)77{78char *sp;7980if (flags & UU_NAME_DOMAIN) {81sp = strchr(b, ',');82if (sp != NULL && sp < e) {83if (!is_valid_ident(b, sp, 1))84return (0);85b = sp + 1;86}87}8889return (is_valid_ident(b, e, 0));90}9192int93uu_check_name(const char *name, uint_t flags)94{95const char *end = name + strlen(name);96const char *p;9798if (flags & ~(UU_NAME_DOMAIN | UU_NAME_PATH)) {99uu_set_error(UU_ERROR_UNKNOWN_FLAG);100return (-1);101}102103if (!(flags & UU_NAME_PATH)) {104if (!is_valid_component(name, end, flags))105goto bad;106return (0);107}108109while ((p = strchr(name, '/')) != NULL) {110if (!is_valid_component(name, p - 1, flags))111goto bad;112name = p + 1;113}114if (!is_valid_component(name, end, flags))115goto bad;116117return (0);118119bad:120uu_set_error(UU_ERROR_INVALID_ARGUMENT);121return (-1);122}123124125