Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 2 | #ifndef _LINUX_STRING_HELPERS_H_ |
| 3 | #define _LINUX_STRING_HELPERS_H_ |
| 4 | |
| 5 | #include <linux/types.h> |
| 6 | |
Kees Cook | 2198531 | 2016-04-20 15:46:25 -0700 | [diff] [blame] | 7 | struct file; |
Andy Shevchenko | c6d24c3 | 2018-05-04 04:13:45 +0300 | [diff] [blame] | 8 | struct task_struct; |
Kees Cook | 2198531 | 2016-04-20 15:46:25 -0700 | [diff] [blame] | 9 | |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 10 | /* Descriptions of the types of units to |
| 11 | * print in */ |
| 12 | enum string_size_units { |
| 13 | STRING_UNITS_10, /* use powers of 10^3 (standard SI) */ |
| 14 | STRING_UNITS_2, /* use binary powers of 2^10 */ |
| 15 | }; |
| 16 | |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 17 | void string_get_size(u64 size, u64 blk_size, enum string_size_units units, |
Rasmus Villemoes | d1214c6 | 2015-02-12 15:01:50 -0800 | [diff] [blame] | 18 | char *buf, int len); |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 19 | |
Andy Shevchenko | 16c7fa0 | 2013-04-30 15:27:30 -0700 | [diff] [blame] | 20 | #define UNESCAPE_SPACE 0x01 |
| 21 | #define UNESCAPE_OCTAL 0x02 |
| 22 | #define UNESCAPE_HEX 0x04 |
| 23 | #define UNESCAPE_SPECIAL 0x08 |
| 24 | #define UNESCAPE_ANY \ |
| 25 | (UNESCAPE_SPACE | UNESCAPE_OCTAL | UNESCAPE_HEX | UNESCAPE_SPECIAL) |
| 26 | |
Andy Shevchenko | 16c7fa0 | 2013-04-30 15:27:30 -0700 | [diff] [blame] | 27 | int string_unescape(char *src, char *dst, size_t size, unsigned int flags); |
| 28 | |
| 29 | static inline int string_unescape_inplace(char *buf, unsigned int flags) |
| 30 | { |
| 31 | return string_unescape(buf, buf, 0, flags); |
| 32 | } |
| 33 | |
| 34 | static inline int string_unescape_any(char *src, char *dst, size_t size) |
| 35 | { |
| 36 | return string_unescape(src, dst, size, UNESCAPE_ANY); |
| 37 | } |
| 38 | |
| 39 | static inline int string_unescape_any_inplace(char *buf) |
| 40 | { |
| 41 | return string_unescape_any(buf, buf, 0); |
| 42 | } |
| 43 | |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 44 | #define ESCAPE_SPACE 0x01 |
| 45 | #define ESCAPE_SPECIAL 0x02 |
| 46 | #define ESCAPE_NULL 0x04 |
| 47 | #define ESCAPE_OCTAL 0x08 |
| 48 | #define ESCAPE_ANY \ |
| 49 | (ESCAPE_SPACE | ESCAPE_OCTAL | ESCAPE_SPECIAL | ESCAPE_NULL) |
| 50 | #define ESCAPE_NP 0x10 |
| 51 | #define ESCAPE_ANY_NP (ESCAPE_ANY | ESCAPE_NP) |
| 52 | #define ESCAPE_HEX 0x20 |
| 53 | |
Rasmus Villemoes | 41416f2 | 2015-04-15 16:17:28 -0700 | [diff] [blame] | 54 | int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz, |
Kees Cook | b40bdb7 | 2015-09-09 15:37:16 -0700 | [diff] [blame] | 55 | unsigned int flags, const char *only); |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 56 | |
J. Bruce Fields | ea053e1 | 2019-06-19 12:30:13 -0400 | [diff] [blame] | 57 | int string_escape_mem_ascii(const char *src, size_t isz, char *dst, |
| 58 | size_t osz); |
| 59 | |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 60 | static inline int string_escape_mem_any_np(const char *src, size_t isz, |
Kees Cook | b40bdb7 | 2015-09-09 15:37:16 -0700 | [diff] [blame] | 61 | char *dst, size_t osz, const char *only) |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 62 | { |
Kees Cook | b40bdb7 | 2015-09-09 15:37:16 -0700 | [diff] [blame] | 63 | return string_escape_mem(src, isz, dst, osz, ESCAPE_ANY_NP, only); |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Rasmus Villemoes | 41416f2 | 2015-04-15 16:17:28 -0700 | [diff] [blame] | 66 | static inline int string_escape_str(const char *src, char *dst, size_t sz, |
Kees Cook | b40bdb7 | 2015-09-09 15:37:16 -0700 | [diff] [blame] | 67 | unsigned int flags, const char *only) |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 68 | { |
Kees Cook | b40bdb7 | 2015-09-09 15:37:16 -0700 | [diff] [blame] | 69 | return string_escape_mem(src, strlen(src), dst, sz, flags, only); |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Rasmus Villemoes | 41416f2 | 2015-04-15 16:17:28 -0700 | [diff] [blame] | 72 | static inline int string_escape_str_any_np(const char *src, char *dst, |
Kees Cook | b40bdb7 | 2015-09-09 15:37:16 -0700 | [diff] [blame] | 73 | size_t sz, const char *only) |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 74 | { |
Kees Cook | b40bdb7 | 2015-09-09 15:37:16 -0700 | [diff] [blame] | 75 | return string_escape_str(src, dst, sz, ESCAPE_ANY_NP, only); |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Kees Cook | b53f27e | 2016-04-20 15:46:23 -0700 | [diff] [blame] | 78 | char *kstrdup_quotable(const char *src, gfp_t gfp); |
Kees Cook | 0d04432 | 2016-04-20 15:46:24 -0700 | [diff] [blame] | 79 | char *kstrdup_quotable_cmdline(struct task_struct *task, gfp_t gfp); |
Kees Cook | 2198531 | 2016-04-20 15:46:25 -0700 | [diff] [blame] | 80 | char *kstrdup_quotable_file(struct file *file, gfp_t gfp); |
Kees Cook | b53f27e | 2016-04-20 15:46:23 -0700 | [diff] [blame] | 81 | |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 82 | #endif |