cifs_unicode.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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,2007
  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. #include <asm/byteorder.h>
  34. #include <linux/types.h>
  35. #include <linux/nls.h>
  36. #define UNIUPR_NOLOWER /* Example to not expand lower case tables */
  37. /*
  38. * Windows maps these to the user defined 16 bit Unicode range since they are
  39. * reserved symbols (along with \ and /), otherwise illegal to store
  40. * in filenames in NTFS
  41. */
  42. #define UNI_ASTERIK (__u16) ('*' + 0xF000)
  43. #define UNI_QUESTION (__u16) ('?' + 0xF000)
  44. #define UNI_COLON (__u16) (':' + 0xF000)
  45. #define UNI_GRTRTHAN (__u16) ('>' + 0xF000)
  46. #define UNI_LESSTHAN (__u16) ('<' + 0xF000)
  47. #define UNI_PIPE (__u16) ('|' + 0xF000)
  48. #define UNI_SLASH (__u16) ('\\' + 0xF000)
  49. /* Just define what we want from uniupr.h. We don't want to define the tables
  50. * in each source file.
  51. */
  52. #ifndef UNICASERANGE_DEFINED
  53. struct UniCaseRange {
  54. wchar_t start;
  55. wchar_t end;
  56. signed char *table;
  57. };
  58. #endif /* UNICASERANGE_DEFINED */
  59. #ifndef UNIUPR_NOUPPER
  60. extern signed char CifsUniUpperTable[512];
  61. extern const struct UniCaseRange CifsUniUpperRange[];
  62. #endif /* UNIUPR_NOUPPER */
  63. #ifndef UNIUPR_NOLOWER
  64. extern signed char UniLowerTable[512];
  65. extern struct UniCaseRange UniLowerRange[];
  66. #endif /* UNIUPR_NOLOWER */
  67. #ifdef __KERNEL__
  68. int cifs_from_ucs2(char *to, const __le16 *from, int tolen, int fromlen,
  69. const struct nls_table *codepage, bool mapchar);
  70. int cifs_strfromUCS_le(char *, const __le16 *, int, const struct nls_table *);
  71. int cifs_strtoUCS(__le16 *, const char *, int, const struct nls_table *);
  72. #endif
  73. /*
  74. * UniStrcat: Concatenate the second string to the first
  75. *
  76. * Returns:
  77. * Address of the first string
  78. */
  79. static inline wchar_t *
  80. UniStrcat(wchar_t *ucs1, const wchar_t *ucs2)
  81. {
  82. wchar_t *anchor = ucs1; /* save a pointer to start of ucs1 */
  83. while (*ucs1++) ; /* To end of first string */
  84. ucs1--; /* Return to the null */
  85. while ((*ucs1++ = *ucs2++)) ; /* copy string 2 over */
  86. return anchor;
  87. }
  88. /*
  89. * UniStrchr: Find a character in a string
  90. *
  91. * Returns:
  92. * Address of first occurrence of character in string
  93. * or NULL if the character is not in the string
  94. */
  95. static inline wchar_t *
  96. UniStrchr(const wchar_t *ucs, wchar_t uc)
  97. {
  98. while ((*ucs != uc) && *ucs)
  99. ucs++;
  100. if (*ucs == uc)
  101. return (wchar_t *) ucs;
  102. return NULL;
  103. }
  104. /*
  105. * UniStrcmp: Compare two strings
  106. *
  107. * Returns:
  108. * < 0: First string is less than second
  109. * = 0: Strings are equal
  110. * > 0: First string is greater than second
  111. */
  112. static inline int
  113. UniStrcmp(const wchar_t *ucs1, const wchar_t *ucs2)
  114. {
  115. while ((*ucs1 == *ucs2) && *ucs1) {
  116. ucs1++;
  117. ucs2++;
  118. }
  119. return (int) *ucs1 - (int) *ucs2;
  120. }
  121. /*
  122. * UniStrcpy: Copy a string
  123. */
  124. static inline wchar_t *
  125. UniStrcpy(wchar_t *ucs1, const wchar_t *ucs2)
  126. {
  127. wchar_t *anchor = ucs1; /* save the start of result string */
  128. while ((*ucs1++ = *ucs2++)) ;
  129. return anchor;
  130. }
  131. /*
  132. * UniStrlen: Return the length of a string (in 16 bit Unicode chars not bytes)
  133. */
  134. static inline size_t
  135. UniStrlen(const wchar_t *ucs1)
  136. {
  137. int i = 0;
  138. while (*ucs1++)
  139. i++;
  140. return i;
  141. }
  142. /*
  143. * UniStrnlen: Return the length (in 16 bit Unicode chars not bytes) of a
  144. * string (length limited)
  145. */
  146. static inline size_t
  147. UniStrnlen(const wchar_t *ucs1, int maxlen)
  148. {
  149. int i = 0;
  150. while (*ucs1++) {
  151. i++;
  152. if (i >= maxlen)
  153. break;
  154. }
  155. return i;
  156. }
  157. /*
  158. * UniStrncat: Concatenate length limited string
  159. */
  160. static inline wchar_t *
  161. UniStrncat(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
  162. {
  163. wchar_t *anchor = ucs1; /* save pointer to string 1 */
  164. while (*ucs1++) ;
  165. ucs1--; /* point to null terminator of s1 */
  166. while (n-- && (*ucs1 = *ucs2)) { /* copy s2 after s1 */
  167. ucs1++;
  168. ucs2++;
  169. }
  170. *ucs1 = 0; /* Null terminate the result */
  171. return (anchor);
  172. }
  173. /*
  174. * UniStrncmp: Compare length limited string
  175. */
  176. static inline int
  177. UniStrncmp(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
  178. {
  179. if (!n)
  180. return 0; /* Null strings are equal */
  181. while ((*ucs1 == *ucs2) && *ucs1 && --n) {
  182. ucs1++;
  183. ucs2++;
  184. }
  185. return (int) *ucs1 - (int) *ucs2;
  186. }
  187. /*
  188. * UniStrncmp_le: Compare length limited string - native to little-endian
  189. */
  190. static inline int
  191. UniStrncmp_le(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 == __le16_to_cpu(*ucs2)) && *ucs1 && --n) {
  196. ucs1++;
  197. ucs2++;
  198. }
  199. return (int) *ucs1 - (int) __le16_to_cpu(*ucs2);
  200. }
  201. /*
  202. * UniStrncpy: Copy length limited string with pad
  203. */
  204. static inline wchar_t *
  205. UniStrncpy(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
  206. {
  207. wchar_t *anchor = ucs1;
  208. while (n-- && *ucs2) /* Copy the strings */
  209. *ucs1++ = *ucs2++;
  210. n++;
  211. while (n--) /* Pad with nulls */
  212. *ucs1++ = 0;
  213. return anchor;
  214. }
  215. /*
  216. * UniStrncpy_le: Copy length limited string with pad to little-endian
  217. */
  218. static inline wchar_t *
  219. UniStrncpy_le(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++ = __le16_to_cpu(*ucs2++);
  224. n++;
  225. while (n--) /* Pad with nulls */
  226. *ucs1++ = 0;
  227. return anchor;
  228. }
  229. /*
  230. * UniStrstr: Find a string in a string
  231. *
  232. * Returns:
  233. * Address of first match found
  234. * NULL if no matching string is found
  235. */
  236. static inline wchar_t *
  237. UniStrstr(const wchar_t *ucs1, const wchar_t *ucs2)
  238. {
  239. const wchar_t *anchor1 = ucs1;
  240. const wchar_t *anchor2 = ucs2;
  241. while (*ucs1) {
  242. if (*ucs1 == *ucs2) {
  243. /* Partial match found */
  244. ucs1++;
  245. ucs2++;
  246. } else {
  247. if (!*ucs2) /* Match found */
  248. return (wchar_t *) anchor1;
  249. ucs1 = ++anchor1; /* No match */
  250. ucs2 = anchor2;
  251. }
  252. }
  253. if (!*ucs2) /* Both end together */
  254. return (wchar_t *) anchor1; /* Match found */
  255. return NULL; /* No match */
  256. }
  257. #ifndef UNIUPR_NOUPPER
  258. /*
  259. * UniToupper: Convert a unicode character to upper case
  260. */
  261. static inline wchar_t
  262. UniToupper(register wchar_t uc)
  263. {
  264. register const struct UniCaseRange *rp;
  265. if (uc < sizeof(CifsUniUpperTable)) {
  266. /* Latin characters */
  267. return uc + CifsUniUpperTable[uc]; /* Use base tables */
  268. } else {
  269. rp = CifsUniUpperRange; /* Use range tables */
  270. while (rp->start) {
  271. if (uc < rp->start) /* Before start of range */
  272. return uc; /* Uppercase = input */
  273. if (uc <= rp->end) /* In range */
  274. return uc + rp->table[uc - rp->start];
  275. rp++; /* Try next range */
  276. }
  277. }
  278. return uc; /* Past last range */
  279. }
  280. /*
  281. * UniStrupr: Upper case a unicode string
  282. */
  283. static inline wchar_t *
  284. UniStrupr(register wchar_t *upin)
  285. {
  286. register wchar_t *up;
  287. up = upin;
  288. while (*up) { /* For all characters */
  289. *up = UniToupper(*up);
  290. up++;
  291. }
  292. return upin; /* Return input pointer */
  293. }
  294. #endif /* UNIUPR_NOUPPER */
  295. #ifndef UNIUPR_NOLOWER
  296. /*
  297. * UniTolower: Convert a unicode character to lower case
  298. */
  299. static inline wchar_t
  300. UniTolower(wchar_t uc)
  301. {
  302. register struct UniCaseRange *rp;
  303. if (uc < sizeof(UniLowerTable)) {
  304. /* Latin characters */
  305. return uc + UniLowerTable[uc]; /* Use base tables */
  306. } else {
  307. rp = UniLowerRange; /* Use range tables */
  308. while (rp->start) {
  309. if (uc < rp->start) /* Before start of range */
  310. return uc; /* Uppercase = input */
  311. if (uc <= rp->end) /* In range */
  312. return uc + rp->table[uc - rp->start];
  313. rp++; /* Try next range */
  314. }
  315. }
  316. return uc; /* Past last range */
  317. }
  318. /*
  319. * UniStrlwr: Lower case a unicode string
  320. */
  321. static inline wchar_t *
  322. UniStrlwr(register wchar_t *upin)
  323. {
  324. register wchar_t *up;
  325. up = upin;
  326. while (*up) { /* For all characters */
  327. *up = UniTolower(*up);
  328. up++;
  329. }
  330. return upin; /* Return input pointer */
  331. }
  332. #endif