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