From patchwork Thu Jan 25 02:53:38 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gabriel Krisman Bertazi X-Patchwork-Id: 865691 Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-ext4-owner@vger.kernel.org; receiver=) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3zRmpt47CQz9t3p for ; Thu, 25 Jan 2018 13:54:46 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933130AbeAYCyp (ORCPT ); Wed, 24 Jan 2018 21:54:45 -0500 Received: from bhuna.collabora.co.uk ([46.235.227.227]:52020 "EHLO bhuna.collabora.co.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932925AbeAYCyo (ORCPT ); Wed, 24 Jan 2018 21:54:44 -0500 Received: from [127.0.0.1] (localhost [127.0.0.1]) (Authenticated sender: krisman) with ESMTPSA id B7AE8270D24 From: Gabriel Krisman Bertazi To: tytso@mit.edu, david@fromorbit.com, olaf@sgi.com, viro@zeniv.linux.org.uk Cc: linux-ext4@vger.kernel.org, linux-fsdevel@vger.kernel.org, alvaro.soliverez@collabora.co.uk, kernel@lists.collabora.co.uk, Gabriel Krisman Bertazi Subject: [PATCH RFC v2 02/13] charsets: ascii: Wrap ascii functions to charsets library Date: Thu, 25 Jan 2018 00:53:38 -0200 Message-Id: <20180125025349.31494-3-krisman@collabora.co.uk> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20180125025349.31494-1-krisman@collabora.co.uk> References: <20180125025349.31494-1-krisman@collabora.co.uk> Sender: linux-ext4-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org This allows filesystems to always use the charsets interfaces, even when not caring about encoding. Changes since RFC v1: - Include length parameter for second string on comparison functions. - Cast length type to size_t. Signed-off-by: Gabriel Krisman Bertazi --- lib/charsets/Makefile | 2 + lib/charsets/ascii.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 lib/charsets/ascii.c diff --git a/lib/charsets/Makefile b/lib/charsets/Makefile index 01ff9fd09f98..2184e7ff25de 100644 --- a/lib/charsets/Makefile +++ b/lib/charsets/Makefile @@ -1,3 +1,5 @@ charsets-y += core.o obj-$(CONFIG_CHARSETS) += charsets.o + +obj-$(CONFIG_CHARSETS) += ascii.o diff --git a/lib/charsets/ascii.c b/lib/charsets/ascii.c new file mode 100644 index 000000000000..42f3ec00e41c --- /dev/null +++ b/lib/charsets/ascii.c @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2017 Collabora Ltd. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it would be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ +#include +#include +#include +#include +#include +#include + +static struct charset_info ascii_info; + +int ascii_strncmp(const struct charset *charset, const char *str1, + size_t len1, const char *str2, size_t len2) +{ + size_t len = (len1 < len2)? len1:len2; + + return strncmp(str1, str2, len); +} + +int ascii_strncasecmp(const struct charset *charset, const char *str1, + size_t len1, const char *str2, size_t len2) +{ + size_t len = (len1 < len2)? len1:len2; + + return strncasecmp(str1, str2, len); +} + +int ascii_normalize(const struct charset *charset, const char *str, + int len, char **normalization) +{ + *normalization = kstrdup(str, GFP_NOFS); + return (*normalization) ? len : -ENOMEM; +} + +int ascii_casefold(const struct charset *charset, const char *str, + int len, char **folded_str) +{ + int i; + char *fold; + + fold = kstrdup(str, GFP_NOFS); + if (!fold) + return -ENOMEM; + + for (i = 0; i < len; i++) + fold[i] = tolower(fold[i]); + + *folded_str = fold; + return len; +} + +static const struct charset_ops ascii_ops = { + .strncmp = ascii_strncmp, + .strncasecmp = ascii_strncasecmp, + .casefold = ascii_casefold, + .normalize = ascii_normalize, +}; + +static struct charset ascii_charset = { + .version = 0, + .info = &ascii_info, + .ops = &ascii_ops +}; + +static struct charset *ascii_load_charset(void *pargs) +{ + return &ascii_charset; +} + +static struct charset_info ascii_info = { + .name = "ascii", + .match_token = "ascii", + .load_charset = ascii_load_charset, +}; + +static int __init init_ascii(void) +{ + charset_register(&ascii_info); + return 0; +} + +static void __exit exit_ascii(void) +{ +} + +module_init(init_ascii); +module_exit(exit_ascii); +MODULE_AUTHOR("Gabriel Krisman Bertazi"); +MODULE_DESCRIPTION("ASCII charset for filesystems"); +MODULE_LICENSE("GPL"); +