-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwin_unicode.c
354 lines (337 loc) · 7.91 KB
/
win_unicode.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*-------
* Module: win_unicode.c
*
* Description: This module contains utf8 <-> ucs2 conversion routines
* under WIndows
*
*-------
*/
#include "psqlodbc.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define byte3check 0xfffff800
#define byte2_base 0x80c0
#define byte2_mask1 0x07c0
#define byte2_mask2 0x003f
#define byte3_base 0x8080e0
#define byte3_mask1 0xf000
#define byte3_mask2 0x0fc0
#define byte3_mask3 0x003f
#define surrog_check 0xfc00
#define surrog1_bits 0xd800
#define surrog2_bits 0xdc00
#define byte4_base 0x808080f0
#define byte4_sr1_mask1 0x0700
#define byte4_sr1_mask2 0x00fc
#define byte4_sr1_mask3 0x0003
#define byte4_sr2_mask1 0x03c0
#define byte4_sr2_mask2 0x003f
#define surrogate_adjust (0x10000 >> 10)
static int little_endian = -1;
SQLULEN ucs2strlen(const SQLWCHAR *ucs2str)
{
SQLULEN len;
for (len = 0; ucs2str[len]; len++)
;
return len;
}
char *ucs2_to_utf8(const SQLWCHAR *ucs2str, SQLLEN ilen, SQLLEN *olen, BOOL lower_identifier)
{
char * utf8str;
/*mylog("ucs2_to_utf8 %p ilen=%d ", ucs2str, ilen);*/
if (!ucs2str)
{
*olen = SQL_NULL_DATA;
return NULL;
}
if (little_endian < 0)
{
int crt = 1;
little_endian = (0 != ((char *) &crt)[0]);
}
if (SQL_NTS == ilen)
ilen = ucs2strlen(ucs2str);
/*mylog(" newlen=%d", ilen);*/
utf8str = (char *) malloc(ilen * 4 + 1);
if (utf8str)
{
int i, len = 0;
UInt2 byte2code;
Int4 byte4code, surrd1, surrd2;
const SQLWCHAR *wstr;
for (i = 0, wstr = ucs2str; i < ilen; i++, wstr++)
{
if (!*wstr)
break;
else if (0 == (*wstr & 0xffffff80)) /* ASCII */
{
if (lower_identifier)
utf8str[len++] = (char) tolower(*wstr);
else
utf8str[len++] = (char) *wstr;
}
else if ((*wstr & byte3check) == 0)
{
byte2code = byte2_base |
((byte2_mask1 & *wstr) >> 6) |
((byte2_mask2 & *wstr) << 8);
if (little_endian)
memcpy(utf8str + len, (char *) &byte2code, sizeof(byte2code));
else
{
utf8str[len] = ((char *) &byte2code)[1];
utf8str[len + 1] = ((char *) &byte2code)[0];
}
len += sizeof(byte2code);
}
/* surrogate pair check for non ucs-2 code */
else if (surrog1_bits == (*wstr & surrog_check))
{
surrd1 = (*wstr & ~surrog_check) + surrogate_adjust;
wstr++;
i++;
surrd2 = (*wstr & ~surrog_check);
byte4code = byte4_base |
((byte4_sr1_mask1 & surrd1) >> 8) |
((byte4_sr1_mask2 & surrd1) << 6) |
((byte4_sr1_mask3 & surrd1) << 20) |
((byte4_sr2_mask1 & surrd2) << 10) |
((byte4_sr2_mask2 & surrd2) << 24);
if (little_endian)
memcpy(utf8str + len, (char *) &byte4code, sizeof(byte4code));
else
{
utf8str[len] = ((char *) &byte4code)[3];
utf8str[len + 1] = ((char *) &byte4code)[2];
utf8str[len + 2] = ((char *) &byte4code)[1];
utf8str[len + 3] = ((char *) &byte4code)[0];
}
len += sizeof(byte4code);
}
else
{
byte4code = byte3_base |
((byte3_mask1 & *wstr) >> 12) |
((byte3_mask2 & *wstr) << 2) |
((byte3_mask3 & *wstr) << 16);
if (little_endian)
memcpy(utf8str + len, (char *) &byte4code, 3);
else
{
utf8str[len] = ((char *) &byte4code)[3];
utf8str[len + 1] = ((char *) &byte4code)[2];
utf8str[len + 2] = ((char *) &byte4code)[1];
}
len += 3;
}
}
utf8str[len] = '\0';
if (olen)
*olen = len;
}
/*mylog(" olen=%d %s\n", *olen, utf8str ? utf8str : "");*/
return utf8str;
}
#define byte3_m1 0x0f
#define byte3_m2 0x3f
#define byte3_m3 0x3f
#define byte2_m1 0x1f
#define byte2_m2 0x3f
#define byte4_m1 0x07
#define byte4_m2 0x3f
#define byte4_m31 0x30
#define byte4_m32 0x0f
#define byte4_m4 0x3f
/*
* Convert a string from UTF-8 encoding to UCS-2.
*
* utf8str - input string in UTF-8
* ilen - length of input string in bytes (or SQL_NTS)
* lfconv - TRUE if line feeds (LF) should be converted to CR + LF
* ucs2str - output buffer
* bufcount - size of output buffer
* errcheck - if TRUE, check for invalidly encoded input characters
*
* Returns the number of SQLWCHARs copied to output buffer. If the output
* buffer is too small, the output is truncated. The output string is
* NULL-terminated, except when the output is truncated.
*/
SQLULEN
utf8_to_ucs2_lf(const char *utf8str, SQLLEN ilen, BOOL lfconv,
SQLWCHAR *ucs2str, SQLULEN bufcount, BOOL errcheck)
{
int i;
SQLULEN rtn, ocount, wcode;
const UCHAR *str;
/*mylog("utf8_to_ucs2 ilen=%d bufcount=%d", ilen, bufcount);*/
if (!utf8str)
return 0;
/*mylog(" string=%s\n", utf8str);*/
if (!bufcount)
ucs2str = NULL;
else if (!ucs2str)
bufcount = 0;
if (ilen < 0)
ilen = strlen(utf8str);
for (i = 0, ocount = 0, str = (SQLCHAR *) utf8str; i < ilen && *str;)
{
if ((*str & 0x80) == 0)
{
if (lfconv && PG_LINEFEED == *str &&
(i == 0 || PG_CARRIAGE_RETURN != str[-1]))
{
if (ocount < bufcount)
ucs2str[ocount] = PG_CARRIAGE_RETURN;
ocount++;
}
if (ocount < bufcount)
ucs2str[ocount] = *str;
ocount++;
i++;
str++;
}
else if (0xf8 == (*str & 0xf8)) /* more than 5 byte code */
{
ocount = (SQLULEN) -1;
goto cleanup;
}
else if (0xf0 == (*str & 0xf8)) /* 4 byte code */
{
if (errcheck)
{
if (i + 4 > ilen ||
0 == (str[1] & 0x80) ||
0 == (str[2] & 0x80) ||
0 == (str[3] & 0x80))
{
ocount = (SQLULEN) -1;
goto cleanup;
}
}
if (ocount < bufcount)
{
wcode = (surrog1_bits |
((((UInt4) *str) & byte4_m1) << 8) |
((((UInt4) str[1]) & byte4_m2) << 2) |
((((UInt4) str[2]) & byte4_m31) >> 4))
- surrogate_adjust;
ucs2str[ocount] = (SQLWCHAR) wcode;
}
ocount++;
if (ocount < bufcount)
{
wcode = surrog2_bits |
((((UInt4) str[2]) & byte4_m32) << 6) |
(((UInt4) str[3]) & byte4_m4);
ucs2str[ocount] = (SQLWCHAR) wcode;
}
ocount++;
i += 4;
str += 4;
}
else if (0xe0 == (*str & 0xf0)) /* 3 byte code */
{
if (errcheck)
{
if (i + 3 > ilen ||
0 == (str[1] & 0x80) ||
0 == (str[2] & 0x80))
{
ocount = (SQLULEN) -1;
goto cleanup;
}
}
if (ocount < bufcount)
{
wcode = ((((UInt4) *str) & byte3_m1) << 12) |
((((UInt4) str[1]) & byte3_m2) << 6) |
(((UInt4) str[2]) & byte3_m3);
ucs2str[ocount] = (SQLWCHAR) wcode;
}
ocount++;
i += 3;
str += 3;
}
else if (0xc0 == (*str & 0xe0)) /* 2 byte code */
{
if (errcheck)
{
if (i + 2 > ilen ||
0 == (str[1] & 0x80))
{
ocount = (SQLULEN) -1;
goto cleanup;
}
}
if (ocount < bufcount)
{
wcode = ((((UInt4) *str) & byte2_m1) << 6) |
(((UInt4) str[1]) & byte2_m2);
ucs2str[ocount] = (SQLWCHAR) wcode;
}
ocount++;
i += 2;
str += 2;
}
else
{
ocount = (SQLULEN) -1;
goto cleanup;
}
}
cleanup:
rtn = ocount;
if (ocount == (SQLULEN) -1)
{
if (!errcheck)
rtn = 0;
ocount = 0;
}
if (ocount < bufcount && ucs2str)
ucs2str[ocount] = 0;
/*mylog(" ocount=%d\n", ocount);*/
return rtn;
}
int msgtowstr(const char *enc, const char *inmsg, int inlen, LPWSTR outmsg, int buflen)
{
int outlen;
#ifdef WIN32
int wlen, cp = CP_ACP;
if (NULL != enc && 0 != atoi(enc))
cp = atoi(enc);
wlen = MultiByteToWideChar(cp, MB_PRECOMPOSED | MB_ERR_INVALID_CHARS,
inmsg, inlen, outmsg, buflen);
mylog(" out=%dchars\n", wlen);
outlen = wlen;
#else
#ifdef HAVE_MBSTOWCS_L
outlen = 0;
#else
outlen = 0;
#endif /* HAVE_MBSTOWCS_L */
#endif /* WIN32 */
if (outlen < buflen)
outmsg[outlen] = 0;
return outlen;
}
int wstrtomsg(const char *enc, const LPWSTR wstr, int wstrlen, char * outmsg, int buflen)
{
int outlen;
#ifdef WIN32
int len, cp = CP_ACP;
if (NULL != enc && 0 != atoi(enc))
cp = atoi(enc);
len = WideCharToMultiByte(cp, 0, wstr, (int) wstrlen, outmsg, buflen, NULL, NULL);
outlen = len;
#else
#ifdef HAVE_MBSTOWCS_L
outlen = 0;
#else
outlen = 0;
#endif /* HAVE_MBSTOWCS_L */
#endif /* WIN32 */
if (outlen < buflen)
outmsg[outlen] = 0;
return outlen;
}