Skip to contentSkip to conversation

lib: introduce string_escape_mem an %*pE specifier

PATCH Superseded Andy Shevchenko 2 participants 16 messages Jul 7, 2014 – Jul 8, 2014

Subsystems (8)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Andy Shevchenko

author reviewer
#0 lore raw

v2 cover lib: introduce string_escape_mem an %*pE specifier

The introduced function is a kind of opposite to string_unescape. We have several users of such functionality each of them created custom implementation. The series contains clean up of test suite, adding new call, and switching few users to use it via %*pE specifier.

Test suite covers all of existing and most of potential use cases.

Changelog v2:
- addressed Joe's comments:
- added patch 4/10
- moving patches 5/10-10/10 to %*pE instead of direct string_escape_mem call, as a result the series and code looks cleaner
- address few of Andrew's comments:
- added patch 1/10
- kernel documentation moved to c-file
- when test case fails the necessary messages are printed on KERN_WARNING level

Andy Shevchenko (10):
lib / string_helpers: move documentation to c-file lib / string_helpers: refactoring the test suite lib / string_helpers: introduce string_escape_mem() lib/vsprintf: add %*pE[achnops] format specifier
wireless: libertas: print esaped string via %*pE
wireless: ipw2x00: print SSID via %*pE
lib80211: remove unused print_ssid()
staging: wlan-ng: use %*pEhp to print SN
staging: rtl8192e: use %*pEn to escape buffer
staging: rtl8192u: use %*pEn to escape buffer

 Documentation/printk-formats.txt               |  28 +++
 drivers/net/wireless/ipw2x00/ipw2100.c         |  22 +-
 drivers/net/wireless/ipw2x00/ipw2200.c         | 270 +++++++--------------
 drivers/net/wireless/ipw2x00/libipw_rx.c       |  65 +++---
 drivers/net/wireless/ipw2x00/libipw_wx.c       |  16 +-
 drivers/net/wireless/libertas/cfg.c            |   8 +-
 drivers/net/wireless/libertas/mesh.c           |   7 +-
 drivers/staging/rtl8192e/rtllib.h              |  14 +-
 drivers/staging/rtl8192u/ieee80211/ieee80211.h |  14 +-
 drivers/staging/wlan-ng/prism2sta.c            |  28 +--
 include/linux/string_helpers.h                 |  65 +++---
 include/net/lib80211.h                         |   5 -
 lib/string_helpers.c                           | 312 +++++++++++++++++++++++++
 lib/test-string_helpers.c                      | 250 +++++++++++++++++++-
 lib/vsprintf.c                                 |  72 ++++++
 net/wireless/lib80211.c                        |  32 ---
 16 files changed, 817 insertions(+), 391 deletions(-)
Signature · 1 line
2.0.1
author reviewer
#1 lore raw

PATCH v2 1/10 lib / string_helpers: move documentation to c-file

The documentation of API belongs to c-file. This patch moves it accordingly.

There is no functional change.

---
 include/linux/string_helpers.h | 34 ----------------------------------
 lib/string_helpers.c           | 38 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 38 insertions(+), 34 deletions(-)
include/linux/string_helpers.h +0 -34
index 3eeee96..5a30f2a 100644
@@ -20,40 +20,6 @@ int string_get_size(u64 size, enum string_size_units units,
 #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'.
- *
- * Because the size of the output will be the same as or less than the size of
- * the input, the transformation may be performed 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)
lib/string_helpers.c +38 -0
index 29033f3..74ec604 100644
@@ -168,6 +168,44 @@ static bool unescape_special(char **src, char **dst)
 	return true;
 }
 
