Send message for WSAAsyncSelect sockets directly from the server,
[wine] / memory / codepage.c
1 /*
2  * Code page functions
3  *
4  * Copyright 2000 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <assert.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "winbase.h"
27 #include "winerror.h"
28 #include "winnls.h"
29 #include "wine/unicode.h"
30 #include "wine/debug.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(string);
33
34 /* current code pages */
35 static const union cptable *ansi_cptable;
36 static const union cptable *oem_cptable;
37 static const union cptable *mac_cptable;
38
39 /* retrieve a code page table from the locale info */
40 static const union cptable *get_locale_cp( LCID lcid, LCTYPE type )
41 {
42     const union cptable *table = NULL;
43     char buf[32];
44
45     if (GetLocaleInfoA( lcid, type, buf, sizeof(buf) )) table = cp_get_table( atoi(buf) );
46     return table;
47 }
48
49 /* setup default codepage info before we can get at the locale stuff */
50 static void init_codepages(void)
51 {
52     ansi_cptable = cp_get_table( 1252 );
53     oem_cptable  = cp_get_table( 437 );
54     mac_cptable  = cp_get_table( 10000 );
55     assert( ansi_cptable );
56     assert( oem_cptable );
57     assert( mac_cptable );
58 }
59
60 /* find the table for a given codepage, handling CP_ACP etc. pseudo-codepages */
61 static const union cptable *get_codepage_table( unsigned int codepage )
62 {
63     const union cptable *ret = NULL;
64
65     if (!ansi_cptable) init_codepages();
66
67     switch(codepage)
68     {
69     case CP_ACP:        return ansi_cptable;
70     case CP_OEMCP:      return oem_cptable;
71     case CP_MACCP:      return mac_cptable;
72     case CP_THREAD_ACP: return get_locale_cp( GetThreadLocale(), LOCALE_IDEFAULTANSICODEPAGE );
73     case CP_UTF7:
74     case CP_UTF8:
75         break;
76     default:
77         if (codepage == ansi_cptable->info.codepage) return ansi_cptable;
78         if (codepage == oem_cptable->info.codepage) return oem_cptable;
79         if (codepage == mac_cptable->info.codepage) return mac_cptable;
80         ret = cp_get_table( codepage );
81         break;
82     }
83     return ret;
84 }
85
86 /* initialize default code pages from locale info */
87 /* FIXME: should be done in init_codepages, but it can't right now */
88 /* since it needs KERNEL32 to be loaded for the locale info. */
89 void CODEPAGE_Init(void)
90 {
91     extern void __wine_init_codepages( const union cptable *ansi, const union cptable *oem );
92     const union cptable *table;
93     LCID lcid = GetUserDefaultLCID();
94
95     if (!ansi_cptable) init_codepages();  /* just in case */
96
97     if ((table = get_locale_cp( lcid, LOCALE_IDEFAULTANSICODEPAGE ))) ansi_cptable = table;
98     if ((table = get_locale_cp( lcid, LOCALE_IDEFAULTMACCODEPAGE ))) mac_cptable = table;
99     if ((table = get_locale_cp( lcid, LOCALE_IDEFAULTCODEPAGE ))) oem_cptable = table;
100     __wine_init_codepages( ansi_cptable, oem_cptable );
101
102     TRACE( "ansi=%03d oem=%03d mac=%03d\n", ansi_cptable->info.codepage,
103            oem_cptable->info.codepage, mac_cptable->info.codepage );
104 }
105
106 /******************************************************************************
107  *              GetACP   (KERNEL32.@)
108  *
109  * RETURNS
110  *    Current ANSI code-page identifier, default if no current defined
111  */
112 UINT WINAPI GetACP(void)
113 {
114     if (!ansi_cptable) init_codepages();
115     return ansi_cptable->info.codepage;
116 }
117
118
119 /***********************************************************************
120  *              GetOEMCP   (KERNEL32.@)
121  */
122 UINT WINAPI GetOEMCP(void)
123 {
124     if (!oem_cptable) init_codepages();
125     return oem_cptable->info.codepage;
126 }
127
128
129 /***********************************************************************
130  *           IsValidCodePage   (KERNEL32.@)
131  */
132 BOOL WINAPI IsValidCodePage( UINT codepage )
133 {
134     return cp_get_table( codepage ) != NULL;
135 }
136
137
138 /***********************************************************************
139  *           IsDBCSLeadByteEx   (KERNEL32.@)
140  */
141 BOOL WINAPI IsDBCSLeadByteEx( UINT codepage, BYTE testchar )
142 {
143     const union cptable *table = get_codepage_table( codepage );
144     return table && is_dbcs_leadbyte( table, testchar );
145 }
146
147
148 /***********************************************************************
149  *           IsDBCSLeadByte   (KERNEL32.@)
150  *           IsDBCSLeadByte   (KERNEL.207)
151  */
152 BOOL WINAPI IsDBCSLeadByte( BYTE testchar )
153 {
154     if (!ansi_cptable) init_codepages();
155     return is_dbcs_leadbyte( ansi_cptable, testchar );
156 }
157
158
159 /***********************************************************************
160  *           GetCPInfo   (KERNEL32.@)
161  */
162 BOOL WINAPI GetCPInfo( UINT codepage, LPCPINFO cpinfo )
163 {
164     const union cptable *table = get_codepage_table( codepage );
165
166     if (!table) 
167     {
168         SetLastError( ERROR_INVALID_PARAMETER );
169         return FALSE;
170     }
171     if (table->info.def_char & 0xff00)
172     {
173         cpinfo->DefaultChar[0] = table->info.def_char & 0xff00;
174         cpinfo->DefaultChar[1] = table->info.def_char & 0x00ff;
175     }
176     else
177     {
178         cpinfo->DefaultChar[0] = table->info.def_char & 0xff;
179         cpinfo->DefaultChar[1] = 0;
180     }
181     if ((cpinfo->MaxCharSize = table->info.char_size) == 2)
182         memcpy( cpinfo->LeadByte, table->dbcs.lead_bytes, sizeof(cpinfo->LeadByte) );
183     else
184         cpinfo->LeadByte[0] = cpinfo->LeadByte[1] = 0;
185
186     return TRUE;
187 }
188
189
190 /***********************************************************************
191  *              EnumSystemCodePagesA   (KERNEL32.@)
192  */
193 BOOL WINAPI EnumSystemCodePagesA( CODEPAGE_ENUMPROCA lpfnCodePageEnum, DWORD flags )
194 {
195     const union cptable *table;
196     char buffer[10];
197     int index = 0;
198
199     for (;;)
200     {
201         if (!(table = cp_enum_table( index++ ))) break;
202         sprintf( buffer, "%d", table->info.codepage );
203         if (!lpfnCodePageEnum( buffer )) break;
204     }
205     return TRUE;
206 }
207
208
209 /***********************************************************************
210  *              EnumSystemCodePagesW   (KERNEL32.@)
211  */
212 BOOL WINAPI EnumSystemCodePagesW( CODEPAGE_ENUMPROCW lpfnCodePageEnum, DWORD flags )
213 {
214     const union cptable *table;
215     WCHAR buffer[10], *p;
216     int page, index = 0;
217
218     for (;;)
219     {
220         if (!(table = cp_enum_table( index++ ))) break;
221         p = buffer + sizeof(buffer)/sizeof(WCHAR);
222         *--p = 0;
223         page = table->info.codepage;
224         do
225         {
226             *--p = '0' + (page % 10);
227             page /= 10;
228         } while( page );
229         if (!lpfnCodePageEnum( p )) break;
230     }
231     return TRUE;
232 }
233
234
235 /***********************************************************************
236  *              MultiByteToWideChar   (KERNEL32.@)
237  *
238  * PARAMS
239  *   page [in]    Codepage character set to convert from
240  *   flags [in]   Character mapping flags
241  *   src [in]     Source string buffer
242  *   srclen [in]  Length of source string buffer
243  *   dst [in]     Destination buffer
244  *   dstlen [in]  Length of destination buffer
245  *
246  * NOTES
247  *   The returned length includes the null terminator character.
248  *
249  * RETURNS
250  *   Success: If dstlen > 0, number of characters written to destination
251  *            buffer.  If dstlen == 0, number of characters needed to do
252  *            conversion.
253  *   Failure: 0. Occurs if not enough space is available.
254  *
255  * ERRORS
256  *   ERROR_INSUFFICIENT_BUFFER
257  *   ERROR_INVALID_PARAMETER
258  *   ERROR_NO_UNICODE_TRANSLATION
259  *
260  */
261 INT WINAPI MultiByteToWideChar( UINT page, DWORD flags, LPCSTR src, INT srclen,
262                                 LPWSTR dst, INT dstlen )
263 {
264     const union cptable *table;
265     int ret;
266
267     if (!src || (!dst && dstlen))
268     {
269         SetLastError( ERROR_INVALID_PARAMETER );
270         return 0;
271     }
272
273     if (srclen == -1) srclen = strlen(src) + 1;
274
275     if (flags & MB_USEGLYPHCHARS) FIXME("MB_USEGLYPHCHARS not supported\n");
276
277     switch(page)
278     {
279     case CP_UTF7:
280         FIXME("UTF not supported\n");
281         SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
282         return 0;
283     case CP_UTF8:
284         ret = utf8_mbstowcs( flags, src, srclen, dst, dstlen );
285         break;
286     default:
287         if (!(table = get_codepage_table( page )))
288         {
289             SetLastError( ERROR_INVALID_PARAMETER );
290             return 0;
291         }
292         ret = cp_mbstowcs( table, flags, src, srclen, dst, dstlen );
293         break;
294     }
295
296     if (ret < 0)
297     {
298         switch(ret)
299         {
300         case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER ); break;
301         case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION ); break;
302         }
303         ret = 0;
304     }
305     return ret;
306 }
307
308
309 /***********************************************************************
310  *              WideCharToMultiByte   (KERNEL32.@)
311  *
312  * PARAMS
313  *   page [in]    Codepage character set to convert to
314  *   flags [in]   Character mapping flags
315  *   src [in]     Source string buffer
316  *   srclen [in]  Length of source string buffer
317  *   dst [in]     Destination buffer
318  *   dstlen [in]  Length of destination buffer
319  *   defchar [in] Default character to use for conversion if no exact
320  *                  conversion can be made
321  *   used [out]   Set if default character was used in the conversion
322  *
323  * NOTES
324  *   The returned length includes the null terminator character.
325  *
326  * RETURNS
327  *   Success: If dstlen > 0, number of characters written to destination
328  *            buffer.  If dstlen == 0, number of characters needed to do
329  *            conversion.
330  *   Failure: 0. Occurs if not enough space is available.
331  *
332  * ERRORS
333  *   ERROR_INSUFFICIENT_BUFFER
334  *   ERROR_INVALID_PARAMETER
335  */
336 INT WINAPI WideCharToMultiByte( UINT page, DWORD flags, LPCWSTR src, INT srclen,
337                                 LPSTR dst, INT dstlen, LPCSTR defchar, BOOL *used )
338 {
339     const union cptable *table;
340     int ret, used_tmp;
341
342     if (!src || (!dst && dstlen))
343     {
344         SetLastError( ERROR_INVALID_PARAMETER );
345         return 0;
346     }
347
348     if (srclen == -1) srclen = strlenW(src) + 1;
349
350     switch(page)
351     {
352     case CP_UTF7:
353         FIXME("UTF-7 not supported\n");
354         SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
355         return 0;
356     case CP_UTF8:
357         ret = utf8_wcstombs( src, srclen, dst, dstlen );
358         break;
359     default:
360         if (!(table = get_codepage_table( page )))
361         {
362             SetLastError( ERROR_INVALID_PARAMETER );
363             return 0;
364         }
365         ret = cp_wcstombs( table, flags, src, srclen, dst, dstlen,
366                            defchar, used ? &used_tmp : NULL );
367         if (used) *used = used_tmp;
368         break;
369     }
370
371     if (ret == -1)
372     {
373         SetLastError( ERROR_INSUFFICIENT_BUFFER );
374         ret = 0;
375     }
376     return ret;
377 }
378
379
380 /******************************************************************************
381  *              GetStringTypeW   (KERNEL32.@)
382  *
383  */
384 BOOL WINAPI GetStringTypeW( DWORD type, LPCWSTR src, INT count, LPWORD chartype )
385 {
386     if (count == -1) count = strlenW(src) + 1;
387     switch(type)
388     {
389     case CT_CTYPE1:
390         while (count--) *chartype++ = get_char_typeW( *src++ ) & 0xfff;
391         break;
392     case CT_CTYPE2:
393         while (count--) *chartype++ = get_char_typeW( *src++ ) >> 12;
394         break;
395     case CT_CTYPE3:
396         FIXME("CT_CTYPE3 not supported.\n");
397     default:
398         SetLastError( ERROR_INVALID_PARAMETER );
399         return FALSE;
400     }
401     return TRUE;
402 }
403
404
405 /******************************************************************************
406  *              GetStringTypeExW   (KERNEL32.@)
407  */
408 BOOL WINAPI GetStringTypeExW( LCID locale, DWORD type, LPCWSTR src, INT count, LPWORD chartype )
409 {
410     /* locale is ignored for Unicode */
411     return GetStringTypeW( type, src, count, chartype );
412 }
413
414 /******************************************************************************
415  *              GetStringTypeA  [KERNEL32.@]
416  */
417 BOOL WINAPI GetStringTypeA(LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype)
418 {
419     char buf[20];
420     UINT cp;
421     INT countW;
422     LPWSTR srcW;
423     BOOL ret = FALSE;
424
425     if(count == -1) count = strlen(src) + 1;
426
427     if(!GetLocaleInfoA(locale, LOCALE_IDEFAULTANSICODEPAGE | LOCALE_NOUSEROVERRIDE,
428                    buf, sizeof(buf)))
429     {
430         FIXME("For locale %04lx using current ANSI code page\n", locale);
431         cp = GetACP();
432     }
433     else
434         cp = atoi(buf);
435
436     countW = MultiByteToWideChar(cp, 0, src, count, NULL, 0);
437     if((srcW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
438     {
439         MultiByteToWideChar(cp, 0, src, count, srcW, countW);
440         ret = GetStringTypeW(type, srcW, count, chartype);
441         HeapFree(GetProcessHeap(), 0, srcW);
442     }
443     return ret;
444 }
445
446 /******************************************************************************
447  *              GetStringTypeExA        [KERNEL32.@]
448  */
449 BOOL WINAPI GetStringTypeExA(LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype)
450 {
451     return GetStringTypeA(locale, type, src, count, chartype);
452 }