cifs_unicode.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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,2005555555555555555555555555555555555555555555555555555555
  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 string (length limited)
  130. */
  131. static inline size_t
  132. UniStrnlen(const wchar_t * ucs1, int maxlen)
  133. {
  134. int i = 0;
  135. while (*ucs1++) {
  136. i++;
  137. if (i >= maxlen)
  138. break;
  139. }
  140. return i;
  141. }
  142. /*
  143. * UniStrncat: Concatenate length limited string
  144. */
  145. static inline wchar_t *
  146. UniStrncat(wchar_t * ucs1, const wchar_t * ucs2, size_t n)
  147. {
  148. wchar_t *anchor = ucs1; /* save pointer to string 1 */
  149. while (*ucs1++) ;
  150. ucs1--; /* point to null terminator of s1 */
  151. while (n-- && (*ucs1 = *ucs2)) { /* copy s2 after s1 */
  152. ucs1++;
  153. ucs2++;
  154. }
  155. *ucs1 = 0; /* Null terminate the result */
  156. return (anchor);
  157. }
  158. /*
  159. * UniStrncmp: Compare length limited string
  160. */
  161. static inline int
  162. UniStrncmp(const wchar_t * ucs1, const wchar_t * ucs2, size_t n)
  163. {
  164. if (!n)
  165. return 0; /* Null strings are equal */
  166. while ((*ucs1 == *ucs2) && *ucs1 && --n) {
  167. ucs1++;
  168. ucs2++;
  169. }
  170. return (int) *ucs1 - (int) *ucs2;
  171. }
  172. /*
  173. * UniStrncmp_le: Compare length limited string - native to little-endian
  174. */
  175. static inline int
  176. UniStrncmp_le(const wchar_t * ucs1, const wchar_t * ucs2, size_t n)
  177. {
  178. if (!n)
  179. return 0; /* Null strings are equal */
  180. while ((*ucs1 == __le16_to_cpu(*ucs2)) && *ucs1 && --n) {
  181. ucs1++;
  182. ucs2++;
  183. }
  184. return (int) *ucs1 - (int) __le16_to_cpu(*ucs2);
  185. }
  186. /*
  187. * UniStrncpy: Copy length limited string with pad
  188. */
  189. static inline wchar_t *
  190. UniStrncpy(wchar_t * ucs1, const wchar_t * ucs2, size_t n)
  191. {
  192. wchar_t *anchor = ucs1;
  193. while (n-- && *ucs2) /* Copy the strings */
  194. *ucs1++ = *ucs2++;
  195. n++;
  196. while (n--) /* Pad with nulls */
  197. *ucs1++ = 0;
  198. return anchor;
  199. }
  200. /*
  201. * UniStrncpy_le: Copy length limited string with pad to little-endian
  202. */
  203. static inline wchar_t *
  204. UniStrncpy_le(wchar_t * ucs1, const wchar_t * ucs2, size_t n)
  205. {
  206. wchar_t *anchor = ucs1;
  207. while (n-- && *ucs2) /* Copy the strings */
  208. *ucs1++ = __le16_to_cpu(*ucs2++);
  209. n++;
  210. while (n--) /* Pad with nulls */
  211. *ucs1++ = 0;
  212. return anchor;
  213. }
  214. /*
  215. * UniStrstr: Find a string in a string
  216. *
  217. * Returns:
  218. * Address of first match found
  219. * NULL if no matching string is found
  220. */
  221. static inline wchar_t *
  222. UniStrstr(const wchar_t * ucs1, const wchar_t * ucs2)
  223. {
  224. const wchar_t *anchor1 = ucs1;
  225. const wchar_t *anchor2 = ucs2;
  226. while (*ucs1) {
  227. if (*ucs1 == *ucs2) { /* Partial match found */
  228. ucs1++;
  229. ucs2++;
  230. } else {
  231. if (!*ucs2) /* Match found */
  232. return (wchar_t *) anchor1;
  233. ucs1 = ++anchor1; /* No match */
  234. ucs2 = anchor2;
  235. }
  236. }
  237. if (!*ucs2) /* Both end together */
  238. return (wchar_t *) anchor1; /* Match found */
  239. return NULL; /* No match */
  240. }
  241. #ifndef UNIUPR_NOUPPER
  242. /*
  243. * UniToupper: Convert a unicode character to upper case
  244. */
  245. static inline wchar_t
  246. UniToupper(register wchar_t uc)
  247. {
  248. register const struct UniCaseRange *rp;
  249. if (uc < sizeof (CifsUniUpperTable)) { /* Latin characters */
  250. return uc + CifsUniUpperTable[uc]; /* Use base tables */
  251. } else {
  252. rp = CifsUniUpperRange; /* Use range tables */
  253. while (rp->start) {
  254. if (uc < rp->start) /* Before start of range */
  255. return uc; /* Uppercase = input */
  256. if (uc <= rp->end) /* In range */
  257. return uc + rp->table[uc - rp->start];
  258. rp++; /* Try next range */
  259. }
  260. }
  261. return uc; /* Past last range */
  262. }
  263. /*
  264. * UniStrupr: Upper case a unicode string
  265. */
  266. static inline wchar_t *
  267. UniStrupr(register wchar_t * upin)
  268. {
  269. register wchar_t *up;
  270. up = upin;
  271. while (*up) { /* For all characters */
  272. *up = UniToupper(*up);
  273. up++;
  274. }
  275. return upin; /* Return input pointer */
  276. }
  277. #endif /* UNIUPR_NOUPPER */
  278. #ifndef UNIUPR_NOLOWER
  279. /*
  280. * UniTolower: Convert a unicode character to lower case
  281. */
  282. static inline wchar_t
  283. UniTolower(wchar_t uc)
  284. {
  285. register struct UniCaseRange *rp;
  286. if (uc < sizeof (UniLowerTable)) { /* Latin characters */
  287. return uc + UniLowerTable[uc]; /* Use base tables */
  288. } else {
  289. rp = UniLowerRange; /* Use range tables */
  290. while (rp->start) {
  291. if (uc < rp->start) /* Before start of range */
  292. return uc; /* Uppercase = input */
  293. if (uc <= rp->end) /* In range */
  294. return uc + rp->table[uc - rp->start];
  295. rp++; /* Try next range */
  296. }
  297. }
  298. return uc; /* Past last range */
  299. }
  300. /*
  301. * UniStrlwr: Lower case a unicode string
  302. */
  303. static inline wchar_t *
  304. UniStrlwr(register wchar_t * upin)
  305. {
  306. register wchar_t *up;
  307. up = upin;
  308. while (*up) { /* For all characters */
  309. *up = UniTolower(*up);
  310. up++;
  311. }
  312. return upin; /* Return input pointer */
  313. }
  314. #endif