bcrypt: Add BCryptDeriveKey stub.
[wine.git] / libs / wine / port.c
blob990b7b7a743e5ce2bcee9344f6462d05395676f9
1 /*
2 * Wine portability routines
4 * Copyright 2000 Alexandre Julliard
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.
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.
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/types.h>
28 #define WINE_UNICODE_INLINE /* nothing */
29 #include "wine/unicode.h"
30 #include "wine/library.h"
31 #include "wine/asm.h"
33 /* functions from libwine_port that are also exported from libwine for backwards compatibility,
34 * on platforms that require it */
35 #ifndef __ANDROID__
36 const void *libwine_port_functions[] =
38 strtolW,
39 vsnprintfW,
40 wine_compare_string,
41 wine_cp_enum_table,
42 wine_cp_get_table,
43 wine_cp_mbstowcs,
44 wine_cp_wcstombs,
45 wine_cpsymbol_mbstowcs,
46 wine_cpsymbol_wcstombs,
47 wine_fold_string,
48 wine_utf8_mbstowcs,
49 wine_utf8_wcstombs
51 #endif
53 /* no longer used, for backwards compatibility only */
54 struct wine_pthread_functions;
55 static void *pthread_functions[8];
57 /***********************************************************************
58 * wine_pthread_get_functions
60 void wine_pthread_get_functions( struct wine_pthread_functions *functions, size_t size )
62 memcpy( functions, &pthread_functions, min( size, sizeof(pthread_functions) ));
66 /***********************************************************************
67 * wine_pthread_set_functions
69 void wine_pthread_set_functions( const struct wine_pthread_functions *functions, size_t size )
71 memcpy( &pthread_functions, functions, min( size, sizeof(pthread_functions) ));
75 /***********************************************************************
76 * wine_switch_to_stack
78 * Switch to the specified stack and call the function.
80 void DECLSPEC_NORETURN wine_switch_to_stack( void (*func)(void *), void *arg, void *stack )
82 wine_call_on_stack( (int (*)(void *))func, arg, stack );
83 abort();
87 /***********************************************************************
88 * wine_call_on_stack
90 * Switch to the specified stack to call the function and return.
93 #if defined(__i386__) && defined(__GNUC__)
94 __ASM_GLOBAL_FUNC( wine_call_on_stack,
95 "pushl %ebp\n\t"
96 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
97 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
98 "pushl %esi\n\t"
99 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
100 __ASM_CFI(".cfi_rel_offset %esi,0\n\t")
101 "movl %esp,%esi\n\t"
102 __ASM_CFI(".cfi_def_cfa_register %esi\n\t")
103 "movl 12(%esp),%ecx\n\t" /* func */
104 "movl 16(%esp),%edx\n\t" /* arg */
105 "movl 20(%esp),%eax\n\t" /* stack */
106 "andl $~15,%eax\n\t"
107 "subl $12,%eax\n\t"
108 "movl %eax,%esp\n\t"
109 "pushl %edx\n\t"
110 "xorl %ebp,%ebp\n\t"
111 "call *%ecx\n\t"
112 "movl %esi,%esp\n\t"
113 "popl %esi\n\t"
114 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
115 __ASM_CFI(".cfi_same_value %esi\n\t")
116 "popl %ebp\n\t"
117 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
118 __ASM_CFI(".cfi_same_value %ebp\n\t")
119 "ret" )
120 #elif defined(__i386__) && defined(_MSC_VER)
121 __declspec(naked) int wine_call_on_stack( int (*func)(void *), void *arg, void *stack )
123 __asm push ebp;
124 __asm push esi;
125 __asm mov ecx, 12[esp];
126 __asm mov edx, 16[esp];
127 __asm mov esi, 20[esp];
128 __asm xchg esp, esi;
129 __asm push edx;
130 __asm xor ebp, ebp;
131 __asm call [ecx];
132 __asm mov esp, esi;
133 __asm pop esi;
134 __asm pop ebp;
135 __asm ret;
137 #elif defined(__x86_64__) && defined(__GNUC__)
138 __ASM_GLOBAL_FUNC( wine_call_on_stack,
139 "pushq %rbp\n\t"
140 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
141 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
142 "movq %rsp,%rbp\n\t"
143 __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
144 "movq %rdi,%rax\n\t" /* func */
145 "movq %rsi,%rdi\n\t" /* arg */
146 "andq $~15,%rdx\n\t" /* stack */
147 "movq %rdx,%rsp\n\t"
148 "callq *%rax\n\t" /* call func */
149 "movq %rbp,%rsp\n\t"
150 __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
151 "popq %rbp\n\t"
152 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
153 __ASM_CFI(".cfi_same_value %rbp\n\t")
154 "ret")
155 #elif defined(__powerpc__) && defined(__GNUC__)
156 __ASM_GLOBAL_FUNC( wine_call_on_stack,
157 "mflr 0\n\t" /* get return address */
158 "stw 0, 4(1)\n\t" /* save return address */
159 "subi 5, 5, 16\n\t" /* reserve space on new stack */
160 "stw 1, 12(5)\n\t" /* store old sp */
161 "mtctr 3\n\t" /* func -> ctr */
162 "mr 3,4\n\t" /* args -> function param 1 (r3) */
163 "mr 1,5\n\t" /* stack */
164 "li 0, 0\n\t" /* zero */
165 "stw 0, 0(1)\n\t" /* bottom of stack */
166 "stwu 1, -16(1)\n\t" /* create a frame for this function */
167 "bctrl\n\t" /* call ctr */
168 "lwz 1, 28(1)\n\t" /* fetch old sp */
169 "lwz 0, 4(1)\n\t" /* fetch return address */
170 "mtlr 0\n\t" /* return address -> lr */
171 "blr") /* return */
172 #elif defined(__arm__) && defined(__GNUC__)
173 __ASM_GLOBAL_FUNC( wine_call_on_stack,
174 "push {r4,LR}\n\t" /* save return address on stack */
175 "mov r4, sp\n\t" /* store old sp in local var */
176 "mov sp, r2\n\t" /* stack */
177 "mov r2, r0\n\t" /* func -> scratch register */
178 "mov r0, r1\n\t" /* arg */
179 "blx r2\n\t" /* call func */
180 "mov sp, r4\n\t" /* restore old sp from local var */
181 "pop {r4,PC}") /* fetch return address into pc */
182 #elif defined(__aarch64__) && defined(__GNUC__)
183 __ASM_GLOBAL_FUNC( wine_call_on_stack,
184 "stp x29, x30, [sp,#-32]!\n\t" /* save return address on stack */
185 "str x19, [sp,#16]\n\t" /* save register on stack */
186 "mov x19, sp\n\t" /* store old sp in local var */
187 "mov sp, x2\n\t" /* stack */
188 "mov x2, x0\n\t" /* func -> scratch register */
189 "mov x0, x1\n\t" /* arg */
190 "blr x2\n\t" /* call func */
191 "mov sp, x19\n\t" /* restore old sp from local var */
192 "ldr x19, [sp,#16]\n\t" /* restore register from stack */
193 "ldp x29, x30, [sp],#32\n\t" /* restore return address */
194 "ret") /* return */
195 #else
196 #error You must implement wine_call_on_stack for your platform
197 #endif