PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/fs/cifs/cifs_unicode.h

https://gitlab.com/LiquidSmooth-Devices/android_kernel_htc_msm8974
C Header | 310 lines | 233 code | 45 blank | 32 comment | 51 complexity | 1b2c06622ba8b7ce9c06d674dfbfe8bf MD5 | raw file
Possible License(s): GPL-2.0
  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
  39. #define UNI_ASTERISK (__u16) ('*' + 0xF000)
  40. #define UNI_QUESTION (__u16) ('?' + 0xF000)
  41. #define UNI_COLON (__u16) (':' + 0xF000)
  42. #define UNI_GRTRTHAN (__u16) ('>' + 0xF000)
  43. #define UNI_LESSTHAN (__u16) ('<' + 0xF000)
  44. #define UNI_PIPE (__u16) ('|' + 0xF000)
  45. #define UNI_SLASH (__u16) ('\\' + 0xF000)
  46. #ifndef UNICASERANGE_DEFINED
  47. struct UniCaseRange {
  48. wchar_t start;
  49. wchar_t end;
  50. signed char *table;
  51. };
  52. #endif
  53. #ifndef UNIUPR_NOUPPER
  54. extern signed char CifsUniUpperTable[512];
  55. extern const struct UniCaseRange CifsUniUpperRange[];
  56. #endif
  57. #ifndef UNIUPR_NOLOWER
  58. extern signed char CifsUniLowerTable[512];
  59. extern const struct UniCaseRange CifsUniLowerRange[];
  60. #endif
  61. #ifdef __KERNEL__
  62. int cifs_from_utf16(char *to, const __le16 *from, int tolen, int fromlen,
  63. const struct nls_table *codepage, bool mapchar);
  64. int cifs_utf16_bytes(const __le16 *from, int maxbytes,
  65. const struct nls_table *codepage);
  66. int cifs_strtoUTF16(__le16 *, const char *, int, const struct nls_table *);
  67. char *cifs_strndup_from_utf16(const char *src, const int maxlen,
  68. const bool is_unicode,
  69. const struct nls_table *codepage);
  70. extern int cifsConvertToUTF16(__le16 *target, const char *source, int maxlen,
  71. const struct nls_table *cp, int mapChars);
  72. #endif
  73. static inline wchar_t *
  74. UniStrcat(wchar_t *ucs1, const wchar_t *ucs2)
  75. {
  76. wchar_t *anchor = ucs1;
  77. while (*ucs1++) ;
  78. ucs1--;
  79. while ((*ucs1++ = *ucs2++)) ;
  80. return anchor;
  81. }
  82. static inline wchar_t *
  83. UniStrchr(const wchar_t *ucs, wchar_t uc)
  84. {
  85. while ((*ucs != uc) && *ucs)
  86. ucs++;
  87. if (*ucs == uc)
  88. return (wchar_t *) ucs;
  89. return NULL;
  90. }
  91. static inline int
  92. UniStrcmp(const wchar_t *ucs1, const wchar_t *ucs2)
  93. {
  94. while ((*ucs1 == *ucs2) && *ucs1) {
  95. ucs1++;
  96. ucs2++;
  97. }
  98. return (int) *ucs1 - (int) *ucs2;
  99. }
  100. static inline wchar_t *
  101. UniStrcpy(wchar_t *ucs1, const wchar_t *ucs2)
  102. {
  103. wchar_t *anchor = ucs1;
  104. while ((*ucs1++ = *ucs2++)) ;
  105. return anchor;
  106. }
  107. static inline size_t
  108. UniStrlen(const wchar_t *ucs1)
  109. {
  110. int i = 0;
  111. while (*ucs1++)
  112. i++;
  113. return i;
  114. }
  115. static inline size_t
  116. UniStrnlen(const wchar_t *ucs1, int maxlen)
  117. {
  118. int i = 0;
  119. while (*ucs1++) {
  120. i++;
  121. if (i >= maxlen)
  122. break;
  123. }
  124. return i;
  125. }
  126. static inline wchar_t *
  127. UniStrncat(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
  128. {
  129. wchar_t *anchor = ucs1;
  130. while (*ucs1++) ;
  131. ucs1--;
  132. while (n-- && (*ucs1 = *ucs2)) {
  133. ucs1++;
  134. ucs2++;
  135. }
  136. *ucs1 = 0;
  137. return (anchor);
  138. }
  139. static inline int
  140. UniStrncmp(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
  141. {
  142. if (!n)
  143. return 0;
  144. while ((*ucs1 == *ucs2) && *ucs1 && --n) {
  145. ucs1++;
  146. ucs2++;
  147. }
  148. return (int) *ucs1 - (int) *ucs2;
  149. }
  150. static inline int
  151. UniStrncmp_le(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
  152. {
  153. if (!n)
  154. return 0;
  155. while ((*ucs1 == __le16_to_cpu(*ucs2)) && *ucs1 && --n) {
  156. ucs1++;
  157. ucs2++;
  158. }
  159. return (int) *ucs1 - (int) __le16_to_cpu(*ucs2);
  160. }
  161. static inline wchar_t *
  162. UniStrncpy(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
  163. {
  164. wchar_t *anchor = ucs1;
  165. while (n-- && *ucs2)
  166. *ucs1++ = *ucs2++;
  167. n++;
  168. while (n--)
  169. *ucs1++ = 0;
  170. return anchor;
  171. }
  172. static inline wchar_t *
  173. UniStrncpy_le(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
  174. {
  175. wchar_t *anchor = ucs1;
  176. while (n-- && *ucs2)
  177. *ucs1++ = __le16_to_cpu(*ucs2++);
  178. n++;
  179. while (n--)
  180. *ucs1++ = 0;
  181. return anchor;
  182. }
  183. static inline wchar_t *
  184. UniStrstr(const wchar_t *ucs1, const wchar_t *ucs2)
  185. {
  186. const wchar_t *anchor1 = ucs1;
  187. const wchar_t *anchor2 = ucs2;
  188. while (*ucs1) {
  189. if (*ucs1 == *ucs2) {
  190. ucs1++;
  191. ucs2++;
  192. } else {
  193. if (!*ucs2)
  194. return (wchar_t *) anchor1;
  195. ucs1 = ++anchor1;
  196. ucs2 = anchor2;
  197. }
  198. }
  199. if (!*ucs2)
  200. return (wchar_t *) anchor1;
  201. return NULL;
  202. }
  203. #ifndef UNIUPR_NOUPPER
  204. static inline wchar_t
  205. UniToupper(register wchar_t uc)
  206. {
  207. register const struct UniCaseRange *rp;
  208. if (uc < sizeof(CifsUniUpperTable)) {
  209. return uc + CifsUniUpperTable[uc];
  210. } else {
  211. rp = CifsUniUpperRange;
  212. while (rp->start) {
  213. if (uc < rp->start)
  214. return uc;
  215. if (uc <= rp->end)
  216. return uc + rp->table[uc - rp->start];
  217. rp++;
  218. }
  219. }
  220. return uc;
  221. }
  222. static inline wchar_t *
  223. UniStrupr(register wchar_t *upin)
  224. {
  225. register wchar_t *up;
  226. up = upin;
  227. while (*up) {
  228. *up = UniToupper(*up);
  229. up++;
  230. }
  231. return upin;
  232. }
  233. #endif
  234. #ifndef UNIUPR_NOLOWER
  235. static inline wchar_t
  236. UniTolower(register wchar_t uc)
  237. {
  238. register const struct UniCaseRange *rp;
  239. if (uc < sizeof(CifsUniLowerTable)) {
  240. return uc + CifsUniLowerTable[uc];
  241. } else {
  242. rp = CifsUniLowerRange;
  243. while (rp->start) {
  244. if (uc < rp->start)
  245. return uc;
  246. if (uc <= rp->end)
  247. return uc + rp->table[uc - rp->start];
  248. rp++;
  249. }
  250. }
  251. return uc;
  252. }
  253. static inline wchar_t *
  254. UniStrlwr(register wchar_t *upin)
  255. {
  256. register wchar_t *up;
  257. up = upin;
  258. while (*up) {
  259. *up = UniTolower(*up);
  260. up++;
  261. }
  262. return upin;
  263. }
  264. #endif
  265. #endif