Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1459934 > unrolled thread

[PATCH v5 0/7] lib: string: add functions to case-convert strings

Started byMarkus Mayer <mmayer@broadcom.com>
First post2016-08-10 23:10 +0200
Last post2016-08-21 23:30 +0200
Articles 7 — 2 participants

Back to article view | Back to linux.kernel


Contents

  [PATCH v5 0/7] lib: string: add functions to case-convert strings Markus Mayer <mmayer@broadcom.com> - 2016-08-10 23:10 +0200
    [PATCH v5 2/7] drm/nouveau/core: make use of new strlcpytolower() function Markus Mayer <mmayer@broadcom.com> - 2016-08-10 23:10 +0200
    [PATCH v5 5/7] iscsi-target: replace iscsi_initiatorname_tolower() with strtolower() Markus Mayer <mmayer@broadcom.com> - 2016-08-10 23:10 +0200
    [PATCH v5 1/7] lib: string: add functions to case-convert strings Markus Mayer <mmayer@broadcom.com> - 2016-08-10 23:10 +0200
    [PATCH v5 6/7] drm/nouveau/fifo/gk104: make use of new strcpytoupper() function Markus Mayer <mmayer@broadcom.com> - 2016-08-10 23:10 +0200
    [PATCH v5 4/7] staging: speakup: replace spk_strlwr() with strlcpytolower() Markus Mayer <mmayer@broadcom.com> - 2016-08-10 23:10 +0200
      Re: [PATCH v5 4/7] staging: speakup: replace spk_strlwr() with  strlcpytolower() Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2016-08-21 23:30 +0200

#1459934 — [PATCH v5 0/7] lib: string: add functions to case-convert strings

FromMarkus Mayer <mmayer@broadcom.com>
Date2016-08-10 23:10 +0200
Subject[PATCH v5 0/7] lib: string: add functions to case-convert strings
Message-ID<s4IVb-204-3@gated-at.bofh.it>
This series introduces a family of generic string case conversion
functions. This kind of functionality is needed in several places in
the kernel. Right now, everybody seems to be implementing their own
copy of this functionality.

Based on the discussion of the previous version of this series[1] and
the use cases found in the kernel, it does look like having several
flavours of case conversion functions is beneficial. The use cases fall
into three categories:
    - copying a string and converting the case while specifying a
      maximum length to mimic strlcpy()
    - copying a string and converting the case without specifying a
      length to mimic strcpy()
    - converting the case of a string in-place (i.e. modifying the
      string that was passed in)

Consequently, I am proposing these new functions:
    int strlcpytoupper(char *dst, const char *src, size_t len);
    int strlcpytolower(char *dst, const char *src, size_t len);
    void strcpytoupper(char *dst, const char *src);
    void strcpytolower(char *dst, const char *src);
    void strtoupper(char *s);
    void strtolower(char *s);

Several drivers are being modified to make use of the functions above.
Another driver that also makes use of this functionality will be
submitted upstream shortly, which prompted this whole exercise.

The changes made here have been compile-tested, but not tried out, due
to lack of required hardware.

Changes since v4:
  - rebased on 4.8-rc1; this only affects patch 6/7 (gk104) and only the
    patch context, not the patch itself
  - added an ACK to patch 4/7 (speakup)
  - added an ACK to patch 7/7 (power_supply)

Changes since v3:
  - strlcpytoupper() and strlcpytolower() return length of destination
    or -E2BIG (see [2])
  - we use ~(size_t)0 instead of -1 to copy strings of arbitrary length
    in strcpyto*() and strto*()
  - A few ACKs added

Changes since v2:
  - use strlcpy() semantics not strncpy() semantics, i.e. guarantee
    NULL termination
  - as a result strncpyto<upper|lower> are now called
    strlcpyto<upper|lower>
  - make functions void
  - use len == -1 (SIZE_MAX) as no-limit indicator rather then len == 0
  - change PATCH 2/7 to match strlcpy() semantics
  - change PATCH 4/7 to match strlcpy() semantics

Changes since v1:
  - expanded strtolower() into a family of functions that cover use
    cases when a length argument is or isn't required and that support
    copying the string into a new buffer or changing it in-place 
  - changed the function semantics to return a pointer to the
    terminating '\0' character of the modified string
  - added strtoupper() functionality mirroring the above
  - dropped the ACPICA patch, since that code is OS independent and
    can't rely on a Linux library function (see [3])
  - Added two new patches replacing strtoupper() implementations

