cifs_unicode.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. /*
  52. * Macs use an older "SFM" mapping of the symbols above. Fortunately it does
  53. * not conflict (although almost does) with the mapping above.
  54. */
  55. #define SFM_ASTERISK ((__u16) 0xF021)
  56. #define SFM_QUESTION ((__u16) 0xF025)
  57. #define SFM_COLON ((__u16) 0xF022)
  58. #define SFM_GRTRTHAN ((__u16) 0xF024)
  59. #define SFM_LESSTHAN ((__u16) 0xF023)
  60. #define SFM_PIPE ((__u16) 0xF027)
  61. #define SFM_SLASH ((__u16) 0xF026)
  62. /*
  63. * Mapping mechanism to use when one of the seven reserved characters is
  64. * encountered. We can only map using one of the mechanisms at a time
  65. * since otherwise readdir could return directory entries which we would
  66. * not be able to open
  67. *
  68. * NO_MAP_UNI_RSVD = do not perform any remapping of the character
  69. * SFM_MAP_UNI_RSVD = map reserved characters using SFM scheme (MAC compatible)
  70. * SFU_MAP_UNI_RSVD = map reserved characters ala SFU ("mapchars" option)
  71. *
  72. */
  73. #define NO_MAP_UNI_RSVD 0
  74. #define SFM_MAP_UNI_RSVD 1
  75. #define SFU_MAP_UNI_RSVD 2
  76. /* Just define what we want from uniupr.h. We don't want to define the tables
  77. * in each source file.
  78. */
  79. #ifndef UNICASERANGE_DEFINED
  80. struct UniCaseRange {
  81. wchar_t start;
  82. wchar_t end;
  83. signed char *table;
  84. };
  85. #endif /* UNICASERANGE_DEFINED */
  86. #ifndef UNIUPR_NOUPPER
  87. extern signed char CifsUniUpperTable[512];
  88. extern const struct UniCaseRange CifsUniUpperRange[];
  89. #endif /* UNIUPR_NOUPPER */
  90. #ifndef UNIUPR_NOLOWER
  91. extern signed char CifsUniLowerTable[512];
  92. extern const struct UniCaseRange CifsUniLowerRange[];
  93. #endif /* UNIUPR_NOLOWER */
  94. #ifdef __KERNEL__
  95. int cifs_from_utf16(char *to, const __le16 *from, int tolen, int fromlen,
  96. const struct nls_table *cp, int map_type);
  97. int cifs_utf16_bytes(const __le16 *from, int maxbytes,
  98. const struct nls_table *codepage);
  99. int cifs_strtoUTF16(__le16 *, const char *, int, const struct nls_table *);
  100. char *cifs_strndup_from_utf16(const char *src, const int maxlen,
  101. const bool is_unicode,
  102. const struct nls_table *codepage);
  103. extern int cifsConvertToUTF16(__le16 *target, const char *source, int maxlen,
  104. const struct nls_table *cp, int mapChars);
  105. extern int cifs_remap(struct cifs_sb_info *cifs_sb);
  106. #ifdef CONFIG_CIFS_SMB2
  107. extern __le16 *cifs_strndup_to_utf16(const char *src, const int maxlen,
  108. int *utf16_len, const struct nls_table *cp,
  109. int remap);
  110. #endif /* CONFIG_CIFS_SMB2 */
  111. #endif
  112. wchar_t cifs_toupper(wchar_t in);
  113. /*
  114. * UniStrcat: Concatenate the second string to the first
  115. *
  116. * Returns:
  117. * Address of the first string
  118. */
  119. static inline wchar_t *
  120. UniStrcat(wchar_t *ucs1, const wchar_t *ucs2)
  121. {
  122. wchar_t *anchor = ucs1; /* save a pointer to start of ucs1 */
  123. while (*ucs1++) ; /* To end of first string */
  124. ucs1--; /* Return to the null */
  125. while ((*ucs1++ = *ucs2++)) ; /* copy string 2 over */
  126. return anchor;
  127. }
  128. /*
  129. * UniStrchr: Find a character in a string
  130. *
  131. * Returns:
  132. * Address of first occurrence of character in string
  133. * or NULL if the character is not in the string
  134. */
  135. static inline wchar_t *
  136. UniStrchr(const wchar_t *ucs, wchar_t uc)
  137. {
  138. while ((*ucs != uc) && *ucs)
  139. ucs++;
  140. if (*ucs == uc)
  141. return (wchar_t *) ucs;
  142. return NULL;
  143. }
  144. /*
  145. * UniStrcmp: Compare two strings
  146. *
  147. * Returns:
  148. * < 0: First string is less than second
  149. * = 0: Strings are equal
  150. * > 0: First string is greater than second
  151. */
  152. static inline int
  153. UniStrcmp(const wchar_t *ucs1, const wchar_t *ucs2)
  154. {
  155. while ((*ucs1 == *ucs2) && *ucs1) {
  156. ucs1++;
  157. ucs2++;
  158. }
  159. return (int) *ucs1 - (int) *ucs2;
  160. }
  161. /*
  162. * UniStrcpy: Copy a string
  163. */
  164. static inline wchar_t *
  165. UniStrcpy(wchar_t *ucs1, const wchar_t *ucs2)
  166. {
  167. wchar_t *anchor = ucs1; /* save the start of result string */
  168. while ((*ucs1++ = *ucs2++)) ;
  169. return anchor;
  170. }
  171. /*
  172. * UniStrlen: Return the length of a string (in 16 bit Unicode chars not bytes)
  173. */
  174. static inline size_t
  175. UniStrlen(const wchar_t *ucs1)
  176. {
  177. int i = 0;
  178. while (*ucs1++)
  179. i++;
  180. return i;
  181. }
  182. /*
  183. * UniStrnlen: Return the length (in 16 bit Unicode chars not bytes) of a
  184. * string (length limited)
  185. */
  186. static inline size_t
  187. UniStrnlen(const wchar_t *ucs1, int maxlen)
  188. {
  189. int i = 0;
  190. while (*ucs1++) {
  191. i++;
  192. if (i >= maxlen)
  193. break;
  194. }
  195. return i;
  196. }
  197. /*
  198. * UniStrncat: Concatenate length limited string
  199. */
  200. static inline wchar_t *
  201. UniStrncat(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
  202. {
  203. wchar_t *anchor = ucs1; /* save pointer to string 1 */
  204. while (*ucs1++) ;
  205. ucs1--; /* point to null terminator of s1 */
  206. while (n-- && (*ucs1 = *ucs2)) { /* copy s2 after s1 */
  207. ucs1++;
  208. ucs2++;
  209. }
  210. *ucs1 = 0; /* Null terminate the result */
  211. return (anchor);
  212. }
  213. /*
  214. * UniStrncmp: Compare length limited string
  215. */
  216. static inline int
  217. UniStrncmp(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
  218. {
  219. if (!n)
  220. return 0; /* Null strings are equal */
  221. while ((*ucs1 == *ucs2) && *ucs1 && --n) {
  222. ucs1++;
  223. ucs2++;
  224. }
  225. return (int) *ucs1 - (int) *ucs2;
  226. }
  227. /*
  228. * UniStrncmp_le: Compare length limited string - native to little-endian
  229. */
  230. static inline int
  231. UniStrncmp_le(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
  232. {
  233. if (!n)
  234. return 0; /* Null strings are equal */
  235. while ((*ucs1 == __le16_to_cpu(*ucs2)) && *ucs1 && --n) {
  236. ucs1++;
  237. ucs2++;
  238. }
  239. return (int) *ucs1 - (int) __le16_to_cpu(*ucs2);
  240. }
  241. /*
  242. * UniStrncpy: Copy length limited string with pad
  243. */
  244. static inline wchar_t *
  245. UniStrncpy(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
  246. {
  247. wchar_t *anchor = ucs1;
  248. while (n-- && *ucs2) /* Copy the strings */
  249. *ucs1++ = *ucs2++;
  250. n++;
  251. while (n--) /* Pad with nulls */
  252. *ucs1++ = 0;
  253. return anchor;
  254. }
  255. /*
  256. * UniStrncpy_le: Copy length limited string with pad to little-endian
  257. */
  258. static inline wchar_t *
  259. UniStrncpy_le(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
  260. {
  261. wchar_t *anchor = ucs1;
  262. while (n-- && *ucs2) /* Copy the strings */
  263. *ucs1++ = __le16_to_cpu(*ucs2++);
  264. n++;
  265. while (n--) /* Pad with nulls */
  266. *ucs1++ = 0;
  267. return anchor;
  268. }
  269. /*
  270. * UniStrstr: Find a string in a string
  271. *
  272. * Returns:
  273. * Address of first match found
  274. * NULL if no matching string is found
  275. */
  276. static inline wchar_t *
  277. UniStrstr(const wchar_t *ucs1, const wchar_t *ucs2)
  278. {
  279. const wchar_t *anchor1 = ucs1;
  280. const wchar_t *anchor2 = ucs2;
  281. while (*ucs1) {
  282. if (*ucs1 == *ucs2) {
  283. /* Partial match found */
  284. ucs1++;
  285. ucs2++;
  286. } else {
  287. if (!*ucs2) /* Match found */
  288. return (wchar_t *) anchor1;
  289. ucs1 = ++anchor1; /* No match */
  290. ucs2 = anchor2;
  291. }
  292. }
  293. if (!*ucs2) /* Both end together */
  294. return (wchar_t *) anchor1; /* Match found */
  295. return NULL; /* No match */
  296. }
  297. #ifndef UNIUPR_NOUPPER
  298. /*
  299. * UniToupper: Convert a unicode character to upper case
  300. */
  301. static inline wchar_t
  302. UniToupper(register wchar_t uc)
  303. {
  304. register const struct UniCaseRange *rp;
  305. if (uc < sizeof(CifsUniUpperTable)) {
  306. /* Latin characters */
  307. return uc + CifsUniUpperTable[uc]; /* Use base tables */
  308. } else {
  309. rp = CifsUniUpperRange; /* Use range tables */
  310. while (rp->start) {
  311. if (uc < rp->start) /* Before start of range */
  312. return uc; /* Uppercase = input */
  313. if (uc <= rp->end) /* In range */
  314. return uc + rp->table[uc - rp->start];
  315. rp++; /* Try next range */
  316. }
  317. }
  318. return uc; /* Past last range */
  319. }
  320. /*
  321. * UniStrupr: Upper case a unicode string
  322. */
  323. static inline __le16 *
  324. UniStrupr(register __le16 *upin)
  325. {
  326. register __le16 *up;
  327. up = upin;
  328. while (*up) { /* For all characters */
  329. *up = cpu_to_le16(UniToupper(le16_to_cpu(*up)));
  330. up++;
  331. }
  332. return upin; /* Return input pointer */
  333. }
  334. #endif /* UNIUPR_NOUPPER */
  335. #ifndef UNIUPR_NOLOWER
  336. /*
  337. * UniTolower: Convert a unicode character to lower case
  338. */
  339. static inline wchar_t
  340. UniTolower(register wchar_t uc)
  341. {
  342. register const struct UniCaseRange *rp;
  343. if (uc < sizeof(CifsUniLowerTable)) {
  344. /* Latin characters */
  345. return uc + CifsUniLowerTable[uc]; /* Use base tables */
  346. } else {
  347. rp = CifsUniLowerRange; /* Use range tables */
  348. while (rp->start) {
  349. if (uc < rp->start) /* Before start of range */
  350. return uc; /* Uppercase = input */
  351. if (uc <= rp->end) /* In range */
  352. return uc + rp->table[uc - rp->start];
  353. rp++; /* Try next range */
  354. }
  355. }
  356. return uc; /* Past last range */
  357. }
  358. /*
  359. * UniStrlwr: Lower case a unicode string
  360. */
  361. static inline wchar_t *
  362. UniStrlwr(register wchar_t *upin)
  363. {
  364. register wchar_t *up;
  365. up = upin;
  366. while (*up) { /* For all characters */
  367. *up = UniTolower(*up);
  368. up++;
  369. }
  370. return upin; /* Return input pointer */
  371. }
  372. #endif
  373. #endif /* _CIFS_UNICODE_H */