misc.c 16.7 KB
Newer Older
1 2
/*
 * Helper functions for ntdll
3 4
 *
 * Copyright 2000 Juergen Schmied
5
 * Copyright 2010 Marcus Meissner
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 21
 */

22 23 24
#include <time.h>
#include <math.h>

25 26
#include "ntstatus.h"
#define WIN32_NO_STATUS
27
#include "wine/debug.h"
28
#include "ntdll_misc.h"
29 30
#include "wmistr.h"
#include "evntrace.h"
31
#include "evntprov.h"
32

33
WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
34

35
LPCSTR debugstr_us( const UNICODE_STRING *us )
36
{
37
    if (!us) return "<null>";
38
    return debugstr_wn(us->Buffer, us->Length / sizeof(WCHAR));
39 40
}

41 42 43
/*********************************************************************
 *                  abs   (NTDLL.@)
 */
44
int CDECL abs( int i )
45
{
46
    return i >= 0 ? i : -i;
47 48 49 50 51
}

/*********************************************************************
 *                  atan   (NTDLL.@)
 */
52
double CDECL atan( double d )
53
{
54
    return unix_funcs->atan( d );
55 56 57 58 59
}

/*********************************************************************
 *                  ceil   (NTDLL.@)
 */
60
double CDECL ceil( double d )
61
{
62
    return unix_funcs->ceil( d );
63 64 65 66 67
}

/*********************************************************************
 *                  cos   (NTDLL.@)
 */
68
double CDECL cos( double d )
69
{
70
    return unix_funcs->cos( d );
71 72 73 74 75
}

/*********************************************************************
 *                  fabs   (NTDLL.@)
 */
76
double CDECL fabs( double d )
77
{
78
    return unix_funcs->fabs( d );
79 80 81 82 83
}

/*********************************************************************
 *                  floor   (NTDLL.@)
 */
84
double CDECL floor( double d )
85
{
86
    return unix_funcs->floor( d );
87 88 89 90 91
}

/*********************************************************************
 *                  log   (NTDLL.@)
 */
92
double CDECL log( double d )
93
{
94
    return unix_funcs->log( d );
95 96 97 98 99
}

/*********************************************************************
 *                  pow   (NTDLL.@)
 */
100
double CDECL pow( double x, double y )
101
{
102
    return unix_funcs->pow( x, y );
103 104 105 106 107
}

/*********************************************************************
 *                  sin   (NTDLL.@)
 */
108
double CDECL sin( double d )
109
{
110
    return unix_funcs->sin( d );
111 112 113 114 115
}

/*********************************************************************
 *                  sqrt   (NTDLL.@)
 */
116
double CDECL sqrt( double d )
117
{
118
    return unix_funcs->sqrt( d );
119 120 121 122 123
}

/*********************************************************************
 *                  tan   (NTDLL.@)
 */
124
double CDECL tan( double d )
125
{
126
    return unix_funcs->tan( d );
127
}
128

129
#if (defined(__GNUC__) || defined(__clang__)) && defined(__i386__)
130 131 132 133 134 135 136 137 138 139

#define FPU_DOUBLE(var) double var; \
    __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var) : )
#define FPU_DOUBLES(var1,var2) double var1,var2; \
    __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var2) : ); \
    __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var1) : )

/*********************************************************************
 *		_CIcos (NTDLL.@)
 */
140
double CDECL _CIcos(void)
141 142
{
    FPU_DOUBLE(x);
143
    return cos(x);
144 145 146 147 148
}

/*********************************************************************
 *		_CIlog (NTDLL.@)
 */
149
double CDECL _CIlog(void)
150 151
{
    FPU_DOUBLE(x);
152
    return log(x);
153 154 155 156 157
}

/*********************************************************************
 *		_CIpow (NTDLL.@)
 */
158
double CDECL _CIpow(void)
159 160
{
    FPU_DOUBLES(x,y);
161
    return pow(x,y);
162 163 164 165 166
}

