Release 1.5.29.
[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_stat32i64(const struct _stat64 *buf64, struct _stat32i64 *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  *  _stat32i64 (MSVCR100.@)
271  */
272 int CDECL _stat32i64(const char *path, struct _stat32i64* buf)
273 {
274     int ret;
275     struct _stat64 buf64;
276
277     ret = _stat64(path, &buf64);
278     if (!ret)
279         stat64_to_stat32i64(&buf64, buf);
280     return ret;
281 }
282
283 /*********************************************************************
284  *  _wstat32i64 (MSVCR100.@)
285  */
286 int CDECL _wstat32i64(const wchar_t *path, struct _stat32i64* buf)
287 {
288     int ret;
289     struct _stat64 buf64;
290
291     ret = _wstat64(path, &buf64);
292     if (!ret)
293         stat64_to_stat32i64(&buf64, buf);
294     return ret;
295 }
296
297 static void stat64_to_stat64i32(const struct _stat64 *buf64, struct _stat64i32 *buf)
298 {
299     buf->st_dev   = buf64->st_dev;
300     buf->st_ino   = buf64->st_ino;
301     buf->st_mode  = buf64->st_mode;
302     buf->st_nlink = buf64->st_nlink;
303     buf->st_uid   = buf64->st_uid;
304     buf->st_gid   = buf64->st_gid;
305     buf->st_rdev  = buf64->st_rdev;
306     buf->st_size  = buf64->st_size;
307     buf->st_atime = buf64->st_atime;
308     buf->st_mtime = buf64->st_mtime;
309     buf->st_ctime = buf64->st_ctime;
310 }
311
312 /*********************************************************************
313  * _fstat64i32 (MSVCR100.@)
314  */
315 int CDECL _fstat64i32(int fd, struct _stat64i32* buf)
316 {
317     int ret;
318     struct _stat64 buf64;
319
320     ret = _fstat64(fd, &buf64);
321     if (!ret)
322         stat64_to_stat64i32(&buf64, buf);
323     return ret;
324 }
325
326 /*********************************************************************
327  * _stat64i32 (MSVCR100.@)
328  */
329 int CDECL _stat64i32(const char* path, struct _stat64i32 * buf)
330 {
331     int ret;
332     struct _stat64 buf64;
333
334     ret = _stat64(path, &buf64);
335     if (!ret)
336         stat64_to_stat64i32(&buf64, buf);
337     return ret;
338 }
339
340 /*********************************************************************
341  * _wstat64i32 (MSVCR100.@)
342  */
343 int CDECL _wstat64i32(const wchar_t *path, struct _stat64i32 *buf)
344 {
345     int ret;
346     struct _stat64 buf64;
347
348     ret = _wstat64(path, &buf64);
349     if (!ret)
350         stat64_to_stat64i32(&buf64, buf);
351     return ret;
352 }
353
354 /*********************************************************************
355  * _atoflt  (MSVCR100.@)
356  */
357 int CDECL _atoflt( _CRT_FLOAT *value, char *str )
358 {
359     return _atoflt_l( value, str, NULL );
360 }
361
362 /*********************************************************************
363  * ?_name_internal_method@type_info@@QBEPBDPAU__type_info_node@@@Z (MSVCR100.@)
364  */
365 DEFINE_THISCALL_WRAPPER(type_info_name_internal_method,8)
366 const char * __thiscall type_info_name_internal_method(type_info * _this, struct __type_info_node *node)
367 {
368     static int once;
369
370     if (node && !once++) FIXME("type_info_node parameter ignored\n");
371
372     if (!_this->name)
373     {
374         /* Create and set the demangled name */
375         /* Note: mangled name in type_info struct always starts with a '.', while
376          * it isn't valid for mangled name.
377          * Is this '.' really part of the mangled name, or has it some other meaning ?
378          */
379         char* name = __unDName(0, _this->mangled + 1, 0, malloc, free, 0x2800);
380         if (name)
381         {
382             unsigned int len = strlen(name);
383
384             /* It seems _unDName may leave blanks at the end of the demangled name */
385             while (len && name[--len] == ' ')
386                 name[len] = '\0';
387
388             if (InterlockedCompareExchangePointer((void**)&_this->name, name, NULL))
389             {
390                 /* Another thread set this member since we checked above - use it */
391                 free(name);
392             }
393         }
394     }
395     TRACE("(%p) returning %s\n", _this, _this->name);
396     return _this->name;
397 }
398
399 /*********************************************************************
400  * _CRT_RTC_INIT (MSVCR100.@)
401  */
402 void* CDECL _CRT_RTC_INIT(void *unk1, void *unk2, int unk3, int unk4, int unk5)
403 {
404     TRACE("%p %p %x %x %x\n", unk1, unk2, unk3, unk4, unk5);
405     return NULL;
406 }
407
408 /*********************************************************************
409  * _CRT_RTC_INITW (MSVCR100.@)
410  */
411 void* CDECL _CRT_RTC_INITW(void *unk1, void *unk2, int unk3, int unk4, int unk5)
412 {
413     TRACE("%p %p %x %x %x\n", unk1, unk2, unk3, unk4, unk5);
414     return NULL;
415 }
416
417 /*********************************************************************
418  * _vswprintf_p (MSVCR100.@)
419  */
420 int CDECL _vswprintf_p(wchar_t *buffer, size_t length, const wchar_t *format, __ms_va_list args)
421 {
422     return _vswprintf_p_l(buffer, length, format, NULL, args);
423 }
424
425 /*********************************************************************
426  * _byteswap_ushort (MSVCR100.@)
427  */
428 unsigned short CDECL _byteswap_ushort(unsigned short s)
429 {
430     return (s<<8) + (s>>8);
431 }
432
433 /*********************************************************************
434  * _byteswap_ulong (MSVCR100.@)
435  */
436 ULONG CDECL _byteswap_ulong(ULONG l)
437 {
438     return (l<<24) + ((l<<8)&0xFF0000) + ((l>>8)&0xFF00) + (l>>24);
439 }
440
441 /*********************************************************************
442  * _byteswap_uint64 (MSVCR100.@)
443  */
444 unsigned __int64 CDECL _byteswap_uint64(unsigned __int64 i)
445 {
446     return (i<<56) + ((i&0xFF00)<<40) + ((i&0xFF0000)<<24) + ((i&0xFF000000)<<8) +
447         ((i>>8)&0xFF000000) + ((i>>24)&0xFF0000) + ((i>>40)&0xFF00) + (i>>56);
448 }
449
450 /*********************************************************************
451  * _sprintf_p (MSVCR100.@)
452  */
453 int CDECL _sprintf_p(char *buffer, size_t length, const char *format, ...)
454 {
455     __ms_va_list valist;
456     int r;
457
458     __ms_va_start(valist, format);
459     r = _vsprintf_p_l(buffer, length, format, NULL, valist);
460     __ms_va_end(valist);
461
462     return r;
463 }
464
465 /*********************************************************************
466  * _get_timezone (MSVCR100.@)
467  */
468 int CDECL _get_timezone(LONG *timezone)
469 {
470     if(!CHECK_PMT(timezone != NULL)) return EINVAL;
471
472     *timezone = *(LONG*)GetProcAddress(GetModuleHandleA("msvcrt.dll"), "_timezone");
473     return 0;
474 }
475
476 /*********************************************************************
477  * _get_daylight (MSVCR100.@)
478  */
479 int CDECL _get_daylight(int *hours)
480 {
481     if(!CHECK_PMT(hours != NULL)) return EINVAL;
482
483     *hours = *(int*)GetProcAddress(GetModuleHandleA("msvcrt.dll"), "_daylight");
484     return 0;
485 }
486
487 /* copied from dlls/msvcrt/heap.c */
488 #define SAVED_PTR(x) ((void *)((DWORD_PTR)((char *)x - sizeof(void *)) & \
489                                ~(sizeof(void *) - 1)))
490
491 /*********************************************************************
492  * _aligned_msize (MSVCR100.@)
493  */
494 size_t CDECL _aligned_msize(void *p, size_t alignment, size_t offset)
495 {
496     void **alloc_ptr;
497
498     if(!CHECK_PMT(p)) return -1;
499
500     if(alignment < sizeof(void*))
501         alignment = sizeof(void*);
502
503     alloc_ptr = SAVED_PTR(p);
504     return _msize(*alloc_ptr)-alignment-sizeof(void*);
505 }
506
507 /*********************************************************************
508  *  DllMain (MSVCR100.@)
509  */
510 BOOL WINAPI DllMain(HINSTANCE hdll, DWORD reason, LPVOID reserved)
511 {
512     switch (reason)
513     {
514     case DLL_WINE_PREATTACH:
515         return FALSE;  /* prefer native version */
516
517     case DLL_PROCESS_ATTACH:
518         DisableThreadLibraryCalls(hdll);
519         _set_printf_count_output(0);
520     }
521     return TRUE;
522 }