Line data Source code
1 : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
2 : * Copyright (C) 2008 Ziglio Frediano
3 : *
4 : * This library is free software; you can redistribute it and/or
5 : * modify it under the terms of the GNU Library General Public
6 : * License as published by the Free Software Foundation; either
7 : * version 2 of the License, or (at your option) any later version.
8 : *
9 : * This library is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 : * Library General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU Library General Public
15 : * License along with this library; if not, write to the
16 : * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 : * Boston, MA 02111-1307, USA.
18 : */
19 :
20 : #include <config.h>
21 :
22 : #include <stdarg.h>
23 : #include <stdio.h>
24 :
25 : #if HAVE_STDLIB_H
26 : #include <stdlib.h>
27 : #endif /* HAVE_STDLIB_H */
28 :
29 : #include <freetds/odbc.h>
30 :
31 : #include <freetds/iconv.h>
32 : #include <freetds/encodings.h>
33 :
34 : /* Compile-time check that sizes are defined correctly
35 : * (otherwise the #if in odbc.h can malfunction)
36 : */
37 : TDS_COMPILE_CHECK(sizecheck_sqlwchar, SIZEOF_SQLWCHAR == sizeof(SQLWCHAR));
38 : TDS_COMPILE_CHECK(sizecheck_wchar_t, SIZEOF_WCHAR_T == sizeof(wchar_t));
39 :
40 : #ifndef sqlwcslen
41 22648 : size_t sqlwcslen(const SQLWCHAR * s)
42 : {
43 22648 : const SQLWCHAR *p = s;
44 :
45 858151 : while (*p)
46 812855 : ++p;
47 22648 : return p - s;
48 : }
49 : #endif
50 :
51 : #ifdef ENABLE_ODBC_WIDE
52 : #if SIZEOF_SQLWCHAR != SIZEOF_WCHAR_T
53 : /**
54 : * Convert a SQLWCHAR string into a wchar_t
55 : * Used only for debugging purpose
56 : * \param str string to convert
57 : * \param bufs linked list of buffer
58 : * \return string converted
59 : */
60 0 : const wchar_t *sqlwstr(const SQLWCHAR *str, SQLWSTRBUF **bufs)
61 : {
62 : wchar_t *dst, *dst_end;
63 0 : const SQLWCHAR *src = str;
64 : SQLWSTRBUF *buf;
65 :
66 0 : if (!str)
67 : return NULL;
68 :
69 : /* allocate buffer for string, we do not care for memory errors */
70 0 : buf = tds_new0(SQLWSTRBUF, 1);
71 0 : if (!buf)
72 : return NULL;
73 0 : buf->next = *bufs;
74 0 : *bufs = buf;
75 :
76 0 : dst = buf->buf;
77 0 : dst_end = dst + (TDS_VECTOR_SIZE(buf->buf) - 1);
78 :
79 0 : for (; *src && dst < dst_end; *dst++ = *src++)
80 0 : continue;
81 0 : *dst = L'\0';
82 :
83 0 : return buf->buf;
84 : }
85 :
86 0 : void sqlwstr_free(SQLWSTRBUF *bufs)
87 : {
88 0 : while (bufs) {
89 0 : SQLWSTRBUF *buf = bufs;
90 0 : bufs = buf->next;
91 0 : free(buf);
92 : }
93 0 : }
94 : #endif
95 : #endif
96 :
97 : #if SIZEOF_SQLWCHAR == 2
98 : # if WORDS_BIGENDIAN
99 : # define ODBC_WIDE_CANONIC TDS_CHARSET_UCS_2BE
100 : # define ODBC_WIDE_CANONIC_UTF TDS_CHARSET_UTF_16BE
101 : # else
102 : # define ODBC_WIDE_CANONIC TDS_CHARSET_UCS_2LE
103 : # define ODBC_WIDE_CANONIC_UTF TDS_CHARSET_UTF_16LE
104 : # endif
105 : #elif SIZEOF_SQLWCHAR == 4
106 : # if WORDS_BIGENDIAN
107 : # define ODBC_WIDE_CANONIC TDS_CHARSET_UCS_4BE
108 : # else
109 : # define ODBC_WIDE_CANONIC TDS_CHARSET_UCS_4LE
110 : # endif
111 : #else
112 : #error SIZEOF_SQLWCHAR not supported !!
113 : #endif
114 :
115 1902 : int odbc_get_wide_canonic(TDSCONNECTION *conn)
116 : {
117 : #if SIZEOF_SQLWCHAR == 2
118 1902 : if (conn->char_convs[client2ucs2]->to.charset.canonic == TDS_CHARSET_UTF_16LE)
119 : return ODBC_WIDE_CANONIC_UTF;
120 : #endif
121 0 : return ODBC_WIDE_CANONIC;
122 : }
|