/src/barebox/lib/ucs2_string.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0 |
2 | | // SPDX-Comment: Origin-URL: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/lib/ucs2_string.c?id=91640531b92ec63e260b9d5c681d387676ec462c |
3 | | |
4 | | #include <linux/ucs2_string.h> |
5 | | #include <linux/module.h> |
6 | | #include <linux/bug.h> |
7 | | #include <linux/errno.h> |
8 | | #include <linux/limits.h> |
9 | | |
10 | | /* Return the number of unicode characters in data */ |
11 | | unsigned long |
12 | | ucs2_strnlen(const ucs2_char_t *s, size_t maxlength) |
13 | 0 | { |
14 | 0 | unsigned long length = 0; |
15 | |
|
16 | 0 | while (*s++ != 0 && length < maxlength) |
17 | 0 | length++; |
18 | 0 | return length; |
19 | 0 | } |
20 | | EXPORT_SYMBOL(ucs2_strnlen); |
21 | | |
22 | | unsigned long |
23 | | ucs2_strlen(const ucs2_char_t *s) |
24 | 0 | { |
25 | 0 | return ucs2_strnlen(s, ~0UL); |
26 | 0 | } |
27 | | EXPORT_SYMBOL(ucs2_strlen); |
28 | | |
29 | | /* |
30 | | * Return the number of bytes is the length of this string |
31 | | * Note: this is NOT the same as the number of unicode characters |
32 | | */ |
33 | | unsigned long |
34 | | ucs2_strsize(const ucs2_char_t *data, unsigned long maxlength) |
35 | 0 | { |
36 | 0 | return ucs2_strnlen(data, maxlength/sizeof(ucs2_char_t)) * sizeof(ucs2_char_t); |
37 | 0 | } |
38 | | EXPORT_SYMBOL(ucs2_strsize); |
39 | | |
40 | | /** |
41 | | * ucs2_strscpy() - Copy a UCS2 string into a sized buffer. |
42 | | * |
43 | | * @dst: Pointer to the destination buffer where to copy the string to. |
44 | | * @src: Pointer to the source buffer where to copy the string from. |
45 | | * @count: Size of the destination buffer, in UCS2 (16-bit) characters. |
46 | | * |
47 | | * Like strscpy(), only for UCS2 strings. |
48 | | * |
49 | | * Copy the source string @src, or as much of it as fits, into the destination |
50 | | * buffer @dst. The behavior is undefined if the string buffers overlap. The |
51 | | * destination buffer @dst is always NUL-terminated, unless it's zero-sized. |
52 | | * |
53 | | * Return: The number of characters copied into @dst (excluding the trailing |
54 | | * %NUL terminator) or -E2BIG if @count is 0 or @src was truncated due to the |
55 | | * destination buffer being too small. |
56 | | */ |
57 | | ssize_t ucs2_strscpy(ucs2_char_t *dst, const ucs2_char_t *src, size_t count) |
58 | 0 | { |
59 | 0 | long res; |
60 | | |
61 | | /* |
62 | | * Ensure that we have a valid amount of space. We need to store at |
63 | | * least one NUL-character. |
64 | | */ |
65 | 0 | if (count == 0 || WARN_ON_ONCE(count > INT_MAX / sizeof(*dst))) |
66 | 0 | return -E2BIG; |
67 | | |
68 | | /* |
69 | | * Copy at most 'count' characters, return early if we find a |
70 | | * NUL-terminator. |
71 | | */ |
72 | 0 | for (res = 0; res < count; res++) { |
73 | 0 | ucs2_char_t c; |
74 | |
|
75 | 0 | c = src[res]; |
76 | 0 | dst[res] = c; |
77 | |
|
78 | 0 | if (!c) |
79 | 0 | return res; |
80 | 0 | } |
81 | | |
82 | | /* |
83 | | * The loop above terminated without finding a NUL-terminator, |
84 | | * exceeding the 'count': Enforce proper NUL-termination and return |
85 | | * error. |
86 | | */ |
87 | 0 | dst[count - 1] = 0; |
88 | 0 | return -E2BIG; |
89 | 0 | } |
90 | | EXPORT_SYMBOL(ucs2_strscpy); |
91 | | |
92 | | int |
93 | | ucs2_strncmp(const ucs2_char_t *a, const ucs2_char_t *b, size_t len) |
94 | 0 | { |
95 | 0 | while (1) { |
96 | 0 | if (len == 0) |
97 | 0 | return 0; |
98 | 0 | if (*a < *b) |
99 | 0 | return -1; |
100 | 0 | if (*a > *b) |
101 | 0 | return 1; |
102 | 0 | if (*a == 0) /* implies *b == 0 */ |
103 | 0 | return 0; |
104 | 0 | a++; |
105 | 0 | b++; |
106 | 0 | len--; |
107 | 0 | } |
108 | 0 | } |
109 | | EXPORT_SYMBOL(ucs2_strncmp); |
110 | | |
111 | | int |
112 | | ucs2_strcmp(const ucs2_char_t *a, const ucs2_char_t *b) |
113 | 0 | { |
114 | 0 | return ucs2_strncmp(a, b, ~0UL); |
115 | 0 | } |
116 | | EXPORT_SYMBOL(ucs2_strcmp); |
117 | | |
118 | | unsigned long |
119 | | ucs2_utf8size(const ucs2_char_t *src) |
120 | 0 | { |
121 | 0 | unsigned long i; |
122 | 0 | unsigned long j = 0; |
123 | |
|
124 | 0 | for (i = 0; src[i]; i++) { |
125 | 0 | u16 c = src[i]; |
126 | |
|
127 | 0 | if (c >= 0x800) |
128 | 0 | j += 3; |
129 | 0 | else if (c >= 0x80) |
130 | 0 | j += 2; |
131 | 0 | else |
132 | 0 | j += 1; |
133 | 0 | } |
134 | |
|
135 | 0 | return j; |
136 | 0 | } |
137 | | EXPORT_SYMBOL(ucs2_utf8size); |
138 | | |
139 | | /* |
140 | | * copy at most maxlength bytes of whole utf8 characters to dest from the |
141 | | * ucs2 string src. |
142 | | * |
143 | | * The return value is the number of characters copied, not including the |
144 | | * final NUL character. |
145 | | */ |
146 | | unsigned long |
147 | | ucs2_as_utf8(u8 *dest, const ucs2_char_t *src, unsigned long maxlength) |
148 | 0 | { |
149 | 0 | unsigned int i; |
150 | 0 | unsigned long j = 0; |
151 | 0 | unsigned long limit = ucs2_strnlen(src, maxlength); |
152 | |
|
153 | 0 | for (i = 0; maxlength && i < limit; i++) { |
154 | 0 | u16 c = src[i]; |
155 | |
|
156 | 0 | if (c >= 0x800) { |
157 | 0 | if (maxlength < 3) |
158 | 0 | break; |
159 | 0 | maxlength -= 3; |
160 | 0 | dest[j++] = 0xe0 | (c & 0xf000) >> 12; |
161 | 0 | dest[j++] = 0x80 | (c & 0x0fc0) >> 6; |
162 | 0 | dest[j++] = 0x80 | (c & 0x003f); |
163 | 0 | } else if (c >= 0x80) { |
164 | 0 | if (maxlength < 2) |
165 | 0 | break; |
166 | 0 | maxlength -= 2; |
167 | 0 | dest[j++] = 0xc0 | (c & 0x7c0) >> 6; |
168 | 0 | dest[j++] = 0x80 | (c & 0x03f); |
169 | 0 | } else { |
170 | 0 | maxlength -= 1; |
171 | 0 | dest[j++] = c & 0x7f; |
172 | 0 | } |
173 | 0 | } |
174 | 0 | if (maxlength) |
175 | 0 | dest[j] = '\0'; |
176 | 0 | return j; |
177 | 0 | } |
178 | | EXPORT_SYMBOL(ucs2_as_utf8); |
179 | | |
180 | | MODULE_DESCRIPTION("UCS2 string handling"); |
181 | | MODULE_LICENSE("GPL v2"); |