forked from therion/therion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thchenc.cxx
200 lines (170 loc) · 4.85 KB
/
thchenc.cxx
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
* @file thchenc.cxx
*/
/* Copyright (C) 2000 Stacho Mudrak
*
* $Date: $
* $RCSfile: $
* $Revision: $
*
* --------------------------------------------------------------------
* 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; either version 2 of the License, or
* any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* --------------------------------------------------------------------
*/
#include "thchenc.h"
#include "thchencdata.cxx"
#include "therion.h"
#include "thexception.h"
void thencode(thbuffer * dest, const char * src, int srcenc)
{
// check if source is not UTF-8
if (srcenc == TT_UTF_8) {
thdecode(dest,TT_ASCII,src);
dest->strcpy(src);
return;
}
size_t srcln = strlen(src), srcx = 0;
unsigned char * srcp, * dstp;
// check buffer size
dest->guarantee(srcln + srcln + srcln + srcln + srcln + srcln);
dstp = (unsigned char *) dest->get_buffer();
srcp = (unsigned char *) src;
while (srcx < srcln) {
// check if encoding isn't needed
if (*srcp < thchenc_facc) {
*dstp = *srcp;
dstp++;
}
// we have to encode
else {
wchar_t dch = thencode_tbl[*srcp - thchenc_facc][srcenc];
// two byte UTF-8 character
if (dch < 0X800) {
*dstp = 192 + (dch / 64);
dstp++;
*dstp = 128 + (dch % 64);
dstp++;
}
// three byte UTF-8 character
else if (dch < 0X10000) {
*dstp = 224 + (dch / 4096);
dstp++;
*dstp = 128 + ((dch % 4096) / 64);
dstp++;
*dstp = 128 + (dch % 64);
dstp++;
}
// longer chars not supported
else
therror(("unicode character over 0xFFFF not supported"));
}
srcx++;
srcp++;
}
// end destination string with 0
*dstp = 0;
}
void thdecode(thbuffer * dest, int destenc, const char * src)
{
// chack if source is not UTF-8
if (destenc == TT_UTF_8) {
dest->strcpy(src);
return;
}
size_t srcln = strlen(src), srcx = 0;
unsigned char * srcp, * dstp;
dest->guarantee(srcln); // check buffer size
dstp = (unsigned char*) dest->get_buffer();
srcp = (unsigned char*) src;
wchar_t sch = 0;
while (srcx < srcln) {
// check if decoding isn't needed
if (*srcp < thchenc_facc)
*dstp = *srcp;
// we have to decode
else {
// one byte character
if (*srcp < 0X7F)
sch = (wchar_t) *srcp;
// two byte character
else if ((*srcp / 32) == 6) {
sch = 64 * (*srcp % 32);
srcp++;
srcx++;
if ((srcx >= srcln) || (*srcp < 128))
therror(("invalid UTF-8 string -- \"%s\"",src));
sch += *srcp % 64;
}
// three byte UTF-8 character
else if ((*srcp / 16) == 14) {
sch = 4096 * (*srcp % 16);
srcp++;
srcx++;
if ((srcx >= srcln) || (*srcp < 128))
therror(("invalid UTF-8 string -- \"%s\"",src));
sch += 64 * (*srcp % 64);
srcp++;
srcx++;
if ((srcx >= srcln) || (*srcp < 128))
therror(("invalid UTF-8 string -- \"%s\"",src));
sch += *srcp % 64;
}
// longer chars not supported
else
therror(("invalid UTF-8 string -- \"%s\"",src));
// now we have whchar_t value of UTF-8 character in sch
if (sch < thchenc_fucc)
*dstp = (char) sch;
else {
// let's binsearch it's position in the table
long a = 0, b = (unsigned long)thdecode_tbl_size, x, ix = -1, r, sv = (unsigned long)sch;
while (a <= b) {
x = (unsigned long)((a + b) / 2);
r = sv - long(thdecode_tbl_idx[x]);
if (r == 0) {
ix = x;
break;
}
if (r > 0)
a = x + 1;
else
b = x - 1;
}
if (ix == -1)
*dstp = thdecode_undef;
else
*dstp = thdecode_tbl[ix][destenc];
}
} // end of decoding
srcx++;
srcp++;
dstp++;
}
// end destination string with 0
*dstp = 0;
}
void thprint_encodings()
{
for(int i = 0; i <= TT_UTF_8; i++) {
thprintf("%s\n", thtt_encoding[i].s);
}
}
int thparse_encoding(char * encstr)
{
int eid = thmatch_token(encstr, thtt_encoding);
if (eid == TT_UNKNOWN_ENCODING)
ththrow("invalid encoding -- {}", encstr);
return eid;
}