bus-internal.c revision f5d8989ce5fc4e6eb338ec7b1b2c6d6a74c44c63
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering/***
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering This file is part of systemd.
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering Copyright 2013 Lennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering systemd is free software; you can redistribute it and/or modify it
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering under the terms of the GNU Lesser General Public License as published by
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering the Free Software Foundation; either version 2.1 of the License, or
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering (at your option) any later version.
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering systemd is distributed in the hope that it will be useful, but
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering Lesser General Public License for more details.
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering You should have received a copy of the GNU Lesser General Public License
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering***/
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering#include "bus-internal.h"
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poetteringbool object_path_is_valid(const char *p) {
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering const char *q;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering bool slash;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (!p)
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return false;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (p[0] != '/')
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return false;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (p[1] == 0)
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return true;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering for (slash = true, q = p+1; *q; q++)
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (*q == '/') {
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (slash)
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return false;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering slash = true;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering } else {
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering bool good;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering good =
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering (*q >= 'a' && *q <= 'z') ||
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering (*q >= 'A' && *q <= 'Z') ||
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering (*q >= '0' && *q <= '9') ||
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering *q == '_';
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (!good)
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return false;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering slash = false;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering }
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (slash)
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return false;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return true;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering}
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poetteringchar* object_path_startswith(const char *a, const char *b) {
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering const char *p;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (!object_path_is_valid(a) ||
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering !object_path_is_valid(b))
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return NULL;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (streq(b, "/"))
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return (char*) a + 1;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering p = startswith(a, b);
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (!p)
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return NULL;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (*p == 0)
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return (char*) p;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (*p == '/')
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return (char*) p + 1;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return NULL;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering}
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poetteringbool interface_name_is_valid(const char *p) {
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering const char *q;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering bool dot, found_dot = false;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (isempty(p))
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return false;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering for (dot = true, q = p; *q; q++)
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (*q == '.') {
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (dot)
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return false;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering found_dot = dot = true;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering } else {
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering bool good;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering good =
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering (*q >= 'a' && *q <= 'z') ||
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering (*q >= 'A' && *q <= 'Z') ||
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering (!dot && *q >= '0' && *q <= '9') ||
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering *q == '_';
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (!good)
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return false;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering dot = false;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering }
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (q - p > 255)
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return false;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (dot)
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return false;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (!found_dot)
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return false;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return true;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering}
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poetteringbool service_name_is_valid(const char *p) {
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering const char *q;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering bool dot, found_dot = false, unique;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (isempty(p))
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return false;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering unique = p[0] == ':';
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering for (dot = true, q = unique ? p+1 : p; *q; q++)
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (*q == '.') {
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (dot)
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return false;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering found_dot = dot = true;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering } else {
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering bool good;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering good =
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering (*q >= 'a' && *q <= 'z') ||
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering (*q >= 'A' && *q <= 'Z') ||
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering ((!dot || unique) && *q >= '0' && *q <= '9') ||
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering *q == '_' || *q == '-';
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (!good)
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return false;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering dot = false;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering }
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (q - p > 255)
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return false;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (dot)
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return false;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (!found_dot)
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return false;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return true;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering}
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poetteringchar* service_name_startswith(const char *a, const char *b) {
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering const char *p;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (!service_name_is_valid(a) ||
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering !service_name_is_valid(b))
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return NULL;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering p = startswith(a, b);
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (!p)
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return NULL;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (*p == 0)
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return (char*) p;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (*p == '.')
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return (char*) p + 1;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return NULL;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering}
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poetteringbool member_name_is_valid(const char *p) {
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering const char *q;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (isempty(p))
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return false;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering for (q = p; *q; q++) {
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering bool good;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering good =
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering (*q >= 'a' && *q <= 'z') ||
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering (*q >= 'A' && *q <= 'Z') ||
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering (*q >= '0' && *q <= '9') ||
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering *q == '_';
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (!good)
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return false;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering }
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (q - p > 255)
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return false;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return true;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering}
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poetteringstatic bool complex_pattern_check(char c, const char *a, const char *b) {
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering bool separator = false;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (!a && !b)
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return true;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (!a || !b)
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return false;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering for (;;) {
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (*a != *b)
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return (separator && (*a == 0 || *b == 0)) ||
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering (*a == 0 && *b == c && b[1] == 0) ||
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering (*b == 0 && *a == c && a[1] == 0);
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (*a == 0)
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return true;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering separator = *a == c;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering a++, b++;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering }
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering}
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poetteringbool namespace_complex_pattern(const char *pattern, const char *value) {
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return complex_pattern_check('.', pattern, value);
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering}
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poetteringbool path_complex_pattern(const char *pattern, const char *value) {
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return complex_pattern_check('/', pattern, value);
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering}
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poetteringstatic bool simple_pattern_check(char c, const char *a, const char *b) {
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (!a && !b)
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return true;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (!a || !b)
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return false;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering for (;;) {
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (*a != *b)
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return *a == 0 && *b == c;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (*a == 0)
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return true;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering a++, b++;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering }
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering}
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poetteringbool namespace_simple_pattern(const char *pattern, const char *value) {
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return simple_pattern_check('.', pattern, value);
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering}
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poetteringbool path_simple_pattern(const char *pattern, const char *value) {
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return simple_pattern_check('/', pattern, value);
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering}
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poetteringint bus_message_type_from_string(const char *s, uint8_t *u) {
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (streq(s, "signal"))
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering *u = SD_BUS_MESSAGE_SIGNAL;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering else if (streq(s, "method_call"))
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering *u = SD_BUS_MESSAGE_METHOD_CALL;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering else if (streq(s, "error"))
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering *u = SD_BUS_MESSAGE_METHOD_ERROR;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering else if (streq(s, "method_return"))
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering *u = SD_BUS_MESSAGE_METHOD_RETURN;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering else
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return -EINVAL;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return 0;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering}
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poetteringconst char *bus_message_type_to_string(uint8_t u) {
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (u == SD_BUS_MESSAGE_SIGNAL)
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return "signal";
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering else if (u == SD_BUS_MESSAGE_METHOD_CALL)
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return "method_call";
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering else if (u == SD_BUS_MESSAGE_METHOD_ERROR)
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return "error";
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering else if (u == SD_BUS_MESSAGE_METHOD_RETURN)
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return "method_return";
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering else
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return NULL;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering}
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poetteringchar *bus_address_escape(const char *v) {
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering const char *a;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering char *r, *b;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering r = new(char, strlen(v)*3+1);
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if (!r)
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return NULL;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering for (a = v, b = r; *a; a++) {
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering if ((*a >= '0' && *a <= '9') ||
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering (*a >= 'a' && *a <= 'z') ||
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering (*a >= 'A' && *a <= 'Z') ||
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering strchr("_-/.", *a))
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering *(b++) = *a;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering else {
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering *(b++) = '%';
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering *(b++) = hexchar(*a >> 4);
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering *(b++) = hexchar(*a & 0xF);
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering }
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering }
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering *b = 0;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering return r;
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering}
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering