jfs_unicode.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Copyright (C) International Business Machines Corp., 2000-2002
  4. * Portions Copyright (C) Christoph Hellwig, 2001-2002
  5. */
  6. #ifndef _H_JFS_UNICODE
  7. #define _H_JFS_UNICODE
  8. #include <linux/slab.h>
  9. #include <asm/byteorder.h>
  10. #include "../nls/nls_ucs2_data.h"
  11. #include "jfs_types.h"
  12. extern int get_UCSname(struct component_name *, struct dentry *);
  13. extern int jfs_strfromUCS_le(char *, const __le16 *, int, struct nls_table *);
  14. #define free_UCSname(COMP) kfree((COMP)->name)
  15. /*
  16. * UniStrcpy: Copy a string
  17. */
  18. static inline wchar_t *UniStrcpy(wchar_t * ucs1, const wchar_t * ucs2)
  19. {
  20. wchar_t *anchor = ucs1; /* save the start of result string */
  21. while ((*ucs1++ = *ucs2++));
  22. return anchor;
  23. }
  24. /*
  25. * UniStrncpy: Copy length limited string with pad
  26. */
  27. static inline __le16 *UniStrncpy_le(__le16 * ucs1, const __le16 * ucs2,
  28. size_t n)
  29. {
  30. __le16 *anchor = ucs1;
  31. while (n-- && *ucs2) /* Copy the strings */
  32. *ucs1++ = *ucs2++;
  33. n++;
  34. while (n--) /* Pad with nulls */
  35. *ucs1++ = 0;
  36. return anchor;
  37. }
  38. /*
  39. * UniStrncmp_le: Compare length limited string - native to little-endian
  40. */
  41. static inline int UniStrncmp_le(const wchar_t * ucs1, const __le16 * ucs2,
  42. size_t n)
  43. {
  44. if (!n)
  45. return 0; /* Null strings are equal */
  46. while ((*ucs1 == __le16_to_cpu(*ucs2)) && *ucs1 && --n) {
  47. ucs1++;
  48. ucs2++;
  49. }
  50. return (int) *ucs1 - (int) __le16_to_cpu(*ucs2);
  51. }
  52. /*
  53. * UniStrncpy_to_le: Copy length limited string with pad to little-endian
  54. */
  55. static inline __le16 *UniStrncpy_to_le(__le16 * ucs1, const wchar_t * ucs2,
  56. size_t n)
  57. {
  58. __le16 *anchor = ucs1;
  59. while (n-- && *ucs2) /* Copy the strings */
  60. *ucs1++ = cpu_to_le16(*ucs2++);
  61. n++;
  62. while (n--) /* Pad with nulls */
  63. *ucs1++ = 0;
  64. return anchor;
  65. }
  66. /*
  67. * UniStrncpy_from_le: Copy length limited string with pad from little-endian
  68. */
  69. static inline wchar_t *UniStrncpy_from_le(wchar_t * ucs1, const __le16 * ucs2,
  70. size_t n)
  71. {
  72. wchar_t *anchor = ucs1;
  73. while (n-- && *ucs2) /* Copy the strings */
  74. *ucs1++ = __le16_to_cpu(*ucs2++);
  75. n++;
  76. while (n--) /* Pad with nulls */
  77. *ucs1++ = 0;
  78. return anchor;
  79. }
  80. /*
  81. * UniToupper: Convert a unicode character to upper case
  82. */
  83. static inline wchar_t UniToupper(wchar_t uc)
  84. {
  85. const struct UniCaseRange *rp;
  86. if (uc < sizeof(NlsUniUpperTable)) { /* Latin characters */
  87. return uc + NlsUniUpperTable[uc]; /* Use base tables */
  88. } else {
  89. rp = NlsUniUpperRange; /* Use range tables */
  90. while (rp->start) {
  91. if (uc < rp->start) /* Before start of range */
  92. return uc; /* Uppercase = input */
  93. if (uc <= rp->end) /* In range */
  94. return uc + rp->table[uc - rp->start];
  95. rp++; /* Try next range */
  96. }
  97. }
  98. return uc; /* Past last range */
  99. }
  100. /*
  101. * UniStrupr: Upper case a unicode string
  102. */
  103. static inline wchar_t *UniStrupr(wchar_t * upin)
  104. {
  105. wchar_t *up;
  106. up = upin;
  107. while (*up) { /* For all characters */
  108. *up = UniToupper(*up);
  109. up++;
  110. }
  111. return upin; /* Return input pointer */
  112. }
  113. #endif /* !_H_JFS_UNICODE */