2
* unistr.c - Unicode string handling. Part of the Linux-NTFS project.
4
* Copyright (c) 2000-2004 Anton Altaparmakov
5
* Copyright (c) 2002-2006 Szabolcs Szakacsits
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.
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.
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
54
* All these routines assume that the Unicode characters are in little endian
55
* encoding inside the strings!!!
59
* This is used by the name collation functions to quickly determine what
60
* characters are (in)valid.
63
static const u8 legal_ansi_char_array[0x40] = {
64
0x00, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
65
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
67
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
68
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
70
0x17, 0x07, 0x18, 0x17, 0x17, 0x17, 0x17, 0x17,
71
0x17, 0x17, 0x18, 0x16, 0x16, 0x17, 0x07, 0x00,
73
0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17,
74
0x17, 0x17, 0x04, 0x16, 0x18, 0x16, 0x18, 0x18,
79
* ntfs_names_are_equal - compare two Unicode names for equality
80
* @s1: name to compare to @s2
81
* @s1_len: length in Unicode characters of @s1
82
* @s2: name to compare to @s1
83
* @s2_len: length in Unicode characters of @s2
84
* @ic: ignore case bool
85
* @upcase: upcase table (only if @ic == IGNORE_CASE)
86
* @upcase_size: length in Unicode characters of @upcase (if present)
88
* Compare the names @s1 and @s2 and return TRUE (1) if the names are
89
* identical, or FALSE (0) if they are not identical. If @ic is IGNORE_CASE,
90
* the @upcase table is used to perform a case insensitive comparison.
92
BOOL ntfs_names_are_equal(const ntfschar *s1, size_t s1_len,
93
const ntfschar *s2, size_t s2_len,
94
const IGNORE_CASE_BOOL ic,
95
const ntfschar *upcase, const u32 upcase_size)
101
if (ic == CASE_SENSITIVE)
102
return ntfs_ucsncmp(s1, s2, s1_len) ? FALSE: TRUE;
103
return ntfs_ucsncasecmp(s1, s2, s1_len, upcase, upcase_size) ? FALSE:
108
* ntfs_names_collate - collate two Unicode names
109
* @name1: first Unicode name to compare
110
* @name1_len: length of first Unicode name to compare
111
* @name2: second Unicode name to compare
112
* @name2_len: length of second Unicode name to compare
113
* @err_val: if @name1 contains an invalid character return this value
114
* @ic: either CASE_SENSITIVE or IGNORE_CASE
115
* @upcase: upcase table (ignored if @ic is CASE_SENSITIVE)
116
* @upcase_len: upcase table size (ignored if @ic is CASE_SENSITIVE)
118
* ntfs_names_collate() collates two Unicode names and returns:
120
* -1 if the first name collates before the second one,
121
* 0 if the names match,
122
* 1 if the second name collates before the first one, or
123
* @err_val if an invalid character is found in @name1 during the comparison.
125
* The following characters are considered invalid: '"', '*', '<', '>' and '?'.
127
int ntfs_names_collate(const ntfschar *name1, const u32 name1_len,
128
const ntfschar *name2, const u32 name2_len,
129
const int err_val __attribute__((unused)),
130
const IGNORE_CASE_BOOL ic, const ntfschar *upcase,
131
const u32 upcase_len)
137
if (!name1 || !name2 || (ic && (!upcase || !upcase_len))) {
138
ntfs_log_debug("ntfs_names_collate received NULL pointer!\n");
142
for (cnt = 0; cnt < min(name1_len, name2_len); ++cnt) {
143
c1 = le16_to_cpu(*name1);
145
c2 = le16_to_cpu(*name2);
149
c1 = le16_to_cpu(upcase[c1]);
151
c2 = le16_to_cpu(upcase[c2]);
154
if (c1 < 64 && legal_ansi_char_array[c1] & 8)
162
if (name1_len < name2_len)
164
if (name1_len == name2_len)
166
/* name1_len > name2_len */
168
c1 = le16_to_cpu(*name1);
169
if (c1 < 64 && legal_ansi_char_array[c1] & 8)
176
* ntfs_ucsncmp - compare two little endian Unicode strings
179
* @n: maximum unicode characters to compare
181
* Compare the first @n characters of the Unicode strings @s1 and @s2,
182
* The strings in little endian format and appropriate le16_to_cpu()
183
* conversion is performed on non-little endian machines.
185
* The function returns an integer less than, equal to, or greater than zero
186
* if @s1 (or the first @n Unicode characters thereof) is found, respectively,
187
* to be less than, to match, or be greater than @s2.
189
int ntfs_ucsncmp(const ntfschar *s1, const ntfschar *s2, size_t n)
196
ntfs_log_debug("ntfs_wcsncmp() received NULL pointer!\n");
200
for (i = 0; i < n; ++i) {
201
c1 = le16_to_cpu(s1[i]);
202
c2 = le16_to_cpu(s2[i]);
214
* ntfs_ucsncasecmp - compare two little endian Unicode strings, ignoring case
217
* @n: maximum unicode characters to compare
218
* @upcase: upcase table
219
* @upcase_size: upcase table size in Unicode characters
221
* Compare the first @n characters of the Unicode strings @s1 and @s2,
222
* ignoring case. The strings in little endian format and appropriate
223
* le16_to_cpu() conversion is performed on non-little endian machines.
225
* Each character is uppercased using the @upcase table before the comparison.
227
* The function returns an integer less than, equal to, or greater than zero
228
* if @s1 (or the first @n Unicode characters thereof) is found, respectively,
229
* to be less than, to match, or be greater than @s2.
231
int ntfs_ucsncasecmp(const ntfschar *s1, const ntfschar *s2, size_t n,
232
const ntfschar *upcase, const u32 upcase_size)
238
if (!s1 || !s2 || !upcase) {
239
ntfs_log_debug("ntfs_wcsncasecmp() received NULL pointer!\n");
243
for (i = 0; i < n; ++i) {
244
if ((c1 = le16_to_cpu(s1[i])) < upcase_size)
245
c1 = le16_to_cpu(upcase[c1]);
246
if ((c2 = le16_to_cpu(s2[i])) < upcase_size)
247
c2 = le16_to_cpu(upcase[c2]);
259
* ntfs_ucsnlen - determine the length of a little endian Unicode string
260
* @s: pointer to Unicode string
261
* @maxlen: maximum length of string @s
263
* Return the number of Unicode characters in the little endian Unicode
264
* string @s up to a maximum of maxlen Unicode characters, not including
265
* the terminating (ntfschar)'\0'. If there is no (ntfschar)'\0' between @s
266
* and @s + @maxlen, @maxlen is returned.
268
* This function never looks beyond @s + @maxlen.
270
u32 ntfs_ucsnlen(const ntfschar *s, u32 maxlen)
274
for (i = 0; i < maxlen; i++) {
275
if (!le16_to_cpu(s[i]))
282
* ntfs_ucsndup - duplicate little endian Unicode string
283
* @s: pointer to Unicode string
284
* @maxlen: maximum length of string @s
286
* Return a pointer to a new little endian Unicode string which is a duplicate
287
* of the string s. Memory for the new string is obtained with ntfs_malloc(3),
288
* and can be freed with free(3).
290
* A maximum of @maxlen Unicode characters are copied and a terminating
291
* (ntfschar)'\0' little endian Unicode character is added.
293
* This function never looks beyond @s + @maxlen.
295
* Return a pointer to the new little endian Unicode string on success and NULL
296
* on failure with errno set to the error code.
298
ntfschar *ntfs_ucsndup(const ntfschar *s, u32 maxlen)
303
len = ntfs_ucsnlen(s, maxlen);
304
dst = ntfs_malloc((len + 1) * sizeof(ntfschar));
306
memcpy(dst, s, len * sizeof(ntfschar));
307
dst[len] = cpu_to_le16(L'\0');
313
* ntfs_name_upcase - Map an Unicode name to its uppercase equivalent
323
void ntfs_name_upcase(ntfschar *name, u32 name_len, const ntfschar *upcase,
324
const u32 upcase_len)
329
for (i = 0; i < name_len; i++)
330
if ((u = le16_to_cpu(name[i])) < upcase_len)
335
* ntfs_file_value_upcase - Convert a filename to upper case
344
void ntfs_file_value_upcase(FILE_NAME_ATTR *file_name_attr,
345
const ntfschar *upcase, const u32 upcase_len)
347
ntfs_name_upcase((ntfschar*)&file_name_attr->file_name,
348
file_name_attr->file_name_length, upcase, upcase_len);
352
* ntfs_file_values_compare - Which of two filenames should be listed first
364
int ntfs_file_values_compare(const FILE_NAME_ATTR *file_name_attr1,
365
const FILE_NAME_ATTR *file_name_attr2,
366
const int err_val, const IGNORE_CASE_BOOL ic,
367
const ntfschar *upcase, const u32 upcase_len)
369
return ntfs_names_collate((ntfschar*)&file_name_attr1->file_name,
370
file_name_attr1->file_name_length,
371
(ntfschar*)&file_name_attr2->file_name,
372
file_name_attr2->file_name_length,
373
err_val, ic, upcase, upcase_len);
377
* ntfs_ucstombs - convert a little endian Unicode string to a multibyte string
378
* @ins: input Unicode string buffer
379
* @ins_len: length of input string in Unicode characters
380
* @outs: on return contains the (allocated) output multibyte string
381
* @outs_len: length of output buffer in bytes
383
* Convert the input little endian, 2-byte Unicode string @ins, of length
384
* @ins_len into the multibyte string format dictated by the current locale.
386
* If *@outs is NULL, the function allocates the string and the caller is
387
* responsible for calling free(*@outs); when finished with it.
389
* On success the function returns the number of bytes written to the output
390
* string *@outs (>= 0), not counting the terminating NULL byte. If the output
391
* string buffer was allocated, *@outs is set to it.
393
* On error, -1 is returned, and errno is set to the error code. The following
394
* error codes can be expected:
395
* EINVAL Invalid arguments (e.g. @ins or @outs is NULL).
396
* EILSEQ The input string cannot be represented as a multibyte
397
* sequence according to the current locale.
398
* ENAMETOOLONG Destination buffer is too small for input string.
399
* ENOMEM Not enough memory to allocate destination buffer.
401
int ntfs_ucstombs(const ntfschar *ins, const int ins_len, char **outs,
418
if (mbs && !mbs_len) {
419
errno = ENAMETOOLONG;
423
mbs_len = (ins_len + 1) * MB_CUR_MAX;
424
mbs = ntfs_malloc(mbs_len);
429
memset(&mbstate, 0, sizeof(mbstate));
433
for (i = o = 0; i < ins_len; i++) {
434
/* Reallocate memory if necessary or abort. */
435
if ((int)(o + MB_CUR_MAX) > mbs_len) {
438
errno = ENAMETOOLONG;
441
tc = ntfs_malloc((mbs_len + 64) & ~63);
444
memcpy(tc, mbs, mbs_len);
445
mbs_len = (mbs_len + 64) & ~63;
449
/* Convert the LE Unicode character to a CPU wide character. */
450
wc = (wchar_t)le16_to_cpu(ins[i]);
453
/* Convert the CPU endian wide character to multibyte. */
455
cnt = wcrtomb(mbs + o, wc, &mbstate);
457
cnt = wctomb(mbs + o, wc);
462
ntfs_log_debug("Eeek. cnt <= 0, cnt = %i\n", cnt);
469
/* Make sure we are back in the initial state. */
470
if (!mbsinit(&mbstate)) {
471
ntfs_log_debug("Eeek. mbstate not in initial state!\n");
476
/* Now write the NULL character. */
491
* ntfs_mbstoucs - convert a multibyte string to a little endian Unicode string
492
* @ins: input multibyte string buffer
493
* @outs: on return contains the (allocated) output Unicode string
494
* @outs_len: length of output buffer in Unicode characters
496
* Convert the input multibyte string @ins, from the current locale into the
497
* corresponding little endian, 2-byte Unicode string.
499
* If *@outs is NULL, the function allocates the string and the caller is
500
* responsible for calling free(*@outs); when finished with it.
502
* On success the function returns the number of Unicode characters written to
503
* the output string *@outs (>= 0), not counting the terminating Unicode NULL
504
* character. If the output string buffer was allocated, *@outs is set to it.
506
* On error, -1 is returned, and errno is set to the error code. The following
507
* error codes can be expected:
508
* EINVAL Invalid arguments (e.g. @ins or @outs is NULL).
509
* EILSEQ The input string cannot be represented as a Unicode
510
* string according to the current locale.
511
* ENAMETOOLONG Destination buffer is too small for input string.
512
* ENOMEM Not enough memory to allocate destination buffer.
514
int ntfs_mbstoucs(const char *ins, ntfschar **outs, int outs_len)
519
int i, o, cnt, ins_len, ucs_len, ins_size;
530
if (ucs && !ucs_len) {
531
errno = ENAMETOOLONG;
534
/* Determine the size of the multi-byte string in bytes. */
535
ins_size = strlen(ins);
536
/* Determine the length of the multi-byte string. */
538
#if defined(HAVE_MBSINIT)
539
memset(&mbstate, 0, sizeof(mbstate));
540
ins_len = mbsrtowcs(NULL, (const char **)&s, 0, &mbstate);
542
if (!ins_len && *ins) {
543
/* Older Cygwin had broken mbsrtowcs() implementation. */
544
ins_len = strlen(ins);
547
#elif !defined(DJGPP)
548
ins_len = mbstowcs(NULL, s, 0);
550
/* Eeek!!! DJGPP has broken mbstowcs() implementation!!! */
551
ins_len = strlen(ins);
556
if ((s != ins) || !mbsinit(&mbstate)) {
563
/* Add the NULL terminator. */
567
ucs = ntfs_malloc(ucs_len * sizeof(ntfschar));
572
memset(&mbstate, 0, sizeof(mbstate));
574
mbtowc(NULL, NULL, 0);
576
for (i = o = cnt = 0; i < ins_size; i += cnt, o++) {
577
/* Reallocate memory if necessary or abort. */
581
errno = ENAMETOOLONG;
585
* We will never get here but hey, it's only a bit of
588
ucs_len = (ucs_len * sizeof(ntfschar) + 64) & ~63;
589
tc = (ntfschar*)realloc(ucs, ucs_len);
593
ucs_len /= sizeof(ntfschar);
595
/* Convert the multibyte character to a wide character. */
597
cnt = mbrtowc(&wc, ins + i, ins_size - i, &mbstate);
599
cnt = mbtowc(&wc, ins + i, ins_size - i);
606
ntfs_log_trace("Eeek. cnt = %i\n", cnt);
610
/* Make sure we are not overflowing the NTFS Unicode set. */
611
if ((unsigned long)wc >= (unsigned long)(1 <<
612
(8 * sizeof(ntfschar)))) {
616
/* Convert the CPU wide character to a LE Unicode character. */
617
ucs[o] = cpu_to_le16(wc);
620
/* Make sure we are back in the initial state. */
621
if (!mbsinit(&mbstate)) {
622
ntfs_log_trace("Eeek. mbstate not in initial state!\n");
627
/* Now write the NULL character. */
628
ucs[o] = cpu_to_le16(L'\0');
642
* ntfs_upcase_table_build - build the default upcase table for NTFS
643
* @uc: destination buffer where to store the built table
644
* @uc_len: size of destination buffer in bytes
646
* ntfs_upcase_table_build() builds the default upcase table for NTFS and
647
* stores it in the caller supplied buffer @uc of size @uc_len.
649
* Note, @uc_len must be at least 128kiB in size or bad things will happen!
651
void ntfs_upcase_table_build(ntfschar *uc, u32 uc_len)
653
static int uc_run_table[][3] = { /* Start, End, Add */
654
{0x0061, 0x007B, -32}, {0x0451, 0x045D, -80}, {0x1F70, 0x1F72, 74},
655
{0x00E0, 0x00F7, -32}, {0x045E, 0x0460, -80}, {0x1F72, 0x1F76, 86},
656
{0x00F8, 0x00FF, -32}, {0x0561, 0x0587, -48}, {0x1F76, 0x1F78, 100},
657
{0x0256, 0x0258, -205}, {0x1F00, 0x1F08, 8}, {0x1F78, 0x1F7A, 128},
658
{0x028A, 0x028C, -217}, {0x1F10, 0x1F16, 8}, {0x1F7A, 0x1F7C, 112},
659
{0x03AC, 0x03AD, -38}, {0x1F20, 0x1F28, 8}, {0x1F7C, 0x1F7E, 126},
660
{0x03AD, 0x03B0, -37}, {0x1F30, 0x1F38, 8}, {0x1FB0, 0x1FB2, 8},
661
{0x03B1, 0x03C2, -32}, {0x1F40, 0x1F46, 8}, {0x1FD0, 0x1FD2, 8},
662
{0x03C2, 0x03C3, -31}, {0x1F51, 0x1F52, 8}, {0x1FE0, 0x1FE2, 8},
663
{0x03C3, 0x03CC, -32}, {0x1F53, 0x1F54, 8}, {0x1FE5, 0x1FE6, 7},
664
{0x03CC, 0x03CD, -64}, {0x1F55, 0x1F56, 8}, {0x2170, 0x2180, -16},
665
{0x03CD, 0x03CF, -63}, {0x1F57, 0x1F58, 8}, {0x24D0, 0x24EA, -26},
666
{0x0430, 0x0450, -32}, {0x1F60, 0x1F68, 8}, {0xFF41, 0xFF5B, -32},
669
static int uc_dup_table[][2] = { /* Start, End */
670
{0x0100, 0x012F}, {0x01A0, 0x01A6}, {0x03E2, 0x03EF}, {0x04CB, 0x04CC},
671
{0x0132, 0x0137}, {0x01B3, 0x01B7}, {0x0460, 0x0481}, {0x04D0, 0x04EB},
672
{0x0139, 0x0149}, {0x01CD, 0x01DD}, {0x0490, 0x04BF}, {0x04EE, 0x04F5},
673
{0x014A, 0x0178}, {0x01DE, 0x01EF}, {0x04BF, 0x04BF}, {0x04F8, 0x04F9},
674
{0x0179, 0x017E}, {0x01F4, 0x01F5}, {0x04C1, 0x04C4}, {0x1E00, 0x1E95},
675
{0x018B, 0x018B}, {0x01FA, 0x0218}, {0x04C7, 0x04C8}, {0x1EA0, 0x1EF9},
678
static int uc_byte_table[][2] = { /* Offset, Value */
679
{0x00FF, 0x0178}, {0x01AD, 0x01AC}, {0x01F3, 0x01F1}, {0x0269, 0x0196},
680
{0x0183, 0x0182}, {0x01B0, 0x01AF}, {0x0253, 0x0181}, {0x026F, 0x019C},
681
{0x0185, 0x0184}, {0x01B9, 0x01B8}, {0x0254, 0x0186}, {0x0272, 0x019D},
682
{0x0188, 0x0187}, {0x01BD, 0x01BC}, {0x0259, 0x018F}, {0x0275, 0x019F},
683
{0x018C, 0x018B}, {0x01C6, 0x01C4}, {0x025B, 0x0190}, {0x0283, 0x01A9},
684
{0x0192, 0x0191}, {0x01C9, 0x01C7}, {0x0260, 0x0193}, {0x0288, 0x01AE},
685
{0x0199, 0x0198}, {0x01CC, 0x01CA}, {0x0263, 0x0194}, {0x0292, 0x01B7},
686
{0x01A8, 0x01A7}, {0x01DD, 0x018E}, {0x0268, 0x0197},
691
memset((char*)uc, 0, uc_len);
695
for (i = 0; (u32)i < uc_len; i++)
697
for (r = 0; uc_run_table[r][0]; r++)
698
for (i = uc_run_table[r][0]; i < uc_run_table[r][1]; i++)
699
uc[i] += uc_run_table[r][2];
700
for (r = 0; uc_dup_table[r][0]; r++)
701
for (i = uc_dup_table[r][0]; i < uc_dup_table[r][1]; i += 2)
703
for (r = 0; uc_byte_table[r][0]; r++)
704
uc[uc_byte_table[r][0]] = uc_byte_table[r][1];
708
* ntfs_str2ucs - convert a string to a valid NTFS file name
710
* @len: length of output buffer in Unicode characters
712
* Convert the input @s string into the corresponding little endian,
713
* 2-byte Unicode string. The length of the converted string is less
714
* or equal to the maximum length allowed by the NTFS format (255).
716
* If @s is NULL then return AT_UNNAMED.
718
* On success the function returns the Unicode string in an allocated
719
* buffer and the caller is responsible to free it when it's not needed
722
* On error NULL is returned and errno is set to the error code.
724
ntfschar *ntfs_str2ucs(const char *s, int *len)
726
ntfschar *ucs = NULL;
728
if (s && ((*len = ntfs_mbstoucs(s, &ucs, 0)) == -1)) {
729
ntfs_log_perror("Couldn't convert '%s' to Unicode", s);
732
if (*len > NTFS_MAX_NAME_LEN) {
734
errno = ENAMETOOLONG;
745
* ntfs_ucsfree - free memory allocated by ntfs_str2ucs()
746
* @ucs input string to be freed
748
* Free memory at @ucs and which was allocated by ntfs_str2ucs.
750
* Return value: none.
752
void ntfs_ucsfree(ntfschar *ucs)
754
if (ucs && (ucs != AT_UNNAMED))