/*********************************************************************
 *		_CIsin (NTDLL.@)
 */
167
double CDECL _CIsin(void)
168 169
{
    FPU_DOUBLE(x);
170
    return sin(x);
171 172 173 174 175
}

/*********************************************************************
 *		_CIsqrt (NTDLL.@)
 */
176
double CDECL _CIsqrt(void)
177 178
{
    FPU_DOUBLE(x);
179
    return sqrt(x);
180 181 182 183 184
}

/*********************************************************************
 *                  _ftol   (NTDLL.@)
 */
185
LONGLONG CDECL _ftol(void)
186 187 188 189 190
{
    FPU_DOUBLE(x);
    return (LONGLONG)x;
}

191
#endif /* (defined(__GNUC__) || defined(__clang__)) && defined(__i386__) */
192

193
static void
194 195
NTDLL_mergesort( void *arr, void *barr, size_t elemsize, int(__cdecl *compar)(const void *, const void *),
                 size_t left, size_t right )
196 197
{
    if(right>left) {
198
        size_t i, j, k, m;
199
        m=left+(right-left)/2;
200 201
        NTDLL_mergesort( arr, barr, elemsize, compar, left, m);
        NTDLL_mergesort( arr, barr, elemsize, compar, m+1, right);
202 203

#define X(a,i) ((char*)a+elemsize*(i))
204 205 206
        for (k=left, i=left, j=m+1; i<=m && j<=right; k++) {
            if (compar(X(arr, i), X(arr,j)) <= 0) {
                memcpy(X(barr,k), X(arr, i), elemsize);
207 208
                i++;
            } else {
209 210
                memcpy(X(barr,k), X(arr, j), elemsize);
                j++;
211 212
            }
        }
213 214 215 216 217 218
        if (i<=m)
            memcpy(X(barr,k), X(arr,i), (m-i+1)*elemsize);
        else
            memcpy(X(barr,k), X(arr,j), (right-j+1)*elemsize);

        memcpy(X(arr, left), X(barr, left), (right-left+1)*elemsize);
219 220 221 222 223 224 225
    }
#undef X
}

/*********************************************************************
 *                  qsort   (NTDLL.@)
 */
226 227
void __cdecl qsort( void *base, size_t nmemb, size_t size,
                    int (__cdecl *compar)(const void *, const void *) )
228
{
229 230 231
    void *secondarr;
    if (nmemb < 2 || size == 0) return;
    secondarr = RtlAllocateHeap (GetProcessHeap(), 0, nmemb*size);
232
    NTDLL_mergesort( base, secondarr, size, compar, 0, nmemb-1 );
233 234
    RtlFreeHeap (GetProcessHeap(),0, secondarr);
}
235 236 237 238

/*********************************************************************
 *                  bsearch   (NTDLL.@)
 */
239 240
void * __cdecl bsearch( const void *key, const void *base, size_t nmemb,
                        size_t size, int (__cdecl *compar)(const void *, const void *) )
241
{
242 243 244 245 246
    ssize_t min = 0;
    ssize_t max = nmemb - 1;

    while (min <= max)
    {
247
        ssize_t cursor = min + (max - min) / 2;
248
        int ret = compar(key,(const char *)base+(cursor*size));
249 250 251
        if (!ret)
            return (char*)base+(cursor*size);
        if (ret < 0)
252
            max = cursor - 1;
253
        else
254
            min = cursor + 1;
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
    }
    return NULL;
}


/*********************************************************************
 *                  _lfind   (NTDLL.@)
 */
void * __cdecl _lfind( const void *key, const void *base, unsigned int *nmemb,
                       size_t size, int(__cdecl *compar)(const void *, const void *) )
{
    size_t i, n = *nmemb;

    for (i=0;i<n;i++)
        if (!compar(key,(char*)base+(size*i)))
            return (char*)base+(size*i);
    return NULL;
}
273

274 275 276 277 278 279 280 281 282
/******************************************************************************
 *                  WinSqmEndSession   (NTDLL.@)
 */
NTSTATUS WINAPI WinSqmEndSession(HANDLE session)
{
    FIXME("(%p): stub\n", session);
    return STATUS_NOT_IMPLEMENTED;
}

283 284 285 286 287 288 289 290
/*********************************************************************
 *          WinSqmIncrementDWORD (NTDLL.@)
 */
void WINAPI WinSqmIncrementDWORD(DWORD unk1, DWORD unk2, DWORD unk3)
{
    FIXME("(%d, %d, %d): stub\n", unk1, unk2, unk3);
}

291 292 293 294 295
/*********************************************************************
 *                  WinSqmIsOptedIn   (NTDLL.@)
 */
BOOL WINAPI WinSqmIsOptedIn(void)
{
296
    FIXME("(): stub\n");
297 298
    return FALSE;
}
299

300 301 302 303 304 305 306 307 308
/******************************************************************************
 *                  WinSqmStartSession   (NTDLL.@)
 */
HANDLE WINAPI WinSqmStartSession(GUID *sessionguid, DWORD sessionid, DWORD unknown1)
{
    FIXME("(%p, 0x%x, 0x%x): stub\n", sessionguid, sessionid, unknown1);
    return INVALID_HANDLE_VALUE;
}

309 310 311 312 313 314 315 316
/***********************************************************************
 *          WinSqmSetDWORD (NTDLL.@)
 */
void WINAPI WinSqmSetDWORD(HANDLE session, DWORD datapoint_id, DWORD datapoint_value)
{
    FIXME("(%p, %d, %d): stub\n", session, datapoint_id, datapoint_value);
}

317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
/******************************************************************************
 *                  EtwEventActivityIdControl (NTDLL.@)
 */
ULONG WINAPI EtwEventActivityIdControl(ULONG code, GUID *guid)
{
    static int once;

    if (!once++) FIXME("0x%x, %p: stub\n", code, guid);
    return ERROR_SUCCESS;
}

/******************************************************************************
 *                  EtwEventProviderEnabled (NTDLL.@)
 */
BOOLEAN WINAPI EtwEventProviderEnabled( REGHANDLE handle, UCHAR level, ULONGLONG keyword )
{
    FIXME("%s, %u, %s: stub\n", wine_dbgstr_longlong(handle), level, wine_dbgstr_longlong(keyword));
    return FALSE;
}

337 338 339 340 341 342 343 344
/******************************************************************************
 *                  EtwEventRegister (NTDLL.@)
 */
ULONG WINAPI EtwEventRegister( LPCGUID provider, PENABLECALLBACK callback, PVOID context,
                PREGHANDLE handle )
{
    FIXME("(%s, %p, %p, %p) stub.\n", debugstr_guid(provider), callback, context, handle);

345 346
    if (!handle) return ERROR_INVALID_PARAMETER;

347 348 349 350 351 352 353 354 355 356 357 358 359
    *handle = 0xdeadbeef;
    return ERROR_SUCCESS;
}

/******************************************************************************
 *                  EtwEventUnregister (NTDLL.@)
 */
ULONG WINAPI EtwEventUnregister( REGHANDLE handle )
{
    FIXME("(%s) stub.\n", wine_dbgstr_longlong(handle));
    return ERROR_SUCCESS;
}

360 361 362 363 364 365 366 367 368 369
/*********************************************************************
 *                  EtwEventSetInformation   (NTDLL.@)
 */
ULONG WINAPI EtwEventSetInformation( REGHANDLE handle, EVENT_INFO_CLASS class, void *info,
                                     ULONG length )
{
    FIXME("(%s, %u, %p, %u) stub\n", wine_dbgstr_longlong(handle), class, info, length);
    return ERROR_SUCCESS;
}

370 371 372 373 374 375 376 377 378 379
/******************************************************************************
 *                  EtwEventWriteString   (NTDLL.@)
 */
ULONG WINAPI EtwEventWriteString( REGHANDLE handle, UCHAR level, ULONGLONG keyword, PCWSTR string )
{
    FIXME("%s, %u, %s, %s: stub\n", wine_dbgstr_longlong(handle), level,
          wine_dbgstr_longlong(keyword), debugstr_w(string));
    return ERROR_SUCCESS;
}

380 381 382 383 384 385 386 387 388 389 390
/******************************************************************************
 *                  EtwEventWriteTransfer   (NTDLL.@)
 */
ULONG WINAPI EtwEventWriteTransfer( REGHANDLE handle, PCEVENT_DESCRIPTOR descriptor, LPCGUID activity,
                                    LPCGUID related, ULONG count, PEVENT_DATA_DESCRIPTOR data )
{
    FIXME("%s, %p, %s, %s, %u, %p: stub\n", wine_dbgstr_longlong(handle), descriptor,
          debugstr_guid(activity), debugstr_guid(related), count, data);
    return ERROR_SUCCESS;
}

391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
/******************************************************************************
 *                  EtwRegisterTraceGuidsW (NTDLL.@)
 *
 * Register an event trace provider and the event trace classes that it uses
 * to generate events.
 *
 * PARAMS
 *  RequestAddress     [I]   ControlCallback function
 *  RequestContext     [I]   Optional provider-defined context
 *  ControlGuid        [I]   GUID of the registering provider
 *  GuidCount          [I]   Number of elements in the TraceGuidReg array
 *  TraceGuidReg       [I/O] Array of TRACE_GUID_REGISTRATION structures
 *  MofImagePath       [I]   not supported, set to NULL
 *  MofResourceName    [I]   not supported, set to NULL
 *  RegistrationHandle [O]   Provider's registration handle
 *
 * RETURNS
 *  Success: ERROR_SUCCESS
 *  Failure: System error code
 */
ULONG WINAPI EtwRegisterTraceGuidsW( WMIDPREQUEST RequestAddress,
                void *RequestContext, const GUID *ControlGuid, ULONG GuidCount,
                TRACE_GUID_REGISTRATION *TraceGuidReg, const WCHAR *MofImagePath,
                const WCHAR *MofResourceName, TRACEHANDLE *RegistrationHandle )
{
    FIXME("(%p, %p, %s, %u, %p, %s, %s, %p): stub\n", RequestAddress, RequestContext,
          debugstr_guid(ControlGuid), GuidCount, TraceGuidReg, debugstr_w(MofImagePath),
          debugstr_w(MofResourceName), RegistrationHandle);

    if (TraceGuidReg)
    {
        ULONG i;
        for (i = 0; i < GuidCount; i++)
        {
            FIXME("  register trace class %s\n", debugstr_guid(TraceGuidReg[i].Guid));
            TraceGuidReg[i].RegHandle = (HANDLE)0xdeadbeef;
        }
    }
    *RegistrationHandle = (TRACEHANDLE)0xdeadbeef;
    return ERROR_SUCCESS;
}

/******************************************************************************
 *                  EtwRegisterTraceGuidsA (NTDLL.@)
 */
ULONG WINAPI EtwRegisterTraceGuidsA( WMIDPREQUEST RequestAddress,
                void *RequestContext, const GUID *ControlGuid, ULONG GuidCount,
                TRACE_GUID_REGISTRATION *TraceGuidReg, const char *MofImagePath,
                const char *MofResourceName, TRACEHANDLE *RegistrationHandle )
{
    FIXME("(%p, %p, %s, %u, %p, %s, %s, %p): stub\n", RequestAddress, RequestContext,
          debugstr_guid(ControlGuid), GuidCount, TraceGuidReg, debugstr_a(MofImagePath),
          debugstr_a(MofResourceName), RegistrationHandle);
    return ERROR_SUCCESS;
}
446 447 448 449 450 451 452 453 454 455 456 457

/******************************************************************************
 *                  EtwUnregisterTraceGuids (NTDLL.@)
 */
