1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * unistr.c - NTFS Unicode string handling. Part of the Linux-NTFS project.
5 * Copyright (c) 2001-2006 Anton Altaparmakov
8 #include <linux/slab.h>
18 * All these routines assume that the Unicode characters are in little endian
19 * encoding inside the strings!!!
23 * This is used by the name collation functions to quickly determine what
24 * characters are (in)valid.
26 static const u8 legal_ansi_char_array
[0x40] = {
27 0x00, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
28 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
30 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
31 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
33 0x17, 0x07, 0x18, 0x17, 0x17, 0x17, 0x17, 0x17,
34 0x17, 0x17, 0x18, 0x16, 0x16, 0x17, 0x07, 0x00,
36 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17,
37 0x17, 0x17, 0x04, 0x16, 0x18, 0x16, 0x18, 0x18,
41 * ntfs_are_names_equal - compare two Unicode names for equality
42 * @s1: name to compare to @s2
43 * @s1_len: length in Unicode characters of @s1
44 * @s2: name to compare to @s1
45 * @s2_len: length in Unicode characters of @s2
46 * @ic: ignore case bool
47 * @upcase: upcase table (only if @ic == IGNORE_CASE)
48 * @upcase_size: length in Unicode characters of @upcase (if present)
50 * Compare the names @s1 and @s2 and return 'true' (1) if the names are
51 * identical, or 'false' (0) if they are not identical. If @ic is IGNORE_CASE,
52 * the @upcase table is used to performa a case insensitive comparison.
54 bool ntfs_are_names_equal(const ntfschar
*s1
, size_t s1_len
,
55 const ntfschar
*s2
, size_t s2_len
, const IGNORE_CASE_BOOL ic
,
56 const ntfschar
*upcase
, const u32 upcase_size
)
60 if (ic
== CASE_SENSITIVE
)
61 return !ntfs_ucsncmp(s1
, s2
, s1_len
);
62 return !ntfs_ucsncasecmp(s1
, s2
, s1_len
, upcase
, upcase_size
);
66 * ntfs_collate_names - collate two Unicode names
67 * @name1: first Unicode name to compare
68 * @name2: second Unicode name to compare
69 * @err_val: if @name1 contains an invalid character return this value
70 * @ic: either CASE_SENSITIVE or IGNORE_CASE
71 * @upcase: upcase table (ignored if @ic is CASE_SENSITIVE)
72 * @upcase_len: upcase table size (ignored if @ic is CASE_SENSITIVE)
74 * ntfs_collate_names collates two Unicode names and returns:
76 * -1 if the first name collates before the second one,
77 * 0 if the names match,
78 * 1 if the second name collates before the first one, or
79 * @err_val if an invalid character is found in @name1 during the comparison.
81 * The following characters are considered invalid: '"', '*', '<', '>' and '?'.
83 int ntfs_collate_names(const ntfschar
*name1
, const u32 name1_len
,
84 const ntfschar
*name2
, const u32 name2_len
,
85 const int err_val
, const IGNORE_CASE_BOOL ic
,
86 const ntfschar
*upcase
, const u32 upcase_len
)
92 if (name1_len
> name2_len
)
94 for (cnt
= 0; cnt
< min_len
; ++cnt
) {
95 c1
= le16_to_cpu(*name1
++);
96 c2
= le16_to_cpu(*name2
++);
99 c1
= le16_to_cpu(upcase
[c1
]);
101 c2
= le16_to_cpu(upcase
[c2
]);
103 if (c1
< 64 && legal_ansi_char_array
[c1
] & 8)
110 if (name1_len
< name2_len
)
112 if (name1_len
== name2_len
)
114 /* name1_len > name2_len */
115 c1
= le16_to_cpu(*name1
);
116 if (c1
< 64 && legal_ansi_char_array
[c1
] & 8)
122 * ntfs_ucsncmp - compare two little endian Unicode strings
125 * @n: maximum unicode characters to compare
127 * Compare the first @n characters of the Unicode strings @s1 and @s2,
128 * The strings in little endian format and appropriate le16_to_cpu()
129 * conversion is performed on non-little endian machines.
131 * The function returns an integer less than, equal to, or greater than zero
132 * if @s1 (or the first @n Unicode characters thereof) is found, respectively,
133 * to be less than, to match, or be greater than @s2.
135 int ntfs_ucsncmp(const ntfschar
*s1
, const ntfschar
*s2
, size_t n
)
140 for (i
= 0; i
< n
; ++i
) {
141 c1
= le16_to_cpu(s1
[i
]);
142 c2
= le16_to_cpu(s2
[i
]);
154 * ntfs_ucsncasecmp - compare two little endian Unicode strings, ignoring case
157 * @n: maximum unicode characters to compare
158 * @upcase: upcase table
159 * @upcase_size: upcase table size in Unicode characters
161 * Compare the first @n characters of the Unicode strings @s1 and @s2,
162 * ignoring case. The strings in little endian format and appropriate
163 * le16_to_cpu() conversion is performed on non-little endian machines.
165 * Each character is uppercased using the @upcase table before the comparison.
167 * The function returns an integer less than, equal to, or greater than zero
168 * if @s1 (or the first @n Unicode characters thereof) is found, respectively,
169 * to be less than, to match, or be greater than @s2.
171 int ntfs_ucsncasecmp(const ntfschar
*s1
, const ntfschar
*s2
, size_t n
,
172 const ntfschar
*upcase
, const u32 upcase_size
)
177 for (i
= 0; i
< n
; ++i
) {
178 if ((c1
= le16_to_cpu(s1
[i
])) < upcase_size
)
179 c1
= le16_to_cpu(upcase
[c1
]);
180 if ((c2
= le16_to_cpu(s2
[i
])) < upcase_size
)
181 c2
= le16_to_cpu(upcase
[c2
]);
192 void ntfs_upcase_name(ntfschar
*name
, u32 name_len
, const ntfschar
*upcase
,
193 const u32 upcase_len
)
198 for (i
= 0; i
< name_len
; i
++)
199 if ((u
= le16_to_cpu(name
[i
])) < upcase_len
)
203 void ntfs_file_upcase_value(FILE_NAME_ATTR
*file_name_attr
,
204 const ntfschar
*upcase
, const u32 upcase_len
)
206 ntfs_upcase_name((ntfschar
*)&file_name_attr
->file_name
,
207 file_name_attr
->file_name_length
, upcase
, upcase_len
);
210 int ntfs_file_compare_values(FILE_NAME_ATTR
*file_name_attr1
,
211 FILE_NAME_ATTR
*file_name_attr2
,
212 const int err_val
, const IGNORE_CASE_BOOL ic
,
213 const ntfschar
*upcase
, const u32 upcase_len
)
215 return ntfs_collate_names((ntfschar
*)&file_name_attr1
->file_name
,
216 file_name_attr1
->file_name_length
,
217 (ntfschar
*)&file_name_attr2
->file_name
,
218 file_name_attr2
->file_name_length
,
219 err_val
, ic
, upcase
, upcase_len
);
223 * ntfs_nlstoucs - convert NLS string to little endian Unicode string
224 * @vol: ntfs volume which we are working with
225 * @ins: input NLS string buffer
226 * @ins_len: length of input string in bytes
227 * @outs: on return contains the allocated output Unicode string buffer
229 * Convert the input string @ins, which is in whatever format the loaded NLS
230 * map dictates, into a little endian, 2-byte Unicode string.
232 * This function allocates the string and the caller is responsible for
233 * calling kmem_cache_free(ntfs_name_cache, *@outs); when finished with it.
235 * On success the function returns the number of Unicode characters written to
236 * the output string *@outs (>= 0), not counting the terminating Unicode NULL
237 * character. *@outs is set to the allocated output string buffer.
239 * On error, a negative number corresponding to the error code is returned. In
240 * that case the output string is not allocated. Both *@outs and *@outs_len
241 * are then undefined.
243 * This might look a bit odd due to fast path optimization...
245 int ntfs_nlstoucs(const ntfs_volume
*vol
, const char *ins
,
246 const int ins_len
, ntfschar
**outs
)
248 struct nls_table
*nls
= vol
->nls_map
;
253 /* We do not trust outside sources. */
255 ucs
= kmem_cache_alloc(ntfs_name_cache
, GFP_NOFS
);
257 for (i
= o
= 0; i
< ins_len
; i
+= wc_len
) {
258 wc_len
= nls
->char2uni(ins
+ i
, ins_len
- i
,
260 if (likely(wc_len
>= 0 &&
261 o
< NTFS_MAX_NAME_LEN
)) {
263 ucs
[o
++] = cpu_to_le16(wc
);
265 } /* else if (!wc) */
267 } /* else if (wc_len < 0 ||
268 o >= NTFS_MAX_NAME_LEN) */
274 } /* else if (!ucs) */
275 ntfs_error(vol
->sb
, "Failed to allocate buffer for converted "
276 "name from ntfs_name_cache.");
278 } /* else if (!ins) */
279 ntfs_error(vol
->sb
, "Received NULL pointer.");
282 kmem_cache_free(ntfs_name_cache
, ucs
);
284 ntfs_error(vol
->sb
, "Name using character set %s contains "
285 "characters that cannot be converted to "
286 "Unicode.", nls
->charset
);
288 } else /* if (o >= NTFS_MAX_NAME_LEN) */ {
289 ntfs_error(vol
->sb
, "Name is too long (maximum length for a "
290 "name on NTFS is %d Unicode characters.",
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
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.
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.
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.
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
320 * This might look a bit odd due to fast path optimization...
322 int ntfs_ucstonls(const ntfs_volume
*vol
, const ntfschar
*ins
,
323 const int ins_len
, unsigned char **outs
, int outs_len
)
325 struct nls_table
*nls
= vol
->nls_map
;
327 int i
, o
, ns_len
, wc
;
329 /* We don't trust outside sources. */
338 ns_len
= ins_len
* NLS_MAX_CHARSET_SIZE
;
339 ns
= kmalloc(ns_len
+ 1, GFP_NOFS
);
343 for (i
= o
= 0; i
< ins_len
; i
++) {
344 retry
: wc
= nls
->uni2char(le16_to_cpu(ins
[i
]), ns
+ o
,
351 else if (wc
== -ENAMETOOLONG
&& ns
!= *outs
) {
353 /* Grow in multiples of 64 bytes. */
354 tc
= kmalloc((ns_len
+ 64) &
357 memcpy(tc
, ns
, ns_len
);
358 ns_len
= ((ns_len
+ 64) & ~63) - 1;
362 } /* No memory so goto conversion_error; */
363 } /* wc < 0, real error. */
370 ntfs_error(vol
->sb
, "Received NULL pointer.");
373 ntfs_error(vol
->sb
, "Unicode name contains characters that cannot be "
374 "converted to character set %s. You might want to "
375 "try to use the mount option nls=utf8.", nls
->charset
);
378 if (wc
!= -ENAMETOOLONG
)
382 ntfs_error(vol
->sb
, "Failed to allocate name!");