xref: /linux-6.15/include/linux/string_helpers.h (revision a0809783)
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/types.h>
8 
9 struct file;
10 struct task_struct;
11 
12 /* Descriptions of the types of units to
13  * print in */
14 enum string_size_units {
15 	STRING_UNITS_10,	/* use powers of 10^3 (standard SI) */
16 	STRING_UNITS_2,		/* use binary powers of 2^10 */
17 };
18 
19 void string_get_size(u64 size, u64 blk_size, enum string_size_units units,
20 		     char *buf, int len);
21 
22 #define UNESCAPE_SPACE		BIT(0)
23 #define UNESCAPE_OCTAL		BIT(1)
24 #define UNESCAPE_HEX		BIT(2)
25 #define UNESCAPE_SPECIAL	BIT(3)
26 #define UNESCAPE_ANY		\
27 	(UNESCAPE_SPACE | UNESCAPE_OCTAL | UNESCAPE_HEX | UNESCAPE_SPECIAL)
28 
29 int string_unescape(char *src, char *dst, size_t size, unsigned int flags);
30 
31 static inline int string_unescape_inplace(char *buf, unsigned int flags)
32 {
33 	return string_unescape(buf, buf, 0, flags);
34 }
35 
36 static inline int string_unescape_any(char *src, char *dst, size_t size)
37 {
38 	return string_unescape(src, dst, size, UNESCAPE_ANY);
39 }
40 
41 static inline int string_unescape_any_inplace(char *buf)
42 {
43 	return string_unescape_any(buf, buf, 0);
44 }
45 
46 #define ESCAPE_SPACE		BIT(0)
47 #define ESCAPE_SPECIAL		BIT(1)
48 #define ESCAPE_NULL		BIT(2)
49 #define ESCAPE_OCTAL		BIT(3)
50 #define ESCAPE_ANY		\
51 	(ESCAPE_SPACE | ESCAPE_OCTAL | ESCAPE_SPECIAL | ESCAPE_NULL)
52 #define ESCAPE_NP		BIT(4)
53 #define ESCAPE_ANY_NP		(ESCAPE_ANY | ESCAPE_NP)
54 #define ESCAPE_HEX		BIT(5)
55 #define ESCAPE_NA		BIT(6)
56 
57 int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz,
58 		unsigned int flags, const char *only);
59 
60 int string_escape_mem_ascii(const char *src, size_t isz, char *dst,
61 					size_t osz);
62 
63 static inline int string_escape_mem_any_np(const char *src, size_t isz,
64 		char *dst, size_t osz, const char *only)
65 {
66 	return string_escape_mem(src, isz, dst, osz, ESCAPE_ANY_NP, only);
67 }
68 
69 static inline int string_escape_str(const char *src, char *dst, size_t sz,
70 		unsigned int flags, const char *only)
71 {
72 	return string_escape_mem(src, strlen(src), dst, sz, flags, only);
73 }
74 
75 static inline int string_escape_str_any_np(const char *src, char *dst,
76 		size_t sz, const char *only)
77 {
78 	return string_escape_str(src, dst, sz, ESCAPE_ANY_NP, only);
79 }
80 
81 static inline void string_upper(char *dst, const char *src)
82 {
83 	do {
84 		*dst++ = toupper(*src);
85 	} while (*src++);
86 }
87 
88 static inline void string_lower(char *dst, const char *src)
89 {
90 	do {
91 		*dst++ = tolower(*src);
92 	} while (*src++);
93 }
94 
95 char *kstrdup_quotable(const char *src, gfp_t gfp);
96 char *kstrdup_quotable_cmdline(struct task_struct *task, gfp_t gfp);
97 char *kstrdup_quotable_file(struct file *file, gfp_t gfp);
98 
99 void kfree_strarray(char **array, size_t n);
100 
101 #endif
102