cifs_unicode.h 7.9 KB

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