+/**
+ * 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
+ *
+ * Description:
+ * The function unquotes characters in the given string.
+ *
+ * Because the size of the output will be the same as or less than the size of
+ * the input, the transformation may be performed 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.
+ *
+ * Return:
+ * The amount of the characters processed to the destination buffer excluding
+ * trailing '\0' is returned.
+ */
 int string_unescape(char *src, char *dst, size_t size, unsigned int flags)
 {
 	char *out = dst;
Signature · 1 line
2.0.1
author reviewer
#2 lore raw

PATCH v2 2/10 lib / string_helpers: refactoring the test suite

In reply to Andy Shevchenko

This patch prepares test suite for a following update. It introduces test_string_check_buf() helper which checks the result and dumps an error.

---
 lib/test-string_helpers.c | 39 +++++++++++++++++++++++++++------------
 1 file changed, 27 insertions(+), 12 deletions(-)
lib/test-string_helpers.c +27 -12
index 6ac48de..ac44c92 100644
@@ -10,6 +10,26 @@
 #include <linux/string.h>
 #include <linux/string_helpers.h>
 
+static __init bool test_string_check_buf(const char *name, unsigned int flags,
+					 char *in, size_t p,
+					 char *out_real, size_t q_real,
+					 char *out_test, size_t q_test)
+{
+	if (q_real == q_test && !memcmp(out_test, out_real, q_test))
+		return true;
+
+	pr_warn("Test '%s' failed: flags = %u\n", name, flags);
+
+	print_hex_dump(KERN_WARNING, "Input: ", DUMP_PREFIX_NONE, 16, 1,
+		       in, p, true);
+	print_hex_dump(KERN_WARNING, "Expected: ", DUMP_PREFIX_NONE, 16, 1,
+		       out_test, q_test, true);
+	print_hex_dump(KERN_WARNING, "Got: ", DUMP_PREFIX_NONE, 16, 1,
+		       out_real, q_real, true);
+
+	return false;
+}
+
 struct test_string {
 	const char *in;
 	const char *out;
@@ -39,7 +59,8 @@ static const struct test_string strings[] __initconst = {
 	},
 };
 
-static void __init test_string_unescape(unsigned int flags, bool inplace)
+static void __init test_string_unescape(const char *name, unsigned int flags,
+					bool inplace)
 {
 	char in[256];
 	char out_test[256];
@@ -77,15 +98,8 @@ static void __init test_string_unescape(unsigned int flags, bool inplace)
 		q_real = string_unescape(in, out_real, q_real, flags);
 	}
 
-	if (q_real != q_test || memcmp(out_test, out_real, q_test)) {
-		pr_warn("Test failed: flags = %u\n", flags);
-		print_hex_dump(KERN_WARNING, "Input: ",
-			       DUMP_PREFIX_NONE, 16, 1, in, p - 1, true);
-		print_hex_dump(KERN_WARNING, "Expected: ",
-			       DUMP_PREFIX_NONE, 16, 1, out_test, q_test, true);
-		print_hex_dump(KERN_WARNING, "Got: ",
-			       DUMP_PREFIX_NONE, 16, 1, out_real, q_real, true);
-	}
+	test_string_check_buf(name, flags, in, p - 1, out_real, q_real,
+			      out_test, q_test);
 }
 
 static int __init test_string_helpers_init(void)
@@ -94,8 +108,9 @@ static int __init test_string_helpers_init(void)
 
 	pr_info("Running tests...\n");
 	for (i = 0; i < UNESCAPE_ANY + 1; i++)
-		test_string_unescape(i, false);
-	test_string_unescape(get_random_int() % (UNESCAPE_ANY + 1), true);
+		test_string_unescape("unescape", i, false);
+	test_string_unescape("unescape inplace",
+			     get_random_int() % (UNESCAPE_ANY + 1), true);
 
 	return -EINVAL;
 }
Signature · 1 line
2.0.1
author reviewer
#3 lore raw

PATCH v2 3/10 lib / string_helpers: introduce string_escape_mem()

In reply to Andy Shevchenko

This is almost the opposite function to string_unescape(). Nevertheless it handles \0 and could be used for any byte buffer.

The documentation is supplied together with the function prototype.

The test cases covers most of the scenarios and would be expanded later on.

---
 include/linux/string_helpers.h |  31 +++++
 lib/string_helpers.c           | 274 +++++++++++++++++++++++++++++++++++++++++
 lib/test-string_helpers.c      | 213 ++++++++++++++++++++++++++++++++
 3 files changed, 518 insertions(+)
include/linux/string_helpers.h +31 -0
index 5a30f2a..6eb567a 100644
@@ -37,4 +37,35 @@ static inline int string_unescape_any_inplace(char *buf)
 	return string_unescape_any(buf, buf, 0);
 }
 
+#define ESCAPE_SPACE		0x01
+#define ESCAPE_SPECIAL		0x02
+#define ESCAPE_NULL		0x04
+#define ESCAPE_OCTAL		0x08
+#define ESCAPE_ANY		\
+	(ESCAPE_SPACE | ESCAPE_OCTAL | ESCAPE_SPECIAL | ESCAPE_NULL)
+#define ESCAPE_NP		0x10
+#define ESCAPE_ANY_NP		(ESCAPE_ANY | ESCAPE_NP)
+#define ESCAPE_HEX		0x20
+
+int string_escape_mem(const char *src, size_t isz, char **dst, size_t osz,
+		unsigned int flags, const char *esc);
+
+static inline int string_escape_mem_any_np(const char *src, size_t isz,
+		char **dst, size_t osz, const char *esc)
+{
+	return string_escape_mem(src, isz, dst, osz, ESCAPE_ANY_NP, esc);
+}
+
+static inline int string_escape_str(const char *src, char **dst, size_t sz,
+		unsigned int flags, const char *esc)
+{
+	return string_escape_mem(src, strlen(src), dst, sz, flags, esc);
+}
+
+static inline int string_escape_str_any_np(const char *src, char **dst,
+		size_t sz, const char *esc)
+{
+	return string_escape_str(src, dst, sz, ESCAPE_ANY_NP, esc);
+}
+
 #endif
lib/string_helpers.c +274 -0

283 lines not on this page · raw message

lib/test-string_helpers.c +213 -0

225 lines not on this page · raw message

Signature · 1 line
2.0.1
author reviewer
#4 lore raw

PATCH v2 4/10 lib/vsprintf: add %*pE[achnops] format specifier

In reply to Andy Shevchenko

This allows user to print a given buffer as esaped string. The rules applied accordingly to the mix of the flags provided by additional format letters.

