diff options
author | Andy Shevchenko <andriy.shevchenko@linux.intel.com> | 2013-04-30 18:27:30 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2013-04-30 20:04:03 -0400 |
commit | 16c7fa05829e8b91db48e3539c5d6ff3c2b18a23 (patch) | |
tree | 6658e9f27c033e5f6de19284fe9a6b8485360aa6 /include/linux/string_helpers.h | |
parent | e1d12f327037a59e06c66520951ab4e0bd29f9c4 (diff) |
lib/string_helpers: introduce generic string_unescape
There are several places in kernel where modules unescapes input to convert
C-Style Escape Sequences into byte codes.
The patch provides generic implementation of such approach. Test cases are
also included into the patch.
[akpm@linux-foundation.org: clarify comment]
[akpm@linux-foundation.org: export get_random_int() to modules]
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Samuel Thibault <samuel.thibault@ens-lyon.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Jason Baron <jbaron@redhat.com>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: William Hubbs <w.d.hubbs@gmail.com>
Cc: Chris Brannon <chris@the-brannons.com>
Cc: Kirk Reiser <kirk@braille.uwo.ca>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'include/linux/string_helpers.h')
-rw-r--r-- | include/linux/string_helpers.h | 58 |
1 files changed, 58 insertions, 0 deletions
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 |