Linux: Implement interfaces for memory protection keys
[glibc.git] / nss / rewrite_field.c
blob3c456fe22d7424203a14384b0faab538a89211d7
1 /* Copyright (C) 2015-2017 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
18 #include <nss.h>
19 #include <string.h>
21 /* Rewrite VALUE to a valid field value in the NSS database. Invalid
22 characters are replaced with a single space character ' '. If
23 VALUE is NULL, the empty string is returned. *TO_BE_FREED is
24 overwritten with a pointer the caller has to free if the function
25 returns successfully. On failure, return NULL. */
26 const char *
27 __nss_rewrite_field (const char *value, char **to_be_freed)
29 *to_be_freed = NULL;
30 if (value == NULL)
31 return "";
33 /* Search for non-allowed characters. */
34 const char *p = strpbrk (value, __nss_invalid_field_characters);
35 if (p == NULL)
36 return value;
37 *to_be_freed = __strdup (value);
38 if (*to_be_freed == NULL)
39 return NULL;
41 /* Switch pointer to freshly-allocated buffer. */
42 char *bad = *to_be_freed + (p - value);
45 *bad = ' ';
46 bad = strpbrk (bad + 1, __nss_invalid_field_characters);
48 while (bad != NULL);
50 return *to_be_freed;