For example, if the given buffer:

	1b 62 20 5c 43 07 22 90 0d 5d

The result strings could be:

	%*pE            "\eb \C\a"\220\r]"
	%*pEhp          "\x1bb \C\x07"\x90\x0d]"
	%*pEa           "\e\142\040\\\103\a\042\220\r\135"

Please, read Documentation/printk-formats.txt and lib/string_helpers.c kernel documentation to get further information.

---
 Documentation/printk-formats.txt | 28 ++++++++++++++++
 lib/vsprintf.c                   | 72 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 100 insertions(+)
Documentation/printk-formats.txt +28 -0
index 3b56a99..e2dc566 100644
@@ -70,6 +70,34 @@ DMA addresses types dma_addr_t:
 	For printing a dma_addr_t type which can vary based on build options,
 	regardless of the width of the CPU data path. Passed by reference.
 
+Raw buffer as an escaped string:
+
+	%*pE[achnops]
+
+	For printing raw buffer as an escaped string. For the following buffer
+
+		1b 62 20 5c 43 07 22 90 0d 5d
+
+	few examples show how the conversion could be done (the result string
+	without embraced quotes):
+
+		%*pE		"\eb \C\a"\220\r]"
+		%*pEhp		"\x1bb \C\x07"\x90\x0d]"
+		%*pEa		"\e\142\040\\\103\a\042\220\r\135"
+
+	The converion rules are defined by combination of the following flags
+	(see string_escape_mem() kernel documentation for the details):
+		a - ESCAPE_ANY
+		c - ESCAPE_SPECIAL
+		h - ESCAPE_HEX
+		n - ESCAPE_NULL
+		o - ESCAPE_OCTAL
+		p - ESCAPE_NP
+		s - ESCAPE_SPACE
+	By default ESCAPE_ANY_NP is used.
+
+	If field width is ommited the 1 byte only will be escaped.
+
 Raw buffer as a hex string:
 	%*ph	00 01 02  ...  3f
 	%*phC	00:01:02: ... :3f
lib/vsprintf.c +72 -0

102 lines not on this page · raw message

Signature · 1 line
2.0.1
#5 lore raw

Re: [PATCH v2 04/10] lib/vsprintf: add %*pE[achnops] format specifier

[trivial notes]

Quoted 1 line · Andy Shevchenko

This allows user to print a given buffer as esaped string. The rules applied

as an escaped

Quoted 1 line

accordingly to the mix of the flags provided by additional format letters.

rules are applied according to an optional mix of flags

Quoted 5 lines

For example, if the given buffer:

	1b 62 20 5c 43 07 22 90 0d 5d

The result strings could be:

would be

Quoted 1 line
	%*pE            "\eb \C\a"\220\r]"

Maybe say something about ssid's here.
or maybe add an %*pES just for the ssid type.

Documentation/printk-formats.txt

Quoted 1 line
Documentation/printk-formats.txt +0 -0

[]

Quoted 4 lines
Documentation/printk-formats.txt +0 -0
@@ -70,6 +70,34 @@ DMA addresses types dma_addr_t:
 	For printing a dma_addr_t type which can vary based on build options,
 	regardless of the width of the CPU data path. Passed by reference.
 

Documentation/printk-formats.txt DMA addresses types dma_addr_t: · line 82

Quoted 10 lines
Documentation/printk-formats.txt +10 -0
+Raw buffer as an escaped string:
+
+	%*pE[achnops]
+
+	For printing raw buffer as an escaped string. For the following buffer
+
+		1b 62 20 5c 43 07 22 90 0d 5d
+
+	few examples show how the conversion could be done (the result string
+	without embraced quotes):

'embraced' is a bit of an awkward word choice. Maybe surrounding.

Documentation/printk-formats.txt DMA addresses types dma_addr_t: · line 84

Quoted 2 lines
Documentation/printk-formats.txt +2 -0
+
+		%*pE		"\eb \C\a"\220\r]"

Oh, the initial commit log comments above are really for this block

Documentation/printk-formats.txt DMA addresses types dma_addr_t: · line 88

Quoted 4 lines
Documentation/printk-formats.txt +4 -0
+		%*pEhp		"\x1bb \C\x07"\x90\x0d]"
+		%*pEa		"\e\142\040\\\103\a\042\220\r\135"
+
+	The converion rules are defined by combination of the following flags

conversion

Documentation/printk-formats.txt DMA addresses types dma_addr_t: · line 99

Quoted 11 lines
Documentation/printk-formats.txt +11 -0
+	(see string_escape_mem() kernel documentation for the details):
+		a - ESCAPE_ANY
+		c - ESCAPE_SPECIAL
+		h - ESCAPE_HEX
+		n - ESCAPE_NULL
+		o - ESCAPE_OCTAL
+		p - ESCAPE_NP
+		s - ESCAPE_SPACE
+	By default ESCAPE_ANY_NP is used.
+
+	If field width is ommited the 1 byte only will be escaped.

omitted

author reviewer
#6 lore raw

Re: [PATCH v2 04/10] lib/vsprintf: add %*pE[achnops] format specifier

Quoted 1 line · Joe Perches

[trivial notes]