[1] https://lkml.org/lkml/2016/6/30/727
[2] https://lkml.org/lkml/2016/7/10/4
[3] https://lkml.org/lkml/2016/7/1/9

Markus Mayer (7):
  lib: string: add functions to case-convert strings
  drm/nouveau/core: make use of new strlcpytolower() function
  ACPI / device_sysfs: make use of new strtolower() function
  staging: speakup: replace spk_strlwr() with strlcpytolower()
  iscsi-target: replace iscsi_initiatorname_tolower() with strtolower()
  drm/nouveau/fifo/gk104: make use of new strcpytoupper() function
  power_supply: make use of new strcpytoupper() function

 drivers/acpi/device_sysfs.c                      |  4 +--
 drivers/gpu/drm/nouveau/nvkm/core/firmware.c     |  9 +----
 drivers/gpu/drm/nouveau/nvkm/engine/fifo/gk104.c |  5 +--
 drivers/power/power_supply_sysfs.c               | 13 +++----
 drivers/staging/speakup/kobjects.c               |  3 +-
 drivers/staging/speakup/main.c                   |  3 +-
 drivers/staging/speakup/speakup.h                |  1 -
 drivers/staging/speakup/varhandlers.c            | 12 -------
 drivers/target/iscsi/iscsi_target_nego.c         | 17 +--------
 include/linux/string.h                           | 40 +++++++++++++++++++++
 lib/string.c                                     | 46 ++++++++++++++++++++++++
 11 files changed, 98 insertions(+), 55 deletions(-)

-- 
2.7.4

[toc] | [next] | [standalone]


#1459935 — [PATCH v5 2/7] drm/nouveau/core: make use of new strlcpytolower() function

FromMarkus Mayer <mmayer@broadcom.com>
Date2016-08-10 23:10 +0200
Subject[PATCH v5 2/7] drm/nouveau/core: make use of new strlcpytolower() function
Message-ID<s4IVc-204-17@gated-at.bofh.it>
In reply to#1459934
Call strlcpytolower() rather than copying the string explicitly and
then walking it to convert it to lowercase.

Signed-off-by: Markus Mayer <mmayer@broadcom.com>
---
 drivers/gpu/drm/nouveau/nvkm/core/firmware.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)

