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