misc.c 16.3 KB
Newer Older
1 2 3 4
/*
 * msvcrt.dll misc functions
 *
 * Copyright 2000 Jon Griffiths
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * 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
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20
 */

21 22 23
#include "config.h"
#include "wine/port.h"

24
#include <stdlib.h>
25 26

#include "msvcrt.h"
27
#include "wine/debug.h"
28
#include "ntsecapi.h"
29
#include "windows.h"
30
#include "wine/asm.h"
31 32

WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
33

34
static unsigned int output_format;
35 36 37 38

/*********************************************************************
 *		_beep (MSVCRT.@)
 */
39
void CDECL MSVCRT__beep( unsigned int freq, unsigned int duration)
40 41 42 43 44
{
    TRACE(":Freq %d, Duration %d\n",freq,duration);
    Beep(freq, duration);
}

45 46 47
/*********************************************************************
 *		srand (MSVCRT.@)
 */
48
void CDECL MSVCRT_srand( unsigned int seed )
49 50 51 52 53
{
    thread_data_t *data = msvcrt_get_thread_data();
    data->random_seed = seed;
}

54 55 56
/*********************************************************************
 *		rand (MSVCRT.@)
 */
57
int CDECL MSVCRT_rand(void)
58
{
59 60 61 62 63 64
    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;
65 66
}

67 68 69 70 71 72 73 74 75 76 77 78 79
/*********************************************************************
 *		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;
}

80 81 82
/*********************************************************************
 *		_sleep (MSVCRT.@)
 */
83
void CDECL MSVCRT__sleep(MSVCRT_ulong timeout)
84
{
85
  TRACE("_sleep for %d milliseconds\n",timeout);
86 87 88 89 90 91
  Sleep((timeout)?timeout:1);
}

/*********************************************************************
 *		_lfind (MSVCRT.@)
 */
92 93
void* CDECL _lfind(const void* match, const void* start,
                   unsigned int* array_size, unsigned int elem_size,
94
                   int (CDECL *cf)(const void*,const void*) )
95 96 97 98 99 100 101
{
  unsigned int size = *array_size;
  if (size)
    do
    {
      if (cf(match, start) == 0)
        return (void *)start; /* found */
102
      start = (const char *)start + elem_size;
103 104 105 106
    } while (--size);
  return NULL;
}

Daniel Lehman's avatar
Daniel Lehman committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
/*********************************************************************
 *		_lfind_s (MSVCRT.@)
 */
void* CDECL _lfind_s(const void* match, const void* start,
                   unsigned int* array_size, unsigned int elem_size,
                   int (CDECL *cf)(void*,const void*,const void*),
                   void* context)
{
  unsigned int size;
  if (!MSVCRT_CHECK_PMT(match != NULL)) return NULL;
  if (!MSVCRT_CHECK_PMT(array_size != NULL)) return NULL;
  if (!MSVCRT_CHECK_PMT(start != NULL || *array_size == 0)) return NULL;
  if (!MSVCRT_CHECK_PMT(cf != NULL)) return NULL;
  if (!MSVCRT_CHECK_PMT(elem_size != 0)) return NULL;

  size = *array_size;
  if (size)
    do
    {
      if (cf(context, match, start) == 0)
        return (void *)start; /* found */
      start = (const char *)start + elem_size;
    } while (--size);
  return NULL;
}

133 134 135
/*********************************************************************
 *		_lsearch (MSVCRT.@)
 */
136 137
void* CDECL _lsearch(const void* match, void* start,
                     unsigned int* array_size, unsigned int elem_size,
138
                     int (CDECL *cf)(const void*,const void*) )
139 140 141 142 143 144 145
{
  unsigned int size = *array_size;
  if (size)
    do
    {
      if (cf(match, start) == 0)
        return start; /* found */
146
      start = (char*)start + elem_size;
147 148 149 150 151 152 153 154
    } while (--size);

  /* not found, add to end */
  memcpy(start, match, elem_size);
  array_size[0]++;
  return start;
}

155 156 157 158 159 160 161 162 163 164
/*********************************************************************
 *                  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;

165 166
    if (!MSVCRT_CHECK_PMT(size != 0)) return NULL;
    if (!MSVCRT_CHECK_PMT(compare != NULL)) return NULL;
167 168 169

    while (min <= max)
    {
170
        ssize_t cursor = min + (max - min) / 2;
171 172 173 174 175 176 177 178 179 180 181
        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;
}

182 183 184 185 186 187 188 189 190 191 192 193 194 195
static int CDECL compare_wrapper(void *ctx, const void *e1, const void *e2)
{
    int (__cdecl *compare)(const void *, const void *) = ctx;
    return compare(e1, e2);
}

/*********************************************************************
 *                  bsearch (msvcrt.@)
 */
