ntdll: Add ARM64 signal handling.
[wine] / dlls / msvcr100 / msvcr100.c
1 /*
2  * msvcr100 specific functions
3  *
4  * Copyright 2010 Detlef Riekenberg
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 "config.h"
22 #include <stdarg.h>
23
24 #include "stdio.h"
25 #include "stdlib.h"
26 #include "errno.h"
27 #include "malloc.h"
28 #include "limits.h"
29 #include "sys/stat.h"
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
35
36 #define INVALID_PMT(x,err)   (*_errno() = (err), _invalid_parameter(NULL, NULL, NULL, 0, 0))
37 #define CHECK_PMT_ERR(x,err) ((x) || (INVALID_PMT( 0, (err) ), FALSE))
38 #define CHECK_PMT(x)         CHECK_PMT_ERR((x), EINVAL)
39
40 #ifdef __i386__  /* thiscall functions are i386-specific */
41
42 #define THISCALL(func) __thiscall_ ## func
43 #define THISCALL_NAME(func) __ASM_NAME("__thiscall_" #func)
44 #define __thiscall __stdcall
45 #define DEFINE_THISCALL_WRAPPER(func,args) \
46     extern void THISCALL(func)(void); \
47     __ASM_GLOBAL_FUNC(__thiscall_ ## func, \
48                       "popl %eax\n\t" \
49                       "pushl %ecx\n\t" \
50                       "pushl %eax\n\t" \
51                       "jmp " __ASM_NAME(#func) __ASM_STDCALL(args) )
52
53 #else /* __i386__ */
54
55 #define THISCALL(func) func
56 #define THISCALL_NAME(func) __ASM_NAME(#func)
57 #define __thiscall __cdecl
58 #define DEFINE_THISCALL_WRAPPER(func,args) /* nothing */
59
60 #endif /* __i386__ */
61
62 struct __type_info_node
63 {
64     void *memPtr;
65     struct __type_info_node* next;
66 };
67
68 typedef struct __type_info
69 {
70     const void *vtable;
71     char       *name;        /* Unmangled name, allocated lazily */
72     char        mangled[32]; /* Variable length, but we declare it large enough for static RTTI */
73 } type_info;
74
75 typedef void* (__cdecl *malloc_func_t)(size_t);
76 typedef void  (__cdecl *free_func_t)(void*);
77
78 extern char* __cdecl __unDName(char *,const char*,int,malloc_func_t,free_func_t,unsigned short int);
79
80 /*********************************************************************
81  *  *  stat64_to_stat32 [internal]
82  *   */
83 static void stat64_to_stat32(const struct _stat64 *buf64, struct _stat32 *buf)
84 {
85     buf->st_dev   = buf64->st_dev;
86     buf->st_ino   = buf64->st_ino;
87     buf->st_mode  = buf64->st_mode;
88     buf->st_nlink = buf64->st_nlink;
89     buf->st_uid   = buf64->st_uid;
90     buf->st_gid   = buf64->st_gid;
91     buf->st_rdev  = buf64->st_rdev;
92     buf->st_size  = buf64->st_size;
93     buf->st_atime = buf64->st_atime;
94     buf->st_mtime = buf64->st_mtime;
95     buf->st_ctime = buf64->st_ctime;
96 }
97
98  /*********************************************************************
99  *              wmemcpy_s (MSVCR100.@)
100  */
101 int CDECL wmemcpy_s(wchar_t *dest, size_t numberOfElements, const wchar_t *src, size_t count)
102 {
103     TRACE("(%p %lu %p %lu)\n", dest, (unsigned long)numberOfElements, src, (unsigned long)count);
104
105     if (!count)
106         return 0;
107
108     if (!CHECK_PMT(dest != NULL)) return EINVAL;
109
110     if (!CHECK_PMT(src != NULL)) {
111         memset(dest, 0, numberOfElements*sizeof(wchar_t));
112         return EINVAL;
113     }
114     if (!CHECK_PMT_ERR(count <= numberOfElements, ERANGE)) {
115         memset(dest, 0, numberOfElements*sizeof(wchar_t));
116         return ERANGE;
117     }
118
119     memcpy(dest, src, sizeof(wchar_t)*count);
120     return 0;
121 }
122
123  /*********************************************************************
124  *              wmemmove_s (MSVCR100.@)
125  */
126 int CDECL wmemmove_s(wchar_t *dest, size_t numberOfElements, const wchar_t *src, size_t count)
127 {
128     TRACE("(%p %lu %p %lu)\n", dest, (unsigned long)numberOfElements, src, (unsigned long)count);
129
130     if (!count)
131         return 0;
132
133     /* Native does not seem to conform to 6.7.1.2.3 in
134      * http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1225.pdf
135      * in that it does not zero the output buffer on constraint violation.
136      */
137     if (!CHECK_PMT(dest != NULL)) return EINVAL;
138     if (!CHECK_PMT(src != NULL)) return EINVAL;
139     if (!CHECK_PMT_ERR(count <= numberOfElements, ERANGE)) return ERANGE;
140
141     memmove(dest, src, sizeof(wchar_t)*count);
142     return 0;
143 }
144
145 /*********************************************************************
146  *  _encoded_null (MSVCR100.@)
147  */
148 void * CDECL _encoded_null(void)
149 {
150     TRACE("\n");
151
152     return EncodePointer(NULL);
153 }
154
155 /*********************************************************************
156  * _invalid_parameter_noinfo (MSVCR100.@)
157  */
158 void CDECL _invalid_parameter_noinfo(void)
159 {
160     _invalid_parameter( NULL, NULL, NULL, 0, 0 );
161 }
162
163 /*********************************************************************
164  * __sys_nerr (MSVCR100.@)
165  */
166 int* CDECL __sys_nerr(void)
167 {
168     return (int*)GetProcAddress(GetModuleHandleA("msvcrt.dll"), "_sys_nerr");
169 }
170
171 /*********************************************************************
172  *  __sys_errlist (MSVCR100.@)
173  */
174 char** CDECL __sys_errlist(void)
175 {
176     return (char**)GetProcAddress(GetModuleHandleA("msvcrt.dll"), "_sys_errlist");
177 }
178
179 /*********************************************************************
180  * __clean_type_info_names_internal (MSVCR100.@)
181  */
182 void CDECL __clean_type_info_names_internal(void *p)
183 {
184     FIXME("(%p) stub\n", p);
185 }
186
187 /*********************************************************************
188  * _recalloc (MSVCR100.@)
189  */
190 void* CDECL _recalloc(void* mem, size_t num, size_t size)
191 {
192     size_t old_size;
193     void *ret;
194
195     if(!mem)
196         return calloc(num, size);
197
198     size = num*size;
199     old_size = _msize(mem);
200
201     ret = realloc(mem, size);
202     if(!ret) {
203         *_errno() = ENOMEM;
204         return NULL;
205     }
206
207     if(size>old_size)
208         memset((BYTE*)ret+old_size, 0, size-old_size);
209     return ret;
210 }
211
212 /*********************************************************************
213  *  _fstat32 (MSVCR100.@)
214  */
215 int CDECL _fstat32(int fd, struct _stat32* buf)
216 {
217     int ret;
218     struct _stat64 buf64;
219
220     ret = _fstat64(fd, &buf64);
221     if (!ret)
222         stat64_to_stat32(&buf64, buf);
223     return ret;
224 }
225
226 /*********************************************************************
227  *  _stat32 (MSVCR100.@)
228  */
229 int CDECL _stat32(const char *path, struct _stat32* buf)
230 {
231     int ret;
232     struct _stat64 buf64;
233
234     ret = _stat64(path, &buf64);
235     if (!ret)
236         stat64_to_stat32(&buf64, buf);
237     return ret;
238 }
239
240 /*********************************************************************
241  *  _wstat32 (MSVCR100.@)
242  */
243 int CDECL _wstat32(const wchar_t *path, struct _stat32* buf)
244 {
245     int ret;
246     struct _stat64 buf64;
247
248     ret = _wstat64(path, &buf64);
249     if (!ret)
250         stat64_to_stat32(&buf64, buf);
251     return ret;
252 }
253
254 static void stat64_to_stat64i32(const struct _stat64 *buf64, struct _stat64i32 *buf)
255 {
256     buf->st_dev   = buf64->st_dev;
257     buf->st_ino   = buf64->st_ino;
258     buf->st_mode  = buf64->st_mode;
259     buf->st_nlink = buf64->st_nlink;
260     buf->st_uid   = buf64->st_uid;
261     buf->st_gid   = buf64->st_gid;
262     buf->st_rdev  = buf64->st_rdev;
263     buf->st_size  = buf64->st_size;
264     buf->st_atime = buf64->st_atime;
265     buf->st_mtime = buf64->st_mtime;
266     buf->st_ctime = buf64->st_ctime;
267 }
268
269 /*********************************************************************
270  * _fstat64i32 (MSVCR100.@)
271  */
272 int CDECL _fstat64i32(int fd, struct _stat64i32* buf)
273 {
274     int ret;
275     struct _stat64 buf64;
276
277     ret = _fstat64(fd, &buf64);
278     if (!ret)
279         stat64_to_stat64i32(&buf64, buf);
280     return ret;
281 }
282
283 /*********************************************************************
284  * _stat64i32 (MSVCR100.@)
285  */
286 int CDECL _stat64i32(const char* path, struct _stat64i32 * buf)
287 {
288     int ret;
289     struct _stat64 buf64;
290
291     ret = _stat64(path, &buf64);
292     if (!ret)
293         stat64_to_stat64i32(&buf64, buf);
294     return ret;
295 }
296
297 /*********************************************************************
298  * _wstat64i32 (MSVCR100.@)
299  */
300 int CDECL _wstat64i32(const wchar_t *path, struct _stat64i32 *buf)
301 {
302     int ret;
303     struct _stat64 buf64;
304
305     ret = _wstat64(path, &buf64);
306     if (!ret)
307         stat64_to_stat64i32(&buf64, buf);
308     return ret;
309 }
310
311 /*********************************************************************
312  * _atoflt  (MSVCR100.@)
313  */
314 int CDECL _atoflt( _CRT_FLOAT *value, char *str )
315 {
316     return _atoflt_l( value, str, NULL );
317 }
318
319 /*********************************************************************
320  * ?_name_internal_method@type_info@@QBEPBDPAU__type_info_node@@@Z (MSVCR100.@)
321  */
322 DEFINE_THISCALL_WRAPPER(type_info_name_internal_method,8)
323 const char * __thiscall type_info_name_internal_method(type_info * _this, struct __type_info_node *node)
324 {
325     static int once;
326
327     if (node && !once++) FIXME("type_info_node parameter ignored\n");
328
329     if (!_this->name)
330     {
331         /* Create and set the demangled name */
332         /* Note: mangled name in type_info struct always starts with a '.', while
333          * it isn't valid for mangled name.
334          * Is this '.' really part of the mangled name, or has it some other meaning ?
335          */
336         char* name = __unDName(0, _this->mangled + 1, 0, malloc, free, 0x2800);
337         if (name)
338         {
339             unsigned int len = strlen(name);
340
341             /* It seems _unDName may leave blanks at the end of the demangled name */
342             while (len && name[--len] == ' ')
343                 name[len] = '\0';
344
345             if (InterlockedCompareExchangePointer((void**)&_this->name, name, NULL))
346             {
347                 /* Another thread set this member since we checked above - use it */
348                 free(name);
349             }
350         }
351     }
352     TRACE("(%p) returning %s\n", _this, _this->name);
353     return _this->name;
354 }
355
356 /*********************************************************************
357  * _CRT_RTC_INIT (MSVCR100.@)
358  */
359 void* CDECL _CRT_RTC_INIT(void *unk1, void *unk2, int unk3, int unk4, int unk5)
360 {
361     TRACE("%p %p %x %x %x\n", unk1, unk2, unk3, unk4, unk5);
362     return NULL;
363 }
364
365 /*********************************************************************
366  * _CRT_RTC_INITW (MSVCR100.@)
367  */
368 void* CDECL _CRT_RTC_INITW(void *unk1, void *unk2, int unk3, int unk4, int unk5)
369 {
370     TRACE("%p %p %x %x %x\n", unk1, unk2, unk3, unk4, unk5);
371     return NULL;
372 }
373
374 /*********************************************************************
375  * _vswprintf_p (MSVCR100.@)
376  */
377 int CDECL _vswprintf_p(wchar_t *buffer, size_t length, const wchar_t *format, __ms_va_list args)
378 {
379     return _vswprintf_p_l(buffer, length, format, NULL, args);
380 }
381
382 /*********************************************************************
383  * _byteswap_ushort (MSVCR100.@)
384  */
385 unsigned short CDECL _byteswap_ushort(unsigned short s)
386 {
387     return (s<<8) + (s>>8);
388 }
389
390 /*********************************************************************
391  * _byteswap_ulong (MSVCR100.@)
392  */
393 ULONG CDECL _byteswap_ulong(ULONG l)
394 {
395     return (l<<24) + ((l<<8)&0xFF0000) + ((l>>8)&0xFF00) + (l>>24);
396 }
397
398 /*********************************************************************
399  * _byteswap_uint64 (MSVCR100.@)
400  */
401 unsigned __int64 CDECL _byteswap_uint64(unsigned __int64 i)
402 {
403     return (i<<56) + ((i&0xFF00)<<40) + ((i&0xFF0000)<<24) + ((i&0xFF000000)<<8) +
404         ((i>>8)&0xFF000000) + ((i>>24)&0xFF0000) + ((i>>40)&0xFF00) + (i>>56);
405 }
406
407 /*********************************************************************
408  * fread_s (MSVCR100.@)
409  */
410 size_t CDECL fread_s(void *buf, size_t buf_size, size_t elem_size, size_t count, FILE *stream)
411 {
412     size_t bytes_left, buf_pos;
413
414     TRACE("(%p %lu %lu %lu %p\n", buf, (unsigned long)buf_size,
415             (unsigned long)elem_size, (unsigned long)count, stream);
416
417
418     if(!CHECK_PMT(stream != NULL)) {
419         if(buf && buf_size)
420             memset(buf, 0, buf_size);
421         return 0;
422     }
423     if(!elem_size || !count) return 0;
424     if(!CHECK_PMT(buf != NULL)) return 0;
425     if(!CHECK_PMT(SIZE_MAX/count >= elem_size)) return 0;
426
427     bytes_left = elem_size*count;
428     buf_pos = 0;
429     while(bytes_left) {
430         if(stream->_cnt > 0) {
431             size_t size = bytes_left<stream->_cnt ? bytes_left : stream->_cnt;
432
433             if(!CHECK_PMT_ERR(size <= buf_size-buf_pos, ERANGE)) {
434                 memset(buf, 0, buf_size);
435                 return 0;
436             }
437
438             fread((char*)buf+buf_pos, 1, size, stream);
439             buf_pos += size;
440             bytes_left -= size;
441         }else {
442             int c = _filbuf(stream);
443
444             if(c == EOF)
445                 break;
446
447             if(!CHECK_PMT_ERR(buf_size-buf_pos > 0, ERANGE)) {
448                 memset(buf, 0, buf_size);
449                 return 0;
450             }
451
452             ((char*)buf)[buf_pos++] = c;
453             bytes_left--;
454         }
455     }
456
457     return buf_pos/elem_size;
458 }
459
460 /*********************************************************************
461  * _sprintf_p (MSVCR100.@)
462  */
463 int CDECL _sprintf_p(char *buffer, size_t length, const char *format, ...)
464 {
465     __ms_va_list valist;
466     int r;
467
468     __ms_va_start(valist, format);
469     r = _vsprintf_p_l(buffer, length, format, NULL, valist);
470     __ms_va_end(valist);
471
472     return r;
473 }
474
475 /*********************************************************************
476  * _get_timezone (MSVCR100.@)
477  */
478 int CDECL _get_timezone(LONG *timezone)
479 {
480     if(!CHECK_PMT(timezone != NULL)) return EINVAL;
481
482     *timezone = *(LONG*)GetProcAddress(GetModuleHandleA("msvcrt.dll"), "_timezone");
483     return 0;
484 }
485
486 /* copied from dlls/msvcrt/heap.c */
487 #define SAVED_PTR(x) ((void *)((DWORD_PTR)((char *)x - sizeof(void *)) & \
488                                ~(sizeof(void *) - 1)))
489
490 /*********************************************************************
491  * _aligned_msize (MSVCR100.@)
492  */
493 size_t CDECL _aligned_msize(void *p, size_t alignment, size_t offset)
494 {
495     void **alloc_ptr;
496
497     if(!CHECK_PMT(p)) return -1;
498
499     if(alignment < sizeof(void*))
500         alignment = sizeof(void*);
501
502     alloc_ptr = SAVED_PTR(p);
503     return _msize(*alloc_ptr)-alignment-sizeof(void*);
504 }
505
506 /*********************************************************************
507  *  DllMain (MSVCR100.@)
508  */
509 BOOL WINAPI DllMain(HINSTANCE hdll, DWORD reason, LPVOID reserved)
510 {
511     switch (reason)
512     {
513     case DLL_WINE_PREATTACH:
514         return FALSE;  /* prefer native version */
515
516     case DLL_PROCESS_ATTACH:
517         DisableThreadLibraryCalls(hdll);
518         _set_printf_count_output(0);
519     }
520     return TRUE;
521 }