cifs_unicode.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. * cifs_unicode: Unicode kernel case support
  3. *
  4. * Function:
  5. * Convert a unicode character to upper or lower case using
  6. * compressed tables.
  7. *
  8. * Copyright (c) International Business Machines Corp., 2000,2009
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  18. * the GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. *
  25. * Notes:
  26. * These APIs are based on the C library functions. The semantics
  27. * should match the C functions but with expanded size operands.
  28. *
  29. * The upper/lower functions are based on a table created by mkupr.
  30. * This is a compressed table of upper and lower case conversion.
  31. *
  32. */
  33. #ifndef _CIFS_UNICODE_H
  34. #define _CIFS_UNICODE_H
  35. #include <asm/byteorder.h>
  36. #include <linux/types.h>
  37. #include <linux/nls.h>
  38. #define UNIUPR_NOLOWER /* Example to not expand lower case tables */
  39. /*
  40. * Windows maps these to the user defined 16 bit Unicode range since they are
  41. * reserved symbols (along with \ and /), otherwise illegal to store
  42. * in filenames in NTFS
  43. */
  44. #define UNI_ASTERISK (__u16) ('*' + 0xF000)
  45. #define UNI_QUESTION (__u16) ('?' + 0xF000)
  46. #define UNI_COLON (__u16) (':' + 0xF000)
  47. #define UNI_GRTRTHAN (__u16) ('>' + 0xF000)
  48. #define UNI_LESSTHAN (__u16) ('<' + 0xF000)
  49. #define UNI_PIPE (__u16) ('|' + 0xF000)
  50. #define UNI_SLASH (__u16) ('\\' + 0xF000)
  51. /* Just define what we want from uniupr.h. We don't want to define the tables
  52. * in each source file.
  53. */
  54. #ifndef UNICASERANGE_DEFINED
  55. struct UniCaseRange {
  56. wchar_t start;
  57. wchar_t end;
  58. signed char *table;
  59. };
  60. #endif /* UNICASERANGE_DEFINED */
  61. #ifndef UNIUPR_NOUPPER
  62. extern signed char CifsUniUpperTable[512];
  63. extern const struct UniCaseRange CifsUniUpperRange[];
  64. #endif /* UNIUPR_NOUPPER */
  65. #ifndef UNIUPR_NOLOWER
  66. extern signed char CifsUniLowerTable[512];
  67. extern const struct UniCaseRange CifsUniLowerRange[];
  68. #endif /* UNIUPR_NOLOWER */
  69. #ifdef __KERNEL__
  70. int cifs_from_utf16(char *to, const __le16 *from, int tolen, int fromlen,
  71. const struct nls_table *codepage, bool mapchar);
  72. int cifs_utf16_bytes(const __le16 *from, int maxbytes,
  73. const struct nls_table *codepage);
  74. int cifs_strtoUTF16(__le16 *, const char *, int, const struct nls_table *);
  75. char *cifs_strndup_from_utf16(const char *src, const int maxlen,
  76. const bool is_unicode,
  77. const struct nls_table *codepage);
  78. extern int cifsConvertToUTF16(__le16 *target, const char *source, int maxlen,
  79. const struct nls_table *cp, int mapChars);
  80. #ifdef CONFIG_CIFS_SMB2
  81. extern __le16 *cifs_strndup_to_utf16(const char *src, const int maxlen,
  82. int *utf16_len, const struct nls_table *cp,
  83. int remap);
  84. #endif /* CONFIG_CIFS_SMB2 */
  85. #endif
  86. wchar_t cifs_toupper(wchar_t in);
  87. /*
  88. * UniStrcat: Concatenate the second string to the first
  89. *
  90. * Returns:
  91. * Address of the first string
  92. */
  93. static inline wchar_t *
  94. UniStrcat(wchar_t *ucs1, const wchar_t *ucs2)
  95. {
  96. wchar_t *anchor = ucs1; /* save a pointer to start of ucs1 */
  97. while (*ucs1++) ; /* To end of first string */
  98. ucs1--; /* Return to the null */
  99. while ((*ucs1++ = *ucs2++)) ; /* copy string 2 over */
  100. return anchor;
  101. }
  102. /*
  103. * UniStrchr: Find a character in a string
  104. *
  105. * Returns:
  106. * Address of first occurrence of character in string
  107. * or NULL if the character is not in the string
  108. */
  109. static inline wchar_t *
  110. UniStrchr(const wchar_t *ucs, wchar_t uc)
  111. {
  112. while ((*ucs != uc) && *ucs)
  113. ucs++;
  114. if (*ucs == uc)
  115. return (wchar_t *) ucs;
  116. return NULL;
  117. }
  118. /*
  119. * UniStrcmp: Compare two strings
  120. *
  121. * Returns:
  122. * < 0: First string is less than second
  123. * = 0: Strings are equal
  124. * > 0: First string is greater than second
  125. */
  126. static inline int
  127. UniStrcmp(const wchar_t *ucs1, const wchar_t *ucs2)
  128. {
  129. while ((*ucs1 == *ucs2) && *ucs1) {
  130. ucs1++;
  131. ucs2++;
  132. }
  133. return (int) *ucs1 - (int) *ucs2;
  134. }
  135. /*
  136. * UniStrcpy: Copy a string
  137. */
  138. static inline wchar_t *
  139. UniStrcpy(wchar_t *ucs1, const wchar_t *ucs2)
  140. {
  141. wchar_t *anchor = ucs1; /* save the start of result string */
  142. while ((*ucs1++ = *ucs2++)) ;
  143. return anchor;
  144. }
  145. /*
  146. * UniStrlen: Return the length of a string (in 16 bit Unicode chars not bytes)
  147. */
  148. static inline size_t
  149. UniStrlen(const wchar_t *ucs1)
  150. {
  151. int i = 0;
  152. while (*ucs1++)
  153. i++;
  154. return i;
  155. }
  156. /*
  157. * UniStrnlen: Return the length (in 16 bit Unicode chars not bytes) of a
  158. * string (length limited)
  159. */
  160. static inline size_t
  161. UniStrnlen(const wchar_t *ucs1, int maxlen)
  162. {
  163. int i = 0;
  164. while (*ucs1++) {
  165. i++;
  166. if (i >= maxlen)
  167. break;
  168. }
  169. return i;
  170. }
  171. /*
  172. * UniStrncat: Concatenate length limited string
  173. */
  174. static inline wchar_t *
  175. UniStrncat(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
  176. {
  177. wchar_t *anchor = ucs1; /* save pointer to string 1 */
  178. while (*ucs1++) ;
  179. ucs1--; /* point to null terminator of s1 */
  180. while (n-- && (*ucs1 = *ucs2)) { /* copy s2 after s1 */
  181. ucs1++;
  182. ucs2++;
  183. }
  184. *ucs1 = 0; /* Null terminate the result */
  185. return (anchor);
  186. }
  187. /*
  188. * UniStrncmp: Compare length limited string
  189. */
  190. static inline int
  191. UniStrncmp(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
  192. {
  193. if (!n)
  194. return 0; /* Null strings are equal */
  195. while ((*ucs1 == *ucs2) && *ucs1 && --n) {
  196. ucs1++;
  197. ucs2++;
  198. }
  199. return (int) *ucs1 - (int) *ucs2;
  200. }
  201. /*
  202. * UniStrncmp_le: Compare length limited string - native to little-endian
  203. */
  204. static inline int
  205. UniStrncmp_le(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
  206. {
  207. if (!n)
  208. return 0; /* Null strings are equal */
  209. while ((*ucs1 == __le16_to_cpu(*ucs2)) && *ucs1 && --n) {
  210. ucs1++;
  211. ucs2++;
  212. }
  213. return (int) *ucs1 - (int) __le16_to_cpu(*ucs2);
  214. }
  215. /*
  216. * UniStrncpy: Copy length limited string with pad
  217. */
  218. static inline wchar_t *
  219. UniStrncpy(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
  220. {
  221. wchar_t *anchor = ucs1;
  222. while (n-- && *ucs2) /* Copy the strings */
  223. *ucs1++ = *ucs2++;
  224. n++;
  225. while (n--) /* Pad with nulls */
  226. *ucs1++ = 0;
  227. return anchor;
  228. }
  229. /*
  230. * UniStrncpy_le: Copy length limited string with pad to little-endian
  231. */
  232. static inline wchar_t *
  233. UniStrncpy_le(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
  234. {
  235. wchar_t *anchor = ucs1;
  236. while (n-- && *ucs2) /* Copy the strings */
  237. *ucs1++ = __le16_to_cpu(*ucs2++);
  238. n++;
  239. while (n--) /* Pad with nulls */
  240. *ucs1++ = 0;
  241. return anchor;
  242. }
  243. /*
  244. * UniStrstr: Find a string in a string
  245. *
  246. * Returns:
  247. * Address of first match found
  248. * NULL if no matching string is found
  249. */
  250. static inline wchar_t *
  251. UniStrstr(const wchar_t *ucs1, const wchar_t *ucs2)
  252. {
  253. const wchar_t *anchor1 = ucs1;
  254. const wchar_t *anchor2 = ucs2;
  255. while (*ucs1) {
  256. if (*ucs1 == *ucs2) {
  257. /* Partial match found */
  258. ucs1++;
  259. ucs2++;
  260. } else {
  261. if (!*ucs2) /* Match found */
  262. return (wchar_t *) anchor1;
  263. ucs1 = ++anchor1; /* No match */
  264. ucs2 = anchor2;
  265. }
  266. }
  267. if (!*ucs2) /* Both end together */
  268. return (wchar_t *) anchor1; /* Match found */
  269. return NULL; /* No match */
  270. }
  271. #ifndef UNIUPR_NOUPPER
  272. /*
  273. * UniToupper: Convert a unicode character to upper case
  274. */
  275. static inline wchar_t
  276. UniToupper(register wchar_t uc)
  277. {
  278. register const struct UniCaseRange *rp;
  279. if (uc < sizeof(CifsUniUpperTable)) {
  280. /* Latin characters */
  281. return uc + CifsUniUpperTable[uc]; /* Use base tables */
  282. } else {
  283. rp = CifsUniUpperRange; /* Use range tables */
  284. while (rp->start) {
  285. if (uc < rp->start) /* Before start of range */
  286. return uc; /* Uppercase = input */
  287. if (uc <= rp->end) /* In range */
  288. return uc + rp->table[uc - rp->start];
  289. rp++; /* Try next range */
  290. }
  291. }
  292. return uc; /* Past last range */
  293. }
  294. /*
  295. * UniStrupr: Upper case a unicode string
  296. */
  297. static inline __le16 *
  298. UniStrupr(register __le16 *upin)
  299. {
  300. register __le16 *up;
  301. up = upin;
  302. while (*up) { /* For all characters */
  303. *up = cpu_to_le16(UniToupper(le16_to_cpu(*up)));
  304. up++;
  305. }
  306. return upin; /* Return input pointer */
  307. }
  308. #endif /* UNIUPR_NOUPPER */
  309. #ifndef UNIUPR_NOLOWER
  310. /*
  311. * UniTolower: Convert a unicode character to lower case
  312. */
  313. static inline wchar_t
  314. UniTolower(register wchar_t uc)
  315. {
  316. register const struct UniCaseRange *rp;
  317. if (uc < sizeof(CifsUniLowerTable)) {
  318. /* Latin characters */
  319. return uc + CifsUniLowerTable[uc]; /* Use base tables */
  320. } else {
  321. rp = CifsUniLowerRange; /* Use range tables */
  322. while (rp->start) {
  323. if (uc < rp->start) /* Before start of range */
  324. return uc; /* Uppercase = input */
  325. if (uc <= rp->end) /* In range */
  326. return uc + rp->table[uc - rp->start];
  327. rp++; /* Try next range */
  328. }
  329. }
  330. return uc; /* Past last range */
  331. }
  332. /*
  333. * UniStrlwr: Lower case a unicode string
  334. */
  335. static inline wchar_t *
  336. UniStrlwr(register wchar_t *upin)
  337. {
  338. register wchar_t *up;
  339. up = upin;
  340. while (*up) { /* For all characters */
  341. *up = UniTolower(*up);
  342. up++;
  343. }
  344. return upin; /* Return input pointer */
  345. }
  346. #endif
  347. #endif /* _CIFS_UNICODE_H */