void* CDECL MSVCRT_bsearch(const void *key, const void *base, MSVCRT_size_t nmemb,
        MSVCRT_size_t size, int (__cdecl *compar)(const void *, const void *))
{
    return MSVCRT_bsearch_s(key, base, nmemb, size, compare_wrapper, compar);
}
196 197
/*********************************************************************
 *		_chkesp (MSVCRT.@)
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
 *
 * 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.
216
 */
217 218 219
#ifdef __i386__

# ifdef __GNUC__
220

221 222 223 224
__ASM_GLOBAL_FUNC(_chkesp,
                  "jnz 1f\n\t"
                  "ret\n"
                  "1:\tpushl %ebp\n\t"
225 226
                  __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
                  __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
227
                  "movl %esp,%ebp\n\t"
228
                  __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
229
                  "subl $12,%esp\n\t"
230 231 232 233 234 235 236
                  "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"
237
                  "leave\n\t"
238 239
                  __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
                  __ASM_CFI(".cfi_same_value %ebp\n\t")
240
                  "ret")
241

242
void CDECL DECLSPEC_HIDDEN MSVCRT_chkesp_fail(void)
243 244 245
{
  ERR("Stack pointer incorrect after last function call - Bad prototype/spec entry?\n");
  DebugBreak();
246
}
247 248

# else  /* __GNUC__ */
249 250 251

/**********************************************************************/

252
void CDECL _chkesp(void)
253 254 255
{
}

256 257 258
# endif  /* __GNUC__ */

#endif  /* __i386__ */
259

260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
static inline void swap(char *l, char *r, MSVCRT_size_t size)
{
    char tmp;

    while(size--) {
        tmp = *l;
        *l++ = *r;
        *r++ = tmp;
    }
}

static void small_sort(void *base, MSVCRT_size_t nmemb, MSVCRT_size_t size,
        int (CDECL *compar)(void *, const void *, const void *), void *context)
{
    MSVCRT_size_t e, i;
    char *max, *p;

    for(e=nmemb; e>1; e--) {
        max = base;
        for(i=1; i<e; i++) {
            p = (char*)base + i*size;
            if(compar(context, p, max) > 0)
                max = p;
        }

        if(p != max)
            swap(p, max, size);
    }
}

static void quick_sort(void *base, MSVCRT_size_t nmemb, MSVCRT_size_t size,
        int (CDECL *compar)(void *, const void *, const void *), void *context)
292
{
293 294 295
    MSVCRT_size_t stack_lo[8*sizeof(MSVCRT_size_t)], stack_hi[8*sizeof(MSVCRT_size_t)];
    MSVCRT_size_t beg, end, lo, hi, med;
    int stack_pos;
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312

    stack_pos = 0;
    stack_lo[stack_pos] = 0;
    stack_hi[stack_pos] = nmemb-1;

#define X(i) ((char*)base+size*(i))
    while(stack_pos >= 0) {
        beg = stack_lo[stack_pos];
        end = stack_hi[stack_pos--];

        if(end-beg < 8) {
            small_sort(X(beg), end-beg+1, size, compar, context);
            continue;
        }

        lo = beg;
        hi = end;
313
        med = lo + (hi-lo+1)/2;
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
        if(compar(context, X(lo), X(med)) > 0)
            swap(X(lo), X(med), size);
        if(compar(context, X(lo), X(hi)) > 0)
            swap(X(lo), X(hi), size);
        if(compar(context, X(med), X(hi)) > 0)
            swap(X(med), X(hi), size);

        lo++;
        hi--;
        while(1) {
            while(lo <= hi) {
                if(lo!=med && compar(context, X(lo), X(med))>0)
                    break;
                lo++;
            }

            while(med != hi) {
                if(compar(context, X(hi), X(med)) <= 0)
                    break;
                hi--;
334
            }
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361

            if(hi < lo)
                break;

            swap(X(lo), X(hi), size);
            if(hi == med)
                med = lo;
            lo++;
            hi--;
        }

        while(hi > beg) {
            if(hi!=med && compar(context, X(hi), X(med))!=0)
                break;
            hi--;
        }

        if(hi-beg >= end-lo) {
            stack_lo[++stack_pos] = beg;
            stack_hi[stack_pos] = hi;
            stack_lo[++stack_pos] = lo;
            stack_hi[stack_pos] = end;
        }else {
            stack_lo[++stack_pos] = lo;
            stack_hi[stack_pos] = end;
            stack_lo[++stack_pos] = beg;
            stack_hi[stack_pos] = hi;
362 363 364 365 366 367 368 369
        }
    }
#undef X
}