Thanks! Fixed locally, though am waiting for few more days if any other comment comes.

Quoted 14 lines · Andy Shevchenko
Quoted 1 line

This allows user to print a given buffer as esaped string. The rules applied

as an escaped

Quoted 1 line

accordingly to the mix of the flags provided by additional format letters.

rules are applied according to an optional mix of flags

Quoted 4 lines

For example, if the given buffer:

	1b 62 20 5c 43 07 22 90 0d 5d
Quoted 7 lines
Quoted 1 line

The result strings could be:

would be

Quoted 1 line
	%*pE            "\eb \C\a"\220\r]"

Maybe say something about ssid's here.

Mentioned in the printk-formats.txt part.

Quoted 1 line

or maybe add an %*pES just for the ssid type.

I gave a thought to it, and it seems to me we will increase complexity of the code to get none beneficial. The SSID in most cases has a variable-length, thus we will pass it anyway, what else could be differ here? Default set of flags?

Quoted 20 lines

Documentation/printk-formats.txt

Quoted 2 lines
Documentation/printk-formats.txt +0 -0

[]

Quoted 4 lines
Documentation/printk-formats.txt +0 -0
@@ -70,6 +70,34 @@ DMA addresses types dma_addr_t:
 	For printing a dma_addr_t type which can vary based on build options,
 	regardless of the width of the CPU data path. Passed by reference.
 

Documentation/printk-formats.txt DMA addresses types dma_addr_t: · line 82

Quoted 10 lines
Documentation/printk-formats.txt +10 -0
+Raw buffer as an escaped string:
+
+	%*pE[achnops]
+
+	For printing raw buffer as an escaped string. For the following buffer
+
+		1b 62 20 5c 43 07 22 90 0d 5d
+
+	few examples show how the conversion could be done (the result string
+	without embraced quotes):

'embraced' is a bit of an awkward word choice. Maybe surrounding.

Quoted 4 lines

Documentation/printk-formats.txt DMA addresses types dma_addr_t: · line 84

Quoted 2 lines
Documentation/printk-formats.txt +2 -0
+
+		%*pE		"\eb \C\a"\220\r]"

Oh, the initial commit log comments above are really for this block

Phrasing is a bit different, though I rephrased above sentence.

Quoted 23 lines

Documentation/printk-formats.txt DMA addresses types dma_addr_t: · line 88

Quoted 5 lines
Documentation/printk-formats.txt +4 -0
+		%*pEhp		"\x1bb \C\x07"\x90\x0d]"
+		%*pEa		"\e\142\040\\\103\a\042\220\r\135"
+
+	The converion rules are defined by combination of the following flags

conversion

Documentation/printk-formats.txt DMA addresses types dma_addr_t: · line 99

Quoted 11 lines
Documentation/printk-formats.txt +11 -0
+	(see string_escape_mem() kernel documentation for the details):
+		a - ESCAPE_ANY
+		c - ESCAPE_SPECIAL
+		h - ESCAPE_HEX
+		n - ESCAPE_NULL
+		o - ESCAPE_OCTAL
+		p - ESCAPE_NP
+		s - ESCAPE_SPACE
+	By default ESCAPE_ANY_NP is used.
+
+	If field width is ommited the 1 byte only will be escaped.

omitted

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
#7 lore raw

Re: [PATCH v2 04/10] lib/vsprintf: add %*pE[achnops] format specifier

In reply to Andy Shevchenko

lib/vsprintf.c

Quoted 1 line · Andy Shevchenko
lib/vsprintf.c +0 -0

[]

lib/vsprintf.c char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa, · line 1106

Quoted 3 lines

static noinline_for_stack

lib/vsprintf.c +2 -0
+char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
+		     const char *fmt)

[]

lib/vsprintf.c char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa, · line 1115

Quoted 3 lines
lib/vsprintf.c +3 -0
+	if (spec.field_width == 0)
+		/* nothing to print */
+		return buf;

[]

lib/vsprintf.c char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa, · line 1153

Quoted 1 line
lib/vsprintf.c +1 -0
+	len = spec.field_width < 0 ? 1 : spec.field_width;

If field_width is not specified, emit a single byte?

Perhaps better if -1 was accepted by string_escape_mem as a strlen request or just ignored as a 0 length is above.

fyi: hex_string emits nothing on printk("%ph", buf);

author reviewer
#8 lore raw

Re: [PATCH v2 04/10] lib/vsprintf: add %*pE[achnops] format specifier

Quoted 8 lines · Joe Perches

lib/vsprintf.c

Quoted 1 line · Andy Shevchenko
lib/vsprintf.c +0 -0

[]

lib/vsprintf.c char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa, · line 1106

Quoted 3 lines

static noinline_for_stack

lib/vsprintf.c +2 -0
+char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
+		     const char *fmt)

[]

Quoted 7 lines

lib/vsprintf.c char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa, · line 1115

Quoted 3 lines
lib/vsprintf.c +3 -0
+	if (spec.field_width == 0)
+		/* nothing to print */
+		return buf;

[]

lib/vsprintf.c char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa, · line 1153

