nls_ucs2_utils.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Some of the source code in this file came from fs/cifs/cifs_unicode.c
  4. * and then via server/unicode.c
  5. * cifs_unicode: Unicode kernel case support
  6. *
  7. * Function:
  8. * Convert a unicode character to upper or lower case using
  9. * compressed tables.
  10. *
  11. * Copyright (c) International Business Machines Corp., 2000,2009
  12. *
  13. *
  14. * Notes:
  15. * These APIs are based on the C library functions. The semantics
  16. * should match the C functions but with expanded size operands.
  17. *
  18. * The upper/lower functions are based on a table created by mkupr.
  19. * This is a compressed table of upper and lower case conversion.
  20. *
  21. */
  22. #ifndef _NLS_UCS2_UTILS_H
  23. #define _NLS_UCS2_UTILS_H
  24. #include <asm/byteorder.h>
  25. #include <linux/types.h>
  26. #include <linux/nls.h>
  27. #include <linux/unicode.h>
  28. #include "nls_ucs2_data.h"
  29. /*
  30. * Windows maps these to the user defined 16 bit Unicode range since they are
  31. * reserved symbols (along with \ and /), otherwise illegal to store
  32. * in filenames in NTFS
  33. */
  34. #define UNI_ASTERISK ((__u16)('*' + 0xF000))
  35. #define UNI_QUESTION ((__u16)('?' + 0xF000))
  36. #define UNI_COLON ((__u16)(':' + 0xF000))
  37. #define UNI_GRTRTHAN ((__u16)('>' + 0xF000))
  38. #define UNI_LESSTHAN ((__u16)('<' + 0xF000))
  39. #define UNI_PIPE ((__u16)('|' + 0xF000))
  40. #define UNI_SLASH ((__u16)('\\' + 0xF000))
  41. /*
  42. * UniStrcat: Concatenate the second string to the first
  43. *
  44. * Returns:
  45. * Address of the first string
  46. */
  47. static inline wchar_t *UniStrcat(wchar_t *ucs1, const wchar_t *ucs2)
  48. {
  49. wchar_t *anchor = ucs1; /* save a pointer to start of ucs1 */
  50. while (*ucs1++)
  51. /*NULL*/; /* To end of first string */
  52. ucs1--; /* Return to the null */
  53. while ((*ucs1++ = *ucs2++))
  54. /*NULL*/; /* copy string 2 over */
  55. return anchor;
  56. }
  57. /*
  58. * UniStrchr: Find a character in a string
  59. *
  60. * Returns:
  61. * Address of first occurrence of character in string
  62. * or NULL if the character is not in the string
  63. */
  64. static inline wchar_t *UniStrchr(const wchar_t *ucs, wchar_t uc)
  65. {
  66. while ((*ucs != uc) && *ucs)
  67. ucs++;
  68. if (*ucs == uc)
  69. return (wchar_t *)ucs;
  70. return NULL;
  71. }
  72. /*
  73. * UniStrcmp: Compare two strings
  74. *
  75. * Returns:
  76. * < 0: First string is less than second
  77. * = 0: Strings are equal
  78. * > 0: First string is greater than second
  79. */
  80. static inline int UniStrcmp(const wchar_t *ucs1, const wchar_t *ucs2)
  81. {
  82. while ((*ucs1 == *ucs2) && *ucs1) {
  83. ucs1++;
  84. ucs2++;
  85. }
  86. return (int)*ucs1 - (int)*ucs2;
  87. }
  88. /*
  89. * UniStrcpy: Copy a string
  90. */
  91. static inline wchar_t *UniStrcpy(wchar_t *ucs1, const wchar_t *ucs2)
  92. {
  93. wchar_t *anchor = ucs1; /* save the start of result string */
  94. while ((*ucs1++ = *ucs2++))
  95. /*NULL*/;
  96. return anchor;
  97. }
  98. /*
  99. * UniStrlen: Return the length of a string (in 16 bit Unicode chars not bytes)
  100. */
  101. static inline size_t UniStrlen(const wchar_t *ucs1)
  102. {
  103. int i = 0;
  104. while (*ucs1++)
  105. i++;
  106. return i;
  107. }
  108. /*
  109. * UniStrnlen: Return the length (in 16 bit Unicode chars not bytes) of a
  110. * string (length limited)
  111. */
  112. static inline size_t UniStrnlen(const wchar_t *ucs1, int maxlen)
  113. {
  114. int i = 0;
  115. while (*ucs1++) {
  116. i++;
  117. if (i >= maxlen)
  118. break;
  119. }
  120. return i;
  121. }
  122. /*
  123. * UniStrncat: Concatenate length limited string
  124. */
  125. static inline wchar_t *UniStrncat(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
  126. {
  127. wchar_t *anchor = ucs1; /* save pointer to string 1 */
  128. while (*ucs1++)
  129. /*NULL*/;
  130. ucs1--; /* point to null terminator of s1 */
  131. while (n-- && (*ucs1 = *ucs2)) { /* copy s2 after s1 */
  132. ucs1++;
  133. ucs2++;
  134. }
  135. *ucs1 = 0; /* Null terminate the result */
  136. return anchor;
  137. }
  138. /*
  139. * UniStrncmp: Compare length limited string
  140. */
  141. static inline int UniStrncmp(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
  142. {
  143. if (!n)
  144. return 0; /* Null strings are equal */
  145. while ((*ucs1 == *ucs2) && *ucs1 && --n) {
  146. ucs1++;
  147. ucs2++;
  148. }
  149. return (int)*ucs1 - (int)*ucs2;
  150. }
  151. /*
  152. * UniStrncmp_le: Compare length limited string - native to little-endian
  153. */
  154. static inline int
  155. UniStrncmp_le(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
  156. {
  157. if (!n)
  158. return 0; /* Null strings are equal */
  159. while ((*ucs1 == __le16_to_cpu(*ucs2)) && *ucs1 && --n) {
  160. ucs1++;
  161. ucs2++;
  162. }
  163. return (int)*ucs1 - (int)__le16_to_cpu(*ucs2);
  164. }
  165. /*
  166. * UniStrncpy: Copy length limited string with pad
  167. */
  168. static inline wchar_t *UniStrncpy(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
  169. {
  170. wchar_t *anchor = ucs1;
  171. while (n-- && *ucs2) /* Copy the strings */
  172. *ucs1++ = *ucs2++;
  173. n++;
  174. while (n--) /* Pad with nulls */
  175. *ucs1++ = 0;
  176. return anchor;
  177. }
  178. /*
  179. * UniStrncpy_le: Copy length limited string with pad to little-endian
  180. */
  181. static inline wchar_t *UniStrncpy_le(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
  182. {
  183. wchar_t *anchor = ucs1;
  184. while (n-- && *ucs2) /* Copy the strings */
  185. *ucs1++ = __le16_to_cpu(*ucs2++);
  186. n++;
  187. while (n--) /* Pad with nulls */
  188. *ucs1++ = 0;
  189. return anchor;
  190. }
  191. /*
  192. * UniStrstr: Find a string in a string
  193. *
  194. * Returns:
  195. * Address of first match found
  196. * NULL if no matching string is found
  197. */
  198. static inline wchar_t *UniStrstr(const wchar_t *ucs1, const wchar_t *ucs2)
  199. {
  200. const wchar_t *anchor1 = ucs1;
  201. const wchar_t *anchor2 = ucs2;
  202. while (*ucs1) {
  203. if (*ucs1 == *ucs2) {
  204. /* Partial match found */
  205. ucs1++;
  206. ucs2++;
  207. } else {
  208. if (!*ucs2) /* Match found */
  209. return (wchar_t *)anchor1;
  210. ucs1 = ++anchor1; /* No match */
  211. ucs2 = anchor2;
  212. }
  213. }
  214. if (!*ucs2) /* Both end together */
  215. return (wchar_t *)anchor1; /* Match found */
  216. return NULL; /* No match */
  217. }
  218. #ifndef UNIUPR_NOUPPER
  219. /*
  220. * UniToupper: Convert a unicode character to upper case
  221. */
  222. static inline wchar_t UniToupper(register wchar_t uc)
  223. {
  224. register const struct UniCaseRange *rp;
  225. if (uc < sizeof(NlsUniUpperTable)) {
  226. /* Latin characters */
  227. return uc + NlsUniUpperTable[uc]; /* Use base tables */
  228. }
  229. rp = NlsUniUpperRange; /* Use range tables */
  230. while (rp->start) {
  231. if (uc < rp->start) /* Before start of range */
  232. return uc; /* Uppercase = input */
  233. if (uc <= rp->end) /* In range */
  234. return uc + rp->table[uc - rp->start];
  235. rp++; /* Try next range */
  236. }
  237. return uc; /* Past last range */
  238. }
  239. /*
  240. * UniStrupr: Upper case a unicode string
  241. */
  242. static inline __le16 *UniStrupr(register __le16 *upin)
  243. {
  244. register __le16 *up;
  245. up = upin;
  246. while (*up) { /* For all characters */
  247. *up = cpu_to_le16(UniToupper(le16_to_cpu(*up)));
  248. up++;
  249. }
  250. return upin; /* Return input pointer */
  251. }
  252. #endif /* UNIUPR_NOUPPER */
  253. #endif /* _NLS_UCS2_UTILS_H */