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 #define UNESCAPE_ALL_MASK	GENMASK(3, 0)
30 
31 int string_unescape(char *src, char *dst, size_t size, unsigned int flags);
32 
string_unescape_inplace(char * buf,unsigned int flags)33 static inline int string_unescape_inplace(char *buf, unsigned int flags)
34 {
35 	return string_unescape(buf, buf, 0, flags);
36 }
37 
string_unescape_any(char * src,char * dst,size_t size)38 static inline int string_unescape_any(char *src, char *dst, size_t size)
39 {
40 	return string_unescape(src, dst, size, UNESCAPE_ANY);
41 }
42 
string_unescape_any_inplace(char * buf)43 static inline int string_unescape_any_inplace(char *buf)
44 {
45 	return string_unescape_any(buf, buf, 0);
46 }
47 
48 #define ESCAPE_SPACE		BIT(0)
49 #define ESCAPE_SPECIAL		BIT(1)
50 #define ESCAPE_NULL		BIT(2)
51 #define ESCAPE_OCTAL		BIT(3)
52 #define ESCAPE_ANY		\
53 	(ESCAPE_SPACE | ESCAPE_OCTAL | ESCAPE_SPECIAL | ESCAPE_NULL)
54 #define ESCAPE_NP		BIT(4)
55 #define ESCAPE_ANY_NP		(ESCAPE_ANY | ESCAPE_NP)
56 #define ESCAPE_HEX		BIT(5)
57 #define ESCAPE_NA		BIT(6)
58 #define ESCAPE_NAP		BIT(7)
59 #define ESCAPE_APPEND		BIT(8)
60 
61 #define ESCAPE_ALL_MASK		GENMASK(8, 0)
62 
63 int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz,
64 		unsigned int flags, const char *only);
65 
string_escape_mem_any_np(const char * src,size_t isz,char * dst,size_t osz,const char * only)66 static inline int string_escape_mem_any_np(const char *src, size_t isz,
67 		char *dst, size_t osz, const char *only)
68 {
69 	return string_escape_mem(src, isz, dst, osz, ESCAPE_ANY_NP, only);
70 }
71 
string_escape_str(const char * src,char * dst,size_t sz,unsigned int flags,const char * only)72 static inline int string_escape_str(const char *src, char *dst, size_t sz,
73 		unsigned int flags, const char *only)
74 {
75 	return string_escape_mem(src, strlen(src), dst, sz, flags, only);
76 }
77 
string_escape_str_any_np(const char * src,char * dst,size_t sz,const char * only)78 static inline int string_escape_str_any_np(const char *src, char *dst,
79 		size_t sz, const char *only)
80 {
81 	return string_escape_str(src, dst, sz, ESCAPE_ANY_NP, only);
82 }
83 
string_upper(char * dst,const char * src)84 static inline void string_upper(char *dst, const char *src)
85 {
86 	do {
87 		*dst++ = toupper(*src);
88 	} while (*src++);
89 }
90 
string_lower(char * dst,const char * src)91 static inline void string_lower(char *dst, const char *src)
92 {
93 	do {
94 		*dst++ = tolower(*src);
95 	} while (*src++);
96 }
97 
98 char *kstrdup_quotable(const char *src, gfp_t gfp);
99 char *kstrdup_quotable_cmdline(struct task_struct *task, gfp_t gfp);
100 char *kstrdup_quotable_file(struct file *file, gfp_t gfp);
101 
102 void kfree_strarray(char **array, size_t n);
103 
104 #endif
105