blob: 4397c52ec4a496f61f7c26599a515276c0acc3ee [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;
8
James Bottomley3c9f3682008-08-31 10:13:54 -05009/* Descriptions of the types of units to
10 * print in */
11enum string_size_units {
12 STRING_UNITS_10, /* use powers of 10^3 (standard SI) */
13 STRING_UNITS_2, /* use binary powers of 2^10 */
14};
15
James Bottomleyb9f28d82015-03-05 18:47:01 -080016void string_get_size(u64 size, u64 blk_size, enum string_size_units units,
Rasmus Villemoesd1214c62015-02-12 15:01:50 -080017 char *buf, int len);
James Bottomley3c9f3682008-08-31 10:13:54 -050018
Andy Shevchenko16c7fa02013-04-30 15:27:30 -070019#define UNESCAPE_SPACE 0x01
20#define UNESCAPE_OCTAL 0x02
21#define UNESCAPE_HEX 0x04
22#define UNESCAPE_SPECIAL 0x08
23#define UNESCAPE_ANY \
24 (UNESCAPE_SPACE | UNESCAPE_OCTAL | UNESCAPE_HEX | UNESCAPE_SPECIAL)
25
Andy Shevchenko16c7fa02013-04-30 15:27:30 -070026int string_unescape(char *src, char *dst, size_t size, unsigned int flags);
27
28static inline int string_unescape_inplace(char *buf, unsigned int flags)
29{
30 return string_unescape(buf, buf, 0, flags);
31}
32
33static inline int string_unescape_any(char *src, char *dst, size_t size)
34{
35 return string_unescape(src, dst, size, UNESCAPE_ANY);
36}
37
38static inline int string_unescape_any_inplace(char *buf)
39{
40 return string_unescape_any(buf, buf, 0);
41}
42
Andy Shevchenkoc8250382014-10-13 15:55:16 -070043#define ESCAPE_SPACE 0x01
44#define ESCAPE_SPECIAL 0x02
45#define ESCAPE_NULL 0x04
46#define ESCAPE_OCTAL 0x08
47#define ESCAPE_ANY \
48 (ESCAPE_SPACE | ESCAPE_OCTAL | ESCAPE_SPECIAL | ESCAPE_NULL)
49#define ESCAPE_NP 0x10
50#define ESCAPE_ANY_NP (ESCAPE_ANY | ESCAPE_NP)
51#define ESCAPE_HEX 0x20
52
Rasmus Villemoes41416f22015-04-15 16:17:28 -070053int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz,
Kees Cookb40bdb72015-09-09 15:37:16 -070054 unsigned int flags, const char *only);
Andy Shevchenkoc8250382014-10-13 15:55:16 -070055
56static inline int string_escape_mem_any_np(const char *src, size_t isz,
Kees Cookb40bdb72015-09-09 15:37:16 -070057 char *dst, size_t osz, const char *only)
Andy Shevchenkoc8250382014-10-13 15:55:16 -070058{
Kees Cookb40bdb72015-09-09 15:37:16 -070059 return string_escape_mem(src, isz, dst, osz, ESCAPE_ANY_NP, only);
Andy Shevchenkoc8250382014-10-13 15:55:16 -070060}
61
Rasmus Villemoes41416f22015-04-15 16:17:28 -070062static inline int string_escape_str(const char *src, char *dst, size_t sz,
Kees Cookb40bdb72015-09-09 15:37:16 -070063 unsigned int flags, const char *only)
Andy Shevchenkoc8250382014-10-13 15:55:16 -070064{
Kees Cookb40bdb72015-09-09 15:37:16 -070065 return string_escape_mem(src, strlen(src), dst, sz, flags, only);
Andy Shevchenkoc8250382014-10-13 15:55:16 -070066}
67
Rasmus Villemoes41416f22015-04-15 16:17:28 -070068static inline int string_escape_str_any_np(const char *src, char *dst,
Kees Cookb40bdb72015-09-09 15:37:16 -070069 size_t sz, const char *only)
Andy Shevchenkoc8250382014-10-13 15:55:16 -070070{
Kees Cookb40bdb72015-09-09 15:37:16 -070071 return string_escape_str(src, dst, sz, ESCAPE_ANY_NP, only);
Andy Shevchenkoc8250382014-10-13 15:55:16 -070072}
73
Kees Cookb53f27e2016-04-20 15:46:23 -070074char *kstrdup_quotable(const char *src, gfp_t gfp);
Kees Cook0d044322016-04-20 15:46:24 -070075char *kstrdup_quotable_cmdline(struct task_struct *task, gfp_t gfp);
Kees Cook21985312016-04-20 15:46:25 -070076char *kstrdup_quotable_file(struct file *file, gfp_t gfp);
Kees Cookb53f27e2016-04-20 15:46:23 -070077
James Bottomley3c9f3682008-08-31 10:13:54 -050078#endif