Quoted 1 line
lib/vsprintf.c +1 -0
+	len = spec.field_width < 0 ? 1 : spec.field_width;

If field_width is not specified, emit a single byte?

A single byte to be escaped (it might be \000, for example, on output).

Quoted 3 lines

Perhaps better if -1 was accepted by string_escape_mem as a strlen request or just ignored as a 0 length is above.

Would it be any benefit here?

Quoted 1 line

fyi: hex_string emits nothing on printk("%ph", buf);

Works just fine. How did you check it?

pr_info("%ph\n", in); => test_string_helpers: 1b

(I suppose "\n" flushes a buffer for me.)

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
#9 lore raw

Re: [PATCH v2 04/10] lib/vsprintf: add %*pE[achnops] format specifier

Quoted 6 lines · Andy Shevchenko
Quoted 3 lines · Joe Perches

Perhaps better if -1 was accepted by string_escape_mem as a strlen request or just ignored as a 0 length is above.

Would it be any benefit here?

Dunno, it was just a thought for you.

Quoted 3 lines
Quoted 1 line

fyi: hex_string emits nothing on printk("%ph", buf);

Works just fine. How did you check it?

By not reading the code correctly.
So what you have is fine.

author reviewer
#10 lore raw

PATCH v2 5/10 wireless: libertas: print esaped string via %*pE

In reply to Andy Shevchenko

Instead of custom approach this allows to print escaped strings via recently added kernel extension: %*pE.

---
 drivers/net/wireless/libertas/cfg.c  | 8 ++------
 drivers/net/wireless/libertas/mesh.c | 7 +++----
 2 files changed, 5 insertions(+), 10 deletions(-)
