misc.c 15.2 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
 */

#include "config.h"

24 25
#include <time.h>
#include <math.h>
26 27 28
#ifdef HAVE_SYS_UTSNAME_H
#include <sys/utsname.h>
#endif
29

30 31
#include "ntstatus.h"
#define WIN32_NO_STATUS
32
#include "wine/library.h"
33
#include "wine/debug.h"
34
#include "ntdll_misc.h"
35 36
#include "wmistr.h"
#include "evntrace.h"
37
#include "evntprov.h"
38

39
WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
40

41
LPCSTR debugstr_ObjectAttributes(const OBJECT_ATTRIBUTES *oa)
42
{
43 44 45 46
    if (!oa) return "<null>";
    return wine_dbg_sprintf( "{name=%s, attr=0x%08x, hRoot=%p, sd=%p}\n",
                             debugstr_us(oa->ObjectName), oa->Attributes,
                             oa->RootDirectory, oa->SecurityDescriptor );
47 48
}

49
LPCSTR debugstr_us( const UNICODE_STRING *us )
50
{
51
    if (!us) return "<null>";
52
    return debugstr_wn(us->Buffer, us->Length / sizeof(WCHAR));
53 54
}

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
/*********************************************************************
 *                  wine_get_version   (NTDLL.@)
 */
const char * CDECL NTDLL_wine_get_version(void)
{
    return wine_get_version();
}

/*********************************************************************
 *                  wine_get_build_id   (NTDLL.@)
 */
const char * CDECL NTDLL_wine_get_build_id(void)
{
    return wine_get_build_id();
}

71 72 73 74 75 76 77
/*********************************************************************
 *                  wine_get_host_version   (NTDLL.@)
 */
void CDECL NTDLL_wine_get_host_version( const char **sysname, const char **release )
{
#ifdef HAVE_SYS_UTSNAME_H
    static struct utsname buf;
78
    static BOOL init_done;
79 80 81 82

    if (!init_done)
    {
        uname( &buf );
83
        init_done = TRUE;
84 85 86 87 88 89 90 91 92
    }
    if (sysname) *sysname = buf.sysname;
    if (release) *release = buf.release;
#else
    if (sysname) *sysname = "";
    if (release) *release = "";
#endif
}

93 94 95
/*********************************************************************
 *                  abs   (NTDLL.@)
 */
96
int CDECL NTDLL_abs( int i )
97 98 99 100 101 102 103
{
    return abs( i );
}

/*********************************************************************
 *                  labs   (NTDLL.@)
 */
104
LONG CDECL NTDLL_labs( LONG i )
105 106 107 108 109 110 111
{
    return labs( i );
}

/*********************************************************************
 *                  atan   (NTDLL.@)
 */
112
double CDECL NTDLL_atan( double d )
113 114 115 116 117 118 119
{
    return atan( d );
}

/*********************************************************************
 *                  ceil   (NTDLL.@)
 */
120
double CDECL NTDLL_ceil( double d )
121 122 123 124 125 126 127
{
    return ceil( d );
}

/*********************************************************************
 *                  cos   (NTDLL.@)
 */
128
double CDECL NTDLL_cos( double d )
129 130 131 132 133 134 135
{
    return cos( d );
}

/*********************************************************************
 *                  fabs   (NTDLL.@)
 */
136
double CDECL NTDLL_fabs( double d )
137 138 139 140 141 142 143
{
    return fabs( d );
}

/*********************************************************************
 *                  floor   (NTDLL.@)
 */
144
double CDECL NTDLL_floor( double d )
145 146 147 148 149 150 151
{
    return floor( d );
}

/*********************************************************************
 *                  log   (NTDLL.@)
 */
152
double CDECL NTDLL_log( double d )
153 154 155 156 157 158 159
{
    return log( d );
}

/*********************************************************************
 *                  pow   (NTDLL.@)
 */
160
double CDECL NTDLL_pow( double x, double y )
161 162 163 164 165 166 167
{
    return pow( x, y );
}

/*********************************************************************
 *                  sin   (NTDLL.@)
 */
168
double CDECL NTDLL_sin( double d )
169 170 171 172 173 174 175
{
    return sin( d );
}

/*********************************************************************
 *                  sqrt   (NTDLL.@)
 */
176
double CDECL NTDLL_sqrt( double d )
177 178 179 180 181 182 183
{
    return sqrt( d );
}

/*********************************************************************
 *                  tan   (NTDLL.@)
 */
184
double CDECL NTDLL_tan( double d )
185 186 187
{
    return tan( d );
}
188

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
#if defined(__GNUC__) && defined(__i386__)

#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.@)
 */
double CDECL NTDLL__CIcos(void)
{
    FPU_DOUBLE(x);
    return NTDLL_cos(x);
}

/*********************************************************************
 *		_CIlog (NTDLL.@)
 */
double CDECL NTDLL__CIlog(void)
{
    FPU_DOUBLE(x);
    return NTDLL_log(x);
}

/*********************************************************************
 *		_CIpow (NTDLL.@)
 */
double CDECL NTDLL__CIpow(void)
{
    FPU_DOUBLES(x,y);
    return NTDLL_pow(x,y);
}

/*********************************************************************
 *		_CIsin (NTDLL.@)
 */
double CDECL NTDLL__CIsin(void)
{
    FPU_DOUBLE(x);
    return NTDLL_sin(x);
}

/*********************************************************************
 *		_CIsqrt (NTDLL.@)
 */
double CDECL NTDLL__CIsqrt(void)
{
    FPU_DOUBLE(x);
    return NTDLL_sqrt(x);
}

/*********************************************************************
 *                  _ftol   (NTDLL.@)
 */
