xref: /linux-6.15/include/linux/string_helpers.h (revision a0809783)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
23c9f3681SJames Bottomley #ifndef _LINUX_STRING_HELPERS_H_
33c9f3681SJames Bottomley #define _LINUX_STRING_HELPERS_H_
43c9f3681SJames Bottomley 
5994b6970SAndy Shevchenko #include <linux/bits.h>
658eeba0bSVadim Pasternak #include <linux/ctype.h>
73c9f3681SJames Bottomley #include <linux/types.h>
83c9f3681SJames Bottomley 
921985319SKees Cook struct file;
10c6d24c32SAndy Shevchenko struct task_struct;
1121985319SKees Cook 
123c9f3681SJames Bottomley /* Descriptions of the types of units to
133c9f3681SJames Bottomley  * print in */
143c9f3681SJames Bottomley enum string_size_units {
153c9f3681SJames Bottomley 	STRING_UNITS_10,	/* use powers of 10^3 (standard SI) */
163c9f3681SJames Bottomley 	STRING_UNITS_2,		/* use binary powers of 2^10 */
173c9f3681SJames Bottomley };
183c9f3681SJames Bottomley 
19b9f28d86SJames Bottomley void string_get_size(u64 size, u64 blk_size, enum string_size_units units,
203c9f3681SJames Bottomley 		     char *buf, int len);
213c9f3681SJames Bottomley 
22994b6970SAndy Shevchenko #define UNESCAPE_SPACE		BIT(0)
23994b6970SAndy Shevchenko #define UNESCAPE_OCTAL		BIT(1)
24994b6970SAndy Shevchenko #define UNESCAPE_HEX		BIT(2)
25994b6970SAndy Shevchenko #define UNESCAPE_SPECIAL	BIT(3)
2616c7fa05SAndy Shevchenko #define UNESCAPE_ANY		\
2716c7fa05SAndy Shevchenko 	(UNESCAPE_SPACE | UNESCAPE_OCTAL | UNESCAPE_HEX | UNESCAPE_SPECIAL)
2816c7fa05SAndy Shevchenko 
2916c7fa05SAndy Shevchenko int string_unescape(char *src, char *dst, size_t size, unsigned int flags);
3016c7fa05SAndy Shevchenko 
3116c7fa05SAndy Shevchenko static inline int string_unescape_inplace(char *buf, unsigned int flags)
3216c7fa05SAndy Shevchenko {
3316c7fa05SAndy Shevchenko 	return string_unescape(buf, buf, 0, flags);
3416c7fa05SAndy Shevchenko }
3516c7fa05SAndy Shevchenko 
3616c7fa05SAndy Shevchenko static inline int string_unescape_any(char *src, char *dst, size_t size)
3716c7fa05SAndy Shevchenko {
3816c7fa05SAndy Shevchenko 	return string_unescape(src, dst, size, UNESCAPE_ANY);
3916c7fa05SAndy Shevchenko }
4016c7fa05SAndy Shevchenko 
4116c7fa05SAndy Shevchenko static inline int string_unescape_any_inplace(char *buf)
4216c7fa05SAndy Shevchenko {
4316c7fa05SAndy Shevchenko 	return string_unescape_any(buf, buf, 0);
4416c7fa05SAndy Shevchenko }
4516c7fa05SAndy Shevchenko 
46994b6970SAndy Shevchenko #define ESCAPE_SPACE		BIT(0)
47994b6970SAndy Shevchenko #define ESCAPE_SPECIAL		BIT(1)
48994b6970SAndy Shevchenko #define ESCAPE_NULL		BIT(2)
49994b6970SAndy Shevchenko #define ESCAPE_OCTAL		BIT(3)
50c8250381SAndy Shevchenko #define ESCAPE_ANY		\
51c8250381SAndy Shevchenko 	(ESCAPE_SPACE | ESCAPE_OCTAL | ESCAPE_SPECIAL | ESCAPE_NULL)
52994b6970SAndy Shevchenko #define ESCAPE_NP		BIT(4)
53c8250381SAndy Shevchenko #define ESCAPE_ANY_NP		(ESCAPE_ANY | ESCAPE_NP)
54994b6970SAndy Shevchenko #define ESCAPE_HEX		BIT(5)
55*a0809783SAndy Shevchenko #define ESCAPE_NA		BIT(6)
56c8250381SAndy Shevchenko 
5741416f23SRasmus Villemoes int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz,
58b40bdb7fSKees Cook 		unsigned int flags, const char *only);
59c8250381SAndy Shevchenko 
60ea053e16SJ. Bruce Fields int string_escape_mem_ascii(const char *src, size_t isz, char *dst,
61ea053e16SJ. Bruce Fields 					size_t osz);
62ea053e16SJ. Bruce Fields 
63c8250381SAndy Shevchenko static inline int string_escape_mem_any_np(const char *src, size_t isz,
64b40bdb7fSKees Cook 		char *dst, size_t osz, const char *only)
65c8250381SAndy Shevchenko {
66b40bdb7fSKees Cook 	return string_escape_mem(src, isz, dst, osz, ESCAPE_ANY_NP, only);
67c8250381SAndy Shevchenko }
68c8250381SAndy Shevchenko 
6941416f23SRasmus Villemoes static inline int string_escape_str(const char *src, char *dst, size_t sz,
70b40bdb7fSKees Cook 		unsigned int flags, const char *only)
71c8250381SAndy Shevchenko {
72b40bdb7fSKees Cook 	return string_escape_mem(src, strlen(src), dst, sz, flags, only);
73c8250381SAndy Shevchenko }
74c8250381SAndy Shevchenko 
7541416f23SRasmus Villemoes static inline int string_escape_str_any_np(const char *src, char *dst,
76b40bdb7fSKees Cook 		size_t sz, const char *only)
77c8250381SAndy Shevchenko {
78b40bdb7fSKees Cook 	return string_escape_str(src, dst, sz, ESCAPE_ANY_NP, only);
79c8250381SAndy Shevchenko }
80c8250381SAndy Shevchenko 
8158eeba0bSVadim Pasternak static inline void string_upper(char *dst, const char *src)
8258eeba0bSVadim Pasternak {
8358eeba0bSVadim Pasternak 	do {
8458eeba0bSVadim Pasternak 		*dst++ = toupper(*src);
8558eeba0bSVadim Pasternak 	} while (*src++);
8658eeba0bSVadim Pasternak }
8758eeba0bSVadim Pasternak 
8858eeba0bSVadim Pasternak static inline void string_lower(char *dst, const char *src)
8958eeba0bSVadim Pasternak {
9058eeba0bSVadim Pasternak 	do {
9158eeba0bSVadim Pasternak 		*dst++ = tolower(*src);
9258eeba0bSVadim Pasternak 	} while (*src++);
9358eeba0bSVadim Pasternak }
9458eeba0bSVadim Pasternak 
95b53f27e4SKees Cook char *kstrdup_quotable(const char *src, gfp_t gfp);
960d044328SKees Cook char *kstrdup_quotable_cmdline(struct task_struct *task, gfp_t gfp);
9721985319SKees Cook char *kstrdup_quotable_file(struct file *file, gfp_t gfp);
98b53f27e4SKees Cook 
990fd16012SBartosz Golaszewski void kfree_strarray(char **array, size_t n);
1000fd16012SBartosz Golaszewski 
1013c9f3681SJames Bottomley #endif
102