1 #include <windows.h>
2 #include <malloc.h>
3 #include <errno.h>
4 #include <msvcrt.h>
5 #include <sec_api/wchar_s.h>
6
7 static errno_t __cdecl _int_wmemmove_s (wchar_t *, size_t, const wchar_t*, size_t);
8 static errno_t __cdecl _stub (wchar_t *, size_t, const wchar_t *, size_t);
9
10 errno_t __cdecl (*__MINGW_IMP_SYMBOL(wmemmove_s))(wchar_t *, size_t, const wchar_t *, size_t) =
11 _stub;
12
13 static errno_t __cdecl
_stub(wchar_t * d,size_t dn,const wchar_t * s,size_t n)14 _stub (wchar_t *d, size_t dn, const wchar_t *s, size_t n)
15 {
16 errno_t __cdecl (*f)(wchar_t *, size_t, const wchar_t *, size_t) = __MINGW_IMP_SYMBOL(wmemmove_s);
17
18 if (f == _stub)
19 {
20 f = (errno_t __cdecl (*)(wchar_t *, size_t, const wchar_t *, size_t))
21 GetProcAddress (__mingw_get_msvcrt_handle (), "wmemmove_s");
22 if (!f)
23 f = _int_wmemmove_s;
24 __MINGW_IMP_SYMBOL(wmemmove_s) = f;
25 }
26 return (*f)(d, dn, s, n);
27 }
28
29 errno_t __cdecl
wmemmove_s(wchar_t * d,size_t dn,const wchar_t * s,size_t n)30 wmemmove_s (wchar_t *d, size_t dn, const wchar_t *s, size_t n)
31 {
32 return _stub (d, dn, s, n);
33 }
34
35 static errno_t __cdecl
_int_wmemmove_s(wchar_t * d,size_t dn,const wchar_t * s,size_t n)36 _int_wmemmove_s (wchar_t *d, size_t dn, const wchar_t *s, size_t n)
37 {
38 if (!n)
39 return 0;
40
41 if (!d || !s)
42 {
43 if (d)
44 memset (d, 0, dn * sizeof (wchar_t));
45 errno = EINVAL;
46 return EINVAL;
47 }
48
49 if (dn < n)
50 {
51 memset (d, 0, dn * sizeof (wchar_t));
52
53 errno = ERANGE;
54 return ERANGE;
55 }
56
57 memmove (d, s, n * sizeof (wchar_t));
58
59 return 0;
60 }
61