Fixed GetLocaleInfoW to handle Unicode properly and completed
[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 "thread.h"
31 #include "wine/debug.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(string);
34
35 /* current code pages */
36 static const union cptable *ansi_cptable;
37 static const union cptable *oem_cptable;
38 static const union cptable *mac_cptable;
39 static LCID default_lcid = MAKELCID( MAKELANGID(LANG_ENGLISH,SUBLANG_DEFAULT), SORT_DEFAULT );
40
41 /* setup default codepage info before we can get at the locale stuff */
42 static void init_codepages(void)
43 {
44     ansi_cptable = cp_get_table( 1252 );
45     oem_cptable  = cp_get_table( 437 );
46     mac_cptable  = cp_get_table( 10000 );
47     assert( ansi_cptable );
48     assert( oem_cptable );
49     assert( mac_cptable );
50 }
51
52 /* find the table for a given codepage, handling CP_ACP etc. pseudo-codepages */
53 static const union cptable *get_codepage_table( unsigned int codepage )
54 {
55     const union cptable *ret = NULL;
56
57     if (!ansi_cptable) init_codepages();
58
59     switch(codepage)
60     {
61     case CP_ACP:
62         return ansi_cptable;
63     case CP_OEMCP:
64         return oem_cptable;
65     case CP_MACCP:
66         return mac_cptable;
67     case CP_UTF7:
68     case CP_UTF8:
69         break;
70     case CP_THREAD_ACP:
71         if (!(codepage = NtCurrentTeb()->code_page)) return ansi_cptable;
72         /* fall through */
73     default:
74         if (codepage == ansi_cptable->info.codepage) return ansi_cptable;
75         if (codepage == oem_cptable->info.codepage) return oem_cptable;
76         if (codepage == mac_cptable->info.codepage) return mac_cptable;
77         ret = cp_get_table( codepage );
78         break;
79     }
80     return ret;
81 }
82
83 /* initialize default code pages from locale info */
84 /* FIXME: should be done in init_codepages, but it can't right now */
85 /* since it needs KERNEL32 to be loaded for the locale info. */
86 void CODEPAGE_Init( UINT ansi, UINT oem, UINT mac, LCID lcid )
87 {
88     extern void __wine_init_codepages( const union cptable *ansi, const union cptable *oem );
89     const union cptable *table;
90
91     default_lcid = lcid;
92     if (!ansi_cptable) init_codepages();  /* just in case */
93
94     if ((table = cp_get_table( ansi ))) ansi_cptable = table;
95     if ((table = cp_get_table( oem ))) oem_cptable = table;
96     if ((table = cp_get_table( mac ))) mac_cptable = table;
97     __wine_init_codepages( ansi_cptable, oem_cptable );
98
99     TRACE( "ansi=%03d oem=%03d mac=%03d\n", ansi_cptable->info.codepage,
100            oem_cptable->info.codepage, mac_cptable->info.codepage );
101 }
102
103 /******************************************************************************
104  *              GetACP   (KERNEL32.@)
105  *
106  * RETURNS
107  *    Current ANSI code-page identifier, default if no current defined
108  */
109 UINT WINAPI GetACP(void)
110 {
111     if (!ansi_cptable) return 1252;
112     return ansi_cptable->info.codepage;
113 }
114
115
116 /***********************************************************************
117  *              GetOEMCP   (KERNEL32.@)
118  */
119 UINT WINAPI GetOEMCP(void)
120 {
121     if (!oem_cptable) return 437;
122     return oem_cptable->info.codepage;
123 }
124
125
126 /***********************************************************************
127  *           IsValidCodePage   (KERNEL32.@)
128  */
129 BOOL WINAPI IsValidCodePage( UINT codepage )
130 {
131     switch(codepage) {
132     case CP_SYMBOL:
133         return FALSE;
134     case CP_UTF7:
135     case CP_UTF8:
136         return TRUE;
137     default:
138         return cp_get_table( codepage ) != NULL;
139     }
140 }
141
142
143 /***********************************************************************
144  *              GetUserDefaultLangID (KERNEL32.@)
145  */
146 LANGID WINAPI GetUserDefaultLangID(void)
147 {
148     return LANGIDFROMLCID(default_lcid);
149 }
150
151
152 /***********************************************************************
153  *              GetSystemDefaultLangID (KERNEL32.@)
154  */
155 LANGID WINAPI GetSystemDefaultLangID(void)
156 {
157     return GetUserDefaultLangID();
158 }
159
160
161 /***********************************************************************
162  *              GetUserDefaultLCID (KERNEL32.@)
163  */
164 LCID WINAPI GetUserDefaultLCID(void)
165 {
166     return default_lcid;
167 }
168
169
170 /***********************************************************************
171  *              GetSystemDefaultLCID (KERNEL32.@)
172  */
173 LCID WINAPI GetSystemDefaultLCID(void)
174 {
175     return GetUserDefaultLCID();
176 }
177
178
179 /***********************************************************************
180  *           IsDBCSLeadByteEx   (KERNEL32.@)
181  */
182 BOOL WINAPI IsDBCSLeadByteEx( UINT codepage, BYTE testchar )
183 {
184     const union cptable *table = get_codepage_table( codepage );
185     return table && is_dbcs_leadbyte( table, testchar );
186 }
187
188
189 /***********************************************************************
190  *           IsDBCSLeadByte   (KERNEL32.@)
191  *           IsDBCSLeadByte   (KERNEL.207)
192  */
193 BOOL WINAPI IsDBCSLeadByte( BYTE testchar )
194 {
195     if (!ansi_cptable) return FALSE;
196     return is_dbcs_leadbyte( ansi_cptable, testchar );
197 }
198
199
200 /***********************************************************************
201  *           GetCPInfo   (KERNEL32.@)
202  */
203 BOOL WINAPI GetCPInfo( UINT codepage, LPCPINFO cpinfo )
204 {
205     const union cptable *table = get_codepage_table( codepage );
206
207     if (!table)
208     {
209         SetLastError( ERROR_INVALID_PARAMETER );
210         return FALSE;
211     }
212     if (table->info.def_char & 0xff00)
213     {
214         cpinfo->DefaultChar[0] = table->info.def_char & 0xff00;
215         cpinfo->DefaultChar[1] = table->info.def_char & 0x00ff;
216     }
217     else
218     {
219         cpinfo->DefaultChar[0] = table->info.def_char & 0xff;
220         cpinfo->DefaultChar[1] = 0;
221     }
222     if ((cpinfo->MaxCharSize = table->info.char_size) == 2)
223         memcpy( cpinfo->LeadByte, table->dbcs.lead_bytes, sizeof(cpinfo->LeadByte) );
224     else
225         cpinfo->LeadByte[0] = cpinfo->LeadByte[1] = 0;
226
227     return TRUE;
228 }
229
230
231 /***********************************************************************
232  *              EnumSystemCodePagesA   (KERNEL32.@)
233  */
234 BOOL WINAPI EnumSystemCodePagesA( CODEPAGE_ENUMPROCA lpfnCodePageEnum, DWORD flags )
235 {
236     const union cptable *table;
237     char buffer[10];
238     int index = 0;
239
240     for (;;)
241     {
242         if (!(table = cp_enum_table( index++ ))) break;
243         sprintf( buffer, "%d", table->info.codepage );
244         if (!lpfnCodePageEnum( buffer )) break;
245     }
246     return TRUE;
247 }
248
249
250 /***********************************************************************
251  *              EnumSystemCodePagesW   (KERNEL32.@)
252  */
253 BOOL WINAPI EnumSystemCodePagesW( CODEPAGE_ENUMPROCW lpfnCodePageEnum, DWORD flags )
254 {
255     const union cptable *table;
256     WCHAR buffer[10], *p;
257     int page, index = 0;
258
259     for (;;)
260     {
261         if (!(table = cp_enum_table( index++ ))) break;
262         p = buffer + sizeof(buffer)/sizeof(WCHAR);
263         *--p = 0;
264         page = table->info.codepage;
265         do
266         {
267             *--p = '0' + (page % 10);
268             page /= 10;
269         } while( page );
270         if (!lpfnCodePageEnum( p )) break;
271     }
272     return TRUE;
273 }
274
275
276 /***********************************************************************
277  *              MultiByteToWideChar   (KERNEL32.@)
278  *
279  * PARAMS
280  *   page [in]    Codepage character set to convert from
281  *   flags [in]   Character mapping flags
282  *   src [in]     Source string buffer
283  *   srclen [in]  Length of source string buffer
284  *   dst [in]     Destination buffer
285  *   dstlen [in]  Length of destination buffer
286  *
287  * NOTES
288  *   The returned length includes the null terminator character.
289  *
290  * RETURNS
291  *   Success: If dstlen > 0, number of characters written to destination
292  *            buffer.  If dstlen == 0, number of characters needed to do
293  *            conversion.
294  *   Failure: 0. Occurs if not enough space is available.
295  *
296  * ERRORS
297  *   ERROR_INSUFFICIENT_BUFFER
298  *   ERROR_INVALID_PARAMETER
299  *   ERROR_NO_UNICODE_TRANSLATION
300  *
301  */
302 INT WINAPI MultiByteToWideChar( UINT page, DWORD flags, LPCSTR src, INT srclen,
303                                 LPWSTR dst, INT dstlen )
304 {
305     const union cptable *table;
306     int ret;
307
308     if (!src || (!dst && dstlen))
309     {
310         SetLastError( ERROR_INVALID_PARAMETER );
311         return 0;
312     }
313
314     if (srclen == -1) srclen = strlen(src) + 1;
315
316     if (flags & MB_USEGLYPHCHARS) FIXME("MB_USEGLYPHCHARS not supported\n");
317
318     switch(page)
319     {
320     case CP_UTF7:
321         FIXME("UTF not supported\n");
322         SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
323         return 0;
324     case CP_UTF8:
325         ret = utf8_mbstowcs( flags, src, srclen, dst, dstlen );
326         break;
327     default:
328         if (!(table = get_codepage_table( page )))
329         {
330             SetLastError( ERROR_INVALID_PARAMETER );
331             return 0;
332         }
333         ret = cp_mbstowcs( table, flags, src, srclen, dst, dstlen );
334         break;
335     }
336
337     if (ret < 0)
338     {
339         switch(ret)
340         {
341         case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER ); break;
342         case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION ); break;
343         }
344         ret = 0;
345     }
346     return ret;
347 }
348
349
350 /***********************************************************************
351  *              WideCharToMultiByte   (KERNEL32.@)
352  *
353  * PARAMS
354  *   page [in]    Codepage character set to convert to
355  *   flags [in]   Character mapping flags
356  *   src [in]     Source string buffer
357  *   srclen [in]  Length of source string buffer
358  *   dst [in]     Destination buffer
359  *   dstlen [in]  Length of destination buffer
360  *   defchar [in] Default character to use for conversion if no exact
361  *                  conversion can be made
362  *   used [out]   Set if default character was used in the conversion
363  *
364  * NOTES
365  *   The returned length includes the null terminator character.
366  *
367  * RETURNS
368  *   Success: If dstlen > 0, number of characters written to destination
369  *            buffer.  If dstlen == 0, number of characters needed to do
370  *            conversion.
371  *   Failure: 0. Occurs if not enough space is available.
372  *
373  * ERRORS
374  *   ERROR_INSUFFICIENT_BUFFER
375  *   ERROR_INVALID_PARAMETER
376  */
377 INT WINAPI WideCharToMultiByte( UINT page, DWORD flags, LPCWSTR src, INT srclen,
378                                 LPSTR dst, INT dstlen, LPCSTR defchar, BOOL *used )
379 {
380     const union cptable *table;
381     int ret, used_tmp;
382
383     if (!src || (!dst && dstlen))
384     {
385         SetLastError( ERROR_INVALID_PARAMETER );
386         return 0;
387     }
388
389     if (srclen == -1) srclen = strlenW(src) + 1;
390
391     switch(page)
392     {
393     case CP_UTF7:
394         FIXME("UTF-7 not supported\n");
395         SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
396         return 0;
397     case CP_UTF8:
398         ret = utf8_wcstombs( src, srclen, dst, dstlen );
399         break;
400     default:
401         if (!(table = get_codepage_table( page )))
402         {
403             SetLastError( ERROR_INVALID_PARAMETER );
404             return 0;
405         }
406         ret = cp_wcstombs( table, flags, src, srclen, dst, dstlen,
407                            defchar, used ? &used_tmp : NULL );
408         if (used) *used = used_tmp;
409         break;
410     }
411
412     if (ret == -1)
413     {
414         SetLastError( ERROR_INSUFFICIENT_BUFFER );
415         ret = 0;
416     }
417     return ret;
418 }