/*********************************************************************
 * qsort_s (MSVCRT.@)
 *
370 371
 * This function is trying to sort data doing identical comparisons
 * as native does. There are still cases where it behaves differently.
372 373 374 375
 */
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)
{
376
    const MSVCRT_size_t total_size = nmemb*size;
377

378 379 380 381
    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;
382 383 384

    if (nmemb < 2) return;

385
    quick_sort(base, nmemb, size, compar, context);
386
}
387

388 389 390 391 392 393
/*********************************************************************
 * qsort (MSVCRT.@)
 */
void CDECL MSVCRT_qsort(void *base, MSVCRT_size_t nmemb, MSVCRT_size_t size,
        int (CDECL *compar)(const void*, const void*))
{
394
    MSVCRT_qsort_s(base, nmemb, size, compare_wrapper, compar);
395 396
}

397 398 399
/*********************************************************************
 * _get_output_format (MSVCRT.@)
 */
400
unsigned int CDECL MSVCRT__get_output_format(void)
401
{
402 403 404 405 406 407
   return output_format;
}

/*********************************************************************
 * _set_output_format (MSVCRT.@)
 */
408
unsigned int CDECL MSVCRT__set_output_format(unsigned int new_output_format)
409 410 411 412 413 414 415 416
{
    unsigned int ret = output_format;

    if(!MSVCRT_CHECK_PMT(new_output_format==0 || new_output_format==MSVCRT__TWO_DIGIT_EXPONENT))
        return ret;

    output_format = new_output_format;
    return ret;
417
}
418 419 420 421

/*********************************************************************
 * _resetstkoflw (MSVCRT.@)
 */
422
int CDECL MSVCRT__resetstkoflw(void)
423 424
{
    int stack_addr;
425
    DWORD oldprot;
426 427

    /* causes stack fault that updates NtCurrentTeb()->Tib.StackLimit */
428
    return VirtualProtect(&stack_addr, 1, PAGE_GUARD|PAGE_READWRITE, &oldprot);
429
}
430

431
#if _MSVCR_VER>=80 && _MSVCR_VER<=90
432

433
/*********************************************************************
434
 *  _decode_pointer (MSVCR80.@)
435 436 437 438 439 440 441
 */
void * CDECL MSVCRT_decode_pointer(void * ptr)
{
    return DecodePointer(ptr);
}

/*********************************************************************
442
 *  _encode_pointer (MSVCR80.@)
443 444 445 446 447 448
 */
void * CDECL MSVCRT_encode_pointer(void * ptr)
{
    return EncodePointer(ptr);
}

449 450 451
#endif /* _MSVCR_VER>=80 && _MSVCR_VER<=90 */

#if _MSVCR_VER>=80 && _MSVCR_VER<=100
452
/*********************************************************************
453
 *  _encoded_null (MSVCR80.@)
454 455 456 457 458 459 460
 */
void * CDECL _encoded_null(void)
{
    TRACE("\n");

    return EncodePointer(NULL);
}
461
#endif
462 463

#if _MSVCR_VER>=70
464
/*********************************************************************
465
 * _CRT_RTC_INIT (MSVCR70.@)
466 467 468 469 470 471
 */
void* CDECL _CRT_RTC_INIT(void *unk1, void *unk2, int unk3, int unk4, int unk5)
{
    TRACE("%p %p %x %x %x\n", unk1, unk2, unk3, unk4, unk5);
    return NULL;
}
472 473 474
#endif

#if _MSVCR_VER>=80
475 476

/*********************************************************************
477
 * _CRT_RTC_INITW (MSVCR80.@)
478 479 480 481 482 483 484 485
 */
void* CDECL _CRT_RTC_INITW(void *unk1, void *unk2, int unk3, int unk4, int unk5)
{
    TRACE("%p %p %x %x %x\n", unk1, unk2, unk3, unk4, unk5);
    return NULL;
}

