unistr.c 12 KB

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