2 * unistr.c - Unicode string handling. Part of the Linux-NTFS project.
4 * Copyright (c) 2000-2002 Anton Altaparmakov
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.
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.
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
36 * All these routines assume that the Unicode characters are in little endian
37 * encoding inside the strings!!!
41 * This is used by the name collation functions to quickly determine what
42 * characters are (in)valid.
44 const u8 legal_ansi_char_array[0x40] = {
45 0x00, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
46 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
48 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
49 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
51 0x17, 0x07, 0x18, 0x17, 0x17, 0x17, 0x17, 0x17,
52 0x17, 0x17, 0x18, 0x16, 0x16, 0x17, 0x07, 0x00,
54 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17,
55 0x17, 0x17, 0x04, 0x16, 0x18, 0x16, 0x18, 0x18,
59 * ntfs_names_are_equal - compare two Unicode names for equality
60 * @s1: name to compare to @s2
61 * @s1_len: length in Unicode characters of @s1
62 * @s2: name to compare to @s1
63 * @s2_len: length in Unicode characters of @s2
64 * @ic: ignore case bool
65 * @upcase: upcase table (only if @ic == IGNORE_CASE)
66 * @upcase_size: length in Unicode characters of @upcase (if present)
68 * Compare the names @s1 and @s2 and return TRUE (1) if the names are
69 * identical, or FALSE (0) if they are not identical. If @ic is IGNORE_CASE,
70 * the @upcase table is used to performa a case insensitive comparison.
72 BOOL ntfs_names_are_equal(const uchar_t *s1, size_t s1_len,
73 const uchar_t *s2, size_t s2_len,
74 const IGNORE_CASE_BOOL ic,
75 const uchar_t *upcase, const u32 upcase_size)
81 if (ic == CASE_SENSITIVE)
82 return ntfs_ucsncmp(s1, s2, s1_len) ? FALSE: TRUE;
83 return ntfs_ucsncasecmp(s1, s2, s1_len, upcase, upcase_size) ? FALSE:
88 * ntfs_names_collate - collate two Unicode names
89 * @upcase: upcase table (ignored if @ic is CASE_SENSITIVE)
90 * @upcase_len: upcase table size (ignored if @ic is CASE_SENSITIVE)
91 * @name1: first Unicode name to compare
92 * @name2: second Unicode name to compare
93 * @ic: either CASE_SENSITIVE or IGNORE_CASE
94 * @err_val: if @name1 contains an invalid character return this value
96 * ntfs_names_collate() collates two Unicode names and returns:
98 * -1 if the first name collates before the second one,
99 * 0 if the names match,
100 * 1 if the second name collates before the first one, or
101 * @err_val if an invalid character is found in @name1 during the comparison.
103 * The following characters are considered invalid: '"', '*', '<', '>' and '?'.
105 int ntfs_names_collate(const uchar_t *name1, const u32 name1_len,
106 const uchar_t *name2, const u32 name2_len,
107 const int err_val, const IGNORE_CASE_BOOL ic,
108 const uchar_t *upcase, const u32 upcase_len)
114 if (!name1 || !name2 || (ic && !upcase && upcase_len)) {
115 Dputs("ntfs_names_collate received NULL pointer!");
119 for (cnt = 0; cnt < min(name1_len, name2_len); ++cnt)
121 c1 = le16_to_cpu(*name1++);
122 c2 = le16_to_cpu(*name2++);
125 c1 = le16_to_cpu(upcase[c1]);
127 c2 = le16_to_cpu(upcase[c2]);
129 if (c1 < 64 && legal_ansi_char_array[c1] & 8)
136 if (name1_len < name2_len)
138 if (name1_len == name2_len)
140 /* name1_len > name2_len */
141 c1 = le16_to_cpu(*name1);
142 if (c1 < 64 && legal_ansi_char_array[c1] & 8)
148 * ntfs_ucsncmp - compare two little endian Unicode strings
151 * @n: maximum unicode characters to compare
153 * Compare the first @n characters of the Unicode strings @s1 and @s2,
154 * The strings in little endian format and appropriate le16_to_cpu()
155 * conversion is performed on non-little endian machines.
157 * The function returns an integer less than, equal to, or greater than zero
158 * if @s1 (or the first @n Unicode characters thereof) is found, respectively,
159 * to be less than, to match, or be greater than @s2.
161 int ntfs_ucsncmp(const uchar_t *s1, const uchar_t *s2, size_t n)
168 Dputs("ntfs_wcsncmp() received NULL pointer!");
172 for (i = 0; i < n; ++i) {
173 c1 = le16_to_cpu(s1[i]);
174 c2 = le16_to_cpu(s2[i]);
186 * ntfs_ucsncasecmp - compare two little endian Unicode strings, ignoring case
189 * @n: maximum unicode characters to compare
190 * @upcase: upcase table
191 * @upcase_size: upcase table size in Unicode characters
193 * Compare the first @n characters of the Unicode strings @s1 and @s2,
194 * ignoring case. The strings in little endian format and appropriate
195 * le16_to_cpu() conversion is performed on non-little endian machines.
197 * Each character is uppercased using the @upcase table before the comparison.
199 * The function returns an integer less than, equal to, or greater than zero
200 * if @s1 (or the first @n Unicode characters thereof) is found, respectively,
201 * to be less than, to match, or be greater than @s2.
203 int ntfs_ucsncasecmp(const uchar_t *s1, const uchar_t *s2, size_t n,
204 const uchar_t *upcase, const u32 upcase_size)
210 if (!s1 || !s2 || !upcase) {
211 Dputs("ntfs_wcsncasecmp() received NULL pointer!");
215 for (i = 0; i < n; ++i) {
216 if ((c1 = le16_to_cpu(s1[i])) < upcase_size)
217 c1 = le16_to_cpu(upcase[c1]);
218 if ((c2 = le16_to_cpu(s2[i])) < upcase_size)
219 c2 = le16_to_cpu(upcase[c2]);
231 * ntfs_ucsnlen - determine the length of a little endian Unicode string
232 * @s: pointer to Unicode string
233 * @maxlen: maximum length of string @s
235 * Return the number of Unicode characters in the little endian Unicode
236 * string @s up to a maximum of maxlen Unicode characters, not including
237 * the terminating (uchar_t)'\0'. If there is no (uchar_t)'\0' between @s
238 * and @s + @maxlen, @maxlen is returned.
240 * This function never looks beyond @s + @maxlen.
242 u32 ntfs_ucsnlen(const uchar_t *s, u32 maxlen)
246 for (i = 0; i < maxlen; i++) {
247 if (!le16_to_cpu(s[i]))
256 void ntfs_name_upcase(uchar_t *name, u32 name_len, const uchar_t *upcase,
257 const u32 upcase_len)
262 for (i = 0; i < name_len; i++)
263 if ((u = le16_to_cpu(name[i])) < upcase_len)
268 * ntfs_file_value_upcase
270 void ntfs_file_value_upcase(FILE_NAME_ATTR *file_name_attr,
271 const uchar_t *upcase, const u32 upcase_len)
273 ntfs_name_upcase((uchar_t*)&file_name_attr->file_name,
274 file_name_attr->file_name_length, upcase, upcase_len);
278 * ntfs_file_values_compare
280 int ntfs_file_values_compare(FILE_NAME_ATTR *file_name_attr1,
281 FILE_NAME_ATTR *file_name_attr2,
282 const int err_val, const IGNORE_CASE_BOOL ic,
283 const uchar_t *upcase, const u32 upcase_len)
285 return ntfs_names_collate((uchar_t*)&file_name_attr1->file_name,
286 file_name_attr1->file_name_length,
287 (uchar_t*)&file_name_attr2->file_name,
288 file_name_attr2->file_name_length,
289 err_val, ic, upcase, upcase_len);
293 * ntfs_ucstombs - convert a little endian Unicode string to a multibyte string
294 * @ins: input Unicode string buffer
295 * @ins_len: length of input string in Unicode characters
296 * @outs: on return contains the (allocated) output multibyte string
297 * @outs_len: length of output buffer in bytes
299 * Convert the input little endian, 2-byte Unicode string @ins, of length
300 * @ins_len into the multibyte string format dictated by the current locale.
302 * If *@outs is NULL, the function allocates the string and the caller is
303 * responsible for calling free(*@outs); when finished with it.
305 * On success the function returns the number of bytes written to the output
306 * string *@outs (>= 0), not counting the terminating NULL byte. If the output
307 * string buffer was allocated, *@outs is set to it.
309 * On error, -1 is returned, and errno is set to the error code. The following
310 * error codes can be expected:
311 * EINVAL Invalid arguments (e.g. @ins or @outs is NULL).
312 * EILSEQ The input string cannot be represented as a multibyte
313 * sequence according to the current locale.
314 * ENAMETOOLONG Destination buffer is too small for input string.
315 * ENOMEM Not enough memory to allocate destination buffer.
317 int ntfs_ucstombs(const uchar_t *ins, const int ins_len, char **outs,
332 if (mbs && !mbs_len) {
333 errno = ENAMETOOLONG;
337 mbs_len = (ins_len + 1) * MB_CUR_MAX;
338 mbs = (char*)malloc(mbs_len);
342 memset(&mbstate, 0, sizeof(mbstate));
343 for (i = o = 0; i < ins_len; i++) {
344 /* Reallocate memory if necessary or abort. */
345 if (o + MB_CUR_MAX > mbs_len) {
348 errno = ENAMETOOLONG;
351 tc = (char*)malloc((mbs_len + 64) & ~63);
354 memcpy(tc, mbs, mbs_len);
355 mbs_len = (mbs_len + 64) & ~63;
359 /* Convert the LE Unicode character to a CPU wide character. */
360 wc = (wchar_t)le16_to_cpu(ins[i]);
363 /* Convert the CPU endian wide character to multibyte. */
364 cnt = wcrtomb(mbs + o, wc, &mbstate);
368 Dprintf("Eeek. cnt <= 0, cnt = %i\n", cnt);
374 /* Make sure we are back in the initial state. */
375 if (!mbsinit(&mbstate)) {
376 Dputs("Eeek. mbstate not in initial state!");
380 /* Now write the NULL character. */
395 * ntfs_mbstoucs - convert a multibyte string to a little endian Unicode string
396 * @ins: input multibyte string buffer
397 * @outs: on return contains the (allocated) output Unicode string
398 * @outs_len: length of output buffer in Unicode characters
400 * Convert the input multibyte string @ins, from the current locale into the
401 * corresponding little endian, 2-byte Unicode string.
403 * If *@outs is NULL, the function allocates the string and the caller is
404 * responsible for calling free(*@outs); when finished with it.
406 * On success the function returns the number of Unicode characters written to
407 * the output string *@outs (>= 0), not counting the terminating Unicode NULL
408 * character. If the output string buffer was allocated, *@outs is set to it.
410 * On error, -1 is returned, and errno is set to the error code. The following
411 * error codes can be expected:
412 * EINVAL Invalid arguments (e.g. @ins or @outs is NULL).
413 * EILSEQ The input string cannot be represented as a Unicode
414 * string according to the current locale.
415 * ENAMETOOLONG Destination buffer is too small for input string.
416 * ENOMEM Not enough memory to allocate destination buffer.
418 int ntfs_mbstoucs(char *ins, uchar_t **outs, int outs_len)
423 int i, o, cnt, ins_len, ucs_len;
432 if (ucs && !ucs_len) {
433 errno = ENAMETOOLONG;
436 /* Determine the length of the multi-byte string. */
438 memset(&mbstate, 0, sizeof(mbstate));
439 ins_len = mbsrtowcs(NULL, (const char **)&s, 0, &mbstate);
442 if ((s != ins) || !mbsinit(&mbstate)) {
446 /* Add the NULL terminator. */
450 ucs = (uchar_t*)malloc(ucs_len * sizeof(uchar_t));
454 memset(&mbstate, 0, sizeof(mbstate));
455 for (i = o = cnt = 0; o < ins_len; i += cnt, o++) {
456 /* Reallocate memory if necessary or abort. */
460 errno = ENAMETOOLONG;
464 * We will never get here but hey, it's only a bit of
467 ucs_len = (ucs_len * sizeof(uchar_t) + 64) & ~63;
468 tc = (uchar_t*)realloc(ucs, ucs_len);
472 ucs_len /= sizeof(uchar_t);
474 /* Convert the multibyte character to a wide character. */
475 cnt = mbrtowc(&wc, ins + i, ins_len - i, &mbstate);
481 Dprintf("%s(): Eeek. cnt = %i\n", __FUNCTION__, cnt);
485 /* Make sure we are not overflowing the NTFS Unicode set. */
486 if ((unsigned long)wc >= (unsigned long)(1 <<
487 (8 * sizeof(uchar_t)))) {
491 /* Convert the CPU wide character to a LE Unicode character. */
492 ucs[o] = cpu_to_le16(wc);
494 /* Make sure we are back in the initial state. */
495 if (!mbsinit(&mbstate)) {
496 Dprintf("%s(): Eeek. mbstate not in initial state!\n",
501 /* Now write the NULL character. */
502 ucs[o] = cpu_to_le16(L'\0');