cifs_unicode.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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_ucs2(char *to, const __le16 *from, int tolen, int fromlen,
  71. const struct nls_table *codepage, bool mapchar);
  72. int cifs_ucs2_bytes(const __le16 *from, int maxbytes,
  73. const struct nls_table *codepage);
  74. int cifs_strtoUCS(__le16 *, const char *, int, const struct nls_table *);
  75. char *cifs_strndup_from_ucs(const char *src, const int maxlen,
  76. const bool is_unicode,
  77. const struct nls_table *codepage);
  78. extern int cifsConvertToUCS(__le16 *target, const char *source, int maxlen,
  79. const struct nls_table *cp, int mapChars);
  80. #endif
  81. /*
  82. * UniStrcat: Concatenate the second string to the first
  83. *
  84. * Returns:
  85. * Address of the first string
  86. */
  87. static inline wchar_t *
  88. UniStrcat(wchar_t *ucs1, const wchar_t *ucs2)
  89. {
  90. wchar_t *anchor = ucs1; /* save a pointer to start of ucs1 */
  91. while (*ucs1++) ; /* To end of first string */
  92. ucs1--; /* Return to the null */
  93. while ((*ucs1++ = *ucs2++)) ; /* copy string 2 over */
  94. return anchor;
  95. }
  96. /*
  97. * UniStrchr: Find a character in a string
  98. *
  99. * Returns:
  100. * Address of first occurrence of character in string
  101. * or NULL if the character is not in the string
  102. */
  103. static inline wchar_t *
  104. UniStrchr(const wchar_t *ucs, wchar_t uc)
  105. {
  106. while ((*ucs != uc) && *ucs)
  107. ucs++;
  108. if (*ucs == uc)
  109. return (wchar_t *) ucs;
  110. return NULL;
  111. }
  112. /*
  113. * UniStrcmp: Compare two strings
  114. *
  115. * Returns:
  116. * < 0: First string is less than second
  117. * = 0: Strings are equal
  118. * > 0: First string is greater than second
  119. */
  120. static inline int
  121. UniStrcmp(const wchar_t *ucs1, const wchar_t *ucs2)
  122. {
  123. while ((*ucs1 == *ucs2) && *ucs1) {
  124. ucs1++;
  125. ucs2++;
  126. }
  127. return (int) *ucs1 - (int) *ucs2;
  128. }
  129. /*
  130. * UniStrcpy: Copy a string
  131. */
  132. static inline wchar_t *
  133. UniStrcpy(wchar_t *ucs1, const wchar_t *ucs2)
  134. {
  135. wchar_t *anchor = ucs1; /* save the start of result string */
  136. while ((*ucs1++ = *ucs2++)) ;
  137. return anchor;
  138. }
  139. /*
  140. * UniStrlen: Return the length of a string (in 16 bit Unicode chars not bytes)
  141. */
  142. static inline size_t
  143. UniStrlen(const wchar_t *ucs1)
  144. {
  145. int i = 0;
  146. while (*ucs1++)
  147. i++;
  148. return i;
  149. }
  150. /*
  151. * UniStrnlen: Return the length (in 16 bit Unicode chars not bytes) of a
  152. * string (length limited)
  153. */
  154. static inline size_t
  155. UniStrnlen(const wchar_t *ucs1, int maxlen)
  156. {
  157. int i = 0;
  158. while (*ucs1++) {
  159. i++;
  160. if (i >= maxlen)
  161. break;
  162. }
  163. return i;
  164. }
  165. /*
  166. * UniStrncat: Concatenate length limited string
  167. */
  168. static inline wchar_t *
  169. UniStrncat(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
  170. {
  171. wchar_t *anchor = ucs1; /* save pointer to string 1 */
  172. while (*ucs1++) ;
  173. ucs1--; /* point to null terminator of s1 */
  174. while (n-- && (*ucs1 = *ucs2)) { /* copy s2 after s1 */
  175. ucs1++;
  176. ucs2++;
  177. }
  178. *ucs1 = 0; /* Null terminate the result */
  179. return (anchor);
  180. }
  181. /*
  182. * UniStrncmp: Compare length limited string
  183. */
  184. static inline int
  185. UniStrncmp(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
  186. {
  187. if (!n)
  188. return 0; /* Null strings are equal */
  189. while ((*ucs1 == *ucs2) && *ucs1 && --n) {
  190. ucs1++;
  191. ucs2++;
  192. }
  193. return (int) *ucs1 - (int) *ucs2;
  194. }
  195. /*
  196. * UniStrncmp_le: Compare length limited string - native to little-endian
  197. */
  198. static inline int
  199. UniStrncmp_le(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
  200. {
  201. if (!n)
  202. return 0; /* Null strings are equal */
  203. while ((*ucs1 == __le16_to_cpu(*ucs2)) && *ucs1 && --n) {
  204. ucs1++;
  205. ucs2++;
  206. }
  207. return (int) *ucs1 - (int) __le16_to_cpu(*ucs2);
  208. }
  209. /*
  210. * UniStrncpy: Copy length limited string with pad
  211. */
  212. static inline wchar_t *
  213. UniStrncpy(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
  214. {
  215. wchar_t *anchor = ucs1;
  216. while (n-- && *ucs2) /* Copy the strings */
  217. *ucs1++ = *ucs2++;
  218. n++;
  219. while (n--) /* Pad with nulls */
  220. *ucs1++ = 0;
  221. return anchor;
  222. }
  223. /*
  224. * UniStrncpy_le: Copy length limited string with pad to little-endian
  225. */
  226. static inline wchar_t *
  227. UniStrncpy_le(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
  228. {
  229. wchar_t *anchor = ucs1;
  230. while (n-- && *ucs2) /* Copy the strings */
  231. *ucs1++ = __le16_to_cpu(*ucs2++);
  232. n++;
  233. while (n--) /* Pad with nulls */
  234. *ucs1++ = 0;
  235. return anchor;
  236. }
  237. /*
  238. * UniStrstr: Find a string in a string
  239. *
  240. * Returns:
  241. * Address of first match found
  242. * NULL if no matching string is found
  243. */
  244. static inline wchar_t *
  245. UniStrstr(const wchar_t *ucs1, const wchar_t *ucs2)
  246. {
  247. const wchar_t *anchor1 = ucs1;
  248. const wchar_t *anchor2 = ucs2;
  249. while (*ucs1) {
  250. if (*ucs1 == *ucs2) {
  251. /* Partial match found */
  252. ucs1++;
  253. ucs2++;
  254. } else {
  255. if (!*ucs2) /* Match found */
  256. return (wchar_t *) anchor1;
  257. ucs1 = ++anchor1; /* No match */
  258. ucs2 = anchor2;
  259. }
  260. }
  261. if (!*ucs2) /* Both end together */
  262. return (wchar_t *) anchor1; /* Match found */
  263. return NULL; /* No match */
  264. }
  265. #ifndef UNIUPR_NOUPPER
  266. /*
  267. * UniToupper: Convert a unicode character to upper case
  268. */
  269. static inline wchar_t
  270. UniToupper(register wchar_t uc)
  271. {
  272. register const struct UniCaseRange *rp;
  273. if (uc < sizeof(CifsUniUpperTable)) {
  274. /* Latin characters */
  275. return uc + CifsUniUpperTable[uc]; /* Use base tables */
  276. } else {
  277. rp = CifsUniUpperRange; /* Use range tables */
  278. while (rp->start) {
  279. if (uc < rp->start) /* Before start of range */
  280. return uc; /* Uppercase = input */
  281. if (uc <= rp->end) /* In range */
  282. return uc + rp->table[uc - rp->start];
  283. rp++; /* Try next range */
  284. }
  285. }
  286. return uc; /* Past last range */
  287. }
  288. /*
  289. * UniStrupr: Upper case a unicode string
  290. */
  291. static inline wchar_t *
  292. UniStrupr(register wchar_t *upin)
  293. {
  294. register wchar_t *up;
  295. up = upin;
  296. while (*up) { /* For all characters */
  297. *up = UniToupper(*up);
  298. up++;
  299. }
  300. return upin; /* Return input pointer */
  301. }
  302. #endif /* UNIUPR_NOUPPER */
  303. #ifndef UNIUPR_NOLOWER
  304. /*
  305. * UniTolower: Convert a unicode character to lower case
  306. */
  307. static inline wchar_t
  308. UniTolower(register wchar_t uc)
  309. {
  310. register const struct UniCaseRange *rp;
  311. if (uc < sizeof(CifsUniLowerTable)) {
  312. /* Latin characters */
  313. return uc + CifsUniLowerTable[uc]; /* Use base tables */
  314. } else {
  315. rp = CifsUniLowerRange; /* Use range tables */
  316. while (rp->start) {
  317. if (uc < rp->start) /* Before start of range */
  318. return uc; /* Uppercase = input */
  319. if (uc <= rp->end) /* In range */
  320. return uc + rp->table[uc - rp->start];
  321. rp++; /* Try next range */
  322. }
  323. }
  324. return uc; /* Past last range */
  325. }
  326. /*
  327. * UniStrlwr: Lower case a unicode string
  328. */
  329. static inline wchar_t *
  330. UniStrlwr(register wchar_t *upin)
  331. {
  332. register wchar_t *up;
  333. up = upin;
  334. while (*up) { /* For all characters */
  335. *up = UniTolower(*up);
  336. up++;
  337. }
  338. return upin; /* Return input pointer */
  339. }
  340. #endif
  341. #endif /* _CIFS_UNICODE_H */