cifs_unicode.h 9.6 KB

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