-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutf8_and_euc_eudc.c
291 lines (249 loc) · 6.28 KB
/
utf8_and_euc_eudc.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* utf8_and_euc_eudc.c: EUC_JP EUDC <--> UTF8
*
* Copyright (c) 2010, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
*
* EUC_JP : F5A1 - FEFE, 8FF5A1 - 8FFEFE
* UTF8 : E000 - E3AB, E3AC - E757
*/
#include "postgres.h"
#include "fmgr.h"
#include "mb/pg_wchar.h"
#include "eudc.h"
#include "pgut/pgut-be.h"
static PGFunction euc_jp_to_utf8 = NULL;
static PGFunction utf8_to_euc_jp = NULL;
PG_FUNCTION_INFO_V1(euc_jp_eudc_to_utf8);
PG_FUNCTION_INFO_V1(utf8_to_euc_jp_eudc);
extern Datum PGDLLEXPORT euc_jp_eudc_to_utf8(PG_FUNCTION_ARGS);
extern Datum PGDLLEXPORT utf8_to_euc_jp_eudc(PG_FUNCTION_ARGS);
static inline int
pg_euc_mblen(const unsigned char *s)
{
int len;
if (*s == SS2)
len = 2;
else if (*s == SS3)
len = 3;
else if (IS_HIGHBIT_SET(*s))
len = 2;
else
len = 1;
return len;
}
/* ----------
* conv_proc(
* INTEGER, -- source encoding id
* INTEGER, -- destination encoding id
* CSTRING, -- source string (null terminated C string)
* CSTRING, -- destination string (null terminated C string)
* INTEGER -- source string length
* ) returns VOID;
* ----------
*/
Datum
euc_jp_eudc_to_utf8(PG_FUNCTION_ARGS)
{
unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
unsigned char *p;
unsigned char *fallback_character = NULL;
int len = PG_GETARG_INT32(4);
int euc_jp_len;
int clen;
CHECK_ENCODING_CONVERSION_ARGS(PG_EUC_JP, PG_UTF8);
if (euc_jp_to_utf8 == NULL)
euc_jp_to_utf8 = load_external_function(
"utf8_and_euc_jp", "euc_jp_to_utf8", true, NULL);
*dest = '\0';
p = src;
euc_jp_len = 0;
for (; len > 0; len -= clen)
{
const unsigned char *c = p + euc_jp_len;
if (c[0] == '\0')
report_invalid_encoding(PG_EUC_JP, (const char *) c, len);
clen = pg_euc_mblen(c);
if ((clen == 2 &&
0xf5 <= c[0] && c[0] <= 0xfe &&
0xa1 <= c[1] && c[1] <= 0xfe) ||
(clen == 3 && c[0] == 0x8f &&
0xf5 <= c[1] && c[1] <= 0xfe &&
0xa1 <= c[2] && c[2] <= 0xfe))
{
int ucs;
int m;
int n;
/* EUC_JP to UTF8 */
if (euc_jp_len > 0)
{
DirectFunctionCall5(euc_jp_to_utf8, PG_EUC_JP, PG_UTF8,
CStringGetDatum(p), CStringGetDatum(dest),
euc_jp_len);
dest = dest + strlen((char *) dest);
p += euc_jp_len;
euc_jp_len = 0;
}
p += clen;
if (clen == 2)
elog(eudc_log_level,
"eudc character found: %02x%02x in EUC_JP to UTF8 conversion",
c[0], c[1]);
else
elog(eudc_log_level,
"eudc character found: %02x%02x%02x in EUC_JP to UTF8 conversion",
c[0], c[1], c[2]);
/* EUC_JP EUDC to UTF8 */
if (eudc_fallback_character && eudc_fallback_character[0])
{
/* map to fallback character */
int i;
if (fallback_character == NULL)
{
fallback_character = pg_do_encoding_conversion(
(unsigned char *) eudc_fallback_character,
strlen(eudc_fallback_character),
GetDatabaseEncoding(),
PG_UTF8);
}
for (i = 0; fallback_character[i]; i++)
*dest++ = fallback_character[i];
}
else
{
/* linear mapping */
if (len == 3)
{
ucs = 0xe3ac;
c++;
}
else
ucs = 0xe000;
n = c[0] - 0xf5;
m = c[1] - 0xa1;
ucs += n * 94 + m;
*dest++ = (ucs >> 12) | 0xe0;
*dest++ = (ucs & 0x0fc0) >> 6 | 0x80;
*dest++ = (ucs & 0x003f) | 0x80;
}
*dest = '\0';
}
else
euc_jp_len += clen;
}
/* EUC_JP to UTF8 */
if (euc_jp_len > 0)
DirectFunctionCall5(euc_jp_to_utf8, PG_EUC_JP, PG_UTF8,
CStringGetDatum(p), CStringGetDatum(dest),
euc_jp_len);
if (fallback_character != NULL &&
fallback_character != (unsigned char*) eudc_fallback_character)
pfree(fallback_character);
PG_RETURN_VOID();
}
Datum
utf8_to_euc_jp_eudc(PG_FUNCTION_ARGS)
{
unsigned char *src = (unsigned char *) PG_GETARG_CSTRING(2);
unsigned char *dest = (unsigned char *) PG_GETARG_CSTRING(3);
unsigned char *p;
unsigned char *fallback_character = NULL;
int len = PG_GETARG_INT32(4);
int utf8_len;
int clen;
CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, PG_EUC_JP);
if (utf8_to_euc_jp == NULL)
utf8_to_euc_jp = load_external_function(
"utf8_and_euc_jp", "utf8_to_euc_jp", true, NULL);
*dest = '\0';
p = src;
utf8_len = 0;
for (; len > 0; len -= clen)
{
const unsigned char *c = p + utf8_len;
int ucs;
if (c[0] == '\0')
break;
clen = pg_utf_mblen(p + utf8_len);
if (len < clen)
break;
if (clen != 3)
{
utf8_len += clen;
continue;
}
ucs = (((int) c[0] & 0x0f) << 12) |
(((int) c[1] & 0x3f) << 6) |
((int) c[2] & 0x3f);
if (ucs >= 0xe000 && ucs <= 0xe757)
{
int m;
int n;
/* UTF8 to EUC_JP */
if (utf8_len > 0)
{
DirectFunctionCall5(utf8_to_euc_jp, PG_UTF8, PG_EUC_JP,
CStringGetDatum(p), CStringGetDatum(dest),
utf8_len);
dest = dest + strlen((char *) dest);
p += utf8_len;
utf8_len = 0;
}
p += clen;
elog(eudc_log_level,
"eudc character found: %02x%02x%02x in UTF8 to EUC_JP conversion",
c[0], c[1], c[2]);
/* UTF8 to EUC_JP EUDC */
if (eudc_fallback_character && eudc_fallback_character[0])
{
/* map to fallback character */
int i;
if (fallback_character == NULL)
{
fallback_character = pg_do_encoding_conversion(
(unsigned char *) eudc_fallback_character,
strlen(eudc_fallback_character),
GetDatabaseEncoding(),
PG_EUC_JP);
}
for (i = 0; fallback_character[i]; i++)
*dest++ = fallback_character[i];
}
else
{
/* linear mapping */
if (ucs < 0xe3ac)
{
m = (ucs - 0xE000) % 94;
n = (ucs - 0xE000) / 94;
/* euc_jp = 0xF5A1 + n * 0x100 + m */
*dest++ = 0xf5 + n;
*dest++ = 0xa1 + m;
}
else
{
m = (ucs - 0xE3AC) % 94;
n = (ucs - 0xE3AC) / 94;
/* euc_jp = 0x8FF5A1 + n * 0x100 + m */
*dest++ = 0x8f;
*dest++ = 0xf5 + n;
*dest++ = 0xa1 + m;
}
}
*dest = '\0';
}
else
utf8_len += clen;
}
if (len > 0)
report_invalid_encoding(PG_UTF8, (const char *) p + utf8_len, len);
/* UTF8 to EUC_JP */
if (utf8_len > 0)
DirectFunctionCall5(utf8_to_euc_jp, PG_UTF8, PG_EUC_JP,
CStringGetDatum(p), CStringGetDatum(dest),
utf8_len);
if (fallback_character != NULL &&
fallback_character != (unsigned char*) eudc_fallback_character)
pfree(fallback_character);
PG_RETURN_VOID();
}