blob: f42fd860be84b7afbbfd7b88283019ea9aa489e7 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
/*
*
* Copyright 1998 CrossWire Bible Society (http://www.crosswire.org)
* CrossWire Bible Society
* P. O. Box 2528
* Tempe, AZ 85280-2528
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation version 2.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
*/
#include "swunicod.h"
unsigned char* UTF32to8 (unsigned long utf32, unsigned char * utf8) {
unsigned int i;
for (i = 0; i < 6; i++) utf8[i] = 0;
if (utf32 < 0x80) {
utf8[0] = (char)utf32;
}
else if (utf32 < 0x800) {
i = utf32 & 0x3f;
utf8[1] = 0x80 | i;
utf32 >>= 6;
i = utf32 & 0x1f;
utf8[0] = 0xc0 | i;
}
else if (utf32 < 0x10000) {
i = utf32 & 0x3f;
utf8[2] = 0x80 | i;
utf32 >>= 6;
i = utf32 & 0x3f;
utf8[1] = 0x80 | i;
utf32 >>= 6;
i = utf32 & 0x0f;
utf8[0] = 0xe0 | i;
}
else if (utf32 < 0x200000) {
i = utf32 & 0x3f;
utf8[3] = 0x80 | i;
utf32 >>= 6;
i = utf32 & 0x3f;
utf8[2] = 0x80 | i;
utf32 >>= 6;
i = utf32 & 0x3f;
utf8[1] = 0x80 | i;
utf32 >>= 6;
i = utf32 & 0x07;
utf8[0] = 0xf0 | i;
}
else if (utf32 < 0x4000000) {
i = utf32 & 0x3f;
utf8[4] = 0x80 | i;
utf32 >>= 6;
i = utf32 & 0x3f;
utf8[3] = 0x80 | i;
utf32 >>= 6;
i = utf32 & 0x3f;
utf8[2] = 0x80 | i;
utf32 >>= 6;
i = utf32 & 0x3f;
utf8[1] = 0x80 | i;
utf32 >>= 6;
i = utf32 & 0x03;
utf8[0] = 0xf8 | i;
}
else if (utf32 < 0x80000000) {
i = utf32 & 0x3f;
utf8[5] = 0x80 | i;
utf32 >>= 6;
i = utf32 & 0x3f;
utf8[4] = 0x80 | i;
utf32 >>= 6;
i = utf32 & 0x3f;
utf8[3] = 0x80 | i;
utf32 >>= 6;
i = utf32 & 0x3f;
utf8[2] = 0x80 | i;
utf32 >>= 6;
i = utf32 & 0x3f;
utf8[1] = 0x80 | i;
utf32 >>= 6;
i = utf32 & 0x01;
utf8[0] = 0xfc | i;
}
return utf8;
}
/** Converts a UTF-8 encoded 1-6 byte array into a 32-bit unsigned integer UTF-32 value
* @param utf8 pointer to an array of 6 unsigned chars containing the UTF-8 value, starting in the utf8[0]
* @param utf32 the UTF-32 Unicode code point value
*/
unsigned long UTF8to32 (unsigned char * utf8) {
unsigned char i = utf8[0];
unsigned char count;
unsigned long utf32 = 0;
for (count = 0; i & 0x80; count++) i <<= 1;
if (!count) {
return utf8[0];
}
else if (count == 1) {
return 0xffff;
}
else {
count--;
utf32 = i >> count;
for (i = 1; i <= count; i++) {
if (0xc0 & utf8[i] != 0x80) {
return 0xffff;
}
utf32 <<= 6;
utf32 |= (utf8[i] & 0x3f);
}
}
return utf32;
}
|