1 /* Copyright (C) 2001-2012 Artifex Software, Inc.
2 All Rights Reserved.
3
4 This software is provided AS-IS with no warranty, either express or
5 implied.
6
7 This software is distributed under license and may not be copied,
8 modified or distributed except as expressly authorized under the terms
9 of the license contained in the file LICENSE in this distribution.
10
11 Refer to licensing information at http://www.artifex.com or contact
12 Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134, San Rafael,
13 CA 94903, U.S.A., +1(415)492-9861, for further information.
14 */
15
16
17 #include "windows_.h"
18
19 #ifndef WINDOWS_NO_UNICODE
utf8_to_wchar(wchar_t * out,const char * in)20 int utf8_to_wchar(wchar_t *out, const char *in)
21 {
22 unsigned int i;
23 unsigned int len = 1;
24 unsigned char c;
25
26 if (out) {
27 while (i = *(unsigned char *)in++) {
28 if (i < 0x80) {
29 *out++ = (wchar_t)i;
30 len++;
31 } else if ((i & 0xE0) == 0xC0) {
32 i &= 0x1F;
33 c = (unsigned char)*in++;
34 if ((c & 0xC0) != 0x80)
35 return -1;
36 i = (i<<6) | (c & 0x3f);
37 *out++ = (wchar_t)i;
38 len++;
39 } else if ((i & 0xF0) == 0xE0) {
40 i &= 0xF;
41 c = (unsigned char)*in++;
42 if ((c & 0xC0) != 0x80)
43 return -1;
44 i = (i<<6) | (c & 0x3f);
45 c = (unsigned char)*in++;
46 if ((c & 0xC0) != 0x80)
47 return -1;
48 i = (i<<6) | (c & 0x3f);
49 *out++ = (wchar_t)i;
50 len++;
51 } else {
52 return -1;
53 }
54 }
55 *out = 0;
56 } else {
57 while (i = *(unsigned char *)in++) {
58 if (i < 0x80) {
59 len++;
60 } else if ((i & 0xE0) == 0xC0) {
61 in++;
62 len += 2;
63 } else if ((i & 0xF0) == 0xE0) {
64 in+=2;
65 len += 3;
66 } else {
67 return -1;
68 }
69 }
70 }
71 return len;
72 }
73
wchar_to_utf8(char * out,const wchar_t * in)74 int wchar_to_utf8(char *out, const wchar_t *in)
75 {
76 unsigned int i;
77 unsigned int len = 1;
78
79 if (out) {
80 while (i = (unsigned int)*in++) {
81 if (i < 0x80) {
82 *out++ = (char)i;
83 len++;
84 } else if (i < 0x800) {
85 *out++ = 0xC0 | ( i>> 6 );
86 *out++ = 0x80 | ( i & 0x3F);
87 len++;
88 } else /* if (i < 0x10000) */ {
89 *out++ = 0xE0 | ( i>>12 );
90 *out++ = 0x80 | ((i>> 6) & 0x3F);
91 *out++ = 0x80 | ( i & 0x3F);
92 len++;
93 }
94 }
95 *out = 0;
96 } else {
97 while (i = (unsigned int)*in++) {
98 if (i < 0x80) {
99 len++;
100 } else if (i < 0x800) {
101 len += 2;
102 } else /* if (i < 0x10000) */ {
103 len += 3;
104 }
105 }
106 }
107 return len;
108 }
109 #endif
110