util.c revision ca92350db6ad6ac344181f7b8ec695eda29da675
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor/*
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor Authors:
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor Simo Sorce <ssorce@redhat.com>
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor Copyright (C) 2009 Red Hat
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor This program is free software; you can redistribute it and/or modify
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor it under the terms of the GNU General Public License as published by
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor the Free Software Foundation; either version 3 of the License, or
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor (at your option) any later version.
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen This program is distributed in the hope that it will be useful,
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen but WITHOUT ANY WARRANTY; without even the implied warranty of
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor GNU General Public License for more details.
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor You should have received a copy of the GNU General Public License
d229f940abfb2490dee17979e9a5ff31b7012eb5rbowen along with this program. If not, see <http://www.gnu.org/licenses/>.
3f08db06526d6901aa08c110b5bc7dde6bc39905nd*/
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#include <ctype.h>
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
3f08db06526d6901aa08c110b5bc7dde6bc39905nd#include "talloc.h"
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#include "util/util.h"
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor#include "dhash.h"
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor/* split a string into an allocated array of strings.
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * the separator is a string, and is case-sensitive.
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung * optionally single values can be trimmed of of spaces and tabs */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzorint split_on_separator(TALLOC_CTX *mem_ctx, const char *str,
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor const char sep, bool trim, char ***_list, int *size)
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor{
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor const char *t, *p, *n;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor size_t l, len;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor char **list, **r;
48c64aeceef385e19025b384bd719b2a9789592dnd const char sep_str[2] = { sep, '\0'};
48c64aeceef385e19025b384bd719b2a9789592dnd
48c64aeceef385e19025b384bd719b2a9789592dnd if (!str || !*str || !_list) return EINVAL;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor t = str;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor list = NULL;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor l = 0;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor /* trim leading whitespace */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (trim)
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor while (isspace(*t)) t++;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor /* find substrings separated by the separator */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor while (t && (p = strpbrk(t, sep_str))) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor len = p - t;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor n = p + 1; /* save next string starting point */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (trim) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor /* strip whitespace after the separator
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * so it's not in the next token */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor while (isspace(*t)) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor t++;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor len--;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (len == 0) break;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor p--;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor /* strip whitespace before the separator
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * so it's not in the current token */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor while (len > 0 && (isspace(*p))) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor len--;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor p--;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor /* Add the token to the array, +2 b/c of the trailing NULL */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor r = talloc_realloc(mem_ctx, list, char *, l + 2);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (!r) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor talloc_free(list);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor return ENOMEM;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor } else {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor list = r;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cae0359c9286c8e34cbccd15eee2da90562c1ee2sf if (len == 0) {
cae0359c9286c8e34cbccd15eee2da90562c1ee2sf list[l] = talloc_strdup(list, "");
cae0359c9286c8e34cbccd15eee2da90562c1ee2sf } else {
cae0359c9286c8e34cbccd15eee2da90562c1ee2sf list[l] = talloc_strndup(list, t, len);
cae0359c9286c8e34cbccd15eee2da90562c1ee2sf }
cae0359c9286c8e34cbccd15eee2da90562c1ee2sf if (!list[l]) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor talloc_free(list);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor return ENOMEM;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor l++;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor t = n; /* move to next string */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor /* Handle the last remaining token */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (t) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor r = talloc_realloc(mem_ctx, list, char *, l + 2);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (!r) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor talloc_free(list);
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh return ENOMEM;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor } else {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor list = r;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (trim) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor /* trim leading whitespace */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor len = strlen(t);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor while (isspace(*t)) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor t++;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor len--;
fed47023e9be04c612b5f6d4a5ee2b8e7c587181rbowen if (len == 0) break;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor /* trim trailing whitespace */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor p = t + len - 1;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor while (len > 0 && (isspace(*p))) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor len--;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor p--;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (len == 0) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor list[l] = talloc_strdup(list, "");
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor } else {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor list[l] = talloc_strndup(list, t, len);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor } else {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor list[l] = talloc_strdup(list, t);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (!list[l]) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor talloc_free(list);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor return ENOMEM;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor l++;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh list[l] = NULL; /* terminate list */
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh if (size) *size = l + 1;
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh *_list = list;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor return EOK;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor}
f039cf01b271a31e317d5b84f24cb135f1c1b6d7nd
f039cf01b271a31e317d5b84f24cb135f1c1b6d7ndstatic void free_args(char **args)
48c64aeceef385e19025b384bd719b2a9789592dnd{
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor int i;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (args) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor for (i = 0; args[i]; i++) free(args[i]);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor free(args);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor}
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor/* parse a string into arguments.
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * arguments are separated by a space
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * '\' is an escape character and can be used only to escape
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * itself or the white space.
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh */
ba543b319188dc1887607f6d59feddc00e38eee2humbedoohchar **parse_args(const char *str)
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh{
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh const char *p;
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh char **ret, **r;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor char *tmp;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor int num;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor int i, e;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor tmp = malloc(strlen(str) + 1);
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh if (!tmp) return NULL;
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh ret = NULL;
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh num = 0;
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh e = 0;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor i = 0;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor p = str;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor while (*p) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor switch (*p) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor case '\\':
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (e) {
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh tmp[i] = '\\';
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor i++;
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh e = 0;
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh } else {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor e = 1;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor break;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor case ' ':
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (e) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor tmp[i] = ' ';
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor i++;
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh e = 0;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor } else {
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh tmp[i] = '\0';
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh i++;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor break;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor default:
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (e) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor tmp[i] = '\\';
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor i++;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor e = 0;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor tmp[i] = *p;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor i++;
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh break;
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh }
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh p++;
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor /* check if this was the last char */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (*p == '\0') {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (e) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor tmp[i] = '\\';
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor i++;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor e = 0;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor tmp[i] = '\0';
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor i++;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (tmp[i-1] != '\0' || strlen(tmp) == 0) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor /* check next char and skip multiple spaces */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor continue;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor r = realloc(ret, (num + 2) * sizeof(char *));
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (!r) goto fail;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor ret = r;
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh ret[num+1] = NULL;
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh ret[num] = strdup(tmp);
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh if (!ret[num]) goto fail;
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh num++;
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh i = 0;
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh }
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh free(tmp);
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh return ret;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzorfail:
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor free(tmp);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor free_args(ret);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor return NULL;
cae0359c9286c8e34cbccd15eee2da90562c1ee2sf}
cae0359c9286c8e34cbccd15eee2da90562c1ee2sf
cae0359c9286c8e34cbccd15eee2da90562c1ee2sfchar **dup_string_list(TALLOC_CTX *memctx, const char **str_list)
cae0359c9286c8e34cbccd15eee2da90562c1ee2sf{
cae0359c9286c8e34cbccd15eee2da90562c1ee2sf int i = 0;
cae0359c9286c8e34cbccd15eee2da90562c1ee2sf int j = 0;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor char **dup_list;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (!str_list) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor return NULL;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor /* Find the size of the list */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor while (str_list[i]) i++;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor dup_list = talloc_array(memctx, char *, i+1);
fed47023e9be04c612b5f6d4a5ee2b8e7c587181rbowen if (!dup_list) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor return NULL;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor /* Copy the elements */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor for (j = 0; j < i; j++) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor dup_list[j] = talloc_strdup(dup_list, str_list[j]);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (!dup_list[j]) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor talloc_free(dup_list);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor return NULL;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor /* NULL-terminate the list */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor dup_list[i] = NULL;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor return dup_list;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor}
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor/* Take two string lists (terminated on a NULL char*)
fed47023e9be04c612b5f6d4a5ee2b8e7c587181rbowen * and return up to three arrays of strings based on
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * shared ownership.
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor *
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * Pass NULL to any return type you don't care about
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzorerrno_t diff_string_lists(TALLOC_CTX *memctx,
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor char **_list1,
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor char **_list2,
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor char ***_list1_only,
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor char ***_list2_only,
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor char ***_both_lists)
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor{
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor int error;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor errno_t ret;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor int i;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor int i2 = 0;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor int i12 = 0;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor hash_table_t *table;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor hash_key_t key;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor hash_value_t value;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor char **list1 = NULL;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor char **list2 = NULL;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor char **list1_only = NULL;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor char **list2_only = NULL;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor char **both_lists = NULL;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor unsigned long count;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor hash_key_t *keys;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor TALLOC_CTX *tmp_ctx = talloc_new(memctx);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (!tmp_ctx) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor return ENOMEM;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (!_list1) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor list1 = talloc_array(tmp_ctx, char *, 1);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (!list1) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor talloc_free(tmp_ctx);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor return ENOMEM;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor list1[0] = NULL;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor else {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor list1 = _list1;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (!_list2) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor list2 = talloc_array(tmp_ctx, char *, 1);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (!list2) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor talloc_free(tmp_ctx);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor return ENOMEM;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor list2[0] = NULL;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor else {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor list2 = _list2;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor error = hash_create(10, &table, NULL, NULL);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (error != HASH_SUCCESS) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor talloc_free(tmp_ctx);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor return EIO;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor key.type = HASH_KEY_STRING;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor value.type = HASH_VALUE_UNDEF;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor /* Add all entries from list 1 into a hash table */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor i = 0;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor while (list1[i]) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor key.str = talloc_strdup(tmp_ctx, list1[i]);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor error = hash_enter(table, &key, &value);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (error != HASH_SUCCESS) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor ret = EIO;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor goto done;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor i++;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor /* Iterate through list 2 and remove matching items */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor i = 0;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor while (list2[i]) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor key.str = talloc_strdup(tmp_ctx, list2[i]);
f039cf01b271a31e317d5b84f24cb135f1c1b6d7nd error = hash_delete(table, &key);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (error == HASH_SUCCESS) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (_both_lists) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor /* String was present in both lists */
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh i12++;
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh both_lists = talloc_realloc(tmp_ctx, both_lists, char *, i12+1);
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh if (!both_lists) {
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh ret = ENOMEM;
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh goto done;
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor both_lists[i12-1] = talloc_strdup(both_lists, list2[i]);
f039cf01b271a31e317d5b84f24cb135f1c1b6d7nd if (!both_lists[i12-1]) {
f039cf01b271a31e317d5b84f24cb135f1c1b6d7nd ret = ENOMEM;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor goto done;
f039cf01b271a31e317d5b84f24cb135f1c1b6d7nd }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor both_lists[i12] = NULL;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh }
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh else if (error == HASH_ERROR_KEY_NOT_FOUND) {
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh if (_list2_only) {
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh /* String was present only in list2 */
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh i2++;
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh list2_only = talloc_realloc(tmp_ctx, list2_only,
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor char *, i2+1);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (!list2_only) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor ret = ENOMEM;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor goto done;
f039cf01b271a31e317d5b84f24cb135f1c1b6d7nd }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor list2_only[i2-1] = talloc_strdup(list2_only, list2[i]);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (!list2_only[i2-1]) {
f039cf01b271a31e317d5b84f24cb135f1c1b6d7nd ret = ENOMEM;
f039cf01b271a31e317d5b84f24cb135f1c1b6d7nd goto done;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor list2_only[i2] = NULL;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor else {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor /* An error occurred */
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor ret = EIO;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor goto done;
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh }
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh i++;
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh }
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh /* Get the leftover entries in the hash table */
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh if (_list1_only) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor error = hash_keys(table, &count, &keys);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (error != HASH_SUCCESS) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor ret = EIO;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor goto done;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor list1_only = talloc_array(tmp_ctx, char *, count+1);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (!list1_only) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor ret = ENOMEM;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor goto done;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor for (i = 0; i < count; i++) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor list1_only[i] = talloc_strdup(list1_only, keys[i].str);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (!list1_only[i]) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor ret = ENOMEM;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor goto done;
cae0359c9286c8e34cbccd15eee2da90562c1ee2sf }
cae0359c9286c8e34cbccd15eee2da90562c1ee2sf }
cae0359c9286c8e34cbccd15eee2da90562c1ee2sf list1_only[count] = NULL;
cae0359c9286c8e34cbccd15eee2da90562c1ee2sf
cae0359c9286c8e34cbccd15eee2da90562c1ee2sf free(keys);
cae0359c9286c8e34cbccd15eee2da90562c1ee2sf
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor *_list1_only = talloc_steal(memctx, list1_only);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (_list2_only) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (list2_only) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor *_list2_only = talloc_steal(memctx, list2_only);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor else {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor *_list2_only = talloc_array(memctx, char *, 1);
fed47023e9be04c612b5f6d4a5ee2b8e7c587181rbowen if (!(*_list2_only)) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor ret = ENOMEM;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor goto done;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor *_list2_only[0] = NULL;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (_both_lists) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (both_lists) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor *_both_lists = talloc_steal(memctx, both_lists);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor else {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor *_both_lists = talloc_array(memctx, char *, 1);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor if (!(*_both_lists)) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor ret = ENOMEM;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor goto done;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor *_both_lists[0] = NULL;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor ret = EOK;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzordone:
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor hash_destroy(table);
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh talloc_free(tmp_ctx);
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh return ret;
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh}
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh
ba543b319188dc1887607f6d59feddc00e38eee2humbedoohstatic void *hash_talloc(const size_t size, void *pvt)
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh{
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor return talloc_size(pvt, size);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor}
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzorstatic void hash_talloc_free(void *ptr, void *pvt)
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor{
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor talloc_free(ptr);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor}
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
ba543b319188dc1887607f6d59feddc00e38eee2humbedooherrno_t sss_hash_create(TALLOC_CTX *mem_ctx,
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh unsigned long count,
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh hash_table_t **tbl)
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh{
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh errno_t ret;
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh hash_table_t *table;
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh int hret;
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh TALLOC_CTX *internal_ctx;
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh internal_ctx = talloc_new(NULL);
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh if (!internal_ctx) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor return ENOMEM;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor hret = hash_create_ex(count, &table, 0, 0, 0, 0,
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor hash_talloc, hash_talloc_free,
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor internal_ctx, NULL, NULL);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor switch (hret) {
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor case HASH_SUCCESS:
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor /* Steal the table pointer onto the mem_ctx,
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * then make the internal_ctx a child of
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor * table.
cae0359c9286c8e34cbccd15eee2da90562c1ee2sf *
cae0359c9286c8e34cbccd15eee2da90562c1ee2sf * This way, we can clean up the values when
cae0359c9286c8e34cbccd15eee2da90562c1ee2sf * we talloc_free() the table
cae0359c9286c8e34cbccd15eee2da90562c1ee2sf */
cae0359c9286c8e34cbccd15eee2da90562c1ee2sf *tbl = talloc_steal(mem_ctx, table);
cae0359c9286c8e34cbccd15eee2da90562c1ee2sf talloc_steal(table, internal_ctx);
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor return EOK;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor case HASH_ERROR_NO_MEMORY:
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor ret = ENOMEM;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor default:
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor ret = EIO;
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor }
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor DEBUG(0, ("Could not create hash table: [%d][%s]\n",
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor hret, hash_error_string(hret)));
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh talloc_free(internal_ctx);
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh return ret;
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh}
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooherrno_t sss_filter_sanitize(TALLOC_CTX *mem_ctx,
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh const char *input,
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh char **sanitized)
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh{
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh char *output;
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh size_t i = 0;
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh size_t j = 0;
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh /* Assume the worst-case. We'll resize it later, once */
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh output = talloc_array(mem_ctx, char, strlen(input) * 3 + 1);
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh if (!output) {
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh return ENOMEM;
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh }
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh
5effc8b39fae5cd169d17f342bfc265705840014rbowen while (input[i]) {
d229f940abfb2490dee17979e9a5ff31b7012eb5rbowen switch(input[i]) {
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd case '*':
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd output[j++] = '\\';
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd output[j++] = '2';
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd output[j++] = 'a';
cc8190433d13f5e9de618c5d7f10c824c0c1919cgryzor break;
case '(':
output[j++] = '\\';
output[j++] = '2';
output[j++] = '8';
break;
case ')':
output[j++] = '\\';
output[j++] = '2';
output[j++] = '9';
break;
case '\\':
output[j++] = '\\';
output[j++] = '5';
output[j++] = 'c';
break;
default:
output[j++] = input[i];
}
i++;
}
output[j] = '\0';
*sanitized = talloc_realloc(mem_ctx, output, char, j+1);
if (!*sanitized) {
talloc_free(output);
return ENOMEM;
}
return EOK;
}