]> git.ipfire.org Git - thirdparty/kernel/linux.git/blame - include/linux/string_helpers.h
Linux 6.16-rc5
[thirdparty/kernel/linux.git] / include / linux / string_helpers.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
3c9f3681
JB
2#ifndef _LINUX_STRING_HELPERS_H_
3#define _LINUX_STRING_HELPERS_H_
4
994b6970 5#include <linux/bits.h>
58eeba0b 6#include <linux/ctype.h>
fca76071 7#include <linux/string_choices.h>
bfb3ba32 8#include <linux/string.h>
3c9f3681
JB
9#include <linux/types.h>
10
acdb89b6 11struct device;
21985319 12struct file;
c6d24c32 13struct task_struct;
21985319 14
f1db99c0
AS
15static inline bool string_is_terminated(const char *s, int len)
16{
17 return memchr(s, '\0', len) ? true : false;
18}
19
f0b7f8ad 20/* Descriptions of the types of units to print in */
3c9f3681
JB
21enum string_size_units {
22 STRING_UNITS_10, /* use powers of 10^3 (standard SI) */
23 STRING_UNITS_2, /* use binary powers of 2^10 */
f0b7f8ad
AS
24 STRING_UNITS_MASK = BIT(0),
25
26 /* Modifiers */
27 STRING_UNITS_NO_SPACE = BIT(30),
28 STRING_UNITS_NO_BYTES = BIT(31),
3c9f3681
JB
29};
30
f0b7f8ad 31int string_get_size(u64 size, u64 blk_size, const enum string_size_units units,
83feeb19 32 char *buf, int len);
3c9f3681 33
83b9ae77 34int parse_int_array(const char *buf, size_t count, int **array);
f0b93323
CR
35int parse_int_array_user(const char __user *from, size_t count, int **array);
36
994b6970
AS
37#define UNESCAPE_SPACE BIT(0)
38#define UNESCAPE_OCTAL BIT(1)
39#define UNESCAPE_HEX BIT(2)
40#define UNESCAPE_SPECIAL BIT(3)
16c7fa05
AS
41#define UNESCAPE_ANY \
42 (UNESCAPE_SPACE | UNESCAPE_OCTAL | UNESCAPE_HEX | UNESCAPE_SPECIAL)
43
259fa5d7
AS
44#define UNESCAPE_ALL_MASK GENMASK(3, 0)
45
16c7fa05
AS
46int string_unescape(char *src, char *dst, size_t size, unsigned int flags);
47
48static inline int string_unescape_inplace(char *buf, unsigned int flags)
49{
50 return string_unescape(buf, buf, 0, flags);
51}
52
53static inline int string_unescape_any(char *src, char *dst, size_t size)
54{
55 return string_unescape(src, dst, size, UNESCAPE_ANY);
56}
57
58static inline int string_unescape_any_inplace(char *buf)
59{
60 return string_unescape_any(buf, buf, 0);
61}
62
994b6970
AS
63#define ESCAPE_SPACE BIT(0)
64#define ESCAPE_SPECIAL BIT(1)
65#define ESCAPE_NULL BIT(2)
66#define ESCAPE_OCTAL BIT(3)
c8250381
AS
67#define ESCAPE_ANY \
68 (ESCAPE_SPACE | ESCAPE_OCTAL | ESCAPE_SPECIAL | ESCAPE_NULL)
994b6970 69#define ESCAPE_NP BIT(4)
c8250381 70#define ESCAPE_ANY_NP (ESCAPE_ANY | ESCAPE_NP)
994b6970 71#define ESCAPE_HEX BIT(5)
a0809783 72#define ESCAPE_NA BIT(6)
0362c27f 73#define ESCAPE_NAP BIT(7)
aec0d096 74#define ESCAPE_APPEND BIT(8)
c8250381 75
259fa5d7
AS
76#define ESCAPE_ALL_MASK GENMASK(8, 0)
77
41416f23 78int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz,
b40bdb7f 79 unsigned int flags, const char *only);
c8250381
AS
80
81static inline int string_escape_mem_any_np(const char *src, size_t isz,
b40bdb7f 82 char *dst, size_t osz, const char *only)
c8250381 83{
b40bdb7f 84 return string_escape_mem(src, isz, dst, osz, ESCAPE_ANY_NP, only);
c8250381
AS
85}
86
41416f23 87static inline int string_escape_str(const char *src, char *dst, size_t sz,
b40bdb7f 88 unsigned int flags, const char *only)
c8250381 89{
b40bdb7f 90 return string_escape_mem(src, strlen(src), dst, sz, flags, only);
c8250381
AS
91}
92
41416f23 93static inline int string_escape_str_any_np(const char *src, char *dst,
b40bdb7f 94 size_t sz, const char *only)
c8250381 95{
b40bdb7f 96 return string_escape_str(src, dst, sz, ESCAPE_ANY_NP, only);
c8250381
AS
97}
98
58eeba0b
VP
99static inline void string_upper(char *dst, const char *src)
100{
101 do {
102 *dst++ = toupper(*src);
103 } while (*src++);
104}
105
106static inline void string_lower(char *dst, const char *src)
107{
108 do {
109 *dst++ = tolower(*src);
110 } while (*src++);
111}
112
b53f27e4 113char *kstrdup_quotable(const char *src, gfp_t gfp);
0d044328 114char *kstrdup_quotable_cmdline(struct task_struct *task, gfp_t gfp);
21985319 115char *kstrdup_quotable_file(struct file *file, gfp_t gfp);
b53f27e4 116
045ad464
AS
117char *kstrdup_and_replace(const char *src, char old, char new, gfp_t gfp);
118
418e0a35 119char **kasprintf_strarray(gfp_t gfp, const char *prefix, size_t n);
0fd16012
BG
120void kfree_strarray(char **array, size_t n);
121
acdb89b6
AS
122char **devm_kasprintf_strarray(struct device *dev, const char *prefix, size_t n);
123
3c9f3681 124#endif