]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
3c9f3681 JB |
2 | #ifndef _LINUX_STRING_HELPERS_H_ |
3 | #define _LINUX_STRING_HELPERS_H_ | |
4 | ||
58eeba0b | 5 | #include <linux/ctype.h> |
3c9f3681 JB |
6 | #include <linux/types.h> |
7 | ||
21985319 | 8 | struct file; |
c6d24c32 | 9 | struct task_struct; |
21985319 | 10 | |
3c9f3681 JB |
11 | /* Descriptions of the types of units to |
12 | * print in */ | |
13 | enum string_size_units { | |
14 | STRING_UNITS_10, /* use powers of 10^3 (standard SI) */ | |
15 | STRING_UNITS_2, /* use binary powers of 2^10 */ | |
16 | }; | |
17 | ||
b9f28d86 | 18 | void string_get_size(u64 size, u64 blk_size, enum string_size_units units, |
d1214c65 | 19 | char *buf, int len); |
3c9f3681 | 20 | |
16c7fa05 AS |
21 | #define UNESCAPE_SPACE 0x01 |
22 | #define UNESCAPE_OCTAL 0x02 | |
23 | #define UNESCAPE_HEX 0x04 | |
24 | #define UNESCAPE_SPECIAL 0x08 | |
25 | #define UNESCAPE_ANY \ | |
26 | (UNESCAPE_SPACE | UNESCAPE_OCTAL | UNESCAPE_HEX | UNESCAPE_SPECIAL) | |
27 | ||
16c7fa05 AS |
28 | int string_unescape(char *src, char *dst, size_t size, unsigned int flags); |
29 | ||
30 | static inline int string_unescape_inplace(char *buf, unsigned int flags) | |
31 | { | |
32 | return string_unescape(buf, buf, 0, flags); | |
33 | } | |
34 | ||
35 | static inline int string_unescape_any(char *src, char *dst, size_t size) | |
36 | { | |
37 | return string_unescape(src, dst, size, UNESCAPE_ANY); | |
38 | } | |
39 | ||
40 | static inline int string_unescape_any_inplace(char *buf) | |
41 | { | |
42 | return string_unescape_any(buf, buf, 0); | |
43 | } | |
44 | ||
c8250381 AS |
45 | #define ESCAPE_SPACE 0x01 |
46 | #define ESCAPE_SPECIAL 0x02 | |
47 | #define ESCAPE_NULL 0x04 | |
48 | #define ESCAPE_OCTAL 0x08 | |
49 | #define ESCAPE_ANY \ | |
50 | (ESCAPE_SPACE | ESCAPE_OCTAL | ESCAPE_SPECIAL | ESCAPE_NULL) | |
51 | #define ESCAPE_NP 0x10 | |
52 | #define ESCAPE_ANY_NP (ESCAPE_ANY | ESCAPE_NP) | |
53 | #define ESCAPE_HEX 0x20 | |
54 | ||
41416f23 | 55 | int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz, |
b40bdb7f | 56 | unsigned int flags, const char *only); |
c8250381 | 57 | |
ea053e16 BF |
58 | int string_escape_mem_ascii(const char *src, size_t isz, char *dst, |
59 | size_t osz); | |
60 | ||
c8250381 | 61 | static inline int string_escape_mem_any_np(const char *src, size_t isz, |
b40bdb7f | 62 | char *dst, size_t osz, const char *only) |
c8250381 | 63 | { |
b40bdb7f | 64 | return string_escape_mem(src, isz, dst, osz, ESCAPE_ANY_NP, only); |
c8250381 AS |
65 | } |
66 | ||
41416f23 | 67 | static inline int string_escape_str(const char *src, char *dst, size_t sz, |
b40bdb7f | 68 | unsigned int flags, const char *only) |
c8250381 | 69 | { |
b40bdb7f | 70 | return string_escape_mem(src, strlen(src), dst, sz, flags, only); |
c8250381 AS |
71 | } |
72 | ||
41416f23 | 73 | static inline int string_escape_str_any_np(const char *src, char *dst, |
b40bdb7f | 74 | size_t sz, const char *only) |
c8250381 | 75 | { |
b40bdb7f | 76 | return string_escape_str(src, dst, sz, ESCAPE_ANY_NP, only); |
c8250381 AS |
77 | } |
78 | ||
58eeba0b VP |
79 | static inline void string_upper(char *dst, const char *src) |
80 | { | |
81 | do { | |
82 | *dst++ = toupper(*src); | |
83 | } while (*src++); | |
84 | } | |
85 | ||
86 | static inline void string_lower(char *dst, const char *src) | |
87 | { | |
88 | do { | |
89 | *dst++ = tolower(*src); | |
90 | } while (*src++); | |
91 | } | |
92 | ||
b53f27e4 | 93 | char *kstrdup_quotable(const char *src, gfp_t gfp); |
0d044328 | 94 | char *kstrdup_quotable_cmdline(struct task_struct *task, gfp_t gfp); |
21985319 | 95 | char *kstrdup_quotable_file(struct file *file, gfp_t gfp); |
b53f27e4 | 96 | |
0fd16012 BG |
97 | void kfree_strarray(char **array, size_t n); |
98 | ||
3c9f3681 | 99 | #endif |