1 /**
2 * unistr.c - Unicode string handling. Part of the Linux-NTFS project.
3 *
4 * Copyright (c) 2000-2006 Anton Altaparmakov
5 * Copyright (c) 2005-2007 Yura Pakhuchiy
6 *
7 * This program/include file is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program/include file is distributed in the hope that it will be
13 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program (in the main directory of the Linux-NTFS
19 * distribution in the file COPYING); if not, write to the Free Software
20 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #ifdef HAVE_STDIO_H
28 #include <stdio.h>
29 #endif
30 #ifdef HAVE_STDLIB_H
31 #include <stdlib.h>
32 #endif
33 #ifdef HAVE_WCHAR_H
34 #include <wchar.h>
35 #endif
36 #ifdef HAVE_STRING_H
37 #include <string.h>
38 #endif
39 #ifdef HAVE_ERRNO_H
40 #include <errno.h>
41 #endif
42
43 #include "compat.h"
44 #include "attrib.h"
45 #include "endians.h"
46 #include "types.h"
47 #include "unistr.h"
48 #include "debug.h"
49 #include "logging.h"
50
51 /*
52 * IMPORTANT
53 * =========
54 *
55 * All these routines assume that the Unicode characters are in little endian
56 * encoding inside the strings!!!
57 */
58
59 /*
60 * This is used by the name collation functions to quickly determine what
61 * characters are (in)valid.
62 */
63 #if 0
64 static const u8 legal_ansi_char_array[0x40] = {
65 0x00, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
66 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
67
68 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
69 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
70
71 0x17, 0x07, 0x18, 0x17, 0x17, 0x17, 0x17, 0x17,
72 0x17, 0x17, 0x18, 0x16, 0x16, 0x17, 0x07, 0x00,
73
74 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17,
75 0x17, 0x17, 0x04, 0x16, 0x18, 0x16, 0x18, 0x18,
76 };
77 #endif
78
79 /**
80 * ntfs_names_are_equal - compare two Unicode names for equality
81 * @s1: name to compare to @s2
82 * @s1_len: length in Unicode characters of @s1
83 * @s2: name to compare to @s1
84 * @s2_len: length in Unicode characters of @s2
85 * @ic: ignore case bool
86 * @upcase: upcase table (only if @ic == IGNORE_CASE)
87 * @upcase_size: length in Unicode characters of @upcase (if present)
88 *
89 * Compare the names @s1 and @s2 and return TRUE (1) if the names are
90 * identical, or FALSE (0) if they are not identical. If @ic is IGNORE_CASE,
91 * the @upcase table is used to perform a case insensitive comparison.
92 */
ntfs_names_are_equal(const ntfschar * s1,size_t s1_len,const ntfschar * s2,size_t s2_len,const IGNORE_CASE_BOOL ic,const ntfschar * upcase,const u32 upcase_size)93 BOOL ntfs_names_are_equal(const ntfschar *s1, size_t s1_len,
94 const ntfschar *s2, size_t s2_len,
95 const IGNORE_CASE_BOOL ic,
96 const ntfschar *upcase, const u32 upcase_size)
97 {
98 if (s1_len != s2_len)
99 return FALSE;
100 if (!s1_len)
101 return TRUE;
102 if (ic == CASE_SENSITIVE)
103 return ntfs_ucsncmp(s1, s2, s1_len) ? FALSE: TRUE;
104 return ntfs_ucsncasecmp(s1, s2, s1_len, upcase, upcase_size) ? FALSE:
105 TRUE;
106 }
107
108 /**
109 * ntfs_names_collate - collate two Unicode names
110 * @name1: first Unicode name to compare
111 * @name1_len: length of first Unicode name to compare
112 * @name2: second Unicode name to compare
113 * @name2_len: length of second Unicode name to compare
114 * @err_val: if @name1 contains an invalid character return this value
115 * @ic: either CASE_SENSITIVE or IGNORE_CASE
116 * @upcase: upcase table (ignored if @ic is CASE_SENSITIVE)
117 * @upcase_len: upcase table size (ignored if @ic is CASE_SENSITIVE)
118 *
119 * ntfs_names_collate() collates two Unicode names and returns:
120 *
121 * -1 if the first name collates before the second one,
122 * 0 if the names match,
123 * 1 if the second name collates before the first one, or
124 * @err_val if an invalid character is found in @name1 during the comparison.
125 *
126 * The following characters are considered invalid: '"', '*', '<', '>' and '?'.
127 */
ntfs_names_collate(const ntfschar * name1,const u32 name1_len,const ntfschar * name2,const u32 name2_len,const int err_val,const IGNORE_CASE_BOOL ic,const ntfschar * upcase,const u32 upcase_len)128 int ntfs_names_collate(const ntfschar *name1, const u32 name1_len,
129 const ntfschar *name2, const u32 name2_len,
130 const int err_val __attribute__((unused)),
131 const IGNORE_CASE_BOOL ic, const ntfschar *upcase,
132 const u32 upcase_len)
133 {
134 u32 cnt;
135 u16 c1, c2;
136
137 #ifdef DEBUG
138 if (!name1 || !name2 || (ic && (!upcase || !upcase_len))) {
139 ntfs_log_debug("ntfs_names_collate received NULL pointer!\n");
140 exit(1);
141 }
142 #endif
143 for (cnt = 0; cnt < min(name1_len, name2_len); ++cnt) {
144 c1 = le16_to_cpu(*name1);
145 name1++;
146 c2 = le16_to_cpu(*name2);
147 name2++;
148 if (ic) {
149 if (c1 < upcase_len)
150 c1 = le16_to_cpu(upcase[c1]);
151 if (c2 < upcase_len)
152 c2 = le16_to_cpu(upcase[c2]);
153 }
154 #if 0
155 if (c1 < 64 && legal_ansi_char_array[c1] & 8)
156 return err_val;
157 #endif
158 if (c1 < c2)
159 return -1;
160 if (c1 > c2)
161 return 1;
162 }
163 if (name1_len < name2_len)
164 return -1;
165 if (name1_len == name2_len)
166 return 0;
167 /* name1_len > name2_len */
168 #if 0
169 c1 = le16_to_cpu(*name1);
170 if (c1 < 64 && legal_ansi_char_array[c1] & 8)
171 return err_val;
172 #endif
173 return 1;
174 }
175
176 /**
177 * ntfs_ucsncmp - compare two little endian Unicode strings
178 * @s1: first string
179 * @s2: second string
180 * @n: maximum unicode characters to compare
181 *
182 * Compare the first @n characters of the Unicode strings @s1 and @s2,
183 * The strings in little endian format and appropriate le16_to_cpu()
184 * conversion is performed on non-little endian machines.
185 *
186 * The function returns an integer less than, equal to, or greater than zero
187 * if @s1 (or the first @n Unicode characters thereof) is found, respectively,
188 * to be less than, to match, or be greater than @s2.
189 */
ntfs_ucsncmp(const ntfschar * s1,const ntfschar * s2,size_t n)190 int ntfs_ucsncmp(const ntfschar *s1, const ntfschar *s2, size_t n)
191 {
192 u16 c1, c2;
193 size_t i;
194
195 #ifdef DEBUG
196 if (!s1 || !s2) {
197 ntfs_log_debug("ntfs_wcsncmp() received NULL pointer!\n");
198 exit(1);
199 }
200 #endif
201 for (i = 0; i < n; ++i) {
202 c1 = le16_to_cpu(s1[i]);
203 c2 = le16_to_cpu(s2[i]);
204 if (c1 < c2)
205 return -1;
206 if (c1 > c2)
207 return 1;
208 if (!c1)
209 break;
210 }
211 return 0;
212 }
213
214 /**
215 * ntfs_ucsncasecmp - compare two little endian Unicode strings, ignoring case
216 * @s1: first string
217 * @s2: second string
218 * @n: maximum unicode characters to compare
219 * @upcase: upcase table
220 * @upcase_size: upcase table size in Unicode characters
221 *
222 * Compare the first @n characters of the Unicode strings @s1 and @s2,
223 * ignoring case. The strings in little endian format and appropriate
224 * le16_to_cpu() conversion is performed on non-little endian machines.
225 *
226 * Each character is uppercased using the @upcase table before the comparison.
227 *
228 * The function returns an integer less than, equal to, or greater than zero
229 * if @s1 (or the first @n Unicode characters thereof) is found, respectively,
230 * to be less than, to match, or be greater than @s2.
231 */
ntfs_ucsncasecmp(const ntfschar * s1,const ntfschar * s2,size_t n,const ntfschar * upcase,const u32 upcase_size)232 int ntfs_ucsncasecmp(const ntfschar *s1, const ntfschar *s2, size_t n,
233 const ntfschar *upcase, const u32 upcase_size)
234 {
235 u16 c1, c2;
236 size_t i;
237
238 #ifdef DEBUG
239 if (!s1 || !s2 || !upcase) {
240 ntfs_log_debug("ntfs_wcsncasecmp() received NULL pointer!\n");
241 exit(1);
242 }
243 #endif
244 for (i = 0; i < n; ++i) {
245 if ((c1 = le16_to_cpu(s1[i])) < upcase_size)
246 c1 = le16_to_cpu(upcase[c1]);
247 if ((c2 = le16_to_cpu(s2[i])) < upcase_size)
248 c2 = le16_to_cpu(upcase[c2]);
249 if (c1 < c2)
250 return -1;
251 if (c1 > c2)
252 return 1;
253 if (!c1)
254 break;
255 }
256 return 0;
257 }
258
259 /**
260 * ntfs_ucsnlen - determine the length of a little endian Unicode string
261 * @s: pointer to Unicode string
262 * @maxlen: maximum length of string @s
263 *
264 * Return the number of Unicode characters in the little endian Unicode
265 * string @s up to a maximum of maxlen Unicode characters, not including
266 * the terminating (ntfschar)'\0'. If there is no (ntfschar)'\0' between @s
267 * and @s + @maxlen, @maxlen is returned.
268 *
269 * This function never looks beyond @s + @maxlen.
270 */
ntfs_ucsnlen(const ntfschar * s,u32 maxlen)271 u32 ntfs_ucsnlen(const ntfschar *s, u32 maxlen)
272 {
273 u32 i;
274
275 for (i = 0; i < maxlen; i++) {
276 if (!le16_to_cpu(s[i]))
277 break;
278 }
279 return i;
280 }
281
282 /**
283 * ntfs_ucsndup - duplicate little endian Unicode string
284 * @s: pointer to Unicode string
285 * @maxlen: maximum length of string @s
286 *
287 * Return a pointer to a new little endian Unicode string which is a duplicate
288 * of the string s. Memory for the new string is obtained with malloc(3), and
289 * can be freed with free(3).
290 *
291 * A maximum of @maxlen Unicode characters are copied and a terminating
292 * (ntfschar)'\0' little endian Unicode character is added.
293 *
294 * This function never looks beyond @s + @maxlen.
295 *
296 * Return a pointer to the new little endian Unicode string on success and NULL
297 * on failure with errno set to the error code.
298 */
ntfs_ucsndup(const ntfschar * s,u32 maxlen)299 ntfschar *ntfs_ucsndup(const ntfschar *s, u32 maxlen)
300 {
301 ntfschar *dst;
302 u32 len;
303
304 len = ntfs_ucsnlen(s, maxlen);
305 dst = ntfs_malloc((len + 1) * sizeof(ntfschar));
306 if (dst) {
307 memcpy(dst, s, len * sizeof(ntfschar));
308 dst[len] = 0;
309 }
310 return dst;
311 }
312
313 /**
314 * ntfs_name_upcase - Map an Unicode name to its uppercase equivalent
315 * @name:
316 * @name_len:
317 * @upcase:
318 * @upcase_len:
319 *
320 * Description...
321 *
322 * Returns:
323 */
ntfs_name_upcase(ntfschar * name,u32 name_len,const ntfschar * upcase,const u32 upcase_len)324 void ntfs_name_upcase(ntfschar *name, u32 name_len, const ntfschar *upcase,
325 const u32 upcase_len)
326 {
327 u32 i;
328 u16 u;
329
330 for (i = 0; i < name_len; i++)
331 if ((u = le16_to_cpu(name[i])) < upcase_len)
332 name[i] = upcase[u];
333 }
334
335 /**
336 * ntfs_file_value_upcase - Convert a filename to upper case
337 * @file_name_attr:
338 * @upcase:
339 * @upcase_len:
340 *
341 * Description...
342 *
343 * Returns:
344 */
ntfs_file_value_upcase(FILE_NAME_ATTR * file_name_attr,const ntfschar * upcase,const u32 upcase_len)345 void ntfs_file_value_upcase(FILE_NAME_ATTR *file_name_attr,
346 const ntfschar *upcase, const u32 upcase_len)
347 {
348 ntfs_name_upcase((ntfschar*)&file_name_attr->file_name,
349 file_name_attr->file_name_length, upcase, upcase_len);
350 }
351
352 /**
353 * ntfs_file_values_compare - Which of two filenames should be listed first
354 * @file_name_attr1:
355 * @file_name_attr2:
356 * @err_val:
357 * @ic:
358 * @upcase:
359 * @upcase_len:
360 *
361 * Description...
362 *
363 * Returns:
364 */
ntfs_file_values_compare(const FILE_NAME_ATTR * file_name_attr1,const FILE_NAME_ATTR * file_name_attr2,const int err_val,const IGNORE_CASE_BOOL ic,const ntfschar * upcase,const u32 upcase_len)365 int ntfs_file_values_compare(const FILE_NAME_ATTR *file_name_attr1,
366 const FILE_NAME_ATTR *file_name_attr2,
367 const int err_val, const IGNORE_CASE_BOOL ic,
368 const ntfschar *upcase, const u32 upcase_len)
369 {
370 return ntfs_names_collate((ntfschar*)&file_name_attr1->file_name,
371 file_name_attr1->file_name_length,
372 (ntfschar*)&file_name_attr2->file_name,
373 file_name_attr2->file_name_length,
374 err_val, ic, upcase, upcase_len);
375 }
376
377 /**
378 * ntfs_ucstombs - convert a little endian Unicode string to a multibyte string
379 * @ins: input Unicode string buffer
380 * @ins_len: length of input string in Unicode characters
381 * @outs: on return contains the (allocated) output multibyte string
382 * @outs_len: length of output buffer in bytes
383 *
384 * Convert the input little endian, 2-byte Unicode string @ins, of length
385 * @ins_len into the multibyte string format dictated by the current locale.
386 *
387 * If *@outs is NULL, the function allocates the string and the caller is
388 * responsible for calling free(*@outs); when finished with it.
389 *
390 * On success the function returns the number of bytes written to the output
391 * string *@outs (>= 0), not counting the terminating NULL byte. If the output
392 * string buffer was allocated, *@outs is set to it.
393 *
394 * On error, -1 is returned, and errno is set to the error code. The following
395 * error codes can be expected:
396 * EINVAL Invalid arguments (e.g. @ins or @outs is NULL).
397 * EILSEQ The input string cannot be represented as a multibyte
398 * sequence according to the current locale.
399 * ENAMETOOLONG Destination buffer is too small for input string.
400 * ENOMEM Not enough memory to allocate destination buffer.
401 */
ntfs_ucstombs(const ntfschar * ins,const int ins_len,char ** outs,int outs_len)402 int ntfs_ucstombs(const ntfschar *ins, const int ins_len, char **outs,
403 int outs_len)
404 {
405 char *mbs;
406 wchar_t wc;
407 int i, o, mbs_len;
408 int cnt = 0;
409 #ifdef HAVE_MBSINIT
410 mbstate_t mbstate;
411 #endif
412
413 if (!ins || !outs) {
414 errno = EINVAL;
415 return -1;
416 }
417 mbs = *outs;
418 mbs_len = outs_len;
419 if (mbs && !mbs_len) {
420 errno = ENAMETOOLONG;
421 return -1;
422 }
423 if (!mbs) {
424 mbs_len = (ins_len + 1) * MB_CUR_MAX;
425 mbs = (char*)ntfs_malloc(mbs_len);
426 if (!mbs)
427 return -1;
428 }
429 #ifdef HAVE_MBSINIT
430 memset(&mbstate, 0, sizeof(mbstate));
431 #else
432 wctomb(NULL, 0);
433 #endif
434 for (i = o = 0; i < ins_len; i++) {
435 /* Reallocate memory if necessary or abort. */
436 if ((int)(o + MB_CUR_MAX) > mbs_len) {
437 char *tc;
438 if (mbs == *outs) {
439 errno = ENAMETOOLONG;
440 return -1;
441 }
442 tc = (char*)ntfs_malloc((mbs_len + 64) & ~63);
443 if (!tc)
444 goto err_out;
445 memcpy(tc, mbs, mbs_len);
446 mbs_len = (mbs_len + 64) & ~63;
447 free(mbs);
448 mbs = tc;
449 }
450 /* Convert the LE Unicode character to a CPU wide character. */
451 wc = (wchar_t)le16_to_cpu(ins[i]);
452 if (!wc)
453 break;
454 /* Convert the CPU endian wide character to multibyte. */
455 #ifdef HAVE_MBSINIT
456 cnt = wcrtomb(mbs + o, wc, &mbstate);
457 #else
458 cnt = wctomb(mbs + o, wc);
459 #endif
460 if (cnt == -1)
461 goto err_out;
462 if (cnt <= 0) {
463 ntfs_log_debug("Eeek. cnt <= 0, cnt = %i\n", cnt);
464 errno = EINVAL;
465 goto err_out;
466 }
467 o += cnt;
468 }
469 #ifdef HAVE_MBSINIT
470 /* Make sure we are back in the initial state. */
471 if (!mbsinit(&mbstate)) {
472 ntfs_log_debug("Eeek. mbstate not in initial state!\n");
473 errno = EILSEQ;
474 goto err_out;
475 }
476 #endif
477 /* Now write the NULL character. */
478 mbs[o] = 0;
479 if (*outs != mbs)
480 *outs = mbs;
481 return o;
482 err_out:
483 if (mbs != *outs)
484 free(mbs);
485 return -1;
486 }
487
488 /**
489 * ntfs_mbstoucs - convert a multibyte string to a little endian Unicode string
490 * @ins: input multibyte string buffer
491 * @outs: on return contains the (allocated) output Unicode string
492 * @outs_len: length of output buffer in Unicode characters
493 *
494 * Convert the input multibyte string @ins, from the current locale into the
495 * corresponding little endian, 2-byte Unicode string.
496 *
497 * If *@outs is NULL, the function allocates the string and the caller is
498 * responsible for calling free(*@outs); when finished with it.
499 *
500 * On success the function returns the number of Unicode characters written to
501 * the output string *@outs (>= 0), not counting the terminating Unicode NULL
502 * character. If the output string buffer was allocated, *@outs is set to it.
503 *
504 * On error, -1 is returned, and errno is set to the error code. The following
505 * error codes can be expected:
506 * EINVAL Invalid arguments (e.g. @ins or @outs is NULL).
507 * EILSEQ The input string cannot be represented as a Unicode
508 * string according to the current locale.
509 * ENAMETOOLONG Destination buffer is too small for input string.
510 * ENOMEM Not enough memory to allocate destination buffer.
511 */
ntfs_mbstoucs(const char * ins,ntfschar ** outs,int outs_len)512 int ntfs_mbstoucs(const char *ins, ntfschar **outs, int outs_len)
513 {
514 ntfschar *ucs;
515 const char *s;
516 wchar_t wc;
517 int i, o, cnt, ins_len, ucs_len, ins_size;
518 #ifdef HAVE_MBSINIT
519 mbstate_t mbstate;
520 #endif
521
522 if (!ins || !outs) {
523 errno = EINVAL;
524 return -1;
525 }
526 ucs = *outs;
527 ucs_len = outs_len;
528 if (ucs && !ucs_len) {
529 errno = ENAMETOOLONG;
530 return -1;
531 }
532 /* Determine the size of the multi-byte string in bytes. */
533 ins_size = strlen(ins);
534 /* Determine the length of the multi-byte string. */
535 s = ins;
536 #if defined(HAVE_MBSINIT)
537 memset(&mbstate, 0, sizeof(mbstate));
538 ins_len = mbsrtowcs(NULL, (const char **)&s, 0, &mbstate);
539 #ifdef __CYGWIN32__
540 if (!ins_len && *ins) {
541 /* Older Cygwin had broken mbsrtowcs() implementation. */
542 ins_len = strlen(ins);
543 }
544 #endif
545 #elif !defined(DJGPP)
546 ins_len = mbstowcs(NULL, s, 0);
547 #else
548 /* Eeek!!! DJGPP has broken mbstowcs() implementation!!! */
549 ins_len = strlen(ins);
550 #endif
551 if (ins_len == -1)
552 return ins_len;
553 #ifdef HAVE_MBSINIT
554 if ((s != ins) || !mbsinit(&mbstate)) {
555 #else
556 if (s != ins) {
557 #endif
558 errno = EILSEQ;
559 return -1;
560 }
561 /* Add the NULL terminator. */
562 ins_len++;
563 if (!ucs) {
564 ucs_len = ins_len;
565 ucs = (ntfschar*)ntfs_malloc(ucs_len * sizeof(ntfschar));
566 if (!ucs)
567 return -1;
568 }
569 #ifdef HAVE_MBSINIT
570 memset(&mbstate, 0, sizeof(mbstate));
571 #else
572 mbtowc(NULL, NULL, 0);
573 #endif
574 for (i = o = cnt = 0; i < ins_size; i += cnt, o++) {
575 /* Reallocate memory if necessary or abort. */
576 if (o >= ucs_len) {
577 ntfschar *tc;
578 if (ucs == *outs) {
579 errno = ENAMETOOLONG;
580 return -1;
581 }
582 /*
583 * We will never get here but hey, it's only a bit of
584 * extra code...
585 */
586 ucs_len = (ucs_len * sizeof(ntfschar) + 64) & ~63;
587 tc = (ntfschar*)realloc(ucs, ucs_len);
588 if (!tc)
589 goto err_out;
590 ucs = tc;
591 ucs_len /= sizeof(ntfschar);
592 }
593 /* Convert the multibyte character to a wide character. */
594 #ifdef HAVE_MBSINIT
595 cnt = mbrtowc(&wc, ins + i, ins_size - i, &mbstate);
596 #else
597 cnt = mbtowc(&wc, ins + i, ins_size - i);
598 #endif
599 if (!cnt)
600 break;
601 if (cnt == -1)
602 goto err_out;
603 if (cnt < -1) {
604 ntfs_log_trace("Eeek. cnt = %i\n", cnt);
605 errno = EINVAL;
606 goto err_out;
607 }
608 /* Make sure we are not overflowing the NTFS Unicode set. */
609 if ((unsigned long)wc >= (unsigned long)(1 <<
610 (8 * sizeof(ntfschar)))) {
611 errno = EILSEQ;
612 goto err_out;
613 }
614 /* Convert the CPU wide character to a LE Unicode character. */
615 ucs[o] = cpu_to_le16(wc);
616 }
617 #ifdef HAVE_MBSINIT
618 /* Make sure we are back in the initial state. */
619 if (!mbsinit(&mbstate)) {
620 ntfs_log_trace("Eeek. mbstate not in initial state!\n");
621 errno = EILSEQ;
622 goto err_out;
623 }
624 #endif
625 /* Now write the NULL character. */
626 ucs[o] = 0;
627 if (*outs != ucs)
628 *outs = ucs;
629 return o;
630 err_out:
631 if (ucs != *outs)
632 free(ucs);
633 return -1;
634 }
635
636 /**
637 * ntfs_upcase_table_build - build the default upcase table for NTFS
638 * @uc: destination buffer where to store the built table
639 * @uc_len: size of destination buffer in bytes
640 *
641 * ntfs_upcase_table_build() builds the default upcase table for NTFS and
642 * stores it in the caller supplied buffer @uc of size @uc_len.
643 *
644 * The generated $UpCase table is the one used by Windows Vista.
645 *
646 * Note, @uc_len must be at least 128kiB in size or bad things will happen!
647 */
648 void ntfs_upcase_table_build(ntfschar *uc, u32 uc_len)
649 {
650 /*
651 * "Start" is inclusive and "End" is exclusive, every value has the
652 * value of "Add" added to it.
653 */
654 static int add[][3] = { /* Start, End, Add */
655 {0x0061, 0x007b, -32}, {0x00e0, 0x00f7, -32}, {0x00f8, 0x00ff, -32},
656 {0x0256, 0x0258, -205}, {0x028a, 0x028c, -217}, {0x037b, 0x037e, 130},
657 {0x03ac, 0x03ad, -38}, {0x03ad, 0x03b0, -37}, {0x03b1, 0x03c2, -32},
658 {0x03c2, 0x03c3, -31}, {0x03c3, 0x03cc, -32}, {0x03cc, 0x03cd, -64},
659 {0x03cd, 0x03cf, -63}, {0x0430, 0x0450, -32}, {0x0450, 0x0460, -80},
660 {0x0561, 0x0587, -48}, {0x1f00, 0x1f08, 8}, {0x1f10, 0x1f16, 8},
661 {0x1f20, 0x1f28, 8}, {0x1f30, 0x1f38, 8}, {0x1f40, 0x1f46, 8},
662 {0x1f51, 0x1f52, 8}, {0x1f53, 0x1f54, 8}, {0x1f55, 0x1f56, 8},
663 {0x1f57, 0x1f58, 8}, {0x1f60, 0x1f68, 8}, {0x1f70, 0x1f72, 74},
664 {0x1f72, 0x1f76, 86}, {0x1f76, 0x1f78, 100}, {0x1f78, 0x1f7a, 128},
665 {0x1f7a, 0x1f7c, 112}, {0x1f7c, 0x1f7e, 126}, {0x1f80, 0x1f88, 8},
666 {0x1f90, 0x1f98, 8}, {0x1fa0, 0x1fa8, 8}, {0x1fb0, 0x1fb2, 8},
667 {0x1fb3, 0x1fb4, 9}, {0x1fcc, 0x1fcd, -9}, {0x1fd0, 0x1fd2, 8},
668 {0x1fe0, 0x1fe2, 8}, {0x1fe5, 0x1fe6, 7}, {0x1ffc, 0x1ffd, -9},
669 {0x2170, 0x2180, -16}, {0x24d0, 0x24ea, -26}, {0x2c30, 0x2c5f, -48},
670 {0x2d00, 0x2d26, -7264}, {0xff41, 0xff5b, -32}, {0}
671 };
672 /*
673 * "Start" is exclusive and "End" is inclusive, every second value is
674 * decremented by one.
675 */
676 static int skip_dec[][2] = { /* Start, End */
677 {0x0100, 0x012f}, {0x0132, 0x0137}, {0x0139, 0x0149}, {0x014a, 0x0178},
678 {0x0179, 0x017e}, {0x01a0, 0x01a6}, {0x01b3, 0x01b7}, {0x01cd, 0x01dd},
679 {0x01de, 0x01ef}, {0x01f4, 0x01f5}, {0x01f8, 0x01f9}, {0x01fa, 0x0220},
680 {0x0222, 0x0234}, {0x023b, 0x023c}, {0x0241, 0x0242}, {0x0246, 0x024f},
681 {0x03d8, 0x03ef}, {0x03f7, 0x03f8}, {0x03fa, 0x03fb}, {0x0460, 0x0481},
682 {0x048a, 0x04bf}, {0x04c1, 0x04c4}, {0x04c5, 0x04c8}, {0x04c9, 0x04ce},
683 {0x04ec, 0x04ed}, {0x04d0, 0x04eb}, {0x04ee, 0x04f5}, {0x04f6, 0x0513},
684 {0x1e00, 0x1e95}, {0x1ea0, 0x1ef9}, {0x2183, 0x2184}, {0x2c60, 0x2c61},
685 {0x2c67, 0x2c6c}, {0x2c75, 0x2c76}, {0x2c80, 0x2ce3}, {0}
686 };
687 /*
688 * Set the Unicode character at offset "Offset" to "Value". Note,
689 * "Value" is host endian.
690 */
691 static int set[][2] = { /* Offset, Value */
692 {0x00ff, 0x0178}, {0x0180, 0x0243}, {0x0183, 0x0182}, {0x0185, 0x0184},
693 {0x0188, 0x0187}, {0x018c, 0x018b}, {0x0192, 0x0191}, {0x0195, 0x01f6},
694 {0x0199, 0x0198}, {0x019a, 0x023d}, {0x019e, 0x0220}, {0x01a8, 0x01a7},
695 {0x01ad, 0x01ac}, {0x01b0, 0x01af}, {0x01b9, 0x01b8}, {0x01bd, 0x01bc},
696 {0x01bf, 0x01f7}, {0x01c6, 0x01c4}, {0x01c9, 0x01c7}, {0x01cc, 0x01ca},
697 {0x01dd, 0x018e}, {0x01f3, 0x01f1}, {0x023a, 0x2c65}, {0x023e, 0x2c66},
698 {0x0253, 0x0181}, {0x0254, 0x0186}, {0x0259, 0x018f}, {0x025b, 0x0190},
699 {0x0260, 0x0193}, {0x0263, 0x0194}, {0x0268, 0x0197}, {0x0269, 0x0196},
700 {0x026b, 0x2c62}, {0x026f, 0x019c}, {0x0272, 0x019d}, {0x0275, 0x019f},
701 {0x027d, 0x2c64}, {0x0280, 0x01a6}, {0x0283, 0x01a9}, {0x0288, 0x01ae},
702 {0x0289, 0x0244}, {0x028c, 0x0245}, {0x0292, 0x01b7}, {0x03f2, 0x03f9},
703 {0x04cf, 0x04c0}, {0x1d7d, 0x2c63}, {0x214e, 0x2132}, {0}
704 };
705 unsigned i, r;
706
707 memset(uc, 0, uc_len);
708 uc_len /= 2;
709 /* Start with a one-to-one mapping, i.e. no upcasing happens at all. */
710 for (i = 0; i < uc_len; i++)
711 uc[i] = cpu_to_le16(i);
712 /* Adjust specified runs by the specified amount. */
713 for (r = 0; add[r][0]; r++)
714 for (i = add[r][0]; i < add[r][1]; i++)
715 uc[i] = cpu_to_le16(le16_to_cpu(uc[i]) + add[r][2]);
716 /* Decrement every second value in specified runs. */
717 for (r = 0; skip_dec[r][0]; r++)
718 for (i = skip_dec[r][0]; i < skip_dec[r][1];
719 i += 2)
720 uc[i + 1] = cpu_to_le16(le16_to_cpu(uc[i + 1]) - 1);
721 /* Set specified characters to specified values. */
722 for (r = 0; set[r][0]; r++)
723 uc[set[r][0]] = cpu_to_le16(set[r][1]);
724 }
725
726 /**
727 * ntfs_str2ucs - convert a string to a valid NTFS file name
728 * @s: input string
729 * @len: length of output buffer in Unicode characters
730 *
731 * Convert the input @s string into the corresponding little endian,
732 * 2-byte Unicode string. The length of the converted string is less
733 * or equal to the maximum length allowed by the NTFS format (255).
734 *
735 * If @s is NULL then return AT_UNNAMED.
736 *
737 * On success the function returns the Unicode string in an allocated
738 * buffer and the caller is responsible to free it when it's not needed
739 * anymore.
740 *
741 * On error NULL is returned and errno is set to the error code.
742 */
743 ntfschar *ntfs_str2ucs(const char *s, int *len)
744 {
745 ntfschar *ucs = NULL;
746
747 if (s && ((*len = ntfs_mbstoucs(s, &ucs, 0)) == -1)) {
748 ntfs_log_perror("Couldn't convert '%s' to Unicode", s);
749 return NULL;
750 }
751 if (*len > NTFS_MAX_NAME_LEN) {
752 free(ucs);
753 errno = ENAMETOOLONG;
754 return NULL;
755 }
756 if (!ucs || !*len) {
757 ucs = AT_UNNAMED;
758 *len = 0;
759 }
760 return ucs;
761 }
762
763 /**
764 * ntfs_ucsfree - free memory allocated by ntfs_str2ucs()
765 * @ucs: input string to be freed
766 *
767 * Free memory at @ucs and which was allocated by ntfs_str2ucs.
768 *
769 * Return value: none.
770 */
771 void ntfs_ucsfree(ntfschar *ucs)
772 {
773 if (ucs && (ucs != AT_UNNAMED))
774 free(ucs);
775 }
776
777