drivers/net/wireless/libertas/cfg.c +2 -6
index 47a998d..1188814 100644
@@ -590,7 +590,6 @@ static int lbs_ret_scan(struct lbs_private *priv, unsigned long dummy,
 		int chan_no = -1;
 		const u8 *ssid = NULL;
 		u8 ssid_len = 0;
-		DECLARE_SSID_BUF(ssid_buf);
 
 		int len = get_unaligned_le16(pos);
 		pos += 2;
@@ -644,10 +643,8 @@ static int lbs_ret_scan(struct lbs_private *priv, unsigned long dummy,
 			struct ieee80211_channel *channel =
 				ieee80211_get_channel(wiphy, freq);
 
-			lbs_deb_scan("scan: %pM, capa %04x, chan %2d, %s, "
-				     "%d dBm\n",
-				     bssid, capa, chan_no,
-				     print_ssid(ssid_buf, ssid, ssid_len),
+			lbs_deb_scan("scan: %pM, capa %04x, chan %2d, %*pE, %d dBm\n",
+				     bssid, capa, chan_no, ssid_len, ssid,
 				     LBS_SCAN_RSSI_TO_MBM(rssi)/100);
 
 			if (channel &&
@@ -1982,7 +1979,6 @@ static int lbs_join_ibss(struct wiphy *wiphy, struct net_device *dev,
 	struct lbs_private *priv = wiphy_priv(wiphy);
 	int ret = 0;
 	struct cfg80211_bss *bss;
-	DECLARE_SSID_BUF(ssid_buf);
 
 	if (dev == priv->mesh_dev)
 		return -EOPNOTSUPP;
drivers/net/wireless/libertas/mesh.c +3 -4
index 6fef746..b211ff8 100644
@@ -93,7 +93,6 @@ static int lbs_mesh_config(struct lbs_private *priv, uint16_t action,
 {
 	struct cmd_ds_mesh_config cmd;
 	struct mrvl_meshie *ie;
-	DECLARE_SSID_BUF(ssid);
 
 	memset(&cmd, 0, sizeof(cmd));
 	cmd.channel = cpu_to_le16(chan);
@@ -122,9 +121,9 @@ static int lbs_mesh_config(struct lbs_private *priv, uint16_t action,
 	default:
 		return -1;
 	}
-	lbs_deb_cmd("mesh config action %d type %x channel %d SSID %s\n",
-		    action, priv->mesh_tlv, chan,
-		    print_ssid(ssid, priv->mesh_ssid, priv->mesh_ssid_len));
+	lbs_deb_cmd("mesh config action %d type %x channel %d SSID %*pE\n",
+		    action, priv->mesh_tlv, chan, priv->mesh_ssid_len,
+		    priv->mesh_ssid);
 
 	return __lbs_mesh_config_send(priv, &cmd, action, priv->mesh_tlv);
 }
Signature · 1 line
2.0.1
author reviewer
#11 lore raw

PATCH v2 6/10 wireless: ipw2x00: print SSID via %*pE

In reply to Andy Shevchenko

Instead of custom approach this allows to print escaped strings via recently added kernel extension: %*pE.

---
 drivers/net/wireless/ipw2x00/ipw2100.c   |  22 +--
 drivers/net/wireless/ipw2x00/ipw2200.c   | 270 ++++++++++---------------------
 drivers/net/wireless/ipw2x00/libipw_rx.c |  65 +++-----
 drivers/net/wireless/ipw2x00/libipw_wx.c |  16 +-
 4 files changed, 126 insertions(+), 247 deletions(-)
drivers/net/wireless/ipw2x00/ipw2100.c +7 -15
index 1ab8e50..aac2953 100644
@@ -2005,7 +2005,6 @@ static void isr_indicate_associated(struct ipw2100_priv *priv, u32 status)
 	u32 chan;
 	char *txratename;
 	u8 bssid[ETH_ALEN];
-	DECLARE_SSID_BUF(ssid);
 
 	/*
 	 * TBD: BSSID is usually 00:00:00:00:00:00 here and not
@@ -2067,8 +2066,8 @@ static void isr_indicate_associated(struct ipw2100_priv *priv, u32 status)
 		break;
 	}
 
-	IPW_DEBUG_INFO("%s: Associated with '%s' at %s, channel %d (BSSID=%pM)\n",
-		       priv->net_dev->name, print_ssid(ssid, essid, essid_len),
+	IPW_DEBUG_INFO("%s: Associated with '%*pE' at %s, channel %d (BSSID=%pM)\n",
+		       priv->net_dev->name, essid_len, essid,
 		       txratename, chan, bssid);
 
 	/* now we copy read ssid into dev */
@@ -2095,9 +2094,8 @@ static int ipw2100_set_essid(struct ipw2100_priv *priv, char *essid,
 		.host_command_length = ssid_len
 	};
 	int err;
-	DECLARE_SSID_BUF(ssid);
 
-	IPW_DEBUG_HC("SSID: '%s'\n", print_ssid(ssid, essid, ssid_len));
+	IPW_DEBUG_HC("SSID: '%*pE'\n", ssid_len, essid);
 
 	if (ssid_len)
 		memcpy(cmd.host_command_parameters, essid, ssid_len);
@@ -2138,11 +2136,8 @@ static int ipw2100_set_essid(struct ipw2100_priv *priv, char *essid,
 
 static void isr_indicate_association_lost(struct ipw2100_priv *priv, u32 status)
 {
-	DECLARE_SSID_BUF(ssid);
-
 	IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE | IPW_DL_ASSOC,
-		  "disassociated: '%s' %pM\n",
-		  print_ssid(ssid, priv->essid, priv->essid_len),
+		  "disassociated: '%*pE' %pM\n", priv->essid_len, priv->essid,
 		  priv->bssid);
 
 	priv->status &= ~(STATUS_ASSOCIATED | STATUS_ASSOCIATING);
@@ -6975,7 +6970,6 @@ static int ipw2100_wx_set_essid(struct net_device *dev,
 	char *essid = "";	/* ANY */
 	int length = 0;
 	int err = 0;
-	DECLARE_SSID_BUF(ssid);
 
 	mutex_lock(&priv->action_mutex);
 	if (!(priv->status & STATUS_INITIALIZED)) {
@@ -7005,8 +6999,7 @@ static int ipw2100_wx_set_essid(struct net_device *dev,
 		goto done;
 	}
 
-	IPW_DEBUG_WX("Setting ESSID: '%s' (%d)\n",
-		     print_ssid(ssid, essid, length), length);
+	IPW_DEBUG_WX("Setting ESSID: '%*pE' (%d)\n", length, essid, length);
 
 	priv->essid_len = length;
 	memcpy(priv->essid, essid, priv->essid_len);
@@ -7027,13 +7020,12 @@ static int ipw2100_wx_get_essid(struct net_device *dev,
 	 */
 
 	struct ipw2100_priv *priv = libipw_priv(dev);
-	DECLARE_SSID_BUF(ssid);
 
 	/* If we are associated, trying to associate, or have a statically
 	 * configured ESSID then return that; otherwise return ANY */
 	if (priv->config & CFG_STATIC_ESSID || priv->status & STATUS_ASSOCIATED) {
-		IPW_DEBUG_WX("Getting essid: '%s'\n",
-			     print_ssid(ssid, priv->essid, priv->essid_len));
+		IPW_DEBUG_WX("Getting essid: '%*pE'\n",
+			     priv->essid_len, priv->essid);
 		memcpy(extra, priv->essid, priv->essid_len);
 		wrqu->essid.length = priv->essid_len;
 		wrqu->essid.flags = 1;	/* active */
drivers/net/wireless/ipw2x00/ipw2200.c +87 -183

557 lines not on this page · raw message

drivers/net/wireless/ipw2x00/libipw_rx.c +26 -39

119 lines not on this page · raw message

drivers/net/wireless/ipw2x00/libipw_wx.c +6 -10
index 54aba47..dd29f46 100644
@@ -272,7 +272,6 @@ int libipw_wx_get_scan(struct libipw_device *ieee,
 	char *ev = extra;
 	char *stop = ev + wrqu->data.length;
 	int i = 0;
-	DECLARE_SSID_BUF(ssid);
 
 	LIBIPW_DEBUG_WX("Getting scan\n");
 
@@ -290,12 +289,10 @@ int libipw_wx_get_scan(struct libipw_device *ieee,
 			ev = libipw_translate_scan(ieee, ev, stop, network,
 						      info);
 		else {
-			LIBIPW_DEBUG_SCAN("Not showing network '%s ("
-					     "%pM)' due to age (%ums).\n",
-					     print_ssid(ssid, network->ssid,
-							 network->ssid_len),
-					     network->bssid,
-					     elapsed_jiffies_msecs(
+			LIBIPW_DEBUG_SCAN("Not showing network '%*pE (%pM)' due to age (%ums).\n",
+					  network->ssid_len, network->ssid,
+					  network->bssid,
+					  elapsed_jiffies_msecs(
 					               network->last_scanned));
 		}
 	}
@@ -322,7 +319,6 @@ int libipw_wx_set_encode(struct libipw_device *ieee,
 	int i, key, key_provided, len;
 	struct lib80211_crypt_data **crypt;
 	int host_crypto = ieee->host_encrypt || ieee->host_decrypt;
-	DECLARE_SSID_BUF(ssid);
 
 	LIBIPW_DEBUG_WX("SET_ENCODE\n");
 
@@ -417,8 +413,8 @@ int libipw_wx_set_encode(struct libipw_device *ieee,
 		if (len > erq->length)
 			memset(sec.keys[key] + erq->length, 0,
 			       len - erq->length);
-		LIBIPW_DEBUG_WX("Setting key %d to '%s' (%d:%d bytes)\n",
-				   key, print_ssid(ssid, sec.keys[key], len),
+		LIBIPW_DEBUG_WX("Setting key %d to '%*pE' (%d:%d bytes)\n",
+				   key, len, sec.keys[key],
 				   erq->length, len);
 		sec.key_sizes[key] = len;
 		if (*crypt)
Signature · 1 line
2.0.1
author reviewer
#12 lore raw

PATCH v2 7/10 lib80211: remove unused print_ssid()

In reply to Andy Shevchenko

In kernel we have %*pE specifier to print an escaped buffer. All users now switched to that approach.

This fixes a bug as well. The current implementation wrongly prints octal
numbers: only two first digits are used in case when 3 are required and the rest of the string ends up cut off.

Additionally by default the \f, \v, \a, and \e are escaped to their alphabetic representation. It's safe to do since it is currently used for messaging only.

---
 include/net/lib80211.h  |  5 -----
 net/wireless/lib80211.c | 32 --------------------------------
 2 files changed, 37 deletions(-)
include/net/lib80211.h +0 -5
index be95b92..aab0f42 100644
@@ -32,11 +32,6 @@
 #include <linux/timer.h>
 #include <linux/seq_file.h>
 
-/* print_ssid() is intended to be used in debug (and possibly error)
- * messages. It should never be used for passing ssid to user space. */
-const char *print_ssid(char *buf, const char *ssid, u8 ssid_len);
-#define DECLARE_SSID_BUF(var) char var[IEEE80211_MAX_SSID_LEN * 4 + 1] __maybe_unused
-
 #define NUM_WEP_KEYS	4
 
 enum {
net/wireless/lib80211.c +0 -32
index a55c27b..4596115 100644
@@ -46,38 +46,6 @@ static void lib80211_crypt_deinit_entries(struct lib80211_crypt_info *info,
 static void lib80211_crypt_quiescing(struct lib80211_crypt_info *info);
 static void lib80211_crypt_deinit_handler(unsigned long data);
 
-const char *print_ssid(char *buf, const char *ssid, u8 ssid_len)
-{
-	const char *s = ssid;
-	char *d = buf;
-
-	ssid_len = min_t(u8, ssid_len, IEEE80211_MAX_SSID_LEN);
-	while (ssid_len--) {
-		if (isprint(*s)) {
-			*d++ = *s++;
-			continue;
-		}
-
-		*d++ = '\\';
-		if (*s == '\0')
-			*d++ = '0';
-		else if (*s == '\n')
-			*d++ = 'n';
-		else if (*s == '\r')
-			*d++ = 'r';
-		else if (*s == '\t')
-			*d++ = 't';
-		else if (*s == '\\')
-			*d++ = '\\';
-		else
-			d += snprintf(d, 3, "%03o", *s);
-		s++;
-	}
-	*d = '\0';
-	return buf;
-}
-EXPORT_SYMBOL(print_ssid);
-
 int lib80211_crypt_info_init(struct lib80211_crypt_info *info, char *name,
 				spinlock_t *lock)
 {
Signature · 1 line
2.0.1
author reviewer
#13 lore raw

PATCH v2 8/10 staging: wlan-ng: use %*pEhp to print SN

In reply to Andy Shevchenko

This is a generic specifier to print an escaped buffer by given criteria. Let's use it instead of custom approach.

---
 drivers/staging/wlan-ng/prism2sta.c | 28 ++--------------------------
 1 file changed, 2 insertions(+), 26 deletions(-)
drivers/staging/wlan-ng/prism2sta.c +2 -26
index 209e4db..688e827 100644
@@ -60,7 +60,6 @@
 #include <linux/netdevice.h>
 #include <linux/workqueue.h>
 #include <linux/byteorder/generic.h>
-#include <linux/ctype.h>
 
 #include <linux/io.h>
 #include <linux/delay.h>
@@ -81,27 +80,6 @@
 #include "hfa384x.h"
 #include "prism2mgmt.h"
 
-/* Create a string of printable chars from something that might not be */
-/* It's recommended that the str be 4*len + 1 bytes long */
-#define wlan_mkprintstr(buf, buflen, str, strlen) \
-{ \
-	int i = 0; \
-	int j = 0; \
-	memset(str, 0, (strlen)); \
-	for (i = 0; i < (buflen); i++) { \
-		if (isprint((buf)[i])) { \
-			(str)[j] = (buf)[i]; \
-			j++; \
-		} else { \
-			(str)[j] = '\\'; \
-			(str)[j+1] = 'x'; \
-			(str)[j+2] = hex_asc_hi((buf)[i]); \
-			(str)[j+3] = hex_asc_lo((buf)[i]); \
-			j += 4; \
-		} \
-	} \
-}
-
 static char *dev_info = "prism2_usb";
 static wlandevice_t *create_wlan(void);
 
@@ -607,7 +585,6 @@ static int prism2sta_getcardinfo(wlandevice_t *wlandev)
 	hfa384x_t *hw = (hfa384x_t *) wlandev->priv;
 	u16 temp;
 	u8 snum[HFA384x_RID_NICSERIALNUMBER_LEN];
-	char pstr[(HFA384x_RID_NICSERIALNUMBER_LEN * 4) + 1];
 
 	/* Collect version and compatibility info */
 	/*  Some are critical, some are not */
@@ -862,9 +839,8 @@ static int prism2sta_getcardinfo(wlandevice_t *wlandev)
 	result = hfa384x_drvr_getconfig(hw, HFA384x_RID_NICSERIALNUMBER,
 					snum, HFA384x_RID_NICSERIALNUMBER_LEN);
 	if (!result) {
-		wlan_mkprintstr(snum, HFA384x_RID_NICSERIALNUMBER_LEN,
-				pstr, sizeof(pstr));
-		netdev_info(wlandev->netdev, "Prism2 card SN: %s\n", pstr);
+		netdev_info(wlandev->netdev, "Prism2 card SN: %*pEhp\n",
+			    HFA384x_RID_NICSERIALNUMBER_LEN, snum);
 	} else {
 		netdev_err(wlandev->netdev, "Failed to retrieve Prism2 Card SN\n");
 		goto failed;
Signature · 1 line
2.0.1
author reviewer
#14 lore raw

PATCH v2 9/10 staging: rtl8192e: use %*pEn to escape buffer

In reply to Andy Shevchenko

Let's use kernel's native specifier to escape a buffer.

---
 drivers/staging/rtl8192e/rtllib.h | 14 +-------------
 1 file changed, 1 insertion(+), 13 deletions(-)
drivers/staging/rtl8192e/rtllib.h +1 -13
index 83f5f57..9840be0 100644
@@ -2956,25 +2956,13 @@ extern inline int rtllib_get_scans(struct rtllib_device *ieee)
 static inline const char *escape_essid(const char *essid, u8 essid_len)
 {
 	static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
-	const char *s = essid;
-	char *d = escaped;
 
 	if (rtllib_is_empty_essid(essid, essid_len)) {
 		memcpy(escaped, "<hidden>", sizeof("<hidden>"));
 		return escaped;
 	}
 
-	essid_len = min(essid_len, (u8)IW_ESSID_MAX_SIZE);
-	while (essid_len--) {
-		if (*s == '\0') {
-			*d++ = '\\';
-			*d++ = '0';
-			s++;
-		} else {
-			*d++ = *s++;
-		}
-	}
-	*d = '\0';
+	snprintf(escaped, sizeof(escaped), "%*pEn", essid_len, essid);
 	return escaped;
 }
 
Signature · 1 line
2.0.1
author reviewer
#15 lore raw

PATCH v2 10/10 staging: rtl8192u: use %*pEn to escape buffer

In reply to Andy Shevchenko

Let's use kernel's native specifier to escape a buffer.

---
 drivers/staging/rtl8192u/ieee80211/ieee80211.h | 14 +-------------
 1 file changed, 1 insertion(+), 13 deletions(-)
drivers/staging/rtl8192u/ieee80211/ieee80211.h +1 -13
index 1040bab..1a265f9 100644
@@ -2576,25 +2576,13 @@ static inline int ieee80211_get_scans(struct ieee80211_device *ieee)
 
 static inline const char *escape_essid(const char *essid, u8 essid_len) {
 	static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
-	const char *s = essid;
-	char *d = escaped;
 
 	if (ieee80211_is_empty_essid(essid, essid_len)) {
 		memcpy(escaped, "<hidden>", sizeof("<hidden>"));
 		return escaped;
 	}
 
-	essid_len = min(essid_len, (u8)IW_ESSID_MAX_SIZE);
-	while (essid_len--) {
-		if (*s == '\0') {
-			*d++ = '\\';
-			*d++ = '0';
-			s++;
-		} else {
-			*d++ = *s++;
-		}
-	}
-	*d = '\0';
+	snprintf(escaped, sizeof(escaped), "%*pEn", essid_len, essid);
 	return escaped;
 }
 
Signature · 1 line
2.0.1