kernel32: Add a shared memory test.
[wine] / dlls / msvcrt / misc.c
1 /*
2  * msvcrt.dll misc functions
3  *
4  * Copyright 2000 Jon Griffiths
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 "wine/port.h"
23
24 #include <stdlib.h>
25
26 #include "msvcrt.h"
27 #include "wine/debug.h"
28 #include "ntsecapi.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
31
32 static unsigned int output_format;
33
34 /*********************************************************************
35  *              _beep (MSVCRT.@)
36  */
37 void CDECL MSVCRT__beep( unsigned int freq, unsigned int duration)
38 {
39     TRACE(":Freq %d, Duration %d\n",freq,duration);
40     Beep(freq, duration);
41 }
42
43 /*********************************************************************
44  *              srand (MSVCRT.@)
45  */
46 void CDECL MSVCRT_srand( unsigned int seed )
47 {
48     thread_data_t *data = msvcrt_get_thread_data();
49     data->random_seed = seed;
50 }
51
52 /*********************************************************************
53  *              rand (MSVCRT.@)
54  */
55 int CDECL MSVCRT_rand(void)
56 {
57     thread_data_t *data = msvcrt_get_thread_data();
58
59     /* this is the algorithm used by MSVC, according to
60      * http://en.wikipedia.org/wiki/List_of_pseudorandom_number_generators */
61     data->random_seed = data->random_seed * 214013 + 2531011;
62     return (data->random_seed >> 16) & MSVCRT_RAND_MAX;
63 }
64
65 /*********************************************************************
66  *              rand_s (MSVCRT.@)
67  */
68 int CDECL MSVCRT_rand_s(unsigned int *pval)
69 {
70     if (!pval || !RtlGenRandom(pval, sizeof(*pval)))
71     {
72         *MSVCRT__errno() = MSVCRT_EINVAL;
73         return MSVCRT_EINVAL;
74     }
75     return 0;
76 }
77
78 /*********************************************************************
79  *              _sleep (MSVCRT.@)
80  */
81 void CDECL MSVCRT__sleep(MSVCRT_ulong timeout)
82 {
83   TRACE("_sleep for %d milliseconds\n",timeout);
84   Sleep((timeout)?timeout:1);
85 }
86
87 /*********************************************************************
88  *              _lfind (MSVCRT.@)
89  */
90 void* CDECL _lfind(const void* match, const void* start,
91                    unsigned int* array_size, unsigned int elem_size,
92                    int (CDECL *cf)(const void*,const void*) )
93 {
94   unsigned int size = *array_size;
95   if (size)
96     do
97     {
98       if (cf(match, start) == 0)
99         return (void *)start; /* found */
100       start = (const char *)start + elem_size;
101     } while (--size);
102   return NULL;
103 }
104
105 /*********************************************************************
106  *              _lsearch (MSVCRT.@)
107  */
108 void* CDECL _lsearch(const void* match, void* start,
109                      unsigned int* array_size, unsigned int elem_size,
110                      int (CDECL *cf)(const void*,const void*) )
111 {
112   unsigned int size = *array_size;
113   if (size)
114     do
115     {
116       if (cf(match, start) == 0)
117         return start; /* found */
118       start = (char*)start + elem_size;
119     } while (--size);
120
121   /* not found, add to end */
122   memcpy(start, match, elem_size);
123   array_size[0]++;
124   return start;
125 }
126
127 /*********************************************************************
128  *                  bsearch_s (msvcrt.@)
129  */
130 void* CDECL MSVCRT_bsearch_s(const void *key, const void *base,
131                              MSVCRT_size_t nmemb, MSVCRT_size_t size,
132                              int (__cdecl *compare)(void *, const void *, const void *), void *ctx)
133 {
134     ssize_t min = 0;
135     ssize_t max = nmemb - 1;
136
137     if (!MSVCRT_CHECK_PMT(size != 0)) return NULL;
138     if (!MSVCRT_CHECK_PMT(compare != NULL)) return NULL;
139
140     while (min <= max)
141     {
142         ssize_t cursor = (min + max) / 2;
143         int ret = compare(ctx, key,(const char *)base+(cursor*size));
144         if (!ret)
145             return (char*)base+(cursor*size);
146         if (ret < 0)
147             max = cursor - 1;
148         else
149             min = cursor + 1;
150     }
151     return NULL;
152 }
153
154 /*********************************************************************
155  *              _chkesp (MSVCRT.@)
156  *
157  * Trap to a debugger if the value of the stack pointer has changed.
158  *
159  * PARAMS
160  *  None.
161  *
162  * RETURNS
163  *  Does not return.
164  *
165  * NOTES
166  *  This function is available for iX86 only.
167  *
168  *  When VC++ generates debug code, it stores the value of the stack pointer
169  *  before calling any external function, and checks the value following
170  *  the call. It then calls this function, which will trap if the values are
171  *  not the same. Usually this means that the prototype used to call
172  *  the function is incorrect.  It can also mean that the .spec entry has
173  *  the wrong calling convention or parameters.
174  */
175 #ifdef __i386__
176
177 # ifdef __GNUC__
178
179 __ASM_GLOBAL_FUNC(_chkesp,
180                   "jnz 1f\n\t"
181                   "ret\n"
182                   "1:\tpushl %ebp\n\t"
183                   __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
184                   __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
185                   "movl %esp,%ebp\n\t"
186                   __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
187                   "subl $12,%esp\n\t"
188                   "pushl %eax\n\t"
189                   "pushl %ecx\n\t"
190                   "pushl %edx\n\t"
191                   "call " __ASM_NAME("MSVCRT_chkesp_fail") "\n\t"
192                   "popl %edx\n\t"
193                   "popl %ecx\n\t"
194                   "popl %eax\n\t"
195                   "leave\n\t"
196                   __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
197                   __ASM_CFI(".cfi_same_value %ebp\n\t")
198                   "ret")
199
200 void CDECL MSVCRT_chkesp_fail(void)
201 {
202   ERR("Stack pointer incorrect after last function call - Bad prototype/spec entry?\n");
203   DebugBreak();
204 }
205
206 # else  /* __GNUC__ */
207
208 /**********************************************************************/
209
210 void CDECL _chkesp(void)
211 {
212 }
213
214 # endif  /* __GNUC__ */
215
216 #endif  /* __i386__ */
217
218 /*********************************************************************
219  * Helper function for MSVCRT_qsort_s.
220  *
221  * Based on NTDLL_qsort in dlls/ntdll/misc.c
222  */
223 static void MSVCRT_mergesort( void *arr, void *barr, size_t elemsize,
224         int (CDECL *compar)(void *, const void *, const void *),
225         size_t left, size_t right, void *context )
226 {
227     if (right>left) {
228         size_t i, j, k, m;
229         m=left+(right-left)/2;
230         MSVCRT_mergesort(arr, barr, elemsize, compar, left, m, context);
231         MSVCRT_mergesort(arr, barr, elemsize, compar, m+1, right, context);
232
233 #define X(a,i) ((char*)a+elemsize*(i))
234         for (i=m+1; i>left; i--)
235             memcpy (X(barr,(i-1)),X(arr,(i-1)),elemsize);
236         for (j=m; j<right; j++)
237             memcpy (X(barr,(right+m-j)),X(arr,(j+1)),elemsize);
238
239         /* i=left; j=right; */
240         for (k=left; i<=m && j>m; k++) {
241             if (i==j || compar(context, X(barr,i),X(barr,j))<=0) {
242                 memcpy(X(arr,k),X(barr,i),elemsize);
243                 i++;
244             } else {
245                 memcpy(X(arr,k),X(barr,j),elemsize);
246                 j--;
247             }
248         }
249         for (; i<=m; i++, k++)
250             memcpy(X(arr,k),X(barr,i),elemsize);
251         for (; j>m; j--, k++)
252             memcpy(X(arr,k),X(barr,j),elemsize);
253     }
254 #undef X
255 }
256
257 /*********************************************************************
258  * qsort_s (MSVCRT.@)
259  *
260  * Based on NTDLL_qsort in dlls/ntdll/misc.c
261  */
262 void CDECL MSVCRT_qsort_s(void *base, MSVCRT_size_t nmemb, MSVCRT_size_t size,
263     int (CDECL *compar)(void *, const void *, const void *), void *context)
264 {
265     void *secondarr;
266     const size_t total_size = nmemb*size;
267
268     if (!MSVCRT_CHECK_PMT(base != NULL || (base == NULL && nmemb == 0))) return;
269     if (!MSVCRT_CHECK_PMT(size > 0)) return;
270     if (!MSVCRT_CHECK_PMT(compar != NULL)) return;
271     if (total_size / size != nmemb) return;
272
273     if (nmemb < 2) return;
274
275     secondarr = MSVCRT_malloc(total_size);
276     if (!secondarr)
277         return;
278     MSVCRT_mergesort(base, secondarr, size, compar, 0, nmemb-1, context);
279     MSVCRT_free(secondarr);
280 }
281
282 /*********************************************************************
283  * _get_output_format (MSVCRT.@)
284  */
285 unsigned int CDECL _get_output_format(void)
286 {
287    return output_format;
288 }
289
290 /*********************************************************************
291  * _set_output_format (MSVCRT.@)
292  */
293 unsigned int CDECL _set_output_format(unsigned int new_output_format)
294 {
295     unsigned int ret = output_format;
296
297     if(!MSVCRT_CHECK_PMT(new_output_format==0 || new_output_format==MSVCRT__TWO_DIGIT_EXPONENT))
298         return ret;
299
300     output_format = new_output_format;
301     return ret;
302 }
303
304 /*********************************************************************
305  * _resetstkoflw (MSVCRT.@)
306  */
307 int CDECL MSVCRT__resetstkoflw(void)
308 {
309     int stack_addr;
310
311     /* causes stack fault that updates NtCurrentTeb()->Tib.StackLimit */
312     return VirtualProtect( &stack_addr, 1, PAGE_GUARD|PAGE_READWRITE, NULL );
313 }