diff mbox

[linux-cifs-client,01/12] nls: add a nls_nullsize inline

Message ID 1241036694-19940-2-git-send-email-jlayton@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Jeff Layton April 29, 2009, 8:24 p.m. UTC
It's possible for character sets to require a multi-byte null
string terminator. Add a helper function that determines the size
of the null terminator at runtime.

Signed-off-by: Jeff Layton <jlayton@redhat.com>
---
 include/linux/nls.h |   19 +++++++++++++++++++
 1 files changed, 19 insertions(+), 0 deletions(-)

Comments

Suresh Jayaraman April 30, 2009, 8:20 a.m. UTC | #1
Jeff Layton wrote:
> It's possible for character sets to require a multi-byte null
> string terminator. Add a helper function that determines the size
> of the null terminator at runtime.
> 
> Signed-off-by: Jeff Layton <jlayton@redhat.com>
> ---
>  include/linux/nls.h |   19 +++++++++++++++++++
>  1 files changed, 19 insertions(+), 0 deletions(-)
> 
> diff --git a/include/linux/nls.h b/include/linux/nls.h
> index 6a88220..52b1a76 100644
> --- a/include/linux/nls.h
> +++ b/include/linux/nls.h
> @@ -58,6 +58,25 @@ static inline int nls_strnicmp(struct nls_table *t, const unsigned char *s1,
>  	return 0;
>  }
>  
> +/*
> + * nls_nullsize - return length of null character for codepage
> + * @codepage - codepage for which to return length of NULL terminator
> + *
> + * Since we can't guarantee that the null terminator will be a particular
> + * length, we have to check against the codepage. If there's a problem
> + * determining it, assume a single-byte NULL terminator.
> + */
> +static inline int
> +nls_nullsize(const struct nls_table *codepage)
> +{
> +	int charlen;
> +	char tmp[NLS_MAX_CHARSET_SIZE];
> +
> +	charlen = codepage->uni2char(0, tmp, NLS_MAX_CHARSET_SIZE);
> +
> +	return charlen > 0 ? charlen : 1;
> +}
> +
>  #define MODULE_ALIAS_NLS(name)	MODULE_ALIAS("nls_" __stringify(name))
>  
>  #endif /* _LINUX_NLS_H */

Though any of the supported charsets does seem to use multi-byte null
termination, it makes more sense to have forward-compatible code with
negligible overhead.

Acked-by: Suresh Jayaraman <sjayaraman@suse.de>
diff mbox

Patch

diff --git a/include/linux/nls.h b/include/linux/nls.h
index 6a88220..52b1a76 100644
--- a/include/linux/nls.h
+++ b/include/linux/nls.h
@@ -58,6 +58,25 @@  static inline int nls_strnicmp(struct nls_table *t, const unsigned char *s1,
 	return 0;
 }
 
+/*
+ * nls_nullsize - return length of null character for codepage
+ * @codepage - codepage for which to return length of NULL terminator
+ *
+ * Since we can't guarantee that the null terminator will be a particular
+ * length, we have to check against the codepage. If there's a problem
+ * determining it, assume a single-byte NULL terminator.
+ */
+static inline int
+nls_nullsize(const struct nls_table *codepage)
+{
+	int charlen;
+	char tmp[NLS_MAX_CHARSET_SIZE];
+
+	charlen = codepage->uni2char(0, tmp, NLS_MAX_CHARSET_SIZE);
+
+	return charlen > 0 ? charlen : 1;
+}
+
 #define MODULE_ALIAS_NLS(name)	MODULE_ALIAS("nls_" __stringify(name))
 
 #endif /* _LINUX_NLS_H */