2 * fribidi-char-sets-utf8.c - UTF-8 character set conversion routines
5 * Behdad Esfahbod, 2001, 2002, 2004
6 * Dov Grobgeld, 1999, 2000
8 * Copyright (C) 2004 Sharif FarsiWeb, Inc
9 * Copyright (C) 2001,2002 Behdad Esfahbod
10 * Copyright (C) 1999,2000 Dov Grobgeld
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this library, in a file named COPYING; if not, write to the
24 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 * Boston, MA 02110-1301, USA
27 * For licensing issues, contact <fribidi.license@gmail.com>.
32 #include <fribidi-char-sets-utf8.h>
34 #include <fribidi-unicode.h>
37 fribidi_utf8_to_unicode (
45 FriBidiStrIndex length;
46 const unsigned char *s = (unsigned const char *) ss;
47 const unsigned char *t = s;
50 while ((FriBidiStrIndex) (s - t) < len)
52 register unsigned char ch = *s;
53 if (ch <= 0x7f) /* one byte */
57 else if (ch <= 0xdf) /* 2 byte */
59 *us++ = ((*s & 0x1f) << 6) + (*(s + 1) & 0x3f);
62 else if (ch <= 0xef) /* 3 byte */
65 ((int) (*s & 0x0f) << 12) +
66 ((*(s + 1) & 0x3f) << 6) + (*(s + 2) & 0x3f);
72 ((int) (*s & 0x07) << 18) +
73 ((*(s + 1) & 0x3f) << 12) +
74 ((*(s + 2) & 0x3f) << 6) +
75 ((*(s + 3) & 0x3f) << 0);
84 fribidi_unicode_to_utf8 (
86 const FriBidiChar *us,
93 unsigned char *s = (unsigned char *) ss;
96 for (i = 0; i < len; i++)
98 FriBidiChar mychar = us[i];
103 else if (mychar <= 0x7FF)
105 *t++ = 0xC0 | (unsigned char) (mychar >> 6); /* upper 5 bits */
106 *t++ = 0x80 | (unsigned char) (mychar & 0x3F); /* lower 6 bits */
108 else if (mychar <= 0xFFFF)
110 *t++ = 0xE0 | (unsigned char) (mychar >> 12); /* upper 4 bits */
111 *t++ = 0x80 | (unsigned char) ((mychar >> 6) & 0x3F); /* next 6 bits */
112 *t++ = 0x80 | (unsigned char) (mychar & 0x3F); /* lowest 6 bits */
114 else if (mychar < FRIBIDI_UNICODE_CHARS)
116 *t++ = 0xF0 | (unsigned char) ((mychar >> 18) & 0x07); /* upper 3 bits */
117 *t++ = 0x80 | (unsigned char) ((mychar >> 12) & 0x3F); /* next 6 bits */
118 *t++ = 0x80 | (unsigned char) ((mychar >> 6) & 0x3F); /* next 6 bits */
119 *t++ = 0x80 | (unsigned char) (mychar & 0x3F); /* lowest 6 bits */
127 /* Editor directions:
128 * vim:textwidth=78:tabstop=8:shiftwidth=2:autoindent:cindent