1da2e3ebdSchin /***********************************************************************
2da2e3ebdSchin * *
3da2e3ebdSchin * This software is part of the ast package *
4*b30d1939SAndy Fiddaman * Copyright (c) 1985-2012 AT&T Intellectual Property *
5da2e3ebdSchin * and is licensed under the *
6*b30d1939SAndy Fiddaman * Eclipse Public License, Version 1.0 *
77c2fbfb3SApril Chin * by AT&T Intellectual Property *
8da2e3ebdSchin * *
9da2e3ebdSchin * A copy of the License is available at *
10*b30d1939SAndy Fiddaman * http://www.eclipse.org/org/documents/epl-v10.html *
11*b30d1939SAndy Fiddaman * (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12da2e3ebdSchin * *
13da2e3ebdSchin * Information and Software Systems Research *
14da2e3ebdSchin * AT&T Research *
15da2e3ebdSchin * Florham Park NJ *
16da2e3ebdSchin * *
17da2e3ebdSchin * Glenn Fowler <gsf@research.att.com> *
18da2e3ebdSchin * David Korn <dgk@research.att.com> *
19da2e3ebdSchin * Phong Vo <kpv@research.att.com> *
20da2e3ebdSchin * *
21da2e3ebdSchin ***********************************************************************/
22da2e3ebdSchin #pragma prototyped
23da2e3ebdSchin /*
24da2e3ebdSchin * Glenn Fowler
25da2e3ebdSchin * AT&T Research
26da2e3ebdSchin *
27*b30d1939SAndy Fiddaman * convert wide character to utf8 in s
28*b30d1939SAndy Fiddaman * s must have room for at least 6 bytes
29*b30d1939SAndy Fiddaman * return value is the number of chars placed in s
30*b30d1939SAndy Fiddaman * thanks Tom Duff
31da2e3ebdSchin */
32da2e3ebdSchin
33da2e3ebdSchin #include <ast.h>
34da2e3ebdSchin
35*b30d1939SAndy Fiddaman typedef struct Utf8_s
36*b30d1939SAndy Fiddaman {
37*b30d1939SAndy Fiddaman uint32_t range;
38*b30d1939SAndy Fiddaman unsigned short prefix;
39*b30d1939SAndy Fiddaman unsigned short shift;
40*b30d1939SAndy Fiddaman } Utf8_t;
41*b30d1939SAndy Fiddaman
42*b30d1939SAndy Fiddaman static const Utf8_t ops[] =
43*b30d1939SAndy Fiddaman {
44*b30d1939SAndy Fiddaman { 0x00000080, 0x00, 0 },
45*b30d1939SAndy Fiddaman { 0x00000800, 0xc0, 6 },
46*b30d1939SAndy Fiddaman { 0x00010000, 0xe0, 12 },
47*b30d1939SAndy Fiddaman { 0x00200000, 0xf0, 18 },
48*b30d1939SAndy Fiddaman { 0x04000000, 0xf8, 24 },
49*b30d1939SAndy Fiddaman { 0x80000000, 0xfc, 30 }
50*b30d1939SAndy Fiddaman };
51*b30d1939SAndy Fiddaman
52da2e3ebdSchin int
wc2utf8(register char * s,register uint32_t w)53*b30d1939SAndy Fiddaman wc2utf8(register char* s, register uint32_t w)
54da2e3ebdSchin {
55*b30d1939SAndy Fiddaman register int i;
56*b30d1939SAndy Fiddaman char* b;
57da2e3ebdSchin
58*b30d1939SAndy Fiddaman for (i = 0; i < elementsof(ops); i++)
59*b30d1939SAndy Fiddaman if (w < ops[i].range)
60da2e3ebdSchin {
61*b30d1939SAndy Fiddaman b = s;
62*b30d1939SAndy Fiddaman *s++ = ops[i].prefix | (w >> ops[i].shift);
63*b30d1939SAndy Fiddaman switch (ops[i].shift)
64da2e3ebdSchin {
65*b30d1939SAndy Fiddaman case 30: *s++ = 0x80 | ((w >> 24) & 0x3f);
66*b30d1939SAndy Fiddaman /* FALLTHROUGH */
67*b30d1939SAndy Fiddaman case 24: *s++ = 0x80 | ((w >> 18) & 0x3f);
68*b30d1939SAndy Fiddaman /* FALLTHROUGH */
69*b30d1939SAndy Fiddaman case 18: *s++ = 0x80 | ((w >> 12) & 0x3f);
70*b30d1939SAndy Fiddaman /* FALLTHROUGH */
71*b30d1939SAndy Fiddaman case 12: *s++ = 0x80 | ((w >> 6) & 0x3f);
72*b30d1939SAndy Fiddaman /* FALLTHROUGH */
73*b30d1939SAndy Fiddaman case 6: *s++ = 0x80 | (w & 0x3f);
74da2e3ebdSchin }
75*b30d1939SAndy Fiddaman return s - b;
76da2e3ebdSchin }
77*b30d1939SAndy Fiddaman return 0;
78da2e3ebdSchin }
79