]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * unistr.c - NTFS Unicode string handling. Part of the Linux-NTFS project. | |
3 | * | |
c394e458 | 4 | * Copyright (c) 2001-2005 Anton Altaparmakov |
1da177e4 LT |
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 "types.h" | |
23 | #include "debug.h" | |
24 | #include "ntfs.h" | |
25 | ||
26 | /* | |
27 | * IMPORTANT | |
28 | * ========= | |
29 | * | |
30 | * All these routines assume that the Unicode characters are in little endian | |
31 | * encoding inside the strings!!! | |
32 | */ | |
33 | ||
34 | /* | |
35 | * This is used by the name collation functions to quickly determine what | |
36 | * characters are (in)valid. | |
37 | */ | |
38 | static const u8 legal_ansi_char_array[0x40] = { | |
39 | 0x00, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, | |
40 | 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, | |
41 | ||
42 | 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, | |
43 | 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, | |
44 | ||
45 | 0x17, 0x07, 0x18, 0x17, 0x17, 0x17, 0x17, 0x17, | |
46 | 0x17, 0x17, 0x18, 0x16, 0x16, 0x17, 0x07, 0x00, | |
47 | ||
48 | 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, | |
49 | 0x17, 0x17, 0x04, 0x16, 0x18, 0x16, 0x18, 0x18, | |
50 | }; | |
51 | ||
52 | /** | |
53 | * ntfs_are_names_equal - compare two Unicode names for equality | |
54 | * @s1: name to compare to @s2 | |
55 | * @s1_len: length in Unicode characters of @s1 | |
56 | * @s2: name to compare to @s1 | |
57 | * @s2_len: length in Unicode characters of @s2 | |
58 | * @ic: ignore case bool | |
59 | * @upcase: upcase table (only if @ic == IGNORE_CASE) | |
60 | * @upcase_size: length in Unicode characters of @upcase (if present) | |
61 | * | |
62 | * Compare the names @s1 and @s2 and return TRUE (1) if the names are | |
63 | * identical, or FALSE (0) if they are not identical. If @ic is IGNORE_CASE, | |
64 | * the @upcase table is used to performa a case insensitive comparison. | |
65 | */ | |
66 | BOOL ntfs_are_names_equal(const ntfschar *s1, size_t s1_len, | |
67 | const ntfschar *s2, size_t s2_len, const IGNORE_CASE_BOOL ic, | |
68 | const ntfschar *upcase, const u32 upcase_size) | |
69 | { | |
70 | if (s1_len != s2_len) | |
71 | return FALSE; | |
72 | if (ic == CASE_SENSITIVE) | |
73 | return !ntfs_ucsncmp(s1, s2, s1_len); | |
74 | return !ntfs_ucsncasecmp(s1, s2, s1_len, upcase, upcase_size); | |
75 | } | |
76 | ||
77 | /** | |
78 | * ntfs_collate_names - collate two Unicode names | |
79 | * @name1: first Unicode name to compare | |
80 | * @name2: second Unicode name to compare | |
81 | * @err_val: if @name1 contains an invalid character return this value | |
82 | * @ic: either CASE_SENSITIVE or IGNORE_CASE | |
83 | * @upcase: upcase table (ignored if @ic is CASE_SENSITIVE) | |
84 | * @upcase_len: upcase table size (ignored if @ic is CASE_SENSITIVE) | |
85 | * | |
86 | * ntfs_collate_names collates two Unicode names and returns: | |
87 | * | |
88 | * -1 if the first name collates before the second one, | |
89 | * 0 if the names match, | |
90 | * 1 if the second name collates before the first one, or | |
91 | * @err_val if an invalid character is found in @name1 during the comparison. | |
92 | * | |
93 | * The following characters are considered invalid: '"', '*', '<', '>' and '?'. | |
94 | */ | |
95 | int ntfs_collate_names(const ntfschar *name1, const u32 name1_len, | |
96 | const ntfschar *name2, const u32 name2_len, | |
97 | const int err_val, const IGNORE_CASE_BOOL ic, | |
98 | const ntfschar *upcase, const u32 upcase_len) | |
99 | { | |
100 | u32 cnt, min_len; | |
101 | u16 c1, c2; | |
102 | ||
103 | min_len = name1_len; | |
104 | if (name1_len > name2_len) | |
105 | min_len = name2_len; | |
106 | for (cnt = 0; cnt < min_len; ++cnt) { | |
107 | c1 = le16_to_cpu(*name1++); | |
108 | c2 = le16_to_cpu(*name2++); | |
109 | if (ic) { | |
110 | if (c1 < upcase_len) | |
111 | c1 = le16_to_cpu(upcase[c1]); | |
112 | if (c2 < upcase_len) | |
113 | c2 = le16_to_cpu(upcase[c2]); | |
114 | } | |
115 | if (c1 < 64 && legal_ansi_char_array[c1] & 8) | |
116 | return err_val; | |
117 | if (c1 < c2) | |
118 | return -1; | |
119 | if (c1 > c2) | |
120 | return 1; | |
121 | } | |
122 | if (name1_len < name2_len) | |
123 | return -1; | |
124 | if (name1_len == name2_len) | |
125 | return 0; | |
126 | /* name1_len > name2_len */ | |
127 | c1 = le16_to_cpu(*name1); | |
128 | if (c1 < 64 && legal_ansi_char_array[c1] & 8) | |
129 | return err_val; | |
130 | return 1; | |
131 | } | |
132 | ||
133 | /** | |
134 | * ntfs_ucsncmp - compare two little endian Unicode strings | |
135 | * @s1: first string | |
136 | * @s2: second string | |
137 | * @n: maximum unicode characters to compare | |
138 | * | |
139 | * Compare the first @n characters of the Unicode strings @s1 and @s2, | |
140 | * The strings in little endian format and appropriate le16_to_cpu() | |
141 | * conversion is performed on non-little endian machines. | |
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 | */ | |
147 | int ntfs_ucsncmp(const ntfschar *s1, const ntfschar *s2, size_t n) | |
148 | { | |
149 | u16 c1, c2; | |
150 | size_t i; | |
151 | ||
152 | for (i = 0; i < n; ++i) { | |
153 | c1 = le16_to_cpu(s1[i]); | |
154 | c2 = le16_to_cpu(s2[i]); | |
155 | if (c1 < c2) | |
156 | return -1; | |
157 | if (c1 > c2) | |
158 | return 1; | |
159 | if (!c1) | |
160 | break; | |
161 | } | |
162 | return 0; | |
163 | } | |
164 | ||
165 | /** | |
166 | * ntfs_ucsncasecmp - compare two little endian Unicode strings, ignoring case | |
167 | * @s1: first string | |
168 | * @s2: second string | |
169 | * @n: maximum unicode characters to compare | |
170 | * @upcase: upcase table | |
171 | * @upcase_size: upcase table size in Unicode characters | |
172 | * | |
173 | * Compare the first @n characters of the Unicode strings @s1 and @s2, | |
174 | * ignoring case. The strings in little endian format and appropriate | |
175 | * le16_to_cpu() conversion is performed on non-little endian machines. | |
176 | * | |
177 | * Each character is uppercased using the @upcase table before the comparison. | |
178 | * | |
179 | * The function returns an integer less than, equal to, or greater than zero | |
180 | * if @s1 (or the first @n Unicode characters thereof) is found, respectively, | |
181 | * to be less than, to match, or be greater than @s2. | |
182 | */ | |
183 | int ntfs_ucsncasecmp(const ntfschar *s1, const ntfschar *s2, size_t n, | |
184 | const ntfschar *upcase, const u32 upcase_size) | |
185 | { | |
186 | size_t i; | |
187 | u16 c1, c2; | |
188 | ||
189 | for (i = 0; i < n; ++i) { | |
190 | if ((c1 = le16_to_cpu(s1[i])) < upcase_size) | |
191 | c1 = le16_to_cpu(upcase[c1]); | |
192 | if ((c2 = le16_to_cpu(s2[i])) < upcase_size) | |
193 | c2 = le16_to_cpu(upcase[c2]); | |
194 | if (c1 < c2) | |
195 | return -1; | |
196 | if (c1 > c2) | |
197 | return 1; | |
198 | if (!c1) | |
199 | break; | |
200 | } | |
201 | return 0; | |
202 | } | |
203 | ||
204 | void ntfs_upcase_name(ntfschar *name, u32 name_len, const ntfschar *upcase, | |
205 | const u32 upcase_len) | |
206 | { | |
207 | u32 i; | |
208 | u16 u; | |
209 | ||
210 | for (i = 0; i < name_len; i++) | |
211 | if ((u = le16_to_cpu(name[i])) < upcase_len) | |
212 | name[i] = upcase[u]; | |
213 | } | |
214 | ||
215 | void ntfs_file_upcase_value(FILE_NAME_ATTR *file_name_attr, | |
216 | const ntfschar *upcase, const u32 upcase_len) | |
217 | { | |
218 | ntfs_upcase_name((ntfschar*)&file_name_attr->file_name, | |
219 | file_name_attr->file_name_length, upcase, upcase_len); | |
220 | } | |
221 | ||
222 | int ntfs_file_compare_values(FILE_NAME_ATTR *file_name_attr1, | |
223 | FILE_NAME_ATTR *file_name_attr2, | |
224 | const int err_val, const IGNORE_CASE_BOOL ic, | |
225 | const ntfschar *upcase, const u32 upcase_len) | |
226 | { | |
227 | return ntfs_collate_names((ntfschar*)&file_name_attr1->file_name, | |
228 | file_name_attr1->file_name_length, | |
229 | (ntfschar*)&file_name_attr2->file_name, | |
230 | file_name_attr2->file_name_length, | |
231 | err_val, ic, upcase, upcase_len); | |
232 | } | |
233 | ||
234 | /** | |
235 | * ntfs_nlstoucs - convert NLS string to little endian Unicode string | |
236 | * @vol: ntfs volume which we are working with | |
237 | * @ins: input NLS string buffer | |
238 | * @ins_len: length of input string in bytes | |
239 | * @outs: on return contains the allocated output Unicode string buffer | |
240 | * | |
241 | * Convert the input string @ins, which is in whatever format the loaded NLS | |
242 | * map dictates, into a little endian, 2-byte Unicode string. | |
243 | * | |
244 | * This function allocates the string and the caller is responsible for | |
245 | * calling kmem_cache_free(ntfs_name_cache, @outs); when finished with it. | |
246 | * | |
247 | * On success the function returns the number of Unicode characters written to | |
248 | * the output string *@outs (>= 0), not counting the terminating Unicode NULL | |
249 | * character. *@outs is set to the allocated output string buffer. | |
250 | * | |
251 | * On error, a negative number corresponding to the error code is returned. In | |
252 | * that case the output string is not allocated. Both *@outs and *@outs_len | |
253 | * are then undefined. | |
254 | * | |
255 | * This might look a bit odd due to fast path optimization... | |
256 | */ | |
257 | int ntfs_nlstoucs(const ntfs_volume *vol, const char *ins, | |
258 | const int ins_len, ntfschar **outs) | |
259 | { | |
260 | struct nls_table *nls = vol->nls_map; | |
261 | ntfschar *ucs; | |
262 | wchar_t wc; | |
263 | int i, o, wc_len; | |
264 | ||
265 | /* We don't trust outside sources. */ | |
266 | if (ins) { | |
2fb21db2 | 267 | ucs = kmem_cache_alloc(ntfs_name_cache, SLAB_NOFS); |
1da177e4 LT |
268 | if (ucs) { |
269 | for (i = o = 0; i < ins_len; i += wc_len) { | |
270 | wc_len = nls->char2uni(ins + i, ins_len - i, | |
271 | &wc); | |
272 | if (wc_len >= 0) { | |
273 | if (wc) { | |
274 | ucs[o++] = cpu_to_le16(wc); | |
275 | continue; | |
276 | } /* else (!wc) */ | |
277 | break; | |
278 | } /* else (wc_len < 0) */ | |
279 | goto conversion_err; | |
280 | } | |
281 | ucs[o] = 0; | |
282 | *outs = ucs; | |
283 | return o; | |
284 | } /* else (!ucs) */ | |
285 | ntfs_error(vol->sb, "Failed to allocate name from " | |
286 | "ntfs_name_cache!"); | |
287 | return -ENOMEM; | |
288 | } /* else (!ins) */ | |
289 | ntfs_error(NULL, "Received NULL pointer."); | |
290 | return -EINVAL; | |
291 | conversion_err: | |
292 | ntfs_error(vol->sb, "Name using character set %s contains characters " | |
293 | "that cannot be converted to Unicode.", nls->charset); | |
294 | kmem_cache_free(ntfs_name_cache, ucs); | |
295 | return -EILSEQ; | |
296 | } | |
297 | ||
298 | /** | |
299 | * ntfs_ucstonls - convert little endian Unicode string to NLS string | |
300 | * @vol: ntfs volume which we are working with | |
301 | * @ins: input Unicode string buffer | |
302 | * @ins_len: length of input string in Unicode characters | |
303 | * @outs: on return contains the (allocated) output NLS string buffer | |
304 | * @outs_len: length of output string buffer in bytes | |
305 | * | |
306 | * Convert the input little endian, 2-byte Unicode string @ins, of length | |
307 | * @ins_len into the string format dictated by the loaded NLS. | |
308 | * | |
309 | * If *@outs is NULL, this function allocates the string and the caller is | |
310 | * responsible for calling kfree(*@outs); when finished with it. In this case | |
311 | * @outs_len is ignored and can be 0. | |
312 | * | |
313 | * On success the function returns the number of bytes written to the output | |
314 | * string *@outs (>= 0), not counting the terminating NULL byte. If the output | |
315 | * string buffer was allocated, *@outs is set to it. | |
316 | * | |
317 | * On error, a negative number corresponding to the error code is returned. In | |
318 | * that case the output string is not allocated. The contents of *@outs are | |
319 | * then undefined. | |
320 | * | |
321 | * This might look a bit odd due to fast path optimization... | |
322 | */ | |
323 | int ntfs_ucstonls(const ntfs_volume *vol, const ntfschar *ins, | |
324 | const int ins_len, unsigned char **outs, int outs_len) | |
325 | { | |
326 | struct nls_table *nls = vol->nls_map; | |
327 | unsigned char *ns; | |
328 | int i, o, ns_len, wc; | |
329 | ||
330 | /* We don't trust outside sources. */ | |
331 | if (ins) { | |
332 | ns = *outs; | |
333 | ns_len = outs_len; | |
334 | if (ns && !ns_len) { | |
335 | wc = -ENAMETOOLONG; | |
336 | goto conversion_err; | |
337 | } | |
338 | if (!ns) { | |
339 | ns_len = ins_len * NLS_MAX_CHARSET_SIZE; | |
340 | ns = (unsigned char*)kmalloc(ns_len + 1, GFP_NOFS); | |
341 | if (!ns) | |
342 | goto mem_err_out; | |
343 | } | |
344 | for (i = o = 0; i < ins_len; i++) { | |
345 | retry: wc = nls->uni2char(le16_to_cpu(ins[i]), ns + o, | |
346 | ns_len - o); | |
347 | if (wc > 0) { | |
348 | o += wc; | |
349 | continue; | |
350 | } else if (!wc) | |
351 | break; | |
352 | else if (wc == -ENAMETOOLONG && ns != *outs) { | |
353 | unsigned char *tc; | |
354 | /* Grow in multiples of 64 bytes. */ | |
355 | tc = (unsigned char*)kmalloc((ns_len + 64) & | |
356 | ~63, GFP_NOFS); | |
357 | if (tc) { | |
358 | memcpy(tc, ns, ns_len); | |
359 | ns_len = ((ns_len + 64) & ~63) - 1; | |
360 | kfree(ns); | |
361 | ns = tc; | |
362 | goto retry; | |
363 | } /* No memory so goto conversion_error; */ | |
364 | } /* wc < 0, real error. */ | |
365 | goto conversion_err; | |
366 | } | |
367 | ns[o] = 0; | |
368 | *outs = ns; | |
369 | return o; | |
370 | } /* else (!ins) */ | |
371 | ntfs_error(vol->sb, "Received NULL pointer."); | |
372 | return -EINVAL; | |
373 | conversion_err: | |
374 | ntfs_error(vol->sb, "Unicode name contains characters that cannot be " | |
f94ad38e AA |
375 | "converted to character set %s. You might want to " |
376 | "try to use the mount option nls=utf8.", nls->charset); | |
1da177e4 LT |
377 | if (ns != *outs) |
378 | kfree(ns); | |
379 | if (wc != -ENAMETOOLONG) | |
380 | wc = -EILSEQ; | |
381 | return wc; | |
382 | mem_err_out: | |
383 | ntfs_error(vol->sb, "Failed to allocate name!"); | |
384 | return -ENOMEM; | |
385 | } |