d3d10/effect: Add support for 'imul' instruction.
[wine.git] / dlls / kernel32 / tests / codepage.c
blob08d2514e3e4ad68bddbb7dcbbb5c9430bf92d856
1 /*
2 * Unit tests for code page to/from unicode translations
4 * Copyright (c) 2002 Dmitry Timoshkov
5 * Copyright (c) 2008 Colin Finck
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.
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.
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
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <limits.h>
26 #include "wine/test.h"
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winnls.h"
31 static const char foobarA[] = "foobar";
32 static const WCHAR foobarW[] = {'f','o','o','b','a','r',0};
34 static void test_destination_buffer(void)
36 LPSTR buffer;
37 INT maxsize;
38 INT needed;
39 INT len;
41 SetLastError(0xdeadbeef);
42 needed = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, NULL, 0, NULL, NULL);
43 ok( (needed > 0), "returned %d with %lu (expected '> 0')\n",
44 needed, GetLastError());
46 maxsize = needed*2;
47 buffer = HeapAlloc(GetProcessHeap(), 0, maxsize);
48 if (buffer == NULL) return;
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 %lu and '%s' (expected '> 0')\n",
56 len, GetLastError(), buffer);
57 ok(!lstrcmpA(buffer, "foobar"), "expected \"foobar\" got \"%s\"\n", buffer);
59 memset(buffer, 'x', maxsize);
60 buffer[maxsize] = '\0';
61 SetLastError(0xdeadbeef);
62 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, buffer, needed, NULL, NULL);
63 ok( (len > 0), "returned %d with %lu and '%s' (expected '> 0')\n",
64 len, GetLastError(), buffer);
65 ok(!lstrcmpA(buffer, "foobar"), "expected \"foobar\" got \"%s\"\n", buffer);
67 memset(buffer, 'x', maxsize);
68 buffer[maxsize] = '\0';
69 SetLastError(0xdeadbeef);
70 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, buffer, needed-1, NULL, NULL);
71 ok( !len && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
72 "returned %d with %lu and '%s' (expected '0' with "
73 "ERROR_INSUFFICIENT_BUFFER)\n", len, GetLastError(), buffer);
74 ok(!strncmp(buffer, "foobar", 6), "expected \"foobar\" got \"%s\"\n", buffer);
75 ok(buffer[6] == 'x', "expected buf[5]=='x', got \"%s\"\n", buffer);
77 memset(buffer, 'x', maxsize);
78 buffer[maxsize] = '\0';
79 SetLastError(0xdeadbeef);
80 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, buffer, 1, NULL, NULL);
81 ok( !len && (GetLastError() == ERROR_INSUFFICIENT_BUFFER),
82 "returned %d with %lu and '%s' (expected '0' with "
83 "ERROR_INSUFFICIENT_BUFFER)\n", len, GetLastError(), buffer);
84 ok(buffer[0] == 'f', "expected buf[1]=='f', got \"%s\"\n", buffer);
85 ok(buffer[1] == 'x', "expected buf[1]=='x', got \"%s\"\n", buffer);
87 SetLastError(0xdeadbeef);
88 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, buffer, 0, NULL, NULL);
89 ok( (len > 0), "returned %d with %lu (expected '> 0')\n",
90 len, GetLastError());
92 SetLastError(0xdeadbeef);
93 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, NULL, needed, NULL, NULL);
94 ok( !len && (GetLastError() == ERROR_INVALID_PARAMETER),
95 "returned %d with %lu (expected '0' with "
96 "ERROR_INVALID_PARAMETER)\n", len, GetLastError());
98 HeapFree(GetProcessHeap(), 0, buffer);
102 static void test_null_source(void)
104 int len;
105 DWORD GLE;
107 SetLastError(0);
108 len = WideCharToMultiByte(CP_ACP, 0, NULL, 0, NULL, 0, NULL, NULL);
109 GLE = GetLastError();
110 ok(!len && GLE == ERROR_INVALID_PARAMETER,
111 "WideCharToMultiByte returned %d with GLE=%lu (expected 0 with ERROR_INVALID_PARAMETER)\n",
112 len, GLE);
114 SetLastError(0);
115 len = WideCharToMultiByte(CP_ACP, 0, NULL, -1, NULL, 0, NULL, NULL);
116 GLE = GetLastError();
117 ok(!len && GLE == ERROR_INVALID_PARAMETER,
118 "WideCharToMultiByte returned %d with GLE=%lu (expected 0 with ERROR_INVALID_PARAMETER)\n",
119 len, GLE);
122 static void test_negative_source_length(void)
124 int len;
125 char buf[10];
126 WCHAR bufW[10];
128 /* Test, whether any negative source length works as strlen() + 1 */
129 SetLastError( 0xdeadbeef );
130 memset(buf,'x',sizeof(buf));
131 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -2002, buf, 10, NULL, NULL);
132 ok(len == 7 && GetLastError() == 0xdeadbeef,
133 "WideCharToMultiByte(-2002): len=%d error=%lu\n", len, GetLastError());
134 ok(!lstrcmpA(buf, "foobar"),
135 "WideCharToMultiByte(-2002): expected \"foobar\" got \"%s\"\n", buf);
137 SetLastError( 0xdeadbeef );
138 memset(bufW,'x',sizeof(bufW));
139 len = MultiByteToWideChar(CP_ACP, 0, "foobar", -2002, bufW, 10);
140 ok(len == 7 && !lstrcmpW(bufW, foobarW) && GetLastError() == 0xdeadbeef,
141 "MultiByteToWideChar(-2002): len=%d error=%lu\n", len, GetLastError());
143 SetLastError(0xdeadbeef);
144 memset(bufW, 'x', sizeof(bufW));
145 len = MultiByteToWideChar(CP_ACP, 0, "foobar", -1, bufW, 6);
146 ok(len == 0 && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
147 "MultiByteToWideChar(-1): len=%d error=%lu\n", len, GetLastError());
150 #define LONGBUFLEN 100000
151 static void test_negative_dest_length(void)
153 int len, i;
154 static WCHAR bufW[LONGBUFLEN];
155 static char bufA[LONGBUFLEN];
156 static WCHAR originalW[LONGBUFLEN];
157 static char originalA[LONGBUFLEN];
158 DWORD theError;
160 /* Test return on -1 dest length */
161 SetLastError( 0xdeadbeef );
162 memset(bufA,'x',sizeof(bufA));
163 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, bufA, -1, NULL, NULL);
164 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER,
165 "WideCharToMultiByte(destlen -1): len=%d error=%lx\n", len, GetLastError());
167 SetLastError( 0xdeadbeef );
168 memset(bufW,'x',sizeof(bufW));
169 len = MultiByteToWideChar(CP_ACP, 0, foobarA, -1, bufW, -1);
170 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER,
171 "MultiByteToWideChar(destlen -1): len=%d error=%lx\n", len, GetLastError());
173 /* Test return on -1000 dest length */
174 SetLastError( 0xdeadbeef );
175 memset(bufA,'x',sizeof(bufA));
176 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, bufA, -1000, NULL, NULL);
177 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER,
178 "WideCharToMultiByte(destlen -1000): len=%d error=%lx\n", len, GetLastError());
180 SetLastError( 0xdeadbeef );
181 memset(bufW,'x',sizeof(bufW));
182 len = MultiByteToWideChar(CP_ACP, 0, foobarA, -1000, bufW, -1);
183 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER,
184 "MultiByteToWideChar(destlen -1000): len=%d error=%lx\n", len, GetLastError());
186 /* Test return on INT_MAX dest length */
187 SetLastError( 0xdeadbeef );
188 memset(bufA,'x',sizeof(bufA));
189 len = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, bufA, INT_MAX, NULL, NULL);
190 ok(len == 7 && !lstrcmpA(bufA, "foobar") && GetLastError() == 0xdeadbeef,
191 "WideCharToMultiByte(destlen INT_MAX): len=%d error=%lx\n", len, GetLastError());
193 /* Test return on INT_MAX dest length and very long input */
194 SetLastError( 0xdeadbeef );
195 memset(bufA,'x',sizeof(bufA));
196 for (i=0; i < LONGBUFLEN - 1; i++) {
197 originalW[i] = 'Q';
198 originalA[i] = 'Q';
200 originalW[LONGBUFLEN-1] = 0;
201 originalA[LONGBUFLEN-1] = 0;
202 len = WideCharToMultiByte(CP_ACP, 0, originalW, -1, bufA, INT_MAX, NULL, NULL);
203 theError = GetLastError();
204 ok(len == LONGBUFLEN && !lstrcmpA(bufA, originalA) && theError == 0xdeadbeef,
205 "WideCharToMultiByte(srclen %d, destlen INT_MAX): len %d error=%lx\n", LONGBUFLEN, len, theError);
209 static void test_other_invalid_parameters(void)
211 char c_string[] = "Hello World";
212 size_t c_string_len = sizeof(c_string);
213 WCHAR w_string[] = {'H','e','l','l','o',' ','W','o','r','l','d',0};
214 size_t w_string_len = ARRAY_SIZE(w_string);
215 BOOL used;
216 INT len;
218 /* Unrecognized flag => ERROR_INVALID_FLAGS */
219 SetLastError(0xdeadbeef);
220 len = WideCharToMultiByte(CP_ACP, 0x100, w_string, -1, c_string, c_string_len, NULL, NULL);
221 ok(len == 0 && GetLastError() == ERROR_INVALID_FLAGS, "len=%d error=%lx\n", len, GetLastError());
223 SetLastError(0xdeadbeef);
224 len = WideCharToMultiByte(CP_ACP, 0x800, w_string, -1, c_string, c_string_len, NULL, NULL);
225 ok(len == 0 && GetLastError() == ERROR_INVALID_FLAGS, "len=%d error=%lx\n", len, GetLastError());
227 SetLastError(0xdeadbeef);
228 len = MultiByteToWideChar(CP_ACP, 0x10, c_string, -1, w_string, w_string_len);
229 ok(len == 0 && GetLastError() == ERROR_INVALID_FLAGS, "len=%d error=%lx\n", len, GetLastError());
232 /* Unrecognized flag and invalid codepage => ERROR_INVALID_PARAMETER */
233 SetLastError(0xdeadbeef);
234 len = WideCharToMultiByte(0xdeadbeef, 0x100, w_string, w_string_len, c_string, c_string_len, NULL, NULL);
235 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER, "len=%d error=%lx\n", len, GetLastError());
237 SetLastError(0xdeadbeef);
238 len = MultiByteToWideChar(0xdeadbeef, 0x10, c_string, c_string_len, w_string, w_string_len);
239 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER, "len=%d error=%lx\n", len, GetLastError());
242 /* Unrecognized flag and src is NULL => ERROR_INVALID_PARAMETER */
243 SetLastError(0xdeadbeef);
244 len = WideCharToMultiByte(CP_ACP, 0x100, NULL, -1, c_string, c_string_len, NULL, NULL);
245 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER, "len=%d error=%lx\n", len, GetLastError());
247 SetLastError(0xdeadbeef);
248 len = MultiByteToWideChar(CP_ACP, 0x10, NULL, -1, w_string, w_string_len);
249 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER, "len=%d error=%lx\n", len, GetLastError());
252 /* srclen=0 => ERROR_INVALID_PARAMETER */
253 SetLastError(0xdeadbeef);
254 len = WideCharToMultiByte(CP_ACP, 0, w_string, 0, c_string, c_string_len, NULL, NULL);
255 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER, "len=%d error=%lx\n", len, GetLastError());
257 SetLastError(0xdeadbeef);
258 len = MultiByteToWideChar(CP_ACP, 0, c_string, 0, w_string, w_string_len);
259 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER, "len=%d error=%lx\n", len, GetLastError());
262 /* dst=NULL but dstlen not 0 => ERROR_INVALID_PARAMETER */
263 SetLastError(0xdeadbeef);
264 len = WideCharToMultiByte(CP_ACP, 0, w_string, w_string_len, NULL, c_string_len, NULL, NULL);
265 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER, "len=%d error=%lx\n", len, GetLastError());
267 SetLastError(0xdeadbeef);
268 len = MultiByteToWideChar(CP_ACP, 0, c_string, c_string_len, NULL, w_string_len);
269 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER, "len=%d error=%lx\n", len, GetLastError());
272 /* CP_UTF7 or CP_SYMBOL and defchar not NULL => ERROR_INVALID_PARAMETER */
273 /* CP_UTF8 allows it since win10 1709 */
274 SetLastError(0xdeadbeef);
275 len = WideCharToMultiByte(CP_UTF7, 0, w_string, w_string_len, c_string, c_string_len, c_string, NULL);
276 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER, "len=%d error=%lx\n", len, GetLastError());
278 SetLastError(0xdeadbeef);
279 len = WideCharToMultiByte(CP_UTF8, 0, w_string, w_string_len, c_string, c_string_len, c_string, NULL);
280 ok(len == 12 || broken(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER),
281 "len=%d error=%lx\n", len, GetLastError());
283 SetLastError(0xdeadbeef);
284 len = WideCharToMultiByte(CP_SYMBOL, 0, w_string, w_string_len, c_string, c_string_len, c_string, NULL);
285 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER, "len=%d error=%lx\n", len, GetLastError());
288 /* CP_UTF7 or CP_SYMBOL and used not NULL => ERROR_INVALID_PARAMETER */
289 /* CP_UTF8 allows it since win10 1709 */
290 SetLastError(0xdeadbeef);
291 len = WideCharToMultiByte(CP_UTF7, 0, w_string, w_string_len, c_string, c_string_len, NULL, &used);
292 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER, "len=%d error=%lx\n", len, GetLastError());
294 SetLastError(0xdeadbeef);
295 len = WideCharToMultiByte(CP_UTF8, 0, w_string, w_string_len, c_string, c_string_len, NULL, &used);
296 ok(len == 12 || broken(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER),
297 "len=%d error=%lx\n", len, GetLastError());
299 SetLastError(0xdeadbeef);
300 len = WideCharToMultiByte(CP_SYMBOL, 0, w_string, w_string_len, c_string, c_string_len, NULL, &used);
301 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER, "len=%d error=%lx\n", len, GetLastError());
304 /* CP_UTF7, flags not 0 and used not NULL => ERROR_INVALID_PARAMETER */
305 /* (tests precedence of ERROR_INVALID_PARAMETER over ERROR_INVALID_FLAGS) */
306 /* The same test with CP_SYMBOL instead of CP_UTF7 gives ERROR_INVALID_FLAGS
307 instead except on Windows NT4 */
308 SetLastError(0xdeadbeef);
309 len = WideCharToMultiByte(CP_UTF7, 1, w_string, w_string_len, c_string, c_string_len, NULL, &used);
310 ok(len == 0 && GetLastError() == ERROR_INVALID_PARAMETER, "len=%d error=%lx\n", len, GetLastError());
312 /* CP_UTF8, unrecognized flag and used not NULL => ERROR_INVALID_PARAMETER */
313 SetLastError(0xdeadbeef);
314 len = WideCharToMultiByte(CP_UTF8, 0x100, w_string, w_string_len, c_string, c_string_len, NULL, &used);
315 ok(len == 0, "wrong ret %d\n", len);
316 ok(GetLastError() == ERROR_INVALID_PARAMETER
317 || GetLastError() == ERROR_INVALID_FLAGS /* Win10 1709+ */, "wrong error %lu\n", GetLastError());
320 static void test_overlapped_buffers(void)
322 static const WCHAR strW[] = {'j','u','s','t',' ','a',' ','t','e','s','t',0};
323 static const char strA[] = "just a test";
324 char buf[256];
325 int ret;
327 /* limit such that strA's NUL terminator overlaps strW's NUL */
328 size_t overlap_limit = (sizeof(strW)-sizeof(strA)) - (sizeof(strW[0])-sizeof(strA[0]));
330 SetLastError(0xdeadbeef);
331 memcpy(buf + 1, strW, sizeof(strW));
332 ret = WideCharToMultiByte(CP_ACP, 0, (WCHAR *)(buf + 1), -1, buf, sizeof(buf), NULL, NULL);
333 ok(ret == sizeof(strA), "unexpected ret %d\n", ret);
334 ok(!memcmp(buf, strA, sizeof(strA)), "conversion failed: %s\n", buf);
335 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %lu\n", GetLastError());
337 SetLastError(0xdeadbeef);
338 memcpy(buf + overlap_limit, strA, sizeof(strA));
339 ret = MultiByteToWideChar(CP_ACP, 0, buf + overlap_limit, -1, (WCHAR *)buf, sizeof(buf) / sizeof(WCHAR));
340 ok(ret == ARRAY_SIZE(strW), "unexpected ret %d\n", ret);
341 ok(!memcmp(buf, strW, sizeof(strW)), "conversion failed: %s\n", wine_dbgstr_wn((WCHAR *)buf, ARRAY_SIZE(strW)));
342 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %lu\n", GetLastError());
345 static void test_string_conversion(LPBOOL bUsedDefaultChar)
347 char mbc;
348 char mbs[15];
349 int ret;
350 WCHAR wc1 = 228; /* Western Windows-1252 character */
351 WCHAR wc2 = 1088; /* Russian Windows-1251 character not displayable for Windows-1252 */
352 static const WCHAR wcs[] = {'T', 'h', 1088, 'i', 0}; /* String with ASCII characters and a Russian character */
353 static const WCHAR dbwcs[] = {28953, 25152, 0}; /* String with Chinese (codepage 950) characters */
354 static const WCHAR dbwcs2[] = {0x7bb8, 0x3d, 0xc813, 0xac00, 0xb77d, 0};
355 static const char default_char[] = {0xa3, 0xbf, 0};
357 SetLastError(0xdeadbeef);
358 ret = WideCharToMultiByte(1252, 0, &wc1, 1, &mbc, 1, NULL, bUsedDefaultChar);
359 ok(ret == 1, "ret is %d\n", ret);
360 ok(mbc == '\xe4', "mbc is %d\n", mbc);
361 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
362 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %lu\n", GetLastError());
364 SetLastError(0xdeadbeef);
365 ret = WideCharToMultiByte(1252, 0, &wc2, 1, &mbc, 1, NULL, bUsedDefaultChar);
366 ok(ret == 1, "ret is %d\n", ret);
367 ok(mbc == 63, "mbc is %d\n", mbc);
368 if(bUsedDefaultChar) ok(*bUsedDefaultChar == TRUE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
369 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %lu\n", GetLastError());
371 if (IsValidCodePage(1251))
373 SetLastError(0xdeadbeef);
374 ret = WideCharToMultiByte(1251, 0, &wc2, 1, &mbc, 1, NULL, bUsedDefaultChar);
375 ok(ret == 1, "ret is %d\n", ret);
376 ok(mbc == '\xf0', "mbc is %d\n", mbc);
377 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
378 ok(GetLastError() == 0xdeadbeef ||
379 broken(GetLastError() == 0), /* win95 */
380 "GetLastError() is %lu\n", GetLastError());
382 SetLastError(0xdeadbeef);
383 ret = WideCharToMultiByte(1251, 0, &wc1, 1, &mbc, 1, NULL, bUsedDefaultChar);
384 ok(ret == 1, "ret is %d\n", ret);
385 ok(mbc == 97, "mbc is %d\n", mbc);
386 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
387 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %lu\n", GetLastError());
389 else
390 skip("Codepage 1251 not available\n");
392 /* This call triggers the last Win32 error */
393 SetLastError(0xdeadbeef);
394 ret = WideCharToMultiByte(1252, 0, wcs, -1, &mbc, 1, NULL, bUsedDefaultChar);
395 ok(ret == 0, "ret is %d\n", ret);
396 ok(mbc == 84, "mbc is %d\n", mbc);
397 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
398 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetLastError() is %lu\n", GetLastError());
400 SetLastError(0xdeadbeef);
401 ret = WideCharToMultiByte(1252, 0, wcs, -1, mbs, sizeof(mbs), NULL, bUsedDefaultChar);
402 ok(ret == 5, "ret is %d\n", ret);
403 ok(!strcmp(mbs, "Th?i"), "mbs is %s\n", mbs);
404 if(bUsedDefaultChar) ok(*bUsedDefaultChar == TRUE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
405 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %lu\n", GetLastError());
406 mbs[0] = 0;
408 /* WideCharToMultiByte mustn't add any null character automatically.
409 So in this case, we should get the same string again, even if we only copied the first three bytes. */
410 SetLastError(0xdeadbeef);
411 ret = WideCharToMultiByte(1252, 0, wcs, 3, mbs, sizeof(mbs), NULL, bUsedDefaultChar);
412 ok(ret == 3, "ret is %d\n", ret);
413 ok(!strcmp(mbs, "Th?i"), "mbs is %s\n", mbs);
414 if(bUsedDefaultChar) ok(*bUsedDefaultChar == TRUE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
415 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %lu\n", GetLastError());
416 ZeroMemory(mbs, 5);
418 /* Now this shouldn't be the case like above as we zeroed the complete string buffer. */
419 SetLastError(0xdeadbeef);
420 ret = WideCharToMultiByte(1252, 0, wcs, 3, mbs, sizeof(mbs), NULL, bUsedDefaultChar);
421 ok(ret == 3, "ret is %d\n", ret);
422 ok(!strcmp(mbs, "Th?"), "mbs is %s\n", mbs);
423 if(bUsedDefaultChar) ok(*bUsedDefaultChar == TRUE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
424 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %lu\n", GetLastError());
426 /* Double-byte tests */
427 ret = WideCharToMultiByte(1252, 0, dbwcs, 3, mbs, sizeof(mbs), NULL, bUsedDefaultChar);
428 ok(ret == 3, "ret is %d\n", ret);
429 ok(!strcmp(mbs, "??"), "mbs is %s\n", mbs);
430 if(bUsedDefaultChar) ok(*bUsedDefaultChar == TRUE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
432 ret = WideCharToMultiByte(936, WC_COMPOSITECHECK, dbwcs2, -1, mbs, sizeof(mbs), (const char *)default_char, bUsedDefaultChar);
433 ok(ret == 10, "ret is %d\n", ret);
434 ok(!strcmp(mbs, "\xf3\xe7\x3d\xa3\xbf\xa3\xbf\xa3\xbf"), "mbs is %s\n", mbs);
435 if(bUsedDefaultChar) ok(*bUsedDefaultChar == TRUE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
437 /* Show that characters are not truncated */
438 ZeroMemory(mbs, 5);
439 ret = WideCharToMultiByte(936, 0, dbwcs2, -1, mbs, 1, (const char *)default_char, bUsedDefaultChar);
440 ok(!ret, "ret is %d\n", ret);
441 ok(mbs[0] == '\0', "mbs is %s\n", mbs);
442 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
443 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetLastError() is %lu\n", GetLastError());
445 /* And destination is not null-terminated even when too short */
446 FillMemory(mbs, 5, 'x');
447 mbs[5] = '\0';
448 ret = WideCharToMultiByte(936, 0, dbwcs2, -1, mbs, 2, (const char *)default_char, bUsedDefaultChar);
449 ok(!ret, "ret is %d\n", ret);
450 ok(!strcmp(mbs, "\xf3\xe7""xxx"), "mbs is %s\n", mbs);
451 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
452 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetLastError() is %lu\n", GetLastError());
454 /* Length-only tests */
455 SetLastError(0xdeadbeef);
456 ret = WideCharToMultiByte(1252, 0, &wc2, 1, NULL, 0, NULL, bUsedDefaultChar);
457 ok(ret == 1, "ret is %d\n", ret);
458 if(bUsedDefaultChar) ok(*bUsedDefaultChar == TRUE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
459 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %lu\n", GetLastError());
461 SetLastError(0xdeadbeef);
462 ret = WideCharToMultiByte(1252, 0, wcs, -1, NULL, 0, NULL, bUsedDefaultChar);
463 ok(ret == 5, "ret is %d\n", ret);
464 if(bUsedDefaultChar) ok(*bUsedDefaultChar == TRUE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
465 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %lu\n", GetLastError());
467 if (!IsValidCodePage(950))
469 skip("Codepage 950 not available\n");
470 return;
473 /* Double-byte tests */
474 SetLastError(0xdeadbeef);
475 ret = WideCharToMultiByte(950, 0, dbwcs, -1, mbs, sizeof(mbs), NULL, bUsedDefaultChar);
476 ok(ret == 5, "ret is %d\n", ret);
477 ok(!strcmp(mbs, "\xb5H\xa9\xd2"), "mbs is %s\n", mbs);
478 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
479 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %lu\n", GetLastError());
481 SetLastError(0xdeadbeef);
482 ret = WideCharToMultiByte(950, 0, dbwcs, 1, &mbc, 1, NULL, bUsedDefaultChar);
483 ok(ret == 0, "ret is %d\n", ret);
484 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
485 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "GetLastError() is %lu\n", GetLastError());
486 ZeroMemory(mbs, 5);
488 SetLastError(0xdeadbeef);
489 ret = WideCharToMultiByte(950, 0, dbwcs, 1, mbs, sizeof(mbs), NULL, bUsedDefaultChar);
490 ok(ret == 2, "ret is %d\n", ret);
491 ok(!strcmp(mbs, "\xb5H"), "mbs is %s\n", mbs);
492 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
493 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %lu\n", GetLastError());
495 /* Length-only tests */
496 SetLastError(0xdeadbeef);
497 ret = WideCharToMultiByte(950, 0, dbwcs, 1, NULL, 0, NULL, bUsedDefaultChar);
498 ok(ret == 2, "ret is %d\n", ret);
499 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
500 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %lu\n", GetLastError());
502 SetLastError(0xdeadbeef);
503 ret = WideCharToMultiByte(950, 0, dbwcs, -1, NULL, 0, NULL, bUsedDefaultChar);
504 ok(ret == 5, "ret is %d\n", ret);
505 if(bUsedDefaultChar) ok(*bUsedDefaultChar == FALSE, "bUsedDefaultChar is %d\n", *bUsedDefaultChar);
506 ok(GetLastError() == 0xdeadbeef, "GetLastError() is %lu\n", GetLastError());
509 static void test_utf7_encoding(void)
511 WCHAR input[16];
512 char output[16], expected[16];
513 int i, len, expected_len;
515 static const BOOL directly_encodable_table[] =
517 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, /* 0x00 - 0x0F */
518 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 - 0x1F */
519 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* 0x20 - 0x2F */
520 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 0x30 - 0x3F */
521 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40 - 0x4F */
522 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 0x50 - 0x5F */
523 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60 - 0x6F */
524 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 /* 0x70 - 0x7F */
526 static const char base64_encoding_table[] =
527 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
529 const struct
531 /* inputs */
532 WCHAR src[16];
533 int srclen;
534 char *dst;
535 int dstlen;
536 /* expected outputs */
537 char expected_dst[16];
538 int chars_written;
539 int len;
541 tests[] =
543 /* tests string conversion with srclen=-1 */
545 {0x4F60,0x597D,0x5417,0}, -1, output, sizeof(output) - 1,
546 "+T2BZfVQX-", 11, 11
548 /* tests string conversion with srclen=-2 */
550 {0x4F60,0x597D,0x5417,0}, -2, output, sizeof(output) - 1,
551 "+T2BZfVQX-", 11, 11
553 /* tests string conversion with dstlen=strlen(expected_dst) */
555 {0x4F60,0x597D,0x5417,0}, -1, output, 10,
556 "+T2BZfVQX-", 10, 0
558 /* tests string conversion with dstlen=strlen(expected_dst)+1 */
560 {0x4F60,0x597D,0x5417,0}, -1, output, 11,
561 "+T2BZfVQX-", 11, 11
563 /* tests string conversion with dstlen=strlen(expected_dst)+2 */
565 {0x4F60,0x597D,0x5417,0}, -1, output, 12,
566 "+T2BZfVQX-", 11, 11
568 /* tests dry run with dst=NULL and dstlen=0 */
570 {0x4F60,0x597D,0x5417,0}, -1, NULL, 0,
571 {}, 0, 11
573 /* tests dry run with dst!=NULL and dstlen=0 */
575 {0x4F60,0x597D,0x5417,0}, -1, output, 0,
576 {}, 0, 11
578 /* tests srclen < strlenW(src) with directly encodable chars */
580 {'h','e','l','l','o',0}, 2, output, sizeof(output) - 1,
581 "he", 2, 2
583 /* tests srclen < strlenW(src) with non-directly encodable chars */
585 {0x4F60,0x597D,0x5417,0}, 2, output, sizeof(output) - 1,
586 "+T2BZfQ-", 8, 8
588 /* tests a single null char */
590 {0}, -1, output, sizeof(output) - 1,
591 "", 1, 1
593 /* tests a buffer that runs out while not encoding a UTF-7 sequence */
595 {'h','e','l','l','o',0}, -1, output, 2,
596 "he", 2, 0
598 /* tests a buffer that runs out after writing 1 base64 character */
600 {0x4F60,0x0001,0}, -1, output, 2,
601 "+T", 2, 0
603 /* tests a buffer that runs out after writing 2 base64 characters */
605 {0x4F60,0x0001,0}, -1, output, 3,
606 "+T2", 3, 0
608 /* tests a buffer that runs out after writing 3 base64 characters */
610 {0x4F60,0x0001,0}, -1, output, 4,
611 "+T2A", 4, 0
613 /* tests a buffer that runs out just after writing the + sign */
615 {0x4F60,0}, -1, output, 1,
616 "+", 1, 0
618 /* tests a buffer that runs out just before writing the - sign
619 * the number of bits to encode here is evenly divisible by 6 */
621 {0x4F60,0x597D,0x5417,0}, -1, output, 9,
622 "+T2BZfVQX", 9, 0
624 /* tests a buffer that runs out just before writing the - sign
625 * the number of bits to encode here is NOT evenly divisible by 6 */
627 {0x4F60,0}, -1, output, 4,
628 "+T2", 3, 0
630 /* tests a buffer that runs out in the middle of escaping a + sign */
632 {'+',0}, -1, output, 1,
633 "+", 1, 0
637 /* test which characters are encoded if surrounded by non-encoded characters */
638 for (i = 0; i <= 0xFFFF; i++)
640 input[0] = ' ';
641 input[1] = i;
642 input[2] = ' ';
643 input[3] = 0;
645 memset(output, '#', sizeof(output) - 1);
646 output[sizeof(output) - 1] = 0;
648 len = WideCharToMultiByte(CP_UTF7, 0, input, 4, output, sizeof(output) - 1, NULL, NULL);
650 if (i == '+')
652 /* '+' is a special case and is encoded as "+-" */
653 expected_len = 5;
654 strcpy(expected, " +- ");
656 else if (i <= 0x7F && directly_encodable_table[i])
658 /* encodes directly */
659 expected_len = 4;
660 sprintf(expected, " %c ", i);
662 else
664 /* base64-encodes */
665 expected_len = 8;
666 sprintf(expected, " +%c%c%c- ",
667 base64_encoding_table[(i & 0xFC00) >> 10],
668 base64_encoding_table[(i & 0x03F0) >> 4],
669 base64_encoding_table[(i & 0x000F) << 2]);
672 ok(len == expected_len, "i=0x%04x: expected len=%i, got len=%i\n", i, expected_len, len);
673 ok(memcmp(output, expected, expected_len) == 0,
674 "i=0x%04x: expected output='%s', got output='%s'\n", i, expected, output);
675 ok(output[expected_len] == '#', "i=0x%04x: expected output[%i]='#', got output[%i]=%i\n",
676 i, expected_len, expected_len, output[expected_len]);
679 /* test which one-byte characters are absorbed into surrounding base64 blocks
680 * (Windows always ends the base64 block when it encounters a directly encodable character) */
681 for (i = 0; i <= 0xFFFF; i++)
683 input[0] = 0x2672;
684 input[1] = i;
685 input[2] = 0x2672;
686 input[3] = 0;
688 memset(output, '#', sizeof(output) - 1);
689 output[sizeof(output) - 1] = 0;
691 len = WideCharToMultiByte(CP_UTF7, 0, input, 4, output, sizeof(output) - 1, NULL, NULL);
693 if (i == '+')
695 /* '+' is a special case and is encoded as "+-" */
696 expected_len = 13;
697 strcpy(expected, "+JnI-+-+JnI-");
699 else if (i <= 0x7F && directly_encodable_table[i])
701 /* encodes directly */
702 expected_len = 12;
703 sprintf(expected, "+JnI-%c+JnI-", i);
705 else
707 /* base64-encodes */
708 expected_len = 11;
709 sprintf(expected, "+Jn%c%c%c%cZy-",
710 base64_encoding_table[8 | ((i & 0xC000) >> 14)],
711 base64_encoding_table[(i & 0x3F00) >> 8],
712 base64_encoding_table[(i & 0x00FC) >> 2],
713 base64_encoding_table[((i & 0x0003) << 4) | 2]);
716 ok(len == expected_len, "i=0x%04x: expected len=%i, got len=%i\n", i, expected_len, len);
717 ok(memcmp(output, expected, expected_len) == 0,
718 "i=0x%04x: expected output='%s', got output='%s'\n", i, expected, output);
719 ok(output[expected_len] == '#', "i=0x%04x: expected output[%i]='#', got output[%i]=%i\n",
720 i, expected_len, expected_len, output[expected_len]);
723 for (i = 0; i < ARRAY_SIZE(tests); i++)
725 memset(output, '#', sizeof(output) - 1);
726 output[sizeof(output) - 1] = 0;
727 SetLastError(0xdeadbeef);
729 len = WideCharToMultiByte(CP_UTF7, 0, tests[i].src, tests[i].srclen,
730 tests[i].dst, tests[i].dstlen, NULL, NULL);
732 if (!tests[i].len)
734 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
735 "tests[%i]: expected error=0x%x, got error=0x%lx\n",
736 i, ERROR_INSUFFICIENT_BUFFER, GetLastError());
738 ok(len == tests[i].len, "tests[%i]: expected len=%i, got len=%i\n", i, tests[i].len, len);
740 if (tests[i].dst)
742 ok(memcmp(tests[i].dst, tests[i].expected_dst, tests[i].chars_written) == 0,
743 "tests[%i]: expected dst='%s', got dst='%s'\n",
744 i, tests[i].expected_dst, tests[i].dst);
745 ok(tests[i].dst[tests[i].chars_written] == '#',
746 "tests[%i]: expected dst[%i]='#', got dst[%i]=%i\n",
747 i, tests[i].chars_written, tests[i].chars_written, tests[i].dst[tests[i].chars_written]);
752 static void test_utf7_decoding(void)
754 char input[32];
755 WCHAR output[32], expected[32];
756 int i, len, expected_len;
758 static const signed char base64_decoding_table[] =
760 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x00-0x0F */
761 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x10-0x1F */
762 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, /* 0x20-0x2F */
763 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, /* 0x30-0x3F */
764 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 0x40-0x4F */
765 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 0x50-0x5F */
766 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /* 0x60-0x6F */
767 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 /* 0x70-0x7F */
770 struct
772 /* inputs */
773 char src[32];
774 int srclen;
775 WCHAR *dst;
776 int dstlen;
777 /* expected outputs */
778 WCHAR expected_dst[32];
779 int chars_written;
780 int len;
782 tests[] =
784 /* tests string conversion with srclen=-1 */
786 "+T2BZfQ-", -1, output, ARRAY_SIZE(output) - 1,
787 {0x4F60,0x597D,0}, 3, 3
789 /* tests string conversion with srclen=-2 */
791 "+T2BZfQ-", -2, output, ARRAY_SIZE(output) - 1,
792 {0x4F60,0x597D,0}, 3, 3
794 /* tests string conversion with dstlen=strlen(expected_dst) */
796 "+T2BZfQ-", -1, output, 2,
797 {0x4F60,0x597D}, 2, 0
799 /* tests string conversion with dstlen=strlen(expected_dst)+1 */
801 "+T2BZfQ-", -1, output, 3,
802 {0x4F60,0x597D,0}, 3, 3
804 /* tests string conversion with dstlen=strlen(expected_dst)+2 */
806 "+T2BZfQ-", -1, output, 4,
807 {0x4F60,0x597D,0}, 3, 3
809 /* tests dry run with dst=NULL and dstlen=0 */
811 "+T2BZfQ-", -1, NULL, 0,
812 {}, 0, 3
814 /* tests dry run with dst!=NULL and dstlen=0 */
816 "+T2BZfQ-", -1, output, 0,
817 {}, 0, 3
819 /* tests ill-formed UTF-7: 6 bits, not enough for a byte pair */
821 "+T-+T-+T-hello", -1, output, ARRAY_SIZE(output) - 1,
822 {'h','e','l','l','o',0}, 6, 6
824 /* tests ill-formed UTF-7: 12 bits, not enough for a byte pair */
826 "+T2-+T2-+T2-hello", -1, output, ARRAY_SIZE(output) - 1,
827 {'h','e','l','l','o',0}, 6, 6
829 /* tests ill-formed UTF-7: 18 bits, not a multiple of 16 and the last bit is a 1 */
831 "+T2B-+T2B-+T2B-hello", -1, output, ARRAY_SIZE(output) - 1,
832 {0x4F60,0x4F60,0x4F60,'h','e','l','l','o',0}, 9, 9
834 /* tests ill-formed UTF-7: 24 bits, a multiple of 8 but not a multiple of 16 */
836 "+T2BZ-+T2BZ-+T2BZ-hello", -1, output, ARRAY_SIZE(output) - 1,
837 {0x4F60,0x4F60,0x4F60,'h','e','l','l','o',0}, 9, 9
839 /* tests UTF-7 followed by characters that should be encoded but aren't */
841 "+T2BZ-\x82\xFE", -1, output, ARRAY_SIZE(output) - 1,
842 {0x4F60,0x0082,0x00FE,0}, 4, 4
844 /* tests srclen > strlen(src) */
846 "a\0b", 4, output, ARRAY_SIZE(output) - 1,
847 {'a',0,'b',0}, 4, 4
849 /* tests srclen < strlen(src) outside of a UTF-7 sequence */
851 "hello", 2, output, ARRAY_SIZE(output) - 1,
852 {'h','e'}, 2, 2
854 /* tests srclen < strlen(src) inside of a UTF-7 sequence */
856 "+T2BZfQ-", 4, output, ARRAY_SIZE(output) - 1,
857 {0x4F60}, 1, 1
859 /* tests srclen < strlen(src) right at the beginning of a UTF-7 sequence */
861 "hi+T2A-", 3, output, ARRAY_SIZE(output) - 1,
862 {'h','i'}, 2, 2
864 /* tests srclen < strlen(src) right at the end of a UTF-7 sequence */
866 "+T2A-hi", 5, output, ARRAY_SIZE(output) - 1,
867 {0x4F60}, 1, 1
869 /* tests srclen < strlen(src) at the beginning of an escaped + sign */
871 "hi+-", 3, output, ARRAY_SIZE(output) - 1,
872 {'h','i'}, 2, 2
874 /* tests srclen < strlen(src) at the end of an escaped + sign */
876 "+-hi", 2, output, ARRAY_SIZE(output) - 1,
877 {'+'}, 1, 1
879 /* tests len=0 but no error */
881 "+", 1, output, ARRAY_SIZE(output) - 1,
882 {}, 0, 0
884 /* tests a single null char */
886 "", -1, output, ARRAY_SIZE(output) - 1,
887 {0}, 1, 1
889 /* tests a buffer that runs out while not decoding a UTF-7 sequence */
891 "hello", -1, output, 2,
892 {'h','e'}, 2, 0
894 /* tests a buffer that runs out in the middle of decoding a UTF-7 sequence */
896 "+T2BZfQ-", -1, output, 1,
897 {0x4F60}, 1, 0
901 /* test which one-byte characters remove stray + signs */
902 for (i = 0; i < 256; i++)
904 sprintf(input, "+%c+AAA", i);
906 memset(output, 0x23, sizeof(output) - sizeof(WCHAR));
907 output[ARRAY_SIZE(output) - 1] = 0;
909 len = MultiByteToWideChar(CP_UTF7, 0, input, 7, output, ARRAY_SIZE(output) - 1);
911 if (i == '-')
913 /* removes the - sign */
914 expected_len = 3;
915 expected[0] = 0x002B;
916 expected[1] = 0;
917 expected[2] = 0;
919 else if (i <= 0x7F && base64_decoding_table[i] != -1)
921 /* absorbs the character into the base64 sequence */
922 expected_len = 2;
923 expected[0] = (base64_decoding_table[i] << 10) | 0x03E0;
924 expected[1] = 0;
926 else
928 /* removes the + sign */
929 expected_len = 3;
930 expected[0] = i;
931 expected[1] = 0;
932 expected[2] = 0;
934 expected[expected_len] = 0x2323;
936 ok(len == expected_len, "i=0x%02x: expected len=%i, got len=%i\n", i, expected_len, len);
937 ok(memcmp(output, expected, (expected_len + 1) * sizeof(WCHAR)) == 0,
938 "i=0x%02x: expected output=%s, got output=%s\n",
939 i, wine_dbgstr_wn(expected, expected_len + 1), wine_dbgstr_wn(output, expected_len + 1));
942 /* test which one-byte characters terminate a sequence
943 * also test whether the unfinished byte pair is discarded or not */
944 for (i = 0; i < 256; i++)
946 sprintf(input, "+B%c+AAA", i);
948 memset(output, 0x23, sizeof(output) - sizeof(WCHAR));
949 output[ARRAY_SIZE(output) - 1] = 0;
951 len = MultiByteToWideChar(CP_UTF7, 0, input, 8, output, ARRAY_SIZE(output) - 1);
953 if (i == '-')
955 /* explicitly terminates */
956 expected_len = 2;
957 expected[0] = 0;
958 expected[1] = 0;
960 else if (i <= 0x7F)
962 if (base64_decoding_table[i] != -1)
964 /* absorbs the character into the base64 sequence */
965 expected_len = 3;
966 expected[0] = 0x0400 | (base64_decoding_table[i] << 4) | 0x000F;
967 expected[1] = 0x8000;
968 expected[2] = 0;
970 else
972 /* implicitly terminates and discards the unfinished byte pair */
973 expected_len = 3;
974 expected[0] = i;
975 expected[1] = 0;
976 expected[2] = 0;
979 else
981 /* implicitly terminates but does not the discard unfinished byte pair */
982 expected_len = 3;
983 expected[0] = i;
984 expected[1] = 0x0400;
985 expected[2] = 0;
987 expected[expected_len] = 0x2323;
989 ok(len == expected_len, "i=0x%02x: expected len=%i, got len=%i\n", i, expected_len, len);
990 ok(memcmp(output, expected, (expected_len + 1) * sizeof(WCHAR)) == 0,
991 "i=0x%02x: expected output=%s, got output=%s\n",
992 i, wine_dbgstr_wn(expected, expected_len + 1), wine_dbgstr_wn(output, expected_len + 1));
995 for (i = 0; i < ARRAY_SIZE(tests); i++)
997 memset(output, 0x23, sizeof(output) - sizeof(WCHAR));
998 output[ARRAY_SIZE(output) - 1] = 0;
999 SetLastError(0xdeadbeef);
1001 len = MultiByteToWideChar(CP_UTF7, 0, tests[i].src, tests[i].srclen,
1002 tests[i].dst, tests[i].dstlen);
1004 tests[i].expected_dst[tests[i].chars_written] = 0x2323;
1006 if (!tests[i].len && tests[i].chars_written)
1008 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1009 "tests[%i]: expected error=0x%x, got error=0x%lx\n",
1010 i, ERROR_INSUFFICIENT_BUFFER, GetLastError());
1012 ok(len == tests[i].len, "tests[%i]: expected len=%i, got len=%i\n", i, tests[i].len, len);
1014 if (tests[i].dst)
1016 ok(memcmp(tests[i].dst, tests[i].expected_dst, (tests[i].chars_written + 1) * sizeof(WCHAR)) == 0,
1017 "tests[%i]: expected dst=%s, got dst=%s\n",
1018 i, wine_dbgstr_wn(tests[i].expected_dst, tests[i].chars_written + 1),
1019 wine_dbgstr_wn(tests[i].dst, tests[i].chars_written + 1));
1024 static void test_undefined_byte_char(void)
1026 static const struct tag_testset {
1027 UINT codepage;
1028 LPCSTR str;
1029 BOOL is_error;
1030 } testset[] = {
1031 { 37, "\x6f", FALSE },
1032 { 874, "\xdd", TRUE },
1033 { 932, "\xfe", TRUE },
1034 { 932, "\x80", FALSE },
1035 { 932, "\x81\x45", FALSE },
1036 { 936, "\xff", TRUE },
1037 { 949, "\xff", TRUE },
1038 { 950, "\xff", TRUE },
1039 { 1252, "?", FALSE },
1040 { 1252, "\x90", FALSE },
1041 { 1253, "\xaa", TRUE },
1042 { 1255, "\xff", TRUE },
1043 { 1257, "\xa5", TRUE },
1045 INT i, ret;
1046 DWORD err;
1048 for (i = 0; i < ARRAY_SIZE(testset); i++) {
1049 if (! IsValidCodePage(testset[i].codepage))
1051 skip("Codepage %d not available\n", testset[i].codepage);
1052 continue;
1055 SetLastError(0xdeadbeef);
1056 ret = MultiByteToWideChar(testset[i].codepage, MB_ERR_INVALID_CHARS,
1057 testset[i].str, -1, NULL, 0);
1058 err = GetLastError();
1059 if (testset[i].is_error) {
1060 ok(err == ERROR_NO_UNICODE_TRANSLATION, "Test %u: err is %lu\n", i, err);
1061 ok(ret == 0, "Test %u: ret is %d\n", i, ret);
1063 else {
1064 ok(err == 0xdeadbeef, "Test %u: err is %lu\n", i, err);
1065 ok(ret == 2, "Test %u: ret is %d\n", i, ret);
1068 SetLastError(0xdeadbeef);
1069 ret = MultiByteToWideChar(testset[i].codepage, 0,
1070 testset[i].str, -1, NULL, 0);
1071 err = GetLastError();
1072 ok(err == 0xdeadbeef, "Test %u: err is %lu\n", i, err);
1073 ok(ret == 2, "Test %u: ret is %d\n", i, ret);
1077 static void test_threadcp(void)
1079 static const LCID ENGLISH = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
1080 static const LCID HINDI = MAKELCID(MAKELANGID(LANG_HINDI, SUBLANG_HINDI_INDIA), SORT_DEFAULT);
1081 static const LCID GEORGIAN = MAKELCID(MAKELANGID(LANG_GEORGIAN, SUBLANG_GEORGIAN_GEORGIA), SORT_DEFAULT);
1082 static const LCID RUSSIAN = MAKELCID(MAKELANGID(LANG_RUSSIAN, SUBLANG_RUSSIAN_RUSSIA), SORT_DEFAULT);
1083 static const LCID JAPANESE = MAKELCID(MAKELANGID(LANG_JAPANESE, SUBLANG_JAPANESE_JAPAN), SORT_DEFAULT);
1084 static const LCID CHINESE = MAKELCID(MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED), SORT_DEFAULT);
1086 BOOL islead, islead_default;
1087 CPINFOEXA cpi;
1088 UINT cp, acp;
1089 int i, num;
1090 LCID last;
1091 BOOL ret;
1093 struct test {
1094 LCID lcid;
1095 UINT threadcp;
1096 } lcids[] = {
1097 { HINDI, 0 },
1098 { GEORGIAN, 0 },
1099 { ENGLISH, 1252 },
1100 { RUSSIAN, 1251 },
1101 { JAPANESE, 932 },
1102 { CHINESE, 936 }
1105 struct test_islead_nocp {
1106 LCID lcid;
1107 BYTE testchar;
1108 } isleads_nocp[] = {
1109 { HINDI, 0x00 },
1110 { HINDI, 0x81 },
1111 { HINDI, 0xa0 },
1112 { HINDI, 0xe0 },
1114 { GEORGIAN, 0x00 },
1115 { GEORGIAN, 0x81 },
1116 { GEORGIAN, 0xa0 },
1117 { GEORGIAN, 0xe0 },
1120 struct test_islead {
1121 LCID lcid;
1122 BYTE testchar;
1123 BOOL islead;
1124 } isleads[] = {
1125 { ENGLISH, 0x00, FALSE },
1126 { ENGLISH, 0x81, FALSE },
1127 { ENGLISH, 0xa0, FALSE },
1128 { ENGLISH, 0xe0, FALSE },
1130 { RUSSIAN, 0x00, FALSE },
1131 { RUSSIAN, 0x81, FALSE },
1132 { RUSSIAN, 0xa0, FALSE },
1133 { RUSSIAN, 0xe0, FALSE },
1135 { JAPANESE, 0x00, FALSE },
1136 { JAPANESE, 0x81, TRUE },
1137 { JAPANESE, 0xa0, FALSE },
1138 { JAPANESE, 0xe0, TRUE },
1140 { CHINESE, 0x00, FALSE },
1141 { CHINESE, 0x81, TRUE },
1142 { CHINESE, 0xa0, TRUE },
1143 { CHINESE, 0xe0, TRUE },
1146 last = GetThreadLocale();
1147 acp = GetACP();
1149 for (i = 0; i < ARRAY_SIZE(lcids); i++)
1151 SetThreadLocale(lcids[i].lcid);
1153 cp = 0xdeadbeef;
1154 GetLocaleInfoA(lcids[i].lcid, LOCALE_IDEFAULTANSICODEPAGE|LOCALE_RETURN_NUMBER, (LPSTR)&cp, sizeof(cp));
1155 ok(cp == lcids[i].threadcp, "wrong codepage %u for lcid %04lx, should be %u\n", cp, lcids[i].lcid, lcids[i].threadcp);
1157 /* GetCPInfoEx/GetCPInfo - CP_ACP */
1158 SetLastError(0xdeadbeef);
1159 memset(&cpi, 0, sizeof(cpi));
1160 ret = GetCPInfoExA(CP_ACP, 0, &cpi);
1161 ok(ret, "GetCPInfoExA failed for lcid %04lx, error %ld\n", lcids[i].lcid, GetLastError());
1162 ok(cpi.CodePage == acp, "wrong codepage %u for lcid %04lx, should be %u\n", cpi.CodePage, lcids[i].lcid, acp);
1164 /* WideCharToMultiByte - CP_ACP */
1165 num = WideCharToMultiByte(CP_ACP, 0, foobarW, -1, NULL, 0, NULL, NULL);
1166 ok(num == 7, "ret is %d (%04lx)\n", num, lcids[i].lcid);
1168 /* MultiByteToWideChar - CP_ACP */
1169 num = MultiByteToWideChar(CP_ACP, 0, "foobar", -1, NULL, 0);
1170 ok(num == 7, "ret is %d (%04lx)\n", num, lcids[i].lcid);
1172 /* GetCPInfoEx/GetCPInfo - CP_THREAD_ACP */
1173 SetLastError(0xdeadbeef);
1174 memset(&cpi, 0, sizeof(cpi));
1175 ret = GetCPInfoExA(CP_THREAD_ACP, 0, &cpi);
1176 ok(ret, "GetCPInfoExA failed for lcid %04lx, error %ld\n", lcids[i].lcid, GetLastError());
1177 if (lcids[i].threadcp)
1178 ok(cpi.CodePage == lcids[i].threadcp || cpi.CodePage == CP_UTF8 /* Win10 1809+ */,
1179 "wrong codepage %u for lcid %04lx, should be %u\n",
1180 cpi.CodePage, lcids[i].lcid, lcids[i].threadcp);
1181 else
1182 ok(cpi.CodePage == acp || cpi.CodePage == CP_UTF8 /* Win10 1809+ */,
1183 "wrong codepage %u for lcid %04lx, should be %u\n", cpi.CodePage, lcids[i].lcid, acp);
1185 /* WideCharToMultiByte - CP_THREAD_ACP */
1186 num = WideCharToMultiByte(CP_THREAD_ACP, 0, foobarW, -1, NULL, 0, NULL, NULL);
1187 ok(num == 7, "ret is %d (%04lx)\n", num, lcids[i].lcid);
1189 /* MultiByteToWideChar - CP_THREAD_ACP */
1190 num = MultiByteToWideChar(CP_THREAD_ACP, 0, "foobar", -1, NULL, 0);
1191 ok(num == 7, "ret is %d (%04lx)\n", num, lcids[i].lcid);
1194 /* IsDBCSLeadByteEx - locales without codepage */
1195 for (i = 0; i < ARRAY_SIZE(isleads_nocp); i++)
1197 SetThreadLocale(isleads_nocp[i].lcid);
1199 GetCPInfoExA(CP_THREAD_ACP, 0, &cpi);
1200 islead_default = IsDBCSLeadByteEx(cpi.CodePage, isleads_nocp[i].testchar);
1201 islead = IsDBCSLeadByteEx(CP_THREAD_ACP, isleads_nocp[i].testchar);
1203 ok(islead == islead_default, "wrong islead %i for test char %x in lcid %04lx. should be %i\n",
1204 islead, isleads_nocp[i].testchar, isleads_nocp[i].lcid, islead_default);
1207 /* IsDBCSLeadByteEx - locales with codepage */
1208 for (i = 0; i < ARRAY_SIZE(isleads); i++)
1210 SetThreadLocale(isleads[i].lcid);
1212 GetCPInfoExA(CP_THREAD_ACP, 0, &cpi);
1213 islead = IsDBCSLeadByteEx(CP_THREAD_ACP, isleads[i].testchar);
1214 ok(islead == isleads[i].islead || (cpi.CodePage == CP_UTF8 && !islead),
1215 "wrong islead %i for test char %x in lcid %04lx. should be %i\n",
1216 islead, isleads[i].testchar, isleads[i].lcid, isleads[i].islead);
1219 SetThreadLocale(last);
1222 static void test_dbcs_to_widechar(void)
1224 int i, count, count2;
1225 WCHAR wbuf[5];
1226 unsigned char buf[] = {0xbf, 0xb4, 0xc7, '\0', 'x'};
1227 static const DWORD flags[] = {
1228 MB_PRECOMPOSED,
1229 MB_COMPOSITE,
1231 MB_PRECOMPOSED|MB_USEGLYPHCHARS,
1232 MB_COMPOSITE |MB_USEGLYPHCHARS,
1234 MB_PRECOMPOSED|MB_ERR_INVALID_CHARS,
1235 MB_COMPOSITE |MB_ERR_INVALID_CHARS,
1237 MB_PRECOMPOSED|MB_ERR_INVALID_CHARS|MB_USEGLYPHCHARS,
1238 MB_COMPOSITE |MB_ERR_INVALID_CHARS|MB_USEGLYPHCHARS,
1241 for (i = 0; i < ARRAY_SIZE(flags); ++i)
1243 memset(wbuf, 0xff, sizeof(wbuf));
1244 count = MultiByteToWideChar(936, flags[i], (char*)&buf[0], 2, NULL, 0);
1245 count2 = MultiByteToWideChar(936, flags[i], (char*)&buf[0], 2, wbuf, count);
1247 ok(count == 1, "%04lx: returned %d (expected 1)\n", flags[i], count);
1248 ok(count2 == 1, "%04lx: returned %d (expected 1)\n", flags[i], count2);
1249 ok(wbuf[0] == 0x770b, "%04lx: returned %04x (expected 770b)\n", flags[i], wbuf[0]);
1250 ok(wbuf[1] == 0xffff, "%04lx: returned %04x (expected ffff)\n", flags[i], wbuf[1]);
1253 for (i = 0; i < ARRAY_SIZE(flags); ++i)
1255 memset(wbuf, 0xff, sizeof(wbuf));
1256 count = MultiByteToWideChar(936, flags[i], (char*)&buf[0], 3, NULL, 0);
1257 SetLastError( 0xdeadbeef );
1258 count2 = MultiByteToWideChar(936, flags[i], (char*)&buf[0], 3, wbuf, count);
1260 if (flags[i] & MB_ERR_INVALID_CHARS)
1262 ok(count == 0, "%04lx: returned %d (expected 0)\n", flags[i], count);
1263 ok(count2 == 0, "%04lx: returned %d (expected 0)\n", flags[i], count2);
1264 ok(GetLastError() == ERROR_NO_UNICODE_TRANSLATION, "%04lx: returned %ld (expected %d)\n",
1265 flags[i], GetLastError(), ERROR_NO_UNICODE_TRANSLATION);
1267 else
1269 ok(count == 2, "%04lx: returned %d (expected 2)\n", flags[i], count);
1270 ok(count2 == 2, "%04lx: returned %d (expected 2)\n", flags[i], count2);
1271 ok(wbuf[0] == 0x770b, "%04lx: returned %04x (expected 770b)\n", flags[i], wbuf[0]);
1272 ok(wbuf[1] == 0x003f || broken(wbuf[1] == 0), /*windows xp*/
1273 "%04lx: wrong wide char: %04x\n", flags[i], wbuf[1]);
1274 ok(wbuf[2] == 0xffff, "%04lx: returned %04x (expected ffff)\n", flags[i], wbuf[2]);
1278 /* src ends with null character */
1279 for (i = 0; i < ARRAY_SIZE(flags); ++i)
1281 memset(wbuf, 0xff, sizeof(wbuf));
1282 count = MultiByteToWideChar(936, flags[i], (char*)&buf[0], 4, NULL, 0);
1283 SetLastError( 0xdeadbeef );
1284 count2 = MultiByteToWideChar(936, flags[i], (char*)&buf[0], 4, wbuf, count);
1285 ok(count == count2, "%04lx: returned %d (expected %d)\n", flags[i], count2, count);
1287 if (flags[i] & MB_ERR_INVALID_CHARS)
1289 ok(count == 0, "%04lx: returned %d (expected 0)\n", flags[i], count);
1290 ok(GetLastError() == ERROR_NO_UNICODE_TRANSLATION, "%04lx: returned %ld (expected %d)\n",
1291 flags[i], GetLastError(), ERROR_NO_UNICODE_TRANSLATION);
1293 else
1295 WCHAR wbuf_ok[] = { 0x770b, 0x003f, '\0', 0xffff };
1296 WCHAR wbuf_broken[] = { 0x770b, '\0', 0xffff, 0xffff };
1297 ok(count == 3 || broken(count == 2 /*windows xp*/),
1298 "%04lx: returned %d (expected 3)\n", flags[i], count);
1299 ok(!memcmp(wbuf, wbuf_ok, sizeof(wbuf_ok))
1300 || broken(!memcmp(wbuf, wbuf_broken, sizeof(wbuf_broken))),
1301 "%04lx: returned %04x %04x %04x %04x (expected %04x %04x %04x %04x)\n",
1302 flags[i], wbuf[0], wbuf[1], wbuf[2], wbuf[3],
1303 wbuf_ok[0], wbuf_ok[1], wbuf_ok[2], wbuf_ok[3]);
1307 /* src has null character, but not ends with it */
1308 for (i = 0; i < ARRAY_SIZE(flags); ++i)
1310 memset(wbuf, 0xff, sizeof(wbuf));
1311 count = MultiByteToWideChar(936, flags[i], (char*)&buf[0], 5, NULL, 0);
1312 SetLastError( 0xdeadbeef );
1313 count2 = MultiByteToWideChar(936, flags[i], (char*)&buf[0], 5, wbuf, count);
1314 ok(count == count2, "%04lx: returned %d (expected %d)\n", flags[i], count2, count);
1316 if (flags[i] & MB_ERR_INVALID_CHARS)
1318 ok(count == 0, "%04lx: returned %d (expected 0)\n", flags[i], count);
1319 ok(GetLastError() == ERROR_NO_UNICODE_TRANSLATION, "%04lx: returned %ld (expected %d)\n",
1320 flags[i], GetLastError(), ERROR_NO_UNICODE_TRANSLATION);
1322 else
1324 WCHAR wbuf_ok[] = { 0x770b, 0x003f, '\0', 'x', 0xffff };
1325 WCHAR wbuf_broken[] = { 0x770b, '\0', 'x', 0xffff, 0xffff };
1326 ok(count == 4 || broken(count == 3),
1327 "%04lx: returned %d (expected 4)\n", flags[i], count);
1328 ok(!memcmp(wbuf, wbuf_ok, sizeof(wbuf_ok))
1329 || broken(!memcmp(wbuf, wbuf_broken, sizeof(wbuf_broken))),
1330 "%04lx: returned %04x %04x %04x %04x %04x (expected %04x %04x %04x %04x %04x)\n",
1331 flags[i], wbuf[0], wbuf[1], wbuf[2], wbuf[3], wbuf[4],
1332 wbuf_ok[0], wbuf_ok[1], wbuf_ok[2], wbuf_ok[3], wbuf_ok[4]);
1337 START_TEST(codepage)
1339 BOOL bUsedDefaultChar;
1341 test_destination_buffer();
1342 test_null_source();
1343 test_negative_source_length();
1344 test_negative_dest_length();
1345 test_other_invalid_parameters();
1346 test_overlapped_buffers();
1348 /* WideCharToMultiByte has two code paths, test both here */
1349 test_string_conversion(NULL);
1350 test_string_conversion(&bUsedDefaultChar);
1352 test_utf7_encoding();
1353 test_utf7_decoding();
1355 test_undefined_byte_char();
1356 test_threadcp();
1358 test_dbcs_to_widechar();