1 /* $NetBSD: unicode.h,v 1.1.1.1 2007/03/06 00:10:39 dillo Exp $ */
4 * Copyright (c) 2007 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/types.h>
36 #define UNICODE_DECOMPOSE 0x01
37 #define UNICODE_PRECOMPOSE 0x02
38 #define UNICODE_UTF8_LATIN1_FALLBACK 0x03
40 size_t utf8_to_utf16(uint16_t *, size_t, const char *, size_t, int, int *);
41 size_t utf16_to_utf8(char *, size_t, const uint16_t *, size_t, int, int *);
44 utf8_to_utf16(uint16_t *dst, size_t dst_len,
45 const char *src, size_t src_len,
48 const unsigned char *s;
53 #define IS_CONT(c) (((c)&0xc0) == 0x80)
56 s = (const unsigned char *)src;
58 while (spos<src_len) {
61 else if ((flags & UNICODE_UTF8_LATIN1_FALLBACK)
62 && (spos >= src_len || !IS_CONT(s[spos+1]))
64 /* not valid UTF-8, assume ISO 8859-1 */
67 else if (s[spos] < 0xc0 || s[spos] >= 0xf5) {
68 /* continuation byte without lead byte
69 or lead byte for codepoint above 0x10ffff */
74 else if (s[spos] < 0xe0) {
75 if (spos >= src_len || !IS_CONT(s[spos+1])) {
80 c = ((s[spos] & 0x3f) << 6) | (s[spos+1] & 0x3f);
83 /* overlong encoding */
88 else if (s[spos] < 0xf0) {
90 || !IS_CONT(s[spos+1]) || !IS_CONT(s[spos+2])) {
95 c = ((s[spos] & 0x0f) << 12) | ((s[spos+1] & 0x3f) << 6)
98 if (c < 0x800 || (c & 0xdf00) == 0xd800 ) {
99 /* overlong encoding or encoded surrogate */
106 /* UTF-16 surrogate pair */
108 if (spos >= src_len-3 || !IS_CONT(s[spos+1])
109 || !IS_CONT(s[spos+2]) || !IS_CONT(s[spos+3])) {
115 cc = ((s[spos] & 0x03) << 18) | ((s[spos+1] & 0x3f) << 12)
116 | ((s[spos+2] & 0x3f) << 6) | (s[spos+3] & 0x3f);
119 /* overlong encoding */
123 if (dst && dpos < dst_len)
124 dst[dpos] = (0xd800 | ((cc-0x10000)>>10));
126 c = 0xdc00 | ((cc-0x10000) & 0x3ffff);
129 if (dst && dpos < dst_len)
144 utf16_to_utf8(char *dst, size_t dst_len,
145 const uint16_t *src, size_t src_len,
146 int flags, int *errp)
151 #define CHECK_LENGTH(l) (dpos > dst_len-(l) ? dst=NULL : NULL)
152 #define ADD_BYTE(b) (dst ? dst[dpos] = (b) : 0, dpos++)
156 for (spos=0; spos<src_len; spos++) {
157 if (src[spos] < 0x80) {
161 else if (src[spos] < 0x800) {
163 ADD_BYTE(0xc0 | (src[spos]>>6));
164 ADD_BYTE(0x80 | (src[spos] & 0x3f));
166 else if ((src[spos] & 0xdc00) == 0xd800) {
168 /* first surrogate */
169 if (spos == src_len - 1 || (src[spos] & 0xdc00) != 0xdc00) {
170 /* no second surrogate present */
176 c = (((src[spos]&0x3ff) << 10) | (src[spos+1]&0x3ff)) + 0x10000;
177 ADD_BYTE(0xf0 | (c>>18));
178 ADD_BYTE(0x80 | ((c>>12) & 0x3f));
179 ADD_BYTE(0x80 | ((c>>6) & 0x3f));
180 ADD_BYTE(0x80 | (c & 0x3f));
182 else if ((src[spos] & 0xdc00) == 0xdc00) {
183 /* second surrogate without preceding first surrogate */
188 ADD_BYTE(0xe0 | src[spos]>>12);
189 ADD_BYTE(0x80 | ((src[spos]>>6) & 0x3f));
190 ADD_BYTE(0x80 | (src[spos] & 0x3f));