2001-01-10 23:59:25 +00:00
|
|
|
/*
|
|
|
|
* msvcrt.dll misc functions
|
|
|
|
*
|
|
|
|
* Copyright 2000 Jon Griffiths
|
2002-03-09 23:29:33 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 12:49:52 +00:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2001-01-10 23:59:25 +00:00
|
|
|
*/
|
|
|
|
|
2003-03-15 22:26:09 +00:00
|
|
|
#include "config.h"
|
|
|
|
#include "wine/port.h"
|
|
|
|
|
2001-04-10 23:25:25 +00:00
|
|
|
#include <stdlib.h>
|
2001-04-23 18:20:55 +00:00
|
|
|
|
|
|
|
#include "msvcrt.h"
|
2002-01-22 00:57:16 +00:00
|
|
|
#include "wine/debug.h"
|
2010-01-15 17:02:56 +00:00
|
|
|
#include "ntsecapi.h"
|
2002-01-22 00:57:16 +00:00
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
|
2001-01-10 23:59:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _beep (MSVCRT.@)
|
|
|
|
*/
|
2011-06-03 13:28:32 +00:00
|
|
|
void CDECL MSVCRT__beep( unsigned int freq, unsigned int duration)
|
2001-01-10 23:59:25 +00:00
|
|
|
{
|
|
|
|
TRACE(":Freq %d, Duration %d\n",freq,duration);
|
|
|
|
Beep(freq, duration);
|
|
|
|
}
|
|
|
|
|
2006-01-14 16:27:28 +00:00
|
|
|
/*********************************************************************
|
|
|
|
* srand (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 09:21:19 +00:00
|
|
|
void CDECL MSVCRT_srand( unsigned int seed )
|
2006-01-14 16:27:28 +00:00
|
|
|
{
|
|
|
|
thread_data_t *data = msvcrt_get_thread_data();
|
|
|
|
data->random_seed = seed;
|
|
|
|
}
|
|
|
|
|
2001-01-10 23:59:25 +00:00
|
|
|
/*********************************************************************
|
|
|
|
* rand (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 09:21:19 +00:00
|
|
|
int CDECL MSVCRT_rand(void)
|
2001-01-10 23:59:25 +00:00
|
|
|
{
|
2006-01-14 16:27:28 +00:00
|
|
|
thread_data_t *data = msvcrt_get_thread_data();
|
|
|
|
|
|
|
|
/* this is the algorithm used by MSVC, according to
|
|
|
|
* http://en.wikipedia.org/wiki/List_of_pseudorandom_number_generators */
|
|
|
|
data->random_seed = data->random_seed * 214013 + 2531011;
|
|
|
|
return (data->random_seed >> 16) & MSVCRT_RAND_MAX;
|
2001-01-10 23:59:25 +00:00
|
|
|
}
|
|
|
|
|
2010-01-15 17:02:56 +00:00
|
|
|
/*********************************************************************
|
|
|
|
* rand_s (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
int CDECL MSVCRT_rand_s(unsigned int *pval)
|
|
|
|
{
|
|
|
|
if (!pval || !RtlGenRandom(pval, sizeof(*pval)))
|
|
|
|
{
|
|
|
|
*MSVCRT__errno() = MSVCRT_EINVAL;
|
|
|
|
return MSVCRT_EINVAL;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-01-10 23:59:25 +00:00
|
|
|
/*********************************************************************
|
|
|
|
* _sleep (MSVCRT.@)
|
|
|
|
*/
|
2009-05-23 09:49:09 +00:00
|
|
|
void CDECL MSVCRT__sleep(MSVCRT_ulong timeout)
|
2001-01-10 23:59:25 +00:00
|
|
|
{
|
2009-05-23 09:49:09 +00:00
|
|
|
TRACE("_sleep for %d milliseconds\n",timeout);
|
2001-01-10 23:59:25 +00:00
|
|
|
Sleep((timeout)?timeout:1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _lfind (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 09:21:19 +00:00
|
|
|
void* CDECL _lfind(const void* match, const void* start,
|
|
|
|
unsigned int* array_size, unsigned int elem_size,
|
2010-05-12 15:15:29 +00:00
|
|
|
int (CDECL *cf)(const void*,const void*) )
|
2001-01-10 23:59:25 +00:00
|
|
|
{
|
|
|
|
unsigned int size = *array_size;
|
|
|
|
if (size)
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (cf(match, start) == 0)
|
|
|
|
return (void *)start; /* found */
|
2006-09-18 21:18:41 +00:00
|
|
|
start = (const char *)start + elem_size;
|
2001-01-10 23:59:25 +00:00
|
|
|
} while (--size);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _lsearch (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 09:21:19 +00:00
|
|
|
void* CDECL _lsearch(const void* match, void* start,
|
|
|
|
unsigned int* array_size, unsigned int elem_size,
|
2010-05-12 15:15:29 +00:00
|
|
|
int (CDECL *cf)(const void*,const void*) )
|
2001-01-10 23:59:25 +00:00
|
|
|
{
|
|
|
|
unsigned int size = *array_size;
|
|
|
|
if (size)
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (cf(match, start) == 0)
|
|
|
|
return start; /* found */
|
2002-07-03 21:10:43 +00:00
|
|
|
start = (char*)start + elem_size;
|
2001-01-10 23:59:25 +00:00
|
|
|
} while (--size);
|
|
|
|
|
|
|
|
/* not found, add to end */
|
|
|
|
memcpy(start, match, elem_size);
|
|
|
|
array_size[0]++;
|
|
|
|
return start;
|
|
|
|
}
|
|
|
|
|
2012-07-16 18:36:10 +00:00
|
|
|
/*********************************************************************
|
|
|
|
* bsearch_s (msvcrt.@)
|
|
|
|
*/
|
|
|
|
void* CDECL MSVCRT_bsearch_s(const void *key, const void *base,
|
|
|
|
MSVCRT_size_t nmemb, MSVCRT_size_t size,
|
|
|
|
int (__cdecl *compare)(void *, const void *, const void *), void *ctx)
|
|
|
|
{
|
|
|
|
ssize_t min = 0;
|
|
|
|
ssize_t max = nmemb - 1;
|
|
|
|
|
2012-07-26 09:05:32 +00:00
|
|
|
if (!MSVCRT_CHECK_PMT(size != 0)) return NULL;
|
|
|
|
if (!MSVCRT_CHECK_PMT(compare != NULL)) return NULL;
|
2012-07-16 18:36:10 +00:00
|
|
|
|
|
|
|
while (min <= max)
|
|
|
|
{
|
|
|
|
ssize_t cursor = (min + max) / 2;
|
|
|
|
int ret = compare(ctx, key,(const char *)base+(cursor*size));
|
|
|
|
if (!ret)
|
|
|
|
return (char*)base+(cursor*size);
|
|
|
|
if (ret < 0)
|
|
|
|
max = cursor - 1;
|
|
|
|
else
|
|
|
|
min = cursor + 1;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-01-10 23:59:25 +00:00
|
|
|
/*********************************************************************
|
|
|
|
* _chkesp (MSVCRT.@)
|
2003-03-15 22:26:09 +00:00
|
|
|
*
|
|
|
|
* Trap to a debugger if the value of the stack pointer has changed.
|
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* None.
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* Does not return.
|
|
|
|
*
|
|
|
|
* NOTES
|
|
|
|
* This function is available for iX86 only.
|
|
|
|
*
|
|
|
|
* When VC++ generates debug code, it stores the value of the stack pointer
|
|
|
|
* before calling any external function, and checks the value following
|
|
|
|
* the call. It then calls this function, which will trap if the values are
|
|
|
|
* not the same. Usually this means that the prototype used to call
|
|
|
|
* the function is incorrect. It can also mean that the .spec entry has
|
|
|
|
* the wrong calling convention or parameters.
|
2001-01-10 23:59:25 +00:00
|
|
|
*/
|
2003-03-15 22:26:09 +00:00
|
|
|
#ifdef __i386__
|
|
|
|
|
|
|
|
# ifdef __GNUC__
|
2004-05-17 21:08:31 +00:00
|
|
|
|
2003-03-15 22:26:09 +00:00
|
|
|
__ASM_GLOBAL_FUNC(_chkesp,
|
|
|
|
"jnz 1f\n\t"
|
|
|
|
"ret\n"
|
|
|
|
"1:\tpushl %ebp\n\t"
|
2009-06-25 09:50:42 +00:00
|
|
|
__ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
|
|
|
|
__ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
|
2003-03-15 22:26:09 +00:00
|
|
|
"movl %esp,%ebp\n\t"
|
2009-06-25 09:50:42 +00:00
|
|
|
__ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
|
2005-11-23 18:55:06 +00:00
|
|
|
"subl $12,%esp\n\t"
|
2003-03-15 22:26:09 +00:00
|
|
|
"pushl %eax\n\t"
|
|
|
|
"pushl %ecx\n\t"
|
|
|
|
"pushl %edx\n\t"
|
|
|
|
"call " __ASM_NAME("MSVCRT_chkesp_fail") "\n\t"
|
|
|
|
"popl %edx\n\t"
|
|
|
|
"popl %ecx\n\t"
|
|
|
|
"popl %eax\n\t"
|
2005-11-23 18:55:06 +00:00
|
|
|
"leave\n\t"
|
2009-06-25 09:50:42 +00:00
|
|
|
__ASM_CFI(".cfi_def_cfa %esp,4\n\t")
|
|
|
|
__ASM_CFI(".cfi_same_value %ebp\n\t")
|
2007-01-12 23:38:07 +00:00
|
|
|
"ret")
|
2001-01-10 23:59:25 +00:00
|
|
|
|
2006-06-13 09:21:19 +00:00
|
|
|
void CDECL MSVCRT_chkesp_fail(void)
|
2003-03-15 22:26:09 +00:00
|
|
|
{
|
|
|
|
ERR("Stack pointer incorrect after last function call - Bad prototype/spec entry?\n");
|
|
|
|
DebugBreak();
|
2001-01-10 23:59:25 +00:00
|
|
|
}
|
2003-03-15 22:26:09 +00:00
|
|
|
|
|
|
|
# else /* __GNUC__ */
|
2004-05-17 21:08:31 +00:00
|
|
|
|
|
|
|
/**********************************************************************/
|
|
|
|
|
2006-06-13 09:21:19 +00:00
|
|
|
void CDECL _chkesp(void)
|
2004-05-17 21:08:31 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2003-03-15 22:26:09 +00:00
|
|
|
# endif /* __GNUC__ */
|
|
|
|
|
|
|
|
#endif /* __i386__ */
|
2010-11-17 16:07:47 +00:00
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* Helper function for MSVCRT_qsort_s.
|
|
|
|
*
|
|
|
|
* Based on NTDLL_qsort in dlls/ntdll/misc.c
|
|
|
|
*/
|
|
|
|
static void MSVCRT_mergesort( void *arr, void *barr, size_t elemsize,
|
|
|
|
int (CDECL *compar)(void *, const void *, const void *),
|
|
|
|
size_t left, size_t right, void *context )
|
|
|
|
{
|
|
|
|
if (right>left) {
|
|
|
|
size_t i, j, k, m;
|
|
|
|
m=left+(right-left)/2;
|
|
|
|
MSVCRT_mergesort(arr, barr, elemsize, compar, left, m, context);
|
|
|
|
MSVCRT_mergesort(arr, barr, elemsize, compar, m+1, right, context);
|
|
|
|
|
|
|
|
#define X(a,i) ((char*)a+elemsize*(i))
|
|
|
|
for (i=m+1; i>left; i--)
|
|
|
|
memcpy (X(barr,(i-1)),X(arr,(i-1)),elemsize);
|
|
|
|
for (j=m; j<right; j++)
|
|
|
|
memcpy (X(barr,(right+m-j)),X(arr,(j+1)),elemsize);
|
|
|
|
|
|
|
|
/* i=left; j=right; */
|
|
|
|
for (k=left; i<=m && j>m; k++) {
|
|
|
|
if (i==j || compar(context, X(barr,i),X(barr,j))<=0) {
|
|
|
|
memcpy(X(arr,k),X(barr,i),elemsize);
|
|
|
|
i++;
|
|
|
|
} else {
|
|
|
|
memcpy(X(arr,k),X(barr,j),elemsize);
|
|
|
|
j--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (; i<=m; i++, k++)
|
|
|
|
memcpy(X(arr,k),X(barr,i),elemsize);
|
|
|
|
for (; j>m; j--, k++)
|
|
|
|
memcpy(X(arr,k),X(barr,j),elemsize);
|
|
|
|
}
|
|
|
|
#undef X
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* qsort_s (MSVCRT.@)
|
|
|
|
*
|
|
|
|
* Based on NTDLL_qsort in dlls/ntdll/misc.c
|
|
|
|
*/
|
|
|
|
void CDECL MSVCRT_qsort_s(void *base, MSVCRT_size_t nmemb, MSVCRT_size_t size,
|
|
|
|
int (CDECL *compar)(void *, const void *, const void *), void *context)
|
|
|
|
{
|
|
|
|
void *secondarr;
|
|
|
|
const size_t total_size = nmemb*size;
|
|
|
|
|
2012-07-26 09:05:32 +00:00
|
|
|
if (!MSVCRT_CHECK_PMT(base != NULL || (base == NULL && nmemb == 0))) return;
|
|
|
|
if (!MSVCRT_CHECK_PMT(size > 0)) return;
|
|
|
|
if (!MSVCRT_CHECK_PMT(compar != NULL)) return;
|
|
|
|
if (total_size / size != nmemb) return;
|
2010-11-17 16:07:47 +00:00
|
|
|
|
|
|
|
if (nmemb < 2) return;
|
|
|
|
|
|
|
|
secondarr = MSVCRT_malloc(total_size);
|
|
|
|
if (!secondarr)
|
|
|
|
return;
|
|
|
|
MSVCRT_mergesort(base, secondarr, size, compar, 0, nmemb-1, context);
|
|
|
|
MSVCRT_free(secondarr);
|
|
|
|
}
|
2011-03-08 19:41:07 +00:00
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _get_output_format (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
unsigned int CDECL _get_output_format(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2011-04-20 10:37:56 +00:00
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _resetstkoflw (MSVCRT.@)
|
|
|
|
*/
|
2011-06-03 13:28:32 +00:00
|
|
|
int CDECL MSVCRT__resetstkoflw(void)
|
2011-04-20 10:37:56 +00:00
|
|
|
{
|
|
|
|
int stack_addr;
|
|
|
|
|
|
|
|
/* causes stack fault that updates NtCurrentTeb()->Tib.StackLimit */
|
|
|
|
return VirtualProtect( &stack_addr, 1, PAGE_GUARD|PAGE_READWRITE, NULL );
|
|
|
|
}
|