From patchwork Wed Aug 10 21:04:51 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Markus Mayer X-Patchwork-Id: 9276953 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 3411160752 for ; Fri, 12 Aug 2016 13:14:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 25347289BF for ; Fri, 12 Aug 2016 13:14:49 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1A0D6289E8; Fri, 12 Aug 2016 13:14:49 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.1 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 8DF4C289BF for ; Fri, 12 Aug 2016 13:14:48 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 6927D6EB77; Fri, 12 Aug 2016 13:14:01 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-qt0-x22e.google.com (mail-qt0-x22e.google.com [IPv6:2607:f8b0:400d:c0d::22e]) by gabe.freedesktop.org (Postfix) with ESMTPS id 38BE66E87B for ; Wed, 10 Aug 2016 21:05:31 +0000 (UTC) Received: by mail-qt0-x22e.google.com with SMTP id x25so27179405qtx.2 for ; Wed, 10 Aug 2016 14:05:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=broadcom.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=4F/7jG+c1oK/h10j94FHPHFh9Kz8ozIa+2pWIJfghr4=; b=W/emPouL0bSRRY9Z6fRjmVEHBGrXiZS1Bhl8vUnRC1wUZDIeUTms3AOLjX1BXY2Jh+ UNIQqtD+Oqqdc31kmy41sV5a9UhQY/NzZgqQAJliCaUUfErEXdI3e3g9XyOIAZSK+k0y cHZSpx1p52lKBoIYtnbDcpLJX3hOr1N7pnhBA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=4F/7jG+c1oK/h10j94FHPHFh9Kz8ozIa+2pWIJfghr4=; b=epPn2MHeJBa40dHnX1eVVWo+a9fOZ2mYsm0/+e5dVKpU64unSxPZUKqDKt5eGrdZTE qtN1cy5eND4KX99qNUlpA4zK9HIb97sThIXsKgTAoKnJL85JFv8Uw2NEi9K1x/Nsb317 j9yW5A7B1cLkZZGomBEzkdFReNthBXp3cBEmXw3cMb8agR+Z92BaN5+4avTrm4LNuCKA yJcnfP3zz9XWtHsSxeNM71PPLhcJNhQBw2VudYAE/U0KCeDbSYH9+57GhLgIypWYs9Tj 86I6rQRoWoyFquES+QH0bB7RhL7me168/8Kt1di+NtI6jYV8vKHFJfsR7rDFY6aRwPIZ Fr0g== X-Gm-Message-State: AEkoousmCOkT+34DUCI+HFe0h3JuiUO2YYqAA6CI9atB9J8wIpcZS720VeBKWohfDHGjOFQF X-Received: by 10.237.42.197 with SMTP id t63mr7030276qtd.84.1470863130113; Wed, 10 Aug 2016 14:05:30 -0700 (PDT) Received: from lbrmn-mmayer.lab.lan ([216.31.219.19]) by smtp.gmail.com with ESMTPSA id t42sm24370553qte.24.2016.08.10.14.05.25 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 10 Aug 2016 14:05:29 -0700 (PDT) From: Markus Mayer To: Andrew Morton , Al Viro , Rasmus Villemoes , Chris Metcalf , Kees Cook Subject: [PATCH v5 1/7] lib: string: add functions to case-convert strings Date: Wed, 10 Aug 2016 14:04:51 -0700 Message-Id: <1470863097-21138-2-git-send-email-mmayer@broadcom.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1470863097-21138-1-git-send-email-mmayer@broadcom.com> References: <1470863097-21138-1-git-send-email-mmayer@broadcom.com> X-Mailman-Approved-At: Fri, 12 Aug 2016 13:13:55 +0000 Cc: devel@driverdev.osuosl.org, linux-scsi@vger.kernel.org, linux-pm@vger.kernel.org, nouveau@lists.freedesktop.org, speakup@linux-speakup.org, linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-acpi@vger.kernel.org, target-devel@vger.kernel.org, Markus Mayer X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP 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 --- 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);