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