blob: 8654b0455f97dbd98afa50e590f7a6b8a4ac0fb8 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/* libs/cutils/strdup8to16.c
2**
3** Copyright 2006, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#include <cutils/jstring.h>
19#include <assert.h>
20#include <stdlib.h>
21
22/* See http://www.unicode.org/reports/tr22/ for discussion
23 * on invalid sequences
24 */
25
26#define UTF16_REPLACEMENT_CHAR 0xfffd
27
28/* Clever trick from Dianne that returns 1-4 depending on leading bit sequence*/
29#define UTF8_SEQ_LENGTH(ch) (((0xe5000000 >> ((ch >> 3) & 0x1e)) & 3) + 1)
30
31/* note: macro expands to multiple lines */
32#define UTF8_SHIFT_AND_MASK(unicode, byte) \
33 (unicode)<<=6; (unicode) |= (0x3f & (byte));
34
35#define UNICODE_UPPER_LIMIT 0x10fffd
36
37/**
38 * out_len is an out parameter (which may not be null) containing the
39 * length of the UTF-16 string (which may contain embedded \0's)
40 */
41
42extern char16_t * strdup8to16 (const char* s, size_t *out_len)
43{
44 char16_t *ret;
45 size_t len;
46
47 if (s == NULL) return NULL;
48
49 len = strlen8to16(s);
50
51 // no plus-one here. UTF-16 strings are not null terminated
52 ret = (char16_t *) malloc (sizeof(char16_t) * len);
53
54 return strcpy8to16 (ret, s, out_len);
55}
56
57/**
58 * Like "strlen", but for strings encoded with Java's modified UTF-8.
59 *
60 * The value returned is the number of UTF-16 characters required
61 * to represent this string.
62 */
63extern size_t strlen8to16 (const char* utf8Str)
64{
65 size_t len = 0;
66 int ic;
67 int expected = 0;
68
69 while ((ic = *utf8Str++) != '\0') {
70 /* bytes that start 0? or 11 are lead bytes and count as characters.*/
71 /* bytes that start 10 are extention bytes and are not counted */
72
73 if ((ic & 0xc0) == 0x80) {
74 /* count the 0x80 extention bytes. if we have more than
75 * expected, then start counting them because strcpy8to16
76 * will insert UTF16_REPLACEMENT_CHAR's
77 */
78 expected--;
79 if (expected < 0) {
80 len++;
81 }
82 } else {
83 len++;
84 expected = UTF8_SEQ_LENGTH(ic) - 1;
85
86 /* this will result in a surrogate pair */
87 if (expected == 3) {
88 len++;
89 }
90 }
91 }
92
93 return len;
94}
95
96
97
98/*
99 * Retrieve the next UTF-32 character from a UTF-8 string.
100 *
101 * Stops at inner \0's
102 *
103 * Returns UTF16_REPLACEMENT_CHAR if an invalid sequence is encountered
104 *
105 * Advances "*pUtf8Ptr" to the start of the next character.
106 */
107static inline uint32_t getUtf32FromUtf8(const char** pUtf8Ptr)
108{
109 uint32_t ret;
110 int seq_len;
111 int i;
112
113 /* Mask for leader byte for lengths 1, 2, 3, and 4 respectively*/
114 static const char leaderMask[4] = {0xff, 0x1f, 0x0f, 0x07};
115
116 /* Bytes that start with bits "10" are not leading characters. */
117 if (((**pUtf8Ptr) & 0xc0) == 0x80) {
118 (*pUtf8Ptr)++;
119 return UTF16_REPLACEMENT_CHAR;
120 }
121
122 /* note we tolerate invalid leader 11111xxx here */
123 seq_len = UTF8_SEQ_LENGTH(**pUtf8Ptr);
124
125 ret = (**pUtf8Ptr) & leaderMask [seq_len - 1];
126
127 if (**pUtf8Ptr == '\0') return ret;
128
129 (*pUtf8Ptr)++;
130 for (i = 1; i < seq_len ; i++, (*pUtf8Ptr)++) {
131 if ((**pUtf8Ptr) == '\0') return UTF16_REPLACEMENT_CHAR;
132 if (((**pUtf8Ptr) & 0xc0) != 0x80) return UTF16_REPLACEMENT_CHAR;
133
134 UTF8_SHIFT_AND_MASK(ret, **pUtf8Ptr);
135 }
136
137 return ret;
138}
139
140
141/**
142 * out_len is an out parameter (which may not be null) containing the
143 * length of the UTF-16 string (which may contain embedded \0's)
144 */
145
146extern char16_t * strcpy8to16 (char16_t *utf16Str, const char*utf8Str,
147 size_t *out_len)
148{
149 char16_t *dest = utf16Str;
150
151 while (*utf8Str != '\0') {
152 uint32_t ret;
153
154 ret = getUtf32FromUtf8(&utf8Str);
155
156 if (ret <= 0xffff) {
157 *dest++ = (char16_t) ret;
158 } else if (ret <= UNICODE_UPPER_LIMIT) {
159 /* Create surrogate pairs */
160 /* See http://en.wikipedia.org/wiki/UTF-16/UCS-2#Method_for_code_points_in_Plane_1.2C_Plane_2 */
161
162 *dest++ = 0xd800 | ((ret - 0x10000) >> 10);
163 *dest++ = 0xdc00 | ((ret - 0x10000) & 0x3ff);
164 } else {
165 *dest++ = UTF16_REPLACEMENT_CHAR;
166 }
167 }
168
169 *out_len = dest - utf16Str;
170
171 return utf16Str;
172}
173
174/**
175 * length is the number of characters in the UTF-8 string.
176 * out_len is an out parameter (which may not be null) containing the
177 * length of the UTF-16 string (which may contain embedded \0's)
178 */
179
180extern char16_t * strcpylen8to16 (char16_t *utf16Str, const char*utf8Str,
181 int length, size_t *out_len)
182{
183 /* TODO: Share more of this code with the method above. Only 2 lines changed. */
184
185 char16_t *dest = utf16Str;
186
187 const char *end = utf8Str + length; /* This line */
188 while (utf8Str < end) { /* and this line changed. */
189 uint32_t ret;
190
191 ret = getUtf32FromUtf8(&utf8Str);
192
193 if (ret <= 0xffff) {
194 *dest++ = (char16_t) ret;
195 } else if (ret <= UNICODE_UPPER_LIMIT) {
196 /* Create surrogate pairs */
197 /* See http://en.wikipedia.org/wiki/UTF-16/UCS-2#Method_for_code_points_in_Plane_1.2C_Plane_2 */
198
199 *dest++ = 0xd800 | ((ret - 0x10000) >> 10);
200 *dest++ = 0xdc00 | ((ret - 0x10000) & 0x3ff);
201 } else {
202 *dest++ = UTF16_REPLACEMENT_CHAR;
203 }
204 }
205
206 *out_len = dest - utf16Str;
207
208 return utf16Str;
209}