1
/* FriBidi - Library of BiDi algorithm
2
* Copyright (C) 1999,2000 Dov Grobgeld, and
3
* Copyright (C) 2001,2002 Behdad Esfahbod.
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Lesser General Public
7
* License as published by the Free Software Foundation; either
8
* version 2.1 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Lesser General Public License for more details.
15
* You should have received a copy of the GNU Lesser General Public License
16
* along with this library, in a file named COPYING; if not, write to the
17
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18
* Boston, MA 02111-1307, USA
20
* For licensing issues, contact <dov@imagic.weizmann.ac.il> and
24
#include "fribidi_config.h"
25
#ifndef FRIBIDI_NO_CHARSETS
29
/* the following added by Raphael Finkel <raphael@cs.uky.edu> 12/1999 */
32
fribidi_utf8_to_unicode (char *s, int len, FriBidiChar *us)
33
/* warning: the length of input string may exceed the length of the output */
41
if (*(unsigned char *) s <= 0x7f) /* one byte */
43
*us++ = *s++; /* expand with 0s */
45
else if (*(unsigned char *) s <= 0xdf) /* 2 byte */
48
((*(unsigned char *) s & 0x1f) << 6) +
49
((*(unsigned char *) (s + 1)) & 0x3f);
55
((int) (*(unsigned char *) s & 0x0f) << 12) +
56
((*(unsigned char *) (s + 1) & 0x3f) << 6) +
57
(*(unsigned char *) (s + 2) & 0x3f);
67
fribidi_unicode_to_utf8 (FriBidiChar *us, int length, char *s)
68
/* warning: the length of output string may exceed the length of the input */
74
for (i = 0; i < length; i++)
76
FriBidiChar mychar = us[i];
81
else if (mychar <= 0x7FF)
83
*t++ = 0xC0 | (unsigned char) (mychar >> 6); /* upper 5 bits */
84
*t++ = 0x80 | (unsigned char) (mychar & 0x3F); /* lower 6 bits */
86
else if (mychar <= 0xFFFF)
88
*t++ = 0xE0 | (unsigned char) (mychar >> 12); /* upper 4 bits */
89
*t++ = 0x80 | (unsigned char) ((mychar >> 6) & 0x3F); /* next 6 bits */
90
*t++ = 0x80 | (unsigned char) (mychar & 0x3F); /* lowest 6 bits */
92
else if (mychar < FRIBIDI_UNICODE_CHARS)
94
*t++ = 0xF0 | (unsigned char) ((mychar >> 18) & 0x07); /* upper 3 bits */
95
*t++ = 0x80 | (unsigned char) ((mychar >> 12) & 0x3F); /* next 6 bits */
96
*t++ = 0x80 | (unsigned char) ((mychar >> 6) & 0x3F); /* next 6 bits */
97
*t++ = 0x80 | (unsigned char) (mychar & 0x3F); /* lowest 6 bits */