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 device; 11 struct file; 12 struct task_struct; 13 14 /* Descriptions of the types of units to 15 * print in */ 16 enum string_size_units { 17 STRING_UNITS_10, /* use powers of 10^3 (standard SI) */ 18 STRING_UNITS_2, /* use binary powers of 2^10 */ 19 }; 20 21 void string_get_size(u64 size, u64 blk_size, enum string_size_units units, 22 char *buf, int len); 23 24 int parse_int_array_user(const char __user *from, size_t count, int **array); 25 26 #define UNESCAPE_SPACE BIT(0) 27 #define UNESCAPE_OCTAL BIT(1) 28 #define UNESCAPE_HEX BIT(2) 29 #define UNESCAPE_SPECIAL BIT(3) 30 #define UNESCAPE_ANY \ 31 (UNESCAPE_SPACE | UNESCAPE_OCTAL | UNESCAPE_HEX | UNESCAPE_SPECIAL) 32 33 #define UNESCAPE_ALL_MASK GENMASK(3, 0) 34 35 int string_unescape(char *src, char *dst, size_t size, unsigned int flags); 36 37 static inline int string_unescape_inplace(char *buf, unsigned int flags) 38 { 39 return string_unescape(buf, buf, 0, flags); 40 } 41 42 static inline int string_unescape_any(char *src, char *dst, size_t size) 43 { 44 return string_unescape(src, dst, size, UNESCAPE_ANY); 45 } 46 47 static inline int string_unescape_any_inplace(char *buf) 48 { 49 return string_unescape_any(buf, buf, 0); 50 } 51 52 #define ESCAPE_SPACE BIT(0) 53 #define ESCAPE_SPECIAL BIT(1) 54 #define ESCAPE_NULL BIT(2) 55 #define ESCAPE_OCTAL BIT(3) 56 #define ESCAPE_ANY \ 57 (ESCAPE_SPACE | ESCAPE_OCTAL | ESCAPE_SPECIAL | ESCAPE_NULL) 58 #define ESCAPE_NP BIT(4) 59 #define ESCAPE_ANY_NP (ESCAPE_ANY | ESCAPE_NP) 60 #define ESCAPE_HEX BIT(5) 61 #define ESCAPE_NA BIT(6) 62 #define ESCAPE_NAP BIT(7) 63 #define ESCAPE_APPEND BIT(8) 64 65 #define ESCAPE_ALL_MASK GENMASK(8, 0) 66 67 int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz, 68 unsigned int flags, const char *only); 69 70 static inline int string_escape_mem_any_np(const char *src, size_t isz, 71 char *dst, size_t osz, const char *only) 72 { 73 return string_escape_mem(src, isz, dst, osz, ESCAPE_ANY_NP, only); 74 } 75 76 static inline int string_escape_str(const char *src, char *dst, size_t sz, 77 unsigned int flags, const char *only) 78 { 79 return string_escape_mem(src, strlen(src), dst, sz, flags, only); 80 } 81 82 static inline int string_escape_str_any_np(const char *src, char *dst, 83 size_t sz, const char *only) 84 { 85 return string_escape_str(src, dst, sz, ESCAPE_ANY_NP, only); 86 } 87 88 static inline void string_upper(char *dst, const char *src) 89 { 90 do { 91 *dst++ = toupper(*src); 92 } while (*src++); 93 } 94 95 static inline void string_lower(char *dst, const char *src) 96 { 97 do { 98 *dst++ = tolower(*src); 99 } while (*src++); 100 } 101 102 char *kstrdup_quotable(const char *src, gfp_t gfp); 103 char *kstrdup_quotable_cmdline(struct task_struct *task, gfp_t gfp); 104 char *kstrdup_quotable_file(struct file *file, gfp_t gfp); 105 106 char **kasprintf_strarray(gfp_t gfp, const char *prefix, size_t n); 107 void kfree_strarray(char **array, size_t n); 108 109 char **devm_kasprintf_strarray(struct device *dev, const char *prefix, size_t n); 110 111 static inline const char *str_yes_no(bool v) 112 { 113 return v ? "yes" : "no"; 114 } 115 116 static inline const char *str_on_off(bool v) 117 { 118 return v ? "on" : "off"; 119 } 120 121 static inline const char *str_enable_disable(bool v) 122 { 123 return v ? "enable" : "disable"; 124 } 125 126 static inline const char *str_enabled_disabled(bool v) 127 { 128 return v ? "enabled" : "disabled"; 129 } 130 131 static inline const char *str_read_write(bool v) 132 { 133 return v ? "read" : "write"; 134 } 135 136 #endif 137