cifs_unicode.h 8.7 KB

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