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