From patchwork Fri Apr 17 15:09:28 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suresh Jayaraman X-Patchwork-Id: 18695 Received: from lists.samba.org (mail.samba.org [66.70.73.150]) by demeter.kernel.org (8.14.2/8.14.2) with ESMTP id n3HFA2N5028579 for ; Fri, 17 Apr 2009 15:10:02 GMT Received: from dp.samba.org (localhost [127.0.0.1]) by lists.samba.org (Postfix) with ESMTP id 73D9C163D17 for ; Fri, 17 Apr 2009 15:09:41 +0000 (GMT) X-Spam-Checker-Version: SpamAssassin 3.1.7 (2006-10-05) on dp.samba.org X-Spam-Level: X-Spam-Status: No, score=-2.6 required=3.8 tests=BAYES_00 autolearn=ham version=3.1.7 X-Original-To: linux-cifs-client@lists.samba.org Delivered-To: linux-cifs-client@lists.samba.org Received: from mx1.suse.de (cantor.suse.de [195.135.220.2]) by lists.samba.org (Postfix) with ESMTP id 2FD05163B84 for ; Fri, 17 Apr 2009 15:09:18 +0000 (GMT) Received: from Relay1.suse.de (relay-ext.suse.de [195.135.221.8]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.suse.de (Postfix) with ESMTP id 228CD5362F; Fri, 17 Apr 2009 17:09:38 +0200 (CEST) Message-ID: <49E89BA8.4020107@suse.de> Date: Fri, 17 Apr 2009 20:39:28 +0530 From: Suresh Jayaraman User-Agent: Thunderbird 2.0.0.19 (X11/20081227) MIME-Version: 1.0 To: Steve French X-Enigmail-Version: 0.95.7 Cc: "linux-cifs-client@lists.samba.org" , Jeff Layton Subject: [linux-cifs-client] [PATCH 1/5] cifs: Introduce helpers for unicode buffer handing X-BeenThere: linux-cifs-client@lists.samba.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: The Linux CIFS VFS client List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: linux-cifs-client-bounces+patchwork-cifs-client=patchwork.kernel.org@lists.samba.org Errors-To: linux-cifs-client-bounces+patchwork-cifs-client=patchwork.kernel.org@lists.samba.org Introduce helpers to compute length of NLS string in bytes and to convert ucs string to nls format. Signed-off-by: Suresh Jayaraman --- fs/cifs/cifs_unicode.h | 27 +++++++++++++++++++++++++++ fs/cifs/cifsproto.h | 2 ++ fs/cifs/cifssmb.c | 31 +++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 0 deletions(-) Index: cifs-2.6.git/fs/cifs/cifs_unicode.h =================================================================== --- cifs-2.6.git.orig/fs/cifs/cifs_unicode.h +++ cifs-2.6.git/fs/cifs/cifs_unicode.h @@ -159,6 +159,33 @@ UniStrnlen(const wchar_t *ucs1, int maxl } /* + * UniStrnlenBytes: Return the length of a NLS string in bytes. Also, populates + * 'nchars' with the length of string in 16 bit Unicode chars. + */ +static inline size_t +UniStrnlenBytes(const wchar_t *str, int maxlen, int *nchars, + const struct nls_table *codepage) +{ + int nc; + size_t nbytes = 0; + char buf[NLS_MAX_CHARSET_SIZE]; /* enough for one char at a time */ + + *nchars = 0; + while (*str++ && maxlen) { + nc = codepage->uni2char(*str, buf, NLS_MAX_CHARSET_SIZE); + if (nc > 0) + nbytes += nc; + else + nbytes += 1; /* for '?' */ + (*nchars)++; + if (*nchars >= maxlen) + break; + } + + return nbytes; +} + +/* * UniStrncat: Concatenate length limited string */ static inline wchar_t * Index: cifs-2.6.git/fs/cifs/cifsproto.h =================================================================== --- cifs-2.6.git.orig/fs/cifs/cifsproto.h +++ cifs-2.6.git/fs/cifs/cifsproto.h @@ -383,4 +383,6 @@ extern int CIFSSMBSetPosixACL(const int const struct nls_table *nls_codepage, int remap_special_chars); extern int CIFSGetExtAttr(const int xid, struct cifsTconInfo *tcon, const int netfid, __u64 *pExtAttrBits, __u64 *pMask); +extern int cifs_ucs_to_nls(char **dst, const char *src, const int maxlen, + int *plen, const struct nls_table *nls_codepage); #endif /* _CIFSPROTO_H */ Index: cifs-2.6.git/fs/cifs/cifssmb.c =================================================================== --- cifs-2.6.git.orig/fs/cifs/cifssmb.c +++ cifs-2.6.git/fs/cifs/cifssmb.c @@ -81,6 +81,37 @@ static struct { #endif /* CONFIG_CIFS_WEAK_PW_HASH */ #endif /* CIFS_POSIX */ + +/* + * Calculates, allocates memory and converts to a NLS string. + * Note: caller is responsible for freeing dst if function returned 0. + * returns: + * on success - 0 + * on failure - errno + */ +int +cifs_ucs_to_nls(char **dst, const char *src, const int maxlen, int *plen, + const struct nls_table *nls_codepage) +{ + size_t nbytes; + + nbytes = UniStrnlenBytes((wchar_t *)src, maxlen, plen, nls_codepage); + cFYI(1, ("UniStrnlenBytes returned %ld bytes", nbytes)); + *dst = kzalloc(nbytes + 2, GFP_KERNEL); + if (!*dst) + goto err_exit; + + cifs_strfromUCS_le(*dst, (__le16 *)src, *plen, nls_codepage); + + /* kzalloc() ensures NULL termination */ + + return 0; + +err_exit: + cERROR(1, ("Failed to allocate buffer for string\n")); + return -ENOMEM; +} + /* Allocates buffer into dst and copies smb string from src to it. * caller is responsible for freeing dst if function returned 0. * returns: