FreeTDS API
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
tdsiconv.h
1 /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
2  * Copyright (C) 2002, 2003, 2004 Brian Bruns
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 #ifndef _tds_iconv_h_
21 #define _tds_iconv_h_
22 
23 /* $Id: tdsiconv.h,v 1.36 2007/03/12 13:28:50 freddy77 Exp $ */
24 
25 #if HAVE_ICONV
26 #include <iconv.h>
27 #else
28 /* Define iconv_t for src/replacements/iconv.c. */
29 #undef iconv_t
30 typedef void *iconv_t;
31 #endif /* HAVE_ICONV */
32 
33 #if HAVE_ERRNO_H
34 #include <errno.h>
35 #endif
36 
37 #if HAVE_WCHAR_H
38 #include <wchar.h>
39 #endif
40 
41 /* The following EILSEQ advice is borrowed verbatim from GNU iconv. */
42 /* Some systems, like SunOS 4, don't have EILSEQ. Some systems, like BSD/OS,
43  have EILSEQ in a different header. On these systems, define EILSEQ
44  ourselves. */
45 #ifndef EILSEQ
46 # define EILSEQ ENOENT
47 #endif
48 
49 #if HAVE_STDLIB_H
50 #include <stdlib.h>
51 #endif /* HAVE_STDLIB_H */
52 
53 #if defined(__GNUC__) && __GNUC__ >= 4
54 #pragma GCC visibility push(hidden)
55 #endif
56 
57 #ifdef __cplusplus
58 extern "C"
59 {
60 #endif
61 
62 #if ! HAVE_ICONV
63 
64  /* FYI, the first 4 entries look like this:
65  * {"ISO-8859-1", 1, 1}, -> 0
66  * {"US-ASCII", 1, 4}, -> 1
67  * {"UCS-2LE", 2, 2}, -> 2
68  * {"UCS-2BE", 2, 2}, -> 3
69  *
70  * These conversions are supplied by src/replacements/iconv.c for the sake of those who don't
71  * have or otherwise need an iconv.
72  */
73 enum ICONV_CD_VALUE
74 {
75  Like_to_Like = 0x100
76  , Latin1_ASCII = 0x01
77  , ASCII_Latin1 = 0x10
78 
79  , Latin1_UCS2LE = 0x02
80  , UCS2LE_Latin1 = 0x20
81  , ASCII_UCS2LE = 0x12
82  , UCS2LE_ASCII = 0x21
83 
84  , Latin1_UTF8 = 0x03
85  , UTF8_Latin1 = 0x30
86  , ASCII_UTF8 = 0x13
87  , UTF8_ASCII = 0x31
88  , UCS2LE_UTF8 = 0x23
89  , UTF8_UCS2LE = 0x32
90 
91 #ifdef DOS32X
92  , WinEE_UCS2LE = 0x42
93  , UCS2LE_WinEE = 0x24
94  , WinCYR_UCS2LE = 0x52
95  , UCS2LE_WinCYR = 0x25
96  , WinTUR_UCS2LE = 0x62
97  , UCS2LE_WinTUR = 0x26
98  , WinARA_UCS2LE = 0x72
99  , UCS2LE_WinARA = 0x27
100 #endif
101  /* these aren't needed
102  * , Latin1_UCS2BE = 0x03
103  * , UCS2BE_Latin1 = 0x30
104  */
105 };
106 
107 iconv_t tds_sys_iconv_open(const char *tocode, const char *fromcode);
108 size_t tds_sys_iconv(iconv_t cd, const char **inbuf, size_t * inbytesleft, char **outbuf, size_t * outbytesleft);
109 int tds_sys_iconv_close(iconv_t cd);
110 #else
111 #define tds_sys_iconv_open iconv_open
112 #define tds_sys_iconv iconv
113 #define tds_sys_iconv_close iconv_close
114 #endif /* !HAVE_ICONV */
115 
116 
117 typedef enum
118 { to_server, to_client } TDS_ICONV_DIRECTION;
119 
120 typedef struct _character_set_alias
121 {
122  const char *alias;
123  int canonic;
125 
126 typedef struct _tds_errno_message_flags {
127  unsigned int e2big:1;
128  unsigned int eilseq:1;
129  unsigned int einval:1;
131 
133 {
134  TDS_ENCODING client_charset;
135  TDS_ENCODING server_charset;
136 
137 #define TDS_ENCODING_INDIRECT 1
138 #define TDS_ENCODING_SWAPBYTE 2
139 #define TDS_ENCODING_MEMCPY 4
140  unsigned int flags;
141 
142  iconv_t to_wire; /* conversion from client charset to server's format */
143  iconv_t from_wire; /* conversion from server's format to client charset */
144 
145  iconv_t to_wire2; /* conversion from client charset to server's format - indirect */
146  iconv_t from_wire2; /* conversion from server's format to client charset - indirect */
147 
148  /*
149  * Suppress error messages that would otherwise be emitted by tds_iconv().
150  * Functions that process large buffers ask tds_iconv to convert it in "chunks".
151  * We don't want to emit spurious EILSEQ errors or multiple errors for one
152  * buffer. tds_iconv() checks this structure before emiting a message, and
153  * adds to it whenever it emits one. Callers that handle a particular situation themselves
154  * can prepopulate it.
155  */
156  TDS_ERRNO_MESSAGE_FLAGS suppress;
157 };
158 
159 /* We use ICONV_CONST for tds_iconv(), even if we don't have iconv() */
160 #ifndef ICONV_CONST
161 # define ICONV_CONST const
162 #endif
163 
164 size_t tds_iconv_fread(iconv_t cd, FILE * stream, size_t field_len, size_t term_len, char *outbuf, size_t * outbytesleft);
165 size_t tds_iconv(TDSSOCKET * tds, const TDSICONV * char_conv, TDS_ICONV_DIRECTION io,
166  const char **inbuf, size_t * inbytesleft, char **outbuf, size_t * outbytesleft);
167 const char *tds_canonical_charset_name(const char *charset_name);
168 const char *tds_sybase_charset_name(const char *charset_name);
169 
170 #ifdef __cplusplus
171 }
172 #endif
173 
174 #if defined(__GNUC__) && __GNUC__ >= 4
175 #pragma GCC visibility pop
176 #endif
177 
178 #endif /* _tds_iconv_h_ */
const char * tds_sybase_charset_name(const char *charset_name)
Determine the name Sybase uses for a character set, given a canonical iconv name. ...
Definition: iconv.c:1195
Definition: tdsiconv.h:120
Definition: tdsiconv.h:132
Information relevant to libiconv.
Definition: tds.h:927
const char * tds_canonical_charset_name(const char *charset_name)
Determine canonical iconv character set name.
Definition: iconv.c:1177
Hold information for a server connection.
Definition: tds.h:1276
size_t tds_iconv_fread(iconv_t cd, FILE *stream, size_t field_len, size_t term_len, char *outbuf, size_t *outbytesleft)
Read a data file, passing the data through iconv().
Definition: iconv.c:841
size_t tds_iconv(TDSSOCKET *tds, const TDSICONV *conv, TDS_ICONV_DIRECTION io, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
Wrapper around iconv(3).
Definition: iconv.c:606
Definition: tdsiconv.h:126
iconv_t tds_sys_iconv_open(const char *tocode, const char *fromcode)
Inputs are FreeTDS canonical names, no other.
Definition: iconv.c:67