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