1 /*
2  * unistr.c - Unicode string handling. Part of the Linux-NTFS project.
3  *
4  * Copyright (c) 2000,2001 Anton Altaparmakov.
5  *
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.
10  *
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.
15  *
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
20  */
21 
22 #include <linux/string.h>
23 #include <asm/byteorder.h>
24 
25 #include "unistr.h"
26 #include "macros.h"
27 
28 /*
29  * This is used by the name collation functions to quickly determine what
30  * characters are (in)valid.
31  */
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,
35 
36 	0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
37 	0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
38 
39 	0x17, 0x07, 0x18, 0x17, 0x17, 0x17, 0x17, 0x17,
40 	0x17, 0x17, 0x18, 0x16, 0x16, 0x17, 0x07, 0x00,
41 
42 	0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17,
43 	0x17, 0x17, 0x04, 0x16, 0x18, 0x16, 0x18, 0x18,
44 };
45 
46 /**
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)
55  *
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.
59  */
ntfs_are_names_equal(wchar_t * s1,size_t s1_len,wchar_t * s2,size_t s2_len,int ic,wchar_t * upcase,__u32 upcase_size)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)
63 {
64 	if (s1_len != s2_len)
65 		return 0;
66 	if (!ic)
67 		return memcmp(s1, s2, s1_len << 1) ? 0: 1;
68 	return ntfs_wcsncasecmp(s1, s2, s1_len, upcase, upcase_size) ? 0: 1;
69 }
70 
71 /**
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
79  *
80  * ntfs_collate_names collates two Unicode names and returns:
81  *
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.
86  *
87  * The following characters are considered invalid: '"', '*', '<', '>' and '?'.
88  */
ntfs_collate_names(wchar_t * upcase,__u32 upcase_len,wchar_t * name1,__u32 name1_len,wchar_t * name2,__u32 name2_len,int ic,int err_val)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,
92 		       int ic, int err_val)
93 {
94 	__u32 cnt, min_len;
95 	wchar_t c1, c2;
96 
97 	min_len = name1_len;
98 	if (min_len > name2_len)
99 		min_len = name2_len;
100 	for (cnt = 0; cnt < min_len; ++cnt) {
101 		c1 = le16_to_cpu(*name1++);
102 		c2 = le16_to_cpu(*name2++);
103 		if (ic) {
104 			if (c1 < upcase_len)
105 				c1 = le16_to_cpu(upcase[c1]);
106 			if (c2 < upcase_len)
107 				c2 = le16_to_cpu(upcase[c2]);
108 		}
109 		if (c1 < 64 && legal_ansi_char_array[c1] & 8)
110 			return err_val;
111 		if (c1 < c2)
112 			return -1;
113 		if (c1 > c2)
114 			return 1;
115 		++name1;
116 		++name2;
117 	}
118 	if (name1_len < name2_len)
119 		return -1;
120 	if (name1_len == name2_len)
121 		return 0;
122 	/* name1_len > name2_len */
123 	c1 = le16_to_cpu(*name1);
124 	if (c1 < 64 && legal_ansi_char_array[c1] & 8)
125 		return err_val;
126 	return 1;
127 }
128 
129 /**
130  * ntfs_wcsncasecmp - compare two little endian Unicode strings, ignoring case
131  * @s1:			first string
132  * @s2:			second string
133  * @n:			maximum unicode characters to compare
134  * @upcase:		upcase table
135  * @upcase_size:	upcase table size in Unicode characters
136  *
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.
140  *
141  * Each character is uppercased using the @upcase table before the comparison.
142  *
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.
146  */
ntfs_wcsncasecmp(wchar_t * s1,wchar_t * s2,size_t n,wchar_t * upcase,__u32 upcase_size)147 int ntfs_wcsncasecmp(wchar_t *s1, wchar_t *s2, size_t n,
148 		     wchar_t *upcase, __u32 upcase_size)
149 {
150 	wchar_t c1, c2;
151 	size_t i;
152 
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]);
158 		if (c1 < c2)
159 			return -1;
160 		if (c1 > c2)
161 			return 1;
162 		if (!c1)
163 			break;
164 	}
165 	return 0;
166 }
167 
168