jfs_unicode.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 "jfs_types.h"
  11. typedef struct {
  12. wchar_t start;
  13. wchar_t end;
  14. signed char *table;
  15. } UNICASERANGE;
  16. extern signed char UniUpperTable[512];
  17. extern UNICASERANGE UniUpperRange[];
  18. extern int get_UCSname(struct component_name *, struct dentry *);
  19. extern int jfs_strfromUCS_le(char *, const __le16 *, int, struct nls_table *);
  20. #define free_UCSname(COMP) kfree((COMP)->name)
  21. /*
  22. * UniStrcpy: Copy a string
  23. */
  24. static inline wchar_t *UniStrcpy(wchar_t * ucs1, const wchar_t * ucs2)
  25. {
  26. wchar_t *anchor = ucs1; /* save the start of result string */
  27. while ((*ucs1++ = *ucs2++));
  28. return anchor;
  29. }
  30. /*
  31. * UniStrncpy: Copy length limited string with pad
  32. */
  33. static inline __le16 *UniStrncpy_le(__le16 * ucs1, const __le16 * ucs2,
  34. size_t n)
  35. {
  36. __le16 *anchor = ucs1;
  37. while (n-- && *ucs2) /* Copy the strings */
  38. *ucs1++ = *ucs2++;
  39. n++;
  40. while (n--) /* Pad with nulls */
  41. *ucs1++ = 0;
  42. return anchor;
  43. }
  44. /*
  45. * UniStrncmp_le: Compare length limited string - native to little-endian
  46. */
  47. static inline int UniStrncmp_le(const wchar_t * ucs1, const __le16 * ucs2,
  48. size_t n)
  49. {
  50. if (!n)
  51. return 0; /* Null strings are equal */
  52. while ((*ucs1 == __le16_to_cpu(*ucs2)) && *ucs1 && --n) {
  53. ucs1++;
  54. ucs2++;
  55. }
  56. return (int) *ucs1 - (int) __le16_to_cpu(*ucs2);
  57. }
  58. /*
  59. * UniStrncpy_to_le: Copy length limited string with pad to little-endian
  60. */
  61. static inline __le16 *UniStrncpy_to_le(__le16 * ucs1, const wchar_t * ucs2,
  62. size_t n)
  63. {
  64. __le16 *anchor = ucs1;
  65. while (n-- && *ucs2) /* Copy the strings */
  66. *ucs1++ = cpu_to_le16(*ucs2++);
  67. n++;
  68. while (n--) /* Pad with nulls */
  69. *ucs1++ = 0;
  70. return anchor;
  71. }
  72. /*
  73. * UniStrncpy_from_le: Copy length limited string with pad from little-endian
  74. */
  75. static inline wchar_t *UniStrncpy_from_le(wchar_t * ucs1, const __le16 * ucs2,
  76. size_t n)
  77. {
  78. wchar_t *anchor = ucs1;
  79. while (n-- && *ucs2) /* Copy the strings */
  80. *ucs1++ = __le16_to_cpu(*ucs2++);
  81. n++;
  82. while (n--) /* Pad with nulls */
  83. *ucs1++ = 0;
  84. return anchor;
  85. }
  86. /*
  87. * UniToupper: Convert a unicode character to upper case
  88. */
  89. static inline wchar_t UniToupper(wchar_t uc)
  90. {
  91. UNICASERANGE *rp;
  92. if (uc < sizeof(UniUpperTable)) { /* Latin characters */
  93. return uc + UniUpperTable[uc]; /* Use base tables */
  94. } else {
  95. rp = UniUpperRange; /* Use range tables */
  96. while (rp->start) {
  97. if (uc < rp->start) /* Before start of range */
  98. return uc; /* Uppercase = input */
  99. if (uc <= rp->end) /* In range */
  100. return uc + rp->table[uc - rp->start];
  101. rp++; /* Try next range */
  102. }
  103. }
  104. return uc; /* Past last range */
  105. }
  106. /*
  107. * UniStrupr: Upper case a unicode string
  108. */
  109. static inline wchar_t *UniStrupr(wchar_t * upin)
  110. {
  111. wchar_t *up;
  112. up = upin;
  113. while (*up) { /* For all characters */
  114. *up = UniToupper(*up);
  115. up++;
  116. }
  117. return upin; /* Return input pointer */
  118. }
  119. #endif /* !_H_JFS_UNICODE */