Merge tag 'drm-intel-next-2024-02-07' of git://anongit.freedesktop.org/drm/drm-intel...
[linux-2.6-microblaze.git] / include / linux / string_helpers.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_STRING_HELPERS_H_
3 #define _LINUX_STRING_HELPERS_H_
4
5 #include <linux/bits.h>
6 #include <linux/ctype.h>
7 #include <linux/string_choices.h>
8 #include <linux/string.h>
9 #include <linux/types.h>
10
11 struct device;
12 struct file;
13 struct task_struct;
14
15 static inline bool string_is_terminated(const char *s, int len)
16 {
17         return memchr(s, '\0', len) ? true : false;
18 }
19
20 /* Descriptions of the types of units to
21  * print in */
22 enum string_size_units {
23         STRING_UNITS_10,        /* use powers of 10^3 (standard SI) */
24         STRING_UNITS_2,         /* use binary powers of 2^10 */
25 };
26
27 int string_get_size(u64 size, u64 blk_size, enum string_size_units units,
28                     char *buf, int len);
29
30 int parse_int_array_user(const char __user *from, size_t count, int **array);
31
32 #define UNESCAPE_SPACE          BIT(0)
33 #define UNESCAPE_OCTAL          BIT(1)
34 #define UNESCAPE_HEX            BIT(2)
35 #define UNESCAPE_SPECIAL        BIT(3)
36 #define UNESCAPE_ANY            \
37         (UNESCAPE_SPACE | UNESCAPE_OCTAL | UNESCAPE_HEX | UNESCAPE_SPECIAL)
38
39 #define UNESCAPE_ALL_MASK       GENMASK(3, 0)
40
41 int string_unescape(char *src, char *dst, size_t size, unsigned int flags);
42
43 static inline int string_unescape_inplace(char *buf, unsigned int flags)
44 {
45         return string_unescape(buf, buf, 0, flags);
46 }
47
48 static inline int string_unescape_any(char *src, char *dst, size_t size)
49 {
50         return string_unescape(src, dst, size, UNESCAPE_ANY);
51 }
52
53 static inline int string_unescape_any_inplace(char *buf)
54 {
55         return string_unescape_any(buf, buf, 0);
56 }
57
58 #define ESCAPE_SPACE            BIT(0)
59 #define ESCAPE_SPECIAL          BIT(1)
60 #define ESCAPE_NULL             BIT(2)
61 #define ESCAPE_OCTAL            BIT(3)
62 #define ESCAPE_ANY              \
63         (ESCAPE_SPACE | ESCAPE_OCTAL | ESCAPE_SPECIAL | ESCAPE_NULL)
64 #define ESCAPE_NP               BIT(4)
65 #define ESCAPE_ANY_NP           (ESCAPE_ANY | ESCAPE_NP)
66 #define ESCAPE_HEX              BIT(5)
67 #define ESCAPE_NA               BIT(6)
68 #define ESCAPE_NAP              BIT(7)
69 #define ESCAPE_APPEND           BIT(8)
70
71 #define ESCAPE_ALL_MASK         GENMASK(8, 0)
72
73 int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz,
74                 unsigned int flags, const char *only);
75
76 static inline int string_escape_mem_any_np(const char *src, size_t isz,
77                 char *dst, size_t osz, const char *only)
78 {
79         return string_escape_mem(src, isz, dst, osz, ESCAPE_ANY_NP, only);
80 }
81
82 static inline int string_escape_str(const char *src, char *dst, size_t sz,
83                 unsigned int flags, const char *only)
84 {
85         return string_escape_mem(src, strlen(src), dst, sz, flags, only);
86 }
87
88 static inline int string_escape_str_any_np(const char *src, char *dst,
89                 size_t sz, const char *only)
90 {
91         return string_escape_str(src, dst, sz, ESCAPE_ANY_NP, only);
92 }
93
94 static inline void string_upper(char *dst, const char *src)
95 {
96         do {
97                 *dst++ = toupper(*src);
98         } while (*src++);
99 }
100
101 static inline void string_lower(char *dst, const char *src)
102 {
103         do {
104                 *dst++ = tolower(*src);
105         } while (*src++);
106 }
107
108 char *kstrdup_quotable(const char *src, gfp_t gfp);
109 char *kstrdup_quotable_cmdline(struct task_struct *task, gfp_t gfp);
110 char *kstrdup_quotable_file(struct file *file, gfp_t gfp);
111
112 char *kstrdup_and_replace(const char *src, char old, char new, gfp_t gfp);
113
114 char **kasprintf_strarray(gfp_t gfp, const char *prefix, size_t n);
115 void kfree_strarray(char **array, size_t n);
116
117 char **devm_kasprintf_strarray(struct device *dev, const char *prefix, size_t n);
118
119 #endif