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