diff options
-rw-r--r-- | drivers/char/random.c | 1 | ||||
-rw-r--r-- | include/linux/string_helpers.h | 58 | ||||
-rw-r--r-- | lib/Kconfig.debug | 3 | ||||
-rw-r--r-- | lib/Makefile | 4 | ||||
-rw-r--r-- | lib/string_helpers.c | 133 | ||||
-rw-r--r-- | lib/test-string_helpers.c | 103 |
6 files changed, 301 insertions, 1 deletions
diff --git a/drivers/char/random.c b/drivers/char/random.c index 32a6c5764950..cd9a6211dcad 100644 --- a/drivers/char/random.c +++ b/drivers/char/random.c | |||
@@ -1485,6 +1485,7 @@ unsigned int get_random_int(void) | |||
1485 | 1485 | ||
1486 | return ret; | 1486 | return ret; |
1487 | } | 1487 | } |
1488 | EXPORT_SYMBOL(get_random_int); | ||
1488 | 1489 | ||
1489 | /* | 1490 | /* |
1490 | * randomize_range() returns a start address such that | 1491 | * randomize_range() returns a start address such that |
diff --git a/include/linux/string_helpers.h b/include/linux/string_helpers.h index a3eb2f65b656..3eeee9672a4a 100644 --- a/include/linux/string_helpers.h +++ b/include/linux/string_helpers.h | |||
@@ -13,4 +13,62 @@ enum string_size_units { | |||
13 | int string_get_size(u64 size, enum string_size_units units, | 13 | int string_get_size(u64 size, enum string_size_units units, |
14 | char *buf, int len); | 14 | char *buf, int len); |
15 | 15 | ||
16 | #define UNESCAPE_SPACE 0x01 | ||
17 | #define UNESCAPE_OCTAL 0x02 | ||
18 | #define UNESCAPE_HEX 0x04 | ||
19 | #define UNESCAPE_SPECIAL 0x08 | ||
20 | #define UNESCAPE_ANY \ | ||
21 | (UNESCAPE_SPACE | UNESCAPE_OCTAL | UNESCAPE_HEX | UNESCAPE_SPECIAL) | ||
22 | |||
23 | /** | ||
24 | * string_unescape - unquote characters in the given string | ||
25 | * @src: source buffer (escaped) | ||
26 | * @dst: destination buffer (unescaped) | ||
27 | * @size: size of the destination buffer (0 to unlimit) | ||
28 | * @flags: combination of the flags (bitwise OR): | ||
29 | * %UNESCAPE_SPACE: | ||
30 | * '\f' - form feed | ||
31 | * '\n' - new line | ||
32 | * '\r' - carriage return | ||
33 | * '\t' - horizontal tab | ||
34 | * '\v' - vertical tab | ||
35 | * %UNESCAPE_OCTAL: | ||
36 | * '\NNN' - byte with octal value NNN (1 to 3 digits) | ||
37 | * %UNESCAPE_HEX: | ||
38 | * '\xHH' - byte with hexadecimal value HH (1 to 2 digits) | ||
39 | * %UNESCAPE_SPECIAL: | ||
40 | * '\"' - double quote | ||
41 | * '\\' - backslash | ||
42 | * '\a' - alert (BEL) | ||
43 | * '\e' - escape | ||
44 | * %UNESCAPE_ANY: | ||
45 | * all previous together | ||
46 | * | ||
47 | * Returns amount of characters processed to the destination buffer excluding | ||
48 | * trailing '\0'. | ||
49 | * | ||
50 | * Because the size of the output will be the same as or less than the size of | ||
51 | * the input, the transformation may be performed in place. | ||
52 | * | ||
53 | * Caller must provide valid source and destination pointers. Be aware that | ||
54 | * destination buffer will always be NULL-terminated. Source string must be | ||
55 | * NULL-terminated as well. | ||
56 | */ | ||
57 | int string_unescape(char *src, char *dst, size_t size, unsigned int flags); | ||
58 | |||
59 | static inline int string_unescape_inplace(char *buf, unsigned int flags) | ||
60 | { | ||
61 | return string_unescape(buf, buf, 0, flags); | ||
62 | } | ||
63 | |||
64 | static inline int string_unescape_any(char *src, char *dst, size_t size) | ||
65 | { | ||
66 | return string_unescape(src, dst, size, UNESCAPE_ANY); | ||
67 | } | ||
68 | |||
69 | static inline int string_unescape_any_inplace(char *buf) | ||
70 | { | ||
71 | return string_unescape_any(buf, buf, 0); | ||
72 | } | ||
73 | |||
16 | #endif | 74 | #endif |
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 28be08c09bab..77ebaa3dfa12 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug | |||
@@ -1463,5 +1463,8 @@ source "lib/Kconfig.kgdb" | |||
1463 | 1463 | ||
1464 | source "lib/Kconfig.kmemcheck" | 1464 | source "lib/Kconfig.kmemcheck" |
1465 | 1465 | ||
1466 | config TEST_STRING_HELPERS | ||
1467 | tristate "Test functions located in the string_helpers module at runtime" | ||
1468 | |||
1466 | config TEST_KSTRTOX | 1469 | config TEST_KSTRTOX |
1467 | tristate "Test kstrto*() family of functions at runtime" | 1470 | tristate "Test kstrto*() family of functions at runtime" |
diff --git a/lib/Makefile b/lib/Makefile index 6e2cc561f761..23c9a0fe74fc 100644 --- a/lib/Makefile +++ b/lib/Makefile | |||
@@ -22,8 +22,10 @@ lib-y += kobject.o klist.o | |||
22 | 22 | ||
23 | obj-y += bcd.o div64.o sort.o parser.o halfmd4.o debug_locks.o random32.o \ | 23 | obj-y += bcd.o div64.o sort.o parser.o halfmd4.o debug_locks.o random32.o \ |
24 | bust_spinlocks.o hexdump.o kasprintf.o bitmap.o scatterlist.o \ | 24 | bust_spinlocks.o hexdump.o kasprintf.o bitmap.o scatterlist.o \ |
25 | string_helpers.o gcd.o lcm.o list_sort.o uuid.o flex_array.o \ | 25 | gcd.o lcm.o list_sort.o uuid.o flex_array.o \ |
26 | bsearch.o find_last_bit.o find_next_bit.o llist.o memweight.o kfifo.o | 26 | bsearch.o find_last_bit.o find_next_bit.o llist.o memweight.o kfifo.o |
27 | obj-y += string_helpers.o | ||
28 | obj-$(CONFIG_TEST_STRING_HELPERS) += test-string_helpers.o | ||
27 | obj-y += kstrtox.o | 29 | obj-y += kstrtox.o |
28 | obj-$(CONFIG_TEST_KSTRTOX) += test-kstrtox.o | 30 | obj-$(CONFIG_TEST_KSTRTOX) += test-kstrtox.o |
29 | 31 | ||
diff --git a/lib/string_helpers.c b/lib/string_helpers.c index 1cffc223bff5..ed5c1454dd62 100644 --- a/lib/string_helpers.c +++ b/lib/string_helpers.c | |||
@@ -2,10 +2,12 @@ | |||
2 | * Helpers for formatting and printing strings | 2 | * Helpers for formatting and printing strings |
3 | * | 3 | * |
4 | * Copyright 31 August 2008 James Bottomley | 4 | * Copyright 31 August 2008 James Bottomley |
5 | * Copyright (C) 2013, Intel Corporation | ||
5 | */ | 6 | */ |
6 | #include <linux/kernel.h> | 7 | #include <linux/kernel.h> |
7 | #include <linux/math64.h> | 8 | #include <linux/math64.h> |
8 | #include <linux/export.h> | 9 | #include <linux/export.h> |
10 | #include <linux/ctype.h> | ||
9 | #include <linux/string_helpers.h> | 11 | #include <linux/string_helpers.h> |
10 | 12 | ||
11 | /** | 13 | /** |
@@ -66,3 +68,134 @@ int string_get_size(u64 size, const enum string_size_units units, | |||
66 | return 0; | 68 | return 0; |
67 | } | 69 | } |
68 | EXPORT_SYMBOL(string_get_size); | 70 | EXPORT_SYMBOL(string_get_size); |
71 | |||
72 | static bool unescape_space(char **src, char **dst) | ||
73 | { | ||
74 | char *p = *dst, *q = *src; | ||
75 | |||
76 | switch (*q) { | ||
77 | case 'n': | ||
78 | *p = '\n'; | ||
79 | break; | ||
80 | case 'r': | ||
81 | *p = '\r'; | ||
82 | break; | ||
83 | case 't': | ||
84 | *p = '\t'; | ||
85 | break; | ||
86 | case 'v': | ||
87 | *p = '\v'; | ||
88 | break; | ||
89 | case 'f': | ||
90 | *p = '\f'; | ||
91 | break; | ||
92 | default: | ||
93 | return false; | ||
94 | } | ||
95 | *dst += 1; | ||
96 | *src += 1; | ||
97 | return true; | ||
98 | } | ||
99 | |||
100 | static bool unescape_octal(char **src, char **dst) | ||
101 | { | ||
102 | char *p = *dst, *q = *src; | ||
103 | u8 num; | ||
104 | |||
105 | if (isodigit(*q) == 0) | ||
106 | return false; | ||
107 | |||
108 | num = (*q++) & 7; | ||
109 | while (num < 32 && isodigit(*q) && (q - *src < 3)) { | ||
110 | num <<= 3; | ||
111 | num += (*q++) & 7; | ||
112 | } | ||
113 | *p = num; | ||
114 | *dst += 1; | ||
115 | *src = q; | ||
116 | return true; | ||
117 | } | ||
118 | |||
119 | static bool unescape_hex(char **src, char **dst) | ||
120 | { | ||
121 | char *p = *dst, *q = *src; | ||
122 | int digit; | ||
123 | u8 num; | ||
124 | |||
125 | if (*q++ != 'x') | ||
126 | return false; | ||
127 | |||
128 | num = digit = hex_to_bin(*q++); | ||
129 | if (digit < 0) | ||
130 | return false; | ||
131 | |||
132 | digit = hex_to_bin(*q); | ||
133 | if (digit >= 0) { | ||
134 | q++; | ||
135 | num = (num << 4) | digit; | ||
136 | } | ||
137 | *p = num; | ||
138 | *dst += 1; | ||
139 | *src = q; | ||
140 | return true; | ||
141 | } | ||
142 | |||
143 | static bool unescape_special(char **src, char **dst) | ||
144 | { | ||
145 | char *p = *dst, *q = *src; | ||
146 | |||
147 | switch (*q) { | ||
148 | case '\"': | ||
149 | *p = '\"'; | ||
150 | break; | ||
151 | case '\\': | ||
152 | *p = '\\'; | ||
153 | break; | ||
154 | case 'a': | ||
155 | *p = '\a'; | ||
156 | break; | ||
157 | case 'e': | ||
158 | *p = '\e'; | ||
159 | break; | ||
160 | default: | ||
161 | return false; | ||
162 | } | ||
163 | *dst += 1; | ||
164 | *src += 1; | ||
165 | return true; | ||
166 | } | ||
167 | |||
168 | int string_unescape(char *src, char *dst, size_t size, unsigned int flags) | ||
169 | { | ||
170 | char *out = dst; | ||
171 | |||
172 | while (*src && --size) { | ||
173 | if (src[0] == '\\' && src[1] != '\0' && size > 1) { | ||
174 | src++; | ||
175 | size--; | ||
176 | |||
177 | if (flags & UNESCAPE_SPACE && | ||
178 | unescape_space(&src, &out)) | ||
179 | continue; | ||
180 | |||
181 | if (flags & UNESCAPE_OCTAL && | ||
182 | unescape_octal(&src, &out)) | ||
183 | continue; | ||
184 | |||
185 | if (flags & UNESCAPE_HEX && | ||
186 | unescape_hex(&src, &out)) | ||
187 | continue; | ||
188 | |||
189 | if (flags & UNESCAPE_SPECIAL && | ||
190 | unescape_special(&src, &out)) | ||
191 | continue; | ||
192 | |||
193 | *out++ = '\\'; | ||
194 | } | ||
195 | *out++ = *src++; | ||
196 | } | ||
197 | *out = '\0'; | ||
198 | |||
199 | return out - dst; | ||
200 | } | ||
201 | EXPORT_SYMBOL(string_unescape); | ||
diff --git a/lib/test-string_helpers.c b/lib/test-string_helpers.c new file mode 100644 index 000000000000..6ac48de04c0e --- /dev/null +++ b/lib/test-string_helpers.c | |||
@@ -0,0 +1,103 @@ | |||
1 | /* | ||
2 | * Test cases for lib/string_helpers.c module. | ||
3 | */ | ||
4 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | ||
5 | |||
6 | #include <linux/init.h> | ||
7 | #include <linux/kernel.h> | ||
8 | #include <linux/module.h> | ||
9 | #include <linux/random.h> | ||
10 | #include <linux/string.h> | ||
11 | #include <linux/string_helpers.h> | ||
12 | |||
13 | struct test_string { | ||
14 | const char *in; | ||
15 | const char *out; | ||
16 | unsigned int flags; | ||
17 | }; | ||
18 | |||
19 | static const struct test_string strings[] __initconst = { | ||
20 | { | ||
21 | .in = "\\f\\ \\n\\r\\t\\v", | ||
22 | .out = "\f\\ \n\r\t\v", | ||
23 | .flags = UNESCAPE_SPACE, | ||
24 | }, | ||
25 | { | ||
26 | .in = "\\40\\1\\387\\0064\\05\\040\\8a\\110\\777", | ||
27 | .out = " \001\00387\0064\005 \\8aH?7", | ||
28 | .flags = UNESCAPE_OCTAL, | ||
29 | }, | ||
30 | { | ||
31 | .in = "\\xv\\xa\\x2c\\xD\\x6f2", | ||
32 | .out = "\\xv\n,\ro2", | ||
33 | .flags = UNESCAPE_HEX, | ||
34 | }, | ||
35 | { | ||
36 | .in = "\\h\\\\\\\"\\a\\e\\", | ||
37 | .out = "\\h\\\"\a\e\\", | ||
38 | .flags = UNESCAPE_SPECIAL, | ||
39 | }, | ||
40 | }; | ||
41 | |||
42 | static void __init test_string_unescape(unsigned int flags, bool inplace) | ||
43 | { | ||
44 | char in[256]; | ||
45 | char out_test[256]; | ||
46 | char out_real[256]; | ||
47 | int i, p = 0, q_test = 0, q_real = sizeof(out_real); | ||
48 | |||
49 | for (i = 0; i < ARRAY_SIZE(strings); i++) { | ||
50 | const char *s = strings[i].in; | ||
51 | int len = strlen(strings[i].in); | ||
52 | |||
53 | /* Copy string to in buffer */ | ||
54 | memcpy(&in[p], s, len); | ||
55 | p += len; | ||
56 | |||
57 | /* Copy expected result for given flags */ | ||
58 | if (flags & strings[i].flags) { | ||
59 | s = strings[i].out; | ||
60 | len = strlen(strings[i].out); | ||
61 | } | ||
62 | memcpy(&out_test[q_test], s, len); | ||
63 | q_test += len; | ||
64 | } | ||
65 | in[p++] = '\0'; | ||
66 | |||
67 | /* Call string_unescape and compare result */ | ||
68 | if (inplace) { | ||
69 | memcpy(out_real, in, p); | ||
70 | if (flags == UNESCAPE_ANY) | ||
71 | q_real = string_unescape_any_inplace(out_real); | ||
72 | else | ||
73 | q_real = string_unescape_inplace(out_real, flags); | ||
74 | } else if (flags == UNESCAPE_ANY) { | ||
75 | q_real = string_unescape_any(in, out_real, q_real); | ||
76 | } else { | ||
77 | q_real = string_unescape(in, out_real, q_real, flags); | ||
78 | } | ||
79 | |||
80 | if (q_real != q_test || memcmp(out_test, out_real, q_test)) { | ||
81 | pr_warn("Test failed: flags = %u\n", flags); | ||
82 | print_hex_dump(KERN_WARNING, "Input: ", | ||
83 | DUMP_PREFIX_NONE, 16, 1, in, p - 1, true); | ||
84 | print_hex_dump(KERN_WARNING, "Expected: ", | ||
85 | DUMP_PREFIX_NONE, 16, 1, out_test, q_test, true); | ||
86 | print_hex_dump(KERN_WARNING, "Got: ", | ||
87 | DUMP_PREFIX_NONE, 16, 1, out_real, q_real, true); | ||
88 | } | ||
89 | } | ||
90 | |||
91 | static int __init test_string_helpers_init(void) | ||
92 | { | ||
93 | unsigned int i; | ||
94 | |||
95 | pr_info("Running tests...\n"); | ||
96 | for (i = 0; i < UNESCAPE_ANY + 1; i++) | ||
97 | test_string_unescape(i, false); | ||
98 | test_string_unescape(get_random_int() % (UNESCAPE_ANY + 1), true); | ||
99 | |||
100 | return -EINVAL; | ||
101 | } | ||
102 | module_init(test_string_helpers_init); | ||
103 | MODULE_LICENSE("Dual BSD/GPL"); | ||