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