/src/sudo/plugins/sudoers/strlcpy_unesc.c
Line | Count | Source |
1 | | /* |
2 | | * SPDX-License-Identifier: ISC |
3 | | * |
4 | | * Copyright (c) 2021 Todd C. Miller <Todd.Miller@sudo.ws> |
5 | | * |
6 | | * Permission to use, copy, modify, and distribute this software for any |
7 | | * purpose with or without fee is hereby granted, provided that the above |
8 | | * copyright notice and this permission notice appear in all copies. |
9 | | * |
10 | | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
11 | | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
12 | | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
13 | | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
14 | | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
15 | | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
16 | | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
17 | | */ |
18 | | |
19 | | #include <config.h> |
20 | | |
21 | | #include <sys/types.h> |
22 | | #include <stdio.h> |
23 | | #include <ctype.h> |
24 | | |
25 | | #include <sudoers.h> |
26 | | |
27 | | /* |
28 | | * Like strlcpy() but collapses non-space chars escaped with a backslash. |
29 | | */ |
30 | | size_t |
31 | | strlcpy_unescape(char *restrict dst, const char *restrict src, size_t size) |
32 | 3.25M | { |
33 | 3.25M | size_t len = 0; |
34 | 3.25M | char ch; |
35 | 3.25M | debug_decl(strlcpy_unescape, SUDOERS_DEBUG_UTIL); |
36 | | |
37 | 34.8M | while ((ch = *src++) != '\0') { |
38 | 31.6M | if (ch == '\\' && *src != '\0' && !isspace((unsigned char)*src)) |
39 | 4.25k | ch = *src++; |
40 | 31.6M | if (size > 1) { |
41 | 31.6M | *dst++ = ch; |
42 | 31.6M | size--; |
43 | 31.6M | } |
44 | 31.6M | len++; |
45 | 31.6M | } |
46 | 3.25M | if (size > 0) |
47 | 3.25M | *dst = '\0'; |
48 | | |
49 | 3.25M | debug_return_size_t(len); |
50 | 3.25M | } |