ULONG WINAPI EtwUnregisterTraceGuids( TRACEHANDLE RegistrationHandle )
{
    if (!RegistrationHandle)
         return ERROR_INVALID_PARAMETER;

    FIXME("%s: stub\n", wine_dbgstr_longlong(RegistrationHandle));
    return ERROR_SUCCESS;
}
458 459 460 461 462 463 464 465 466

/******************************************************************************
 *                  EtwEventEnabled (NTDLL.@)
 */
BOOLEAN WINAPI EtwEventEnabled( REGHANDLE handle, const EVENT_DESCRIPTOR *descriptor )
{
    FIXME("(%s, %p): stub\n", wine_dbgstr_longlong(handle), descriptor);
    return FALSE;
}
467 468 469 470 471 472 473 474 475 476

/******************************************************************************
 *                  EtwEventWrite (NTDLL.@)
 */
ULONG WINAPI EtwEventWrite( REGHANDLE handle, const EVENT_DESCRIPTOR *descriptor, ULONG count,
    EVENT_DATA_DESCRIPTOR *data )
{
    FIXME("(%s, %p, %u, %p): stub\n", wine_dbgstr_longlong(handle), descriptor, count, data);
    return ERROR_SUCCESS;
}
477

478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
/******************************************************************************
 *                  EtwGetTraceEnableFlags (NTDLL.@)
 */
ULONG WINAPI EtwGetTraceEnableFlags( TRACEHANDLE handle )
{
    FIXME("(%s) stub\n", wine_dbgstr_longlong(handle));
    return 0;
}

/******************************************************************************
 *                  EtwGetTraceEnableLevel (NTDLL.@)
 */
UCHAR WINAPI EtwGetTraceEnableLevel( TRACEHANDLE handle )
{
    FIXME("(%s) stub\n", wine_dbgstr_longlong(handle));
    return TRACE_LEVEL_VERBOSE;
}

/******************************************************************************
 *                  EtwGetTraceLoggerHandle (NTDLL.@)
 */
TRACEHANDLE WINAPI EtwGetTraceLoggerHandle( PVOID buf )
{
    FIXME("(%p) stub\n", buf);
    return INVALID_PROCESSTRACE_HANDLE;
}

/******************************************************************************
 *                  EtwLogTraceEvent (NTDLL.@)
 */
ULONG WINAPI EtwLogTraceEvent( TRACEHANDLE SessionHandle, PEVENT_TRACE_HEADER EventTrace )
{
    FIXME("%s %p\n", wine_dbgstr_longlong(SessionHandle), EventTrace);
    return ERROR_CALL_NOT_IMPLEMENTED;
}

/******************************************************************************
 *                  EtwTraceMessageVa (NTDLL.@)
 */
ULONG WINAPI EtwTraceMessageVa( TRACEHANDLE handle, ULONG flags, LPGUID guid, USHORT number,
                                __ms_va_list args )
{
    FIXME("(%s %x %s %d) : stub\n", wine_dbgstr_longlong(handle), flags, debugstr_guid(guid), number);
    return ERROR_SUCCESS;
}

/******************************************************************************
 *                  EtwTraceMessage (NTDLL.@)
 */
527
ULONG WINAPIV EtwTraceMessage( TRACEHANDLE handle, ULONG flags, LPGUID guid, /*USHORT*/ ULONG number, ... )
528 529 530 531 532 533 534 535 536 537
{
    __ms_va_list valist;
    ULONG ret;

    __ms_va_start( valist, number );
    ret = EtwTraceMessageVa( handle, flags, guid, number, valist );
    __ms_va_end( valist );
    return ret;
}

538 539 540 541 542 543 544 545 546 547 548
/*********************************************************************
 *                  ApiSetQueryApiSetPresence   (NTDLL.@)
 */
BOOL WINAPI ApiSetQueryApiSetPresence(const UNICODE_STRING *namespace, BOOLEAN *present)
{
    FIXME("(%s, %p) stub!\n", debugstr_us(namespace), present);

    if(present)
        *present = TRUE;
    return TRUE;
}