[INETCOMM_WINETEST] Sync with Wine Staging 2.2. CORE-12823
[reactos.git] / rostests / winetests / kernel32 / codepage.c
1 /*
2 * Unit tests for code page to/from unicode translations
3 *
4 * Copyright (c) 2002 Dmitry Timoshkov
5 * Copyright (c) 2008 Colin Finck
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <limits.h>
25
26 #include "wine/test.h"
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winnls.h"
30
31 static const char foobarA[] = "foobar";
32 static const WCHAR foobarW[] = {'f','o','o','b','a','r',0};
33
34 static void test_destination_buffer(void)
35 {
36 LPSTR buffer;
37 INT maxsize;
38 INT needed;
39 INT len;
40
41 SetLastError(0xdeadbeef);
42 needed = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, NULL, 0, NULL, NULL);
43 ok( (needed > 0), "returned %d with %u (expected '> 0')\n",
44 needed, GetLastError());
45
46 maxsize = needed*2;
47 buffer = HeapAlloc(GetProcessHeap(), 0, maxsize);
48 if (buffer == NULL) return;
49
50 maxsize--;
51 memset(buffer, 'x', maxsize);
52 buffer[maxsize] = '\0';
53 SetLastError(0xdeadbeef);
54 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, buffer, needed+1, NULL, NULL);
55 ok( (len > 0), "returned %d with %u and '%s' (expected '> 0')\n",
56 len, GetLastError(), buffer);
57
58 memset(buffer, 'x', maxsize);
59 buffer[maxsize] = '\0';
60 SetLastError(0xdeadbeef);
61 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, buffer, needed, NULL, NULL);
62 ok( (len > 0), "returned %d with %u and '%s' (expected '> 0')\n",
63 len, GetLastError(), buffer);
64
65 memset(buffer, 'x', maxsize);
66 buffer[maxsize] = '\0';
67 SetLastError(0xdeadbeef);
68 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, buffer, needed-1, NULL, NULL);
69 ok( !len && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
70 "returned %d with %u and '%s' (expected '0' with "
71 "ERROR_INSUFFICIENT_BUFFER)\n", len, GetLastError(), buffer);
72
73 memset(buffer, 'x', maxsize);
74 buffer[maxsize] = '\0';
75 SetLastError(0xdeadbeef);
76 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, buffer, 1, NULL, NULL);
77 ok( !len && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
78 "returned %d with %u and '%s' (expected '0' with "
79 "ERROR_INSUFFICIENT_BUFFER)\n", len, GetLastError(), buffer);
80
81 SetLastError(0xdeadbeef);
82 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, buffer, 0, NULL, NULL);
83 ok( (len > 0), "returned %d with %u (expected '> 0')\n",
84 len, GetLastError());
85
86 SetLastError(0xdeadbeef);
87 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, NULL, needed, NULL, NULL);
88 ok( !len && (GetLastError() == ERROR_INVALID_PARAMETER),
89 "returned %d with %u (expected '0' with "
90 "ERROR_INVALID_PARAMETER)\n", len, GetLastError());
91
92 HeapFree(GetProcessHeap(), 0, buffer);
93 }
94
95
96 static void test_null_source(void)
97 {
98 int len;
99 DWORD GLE;
100
101 SetLastError(0);
102 len = WideCharToMultiByte(CP_ACP, 0, NULL, 0, NULL, 0, NULL, NULL);
103 GLE = GetLastError();
104 ok(!len && GLE == ERROR_INVALID_PARAMETER,
105 "WideCharToMultiByte returned %d with GLE=%u (expected 0 with ERROR_INVALID_PARAMETER)\n",
106 len, GLE);
107
108 SetLastError(0);
109 len = WideCharToMultiByte(CP_ACP, 0, NULL, -1, NULL, 0, NULL, NULL);
110 GLE = GetLastError();
111 ok(!len && GLE == ERROR_INVALID_PARAMETER,
112 "WideCharToMultiByte returned %d with GLE=%u (expected 0 with ERROR_INVALID_PARAMETER)\n",
113 len, GLE);
114 }
115
116 static void test_negative_source_length(void)
117 {
118 int len;
119 char buf[10];
120 WCHAR bufW[10];
121
122 /* Test, whether any negative source length works as strlen() + 1 */
123 SetLastError( 0xdeadbeef );
124 memset(buf,'x',sizeof(buf));
125 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -2002, buf, 10, NULL, NULL);
126 ok(len == 7 && GetLastError() == 0xdeadbeef,
127 "WideCharToMultiByte(-2002): len=%d error=%u\n", len, GetLastError());
128 ok(!lstrcmpA(buf, "foobar"),
129 "WideCharToMultiByte(-2002): expected \"foobar\" got \"%s\"\n", buf);
130
131 SetLastError( 0xdeadbeef );
132 memset(bufW,'x',sizeof(bufW));
133 len = MultiByteToWideChar(CP_ACP, 0, "foobar", -2002, bufW, 10);
134 ok(len == 7 && !lstrcmpW(bufW, foobarW) && GetLastError() == 0xdeadbeef,
135 "MultiByteToWideChar(-2002): len=%d error=%u\n", len, GetLastError());
136
137 SetLastError(0xdeadbeef);
138 memset(bufW, 'x', sizeof(bufW));
139 len = MultiByteToWideChar(CP_ACP, 0, "foobar", -1, bufW, 6);
140 ok(len == 0 && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
141 "MultiByteToWideChar(-1): len=%d error=%u\n", len, GetLastError());
142 }
143
144 #define LONGBUFLEN 100000
145 static void test_negative_dest_length(void)
146 {
147 int len, i;
148 static WCHAR bufW[LONGBUFLEN];
149 static char bufA[LONGBUFLEN];
150 static WCHAR originalW[LONGBUFLEN];
151 static char originalA[LONGBUFLEN];
152 DWORD theError;
153
154 /* Test return on -1 dest length */
155 SetLastError( 0xdeadbeef );
156 memset(bufA,'x',sizeof(bufA));
157 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, bufA, -1, NULL, NULL);
158 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER,
159 "WideCharToMultiByte(destlen -1): len=%d error=%x\n", len, GetLastError());
160
161 SetLastError( 0xdeadbeef );
162 memset(bufW,'x',sizeof(bufW));
163 len = MultiByteToWideChar(CP_ACP, 0, foobarA, -1, bufW, -1);
164 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER,
165 "MultiByteToWideChar(destlen -1): len=%d error=%x\n", len, GetLastError());
166
167 /* Test return on -1000 dest length */
168 SetLastError( 0xdeadbeef );
169 memset(bufA,'x',sizeof(bufA));
170 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, bufA, -1000, NULL, NULL);
171 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER,
172 "WideCharToMultiByte(destlen -1000): len=%d error=%x\n", len, GetLastError());
173
174 SetLastError( 0xdeadbeef );
175 memset(bufW,'x',sizeof(bufW));
176 len = MultiByteToWideChar(CP_ACP, 0, foobarA, -1000, bufW, -1);
177 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER,
178 "MultiByteToWideChar(destlen -1000): len=%d error=%x\n", len, GetLastError());
179
180 /* Test return on INT_MAX dest length */
181 SetLastError( 0xdeadbeef );
182 memset(bufA,'x',sizeof(bufA));
183 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, bufA, INT_MAX, NULL, NULL);
184 ok(len == 7 && !lstrcmpA(bufA, "foobar") && GetLastError() == 0xdeadbeef,
185 "WideCharToMultiByte(destlen INT_MAX): len=%d error=%x\n", len, GetLastError());
186
187 /* Test return on INT_MAX dest length and very long input */
188 SetLastError( 0xdeadbeef );
189 memset(bufA,'x',sizeof(bufA));
190 for (i=0; i < LONGBUFLEN - 1; i++) {
191 originalW[i] = 'Q';
192 originalA[i] = 'Q';
193 }
194 originalW[LONGBUFLEN-1] = 0;
195 originalA[LONGBUFLEN-1] = 0;
196 len = WideCharToMultiByte(CP_ACP, 0, originalW, -1, bufA, INT_MAX, NULL, NULL);
197 theError = GetLastError();
198 ok(len == LONGBUFLEN && !lstrcmpA(bufA, originalA) && theError == 0xdeadbeef,
199 "WideCharToMultiByte(srclen %d, destlen INT_MAX): len %d error=%x\n", LONGBUFLEN, len, theError);
200
201 }
202
203 static void test_other_invalid_parameters(void)
204 {
205 char c_string[] = "Hello World";
206 size_t c_string_len = sizeof(c_string);
207 WCHAR w_string[] = {'H','e','l','l','o',' ','W','o','r','l','d',0};
208 size_t w_string_len = sizeof(w_string) / sizeof(WCHAR);
209 BOOL used;
210 INT len;
211
212 /* srclen=0 => ERROR_INVALID_PARAMETER */
213 SetLastError(0xdeadbeef);
214 len = WideCharToMultiByte(CP_ACP, 0, w_string, 0, c_string, c_string_len, NULL, NULL);
215 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER, "len=%d error=%x\n", len, GetLastError());
216
217 SetLastError(0xdeadbeef);
218 len = MultiByteToWideChar(CP_ACP, 0, c_string, 0, w_string, w_string_len);
219 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER, "len=%d error=%x\n", len, GetLastError());
220
221
222 /* dst=NULL but dstlen not 0 => ERROR_INVALID_PARAMETER */
223 SetLastError(0xdeadbeef);
224 len = WideCharToMultiByte(CP_ACP, 0, w_string, w_string_len, NULL, c_string_len, NULL, NULL);
225 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER, "len=%d error=%x\n", len, GetLastError());
226
227 SetLastError(0xdeadbeef);
228 len = MultiByteToWideChar(CP_ACP, 0, c_string, c_string_len, NULL, w_string_len);
229 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER, "len=%d error=%x\n", len, GetLastError());
230
231
232 /* CP_UTF7, CP_UTF8, or CP_SYMBOL and defchar not NULL => ERROR_INVALID_PARAMETER */
233 /* CP_SYMBOL's behavior here is undocumented */
234 SetLastError(0xdeadbeef);
235 len = WideCharToMultiByte(CP_UTF7, 0, w_string, w_string_len, c_string, c_string_len, c_string, NULL);
236 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER, "len=%d error=%x\n", len, GetLastError());
237
238 SetLastError(0xdeadbeef);
239 len = WideCharToMultiByte(CP_UTF8, 0, w_string, w_string_len, c_string, c_string_len, c_string, NULL);
240 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER, "len=%d error=%x\n", len, GetLastError());
241
242 SetLastError(0xdeadbeef);
243 len = WideCharToMultiByte(CP_SYMBOL, 0, w_string, w_string_len, c_string, c_string_len, c_string, NULL);
244 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER, "len=%d error=%x\n", len, GetLastError());
245
246
247 /* CP_UTF7, CP_UTF8, or CP_SYMBOL and used not NULL => ERROR_INVALID_PARAMETER */
248 /* CP_SYMBOL's behavior here is undocumented */
249 SetLastError(0xdeadbeef);
250 len = WideCharToMultiByte(CP_UTF7, 0, w_string, w_string_len, c_string, c_string_len, NULL, &used);
251 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER, "len=%d error=%x\n", len, GetLastError());
252
253 SetLastError(0xdeadbeef);
254 len = WideCharToMultiByte(CP_UTF8, 0, w_string, w_string_len, c_string, c_string_len, NULL, &used);
255 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER, "len=%d error=%x\n", len, GetLastError());
256
257 SetLastError(0xdeadbeef);
258 len = WideCharToMultiByte(CP_SYMBOL, 0, w_string, w_string_len, c_string, c_string_len, NULL, &used);
259 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER, "len=%d error=%x\n", len, GetLastError());
260
261
262 /* CP_UTF7, flags not 0 and used not NULL => ERROR_INVALID_PARAMETER */
263 /* (tests precedence of ERROR_INVALID_PARAMETER over ERROR_INVALID_FLAGS) */
264 /* The same test with CP_SYMBOL instead of CP_UTF7 gives ERROR_INVALID_FLAGS
265 instead except on Windows NT4 */
266 SetLastError(0xdeadbeef);
267 len = WideCharToMultiByte(CP_UTF7, 1, w_string, w_string_len, c_string, c_string_len, NULL, &used);
268 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER, "len=%d error=%x\n", len, GetLastError());
269 }
270
271 static void test_overlapped_buffers(void)
272 {
273 static const WCHAR strW[] = {'j','u','s','t',' ','a',' ','t','e','s','t',0};
274 static const char strA[] = "just a test";
275 char buf[256];
276 int ret;
277
278 SetLastError(0xdeadbeef);
279 memcpy(buf + 1, strW, sizeof(strW));
280 ret = WideCharToMultiByte(CP_ACP, 0, (WCHAR *)(buf + 1), -1, buf, sizeof(buf), NULL, NULL);
281 ok(ret == sizeof(strA), "unexpected ret %d\n", ret);
282 ok(!memcmp(buf, strA, sizeof(strA)), "conversion failed: %s\n", buf);
283 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
284 }
285
286 static void test_string_conversion(LPBOOL bUsedDefaultChar)
287 {
288 char mbc;
289 char mbs[15];
290 int ret;
291 WCHAR wc1 = 228; /* Western Windows-1252 character */
292 WCHAR wc2 = 1088; /* Russian Windows-1251 character not displayable for Windows-1252 */
293 static const WCHAR wcs[] = {'T', 'h', 1088, 'i', 0}; /* String with ASCII characters and a Russian character */
294 static const WCHAR dbwcs[] = {28953, 25152, 0}; /* String with Chinese (codepage 950) characters */
295 static const WCHAR dbwcs2[] = {0x7bb8, 0x3d, 0xc813, 0xac00, 0xb77d, 0};
296 static const char default_char[] = {0xa3, 0xbf, 0};
297
298 SetLastError(0xdeadbeef);
299 ret = WideCharToMultiByte(1252, 0, &wc1, 1, &mbc, 1, NULL, bUsedDefaultChar);
300 ok(ret == 1, "ret is %d\n", ret);
301 ok(mbc == '\xe4', "mbc is %d\n", mbc);
302 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
303 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
304
305 SetLastError(0xdeadbeef);
306 ret = WideCharToMultiByte(1252, 0, &wc2, 1, &mbc, 1, NULL, bUsedDefaultChar);
307 ok(ret == 1, "ret is %d\n", ret);
308 ok(mbc == 63, "mbc is %d\n", mbc);
309 if(bUsedDefaultChar) ok(*bUsedDefaultChar == TRUE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
310 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
311
312 if (IsValidCodePage(1251))
313 {
314 SetLastError(0xdeadbeef);
315 ret = WideCharToMultiByte(1251, 0, &wc2, 1, &mbc, 1, NULL, bUsedDefaultChar);
316 ok(ret == 1, "ret is %d\n", ret);
317 ok(mbc == '\xf0', "mbc is %d\n", mbc);
318 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
319 ok(GetLastError() == 0xdeadbeef ||
320 broken(GetLastError() == 0), /* win95 */
321 "GetLastError() is %u\n", GetLastError());
322
323 SetLastError(0xdeadbeef);
324 ret = WideCharToMultiByte(1251, 0, &wc1, 1, &mbc, 1, NULL, bUsedDefaultChar);
325 ok(ret == 1, "ret is %d\n", ret);
326 ok(mbc == 97, "mbc is %d\n", mbc);
327 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
328 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
329 }
330 else
331 skip("Codepage 1251 not available\n");
332
333 /* This call triggers the last Win32 error */
334 SetLastError(0xdeadbeef);
335 ret = WideCharToMultiByte(1252, 0, wcs, -1, &mbc, 1, NULL, bUsedDefaultChar);
336 ok(ret == 0, "ret is %d\n", ret);
337 ok(mbc == 84, "mbc is %d\n", mbc);
338 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
339 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetLastError() is %u\n", GetLastError());
340
341 SetLastError(0xdeadbeef);
342 ret = WideCharToMultiByte(1252, 0, wcs, -1, mbs, sizeof(mbs), NULL, bUsedDefaultChar);
343 ok(ret == 5, "ret is %d\n", ret);
344 ok(!strcmp(mbs, "Th?i"), "mbs is %s\n", mbs);
345 if(bUsedDefaultChar) ok(*bUsedDefaultChar == TRUE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
346 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
347 mbs[0] = 0;
348
349 /* WideCharToMultiByte mustn't add any null character automatically.
350 So in this case, we should get the same string again, even if we only copied the first three bytes. */
351 SetLastError(0xdeadbeef);
352 ret = WideCharToMultiByte(1252, 0, wcs, 3, mbs, sizeof(mbs), NULL, bUsedDefaultChar);
353 ok(ret == 3, "ret is %d\n", ret);
354 ok(!strcmp(mbs, "Th?i"), "mbs is %s\n", mbs);
355 if(bUsedDefaultChar) ok(*bUsedDefaultChar == TRUE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
356 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
357 ZeroMemory(mbs, 5);
358
359 /* Now this shouldn't be the case like above as we zeroed the complete string buffer. */
360 SetLastError(0xdeadbeef);
361 ret = WideCharToMultiByte(1252, 0, wcs, 3, mbs, sizeof(mbs), NULL, bUsedDefaultChar);
362 ok(ret == 3, "ret is %d\n", ret);
363 ok(!strcmp(mbs, "Th?"), "mbs is %s\n", mbs);
364 if(bUsedDefaultChar) ok(*bUsedDefaultChar == TRUE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
365 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
366
367 /* Double-byte tests */
368 ret = WideCharToMultiByte(1252, 0, dbwcs, 3, mbs, sizeof(mbs), NULL, bUsedDefaultChar);
369 ok(ret == 3, "ret is %d\n", ret);
370 ok(!strcmp(mbs, "??"), "mbs is %s\n", mbs);
371 if(bUsedDefaultChar) ok(*bUsedDefaultChar == TRUE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
372
373 ret = WideCharToMultiByte(936, WC_COMPOSITECHECK, dbwcs2, -1, mbs, sizeof(mbs), (const char *)default_char, bUsedDefaultChar);
374 ok(ret == 10, "ret is %d\n", ret);
375 ok(!strcmp(mbs, "\xf3\xe7\x3d\xa3\xbf\xa3\xbf\xa3\xbf"), "mbs is %s\n", mbs);
376 if(bUsedDefaultChar) ok(*bUsedDefaultChar == TRUE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
377
378 /* Length-only tests */
379 SetLastError(0xdeadbeef);
380 ret = WideCharToMultiByte(1252, 0, &wc2, 1, NULL, 0, NULL, bUsedDefaultChar);
381 ok(ret == 1, "ret is %d\n", ret);
382 if(bUsedDefaultChar) ok(*bUsedDefaultChar == TRUE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
383 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
384
385 SetLastError(0xdeadbeef);
386 ret = WideCharToMultiByte(1252, 0, wcs, -1, NULL, 0, NULL, bUsedDefaultChar);
387 ok(ret == 5, "ret is %d\n", ret);
388 if(bUsedDefaultChar) ok(*bUsedDefaultChar == TRUE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
389 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
390
391 if (!IsValidCodePage(950))
392 {
393 skip("Codepage 950 not available\n");
394 return;
395 }
396
397 /* Double-byte tests */
398 SetLastError(0xdeadbeef);
399 ret = WideCharToMultiByte(950, 0, dbwcs, -1, mbs, sizeof(mbs), NULL, bUsedDefaultChar);
400 ok(ret == 5, "ret is %d\n", ret);
401 ok(!strcmp(mbs, "\xb5H\xa9\xd2"), "mbs is %s\n", mbs);
402 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
403 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
404
405 SetLastError(0xdeadbeef);
406 ret = WideCharToMultiByte(950, 0, dbwcs, 1, &mbc, 1, NULL, bUsedDefaultChar);
407 ok(ret == 0, "ret is %d\n", ret);
408 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
409 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetLastError() is %u\n", GetLastError());
410 ZeroMemory(mbs, 5);
411
412 SetLastError(0xdeadbeef);
413 ret = WideCharToMultiByte(950, 0, dbwcs, 1, mbs, sizeof(mbs), NULL, bUsedDefaultChar);
414 ok(ret == 2, "ret is %d\n", ret);
415 ok(!strcmp(mbs, "\xb5H"), "mbs is %s\n", mbs);
416 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
417 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
418
419 /* Length-only tests */
420 SetLastError(0xdeadbeef);
421 ret = WideCharToMultiByte(950, 0, dbwcs, 1, NULL, 0, NULL, bUsedDefaultChar);
422 ok(ret == 2, "ret is %d\n", ret);
423 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
424 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
425
426 SetLastError(0xdeadbeef);
427 ret = WideCharToMultiByte(950, 0, dbwcs, -1, NULL, 0, NULL, bUsedDefaultChar);
428 ok(ret == 5, "ret is %d\n", ret);
429 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
430 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %u\n", GetLastError());
431 }
432
433 static void test_utf7_encoding(void)
434 {
435 WCHAR input[16];
436 char output[16], expected[16];
437 int i, len, expected_len;
438
439 static const BOOL directly_encodable_table[] =
440 {
441 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, /* 0x00 - 0x0F */
442 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 - 0x1F */
443 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* 0x20 - 0x2F */
444 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 0x30 - 0x3F */
445 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40 - 0x4F */
446 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 0x50 - 0x5F */
447 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60 - 0x6F */
448 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 /* 0x70 - 0x7F */
449 };
450 static const char base64_encoding_table[] =
451 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
452
453 const struct
454 {
455 /* inputs */
456 WCHAR src[16];
457 int srclen;
458 char *dst;
459 int dstlen;
460 /* expected outputs */
461 char expected_dst[16];
462 int chars_written;
463 int len;
464 }
465 tests[] =
466 {
467 /* tests string conversion with srclen=-1 */
468 {
469 {0x4F60,0x597D,0x5417,0}, -1, output, sizeof(output) - 1,
470 "+T2BZfVQX-", 11, 11
471 },
472 /* tests string conversion with srclen=-2 */
473 {
474 {0x4F60,0x597D,0x5417,0}, -2, output, sizeof(output) - 1,
475 "+T2BZfVQX-", 11, 11
476 },
477 /* tests string conversion with dstlen=strlen(expected_dst) */
478 {
479 {0x4F60,0x597D,0x5417,0}, -1, output, 10,
480 "+T2BZfVQX-", 10, 0
481 },
482 /* tests string conversion with dstlen=strlen(expected_dst)+1 */
483 {
484 {0x4F60,0x597D,0x5417,0}, -1, output, 11,
485 "+T2BZfVQX-", 11, 11
486 },
487 /* tests string conversion with dstlen=strlen(expected_dst)+2 */
488 {
489 {0x4F60,0x597D,0x5417,0}, -1, output, 12,
490 "+T2BZfVQX-", 11, 11
491 },
492 /* tests dry run with dst=NULL and dstlen=0 */
493 {
494 {0x4F60,0x597D,0x5417,0}, -1, NULL, 0,
495 {0}, 0, 11
496 },
497 /* tests dry run with dst!=NULL and dstlen=0 */
498 {
499 {0x4F60,0x597D,0x5417,0}, -1, output, 0,
500 {0}, 0, 11
501 },
502 /* tests srclen < strlenW(src) with directly encodable chars */
503 {
504 {'h','e','l','l','o',0}, 2, output, sizeof(output) - 1,
505 "he", 2, 2
506 },
507 /* tests srclen < strlenW(src) with non-directly encodable chars */
508 {
509 {0x4F60,0x597D,0x5417,0}, 2, output, sizeof(output) - 1,
510 "+T2BZfQ-", 8, 8
511 },
512 /* tests a single null char */
513 {
514 {0}, -1, output, sizeof(output) - 1,
515 "", 1, 1
516 },
517 /* tests a buffer that runs out while not encoding a UTF-7 sequence */
518 {
519 {'h','e','l','l','o',0}, -1, output, 2,
520 "he", 2, 0
521 },
522 /* tests a buffer that runs out after writing 1 base64 character */
523 {
524 {0x4F60,0x0001,0}, -1, output, 2,
525 "+T", 2, 0
526 },
527 /* tests a buffer that runs out after writing 2 base64 characters */
528 {
529 {0x4F60,0x0001,0}, -1, output, 3,
530 "+T2", 3, 0
531 },
532 /* tests a buffer that runs out after writing 3 base64 characters */
533 {
534 {0x4F60,0x0001,0}, -1, output, 4,
535 "+T2A", 4, 0
536 },
537 /* tests a buffer that runs out just after writing the + sign */
538 {
539 {0x4F60,0}, -1, output, 1,
540 "+", 1, 0
541 },
542 /* tests a buffer that runs out just before writing the - sign
543 * the number of bits to encode here is evenly divisible by 6 */
544 {
545 {0x4F60,0x597D,0x5417,0}, -1, output, 9,
546 "+T2BZfVQX", 9, 0
547 },
548 /* tests a buffer that runs out just before writing the - sign
549 * the number of bits to encode here is NOT evenly divisible by 6 */
550 {
551 {0x4F60,0}, -1, output, 4,
552 "+T2", 3, 0
553 },
554 /* tests a buffer that runs out in the middle of escaping a + sign */
555 {
556 {'+',0}, -1, output, 1,
557 "+", 1, 0
558 }
559 };
560
561 /* test which characters are encoded if surrounded by non-encoded characters */
562 for (i = 0; i <= 0xFFFF; i++)
563 {
564 input[0] = ' ';
565 input[1] = i;
566 input[2] = ' ';
567 input[3] = 0;
568
569 memset(output, '#', sizeof(output) - 1);
570 output[sizeof(output) - 1] = 0;
571
572 len = WideCharToMultiByte(CP_UTF7, 0, input, 4, output, sizeof(output) - 1, NULL, NULL);
573
574 if (i == '+')
575 {
576 /* '+' is a special case and is encoded as "+-" */
577 expected_len = 5;
578 strcpy(expected, " +- ");
579 }
580 else if (i <= 0x7F && directly_encodable_table[i])
581 {
582 /* encodes directly */
583 expected_len = 4;
584 sprintf(expected, " %c ", i);
585 }
586 else
587 {
588 /* base64-encodes */
589 expected_len = 8;
590 sprintf(expected, " +%c%c%c- ",
591 base64_encoding_table[(i & 0xFC00) >> 10],
592 base64_encoding_table[(i & 0x03F0) >> 4],
593 base64_encoding_table[(i & 0x000F) << 2]);
594 }
595
596 ok(len == expected_len, "i=0x%04x: expected len=%i, got len=%i\n", i, expected_len, len);
597 ok(memcmp(output, expected, expected_len) == 0,
598 "i=0x%04x: expected output='%s', got output='%s'\n", i, expected, output);
599 ok(output[expected_len] == '#', "i=0x%04x: expected output[%i]='#', got output[%i]=%i\n",
600 i, expected_len, expected_len, output[expected_len]);
601 }
602
603 /* test which one-byte characters are absorbed into surrounding base64 blocks
604 * (Windows always ends the base64 block when it encounters a directly encodable character) */
605 for (i = 0; i <= 0xFFFF; i++)
606 {
607 input[0] = 0x2672;
608 input[1] = i;
609 input[2] = 0x2672;
610 input[3] = 0;
611
612 memset(output, '#', sizeof(output) - 1);
613 output[sizeof(output) - 1] = 0;
614
615 len = WideCharToMultiByte(CP_UTF7, 0, input, 4, output, sizeof(output) - 1, NULL, NULL);
616
617 if (i == '+')
618 {
619 /* '+' is a special case and is encoded as "+-" */
620 expected_len = 13;
621 strcpy(expected, "+JnI-+-+JnI-");
622 }
623 else if (i <= 0x7F && directly_encodable_table[i])
624 {
625 /* encodes directly */
626 expected_len = 12;
627 sprintf(expected, "+JnI-%c+JnI-", i);
628 }
629 else
630 {
631 /* base64-encodes */
632 expected_len = 11;
633 sprintf(expected, "+Jn%c%c%c%cZy-",
634 base64_encoding_table[8 | ((i & 0xC000) >> 14)],
635 base64_encoding_table[(i & 0x3F00) >> 8],
636 base64_encoding_table[(i & 0x00FC) >> 2],
637 base64_encoding_table[((i & 0x0003) << 4) | 2]);
638 }
639
640 ok(len == expected_len, "i=0x%04x: expected len=%i, got len=%i\n", i, expected_len, len);
641 ok(memcmp(output, expected, expected_len) == 0,
642 "i=0x%04x: expected output='%s', got output='%s'\n", i, expected, output);
643 ok(output[expected_len] == '#', "i=0x%04x: expected output[%i]='#', got output[%i]=%i\n",
644 i, expected_len, expected_len, output[expected_len]);
645 }
646
647 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++)
648 {
649 memset(output, '#', sizeof(output) - 1);
650 output[sizeof(output) - 1] = 0;
651 SetLastError(0xdeadbeef);
652
653 len = WideCharToMultiByte(CP_UTF7, 0, tests[i].src, tests[i].srclen,
654 tests[i].dst, tests[i].dstlen, NULL, NULL);
655
656 if (!tests[i].len)
657 {
658 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
659 "tests[%i]: expected error=0x%x, got error=0x%x\n",
660 i, ERROR_INSUFFICIENT_BUFFER, GetLastError());
661 }
662 ok(len == tests[i].len, "tests[%i]: expected len=%i, got len=%i\n", i, tests[i].len, len);
663
664 if (tests[i].dst)
665 {
666 ok(memcmp(tests[i].dst, tests[i].expected_dst, tests[i].chars_written) == 0,
667 "tests[%i]: expected dst='%s', got dst='%s'\n",
668 i, tests[i].expected_dst, tests[i].dst);
669 ok(tests[i].dst[tests[i].chars_written] == '#',
670 "tests[%i]: expected dst[%i]='#', got dst[%i]=%i\n",
671 i, tests[i].chars_written, tests[i].chars_written, tests[i].dst[tests[i].chars_written]);
672 }
673 }
674 }
675
676 static void test_utf7_decoding(void)
677 {
678 char input[32];
679 WCHAR output[32], expected[32];
680 int i, len, expected_len;
681
682 static const signed char base64_decoding_table[] =
683 {
684 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x00-0x0F */
685 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x10-0x1F */
686 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, /* 0x20-0x2F */
687 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, /* 0x30-0x3F */
688 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 0x40-0x4F */
689 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 0x50-0x5F */
690 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /* 0x60-0x6F */
691 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 /* 0x70-0x7F */
692 };
693
694 struct
695 {
696 /* inputs */
697 char src[32];
698 int srclen;
699 WCHAR *dst;
700 int dstlen;
701 /* expected outputs */
702 WCHAR expected_dst[32];
703 int chars_written;
704 int len;
705 }
706 tests[] =
707 {
708 /* tests string conversion with srclen=-1 */
709 {
710 "+T2BZfQ-", -1, output, sizeof(output) / sizeof(WCHAR) - 1,
711 {0x4F60,0x597D,0}, 3, 3
712 },
713 /* tests string conversion with srclen=-2 */
714 {
715 "+T2BZfQ-", -2, output, sizeof(output) / sizeof(WCHAR) - 1,
716 {0x4F60,0x597D,0}, 3, 3
717 },
718 /* tests string conversion with dstlen=strlen(expected_dst) */
719 {
720 "+T2BZfQ-", -1, output, 2,
721 {0x4F60,0x597D}, 2, 0
722 },
723 /* tests string conversion with dstlen=strlen(expected_dst)+1 */
724 {
725 "+T2BZfQ-", -1, output, 3,
726 {0x4F60,0x597D,0}, 3, 3
727 },
728 /* tests string conversion with dstlen=strlen(expected_dst)+2 */
729 {
730 "+T2BZfQ-", -1, output, 4,
731 {0x4F60,0x597D,0}, 3, 3
732 },
733 /* tests dry run with dst=NULL and dstlen=0 */
734 {
735 "+T2BZfQ-", -1, NULL, 0,
736 {0}, 0, 3
737 },
738 /* tests dry run with dst!=NULL and dstlen=0 */
739 {
740 "+T2BZfQ-", -1, output, 0,
741 {0}, 0, 3
742 },
743 /* tests ill-formed UTF-7: 6 bits, not enough for a byte pair */
744 {
745 "+T-+T-+T-hello", -1, output, sizeof(output) / sizeof(WCHAR) - 1,
746 {'h','e','l','l','o',0}, 6, 6
747 },
748 /* tests ill-formed UTF-7: 12 bits, not enough for a byte pair */
749 {
750 "+T2-+T2-+T2-hello", -1, output, sizeof(output) / sizeof(WCHAR) - 1,
751 {'h','e','l','l','o',0}, 6, 6
752 },
753 /* tests ill-formed UTF-7: 18 bits, not a multiple of 16 and the last bit is a 1 */
754 {
755 "+T2B-+T2B-+T2B-hello", -1, output, sizeof(output) / sizeof(WCHAR) - 1,
756 {0x4F60,0x4F60,0x4F60,'h','e','l','l','o',0}, 9, 9
757 },
758 /* tests ill-formed UTF-7: 24 bits, a multiple of 8 but not a multiple of 16 */
759 {
760 "+T2BZ-+T2BZ-+T2BZ-hello", -1, output, sizeof(output) / sizeof(WCHAR) - 1,
761 {0x4F60,0x4F60,0x4F60,'h','e','l','l','o',0}, 9, 9
762 },
763 /* tests UTF-7 followed by characters that should be encoded but aren't */
764 {
765 "+T2BZ-\x82\xFE", -1, output, sizeof(output) / sizeof(WCHAR) - 1,
766 {0x4F60,0x0082,0x00FE,0}, 4, 4
767 },
768 /* tests srclen > strlen(src) */
769 {
770 "a\0b", 4, output, sizeof(output) / sizeof(WCHAR) - 1,
771 {'a',0,'b',0}, 4, 4
772 },
773 /* tests srclen < strlen(src) outside of a UTF-7 sequence */
774 {
775 "hello", 2, output, sizeof(output) / sizeof(WCHAR) - 1,
776 {'h','e'}, 2, 2
777 },
778 /* tests srclen < strlen(src) inside of a UTF-7 sequence */
779 {
780 "+T2BZfQ-", 4, output, sizeof(output) / sizeof(WCHAR) - 1,
781 {0x4F60}, 1, 1
782 },
783 /* tests srclen < strlen(src) right at the beginning of a UTF-7 sequence */
784 {
785 "hi+T2A-", 3, output, sizeof(output) / sizeof(WCHAR) - 1,
786 {'h','i'}, 2, 2
787 },
788 /* tests srclen < strlen(src) right at the end of a UTF-7 sequence */
789 {
790 "+T2A-hi", 5, output, sizeof(output) / sizeof(WCHAR) - 1,
791 {0x4F60}, 1, 1
792 },
793 /* tests srclen < strlen(src) at the beginning of an escaped + sign */
794 {
795 "hi+-", 3, output, sizeof(output) / sizeof(WCHAR) - 1,
796 {'h','i'}, 2, 2
797 },
798 /* tests srclen < strlen(src) at the end of an escaped + sign */
799 {
800 "+-hi", 2, output, sizeof(output) / sizeof(WCHAR) - 1,
801 {'+'}, 1, 1
802 },
803 /* tests len=0 but no error */
804 {
805 "+", 1, output, sizeof(output) / sizeof(WCHAR) - 1,
806 {0}, 0, 0
807 },
808 /* tests a single null char */
809 {
810 "", -1, output, sizeof(output) / sizeof(WCHAR) - 1,
811 {0}, 1, 1
812 },
813 /* tests a buffer that runs out while not decoding a UTF-7 sequence */
814 {
815 "hello", -1, output, 2,
816 {'h','e'}, 2, 0
817 },
818 /* tests a buffer that runs out in the middle of decoding a UTF-7 sequence */
819 {
820 "+T2BZfQ-", -1, output, 1,
821 {0x4F60}, 1, 0
822 }
823 };
824
825 /* test which one-byte characters remove stray + signs */
826 for (i = 0; i < 256; i++)
827 {
828 sprintf(input, "+%c+AAA", i);
829
830 memset(output, 0x23, sizeof(output) - sizeof(WCHAR));
831 output[sizeof(output) / sizeof(WCHAR) - 1] = 0;
832
833 len = MultiByteToWideChar(CP_UTF7, 0, input, 7, output, sizeof(output) / sizeof(WCHAR) - 1);
834
835 if (i == '-')
836 {
837 /* removes the - sign */
838 expected_len = 3;
839 expected[0] = 0x002B;
840 expected[1] = 0;
841 expected[2] = 0;
842 }
843 else if (i <= 0x7F && base64_decoding_table[i] != -1)
844 {
845 /* absorbs the character into the base64 sequence */
846 expected_len = 2;
847 expected[0] = (base64_decoding_table[i] << 10) | 0x03E0;
848 expected[1] = 0;
849 }
850 else
851 {
852 /* removes the + sign */
853 expected_len = 3;
854 expected[0] = i;
855 expected[1] = 0;
856 expected[2] = 0;
857 }
858 expected[expected_len] = 0x2323;
859
860 ok(len == expected_len, "i=0x%02x: expected len=%i, got len=%i\n", i, expected_len, len);
861 ok(memcmp(output, expected, (expected_len + 1) * sizeof(WCHAR)) == 0,
862 "i=0x%02x: expected output=%s, got output=%s\n",
863 i, wine_dbgstr_wn(expected, expected_len + 1), wine_dbgstr_wn(output, expected_len + 1));
864 }
865
866 /* test which one-byte characters terminate a sequence
867 * also test whether the unfinished byte pair is discarded or not */
868 for (i = 0; i < 256; i++)
869 {
870 sprintf(input, "+B%c+AAA", i);
871
872 memset(output, 0x23, sizeof(output) - sizeof(WCHAR));
873 output[sizeof(output) / sizeof(WCHAR) - 1] = 0;
874
875 len = MultiByteToWideChar(CP_UTF7, 0, input, 8, output, sizeof(output) / sizeof(WCHAR) - 1);
876
877 if (i == '-')
878 {
879 /* explicitly terminates */
880 expected_len = 2;
881 expected[0] = 0;
882 expected[1] = 0;
883 }
884 else if (i <= 0x7F)
885 {
886 if (base64_decoding_table[i] != -1)
887 {
888 /* absorbs the character into the base64 sequence */
889 expected_len = 3;
890 expected[0] = 0x0400 | (base64_decoding_table[i] << 4) | 0x000F;
891 expected[1] = 0x8000;
892 expected[2] = 0;
893 }
894 else
895 {
896 /* implicitly terminates and discards the unfinished byte pair */
897 expected_len = 3;
898 expected[0] = i;
899 expected[1] = 0;
900 expected[2] = 0;
901 }
902 }
903 else
904 {
905 /* implicitly terminates but does not the discard unfinished byte pair */
906 expected_len = 3;
907 expected[0] = i;
908 expected[1] = 0x0400;
909 expected[2] = 0;
910 }
911 expected[expected_len] = 0x2323;
912
913 ok(len == expected_len, "i=0x%02x: expected len=%i, got len=%i\n", i, expected_len, len);
914 ok(memcmp(output, expected, (expected_len + 1) * sizeof(WCHAR)) == 0,
915 "i=0x%02x: expected output=%s, got output=%s\n",
916 i, wine_dbgstr_wn(expected, expected_len + 1), wine_dbgstr_wn(output, expected_len + 1));
917 }
918
919 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++)
920 {
921 memset(output, 0x23, sizeof(output) - sizeof(WCHAR));
922 output[sizeof(output) / sizeof(WCHAR) - 1] = 0;
923 SetLastError(0xdeadbeef);
924
925 len = MultiByteToWideChar(CP_UTF7, 0, tests[i].src, tests[i].srclen,
926 tests[i].dst, tests[i].dstlen);
927
928 tests[i].expected_dst[tests[i].chars_written] = 0x2323;
929
930 if (!tests[i].len && tests[i].chars_written)
931 {
932 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
933 "tests[%i]: expected error=0x%x, got error=0x%x\n",
934 i, ERROR_INSUFFICIENT_BUFFER, GetLastError());
935 }
936 ok(len == tests[i].len, "tests[%i]: expected len=%i, got len=%i\n", i, tests[i].len, len);
937
938 if (tests[i].dst)
939 {
940 ok(memcmp(tests[i].dst, tests[i].expected_dst, (tests[i].chars_written + 1) * sizeof(WCHAR)) == 0,
941 "tests[%i]: expected dst=%s, got dst=%s\n",
942 i, wine_dbgstr_wn(tests[i].expected_dst, tests[i].chars_written + 1),
943 wine_dbgstr_wn(tests[i].dst, tests[i].chars_written + 1));
944 }
945 }
946 }
947
948 static void test_undefined_byte_char(void)
949 {
950 static const struct tag_testset {
951 INT codepage;
952 LPCSTR str;
953 BOOL is_error;
954 } testset[] = {
955 { 874, "\xdd", TRUE },
956 { 932, "\xfe", TRUE },
957 { 932, "\x80", FALSE },
958 { 936, "\xff", TRUE },
959 { 949, "\xff", TRUE },
960 { 950, "\xff", TRUE },
961 { 1252, "\x90", FALSE },
962 { 1253, "\xaa", TRUE },
963 { 1255, "\xff", TRUE },
964 { 1257, "\xa5", TRUE },
965 };
966 INT i, ret;
967
968 for (i = 0; i < (sizeof(testset) / sizeof(testset[0])); i++) {
969 if (! IsValidCodePage(testset[i].codepage))
970 {
971 skip("Codepage %d not available\n", testset[i].codepage);
972 continue;
973 }
974
975 SetLastError(0xdeadbeef);
976 ret = MultiByteToWideChar(testset[i].codepage, MB_ERR_INVALID_CHARS,
977 testset[i].str, -1, NULL, 0);
978 if (testset[i].is_error) {
979 ok(ret == 0 && GetLastError() == ERROR_NO_UNICODE_TRANSLATION,
980 "ret is %d, GetLastError is %u (cp %d)\n",
981 ret, GetLastError(), testset[i].codepage);
982 }
983 else {
984 ok(ret == strlen(testset[i].str)+1 && GetLastError() == 0xdeadbeef,
985 "ret is %d, GetLastError is %u (cp %d)\n",
986 ret, GetLastError(), testset[i].codepage);
987 }
988
989 SetLastError(0xdeadbeef);
990 ret = MultiByteToWideChar(testset[i].codepage, 0,
991 testset[i].str, -1, NULL, 0);
992 ok(ret == strlen(testset[i].str)+1 && GetLastError() == 0xdeadbeef,
993 "ret is %d, GetLastError is %u (cp %d)\n",
994 ret, GetLastError(), testset[i].codepage);
995 }
996 }
997
998 static void test_threadcp(void)
999 {
1000 static const LCID ENGLISH = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
1001 static const LCID HINDI = MAKELCID(MAKELANGID(LANG_HINDI, SUBLANG_HINDI_INDIA), SORT_DEFAULT);
1002 static const LCID GEORGIAN = MAKELCID(MAKELANGID(LANG_GEORGIAN, SUBLANG_GEORGIAN_GEORGIA), SORT_DEFAULT);
1003 static const LCID RUSSIAN = MAKELCID(MAKELANGID(LANG_RUSSIAN, SUBLANG_RUSSIAN_RUSSIA), SORT_DEFAULT);
1004 static const LCID JAPANESE = MAKELCID(MAKELANGID(LANG_JAPANESE, SUBLANG_JAPANESE_JAPAN), SORT_DEFAULT);
1005 static const LCID CHINESE = MAKELCID(MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED), SORT_DEFAULT);
1006
1007 BOOL islead, islead_acp;
1008 CPINFOEXA cpi;
1009 UINT cp, acp;
1010 int i, num;
1011 LCID last;
1012 BOOL ret;
1013
1014 struct test {
1015 LCID lcid;
1016 UINT threadcp;
1017 } lcids[] = {
1018 { HINDI, 0 },
1019 { GEORGIAN, 0 },
1020 { ENGLISH, 1252 },
1021 { RUSSIAN, 1251 },
1022 { JAPANESE, 932 },
1023 { CHINESE, 936 }
1024 };
1025
1026 struct test_islead_nocp {
1027 LCID lcid;
1028 BYTE testchar;
1029 } isleads_nocp[] = {
1030 { HINDI, 0x00 },
1031 { HINDI, 0x81 },
1032 { HINDI, 0xa0 },
1033 { HINDI, 0xe0 },
1034
1035 { GEORGIAN, 0x00 },
1036 { GEORGIAN, 0x81 },
1037 { GEORGIAN, 0xa0 },
1038 { GEORGIAN, 0xe0 },
1039 };
1040
1041 struct test_islead {
1042 LCID lcid;
1043 BYTE testchar;
1044 BOOL islead;
1045 } isleads[] = {
1046 { ENGLISH, 0x00, FALSE },
1047 { ENGLISH, 0x81, FALSE },
1048 { ENGLISH, 0xa0, FALSE },
1049 { ENGLISH, 0xe0, FALSE },
1050
1051 { RUSSIAN, 0x00, FALSE },
1052 { RUSSIAN, 0x81, FALSE },
1053 { RUSSIAN, 0xa0, FALSE },
1054 { RUSSIAN, 0xe0, FALSE },
1055
1056 { JAPANESE, 0x00, FALSE },
1057 { JAPANESE, 0x81, TRUE },
1058 { JAPANESE, 0xa0, FALSE },
1059 { JAPANESE, 0xe0, TRUE },
1060
1061 { CHINESE, 0x00, FALSE },
1062 { CHINESE, 0x81, TRUE },
1063 { CHINESE, 0xa0, TRUE },
1064 { CHINESE, 0xe0, TRUE },
1065 };
1066
1067 last = GetThreadLocale();
1068 acp = GetACP();
1069
1070 for (i = 0; i < sizeof(lcids)/sizeof(lcids[0]); i++)
1071 {
1072 SetThreadLocale(lcids[i].lcid);
1073
1074 cp = 0xdeadbeef;
1075 GetLocaleInfoA(lcids[i].lcid, LOCALE_IDEFAULTANSICODEPAGE|LOCALE_RETURN_NUMBER, (LPSTR)&cp, sizeof(cp));
1076 ok(cp == lcids[i].threadcp, "wrong codepage %u for lcid %04x, should be %u\n", cp, lcids[i].threadcp, cp);
1077
1078 /* GetCPInfoEx/GetCPInfo - CP_ACP */
1079 SetLastError(0xdeadbeef);
1080 memset(&cpi, 0, sizeof(cpi));
1081 ret = GetCPInfoExA(CP_ACP, 0, &cpi);
1082 ok(ret, "GetCPInfoExA failed for lcid %04x, error %d\n", lcids[i].lcid, GetLastError());
1083 ok(cpi.CodePage == acp, "wrong codepage %u for lcid %04x, should be %u\n", cpi.CodePage, lcids[i].lcid, acp);
1084
1085 /* WideCharToMultiByte - CP_ACP */
1086 num = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, NULL, 0, NULL, NULL);
1087 ok(num == 7, "ret is %d (%04x)\n", num, lcids[i].lcid);
1088
1089 /* MultiByteToWideChar - CP_ACP */
1090 num = MultiByteToWideChar(CP_ACP, 0, "foobar", -1, NULL, 0);
1091 ok(num == 7, "ret is %d (%04x)\n", num, lcids[i].lcid);
1092
1093 /* GetCPInfoEx/GetCPInfo - CP_THREAD_ACP */
1094 SetLastError(0xdeadbeef);
1095 memset(&cpi, 0, sizeof(cpi));
1096 ret = GetCPInfoExA(CP_THREAD_ACP, 0, &cpi);
1097 ok(ret, "GetCPInfoExA failed for lcid %04x, error %d\n", lcids[i].lcid, GetLastError());
1098 if (lcids[i].threadcp)
1099 ok(cpi.CodePage == lcids[i].threadcp, "wrong codepage %u for lcid %04x, should be %u\n",
1100 cpi.CodePage, lcids[i].lcid, lcids[i].threadcp);
1101 else
1102 ok(cpi.CodePage == acp, "wrong codepage %u for lcid %04x, should be %u\n",
1103 cpi.CodePage, lcids[i].lcid, acp);
1104
1105 /* WideCharToMultiByte - CP_THREAD_ACP */
1106 num = WideCharToMultiByte(CP_THREAD_ACP, 0, foobarW, -1, NULL, 0, NULL, NULL);
1107 ok(num == 7, "ret is %d (%04x)\n", num, lcids[i].lcid);
1108
1109 /* MultiByteToWideChar - CP_THREAD_ACP */
1110 num = MultiByteToWideChar(CP_THREAD_ACP, 0, "foobar", -1, NULL, 0);
1111 ok(num == 7, "ret is %d (%04x)\n", num, lcids[i].lcid);
1112 }
1113
1114 /* IsDBCSLeadByteEx - locales without codepage */
1115 for (i = 0; i < sizeof(isleads_nocp)/sizeof(isleads_nocp[0]); i++)
1116 {
1117 SetThreadLocale(isleads_nocp[i].lcid);
1118
1119 islead_acp = IsDBCSLeadByteEx(CP_ACP, isleads_nocp[i].testchar);
1120 islead = IsDBCSLeadByteEx(CP_THREAD_ACP, isleads_nocp[i].testchar);
1121
1122 ok(islead == islead_acp, "wrong islead %i for test char %x in lcid %04x. should be %i\n",
1123 islead, isleads_nocp[i].testchar, isleads_nocp[i].lcid, islead_acp);
1124 }
1125
1126 /* IsDBCSLeadByteEx - locales with codepage */
1127 for (i = 0; i < sizeof(isleads)/sizeof(isleads[0]); i++)
1128 {
1129 SetThreadLocale(isleads[i].lcid);
1130
1131 islead = IsDBCSLeadByteEx(CP_THREAD_ACP, isleads[i].testchar);
1132 ok(islead == isleads[i].islead, "wrong islead %i for test char %x in lcid %04x. should be %i\n",
1133 islead, isleads[i].testchar, isleads[i].lcid, isleads[i].islead);
1134 }
1135
1136 SetThreadLocale(last);
1137 }
1138
1139 static void test_dbcs_to_widechar(void)
1140 {
1141 int i, count, count2;
1142 WCHAR wbuf[5];
1143 unsigned char buf[] = {0xbf, 0xb4, 0xc7, '\0', 'x'};
1144 static const DWORD flags[] = {
1145 MB_PRECOMPOSED,
1146 MB_COMPOSITE,
1147
1148 MB_PRECOMPOSED|MB_USEGLYPHCHARS,
1149 MB_COMPOSITE |MB_USEGLYPHCHARS,
1150
1151 MB_PRECOMPOSED|MB_ERR_INVALID_CHARS,
1152 MB_COMPOSITE |MB_ERR_INVALID_CHARS,
1153
1154 MB_PRECOMPOSED|MB_ERR_INVALID_CHARS|MB_USEGLYPHCHARS,
1155 MB_COMPOSITE |MB_ERR_INVALID_CHARS|MB_USEGLYPHCHARS,
1156 };
1157
1158 for (i = 0; i < sizeof(flags)/sizeof(DWORD); ++i)
1159 {
1160 memset(wbuf, 0xff, sizeof(wbuf));
1161 count = MultiByteToWideChar(936, flags[i], (char*)&buf[0], 2, NULL, 0);
1162 count2 = MultiByteToWideChar(936, flags[i], (char*)&buf[0], 2, wbuf, count);
1163
1164 ok(count == 1, "%04x: returned %d (expected 1)\n", flags[i], count);
1165 ok(count2 == 1, "%04x: returned %d (expected 1)\n", flags[i], count2);
1166 ok(wbuf[0] == 0x770b, "%04x: returned %04x (expected 770b)\n", flags[i], wbuf[0]);
1167 ok(wbuf[1] == 0xffff, "%04x: returned %04x (expected ffff)\n", flags[i], wbuf[1]);
1168 }
1169
1170 for (i = 0; i < sizeof(flags)/sizeof(DWORD); ++i)
1171 {
1172 memset(wbuf, 0xff, sizeof(wbuf));
1173 count = MultiByteToWideChar(936, flags[i], (char*)&buf[0], 3, NULL, 0);
1174 SetLastError( 0xdeadbeef );
1175 count2 = MultiByteToWideChar(936, flags[i], (char*)&buf[0], 3, wbuf, count);
1176
1177 if (flags[i] & MB_ERR_INVALID_CHARS)
1178 {
1179 ok(count == 0, "%04x: returned %d (expected 0)\n", flags[i], count);
1180 ok(count2 == 0, "%04x: returned %d (expected 0)\n", flags[i], count2);
1181 ok(GetLastError() == ERROR_NO_UNICODE_TRANSLATION, "%04x: returned %d (expected %d)\n",
1182 flags[i], GetLastError(), ERROR_NO_UNICODE_TRANSLATION);
1183 }
1184 else
1185 {
1186 ok(count == 2, "%04x: returned %d (expected 2)\n", flags[i], count);
1187 ok(count2 == 2, "%04x: returned %d (expected 2)\n", flags[i], count2);
1188 ok(wbuf[0] == 0x770b, "%04x: returned %04x (expected 770b)\n", flags[i], wbuf[0]);
1189 ok(wbuf[1] == 0x003f || broken(wbuf[1] == 0), /*windows xp*/
1190 "%04x: wrong wide char: %04x\n", flags[i], wbuf[1]);
1191 ok(wbuf[2] == 0xffff, "%04x: returned %04x (expected ffff)\n", flags[i], wbuf[2]);
1192 }
1193 }
1194
1195 /* src ends with null character */
1196 for (i = 0; i < sizeof(flags)/sizeof(DWORD); ++i)
1197 {
1198 memset(wbuf, 0xff, sizeof(wbuf));
1199 count = MultiByteToWideChar(936, flags[i], (char*)&buf[0], 4, NULL, 0);
1200 SetLastError( 0xdeadbeef );
1201 count2 = MultiByteToWideChar(936, flags[i], (char*)&buf[0], 4, wbuf, count);
1202 ok(count == count2, "%04x: returned %d (expected %d)\n", flags[i], count2, count);
1203
1204 if (flags[i] & MB_ERR_INVALID_CHARS)
1205 {
1206 ok(count == 0, "%04x: returned %d (expected 0)\n", flags[i], count);
1207 ok(GetLastError() == ERROR_NO_UNICODE_TRANSLATION, "%04x: returned %d (expected %d)\n",
1208 flags[i], GetLastError(), ERROR_NO_UNICODE_TRANSLATION);
1209 }
1210 else
1211 {
1212 WCHAR wbuf_ok[] = { 0x770b, 0x003f, '\0', 0xffff };
1213 WCHAR wbuf_broken[] = { 0x770b, '\0', 0xffff, 0xffff };
1214 ok(count == 3 || broken(count == 2 /*windows xp*/),
1215 "%04x: returned %d (expected 3)\n", flags[i], count);
1216 ok(!memcmp(wbuf, wbuf_ok, sizeof(wbuf_ok))
1217 || broken(!memcmp(wbuf, wbuf_broken, sizeof(wbuf_broken))),
1218 "%04x: returned %04x %04x %04x %04x (expected %04x %04x %04x %04x)\n",
1219 flags[i], wbuf[0], wbuf[1], wbuf[2], wbuf[3],
1220 wbuf_ok[0], wbuf_ok[1], wbuf_ok[2], wbuf_ok[3]);
1221 }
1222 }
1223
1224 /* src has null character, but not ends with it */
1225 for (i = 0; i < sizeof(flags)/sizeof(DWORD); ++i)
1226 {
1227 memset(wbuf, 0xff, sizeof(wbuf));
1228 count = MultiByteToWideChar(936, flags[i], (char*)&buf[0], 5, NULL, 0);
1229 SetLastError( 0xdeadbeef );
1230 count2 = MultiByteToWideChar(936, flags[i], (char*)&buf[0], 5, wbuf, count);
1231 ok(count == count2, "%04x: returned %d (expected %d)\n", flags[i], count2, count);
1232
1233 if (flags[i] & MB_ERR_INVALID_CHARS)
1234 {
1235 ok(count == 0, "%04x: returned %d (expected 0)\n", flags[i], count);
1236 ok(GetLastError() == ERROR_NO_UNICODE_TRANSLATION, "%04x: returned %d (expected %d)\n",
1237 flags[i], GetLastError(), ERROR_NO_UNICODE_TRANSLATION);
1238 }
1239 else
1240 {
1241 WCHAR wbuf_ok[] = { 0x770b, 0x003f, '\0', 'x', 0xffff };
1242 WCHAR wbuf_broken[] = { 0x770b, '\0', 'x', 0xffff, 0xffff };
1243 ok(count == 4 || broken(count == 3),
1244 "%04x: returned %d (expected 4)\n", flags[i], count);
1245 ok(!memcmp(wbuf, wbuf_ok, sizeof(wbuf_ok))
1246 || broken(!memcmp(wbuf, wbuf_broken, sizeof(wbuf_broken))),
1247 "%04x: returned %04x %04x %04x %04x %04x (expected %04x %04x %04x %04x %04x)\n",
1248 flags[i], wbuf[0], wbuf[1], wbuf[2], wbuf[3], wbuf[4],
1249 wbuf_ok[0], wbuf_ok[1], wbuf_ok[2], wbuf_ok[3], wbuf_ok[4]);
1250 }
1251 }
1252 }
1253
1254 START_TEST(codepage)
1255 {
1256 BOOL bUsedDefaultChar;
1257
1258 test_destination_buffer();
1259 test_null_source();
1260 test_negative_source_length();
1261 test_negative_dest_length();
1262 test_other_invalid_parameters();
1263 test_overlapped_buffers();
1264
1265 /* WideCharToMultiByte has two code paths, test both here */
1266 test_string_conversion(NULL);
1267 test_string_conversion(&bUsedDefaultChar);
1268
1269 test_utf7_encoding();
1270 test_utf7_decoding();
1271
1272 test_undefined_byte_char();
1273 test_threadcp();
1274
1275 test_dbcs_to_widechar();
1276 }