Line data Source code
1 : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
2 : * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 Brian Bruns
3 : * Copyright (C) 2010 Frediano Ziglio
4 : *
5 : * This library is free software; you can redistribute it and/or
6 : * modify it under the terms of the GNU Library General Public
7 : * License as published by the Free Software Foundation; either
8 : * version 2 of the License, or (at your option) any later version.
9 : *
10 : * This library is distributed in the hope that it will be useful,
11 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 : * Library General Public License for more details.
14 : *
15 : * You should have received a copy of the GNU Library General Public
16 : * License along with this library; if not, write to the
17 : * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 : * Boston, MA 02111-1307, USA.
19 : */
20 :
21 : /**
22 : * \file
23 : * \brief Handle character conversions to/from server
24 : */
25 :
26 : #include <config.h>
27 :
28 : #include <stdarg.h>
29 : #include <stdio.h>
30 : #include <assert.h>
31 :
32 : #if HAVE_STRING_H
33 : #include <string.h>
34 : #endif /* HAVE_STRING_H */
35 : #if HAVE_ERRNO_H
36 : #include <errno.h>
37 : #endif
38 :
39 : #include <freetds/tds.h>
40 : #include <freetds/iconv.h>
41 : #include <freetds/bool.h>
42 : #include <freetds/bytes.h>
43 : #if HAVE_ICONV
44 : #include <iconv.h>
45 : #endif
46 :
47 : #define CHARSIZE(charset) ( ((charset)->min_bytes_per_char == (charset)->max_bytes_per_char )? \
48 : (charset)->min_bytes_per_char : 0 )
49 :
50 :
51 : static int collate2charset(TDSCONNECTION * conn, const TDS_UCHAR collate[5]);
52 : static size_t skip_one_input_sequence(iconv_t cd, const TDS_ENCODING * charset, const char **input, size_t * input_size);
53 : static int tds_iconv_info_init(TDSICONV * char_conv, int client_canonic, int server_canonic);
54 : static bool tds_iconv_init(void);
55 : static void _iconv_close(iconv_t * cd);
56 : static void tds_iconv_info_close(TDSICONV * char_conv);
57 :
58 :
59 : /**
60 : * \ingroup libtds
61 : * \defgroup conv Charset conversion
62 : * Convert between different charsets.
63 : */
64 :
65 : #define TDS_ICONV_ENCODING_TABLES
66 : #include <freetds/encodings.h>
67 :
68 : /* this will contain real iconv names */
69 : static const char *iconv_names[TDS_VECTOR_SIZE(canonic_charsets)];
70 : static bool iconv_initialized = false;
71 : static const char *ucs2name;
72 :
73 : enum
74 : { POS_ISO1, POS_UTF8, POS_UCS2LE, POS_UCS2BE };
75 :
76 : static const struct {
77 : uint32_t len;
78 : /* this field must be aligned at least to 2 bytes */
79 : char data[12];
80 : } test_strings[4] = {
81 : /* same string in required charsets */
82 : { 4, "Ao\xD3\xE5" },
83 : { 6, "Ao\xC3\x93\xC3\xA5" },
84 : { 8, "A\x00o\x000\xD3\x00\xE5\x00" },
85 : { 8, "\x00" "A\x00o\x000\xD3\x00\xE5" },
86 : };
87 :
88 : /**
89 : * Initialize charset searching for UTF-8, UCS-2 and ISO8859-1
90 : */
91 : static bool
92 2004 : tds_iconv_init(void)
93 : {
94 : int i;
95 : iconv_t cd;
96 :
97 : /* first entries should be constants */
98 : assert(strcmp(canonic_charsets[POS_ISO1].name, "ISO-8859-1") == 0);
99 : assert(strcmp(canonic_charsets[POS_UTF8].name, "UTF-8") == 0);
100 : assert(strcmp(canonic_charsets[POS_UCS2LE].name, "UCS-2LE") == 0);
101 : assert(strcmp(canonic_charsets[POS_UCS2BE].name, "UCS-2BE") == 0);
102 :
103 : /* fast tests for GNU-iconv */
104 2004 : cd = tds_sys_iconv_open("ISO-8859-1", "UTF-8");
105 2004 : if (cd != (iconv_t) -1) {
106 2004 : iconv_names[POS_ISO1] = "ISO-8859-1";
107 2004 : iconv_names[POS_UTF8] = "UTF-8";
108 2004 : tds_sys_iconv_close(cd);
109 : } else {
110 :
111 : /* search names for ISO8859-1 and UTF-8 */
112 0 : for (i = 0; iconv_aliases[i].alias; ++i) {
113 : int j;
114 :
115 0 : if (iconv_aliases[i].canonic != POS_ISO1)
116 0 : continue;
117 0 : for (j = 0; iconv_aliases[j].alias; ++j) {
118 0 : if (iconv_aliases[j].canonic != POS_UTF8)
119 0 : continue;
120 :
121 0 : cd = tds_sys_iconv_open(iconv_aliases[i].alias, iconv_aliases[j].alias);
122 0 : if (cd != (iconv_t) -1) {
123 0 : iconv_names[POS_ISO1] = iconv_aliases[i].alias;
124 0 : iconv_names[POS_UTF8] = iconv_aliases[j].alias;
125 0 : tds_sys_iconv_close(cd);
126 0 : break;
127 : }
128 : }
129 0 : if (iconv_names[POS_ISO1])
130 : break;
131 : }
132 : /* required characters not found !!! */
133 0 : if (!iconv_names[POS_ISO1]) {
134 0 : tdsdump_log(TDS_DBG_ERROR, "iconv name for ISO-8859-1 not found\n");
135 : return false;
136 : }
137 : }
138 :
139 : /* now search for UCS-2 */
140 2004 : cd = tds_sys_iconv_open(iconv_names[POS_ISO1], "UCS-2LE");
141 2004 : if (cd != (iconv_t) -1) {
142 2004 : iconv_names[POS_UCS2LE] = "UCS-2LE";
143 2004 : tds_sys_iconv_close(cd);
144 : }
145 2004 : cd = tds_sys_iconv_open(iconv_names[POS_ISO1], "UCS-2BE");
146 2004 : if (cd != (iconv_t) -1) {
147 2004 : iconv_names[POS_UCS2BE] = "UCS-2BE";
148 2004 : tds_sys_iconv_close(cd);
149 : }
150 :
151 : /* long search needed ?? */
152 2004 : if (!iconv_names[POS_UCS2LE] || !iconv_names[POS_UCS2BE]) {
153 0 : for (i = 0; iconv_aliases[i].alias; ++i) {
154 0 : if (strncmp(canonic_charsets[iconv_aliases[i].canonic].name, "UCS-2", 5) != 0)
155 0 : continue;
156 :
157 0 : cd = tds_sys_iconv_open(iconv_aliases[i].alias, iconv_names[POS_ISO1]);
158 0 : if (cd != (iconv_t) -1) {
159 : char ib[1];
160 : char ob[4];
161 : size_t il, ol;
162 : ICONV_CONST char *pib;
163 : char *pob;
164 0 : int byte_sequence = 0;
165 :
166 : /* try to convert 'A' and check result */
167 0 : ib[0] = 0x41;
168 0 : pib = ib;
169 0 : pob = ob;
170 0 : il = 1;
171 0 : ol = 4;
172 0 : ob[0] = ob[1] = 0;
173 0 : if (tds_sys_iconv(cd, &pib, &il, &pob, &ol) != (size_t) - 1) {
174 : /* byte order sequence ?? */
175 0 : if (ol == 0) {
176 0 : ob[0] = ob[2];
177 0 : byte_sequence = 1;
178 : /* TODO save somewhere */
179 : }
180 :
181 : /* save name without sequence (if present) */
182 0 : if (ob[0])
183 0 : il = POS_UCS2LE;
184 : else
185 0 : il = POS_UCS2BE;
186 0 : if (!iconv_names[il] || !byte_sequence)
187 0 : iconv_names[il] = iconv_aliases[i].alias;
188 : }
189 0 : tds_sys_iconv_close(cd);
190 : }
191 : }
192 : }
193 : /* we need a UCS-2 (big endian or little endian) */
194 2004 : if (!iconv_names[POS_UCS2LE] && !iconv_names[POS_UCS2BE]) {
195 0 : tdsdump_log(TDS_DBG_ERROR, "iconv name for UCS-2 not found\n");
196 : return false;
197 : }
198 :
199 2004 : ucs2name = iconv_names[POS_UCS2LE] ? iconv_names[POS_UCS2LE] : iconv_names[POS_UCS2BE];
200 :
201 10020 : for (i = 0; i < 4; ++i)
202 8032 : tdsdump_log(TDS_DBG_INFO1, "local name for %s is %s\n", canonic_charsets[i].name,
203 16 : iconv_names[i] ? iconv_names[i] : "(null)");
204 :
205 : /* base conversions checks */
206 32064 : for (i = 0; i < 4 * 4; ++i) {
207 32064 : const int from = i / 4;
208 32064 : const int to = i % 4;
209 : char ob[16];
210 : size_t il, ol;
211 : ICONV_CONST char *pib;
212 : char *pob;
213 : size_t res;
214 :
215 32064 : if (!iconv_names[from] || !iconv_names[to])
216 0 : continue;
217 32064 : cd = tds_sys_iconv_open(iconv_names[to], iconv_names[from]);
218 32064 : if (cd == (iconv_t) -1) {
219 0 : tdsdump_log(TDS_DBG_ERROR, "iconv_open(%s, %s) failed\n", iconv_names[to], iconv_names[from]);
220 0 : return false;
221 : }
222 :
223 32064 : pib = (ICONV_CONST char *) test_strings[from].data;
224 32064 : il = test_strings[from].len;
225 32064 : pob = ob;
226 32064 : ol = sizeof(ob);
227 32064 : res = tds_sys_iconv(cd, &pib, &il, &pob, &ol);
228 32064 : tds_sys_iconv_close(cd);
229 :
230 32064 : if (res != 0
231 32064 : || sizeof(ob) - ol != test_strings[to].len
232 32064 : || memcmp(ob, test_strings[to].data, test_strings[to].len) != 0) {
233 0 : tdsdump_log(TDS_DBG_ERROR, "iconv(%s, %s) failed res %d\n", iconv_names[to], iconv_names[from], (int) res);
234 0 : tdsdump_log(TDS_DBG_ERROR, "len %d\n", (int) (sizeof(ob) - ol));
235 : return false;
236 : }
237 : }
238 :
239 : /* success (it should always occurs) */
240 : return true;
241 : }
242 :
243 : /**
244 : * Get iconv name given canonic
245 : */
246 : static const char *
247 3550 : tds_set_iconv_name(int charset)
248 : {
249 : int i;
250 : iconv_t cd;
251 : const char *name;
252 :
253 3550 : assert(iconv_initialized);
254 :
255 : /* try using canonic name and UTF-8 and UCS2 */
256 3550 : name = canonic_charsets[charset].name;
257 3550 : cd = tds_sys_iconv_open(iconv_names[POS_UTF8], name);
258 3550 : if (cd != (iconv_t) -1)
259 : goto found;
260 0 : cd = tds_sys_iconv_open(ucs2name, name);
261 0 : if (cd != (iconv_t) -1)
262 : goto found;
263 :
264 : /* try all alternatives */
265 0 : for (i = 0; iconv_aliases[i].alias; ++i) {
266 0 : if (iconv_aliases[i].canonic != charset)
267 0 : continue;
268 :
269 0 : name = iconv_aliases[i].alias;
270 0 : cd = tds_sys_iconv_open(iconv_names[POS_UTF8], name);
271 0 : if (cd != (iconv_t) -1)
272 : goto found;
273 0 : cd = tds_sys_iconv_open(ucs2name, name);
274 0 : if (cd != (iconv_t) -1)
275 : goto found;
276 : }
277 :
278 : /* charset not found, pretend it's ISO 8859-1 */
279 0 : iconv_names[charset] = canonic_charsets[POS_ISO1].name;
280 0 : return NULL;
281 :
282 3550 : found:
283 3550 : iconv_names[charset] = name;
284 3550 : tds_sys_iconv_close(cd);
285 3550 : return name;
286 : }
287 :
288 : static void
289 : tds_iconv_reset(TDSICONV *conv)
290 : {
291 : /*
292 : * (min|max)_bytes_per_char can be used to divide
293 : * so init to safe values
294 : */
295 24258 : conv->to.charset.min_bytes_per_char = 1;
296 24258 : conv->to.charset.max_bytes_per_char = 1;
297 24258 : conv->from.charset.min_bytes_per_char = 1;
298 24258 : conv->from.charset.max_bytes_per_char = 1;
299 :
300 24258 : conv->to.charset.name = conv->from.charset.name = "";
301 24258 : conv->to.charset.canonic = conv->from.charset.canonic = 0;
302 24258 : conv->to.cd = (iconv_t) -1;
303 24258 : conv->from.cd = (iconv_t) -1;
304 : }
305 :
306 : /**
307 : * Allocate iconv stuff
308 : * \return 0 for success
309 : */
310 : int
311 4637 : tds_iconv_alloc(TDSCONNECTION * conn)
312 : {
313 : int i;
314 : TDSICONV *char_conv;
315 :
316 4637 : assert(!conn->char_convs);
317 4637 : if (!(conn->char_convs = tds_new(TDSICONV *, initial_char_conv_count + 1)))
318 : return 1;
319 4637 : char_conv = tds_new0(TDSICONV, initial_char_conv_count);
320 4637 : if (!char_conv) {
321 0 : TDS_ZERO_FREE(conn->char_convs);
322 0 : return 1;
323 : }
324 4637 : conn->char_conv_count = initial_char_conv_count + 1;
325 :
326 13911 : for (i = 0; i < initial_char_conv_count; ++i) {
327 9274 : conn->char_convs[i] = &char_conv[i];
328 18548 : tds_iconv_reset(&char_conv[i]);
329 : }
330 :
331 : /* chardata is just a pointer to another iconv info */
332 4637 : conn->char_convs[initial_char_conv_count] = conn->char_convs[client2server_chardata];
333 :
334 4637 : return 0;
335 : }
336 :
337 : /**
338 : * \addtogroup conv
339 : * @{
340 : * Set up the initial iconv conversion descriptors.
341 : * When the socket is allocated, three TDSICONV structures are attached to iconv.
342 : * They have fixed meanings:
343 : * \li 0. Client <-> UCS-2 (client2ucs2)
344 : * \li 1. Client <-> server single-byte charset (client2server_chardata)
345 : *
346 : * Other designs that use less data are possible, but these three conversion needs are
347 : * very often needed. By reserving them, we avoid searching the array for our most common purposes.
348 : *
349 : * To solve different iconv names and portability problems FreeTDS maintains
350 : * a list of aliases each charset.
351 : *
352 : * First we discover the names of our minimum required charsets (UTF-8, ISO8859-1 and UCS2).
353 : * Later, as and when it's needed, we try to discover others.
354 : *
355 : * There is one list of canonic names (GNU iconv names) and two sets of aliases
356 : * (one for other iconv implementations and another for Sybase). For every
357 : * canonic charset name we cache the iconv name found during discovery.
358 : */
359 : TDSRET
360 4472 : tds_iconv_open(TDSCONNECTION * conn, const char *charset, int use_utf16)
361 : {
362 : static const char UCS_2LE[] = "UCS-2LE";
363 : int canonic;
364 4472 : int canonic_charset = tds_canonical_charset(charset);
365 4472 : int canonic_env_charset = conn->env.charset ? tds_canonical_charset(conn->env.charset) : -1;
366 : int fOK;
367 :
368 4472 : TDS_ENCODING *client = &conn->char_convs[client2ucs2]->from.charset;
369 4472 : TDS_ENCODING *server = &conn->char_convs[client2ucs2]->to.charset;
370 :
371 4472 : tdsdump_log(TDS_DBG_FUNC, "tds_iconv_open(%p, %s, %d)\n", conn, charset, use_utf16);
372 :
373 : /* TDS 5.0 support only UTF-16 encodings */
374 4472 : if (IS_TDS50(conn))
375 720 : use_utf16 = true;
376 :
377 : /* initialize */
378 4472 : if (!iconv_initialized) {
379 2004 : if (!tds_iconv_init()) {
380 0 : tdsdump_log(TDS_DBG_ERROR, "error: tds_iconv_init() failed; "
381 : "try using GNU libiconv library\n");
382 : return TDS_FAIL;
383 : }
384 2004 : iconv_initialized = true;
385 : }
386 :
387 : /*
388 : * Client <-> UCS-2 (client2ucs2)
389 : */
390 4472 : tdsdump_log(TDS_DBG_FUNC, "setting up conversions for client charset \"%s\"\n", charset);
391 :
392 4472 : tdsdump_log(TDS_DBG_FUNC, "preparing iconv for \"%s\" <-> \"%s\" conversion\n", charset, UCS_2LE);
393 :
394 4472 : fOK = 0;
395 4472 : if (use_utf16) {
396 3706 : canonic = TDS_CHARSET_UTF_16LE;
397 3706 : fOK = tds_iconv_info_init(conn->char_convs[client2ucs2], canonic_charset, canonic);
398 : }
399 3706 : if (!fOK) {
400 766 : canonic = TDS_CHARSET_UCS_2LE;
401 766 : fOK = tds_iconv_info_init(conn->char_convs[client2ucs2], canonic_charset, canonic);
402 : }
403 4472 : if (!fOK)
404 : return TDS_FAIL;
405 :
406 : /*
407 : * How many UTF-8 bytes we need is a function of what the input character set is.
408 : * TODO This could definitely be more sophisticated, but it deals with the common case.
409 : */
410 4472 : if (client->min_bytes_per_char == 1 && client->max_bytes_per_char == 4 && server->max_bytes_per_char == 1) {
411 : /* ie client is UTF-8 and server is ISO-8859-1 or variant. */
412 0 : client->max_bytes_per_char = 3;
413 : }
414 :
415 : /*
416 : * Client <-> server single-byte charset
417 : * TODO: the server hasn't reported its charset yet, so this logic can't work here.
418 : * not sure what to do about that yet.
419 : */
420 4472 : conn->char_convs[client2server_chardata]->flags = TDS_ENCODING_MEMCPY;
421 4472 : if (canonic_env_charset >= 0) {
422 0 : tdsdump_log(TDS_DBG_FUNC, "preparing iconv for \"%s\" <-> \"%s\" conversion\n", charset, conn->env.charset);
423 0 : fOK = tds_iconv_info_init(conn->char_convs[client2server_chardata], canonic_charset, canonic_env_charset);
424 0 : if (!fOK)
425 : return TDS_FAIL;
426 : } else {
427 4472 : conn->char_convs[client2server_chardata]->from.charset = canonic_charsets[canonic_charset];
428 4472 : conn->char_convs[client2server_chardata]->to.charset = canonic_charsets[canonic_charset];
429 : }
430 :
431 4472 : tdsdump_log(TDS_DBG_FUNC, "tds_iconv_open: done\n");
432 : return TDS_SUCCESS;
433 : }
434 :
435 : /**
436 : * Open iconv descriptors to convert between character sets (both directions).
437 : * 1. Look up the canonical names of the character sets.
438 : * 2. Look up their widths.
439 : * 3. Ask iconv to open a conversion descriptor.
440 : * 4. Fail if any of the above offer any resistance.
441 : * \remarks The charset names written to \a iconv will be the canonical names,
442 : * not necessarily the names passed in.
443 : */
444 : static int
445 10208 : tds_iconv_info_init(TDSICONV * char_conv, int client_canonical, int server_canonical)
446 : {
447 10208 : TDS_ENCODING *client = &char_conv->from.charset;
448 10208 : TDS_ENCODING *server = &char_conv->to.charset;
449 :
450 10208 : assert(char_conv->to.cd == (iconv_t) -1);
451 10208 : assert(char_conv->from.cd == (iconv_t) -1);
452 :
453 10208 : if (client_canonical < 0) {
454 0 : tdsdump_log(TDS_DBG_FUNC, "tds_iconv_info_init: client charset name \"%d\" invalid\n", client_canonical);
455 : return 0;
456 : }
457 :
458 10208 : if (server_canonical < 0) {
459 0 : tdsdump_log(TDS_DBG_FUNC, "tds_iconv_info_init: server charset name \"%d\" invalid\n", server_canonical);
460 : return 0;
461 : }
462 :
463 10208 : *client = canonic_charsets[client_canonical];
464 10208 : *server = canonic_charsets[server_canonical];
465 :
466 : /* special case, same charset, no conversion */
467 10208 : if (client_canonical == server_canonical) {
468 104 : char_conv->to.cd = (iconv_t) -1;
469 104 : char_conv->from.cd = (iconv_t) -1;
470 104 : char_conv->flags = TDS_ENCODING_MEMCPY;
471 104 : return 1;
472 : }
473 :
474 10104 : char_conv->flags = 0;
475 :
476 : /* get iconv names */
477 10104 : if (!iconv_names[client_canonical]) {
478 0 : if (!tds_set_iconv_name(client_canonical)) {
479 0 : tdsdump_log(TDS_DBG_FUNC, "Charset %d not supported by iconv, using \"%s\" instead\n",
480 : client_canonical, iconv_names[client_canonical]);
481 : }
482 : }
483 :
484 10104 : if (!iconv_names[server_canonical]) {
485 3550 : if (!tds_set_iconv_name(server_canonical)) {
486 0 : tdsdump_log(TDS_DBG_FUNC, "Charset %d not supported by iconv, using \"%s\" instead\n",
487 : server_canonical, iconv_names[server_canonical]);
488 : }
489 : }
490 :
491 10104 : char_conv->to.cd = tds_sys_iconv_open(iconv_names[server_canonical], iconv_names[client_canonical]);
492 10104 : if (char_conv->to.cd == (iconv_t) -1) {
493 0 : tdsdump_log(TDS_DBG_FUNC, "tds_iconv_info_init: cannot convert \"%s\"->\"%s\"\n", client->name, server->name);
494 : }
495 :
496 10104 : char_conv->from.cd = tds_sys_iconv_open(iconv_names[client_canonical], iconv_names[server_canonical]);
497 10104 : if (char_conv->from.cd == (iconv_t) -1) {
498 0 : tdsdump_log(TDS_DBG_FUNC, "tds_iconv_info_init: cannot convert \"%s\"->\"%s\"\n", server->name, client->name);
499 : }
500 :
501 : /* TODO, do some optimizations like UCS2 -> UTF8 min,max = 2,2 (UCS2) and 1,4 (UTF8) */
502 :
503 : /* tdsdump_log(TDS_DBG_FUNC, "tds_iconv_info_init: converting \"%s\"->\"%s\"\n", client->name, server->name); */
504 :
505 : return 1;
506 : }
507 :
508 :
509 : static void
510 : _iconv_close(iconv_t * cd)
511 : {
512 : static const iconv_t invalid = (iconv_t) -1;
513 :
514 39054 : if (*cd != invalid) {
515 20088 : tds_sys_iconv_close(*cd);
516 20088 : *cd = invalid;
517 : }
518 : }
519 :
520 : static void
521 19527 : tds_iconv_info_close(TDSICONV * char_conv)
522 : {
523 39054 : _iconv_close(&char_conv->to.cd);
524 39054 : _iconv_close(&char_conv->from.cd);
525 19527 : }
526 :
527 : void
528 0 : tds_iconv_close(TDSCONNECTION * conn)
529 : {
530 : int i;
531 :
532 19527 : for (i = 0; i < conn->char_conv_count; ++i)
533 19527 : tds_iconv_info_close(conn->char_convs[i]);
534 0 : }
535 :
536 : #define CHUNK_ALLOC 4
537 :
538 : void
539 4607 : tds_iconv_free(TDSCONNECTION * conn)
540 : {
541 : int i;
542 :
543 4607 : if (!conn->char_convs)
544 : return;
545 4607 : tds_iconv_close(conn);
546 :
547 4607 : free(conn->char_convs[0]);
548 8323 : for (i = initial_char_conv_count + 1; i < conn->char_conv_count; i += CHUNK_ALLOC)
549 3716 : free(conn->char_convs[i]);
550 4607 : TDS_ZERO_FREE(conn->char_convs);
551 4607 : conn->char_conv_count = 0;
552 : }
553 :
554 : static void
555 : tds_iconv_err(TDSSOCKET *tds, int err)
556 : {
557 6351 : if (tds)
558 4141 : tdserror(tds_get_ctx(tds), tds, err, 0);
559 : }
560 :
561 : /**
562 : * Wrapper around iconv(3). Same parameters, with slightly different behavior.
563 : * \param tds state information for the socket and the TDS protocol
564 : * \param io Enumerated value indicating whether the data are being sent to or received from the server.
565 : * \param conv information about the encodings involved, including the iconv(3) conversion descriptors.
566 : * \param inbuf address of pointer to the input buffer of data to be converted.
567 : * \param inbytesleft address of count of bytes in \a inbuf.
568 : * \param outbuf address of pointer to the output buffer.
569 : * \param outbytesleft address of count of bytes in \a outbuf.
570 : * \retval number of irreversible conversions performed. -1 on error, see iconv(3) documentation for
571 : * a description of the possible values of \e errno.
572 : * \remarks Unlike iconv(3), none of the arguments can be nor point to NULL. Like iconv(3), all pointers will
573 : * be updated. Success is signified by a nonnegative return code and \a *inbytesleft == 0.
574 : * If the conversion descriptor in \a iconv is -1 or NULL, \a inbuf is copied to \a outbuf,
575 : * and all parameters updated accordingly.
576 : *
577 : * If a character in \a inbuf cannot be converted because no such cbaracter exists in the
578 : * \a outbuf character set, we emit messages similar to the ones Sybase emits when it fails such a conversion.
579 : * The message varies depending on the direction of the data.
580 : * On a read error, we emit Msg 2403, Severity 16 (EX_INFO):
581 : * "WARNING! Some character(s) could not be converted into client's character set.
582 : * Unconverted bytes were changed to question marks ('?')."
583 : * On a write error we emit Msg 2402, Severity 16 (EX_USER):
584 : * "Error converting client characters into server's character set. Some character(s) could not be converted."
585 : * and return an error code. Client libraries relying on this routine should reflect an error back to the application.
586 : *
587 : * \todo Check for variable multibyte non-UTF-8 input character set.
588 : * \todo Use more robust error message generation.
589 : * \todo For reads, cope with \a outbuf encodings that don't have the equivalent of an ASCII '?'.
590 : * \todo Support alternative to '?' for the replacement character.
591 : */
592 : size_t
593 1019726 : tds_iconv(TDSSOCKET * tds, TDSICONV * conv, TDS_ICONV_DIRECTION io,
594 : const char **inbuf, size_t * inbytesleft, char **outbuf, size_t * outbytesleft)
595 : {
596 : static const iconv_t invalid = (iconv_t) -1;
597 1019726 : TDSICONVDIR *from = NULL;
598 1019726 : TDSICONVDIR *to = NULL;
599 :
600 1019726 : iconv_t error_cd = invalid;
601 :
602 1019726 : char quest_mark[] = "?"; /* best to leave non-const; implementations vary */
603 : ICONV_CONST char *pquest_mark;
604 : size_t lquest_mark;
605 : size_t irreversible;
606 : size_t one_character;
607 1019726 : bool eilseq_raised = false;
608 : int conv_errno;
609 : /* cast away const-ness */
610 1019726 : TDS_ERRNO_MESSAGE_FLAGS *suppress = (TDS_ERRNO_MESSAGE_FLAGS*) &conv->suppress;
611 :
612 1019726 : assert(inbuf && inbytesleft && outbuf && outbytesleft);
613 :
614 : /* if empty there's nothing to return.
615 : * This fix case with some iconv implementation that does
616 : * not handle *inbuf == NULL and *inbytesleft == 0 as
617 : * empty strings
618 : */
619 1019726 : if (*inbytesleft == 0)
620 : return 0;
621 :
622 1019374 : switch (io) {
623 169930 : case to_server:
624 169930 : from = &conv->from;
625 169930 : to = &conv->to;
626 169930 : break;
627 849444 : case to_client:
628 849444 : from = &conv->to;
629 849444 : to = &conv->from;
630 849444 : break;
631 0 : default:
632 0 : tdsdump_log(TDS_DBG_FUNC, "tds_iconv: unable to determine if %d means in or out. \n", io);
633 0 : assert(io == to_server || io == to_client);
634 : break;
635 : }
636 :
637 : /* silly case, memcpy */
638 1019374 : if (conv->flags & TDS_ENCODING_MEMCPY || to->cd == invalid) {
639 159011 : size_t len = *inbytesleft < *outbytesleft ? *inbytesleft : *outbytesleft;
640 :
641 159011 : memcpy(*outbuf, *inbuf, len);
642 159011 : conv_errno = *inbytesleft > *outbytesleft ? E2BIG : 0;
643 159011 : *inbytesleft -= len;
644 159011 : *outbytesleft -= len;
645 159011 : *inbuf += len;
646 159011 : *outbuf += len;
647 159011 : errno = conv_errno;
648 159011 : return conv_errno ? (size_t) -1 : 0;
649 : }
650 :
651 : /*
652 : * Call iconv() as many times as necessary, until we reach the end of input or exhaust output.
653 : */
654 : for (;;) {
655 1495309 : conv_errno = 0;
656 1495309 : irreversible = tds_sys_iconv(to->cd, (ICONV_CONST char **) inbuf, inbytesleft, outbuf, outbytesleft);
657 :
658 : /* iconv success, return */
659 1495309 : if (irreversible != (size_t) - 1) {
660 1262156 : if (irreversible > 0)
661 0 : eilseq_raised = true;
662 :
663 : /* here we detect end of conversion and try to reset shift state */
664 1262156 : if (inbuf) {
665 : /*
666 : * if inbuf or *inbuf is NULL iconv reset the shift state.
667 : * Note that setting inbytesleft to NULL can cause core so don't do it!
668 : */
669 631078 : inbuf = NULL;
670 631078 : continue;
671 : }
672 : break;
673 : }
674 :
675 : /* save errno, other function could change its value */
676 233153 : conv_errno = errno;
677 :
678 233153 : if (conv_errno == EILSEQ)
679 11170 : eilseq_raised = true;
680 :
681 233153 : if (!eilseq_raised || io != to_client || !inbuf)
682 : break;
683 : /*
684 : * Invalid input sequence encountered reading from server.
685 : * Skip one input sequence, adjusting pointers.
686 : */
687 3876 : one_character = skip_one_input_sequence(to->cd, &from->charset, inbuf, inbytesleft);
688 :
689 3876 : if (!one_character)
690 : break;
691 :
692 : /*
693 : * To replace invalid input with '?', we have to convert a UTF-8 '?' into the output character set.
694 : * In unimaginably weird circumstances, this might be impossible.
695 : * We use UTF-8 instead of ASCII because some implementations
696 : * do not convert singlebyte <-> singlebyte.
697 : */
698 3876 : if (error_cd == invalid) {
699 1938 : error_cd = tds_sys_iconv_open(to->charset.name, iconv_names[POS_UTF8]);
700 1938 : if (error_cd == invalid) {
701 : break; /* what to do? */
702 : }
703 : }
704 :
705 3876 : lquest_mark = 1;
706 3876 : pquest_mark = quest_mark;
707 :
708 3876 : irreversible = tds_sys_iconv(error_cd, &pquest_mark, &lquest_mark, outbuf, outbytesleft);
709 :
710 3876 : if (irreversible == (size_t) - 1)
711 : break;
712 :
713 3868 : if (!*inbytesleft)
714 : break;
715 : }
716 :
717 860363 : if (eilseq_raised && !suppress->eilseq) {
718 : /* invalid multibyte input sequence encountered */
719 6351 : if (io == to_client) {
720 1930 : if (irreversible == (size_t) - 1) {
721 : tds_iconv_err(tds, TDSEICONV2BIG);
722 : } else {
723 1930 : tds_iconv_err(tds, TDSEICONVI);
724 1930 : conv_errno = 0;
725 : }
726 : } else {
727 : tds_iconv_err(tds, TDSEICONVO);
728 : }
729 6351 : suppress->eilseq = 1;
730 : }
731 :
732 858433 : switch (conv_errno) {
733 2970 : case EINVAL: /* incomplete multibyte sequence is encountered */
734 2970 : if (suppress->einval)
735 : break;
736 : /* in chunk conversion this can mean we end a chunk inside a character */
737 0 : tds_iconv_err(tds, TDSEICONVAVAIL);
738 0 : suppress->einval = 1;
739 0 : break;
740 219013 : case E2BIG: /* output buffer has no more room */
741 219013 : if (suppress->e2big)
742 : break;
743 0 : tds_iconv_err(tds, TDSEICONVIU);
744 0 : suppress->e2big = 1;
745 0 : break;
746 : default:
747 : break;
748 : }
749 :
750 1082346 : if (error_cd != invalid) {
751 1938 : tds_sys_iconv_close(error_cd);
752 : }
753 :
754 860363 : errno = conv_errno;
755 860363 : return irreversible;
756 : }
757 :
758 : /**
759 : * Get a iconv info structure, allocate and initialize if needed
760 : */
761 : TDSICONV *
762 28610 : tds_iconv_get_info(TDSCONNECTION * conn, int canonic_client, int canonic_server)
763 : {
764 : TDSICONV *info;
765 : int i;
766 :
767 : /* search a charset from already allocated charsets */
768 87196 : for (i = conn->char_conv_count; --i >= initial_char_conv_count;)
769 52850 : if (canonic_client == conn->char_convs[i]->from.charset.canonic
770 41280 : && canonic_server == conn->char_convs[i]->to.charset.canonic)
771 : return conn->char_convs[i];
772 :
773 : /* allocate a new iconv structure */
774 5736 : if (conn->char_conv_count % CHUNK_ALLOC == ((initial_char_conv_count + 1) % CHUNK_ALLOC)) {
775 : TDSICONV **p;
776 : TDSICONV *infos;
777 :
778 3746 : infos = tds_new(TDSICONV, CHUNK_ALLOC);
779 3746 : if (!infos)
780 : return NULL;
781 3746 : p = (TDSICONV **) realloc(conn->char_convs, sizeof(TDSICONV *) * (conn->char_conv_count + CHUNK_ALLOC));
782 3746 : if (!p) {
783 0 : free(infos);
784 0 : return NULL;
785 : }
786 3746 : conn->char_convs = p;
787 3746 : memset(infos, 0, sizeof(TDSICONV) * CHUNK_ALLOC);
788 18730 : for (i = 0; i < CHUNK_ALLOC; ++i) {
789 14984 : conn->char_convs[i + conn->char_conv_count] = &infos[i];
790 29968 : tds_iconv_reset(&infos[i]);
791 : }
792 : }
793 5736 : info = conn->char_convs[conn->char_conv_count++];
794 :
795 : /* init */
796 5736 : if (tds_iconv_info_init(info, canonic_client, canonic_server))
797 : return info;
798 :
799 0 : tds_iconv_info_close(info);
800 0 : --conn->char_conv_count;
801 0 : return NULL;
802 : }
803 :
804 : TDSICONV *
805 20 : tds_iconv_get(TDSCONNECTION * conn, const char *client_charset, const char *server_charset)
806 : {
807 20 : int canonic_client_charset_num = tds_canonical_charset(client_charset);
808 20 : int canonic_server_charset_num = tds_canonical_charset(server_charset);
809 :
810 20 : if (canonic_client_charset_num < 0) {
811 0 : tdsdump_log(TDS_DBG_FUNC, "tds_iconv_get: what is charset \"%s\"?\n", client_charset);
812 : return NULL;
813 : }
814 20 : if (canonic_server_charset_num < 0) {
815 0 : tdsdump_log(TDS_DBG_FUNC, "tds_iconv_get: what is charset \"%s\"?\n", server_charset);
816 : return NULL;
817 : }
818 :
819 20 : return tds_iconv_get_info(conn, canonic_client_charset_num, canonic_server_charset_num);
820 : }
821 :
822 : /* change singlebyte conversions according to server */
823 : static void
824 9968 : tds_srv_charset_changed_num(TDSCONNECTION * conn, int canonic_charset_num)
825 : {
826 9968 : TDSICONV *char_conv = conn->char_convs[client2server_chardata];
827 :
828 9968 : if (IS_TDS7_PLUS(conn) && canonic_charset_num == TDS_CHARSET_ISO_8859_1)
829 0 : canonic_charset_num = TDS_CHARSET_CP1252;
830 :
831 9968 : tdsdump_log(TDS_DBG_FUNC, "setting server single-byte charset to \"%s\"\n", canonic_charsets[canonic_charset_num].name);
832 :
833 9968 : if (canonic_charset_num == char_conv->to.charset.canonic)
834 : return;
835 :
836 : /* find and set conversion */
837 3178 : char_conv = tds_iconv_get_info(conn, conn->char_convs[client2ucs2]->from.charset.canonic, canonic_charset_num);
838 3178 : if (char_conv)
839 3178 : conn->char_convs[client2server_chardata] = char_conv;
840 : }
841 :
842 : void
843 4406 : tds_srv_charset_changed(TDSCONNECTION * conn, const char *charset)
844 : {
845 4406 : int n = tds_canonical_charset(charset);
846 :
847 : /* ignore request to change to unknown charset */
848 4406 : if (n < 0) {
849 0 : tdsdump_log(TDS_DBG_FUNC, "tds_srv_charset_changed: what is charset \"%s\"?\n", charset);
850 : return;
851 : }
852 :
853 4406 : tds_srv_charset_changed_num(conn, n);
854 : }
855 :
856 : /* change singlebyte conversions according to server */
857 : void
858 5562 : tds7_srv_charset_changed(TDSCONNECTION * conn, TDS_UCHAR collation[5])
859 : {
860 5562 : tds_srv_charset_changed_num(conn, collate2charset(conn, collation));
861 5562 : }
862 :
863 : /**
864 : * Move the input sequence pointer to the next valid position.
865 : * Used when an input character cannot be converted.
866 : * \returns number of bytes to skip.
867 : */
868 : /* FIXME possible buffer reading overflow ?? */
869 : static size_t
870 3876 : skip_one_input_sequence(iconv_t cd, const TDS_ENCODING * charset, const char **input, size_t * input_size)
871 : {
872 3876 : unsigned charsize = CHARSIZE(charset);
873 : char ib[16];
874 : char ob[16];
875 : ICONV_CONST char *pib;
876 : char *pob;
877 : size_t il, ol, l;
878 : iconv_t cd2;
879 :
880 :
881 : /* usually fixed size and UTF-8 do not have state, so do not reset it */
882 0 : if (charsize)
883 : goto skip_charsize;
884 :
885 3876 : if (0 == strcmp(charset->name, "UTF-8")) {
886 : /*
887 : * Deal with UTF-8.
888 : * bytes | bits | representation
889 : * 1 | 7 | 0vvvvvvv
890 : * 2 | 11 | 110vvvvv 10vvvvvv
891 : * 3 | 16 | 1110vvvv 10vvvvvv 10vvvvvv
892 : * 4 | 21 | 11110vvv 10vvvvvv 10vvvvvv 10vvvvvv
893 : */
894 3860 : int c = **input;
895 :
896 3860 : c = c & (c >> 1);
897 : do {
898 7720 : ++charsize;
899 7720 : } while ((c <<= 1) & 0x80);
900 : goto skip_charsize;
901 : }
902 :
903 : /* handle state encoding */
904 :
905 : /* extract state from iconv */
906 16 : pob = ib;
907 16 : ol = sizeof(ib);
908 16 : tds_sys_iconv(cd, NULL, NULL, &pob, &ol);
909 :
910 : /* init destination conversion */
911 : /* TODO use largest fixed size for this platform */
912 16 : cd2 = tds_sys_iconv_open("UCS-4", charset->name);
913 16 : if (cd2 == (iconv_t) -1)
914 : return 0;
915 :
916 : /* add part of input */
917 16 : il = ol;
918 16 : if (il > *input_size)
919 0 : il = *input_size;
920 16 : l = sizeof(ib) - ol;
921 16 : memcpy(ib + l, *input, il);
922 16 : il += l;
923 :
924 : /* translate a single character */
925 16 : pib = ib;
926 16 : pob = ob;
927 : /* TODO use size of largest fixed charset */
928 16 : ol = 4;
929 16 : tds_sys_iconv(cd2, &pib, &il, &pob, &ol);
930 :
931 : /* adjust input */
932 16 : l = (pib - ib) - l;
933 16 : *input += l;
934 16 : *input_size -= l;
935 :
936 : /* extract state */
937 16 : pob = ib;
938 16 : ol = sizeof(ib);
939 16 : tds_sys_iconv(cd, NULL, NULL, &pob, &ol);
940 :
941 : /* set input state */
942 16 : pib = ib;
943 16 : il = sizeof(ib) - ol;
944 16 : pob = ob;
945 16 : ol = sizeof(ob);
946 16 : tds_sys_iconv(cd, &pib, &il, &pob, &ol);
947 :
948 16 : tds_sys_iconv_close(cd2);
949 :
950 16 : if (l != 0)
951 : return l;
952 :
953 : /* last blindly attempt, skip minimum bytes */
954 8 : charsize = charset->min_bytes_per_char;
955 :
956 : /* fall through */
957 :
958 3868 : skip_charsize:
959 3868 : if (charsize > *input_size)
960 : return 0;
961 3868 : *input += charsize;
962 3868 : *input_size -= charsize;
963 3868 : return charsize;
964 : }
965 :
966 : #include <freetds/charset_lookup.h>
967 :
968 : /**
969 : * Determine canonical iconv character set.
970 : * \returns canonical position, or -1 if lookup failed.
971 : * \remarks Returned name can be used in bytes_per_char(), above.
972 : */
973 : int
974 13910 : tds_canonical_charset(const char *charset_name)
975 : {
976 13910 : const struct charset_alias *c = charset_lookup(charset_name, strlen(charset_name));
977 13910 : return c ? c->canonic : -1;
978 : }
979 :
980 : /**
981 : * Determine canonical iconv character set name.
982 : * \returns canonical name, or NULL if lookup failed.
983 : * \remarks Returned name can be used in bytes_per_char(), above.
984 : */
985 : const char *
986 3826 : tds_canonical_charset_name(const char *charset_name)
987 : {
988 : int res;
989 :
990 : /* get numeric pos */
991 3826 : res = tds_canonical_charset(charset_name);
992 3826 : if (res >= 0)
993 3826 : return canonic_charsets[res].name;
994 :
995 : return charset_name; /* hope for the best */
996 : }
997 :
998 : static int
999 27505 : collate2charset(TDSCONNECTION * conn, const TDS_UCHAR collate[5])
1000 : {
1001 27505 : int cp = 0;
1002 27505 : const int sql_collate = collate[4];
1003 : /* extract 16 bit of LCID (it's 20 bits but higher 4 are just variations) */
1004 27505 : const int lcid = TDS_GET_UA2LE(collate);
1005 :
1006 : /* starting with bit 20 (little endian, so 3rd byte bit 4) there are 8 bits:
1007 : * fIgnoreCase fIgnoreAccent fIgnoreKana fIgnoreWidth fBinary fBinary2 fUTF8 FRESERVEDBIT
1008 : * so fUTF8 is on the 4th byte bit 2 */
1009 27505 : if ((collate[3] & 0x4) != 0 && IS_TDS74_PLUS(conn))
1010 : return TDS_CHARSET_UTF_8;
1011 :
1012 : /*
1013 : * The table from the MSQLServer reference "Windows Collation Designators"
1014 : * and from " NLS Information for Microsoft Windows XP".
1015 : *
1016 : * See also https://go.microsoft.com/fwlink/?LinkId=119987 [MSDN-SQLCollation]
1017 : */
1018 :
1019 27505 : switch (sql_collate) {
1020 : case 30: /* SQL_Latin1_General_CP437_BIN */
1021 : case 31: /* SQL_Latin1_General_CP437_CS_AS */
1022 : case 32: /* SQL_Latin1_General_CP437_CI_AS */
1023 : case 33: /* SQL_Latin1_General_Pref_CP437_CI_AS */
1024 : case 34: /* SQL_Latin1_General_CP437_CI_AI */
1025 : return TDS_CHARSET_CP437;
1026 8 : case 40: /* SQL_Latin1_General_CP850_BIN */
1027 : case 41: /* SQL_Latin1_General_CP850_CS_AS */
1028 : case 42: /* SQL_Latin1_General_CP850_CI_AS */
1029 : case 43: /* SQL_Latin1_General_Pref_CP850_CI_AS */
1030 : case 44: /* SQL_Latin1_General_CP850_CI_AI */
1031 : case 49: /* SQL_1xCompat_CP850_CI_AS */
1032 8 : return TDS_CHARSET_CP850;
1033 26997 : case 51: /* SQL_Latin1_General_Cp1_CS_AS_KI_WI */
1034 : case 52: /* SQL_Latin1_General_Cp1_CI_AS_KI_WI */
1035 : case 53: /* SQL_Latin1_General_Pref_Cp1_CI_AS_KI_WI */
1036 : case 54: /* SQL_Latin1_General_Cp1_CI_AI_KI_WI */
1037 26997 : return TDS_CHARSET_CP1252;
1038 0 : case 55: /* SQL_AltDiction_CP850_CS_AS */
1039 : case 56: /* SQL_AltDiction_Pref_CP850_CI_AS */
1040 : case 57: /* SQL_AltDiction_CP850_CI_AI */
1041 : case 58: /* SQL_Scandinavian_Pref_CP850_CI_AS */
1042 : case 59: /* SQL_Scandinavian_CP850_CS_AS */
1043 : case 60: /* SQL_Scandinavian_CP850_CI_AS */
1044 : case 61: /* SQL_AltDiction_CP850_CI_AS */
1045 0 : return TDS_CHARSET_CP850;
1046 0 : case 80: /* SQL_Latin1_General_1250_BIN */
1047 : case 81: /* SQL_Latin1_General_CP1250_CS_AS */
1048 : case 82: /* SQL_Latin1_General_CP1250_CI_AS */
1049 : case 83: /* SQL_Czech_Cp1250_CS_AS_KI_WI */
1050 : case 84: /* SQL_Czech_Cp1250_CI_AS_KI_WI */
1051 : case 85: /* SQL_Hungarian_Cp1250_CS_AS_KI_WI */
1052 : case 86: /* SQL_Hungarian_Cp1250_CI_AS_KI_WI */
1053 : case 87: /* SQL_Polish_Cp1250_CS_AS_KI_WI */
1054 : case 88: /* SQL_Polish_Cp1250_CI_AS_KI_WI */
1055 : case 89: /* SQL_Romanian_Cp1250_CS_AS_KI_WI */
1056 : case 90: /* SQL_Romanian_Cp1250_CI_AS_KI_WI */
1057 : case 91: /* SQL_Croatian_Cp1250_CS_AS_KI_WI */
1058 : case 92: /* SQL_Croatian_Cp1250_CI_AS_KI_WI */
1059 : case 93: /* SQL_Slovak_Cp1250_CS_AS_KI_WI */
1060 : case 94: /* SQL_Slovak_Cp1250_CI_AS_KI_WI */
1061 : case 95: /* SQL_Slovenian_Cp1250_CS_AS_KI_WI */
1062 : case 96: /* SQL_Slovenian_Cp1250_CI_AS_KI_WI */
1063 0 : return TDS_CHARSET_CP1250;
1064 0 : case 104: /* SQL_Latin1_General_1251_BIN */
1065 : case 105: /* SQL_Latin1_General_CP1251_CS_AS */
1066 : case 106: /* SQL_Latin1_General_CP1251_CI_AS */
1067 : case 107: /* SQL_Ukrainian_Cp1251_CS_AS_KI_WI */
1068 : case 108: /* SQL_Ukrainian_Cp1251_CI_AS_KI_WI */
1069 0 : return TDS_CHARSET_CP1251;
1070 0 : case 112: /* SQL_Latin1_General_1253_BIN */
1071 : case 113: /* SQL_Latin1_General_CP1253_CS_AS */
1072 : case 114: /* SQL_Latin1_General_CP1253_CI_AS */
1073 : case 120: /* SQL_MixDiction_CP1253_CS_AS */
1074 : case 121: /* SQL_AltDiction_CP1253_CS_AS */
1075 : case 122: /* SQL_AltDiction2_CP1253_CS_AS */
1076 : case 124: /* SQL_Latin1_General_CP1253_CI_AI */
1077 0 : return TDS_CHARSET_CP1253;
1078 0 : case 128: /* SQL_Latin1_General_1254_BIN */
1079 : case 129: /* SQL_Latin1_General_Cp1254_CS_AS_KI_WI */
1080 : case 130: /* SQL_Latin1_General_Cp1254_CI_AS_KI_WI */
1081 0 : return TDS_CHARSET_CP1254;
1082 0 : case 136: /* SQL_Latin1_General_1255_BIN */
1083 : case 137: /* SQL_Latin1_General_CP1255_CS_AS */
1084 : case 138: /* SQL_Latin1_General_CP1255_CI_AS */
1085 0 : return TDS_CHARSET_CP1255;
1086 0 : case 144: /* SQL_Latin1_General_1256_BIN */
1087 : case 145: /* SQL_Latin1_General_CP1256_CS_AS */
1088 : case 146: /* SQL_Latin1_General_CP1256_CI_AS */
1089 0 : return TDS_CHARSET_CP1256;
1090 0 : case 152: /* SQL_Latin1_General_1257_BIN */
1091 : case 153: /* SQL_Latin1_General_CP1257_CS_AS */
1092 : case 154: /* SQL_Latin1_General_CP1257_CI_AS */
1093 : case 155: /* SQL_Estonian_Cp1257_CS_AS_KI_WI */
1094 : case 156: /* SQL_Estonian_Cp1257_CI_AS_KI_WI */
1095 : case 157: /* SQL_Latvian_Cp1257_CS_AS_KI_WI */
1096 : case 158: /* SQL_Latvian_Cp1257_CI_AS_KI_WI */
1097 : case 159: /* SQL_Lithuanian_Cp1257_CS_AS_KI_WI */
1098 : case 160: /* SQL_Lithuanian_Cp1257_CI_AS_KI_WI */
1099 0 : return TDS_CHARSET_CP1257;
1100 0 : case 183: /* SQL_Danish_Pref_CP1_CI_AS */
1101 : case 184: /* SQL_SwedishPhone_Pref_CP1_CI_AS */
1102 : case 185: /* SQL_SwedishStd_Pref_CP1_CI_AS */
1103 : case 186: /* SQL_Icelandic_Pref_CP1_CI_AS */
1104 0 : return TDS_CHARSET_CP1252;
1105 : }
1106 :
1107 500 : switch (lcid) {
1108 : case 0x405:
1109 : case 0x40e: /* 0x1040e */
1110 : case 0x415:
1111 : case 0x418:
1112 : case 0x41a:
1113 : case 0x41b:
1114 : case 0x41c:
1115 : case 0x424:
1116 : case 0x442:
1117 : case 0x81a:
1118 : case 0x104e: /* ?? */
1119 : case 0x141a:
1120 : cp = TDS_CHARSET_CP1250;
1121 : break;
1122 0 : case 0x402:
1123 : case 0x419:
1124 : case 0x422:
1125 : case 0x423:
1126 : case 0x42f:
1127 : case 0x43f:
1128 : case 0x440:
1129 : case 0x444:
1130 : case 0x450:
1131 : case 0x82c:
1132 : case 0x843:
1133 : case 0xc1a:
1134 : case 0x46d:
1135 : case 0x201a:
1136 : case 0x485:
1137 0 : cp = TDS_CHARSET_CP1251;
1138 0 : break;
1139 476 : case 0x1007:
1140 : case 0x1009:
1141 : case 0x100a:
1142 : case 0x100c:
1143 : case 0x1407:
1144 : case 0x1409:
1145 : case 0x140a:
1146 : case 0x140c:
1147 : case 0x1809:
1148 : case 0x180a:
1149 : case 0x180c:
1150 : case 0x1c09:
1151 : case 0x1c0a:
1152 : case 0x2009:
1153 : case 0x200a:
1154 : case 0x2409:
1155 : case 0x240a:
1156 : case 0x2809:
1157 : case 0x280a:
1158 : case 0x2c09:
1159 : case 0x2c0a:
1160 : case 0x3009:
1161 : case 0x300a:
1162 : case 0x3409:
1163 : case 0x340a:
1164 : case 0x380a:
1165 : case 0x3c0a:
1166 : case 0x400a:
1167 : case 0x403:
1168 : case 0x406:
1169 : case 0x417:
1170 : case 0x42e:
1171 : case 0x43b:
1172 : case 0x452:
1173 : case 0x462:
1174 : case 0x47a:
1175 : case 0x47c:
1176 : case 0x47e:
1177 : case 0x483:
1178 : case 0x407: /* 0x10407 */
1179 : case 0x409:
1180 : case 0x40a:
1181 : case 0x40b:
1182 : case 0x40c:
1183 : case 0x40f:
1184 : case 0x410:
1185 : case 0x413:
1186 : case 0x414:
1187 : case 0x416:
1188 : case 0x41d:
1189 : case 0x421:
1190 : case 0x42d:
1191 : case 0x436:
1192 : case 0x437: /* 0x10437 */
1193 : case 0x438:
1194 : /*case 0x439: ??? Unicode only */
1195 : case 0x43e:
1196 : case 0x440a:
1197 : case 0x441:
1198 : case 0x456:
1199 : case 0x480a:
1200 : case 0x4c0a:
1201 : case 0x500a:
1202 : case 0x807:
1203 : case 0x809:
1204 : case 0x80a:
1205 : case 0x80c:
1206 : case 0x810:
1207 : case 0x813:
1208 : case 0x814:
1209 : case 0x816:
1210 : case 0x81d:
1211 : case 0x83b:
1212 : case 0x83e:
1213 : case 0x85f:
1214 : case 0xc07:
1215 : case 0xc09:
1216 : case 0xc0a:
1217 : case 0xc0c:
1218 476 : cp = TDS_CHARSET_CP1252;
1219 476 : break;
1220 0 : case 0x408:
1221 0 : cp = TDS_CHARSET_CP1253;
1222 0 : break;
1223 0 : case 0x41f:
1224 : case 0x42c:
1225 : case 0x443:
1226 0 : cp = TDS_CHARSET_CP1254;
1227 0 : break;
1228 8 : case 0x40d:
1229 8 : cp = TDS_CHARSET_CP1255;
1230 8 : break;
1231 0 : case 0x1001:
1232 : case 0x1401:
1233 : case 0x1801:
1234 : case 0x1c01:
1235 : case 0x2001:
1236 : case 0x2401:
1237 : case 0x2801:
1238 : case 0x2c01:
1239 : case 0x3001:
1240 : case 0x3401:
1241 : case 0x3801:
1242 : case 0x3c01:
1243 : case 0x4001:
1244 : case 0x401:
1245 : case 0x480:
1246 : case 0x420:
1247 : case 0x429:
1248 : case 0x48c:
1249 : case 0x801:
1250 : case 0xc01:
1251 0 : cp = TDS_CHARSET_CP1256;
1252 0 : break;
1253 0 : case 0x425:
1254 : case 0x426:
1255 : case 0x427:
1256 : case 0x827: /* ?? */
1257 0 : cp = TDS_CHARSET_CP1257;
1258 0 : break;
1259 0 : case 0x42a:
1260 0 : cp = TDS_CHARSET_CP1258;
1261 0 : break;
1262 0 : case 0x41e:
1263 0 : cp = TDS_CHARSET_CP874;
1264 0 : break;
1265 0 : case 0x411: /* 0x10411 */
1266 0 : cp = TDS_CHARSET_CP932;
1267 0 : break;
1268 16 : case 0x1004:
1269 : case 0x804: /* 0x20804 */
1270 16 : cp = TDS_CHARSET_GB18030;
1271 16 : break;
1272 0 : case 0x412: /* 0x10412 */
1273 0 : cp = TDS_CHARSET_CP949;
1274 0 : break;
1275 0 : case 0x1404:
1276 : case 0x404: /* 0x30404 */
1277 : case 0xc04:
1278 0 : cp = TDS_CHARSET_CP950;
1279 0 : break;
1280 0 : default:
1281 0 : cp = TDS_CHARSET_CP1252;
1282 : }
1283 :
1284 : return cp;
1285 : }
1286 :
1287 : /**
1288 : * Get iconv information from a LCID (to support different column encoding under MSSQL2K)
1289 : */
1290 : TDSICONV *
1291 21943 : tds_iconv_from_collate(TDSCONNECTION * conn, const TDS_UCHAR collate[5])
1292 : {
1293 21943 : int canonic_charset = collate2charset(conn, collate);
1294 :
1295 : /* same as client (usually this is true, so this improve performance) ? */
1296 21943 : if (conn->char_convs[client2server_chardata]->to.charset.canonic == canonic_charset)
1297 : return conn->char_convs[client2server_chardata];
1298 :
1299 3112 : return tds_iconv_get_info(conn, conn->char_convs[client2ucs2]->from.charset.canonic, canonic_charset);
1300 : }
1301 :
1302 : /**
1303 : * Returns a collation name for the given charset.
1304 : * It's used more to specify the encoding, the collation is usually case
1305 : * insensitive and accent sensitive.
1306 : */
1307 : const char *
1308 588 : tds_canonical_collate_name(int canonic_charset)
1309 : {
1310 : /*
1311 : * The name returned is chosen for maximum compatibility,
1312 : * most are supported by MSSQL 2000.
1313 : */
1314 588 : switch (canonic_charset) {
1315 : case TDS_CHARSET_CP437:
1316 : return "SQL_Latin1_General_CP437_CI_AS";
1317 4 : case TDS_CHARSET_CP850:
1318 4 : return "SQL_Latin1_General_CP850_CI_AS";
1319 0 : case TDS_CHARSET_CP874:
1320 0 : return "Thai_CI_AS";
1321 0 : case TDS_CHARSET_CP932:
1322 0 : return "Japanese_CI_AS";
1323 0 : case TDS_CHARSET_CP949:
1324 0 : return "Korean_Wansung_CI_AS";
1325 0 : case TDS_CHARSET_CP950:
1326 0 : return "Chinese_Taiwan_Bopomofo_CI_AS";
1327 0 : case TDS_CHARSET_CP1250:
1328 0 : return "SQL_Latin1_General_CP1250_CI_AS";
1329 0 : case TDS_CHARSET_CP1251:
1330 0 : return "SQL_Latin1_General_CP1251_CI_AS";
1331 : case TDS_CHARSET_CP1252:
1332 : break;
1333 0 : case TDS_CHARSET_CP1253:
1334 0 : return "SQL_Latin1_General_CP1253_CI_AS";
1335 0 : case TDS_CHARSET_CP1254:
1336 0 : return "SQL_Latin1_General_CP1254_CI_AS";
1337 0 : case TDS_CHARSET_CP1255:
1338 0 : return "SQL_Latin1_General_CP1255_CI_AS";
1339 0 : case TDS_CHARSET_CP1256:
1340 0 : return "SQL_Latin1_General_CP1256_CI_AS";
1341 0 : case TDS_CHARSET_CP1257:
1342 0 : return "SQL_Latin1_General_CP1257_CI_AS";
1343 0 : case TDS_CHARSET_CP1258:
1344 0 : return "Vietnamese_CI_AS";
1345 0 : case TDS_CHARSET_GB18030:
1346 0 : return "Chinese_PRC_CI_AS";
1347 0 : case TDS_CHARSET_UTF_8:
1348 0 : return "Chinese_PRC_90_CI_AS_SC_UTF8";
1349 : }
1350 584 : return "SQL_Latin1_General_CP1_CI_AS";
1351 : }
1352 :
1353 : /** @} */
|