2 * unistr.c - Unicode string handling. Part of the Linux-NTFS project.
4 * Copyright (c) 2000,2001 Anton Altaparmakov.
6 * This program/include file is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as published
8 * by the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program/include file is distributed in the hope that it will be
12 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program (in the main directory of the Linux-NTFS
18 * distribution in the file COPYING); if not, write to the Free Software
19 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/string.h>
23 #include <asm/byteorder.h>
29 * This is used by the name collation functions to quickly determine what
30 * characters are (in)valid.
32 const __u8 legal_ansi_char_array[0x40] = {
33 0x00, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
34 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
36 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
37 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
39 0x17, 0x07, 0x18, 0x17, 0x17, 0x17, 0x17, 0x17,
40 0x17, 0x17, 0x18, 0x16, 0x16, 0x17, 0x07, 0x00,
42 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17,
43 0x17, 0x17, 0x04, 0x16, 0x18, 0x16, 0x18, 0x18,
47 * ntfs_are_names_equal - compare two Unicode names for equality
48 * @s1: name to compare to @s2
49 * @s1_len: length in Unicode characters of @s1
50 * @s2: name to compare to @s1
51 * @s2_len: length in Unicode characters of @s2
52 * @ic: ignore case bool
53 * @upcase: upcase table (only if @ic == IGNORE_CASE)
54 * @upcase_size: length in Unicode characters of @upcase (if present)
56 * Compare the names @s1 and @s2 and return TRUE (1) if the names are
57 * identical, or FALSE (0) if they are not identical. If @ic is IGNORE_CASE,
58 * the @upcase table is used to performa a case insensitive comparison.
60 int ntfs_are_names_equal(wchar_t *s1, size_t s1_len,
61 wchar_t *s2, size_t s2_len, int ic,
62 wchar_t *upcase, __u32 upcase_size)
67 return memcmp(s1, s2, s1_len << 1) ? 0: 1;
68 return ntfs_wcsncasecmp(s1, s2, s1_len, upcase, upcase_size) ? 0: 1;
72 * ntfs_collate_names - collate two Unicode names
73 * @upcase: upcase table (ignored if @ic is CASE_SENSITIVE)
74 * @upcase_len: upcase table size (ignored if @ic is CASE_SENSITIVE)
75 * @name1: first Unicode name to compare
76 * @name2: second Unicode name to compare
77 * @ic: either CASE_SENSITIVE or IGNORE_CASE
78 * @err_val: if @name1 contains an invalid character return this value
80 * ntfs_collate_names collates two Unicode names and returns:
82 * -1 if the first name collates before the second one,
83 * 0 if the names match,
84 * 1 if the second name collates before the first one, or
85 * @ec if an invalid character is encountered in @name1 during the comparison.
87 * The following characters are considered invalid: '"', '*', '<', '>' and '?'.
89 int ntfs_collate_names(wchar_t *upcase, __u32 upcase_len,
90 wchar_t *name1, __u32 name1_len,
91 wchar_t *name2, __u32 name2_len,
98 if (min_len > name2_len)
100 for (cnt = 0; cnt < min_len; ++cnt) {
101 c1 = le16_to_cpu(*name1++);
102 c2 = le16_to_cpu(*name2++);
105 c1 = le16_to_cpu(upcase[c1]);
107 c2 = le16_to_cpu(upcase[c2]);
109 if (c1 < 64 && legal_ansi_char_array[c1] & 8)
118 if (name1_len < name2_len)
120 if (name1_len == name2_len)
122 /* name1_len > name2_len */
123 c1 = le16_to_cpu(*name1);
124 if (c1 < 64 && legal_ansi_char_array[c1] & 8)
130 * ntfs_wcsncasecmp - compare two little endian Unicode strings, ignoring case
133 * @n: maximum unicode characters to compare
134 * @upcase: upcase table
135 * @upcase_size: upcase table size in Unicode characters
137 * Compare the first @n characters of the Unicode strings @s1 and @s2,
138 * ignoring case. The strings in little endian format and appropriate
139 * le16_to_cpu() conversion is performed on non-little endian machines.
141 * Each character is uppercased using the @upcase table before the comparison.
143 * The function returns an integer less than, equal to, or greater than zero
144 * if @s1 (or the first @n Unicode characters thereof) is found, respectively,
145 * to be less than, to match, or be greater than @s2.
147 int ntfs_wcsncasecmp(wchar_t *s1, wchar_t *s2, size_t n,
148 wchar_t *upcase, __u32 upcase_size)
153 for (i = 0; i < n; ++i) {
154 if ((c1 = le16_to_cpu(s1[i])) < upcase_size)
155 c1 = le16_to_cpu(upcase[c1]);
156 if ((c2 = le16_to_cpu(s2[i])) < upcase_size)
157 c2 = le16_to_cpu(upcase[c2]);