LONGLONG CDECL NTDLL__ftol(void)
{
    FPU_DOUBLE(x);
    return (LONGLONG)x;
}

#endif /* defined(__GNUC__) && defined(__i386__) */
252

253
static void
254 255
NTDLL_mergesort( void *arr, void *barr, size_t elemsize, int(__cdecl *compar)(const void *, const void *),
                 size_t left, size_t right )
256 257
{
    if(right>left) {
258
        size_t i, j, k, m;
259
        m=left+(right-left)/2;
260 261
        NTDLL_mergesort( arr, barr, elemsize, compar, left, m);
        NTDLL_mergesort( arr, barr, elemsize, compar, m+1, right);
262 263

#define X(a,i) ((char*)a+elemsize*(i))
264 265 266
        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);
267 268
                i++;
            } else {
269 270
                memcpy(X(barr,k), X(arr, j), elemsize);
                j++;
271 272
            }
        }
273 274 275 276 277 278
        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);
279 280 281 282 283 284 285 286 287 288
    }
#undef X
}

/*********************************************************************
 *                  qsort   (NTDLL.@)
 */
void __cdecl NTDLL_qsort( void *base, size_t nmemb, size_t size,
                          int(__cdecl *compar)(const void *, const void *) )
{
289 290 291
    void *secondarr;
    if (nmemb < 2 || size == 0) return;
    secondarr = RtlAllocateHeap (GetProcessHeap(), 0, nmemb*size);
292
    NTDLL_mergesort( base, secondarr, size, compar, 0, nmemb-1 );
293 294
    RtlFreeHeap (GetProcessHeap(),0, secondarr);
}
295 296 297 298 299 300 301 302

/*********************************************************************
 *                  bsearch   (NTDLL.@)
 */
void * __cdecl
NTDLL_bsearch( const void *key, const void *base, size_t nmemb,
               size_t size, int (__cdecl *compar)(const void *, const void *) )
{
303 304 305 306 307
    ssize_t min = 0;
    ssize_t max = nmemb - 1;

    while (min <= max)
    {
308
        ssize_t cursor = min + (max - min) / 2;
309
        int ret = compar(key,(const char *)base+(cursor*size));
310 311 312
        if (!ret)
            return (char*)base+(cursor*size);
        if (ret < 0)
313
            max = cursor - 1;
314
        else
315
            min = cursor + 1;
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
    }
    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;
}
334

335 336 337 338 339 340 341 342 343
/******************************************************************************
 *                  WinSqmEndSession   (NTDLL.@)
 */
NTSTATUS WINAPI WinSqmEndSession(HANDLE session)
{
    FIXME("(%p): stub\n", session);
    return STATUS_NOT_IMPLEMENTED;
}

344 345 346 347 348
/*********************************************************************
 *                  WinSqmIsOptedIn   (NTDLL.@)
 */
BOOL WINAPI WinSqmIsOptedIn(void)
{
349
    FIXME("(): stub\n");
350 351
    return FALSE;
}
352

353 354 355 356 357 358 359 360 361
/******************************************************************************
 *                  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;
}

362 363 364 365 366 367 368 369
/***********************************************************************
 *          WinSqmSetDWORD (NTDLL.@)
 */
void WINAPI WinSqmSetDWORD(HANDLE session, DWORD datapoint_id, DWORD datapoint_value)
{
    FIXME("(%p, %d, %d): stub\n", session, datapoint_id, datapoint_value);
}

370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
/******************************************************************************
 *                  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);

    *handle = 0xdeadbeef;
    return ERROR_SUCCESS;
}

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

391 392 393 394 395 396 397 398 399 400
/*********************************************************************
 *                  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;
}

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 446 447 448 449 450 451 452 453 454 455
/******************************************************************************
 *                  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;
}
456 457 458 459 460 461 462 463 464 465 466 467

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

    FIXME("%s: stub\n", wine_dbgstr_longlong(RegistrationHandle));
    return ERROR_SUCCESS;
}
468 469 470 471 472 473 474 475 476

/******************************************************************************
 *                  EtwEventEnabled (NTDLL.@)
 */
BOOLEAN WINAPI EtwEventEnabled( REGHANDLE handle, const EVENT_DESCRIPTOR *descriptor )
{
    FIXME("(%s, %p): stub\n", wine_dbgstr_longlong(handle), descriptor);
    return FALSE;
}
477 478 479 480 481 482 483 484 485 486

/******************************************************************************
 *                  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;
}
487 488 489 490 491 492 493 494

/***********************************************************************
 *		    DbgUiRemoteBreakin (NTDLL.@)
 */
void WINAPI DbgUiRemoteBreakin( void *arg )
{
    FIXME("stub\n");
}
495 496 497 498 499 500 501 502 503 504 505 506

NTSTATUS WINAPI NtCreateLowBoxToken(HANDLE *token_handle, HANDLE existing_token_handle, ACCESS_MASK desired_access,
                                    OBJECT_ATTRIBUTES *object_attributes, SID *package_sid, ULONG capability_count,
                                    SID_AND_ATTRIBUTES *capabilities, ULONG handle_count, HANDLE *handle)
{
    FIXME("(%p, %p, %x, %p, %p, %u, %p, %u, %p): stub\n", token_handle, existing_token_handle, desired_access,
            object_attributes, package_sid, capability_count, capabilities, handle_count, handle);

    /* We need to return a NULL handle since later it will be passed to CloseHandle and that must not fail */
    *token_handle = NULL;
    return STATUS_SUCCESS;
}
507 508 509 510 511 512 513 514 515 516 517 518

/*********************************************************************
 *                  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;
}