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 53c9f3681SJames Bottomley #include <linux/types.h> 63c9f3681SJames Bottomley 721985319SKees Cook struct file; 8c6d24c32SAndy Shevchenko struct task_struct; 921985319SKees Cook 103c9f3681SJames Bottomley /* Descriptions of the types of units to 113c9f3681SJames Bottomley * print in */ 123c9f3681SJames Bottomley enum string_size_units { 133c9f3681SJames Bottomley STRING_UNITS_10, /* use powers of 10^3 (standard SI) */ 143c9f3681SJames Bottomley STRING_UNITS_2, /* use binary powers of 2^10 */ 153c9f3681SJames Bottomley }; 163c9f3681SJames Bottomley 17b9f28d86SJames Bottomley void string_get_size(u64 size, u64 blk_size, enum string_size_units units, 183c9f3681SJames Bottomley char *buf, int len); 193c9f3681SJames Bottomley 2016c7fa05SAndy Shevchenko #define UNESCAPE_SPACE 0x01 2116c7fa05SAndy Shevchenko #define UNESCAPE_OCTAL 0x02 2216c7fa05SAndy Shevchenko #define UNESCAPE_HEX 0x04 2316c7fa05SAndy Shevchenko #define UNESCAPE_SPECIAL 0x08 2416c7fa05SAndy Shevchenko #define UNESCAPE_ANY \ 2516c7fa05SAndy Shevchenko (UNESCAPE_SPACE | UNESCAPE_OCTAL | UNESCAPE_HEX | UNESCAPE_SPECIAL) 2616c7fa05SAndy Shevchenko 2716c7fa05SAndy Shevchenko int string_unescape(char *src, char *dst, size_t size, unsigned int flags); 2816c7fa05SAndy Shevchenko 2916c7fa05SAndy Shevchenko static inline int string_unescape_inplace(char *buf, unsigned int flags) 3016c7fa05SAndy Shevchenko { 3116c7fa05SAndy Shevchenko return string_unescape(buf, buf, 0, flags); 3216c7fa05SAndy Shevchenko } 3316c7fa05SAndy Shevchenko 3416c7fa05SAndy Shevchenko static inline int string_unescape_any(char *src, char *dst, size_t size) 3516c7fa05SAndy Shevchenko { 3616c7fa05SAndy Shevchenko return string_unescape(src, dst, size, UNESCAPE_ANY); 3716c7fa05SAndy Shevchenko } 3816c7fa05SAndy Shevchenko 3916c7fa05SAndy Shevchenko static inline int string_unescape_any_inplace(char *buf) 4016c7fa05SAndy Shevchenko { 4116c7fa05SAndy Shevchenko return string_unescape_any(buf, buf, 0); 4216c7fa05SAndy Shevchenko } 4316c7fa05SAndy Shevchenko 44c8250381SAndy Shevchenko #define ESCAPE_SPACE 0x01 45c8250381SAndy Shevchenko #define ESCAPE_SPECIAL 0x02 46c8250381SAndy Shevchenko #define ESCAPE_NULL 0x04 47c8250381SAndy Shevchenko #define ESCAPE_OCTAL 0x08 48c8250381SAndy Shevchenko #define ESCAPE_ANY \ 49c8250381SAndy Shevchenko (ESCAPE_SPACE | ESCAPE_OCTAL | ESCAPE_SPECIAL | ESCAPE_NULL) 50c8250381SAndy Shevchenko #define ESCAPE_NP 0x10 51c8250381SAndy Shevchenko #define ESCAPE_ANY_NP (ESCAPE_ANY | ESCAPE_NP) 52c8250381SAndy Shevchenko #define ESCAPE_HEX 0x20 53c8250381SAndy Shevchenko 5441416f23SRasmus Villemoes int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz, 55b40bdb7fSKees Cook unsigned int flags, const char *only); 56c8250381SAndy Shevchenko 57*ea053e16SJ. Bruce Fields int string_escape_mem_ascii(const char *src, size_t isz, char *dst, 58*ea053e16SJ. Bruce Fields size_t osz); 59*ea053e16SJ. Bruce Fields 60c8250381SAndy Shevchenko static inline int string_escape_mem_any_np(const char *src, size_t isz, 61b40bdb7fSKees Cook char *dst, size_t osz, const char *only) 62c8250381SAndy Shevchenko { 63b40bdb7fSKees Cook return string_escape_mem(src, isz, dst, osz, ESCAPE_ANY_NP, only); 64c8250381SAndy Shevchenko } 65c8250381SAndy Shevchenko 6641416f23SRasmus Villemoes static inline int string_escape_str(const char *src, char *dst, size_t sz, 67b40bdb7fSKees Cook unsigned int flags, const char *only) 68c8250381SAndy Shevchenko { 69b40bdb7fSKees Cook return string_escape_mem(src, strlen(src), dst, sz, flags, only); 70c8250381SAndy Shevchenko } 71c8250381SAndy Shevchenko 7241416f23SRasmus Villemoes static inline int string_escape_str_any_np(const char *src, char *dst, 73b40bdb7fSKees Cook size_t sz, const char *only) 74c8250381SAndy Shevchenko { 75b40bdb7fSKees Cook return string_escape_str(src, dst, sz, ESCAPE_ANY_NP, only); 76c8250381SAndy Shevchenko } 77c8250381SAndy Shevchenko 78b53f27e4SKees Cook char *kstrdup_quotable(const char *src, gfp_t gfp); 790d044328SKees Cook char *kstrdup_quotable_cmdline(struct task_struct *task, gfp_t gfp); 8021985319SKees Cook char *kstrdup_quotable_file(struct file *file, gfp_t gfp); 81b53f27e4SKees Cook 823c9f3681SJames Bottomley #endif 83