^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * unistr.c - NTFS Unicode string handling. Part of the Linux-NTFS project.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2001-2006 Anton Altaparmakov
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "types.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "ntfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * IMPORTANT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * =========
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * All these routines assume that the Unicode characters are in little endian
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * encoding inside the strings!!!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * This is used by the name collation functions to quickly determine what
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * characters are (in)valid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static const u8 legal_ansi_char_array[0x40] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) 0x00, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) 0x17, 0x07, 0x18, 0x17, 0x17, 0x17, 0x17, 0x17,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) 0x17, 0x17, 0x18, 0x16, 0x16, 0x17, 0x07, 0x00,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) 0x17, 0x17, 0x04, 0x16, 0x18, 0x16, 0x18, 0x18,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * ntfs_are_names_equal - compare two Unicode names for equality
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * @s1: name to compare to @s2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * @s1_len: length in Unicode characters of @s1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * @s2: name to compare to @s1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * @s2_len: length in Unicode characters of @s2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * @ic: ignore case bool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * @upcase: upcase table (only if @ic == IGNORE_CASE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * @upcase_size: length in Unicode characters of @upcase (if present)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * Compare the names @s1 and @s2 and return 'true' (1) if the names are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * identical, or 'false' (0) if they are not identical. If @ic is IGNORE_CASE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * the @upcase table is used to performa a case insensitive comparison.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) bool ntfs_are_names_equal(const ntfschar *s1, size_t s1_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) const ntfschar *s2, size_t s2_len, const IGNORE_CASE_BOOL ic,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) const ntfschar *upcase, const u32 upcase_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (s1_len != s2_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (ic == CASE_SENSITIVE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return !ntfs_ucsncmp(s1, s2, s1_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return !ntfs_ucsncasecmp(s1, s2, s1_len, upcase, upcase_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * ntfs_collate_names - collate two Unicode names
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * @name1: first Unicode name to compare
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * @name2: second Unicode name to compare
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * @err_val: if @name1 contains an invalid character return this value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * @ic: either CASE_SENSITIVE or IGNORE_CASE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * @upcase: upcase table (ignored if @ic is CASE_SENSITIVE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * @upcase_len: upcase table size (ignored if @ic is CASE_SENSITIVE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * ntfs_collate_names collates two Unicode names and returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * -1 if the first name collates before the second one,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * 0 if the names match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * 1 if the second name collates before the first one, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * @err_val if an invalid character is found in @name1 during the comparison.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * The following characters are considered invalid: '"', '*', '<', '>' and '?'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) int ntfs_collate_names(const ntfschar *name1, const u32 name1_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) const ntfschar *name2, const u32 name2_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) const int err_val, const IGNORE_CASE_BOOL ic,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) const ntfschar *upcase, const u32 upcase_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) u32 cnt, min_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) u16 c1, c2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) min_len = name1_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (name1_len > name2_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) min_len = name2_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) for (cnt = 0; cnt < min_len; ++cnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) c1 = le16_to_cpu(*name1++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) c2 = le16_to_cpu(*name2++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (ic) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (c1 < upcase_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) c1 = le16_to_cpu(upcase[c1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (c2 < upcase_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) c2 = le16_to_cpu(upcase[c2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (c1 < 64 && legal_ansi_char_array[c1] & 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return err_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (c1 < c2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (c1 > c2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (name1_len < name2_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (name1_len == name2_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) /* name1_len > name2_len */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) c1 = le16_to_cpu(*name1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (c1 < 64 && legal_ansi_char_array[c1] & 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return err_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * ntfs_ucsncmp - compare two little endian Unicode strings
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * @s1: first string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * @s2: second string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) * @n: maximum unicode characters to compare
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * Compare the first @n characters of the Unicode strings @s1 and @s2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * The strings in little endian format and appropriate le16_to_cpu()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * conversion is performed on non-little endian machines.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * The function returns an integer less than, equal to, or greater than zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * if @s1 (or the first @n Unicode characters thereof) is found, respectively,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * to be less than, to match, or be greater than @s2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) int ntfs_ucsncmp(const ntfschar *s1, const ntfschar *s2, size_t n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) u16 c1, c2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) size_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) for (i = 0; i < n; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) c1 = le16_to_cpu(s1[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) c2 = le16_to_cpu(s2[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (c1 < c2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (c1 > c2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (!c1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * ntfs_ucsncasecmp - compare two little endian Unicode strings, ignoring case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * @s1: first string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * @s2: second string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * @n: maximum unicode characters to compare
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * @upcase: upcase table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) * @upcase_size: upcase table size in Unicode characters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * Compare the first @n characters of the Unicode strings @s1 and @s2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * ignoring case. The strings in little endian format and appropriate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * le16_to_cpu() conversion is performed on non-little endian machines.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) * Each character is uppercased using the @upcase table before the comparison.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * The function returns an integer less than, equal to, or greater than zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) * if @s1 (or the first @n Unicode characters thereof) is found, respectively,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) * to be less than, to match, or be greater than @s2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) int ntfs_ucsncasecmp(const ntfschar *s1, const ntfschar *s2, size_t n,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) const ntfschar *upcase, const u32 upcase_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) size_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) u16 c1, c2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) for (i = 0; i < n; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if ((c1 = le16_to_cpu(s1[i])) < upcase_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) c1 = le16_to_cpu(upcase[c1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if ((c2 = le16_to_cpu(s2[i])) < upcase_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) c2 = le16_to_cpu(upcase[c2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (c1 < c2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (c1 > c2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (!c1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) void ntfs_upcase_name(ntfschar *name, u32 name_len, const ntfschar *upcase,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) const u32 upcase_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) u32 i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) u16 u;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) for (i = 0; i < name_len; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) if ((u = le16_to_cpu(name[i])) < upcase_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) name[i] = upcase[u];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) void ntfs_file_upcase_value(FILE_NAME_ATTR *file_name_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) const ntfschar *upcase, const u32 upcase_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) ntfs_upcase_name((ntfschar*)&file_name_attr->file_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) file_name_attr->file_name_length, upcase, upcase_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) int ntfs_file_compare_values(FILE_NAME_ATTR *file_name_attr1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) FILE_NAME_ATTR *file_name_attr2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) const int err_val, const IGNORE_CASE_BOOL ic,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) const ntfschar *upcase, const u32 upcase_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) return ntfs_collate_names((ntfschar*)&file_name_attr1->file_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) file_name_attr1->file_name_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) (ntfschar*)&file_name_attr2->file_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) file_name_attr2->file_name_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) err_val, ic, upcase, upcase_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) * ntfs_nlstoucs - convert NLS string to little endian Unicode string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) * @vol: ntfs volume which we are working with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) * @ins: input NLS string buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) * @ins_len: length of input string in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) * @outs: on return contains the allocated output Unicode string buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) * Convert the input string @ins, which is in whatever format the loaded NLS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) * map dictates, into a little endian, 2-byte Unicode string.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) * This function allocates the string and the caller is responsible for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) * calling kmem_cache_free(ntfs_name_cache, *@outs); when finished with it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) * On success the function returns the number of Unicode characters written to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) * the output string *@outs (>= 0), not counting the terminating Unicode NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) * character. *@outs is set to the allocated output string buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) * On error, a negative number corresponding to the error code is returned. In
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) * that case the output string is not allocated. Both *@outs and *@outs_len
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) * are then undefined.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) * This might look a bit odd due to fast path optimization...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) int ntfs_nlstoucs(const ntfs_volume *vol, const char *ins,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) const int ins_len, ntfschar **outs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) struct nls_table *nls = vol->nls_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) ntfschar *ucs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) wchar_t wc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) int i, o, wc_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) /* We do not trust outside sources. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) if (likely(ins)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) ucs = kmem_cache_alloc(ntfs_name_cache, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) if (likely(ucs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) for (i = o = 0; i < ins_len; i += wc_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) wc_len = nls->char2uni(ins + i, ins_len - i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) &wc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) if (likely(wc_len >= 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) o < NTFS_MAX_NAME_LEN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) if (likely(wc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) ucs[o++] = cpu_to_le16(wc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) } /* else if (!wc) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) } /* else if (wc_len < 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) o >= NTFS_MAX_NAME_LEN) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) goto name_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) ucs[o] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) *outs = ucs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) return o;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) } /* else if (!ucs) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) ntfs_error(vol->sb, "Failed to allocate buffer for converted "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) "name from ntfs_name_cache.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) } /* else if (!ins) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) ntfs_error(vol->sb, "Received NULL pointer.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) name_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) kmem_cache_free(ntfs_name_cache, ucs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) if (wc_len < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) ntfs_error(vol->sb, "Name using character set %s contains "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) "characters that cannot be converted to "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) "Unicode.", nls->charset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) i = -EILSEQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) } else /* if (o >= NTFS_MAX_NAME_LEN) */ {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) ntfs_error(vol->sb, "Name is too long (maximum length for a "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) "name on NTFS is %d Unicode characters.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) NTFS_MAX_NAME_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) i = -ENAMETOOLONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) * ntfs_ucstonls - convert little endian Unicode string to NLS string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) * @vol: ntfs volume which we are working with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * @ins: input Unicode string buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) * @ins_len: length of input string in Unicode characters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) * @outs: on return contains the (allocated) output NLS string buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) * @outs_len: length of output string buffer in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) * Convert the input little endian, 2-byte Unicode string @ins, of length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) * @ins_len into the string format dictated by the loaded NLS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) * If *@outs is NULL, this function allocates the string and the caller is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) * responsible for calling kfree(*@outs); when finished with it. In this case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) * @outs_len is ignored and can be 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) * On success the function returns the number of bytes written to the output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) * string *@outs (>= 0), not counting the terminating NULL byte. If the output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) * string buffer was allocated, *@outs is set to it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) * On error, a negative number corresponding to the error code is returned. In
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) * that case the output string is not allocated. The contents of *@outs are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) * then undefined.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) * This might look a bit odd due to fast path optimization...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) int ntfs_ucstonls(const ntfs_volume *vol, const ntfschar *ins,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) const int ins_len, unsigned char **outs, int outs_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) struct nls_table *nls = vol->nls_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) unsigned char *ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) int i, o, ns_len, wc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) /* We don't trust outside sources. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) if (ins) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) ns = *outs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) ns_len = outs_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) if (ns && !ns_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) wc = -ENAMETOOLONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) goto conversion_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) if (!ns) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) ns_len = ins_len * NLS_MAX_CHARSET_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) ns = kmalloc(ns_len + 1, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) if (!ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) goto mem_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) for (i = o = 0; i < ins_len; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) retry: wc = nls->uni2char(le16_to_cpu(ins[i]), ns + o,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) ns_len - o);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) if (wc > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) o += wc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) } else if (!wc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) else if (wc == -ENAMETOOLONG && ns != *outs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) unsigned char *tc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) /* Grow in multiples of 64 bytes. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) tc = kmalloc((ns_len + 64) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) ~63, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) if (tc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) memcpy(tc, ns, ns_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) ns_len = ((ns_len + 64) & ~63) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) kfree(ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) ns = tc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) } /* No memory so goto conversion_error; */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) } /* wc < 0, real error. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) goto conversion_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) ns[o] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) *outs = ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) return o;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) } /* else (!ins) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) ntfs_error(vol->sb, "Received NULL pointer.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) conversion_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) ntfs_error(vol->sb, "Unicode name contains characters that cannot be "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) "converted to character set %s. You might want to "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) "try to use the mount option nls=utf8.", nls->charset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) if (ns != *outs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) kfree(ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (wc != -ENAMETOOLONG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) wc = -EILSEQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) return wc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) mem_err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) ntfs_error(vol->sb, "Failed to allocate name!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }