-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUTF8.i
More file actions
181 lines (144 loc) · 3.27 KB
/
UTF8.i
File metadata and controls
181 lines (144 loc) · 3.27 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
UTF8.library
Copyright (C) 2020 Kjetil Hvalstrand
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
#define UTF8_2BYTES 0x80|0x40
#define MCHARS 0x80;
void show_bits(ULONG num)
{
int n;
for (n=32;n!=-1;n--) printf("%c", (1<<n) & num ? '1' : '0' );
printf("\n");
}
ULONG utf8_get_glyph(unsigned char *data, int *len)
{
unsigned char *c;
ULONG ret = 0;
int bytes;
int bits;
int n;
int mask;
bytes = 1;
c =data;
if ( ! (*c & 0x80))
{
ret = *c;
*len = 1;
}
else // muli chars;
{
bits =6;
if ( (*c & 0xC0) == 0x80 )
{
*len =0; return 0;
}
for ( n = 6; (*c&(1<<n)) && (n>0) ; n--)
{
bytes++; bits--;
}
mask = (1<<bits)-1 ;
ret = *c & mask;
for (n=1; n<bytes;n++)
{
c++;
if ( (*c&0xC0) != 0x80) { *len = 0; return 0; }
ret = (ret << 6) | (*c & 0x3F);
if (*c == 0) { *len = 0; return 0; }
}
if (ret<128)
{
ret = 0x20;
}
}
*len = bytes;
return ret;
}
int utf8_estimate_bytesize(ULONG glyph)
{
int n;
int bytes_to_bits[] = {7,11,16,21,26,31};
for (n = 0; n<6; n++)
if (glyph < (1<<bytes_to_bits[n]) ) return n+1;
return 0;
}
int utf8_set_glyph( ULONG glyph, unsigned char *data )
{
unsigned char prefix[] = {0x0,0xC0,0xE0,0xF0,0xF8,0xFC,0xFE};
int n,pos;
int bytes = utf8_estimate_bytesize(glyph);
for ( n = bytes-1; n>0; n-- )
{
data[n] = 0x80 | (glyph & 0x3F);
glyph = glyph >> 6;
}
data[0] = prefix[bytes-1] | glyph;
return bytes;
}
int utf8_length( unsigned char *utf8 )
{
char *c;
int cnt = 0;
for (c = utf8 ; *c ;c++ )
{
if (*c < 128) cnt++;
if ( (*c & 0xC0) == 0xC0 ) cnt++;
}
return cnt;
}
unsigned char *utf8_encode( char *ascii, ULONG *codeset_page )
{
char *c;
unsigned char *utf8 = NULL;
int size,pos;
size = 0;
for (c = ascii; *c ; c++ )
size += utf8_estimate_bytesize( codeset_page[ *c ] );
printf("UTF8 buffer %d\n",size);
if (utf8 = IExec -> AllocVec(size+1, MEMF_CLEAR ))
{
pos = 0;
for (c = ascii; *c ; c++ )
pos += utf8_set_glyph( codeset_page[ *c ], utf8 + pos );
utf8[pos] = 0;
}
return utf8;
}
unsigned char *utf8_decode( unsigned char *utf8, ULONG *codeset_page )
{
char *c;
char *new_str = NULL;
int pos,n,len;
ULONG ret;
int size = utf8_length( utf8 );
if (new_str = IExec -> AllocVec(size+1, MEMF_CLEAR ))
{
c = new_str;
pos = 0;
do
{
ret = utf8_get_glyph( utf8 + pos, &len) ;
pos += len;
*c = 0x20; // default value if glyph is not found.
for (n = 0; n<256;n++)
{
if (codeset_page[n] == ret )
{ *c = n; break; }
}
c++;
} while ( (len>0) && (ret > 0) );
*c = 0; // term string
}
return new_str;
}