lib/string_helpers: introduce generic string_unescape
Subsystems (6)
PATCH 1/4 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.
--- include/linux/string_helpers.h | 58 ++++++++++++++++++ lib/Kconfig.debug | 3 + lib/Makefile | 4 +- lib/string_helpers.c | 133 +++++++++++++++++++++++++++++++++++++++++ lib/test-string_helpers.c | 103 +++++++++++++++++++++++++++++++ 5 files changed, 300 insertions(+), 1 deletion(-) create mode 100644 lib/test-string_helpers.c
include/linux/string_helpers.h +58 -0
@@ -13,4 +13,62 @@ enum string_size_units {int string_get_size (u64 size,enum string_size_units units,char *buf,int len); +#define UNESCAPE_SPACE 0x01 +#define UNESCAPE_OCTAL 0x02 +#define UNESCAPE_HEX 0x04 +#define UNESCAPE_SPECIAL 0x08 +#define UNESCAPE_ANY \ + (UNESCAPE_SPACE | UNESCAPE_OCTAL | UNESCAPE_HEX | UNESCAPE_SPECIAL) + +/** +* string_unescape - unquote characters in the given string +* @src: source buffer (escaped) +* @dst: destination buffer (unescaped) +* @size: size of the destination buffer (0 to unlimit) +* @flags: combination of the flags (bitwise OR): +* %UNESCAPE_SPACE: +* '\f' - form feed +* '\n' - new line +* '\r' - carriage return +* '\t' - horizontal tab +* '\v' - vertical tab +* %UNESCAPE_OCTAL: +* '\NNN' - byte with octal value NNN (1 to 3 digits) +* %UNESCAPE_HEX: +* '\xHH' - byte with hexadecimal value HH (1 to 2 digits) +* %UNESCAPE_SPECIAL: +* '\"' - double quote +* '\\' - backslash +* '\a' - alert (BEL) +* '\e' - escape +* %UNESCAPE_ANY: +* all previous together +* +* Returns amount of characters processed to the destination buffer excluding +* trailing '\0'. +* +* Due to size of output will be the same or smaller the translation could be +* done in place. +* +* Caller must provide valid source and destination pointers. Be aware that +* destination buffer will always be NULL-terminated. Source string must be +* NULL-terminated as well. +*/ +int string_unescape (char *src,char *dst,size_t size,unsigned int flags); + +static inline int string_unescape_inplace (char *buf,unsigned int flags) +{ +return string_unescape (buf, buf,0 , flags); +} + +static inline int string_unescape_any (char *src,char *dst,size_t size) +{ +return string_unescape (src, dst, size,UNESCAPE_ANY ); +} + +static inline int string_unescape_any_inplace (char *buf) +{ +return string_unescape_any (buf, buf,0 ); +} +#endif
lib/Kconfig.debug +3 -0
@@ -1481,5 +1481,8 @@ source "lib/Kconfig.kgdb" source "lib/Kconfig.kmemcheck" +config TEST_STRING_HELPERS + tristate "Test functions located in the string_helpers module at runtime" + config TEST_KSTRTOX tristate "Test kstrto*() family of functions at runtime"
lib/Makefile +3 -1
@@ -23,8 +23,10 @@ lib-y += kobject.o klist.o obj-y += bcd.o div64.o sort.o parser.o halfmd4.o debug_locks.o random32.o \ bust_spinlocks.o hexdump.o kasprintf.o bitmap.o scatterlist.o \ - string_helpers.o gcd.o lcm.o list_sort.o uuid.o flex_array.o \ + gcd.o lcm.o list_sort.o uuid.o flex_array.o \ bsearch.o find_last_bit.o find_next_bit.o llist.o memweight.o kfifo.o +obj-y += string_helpers.o +obj-$(CONFIG_TEST_STRING_HELPERS) += test-string_helpers.o obj-y += kstrtox.o obj-$(CONFIG_TEST_KSTRTOX) += test-kstrtox.o
lib/string_helpers.c +133 -0
146 lines not on this page · raw message
lib/test-string_helpers.c new file +103 -0
103 lines not on this page · raw message
Signature · 1 line
1.8.2.rc0.22.gb3600c3
PATCH 2/4 staging: speakup: remove custom string_unescape_any_inplace
There is generic implementation of the function to unescape strings.
--- drivers/staging/speakup/kobjects.c | 6 +++-- drivers/staging/speakup/speakup.h | 1 - drivers/staging/speakup/varhandlers.c | 46 ----------------------------------- 3 files changed, 4 insertions(+), 49 deletions(-)
drivers/staging/speakup/kobjects.c +4 -2
@@ -15,6 +15,7 @@#include <linux/kernel.h> #include <linux/kobject.h> #include <linux/string.h> +#include <linux/string_helpers.h> #include <linux/sysfs.h> #include <linux/ctype.h>
@@ -417,7 +418,7 @@ static ssize_t synth_direct_store(struct kobject *kobj, bytes =min_t (size_t, len,250 );strncpy (tmp, ptr, bytes); tmp[bytes] ='\0' ; -spk_xlate (tmp); +string_unescape_any_inplace (tmp);synth_printf ("%s" , tmp); ptr += bytes; len -= bytes;
@@ -605,7 +606,8 @@ ssize_t spk_var_store(struct kobject *kobj, struct kobj_attribute *attr,if (param->data ==NULL )return 0 ; ret =0 ; - cp =spk_xlate ((char *) buf); + cp = (char *)buf; +string_unescape_any_inplace (cp);spk_lock (flags);switch (param->var_type) {
drivers/staging/speakup/speakup.h +0 -1
@@ -54,7 +54,6 @@ void spk_get_index_count(int *linecount, int *sentcount);extern int spk_set_key_info (const u_char *key_info,u_char *k_buffer);extern char *spk_strlwr (char *s);extern char *spk_s2uchar (char *start,char *dest); -extern char *spk_xlate (char *s);extern int speakup_kobj_init (void );extern void speakup_kobj_exit (void );extern int spk_chartab_get_value (char *keyword);
drivers/staging/speakup/varhandlers.c +0 -46
49 lines not on this page · raw message
Signature · 1 line
1.8.2.rc0.22.gb3600c3
PATCH 3/4 dynamic_debug: reuse generic string_unescape function
In reply to Andy ShevchenkoThere is kernel function to do the job in generic way. Let's use it.
--- lib/dynamic_debug.c | 48 +++++------------------------------------------- 1 file changed, 5 insertions(+), 43 deletions(-)
lib/dynamic_debug.c +5 -43
@@ -24,6 +24,7 @@#include <linux/sysctl.h> #include <linux/ctype.h> #include <linux/string.h> +#include <linux/string_helpers.h> #include <linux/uaccess.h> #include <linux/dynamic_debug.h> #include <linux/debugfs.h>
@@ -276,47 +277,6 @@ static inline int parse_lineno(const char *str, unsigned int *val)return 0 ; } -/* -* Undo octal escaping in a string, inplace. This is useful to -* allow the user to express a query which matches a format -* containing embedded spaces. -*/ -static char *unescape (char *str) -{ -char *in = str; -char *out = str; - -while (*in) { -if (*in =='\\' ) { -if (in[1 ] =='\\' ) { - *out++ ='\\' ; - in +=2 ; -continue ; - }else if (in[1 ] =='t' ) { - *out++ ='\t' ; - in +=2 ; -continue ; - }else if (in[1 ] =='n' ) { - *out++ ='\n' ; - in +=2 ; -continue ; - }else if (isodigit (in[1 ]) && -isodigit (in[2 ]) && -isodigit (in[3 ])) { - *out++ = (((in[1 ] -'0' ) <<6 ) | - ((in[2 ] -'0' ) <<3 ) | - (in[3 ] -'0' )); - in +=4 ; -continue ; - } - } - *out++ = *in++; - } - *out ='\0' ; - -return str; -} -static int check_set (const char **dest,char *src,char *name) {int rc =0 ;
@@ -370,8 +330,10 @@ static int ddebug_parse_query(char *words[], int nwords, }else if (!strcmp (words[i],"module" )) { rc =check_set (&query->module, words[i+1 ],"module" ); }else if (!strcmp (words[i],"format" )) { - rc =check_set (&query->format,unescape (words[i+1 ]), -"format" ); +string_unescape_inplace (words[i+1 ],UNESCAPE_SPACE | +UNESCAPE_OCTAL | +UNESCAPE_SPECIAL ); + rc =check_set (&query->format, words[i+1 ],"format" ); }else if (!strcmp (words[i],"line" )) {char *first = words[i+1 ];char *last =strchr (first,'-' );
Signature · 1 line
1.8.2.rc0.22.gb3600c3
PATCH 4/4 binfmt_misc: reuse string_unescape_inplace()
In reply to Andy ShevchenkoThere is string_unescape_inplace() function which decodes strings in generic way. Let's use it.
--- fs/binfmt_misc.c | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-)
fs/binfmt_misc.c +4 -20
@@ -23,6 +23,7 @@#include <linux/binfmts.h> #include <linux/slab.h> #include <linux/ctype.h> +#include <linux/string_helpers.h> #include <linux/file.h> #include <linux/pagemap.h> #include <linux/namei.h>
@@ -234,24 +235,6 @@ static char *scanarg(char *s, char del)return s; } -static int unquote (char *from) -{ -char c =0 , *s = from, *p = from; - -while ((c = *s++) !='\0' ) { -if (c =='\\' && *s =='x' ) { - s++; - c =toupper (*s++); - *p = (c - (isdigit (c) ?'0' :'A' -10 )) <<4 ; - c =toupper (*s++); - *p++ |= c - (isdigit (c) ?'0' :'A' -10 ); -continue ; - } - *p++ = c; - } -return p - from; -} -static char *check_special_flags (char * sfs,Node * e) {char * p = sfs;
@@ -354,8 +337,9 @@ static Node *create_entry(const char __user *buffer, size_t count) p[-1 ] ='\0' ;if (!e->mask[0 ]) e->mask =NULL ; - e->size =unquote (e->magic); -if (e->mask &&unquote (e->mask) != e->size) + e->size =string_unescape_inplace (e->magic,UNESCAPE_HEX ); +if (e->mask && +string_unescape_inplace (e->mask,UNESCAPE_HEX ) != e->size) goto Einval;if (e->size + e->offset >BINPRM_BUF_SIZE ) goto Einval;
Signature · 1 line
1.8.2.rc0.22.gb3600c3