diff --git a/drivers/gpu/drm/nouveau/nvkm/core/firmware.c b/drivers/gpu/drm/nouveau/nvkm/core/firmware.c
index 34ecd4a..982601e 100644
--- a/drivers/gpu/drm/nouveau/nvkm/core/firmware.c
+++ b/drivers/gpu/drm/nouveau/nvkm/core/firmware.c
@@ -36,16 +36,9 @@ nvkm_firmware_get(struct nvkm_device *device, const char *fwname,
 {
 	char f[64];
 	char cname[16];
-	int i;
 
 	/* Convert device name to lowercase */
-	strncpy(cname, device->chip->name, sizeof(cname));
-	cname[sizeof(cname) - 1] = '\0';
-	i = strlen(cname);
-	while (i) {
-		--i;
-		cname[i] = tolower(cname[i]);
-	}
+	strlcpytolower(cname, device->chip->name, sizeof(cname));
 
 	snprintf(f, sizeof(f), "nvidia/%s/%s.bin", cname, fwname);
 	return request_firmware(fw, f, device->dev);
-- 
2.7.4

[toc] | [prev] | [next] | [standalone]


#1459936 — [PATCH v5 5/7] iscsi-target: replace iscsi_initiatorname_tolower() with strtolower()

FromMarkus Mayer <mmayer@broadcom.com>
Date2016-08-10 23:10 +0200
Subject[PATCH v5 5/7] iscsi-target: replace iscsi_initiatorname_tolower() with strtolower()
Message-ID<s4IVc-204-19@gated-at.bofh.it>
In reply to#1459934
After introducing generic strtolower(), iscsi_initiatorname_tolower() is
no longer needed.

Signed-off-by: Markus Mayer <mmayer@broadcom.com>
Acked-by: Nicholas Bellinger <nab@linux-iscsi.org>
---
 drivers/target/iscsi/iscsi_target_nego.c | 17 +----------------
 1 file changed, 1 insertion(+), 16 deletions(-)

diff --git a/drivers/target/iscsi/iscsi_target_nego.c b/drivers/target/iscsi/iscsi_target_nego.c
index 89d34bd..fa20638 100644
--- a/drivers/target/iscsi/iscsi_target_nego.c
+++ b/drivers/target/iscsi/iscsi_target_nego.c
@@ -987,21 +987,6 @@ static int iscsi_target_do_login(struct iscsi_conn *conn, struct iscsi_login *lo
 	return 0;
 }
 
-static void iscsi_initiatorname_tolower(
-	char *param_buf)
-{
-	char *c;
-	u32 iqn_size = strlen(param_buf), i;
-
-	for (i = 0; i < iqn_size; i++) {
-		c = &param_buf[i];
-		if (!isupper(*c))
-			continue;
-
-		*c = tolower(*c);
-	}
-}
-
 /*
  * Processes the first Login Request..
  */
@@ -1075,7 +1060,7 @@ int iscsi_target_locate_portal(
 	 * RFC-3720 3.2.6.1. section c) that says that iSCSI IQNs
 	 * are NOT case sensitive.
 	 */
-	iscsi_initiatorname_tolower(i_buf);
+	strtolower(i_buf);
 
 	if (!s_buf) {
 		if (!login->leading_connection)
-- 
2.7.4

[toc] | [prev] | [next] | [standalone]


#1459937 — [PATCH v5 1/7] lib: string: add functions to case-convert strings

FromMarkus Mayer <mmayer@broadcom.com>
Date2016-08-10 23:10 +0200
Subject[PATCH v5 1/7] lib: string: add functions to case-convert strings
Message-ID<s4IVc-204-21@gated-at.bofh.it>
In reply to#1459934
Add a collection of generic functions to convert strings to lowercase
or uppercase.

Changing the case of a string (with or without copying it first) seems
to be a recurring requirement in the kernel that is currently being
solved by several duplicated implementations doing the same thing. This
change aims at reducing this code duplication.

The new functions are
    int strlcpytoupper(char *dst, const char *src, size_t len);
    int strlcpytolower(char *dst, const char *src, size_t len);
    void strcpytoupper(char *dst, const char *src);
    void strcpytolower(char *dst, const char *src);
    void strtoupper(char *s);
    void strtolower(char *s);

The "str[l]cpyto*" versions of the function take a destination string
and a source string as arguments. The "strlcpyto*" versions additionally
take a length argument like strlcpy() itself. Lastly, the strto*
functions take a single string argument and modify the passed-in string.

strlcpytoupper() and strlcpytolower() return the number of characters
copied or -E2BIG if the destination string was truncated.

Like strlcpy(), and unlike strncpy(), the functions guarantee NULL
termination of the destination string.

Signed-off-by: Markus Mayer <mmayer@broadcom.com>
---
 include/linux/string.h | 40 ++++++++++++++++++++++++++++++++++++++++
 lib/string.c           | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 86 insertions(+)

diff --git a/include/linux/string.h b/include/linux/string.h
index 26b6f6a..257d797 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -116,6 +116,8 @@ extern void * memchr(const void *,int,__kernel_size_t);
 #endif
 void *memchr_inv(const void *s, int c, size_t n);
 char *strreplace(char *s, char old, char new);
+extern int strlcpytoupper(char *dst, const char *src, size_t len);
+extern int strlcpytolower(char *dst, const char *src, size_t len);
 
 extern void kfree_const(const void *x);
 
@@ -169,4 +171,42 @@ static inline const char *kbasename(const char *path)
 	return tail ? tail + 1 : path;
 }
 
+/**
+ * strcpytoupper - Copy string and convert to uppercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to uppercase.
+ */
+static inline void strcpytoupper(char *dst, const char *src)
+{
+	strlcpytoupper(dst, src, ~(size_t)0);
+}
+
+/**
+ * strcpytolower - Copy string and convert to lowercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to lowercase.
+ */
+static inline void strcpytolower(char *dst, const char *src)
+{
+	strlcpytolower(dst, src, ~(size_t)0);
+}
+
+/**
+ * strtoupper - Convert string to uppercase.
+ * @s: The string to operate on.
+ */
+static inline void strtoupper(char *s)
+{
+	strlcpytoupper(s, s, ~(size_t)0);
+}
+
+/**
+ * strtolower - Convert string to lowercase.
+ * @s: The string to operate on.
+ */
+static inline void strtolower(char *s)
+{
+	strlcpytolower(s, s, ~(size_t)0);
+}
+
 #endif /* _LINUX_STRING_H_ */
diff --git a/lib/string.c b/lib/string.c
index ed83562..d36d5fb2 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -952,3 +952,49 @@ char *strreplace(char *s, char old, char new)
 	return s;
 }
 EXPORT_SYMBOL(strreplace);
+
+/**
+ * strlcpytoupper - Copy a length-limited string and convert to uppercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to uppercase.
+ * @len: Maximum string length. May be SIZE_MAX to set no limit.
+ *
+ * Returns the number of characters copied or -E2BIG if @dst wasn't big enough.
+ */
+int strlcpytoupper(char *dst, const char *src, size_t len)
+{
+	size_t i;
+
+	if (!len)
+		return -E2BIG;
+
+	for (i = 0; i < len && src[i]; ++i)
+		dst[i] = toupper(src[i]);
+	dst[i < len ? i : i - 1] = '\0';
+
+	return (i < len) ? i : -E2BIG;
+}
+EXPORT_SYMBOL(strlcpytoupper);
+
+/**
+ * strlcpytolower - Copy a length-limited string and convert to lowercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to lowercase.
+ * @len: Maximum string length. May be SIZE_MAX to set no limit.
+ *
+ * Returns the number of characters copied or -E2BIG if @dst wasn't big enough.
+ */
+int strlcpytolower(char *dst, const char *src, size_t len)
+{
+	size_t i;
+
+	if (!len)
+		return -E2BIG;
+
+	for (i = 0; i < len && src[i]; ++i)
+		dst[i] = tolower(src[i]);
+	dst[i < len ? i : i - 1] = '\0';
+
+	return (i < len) ? i : -E2BIG;
+}
+EXPORT_SYMBOL(strlcpytolower);
-- 
2.7.4

[toc] | [prev] | [next] | [standalone]


#1459942 — [PATCH v5 6/7] drm/nouveau/fifo/gk104: make use of new strcpytoupper() function

FromMarkus Mayer <mmayer@broadcom.com>
Date2016-08-10 23:10 +0200
Subject[PATCH v5 6/7] drm/nouveau/fifo/gk104: make use of new strcpytoupper() function
Message-ID<s4IVc-204-37@gated-at.bofh.it>
In reply to#1459934
Call strcpytoupper() rather than copying the string explicitly and then
walking it to convert it to uppercase.

Signed-off-by: Markus Mayer <mmayer@broadcom.com>
---
 drivers/gpu/drm/nouveau/nvkm/engine/fifo/gk104.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/drivers/gpu/drm/nouveau/nvkm/engine/fifo/gk104.c b/drivers/gpu/drm/nouveau/nvkm/engine/fifo/gk104.c
index 103c0af..24f4c45 100644
--- a/drivers/gpu/drm/nouveau/nvkm/engine/fifo/gk104.c
+++ b/drivers/gpu/drm/nouveau/nvkm/engine/fifo/gk104.c
@@ -332,10 +332,7 @@ gk104_fifo_intr_fault(struct gk104_fifo *fifo, int unit)
 		enum nvkm_devidx engidx = nvkm_top_fault(device, unit);
 		if (engidx < NVKM_SUBDEV_NR) {
 			const char *src = nvkm_subdev_name[engidx];
-			char *dst = en;
-			do {
-				*dst++ = toupper(*src++);
-			} while(*src);
+			strcpytoupper(en, src);
 			engine = nvkm_device_engine(device, engidx);
 		}
 	} else {
-- 
2.7.4

[toc] | [prev] | [next] | [standalone]


#1459944 — [PATCH v5 4/7] staging: speakup: replace spk_strlwr() with strlcpytolower()

FromMarkus Mayer <mmayer@broadcom.com>
Date2016-08-10 23:10 +0200
Subject[PATCH v5 4/7] staging: speakup: replace spk_strlwr() with strlcpytolower()
Message-ID<s4IVc-204-45@gated-at.bofh.it>
In reply to#1459934
After introducing generic strltolower() and strtolower(), spk_strlwr()
is no longer needed.

Signed-off-by: Markus Mayer <mmayer@broadcom.com>
Acked-by: Samuel Thibault <samuel.thibault@ens-lyon.org>
Acked-by: Chris Brannon <chris@the-brannons.com>
---
 drivers/staging/speakup/kobjects.c    |  3 +--
 drivers/staging/speakup/main.c        |  3 ++-
 drivers/staging/speakup/speakup.h     |  1 -
 drivers/staging/speakup/varhandlers.c | 12 ------------
 4 files changed, 3 insertions(+), 16 deletions(-)

diff --git a/drivers/staging/speakup/kobjects.c b/drivers/staging/speakup/kobjects.c
index 528cbdc..c6e0c2d 100644
--- a/drivers/staging/speakup/kobjects.c
+++ b/drivers/staging/speakup/kobjects.c
@@ -387,11 +387,10 @@ static ssize_t synth_store(struct kobject *kobj, struct kobj_attribute *attr,
 	len = strlen(buf);
 	if (len < 2 || len > 9)
 		return -EINVAL;
-	strncpy(new_synth_name, buf, len);
+	strlcpytolower(new_synth_name, buf, sizeof(new_synth_name));
 	if (new_synth_name[len - 1] == '\n')
 		len--;
 	new_synth_name[len] = '\0';
-	spk_strlwr(new_synth_name);
 	if ((synth != NULL) && (!strcmp(new_synth_name, synth->name))) {
 		pr_warn("%s already in use\n", new_synth_name);
 	} else if (synth_init(new_synth_name) != 0) {
diff --git a/drivers/staging/speakup/main.c b/drivers/staging/speakup/main.c
index 97ca4ec..970f568 100644
--- a/drivers/staging/speakup/main.c
+++ b/drivers/staging/speakup/main.c
@@ -2314,7 +2314,8 @@ static int __init speakup_init(void)
 	spk_initialize_msgs();	/* Initialize arrays for i18n. */
 	spk_reset_default_chars();
 	spk_reset_default_chartab();
-	spk_strlwr(synth_name);
+	if (synth_name)
+		strtolower(synth_name);
 	spk_vars[0].u.n.high = vc->vc_cols;
 	for (var = spk_vars; var->var_id != MAXVARS; var++)
 		speakup_register_var(var);
diff --git a/drivers/staging/speakup/speakup.h b/drivers/staging/speakup/speakup.h
index df74c91..4725785 100644
--- a/drivers/staging/speakup/speakup.h
+++ b/drivers/staging/speakup/speakup.h
@@ -50,7 +50,6 @@ void synth_insert_next_index(int sent_num);
 void spk_reset_index_count(int sc);
 void spk_get_index_count(int *linecount, int *sentcount);
 int spk_set_key_info(const u_char *key_info, u_char *k_buffer);
-char *spk_strlwr(char *s);
 char *spk_s2uchar(char *start, char *dest);
 int speakup_kobj_init(void);
 void speakup_kobj_exit(void);
diff --git a/drivers/staging/speakup/varhandlers.c b/drivers/staging/speakup/varhandlers.c
index e1393d2..a1af222 100644
--- a/drivers/staging/speakup/varhandlers.c
+++ b/drivers/staging/speakup/varhandlers.c
@@ -309,18 +309,6 @@ int spk_set_mask_bits(const char *input, const int which, const int how)
 	return 0;
 }
 
-char *spk_strlwr(char *s)
-{
-	char *p;
-
-	if (!s)
-		return NULL;
-
-	for (p = s; *p; p++)
-		*p = tolower(*p);
-	return s;
-}
-
 char *spk_s2uchar(char *start, char *dest)
 {
 	int val;
-- 
2.7.4

[toc] | [prev] | [next] | [standalone]


#1467269 — Re: [PATCH v5 4/7] staging: speakup: replace spk_strlwr() with strlcpytolower()

FromGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Date2016-08-21 23:30 +0200
SubjectRe: [PATCH v5 4/7] staging: speakup: replace spk_strlwr() with strlcpytolower()
Message-ID<s8ItA-5Ge-29@gated-at.bofh.it>
In reply to#1459944
On Wed, Aug 10, 2016 at 02:04:54PM -0700, Markus Mayer wrote:
> After introducing generic strltolower() and strtolower(), spk_strlwr()
> is no longer needed.
> 
> Signed-off-by: Markus Mayer <mmayer@broadcom.com>
> Acked-by: Samuel Thibault <samuel.thibault@ens-lyon.org>
> Acked-by: Chris Brannon <chris@the-brannons.com>
> ---
>  drivers/staging/speakup/kobjects.c    |  3 +--
>  drivers/staging/speakup/main.c        |  3 ++-
>  drivers/staging/speakup/speakup.h     |  1 -
>  drivers/staging/speakup/varhandlers.c | 12 ------------
>  4 files changed, 3 insertions(+), 16 deletions(-)

Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web