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