1 /*
2  * Unit test suite for string functions.
3  *
4  * Copyright 2004 Uwe Bonnes
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20 
21 #include "precomp.h"
22 
23 #include <mbstring.h>
24 #include <wchar.h>
25 #include <mbctype.h>
26 #include <math.h>
27 
28 static char *buf_to_string(const unsigned char *bin, int len, int nr)
29 {
30     static char buf[2][1024];
31     char *w = buf[nr];
32     int i;
33 
34     for (i = 0; i < len; i++)
35     {
36         sprintf(w, "%02x ", (unsigned char)bin[i]);
37         w += strlen(w);
38     }
39     return buf[nr];
40 }
41 
42 #define expect_eq(expr, value, type, format) { type ret = (expr); ok((value) == ret, #expr " expected " format " got " format "\n", value, ret); }
43 #define expect_bin(buf, value, len) { ok(memcmp((buf), value, len) == 0, "Binary buffer mismatch - expected %s, got %s\n", buf_to_string((unsigned char *)value, len, 1), buf_to_string((buf), len, 0)); }
44 
45 static void* (__cdecl *pmemcpy)(void *, const void *, size_t n);
46 static int (__cdecl *p_memcpy_s)(void *, size_t, const void *, size_t);
47 static int (__cdecl *p_memmove_s)(void *, size_t, const void *, size_t);
48 static int* (__cdecl *pmemcmp)(void *, const void *, size_t n);
49 static int (__cdecl *p_strcpy)(char *dst, const char *src);
50 static int (__cdecl *pstrcpy_s)(char *dst, size_t len, const char *src);
51 static int (__cdecl *pstrcat_s)(char *dst, size_t len, const char *src);
52 static int (__cdecl *p_mbscat_s)(unsigned char *dst, size_t size, const unsigned char *src);
53 static int (__cdecl *p_mbsnbcat_s)(unsigned char *dst, size_t size, const unsigned char *src, size_t count);
54 static int (__cdecl *p_mbsnbcpy_s)(unsigned char * dst, size_t size, const unsigned char * src, size_t count);
55 static int (__cdecl *p__mbscpy_s)(unsigned char*, size_t, const unsigned char*);
56 static int (__cdecl *p_wcscpy_s)(wchar_t *wcDest, size_t size, const wchar_t *wcSrc);
57 static int (__cdecl *p_wcsncpy_s)(wchar_t *wcDest, size_t size, const wchar_t *wcSrc, size_t count);
58 static int (__cdecl *p_wcsncat_s)(wchar_t *dst, size_t elem, const wchar_t *src, size_t count);
59 static int (__cdecl *p_wcsupr_s)(wchar_t *str, size_t size);
60 static size_t (__cdecl *p_strnlen)(const char *, size_t);
61 static __int64 (__cdecl *p_strtoi64)(const char *, char **, int);
62 static unsigned __int64 (__cdecl *p_strtoui64)(const char *, char **, int);
63 static __int64 (__cdecl *p_wcstoi64)(const wchar_t *, wchar_t **, int);
64 static unsigned __int64 (__cdecl *p_wcstoui64)(const wchar_t *, wchar_t **, int);
65 static int (__cdecl *pwcstombs_s)(size_t*,char*,size_t,const wchar_t*,size_t);
66 static int (__cdecl *pmbstowcs_s)(size_t*,wchar_t*,size_t,const char*,size_t);
67 static size_t (__cdecl *p_mbsrtowcs)(wchar_t*, const char**, size_t, mbstate_t*);
68 static int (__cdecl *p_mbsrtowcs_s)(size_t*,wchar_t*,size_t,const char**,size_t,mbstate_t*);
69 static size_t (__cdecl *pwcsrtombs)(char*, const wchar_t**, size_t, int*);
70 static errno_t (__cdecl *p_gcvt_s)(char*,size_t,double,int);
71 static errno_t (__cdecl *p_itoa_s)(int,char*,size_t,int);
72 static errno_t (__cdecl *p_strlwr_s)(char*,size_t);
73 static errno_t (__cdecl *p_ultoa_s)(__msvcrt_ulong,char*,size_t,int);
74 static int *p__mb_cur_max;
75 static unsigned char *p_mbctype;
76 static int (__cdecl *p_wcslwr_s)(wchar_t*,size_t);
77 static errno_t (__cdecl *p_mbsupr_s)(unsigned char *str, size_t numberOfElements);
78 static errno_t (__cdecl *p_mbslwr_s)(unsigned char *str, size_t numberOfElements);
79 static int (__cdecl *p_wctob)(wint_t);
80 static size_t (__cdecl *p_wcrtomb)(char*, wchar_t, mbstate_t*);
81 static int (__cdecl *p_tolower)(int);
82 static size_t (__cdecl *p_mbrlen)(const char*, size_t, mbstate_t*);
83 static size_t (__cdecl *p_mbrtowc)(wchar_t*, const char*, size_t, mbstate_t*);
84 static int (__cdecl *p__atodbl_l)(_CRT_DOUBLE*,char*,_locale_t);
85 static double (__cdecl *p__atof_l)(const char*,_locale_t);
86 static double (__cdecl *p__strtod_l)(const char *,char**,_locale_t);
87 static int (__cdecl *p__strnset_s)(char*,size_t,int,size_t);
88 static int (__cdecl *p__wcsset_s)(wchar_t*,size_t,wchar_t);
89 
90 #define SETNOFAIL(x,y) x = (void*)GetProcAddress(hMsvcrt,y)
91 #define SET(x,y) SETNOFAIL(x,y); ok(x != NULL, "Export '%s' not found\n", y)
92 
93 static HMODULE hMsvcrt;
94 
95 static void test_swab( void ) {
96     char original[]  = "BADCFEHGJILKNMPORQTSVUXWZY@#";
97     char expected1[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ@#";
98     char expected2[] = "ABCDEFGHIJKLMNOPQRSTUVWX$";
99     char expected3[] = "$";
100 
101     char from[30];
102     char to[30];
103 
104     int testsize;
105 
106     /* Test 1 - normal even case */
107     memset(to,'$', sizeof(to));
108     memset(from,'@', sizeof(from));
109     testsize = 26;
110     memcpy(from, original, testsize);
111     _swab( from, to, testsize );
112     ok(memcmp(to,expected1,testsize) == 0, "Testing even size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
113 
114     /* Test 2 - uneven case  */
115     memset(to,'$', sizeof(to));
116     memset(from,'@', sizeof(from));
117     testsize = 25;
118     memcpy(from, original, testsize);
119     _swab( from, to, testsize );
120     ok(memcmp(to,expected2,testsize) == 0, "Testing odd size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
121 
122     /* Test 3 - from = to */
123     memset(to,'$', sizeof(to));
124     memset(from,'@', sizeof(from));
125     testsize = 26;
126     memcpy(to, original, testsize);
127     _swab( to, to, testsize );
128     ok(memcmp(to,expected1,testsize) == 0, "Testing overlapped size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
129 
130     /* Test 4 - 1 bytes */
131     memset(to,'$', sizeof(to));
132     memset(from,'@', sizeof(from));
133     testsize = 1;
134     memcpy(from, original, testsize);
135     _swab( from, to, testsize );
136     ok(memcmp(to,expected3,testsize) == 0, "Testing small size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
137 }
138 
139 #if 0      /* use this to generate more tests */
140 
141 static void test_codepage(int cp)
142 {
143     int i;
144     int prev;
145     int count = 1;
146 
147     ok(_setmbcp(cp) == 0, "Couldn't set mbcp\n");
148 
149     prev = p_mbctype[0];
150     printf("static int result_cp_%d_mbctype[] = { ", cp);
151     for (i = 1; i < 257; i++)
152     {
153         if (p_mbctype[i] != prev)
154         {
155             printf("0x%x,%d, ", prev, count);
156             prev = p_mbctype[i];
157             count = 1;
158         }
159         else
160             count++;
161     }
162     printf("0x%x,%d };\n", prev, count);
163 }
164 
165 #else
166 
167 /* RLE-encoded mbctype tables for given codepages */
168 static int result_cp_932_mbctype[] = { 0x0,65, 0x8,1, 0x18,26, 0x8,6, 0x28,26, 0x8,4,
169   0x0,1, 0x8,1, 0xc,31, 0x8,1, 0xa,5, 0x9,58, 0xc,29, 0,3 };
170 static int result_cp_936_mbctype[] = { 0x0,65, 0x8,1, 0x18,26, 0x8,6, 0x28,26, 0x8,6,
171   0xc,126, 0,1 };
172 static int result_cp_949_mbctype[] = { 0x0,66, 0x18,26, 0x8,6, 0x28,26, 0x8,6, 0xc,126,
173   0,1 };
174 static int result_cp_950_mbctype[] = { 0x0,65, 0x8,1, 0x18,26, 0x8,6, 0x28,26, 0x8,4,
175   0x0,2, 0x4,32, 0xc,94, 0,1 };
176 
177 static void test_cp_table(int cp, int *result)
178 {
179     int i;
180     int count = 0;
181     int curr = 0;
182     _setmbcp(cp);
183     for (i = 0; i < 256; i++)
184     {
185         if (count == 0)
186         {
187             curr = result[0];
188             count = result[1];
189             result += 2;
190         }
191         ok(p_mbctype[i] == curr, "CP%d: Mismatch in ctype for character %d - %d instead of %d\n", cp, i-1, p_mbctype[i], curr);
192         count--;
193     }
194 }
195 
196 #define test_codepage(num) test_cp_table(num, result_cp_##num##_mbctype);
197 
198 #endif
199 
200 static void test_mbcp(void)
201 {
202     int mb_orig_max = *p__mb_cur_max;
203     int curr_mbcp = _getmbcp();
204     unsigned char *mbstring = (unsigned char *)"\xb0\xb1\xb2 \xb3\xb4 \xb5"; /* incorrect string */
205     unsigned char *mbstring2 = (unsigned char *)"\xb0\xb1\xb2\xb3Q\xb4\xb5"; /* correct string */
206     unsigned char *mbsonlylead = (unsigned char *)"\xb0\0\xb1\xb2 \xb3";
207     unsigned char buf[16];
208     int step;
209     CPINFO cp_info;
210 
211     /* _mbtype tests */
212 
213     /* An SBCS codepage test. The ctype of characters on e.g. CP1252 or CP1250 differs slightly
214      * between versions of Windows. Also Windows 9x seems to ignore the codepage and always uses
215      * CP1252 (or the ACP?) so we test only a few ASCII characters */
216     _setmbcp(1252);
217     expect_eq(p_mbctype[10], 0, char, "%x");
218     expect_eq(p_mbctype[50], 0, char, "%x");
219     expect_eq(p_mbctype[66], _SBUP, char, "%x");
220     expect_eq(p_mbctype[100], _SBLOW, char, "%x");
221     expect_eq(p_mbctype[128], 0, char, "%x");
222     _setmbcp(1250);
223     expect_eq(p_mbctype[10], 0, char, "%x");
224     expect_eq(p_mbctype[50], 0, char, "%x");
225     expect_eq(p_mbctype[66], _SBUP, char, "%x");
226     expect_eq(p_mbctype[100], _SBLOW, char, "%x");
227     expect_eq(p_mbctype[128], 0, char, "%x");
228 
229     /* double byte code pages */
230     test_codepage(932);
231     test_codepage(936);
232     test_codepage(949);
233     test_codepage(950);
234 
235     _setmbcp(936);
236     ok(*p__mb_cur_max == mb_orig_max, "__mb_cur_max shouldn't be updated (is %d != %d)\n", *p__mb_cur_max, mb_orig_max);
237     ok(_ismbblead('\354'), "\354 should be a lead byte\n");
238     ok(_ismbblead(' ') == FALSE, "' ' should not be a lead byte\n");
239     ok(_ismbblead(0x1234b0), "0x1234b0 should not be a lead byte\n");
240     ok(_ismbblead(0x123420) == FALSE, "0x123420 should not be a lead byte\n");
241     ok(_ismbbtrail('\xb0'), "\xa0 should be a trail byte\n");
242     ok(_ismbbtrail(' ') == FALSE, "' ' should not be a trail byte\n");
243 
244     /* _ismbslead */
245     expect_eq(_ismbslead(mbstring, &mbstring[0]), -1, int, "%d");
246     expect_eq(_ismbslead(mbstring, &mbstring[1]), FALSE, int, "%d");
247     expect_eq(_ismbslead(mbstring, &mbstring[2]), -1, int, "%d");
248     expect_eq(_ismbslead(mbstring, &mbstring[3]), FALSE, int, "%d");
249     expect_eq(_ismbslead(mbstring, &mbstring[4]), -1, int, "%d");
250     expect_eq(_ismbslead(mbstring, &mbstring[5]), FALSE, int, "%d");
251     expect_eq(_ismbslead(mbstring, &mbstring[6]), FALSE, int, "%d");
252     expect_eq(_ismbslead(mbstring, &mbstring[7]), -1, int, "%d");
253     expect_eq(_ismbslead(mbstring, &mbstring[8]), FALSE, int, "%d");
254 
255     expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[0]), -1, int, "%d");
256     expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[1]), FALSE, int, "%d");
257     expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[2]), FALSE, int, "%d");
258     expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[5]), FALSE, int, "%d");
259 
260     /* _ismbstrail */
261     expect_eq(_ismbstrail(mbstring, &mbstring[0]), FALSE, int, "%d");
262     expect_eq(_ismbstrail(mbstring, &mbstring[1]), -1, int, "%d");
263     expect_eq(_ismbstrail(mbstring, &mbstring[2]), FALSE, int, "%d");
264     expect_eq(_ismbstrail(mbstring, &mbstring[3]), -1, int, "%d");
265     expect_eq(_ismbstrail(mbstring, &mbstring[4]), FALSE, int, "%d");
266     expect_eq(_ismbstrail(mbstring, &mbstring[5]), -1, int, "%d");
267     expect_eq(_ismbstrail(mbstring, &mbstring[6]), FALSE, int, "%d");
268     expect_eq(_ismbstrail(mbstring, &mbstring[7]), FALSE, int, "%d");
269     expect_eq(_ismbstrail(mbstring, &mbstring[8]), -1, int, "%d");
270 
271     expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[0]), FALSE, int, "%d");
272     expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[1]), -1, int, "%d");
273     expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[2]), FALSE, int, "%d");
274     expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[3]), FALSE, int, "%d");
275     expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[4]), FALSE, int, "%d");
276     expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[5]), FALSE, int, "%d");
277 
278     /* _mbsbtype */
279     expect_eq(_mbsbtype(mbstring, 0), _MBC_LEAD, int, "%d");
280     expect_eq(_mbsbtype(mbstring, 1), _MBC_TRAIL, int, "%d");
281     expect_eq(_mbsbtype(mbstring, 2), _MBC_LEAD, int, "%d");
282     expect_eq(_mbsbtype(mbstring, 3), _MBC_ILLEGAL, int, "%d");
283     expect_eq(_mbsbtype(mbstring, 4), _MBC_LEAD, int, "%d");
284     expect_eq(_mbsbtype(mbstring, 5), _MBC_TRAIL, int, "%d");
285     expect_eq(_mbsbtype(mbstring, 6), _MBC_SINGLE, int, "%d");
286     expect_eq(_mbsbtype(mbstring, 7), _MBC_LEAD, int, "%d");
287     expect_eq(_mbsbtype(mbstring, 8), _MBC_ILLEGAL, int, "%d");
288 
289     expect_eq(_mbsbtype(mbsonlylead, 0), _MBC_LEAD, int, "%d");
290     expect_eq(_mbsbtype(mbsonlylead, 1), _MBC_ILLEGAL, int, "%d");
291     expect_eq(_mbsbtype(mbsonlylead, 2), _MBC_ILLEGAL, int, "%d");
292     expect_eq(_mbsbtype(mbsonlylead, 3), _MBC_ILLEGAL, int, "%d");
293     expect_eq(_mbsbtype(mbsonlylead, 4), _MBC_ILLEGAL, int, "%d");
294     expect_eq(_mbsbtype(mbsonlylead, 5), _MBC_ILLEGAL, int, "%d");
295 
296     /* _mbsnextc */
297     expect_eq(_mbsnextc(mbstring), 0xb0b1, int, "%x");
298     expect_eq(_mbsnextc(&mbstring[2]), 0xb220, int, "%x");  /* lead + invalid tail */
299     expect_eq(_mbsnextc(&mbstring[3]), 0x20, int, "%x");    /* single char */
300 
301     /* _mbclen/_mbslen */
302     expect_eq(_mbclen(mbstring), 2, int, "%d");
303     expect_eq(_mbclen(&mbstring[2]), 2, int, "%d");
304     expect_eq(_mbclen(&mbstring[3]), 1, int, "%d");
305     expect_eq(_mbslen(mbstring2), 4, int, "%d");
306     expect_eq(_mbslen(mbsonlylead), 0, int, "%d");          /* lead + NUL not counted as character */
307     expect_eq(_mbslen(mbstring), 4, int, "%d");             /* lead + invalid trail counted */
308 
309     /* mbrlen */
310     if(!setlocale(LC_ALL, ".936") || !p_mbrlen) {
311         win_skip("mbrlen tests\n");
312     }else {
313         mbstate_t state = 0;
314         expect_eq(p_mbrlen((const char*)mbstring, 2, NULL), 2, int, "%d");
315         expect_eq(p_mbrlen((const char*)&mbstring[2], 2, NULL), 2, int, "%d");
316         expect_eq(p_mbrlen((const char*)&mbstring[3], 2, NULL), 1, int, "%d");
317         expect_eq(p_mbrlen((const char*)mbstring, 1, NULL), -2, int, "%d");
318         expect_eq(p_mbrlen((const char*)mbstring, 1, &state), -2, int, "%d");
319         ok(state == mbstring[0], "incorrect state value (%x)\n", state);
320         expect_eq(p_mbrlen((const char*)&mbstring[1], 1, &state), 2, int, "%d");
321     }
322 
323     /* mbrtowc */
324     if(!setlocale(LC_ALL, ".936") || !p_mbrtowc) {
325         win_skip("mbrtowc tests\n");
326     }else {
327         mbstate_t state = 0;
328         wchar_t dst;
329         expect_eq(p_mbrtowc(&dst, (const char*)mbstring, 2, NULL), 2, int, "%d");
330         ok(dst == 0x6c28, "dst = %x, expected 0x6c28\n", dst);
331         expect_eq(p_mbrtowc(&dst, (const char*)mbstring+2, 2, NULL), 2, int, "%d");
332         ok(dst == 0x3f, "dst = %x, expected 0x3f\n", dst);
333         expect_eq(p_mbrtowc(&dst, (const char*)mbstring+3, 2, NULL), 1, int, "%d");
334         ok(dst == 0x20, "dst = %x, expected 0x20\n", dst);
335         expect_eq(p_mbrtowc(&dst, (const char*)mbstring, 1, NULL), -2, int, "%d");
336         ok(dst == 0, "dst = %x, expected 0\n", dst);
337         expect_eq(p_mbrtowc(&dst, (const char*)mbstring, 1, &state), -2, int, "%d");
338         ok(dst == 0, "dst = %x, expected 0\n", dst);
339         ok(state == mbstring[0], "incorrect state value (%x)\n", state);
340         expect_eq(p_mbrtowc(&dst, (const char*)mbstring+1, 1, &state), 2, int, "%d");
341         ok(dst == 0x6c28, "dst = %x, expected 0x6c28\n", dst);
342         ok(state == 0, "incorrect state value (%x)\n", state);
343     }
344     setlocale(LC_ALL, "C");
345 
346     /* _mbccpy/_mbsncpy */
347     memset(buf, 0xff, sizeof(buf));
348     _mbccpy(buf, mbstring);
349     expect_bin(buf, "\xb0\xb1\xff", 3);
350 
351     memset(buf, 0xff, sizeof(buf));
352     _mbsncpy(buf, mbstring, 1);
353     expect_bin(buf, "\xb0\xb1\xff", 3);
354     memset(buf, 0xff, sizeof(buf));
355     _mbsncpy(buf, mbstring, 2);
356     expect_bin(buf, "\xb0\xb1\xb2 \xff", 5);
357     memset(buf, 0xff, sizeof(buf));
358     _mbsncpy(buf, mbstring, 3);
359     expect_bin(buf, "\xb0\xb1\xb2 \xb3\xb4\xff", 7);
360     memset(buf, 0xff, sizeof(buf));
361     _mbsncpy(buf, mbstring, 4);
362     expect_bin(buf, "\xb0\xb1\xb2 \xb3\xb4 \xff", 8);
363     memset(buf, 0xff, sizeof(buf));
364     _mbsncpy(buf, mbstring, 5);
365     expect_bin(buf, "\xb0\xb1\xb2 \xb3\xb4 \0\0\xff", 10);
366     memset(buf, 0xff, sizeof(buf));
367     _mbsncpy(buf, mbsonlylead, 6);
368     expect_bin(buf, "\0\0\0\0\0\0\0\xff", 8);
369 
370     memset(buf, 0xff, sizeof(buf));
371     _mbsnbcpy(buf, mbstring2, 2);
372     expect_bin(buf, "\xb0\xb1\xff", 3);
373     _mbsnbcpy(buf, mbstring2, 3);
374     expect_bin(buf, "\xb0\xb1\0\xff", 4);
375     _mbsnbcpy(buf, mbstring2, 4);
376     expect_bin(buf, "\xb0\xb1\xb2\xb3\xff", 5);
377     memset(buf, 0xff, sizeof(buf));
378     _mbsnbcpy(buf, mbsonlylead, 5);
379     expect_bin(buf, "\0\0\0\0\0\xff", 6);
380 
381     /* _mbsinc/mbsdec */
382     step = _mbsinc(mbstring) - mbstring;
383     ok(step == 2, "_mbsinc adds %d (exp. 2)\n", step);
384     step = _mbsinc(&mbstring[2]) - &mbstring[2];  /* lead + invalid tail */
385     ok(step == 2, "_mbsinc adds %d (exp. 2)\n", step);
386 
387     step = _mbsninc(mbsonlylead, 1) - mbsonlylead;
388     ok(step == 0, "_mbsninc adds %d (exp. 0)\n", step);
389     step = _mbsninc(mbsonlylead, 2) - mbsonlylead;  /* lead + NUL byte + lead + char */
390     ok(step == 0, "_mbsninc adds %d (exp. 0)\n", step);
391     step = _mbsninc(mbstring2, 0) - mbstring2;
392     ok(step == 0, "_mbsninc adds %d (exp. 2)\n", step);
393     step = _mbsninc(mbstring2, 1) - mbstring2;
394     ok(step == 2, "_mbsninc adds %d (exp. 2)\n", step);
395     step = _mbsninc(mbstring2, 2) - mbstring2;
396     ok(step == 4, "_mbsninc adds %d (exp. 4)\n", step);
397     step = _mbsninc(mbstring2, 3) - mbstring2;
398     ok(step == 5, "_mbsninc adds %d (exp. 5)\n", step);
399     step = _mbsninc(mbstring2, 4) - mbstring2;
400     ok(step == 7, "_mbsninc adds %d (exp. 7)\n", step);
401     step = _mbsninc(mbstring2, 5) - mbstring2;
402     ok(step == 7, "_mbsninc adds %d (exp. 7)\n", step);
403     step = _mbsninc(mbstring2, 17) - mbstring2;
404     ok(step == 7, "_mbsninc adds %d (exp. 7)\n", step);
405 
406     /* functions that depend on locale codepage, not mbcp.
407      * we hope the current locale to be SBCS because setlocale(LC_ALL, ".1252") seems not to work yet
408      * (as of Wine 0.9.43)
409      */
410     GetCPInfo(GetACP(), &cp_info);
411     if (cp_info.MaxCharSize == 1)
412     {
413         expect_eq(mblen((char *)mbstring, 3), 1, int, "%x");
414         expect_eq(_mbstrlen((char *)mbstring2), 7, int, "%d");
415     }
416     else
417         skip("Current locale has double-byte charset - could lead to false positives\n");
418 
419     _setmbcp(1361);
420     expect_eq(_ismbblead(0x80), 0, int, "%d");
421     todo_wine {
422       expect_eq(_ismbblead(0x81), 1, int, "%d");
423       expect_eq(_ismbblead(0x83), 1, int, "%d");
424     }
425     expect_eq(_ismbblead(0x84), 1, int, "%d");
426     expect_eq(_ismbblead(0xd3), 1, int, "%d");
427     expect_eq(_ismbblead(0xd7), 0, int, "%d");
428     expect_eq(_ismbblead(0xd8), 1, int, "%d");
429     expect_eq(_ismbblead(0xd9), 1, int, "%d");
430 
431     expect_eq(_ismbbtrail(0x30), 0, int, "%d");
432     expect_eq(_ismbbtrail(0x31), 1, int, "%d");
433     expect_eq(_ismbbtrail(0x7e), 1, int, "%d");
434     expect_eq(_ismbbtrail(0x7f), 0, int, "%d");
435     expect_eq(_ismbbtrail(0x80), 0, int, "%d");
436     expect_eq(_ismbbtrail(0x81), 1, int, "%d");
437     expect_eq(_ismbbtrail(0xfe), 1, int, "%d");
438     expect_eq(_ismbbtrail(0xff), 0, int, "%d");
439 
440     _setmbcp(curr_mbcp);
441 }
442 
443 static void test_mbsspn( void)
444 {
445     unsigned char str1[]="cabernet";
446     unsigned char str2[]="shiraz";
447     unsigned char set[]="abc";
448     unsigned char empty[]="";
449     int ret;
450     ret=_mbsspn( str1, set);
451     ok( ret==3, "_mbsspn returns %d should be 3\n", ret);
452     ret=_mbsspn( str2, set);
453     ok( ret==0, "_mbsspn returns %d should be 0\n", ret);
454     ret=_mbsspn( str1, empty);
455     ok( ret==0, "_mbsspn returns %d should be 0\n", ret);
456 }
457 
458 static void test_mbsspnp( void)
459 {
460     unsigned char str1[]="cabernet";
461     unsigned char str2[]="shiraz";
462     unsigned char set[]="abc";
463     unsigned char empty[]="";
464     unsigned char full[]="abcenrt";
465     unsigned char* ret;
466     ret=_mbsspnp( str1, set);
467     ok( ret[0]=='e', "_mbsspnp returns %c should be e\n", ret[0]);
468     ret=_mbsspnp( str2, set);
469     ok( ret[0]=='s', "_mbsspnp returns %c should be s\n", ret[0]);
470     ret=_mbsspnp( str1, empty);
471     ok( ret[0]=='c', "_mbsspnp returns %c should be c\n", ret[0]);
472     ret=_mbsspnp( str1, full);
473     ok( ret==NULL, "_mbsspnp returns %p should be NULL\n", ret);
474 }
475 
476 static void test_strdup(void)
477 {
478    char *str;
479    str = _strdup( 0 );
480    ok( str == 0, "strdup returns %s should be 0\n", str);
481    free( str );
482 }
483 
484 static void test_strcpy_s(void)
485 {
486     char dest[8];
487     const char small[] = "small";
488     const char big[] = "atoolongstringforthislittledestination";
489     int ret;
490 
491     if(!pstrcpy_s)
492     {
493         win_skip("strcpy_s not found\n");
494         return;
495     }
496 
497     memset(dest, 'X', sizeof(dest));
498     ret = pstrcpy_s(dest, sizeof(dest), small);
499     ok(ret == 0, "Copying a string into a big enough destination returned %d, expected 0\n", ret);
500     ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 'l' &&
501        dest[4] == 'l' && dest[5] == '\0'&& dest[6] == 'X' && dest[7] == 'X',
502        "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
503        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
504 
505     memset(dest, 'X', sizeof(dest));
506     ret = pstrcpy_s(dest, 0, big);
507     ok(ret == EINVAL, "Copying into a destination of size 0 returned %d, expected EINVAL\n", ret);
508     ok(dest[0] == 'X' && dest[1] == 'X' && dest[2] == 'X' && dest[3] == 'X' &&
509        dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
510        "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
511        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
512     ret = pstrcpy_s(dest, 0, NULL);
513     ok(ret == EINVAL, "Copying into a destination of size 0 returned %d, expected EINVAL\n", ret);
514     ok(dest[0] == 'X' && dest[1] == 'X' && dest[2] == 'X' && dest[3] == 'X' &&
515        dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
516        "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
517        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
518 
519     memset(dest, 'X', sizeof(dest));
520     ret = pstrcpy_s(dest, sizeof(dest), big);
521     ok(ret == ERANGE, "Copying a big string in a small location returned %d, expected ERANGE\n", ret);
522     ok(dest[0] == '\0'&& dest[1] == 't' && dest[2] == 'o' && dest[3] == 'o' &&
523        dest[4] == 'l' && dest[5] == 'o' && dest[6] == 'n' && dest[7] == 'g',
524        "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
525        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
526 
527     memset(dest, 'X', sizeof(dest));
528     ret = pstrcpy_s(dest, sizeof(dest), NULL);
529     ok(ret == EINVAL, "Copying from a NULL source string returned %d, expected EINVAL\n", ret);
530     ok(dest[0] == '\0'&& dest[1] == 'X' && dest[2] == 'X' && dest[3] == 'X' &&
531        dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
532        "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
533        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
534 
535     ret = pstrcpy_s(NULL, sizeof(dest), small);
536     ok(ret == EINVAL, "Copying a big string a NULL dest returned %d, expected EINVAL\n", ret);
537 
538     /* strcpy overlapping buffers test */
539     memset(dest, 'X', sizeof(dest));
540     memcpy(dest+1, small, sizeof(small));
541     p_strcpy(dest, dest+1);
542     ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 'l' &&
543             dest[4] == 'l' && dest[5] == '\0' && dest[6] == '\0' && dest[7] == 'X',
544             "Unexpected return data from strcpy: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
545             dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
546 }
547 
548 #define NUMELMS(array) (sizeof(array)/sizeof((array)[0]))
549 
550 #define okchars(dst, b0, b1, b2, b3, b4, b5, b6, b7) \
551     ok(dst[0] == b0 && dst[1] == b1 && dst[2] == b2 && dst[3] == b3 && \
552        dst[4] == b4 && dst[5] == b5 && dst[6] == b6 && dst[7] == b7, \
553        "Bad result: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",\
554        dst[0], dst[1], dst[2], dst[3], dst[4], dst[5], dst[6], dst[7])
555 
556 static void test_memcpy_s(void)
557 {
558     static char dest[8];
559     static const char tiny[] = {'T',0,'I','N','Y',0};
560     static const char big[] = {'a','t','o','o','l','o','n','g','s','t','r','i','n','g',0};
561     int ret;
562     if (!p_memcpy_s) {
563         win_skip("memcpy_s not found\n");
564         return;
565     }
566 
567     /* Normal */
568     memset(dest, 'X', sizeof(dest));
569     ret = p_memcpy_s(dest, NUMELMS(dest), tiny, NUMELMS(tiny));
570     ok(ret == 0, "Copying a buffer into a big enough destination returned %d, expected 0\n", ret);
571     okchars(dest, tiny[0], tiny[1], tiny[2], tiny[3], tiny[4], tiny[5], 'X', 'X');
572 
573     /* Vary source size */
574     errno = 0xdeadbeef;
575     memset(dest, 'X', sizeof(dest));
576     ret = p_memcpy_s(dest, NUMELMS(dest), big, NUMELMS(big));
577     ok(ret == ERANGE, "Copying a big buffer to a small destination returned %d, expected ERANGE\n", ret);
578     ok(errno == ERANGE, "errno is %d, expected ERANGE\n", errno);
579     okchars(dest, 0, 0, 0, 0, 0, 0, 0, 0);
580 
581     /* Replace source with NULL */
582     errno = 0xdeadbeef;
583     memset(dest, 'X', sizeof(dest));
584     ret = p_memcpy_s(dest, NUMELMS(dest), NULL, NUMELMS(tiny));
585     ok(ret == EINVAL, "Copying a NULL source buffer returned %d, expected EINVAL\n", ret);
586     ok(errno == EINVAL, "errno is %d, expected EINVAL\n", errno);
587     okchars(dest, 0, 0, 0, 0, 0, 0, 0, 0);
588 
589     /* Vary dest size */
590     errno = 0xdeadbeef;
591     memset(dest, 'X', sizeof(dest));
592     ret = p_memcpy_s(dest, 0, tiny, NUMELMS(tiny));
593     ok(ret == ERANGE, "Copying into a destination of size 0 returned %d, expected ERANGE\n", ret);
594     ok(errno == ERANGE, "errno is %d, expected ERANGE\n", errno);
595     okchars(dest, 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X');
596 
597     /* Replace dest with NULL */
598     errno = 0xdeadbeef;
599     ret = p_memcpy_s(NULL, NUMELMS(dest), tiny, NUMELMS(tiny));
600     ok(ret == EINVAL, "Copying a tiny buffer to a big NULL destination returned %d, expected EINVAL\n", ret);
601     ok(errno == EINVAL, "errno is %d, expected EINVAL\n", errno);
602 
603     /* Combinations */
604     errno = 0xdeadbeef;
605     memset(dest, 'X', sizeof(dest));
606     ret = p_memcpy_s(dest, 0, NULL, NUMELMS(tiny));
607     ok(ret == EINVAL, "Copying a NULL buffer into a destination of size 0 returned %d, expected EINVAL\n", ret);
608     ok(errno == EINVAL, "errno is %d, expected EINVAL\n", errno);
609     okchars(dest, 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X');
610 }
611 
612 static void test_memmove_s(void)
613 {
614     static char dest[8];
615     static const char tiny[] = {'T',0,'I','N','Y',0};
616     static const char big[] = {'a','t','o','o','l','o','n','g','s','t','r','i','n','g',0};
617     int ret;
618     if (!p_memmove_s) {
619         win_skip("memmove_s not found\n");
620         return;
621     }
622 
623     /* Normal */
624     memset(dest, 'X', sizeof(dest));
625     ret = p_memmove_s(dest, NUMELMS(dest), tiny, NUMELMS(tiny));
626     ok(ret == 0, "Moving a buffer into a big enough destination returned %d, expected 0\n", ret);
627     okchars(dest, tiny[0], tiny[1], tiny[2], tiny[3], tiny[4], tiny[5], 'X', 'X');
628 
629     /* Overlapping */
630     memcpy(dest, big, sizeof(dest));
631     ret = p_memmove_s(dest+1, NUMELMS(dest)-1, dest, NUMELMS(dest)-1);
632     ok(ret == 0, "Moving a buffer up one char returned %d, expected 0\n", ret);
633     okchars(dest, big[0], big[0], big[1], big[2], big[3], big[4], big[5], big[6]);
634 
635     /* Vary source size */
636     errno = 0xdeadbeef;
637     memset(dest, 'X', sizeof(dest));
638     ret = p_memmove_s(dest, NUMELMS(dest), big, NUMELMS(big));
639     ok(ret == ERANGE, "Moving a big buffer to a small destination returned %d, expected ERANGE\n", ret);
640     ok(errno == ERANGE, "errno is %d, expected ERANGE\n", errno);
641     okchars(dest, 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X');
642 
643     /* Replace source with NULL */
644     errno = 0xdeadbeef;
645     memset(dest, 'X', sizeof(dest));
646     ret = p_memmove_s(dest, NUMELMS(dest), NULL, NUMELMS(tiny));
647     ok(ret == EINVAL, "Moving a NULL source buffer returned %d, expected EINVAL\n", ret);
648     ok(errno == EINVAL, "errno is %d, expected EINVAL\n", errno);
649     okchars(dest, 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X');
650 
651     /* Vary dest size */
652     errno = 0xdeadbeef;
653     memset(dest, 'X', sizeof(dest));
654     ret = p_memmove_s(dest, 0, tiny, NUMELMS(tiny));
655     ok(ret == ERANGE, "Moving into a destination of size 0 returned %d, expected ERANGE\n", ret);
656     ok(errno == ERANGE, "errno is %d, expected ERANGE\n", errno);
657     okchars(dest, 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X');
658 
659     /* Replace dest with NULL */
660     errno = 0xdeadbeef;
661     ret = p_memmove_s(NULL, NUMELMS(dest), tiny, NUMELMS(tiny));
662     ok(ret == EINVAL, "Moving a tiny buffer to a big NULL destination returned %d, expected EINVAL\n", ret);
663     ok(errno == EINVAL, "errno is %d, expected EINVAL\n", errno);
664 
665     /* Combinations */
666     errno = 0xdeadbeef;
667     memset(dest, 'X', sizeof(dest));
668     ret = p_memmove_s(dest, 0, NULL, NUMELMS(tiny));
669     ok(ret == EINVAL, "Moving a NULL buffer into a destination of size 0 returned %d, expected EINVAL\n", ret);
670     ok(errno == EINVAL, "errno is %d, expected EINVAL\n", errno);
671     okchars(dest, 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X');
672 }
673 
674 static void test_strcat_s(void)
675 {
676     char dest[8];
677     const char *small = "sma";
678     int ret;
679 
680     if(!pstrcat_s)
681     {
682         win_skip("strcat_s not found\n");
683         return;
684     }
685 
686     memset(dest, 'X', sizeof(dest));
687     dest[0] = '\0';
688     ret = pstrcat_s(dest, sizeof(dest), small);
689     ok(ret == 0, "strcat_s: Copying a string into a big enough destination returned %d, expected 0\n", ret);
690     ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == '\0'&&
691        dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
692        "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
693        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
694     ret = pstrcat_s(dest, sizeof(dest), small);
695     ok(ret == 0, "strcat_s: Attaching a string to a big enough destination returned %d, expected 0\n", ret);
696     ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 's' &&
697        dest[4] == 'm' && dest[5] == 'a' && dest[6] == '\0'&& dest[7] == 'X',
698        "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
699        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
700 
701     ret = pstrcat_s(dest, sizeof(dest), small);
702     ok(ret == ERANGE, "strcat_s: Attaching a string to a filled up destination returned %d, expected ERANGE\n", ret);
703     ok(dest[0] == '\0'&& dest[1] == 'm' && dest[2] == 'a' && dest[3] == 's' &&
704        dest[4] == 'm' && dest[5] == 'a' && dest[6] == 's' && dest[7] == 'm',
705        "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
706        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
707 
708     memset(dest, 'X', sizeof(dest));
709     dest[0] = 'a';
710     dest[1] = '\0';
711 
712     ret = pstrcat_s(dest, 0, small);
713     ok(ret == EINVAL, "strcat_s: Source len = 0 returned %d, expected EINVAL\n", ret);
714     ok(dest[0] == 'a' && dest[1] == '\0'&& dest[2] == 'X' && dest[3] == 'X' &&
715        dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
716        "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
717        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
718 
719     ret = pstrcat_s(dest, 0, NULL);
720     ok(ret == EINVAL, "strcat_s: len = 0 and src = NULL returned %d, expected EINVAL\n", ret);
721     ok(dest[0] == 'a' && dest[1] == '\0'&& dest[2] == 'X' && dest[3] == 'X' &&
722        dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
723        "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
724        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
725 
726     ret = pstrcat_s(dest, sizeof(dest), NULL);
727     ok(ret == EINVAL, "strcat_s:  Sourcing from NULL returned %d, expected EINVAL\n", ret);
728     ok(dest[0] == '\0'&& dest[1] == '\0'&& dest[2] == 'X' && dest[3] == 'X' &&
729        dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
730        "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
731        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
732 
733     ret = pstrcat_s(NULL, sizeof(dest), small);
734     ok(ret == EINVAL, "strcat_s: Writing to a NULL string returned %d, expected EINVAL\n", ret);
735 }
736 
737 static void test__mbscat_s(void)
738 {
739     unsigned char dst[8], src[4];
740     int err;
741     int prev_cp = _getmbcp();
742 
743     if(!p_mbscat_s)
744     {
745         win_skip("_mbscat_s not found\n");
746         return;
747     }
748 
749 
750     src[0] = dst[0] = 0;
751     err = p_mbscat_s(NULL, sizeof(dst), src);
752     ok(err == EINVAL, "_mbscat_s returned %d\n", err);
753 
754     err = p_mbscat_s(dst, sizeof(dst), NULL);
755     ok(err == EINVAL, "_mbscat_s returned %d\n", err);
756 
757     dst[0] = 'a';
758     err = p_mbscat_s(dst, 1, src);
759     ok(err == EINVAL, "_mbscat_s returned %d\n", err);
760 
761     memset(dst, 'a', sizeof(dst));
762     dst[6] = 0;
763     src[0] = 'b';
764     src[1] = 0;
765 
766     err = p_mbscat_s(dst, sizeof(dst), src);
767     ok(err == 0, "_mbscat_s returned %d\n", err);
768     ok(!memcmp(dst, "aaaaaab", 8), "dst = %s\n", dst);
769 
770     err = p_mbscat_s(dst, sizeof(dst), src);
771     ok(err == ERANGE, "_mbscat_s returned %d\n", err);
772     ok(!dst[0], "dst[0] = %c\n", dst[0]);
773     ok(dst[1] == 'a', "dst[1] = %c\n", dst[1]);
774 
775     _setmbcp(932);
776     /* test invalid str in dst */
777     dst[0] = 0x81;
778     dst[1] = 0x81;
779     dst[2] = 0x52;
780     dst[3] = 0;
781     src[0] = 'a';
782     src[1] = 0;
783     err = p_mbscat_s(dst, sizeof(dst), src);
784     ok(err == 0, "_mbscat_s returned %d\n", err);
785 
786     /* test invalid str in src */
787     dst[0] = 0;
788     src[0] = 0x81;
789     src[1] = 0x81;
790     src[2] = 0x52;
791     src[3] = 0;
792     err = p_mbscat_s(dst, sizeof(dst), src);
793     ok(err == 0, "_mbscat_s returned %d\n", err);
794 
795     /* test dst with leading byte on the end of buffer */
796     dst[0] = 'a';
797     dst[1] = 0x81;
798     dst[2] = 0;
799     src[0] = 'R';
800     src[1] = 0;
801     err = p_mbscat_s(dst, sizeof(dst), src);
802     ok(err == EILSEQ, "_mbscat_s returned %d\n", err);
803     ok(!memcmp(dst, "aR", 3), "dst = %s\n", dst);
804 
805     /* test src with leading byte on the end of buffer */
806     dst[0] = 'a';
807     dst[1] = 0;
808     src[0] = 'b';
809     src[1] = 0x81;
810     src[2] = 0;
811     err = p_mbscat_s(dst, sizeof(dst), src);
812     ok(err == EILSEQ, "_mbscat_s returned %d\n", err);
813     ok(!memcmp(dst, "ab", 3), "dst = %s\n", dst);
814     _setmbcp(prev_cp);
815 }
816 
817 static void test__mbsnbcpy_s(void)
818 {
819     unsigned char dest[8];
820     const unsigned char big[] = "atoolongstringforthislittledestination";
821     const unsigned char small[] = "small";
822     int ret;
823 
824     if(!p_mbsnbcpy_s)
825     {
826         win_skip("_mbsnbcpy_s not found\n");
827         return;
828     }
829 
830     memset(dest, 'X', sizeof(dest));
831     ret = p_mbsnbcpy_s(dest, sizeof(dest), small, sizeof(small));
832     ok(ret == 0, "_mbsnbcpy_s: Copying a string into a big enough destination returned %d, expected 0\n", ret);
833     ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 'l' &&
834        dest[4] == 'l' && dest[5] == '\0'&& dest[6] == 'X' && dest[7] == 'X',
835        "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
836        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
837 
838     /* WTF? */
839     memset(dest, 'X', sizeof(dest));
840     ret = p_mbsnbcpy_s(dest, sizeof(dest) - 2, big, sizeof(small));
841     ok(ret == ERANGE, "_mbsnbcpy_s: Copying a too long string returned %d, expected ERANGE\n", ret);
842     ok(dest[0] == '\0'&& dest[1] == 't' && dest[2] == 'o' && dest[3] == 'o' &&
843        dest[4] == 'l' && dest[5] == 'o' && dest[6] == 'X' && dest[7] == 'X',
844        "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
845        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
846 
847     memset(dest, 'X', sizeof(dest));
848     ret = p_mbsnbcpy_s(dest, sizeof(dest) - 2, big, 4);
849     ok(ret == 0, "_mbsnbcpy_s: Copying a too long string with a count cap returned %d, expected 0\n", ret);
850     ok(dest[0] == 'a' && dest[1] == 't' && dest[2] == 'o' && dest[3] == 'o' &&
851        dest[4] == '\0'&& dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
852        "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
853        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
854 
855     memset(dest, 'X', sizeof(dest));
856     ret = p_mbsnbcpy_s(dest, sizeof(dest) - 2, small, sizeof(small) + 10);
857     ok(ret == 0, "_mbsnbcpy_s: Copying more data than the source string len returned %d, expected 0\n", ret);
858     ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 'l' &&
859        dest[4] == 'l' && dest[5] == '\0'&& dest[6] == 'X' && dest[7] == 'X',
860        "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
861        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
862 }
863 
864 static void test__mbscpy_s(void)
865 {
866     const unsigned char src[] = "source string";
867     unsigned char dest[16];
868     int ret;
869 
870     if(!p__mbscpy_s)
871     {
872         win_skip("_mbscpy_s not found\n");
873         return;
874     }
875 
876     ret = p__mbscpy_s(NULL, 0, src);
877     ok(ret == EINVAL, "got %d\n", ret);
878     ret = p__mbscpy_s(NULL, sizeof(dest), src);
879     ok(ret == EINVAL, "got %d\n", ret);
880     ret = p__mbscpy_s(dest, 0, src);
881     ok(ret == EINVAL, "got %d\n", ret);
882     dest[0] = 'x';
883     ret = p__mbscpy_s(dest, sizeof(dest), NULL);
884     ok(ret == EINVAL, "got %d\n", ret);
885     ok(!dest[0], "dest buffer was not modified on invalid argument\n");
886 
887     memset(dest, 'X', sizeof(dest));
888     ret = p__mbscpy_s(dest, sizeof(dest), src);
889     ok(!ret, "got %d\n", ret);
890     ok(!memcmp(dest, src, sizeof(src)), "dest = %s\n", dest);
891     ok(dest[sizeof(src)] == 'X', "unused part of buffer was modified\n");
892 
893     memset(dest, 'X', sizeof(dest));
894     ret = p__mbscpy_s(dest, 4, src);
895     ok(ret == ERANGE, "got %d\n", ret);
896     ok(!dest[0], "incorrect dest buffer (%d)\n", dest[0]);
897     ok(dest[1] == src[1], "incorrect dest buffer (%d)\n", dest[1]);
898 }
899 
900 static void test_wcscpy_s(void)
901 {
902     static const WCHAR szLongText[] = { 'T','h','i','s','A','L','o','n','g','s','t','r','i','n','g',0 };
903     static WCHAR szDest[18];
904     static WCHAR szDestShort[8];
905     int ret;
906 
907     if(!p_wcscpy_s)
908     {
909         win_skip("wcscpy_s not found\n");
910         return;
911     }
912 
913     /* Test NULL Dest */
914     errno = EBADF;
915     ret = p_wcscpy_s(NULL, 18, szLongText);
916     ok(ret == EINVAL, "p_wcscpy_s expect EINVAL got %d\n", ret);
917     ok(errno == EINVAL, "expected errno EINVAL got %d\n", errno);
918 
919     /* Test NULL Source */
920     errno = EBADF;
921     szDest[0] = 'A';
922     ret = p_wcscpy_s(szDest, 18, NULL);
923     ok(ret == EINVAL, "expected EINVAL got %d\n", ret);
924     ok(errno == EINVAL, "expected errno EINVAL got %d\n", errno);
925     ok(szDest[0] == 0, "szDest[0] not 0, got %c\n", szDest[0]);
926 
927     /* Test invalid size */
928     errno = EBADF;
929     szDest[0] = 'A';
930     ret = p_wcscpy_s(szDest, 0, szLongText);
931     /* Later versions changed the return value for this case to EINVAL,
932      * and don't modify the result if the dest size is 0.
933      */
934     ok(ret == ERANGE || ret == EINVAL, "expected ERANGE/EINVAL got %d\n", ret);
935     ok(errno == ERANGE || errno == EINVAL, "expected errno ERANGE/EINVAL got %d\n", errno);
936     ok(szDest[0] == 0 || ret == EINVAL, "szDest[0] not 0\n");
937 
938     /* Copy same buffer size */
939     ret = p_wcscpy_s(szDest, 18, szLongText);
940     ok(ret == 0, "expected 0 got %d\n", ret);
941     ok(lstrcmpW(szDest, szLongText) == 0, "szDest != szLongText\n");
942 
943     /* Copy smaller buffer size */
944     errno = EBADF;
945     szDest[0] = 'A';
946     ret = p_wcscpy_s(szDestShort, 8, szLongText);
947     ok(ret == ERANGE || ret == EINVAL, "expected ERANGE/EINVAL got %d\n", ret);
948     ok(errno == ERANGE || errno == EINVAL, "expected errno ERANGE/EINVAL got %d\n", errno);
949     ok(szDestShort[0] == 0, "szDestShort[0] not 0\n");
950 
951     if(!p_wcsncpy_s)
952     {
953         win_skip("wcsncpy_s not found\n");
954         return;
955     }
956 
957     ret = p_wcsncpy_s(NULL, 18, szLongText, sizeof(szLongText)/sizeof(WCHAR));
958     ok(ret == EINVAL, "p_wcsncpy_s expect EINVAL got %d\n", ret);
959 
960     szDest[0] = 'A';
961     ret = p_wcsncpy_s(szDest, 18, NULL, 1);
962     ok(ret == EINVAL, "expected EINVAL got %d\n", ret);
963     ok(szDest[0] == 0, "szDest[0] not 0\n");
964 
965     szDest[0] = 'A';
966     ret = p_wcsncpy_s(szDest, 18, NULL, 0);
967     ok(ret == 0, "expected ERROR_SUCCESS got %d\n", ret);
968     ok(szDest[0] == 0, "szDest[0] not 0\n");
969 
970     szDest[0] = 'A';
971     ret = p_wcsncpy_s(szDest, 0, szLongText, sizeof(szLongText)/sizeof(WCHAR));
972     ok(ret == ERANGE || ret == EINVAL, "expected ERANGE/EINVAL got %d\n", ret);
973     ok(szDest[0] == 0 || ret == EINVAL, "szDest[0] not 0\n");
974 
975     ret = p_wcsncpy_s(szDest, 18, szLongText, sizeof(szLongText)/sizeof(WCHAR));
976     ok(ret == 0, "expected 0 got %d\n", ret);
977     ok(lstrcmpW(szDest, szLongText) == 0, "szDest != szLongText\n");
978 
979     szDest[0] = 'A';
980     ret = p_wcsncpy_s(szDestShort, 8, szLongText, sizeof(szLongText)/sizeof(WCHAR));
981     ok(ret == ERANGE || ret == EINVAL, "expected ERANGE/EINVAL got %d\n", ret);
982     ok(szDestShort[0] == 0, "szDestShort[0] not 0\n");
983 
984     szDest[0] = 'A';
985     ret = p_wcsncpy_s(szDest, 5, szLongText, -1);
986     ok(ret == STRUNCATE, "expected STRUNCATE got %d\n", ret);
987     ok(szDest[4] == 0, "szDest[4] not 0\n");
988     ok(!memcmp(szDest, szLongText, 4*sizeof(WCHAR)), "szDest = %s\n", wine_dbgstr_w(szDest));
989 
990     ret = p_wcsncpy_s(NULL, 0, (void*)0xdeadbeef, 0);
991     ok(ret == 0, "ret = %d\n", ret);
992 
993     szDestShort[0] = '1';
994     szDestShort[1] = 0;
995     ret = p_wcsncpy_s(szDestShort+1, 4, szDestShort, -1);
996     ok(ret == STRUNCATE, "expected ERROR_SUCCESS got %d\n", ret);
997     ok(szDestShort[0]=='1' && szDestShort[1]=='1' && szDestShort[2]=='1' && szDestShort[3]=='1',
998             "szDestShort = %s\n", wine_dbgstr_w(szDestShort));
999 }
1000 
1001 static void test__wcsupr_s(void)
1002 {
1003     static const WCHAR mixedString[] = {'M', 'i', 'X', 'e', 'D', 'l', 'o', 'w',
1004                                         'e', 'r', 'U', 'P', 'P', 'E', 'R', 0};
1005     static const WCHAR expectedString[] = {'M', 'I', 'X', 'E', 'D', 'L', 'O',
1006                                            'W', 'E', 'R', 'U', 'P', 'P', 'E',
1007                                            'R', 0};
1008     WCHAR testBuffer[2*sizeof(mixedString)/sizeof(WCHAR)];
1009     int ret;
1010 
1011     if (!p_wcsupr_s)
1012     {
1013         win_skip("_wcsupr_s not found\n");
1014         return;
1015     }
1016 
1017     /* Test NULL input string and invalid size. */
1018     errno = EBADF;
1019     ret = p_wcsupr_s(NULL, 0);
1020     ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
1021     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1022 
1023     /* Test NULL input string and valid size. */
1024     errno = EBADF;
1025     ret = p_wcsupr_s(NULL, sizeof(testBuffer)/sizeof(WCHAR));
1026     ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
1027     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1028 
1029     /* Test empty string with zero size. */
1030     errno = EBADF;
1031     testBuffer[0] = '\0';
1032     ret = p_wcsupr_s(testBuffer, 0);
1033     ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
1034     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1035     ok(testBuffer[0] == '\0', "Expected the buffer to be unchanged\n");
1036 
1037     /* Test empty string with size of one. */
1038     testBuffer[0] = '\0';
1039     ret = p_wcsupr_s(testBuffer, 1);
1040     ok(ret == 0, "Expected _wcsupr_s to succeed, got %d\n", ret);
1041     ok(testBuffer[0] == '\0', "Expected the buffer to be unchanged\n");
1042 
1043     /* Test one-byte buffer with zero size. */
1044     errno = EBADF;
1045     testBuffer[0] = 'x';
1046     ret = p_wcsupr_s(testBuffer, 0);
1047     ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
1048     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1049     ok(testBuffer[0] == '\0', "Expected the first buffer character to be null\n");
1050 
1051     /* Test one-byte buffer with size of one. */
1052     errno = EBADF;
1053     testBuffer[0] = 'x';
1054     ret = p_wcsupr_s(testBuffer, 1);
1055     ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
1056     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1057     ok(testBuffer[0] == '\0', "Expected the first buffer character to be null\n");
1058 
1059     /* Test invalid size. */
1060     wcscpy(testBuffer, mixedString);
1061     errno = EBADF;
1062     ret = p_wcsupr_s(testBuffer, 0);
1063     ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
1064     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1065     ok(testBuffer[0] == '\0', "Expected the first buffer character to be null\n");
1066 
1067     /* Test normal string uppercasing. */
1068     wcscpy(testBuffer, mixedString);
1069     ret = p_wcsupr_s(testBuffer, sizeof(mixedString)/sizeof(WCHAR));
1070     ok(ret == 0, "Expected _wcsupr_s to succeed, got %d\n", ret);
1071     ok(!wcscmp(testBuffer, expectedString), "Expected the string to be fully upper-case\n");
1072 
1073     /* Test uppercasing with a shorter buffer size count. */
1074     wcscpy(testBuffer, mixedString);
1075     errno = EBADF;
1076     ret = p_wcsupr_s(testBuffer, sizeof(mixedString)/sizeof(WCHAR) - 1);
1077     ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
1078     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1079     ok(testBuffer[0] == '\0', "Expected the first buffer character to be null\n");
1080 
1081     /* Test uppercasing with a longer buffer size count. */
1082     wcscpy(testBuffer, mixedString);
1083     ret = p_wcsupr_s(testBuffer, sizeof(testBuffer)/sizeof(WCHAR));
1084     ok(ret == 0, "Expected _wcsupr_s to succeed, got %d\n", ret);
1085     ok(!wcscmp(testBuffer, expectedString), "Expected the string to be fully upper-case\n");
1086 }
1087 
1088 static void test__wcslwr_s(void)
1089 {
1090     static const WCHAR mixedString[] = {'M', 'i', 'X', 'e', 'D', 'l', 'o', 'w',
1091                                         'e', 'r', 'U', 'P', 'P', 'E', 'R', 0};
1092     static const WCHAR expectedString[] = {'m', 'i', 'x', 'e', 'd', 'l', 'o',
1093                                            'w', 'e', 'r', 'u', 'p', 'p', 'e',
1094                                            'r', 0};
1095     WCHAR buffer[2*sizeof(mixedString)/sizeof(WCHAR)];
1096     int ret;
1097 
1098     if (!p_wcslwr_s)
1099     {
1100         win_skip("_wcslwr_s not found\n");
1101         return;
1102     }
1103 
1104     /* Test NULL input string and invalid size. */
1105     errno = EBADF;
1106     ret = p_wcslwr_s(NULL, 0);
1107     ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
1108     ok(errno == EINVAL, "expected errno EINVAL, got %d\n", errno);
1109 
1110     /* Test NULL input string and valid size. */
1111     errno = EBADF;
1112     ret = p_wcslwr_s(NULL, sizeof(buffer)/sizeof(buffer[0]));
1113     ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
1114     ok(errno == EINVAL, "expected errno EINVAL, got %d\n", errno);
1115 
1116     /* Test empty string with zero size. */
1117     errno = EBADF;
1118     buffer[0] = 'a';
1119     ret = p_wcslwr_s(buffer, 0);
1120     ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
1121     ok(errno == EINVAL, "expected errno EINVAL, got %d\n", errno);
1122     ok(buffer[0] == 0, "expected empty string\n");
1123 
1124     /* Test empty string with size of one. */
1125     buffer[0] = 0;
1126     ret = p_wcslwr_s(buffer, 1);
1127     ok(ret == 0, "got %d\n", ret);
1128     ok(buffer[0] == 0, "expected buffer to be unchanged\n");
1129 
1130     /* Test one-byte buffer with zero size. */
1131     errno = EBADF;
1132     buffer[0] = 'x';
1133     ret = p_wcslwr_s(buffer, 0);
1134     ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
1135     ok(errno == EINVAL, "expected errno to be EINVAL, got %d\n", errno);
1136     ok(buffer[0] == '\0', "expected empty string\n");
1137 
1138     /* Test one-byte buffer with size of one. */
1139     errno = EBADF;
1140     buffer[0] = 'x';
1141     ret = p_wcslwr_s(buffer, 1);
1142     ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
1143     ok(errno == EINVAL, "expected errno to be EINVAL, got %d\n", errno);
1144     ok(buffer[0] == '\0', "expected empty string\n");
1145 
1146     /* Test invalid size. */
1147     wcscpy(buffer, mixedString);
1148     errno = EBADF;
1149     ret = p_wcslwr_s(buffer, 0);
1150     ok(ret == EINVAL, "Expected EINVAL, got %d\n", ret);
1151     ok(errno == EINVAL, "expected errno to be EINVAL, got %d\n", errno);
1152     ok(buffer[0] == '\0', "expected empty string\n");
1153 
1154     /* Test normal string uppercasing. */
1155     wcscpy(buffer, mixedString);
1156     ret = p_wcslwr_s(buffer, sizeof(mixedString)/sizeof(WCHAR));
1157     ok(ret == 0, "expected 0, got %d\n", ret);
1158     ok(!wcscmp(buffer, expectedString), "expected lowercase\n");
1159 
1160     /* Test uppercasing with a shorter buffer size count. */
1161     wcscpy(buffer, mixedString);
1162     errno = EBADF;
1163     ret = p_wcslwr_s(buffer, sizeof(mixedString)/sizeof(WCHAR) - 1);
1164     ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
1165     ok(errno == EINVAL, "expected errno to be EINVAL, got %d\n", errno);
1166     ok(buffer[0] == '\0', "expected empty string\n");
1167 
1168     /* Test uppercasing with a longer buffer size count. */
1169     wcscpy(buffer, mixedString);
1170     ret = p_wcslwr_s(buffer, sizeof(buffer)/sizeof(WCHAR));
1171     ok(ret == 0, "expected 0, got %d\n", ret);
1172     ok(!wcscmp(buffer, expectedString), "expected lowercase\n");
1173 }
1174 
1175 static void test_mbcjisjms(void)
1176 {
1177     /* List of value-pairs to test. The test assumes the last pair to be {0, ..} */
1178     unsigned int jisjms[][2] = { {0x2020, 0}, {0x2021, 0}, {0x2120, 0}, {0x2121, 0x8140},
1179                                  {0x7f7f, 0}, {0x7f7e, 0}, {0x7e7f, 0}, {0x7e7e, 0xeffc},
1180                                  {0x255f, 0x837e}, {0x2560, 0x8380}, {0x2561, 0x8381},
1181                                  {0x2121FFFF, 0}, {0x2223, 0x81a1}, {0x237e, 0x829e}, {0, 0}};
1182     int cp[] = { 932, 936, 939, 950, 1361, _MB_CP_SBCS };
1183     unsigned int i, j;
1184     int prev_cp = _getmbcp();
1185 
1186     for (i = 0; i < sizeof(cp)/sizeof(cp[0]); i++)
1187     {
1188         _setmbcp(cp[i]);
1189         for (j = 0; jisjms[j][0] != 0; j++)
1190         {
1191             unsigned int ret, exp;
1192             ret = _mbcjistojms(jisjms[j][0]);
1193             exp = (cp[i] == 932) ? jisjms[j][1] : jisjms[j][0];
1194             ok(ret == exp, "Expected 0x%x, got 0x%x (0x%x, codepage=%d)\n",
1195                exp, ret, jisjms[j][0], cp[i]);
1196         }
1197     }
1198     _setmbcp(prev_cp);
1199 }
1200 
1201 static void test_mbcjmsjis(void)
1202 {
1203     /* List of value-pairs to test. The test assumes the last pair to be {0, ..} */
1204     unsigned int jmsjis[][2] = { {0x80fc, 0}, {0x813f, 0}, {0x8140, 0x2121},
1205                                  {0x817e, 0x215f}, {0x817f, 0}, {0x8180, 0x2160},
1206                                  {0x819e, 0x217e}, {0x819f, 0x2221}, {0x81fc, 0x227e},
1207                                  {0x81fd, 0}, {0x9ffc, 0x5e7e}, {0x9ffd, 0},
1208                                  {0xa040, 0}, {0xdffc, 0}, {0xe040, 0x5f21},
1209                                  {0xeffc, 0x7e7e}, {0xf040, 0}, {0x21, 0}, {0, 0}};
1210     int cp[] = { 932, 936, 939, 950, 1361, _MB_CP_SBCS };
1211     unsigned int i, j;
1212     int prev_cp = _getmbcp();
1213 
1214     for (i = 0; i < sizeof(cp)/sizeof(cp[0]); i++)
1215     {
1216         _setmbcp(cp[i]);
1217         for (j = 0; jmsjis[j][0] != 0; j++)
1218         {
1219             unsigned int ret, exp;
1220             ret = _mbcjmstojis(jmsjis[j][0]);
1221             exp = (cp[i] == 932) ? jmsjis[j][1] : jmsjis[j][0];
1222             ok(ret == exp, "Expected 0x%x, got 0x%x (0x%x, codepage=%d)\n",
1223                exp, ret, jmsjis[j][0], cp[i]);
1224         }
1225     }
1226     _setmbcp(prev_cp);
1227 }
1228 
1229 static void test_mbctohira(void)
1230 {
1231     static const unsigned int mbchira_932[][2] = {
1232         {0x8152, 0x8152}, {0x8153, 0x8153}, {0x8154, 0x8154}, {0x8155, 0x8155},
1233         {0x82a0, 0x82a0}, {0x833f, 0x833f}, {0x8340, 0x829f}, {0x837e, 0x82dd},
1234         {0x837f, 0x837f}, {0x8380, 0x82de}, {0x8393, 0x82f1}, {0x8394, 0x8394},
1235         {0x8396, 0x8396}, {0x8397, 0x8397},
1236         {0xa5, 0xa5}, {0xb0, 0xb0}, {0xdd, 0xdd} };
1237     unsigned int i;
1238     unsigned int prev_cp = _getmbcp();
1239 
1240     _setmbcp(_MB_CP_SBCS);
1241     for (i = 0; i < sizeof(mbchira_932)/sizeof(mbchira_932[0]); i++)
1242     {
1243         int ret, exp = mbchira_932[i][0];
1244         ret = _mbctohira(mbchira_932[i][0]);
1245         ok(ret == exp, "Expected 0x%x, got 0x%x\n", exp, ret);
1246     }
1247 
1248     _setmbcp(932);
1249     for (i = 0; i < sizeof(mbchira_932)/sizeof(mbchira_932[0]); i++)
1250     {
1251         unsigned int ret, exp;
1252         ret = _mbctohira(mbchira_932[i][0]);
1253         exp = mbchira_932[i][1];
1254         ok(ret == exp, "Expected 0x%x, got 0x%x\n", exp, ret);
1255     }
1256     _setmbcp(prev_cp);
1257 }
1258 
1259 static void test_mbctokata(void)
1260 {
1261     static const unsigned int mbckata_932[][2] = {
1262         {0x8152, 0x8152}, {0x8153, 0x8153}, {0x8154, 0x8154}, {0x8155, 0x8155},
1263         {0x833f, 0x833f}, {0x829f, 0x8340}, {0x82dd, 0x837e}, {0x837f, 0x837f},
1264         {0x82de, 0x8380}, {0x8394, 0x8394}, {0x8397, 0x8397},
1265         {0xa5, 0xa5}, {0xb0, 0xb0}, {0xdd, 0xdd} };
1266     unsigned int i;
1267     unsigned int prev_cp = _getmbcp();
1268 
1269     _setmbcp(_MB_CP_SBCS);
1270     for (i = 0; i < sizeof(mbckata_932)/sizeof(mbckata_932[0]); i++)
1271     {
1272         int ret, exp = mbckata_932[i][0];
1273         ret = _mbctokata(mbckata_932[i][0]);
1274         ok(ret == exp, "Expected 0x%x, got 0x%x\n", exp, ret);
1275     }
1276 
1277     _setmbcp(932);
1278     for (i = 0; i < sizeof(mbckata_932)/sizeof(mbckata_932[0]); i++)
1279     {
1280         unsigned int ret, exp;
1281         ret = _mbctokata(mbckata_932[i][0]);
1282         exp = mbckata_932[i][1];
1283         ok(ret == exp, "Expected 0x%x, got 0x%x\n", exp, ret);
1284     }
1285     _setmbcp(prev_cp);
1286 }
1287 
1288 static void test_mbbtombc(void)
1289 {
1290     static const unsigned int mbbmbc[][2] = {
1291         {0x1f, 0x1f}, {0x20, 0x8140}, {0x39, 0x8258}, {0x40, 0x8197},
1292         {0x41, 0x8260}, {0x5e, 0x814f}, {0x7e, 0x8150}, {0x7f, 0x7f},
1293         {0x80, 0x80}, {0x81, 0x81}, {0xa0, 0xa0}, {0xa7, 0x8340},
1294         {0xb0, 0x815b}, {0xd1, 0x8380}, {0xff, 0xff}, {0,0}};
1295     int cp[] = { 932, 936, 939, 950, 1361, _MB_CP_SBCS };
1296     int i, j;
1297     int prev_cp = _getmbcp();
1298 
1299     for (i = 0; i < sizeof(cp)/sizeof(cp[0]); i++)
1300     {
1301         _setmbcp(cp[i]);
1302         for (j = 0; mbbmbc[j][0] != 0; j++)
1303         {
1304             unsigned int exp, ret;
1305             ret = _mbbtombc(mbbmbc[j][0]);
1306             exp = (cp[i] == 932) ? mbbmbc[j][1] : mbbmbc[j][0];
1307             ok(ret == exp, "Expected 0x%x, got 0x%x (0x%x, codepage %d)\n",
1308                exp, ret, mbbmbc[j][0], cp[i]);
1309         }
1310     }
1311     _setmbcp(prev_cp);
1312 }
1313 
1314 static void test_mbctombb(void)
1315 {
1316     static const unsigned int mbcmbb_932[][2] = {
1317         {0x829e, 0x829e}, {0x829f, 0xa7}, {0x82f1, 0xdd}, {0x82f2, 0x82f2},
1318         {0x833f, 0x833f}, {0x8340, 0xa7}, {0x837e, 0xd0}, {0x837f, 0x837f},
1319         {0x8380, 0xd1}, {0x8396, 0xb9}, {0x8397, 0x8397}, {0x813f, 0x813f},
1320         {0x8140, 0x20}, {0x814c, 0x814c}, {0x814f, 0x5e}, {0x8197, 0x40},
1321         {0x8198, 0x8198}, {0x8258, 0x39}, {0x8259, 0x8259}, {0x825f, 0x825f},
1322         {0x8260, 0x41}, {0x82f1, 0xdd}, {0x82f2, 0x82f2}, {0,0}};
1323     unsigned int exp, ret, i;
1324     unsigned int prev_cp = _getmbcp();
1325 
1326     _setmbcp(932);
1327     for (i = 0; mbcmbb_932[i][0] != 0; i++)
1328     {
1329         ret = _mbctombb(mbcmbb_932[i][0]);
1330         exp = mbcmbb_932[i][1];
1331         ok(ret == exp, "Expected 0x%x, got 0x%x\n", exp, ret);
1332     }
1333     _setmbcp(prev_cp);
1334 }
1335 
1336 static void test_ismbckata(void) {
1337     struct katakana_pair {
1338         UINT c;
1339         BOOL exp;
1340     };
1341     static const struct katakana_pair tests[] = {
1342         {0x8152, FALSE}, {0x8153, FALSE}, {0x8154, FALSE}, {0x8155, FALSE},
1343         {0x82a0, FALSE}, {0x833f, FALSE}, {0x8340, TRUE }, {0x837e, TRUE },
1344         {0x837f, FALSE}, {0x8380, TRUE }, {0x8396, TRUE }, {0x8397, FALSE},
1345         {0xa5, FALSE}, {0xb0, FALSE}, {0xdd, FALSE}
1346     };
1347     unsigned int prev_cp = _getmbcp();
1348     int ret;
1349     unsigned int i;
1350 
1351     _setmbcp(_MB_CP_SBCS);
1352     for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
1353         ret = _ismbckata(tests[i].c);
1354         ok(!ret, "expected 0, got %d for %04x\n", ret, tests[i].c);
1355     }
1356 
1357     _setmbcp(932);
1358     for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
1359         ret = _ismbckata(tests[i].c);
1360         ok(!!ret == tests[i].exp, "expected %d, got %d for %04x\n",
1361            tests[i].exp, !!ret, tests[i].c);
1362     }
1363 
1364     _setmbcp(prev_cp);
1365 }
1366 
1367 static void test_ismbclegal(void) {
1368     unsigned int prev_cp = _getmbcp();
1369     int ret, exp, err;
1370     unsigned int i;
1371 
1372     _setmbcp(932); /* Japanese */
1373     err = 0;
1374     for(i = 0; i < 0x10000; i++) {
1375         ret = _ismbclegal(i);
1376         exp = ((HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0x9F) ||
1377                (HIBYTE(i) >= 0xE0 && HIBYTE(i) <= 0xFC)) &&
1378               ((LOBYTE(i) >= 0x40 && LOBYTE(i) <= 0x7E) ||
1379                (LOBYTE(i) >= 0x80 && LOBYTE(i) <= 0xFC));
1380         if(ret != exp) {
1381             err = 1;
1382             break;
1383         }
1384     }
1385     ok(!err, "_ismbclegal (932) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
1386     _setmbcp(936); /* Chinese (GBK) */
1387     err = 0;
1388     for(i = 0; i < 0x10000; i++) {
1389         ret = _ismbclegal(i);
1390         exp = HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0xFE &&
1391               LOBYTE(i) >= 0x40 && LOBYTE(i) <= 0xFE;
1392         if(ret != exp) {
1393             err = 1;
1394             break;
1395         }
1396     }
1397     ok(!err, "_ismbclegal (936) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
1398     _setmbcp(949); /* Korean */
1399     err = 0;
1400     for(i = 0; i < 0x10000; i++) {
1401         ret = _ismbclegal(i);
1402         exp = HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0xFE &&
1403               LOBYTE(i) >= 0x41 && LOBYTE(i) <= 0xFE;
1404         if(ret != exp) {
1405             err = 1;
1406             break;
1407         }
1408     }
1409     ok(!err, "_ismbclegal (949) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
1410     _setmbcp(950); /* Chinese (Big5) */
1411     err = 0;
1412     for(i = 0; i < 0x10000; i++) {
1413         ret = _ismbclegal(i);
1414         exp = HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0xFE &&
1415             ((LOBYTE(i) >= 0x40 && LOBYTE(i) <= 0x7E) ||
1416              (LOBYTE(i) >= 0xA1 && LOBYTE(i) <= 0xFE));
1417         if(ret != exp) {
1418             err = 1;
1419             break;
1420         }
1421     }
1422     ok(!err, "_ismbclegal (950) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
1423     _setmbcp(1361); /* Korean (Johab) */
1424     err = 0;
1425     for(i = 0; i < 0x10000; i++) {
1426         ret = _ismbclegal(i);
1427         exp = ((HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0xD3) ||
1428                (HIBYTE(i) >= 0xD8 && HIBYTE(i) <= 0xF9)) &&
1429               ((LOBYTE(i) >= 0x31 && LOBYTE(i) <= 0x7E) ||
1430                (LOBYTE(i) >= 0x81 && LOBYTE(i) <= 0xFE)) &&
1431                 HIBYTE(i) != 0xDF;
1432         if(ret != exp) {
1433             err = 1;
1434             break;
1435         }
1436     }
1437     todo_wine ok(!err, "_ismbclegal (1361) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
1438 
1439     _setmbcp(prev_cp);
1440 }
1441 
1442 static const struct {
1443     const char* string;
1444     const char* delimiter;
1445     int exp_offsetret1; /* returned offset from string after first call to strtok()
1446                            -1 means NULL  */
1447     int exp_offsetret2; /* returned offset from string after second call to strtok()
1448                            -1 means NULL  */
1449     int exp_offsetret3; /* returned offset from string after third call to strtok()
1450                            -1 means NULL  */
1451 } testcases_strtok[] = {
1452     { "red cabernet", " ", 0, 4, -1 },
1453     { "sparkling white riesling", " ", 0, 10, 16 },
1454     { " pale cream sherry", "e ", 1, 6, 9 },
1455     /* end mark */
1456     { 0}
1457 };
1458 
1459 static void test_strtok(void)
1460 {
1461     int i;
1462     char *strret;
1463     char teststr[100];
1464     for( i = 0; testcases_strtok[i].string; i++){
1465         strcpy( teststr, testcases_strtok[i].string);
1466         strret = strtok( teststr, testcases_strtok[i].delimiter);
1467         ok( (int)(strret - teststr) ==  testcases_strtok[i].exp_offsetret1 ||
1468                 (!strret && testcases_strtok[i].exp_offsetret1 == -1),
1469                 "string (%p) \'%s\' return %p\n",
1470                 teststr, testcases_strtok[i].string, strret);
1471         if( !strret) continue;
1472         strret = strtok( NULL, testcases_strtok[i].delimiter);
1473         ok( (int)(strret - teststr) ==  testcases_strtok[i].exp_offsetret2 ||
1474                 (!strret && testcases_strtok[i].exp_offsetret2 == -1),
1475                 "second call string (%p) \'%s\' return %p\n",
1476                 teststr, testcases_strtok[i].string, strret);
1477         if( !strret) continue;
1478         strret = strtok( NULL, testcases_strtok[i].delimiter);
1479         ok( (int)(strret - teststr) ==  testcases_strtok[i].exp_offsetret3 ||
1480                 (!strret && testcases_strtok[i].exp_offsetret3 == -1),
1481                 "third call string (%p) \'%s\' return %p\n",
1482                 teststr, testcases_strtok[i].string, strret);
1483     }
1484 }
1485 
1486 static void test_strtol(void)
1487 {
1488     static char neg[] = "-0x";
1489 
1490     char* e;
1491     LONG l;
1492     ULONG ul;
1493 
1494     /* errno is only set in case of error, so reset errno to EBADF to check for errno modification */
1495     /* errno is modified on W2K8+ */
1496     errno = EBADF;
1497     l = strtol("-1234", &e, 0);
1498     ok(l==-1234, "wrong value %d\n", l);
1499     ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
1500     errno = EBADF;
1501     ul = strtoul("1234", &e, 0);
1502     ok(ul==1234, "wrong value %u\n", ul);
1503     ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
1504 
1505     errno = EBADF;
1506     l = strtol("2147483647L", &e, 0);
1507     ok(l==2147483647, "wrong value %d\n", l);
1508     ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
1509     errno = EBADF;
1510     l = strtol("-2147483648L", &e, 0);
1511     ok(l==-2147483647L - 1, "wrong value %d\n", l);
1512     ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
1513     errno = EBADF;
1514     ul = strtoul("4294967295UL", &e, 0);
1515     ok(ul==4294967295ul, "wrong value %u\n", ul);
1516     ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
1517 
1518     errno = 0;
1519     l = strtol("9223372036854775807L", &e, 0);
1520     ok(l==2147483647, "wrong value %d\n", l);
1521     ok(errno == ERANGE, "wrong errno %d\n", errno);
1522     errno = 0;
1523     ul = strtoul("9223372036854775807L", &e, 0);
1524     ok(ul==4294967295ul, "wrong value %u\n", ul);
1525     ok(errno == ERANGE, "wrong errno %d\n", errno);
1526 
1527     errno = 0;
1528     ul = strtoul("-2", NULL, 0);
1529     ok(ul == -2, "wrong value %u\n", ul);
1530     ok(errno == 0, "wrong errno %d\n", errno);
1531 
1532     errno = 0;
1533     ul = strtoul("-4294967294", NULL, 0);
1534     ok(ul == 2, "wrong value %u\n", ul);
1535     ok(errno == 0, "wrong errno %d\n", errno);
1536 
1537     errno = 0;
1538     ul = strtoul("-4294967295", NULL, 0);
1539     ok(ul==1, "wrong value %u\n", ul);
1540     ok(errno == 0, "wrong errno %d\n", errno);
1541 
1542     errno = 0;
1543     ul = strtoul("-4294967296", NULL, 0);
1544     ok(ul == 1, "wrong value %u\n", ul);
1545     ok(errno == ERANGE, "wrong errno %d\n", errno);
1546 
1547     errno = 0;
1548     l = strtol(neg, &e, 0);
1549     ok(l == 0, "wrong value %d\n", l);
1550     ok(errno == 0, "wrong errno %d\n", errno);
1551     ok(e == neg, "e = %p, neg = %p\n", e, neg);
1552 }
1553 
1554 static void test_strnlen(void)
1555 {
1556     static const char str[] = "string";
1557     size_t res;
1558 
1559     if(!p_strnlen) {
1560         win_skip("strnlen not found\n");
1561         return;
1562     }
1563 
1564     res = p_strnlen(str, 20);
1565     ok(res == 6, "Returned length = %d\n", (int)res);
1566 
1567     res = p_strnlen(str, 3);
1568     ok(res == 3, "Returned length = %d\n", (int)res);
1569 
1570     res = p_strnlen(NULL, 0);
1571     ok(res == 0, "Returned length = %d\n", (int)res);
1572 }
1573 
1574 static void test__strtoi64(void)
1575 {
1576     static const char no1[] = "31923";
1577     static const char no2[] = "-213312";
1578     static const char no3[] = "12aa";
1579     static const char no4[] = "abc12";
1580     static const char overflow[] = "99999999999999999999";
1581     static const char neg_overflow[] = "-99999999999999999999";
1582     static const char hex[] = "0x123";
1583     static const char oct[] = "000123";
1584     static const char blanks[] = "        12 212.31";
1585 
1586     __int64 res;
1587     unsigned __int64 ures;
1588     char *endpos;
1589 
1590     if(!p_strtoi64 || !p_strtoui64) {
1591         win_skip("_strtoi64 or _strtoui64 not found\n");
1592         return;
1593     }
1594 
1595     errno = 0xdeadbeef;
1596     res = p_strtoi64(no1, NULL, 10);
1597     ok(res == 31923, "res != 31923\n");
1598     res = p_strtoi64(no2, NULL, 10);
1599     ok(res == -213312, "res != -213312\n");
1600     res = p_strtoi64(no3, NULL, 10);
1601     ok(res == 12, "res != 12\n");
1602     res = p_strtoi64(no4, &endpos, 10);
1603     ok(res == 0, "res != 0\n");
1604     ok(endpos == no4, "Scanning was not stopped on first character\n");
1605     res = p_strtoi64(hex, &endpos, 10);
1606     ok(res == 0, "res != 0\n");
1607     ok(endpos == hex+1, "Incorrect endpos (%p-%p)\n", hex, endpos);
1608     res = p_strtoi64(oct, &endpos, 10);
1609     ok(res == 123, "res != 123\n");
1610     ok(endpos == oct+strlen(oct), "Incorrect endpos (%p-%p)\n", oct, endpos);
1611     res = p_strtoi64(blanks, &endpos, 10);
1612     ok(res == 12, "res != 12\n");
1613     ok(endpos == blanks+10, "Incorrect endpos (%p-%p)\n", blanks, endpos);
1614     ok(errno == 0xdeadbeef, "errno = %x\n", errno);
1615 
1616     errno = 0xdeadbeef;
1617     res = p_strtoi64(overflow, &endpos, 10);
1618     ok(res == _I64_MAX, "res != _I64_MAX\n");
1619     ok(endpos == overflow+strlen(overflow), "Incorrect endpos (%p-%p)\n", overflow, endpos);
1620     ok(errno == ERANGE, "errno = %x\n", errno);
1621 
1622     errno = 0xdeadbeef;
1623     res = p_strtoi64(neg_overflow, &endpos, 10);
1624     ok(res == _I64_MIN, "res != _I64_MIN\n");
1625     ok(endpos == neg_overflow+strlen(neg_overflow), "Incorrect endpos (%p-%p)\n", neg_overflow, endpos);
1626     ok(errno == ERANGE, "errno = %x\n", errno);
1627 
1628     errno = 0xdeadbeef;
1629     res = p_strtoi64(no1, &endpos, 16);
1630     ok(res == 203043, "res != 203043\n");
1631     ok(endpos == no1+strlen(no1), "Incorrect endpos (%p-%p)\n", no1, endpos);
1632     res = p_strtoi64(no2, &endpos, 16);
1633     ok(res == -2175762, "res != -2175762\n");
1634     ok(endpos == no2+strlen(no2), "Incorrect endpos (%p-%p)\n", no2, endpos);
1635     res = p_strtoi64(no3, &endpos, 16);
1636     ok(res == 4778, "res != 4778\n");
1637     ok(endpos == no3+strlen(no3), "Incorrect endpos (%p-%p)\n", no3, endpos);
1638     res = p_strtoi64(no4, &endpos, 16);
1639     ok(res == 703506, "res != 703506\n");
1640     ok(endpos == no4+strlen(no4), "Incorrect endpos (%p-%p)\n", no4, endpos);
1641     res = p_strtoi64(hex, &endpos, 16);
1642     ok(res == 291, "res != 291\n");
1643     ok(endpos == hex+strlen(hex), "Incorrect endpos (%p-%p)\n", hex, endpos);
1644     res = p_strtoi64(oct, &endpos, 16);
1645     ok(res == 291, "res != 291\n");
1646     ok(endpos == oct+strlen(oct), "Incorrect endpos (%p-%p)\n", oct, endpos);
1647     res = p_strtoi64(blanks, &endpos, 16);
1648     ok(res == 18, "res != 18\n");
1649     ok(endpos == blanks+10, "Incorrect endpos (%p-%p)\n", blanks, endpos);
1650     ok(errno == 0xdeadbeef, "errno = %x\n", errno);
1651 
1652     errno = 0xdeadbeef;
1653     res = p_strtoi64(hex, &endpos, 36);
1654     ok(res == 1541019, "res != 1541019\n");
1655     ok(endpos == hex+strlen(hex), "Incorrect endpos (%p-%p)\n", hex, endpos);
1656     ok(errno == 0xdeadbeef, "errno = %x\n", errno);
1657 
1658     errno = 0xdeadbeef;
1659     res = p_strtoi64(no1, &endpos, 0);
1660     ok(res == 31923, "res != 31923\n");
1661     ok(endpos == no1+strlen(no1), "Incorrect endpos (%p-%p)\n", no1, endpos);
1662     res = p_strtoi64(no2, &endpos, 0);
1663     ok(res == -213312, "res != -213312\n");
1664     ok(endpos == no2+strlen(no2), "Incorrect endpos (%p-%p)\n", no2, endpos);
1665     res = p_strtoi64(no3, &endpos, 10);
1666     ok(res == 12, "res != 12\n");
1667     ok(endpos == no3+2, "Incorrect endpos (%p-%p)\n", no3, endpos);
1668     res = p_strtoi64(no4, &endpos, 10);
1669     ok(res == 0, "res != 0\n");
1670     ok(endpos == no4, "Incorrect endpos (%p-%p)\n", no4, endpos);
1671     res = p_strtoi64(hex, &endpos, 10);
1672     ok(res == 0, "res != 0\n");
1673     ok(endpos == hex+1, "Incorrect endpos (%p-%p)\n", hex, endpos);
1674     res = p_strtoi64(oct, &endpos, 10);
1675     ok(res == 123, "res != 123\n");
1676     ok(endpos == oct+strlen(oct), "Incorrect endpos (%p-%p)\n", oct, endpos);
1677     res = p_strtoi64(blanks, &endpos, 10);
1678     ok(res == 12, "res != 12\n");
1679     ok(endpos == blanks+10, "Incorrect endpos (%p-%p)\n", blanks, endpos);
1680     ok(errno == 0xdeadbeef, "errno = %x\n", errno);
1681 
1682     errno = 0xdeadbeef;
1683     ures = p_strtoui64(no1, &endpos, 0);
1684     ok(ures == 31923, "ures != 31923\n");
1685     ok(endpos == no1+strlen(no1), "Incorrect endpos (%p-%p)\n", no1, endpos);
1686     ures = p_strtoui64(no2, &endpos, 0);
1687     ok(ures == -213312, "ures != -213312\n");
1688     ok(endpos == no2+strlen(no2), "Incorrect endpos (%p-%p)\n", no2, endpos);
1689     ures = p_strtoui64(no3, &endpos, 10);
1690     ok(ures == 12, "ures != 12\n");
1691     ok(endpos == no3+2, "Incorrect endpos (%p-%p)\n", no3, endpos);
1692     ures = p_strtoui64(no4, &endpos, 10);
1693     ok(ures == 0, "ures != 0\n");
1694     ok(endpos == no4, "Incorrect endpos (%p-%p)\n", no4, endpos);
1695     ures = p_strtoui64(hex, &endpos, 10);
1696     ok(ures == 0, "ures != 0\n");
1697     ok(endpos == hex+1, "Incorrect endpos (%p-%p)\n", hex, endpos);
1698     ures = p_strtoui64(oct, &endpos, 10);
1699     ok(ures == 123, "ures != 123\n");
1700     ok(endpos == oct+strlen(oct), "Incorrect endpos (%p-%p)\n", oct, endpos);
1701     ures = p_strtoui64(blanks, &endpos, 10);
1702     ok(ures == 12, "ures != 12\n");
1703     ok(endpos == blanks+10, "Incorrect endpos (%p-%p)\n", blanks, endpos);
1704     ok(errno == 0xdeadbeef, "errno = %x\n", errno);
1705 
1706     errno = 0xdeadbeef;
1707     ures = p_strtoui64(overflow, &endpos, 10);
1708     ok(ures == _UI64_MAX, "ures != _UI64_MAX\n");
1709     ok(endpos == overflow+strlen(overflow), "Incorrect endpos (%p-%p)\n", overflow, endpos);
1710     ok(errno == ERANGE, "errno = %x\n", errno);
1711 
1712     errno = 0xdeadbeef;
1713     ures = p_strtoui64(neg_overflow, &endpos, 10);
1714     ok(ures == 1, "ures != 1\n");
1715     ok(endpos == neg_overflow+strlen(neg_overflow), "Incorrect endpos (%p-%p)\n", neg_overflow, endpos);
1716     ok(errno == ERANGE, "errno = %x\n", errno);
1717 }
1718 
1719 static inline BOOL almost_equal(double d1, double d2) {
1720     if(d1-d2>-1e-30 && d1-d2<1e-30)
1721         return TRUE;
1722     return FALSE;
1723 }
1724 
1725 static void test__strtod(void)
1726 {
1727     const char double1[] = "12.1";
1728     const char double2[] = "-13.721";
1729     const char double3[] = "INF";
1730     const char double4[] = ".21e12";
1731     const char double5[] = "214353e-3";
1732     const char double6[] = "NAN";
1733     const char overflow[] = "1d9999999999999999999";
1734     const char white_chars[] = "  d10";
1735 
1736     char *end;
1737     double d;
1738 
1739     d = strtod(double1, &end);
1740     ok(almost_equal(d, 12.1), "d = %lf\n", d);
1741     ok(end == double1+4, "incorrect end (%d)\n", (int)(end-double1));
1742 
1743     d = strtod(double2, &end);
1744     ok(almost_equal(d, -13.721), "d = %lf\n", d);
1745     ok(end == double2+7, "incorrect end (%d)\n", (int)(end-double2));
1746 
1747     d = strtod(double3, &end);
1748     ok(almost_equal(d, 0), "d = %lf\n", d);
1749     ok(end == double3, "incorrect end (%d)\n", (int)(end-double3));
1750 
1751     d = strtod(double4, &end);
1752     ok(almost_equal(d, 210000000000.0), "d = %lf\n", d);
1753     ok(end == double4+6, "incorrect end (%d)\n", (int)(end-double4));
1754 
1755     d = strtod(double5, &end);
1756     ok(almost_equal(d, 214.353), "d = %lf\n", d);
1757     ok(end == double5+9, "incorrect end (%d)\n", (int)(end-double5));
1758 
1759     d = strtod(double6, &end);
1760     ok(almost_equal(d, 0), "d = %lf\n", d);
1761     ok(end == double6, "incorrect end (%d)\n", (int)(end-double6));
1762 
1763     d = strtod("12.1d2", NULL);
1764     ok(almost_equal(d, 12.1e2), "d = %lf\n", d);
1765 
1766     d = strtod(white_chars, &end);
1767     ok(almost_equal(d, 0), "d = %lf\n", d);
1768     ok(end == white_chars, "incorrect end (%d)\n", (int)(end-white_chars));
1769 
1770     if (!p__strtod_l)
1771         win_skip("_strtod_l not found\n");
1772     else
1773     {
1774         errno = EBADF;
1775         d = strtod(NULL, NULL);
1776         ok(almost_equal(d, 0.0), "d = %lf\n", d);
1777         ok(errno == EINVAL, "errno = %x\n", errno);
1778 
1779         errno = EBADF;
1780         end = (char *)0xdeadbeef;
1781         d = strtod(NULL, &end);
1782         ok(almost_equal(d, 0.0), "d = %lf\n", d);
1783         ok(errno == EINVAL, "errno = %x\n", errno);
1784         ok(!end, "incorrect end ptr %p\n", end);
1785 
1786         errno = EBADF;
1787         d = p__strtod_l(NULL, NULL, NULL);
1788         ok(almost_equal(d, 0.0), "d = %lf\n", d);
1789         ok(errno == EINVAL, "errno = %x\n", errno);
1790     }
1791 
1792     /* Set locale with non '.' decimal point (',') */
1793     if(!setlocale(LC_ALL, "Polish")) {
1794         win_skip("system with limited locales\n");
1795         return;
1796     }
1797 
1798     d = strtod("12.1", NULL);
1799     ok(almost_equal(d, 12.0), "d = %lf\n", d);
1800 
1801     d = strtod("12,1", NULL);
1802     ok(almost_equal(d, 12.1), "d = %lf\n", d);
1803 
1804     setlocale(LC_ALL, "C");
1805 
1806     /* Precision tests */
1807     d = strtod("0.1", NULL);
1808     ok(almost_equal(d, 0.1), "d = %lf\n", d);
1809     d = strtod("-0.1", NULL);
1810     ok(almost_equal(d, -0.1), "d = %lf\n", d);
1811     d = strtod("0.1281832188491894198128921", NULL);
1812     ok(almost_equal(d, 0.1281832188491894198128921), "d = %lf\n", d);
1813     d = strtod("0.82181281288121", NULL);
1814     ok(almost_equal(d, 0.82181281288121), "d = %lf\n", d);
1815     d = strtod("21921922352523587651128218821", NULL);
1816     ok(almost_equal(d, 21921922352523587651128218821.0), "d = %lf\n", d);
1817     d = strtod("0.1d238", NULL);
1818     ok(almost_equal(d, 0.1e238L), "d = %lf\n", d);
1819     d = strtod("0.1D-4736", NULL);
1820     ok(almost_equal(d, 0.1e-4736L), "d = %lf\n", d);
1821 
1822     errno = 0xdeadbeef;
1823     strtod(overflow, &end);
1824     ok(errno == ERANGE, "errno = %x\n", errno);
1825     ok(end == overflow+21, "incorrect end (%d)\n", (int)(end-overflow));
1826 
1827     errno = 0xdeadbeef;
1828     strtod("-1d309", NULL);
1829     ok(errno == ERANGE, "errno = %x\n", errno);
1830 }
1831 
1832 static void test_mbstowcs(void)
1833 {
1834     static const wchar_t wSimple[] = { 't','e','x','t',0 };
1835     static const wchar_t wHiragana[] = { 0x3042,0x3043,0 };
1836     static const wchar_t wEmpty[] = { 0 };
1837     static const char mSimple[] = "text";
1838     static const char mHiragana[] = { 0x82,0xa0,0x82,0xa1,0 };
1839     static const char mEmpty[] = { 0 };
1840 
1841     const wchar_t *pwstr;
1842     wchar_t wOut[6];
1843     char mOut[6];
1844     size_t ret;
1845     int err;
1846     const char *pmbstr;
1847     mbstate_t state;
1848 
1849     wOut[4] = '!'; wOut[5] = '\0';
1850     mOut[4] = '!'; mOut[5] = '\0';
1851 
1852     if(pmbstowcs_s) {
1853         /* crashes on some systems */
1854         errno = 0xdeadbeef;
1855         ret = mbstowcs(wOut, NULL, 4);
1856         ok(ret == -1, "mbstowcs did not return -1\n");
1857         ok(errno == EINVAL, "errno = %d\n", errno);
1858     }
1859 
1860     ret = mbstowcs(NULL, mSimple, 0);
1861     ok(ret == 4, "mbstowcs did not return 4\n");
1862 
1863     ret = mbstowcs(wOut, mSimple, 4);
1864     ok(ret == 4, "mbstowcs did not return 4\n");
1865     ok(!memcmp(wOut, wSimple, 4*sizeof(wchar_t)), "wOut = %s\n", wine_dbgstr_w(wOut));
1866     ok(wOut[4] == '!', "wOut[4] != \'!\'\n");
1867 
1868     ret = mbstowcs(NULL, mEmpty, 1);
1869     ok(ret == 0, "mbstowcs did not return 0, got %d\n", (int)ret);
1870 
1871     ret = mbstowcs(wOut, mEmpty, 1);
1872     ok(ret == 0, "mbstowcs did not return 0, got %d\n", (int)ret);
1873     ok(!memcmp(wOut, wEmpty, sizeof(wEmpty)), "wOut = %s\n", wine_dbgstr_w(wOut));
1874 
1875     ret = wcstombs(NULL, wSimple, 0);
1876     ok(ret == 4, "wcstombs did not return 4\n");
1877 
1878     ret = wcstombs(mOut, wSimple, 6);
1879     ok(ret == 4, "wcstombs did not return 4\n");
1880     ok(!memcmp(mOut, mSimple, 5*sizeof(char)), "mOut = %s\n", mOut);
1881 
1882     ret = wcstombs(mOut, wSimple, 2);
1883     ok(ret == 2, "wcstombs did not return 2\n");
1884     ok(!memcmp(mOut, mSimple, 5*sizeof(char)), "mOut = %s\n", mOut);
1885 
1886     ret = wcstombs(NULL, wEmpty, 1);
1887     ok(ret == 0, "wcstombs did not return 0, got %d\n", (int)ret);
1888 
1889     ret = wcstombs(mOut, wEmpty, 1);
1890     ok(ret == 0, "wcstombs did not return 0, got %d\n", (int)ret);
1891     ok(!memcmp(mOut, mEmpty, sizeof(mEmpty)), "mOut = %s\n", mOut);
1892 
1893     if(!setlocale(LC_ALL, "Japanese_Japan.932")) {
1894         win_skip("Japanese_Japan.932 locale not available\n");
1895         return;
1896     }
1897 
1898     ret = mbstowcs(wOut, mHiragana, 6);
1899     ok(ret == 2, "mbstowcs did not return 2\n");
1900     ok(!memcmp(wOut, wHiragana, sizeof(wHiragana)), "wOut = %s\n", wine_dbgstr_w(wOut));
1901 
1902     ret = mbstowcs(wOut, mEmpty, 6);
1903     ok(ret == 0, "mbstowcs did not return 0, got %d\n", (int)ret);
1904     ok(!memcmp(wOut, wEmpty, sizeof(wEmpty)), "wOut = %s\n", wine_dbgstr_w(wOut));
1905 
1906     ret = wcstombs(mOut, wHiragana, 6);
1907     ok(ret == 4, "wcstombs did not return 4\n");
1908     ok(!memcmp(mOut, mHiragana, sizeof(mHiragana)), "mOut = %s\n", mOut);
1909 
1910     ret = wcstombs(mOut, wEmpty, 6);
1911     ok(ret == 0, "wcstombs did not return 0, got %d\n", (int)ret);
1912     ok(!memcmp(mOut, mEmpty, sizeof(mEmpty)), "mOut = %s\n", mOut);
1913 
1914     if(!pmbstowcs_s || !pwcstombs_s) {
1915         setlocale(LC_ALL, "C");
1916         win_skip("mbstowcs_s or wcstombs_s not available\n");
1917         return;
1918     }
1919 
1920     err = pmbstowcs_s(&ret, wOut, 6, mSimple, _TRUNCATE);
1921     ok(err == 0, "err = %d\n", err);
1922     ok(ret == 5, "mbstowcs_s did not return 5\n");
1923     ok(!memcmp(wOut, wSimple, sizeof(wSimple)), "wOut = %s\n", wine_dbgstr_w(wOut));
1924 
1925     err = pmbstowcs_s(&ret, wOut, 6, mHiragana, _TRUNCATE);
1926     ok(err == 0, "err = %d\n", err);
1927     ok(ret == 3, "mbstowcs_s did not return 3\n");
1928     ok(!memcmp(wOut, wHiragana, sizeof(wHiragana)), "wOut = %s\n", wine_dbgstr_w(wOut));
1929 
1930     err = pmbstowcs_s(&ret, wOut, 6, mEmpty, _TRUNCATE);
1931     ok(err == 0, "err = %d\n", err);
1932     ok(ret == 1, "mbstowcs_s did not return 1, got %d\n", (int)ret);
1933     ok(!memcmp(wOut, wEmpty, sizeof(wEmpty)), "wOut = %s\n", wine_dbgstr_w(wOut));
1934 
1935     err = pmbstowcs_s(&ret, NULL, 0, mHiragana, 1);
1936     ok(err == 0, "err = %d\n", err);
1937     ok(ret == 3, "mbstowcs_s did not return 3\n");
1938 
1939     err = pwcstombs_s(&ret, mOut, 6, wSimple, _TRUNCATE);
1940     ok(err == 0, "err = %d\n", err);
1941     ok(ret == 5, "wcstombs_s did not return 5\n");
1942     ok(!memcmp(mOut, mSimple, sizeof(mSimple)), "mOut = %s\n", mOut);
1943 
1944     err = pwcstombs_s(&ret, mOut, 6, wHiragana, _TRUNCATE);
1945     ok(err == 0, "err = %d\n", err);
1946     ok(ret == 5, "wcstombs_s did not return 5\n");
1947     ok(!memcmp(mOut, mHiragana, sizeof(mHiragana)), "mOut = %s\n", mOut);
1948 
1949     err = pwcstombs_s(&ret, mOut, 6, wEmpty, _TRUNCATE);
1950     ok(err == 0, "err = %d\n", err);
1951     ok(ret == 1, "wcstombs_s did not return 1, got %d\n", (int)ret);
1952     ok(!memcmp(mOut, mEmpty, sizeof(mEmpty)), "mOut = %s\n", mOut);
1953 
1954     err = pwcstombs_s(&ret, NULL, 0, wHiragana, 1);
1955     ok(err == 0, "err = %d\n", err);
1956     ok(ret == 5, "wcstombs_s did not return 5\n");
1957 
1958     if(!pwcsrtombs) {
1959         setlocale(LC_ALL, "C");
1960         win_skip("wcsrtombs not available\n");
1961         return;
1962     }
1963 
1964     pwstr = wSimple;
1965     err = -3;
1966     ret = pwcsrtombs(mOut, &pwstr, 4, &err);
1967     ok(ret == 4, "wcsrtombs did not return 4\n");
1968     ok(err == 0, "err = %d\n", err);
1969     ok(pwstr == wSimple+4, "pwstr = %p (wszSimple = %p)\n", pwstr, wSimple);
1970     ok(!memcmp(mOut, mSimple, ret), "mOut = %s\n", mOut);
1971 
1972     pwstr = wSimple;
1973     ret = pwcsrtombs(mOut, &pwstr, 5, NULL);
1974     ok(ret == 4, "wcsrtombs did not return 4\n");
1975     ok(pwstr == NULL, "pwstr != NULL\n");
1976     ok(!memcmp(mOut, mSimple, sizeof(mSimple)), "mOut = %s\n", mOut);
1977 
1978     if(!p_mbsrtowcs) {
1979         setlocale(LC_ALL, "C");
1980         win_skip("mbsrtowcs not available\n");
1981         return;
1982     }
1983 
1984     pmbstr = mHiragana;
1985     ret = p_mbsrtowcs(NULL, &pmbstr, 6, NULL);
1986     ok(ret == 2, "mbsrtowcs did not return 2\n");
1987     ok(pmbstr == mHiragana, "pmbstr = %p, expected %p\n", pmbstr, mHiragana);
1988 
1989     pmbstr = mHiragana;
1990     ret = p_mbsrtowcs(wOut, &pmbstr, 6, NULL);
1991     ok(ret == 2, "mbsrtowcs did not return 2\n");
1992     ok(!memcmp(wOut, wHiragana, sizeof(wHiragana)), "wOut = %s\n", wine_dbgstr_w(wOut));
1993     ok(!pmbstr, "pmbstr != NULL\n");
1994 
1995     state = mHiragana[0];
1996     pmbstr = mHiragana+1;
1997     ret = p_mbsrtowcs(wOut, &pmbstr, 6, &state);
1998     ok(ret == 2, "mbsrtowcs did not return 2\n");
1999     ok(wOut[0] == 0x3042, "wOut[0] = %x\n", wOut[0]);
2000     ok(wOut[1] == 0xff61, "wOut[1] = %x\n", wOut[1]);
2001     ok(wOut[2] == 0, "wOut[2] = %x\n", wOut[2]);
2002     ok(!pmbstr, "pmbstr != NULL\n");
2003 
2004     errno = EBADF;
2005     ret = p_mbsrtowcs(wOut, NULL, 6, &state);
2006     ok(ret == -1, "mbsrtowcs did not return -1\n");
2007     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2008 
2009     if(!p_mbsrtowcs_s) {
2010         setlocale(LC_ALL, "C");
2011         win_skip("mbsrtowcs_s not available\n");
2012         return;
2013     }
2014 
2015     pmbstr = mHiragana;
2016     err = p_mbsrtowcs_s(&ret, NULL, 0, NULL, 6, NULL);
2017     ok(ret == -1, "mbsrtowcs_s did not return -1\n");
2018     ok(err == EINVAL, "err = %d\n", err);
2019     err = p_mbsrtowcs_s(&ret, NULL, 1, &pmbstr, 6, NULL);
2020     ok(ret == -1, "mbsrtowcs_s did not return -1\n");
2021     ok(err == EINVAL, "err = %d\n", err);
2022     err = p_mbsrtowcs_s(&ret, wOut, 0, &pmbstr, 6, NULL);
2023     ok(ret == -1, "mbsrtowcs_s did not return -1\n");
2024     ok(err == EINVAL, "err = %d\n", err);
2025 
2026     pmbstr = mHiragana;
2027     errno = 0;
2028     err = p_mbsrtowcs_s(&ret, NULL, 0, &pmbstr, 6, NULL);
2029     ok(ret == 3, "mbsrtowcs_s did not return 3\n");
2030     ok(err == 0, "err = %d\n", err);
2031     ok(pmbstr == mHiragana, "pmbstr = %p, expected %p\n", pmbstr, mHiragana);
2032     ok(errno == 0, "errno = %d\n", errno);
2033 
2034     pmbstr = mHiragana;
2035     err = p_mbsrtowcs_s(&ret, wOut, 1, &pmbstr, 6, NULL);
2036     ok(ret == 2, "mbsrtowcs_s did not return 2\n");
2037     ok(err == 0, "err = %d\n", err);
2038     ok(!wOut[0], "wOut[0] = '%c'\n", wOut[0]);
2039     ok(pmbstr == mHiragana+2, "pmbstr = %p, expected %p\n", pmbstr, mHiragana+2);
2040     ok(errno == 0, "errno = %d\n", errno);
2041 
2042     pmbstr = mHiragana;
2043     err = p_mbsrtowcs_s(&ret, wOut, 2, &pmbstr, 6, NULL);
2044     ok(ret == 3, "mbsrtowcs_s did not return 3\n");
2045     ok(err == 0, "err = %d\n", err);
2046     ok(!wOut[0], "wOut[0] = '%c'\n", wOut[0]);
2047     ok(pmbstr == mHiragana+4, "pmbstr = %p, expected %p\n", pmbstr, mHiragana+4);
2048     ok(errno == 0, "errno = %d\n", errno);
2049 
2050     pmbstr = mHiragana;
2051     err = p_mbsrtowcs_s(&ret, wOut, 3, &pmbstr, 6, NULL);
2052     ok(ret == 3, "mbsrtowcs_s did not return 3\n");
2053     ok(err == 0, "err = %d\n", err);
2054     ok(!pmbstr, "pmbstr != NULL\n");
2055     ok(errno == 0, "errno = %d\n", errno);
2056 
2057     setlocale(LC_ALL, "C");
2058 }
2059 
2060 static void test_gcvt(void)
2061 {
2062     char buf[1024], *res;
2063     errno_t err;
2064 
2065     if(!p_gcvt_s) {
2066         win_skip("Skipping _gcvt tests\n");
2067         return;
2068     }
2069 
2070     errno = 0;
2071     res = _gcvt(1.2, -1, buf);
2072     ok(res == NULL, "res != NULL\n");
2073     ok(errno == ERANGE, "errno = %d\n", errno);
2074 
2075     errno = 0;
2076     res = _gcvt(1.2, 5, NULL);
2077     ok(res == NULL, "res != NULL\n");
2078     ok(errno == EINVAL, "errno = %d\n", errno);
2079 
2080     res = gcvt(1.2, 5, buf);
2081     ok(res == buf, "res != buf\n");
2082     ok(!strcmp(buf, "1.2"), "buf = %s\n", buf);
2083 
2084     buf[0] = 'x';
2085     err = p_gcvt_s(buf, 5, 1.2, 10);
2086     ok(err == ERANGE, "err = %d\n", err);
2087     ok(buf[0] == '\0', "buf[0] = %c\n", buf[0]);
2088 
2089     buf[0] = 'x';
2090     err = p_gcvt_s(buf, 4, 123456, 2);
2091     ok(err == ERANGE, "err = %d\n", err);
2092     ok(buf[0] == '\0', "buf[0] = %c\n", buf[0]);
2093 }
2094 
2095 static void test__itoa_s(void)
2096 {
2097     errno_t ret;
2098     char buffer[33];
2099 
2100     if (!p_itoa_s)
2101     {
2102         win_skip("Skipping _itoa_s tests\n");
2103         return;
2104     }
2105 
2106     errno = EBADF;
2107     ret = p_itoa_s(0, NULL, 0, 0);
2108     ok(ret == EINVAL, "Expected _itoa_s to return EINVAL, got %d\n", ret);
2109     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2110 
2111     memset(buffer, 'X', sizeof(buffer));
2112     errno = EBADF;
2113     ret = p_itoa_s(0, buffer, 0, 0);
2114     ok(ret == EINVAL, "Expected _itoa_s to return EINVAL, got %d\n", ret);
2115     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2116     ok(buffer[0] == 'X', "Expected the output buffer to be untouched\n");
2117 
2118     memset(buffer, 'X', sizeof(buffer));
2119     errno = EBADF;
2120     ret = p_itoa_s(0, buffer, sizeof(buffer), 0);
2121     ok(ret == EINVAL, "Expected _itoa_s to return EINVAL, got %d\n", ret);
2122     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2123     ok(buffer[0] == '\0', "Expected the output buffer to be null terminated\n");
2124 
2125     memset(buffer, 'X', sizeof(buffer));
2126     errno = EBADF;
2127     ret = p_itoa_s(0, buffer, sizeof(buffer), 64);
2128     ok(ret == EINVAL, "Expected _itoa_s to return EINVAL, got %d\n", ret);
2129     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2130     ok(buffer[0] == '\0', "Expected the output buffer to be null terminated\n");
2131 
2132     memset(buffer, 'X', sizeof(buffer));
2133     errno = EBADF;
2134     ret = p_itoa_s(12345678, buffer, 4, 10);
2135     ok(ret == ERANGE, "Expected _itoa_s to return ERANGE, got %d\n", ret);
2136     ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
2137     ok(!memcmp(buffer, "\000765", 4),
2138        "Expected the output buffer to be null terminated with truncated output\n");
2139 
2140     memset(buffer, 'X', sizeof(buffer));
2141     errno = EBADF;
2142     ret = p_itoa_s(12345678, buffer, 8, 10);
2143     ok(ret == ERANGE, "Expected _itoa_s to return ERANGE, got %d\n", ret);
2144     ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
2145     ok(!memcmp(buffer, "\0007654321", 8),
2146        "Expected the output buffer to be null terminated with truncated output\n");
2147 
2148     memset(buffer, 'X', sizeof(buffer));
2149     errno = EBADF;
2150     ret = p_itoa_s(-12345678, buffer, 9, 10);
2151     ok(ret == ERANGE, "Expected _itoa_s to return ERANGE, got %d\n", ret);
2152     ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
2153     ok(!memcmp(buffer, "\00087654321", 9),
2154        "Expected the output buffer to be null terminated with truncated output\n");
2155 
2156     ret = p_itoa_s(12345678, buffer, 9, 10);
2157     ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
2158     ok(!strcmp(buffer, "12345678"),
2159        "Expected output buffer string to be \"12345678\", got \"%s\"\n",
2160        buffer);
2161 
2162     ret = p_itoa_s(43690, buffer, sizeof(buffer), 2);
2163     ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
2164     ok(!strcmp(buffer, "1010101010101010"),
2165        "Expected output buffer string to be \"1010101010101010\", got \"%s\"\n",
2166        buffer);
2167 
2168     ret = p_itoa_s(1092009, buffer, sizeof(buffer), 36);
2169     ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
2170     ok(!strcmp(buffer, "nell"),
2171        "Expected output buffer string to be \"nell\", got \"%s\"\n",
2172        buffer);
2173 
2174     ret = p_itoa_s(5704, buffer, sizeof(buffer), 18);
2175     ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
2176     ok(!strcmp(buffer, "hag"),
2177        "Expected output buffer string to be \"hag\", got \"%s\"\n",
2178        buffer);
2179 
2180     ret = p_itoa_s(-12345678, buffer, sizeof(buffer), 10);
2181     ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
2182     ok(!strcmp(buffer, "-12345678"),
2183        "Expected output buffer string to be \"-12345678\", got \"%s\"\n",
2184        buffer);
2185 
2186     itoa(100, buffer, 100);
2187     ok(!strcmp(buffer, "10"),
2188             "Expected output buffer string to be \"10\", got \"%s\"\n", buffer);
2189 }
2190 
2191 static void test__strlwr_s(void)
2192 {
2193     errno_t ret;
2194     char buffer[20];
2195 
2196     if (!p_strlwr_s)
2197     {
2198         win_skip("Skipping _strlwr_s tests\n");
2199         return;
2200     }
2201 
2202     errno = EBADF;
2203     ret = p_strlwr_s(NULL, 0);
2204     ok(ret == EINVAL, "Expected _strlwr_s to return EINVAL, got %d\n", ret);
2205     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2206 
2207     errno = EBADF;
2208     ret = p_strlwr_s(NULL, sizeof(buffer));
2209     ok(ret == EINVAL, "Expected _strlwr_s to return EINVAL, got %d\n", ret);
2210     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2211 
2212     errno = EBADF;
2213     ret = p_strlwr_s(buffer, 0);
2214     ok(ret == EINVAL, "Expected _strlwr_s to return EINVAL, got %d\n", ret);
2215     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2216 
2217     strcpy(buffer, "GoRrIsTeR");
2218     errno = EBADF;
2219     ret = p_strlwr_s(buffer, 5);
2220     ok(ret == EINVAL, "Expected _strlwr_s to return EINVAL, got %d\n", ret);
2221     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2222     ok(!memcmp(buffer, "\0oRrIsTeR", sizeof("\0oRrIsTeR")),
2223        "Expected the output buffer to be \"\\0oRrIsTeR\"\n");
2224 
2225     strcpy(buffer, "GoRrIsTeR");
2226     errno = EBADF;
2227     ret = p_strlwr_s(buffer, sizeof("GoRrIsTeR") - 1);
2228     ok(ret == EINVAL, "Expected _strlwr_s to return EINVAL, got %d\n", ret);
2229     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2230     ok(!memcmp(buffer, "\0oRrIsTeR", sizeof("\0oRrIsTeR")),
2231        "Expected the output buffer to be \"\\0oRrIsTeR\"\n");
2232 
2233     strcpy(buffer, "GoRrIsTeR");
2234     ret = p_strlwr_s(buffer, sizeof("GoRrIsTeR"));
2235     ok(ret == 0, "Expected _strlwr_s to return 0, got %d\n", ret);
2236     ok(!strcmp(buffer, "gorrister"),
2237        "Expected the output buffer to be \"gorrister\", got \"%s\"\n",
2238        buffer);
2239 
2240     memcpy(buffer, "GoRrIsTeR\0ELLEN", sizeof("GoRrIsTeR\0ELLEN"));
2241     ret = p_strlwr_s(buffer, sizeof(buffer));
2242     ok(ret == 0, "Expected _strlwr_s to return 0, got %d\n", ret);
2243     ok(!memcmp(buffer, "gorrister\0ELLEN", sizeof("gorrister\0ELLEN")),
2244        "Expected the output buffer to be \"gorrister\\0ELLEN\", got \"%s\"\n",
2245        buffer);
2246 }
2247 
2248 static void test_wcsncat_s(void)
2249 {
2250     static wchar_t abcW[] = {'a','b','c',0};
2251     int ret;
2252     wchar_t dst[4];
2253     wchar_t src[4];
2254 
2255     if (!p_wcsncat_s)
2256     {
2257         win_skip("skipping wcsncat_s tests\n");
2258         return;
2259     }
2260 
2261     memcpy(src, abcW, sizeof(abcW));
2262     dst[0] = 0;
2263     ret = p_wcsncat_s(NULL, 4, src, 4);
2264     ok(ret == EINVAL, "err = %d\n", ret);
2265     ret = p_wcsncat_s(dst, 0, src, 4);
2266     ok(ret == EINVAL, "err = %d\n", ret);
2267     ret = p_wcsncat_s(dst, 0, src, _TRUNCATE);
2268     ok(ret == EINVAL, "err = %d\n", ret);
2269     ret = p_wcsncat_s(dst, 4, NULL, 0);
2270     ok(ret == 0, "err = %d\n", ret);
2271 
2272     dst[0] = 0;
2273     ret = p_wcsncat_s(dst, 2, src, 4);
2274     ok(ret == ERANGE, "err = %d\n", ret);
2275 
2276     dst[0] = 0;
2277     ret = p_wcsncat_s(dst, 2, src, _TRUNCATE);
2278     ok(ret == STRUNCATE, "err = %d\n", ret);
2279     ok(dst[0] == 'a' && dst[1] == 0, "dst is %s\n", wine_dbgstr_w(dst));
2280 
2281     memcpy(dst, abcW, sizeof(abcW));
2282     dst[3] = 'd';
2283     ret = p_wcsncat_s(dst, 4, src, 4);
2284     ok(ret == EINVAL, "err = %d\n", ret);
2285 }
2286 
2287 static void test__mbsnbcat_s(void)
2288 {
2289     unsigned char dest[16];
2290     const unsigned char first[] = "dinosaur";
2291     const unsigned char second[] = "duck";
2292     int ret;
2293 
2294     if (!p_mbsnbcat_s)
2295     {
2296         win_skip("Skipping _mbsnbcat_s tests\n");
2297         return;
2298     }
2299 
2300     /* Test invalid arguments. */
2301     ret = p_mbsnbcat_s(NULL, 0, NULL, 0);
2302     ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
2303 
2304     errno = EBADF;
2305     ret = p_mbsnbcat_s(NULL, 10, NULL, 0);
2306     ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
2307     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2308 
2309     errno = EBADF;
2310     ret = p_mbsnbcat_s(NULL, 0, NULL, 10);
2311     ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
2312     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2313 
2314     memset(dest, 'X', sizeof(dest));
2315     errno = EBADF;
2316     ret = p_mbsnbcat_s(dest, 0, NULL, 0);
2317     ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
2318     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2319     ok(dest[0] == 'X', "Expected the output buffer to be untouched\n");
2320 
2321     memset(dest, 'X', sizeof(dest));
2322     errno = EBADF;
2323     ret = p_mbsnbcat_s(dest, 0, second, 0);
2324     ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
2325     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2326     ok(dest[0] == 'X', "Expected the output buffer to be untouched\n");
2327 
2328     memset(dest, 'X', sizeof(dest));
2329     errno = EBADF;
2330     ret = p_mbsnbcat_s(dest, sizeof(dest), NULL, 0);
2331     ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
2332     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2333     ok(dest[0] == '\0', "Expected the output buffer to be null terminated\n");
2334 
2335     memset(dest, 'X', sizeof(dest));
2336     errno = EBADF;
2337     ret = p_mbsnbcat_s(dest, sizeof(dest), NULL, 10);
2338     ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
2339     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2340     ok(dest[0] == '\0', "Expected the output buffer to be null terminated\n");
2341 
2342     memset(dest, 'X', sizeof(dest));
2343     dest[0] = '\0';
2344     ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second));
2345     ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
2346     ok(!memcmp(dest, second, sizeof(second)),
2347        "Expected the output buffer string to be \"duck\"\n");
2348 
2349     /* Test source truncation behavior. */
2350     memset(dest, 'X', sizeof(dest));
2351     memcpy(dest, first, sizeof(first));
2352     ret = p_mbsnbcat_s(dest, sizeof(dest), second, 0);
2353     ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
2354     ok(!memcmp(dest, first, sizeof(first)),
2355        "Expected the output buffer string to be \"dinosaur\"\n");
2356 
2357     memset(dest, 'X', sizeof(dest));
2358     memcpy(dest, first, sizeof(first));
2359     ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second));
2360     ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
2361     ok(!memcmp(dest, "dinosaurduck", sizeof("dinosaurduck")),
2362        "Expected the output buffer string to be \"dinosaurduck\"\n");
2363 
2364     memset(dest, 'X', sizeof(dest));
2365     memcpy(dest, first, sizeof(first));
2366     ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second) + 1);
2367     ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
2368     ok(!memcmp(dest, "dinosaurduck", sizeof("dinosaurduck")),
2369        "Expected the output buffer string to be \"dinosaurduck\"\n");
2370 
2371     memset(dest, 'X', sizeof(dest));
2372     memcpy(dest, first, sizeof(first));
2373     ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second) - 1);
2374     ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
2375     ok(!memcmp(dest, "dinosaurduck", sizeof("dinosaurduck")),
2376        "Expected the output buffer string to be \"dinosaurduck\"\n");
2377 
2378     memset(dest, 'X', sizeof(dest));
2379     memcpy(dest, first, sizeof(first));
2380     ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second) - 2);
2381     ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
2382     ok(!memcmp(dest, "dinosaurduc", sizeof("dinosaurduc")),
2383        "Expected the output buffer string to be \"dinosaurduc\"\n");
2384 
2385     /* Test destination truncation behavior. */
2386     memset(dest, 'X', sizeof(dest));
2387     memcpy(dest, first, sizeof(first));
2388     errno = EBADF;
2389     ret = p_mbsnbcat_s(dest, sizeof(first) - 1, second, sizeof(second));
2390     ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
2391     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2392     ok(!memcmp(dest, "\0inosaur", sizeof("\0inosaur") - 1),
2393        "Expected the output buffer string to be \"\\0inosaur\" without ending null terminator\n");
2394 
2395     memset(dest, 'X', sizeof(dest));
2396     memcpy(dest, first, sizeof(first));
2397     errno = EBADF;
2398     ret = p_mbsnbcat_s(dest, sizeof(first), second, sizeof(second));
2399     ok(ret == ERANGE, "Expected _mbsnbcat_s to return ERANGE, got %d\n", ret);
2400     ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
2401     ok(!memcmp(dest, "\0inosaurd", sizeof("\0inosaurd") - 1),
2402        "Expected the output buffer string to be \"\\0inosaurd\" without ending null terminator\n");
2403 
2404     memset(dest, 'X', sizeof(dest));
2405     memcpy(dest, first, sizeof(first));
2406     errno = EBADF;
2407     ret = p_mbsnbcat_s(dest, sizeof(first) + 1, second, sizeof(second));
2408     ok(ret == ERANGE, "Expected _mbsnbcat_s to return ERANGE, got %d\n", ret);
2409     ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
2410     ok(!memcmp(dest, "\0inosaurdu", sizeof("\0inosaurdu") - 1),
2411        "Expected the output buffer string to be \"\\0inosaurdu\" without ending null terminator\n");
2412 }
2413 
2414 static void test__mbsupr_s(void)
2415 {
2416     errno_t ret;
2417     unsigned char buffer[20];
2418 
2419     if (!p_mbsupr_s)
2420     {
2421         win_skip("Skipping _mbsupr_s tests\n");
2422         return;
2423     }
2424 
2425     errno = EBADF;
2426     ret = p_mbsupr_s(NULL, 0);
2427     ok(ret == 0, "Expected _mbsupr_s to return 0, got %d\n", ret);
2428 
2429     errno = EBADF;
2430     ret = p_mbsupr_s(NULL, sizeof(buffer));
2431     ok(ret == EINVAL, "Expected _mbsupr_s to return EINVAL, got %d\n", ret);
2432     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2433 
2434     errno = EBADF;
2435     ret = p_mbsupr_s(buffer, 0);
2436     ok(ret == EINVAL, "Expected _mbsupr_s to return EINVAL, got %d\n", ret);
2437     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2438 
2439     memcpy(buffer, "abcdefgh", sizeof("abcdefgh"));
2440     errno = EBADF;
2441     ret = p_mbsupr_s(buffer, sizeof("abcdefgh"));
2442     ok(ret == 0, "Expected _mbsupr_s to return 0, got %d\n", ret);
2443     ok(!memcmp(buffer, "ABCDEFGH", sizeof("ABCDEFGH")),
2444        "Expected the output buffer to be \"ABCDEFGH\", got \"%s\"\n",
2445        buffer);
2446 
2447     memcpy(buffer, "abcdefgh", sizeof("abcdefgh"));
2448     errno = EBADF;
2449     ret = p_mbsupr_s(buffer, sizeof(buffer));
2450     ok(ret == 0, "Expected _mbsupr_s to return 0, got %d\n", ret);
2451     ok(!memcmp(buffer, "ABCDEFGH", sizeof("ABCDEFGH")),
2452        "Expected the output buffer to be \"ABCDEFGH\", got \"%s\"\n",
2453        buffer);
2454 
2455     memcpy(buffer, "abcdefgh", sizeof("abcdefgh"));
2456     errno = EBADF;
2457     ret = p_mbsupr_s(buffer, 4);
2458     ok(ret == EINVAL, "Expected _mbsupr_s to return EINVAL, got %d\n", ret);
2459     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2460 
2461     memcpy(buffer, "abcdefgh\0ijklmnop", sizeof("abcdefgh\0ijklmnop"));
2462     errno = EBADF;
2463     ret = p_mbsupr_s(buffer, sizeof(buffer));
2464     ok(ret == 0, "Expected _mbsupr_s to return 0, got %d\n", ret);
2465     ok(!memcmp(buffer, "ABCDEFGH\0ijklmnop", sizeof("ABCDEFGH\0ijklmnop")),
2466        "Expected the output buffer to be \"ABCDEFGH\\0ijklmnop\", got \"%s\"\n",
2467        buffer);
2468 
2469 }
2470 
2471 static void test__mbslwr_s(void)
2472 {
2473     errno_t ret;
2474     unsigned char buffer[20];
2475 
2476     if (!p_mbslwr_s)
2477     {
2478         win_skip("Skipping _mbslwr_s tests\n");
2479         return;
2480     }
2481 
2482     errno = EBADF;
2483     ret = p_mbslwr_s(NULL, 0);
2484     ok(ret == 0, "Expected _mbslwr_s to return 0, got %d\n", ret);
2485 
2486     errno = EBADF;
2487     ret = p_mbslwr_s(NULL, sizeof(buffer));
2488     ok(ret == EINVAL, "Expected _mbslwr_s to return EINVAL, got %d\n", ret);
2489     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2490 
2491     errno = EBADF;
2492     ret = p_mbslwr_s(buffer, 0);
2493     ok(ret == EINVAL, "Expected _mbslwr_s to return EINVAL, got %d\n", ret);
2494     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2495 
2496     memcpy(buffer, "ABCDEFGH", sizeof("ABCDEFGH"));
2497     errno = EBADF;
2498     ret = p_mbslwr_s(buffer, sizeof("ABCDEFGH"));
2499     ok(ret == 0, "Expected _mbslwr_s to return 0, got %d\n", ret);
2500     ok(!memcmp(buffer, "abcdefgh", sizeof("abcdefgh")),
2501        "Expected the output buffer to be \"abcdefgh\", got \"%s\"\n",
2502        buffer);
2503 
2504     memcpy(buffer, "ABCDEFGH", sizeof("ABCDEFGH"));
2505     errno = EBADF;
2506     ret = p_mbslwr_s(buffer, sizeof(buffer));
2507     ok(ret == 0, "Expected _mbslwr_s to return 0, got %d\n", ret);
2508     ok(!memcmp(buffer, "abcdefgh", sizeof("abcdefgh")),
2509        "Expected the output buffer to be \"abcdefgh\", got \"%s\"\n",
2510        buffer);
2511 
2512     memcpy(buffer, "ABCDEFGH", sizeof("ABCDEFGH"));
2513     errno = EBADF;
2514     ret = p_mbslwr_s(buffer, 4);
2515     ok(ret == EINVAL, "Expected _mbslwr_s to return EINVAL, got %d\n", ret);
2516     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2517 
2518     memcpy(buffer, "ABCDEFGH\0IJKLMNOP", sizeof("ABCDEFGH\0IJKLMNOP"));
2519     errno = EBADF;
2520     ret = p_mbslwr_s(buffer, sizeof(buffer));
2521     ok(ret == 0, "Expected _mbslwr_s to return 0, got %d\n", ret);
2522     ok(!memcmp(buffer, "abcdefgh\0IJKLMNOP", sizeof("abcdefgh\0IJKLMNOP")),
2523        "Expected the output buffer to be \"abcdefgh\\0IJKLMNOP\", got \"%s\"\n",
2524        buffer);
2525 }
2526 
2527 static void test__mbstok(void)
2528 {
2529     const unsigned char delim[] = "t";
2530 
2531     char str[] = "!.!test";
2532     unsigned char *ret;
2533 
2534     strtok(str, "!");
2535 
2536     ret = _mbstok(NULL, delim);
2537     /* most versions of msvcrt use the same buffer for strtok and _mbstok */
2538     ok(!ret || broken((char*)ret==str+4),
2539             "_mbstok(NULL, \"t\") = %p, expected NULL (%p)\n", ret, str);
2540 
2541     ret = _mbstok(NULL, delim);
2542     ok(!ret, "_mbstok(NULL, \"t\") = %p, expected NULL\n", ret);
2543 }
2544 
2545 static void test__ultoa_s(void)
2546 {
2547     errno_t ret;
2548     char buffer[33];
2549 
2550     if (!p_ultoa_s)
2551     {
2552         win_skip("Skipping _ultoa_s tests\n");
2553         return;
2554     }
2555 
2556     errno = EBADF;
2557     ret = p_ultoa_s(0, NULL, 0, 0);
2558     ok(ret == EINVAL, "Expected _ultoa_s to return EINVAL, got %d\n", ret);
2559     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2560 
2561     memset(buffer, 'X', sizeof(buffer));
2562     errno = EBADF;
2563     ret = p_ultoa_s(0, buffer, 0, 0);
2564     ok(ret == EINVAL, "Expected _ultoa_s to return EINVAL, got %d\n", ret);
2565     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2566     ok(buffer[0] == 'X', "Expected the output buffer to be untouched\n");
2567 
2568     memset(buffer, 'X', sizeof(buffer));
2569     errno = EBADF;
2570     ret = p_ultoa_s(0, buffer, sizeof(buffer), 0);
2571     ok(ret == EINVAL, "Expected _ultoa_s to return EINVAL, got %d\n", ret);
2572     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2573     ok(buffer[0] == '\0', "Expected the output buffer to be null terminated\n");
2574 
2575     memset(buffer, 'X', sizeof(buffer));
2576     errno = EBADF;
2577     ret = p_ultoa_s(0, buffer, sizeof(buffer), 64);
2578     ok(ret == EINVAL, "Expected _ultoa_s to return EINVAL, got %d\n", ret);
2579     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2580     ok(buffer[0] == '\0', "Expected the output buffer to be null terminated\n");
2581 
2582     memset(buffer, 'X', sizeof(buffer));
2583     errno = EBADF;
2584     ret = p_ultoa_s(12345678, buffer, 4, 10);
2585     ok(ret == ERANGE, "Expected _ultoa_s to return ERANGE, got %d\n", ret);
2586     ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
2587     ok(!memcmp(buffer, "\000765", 4),
2588        "Expected the output buffer to be null terminated with truncated output\n");
2589 
2590     memset(buffer, 'X', sizeof(buffer));
2591     errno = EBADF;
2592     ret = p_ultoa_s(12345678, buffer, 8, 10);
2593     ok(ret == ERANGE, "Expected _ultoa_s to return ERANGE, got %d\n", ret);
2594     ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
2595     ok(!memcmp(buffer, "\0007654321", 8),
2596        "Expected the output buffer to be null terminated with truncated output\n");
2597 
2598     ret = p_ultoa_s(12345678, buffer, 9, 10);
2599     ok(ret == 0, "Expected _ultoa_s to return 0, got %d\n", ret);
2600     ok(!strcmp(buffer, "12345678"),
2601        "Expected output buffer string to be \"12345678\", got \"%s\"\n",
2602        buffer);
2603 
2604     ret = p_ultoa_s(43690, buffer, sizeof(buffer), 2);
2605     ok(ret == 0, "Expected _ultoa_s to return 0, got %d\n", ret);
2606     ok(!strcmp(buffer, "1010101010101010"),
2607        "Expected output buffer string to be \"1010101010101010\", got \"%s\"\n",
2608        buffer);
2609 
2610     ret = p_ultoa_s(1092009, buffer, sizeof(buffer), 36);
2611     ok(ret == 0, "Expected _ultoa_s to return 0, got %d\n", ret);
2612     ok(!strcmp(buffer, "nell"),
2613        "Expected output buffer string to be \"nell\", got \"%s\"\n",
2614        buffer);
2615 
2616     ret = p_ultoa_s(5704, buffer, sizeof(buffer), 18);
2617     ok(ret == 0, "Expected _ultoa_s to return 0, got %d\n", ret);
2618     ok(!strcmp(buffer, "hag"),
2619        "Expected output buffer string to be \"hag\", got \"%s\"\n",
2620        buffer);
2621 }
2622 
2623 static void test_wctob(void)
2624 {
2625     int ret;
2626 
2627     if(!p_wctob || !setlocale(LC_ALL, "chinese-traditional")) {
2628         win_skip("Skipping wctob tests\n");
2629         return;
2630     }
2631 
2632     ret = p_wctob(0x8141);
2633     ok(ret == EOF, "ret = %x\n", ret);
2634 
2635     ret = p_wctob(0x81);
2636     ok(ret == EOF, "ret = %x\n", ret);
2637 
2638     ret = p_wctob(0xe0);
2639     ok(ret == 0x61, "ret = %x\n", ret);
2640 
2641     _setmbcp(1250);
2642     ret = p_wctob(0x81);
2643     ok(ret == EOF, "ret = %x\n", ret);
2644 
2645     setlocale(LC_ALL, "C");
2646     ret = p_wctob(0x8141);
2647     ok(ret == EOF, "ret = %x\n", ret);
2648 
2649     ret = p_wctob(0x81);
2650     ok(ret == (int)(char)0x81, "ret = %x\n", ret);
2651 
2652     ret = p_wctob(0x9f);
2653     ok(ret == (int)(char)0x9f, "ret = %x\n", ret);
2654 
2655     ret = p_wctob(0xe0);
2656     ok(ret == (int)(char)0xe0, "ret = %x\n", ret);
2657 }
2658 static void test_wctomb(void)
2659 {
2660     mbstate_t state;
2661     unsigned char dst[10];
2662     size_t ret;
2663 
2664     if(!p_wcrtomb || !setlocale(LC_ALL, "Japanese_Japan.932")) {
2665         win_skip("wcrtomb tests\n");
2666         return;
2667     }
2668 
2669     ret = p_wcrtomb(NULL, 0x3042, NULL);
2670     ok(ret == 2, "wcrtomb did not return 2\n");
2671 
2672     state = 1;
2673     dst[2] = 'a';
2674     ret = p_wcrtomb((char*)dst, 0x3042, &state);
2675     ok(ret == 2, "wcrtomb did not return 2\n");
2676     ok(state == 0, "state != 0\n");
2677     ok(dst[0] == 0x82, "dst[0] = %x, expected 0x82\n", dst[0]);
2678     ok(dst[1] == 0xa0, "dst[1] = %x, expected 0xa0\n", dst[1]);
2679     ok(dst[2] == 'a', "dst[2] != 'a'\n");
2680 
2681     ret = p_wcrtomb((char*)dst, 0x3043, NULL);
2682     ok(ret == 2, "wcrtomb did not return 2\n");
2683     ok(dst[0] == 0x82, "dst[0] = %x, expected 0x82\n", dst[0]);
2684     ok(dst[1] == 0xa1, "dst[1] = %x, expected 0xa1\n", dst[1]);
2685 
2686     ret = p_wcrtomb((char*)dst, 0x20, NULL);
2687     ok(ret == 1, "wcrtomb did not return 1\n");
2688     ok(dst[0] == 0x20, "dst[0] = %x, expected 0x20\n", dst[0]);
2689 
2690     ret = p_wcrtomb((char*)dst, 0xffff, NULL);
2691     ok(ret == -1, "wcrtomb did not return -1\n");
2692     ok(dst[0] == 0x3f, "dst[0] = %x, expected 0x3f\n", dst[0]);
2693 
2694     setlocale(LC_ALL, "C");
2695 }
2696 
2697 static void test_tolower(void)
2698 {
2699     WCHAR chw, lower;
2700     char ch, lch;
2701     int ret, len;
2702 
2703     /* test C locale when locale was never changed */
2704     ret = p_tolower(0x41);
2705     ok(ret == 0x61, "ret = %x\n", ret);
2706 
2707     ret = p_tolower(0xF4);
2708     ok(ret == 0xF4, "ret = %x\n", ret);
2709 
2710     errno = 0xdeadbeef;
2711     ret = p_tolower((char)0xF4);
2712     todo_wine ok(ret == (char)0xF4, "ret = %x\n", ret);
2713     todo_wine ok(errno == 0xdeadbeef, "errno = %d\n", errno);
2714 
2715     errno = 0xdeadbeef;
2716     ret = p_tolower((char)0xD0);
2717     todo_wine ok(ret == (char)0xD0, "ret = %x\n", ret);
2718     todo_wine ok(errno == 0xdeadbeef, "errno = %d\n", errno);
2719 
2720     /* test C locale after setting locale */
2721     if(!setlocale(LC_ALL, "us")) {
2722         win_skip("skipping tolower tests that depends on locale\n");
2723         return;
2724     }
2725     setlocale(LC_ALL, "C");
2726 
2727     ch = 0xF4;
2728     errno = 0xdeadbeef;
2729     ret = p_tolower(ch);
2730     if(!MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, &ch, 1, &chw, 1) ||
2731             LCMapStringW(CP_ACP, LCMAP_LOWERCASE, &chw, 1, &lower, 1) != 1 ||
2732             (len = WideCharToMultiByte(CP_ACP, 0, &lower, 1, &lch, 1, NULL, NULL)) != 1)
2733         len = 0;
2734     if(len)
2735         ok(ret==(unsigned char)lch || broken(ret==ch)/*WinXP-*/, "ret = %x\n", ret);
2736     else
2737         ok(ret == ch, "ret = %x\n", ret);
2738     if(!len || ret==(unsigned char)lch)
2739         ok(errno == EILSEQ, "errno = %d\n", errno);
2740 
2741     ch = 0xD0;
2742     errno = 0xdeadbeef;
2743     ret = p_tolower(ch);
2744     if(!MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, &ch, 1, &chw, 1) ||
2745             LCMapStringW(CP_ACP, LCMAP_LOWERCASE, &chw, 1, &lower, 1) != 1 ||
2746             (len = WideCharToMultiByte(CP_ACP, 0, &lower, 1, &lch, 1, NULL, NULL)) != 1)
2747         len = 0;
2748     if(len)
2749         ok(ret==(unsigned char)lch || broken(ret==ch)/*WinXP-*/, "ret = %x\n", ret);
2750     else
2751         ok(ret == ch, "ret = %x\n", ret);
2752     if(!len || ret==(unsigned char)lch)
2753         ok(errno == EILSEQ, "errno = %d\n", errno);
2754 
2755     ret = p_tolower(0xD0);
2756     ok(ret == 0xD0, "ret = %x\n", ret);
2757 
2758     ok(setlocale(LC_ALL, "us") != NULL, "setlocale failed\n");
2759 
2760     ret = p_tolower((char)0xD0);
2761     ok(ret == 0xF0, "ret = %x\n", ret);
2762 
2763     ret = p_tolower(0xD0);
2764     ok(ret == 0xF0, "ret = %x\n", ret);
2765 
2766     setlocale(LC_ALL, "C");
2767 }
2768 
2769 static void test__atodbl(void)
2770 {
2771     _CRT_DOUBLE d;
2772     char num[32];
2773     int ret;
2774 
2775     if(!p__atodbl_l) {
2776         /* Old versions of msvcrt use different values for _OVERFLOW and _UNDERFLOW
2777          * Because of this lets skip _atodbl tests when _atodbl_l is not available */
2778         win_skip("_atodbl_l is not available\n");
2779         return;
2780     }
2781 
2782     num[0] = 0;
2783     ret = p__atodbl_l(&d, num, NULL);
2784     ok(ret == 0, "_atodbl_l(&d, \"\", NULL) returned %d, expected 0\n", ret);
2785     ok(d.x == 0, "d.x = %lf, expected 0\n", d.x);
2786     ret = _atodbl(&d, num);
2787     ok(ret == 0, "_atodbl(&d, \"\") returned %d, expected 0\n", ret);
2788     ok(d.x == 0, "d.x = %lf, expected 0\n", d.x);
2789 
2790     strcpy(num, "t");
2791     ret = p__atodbl_l(&d, num, NULL);
2792     ok(ret == 0, "_atodbl_l(&d, \"t\", NULL) returned %d, expected 0\n", ret);
2793     ok(d.x == 0, "d.x = %lf, expected 0\n", d.x);
2794     ret = _atodbl(&d, num);
2795     ok(ret == 0, "_atodbl(&d, \"t\") returned %d, expected 0\n", ret);
2796     ok(d.x == 0, "d.x = %lf, expected 0\n", d.x);
2797 
2798     strcpy(num, "0");
2799     ret = p__atodbl_l(&d, num, NULL);
2800     ok(ret == 0, "_atodbl_l(&d, \"0\", NULL) returned %d, expected 0\n", ret);
2801     ok(d.x == 0, "d.x = %lf, expected 0\n", d.x);
2802     ret = _atodbl(&d, num);
2803     ok(ret == 0, "_atodbl(&d, \"0\") returned %d, expected 0\n", ret);
2804     ok(d.x == 0, "d.x = %lf, expected 0\n", d.x);
2805 
2806     strcpy(num, "123");
2807     ret = p__atodbl_l(&d, num, NULL);
2808     ok(ret == 0, "_atodbl_l(&d, \"123\", NULL) returned %d, expected 0\n", ret);
2809     ok(d.x == 123, "d.x = %lf, expected 123\n", d.x);
2810     ret = _atodbl(&d, num);
2811     ok(ret == 0, "_atodbl(&d, \"123\") returned %d, expected 0\n", ret);
2812     ok(d.x == 123, "d.x = %lf, expected 123\n", d.x);
2813 
2814     strcpy(num, "1e-309");
2815     ret = p__atodbl_l(&d, num, NULL);
2816     ok(ret == _UNDERFLOW, "_atodbl_l(&d, \"1e-309\", NULL) returned %d, expected _UNDERFLOW\n", ret);
2817     ok(d.x!=0 && almost_equal(d.x, 0), "d.x = %le, expected 0\n", d.x);
2818     ret = _atodbl(&d, num);
2819     ok(ret == _UNDERFLOW, "_atodbl(&d, \"1e-309\") returned %d, expected _UNDERFLOW\n", ret);
2820     ok(d.x!=0 && almost_equal(d.x, 0), "d.x = %le, expected 0\n", d.x);
2821 
2822     strcpy(num, "1e309");
2823     ret = p__atodbl_l(&d, num, NULL);
2824     ok(ret == _OVERFLOW, "_atodbl_l(&d, \"1e309\", NULL) returned %d, expected _OVERFLOW\n", ret);
2825     ret = _atodbl(&d, num);
2826     ok(ret == _OVERFLOW, "_atodbl(&d, \"1e309\") returned %d, expected _OVERFLOW\n", ret);
2827 }
2828 
2829 static void test__stricmp(void)
2830 {
2831     int ret;
2832 
2833     ret = _stricmp("test", "test");
2834     ok(ret == 0, "_stricmp returned %d\n", ret);
2835     ret = _stricmp("a", "z");
2836     ok(ret < 0, "_stricmp returned %d\n", ret);
2837     ret = _stricmp("z", "a");
2838     ok(ret > 0, "_stricmp returned %d\n", ret);
2839     ret = _stricmp("\xa5", "\xb9");
2840     ok(ret < 0, "_stricmp returned %d\n", ret);
2841 
2842     if(!setlocale(LC_ALL, "polish")) {
2843         win_skip("stricmp tests\n");
2844         return;
2845     }
2846 
2847     ret = _stricmp("test", "test");
2848     ok(ret == 0, "_stricmp returned %d\n", ret);
2849     ret = _stricmp("a", "z");
2850     ok(ret < 0, "_stricmp returned %d\n", ret);
2851     ret = _stricmp("z", "a");
2852     ok(ret > 0, "_stricmp returned %d\n", ret);
2853     ret = _stricmp("\xa5", "\xb9");
2854     ok(ret == 0, "_stricmp returned %d\n", ret);
2855     ret = _stricmp("a", "\xb9");
2856     ok(ret < 0, "_stricmp returned %d\n", ret);
2857 
2858     setlocale(LC_ALL, "C");
2859 }
2860 
2861 static void test__wcstoi64(void)
2862 {
2863     static const WCHAR digit[] = { '9', 0 };
2864     static const WCHAR space[] = { ' ', 0 };
2865     static const WCHAR stock[] = { 0x3231, 0 }; /* PARENTHESIZED IDEOGRAPH STOCK */
2866     static const WCHAR cjk_1[] = { 0x4e00, 0 }; /* CJK Ideograph, First */
2867     static const WCHAR tamil[] = { 0x0bef, 0 }; /* TAMIL DIGIT NINE */
2868     static const WCHAR thai[]  = { 0x0e59, 0 }; /* THAI DIGIT NINE */
2869     static const WCHAR fullwidth[] = { 0xff19, 0 }; /* FULLWIDTH DIGIT NINE */
2870     static const WCHAR superscript1[] = { 0xb9, 0 }; /* SUPERSCRIPT ONE */
2871     static const WCHAR minus_0x91[]  = { '-', 0x0e50, 'x', 0xff19, '1', 0 };
2872     static const WCHAR plus_071[]  = { '+', 0x0e50, 0xff17, '1', 0 };
2873     static const WCHAR hex[] = { 0xff19, 'f', 0x0e59, 0xff46, 0 };
2874     static const WCHAR zeros[] = {
2875         0x660, 0x6f0, 0x966, 0x9e6, 0xa66, 0xae6, 0xb66, 0xc66, 0xce6,
2876         0xd66, 0xe50, 0xed0, 0xf20, 0x1040, 0x17e0, 0x1810, 0xff10
2877     };
2878     int i;
2879 
2880     __int64 res;
2881     unsigned __int64 ures;
2882     WCHAR *endpos;
2883 
2884     if (!p_wcstoi64 || !p_wcstoui64) {
2885         win_skip("_wcstoi64 or _wcstoui64 not found\n");
2886         return;
2887     }
2888 
2889     res = p_wcstoi64(digit, NULL, 10);
2890     ok(res == 9, "res != 9\n");
2891     res = p_wcstoi64(space, &endpos, 0);
2892     ok(endpos == space, "endpos != space\n");
2893     res = p_wcstoi64(stock, &endpos, 10);
2894     ok(res == 0, "res != 0\n");
2895     ok(endpos == stock, "Incorrect endpos (%p-%p)\n", stock, endpos);
2896     res = p_wcstoi64(cjk_1, NULL, 0);
2897     ok(res == 0, "res != 0\n");
2898     res = p_wcstoi64(tamil, &endpos, 10);
2899     ok(res == 0, "res != 0\n");
2900     ok(endpos == tamil, "Incorrect endpos (%p-%p)\n", tamil, endpos);
2901     res = p_wcstoi64(thai, NULL, 10);
2902     ok(res == 9, "res != 9\n");
2903     res = p_wcstoi64(fullwidth, NULL, 10);
2904     ok(res == 9, "res != 9\n");
2905     res = p_wcstoi64(superscript1, NULL, 10);
2906     ok(res == 0, "res != 0\n");
2907     res = p_wcstoi64(hex, NULL, 16);
2908     ok(res == 0x9f9, "res != 0x9f9\n");
2909     res = p_wcstoi64(minus_0x91, NULL, 0);
2910     ok(res == -0x91, "res != -0x91\n");
2911     res = p_wcstoi64(plus_071, NULL, 0);
2912     ok(res == 071, "res != 071\n");
2913 
2914     ures = p_wcstoui64(digit, NULL, 10);
2915     ok(ures == 9, "ures != 9\n");
2916     ures = p_wcstoui64(space, &endpos, 0);
2917     ok(endpos == space, "endpos != space\n");
2918     ures = p_wcstoui64(stock, &endpos, 10);
2919     ok(ures == 0, "ures != 0\n");
2920     ok(endpos == stock, "Incorrect endpos (%p-%p)\n", stock, endpos);
2921     ures = p_wcstoui64(tamil, &endpos, 10);
2922     ok(ures == 0, "ures != 0\n");
2923     ok(endpos == tamil, "Incorrect endpos (%p-%p)\n", tamil, endpos);
2924     ures = p_wcstoui64(thai, NULL, 10);
2925     ok(ures == 9, "ures != 9\n");
2926     ures = p_wcstoui64(fullwidth, NULL, 10);
2927     ok(ures == 9, "ures != 9\n");
2928     ures = p_wcstoui64(superscript1, NULL, 10);
2929     ok(ures == 0, "ures != 0\n");
2930     ures = p_wcstoui64(hex, NULL, 16);
2931     ok(ures == 0x9f9, "ures != 0x9f9\n");
2932     ures = p_wcstoui64(plus_071, NULL, 0);
2933     ok(ures == 071, "ures != 071\n");
2934 
2935     /* Test various unicode digits */
2936     for (i = 0; i < sizeof(zeros) / sizeof(zeros[0]); ++i) {
2937         WCHAR tmp[] = {zeros[i] + 4, zeros[i], zeros[i] + 5, 0};
2938         res = p_wcstoi64(tmp, NULL, 0);
2939         ok(res == 405, "with zero = U+%04X: got %d, expected 405\n", zeros[i], (int)res);
2940         tmp[1] = zeros[i] + 10;
2941         res = p_wcstoi64(tmp, NULL, 16);
2942         ok(res == 4, "with zero = U+%04X: got %d, expected 4\n", zeros[i], (int)res);
2943     }
2944 
2945     return;
2946 }
2947 
2948 static void test_atoi(void)
2949 {
2950     int r;
2951 
2952     r = atoi("0");
2953     ok(r == 0, "atoi(0) = %d\n", r);
2954 
2955     r = atoi("-1");
2956     ok(r == -1, "atoi(-1) = %d\n", r);
2957 
2958     r = atoi("1");
2959     ok(r == 1, "atoi(1) = %d\n", r);
2960 
2961     r = atoi("4294967296");
2962     ok(r == 0, "atoi(4294967296) = %d\n", r);
2963 }
2964 
2965 static void test_atof(void)
2966 {
2967     double d;
2968 
2969     d = atof("0.0");
2970     ok(almost_equal(d, 0.0), "d = %lf\n", d);
2971 
2972     d = atof("1.0");
2973     ok(almost_equal(d, 1.0), "d = %lf\n", d);
2974 
2975     d = atof("-1.0");
2976     ok(almost_equal(d, -1.0), "d = %lf\n", d);
2977 
2978     if (!p__atof_l)
2979     {
2980         win_skip("_atof_l not found\n");
2981         return;
2982     }
2983 
2984     errno = EBADF;
2985     d = atof(NULL);
2986     ok(almost_equal(d, 0.0), "d = %lf\n", d);
2987     ok(errno == EINVAL, "errno = %x\n", errno);
2988 
2989     errno = EBADF;
2990     d = p__atof_l(NULL, NULL);
2991     ok(almost_equal(d, 0.0), "d = %lf\n", d);
2992     ok(errno == EINVAL, "errno = %x\n", errno);
2993 }
2994 
2995 static void test_strncpy(void)
2996 {
2997 #define TEST_STRNCPY_LEN 10
2998     char *ret;
2999     char dst[TEST_STRNCPY_LEN + 1];
3000     char not_null_terminated[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
3001 
3002     /* strlen(src) > TEST_STRNCPY_LEN */
3003     ret = strncpy(dst, "01234567890123456789", TEST_STRNCPY_LEN);
3004     ok(ret == dst, "ret != dst\n");
3005     ok(!strncmp(dst, "0123456789", TEST_STRNCPY_LEN), "dst != 0123456789\n");
3006 
3007     /* without null-terminated */
3008     ret = strncpy(dst, not_null_terminated, TEST_STRNCPY_LEN);
3009     ok(ret == dst, "ret != dst\n");
3010     ok(!strncmp(dst, "0123456789", TEST_STRNCPY_LEN), "dst != 0123456789\n");
3011 
3012     /* strlen(src) < TEST_STRNCPY_LEN */
3013     strcpy(dst, "0123456789");
3014     ret = strncpy(dst, "012345", TEST_STRNCPY_LEN);
3015     ok(ret == dst, "ret != dst\n");
3016     ok(!strcmp(dst, "012345"), "dst != 012345\n");
3017     ok(dst[TEST_STRNCPY_LEN - 1] == '\0', "dst[TEST_STRNCPY_LEN - 1] != 0\n");
3018 
3019     /* strlen(src) == TEST_STRNCPY_LEN */
3020     ret = strncpy(dst, "0123456789", TEST_STRNCPY_LEN);
3021     ok(ret == dst, "ret != dst\n");
3022     ok(!strncmp(dst, "0123456789", TEST_STRNCPY_LEN), "dst != 0123456789\n");
3023 }
3024 
3025 static void test_strxfrm(void)
3026 {
3027     char dest[256];
3028     size_t ret;
3029 
3030     /* crashes on old version of msvcrt */
3031     if(p__atodbl_l) {
3032         errno = 0xdeadbeef;
3033         ret = strxfrm(NULL, "src", 1);
3034         ok(ret == INT_MAX, "ret = %d\n", (int)ret);
3035         ok(errno == EINVAL, "errno = %d\n", errno);
3036 
3037         errno = 0xdeadbeef;
3038         ret = strxfrm(dest, NULL, 100);
3039         ok(ret == INT_MAX, "ret = %d\n", (int)ret);
3040         ok(errno == EINVAL, "errno = %d\n", errno);
3041     }
3042 
3043     ret = strxfrm(NULL, "src", 0);
3044     ok(ret == 3, "ret = %d\n", (int)ret);
3045     dest[0] = 'a';
3046     ret = strxfrm(dest, "src", 0);
3047     ok(ret == 3, "ret = %d\n", (int)ret);
3048     ok(dest[0] == 'a', "dest[0] = %d\n", dest[0]);
3049 
3050     dest[3] = 'a';
3051     ret = strxfrm(dest, "src", 5);
3052     ok(ret == 3, "ret = %d\n", (int)ret);
3053     ok(!strcmp(dest, "src"), "dest = %s\n", dest);
3054 
3055     errno = 0xdeadbeef;
3056     dest[1] = 'a';
3057     ret = strxfrm(dest, "src", 1);
3058     ok(ret == 3, "ret = %d\n", (int)ret);
3059     ok(dest[0] == 's', "dest[0] = %d\n", dest[0]);
3060     ok(dest[1] == 'a', "dest[1] = %d\n", dest[1]);
3061     ok(errno == 0xdeadbeef, "errno = %d\n", errno);
3062 
3063     ret = strxfrm(dest, "", 5);
3064     ok(ret == 0, "ret = %d\n", (int)ret);
3065     ok(!dest[0], "dest[0] = %d\n", dest[0]);
3066 
3067     if(!setlocale(LC_ALL, "polish")) {
3068         win_skip("stxfrm tests\n");
3069         return;
3070     }
3071 
3072     ret = strxfrm(NULL, "src", 0);
3073     ok(ret < sizeof(dest)-1, "ret = %d\n", (int)ret);
3074     dest[0] = 'a';
3075     ret = strxfrm(dest, "src", 0);
3076     ok(ret < sizeof(dest)-1, "ret = %d\n", (int)ret);
3077     ok(dest[0] == 'a', "dest[0] = %d\n", dest[0]);
3078 
3079     ret = strxfrm(dest, "src", ret+1);
3080     ok(ret < sizeof(dest)-1, "ret = %d\n", (int)ret);
3081     ok(dest[0], "dest[0] = 0\n");
3082 
3083     errno = 0xdeadbeef;
3084     dest[0] = 'a';
3085     ret = strxfrm(dest, "src", 5);
3086     ok(ret>5 && ret<sizeof(dest)-1, "ret = %d\n", (int)ret);
3087     ok(!dest[0] || broken(!p__atodbl_l && dest[0]=='a'), "dest[0] = %d\n", dest[0]);
3088 
3089     setlocale(LC_ALL, "C");
3090 }
3091 
3092 static void test__strnset_s(void)
3093 {
3094     char buf[5] = {0};
3095     int r;
3096 
3097     if(!p__strnset_s) {
3098         win_skip("_strnset_s not available\n");
3099         return;
3100     }
3101 
3102     r = p__strnset_s(NULL, 0, 'a', 0);
3103     ok(r == 0, "r = %d\n", r);
3104 
3105     buf[0] = buf[1] = buf[2] = 'b';
3106     r = p__strnset_s(buf, sizeof(buf), 'a', 2);
3107     ok(r == 0, "r = %d\n", r);
3108     ok(!strcmp(buf, "aab"), "buf = %s\n", buf);
3109 
3110     r = p__strnset_s(buf, 0, 'a', 0);
3111     ok(r == EINVAL, "r = %d\n", r);
3112 
3113     r = p__strnset_s(NULL, 0, 'a', 1);
3114     ok(r == EINVAL, "r = %d\n", r);
3115 
3116     buf[3] = 'b';
3117     r = p__strnset_s(buf, sizeof(buf)-1, 'c', 2);
3118     ok(r == EINVAL, "r = %d\n", r);
3119     ok(!buf[0] && buf[1]=='c' && buf[2]=='b', "buf = %s\n", buf);
3120 }
3121 
3122 static void test__wcsset_s(void)
3123 {
3124     wchar_t str[10];
3125     int r;
3126 
3127     if(!p__wcsset_s) {
3128         win_skip("_wcsset_s not available\n");
3129         return;
3130     }
3131 
3132     r = p__wcsset_s(NULL, 0, 'a');
3133     ok(r == EINVAL, "r = %d\n", r);
3134 
3135     str[0] = 'a';
3136     r = p__wcsset_s(str, 0, 'a');
3137     ok(r == EINVAL, "r = %d\n", r);
3138     ok(str[0] == 'a', "str[0] = %d\n", str[0]);
3139 
3140     str[0] = 'a';
3141     str[1] = 'b';
3142     r = p__wcsset_s(str, 2, 'c');
3143     ok(r == EINVAL, "r = %d\n", r);
3144     ok(!str[0], "str[0] = %d\n", str[0]);
3145     ok(str[1] == 'b', "str[1] = %d\n", str[1]);
3146 
3147     str[0] = 'a';
3148     str[1] = 0;
3149     str[2] = 'b';
3150     r = p__wcsset_s(str, 3, 'c');
3151     ok(str[0] == 'c', "str[0] = %d\n", str[0]);
3152     ok(str[1] == 0, "str[1] = %d\n", str[1]);
3153     ok(str[2] == 'b', "str[2] = %d\n", str[2]);
3154 }
3155 
3156 static void test__mbscmp(void)
3157 {
3158     static const unsigned char a[] = {'a',0}, b[] = {'b',0};
3159     int ret;
3160 
3161     if (!p_mbrlen)
3162     {
3163         win_skip("_mbscmp tests\n");
3164         return;
3165     }
3166 
3167     ret = _mbscmp(NULL, NULL);
3168     ok(ret == INT_MAX, "got %d\n", ret);
3169 
3170     ret = _mbscmp(a, NULL);
3171     ok(ret == INT_MAX, "got %d\n", ret);
3172 
3173     ret = _mbscmp(NULL, a);
3174     ok(ret == INT_MAX, "got %d\n", ret);
3175 
3176     ret = _mbscmp(a, a);
3177     ok(!ret, "got %d\n", ret);
3178 
3179     ret = _mbscmp(a, b);
3180     ok(ret == -1, "got %d\n", ret);
3181 
3182     ret = _mbscmp(b, a);
3183     ok(ret == 1, "got %d\n", ret);
3184 }
3185 
3186 static void test__ismbclx(void)
3187 {
3188     int cp, ret;
3189 
3190     ret = _ismbcl0(0);
3191     ok(!ret, "got %d\n", ret);
3192 
3193     cp = _setmbcp(1252);
3194 
3195     ret = _ismbcl0(0x8140);
3196     ok(!ret, "got %d\n", ret);
3197 
3198     _setmbcp(932);
3199 
3200     ret = _ismbcl0(0);
3201     ok(!ret, "got %d\n", ret);
3202 
3203     ret = _ismbcl0(0x8140);
3204     ok(ret, "got %d\n", ret);
3205 
3206     _setmbcp(cp);
3207 }
3208 
3209 START_TEST(string)
3210 {
3211     char mem[100];
3212     static const char xilstring[]="c:/xilinx";
3213     int nLen;
3214 
3215     hMsvcrt = GetModuleHandleA("msvcrt.dll");
3216     if (!hMsvcrt)
3217         hMsvcrt = GetModuleHandleA("msvcrtd.dll");
3218     ok(hMsvcrt != 0, "GetModuleHandleA failed\n");
3219     SET(pmemcpy,"memcpy");
3220     p_memcpy_s = (void*)GetProcAddress( hMsvcrt, "memcpy_s" );
3221     p_memmove_s = (void*)GetProcAddress( hMsvcrt, "memmove_s" );
3222     SET(pmemcmp,"memcmp");
3223     SET(p_mbctype,"_mbctype");
3224     SET(p__mb_cur_max,"__mb_cur_max");
3225     SET(p_strcpy, "strcpy");
3226     pstrcpy_s = (void *)GetProcAddress( hMsvcrt,"strcpy_s" );
3227     pstrcat_s = (void *)GetProcAddress( hMsvcrt,"strcat_s" );
3228     p_mbscat_s = (void*)GetProcAddress( hMsvcrt, "_mbscat_s" );
3229     p_mbsnbcat_s = (void *)GetProcAddress( hMsvcrt,"_mbsnbcat_s" );
3230     p_mbsnbcpy_s = (void *)GetProcAddress( hMsvcrt,"_mbsnbcpy_s" );
3231     p__mbscpy_s = (void *)GetProcAddress( hMsvcrt,"_mbscpy_s" );
3232     p_wcscpy_s = (void *)GetProcAddress( hMsvcrt,"wcscpy_s" );
3233     p_wcsncpy_s = (void *)GetProcAddress( hMsvcrt,"wcsncpy_s" );
3234     p_wcsncat_s = (void *)GetProcAddress( hMsvcrt,"wcsncat_s" );
3235     p_wcsupr_s = (void *)GetProcAddress( hMsvcrt,"_wcsupr_s" );
3236     p_strnlen = (void *)GetProcAddress( hMsvcrt,"strnlen" );
3237     p_strtoi64 = (void *)GetProcAddress(hMsvcrt, "_strtoi64");
3238     p_strtoui64 = (void *)GetProcAddress(hMsvcrt, "_strtoui64");
3239     p_wcstoi64 = (void *)GetProcAddress(hMsvcrt, "_wcstoi64");
3240     p_wcstoui64 = (void *)GetProcAddress(hMsvcrt, "_wcstoui64");
3241     pmbstowcs_s = (void *)GetProcAddress(hMsvcrt, "mbstowcs_s");
3242     pwcstombs_s = (void *)GetProcAddress(hMsvcrt, "wcstombs_s");
3243     pwcsrtombs = (void *)GetProcAddress(hMsvcrt, "wcsrtombs");
3244     p_gcvt_s = (void *)GetProcAddress(hMsvcrt, "_gcvt_s");
3245     p_itoa_s = (void *)GetProcAddress(hMsvcrt, "_itoa_s");
3246     p_strlwr_s = (void *)GetProcAddress(hMsvcrt, "_strlwr_s");
3247     p_ultoa_s = (void *)GetProcAddress(hMsvcrt, "_ultoa_s");
3248     p_wcslwr_s = (void*)GetProcAddress(hMsvcrt, "_wcslwr_s");
3249     p_mbsupr_s = (void*)GetProcAddress(hMsvcrt, "_mbsupr_s");
3250     p_mbslwr_s = (void*)GetProcAddress(hMsvcrt, "_mbslwr_s");
3251     p_wctob = (void*)GetProcAddress(hMsvcrt, "wctob");
3252     p_wcrtomb = (void*)GetProcAddress(hMsvcrt, "wcrtomb");
3253     p_tolower = (void*)GetProcAddress(hMsvcrt, "tolower");
3254     p_mbrlen = (void*)GetProcAddress(hMsvcrt, "mbrlen");
3255     p_mbrtowc = (void*)GetProcAddress(hMsvcrt, "mbrtowc");
3256     p_mbsrtowcs = (void*)GetProcAddress(hMsvcrt, "mbsrtowcs");
3257     p_mbsrtowcs_s = (void*)GetProcAddress(hMsvcrt, "mbsrtowcs_s");
3258     p__atodbl_l = (void*)GetProcAddress(hMsvcrt, "_atodbl_l");
3259     p__atof_l = (void*)GetProcAddress(hMsvcrt, "_atof_l");
3260     p__strtod_l = (void*)GetProcAddress(hMsvcrt, "_strtod_l");
3261     p__strnset_s = (void*)GetProcAddress(hMsvcrt, "_strnset_s");
3262     p__wcsset_s = (void*)GetProcAddress(hMsvcrt, "_wcsset_s");
3263 
3264     /* MSVCRT memcpy behaves like memmove for overlapping moves,
3265        MFC42 CString::Insert seems to rely on that behaviour */
3266     strcpy(mem,xilstring);
3267     nLen=strlen(xilstring);
3268     pmemcpy(mem+5, mem,nLen+1);
3269     ok(pmemcmp(mem+5,xilstring, nLen) == 0,
3270        "Got result %s\n",mem+5);
3271 
3272     /* run tolower tests first */
3273     test_tolower();
3274     test_swab();
3275     test_mbcp();
3276     test_mbsspn();
3277     test_mbsspnp();
3278     test_strdup();
3279     test_strcpy_s();
3280     test_memcpy_s();
3281     test_memmove_s();
3282     test_strcat_s();
3283     test__mbscat_s();
3284     test__mbsnbcpy_s();
3285     test__mbscpy_s();
3286     test_mbcjisjms();
3287     test_mbcjmsjis();
3288     test_mbctohira();
3289     test_mbctokata();
3290     test_mbbtombc();
3291     test_mbctombb();
3292     test_ismbckata();
3293     test_ismbclegal();
3294     test_strtok();
3295     test__mbstok();
3296     test_wcscpy_s();
3297     test__wcsupr_s();
3298     test_strtol();
3299     test_strnlen();
3300     test__strtoi64();
3301     test__strtod();
3302     test_mbstowcs();
3303     test_gcvt();
3304     test__itoa_s();
3305     test__strlwr_s();
3306     test_wcsncat_s();
3307     test__mbsnbcat_s();
3308     test__ultoa_s();
3309     test__wcslwr_s();
3310     test__mbsupr_s();
3311     test__mbslwr_s();
3312     test_wctob();
3313     test_wctomb();
3314     test__atodbl();
3315     test__stricmp();
3316     test__wcstoi64();
3317     test_atoi();
3318     test_atof();
3319     test_strncpy();
3320     test_strxfrm();
3321     test__strnset_s();
3322     test__wcsset_s();
3323     test__mbscmp();
3324     test__ismbclx();
3325 }
3326