cifs_unicode.h 10 KB

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