blob: 21915ad1d5498922bf295fbd635a9776fb8331b6 [file] [log] [blame]
Alexandre Julliardf7bf7ef2000-06-10 04:42:33 +00001/*
2 * Code page functions
3 *
4 * Copyright 2000 Alexandre Julliard
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00005 *
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
Alexandre Julliardf7bf7ef2000-06-10 04:42:33 +000019 */
20
21#include <assert.h>
22#include <stdio.h>
Alexandre Julliard6ce25702000-07-11 22:08:43 +000023#include <stdlib.h>
James Juranf4d5fef2001-01-26 20:43:40 +000024#include <string.h>
Alexandre Julliardf7bf7ef2000-06-10 04:42:33 +000025
26#include "winbase.h"
27#include "winerror.h"
28#include "winnls.h"
29#include "wine/unicode.h"
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000030#include "wine/debug.h"
Alexandre Julliardf7bf7ef2000-06-10 04:42:33 +000031
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000032WINE_DEFAULT_DEBUG_CHANNEL(string);
Alexandre Julliardf7bf7ef2000-06-10 04:42:33 +000033
34/* current code pages */
Alexandre Julliardf7bf7ef2000-06-10 04:42:33 +000035static const union cptable *ansi_cptable;
36static const union cptable *oem_cptable;
37static const union cptable *mac_cptable;
38
Alexandre Julliard6ce25702000-07-11 22:08:43 +000039/* retrieve a code page table from the locale info */
40static 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 */
50static 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}
Alexandre Julliardf7bf7ef2000-06-10 04:42:33 +000059
60/* find the table for a given codepage, handling CP_ACP etc. pseudo-codepages */
61static const union cptable *get_codepage_table( unsigned int codepage )
62{
63 const union cptable *ret = NULL;
64
Alexandre Julliard6ce25702000-07-11 22:08:43 +000065 if (!ansi_cptable) init_codepages();
Alexandre Julliardf7bf7ef2000-06-10 04:42:33 +000066
67 switch(codepage)
68 {
Alexandre Julliardf7bf7ef2000-06-10 04:42:33 +000069 case CP_ACP: return ansi_cptable;
70 case CP_OEMCP: return oem_cptable;
71 case CP_MACCP: return mac_cptable;
Alexandre Julliard6ce25702000-07-11 22:08:43 +000072 case CP_THREAD_ACP: return get_locale_cp( GetThreadLocale(), LOCALE_IDEFAULTANSICODEPAGE );
Alexandre Julliardf7bf7ef2000-06-10 04:42:33 +000073 case CP_UTF7:
74 case CP_UTF8:
75 break;
76 default:
Alexandre Julliard6ce25702000-07-11 22:08:43 +000077 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;
Alexandre Julliardf7bf7ef2000-06-10 04:42:33 +000080 ret = cp_get_table( codepage );
81 break;
82 }
83 return ret;
84}
85
Alexandre Julliard6ce25702000-07-11 22:08:43 +000086/* 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. */
89void CODEPAGE_Init(void)
90{
Alexandre Julliard996c0bf2001-12-05 22:14:57 +000091 extern void __wine_init_codepages( const union cptable *ansi, const union cptable *oem );
Alexandre Julliard6ce25702000-07-11 22:08:43 +000092 const union cptable *table;
93 LCID lcid = GetUserDefaultLCID();
94
95 if (!ansi_cptable) init_codepages(); /* just in case */
Alexandre Julliard996c0bf2001-12-05 22:14:57 +000096
Alexandre Julliard6ce25702000-07-11 22:08:43 +000097 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;
Alexandre Julliard996c0bf2001-12-05 22:14:57 +0000100 __wine_init_codepages( ansi_cptable, oem_cptable );
Alexandre Julliard6ce25702000-07-11 22:08:43 +0000101
102 TRACE( "ansi=%03d oem=%03d mac=%03d\n", ansi_cptable->info.codepage,
103 oem_cptable->info.codepage, mac_cptable->info.codepage );
104}
105
Alexandre Julliardf7bf7ef2000-06-10 04:42:33 +0000106/******************************************************************************
Patrik Stridvall3ca98232001-06-20 23:03:14 +0000107 * GetACP (KERNEL32.@)
Alexandre Julliardf7bf7ef2000-06-10 04:42:33 +0000108 *
109 * RETURNS
110 * Current ANSI code-page identifier, default if no current defined
111 */
112UINT WINAPI GetACP(void)
113{
Alexandre Julliard6ce25702000-07-11 22:08:43 +0000114 if (!ansi_cptable) init_codepages();
115 return ansi_cptable->info.codepage;
Alexandre Julliardf7bf7ef2000-06-10 04:42:33 +0000116}
117
118
119/***********************************************************************
Patrik Stridvall3ca98232001-06-20 23:03:14 +0000120 * GetOEMCP (KERNEL32.@)
Alexandre Julliardf7bf7ef2000-06-10 04:42:33 +0000121 */
122UINT WINAPI GetOEMCP(void)
123{
Alexandre Julliard6ce25702000-07-11 22:08:43 +0000124 if (!oem_cptable) init_codepages();
125 return oem_cptable->info.codepage;
Alexandre Julliardf7bf7ef2000-06-10 04:42:33 +0000126}
127
128
129/***********************************************************************
Patrik Stridvall3ca98232001-06-20 23:03:14 +0000130 * IsValidCodePage (KERNEL32.@)
Alexandre Julliardf7bf7ef2000-06-10 04:42:33 +0000131 */
132BOOL WINAPI IsValidCodePage( UINT codepage )
133{
134 return cp_get_table( codepage ) != NULL;
135}
136
137
138/***********************************************************************
Patrik Stridvall3ca98232001-06-20 23:03:14 +0000139 * IsDBCSLeadByteEx (KERNEL32.@)
Alexandre Julliardf7bf7ef2000-06-10 04:42:33 +0000140 */
141BOOL 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/***********************************************************************
Patrik Stridvall3ca98232001-06-20 23:03:14 +0000149 * IsDBCSLeadByte (KERNEL32.@)
Patrik Stridvall044855c2001-07-11 18:56:41 +0000150 * IsDBCSLeadByte (KERNEL.207)
Alexandre Julliardf7bf7ef2000-06-10 04:42:33 +0000151 */
152BOOL WINAPI IsDBCSLeadByte( BYTE testchar )
153{
Alexandre Julliard6ce25702000-07-11 22:08:43 +0000154 if (!ansi_cptable) init_codepages();
Alexandre Julliardf7bf7ef2000-06-10 04:42:33 +0000155 return is_dbcs_leadbyte( ansi_cptable, testchar );
156}
157
158
159/***********************************************************************
Patrik Stridvall3ca98232001-06-20 23:03:14 +0000160 * GetCPInfo (KERNEL32.@)
Alexandre Julliardf7bf7ef2000-06-10 04:42:33 +0000161 */
162BOOL 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/***********************************************************************
Patrik Stridvall3ca98232001-06-20 23:03:14 +0000191 * EnumSystemCodePagesA (KERNEL32.@)
Alexandre Julliardf7bf7ef2000-06-10 04:42:33 +0000192 */
193BOOL 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/***********************************************************************
Patrik Stridvall3ca98232001-06-20 23:03:14 +0000210 * EnumSystemCodePagesW (KERNEL32.@)
Alexandre Julliardf7bf7ef2000-06-10 04:42:33 +0000211 */
212BOOL 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/***********************************************************************
Patrik Stridvall3ca98232001-06-20 23:03:14 +0000236 * MultiByteToWideChar (KERNEL32.@)
Alexandre Julliardf7bf7ef2000-06-10 04:42:33 +0000237 *
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 */
261INT 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
Wolfgang Schwotzerd9b8f9b2000-07-28 23:58:48 +0000267 if (!src || (!dst && dstlen))
268 {
269 SetLastError( ERROR_INVALID_PARAMETER );
270 return 0;
271 }
272
Alexandre Julliardf7bf7ef2000-06-10 04:42:33 +0000273 if (srclen == -1) srclen = strlen(src) + 1;
274
Alexandre Julliardf7bf7ef2000-06-10 04:42:33 +0000275 if (flags & MB_USEGLYPHCHARS) FIXME("MB_USEGLYPHCHARS not supported\n");
276
Alexandre Julliardaea78532000-08-11 00:44:33 +0000277 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 }
Alexandre Julliardf7bf7ef2000-06-10 04:42:33 +0000295
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/***********************************************************************
Patrik Stridvall3ca98232001-06-20 23:03:14 +0000310 * WideCharToMultiByte (KERNEL32.@)
Alexandre Julliardf7bf7ef2000-06-10 04:42:33 +0000311 *
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 */
336INT 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
Wolfgang Schwotzerd9b8f9b2000-07-28 23:58:48 +0000342 if (!src || (!dst && dstlen))
343 {
344 SetLastError( ERROR_INVALID_PARAMETER );
345 return 0;
346 }
347
Alexandre Julliardf7bf7ef2000-06-10 04:42:33 +0000348 if (srclen == -1) srclen = strlenW(src) + 1;
349
Alexandre Julliardaea78532000-08-11 00:44:33 +0000350 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 }
Alexandre Julliardf7bf7ef2000-06-10 04:42:33 +0000370
371 if (ret == -1)
372 {
373 SetLastError( ERROR_INSUFFICIENT_BUFFER );
374 ret = 0;
375 }
376 return ret;
377}
Alexandre Julliardc97bb4c2000-08-11 20:53:40 +0000378
379
380/******************************************************************************
Patrik Stridvall3ca98232001-06-20 23:03:14 +0000381 * GetStringTypeW (KERNEL32.@)
Alexandre Julliardc97bb4c2000-08-11 20:53:40 +0000382 *
383 */
384BOOL 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/******************************************************************************
Patrik Stridvall3ca98232001-06-20 23:03:14 +0000406 * GetStringTypeExW (KERNEL32.@)
Alexandre Julliardc97bb4c2000-08-11 20:53:40 +0000407 */
408BOOL 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}
Dmitry Timoshkovc59655e2002-02-04 18:34:32 +0000413
414/******************************************************************************
415 * GetStringTypeA [KERNEL32.@]
416 */
417BOOL 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 */
449BOOL WINAPI GetStringTypeExA(LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype)
450{
451 return GetStringTypeA(locale, type, src, count, chartype);
452}