/*********************************************************************
486
 * _byteswap_ushort (MSVCR80.@)
487 488 489 490 491 492 493
 */
unsigned short CDECL _byteswap_ushort(unsigned short s)
{
    return (s<<8) + (s>>8);
}

/*********************************************************************
494
 * _byteswap_ulong (MSVCR80.@)
495 496 497 498 499 500 501
 */
ULONG CDECL MSVCRT__byteswap_ulong(ULONG l)
{
    return (l<<24) + ((l<<8)&0xFF0000) + ((l>>8)&0xFF00) + (l>>24);
}

/*********************************************************************
502
 * _byteswap_uint64 (MSVCR80.@)
503 504 505 506 507 508
 */
unsigned __int64 CDECL _byteswap_uint64(unsigned __int64 i)
{
    return (i<<56) + ((i&0xFF00)<<40) + ((i&0xFF0000)<<24) + ((i&0xFF000000)<<8) +
        ((i>>8)&0xFF000000) + ((i>>24)&0xFF0000) + ((i>>40)&0xFF00) + (i>>56);
}
509

510 511 512 513
#endif /* _MSVCR_VER>=80 */

#if _MSVCR_VER>=110

514 515 516 517 518 519 520 521
/*********************************************************************
 *  __crtGetShowWindowMode (MSVCR110.@)
 */
int CDECL MSVCR110__crtGetShowWindowMode(void)
{
    STARTUPINFOW si;

    GetStartupInfoW(&si);
522 523
    TRACE("flags=%x window=%d\n", si.dwFlags, si.wShowWindow);
    return si.dwFlags & STARTF_USESHOWWINDOW ? si.wShowWindow : SW_SHOWDEFAULT;
524
}
525 526 527 528 529 530 531 532 533 534

/*********************************************************************
 *  __crtInitializeCriticalSectionEx (MSVCR110.@)
 */
BOOL CDECL MSVCR110__crtInitializeCriticalSectionEx(
        CRITICAL_SECTION *cs, DWORD spin_count, DWORD flags)
{
    TRACE("(%p %x %x)\n", cs, spin_count, flags);
    return InitializeCriticalSectionEx(cs, spin_count, flags);
}
535

536 537 538
#endif /* _MSVCR_VER>=110 */

#if _MSVCR_VER>=120
539 540 541 542 543 544 545
/*********************************************************************
 * _vacopy (MSVCR120.@)
 */
void CDECL MSVCR120__vacopy(__ms_va_list *dest, __ms_va_list src)
{
    __ms_va_copy(*dest, src);
}
546
#endif
547

548
#if _MSVCR_VER>=80
549 550 551 552 553 554 555
/*********************************************************************
 * _crt_debugger_hook (MSVCR80.@)
 */
void CDECL MSVCRT__crt_debugger_hook(int reserved)
{
    WARN("(%x)\n", reserved);
}
556
#endif
557

558
#if _MSVCR_VER>=110
559 560 561 562 563 564 565 566 567
/*********************************************************************
 *  __crtUnhandledException (MSVCR110.@)
 */
LONG CDECL MSVCRT__crtUnhandledException(EXCEPTION_POINTERS *ep)
{
    TRACE("(%p)\n", ep);
    SetUnhandledExceptionFilter(NULL);
    return UnhandledExceptionFilter(ep);
}
568 569 570 571 572 573

/* ?_Trace_agents@Concurrency@@YAXW4Agents_EventType@1@_JZZ */
void WINAPIV _Trace_agents(/*enum Concurrency::Agents_EventType*/int type, __int64 id, ...)
{
    FIXME("(%d %s)\n", type, wine_dbgstr_longlong(id));
}
574
#endif
575

576
#if _MSVCR_VER>=120
577 578 579 580 581 582 583 584
/*********************************************************************
 *		__crtSleep (MSVCR120.@)
 */
void CDECL MSVCRT__crtSleep(DWORD timeout)
{
  TRACE("(%u)\n", timeout);
  Sleep(timeout);
}
585 586 587 588 589 590 591 592

/*********************************************************************
 * _SetWinRTOutOfMemoryExceptionCallback (MSVCR120.@)
 */
void CDECL MSVCR120__SetWinRTOutOfMemoryExceptionCallback(void *callback)
{
    FIXME("(%p): stub\n", callback);
}
593
#endif