Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
3c9f3681 JB |
2 | #ifndef _LINUX_STRING_HELPERS_H_ |
3 | #define _LINUX_STRING_HELPERS_H_ | |
4 | ||
994b6970 | 5 | #include <linux/bits.h> |
58eeba0b | 6 | #include <linux/ctype.h> |
bfb3ba32 | 7 | #include <linux/string.h> |
3c9f3681 JB |
8 | #include <linux/types.h> |
9 | ||
acdb89b6 | 10 | struct device; |
21985319 | 11 | struct file; |
c6d24c32 | 12 | struct task_struct; |
21985319 | 13 | |
f1db99c0 AS |
14 | static inline bool string_is_terminated(const char *s, int len) |
15 | { | |
16 | return memchr(s, '\0', len) ? true : false; | |
17 | } | |
18 | ||
3c9f3681 JB |
19 | /* Descriptions of the types of units to |
20 | * print in */ | |
21 | enum string_size_units { | |
22 | STRING_UNITS_10, /* use powers of 10^3 (standard SI) */ | |
23 | STRING_UNITS_2, /* use binary powers of 2^10 */ | |
24 | }; | |
25 | ||
b9f28d86 | 26 | void string_get_size(u64 size, u64 blk_size, enum string_size_units units, |
d1214c65 | 27 | char *buf, int len); |
3c9f3681 | 28 | |
f0b93323 CR |
29 | int parse_int_array_user(const char __user *from, size_t count, int **array); |
30 | ||
994b6970 AS |
31 | #define UNESCAPE_SPACE BIT(0) |
32 | #define UNESCAPE_OCTAL BIT(1) | |
33 | #define UNESCAPE_HEX BIT(2) | |
34 | #define UNESCAPE_SPECIAL BIT(3) | |
16c7fa05 AS |
35 | #define UNESCAPE_ANY \ |
36 | (UNESCAPE_SPACE | UNESCAPE_OCTAL | UNESCAPE_HEX | UNESCAPE_SPECIAL) | |
37 | ||
259fa5d7 AS |
38 | #define UNESCAPE_ALL_MASK GENMASK(3, 0) |
39 | ||
16c7fa05 AS |
40 | int string_unescape(char *src, char *dst, size_t size, unsigned int flags); |
41 | ||
42 | static inline int string_unescape_inplace(char *buf, unsigned int flags) | |
43 | { | |
44 | return string_unescape(buf, buf, 0, flags); | |
45 | } | |
46 | ||
47 | static inline int string_unescape_any(char *src, char *dst, size_t size) | |
48 | { | |
49 | return string_unescape(src, dst, size, UNESCAPE_ANY); | |
50 | } | |
51 | ||
52 | static inline int string_unescape_any_inplace(char *buf) | |
53 | { | |
54 | return string_unescape_any(buf, buf, 0); | |
55 | } | |
56 | ||
994b6970 AS |
57 | #define ESCAPE_SPACE BIT(0) |
58 | #define ESCAPE_SPECIAL BIT(1) | |
59 | #define ESCAPE_NULL BIT(2) | |
60 | #define ESCAPE_OCTAL BIT(3) | |
c8250381 AS |
61 | #define ESCAPE_ANY \ |
62 | (ESCAPE_SPACE | ESCAPE_OCTAL | ESCAPE_SPECIAL | ESCAPE_NULL) | |
994b6970 | 63 | #define ESCAPE_NP BIT(4) |
c8250381 | 64 | #define ESCAPE_ANY_NP (ESCAPE_ANY | ESCAPE_NP) |
994b6970 | 65 | #define ESCAPE_HEX BIT(5) |
a0809783 | 66 | #define ESCAPE_NA BIT(6) |
0362c27f | 67 | #define ESCAPE_NAP BIT(7) |
aec0d096 | 68 | #define ESCAPE_APPEND BIT(8) |
c8250381 | 69 | |
259fa5d7 AS |
70 | #define ESCAPE_ALL_MASK GENMASK(8, 0) |
71 | ||
41416f23 | 72 | int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz, |
b40bdb7f | 73 | unsigned int flags, const char *only); |
c8250381 AS |
74 | |
75 | static inline int string_escape_mem_any_np(const char *src, size_t isz, | |
b40bdb7f | 76 | char *dst, size_t osz, const char *only) |
c8250381 | 77 | { |
b40bdb7f | 78 | return string_escape_mem(src, isz, dst, osz, ESCAPE_ANY_NP, only); |
c8250381 AS |
79 | } |
80 | ||
41416f23 | 81 | static inline int string_escape_str(const char *src, char *dst, size_t sz, |
b40bdb7f | 82 | unsigned int flags, const char *only) |
c8250381 | 83 | { |
b40bdb7f | 84 | return string_escape_mem(src, strlen(src), dst, sz, flags, only); |
c8250381 AS |
85 | } |
86 | ||
41416f23 | 87 | static inline int string_escape_str_any_np(const char *src, char *dst, |
b40bdb7f | 88 | size_t sz, const char *only) |
c8250381 | 89 | { |
b40bdb7f | 90 | return string_escape_str(src, dst, sz, ESCAPE_ANY_NP, only); |
c8250381 AS |
91 | } |
92 | ||
58eeba0b VP |
93 | static inline void string_upper(char *dst, const char *src) |
94 | { | |
95 | do { | |
96 | *dst++ = toupper(*src); | |
97 | } while (*src++); | |
98 | } | |
99 | ||
100 | static inline void string_lower(char *dst, const char *src) | |
101 | { | |
102 | do { | |
103 | *dst++ = tolower(*src); | |
104 | } while (*src++); | |
105 | } | |
106 | ||
b53f27e4 | 107 | char *kstrdup_quotable(const char *src, gfp_t gfp); |
0d044328 | 108 | char *kstrdup_quotable_cmdline(struct task_struct *task, gfp_t gfp); |
21985319 | 109 | char *kstrdup_quotable_file(struct file *file, gfp_t gfp); |
b53f27e4 | 110 | |
418e0a35 | 111 | char **kasprintf_strarray(gfp_t gfp, const char *prefix, size_t n); |
0fd16012 BG |
112 | void kfree_strarray(char **array, size_t n); |
113 | ||
acdb89b6 AS |
114 | char **devm_kasprintf_strarray(struct device *dev, const char *prefix, size_t n); |
115 | ||
ea4692c7 LDM |
116 | static inline const char *str_yes_no(bool v) |
117 | { | |
118 | return v ? "yes" : "no"; | |
119 | } | |
120 | ||
121 | static inline const char *str_on_off(bool v) | |
122 | { | |
123 | return v ? "on" : "off"; | |
124 | } | |
125 | ||
126 | static inline const char *str_enable_disable(bool v) | |
127 | { | |
128 | return v ? "enable" : "disable"; | |
129 | } | |
130 | ||
131 | static inline const char *str_enabled_disabled(bool v) | |
132 | { | |
133 | return v ? "enabled" : "disabled"; | |
134 | } | |
135 | ||
1f5d7ea7 AS |
136 | static inline const char *str_read_write(bool v) |
137 | { | |
138 | return v ? "read" : "write"; | |
139 | } | |
140 | ||
3c9f3681 | 141 | #endif |