Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / fs / ntfs / unistr.c
index 36bee49..b123c0f 100644 (file)
@@ -1,24 +1,28 @@
 /*
  * unistr.c - NTFS Unicode string handling. Part of the Linux-NTFS project.
  *
- * Copyright (c) 2001-2004 Anton Altaparmakov
+ * Copyright (c) 2001-2006 Anton Altaparmakov
  *
  * This program/include file 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; either version 2 of the License, or
  * (at your option) any later version.
  *
- * This program/include file is distributed in the hope that it will be 
- * useful, but WITHOUT ANY WARRANTY; without even the implied warranty 
+ * This program/include file is distributed in the hope that it will 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.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program (in the main directory of the Linux-NTFS 
+ * along with this program (in the main directory of the Linux-NTFS
  * distribution in the file COPYING); if not, write to the Free Software
  * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
+#include <linux/slab.h>
+
+#include "types.h"
+#include "debug.h"
 #include "ntfs.h"
 
 /*
@@ -61,10 +65,9 @@ static const u8 legal_ansi_char_array[0x40] = {
  * identical, or FALSE (0) if they are not identical. If @ic is IGNORE_CASE,
  * the @upcase table is used to performa a case insensitive comparison.
  */
-BOOL ntfs_are_names_equal(const uchar_t *s1, size_t s1_len,
-                    const uchar_t *s2, size_t s2_len,
-                    const IGNORE_CASE_BOOL ic,
-                    const uchar_t *upcase, const u32 upcase_size)
+BOOL ntfs_are_names_equal(const ntfschar *s1, size_t s1_len,
+               const ntfschar *s2, size_t s2_len, const IGNORE_CASE_BOOL ic,
+               const ntfschar *upcase, const u32 upcase_size)
 {
        if (s1_len != s2_len)
                return FALSE;
@@ -78,12 +81,12 @@ BOOL ntfs_are_names_equal(const uchar_t *s1, size_t s1_len,
  * @name1:     first Unicode name to compare
  * @name2:     second Unicode name to compare
  * @err_val:   if @name1 contains an invalid character return this value
- * @ic:         either CASE_SENSITIVE or IGNORE_CASE
+ * @ic:                either CASE_SENSITIVE or IGNORE_CASE
  * @upcase:    upcase table (ignored if @ic is CASE_SENSITIVE)
  * @upcase_len:        upcase table size (ignored if @ic is CASE_SENSITIVE)
  *
  * ntfs_collate_names collates two Unicode names and returns:
- * 
+ *
  *  -1 if the first name collates before the second one,
  *   0 if the names match,
  *   1 if the second name collates before the first one, or
@@ -91,13 +94,13 @@ BOOL ntfs_are_names_equal(const uchar_t *s1, size_t s1_len,
  *
  * The following characters are considered invalid: '"', '*', '<', '>' and '?'.
  */
-int ntfs_collate_names(const uchar_t *name1, const u32 name1_len,
-               const uchar_t *name2, const u32 name2_len,
+int ntfs_collate_names(const ntfschar *name1, const u32 name1_len,
+               const ntfschar *name2, const u32 name2_len,
                const int err_val, const IGNORE_CASE_BOOL ic,
-               const uchar_t *upcase, const u32 upcase_len)
+               const ntfschar *upcase, const u32 upcase_len)
 {
        u32 cnt, min_len;
-       uchar_t c1, c2;
+       u16 c1, c2;
 
        min_len = name1_len;
        if (name1_len > name2_len)
@@ -138,14 +141,14 @@ int ntfs_collate_names(const uchar_t *name1, const u32 name1_len,
  * Compare the first @n characters of the Unicode strings @s1 and @s2,
  * The strings in little endian format and appropriate le16_to_cpu()
  * conversion is performed on non-little endian machines.
- * 
+ *
  * The function returns an integer less than, equal to, or greater than zero
  * if @s1 (or the first @n Unicode characters thereof) is found, respectively,
  * to be less than, to match, or be greater than @s2.
  */
-int ntfs_ucsncmp(const uchar_t *s1, const uchar_t *s2, size_t n)
+int ntfs_ucsncmp(const ntfschar *s1, const ntfschar *s2, size_t n)
 {
-       uchar_t c1, c2;
+       u16 c1, c2;
        size_t i;
 
        for (i = 0; i < n; ++i) {
@@ -172,18 +175,18 @@ int ntfs_ucsncmp(const uchar_t *s1, const uchar_t *s2, size_t n)
  * Compare the first @n characters of the Unicode strings @s1 and @s2,
  * ignoring case. The strings in little endian format and appropriate
  * le16_to_cpu() conversion is performed on non-little endian machines.
- * 
+ *
  * Each character is uppercased using the @upcase table before the comparison.
  *
  * The function returns an integer less than, equal to, or greater than zero
  * if @s1 (or the first @n Unicode characters thereof) is found, respectively,
  * to be less than, to match, or be greater than @s2.
  */
-int ntfs_ucsncasecmp(const uchar_t *s1, const uchar_t *s2, size_t n,
-                    const uchar_t *upcase, const u32 upcase_size)
+int ntfs_ucsncasecmp(const ntfschar *s1, const ntfschar *s2, size_t n,
+               const ntfschar *upcase, const u32 upcase_size)
 {
-       uchar_t c1, c2;
        size_t i;
+       u16 c1, c2;
 
        for (i = 0; i < n; ++i) {
                if ((c1 = le16_to_cpu(s1[i])) < upcase_size)
@@ -200,11 +203,11 @@ int ntfs_ucsncasecmp(const uchar_t *s1, const uchar_t *s2, size_t n,
        return 0;
 }
 
-void ntfs_upcase_name(uchar_t *name, u32 name_len, const uchar_t *upcase,
+void ntfs_upcase_name(ntfschar *name, u32 name_len, const ntfschar *upcase,
                const u32 upcase_len)
 {
        u32 i;
-       uchar_t u;
+       u16 u;
 
        for (i = 0; i < name_len; i++)
                if ((u = le16_to_cpu(name[i])) < upcase_len)
@@ -212,20 +215,20 @@ void ntfs_upcase_name(uchar_t *name, u32 name_len, const uchar_t *upcase,
 }
 
 void ntfs_file_upcase_value(FILE_NAME_ATTR *file_name_attr,
-               const uchar_t *upcase, const u32 upcase_len)
+               const ntfschar *upcase, const u32 upcase_len)
 {
-       ntfs_upcase_name((uchar_t*)&file_name_attr->file_name,
+       ntfs_upcase_name((ntfschar*)&file_name_attr->file_name,
                        file_name_attr->file_name_length, upcase, upcase_len);
 }
 
 int ntfs_file_compare_values(FILE_NAME_ATTR *file_name_attr1,
                FILE_NAME_ATTR *file_name_attr2,
                const int err_val, const IGNORE_CASE_BOOL ic,
-               const uchar_t *upcase, const u32 upcase_len)
+               const ntfschar *upcase, const u32 upcase_len)
 {
-       return ntfs_collate_names((uchar_t*)&file_name_attr1->file_name,
+       return ntfs_collate_names((ntfschar*)&file_name_attr1->file_name,
                        file_name_attr1->file_name_length,
-                       (uchar_t*)&file_name_attr2->file_name,
+                       (ntfschar*)&file_name_attr2->file_name,
                        file_name_attr2->file_name_length,
                        err_val, ic, upcase, upcase_len);
 }
@@ -241,7 +244,7 @@ int ntfs_file_compare_values(FILE_NAME_ATTR *file_name_attr1,
  * map dictates, into a little endian, 2-byte Unicode string.
  *
  * This function allocates the string and the caller is responsible for
- * calling kmem_cache_free(ntfs_name_cache, @outs); when finished with it.
+ * calling kmem_cache_free(ntfs_name_cache, *@outs); when finished with it.
  *
  * On success the function returns the number of Unicode characters written to
  * the output string *@outs (>= 0), not counting the terminating Unicode NULL
@@ -254,44 +257,55 @@ int ntfs_file_compare_values(FILE_NAME_ATTR *file_name_attr1,
  * This might look a bit odd due to fast path optimization...
  */
 int ntfs_nlstoucs(const ntfs_volume *vol, const char *ins,
-               const int ins_len, uchar_t **outs)
+               const int ins_len, ntfschar **outs)
 {
        struct nls_table *nls = vol->nls_map;
-       uchar_t *ucs;
+       ntfschar *ucs;
        wchar_t wc;
        int i, o, wc_len;
 
-       /* We don't trust outside sources. */
-       if (ins) {
-               ucs = (uchar_t*)kmem_cache_alloc(ntfs_name_cache, SLAB_NOFS);
-               if (ucs) {
+       /* We do not trust outside sources. */
+       if (likely(ins)) {
+               ucs = kmem_cache_alloc(ntfs_name_cache, SLAB_NOFS);
+               if (likely(ucs)) {
                        for (i = o = 0; i < ins_len; i += wc_len) {
                                wc_len = nls->char2uni(ins + i, ins_len - i,
                                                &wc);
-                               if (wc_len >= 0) {
-                                       if (wc) {
+                               if (likely(wc_len >= 0 &&
+                                               o < NTFS_MAX_NAME_LEN)) {
+                                       if (likely(wc)) {
                                                ucs[o++] = cpu_to_le16(wc);
                                                continue;
-                                       } /* else (!wc) */
+                                       } /* else if (!wc) */
                                        break;
-                               } /* else (wc_len < 0) */
-                               goto conversion_err;
+                               } /* else if (wc_len < 0 ||
+                                               o >= NTFS_MAX_NAME_LEN) */
+                               goto name_err;
                        }
-                       ucs[o] = cpu_to_le16('\0');
+                       ucs[o] = 0;
                        *outs = ucs;
                        return o;
-               } /* else (!ucs) */
-               ntfs_error(vol->sb, "Failed to allocate name from "
-                               "ntfs_name_cache!");
+               } /* else if (!ucs) */
+               ntfs_error(vol->sb, "Failed to allocate buffer for converted "
+                               "name from ntfs_name_cache.");
                return -ENOMEM;
-       } /* else (!ins) */
-       ntfs_error(NULL, "Received NULL pointer.");
+       } /* else if (!ins) */
+       ntfs_error(vol->sb, "Received NULL pointer.");
        return -EINVAL;
-conversion_err:
-       ntfs_error(vol->sb, "Name using character set %s contains characters "
-                       "that cannot be converted to Unicode.", nls->charset);
+name_err:
        kmem_cache_free(ntfs_name_cache, ucs);
-       return -EILSEQ;
+       if (wc_len < 0) {
+               ntfs_error(vol->sb, "Name using character set %s contains "
+                               "characters that cannot be converted to "
+                               "Unicode.", nls->charset);
+               i = -EILSEQ;
+       } else /* if (o >= NTFS_MAX_NAME_LEN) */ {
+               ntfs_error(vol->sb, "Name is too long (maximum length for a "
+                               "name on NTFS is %d Unicode characters.",
+                               NTFS_MAX_NAME_LEN);
+               i = -ENAMETOOLONG;
+       }
+       return i;
 }
 
 /**
@@ -319,7 +333,7 @@ conversion_err:
  *
  * This might look a bit odd due to fast path optimization...
  */
-int ntfs_ucstonls(const ntfs_volume *vol, const uchar_t *ins,
+int ntfs_ucstonls(const ntfs_volume *vol, const ntfschar *ins,
                const int ins_len, unsigned char **outs, int outs_len)
 {
        struct nls_table *nls = vol->nls_map;
@@ -363,7 +377,7 @@ retry:                      wc = nls->uni2char(le16_to_cpu(ins[i]), ns + o,
                        } /* wc < 0, real error. */
                        goto conversion_err;
                }
-               ns[o] = '\0';
+               ns[o] = 0;
                *outs = ns;
                return o;
        } /* else (!ins) */
@@ -371,7 +385,8 @@ retry:                      wc = nls->uni2char(le16_to_cpu(ins[i]), ns + o,
        return -EINVAL;
 conversion_err:
        ntfs_error(vol->sb, "Unicode name contains characters that cannot be "
-                       "converted to character set %s.", nls->charset);
+                       "converted to character set %s.  You might want to "
+                       "try to use the mount option nls=utf8.", nls->charset);
        if (ns != *outs)
                kfree(ns);
        if (wc != -ENAMETOOLONG)
@@ -381,4 +396,3 @@ mem_err_out:
        ntfs_error(vol->sb, "Failed to allocate name!");
        return -ENOMEM;
 }
-