forked from ThomasDickey/vttest-snapshots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutf8.c
138 lines (122 loc) · 3.41 KB
/
utf8.c
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
/* $Id: utf8.c,v 1.1 2010/08/28 12:28:16 tom Exp $ */
#include <vttest.h>
int
conv_to_utf8(unsigned char *target, unsigned source, unsigned limit)
{
#define CH(n) CharOf((source) >> ((n) * 8))
int rc = 0;
if (source <= 0x0000007f)
rc = 1;
else if (source <= 0x000007ff)
rc = 2;
else if (source <= 0x0000ffff)
rc = 3;
else if (source <= 0x001fffff)
rc = 4;
else if (source <= 0x03ffffff)
rc = 5;
else /* (source <= 0x7fffffff) */
rc = 6;
if ((unsigned) rc > limit) { /* whatever it is, we cannot decode it */
rc = 0;
}
if (target != 0) {
switch (rc) {
case 1:
target[0] = CH(0);
break;
case 2:
target[1] = CharOf(0x80 | (CH(0) & 0x3f));
target[0] = CharOf(0xc0 | (CH(0) >> 6) | ((CH(1) & 0x07) << 2));
break;
case 3:
target[2] = CharOf(0x80 | (CH(0) & 0x3f));
target[1] = CharOf(0x80 | (CH(0) >> 6) | ((CH(1) & 0x0f) << 2));
target[0] = CharOf(0xe0 | ((int) (CH(1) & 0xf0) >> 4));
break;
case 4:
target[3] = CharOf(0x80 | (CH(0) & 0x3f));
target[2] = CharOf(0x80 | (CH(0) >> 6) | ((CH(1) & 0x0f) << 2));
target[1] = CharOf(0x80 |
((int) (CH(1) & 0xf0) >> 4) |
((int) (CH(2) & 0x03) << 4));
target[0] = CharOf(0xf0 | ((int) (CH(2) & 0x1f) >> 2));
break;
case 5:
target[4] = CharOf(0x80 | (CH(0) & 0x3f));
target[3] = CharOf(0x80 | (CH(0) >> 6) | ((CH(1) & 0x0f) << 2));
target[2] = CharOf(0x80 |
((int) (CH(1) & 0xf0) >> 4) |
((int) (CH(2) & 0x03) << 4));
target[1] = CharOf(0x80 | (CH(2) >> 2));
target[0] = CharOf(0xf8 | (CH(3) & 0x03));
break;
case 6:
target[5] = CharOf(0x80 | (CH(0) & 0x3f));
target[4] = CharOf(0x80 | (CH(0) >> 6) | ((CH(1) & 0x0f) << 2));
target[3] = CharOf(0x80 | (CH(1) >> 4) | ((CH(2) & 0x03) << 4));
target[2] = CharOf(0x80 | (CH(2) >> 2));
target[1] = CharOf(0x80 | (CH(3) & 0x3f));
target[0] = CharOf(0xfc | ((int) (CH(3) & 0x40) >> 6));
break;
}
}
return rc; /* number of bytes needed in target */
#undef CH
}
int
conv_to_utf32(unsigned *target, const char *source, unsigned limit)
{
#define CH(n) CharOf((*target) >> ((n) * 8))
int rc = 0;
int j;
unsigned mask = 0;
/*
* Find the number of bytes we will need from the source.
*/
if ((*source & 0x80) == 0) {
rc = 1;
mask = (unsigned) *source;
} else if ((*source & 0xe0) == 0xc0) {
rc = 2;
mask = (unsigned) (*source & 0x1f);
} else if ((*source & 0xf0) == 0xe0) {
rc = 3;
mask = (unsigned) (*source & 0x0f);
} else if ((*source & 0xf8) == 0xf0) {
rc = 4;
mask = (unsigned) (*source & 0x07);
} else if ((*source & 0xfc) == 0xf8) {
rc = 5;
mask = (unsigned) (*source & 0x03);
} else if ((*source & 0xfe) == 0xfc) {
rc = 6;
mask = (unsigned) (*source & 0x01);
}
if ((unsigned) rc > limit) { /* whatever it is, we cannot decode it */
rc = 0;
}
/*
* sanity-check.
*/
if (rc > 1) {
for (j = 1; j < rc; j++) {
if ((source[j] & 0xc0) != 0x80)
break;
}
if (j != rc) {
rc = 0;
}
}
if (target != 0) {
int shift = 0;
*target = 0;
for (j = 1; j < rc; j++) {
*target |= (unsigned) (source[rc - j] & 0x3f) << shift;
shift += 6;
}
*target |= mask << shift;
}
return rc;
#undef CH
}