crypt32: Free the encoded msg (Coverity).
[wine] / dlls / msvcrt / tests / string.c
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 "wine/test.h"
22 #include "winbase.h"
23 #include "winnls.h"
24 #include <string.h>
25 #include <mbstring.h>
26 #include <wchar.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <mbctype.h>
30 #include <locale.h>
31 #include <errno.h>
32 #include <limits.h>
33 #include <math.h>
34
35 static char *buf_to_string(const unsigned char *bin, int len, int nr)
36 {
37     static char buf[2][1024];
38     char *w = buf[nr];
39     int i;
40
41     for (i = 0; i < len; i++)
42     {
43         sprintf(w, "%02x ", (unsigned char)bin[i]);
44         w += strlen(w);
45     }
46     return buf[nr];
47 }
48
49 static void __cdecl test_invalid_parameter_handler(const wchar_t *expression,
50         const wchar_t *function, const wchar_t *file,
51         unsigned line, uintptr_t arg)
52 {
53     /* we just ignore handler calls */
54 }
55
56 #define expect_eq(expr, value, type, format) { type ret = (expr); ok((value) == ret, #expr " expected " format " got " format "\n", value, ret); }
57 #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)); }
58
59 static void* (__cdecl *pmemcpy)(void *, const void *, size_t n);
60 static int (__cdecl *p_memcpy_s)(void *, size_t, const void *, size_t);
61 static int (__cdecl *p_memmove_s)(void *, size_t, const void *, size_t);
62 static int* (__cdecl *pmemcmp)(void *, const void *, size_t n);
63 static int (__cdecl *pstrcpy_s)(char *dst, size_t len, const char *src);
64 static int (__cdecl *pstrcat_s)(char *dst, size_t len, const char *src);
65 static int (__cdecl *p_mbsnbcat_s)(unsigned char *dst, size_t size, const unsigned char *src, size_t count);
66 static int (__cdecl *p_mbsnbcpy_s)(unsigned char * dst, size_t size, const unsigned char * src, size_t count);
67 static int (__cdecl *p_wcscpy_s)(wchar_t *wcDest, size_t size, const wchar_t *wcSrc);
68 static int (__cdecl *p_wcsncpy_s)(wchar_t *wcDest, size_t size, const wchar_t *wcSrc, size_t count);
69 static int (__cdecl *p_wcsncat_s)(wchar_t *dst, size_t elem, const wchar_t *src, size_t count);
70 static int (__cdecl *p_wcsupr_s)(wchar_t *str, size_t size);
71 static size_t (__cdecl *p_strnlen)(const char *, size_t);
72 static __int64 (__cdecl *p_strtoi64)(const char *, char **, int);
73 static unsigned __int64 (__cdecl *p_strtoui64)(const char *, char **, int);
74 static int (__cdecl *pwcstombs_s)(size_t*,char*,size_t,const wchar_t*,size_t);
75 static int (__cdecl *pmbstowcs_s)(size_t*,wchar_t*,size_t,const char*,size_t);
76 static size_t (__cdecl *p_mbsrtowcs)(wchar_t*, const char**, size_t, mbstate_t*);
77 static size_t (__cdecl *pwcsrtombs)(char*, const wchar_t**, size_t, int*);
78 static errno_t (__cdecl *p_gcvt_s)(char*,size_t,double,int);
79 static errno_t (__cdecl *p_itoa_s)(int,char*,size_t,int);
80 static errno_t (__cdecl *p_strlwr_s)(char*,size_t);
81 static errno_t (__cdecl *p_ultoa_s)(__msvcrt_ulong,char*,size_t,int);
82 static int *p__mb_cur_max;
83 static unsigned char *p_mbctype;
84 static _invalid_parameter_handler (__cdecl *p_set_invalid_parameter_handler)(_invalid_parameter_handler);
85 static int (__cdecl *p_wcslwr_s)(wchar_t*,size_t);
86 static errno_t (__cdecl *p_mbsupr_s)(unsigned char *str, size_t numberOfElements);
87 static errno_t (__cdecl *p_mbslwr_s)(unsigned char *str, size_t numberOfElements);
88 static int (__cdecl *p_wctob)(wint_t);
89 static size_t (__cdecl *p_wcrtomb)(char*, wchar_t, mbstate_t*);
90 static int (__cdecl *p_tolower)(int);
91 static size_t (__cdecl *p_mbrlen)(const char*, size_t, mbstate_t*);
92 static size_t (__cdecl *p_mbrtowc)(wchar_t*, const char*, size_t, mbstate_t*);
93 static int (__cdecl *p__atodbl_l)(_CRT_DOUBLE*,char*,_locale_t);
94
95 #define SETNOFAIL(x,y) x = (void*)GetProcAddress(hMsvcrt,y)
96 #define SET(x,y) SETNOFAIL(x,y); ok(x != NULL, "Export '%s' not found\n", y)
97
98 static HMODULE hMsvcrt;
99
100 static void test_swab( void ) {
101     char original[]  = "BADCFEHGJILKNMPORQTSVUXWZY@#";
102     char expected1[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ@#";
103     char expected2[] = "ABCDEFGHIJKLMNOPQRSTUVWX$";
104     char expected3[] = "$";
105     
106     char from[30];
107     char to[30];
108     
109     int testsize;
110     
111     /* Test 1 - normal even case */                               
112     memset(to,'$', sizeof(to));
113     memset(from,'@', sizeof(from));
114     testsize = 26;
115     memcpy(from, original, testsize);
116     _swab( from, to, testsize );
117     ok(memcmp(to,expected1,testsize) == 0, "Testing even size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
118
119     /* Test 2 - uneven case  */                               
120     memset(to,'$', sizeof(to));
121     memset(from,'@', sizeof(from));
122     testsize = 25;
123     memcpy(from, original, testsize);
124     _swab( from, to, testsize );
125     ok(memcmp(to,expected2,testsize) == 0, "Testing odd size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
126
127     /* Test 3 - from = to */                               
128     memset(to,'$', sizeof(to));
129     memset(from,'@', sizeof(from));
130     testsize = 26;
131     memcpy(to, original, testsize);
132     _swab( to, to, testsize );
133     ok(memcmp(to,expected1,testsize) == 0, "Testing overlapped size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
134
135     /* Test 4 - 1 bytes */                               
136     memset(to,'$', sizeof(to));
137     memset(from,'@', sizeof(from));
138     testsize = 1;
139     memcpy(from, original, testsize);
140     _swab( from, to, testsize );
141     ok(memcmp(to,expected3,testsize) == 0, "Testing small size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
142 }
143
144 #if 0      /* use this to generate more tests */
145
146 static void test_codepage(int cp)
147 {
148     int i;
149     int prev;
150     int count = 1;
151
152     ok(_setmbcp(cp) == 0, "Couldn't set mbcp\n");
153
154     prev = p_mbctype[0];
155     printf("static int result_cp_%d_mbctype[] = { ", cp);
156     for (i = 1; i < 257; i++)
157     {
158         if (p_mbctype[i] != prev)
159         {
160             printf("0x%x,%d, ", prev, count);
161             prev = p_mbctype[i];
162             count = 1;
163         }
164         else
165             count++;
166     }
167     printf("0x%x,%d };\n", prev, count);
168 }
169
170 #else
171
172 /* RLE-encoded mbctype tables for given codepages */
173 static int result_cp_932_mbctype[] = { 0x0,65, 0x8,1, 0x18,26, 0x8,6, 0x28,26, 0x8,4,
174   0x0,1, 0x8,1, 0xc,31, 0x8,1, 0xa,5, 0x9,58, 0xc,29, 0,3 };
175 static int result_cp_936_mbctype[] = { 0x0,65, 0x8,1, 0x18,26, 0x8,6, 0x28,26, 0x8,6,
176   0xc,126, 0,1 };
177 static int result_cp_949_mbctype[] = { 0x0,66, 0x18,26, 0x8,6, 0x28,26, 0x8,6, 0xc,126,
178   0,1 };
179 static int result_cp_950_mbctype[] = { 0x0,65, 0x8,1, 0x18,26, 0x8,6, 0x28,26, 0x8,4,
180   0x0,2, 0x4,32, 0xc,94, 0,1 };
181
182 static void test_cp_table(int cp, int *result)
183 {
184     int i;
185     int count = 0;
186     int curr = 0;
187     _setmbcp(cp);
188     for (i = 0; i < 256; i++)
189     {
190         if (count == 0)
191         {
192             curr = result[0];
193             count = result[1];
194             result += 2;
195         }
196         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);
197         count--;
198     }
199 }
200
201 #define test_codepage(num) test_cp_table(num, result_cp_##num##_mbctype);
202
203 #endif
204
205 static void test_mbcp(void)
206 {
207     int mb_orig_max = *p__mb_cur_max;
208     int curr_mbcp = _getmbcp();
209     unsigned char *mbstring = (unsigned char *)"\xb0\xb1\xb2 \xb3\xb4 \xb5"; /* incorrect string */
210     unsigned char *mbstring2 = (unsigned char *)"\xb0\xb1\xb2\xb3Q\xb4\xb5"; /* correct string */
211     unsigned char *mbsonlylead = (unsigned char *)"\xb0\0\xb1\xb2 \xb3";
212     unsigned char buf[16];
213     int step;
214     CPINFO cp_info;
215
216     /* _mbtype tests */
217
218     /* An SBCS codepage test. The ctype of characters on e.g. CP1252 or CP1250 differs slightly
219      * between versions of Windows. Also Windows 9x seems to ignore the codepage and always uses
220      * CP1252 (or the ACP?) so we test only a few ASCII characters */
221     _setmbcp(1252);
222     expect_eq(p_mbctype[10], 0, char, "%x");
223     expect_eq(p_mbctype[50], 0, char, "%x");
224     expect_eq(p_mbctype[66], _SBUP, char, "%x");
225     expect_eq(p_mbctype[100], _SBLOW, char, "%x");
226     expect_eq(p_mbctype[128], 0, char, "%x");
227     _setmbcp(1250);
228     expect_eq(p_mbctype[10], 0, char, "%x");
229     expect_eq(p_mbctype[50], 0, char, "%x");
230     expect_eq(p_mbctype[66], _SBUP, char, "%x");
231     expect_eq(p_mbctype[100], _SBLOW, char, "%x");
232     expect_eq(p_mbctype[128], 0, char, "%x");
233
234     /* double byte code pages */
235     test_codepage(932);
236     test_codepage(936);
237     test_codepage(949);
238     test_codepage(950);
239
240     _setmbcp(936);
241     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);
242     ok(_ismbblead('\354'), "\354 should be a lead byte\n");
243     ok(_ismbblead(' ') == FALSE, "' ' should not be a lead byte\n");
244     ok(_ismbblead(0x1234b0), "0x1234b0 should not be a lead byte\n");
245     ok(_ismbblead(0x123420) == FALSE, "0x123420 should not be a lead byte\n");
246     ok(_ismbbtrail('\xb0'), "\xa0 should be a trail byte\n");
247     ok(_ismbbtrail(' ') == FALSE, "' ' should not be a trail byte\n");
248
249     /* _ismbslead */
250     expect_eq(_ismbslead(mbstring, &mbstring[0]), -1, int, "%d");
251     expect_eq(_ismbslead(mbstring, &mbstring[1]), FALSE, int, "%d");
252     expect_eq(_ismbslead(mbstring, &mbstring[2]), -1, int, "%d");
253     expect_eq(_ismbslead(mbstring, &mbstring[3]), FALSE, int, "%d");
254     expect_eq(_ismbslead(mbstring, &mbstring[4]), -1, int, "%d");
255     expect_eq(_ismbslead(mbstring, &mbstring[5]), FALSE, int, "%d");
256     expect_eq(_ismbslead(mbstring, &mbstring[6]), FALSE, int, "%d");
257     expect_eq(_ismbslead(mbstring, &mbstring[7]), -1, int, "%d");
258     expect_eq(_ismbslead(mbstring, &mbstring[8]), FALSE, int, "%d");
259
260     expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[0]), -1, int, "%d");
261     expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[1]), FALSE, int, "%d");
262     expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[2]), FALSE, int, "%d");
263     expect_eq(_ismbslead(mbsonlylead, &mbsonlylead[5]), FALSE, int, "%d");
264
265     /* _ismbstrail */
266     expect_eq(_ismbstrail(mbstring, &mbstring[0]), FALSE, int, "%d");
267     expect_eq(_ismbstrail(mbstring, &mbstring[1]), -1, int, "%d");
268     expect_eq(_ismbstrail(mbstring, &mbstring[2]), FALSE, int, "%d");
269     expect_eq(_ismbstrail(mbstring, &mbstring[3]), -1, int, "%d");
270     expect_eq(_ismbstrail(mbstring, &mbstring[4]), FALSE, int, "%d");
271     expect_eq(_ismbstrail(mbstring, &mbstring[5]), -1, int, "%d");
272     expect_eq(_ismbstrail(mbstring, &mbstring[6]), FALSE, int, "%d");
273     expect_eq(_ismbstrail(mbstring, &mbstring[7]), FALSE, int, "%d");
274     expect_eq(_ismbstrail(mbstring, &mbstring[8]), -1, int, "%d");
275
276     expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[0]), FALSE, int, "%d");
277     expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[1]), -1, int, "%d");
278     expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[2]), FALSE, int, "%d");
279     expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[3]), FALSE, int, "%d");
280     expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[4]), FALSE, int, "%d");
281     expect_eq(_ismbstrail(mbsonlylead, &mbsonlylead[5]), FALSE, int, "%d");
282
283     /* _mbsbtype */
284     expect_eq(_mbsbtype(mbstring, 0), _MBC_LEAD, int, "%d");
285     expect_eq(_mbsbtype(mbstring, 1), _MBC_TRAIL, int, "%d");
286     expect_eq(_mbsbtype(mbstring, 2), _MBC_LEAD, int, "%d");
287     expect_eq(_mbsbtype(mbstring, 3), _MBC_ILLEGAL, int, "%d");
288     expect_eq(_mbsbtype(mbstring, 4), _MBC_LEAD, int, "%d");
289     expect_eq(_mbsbtype(mbstring, 5), _MBC_TRAIL, int, "%d");
290     expect_eq(_mbsbtype(mbstring, 6), _MBC_SINGLE, int, "%d");
291     expect_eq(_mbsbtype(mbstring, 7), _MBC_LEAD, int, "%d");
292     expect_eq(_mbsbtype(mbstring, 8), _MBC_ILLEGAL, int, "%d");
293
294     expect_eq(_mbsbtype(mbsonlylead, 0), _MBC_LEAD, int, "%d");
295     expect_eq(_mbsbtype(mbsonlylead, 1), _MBC_ILLEGAL, int, "%d");
296     expect_eq(_mbsbtype(mbsonlylead, 2), _MBC_ILLEGAL, int, "%d");
297     expect_eq(_mbsbtype(mbsonlylead, 3), _MBC_ILLEGAL, int, "%d");
298     expect_eq(_mbsbtype(mbsonlylead, 4), _MBC_ILLEGAL, int, "%d");
299     expect_eq(_mbsbtype(mbsonlylead, 5), _MBC_ILLEGAL, int, "%d");
300
301     /* _mbsnextc */
302     expect_eq(_mbsnextc(mbstring), 0xb0b1, int, "%x");
303     expect_eq(_mbsnextc(&mbstring[2]), 0xb220, int, "%x");  /* lead + invalid tail */
304     expect_eq(_mbsnextc(&mbstring[3]), 0x20, int, "%x");    /* single char */
305
306     /* _mbclen/_mbslen */
307     expect_eq(_mbclen(mbstring), 2, int, "%d");
308     expect_eq(_mbclen(&mbstring[2]), 2, int, "%d");
309     expect_eq(_mbclen(&mbstring[3]), 1, int, "%d");
310     expect_eq(_mbslen(mbstring2), 4, int, "%d");
311     expect_eq(_mbslen(mbsonlylead), 0, int, "%d");          /* lead + NUL not counted as character */
312     expect_eq(_mbslen(mbstring), 4, int, "%d");             /* lead + invalid trail counted */
313
314     /* mbrlen */
315     if(!setlocale(LC_ALL, ".936") || !p_mbrlen) {
316         win_skip("mbrlen tests\n");
317     }else {
318         mbstate_t state = 0;
319         expect_eq(p_mbrlen((const char*)mbstring, 2, NULL), 2, int, "%d");
320         expect_eq(p_mbrlen((const char*)&mbstring[2], 2, NULL), 2, int, "%d");
321         expect_eq(p_mbrlen((const char*)&mbstring[3], 2, NULL), 1, int, "%d");
322         expect_eq(p_mbrlen((const char*)mbstring, 1, NULL), -2, int, "%d");
323         expect_eq(p_mbrlen((const char*)mbstring, 1, &state), -2, int, "%d");
324         ok(state == mbstring[0], "incorrect state value (%x)\n", state);
325         expect_eq(p_mbrlen((const char*)&mbstring[1], 1, &state), 2, int, "%d");
326     }
327
328     /* mbrtowc */
329     if(!setlocale(LC_ALL, ".936") || !p_mbrtowc) {
330         win_skip("mbrtowc tests\n");
331     }else {
332         mbstate_t state = 0;
333         wchar_t dst;
334         expect_eq(p_mbrtowc(&dst, (const char*)mbstring, 2, NULL), 2, int, "%d");
335         ok(dst == 0x6c28, "dst = %x, expected 0x6c28\n", dst);
336         expect_eq(p_mbrtowc(&dst, (const char*)mbstring+2, 2, NULL), 2, int, "%d");
337         ok(dst == 0x3f, "dst = %x, expected 0x3f\n", dst);
338         expect_eq(p_mbrtowc(&dst, (const char*)mbstring+3, 2, NULL), 1, int, "%d");
339         ok(dst == 0x20, "dst = %x, expected 0x20\n", dst);
340         expect_eq(p_mbrtowc(&dst, (const char*)mbstring, 1, NULL), -2, int, "%d");
341         ok(dst == 0, "dst = %x, expected 0\n", dst);
342         expect_eq(p_mbrtowc(&dst, (const char*)mbstring, 1, &state), -2, int, "%d");
343         ok(dst == 0, "dst = %x, expected 0\n", dst);
344         ok(state == mbstring[0], "incorrect state value (%x)\n", state);
345         expect_eq(p_mbrtowc(&dst, (const char*)mbstring+1, 1, &state), 2, int, "%d");
346         ok(dst == 0x6c28, "dst = %x, expected 0x6c28\n", dst);
347         ok(state == 0, "incorrect state value (%x)\n", state);
348     }
349     setlocale(LC_ALL, "C");
350
351     /* _mbccpy/_mbsncpy */
352     memset(buf, 0xff, sizeof(buf));
353     _mbccpy(buf, mbstring);
354     expect_bin(buf, "\xb0\xb1\xff", 3);
355
356     memset(buf, 0xff, sizeof(buf));
357     _mbsncpy(buf, mbstring, 1);
358     expect_bin(buf, "\xb0\xb1\xff", 3);
359     memset(buf, 0xff, sizeof(buf));
360     _mbsncpy(buf, mbstring, 2);
361     expect_bin(buf, "\xb0\xb1\xb2 \xff", 5);
362     memset(buf, 0xff, sizeof(buf));
363     _mbsncpy(buf, mbstring, 3);
364     expect_bin(buf, "\xb0\xb1\xb2 \xb3\xb4\xff", 7);
365     memset(buf, 0xff, sizeof(buf));
366     _mbsncpy(buf, mbstring, 4);
367     expect_bin(buf, "\xb0\xb1\xb2 \xb3\xb4 \xff", 8);
368     memset(buf, 0xff, sizeof(buf));
369     _mbsncpy(buf, mbstring, 5);
370     expect_bin(buf, "\xb0\xb1\xb2 \xb3\xb4 \0\0\xff", 10);
371     memset(buf, 0xff, sizeof(buf));
372     _mbsncpy(buf, mbsonlylead, 6);
373     expect_bin(buf, "\0\0\0\0\0\0\0\xff", 8);
374
375     memset(buf, 0xff, sizeof(buf));
376     _mbsnbcpy(buf, mbstring2, 2);
377     expect_bin(buf, "\xb0\xb1\xff", 3);
378     _mbsnbcpy(buf, mbstring2, 3);
379     expect_bin(buf, "\xb0\xb1\0\xff", 4);
380     _mbsnbcpy(buf, mbstring2, 4);
381     expect_bin(buf, "\xb0\xb1\xb2\xb3\xff", 5);
382     memset(buf, 0xff, sizeof(buf));
383     _mbsnbcpy(buf, mbsonlylead, 5);
384     expect_bin(buf, "\0\0\0\0\0\xff", 6);
385
386     /* _mbsinc/mbsdec */
387     step = _mbsinc(mbstring) - mbstring;
388     ok(step == 2, "_mbsinc adds %d (exp. 2)\n", step);
389     step = _mbsinc(&mbstring[2]) - &mbstring[2];  /* lead + invalid tail */
390     ok(step == 2, "_mbsinc adds %d (exp. 2)\n", step);
391
392     step = _mbsninc(mbsonlylead, 1) - mbsonlylead;
393     ok(step == 0, "_mbsninc adds %d (exp. 0)\n", step);
394     step = _mbsninc(mbsonlylead, 2) - mbsonlylead;  /* lead + NUL byte + lead + char */
395     ok(step == 0, "_mbsninc adds %d (exp. 0)\n", step);
396     step = _mbsninc(mbstring2, 0) - mbstring2;
397     ok(step == 0, "_mbsninc adds %d (exp. 2)\n", step);
398     step = _mbsninc(mbstring2, 1) - mbstring2;
399     ok(step == 2, "_mbsninc adds %d (exp. 2)\n", step);
400     step = _mbsninc(mbstring2, 2) - mbstring2;
401     ok(step == 4, "_mbsninc adds %d (exp. 4)\n", step);
402     step = _mbsninc(mbstring2, 3) - mbstring2;
403     ok(step == 5, "_mbsninc adds %d (exp. 5)\n", step);
404     step = _mbsninc(mbstring2, 4) - mbstring2;
405     ok(step == 7, "_mbsninc adds %d (exp. 7)\n", step);
406     step = _mbsninc(mbstring2, 5) - mbstring2;
407     ok(step == 7, "_mbsninc adds %d (exp. 7)\n", step);
408     step = _mbsninc(mbstring2, 17) - mbstring2;
409     ok(step == 7, "_mbsninc adds %d (exp. 7)\n", step);
410
411     /* functions that depend on locale codepage, not mbcp.
412      * we hope the current locale to be SBCS because setlocale(LC_ALL, ".1252") seems not to work yet
413      * (as of Wine 0.9.43)
414      */
415     GetCPInfo(GetACP(), &cp_info);
416     if (cp_info.MaxCharSize == 1)
417     {
418         expect_eq(mblen((char *)mbstring, 3), 1, int, "%x");
419         expect_eq(_mbstrlen((char *)mbstring2), 7, int, "%d");
420     }
421     else
422         skip("Current locale has double-byte charset - could lead to false positives\n");
423
424     _setmbcp(1361);
425     expect_eq(_ismbblead(0x80), 0, int, "%d");
426     todo_wine {
427       expect_eq(_ismbblead(0x81), 1, int, "%d");
428       expect_eq(_ismbblead(0x83), 1, int, "%d");
429     }
430     expect_eq(_ismbblead(0x84), 1, int, "%d");
431     expect_eq(_ismbblead(0xd3), 1, int, "%d");
432     expect_eq(_ismbblead(0xd7), 0, int, "%d");
433     todo_wine {
434       expect_eq(_ismbblead(0xd8), 1, int, "%d");
435     }
436     expect_eq(_ismbblead(0xd9), 1, int, "%d");
437
438     expect_eq(_ismbbtrail(0x30), 0, int, "%d");
439     expect_eq(_ismbbtrail(0x31), 1, int, "%d");
440     expect_eq(_ismbbtrail(0x7e), 1, int, "%d");
441     expect_eq(_ismbbtrail(0x7f), 0, int, "%d");
442     expect_eq(_ismbbtrail(0x80), 0, int, "%d");
443     expect_eq(_ismbbtrail(0x81), 1, int, "%d");
444     expect_eq(_ismbbtrail(0xfe), 1, int, "%d");
445     expect_eq(_ismbbtrail(0xff), 0, int, "%d");
446
447     _setmbcp(curr_mbcp);
448 }
449
450 static void test_mbsspn( void)
451 {
452     unsigned char str1[]="cabernet";
453     unsigned char str2[]="shiraz";
454     unsigned char set[]="abc";
455     unsigned char empty[]="";
456     int ret;
457     ret=_mbsspn( str1, set);
458     ok( ret==3, "_mbsspn returns %d should be 3\n", ret);
459     ret=_mbsspn( str2, set);
460     ok( ret==0, "_mbsspn returns %d should be 0\n", ret);
461     ret=_mbsspn( str1, empty);
462     ok( ret==0, "_mbsspn returns %d should be 0\n", ret);
463 }
464
465 static void test_mbsspnp( void)
466 {
467     unsigned char str1[]="cabernet";
468     unsigned char str2[]="shiraz";
469     unsigned char set[]="abc";
470     unsigned char empty[]="";
471     unsigned char full[]="abcenrt";
472     unsigned char* ret;
473     ret=_mbsspnp( str1, set);
474     ok( ret[0]=='e', "_mbsspnp returns %c should be e\n", ret[0]);
475     ret=_mbsspnp( str2, set);
476     ok( ret[0]=='s', "_mbsspnp returns %c should be s\n", ret[0]);
477     ret=_mbsspnp( str1, empty);
478     ok( ret[0]=='c', "_mbsspnp returns %c should be c\n", ret[0]);
479     ret=_mbsspnp( str1, full);
480     ok( ret==NULL, "_mbsspnp returns %p should be NULL\n", ret);
481 }
482
483 static void test_strdup(void)
484 {
485    char *str;
486    str = _strdup( 0 );
487    ok( str == 0, "strdup returns %s should be 0\n", str);
488    free( str );
489 }
490
491 static void test_strcpy_s(void)
492 {
493     char dest[8];
494     const char *small = "small";
495     const char *big = "atoolongstringforthislittledestination";
496     int ret;
497
498     if(!pstrcpy_s)
499     {
500         skip("strcpy_s not found\n");
501         return;
502     }
503
504     memset(dest, 'X', sizeof(dest));
505     ret = pstrcpy_s(dest, sizeof(dest), small);
506     ok(ret == 0, "Copying a string into a big enough destination returned %d, expected 0\n", ret);
507     ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 'l' &&
508        dest[4] == 'l' && dest[5] == '\0'&& dest[6] == 'X' && dest[7] == 'X',
509        "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
510        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
511
512     memset(dest, 'X', sizeof(dest));
513     ret = pstrcpy_s(dest, 0, big);
514     ok(ret == EINVAL, "Copying into a destination of size 0 returned %d, expected EINVAL\n", ret);
515     ok(dest[0] == 'X' && dest[1] == 'X' && dest[2] == 'X' && dest[3] == 'X' &&
516        dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
517        "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
518        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
519     ret = pstrcpy_s(dest, 0, NULL);
520     ok(ret == EINVAL, "Copying into a destination of size 0 returned %d, expected EINVAL\n", ret);
521     ok(dest[0] == 'X' && dest[1] == 'X' && dest[2] == 'X' && dest[3] == 'X' &&
522        dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
523        "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
524        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
525
526     memset(dest, 'X', sizeof(dest));
527     ret = pstrcpy_s(dest, sizeof(dest), big);
528     ok(ret == ERANGE, "Copying a big string in a small location returned %d, expected ERANGE\n", ret);
529     ok(dest[0] == '\0'&& dest[1] == 't' && dest[2] == 'o' && dest[3] == 'o' &&
530        dest[4] == 'l' && dest[5] == 'o' && dest[6] == 'n' && dest[7] == 'g',
531        "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
532        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
533
534     memset(dest, 'X', sizeof(dest));
535     ret = pstrcpy_s(dest, sizeof(dest), NULL);
536     ok(ret == EINVAL, "Copying from a NULL source string returned %d, expected EINVAL\n", ret);
537     ok(dest[0] == '\0'&& dest[1] == 'X' && dest[2] == 'X' && dest[3] == 'X' &&
538        dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
539        "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
540        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
541
542     ret = pstrcpy_s(NULL, sizeof(dest), small);
543     ok(ret == EINVAL, "Copying a big string a NULL dest returned %d, expected EINVAL\n", ret);
544 }
545
546 #define NUMELMS(array) (sizeof(array)/sizeof((array)[0]))
547
548 #define okchars(dst, b0, b1, b2, b3, b4, b5, b6, b7) \
549     ok(dst[0] == b0 && dst[1] == b1 && dst[2] == b2 && dst[3] == b3 && \
550        dst[4] == b4 && dst[5] == b5 && dst[6] == b6 && dst[7] == b7, \
551        "Bad result: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",\
552        dst[0], dst[1], dst[2], dst[3], dst[4], dst[5], dst[6], dst[7])
553
554 static void test_memcpy_s(void)
555 {
556     static char dest[8];
557     static const char tiny[] = {'T',0,'I','N','Y',0};
558     static const char big[] = {'a','t','o','o','l','o','n','g','s','t','r','i','n','g',0};
559     int ret;
560     if (!p_memcpy_s) {
561         skip("memcpy_s not found\n");
562         return;
563     }
564
565     if (p_set_invalid_parameter_handler)
566         ok(p_set_invalid_parameter_handler(test_invalid_parameter_handler) == NULL,
567             "Invalid parameter handler was already set\n");
568
569     /* Normal */
570     memset(dest, 'X', sizeof(dest));
571     ret = p_memcpy_s(dest, NUMELMS(dest), tiny, NUMELMS(tiny));
572     ok(ret == 0, "Copying a buffer into a big enough destination returned %d, expected 0\n", ret);
573     okchars(dest, tiny[0], tiny[1], tiny[2], tiny[3], tiny[4], tiny[5], 'X', 'X');
574
575     /* Vary source size */
576     errno = 0xdeadbeef;
577     memset(dest, 'X', sizeof(dest));
578     ret = p_memcpy_s(dest, NUMELMS(dest), big, NUMELMS(big));
579     ok(ret == ERANGE, "Copying a big buffer to a small destination returned %d, expected ERANGE\n", ret);
580     ok(errno == ERANGE, "errno is %d, expected ERANGE\n", errno);
581     okchars(dest, 0, 0, 0, 0, 0, 0, 0, 0);
582
583     /* Replace source with NULL */
584     errno = 0xdeadbeef;
585     memset(dest, 'X', sizeof(dest));
586     ret = p_memcpy_s(dest, NUMELMS(dest), NULL, NUMELMS(tiny));
587     ok(ret == EINVAL, "Copying a NULL source buffer returned %d, expected EINVAL\n", ret);
588     ok(errno == EINVAL, "errno is %d, expected EINVAL\n", errno);
589     okchars(dest, 0, 0, 0, 0, 0, 0, 0, 0);
590
591     /* Vary dest size */
592     errno = 0xdeadbeef;
593     memset(dest, 'X', sizeof(dest));
594     ret = p_memcpy_s(dest, 0, tiny, NUMELMS(tiny));
595     ok(ret == ERANGE, "Copying into a destination of size 0 returned %d, expected ERANGE\n", ret);
596     ok(errno == ERANGE, "errno is %d, expected ERANGE\n", errno);
597     okchars(dest, 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X');
598
599     /* Replace dest with NULL */
600     errno = 0xdeadbeef;
601     ret = p_memcpy_s(NULL, NUMELMS(dest), tiny, NUMELMS(tiny));
602     ok(ret == EINVAL, "Copying a tiny buffer to a big NULL destination returned %d, expected EINVAL\n", ret);
603     ok(errno == EINVAL, "errno is %d, expected EINVAL\n", errno);
604
605     /* Combinations */
606     errno = 0xdeadbeef;
607     memset(dest, 'X', sizeof(dest));
608     ret = p_memcpy_s(dest, 0, NULL, NUMELMS(tiny));
609     ok(ret == EINVAL, "Copying a NULL buffer into a destination of size 0 returned %d, expected EINVAL\n", ret);
610     ok(errno == EINVAL, "errno is %d, expected EINVAL\n", errno);
611     okchars(dest, 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X');
612
613     if (p_set_invalid_parameter_handler)
614         ok(p_set_invalid_parameter_handler(NULL) == test_invalid_parameter_handler,
615             "Cannot reset invalid parameter handler\n");
616 }
617
618 static void test_memmove_s(void)
619 {
620     static char dest[8];
621     static const char tiny[] = {'T',0,'I','N','Y',0};
622     static const char big[] = {'a','t','o','o','l','o','n','g','s','t','r','i','n','g',0};
623     int ret;
624     if (!p_memmove_s) {
625         skip("memmove_s not found\n");
626         return;
627     }
628
629     if (p_set_invalid_parameter_handler)
630         ok(p_set_invalid_parameter_handler(test_invalid_parameter_handler) == NULL,
631             "Invalid parameter handler was already set\n");
632
633     /* Normal */
634     memset(dest, 'X', sizeof(dest));
635     ret = p_memmove_s(dest, NUMELMS(dest), tiny, NUMELMS(tiny));
636     ok(ret == 0, "Moving a buffer into a big enough destination returned %d, expected 0\n", ret);
637     okchars(dest, tiny[0], tiny[1], tiny[2], tiny[3], tiny[4], tiny[5], 'X', 'X');
638
639     /* Overlapping */
640     memcpy(dest, big, sizeof(dest));
641     ret = p_memmove_s(dest+1, NUMELMS(dest)-1, dest, NUMELMS(dest)-1);
642     ok(ret == 0, "Moving a buffer up one char returned %d, expected 0\n", ret);
643     okchars(dest, big[0], big[0], big[1], big[2], big[3], big[4], big[5], big[6]);
644
645     /* Vary source size */
646     errno = 0xdeadbeef;
647     memset(dest, 'X', sizeof(dest));
648     ret = p_memmove_s(dest, NUMELMS(dest), big, NUMELMS(big));
649     ok(ret == ERANGE, "Moving a big buffer to a small destination returned %d, expected ERANGE\n", ret);
650     ok(errno == ERANGE, "errno is %d, expected ERANGE\n", errno);
651     okchars(dest, 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X');
652
653     /* Replace source with NULL */
654     errno = 0xdeadbeef;
655     memset(dest, 'X', sizeof(dest));
656     ret = p_memmove_s(dest, NUMELMS(dest), NULL, NUMELMS(tiny));
657     ok(ret == EINVAL, "Moving a NULL source buffer returned %d, expected EINVAL\n", ret);
658     ok(errno == EINVAL, "errno is %d, expected EINVAL\n", errno);
659     okchars(dest, 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X');
660
661     /* Vary dest size */
662     errno = 0xdeadbeef;
663     memset(dest, 'X', sizeof(dest));
664     ret = p_memmove_s(dest, 0, tiny, NUMELMS(tiny));
665     ok(ret == ERANGE, "Moving into a destination of size 0 returned %d, expected ERANGE\n", ret);
666     ok(errno == ERANGE, "errno is %d, expected ERANGE\n", errno);
667     okchars(dest, 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X');
668
669     /* Replace dest with NULL */
670     errno = 0xdeadbeef;
671     ret = p_memmove_s(NULL, NUMELMS(dest), tiny, NUMELMS(tiny));
672     ok(ret == EINVAL, "Moving a tiny buffer to a big NULL destination returned %d, expected EINVAL\n", ret);
673     ok(errno == EINVAL, "errno is %d, expected EINVAL\n", errno);
674
675     /* Combinations */
676     errno = 0xdeadbeef;
677     memset(dest, 'X', sizeof(dest));
678     ret = p_memmove_s(dest, 0, NULL, NUMELMS(tiny));
679     ok(ret == EINVAL, "Moving a NULL buffer into a destination of size 0 returned %d, expected EINVAL\n", ret);
680     ok(errno == EINVAL, "errno is %d, expected EINVAL\n", errno);
681     okchars(dest, 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X');
682
683     if (p_set_invalid_parameter_handler)
684         ok(p_set_invalid_parameter_handler(NULL) == test_invalid_parameter_handler,
685             "Cannot reset invalid parameter handler\n");
686 }
687
688 static void test_strcat_s(void)
689 {
690     char dest[8];
691     const char *small = "sma";
692     int ret;
693
694     if(!pstrcat_s)
695     {
696         skip("strcat_s not found\n");
697         return;
698     }
699
700     memset(dest, 'X', sizeof(dest));
701     dest[0] = '\0';
702     ret = pstrcat_s(dest, sizeof(dest), small);
703     ok(ret == 0, "strcat_s: Copying a string into a big enough destination returned %d, expected 0\n", ret);
704     ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == '\0'&&
705        dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
706        "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
707        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
708     ret = pstrcat_s(dest, sizeof(dest), small);
709     ok(ret == 0, "strcat_s: Attaching a string to a big enough destination returned %d, expected 0\n", ret);
710     ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 's' &&
711        dest[4] == 'm' && dest[5] == 'a' && dest[6] == '\0'&& dest[7] == 'X',
712        "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
713        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
714
715     ret = pstrcat_s(dest, sizeof(dest), small);
716     ok(ret == ERANGE, "strcat_s: Attaching a string to a filled up destination returned %d, expected ERANGE\n", ret);
717     ok(dest[0] == '\0'&& dest[1] == 'm' && dest[2] == 'a' && dest[3] == 's' &&
718        dest[4] == 'm' && dest[5] == 'a' && dest[6] == 's' && dest[7] == 'm',
719        "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
720        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
721
722     memset(dest, 'X', sizeof(dest));
723     dest[0] = 'a';
724     dest[1] = '\0';
725
726     ret = pstrcat_s(dest, 0, small);
727     ok(ret == EINVAL, "strcat_s: Source len = 0 returned %d, expected EINVAL\n", ret);
728     ok(dest[0] == 'a' && 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(dest, 0, NULL);
734     ok(ret == EINVAL, "strcat_s: len = 0 and src = NULL returned %d, expected EINVAL\n", ret);
735     ok(dest[0] == 'a' && dest[1] == '\0'&& dest[2] == 'X' && dest[3] == 'X' &&
736        dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
737        "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
738        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
739
740     ret = pstrcat_s(dest, sizeof(dest), NULL);
741     ok(ret == EINVAL, "strcat_s:  Sourcing from NULL returned %d, expected EINVAL\n", ret);
742     ok(dest[0] == '\0'&& dest[1] == '\0'&& dest[2] == 'X' && dest[3] == 'X' &&
743        dest[4] == 'X' && dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
744        "Unexpected return data from strcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
745        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
746
747     ret = pstrcat_s(NULL, sizeof(dest), small);
748     ok(ret == EINVAL, "strcat_s: Writing to a NULL string returned %d, expected EINVAL\n", ret);
749 }
750
751 static void test__mbsnbcpy_s(void)
752 {
753     unsigned char dest[8];
754     const unsigned char big[] = "atoolongstringforthislittledestination";
755     const unsigned char small[] = "small";
756     int ret;
757
758     if(!p_mbsnbcpy_s)
759     {
760         skip("_mbsnbcpy_s not found\n");
761         return;
762     }
763
764     memset(dest, 'X', sizeof(dest));
765     ret = p_mbsnbcpy_s(dest, sizeof(dest), small, sizeof(small));
766     ok(ret == 0, "_mbsnbcpy_s: Copying a string into a big enough destination returned %d, expected 0\n", ret);
767     ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 'l' &&
768        dest[4] == 'l' && dest[5] == '\0'&& dest[6] == 'X' && dest[7] == 'X',
769        "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
770        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
771
772     /* WTF? */
773     memset(dest, 'X', sizeof(dest));
774     ret = p_mbsnbcpy_s(dest, sizeof(dest) - 2, big, sizeof(small));
775     ok(ret == ERANGE, "_mbsnbcpy_s: Copying a too long string returned %d, expected ERANGE\n", ret);
776     ok(dest[0] == '\0'&& dest[1] == 't' && dest[2] == 'o' && dest[3] == 'o' &&
777        dest[4] == 'l' && dest[5] == 'o' && dest[6] == 'X' && dest[7] == 'X',
778        "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
779        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
780
781     memset(dest, 'X', sizeof(dest));
782     ret = p_mbsnbcpy_s(dest, sizeof(dest) - 2, big, 4);
783     ok(ret == 0, "_mbsnbcpy_s: Copying a too long string with a count cap returned %d, expected 0\n", ret);
784     ok(dest[0] == 'a' && dest[1] == 't' && dest[2] == 'o' && dest[3] == 'o' &&
785        dest[4] == '\0'&& dest[5] == 'X' && dest[6] == 'X' && dest[7] == 'X',
786        "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
787        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
788
789     memset(dest, 'X', sizeof(dest));
790     ret = p_mbsnbcpy_s(dest, sizeof(dest) - 2, small, sizeof(small) + 10);
791     ok(ret == 0, "_mbsnbcpy_s: Copying more data than the source string len returned %d, expected 0\n", ret);
792     ok(dest[0] == 's' && dest[1] == 'm' && dest[2] == 'a' && dest[3] == 'l' &&
793        dest[4] == 'l' && dest[5] == '\0'&& dest[6] == 'X' && dest[7] == 'X',
794        "Unexpected return data from _mbsnbcpy_s: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
795        dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
796 }
797
798 static void test_wcscpy_s(void)
799 {
800     static const WCHAR szLongText[] = { 'T','h','i','s','A','L','o','n','g','s','t','r','i','n','g',0 };
801     static WCHAR szDest[18];
802     static WCHAR szDestShort[8];
803     int ret;
804
805     if(!p_wcscpy_s)
806     {
807         win_skip("wcscpy_s not found\n");
808         return;
809     }
810
811     /* Test NULL Dest */
812     ret = p_wcscpy_s(NULL, 18, szLongText);
813     ok(ret == EINVAL, "p_wcscpy_s expect EINVAL got %d\n", ret);
814
815     /* Test NULL Source */
816     szDest[0] = 'A';
817     ret = p_wcscpy_s(szDest, 18, NULL);
818     ok(ret == EINVAL, "expected EINVAL got %d\n", ret);
819     ok(szDest[0] == 0, "szDest[0] not 0\n");
820
821     /* Test invalid size */
822     szDest[0] = 'A';
823     ret = p_wcscpy_s(szDest, 0, szLongText);
824     /* Later versions changed the return value for this case to EINVAL,
825      * and don't modify the result if the dest size is 0.
826      */
827     ok(ret == ERANGE || ret == EINVAL, "expected ERANGE/EINVAL got %d\n", ret);
828     ok(szDest[0] == 0 || ret == EINVAL, "szDest[0] not 0\n");
829
830     /* Copy same buffer size */
831     ret = p_wcscpy_s(szDest, 18, szLongText);
832     ok(ret == 0, "expected 0 got %d\n", ret);
833     ok(lstrcmpW(szDest, szLongText) == 0, "szDest != szLongText\n");
834
835     /* Copy smaller buffer size */
836     szDest[0] = 'A';
837     ret = p_wcscpy_s(szDestShort, 8, szLongText);
838     ok(ret == ERANGE || ret == EINVAL, "expected ERANGE/EINVAL got %d\n", ret);
839     ok(szDestShort[0] == 0, "szDestShort[0] not 0\n");
840
841     if(!p_wcsncpy_s)
842     {
843         win_skip("wcsncpy_s not found\n");
844         return;
845     }
846
847     ret = p_wcsncpy_s(NULL, 18, szLongText, sizeof(szLongText)/sizeof(WCHAR));
848     ok(ret == EINVAL, "p_wcsncpy_s expect EINVAL got %d\n", ret);
849
850     szDest[0] = 'A';
851     ret = p_wcsncpy_s(szDest, 18, NULL, 1);
852     ok(ret == EINVAL, "expected EINVAL got %d\n", ret);
853     ok(szDest[0] == 0, "szDest[0] not 0\n");
854
855     szDest[0] = 'A';
856     ret = p_wcsncpy_s(szDest, 18, NULL, 0);
857     ok(ret == 0, "expected ERROR_SUCCESS got %d\n", ret);
858     ok(szDest[0] == 0, "szDest[0] not 0\n");
859
860     szDest[0] = 'A';
861     ret = p_wcsncpy_s(szDest, 0, szLongText, sizeof(szLongText)/sizeof(WCHAR));
862     ok(ret == ERANGE || ret == EINVAL, "expected ERANGE/EINVAL got %d\n", ret);
863     ok(szDest[0] == 0 || ret == EINVAL, "szDest[0] not 0\n");
864
865     ret = p_wcsncpy_s(szDest, 18, szLongText, sizeof(szLongText)/sizeof(WCHAR));
866     ok(ret == 0, "expected 0 got %d\n", ret);
867     ok(lstrcmpW(szDest, szLongText) == 0, "szDest != szLongText\n");
868
869     szDest[0] = 'A';
870     ret = p_wcsncpy_s(szDestShort, 8, szLongText, sizeof(szLongText)/sizeof(WCHAR));
871     ok(ret == ERANGE || ret == EINVAL, "expected ERANGE/EINVAL got %d\n", ret);
872     ok(szDestShort[0] == 0, "szDestShort[0] not 0\n");
873
874     szDest[0] = 'A';
875     ret = p_wcsncpy_s(szDest, 5, szLongText, -1);
876     ok(ret == STRUNCATE, "expected STRUNCATE got %d\n", ret);
877     ok(szDest[4] == 0, "szDest[4] not 0\n");
878     ok(!memcmp(szDest, szLongText, 4*sizeof(WCHAR)), "szDest = %s\n", wine_dbgstr_w(szDest));
879
880     ret = p_wcsncpy_s(NULL, 0, (void*)0xdeadbeef, 0);
881     ok(ret == 0, "ret = %d\n", ret);
882
883     szDestShort[0] = '1';
884     szDestShort[1] = 0;
885     ret = p_wcsncpy_s(szDestShort+1, 4, szDestShort, -1);
886     ok(ret == STRUNCATE, "expected ERROR_SUCCESS got %d\n", ret);
887     ok(szDestShort[0]=='1' && szDestShort[1]=='1' && szDestShort[2]=='1' && szDestShort[3]=='1',
888             "szDestShort = %s\n", wine_dbgstr_w(szDestShort));
889 }
890
891 static void test__wcsupr_s(void)
892 {
893     static const WCHAR mixedString[] = {'M', 'i', 'X', 'e', 'D', 'l', 'o', 'w',
894                                         'e', 'r', 'U', 'P', 'P', 'E', 'R', 0};
895     static const WCHAR expectedString[] = {'M', 'I', 'X', 'E', 'D', 'L', 'O',
896                                            'W', 'E', 'R', 'U', 'P', 'P', 'E',
897                                            'R', 0};
898     WCHAR testBuffer[2*sizeof(mixedString)/sizeof(WCHAR)];
899     int ret;
900
901     if (!p_wcsupr_s)
902     {
903         win_skip("_wcsupr_s not found\n");
904         return;
905     }
906
907     /* Test NULL input string and invalid size. */
908     errno = EBADF;
909     ret = p_wcsupr_s(NULL, 0);
910     ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
911     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
912
913     /* Test NULL input string and valid size. */
914     errno = EBADF;
915     ret = p_wcsupr_s(NULL, sizeof(testBuffer)/sizeof(WCHAR));
916     ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
917     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
918
919     /* Test empty string with zero size. */
920     errno = EBADF;
921     testBuffer[0] = '\0';
922     ret = p_wcsupr_s(testBuffer, 0);
923     ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
924     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
925     ok(testBuffer[0] == '\0', "Expected the buffer to be unchanged\n");
926
927     /* Test empty string with size of one. */
928     testBuffer[0] = '\0';
929     ret = p_wcsupr_s(testBuffer, 1);
930     ok(ret == 0, "Expected _wcsupr_s to succeed, got %d\n", ret);
931     ok(testBuffer[0] == '\0', "Expected the buffer to be unchanged\n");
932
933     /* Test one-byte buffer with zero size. */
934     errno = EBADF;
935     testBuffer[0] = 'x';
936     ret = p_wcsupr_s(testBuffer, 0);
937     ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
938     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
939     ok(testBuffer[0] == '\0', "Expected the first buffer character to be null\n");
940
941     /* Test one-byte buffer with size of one. */
942     errno = EBADF;
943     testBuffer[0] = 'x';
944     ret = p_wcsupr_s(testBuffer, 1);
945     ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
946     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
947     ok(testBuffer[0] == '\0', "Expected the first buffer character to be null\n");
948
949     /* Test invalid size. */
950     wcscpy(testBuffer, mixedString);
951     errno = EBADF;
952     ret = p_wcsupr_s(testBuffer, 0);
953     ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
954     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
955     ok(testBuffer[0] == '\0', "Expected the first buffer character to be null\n");
956
957     /* Test normal string uppercasing. */
958     wcscpy(testBuffer, mixedString);
959     ret = p_wcsupr_s(testBuffer, sizeof(mixedString)/sizeof(WCHAR));
960     ok(ret == 0, "Expected _wcsupr_s to succeed, got %d\n", ret);
961     ok(!wcscmp(testBuffer, expectedString), "Expected the string to be fully upper-case\n");
962
963     /* Test uppercasing with a shorter buffer size count. */
964     wcscpy(testBuffer, mixedString);
965     errno = EBADF;
966     ret = p_wcsupr_s(testBuffer, sizeof(mixedString)/sizeof(WCHAR) - 1);
967     ok(ret == EINVAL, "Expected _wcsupr_s to fail with EINVAL, got %d\n", ret);
968     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
969     ok(testBuffer[0] == '\0', "Expected the first buffer character to be null\n");
970
971     /* Test uppercasing with a longer buffer size count. */
972     wcscpy(testBuffer, mixedString);
973     ret = p_wcsupr_s(testBuffer, sizeof(testBuffer)/sizeof(WCHAR));
974     ok(ret == 0, "Expected _wcsupr_s to succeed, got %d\n", ret);
975     ok(!wcscmp(testBuffer, expectedString), "Expected the string to be fully upper-case\n");
976 }
977
978 static void test__wcslwr_s(void)
979 {
980     static const WCHAR mixedString[] = {'M', 'i', 'X', 'e', 'D', 'l', 'o', 'w',
981                                         'e', 'r', 'U', 'P', 'P', 'E', 'R', 0};
982     static const WCHAR expectedString[] = {'m', 'i', 'x', 'e', 'd', 'l', 'o',
983                                            'w', 'e', 'r', 'u', 'p', 'p', 'e',
984                                            'r', 0};
985     WCHAR buffer[2*sizeof(mixedString)/sizeof(WCHAR)];
986     int ret;
987
988     if (!p_wcslwr_s)
989     {
990         win_skip("_wcslwr_s not found\n");
991         return;
992     }
993
994     /* Test NULL input string and invalid size. */
995     errno = EBADF;
996     ret = p_wcslwr_s(NULL, 0);
997     ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
998     ok(errno == EINVAL, "expected errno EINVAL, got %d\n", errno);
999
1000     /* Test NULL input string and valid size. */
1001     errno = EBADF;
1002     ret = p_wcslwr_s(NULL, sizeof(buffer)/sizeof(wchar_t));
1003     ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
1004     ok(errno == EINVAL, "expected errno EINVAL, got %d\n", errno);
1005
1006     /* Test empty string with zero size. */
1007     errno = EBADF;
1008     buffer[0] = 'a';
1009     ret = p_wcslwr_s(buffer, 0);
1010     ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
1011     ok(errno == EINVAL, "expected errno EINVAL, got %d\n", errno);
1012     ok(buffer[0] == 0, "expected empty string\n");
1013
1014     /* Test empty string with size of one. */
1015     buffer[0] = 0;
1016     ret = p_wcslwr_s(buffer, 1);
1017     ok(ret == 0, "got %d\n", ret);
1018     ok(buffer[0] == 0, "expected buffer to be unchanged\n");
1019
1020     /* Test one-byte buffer with zero size. */
1021     errno = EBADF;
1022     buffer[0] = 'x';
1023     ret = p_wcslwr_s(buffer, 0);
1024     ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
1025     ok(errno == EINVAL, "expected errno to be EINVAL, got %d\n", errno);
1026     ok(buffer[0] == '\0', "expected empty string\n");
1027
1028     /* Test one-byte buffer with size of one. */
1029     errno = EBADF;
1030     buffer[0] = 'x';
1031     ret = p_wcslwr_s(buffer, 1);
1032     ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
1033     ok(errno == EINVAL, "expected errno to be EINVAL, got %d\n", errno);
1034     ok(buffer[0] == '\0', "expected empty string\n");
1035
1036     /* Test invalid size. */
1037     wcscpy(buffer, mixedString);
1038     errno = EBADF;
1039     ret = p_wcslwr_s(buffer, 0);
1040     ok(ret == EINVAL, "Expected EINVAL, got %d\n", ret);
1041     ok(errno == EINVAL, "expected errno to be EINVAL, got %d\n", errno);
1042     ok(buffer[0] == '\0', "expected empty string\n");
1043
1044     /* Test normal string uppercasing. */
1045     wcscpy(buffer, mixedString);
1046     ret = p_wcslwr_s(buffer, sizeof(mixedString)/sizeof(WCHAR));
1047     ok(ret == 0, "expected 0, got %d\n", ret);
1048     ok(!wcscmp(buffer, expectedString), "expected lowercase\n");
1049
1050     /* Test uppercasing with a shorter buffer size count. */
1051     wcscpy(buffer, mixedString);
1052     errno = EBADF;
1053     ret = p_wcslwr_s(buffer, sizeof(mixedString)/sizeof(WCHAR) - 1);
1054     ok(ret == EINVAL, "expected EINVAL, got %d\n", ret);
1055     ok(errno == EINVAL, "expected errno to be EINVAL, got %d\n", errno);
1056     ok(buffer[0] == '\0', "expected empty string\n");
1057
1058     /* Test uppercasing with a longer buffer size count. */
1059     wcscpy(buffer, mixedString);
1060     ret = p_wcslwr_s(buffer, sizeof(buffer)/sizeof(WCHAR));
1061     ok(ret == 0, "expected 0, got %d\n", ret);
1062     ok(!wcscmp(buffer, expectedString), "expected lowercase\n");
1063 }
1064
1065 static void test_mbcjisjms(void)
1066 {
1067     /* List of value-pairs to test. The test assumes the last pair to be {0, ..} */
1068     unsigned int jisjms[][2] = { {0x2020, 0}, {0x2021, 0}, {0x2120, 0}, {0x2121, 0x8140},
1069                                  {0x7f7f, 0}, {0x7f7e, 0}, {0x7e7f, 0}, {0x7e7e, 0xeffc},
1070                                  {0x255f, 0x837e}, {0x2560, 0x8380}, {0x2561, 0x8381},
1071                                  {0x2121FFFF, 0}, {0x2223, 0x81a1}, {0x237e, 0x829e}, {0, 0}};
1072     int cp[] = { 932, 936, 939, 950, 1361, _MB_CP_SBCS };
1073     unsigned int i, j;
1074     int prev_cp = _getmbcp();
1075
1076     for (i = 0; i < sizeof(cp)/sizeof(cp[0]); i++)
1077     {
1078         _setmbcp(cp[i]);
1079         for (j = 0; jisjms[j][0] != 0; j++)
1080         {
1081             unsigned int ret, exp;
1082             ret = _mbcjistojms(jisjms[j][0]);
1083             exp = (cp[i] == 932) ? jisjms[j][1] : jisjms[j][0];
1084             ok(ret == exp, "Expected 0x%x, got 0x%x (0x%x, codepage=%d)\n",
1085                exp, ret, jisjms[j][0], cp[i]);
1086         }
1087     }
1088     _setmbcp(prev_cp);
1089 }
1090
1091 static void test_mbcjmsjis(void)
1092 {
1093     /* List of value-pairs to test. The test assumes the last pair to be {0, ..} */
1094     unsigned int jmsjis[][2] = { {0x80fc, 0}, {0x813f, 0}, {0x8140, 0x2121},
1095                                  {0x817e, 0x215f}, {0x817f, 0}, {0x8180, 0x2160},
1096                                  {0x819e, 0x217e}, {0x819f, 0x2221}, {0x81fc, 0x227e},
1097                                  {0x81fd, 0}, {0x9ffc, 0x5e7e}, {0x9ffd, 0},
1098                                  {0xa040, 0}, {0xdffc, 0}, {0xe040, 0x5f21},
1099                                  {0xeffc, 0x7e7e}, {0xf040, 0}, {0x21, 0}, {0, 0}};
1100     int cp[] = { 932, 936, 939, 950, 1361, _MB_CP_SBCS };
1101     unsigned int i, j;
1102     int prev_cp = _getmbcp();
1103
1104     for (i = 0; i < sizeof(cp)/sizeof(cp[0]); i++)
1105     {
1106         _setmbcp(cp[i]);
1107         for (j = 0; jmsjis[j][0] != 0; j++)
1108         {
1109             unsigned int ret, exp;
1110             ret = _mbcjmstojis(jmsjis[j][0]);
1111             exp = (cp[i] == 932) ? jmsjis[j][1] : jmsjis[j][0];
1112             ok(ret == exp, "Expected 0x%x, got 0x%x (0x%x, codepage=%d)\n",
1113                exp, ret, jmsjis[j][0], cp[i]);
1114         }
1115     }
1116     _setmbcp(prev_cp);
1117 }
1118
1119 static void test_mbbtombc(void)
1120 {
1121     static const unsigned int mbbmbc[][2] = {
1122         {0x1f, 0x1f}, {0x20, 0x8140}, {0x39, 0x8258}, {0x40, 0x8197},
1123         {0x41, 0x8260}, {0x5e, 0x814f}, {0x7e, 0x8150}, {0x7f, 0x7f},
1124         {0x80, 0x80}, {0x81, 0x81}, {0xa0, 0xa0}, {0xa7, 0x8340},
1125         {0xb0, 0x815b}, {0xd1, 0x8380}, {0xff, 0xff}, {0,0}};
1126     int cp[] = { 932, 936, 939, 950, 1361, _MB_CP_SBCS };
1127     int i, j;
1128     int prev_cp = _getmbcp();
1129
1130     for (i = 0; i < sizeof(cp)/sizeof(cp[0]); i++)
1131     {
1132         _setmbcp(cp[i]);
1133         for (j = 0; mbbmbc[j][0] != 0; j++)
1134         {
1135             unsigned int exp, ret;
1136             ret = _mbbtombc(mbbmbc[j][0]);
1137             exp = (cp[i] == 932) ? mbbmbc[j][1] : mbbmbc[j][0];
1138             ok(ret == exp, "Expected 0x%x, got 0x%x (0x%x, codepage %d)\n",
1139                exp, ret, mbbmbc[j][0], cp[i]);
1140         }
1141     }
1142     _setmbcp(prev_cp);
1143 }
1144
1145 static void test_mbctombb(void)
1146 {
1147     static const unsigned int mbcmbb_932[][2] = {
1148         {0x829e, 0x829e}, {0x829f, 0xa7}, {0x82f1, 0xdd}, {0x82f2, 0x82f2},
1149         {0x833f, 0x833f}, {0x8340, 0xa7}, {0x837e, 0xd0}, {0x837f, 0x837f},
1150         {0x8380, 0xd1}, {0x8396, 0xb9}, {0x8397, 0x8397}, {0x813f, 0x813f},
1151         {0x8140, 0x20}, {0x814c, 0x814c}, {0x814f, 0x5e}, {0x8197, 0x40},
1152         {0x8198, 0x8198}, {0x8258, 0x39}, {0x8259, 0x8259}, {0x825f, 0x825f},
1153         {0x8260, 0x41}, {0x82f1, 0xdd}, {0x82f2, 0x82f2}, {0,0}};
1154     unsigned int exp, ret, i;
1155     unsigned int prev_cp = _getmbcp();
1156
1157     _setmbcp(932);
1158     for (i = 0; mbcmbb_932[i][0] != 0; i++)
1159     {
1160         ret = _mbctombb(mbcmbb_932[i][0]);
1161         exp = mbcmbb_932[i][1];
1162         ok(ret == exp, "Expected 0x%x, got 0x%x\n", exp, ret);
1163     }
1164     _setmbcp(prev_cp);
1165 }
1166
1167 static void test_ismbclegal(void) {
1168     unsigned int prev_cp = _getmbcp();
1169     int ret, exp, err;
1170     unsigned int i;
1171
1172     _setmbcp(932); /* Japanese */
1173     err = 0;
1174     for(i = 0; i < 0x10000; i++) {
1175         ret = _ismbclegal(i);
1176         exp = ((HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0x9F) ||
1177                (HIBYTE(i) >= 0xE0 && HIBYTE(i) <= 0xFC)) &&
1178               ((LOBYTE(i) >= 0x40 && LOBYTE(i) <= 0x7E) ||
1179                (LOBYTE(i) >= 0x80 && LOBYTE(i) <= 0xFC));
1180         if(ret != exp) {
1181             err = 1;
1182             break;
1183         }
1184     }
1185     ok(!err, "_ismbclegal (932) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
1186     _setmbcp(936); /* Chinese (GBK) */
1187     err = 0;
1188     for(i = 0; i < 0x10000; i++) {
1189         ret = _ismbclegal(i);
1190         exp = HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0xFE &&
1191               LOBYTE(i) >= 0x40 && LOBYTE(i) <= 0xFE;
1192         if(ret != exp) {
1193             err = 1;
1194             break;
1195         }
1196     }
1197     ok(!err, "_ismbclegal (936) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
1198     _setmbcp(949); /* Korean */
1199     err = 0;
1200     for(i = 0; i < 0x10000; i++) {
1201         ret = _ismbclegal(i);
1202         exp = HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0xFE &&
1203               LOBYTE(i) >= 0x41 && LOBYTE(i) <= 0xFE;
1204         if(ret != exp) {
1205             err = 1;
1206             break;
1207         }
1208     }
1209     ok(!err, "_ismbclegal (949) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
1210     _setmbcp(950); /* Chinese (Big5) */
1211     err = 0;
1212     for(i = 0; i < 0x10000; i++) {
1213         ret = _ismbclegal(i);
1214         exp = HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0xFE &&
1215             ((LOBYTE(i) >= 0x40 && LOBYTE(i) <= 0x7E) ||
1216              (LOBYTE(i) >= 0xA1 && LOBYTE(i) <= 0xFE));
1217         if(ret != exp) {
1218             err = 1;
1219             break;
1220         }
1221     }
1222     ok(!err, "_ismbclegal (950) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
1223     _setmbcp(1361); /* Korean (Johab) */
1224     err = 0;
1225     for(i = 0; i < 0x10000; i++) {
1226         ret = _ismbclegal(i);
1227         exp = ((HIBYTE(i) >= 0x81 && HIBYTE(i) <= 0xD3) ||
1228                (HIBYTE(i) >= 0xD8 && HIBYTE(i) <= 0xF9)) &&
1229               ((LOBYTE(i) >= 0x31 && LOBYTE(i) <= 0x7E) ||
1230                (LOBYTE(i) >= 0x81 && LOBYTE(i) <= 0xFE)) &&
1231                 HIBYTE(i) != 0xDF;
1232         if(ret != exp) {
1233             err = 1;
1234             break;
1235         }
1236     }
1237     todo_wine ok(!err, "_ismbclegal (1361) : Expected 0x%x, got 0x%x (0x%x)\n", exp, ret, i);
1238
1239     _setmbcp(prev_cp);
1240 }
1241
1242 static const struct {
1243     const char* string;
1244     const char* delimiter;
1245     int exp_offsetret1; /* returned offset from string after first call to strtok()
1246                            -1 means NULL  */
1247     int exp_offsetret2; /* returned offset from string after second call to strtok()
1248                            -1 means NULL  */
1249     int exp_offsetret3; /* returned offset from string after third call to strtok()
1250                            -1 means NULL  */
1251 } testcases_strtok[] = {
1252     { "red cabernet", " ", 0, 4, -1 },
1253     { "sparkling white riesling", " ", 0, 10, 16 },
1254     { " pale cream sherry", "e ", 1, 6, 9 },
1255     /* end mark */
1256     { 0}
1257 };
1258
1259 static void test_strtok(void)
1260 {
1261     int i;
1262     char *strret;
1263     char teststr[100];
1264     for( i = 0; testcases_strtok[i].string; i++){
1265         strcpy( teststr, testcases_strtok[i].string);
1266         strret = strtok( teststr, testcases_strtok[i].delimiter);
1267         ok( (int)(strret - teststr) ==  testcases_strtok[i].exp_offsetret1 ||
1268                 (!strret && testcases_strtok[i].exp_offsetret1 == -1),
1269                 "string (%p) \'%s\' return %p\n",
1270                 teststr, testcases_strtok[i].string, strret);
1271         if( !strret) continue;
1272         strret = strtok( NULL, testcases_strtok[i].delimiter);
1273         ok( (int)(strret - teststr) ==  testcases_strtok[i].exp_offsetret2 ||
1274                 (!strret && testcases_strtok[i].exp_offsetret2 == -1),
1275                 "second call string (%p) \'%s\' return %p\n",
1276                 teststr, testcases_strtok[i].string, strret);
1277         if( !strret) continue;
1278         strret = strtok( NULL, testcases_strtok[i].delimiter);
1279         ok( (int)(strret - teststr) ==  testcases_strtok[i].exp_offsetret3 ||
1280                 (!strret && testcases_strtok[i].exp_offsetret3 == -1),
1281                 "third call string (%p) \'%s\' return %p\n",
1282                 teststr, testcases_strtok[i].string, strret);
1283     }
1284 }
1285
1286 static void test_strtol(void)
1287 {
1288     char* e;
1289     LONG l;
1290     ULONG ul;
1291
1292     /* errno is only set in case of error, so reset errno to EBADF to check for errno modification */
1293     /* errno is modified on W2K8+ */
1294     errno = EBADF;
1295     l = strtol("-1234", &e, 0);
1296     ok(l==-1234, "wrong value %d\n", l);
1297     ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
1298     errno = EBADF;
1299     ul = strtoul("1234", &e, 0);
1300     ok(ul==1234, "wrong value %u\n", ul);
1301     ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
1302
1303     errno = EBADF;
1304     l = strtol("2147483647L", &e, 0);
1305     ok(l==2147483647, "wrong value %d\n", l);
1306     ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
1307     errno = EBADF;
1308     l = strtol("-2147483648L", &e, 0);
1309     ok(l==-2147483647L - 1, "wrong value %d\n", l);
1310     ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
1311     errno = EBADF;
1312     ul = strtoul("4294967295UL", &e, 0);
1313     ok(ul==4294967295ul, "wrong value %u\n", ul);
1314     ok(errno == EBADF || broken(errno == 0), "wrong errno %d\n", errno);
1315
1316     errno = 0;
1317     l = strtol("9223372036854775807L", &e, 0);
1318     ok(l==2147483647, "wrong value %d\n", l);
1319     ok(errno == ERANGE, "wrong errno %d\n", errno);
1320     errno = 0;
1321     ul = strtoul("9223372036854775807L", &e, 0);
1322     ok(ul==4294967295ul, "wrong value %u\n", ul);
1323     ok(errno == ERANGE, "wrong errno %d\n", errno);
1324 }
1325
1326 static void test_strnlen(void)
1327 {
1328     static const char str[] = "string";
1329     size_t res;
1330
1331     if(!p_strnlen) {
1332         win_skip("strnlen not found\n");
1333         return;
1334     }
1335
1336     res = p_strnlen(str, 20);
1337     ok(res == 6, "Returned length = %d\n", (int)res);
1338
1339     res = p_strnlen(str, 3);
1340     ok(res == 3, "Returned length = %d\n", (int)res);
1341
1342     res = p_strnlen(NULL, 0);
1343     ok(res == 0, "Returned length = %d\n", (int)res);
1344 }
1345
1346 static void test__strtoi64(void)
1347 {
1348     static const char no1[] = "31923";
1349     static const char no2[] = "-213312";
1350     static const char no3[] = "12aa";
1351     static const char no4[] = "abc12";
1352     static const char overflow[] = "99999999999999999999";
1353     static const char neg_overflow[] = "-99999999999999999999";
1354     static const char hex[] = "0x123";
1355     static const char oct[] = "000123";
1356     static const char blanks[] = "        12 212.31";
1357
1358     __int64 res;
1359     unsigned __int64 ures;
1360     char *endpos;
1361
1362     if(!p_strtoi64 || !p_strtoui64) {
1363         win_skip("_strtoi64 or _strtoui64 not found\n");
1364         return;
1365     }
1366
1367     errno = 0xdeadbeef;
1368     res = p_strtoi64(no1, NULL, 10);
1369     ok(res == 31923, "res != 31923\n");
1370     res = p_strtoi64(no2, NULL, 10);
1371     ok(res == -213312, "res != -213312\n");
1372     res = p_strtoi64(no3, NULL, 10);
1373     ok(res == 12, "res != 12\n");
1374     res = p_strtoi64(no4, &endpos, 10);
1375     ok(res == 0, "res != 0\n");
1376     ok(endpos == no4, "Scanning was not stopped on first character\n");
1377     res = p_strtoi64(hex, &endpos, 10);
1378     ok(res == 0, "res != 0\n");
1379     ok(endpos == hex+1, "Incorrect endpos (%p-%p)\n", hex, endpos);
1380     res = p_strtoi64(oct, &endpos, 10);
1381     ok(res == 123, "res != 123\n");
1382     ok(endpos == oct+strlen(oct), "Incorrect endpos (%p-%p)\n", oct, endpos);
1383     res = p_strtoi64(blanks, &endpos, 10);
1384     ok(res == 12, "res != 12\n");
1385     ok(endpos == blanks+10, "Incorrect endpos (%p-%p)\n", blanks, endpos);
1386     ok(errno == 0xdeadbeef, "errno = %x\n", errno);
1387
1388     errno = 0xdeadbeef;
1389     res = p_strtoi64(overflow, &endpos, 10);
1390     ok(res == _I64_MAX, "res != _I64_MAX\n");
1391     ok(endpos == overflow+strlen(overflow), "Incorrect endpos (%p-%p)\n", overflow, endpos);
1392     ok(errno == ERANGE, "errno = %x\n", errno);
1393
1394     errno = 0xdeadbeef;
1395     res = p_strtoi64(neg_overflow, &endpos, 10);
1396     ok(res == _I64_MIN, "res != _I64_MIN\n");
1397     ok(endpos == neg_overflow+strlen(neg_overflow), "Incorrect endpos (%p-%p)\n", neg_overflow, endpos);
1398     ok(errno == ERANGE, "errno = %x\n", errno);
1399
1400     errno = 0xdeadbeef;
1401     res = p_strtoi64(no1, &endpos, 16);
1402     ok(res == 203043, "res != 203043\n");
1403     ok(endpos == no1+strlen(no1), "Incorrect endpos (%p-%p)\n", no1, endpos);
1404     res = p_strtoi64(no2, &endpos, 16);
1405     ok(res == -2175762, "res != -2175762\n");
1406     ok(endpos == no2+strlen(no2), "Incorrect endpos (%p-%p)\n", no2, endpos);
1407     res = p_strtoi64(no3, &endpos, 16);
1408     ok(res == 4778, "res != 4778\n");
1409     ok(endpos == no3+strlen(no3), "Incorrect endpos (%p-%p)\n", no3, endpos);
1410     res = p_strtoi64(no4, &endpos, 16);
1411     ok(res == 703506, "res != 703506\n");
1412     ok(endpos == no4+strlen(no4), "Incorrect endpos (%p-%p)\n", no4, endpos);
1413     res = p_strtoi64(hex, &endpos, 16);
1414     ok(res == 291, "res != 291\n");
1415     ok(endpos == hex+strlen(hex), "Incorrect endpos (%p-%p)\n", hex, endpos);
1416     res = p_strtoi64(oct, &endpos, 16);
1417     ok(res == 291, "res != 291\n");
1418     ok(endpos == oct+strlen(oct), "Incorrect endpos (%p-%p)\n", oct, endpos);
1419     res = p_strtoi64(blanks, &endpos, 16);
1420     ok(res == 18, "res != 18\n");
1421     ok(endpos == blanks+10, "Incorrect endpos (%p-%p)\n", blanks, endpos);
1422     ok(errno == 0xdeadbeef, "errno = %x\n", errno);
1423
1424     errno = 0xdeadbeef;
1425     res = p_strtoi64(hex, &endpos, 36);
1426     ok(res == 1541019, "res != 1541019\n");
1427     ok(endpos == hex+strlen(hex), "Incorrect endpos (%p-%p)\n", hex, endpos);
1428     ok(errno == 0xdeadbeef, "errno = %x\n", errno);
1429
1430     errno = 0xdeadbeef;
1431     res = p_strtoi64(no1, &endpos, 0);
1432     ok(res == 31923, "res != 31923\n");
1433     ok(endpos == no1+strlen(no1), "Incorrect endpos (%p-%p)\n", no1, endpos);
1434     res = p_strtoi64(no2, &endpos, 0);
1435     ok(res == -213312, "res != -213312\n");
1436     ok(endpos == no2+strlen(no2), "Incorrect endpos (%p-%p)\n", no2, endpos);
1437     res = p_strtoi64(no3, &endpos, 10);
1438     ok(res == 12, "res != 12\n");
1439     ok(endpos == no3+2, "Incorrect endpos (%p-%p)\n", no3, endpos);
1440     res = p_strtoi64(no4, &endpos, 10);
1441     ok(res == 0, "res != 0\n");
1442     ok(endpos == no4, "Incorrect endpos (%p-%p)\n", no4, endpos);
1443     res = p_strtoi64(hex, &endpos, 10);
1444     ok(res == 0, "res != 0\n");
1445     ok(endpos == hex+1, "Incorrect endpos (%p-%p)\n", hex, endpos);
1446     res = p_strtoi64(oct, &endpos, 10);
1447     ok(res == 123, "res != 123\n");
1448     ok(endpos == oct+strlen(oct), "Incorrect endpos (%p-%p)\n", oct, endpos);
1449     res = p_strtoi64(blanks, &endpos, 10);
1450     ok(res == 12, "res != 12\n");
1451     ok(endpos == blanks+10, "Incorrect endpos (%p-%p)\n", blanks, endpos);
1452     ok(errno == 0xdeadbeef, "errno = %x\n", errno);
1453
1454     errno = 0xdeadbeef;
1455     ures = p_strtoui64(no1, &endpos, 0);
1456     ok(ures == 31923, "ures != 31923\n");
1457     ok(endpos == no1+strlen(no1), "Incorrect endpos (%p-%p)\n", no1, endpos);
1458     ures = p_strtoui64(no2, &endpos, 0);
1459     ok(ures == -213312, "ures != -213312\n");
1460     ok(endpos == no2+strlen(no2), "Incorrect endpos (%p-%p)\n", no2, endpos);
1461     ures = p_strtoui64(no3, &endpos, 10);
1462     ok(ures == 12, "ures != 12\n");
1463     ok(endpos == no3+2, "Incorrect endpos (%p-%p)\n", no3, endpos);
1464     ures = p_strtoui64(no4, &endpos, 10);
1465     ok(ures == 0, "ures != 0\n");
1466     ok(endpos == no4, "Incorrect endpos (%p-%p)\n", no4, endpos);
1467     ures = p_strtoui64(hex, &endpos, 10);
1468     ok(ures == 0, "ures != 0\n");
1469     ok(endpos == hex+1, "Incorrect endpos (%p-%p)\n", hex, endpos);
1470     ures = p_strtoui64(oct, &endpos, 10);
1471     ok(ures == 123, "ures != 123\n");
1472     ok(endpos == oct+strlen(oct), "Incorrect endpos (%p-%p)\n", oct, endpos);
1473     ures = p_strtoui64(blanks, &endpos, 10);
1474     ok(ures == 12, "ures != 12\n");
1475     ok(endpos == blanks+10, "Incorrect endpos (%p-%p)\n", blanks, endpos);
1476     ok(errno == 0xdeadbeef, "errno = %x\n", errno);
1477
1478     errno = 0xdeadbeef;
1479     ures = p_strtoui64(overflow, &endpos, 10);
1480     ok(ures == _UI64_MAX, "ures != _UI64_MAX\n");
1481     ok(endpos == overflow+strlen(overflow), "Incorrect endpos (%p-%p)\n", overflow, endpos);
1482     ok(errno == ERANGE, "errno = %x\n", errno);
1483
1484     errno = 0xdeadbeef;
1485     ures = p_strtoui64(neg_overflow, &endpos, 10);
1486     ok(ures == 1, "ures != 1\n");
1487     ok(endpos == neg_overflow+strlen(neg_overflow), "Incorrect endpos (%p-%p)\n", neg_overflow, endpos);
1488     ok(errno == ERANGE, "errno = %x\n", errno);
1489 }
1490
1491 static inline BOOL almost_equal(double d1, double d2) {
1492     if(d1-d2>-1e-30 && d1-d2<1e-30)
1493         return TRUE;
1494     return FALSE;
1495 }
1496
1497 static void test__strtod(void)
1498 {
1499     const char double1[] = "12.1";
1500     const char double2[] = "-13.721";
1501     const char double3[] = "INF";
1502     const char double4[] = ".21e12";
1503     const char double5[] = "214353e-3";
1504     const char overflow[] = "1d9999999999999999999";
1505     const char white_chars[] = "  d10";
1506
1507     char *end;
1508     double d;
1509
1510     d = strtod(double1, &end);
1511     ok(almost_equal(d, 12.1), "d = %lf\n", d);
1512     ok(end == double1+4, "incorrect end (%d)\n", (int)(end-double1));
1513
1514     d = strtod(double2, &end);
1515     ok(almost_equal(d, -13.721), "d = %lf\n", d);
1516     ok(end == double2+7, "incorrect end (%d)\n", (int)(end-double2));
1517
1518     d = strtod(double3, &end);
1519     ok(almost_equal(d, 0), "d = %lf\n", d);
1520     ok(end == double3, "incorrect end (%d)\n", (int)(end-double3));
1521
1522     d = strtod(double4, &end);
1523     ok(almost_equal(d, 210000000000.0), "d = %lf\n", d);
1524     ok(end == double4+6, "incorrect end (%d)\n", (int)(end-double4));
1525
1526     d = strtod(double5, &end);
1527     ok(almost_equal(d, 214.353), "d = %lf\n", d);
1528     ok(end == double5+9, "incorrect end (%d)\n", (int)(end-double5));
1529
1530     d = strtod("12.1d2", NULL);
1531     ok(almost_equal(d, 12.1e2), "d = %lf\n", d);
1532
1533     d = strtod(white_chars, &end);
1534     ok(almost_equal(d, 0), "d = %lf\n", d);
1535     ok(end == white_chars, "incorrect end (%d)\n", (int)(end-white_chars));
1536
1537     /* Set locale with non '.' decimal point (',') */
1538     if(!setlocale(LC_ALL, "Polish")) {
1539         win_skip("system with limited locales\n");
1540         return;
1541     }
1542
1543     d = strtod("12.1", NULL);
1544     ok(almost_equal(d, 12.0), "d = %lf\n", d);
1545
1546     d = strtod("12,1", NULL);
1547     ok(almost_equal(d, 12.1), "d = %lf\n", d);
1548
1549     setlocale(LC_ALL, "C");
1550
1551     /* Precision tests */
1552     d = strtod("0.1", NULL);
1553     ok(almost_equal(d, 0.1), "d = %lf\n", d);
1554     d = strtod("-0.1", NULL);
1555     ok(almost_equal(d, -0.1), "d = %lf\n", d);
1556     d = strtod("0.1281832188491894198128921", NULL);
1557     ok(almost_equal(d, 0.1281832188491894198128921), "d = %lf\n", d);
1558     d = strtod("0.82181281288121", NULL);
1559     ok(almost_equal(d, 0.82181281288121), "d = %lf\n", d);
1560     d = strtod("21921922352523587651128218821", NULL);
1561     ok(almost_equal(d, 21921922352523587651128218821.0), "d = %lf\n", d);
1562     d = strtod("0.1d238", NULL);
1563     ok(almost_equal(d, 0.1e238L), "d = %lf\n", d);
1564     d = strtod("0.1D-4736", NULL);
1565     ok(almost_equal(d, 0.1e-4736L), "d = %lf\n", d);
1566
1567     errno = 0xdeadbeef;
1568     strtod(overflow, &end);
1569     ok(errno == ERANGE, "errno = %x\n", errno);
1570     ok(end == overflow+21, "incorrect end (%d)\n", (int)(end-overflow));
1571
1572     errno = 0xdeadbeef;
1573     strtod("-1d309", NULL);
1574     ok(errno == ERANGE, "errno = %x\n", errno);
1575 }
1576
1577 static void test_mbstowcs(void)
1578 {
1579     static const wchar_t wSimple[] = { 't','e','x','t',0 };
1580     static const wchar_t wHiragana[] = { 0x3042,0x3043,0 };
1581     static const char mSimple[] = "text";
1582     static const char mHiragana[] = { 0x82,0xa0,0x82,0xa1,0 };
1583
1584     const wchar_t *pwstr;
1585     wchar_t wOut[6];
1586     char mOut[6];
1587     size_t ret;
1588     int err;
1589     const char *pmbstr;
1590     mbstate_t state;
1591
1592     wOut[4] = '!'; wOut[5] = '\0';
1593     mOut[4] = '!'; mOut[5] = '\0';
1594
1595     ret = mbstowcs(NULL, mSimple, 0);
1596     ok(ret == 4, "mbstowcs did not return 4\n");
1597
1598     ret = mbstowcs(wOut, mSimple, 4);
1599     ok(ret == 4, "mbstowcs did not return 4\n");
1600     ok(!memcmp(wOut, wSimple, 4*sizeof(wchar_t)), "wOut = %s\n", wine_dbgstr_w(wOut));
1601     ok(wOut[4] == '!', "wOut[4] != \'!\'\n");
1602
1603     ret = wcstombs(NULL, wSimple, 0);
1604     ok(ret == 4, "wcstombs did not return 4\n");
1605
1606     ret = wcstombs(mOut, wSimple, 6);
1607     ok(ret == 4, "wcstombs did not return 4\n");
1608     ok(!memcmp(mOut, mSimple, 5*sizeof(char)), "mOut = %s\n", mOut);
1609
1610     ret = wcstombs(mOut, wSimple, 2);
1611     ok(ret == 2, "wcstombs did not return 2\n");
1612     ok(!memcmp(mOut, mSimple, 5*sizeof(char)), "mOut = %s\n", mOut);
1613
1614     if(!setlocale(LC_ALL, "Japanese_Japan.932")) {
1615         win_skip("Japanese_Japan.932 locale not available\n");
1616         return;
1617     }
1618
1619     ret = mbstowcs(wOut, mHiragana, 6);
1620     ok(ret == 2, "mbstowcs did not return 2\n");
1621     ok(!memcmp(wOut, wHiragana, sizeof(wHiragana)), "wOut = %s\n", wine_dbgstr_w(wOut));
1622
1623     ret = wcstombs(mOut, wHiragana, 6);
1624     ok(ret == 4, "wcstombs did not return 4\n");
1625     ok(!memcmp(mOut, mHiragana, sizeof(mHiragana)), "mOut = %s\n", mOut);
1626
1627     if(!pmbstowcs_s || !pwcstombs_s) {
1628         setlocale(LC_ALL, "C");
1629         win_skip("mbstowcs_s or wcstombs_s not available\n");
1630         return;
1631     }
1632
1633     err = pmbstowcs_s(&ret, wOut, 6, mSimple, _TRUNCATE);
1634     ok(err == 0, "err = %d\n", err);
1635     ok(ret == 5, "mbstowcs_s did not return 5\n");
1636     ok(!memcmp(wOut, wSimple, sizeof(wSimple)), "wOut = %s\n", wine_dbgstr_w(wOut));
1637
1638     err = pmbstowcs_s(&ret, wOut, 6, mHiragana, _TRUNCATE);
1639     ok(err == 0, "err = %d\n", err);
1640     ok(ret == 3, "mbstowcs_s did not return 3\n");
1641     ok(!memcmp(wOut, wHiragana, sizeof(wHiragana)), "wOut = %s\n", wine_dbgstr_w(wOut));
1642
1643     err = pmbstowcs_s(&ret, NULL, 0, mHiragana, 1);
1644     ok(err == 0, "err = %d\n", err);
1645     ok(ret == 3, "mbstowcs_s did not return 3\n");
1646
1647     err = pwcstombs_s(&ret, mOut, 6, wSimple, _TRUNCATE);
1648     ok(err == 0, "err = %d\n", err);
1649     ok(ret == 5, "wcstombs_s did not return 5\n");
1650     ok(!memcmp(mOut, mSimple, sizeof(mSimple)), "mOut = %s\n", mOut);
1651
1652     err = pwcstombs_s(&ret, mOut, 6, wHiragana, _TRUNCATE);
1653     ok(err == 0, "err = %d\n", err);
1654     ok(ret == 5, "wcstombs_s did not return 5\n");
1655     ok(!memcmp(mOut, mHiragana, sizeof(mHiragana)), "mOut = %s\n", mOut);
1656
1657     err = pwcstombs_s(&ret, NULL, 0, wHiragana, 1);
1658     ok(err == 0, "err = %d\n", err);
1659     ok(ret == 5, "wcstombs_s did not return 5\n");
1660
1661     if(!pwcsrtombs) {
1662         setlocale(LC_ALL, "C");
1663         win_skip("wcsrtombs not available\n");
1664         return;
1665     }
1666
1667     pwstr = wSimple;
1668     err = -3;
1669     ret = pwcsrtombs(mOut, &pwstr, 4, &err);
1670     ok(ret == 4, "wcsrtombs did not return 4\n");
1671     ok(err == 0, "err = %d\n", err);
1672     ok(pwstr == wSimple+4, "pwstr = %p (wszSimple = %p)\n", pwstr, wSimple);
1673     ok(!memcmp(mOut, mSimple, ret), "mOut = %s\n", mOut);
1674
1675     pwstr = wSimple;
1676     ret = pwcsrtombs(mOut, &pwstr, 5, NULL);
1677     ok(ret == 4, "wcsrtombs did not return 4\n");
1678     ok(pwstr == NULL, "pwstr != NULL\n");
1679     ok(!memcmp(mOut, mSimple, sizeof(mSimple)), "mOut = %s\n", mOut);
1680
1681     if(!p_mbsrtowcs) {
1682         setlocale(LC_ALL, "C");
1683         win_skip("mbsrtowcs not available\n");
1684         return;
1685     }
1686
1687     pmbstr = mHiragana;
1688     ret = p_mbsrtowcs(wOut, &pmbstr, 6, NULL);
1689     ok(ret == 2, "mbsrtowcs did not return 2\n");
1690     ok(!memcmp(wOut, wHiragana, sizeof(wHiragana)), "wOut = %s\n", wine_dbgstr_w(wOut));
1691     ok(!pmbstr, "pmbstr != NULL\n");
1692
1693     state = mHiragana[0];
1694     pmbstr = mHiragana+1;
1695     ret = p_mbsrtowcs(wOut, &pmbstr, 6, &state);
1696     ok(ret == 2, "mbsrtowcs did not return 2\n");
1697     ok(wOut[0] == 0x3042, "wOut[0] = %x\n", wOut[0]);
1698     ok(wOut[1] == 0xff61, "wOut[1] = %x\n", wOut[1]);
1699     ok(wOut[2] == 0, "wOut[2] = %x\n", wOut[2]);
1700     ok(!pmbstr, "pmbstr != NULL\n");
1701
1702     if (p_set_invalid_parameter_handler)
1703         ok(p_set_invalid_parameter_handler(test_invalid_parameter_handler) == NULL,
1704                 "Invalid parameter handler was already set\n");
1705     errno = EBADF;
1706     ret = p_mbsrtowcs(wOut, NULL, 6, &state);
1707     ok(ret == -1, "mbsrtowcs did not return -1\n");
1708     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1709     if (p_set_invalid_parameter_handler)
1710         ok(p_set_invalid_parameter_handler(NULL) == test_invalid_parameter_handler,
1711                 "Cannot reset invalid parameter handler\n");
1712
1713     setlocale(LC_ALL, "C");
1714 }
1715
1716 static void test_gcvt(void)
1717 {
1718     char buf[1024], *res;
1719     errno_t err;
1720
1721     if(!p_gcvt_s) {
1722         win_skip("Skipping _gcvt tests\n");
1723         return;
1724     }
1725
1726     errno = 0;
1727     res = _gcvt(1.2, -1, buf);
1728     ok(res == NULL, "res != NULL\n");
1729     ok(errno == ERANGE, "errno = %d\n", errno);
1730
1731     errno = 0;
1732     res = _gcvt(1.2, 5, NULL);
1733     ok(res == NULL, "res != NULL\n");
1734     ok(errno == EINVAL, "errno = %d\n", errno);
1735
1736     res = gcvt(1.2, 5, buf);
1737     ok(res == buf, "res != buf\n");
1738     ok(!strcmp(buf, "1.2"), "buf = %s\n", buf);
1739
1740     buf[0] = 'x';
1741     err = p_gcvt_s(buf, 5, 1.2, 10);
1742     ok(err == ERANGE, "err = %d\n", err);
1743     ok(buf[0] == '\0', "buf[0] = %c\n", buf[0]);
1744
1745     buf[0] = 'x';
1746     err = p_gcvt_s(buf, 4, 123456, 2);
1747     ok(err == ERANGE, "err = %d\n", err);
1748     ok(buf[0] == '\0', "buf[0] = %c\n", buf[0]);
1749 }
1750
1751 static void test__itoa_s(void)
1752 {
1753     errno_t ret;
1754     char buffer[33];
1755
1756     if (!p_itoa_s)
1757     {
1758         win_skip("Skipping _itoa_s tests\n");
1759         return;
1760     }
1761
1762     if (p_set_invalid_parameter_handler)
1763         ok(p_set_invalid_parameter_handler(test_invalid_parameter_handler) == NULL,
1764                 "Invalid parameter handler was already set\n");
1765
1766     errno = EBADF;
1767     ret = p_itoa_s(0, NULL, 0, 0);
1768     ok(ret == EINVAL, "Expected _itoa_s to return EINVAL, got %d\n", ret);
1769     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1770
1771     memset(buffer, 'X', sizeof(buffer));
1772     errno = EBADF;
1773     ret = p_itoa_s(0, buffer, 0, 0);
1774     ok(ret == EINVAL, "Expected _itoa_s to return EINVAL, got %d\n", ret);
1775     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1776     ok(buffer[0] == 'X', "Expected the output buffer to be untouched\n");
1777
1778     memset(buffer, 'X', sizeof(buffer));
1779     errno = EBADF;
1780     ret = p_itoa_s(0, buffer, sizeof(buffer), 0);
1781     ok(ret == EINVAL, "Expected _itoa_s to return EINVAL, got %d\n", ret);
1782     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1783     ok(buffer[0] == '\0', "Expected the output buffer to be null terminated\n");
1784
1785     memset(buffer, 'X', sizeof(buffer));
1786     errno = EBADF;
1787     ret = p_itoa_s(0, buffer, sizeof(buffer), 64);
1788     ok(ret == EINVAL, "Expected _itoa_s to return EINVAL, got %d\n", ret);
1789     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1790     ok(buffer[0] == '\0', "Expected the output buffer to be null terminated\n");
1791
1792     memset(buffer, 'X', sizeof(buffer));
1793     errno = EBADF;
1794     ret = p_itoa_s(12345678, buffer, 4, 10);
1795     ok(ret == ERANGE, "Expected _itoa_s to return ERANGE, got %d\n", ret);
1796     ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
1797     ok(!memcmp(buffer, "\000765", 4),
1798        "Expected the output buffer to be null terminated with truncated output\n");
1799
1800     memset(buffer, 'X', sizeof(buffer));
1801     errno = EBADF;
1802     ret = p_itoa_s(12345678, buffer, 8, 10);
1803     ok(ret == ERANGE, "Expected _itoa_s to return ERANGE, got %d\n", ret);
1804     ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
1805     ok(!memcmp(buffer, "\0007654321", 8),
1806        "Expected the output buffer to be null terminated with truncated output\n");
1807
1808     memset(buffer, 'X', sizeof(buffer));
1809     errno = EBADF;
1810     ret = p_itoa_s(-12345678, buffer, 9, 10);
1811     ok(ret == ERANGE, "Expected _itoa_s to return ERANGE, got %d\n", ret);
1812     ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
1813     ok(!memcmp(buffer, "\00087654321", 9),
1814        "Expected the output buffer to be null terminated with truncated output\n");
1815
1816     ret = p_itoa_s(12345678, buffer, 9, 10);
1817     ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
1818     ok(!strcmp(buffer, "12345678"),
1819        "Expected output buffer string to be \"12345678\", got \"%s\"\n",
1820        buffer);
1821
1822     ret = p_itoa_s(43690, buffer, sizeof(buffer), 2);
1823     ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
1824     ok(!strcmp(buffer, "1010101010101010"),
1825        "Expected output buffer string to be \"1010101010101010\", got \"%s\"\n",
1826        buffer);
1827
1828     ret = p_itoa_s(1092009, buffer, sizeof(buffer), 36);
1829     ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
1830     ok(!strcmp(buffer, "nell"),
1831        "Expected output buffer string to be \"nell\", got \"%s\"\n",
1832        buffer);
1833
1834     ret = p_itoa_s(5704, buffer, sizeof(buffer), 18);
1835     ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
1836     ok(!strcmp(buffer, "hag"),
1837        "Expected output buffer string to be \"hag\", got \"%s\"\n",
1838        buffer);
1839
1840     ret = p_itoa_s(-12345678, buffer, sizeof(buffer), 10);
1841     ok(ret == 0, "Expected _itoa_s to return 0, got %d\n", ret);
1842     ok(!strcmp(buffer, "-12345678"),
1843        "Expected output buffer string to be \"-12345678\", got \"%s\"\n",
1844        buffer);
1845     if (p_set_invalid_parameter_handler)
1846         ok(p_set_invalid_parameter_handler(NULL) == test_invalid_parameter_handler,
1847                 "Cannot reset invalid parameter handler\n");
1848 }
1849
1850 static void test__strlwr_s(void)
1851 {
1852     errno_t ret;
1853     char buffer[20];
1854
1855     if (!p_strlwr_s)
1856     {
1857         win_skip("Skipping _strlwr_s tests\n");
1858         return;
1859     }
1860
1861     errno = EBADF;
1862     ret = p_strlwr_s(NULL, 0);
1863     ok(ret == EINVAL, "Expected _strlwr_s to return EINVAL, got %d\n", ret);
1864     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1865
1866     errno = EBADF;
1867     ret = p_strlwr_s(NULL, sizeof(buffer));
1868     ok(ret == EINVAL, "Expected _strlwr_s to return EINVAL, got %d\n", ret);
1869     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1870
1871     errno = EBADF;
1872     ret = p_strlwr_s(buffer, 0);
1873     ok(ret == EINVAL, "Expected _strlwr_s to return EINVAL, got %d\n", ret);
1874     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1875
1876     strcpy(buffer, "GoRrIsTeR");
1877     errno = EBADF;
1878     ret = p_strlwr_s(buffer, 5);
1879     ok(ret == EINVAL, "Expected _strlwr_s to return EINVAL, got %d\n", ret);
1880     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1881     ok(!memcmp(buffer, "\0oRrIsTeR", sizeof("\0oRrIsTeR")),
1882        "Expected the output buffer to be \"\\0oRrIsTeR\"\n");
1883
1884     strcpy(buffer, "GoRrIsTeR");
1885     errno = EBADF;
1886     ret = p_strlwr_s(buffer, sizeof("GoRrIsTeR") - 1);
1887     ok(ret == EINVAL, "Expected _strlwr_s to return EINVAL, got %d\n", ret);
1888     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1889     ok(!memcmp(buffer, "\0oRrIsTeR", sizeof("\0oRrIsTeR")),
1890        "Expected the output buffer to be \"\\0oRrIsTeR\"\n");
1891
1892     strcpy(buffer, "GoRrIsTeR");
1893     ret = p_strlwr_s(buffer, sizeof("GoRrIsTeR"));
1894     ok(ret == 0, "Expected _strlwr_s to return 0, got %d\n", ret);
1895     ok(!strcmp(buffer, "gorrister"),
1896        "Expected the output buffer to be \"gorrister\", got \"%s\"\n",
1897        buffer);
1898
1899     memcpy(buffer, "GoRrIsTeR\0ELLEN", sizeof("GoRrIsTeR\0ELLEN"));
1900     ret = p_strlwr_s(buffer, sizeof(buffer));
1901     ok(ret == 0, "Expected _strlwr_s to return 0, got %d\n", ret);
1902     ok(!memcmp(buffer, "gorrister\0ELLEN", sizeof("gorrister\0ELLEN")),
1903        "Expected the output buffer to be \"gorrister\\0ELLEN\", got \"%s\"\n",
1904        buffer);
1905 }
1906
1907 static void test_wcsncat_s(void)
1908 {
1909     static wchar_t abcW[] = {'a','b','c',0};
1910     int ret;
1911     wchar_t dst[4];
1912     wchar_t src[4];
1913
1914     if (!p_wcsncat_s)
1915     {
1916         win_skip("skipping wcsncat_s tests\n");
1917         return;
1918     }
1919
1920     if (p_set_invalid_parameter_handler)
1921         ok(p_set_invalid_parameter_handler(test_invalid_parameter_handler) == NULL,
1922                 "Invalid parameter handler was already set\n");
1923
1924     memcpy(src, abcW, sizeof(abcW));
1925     dst[0] = 0;
1926     ret = p_wcsncat_s(NULL, 4, src, 4);
1927     ok(ret == EINVAL, "err = %d\n", ret);
1928     ret = p_wcsncat_s(dst, 0, src, 4);
1929     ok(ret == EINVAL, "err = %d\n", ret);
1930     ret = p_wcsncat_s(dst, 0, src, _TRUNCATE);
1931     ok(ret == EINVAL, "err = %d\n", ret);
1932     ret = p_wcsncat_s(dst, 4, NULL, 0);
1933     ok(ret == 0, "err = %d\n", ret);
1934
1935     dst[0] = 0;
1936     ret = p_wcsncat_s(dst, 2, src, 4);
1937     ok(ret == ERANGE, "err = %d\n", ret);
1938
1939     dst[0] = 0;
1940     ret = p_wcsncat_s(dst, 2, src, _TRUNCATE);
1941     ok(ret == STRUNCATE, "err = %d\n", ret);
1942     ok(dst[0] == 'a' && dst[1] == 0, "dst is %s\n", wine_dbgstr_w(dst));
1943
1944     memcpy(dst, abcW, sizeof(abcW));
1945     dst[3] = 'd';
1946     ret = p_wcsncat_s(dst, 4, src, 4);
1947     ok(ret == EINVAL, "err = %d\n", ret);
1948
1949     if (p_set_invalid_parameter_handler)
1950         ok(p_set_invalid_parameter_handler(NULL) == test_invalid_parameter_handler,
1951                 "Cannot reset invalid parameter handler\n");
1952 }
1953
1954 static void test__mbsnbcat_s(void)
1955 {
1956     unsigned char dest[16];
1957     const unsigned char first[] = "dinosaur";
1958     const unsigned char second[] = "duck";
1959     int ret;
1960
1961     if (!p_mbsnbcat_s)
1962     {
1963         win_skip("Skipping _mbsnbcat_s tests\n");
1964         return;
1965     }
1966
1967     /* Test invalid arguments. */
1968     ret = p_mbsnbcat_s(NULL, 0, NULL, 0);
1969     ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
1970
1971     errno = EBADF;
1972     ret = p_mbsnbcat_s(NULL, 10, NULL, 0);
1973     ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
1974     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1975
1976     errno = EBADF;
1977     ret = p_mbsnbcat_s(NULL, 0, NULL, 10);
1978     ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
1979     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1980
1981     memset(dest, 'X', sizeof(dest));
1982     errno = EBADF;
1983     ret = p_mbsnbcat_s(dest, 0, NULL, 0);
1984     ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
1985     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1986     ok(dest[0] == 'X', "Expected the output buffer to be untouched\n");
1987
1988     memset(dest, 'X', sizeof(dest));
1989     errno = EBADF;
1990     ret = p_mbsnbcat_s(dest, 0, second, 0);
1991     ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
1992     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
1993     ok(dest[0] == 'X', "Expected the output buffer to be untouched\n");
1994
1995     memset(dest, 'X', sizeof(dest));
1996     errno = EBADF;
1997     ret = p_mbsnbcat_s(dest, sizeof(dest), NULL, 0);
1998     ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
1999     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2000     ok(dest[0] == '\0', "Expected the output buffer to be null terminated\n");
2001
2002     memset(dest, 'X', sizeof(dest));
2003     errno = EBADF;
2004     ret = p_mbsnbcat_s(dest, sizeof(dest), NULL, 10);
2005     ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
2006     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2007     ok(dest[0] == '\0', "Expected the output buffer to be null terminated\n");
2008
2009     memset(dest, 'X', sizeof(dest));
2010     dest[0] = '\0';
2011     ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second));
2012     ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
2013     ok(!memcmp(dest, second, sizeof(second)),
2014        "Expected the output buffer string to be \"duck\"\n");
2015
2016     /* Test source truncation behavior. */
2017     memset(dest, 'X', sizeof(dest));
2018     memcpy(dest, first, sizeof(first));
2019     ret = p_mbsnbcat_s(dest, sizeof(dest), second, 0);
2020     ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
2021     ok(!memcmp(dest, first, sizeof(first)),
2022        "Expected the output buffer string to be \"dinosaur\"\n");
2023
2024     memset(dest, 'X', sizeof(dest));
2025     memcpy(dest, first, sizeof(first));
2026     ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second));
2027     ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
2028     ok(!memcmp(dest, "dinosaurduck", sizeof("dinosaurduck")),
2029        "Expected the output buffer string to be \"dinosaurduck\"\n");
2030
2031     memset(dest, 'X', sizeof(dest));
2032     memcpy(dest, first, sizeof(first));
2033     ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second) + 1);
2034     ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
2035     ok(!memcmp(dest, "dinosaurduck", sizeof("dinosaurduck")),
2036        "Expected the output buffer string to be \"dinosaurduck\"\n");
2037
2038     memset(dest, 'X', sizeof(dest));
2039     memcpy(dest, first, sizeof(first));
2040     ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second) - 1);
2041     ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
2042     ok(!memcmp(dest, "dinosaurduck", sizeof("dinosaurduck")),
2043        "Expected the output buffer string to be \"dinosaurduck\"\n");
2044
2045     memset(dest, 'X', sizeof(dest));
2046     memcpy(dest, first, sizeof(first));
2047     ret = p_mbsnbcat_s(dest, sizeof(dest), second, sizeof(second) - 2);
2048     ok(ret == 0, "Expected _mbsnbcat_s to return 0, got %d\n", ret);
2049     ok(!memcmp(dest, "dinosaurduc", sizeof("dinosaurduc")),
2050        "Expected the output buffer string to be \"dinosaurduc\"\n");
2051
2052     /* Test destination truncation behavior. */
2053     memset(dest, 'X', sizeof(dest));
2054     memcpy(dest, first, sizeof(first));
2055     errno = EBADF;
2056     ret = p_mbsnbcat_s(dest, sizeof(first) - 1, second, sizeof(second));
2057     ok(ret == EINVAL, "Expected _mbsnbcat_s to return EINVAL, got %d\n", ret);
2058     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2059     ok(!memcmp(dest, "\0inosaur", sizeof("\0inosaur") - 1),
2060        "Expected the output buffer string to be \"\\0inosaur\" without ending null terminator\n");
2061
2062     memset(dest, 'X', sizeof(dest));
2063     memcpy(dest, first, sizeof(first));
2064     errno = EBADF;
2065     ret = p_mbsnbcat_s(dest, sizeof(first), second, sizeof(second));
2066     ok(ret == ERANGE, "Expected _mbsnbcat_s to return ERANGE, got %d\n", ret);
2067     ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
2068     ok(!memcmp(dest, "\0inosaurd", sizeof("\0inosaurd") - 1),
2069        "Expected the output buffer string to be \"\\0inosaurd\" without ending null terminator\n");
2070
2071     memset(dest, 'X', sizeof(dest));
2072     memcpy(dest, first, sizeof(first));
2073     errno = EBADF;
2074     ret = p_mbsnbcat_s(dest, sizeof(first) + 1, second, sizeof(second));
2075     ok(ret == ERANGE, "Expected _mbsnbcat_s to return ERANGE, got %d\n", ret);
2076     ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
2077     ok(!memcmp(dest, "\0inosaurdu", sizeof("\0inosaurdu") - 1),
2078        "Expected the output buffer string to be \"\\0inosaurdu\" without ending null terminator\n");
2079 }
2080
2081 static void test__mbsupr_s(void)
2082 {
2083     errno_t ret;
2084     unsigned char buffer[20];
2085
2086     if (!p_mbsupr_s)
2087     {
2088         win_skip("Skipping _mbsupr_s tests\n");
2089         return;
2090     }
2091
2092     errno = EBADF;
2093     ret = p_mbsupr_s(NULL, 0);
2094     ok(ret == 0, "Expected _mbsupr_s to return 0, got %d\n", ret);
2095
2096     errno = EBADF;
2097     ret = p_mbsupr_s(NULL, sizeof(buffer));
2098     ok(ret == EINVAL, "Expected _mbsupr_s to return EINVAL, got %d\n", ret);
2099     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2100
2101     errno = EBADF;
2102     ret = p_mbsupr_s(buffer, 0);
2103     ok(ret == EINVAL, "Expected _mbsupr_s to return EINVAL, got %d\n", ret);
2104     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2105
2106     memcpy(buffer, "abcdefgh", sizeof("abcdefgh"));
2107     errno = EBADF;
2108     ret = p_mbsupr_s(buffer, sizeof("abcdefgh"));
2109     ok(ret == 0, "Expected _mbsupr_s to return 0, got %d\n", ret);
2110     ok(!memcmp(buffer, "ABCDEFGH", sizeof("ABCDEFGH")),
2111        "Expected the output buffer to be \"ABCDEFGH\", got \"%s\"\n",
2112        buffer);
2113
2114     memcpy(buffer, "abcdefgh", sizeof("abcdefgh"));
2115     errno = EBADF;
2116     ret = p_mbsupr_s(buffer, sizeof(buffer));
2117     ok(ret == 0, "Expected _mbsupr_s to return 0, got %d\n", ret);
2118     ok(!memcmp(buffer, "ABCDEFGH", sizeof("ABCDEFGH")),
2119        "Expected the output buffer to be \"ABCDEFGH\", got \"%s\"\n",
2120        buffer);
2121
2122     memcpy(buffer, "abcdefgh", sizeof("abcdefgh"));
2123     errno = EBADF;
2124     ret = p_mbsupr_s(buffer, 4);
2125     ok(ret == EINVAL, "Expected _mbsupr_s to return EINVAL, got %d\n", ret);
2126     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2127
2128     memcpy(buffer, "abcdefgh\0ijklmnop", sizeof("abcdefgh\0ijklmnop"));
2129     errno = EBADF;
2130     ret = p_mbsupr_s(buffer, sizeof(buffer));
2131     ok(ret == 0, "Expected _mbsupr_s to return 0, got %d\n", ret);
2132     ok(!memcmp(buffer, "ABCDEFGH\0ijklmnop", sizeof("ABCDEFGH\0ijklmnop")),
2133        "Expected the output buffer to be \"ABCDEFGH\\0ijklmnop\", got \"%s\"\n",
2134        buffer);
2135
2136 }
2137
2138 static void test__mbslwr_s(void)
2139 {
2140     errno_t ret;
2141     unsigned char buffer[20];
2142
2143     if (!p_mbslwr_s)
2144     {
2145         win_skip("Skipping _mbslwr_s tests\n");
2146         return;
2147     }
2148
2149     errno = EBADF;
2150     ret = p_mbslwr_s(NULL, 0);
2151     ok(ret == 0, "Expected _mbslwr_s to return 0, got %d\n", ret);
2152
2153     errno = EBADF;
2154     ret = p_mbslwr_s(NULL, sizeof(buffer));
2155     ok(ret == EINVAL, "Expected _mbslwr_s to return EINVAL, got %d\n", ret);
2156     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2157
2158     errno = EBADF;
2159     ret = p_mbslwr_s(buffer, 0);
2160     ok(ret == EINVAL, "Expected _mbslwr_s to return EINVAL, got %d\n", ret);
2161     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2162
2163     memcpy(buffer, "ABCDEFGH", sizeof("ABCDEFGH"));
2164     errno = EBADF;
2165     ret = p_mbslwr_s(buffer, sizeof("ABCDEFGH"));
2166     ok(ret == 0, "Expected _mbslwr_s to return 0, got %d\n", ret);
2167     ok(!memcmp(buffer, "abcdefgh", sizeof("abcdefgh")),
2168        "Expected the output buffer to be \"abcdefgh\", got \"%s\"\n",
2169        buffer);
2170
2171     memcpy(buffer, "ABCDEFGH", sizeof("ABCDEFGH"));
2172     errno = EBADF;
2173     ret = p_mbslwr_s(buffer, sizeof(buffer));
2174     ok(ret == 0, "Expected _mbslwr_s to return 0, got %d\n", ret);
2175     ok(!memcmp(buffer, "abcdefgh", sizeof("abcdefgh")),
2176        "Expected the output buffer to be \"abcdefgh\", got \"%s\"\n",
2177        buffer);
2178
2179     memcpy(buffer, "ABCDEFGH", sizeof("ABCDEFGH"));
2180     errno = EBADF;
2181     ret = p_mbslwr_s(buffer, 4);
2182     ok(ret == EINVAL, "Expected _mbslwr_s to return EINVAL, got %d\n", ret);
2183     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2184
2185     memcpy(buffer, "ABCDEFGH\0IJKLMNOP", sizeof("ABCDEFGH\0IJKLMNOP"));
2186     errno = EBADF;
2187     ret = p_mbslwr_s(buffer, sizeof(buffer));
2188     ok(ret == 0, "Expected _mbslwr_s to return 0, got %d\n", ret);
2189     ok(!memcmp(buffer, "abcdefgh\0IJKLMNOP", sizeof("abcdefgh\0IJKLMNOP")),
2190        "Expected the output buffer to be \"abcdefgh\\0IJKLMNOP\", got \"%s\"\n",
2191        buffer);
2192 }
2193
2194 static void test__ultoa_s(void)
2195 {
2196     errno_t ret;
2197     char buffer[33];
2198
2199     if (!p_ultoa_s)
2200     {
2201         win_skip("Skipping _ultoa_s tests\n");
2202         return;
2203     }
2204
2205     errno = EBADF;
2206     ret = p_ultoa_s(0, NULL, 0, 0);
2207     ok(ret == EINVAL, "Expected _ultoa_s to return EINVAL, got %d\n", ret);
2208     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2209
2210     memset(buffer, 'X', sizeof(buffer));
2211     errno = EBADF;
2212     ret = p_ultoa_s(0, buffer, 0, 0);
2213     ok(ret == EINVAL, "Expected _ultoa_s to return EINVAL, got %d\n", ret);
2214     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2215     ok(buffer[0] == 'X', "Expected the output buffer to be untouched\n");
2216
2217     memset(buffer, 'X', sizeof(buffer));
2218     errno = EBADF;
2219     ret = p_ultoa_s(0, buffer, sizeof(buffer), 0);
2220     ok(ret == EINVAL, "Expected _ultoa_s to return EINVAL, got %d\n", ret);
2221     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2222     ok(buffer[0] == '\0', "Expected the output buffer to be null terminated\n");
2223
2224     memset(buffer, 'X', sizeof(buffer));
2225     errno = EBADF;
2226     ret = p_ultoa_s(0, buffer, sizeof(buffer), 64);
2227     ok(ret == EINVAL, "Expected _ultoa_s to return EINVAL, got %d\n", ret);
2228     ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
2229     ok(buffer[0] == '\0', "Expected the output buffer to be null terminated\n");
2230
2231     memset(buffer, 'X', sizeof(buffer));
2232     errno = EBADF;
2233     ret = p_ultoa_s(12345678, buffer, 4, 10);
2234     ok(ret == ERANGE, "Expected _ultoa_s to return ERANGE, got %d\n", ret);
2235     ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
2236     ok(!memcmp(buffer, "\000765", 4),
2237        "Expected the output buffer to be null terminated with truncated output\n");
2238
2239     memset(buffer, 'X', sizeof(buffer));
2240     errno = EBADF;
2241     ret = p_ultoa_s(12345678, buffer, 8, 10);
2242     ok(ret == ERANGE, "Expected _ultoa_s to return ERANGE, got %d\n", ret);
2243     ok(errno == ERANGE, "Expected errno to be ERANGE, got %d\n", errno);
2244     ok(!memcmp(buffer, "\0007654321", 8),
2245        "Expected the output buffer to be null terminated with truncated output\n");
2246
2247     ret = p_ultoa_s(12345678, buffer, 9, 10);
2248     ok(ret == 0, "Expected _ultoa_s to return 0, got %d\n", ret);
2249     ok(!strcmp(buffer, "12345678"),
2250        "Expected output buffer string to be \"12345678\", got \"%s\"\n",
2251        buffer);
2252
2253     ret = p_ultoa_s(43690, buffer, sizeof(buffer), 2);
2254     ok(ret == 0, "Expected _ultoa_s to return 0, got %d\n", ret);
2255     ok(!strcmp(buffer, "1010101010101010"),
2256        "Expected output buffer string to be \"1010101010101010\", got \"%s\"\n",
2257        buffer);
2258
2259     ret = p_ultoa_s(1092009, buffer, sizeof(buffer), 36);
2260     ok(ret == 0, "Expected _ultoa_s to return 0, got %d\n", ret);
2261     ok(!strcmp(buffer, "nell"),
2262        "Expected output buffer string to be \"nell\", got \"%s\"\n",
2263        buffer);
2264
2265     ret = p_ultoa_s(5704, buffer, sizeof(buffer), 18);
2266     ok(ret == 0, "Expected _ultoa_s to return 0, got %d\n", ret);
2267     ok(!strcmp(buffer, "hag"),
2268        "Expected output buffer string to be \"hag\", got \"%s\"\n",
2269        buffer);
2270 }
2271
2272 static void test_wctob(void)
2273 {
2274     int ret;
2275
2276     if(!p_wctob || !setlocale(LC_ALL, "chinese-traditional")) {
2277         win_skip("Skipping wctob tests\n");
2278         return;
2279     }
2280
2281     ret = p_wctob(0x8141);
2282     ok(ret == EOF, "ret = %x\n", ret);
2283
2284     ret = p_wctob(0x81);
2285     ok(ret == EOF, "ret = %x\n", ret);
2286
2287     ret = p_wctob(0xe0);
2288     ok(ret == 0x61, "ret = %x\n", ret);
2289
2290     _setmbcp(1250);
2291     ret = p_wctob(0x81);
2292     ok(ret == EOF, "ret = %x\n", ret);
2293
2294     setlocale(LC_ALL, "C");
2295     ret = p_wctob(0x8141);
2296     ok(ret == EOF, "ret = %x\n", ret);
2297
2298     ret = p_wctob(0x81);
2299     ok(ret == (int)(char)0x81, "ret = %x\n", ret);
2300
2301     ret = p_wctob(0x9f);
2302     ok(ret == (int)(char)0x9f, "ret = %x\n", ret);
2303
2304     ret = p_wctob(0xe0);
2305     ok(ret == (int)(char)0xe0, "ret = %x\n", ret);
2306 }
2307 static void test_wctomb(void)
2308 {
2309     mbstate_t state;
2310     unsigned char dst[10];
2311     size_t ret;
2312
2313     if(!p_wcrtomb || !setlocale(LC_ALL, "Japanese_Japan.932")) {
2314         win_skip("wcrtomb tests\n");
2315         return;
2316     }
2317
2318     ret = p_wcrtomb(NULL, 0x3042, NULL);
2319     ok(ret == 2, "wcrtomb did not return 2\n");
2320
2321     state = 1;
2322     dst[2] = 'a';
2323     ret = p_wcrtomb((char*)dst, 0x3042, &state);
2324     ok(ret == 2, "wcrtomb did not return 2\n");
2325     ok(state == 0, "state != 0\n");
2326     ok(dst[0] == 0x82, "dst[0] = %x, expected 0x82\n", dst[0]);
2327     ok(dst[1] == 0xa0, "dst[1] = %x, expected 0xa0\n", dst[1]);
2328     ok(dst[2] == 'a', "dst[2] != 'a'\n");
2329
2330     ret = p_wcrtomb((char*)dst, 0x3043, NULL);
2331     ok(ret == 2, "wcrtomb did not return 2\n");
2332     ok(dst[0] == 0x82, "dst[0] = %x, expected 0x82\n", dst[0]);
2333     ok(dst[1] == 0xa1, "dst[1] = %x, expected 0xa1\n", dst[1]);
2334
2335     ret = p_wcrtomb((char*)dst, 0x20, NULL);
2336     ok(ret == 1, "wcrtomb did not return 1\n");
2337     ok(dst[0] == 0x20, "dst[0] = %x, expected 0x20\n", dst[0]);
2338
2339     ret = p_wcrtomb((char*)dst, 0xffff, NULL);
2340     ok(ret == -1, "wcrtomb did not return -1\n");
2341     ok(dst[0] == 0x3f, "dst[0] = %x, expected 0x20\n", dst[0]);
2342
2343     setlocale(LC_ALL, "C");
2344 }
2345
2346 static void test_tolower(void)
2347 {
2348     int ret;
2349
2350     ret = p_tolower(0x41);
2351     ok(ret == 0x61, "ret = %x\n", ret);
2352
2353     ret = p_tolower(0xF4);
2354     ok(ret == 0xF4, "ret = %x\n", ret);
2355
2356     ret = p_tolower((char)0xF4);
2357     ok(ret==0xF4/*Vista+*/ || ret==(char)0xF4, "ret = %x\n", ret);
2358
2359     /* is it using different locale (CP_ACP) for negative values?? */
2360     /* current implementation matches msvcr90 behaviour */
2361     ret = p_tolower((char)0xD0);
2362     todo_wine ok(ret==0xF0/*Vista+*/ || ret==(char)0xD0, "ret = %x\n", ret);
2363
2364     ret = p_tolower(0xD0);
2365     ok(ret == 0xD0, "ret = %x\n", ret);
2366
2367     if(!setlocale(LC_ALL, "us")) {
2368         win_skip("skipping tolower tests that depends on locale\n");
2369         return;
2370     }
2371
2372     ret = p_tolower((char)0xD0);
2373     ok(ret == 0xF0, "ret = %x\n", ret);
2374
2375     ret = p_tolower(0xD0);
2376     ok(ret == 0xF0, "ret = %x\n", ret);
2377
2378     setlocale(LC_ALL, "C");
2379 }
2380
2381 static void test__atodbl(void)
2382 {
2383     _CRT_DOUBLE d;
2384     char num[32];
2385     int ret;
2386
2387     if(!p__atodbl_l) {
2388         /* Old versions of msvcrt use different values for _OVERFLOW and _UNDERFLOW
2389          * Because of this lets skip _atodbl tests when _atodbl_l is not available */
2390         win_skip("_atodbl_l is not available\n");
2391         return;
2392     }
2393
2394     num[0] = 0;
2395     ret = p__atodbl_l(&d, num, NULL);
2396     ok(ret == 0, "_atodbl_l(&d, \"\", NULL) returned %d, expected 0\n", ret);
2397     ok(d.x == 0, "d.x = %lf, expected 0\n", d.x);
2398     ret = _atodbl(&d, num);
2399     ok(ret == 0, "_atodbl(&d, \"\") returned %d, expected 0\n", ret);
2400     ok(d.x == 0, "d.x = %lf, expected 0\n", d.x);
2401
2402     strcpy(num, "t");
2403     ret = p__atodbl_l(&d, num, NULL);
2404     ok(ret == 0, "_atodbl_l(&d, \"t\", NULL) returned %d, expected 0\n", ret);
2405     ok(d.x == 0, "d.x = %lf, expected 0\n", d.x);
2406     ret = _atodbl(&d, num);
2407     ok(ret == 0, "_atodbl(&d, \"t\") returned %d, expected 0\n", ret);
2408     ok(d.x == 0, "d.x = %lf, expected 0\n", d.x);
2409
2410     strcpy(num, "0");
2411     ret = p__atodbl_l(&d, num, NULL);
2412     ok(ret == 0, "_atodbl_l(&d, \"0\", NULL) returned %d, expected 0\n", ret);
2413     ok(d.x == 0, "d.x = %lf, expected 0\n", d.x);
2414     ret = _atodbl(&d, num);
2415     ok(ret == 0, "_atodbl(&d, \"0\") returned %d, expected 0\n", ret);
2416     ok(d.x == 0, "d.x = %lf, expected 0\n", d.x);
2417
2418     strcpy(num, "123");
2419     ret = p__atodbl_l(&d, num, NULL);
2420     ok(ret == 0, "_atodbl_l(&d, \"123\", NULL) returned %d, expected 0\n", ret);
2421     ok(d.x == 123, "d.x = %lf, expected 123\n", d.x);
2422     ret = _atodbl(&d, num);
2423     ok(ret == 0, "_atodbl(&d, \"123\") returned %d, expected 0\n", ret);
2424     ok(d.x == 123, "d.x = %lf, expected 123\n", d.x);
2425
2426     strcpy(num, "1e-309");
2427     ret = p__atodbl_l(&d, num, NULL);
2428     ok(ret == _UNDERFLOW, "_atodbl_l(&d, \"1e-309\", NULL) returned %d, expected _UNDERFLOW\n", ret);
2429     ok(d.x!=0 && almost_equal(d.x, 0), "d.x = %le, expected 0\n", d.x);
2430     ret = _atodbl(&d, num);
2431     ok(ret == _UNDERFLOW, "_atodbl(&d, \"1e-309\") returned %d, expected _UNDERFLOW\n", ret);
2432     ok(d.x!=0 && almost_equal(d.x, 0), "d.x = %le, expected 0\n", d.x);
2433
2434     strcpy(num, "1e309");
2435     ret = p__atodbl_l(&d, num, NULL);
2436     ok(ret == _OVERFLOW, "_atodbl_l(&d, \"1e309\", NULL) returned %d, expected _OVERFLOW\n", ret);
2437     ret = _atodbl(&d, num);
2438     ok(ret == _OVERFLOW, "_atodbl(&d, \"1e309\") returned %d, expected _OVERFLOW\n", ret);
2439 }
2440
2441 static void test__stricmp(void)
2442 {
2443     int ret;
2444
2445     ret = _stricmp("test", "test");
2446     ok(ret == 0, "_stricmp returned %d\n", ret);
2447     ret = _stricmp("a", "z");
2448     ok(ret < 0, "_stricmp returned %d\n", ret);
2449     ret = _stricmp("z", "a");
2450     ok(ret > 0, "_stricmp returned %d\n", ret);
2451     ret = _stricmp("\xa5", "\xb9");
2452     ok(ret < 0, "_stricmp returned %d\n", ret);
2453
2454     if(!setlocale(LC_ALL, "polish")) {
2455         win_skip("stricmp tests\n");
2456         return;
2457     }
2458
2459     ret = _stricmp("test", "test");
2460     ok(ret == 0, "_stricmp returned %d\n", ret);
2461     ret = _stricmp("a", "z");
2462     ok(ret < 0, "_stricmp returned %d\n", ret);
2463     ret = _stricmp("z", "a");
2464     ok(ret > 0, "_stricmp returned %d\n", ret);
2465     ret = _stricmp("\xa5", "\xb9");
2466     ok(ret == 0, "_stricmp returned %d\n", ret);
2467
2468     setlocale(LC_ALL, "C");
2469 }
2470
2471 START_TEST(string)
2472 {
2473     char mem[100];
2474     static const char xilstring[]="c:/xilinx";
2475     int nLen;
2476
2477     hMsvcrt = GetModuleHandleA("msvcrt.dll");
2478     if (!hMsvcrt)
2479         hMsvcrt = GetModuleHandleA("msvcrtd.dll");
2480     ok(hMsvcrt != 0, "GetModuleHandleA failed\n");
2481     SET(pmemcpy,"memcpy");
2482     p_memcpy_s = (void*)GetProcAddress( hMsvcrt, "memcpy_s" );
2483     p_memmove_s = (void*)GetProcAddress( hMsvcrt, "memmove_s" );
2484     SET(pmemcmp,"memcmp");
2485     SET(p_mbctype,"_mbctype");
2486     SET(p__mb_cur_max,"__mb_cur_max");
2487     pstrcpy_s = (void *)GetProcAddress( hMsvcrt,"strcpy_s" );
2488     pstrcat_s = (void *)GetProcAddress( hMsvcrt,"strcat_s" );
2489     p_mbsnbcat_s = (void *)GetProcAddress( hMsvcrt,"_mbsnbcat_s" );
2490     p_mbsnbcpy_s = (void *)GetProcAddress( hMsvcrt,"_mbsnbcpy_s" );
2491     p_wcscpy_s = (void *)GetProcAddress( hMsvcrt,"wcscpy_s" );
2492     p_wcsncpy_s = (void *)GetProcAddress( hMsvcrt,"wcsncpy_s" );
2493     p_wcsncat_s = (void *)GetProcAddress( hMsvcrt,"wcsncat_s" );
2494     p_wcsupr_s = (void *)GetProcAddress( hMsvcrt,"_wcsupr_s" );
2495     p_strnlen = (void *)GetProcAddress( hMsvcrt,"strnlen" );
2496     p_strtoi64 = (void *)GetProcAddress(hMsvcrt, "_strtoi64");
2497     p_strtoui64 = (void *)GetProcAddress(hMsvcrt, "_strtoui64");
2498     pmbstowcs_s = (void *)GetProcAddress(hMsvcrt, "mbstowcs_s");
2499     pwcstombs_s = (void *)GetProcAddress(hMsvcrt, "wcstombs_s");
2500     pwcsrtombs = (void *)GetProcAddress(hMsvcrt, "wcsrtombs");
2501     p_gcvt_s = (void *)GetProcAddress(hMsvcrt, "_gcvt_s");
2502     p_itoa_s = (void *)GetProcAddress(hMsvcrt, "_itoa_s");
2503     p_strlwr_s = (void *)GetProcAddress(hMsvcrt, "_strlwr_s");
2504     p_ultoa_s = (void *)GetProcAddress(hMsvcrt, "_ultoa_s");
2505     p_set_invalid_parameter_handler = (void *) GetProcAddress(hMsvcrt, "_set_invalid_parameter_handler");
2506     p_wcslwr_s = (void*)GetProcAddress(hMsvcrt, "_wcslwr_s");
2507     p_mbsupr_s = (void*)GetProcAddress(hMsvcrt, "_mbsupr_s");
2508     p_mbslwr_s = (void*)GetProcAddress(hMsvcrt, "_mbslwr_s");
2509     p_wctob = (void*)GetProcAddress(hMsvcrt, "wctob");
2510     p_wcrtomb = (void*)GetProcAddress(hMsvcrt, "wcrtomb");
2511     p_tolower = (void*)GetProcAddress(hMsvcrt, "tolower");
2512     p_mbrlen = (void*)GetProcAddress(hMsvcrt, "mbrlen");
2513     p_mbrtowc = (void*)GetProcAddress(hMsvcrt, "mbrtowc");
2514     p_mbsrtowcs = (void*)GetProcAddress(hMsvcrt, "mbsrtowcs");
2515     p__atodbl_l = (void*)GetProcAddress(hMsvcrt, "_atodbl_l");
2516
2517     /* MSVCRT memcpy behaves like memmove for overlapping moves,
2518        MFC42 CString::Insert seems to rely on that behaviour */
2519     strcpy(mem,xilstring);
2520     nLen=strlen(xilstring);
2521     pmemcpy(mem+5, mem,nLen+1);
2522     ok(pmemcmp(mem+5,xilstring, nLen) == 0,
2523        "Got result %s\n",mem+5);
2524
2525     /* Test _swab function */
2526     test_swab();
2527
2528     /* Test ismbblead*/
2529     test_mbcp();
2530    /* test _mbsspn */
2531     test_mbsspn();
2532     test_mbsspnp();
2533    /* test _strdup */
2534     test_strdup();
2535     test_strcpy_s();
2536     test_memcpy_s();
2537     test_memmove_s();
2538     test_strcat_s();
2539     test__mbsnbcpy_s();
2540     test_mbcjisjms();
2541     test_mbcjmsjis();
2542     test_mbbtombc();
2543     test_mbctombb();
2544     test_ismbclegal();
2545     test_strtok();
2546     test_wcscpy_s();
2547     test__wcsupr_s();
2548     test_strtol();
2549     test_strnlen();
2550     test__strtoi64();
2551     test__strtod();
2552     test_mbstowcs();
2553     test_gcvt();
2554     test__itoa_s();
2555     test__strlwr_s();
2556     test_wcsncat_s();
2557     test__mbsnbcat_s();
2558     test__ultoa_s();
2559     test__wcslwr_s();
2560     test__mbsupr_s();
2561     test__mbslwr_s();
2562     test_wctob();
2563     test_wctomb();
2564     test_tolower();
2565     test__atodbl();
2566     test__stricmp();
2567 }