blob: c2895513223422d9dac62bdac6313a25a31613b3 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
James Bottomley3c9f3682008-08-31 10:13:54 -05002#ifndef _LINUX_STRING_HELPERS_H_
3#define _LINUX_STRING_HELPERS_H_
4
5#include <linux/types.h>
6
Kees Cook21985312016-04-20 15:46:25 -07007struct file;
Andy Shevchenkoc6d24c32018-05-04 04:13:45 +03008struct task_struct;
Kees Cook21985312016-04-20 15:46:25 -07009
James Bottomley3c9f3682008-08-31 10:13:54 -050010/* Descriptions of the types of units to
11 * print in */
12enum 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 Bottomleyb9f28d82015-03-05 18:47:01 -080017void string_get_size(u64 size, u64 blk_size, enum string_size_units units,
Rasmus Villemoesd1214c62015-02-12 15:01:50 -080018 char *buf, int len);
James Bottomley3c9f3682008-08-31 10:13:54 -050019
Andy Shevchenko16c7fa02013-04-30 15:27:30 -070020#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 Shevchenko16c7fa02013-04-30 15:27:30 -070027int string_unescape(char *src, char *dst, size_t size, unsigned int flags);
28
29static inline int string_unescape_inplace(char *buf, unsigned int flags)
30{
31 return string_unescape(buf, buf, 0, flags);
32}
33
34static inline int string_unescape_any(char *src, char *dst, size_t size)
35{
36 return string_unescape(src, dst, size, UNESCAPE_ANY);
37}
38
39static inline int string_unescape_any_inplace(char *buf)
40{
41 return string_unescape_any(buf, buf, 0);
42}
43
Andy Shevchenkoc8250382014-10-13 15:55:16 -070044#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 Villemoes41416f22015-04-15 16:17:28 -070054int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz,
Kees Cookb40bdb72015-09-09 15:37:16 -070055 unsigned int flags, const char *only);
Andy Shevchenkoc8250382014-10-13 15:55:16 -070056
J. Bruce Fieldsea053e12019-06-19 12:30:13 -040057int string_escape_mem_ascii(const char *src, size_t isz, char *dst,
58 size_t osz);
59
Andy Shevchenkoc8250382014-10-13 15:55:16 -070060static inline int string_escape_mem_any_np(const char *src, size_t isz,
Kees Cookb40bdb72015-09-09 15:37:16 -070061 char *dst, size_t osz, const char *only)
Andy Shevchenkoc8250382014-10-13 15:55:16 -070062{
Kees Cookb40bdb72015-09-09 15:37:16 -070063 return string_escape_mem(src, isz, dst, osz, ESCAPE_ANY_NP, only);
Andy Shevchenkoc8250382014-10-13 15:55:16 -070064}
65
Rasmus Villemoes41416f22015-04-15 16:17:28 -070066static inline int string_escape_str(const char *src, char *dst, size_t sz,
Kees Cookb40bdb72015-09-09 15:37:16 -070067 unsigned int flags, const char *only)
Andy Shevchenkoc8250382014-10-13 15:55:16 -070068{
Kees Cookb40bdb72015-09-09 15:37:16 -070069 return string_escape_mem(src, strlen(src), dst, sz, flags, only);
Andy Shevchenkoc8250382014-10-13 15:55:16 -070070}
71
Rasmus Villemoes41416f22015-04-15 16:17:28 -070072static inline int string_escape_str_any_np(const char *src, char *dst,
Kees Cookb40bdb72015-09-09 15:37:16 -070073 size_t sz, const char *only)
Andy Shevchenkoc8250382014-10-13 15:55:16 -070074{
Kees Cookb40bdb72015-09-09 15:37:16 -070075 return string_escape_str(src, dst, sz, ESCAPE_ANY_NP, only);
Andy Shevchenkoc8250382014-10-13 15:55:16 -070076}
77
Kees Cookb53f27e2016-04-20 15:46:23 -070078char *kstrdup_quotable(const char *src, gfp_t gfp);
Kees Cook0d044322016-04-20 15:46:24 -070079char *kstrdup_quotable_cmdline(struct task_struct *task, gfp_t gfp);
Kees Cook21985312016-04-20 15:46:25 -070080char *kstrdup_quotable_file(struct file *file, gfp_t gfp);
Kees Cookb53f27e2016-04-20 15:46:23 -070081
James Bottomley3c9f3682008-08-31 10:13:54 -050082#endif