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