Server.cs never did anything at all.
[versaplex.git] / vxodbc / win_unicode.cc
blobb3b1d3fa31dffc0e3c0fdfc12ff6f783660dd83f
1 /*
2 * Description: This module contains utf8 <-> ucs2 conversion routines
3 * under Windows
4 */
5 #include "psqlodbc.h"
6 #ifdef WIN32
7 /* gcc <malloc.h> has been replaced by <stdlib.h> */
8 #include <malloc.h>
9 #ifdef _DEBUG
10 #ifndef _MEMORY_DEBUG_
11 #include <stdlib.h>
12 #define _CRTDBG_MAP_ALLOC
13 #include <crtdbg.h>
14 #endif /* _MEMORY_DEBUG_ */
15 #endif /* _DEBUG */
16 #else
17 #include <stdlib.h>
18 #endif /* WIN32 */
19 #include <string.h>
21 #define byte3check 0xfffff800
22 #define byte2_base 0x80c0
23 #define byte2_mask1 0x07c0
24 #define byte2_mask2 0x003f
25 #define byte3_base 0x8080e0
26 #define byte3_mask1 0xf000
27 #define byte3_mask2 0x0fc0
28 #define byte3_mask3 0x003f
30 #include <ctype.h>
31 #ifndef WIN32
32 #ifdef HAVE_ISWASCII
33 #include <wctype.h>
34 #else
35 #include <wchar.h>
36 int iswascii(wchar_t c)
38 return isascii(wctob(c));
40 #endif /* HAVE_ISWASCII */
41 #endif /* WIN32 */
43 SQLULEN ucs2strlen(const SQLWCHAR * ucs2str)
45 SQLULEN len;
46 for (len = 0; ucs2str[len]; len++)
48 return len;
50 char *ucs2_to_utf8(const SQLWCHAR * ucs2str, SQLLEN ilen, SQLLEN * olen,
51 BOOL lower_identifier)
53 char *utf8str;
54 /*mylog("ucs2_to_utf8 %p ilen=%d ", ucs2str, ilen);*/
56 if (!ucs2str)
58 *olen = SQL_NULL_DATA;
59 return NULL;
61 if (SQL_NTS == ilen)
62 ilen = ucs2strlen(ucs2str);
63 /*mylog(" newlen=%d", ilen);*/
64 utf8str = (char *) malloc(ilen * 3 + 1);
65 if (utf8str)
67 int i, len = 0;
68 UInt2 byte2code;
69 Int4 byte4code;
70 const SQLWCHAR *wstr;
72 for (i = 0, wstr = ucs2str; i < ilen; i++, wstr++)
74 if (!*wstr)
75 break;
76 else if (0 == (*wstr & 0xffffff80)) /* ASCII */
78 if (lower_identifier)
79 utf8str[len++] = (char) tolower(*wstr);
80 else
81 utf8str[len++] = (char) *wstr;
82 } else if ((*wstr & byte3check) == 0)
84 byte2code = byte2_base |
85 ((byte2_mask1 & *wstr) >> 6) |
86 ((byte2_mask2 & *wstr) << 8);
87 memcpy(utf8str + len, (char *) &byte2code,
88 sizeof(byte2code));
89 len += sizeof(byte2code);
90 } else
92 byte4code = byte3_base |
93 ((byte3_mask1 & *wstr) >> 12) |
94 ((byte3_mask2 & *wstr) << 2) |
95 ((byte3_mask3 & *wstr) << 16);
96 memcpy(utf8str + len, (char *) &byte4code, 3);
97 len += 3;
100 utf8str[len] = '\0';
101 if (olen)
102 *olen = len;
104 /*mylog(" olen=%d %s\n", *olen, utf8str ? utf8str : "");*/
105 return utf8str;
108 #define byte3_m1 0x0f
109 #define byte3_m2 0x3f
110 #define byte3_m3 0x3f
111 #define byte2_m1 0x1f
112 #define byte2_m2 0x3f
113 SQLULEN utf8_to_ucs2_lf(const char *utf8str, SQLLEN ilen, BOOL lfconv,
114 SQLWCHAR * ucs2str, SQLULEN bufcount)
116 int i;
117 SQLULEN ocount, wcode;
118 const UCHAR *str;
120 /*mylog("utf8_to_ucs2 ilen=%d bufcount=%d", ilen, bufcount);*/
121 if (!utf8str)
122 return 0;
123 /*mylog(" string=%s\n", utf8str);*/
124 if (!bufcount)
125 ucs2str = NULL;
126 else if (!ucs2str)
127 bufcount = 0;
128 if (ilen < 0)
129 ilen = strlen(utf8str);
130 for (i = 0, ocount = 0, str = (const UCHAR *)utf8str; i < ilen && *str;)
132 /* if (iswascii(*str)) */
133 if (isascii(*str))
135 if (lfconv && PG_LINEFEED == *str &&
136 (i == 0 || PG_CARRIAGE_RETURN != str[-1]))
138 if (ocount < bufcount)
139 ucs2str[ocount] = PG_CARRIAGE_RETURN;
140 ocount++;
142 if (ocount < bufcount)
143 ucs2str[ocount] = *str;
144 ocount++;
145 i++;
146 str++;
147 } else if (0xe0 == (*str & 0xe0)) /* 3 byte code */
149 if (ocount < bufcount)
151 wcode = ((((UInt4) * str) & byte3_m1) << 12) |
152 ((((UInt4) str[1]) & byte3_m2) << 6) |
153 (((UInt4) str[2]) & byte3_m3);
154 ucs2str[ocount] = (SQLWCHAR) wcode;
156 ocount++;
157 i += 3;
158 str += 3;
159 } else
161 if (ocount < bufcount)
163 wcode = ((((UInt4) * str) & byte2_m1) << 6) |
164 (((UInt4) str[1]) & byte2_m2);
165 ucs2str[ocount] = (SQLWCHAR) wcode;
167 ocount++;
168 i += 2;
169 str += 2;
172 if (ocount < bufcount && ucs2str)
173 ucs2str[ocount] = 0;
174 /*mylog(" ocount=%d\n", ocount);*/
175 return ocount;