ucs2_string.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/ucs2_string.h>
  3. #include <linux/module.h>
  4. /* Return the number of unicode characters in data */
  5. unsigned long
  6. ucs2_strnlen(const ucs2_char_t *s, size_t maxlength)
  7. {
  8. unsigned long length = 0;
  9. while (*s++ != 0 && length < maxlength)
  10. length++;
  11. return length;
  12. }
  13. EXPORT_SYMBOL(ucs2_strnlen);
  14. unsigned long
  15. ucs2_strlen(const ucs2_char_t *s)
  16. {
  17. return ucs2_strnlen(s, ~0UL);
  18. }
  19. EXPORT_SYMBOL(ucs2_strlen);
  20. /*
  21. * Return the number of bytes is the length of this string
  22. * Note: this is NOT the same as the number of unicode characters
  23. */
  24. unsigned long
  25. ucs2_strsize(const ucs2_char_t *data, unsigned long maxlength)
  26. {
  27. return ucs2_strnlen(data, maxlength/sizeof(ucs2_char_t)) * sizeof(ucs2_char_t);
  28. }
  29. EXPORT_SYMBOL(ucs2_strsize);
  30. int
  31. ucs2_strncmp(const ucs2_char_t *a, const ucs2_char_t *b, size_t len)
  32. {
  33. while (1) {
  34. if (len == 0)
  35. return 0;
  36. if (*a < *b)
  37. return -1;
  38. if (*a > *b)
  39. return 1;
  40. if (*a == 0) /* implies *b == 0 */
  41. return 0;
  42. a++;
  43. b++;
  44. len--;
  45. }
  46. }
  47. EXPORT_SYMBOL(ucs2_strncmp);
  48. unsigned long
  49. ucs2_utf8size(const ucs2_char_t *src)
  50. {
  51. unsigned long i;
  52. unsigned long j = 0;
  53. for (i = 0; src[i]; i++) {
  54. u16 c = src[i];
  55. if (c >= 0x800)
  56. j += 3;
  57. else if (c >= 0x80)
  58. j += 2;
  59. else
  60. j += 1;
  61. }
  62. return j;
  63. }
  64. EXPORT_SYMBOL(ucs2_utf8size);
  65. /*
  66. * copy at most maxlength bytes of whole utf8 characters to dest from the
  67. * ucs2 string src.
  68. *
  69. * The return value is the number of characters copied, not including the
  70. * final NUL character.
  71. */
  72. unsigned long
  73. ucs2_as_utf8(u8 *dest, const ucs2_char_t *src, unsigned long maxlength)
  74. {
  75. unsigned int i;
  76. unsigned long j = 0;
  77. unsigned long limit = ucs2_strnlen(src, maxlength);
  78. for (i = 0; maxlength && i < limit; i++) {
  79. u16 c = src[i];
  80. if (c >= 0x800) {
  81. if (maxlength < 3)
  82. break;
  83. maxlength -= 3;
  84. dest[j++] = 0xe0 | (c & 0xf000) >> 12;
  85. dest[j++] = 0x80 | (c & 0x0fc0) >> 6;
  86. dest[j++] = 0x80 | (c & 0x003f);
  87. } else if (c >= 0x80) {
  88. if (maxlength < 2)
  89. break;
  90. maxlength -= 2;
  91. dest[j++] = 0xc0 | (c & 0x7c0) >> 6;
  92. dest[j++] = 0x80 | (c & 0x03f);
  93. } else {
  94. maxlength -= 1;
  95. dest[j++] = c & 0x7f;
  96. }
  97. }
  98. if (maxlength)
  99. dest[j] = '\0';
  100. return j;
  101. }
  102. EXPORT_SYMBOL(ucs2_as_utf8);
  103. MODULE_LICENSE("GPL v2");