blob: ae8d2491133c9f19b88599999cf14bf0d66fe1c9 [file] [log] [blame]
Matthew Garrett0635eb82013-04-15 13:09:45 -07001#include <linux/ucs2_string.h>
2#include <linux/module.h>
3
4/* Return the number of unicode characters in data */
5unsigned long
6ucs2_strnlen(const ucs2_char_t *s, size_t maxlength)
7{
8 unsigned long length = 0;
9
10 while (*s++ != 0 && length < maxlength)
11 length++;
12 return length;
13}
14EXPORT_SYMBOL(ucs2_strnlen);
15
16unsigned long
17ucs2_strlen(const ucs2_char_t *s)
18{
19 return ucs2_strnlen(s, ~0UL);
20}
21EXPORT_SYMBOL(ucs2_strlen);
22
23/*
24 * Return the number of bytes is the length of this string
25 * Note: this is NOT the same as the number of unicode characters
26 */
27unsigned long
28ucs2_strsize(const ucs2_char_t *data, unsigned long maxlength)
29{
30 return ucs2_strnlen(data, maxlength/sizeof(ucs2_char_t)) * sizeof(ucs2_char_t);
31}
32EXPORT_SYMBOL(ucs2_strsize);
33
34int
35ucs2_strncmp(const ucs2_char_t *a, const ucs2_char_t *b, size_t len)
36{
37 while (1) {
38 if (len == 0)
39 return 0;
40 if (*a < *b)
41 return -1;
42 if (*a > *b)
43 return 1;
44 if (*a == 0) /* implies *b == 0 */
45 return 0;
46 a++;
47 b++;
48 len--;
49 }
50}
51EXPORT_SYMBOL(ucs2_strncmp);
Peter Jones73500262016-02-08 14:48:11 -050052
53unsigned long
54ucs2_utf8size(const ucs2_char_t *src)
55{
56 unsigned long i;
57 unsigned long j = 0;
58
Lukas Wunnercf289ce2016-08-03 10:16:02 +020059 for (i = 0; src[i]; i++) {
Peter Jones73500262016-02-08 14:48:11 -050060 u16 c = src[i];
61
Jason Andryuka6807592016-02-12 23:13:33 +000062 if (c >= 0x800)
Peter Jones73500262016-02-08 14:48:11 -050063 j += 3;
Jason Andryuka6807592016-02-12 23:13:33 +000064 else if (c >= 0x80)
Peter Jones73500262016-02-08 14:48:11 -050065 j += 2;
66 else
67 j += 1;
68 }
69
70 return j;
71}
72EXPORT_SYMBOL(ucs2_utf8size);
73
74/*
75 * copy at most maxlength bytes of whole utf8 characters to dest from the
76 * ucs2 string src.
77 *
78 * The return value is the number of characters copied, not including the
79 * final NUL character.
80 */
81unsigned long
82ucs2_as_utf8(u8 *dest, const ucs2_char_t *src, unsigned long maxlength)
83{
84 unsigned int i;
85 unsigned long j = 0;
86 unsigned long limit = ucs2_strnlen(src, maxlength);
87
88 for (i = 0; maxlength && i < limit; i++) {
89 u16 c = src[i];
90
Jason Andryuka6807592016-02-12 23:13:33 +000091 if (c >= 0x800) {
Peter Jones73500262016-02-08 14:48:11 -050092 if (maxlength < 3)
93 break;
94 maxlength -= 3;
95 dest[j++] = 0xe0 | (c & 0xf000) >> 12;
Jason Andryuka6807592016-02-12 23:13:33 +000096 dest[j++] = 0x80 | (c & 0x0fc0) >> 6;
Peter Jones73500262016-02-08 14:48:11 -050097 dest[j++] = 0x80 | (c & 0x003f);
Jason Andryuka6807592016-02-12 23:13:33 +000098 } else if (c >= 0x80) {
Peter Jones73500262016-02-08 14:48:11 -050099 if (maxlength < 2)
100 break;
101 maxlength -= 2;
Jason Andryuka6807592016-02-12 23:13:33 +0000102 dest[j++] = 0xc0 | (c & 0x7c0) >> 6;
103 dest[j++] = 0x80 | (c & 0x03f);
Peter Jones73500262016-02-08 14:48:11 -0500104 } else {
105 maxlength -= 1;
106 dest[j++] = c & 0x7f;
107 }
108 }
109 if (maxlength)
110 dest[j] = '\0';
111 return j;
112}
113EXPORT_SYMBOL(ucs2_as_utf8);