server: Support unbound console input device.
[wine.git] / dlls / ntdll / misc.c
blob9c7708a24a5255fc74b66412456df4c51989289a
1 /*
2 * Helper functions for ntdll
4 * Copyright 2000 Juergen Schmied
5 * Copyright 2010 Marcus Meissner
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * 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
22 #include <time.h>
23 #include <math.h>
25 #include "ntstatus.h"
26 #define WIN32_NO_STATUS
27 #include "wine/debug.h"
28 #include "ntdll_misc.h"
29 #include "wmistr.h"
30 #include "evntrace.h"
31 #include "evntprov.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
35 LPCSTR debugstr_us( const UNICODE_STRING *us )
37 if (!us) return "<null>";
38 return debugstr_wn(us->Buffer, us->Length / sizeof(WCHAR));
41 /*********************************************************************
42 * abs (NTDLL.@)
44 int CDECL abs( int i )
46 return i >= 0 ? i : -i;
49 /*********************************************************************
50 * atan (NTDLL.@)
52 double CDECL atan( double d )
54 return unix_funcs->atan( d );
57 /*********************************************************************
58 * ceil (NTDLL.@)
60 double CDECL ceil( double d )
62 return unix_funcs->ceil( d );
65 /*********************************************************************
66 * cos (NTDLL.@)
68 double CDECL cos( double d )
70 return unix_funcs->cos( d );
73 /*********************************************************************
74 * fabs (NTDLL.@)
76 double CDECL fabs( double d )
78 return unix_funcs->fabs( d );
81 /*********************************************************************
82 * floor (NTDLL.@)
84 double CDECL floor( double d )
86 return unix_funcs->floor( d );
89 /*********************************************************************
90 * log (NTDLL.@)
92 double CDECL log( double d )
94 return unix_funcs->log( d );
97 /*********************************************************************
98 * pow (NTDLL.@)
100 double CDECL pow( double x, double y )
102 return unix_funcs->pow( x, y );
105 /*********************************************************************
106 * sin (NTDLL.@)
108 double CDECL sin( double d )
110 return unix_funcs->sin( d );
113 /*********************************************************************
114 * sqrt (NTDLL.@)
116 double CDECL sqrt( double d )
118 return unix_funcs->sqrt( d );
121 /*********************************************************************
122 * tan (NTDLL.@)
124 double CDECL tan( double d )
126 return unix_funcs->tan( d );
129 #if (defined(__GNUC__) || defined(__clang__)) && defined(__i386__)
131 #define FPU_DOUBLE(var) double var; \
132 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var) : )
133 #define FPU_DOUBLES(var1,var2) double var1,var2; \
134 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var2) : ); \
135 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var1) : )
137 /*********************************************************************
138 * _CIcos (NTDLL.@)
140 double CDECL _CIcos(void)
142 FPU_DOUBLE(x);
143 return cos(x);
146 /*********************************************************************
147 * _CIlog (NTDLL.@)
149 double CDECL _CIlog(void)
151 FPU_DOUBLE(x);
152 return log(x);
155 /*********************************************************************
156 * _CIpow (NTDLL.@)
158 double CDECL _CIpow(void)
160 FPU_DOUBLES(x,y);
161 return pow(x,y);
164 /*********************************************************************
165 * _CIsin (NTDLL.@)
167 double CDECL _CIsin(void)
169 FPU_DOUBLE(x);
170 return sin(x);
173 /*********************************************************************
174 * _CIsqrt (NTDLL.@)
176 double CDECL _CIsqrt(void)
178 FPU_DOUBLE(x);
179 return sqrt(x);
182 /*********************************************************************
183 * _ftol (NTDLL.@)
185 LONGLONG CDECL _ftol(void)
187 FPU_DOUBLE(x);
188 return (LONGLONG)x;
191 #endif /* (defined(__GNUC__) || defined(__clang__)) && defined(__i386__) */
193 static void
194 NTDLL_mergesort( void *arr, void *barr, size_t elemsize, int(__cdecl *compar)(const void *, const void *),
195 size_t left, size_t right )
197 if(right>left) {
198 size_t i, j, k, m;
199 m=left+(right-left)/2;
200 NTDLL_mergesort( arr, barr, elemsize, compar, left, m);
201 NTDLL_mergesort( arr, barr, elemsize, compar, m+1, right);
203 #define X(a,i) ((char*)a+elemsize*(i))
204 for (k=left, i=left, j=m+1; i<=m && j<=right; k++) {
205 if (compar(X(arr, i), X(arr,j)) <= 0) {
206 memcpy(X(barr,k), X(arr, i), elemsize);
207 i++;
208 } else {
209 memcpy(X(barr,k), X(arr, j), elemsize);
210 j++;
213 if (i<=m)
214 memcpy(X(barr,k), X(arr,i), (m-i+1)*elemsize);
215 else
216 memcpy(X(barr,k), X(arr,j), (right-j+1)*elemsize);
218 memcpy(X(arr, left), X(barr, left), (right-left+1)*elemsize);
220 #undef X
223 /*********************************************************************
224 * qsort (NTDLL.@)
226 void __cdecl qsort( void *base, size_t nmemb, size_t size,
227 int (__cdecl *compar)(const void *, const void *) )
229 void *secondarr;
230 if (nmemb < 2 || size == 0) return;
231 secondarr = RtlAllocateHeap (GetProcessHeap(), 0, nmemb*size);
232 NTDLL_mergesort( base, secondarr, size, compar, 0, nmemb-1 );
233 RtlFreeHeap (GetProcessHeap(),0, secondarr);
236 /*********************************************************************
237 * bsearch (NTDLL.@)
239 void * __cdecl bsearch( const void *key, const void *base, size_t nmemb,
240 size_t size, int (__cdecl *compar)(const void *, const void *) )
242 ssize_t min = 0;
243 ssize_t max = nmemb - 1;
245 while (min <= max)
247 ssize_t cursor = min + (max - min) / 2;
248 int ret = compar(key,(const char *)base+(cursor*size));
249 if (!ret)
250 return (char*)base+(cursor*size);
251 if (ret < 0)
252 max = cursor - 1;
253 else
254 min = cursor + 1;
256 return NULL;
260 /*********************************************************************
261 * _lfind (NTDLL.@)
263 void * __cdecl _lfind( const void *key, const void *base, unsigned int *nmemb,
264 size_t size, int(__cdecl *compar)(const void *, const void *) )
266 size_t i, n = *nmemb;
268 for (i=0;i<n;i++)
269 if (!compar(key,(char*)base+(size*i)))
270 return (char*)base+(size*i);
271 return NULL;
274 /******************************************************************************
275 * WinSqmEndSession (NTDLL.@)
277 NTSTATUS WINAPI WinSqmEndSession(HANDLE session)
279 FIXME("(%p): stub\n", session);
280 return STATUS_NOT_IMPLEMENTED;
283 /*********************************************************************
284 * WinSqmIncrementDWORD (NTDLL.@)
286 void WINAPI WinSqmIncrementDWORD(DWORD unk1, DWORD unk2, DWORD unk3)
288 FIXME("(%d, %d, %d): stub\n", unk1, unk2, unk3);
291 /*********************************************************************
292 * WinSqmIsOptedIn (NTDLL.@)
294 BOOL WINAPI WinSqmIsOptedIn(void)
296 FIXME("(): stub\n");
297 return FALSE;
300 /******************************************************************************
301 * WinSqmStartSession (NTDLL.@)
303 HANDLE WINAPI WinSqmStartSession(GUID *sessionguid, DWORD sessionid, DWORD unknown1)
305 FIXME("(%p, 0x%x, 0x%x): stub\n", sessionguid, sessionid, unknown1);
306 return INVALID_HANDLE_VALUE;
309 /***********************************************************************
310 * WinSqmSetDWORD (NTDLL.@)
312 void WINAPI WinSqmSetDWORD(HANDLE session, DWORD datapoint_id, DWORD datapoint_value)
314 FIXME("(%p, %d, %d): stub\n", session, datapoint_id, datapoint_value);
317 /******************************************************************************
318 * EtwEventActivityIdControl (NTDLL.@)
320 ULONG WINAPI EtwEventActivityIdControl(ULONG code, GUID *guid)
322 static int once;
324 if (!once++) FIXME("0x%x, %p: stub\n", code, guid);
325 return ERROR_SUCCESS;
328 /******************************************************************************
329 * EtwEventProviderEnabled (NTDLL.@)
331 BOOLEAN WINAPI EtwEventProviderEnabled( REGHANDLE handle, UCHAR level, ULONGLONG keyword )
333 FIXME("%s, %u, %s: stub\n", wine_dbgstr_longlong(handle), level, wine_dbgstr_longlong(keyword));
334 return FALSE;
337 /******************************************************************************
338 * EtwEventRegister (NTDLL.@)
340 ULONG WINAPI EtwEventRegister( LPCGUID provider, PENABLECALLBACK callback, PVOID context,
341 PREGHANDLE handle )
343 FIXME("(%s, %p, %p, %p) stub.\n", debugstr_guid(provider), callback, context, handle);
345 if (!handle) return ERROR_INVALID_PARAMETER;
347 *handle = 0xdeadbeef;
348 return ERROR_SUCCESS;
351 /******************************************************************************
352 * EtwEventUnregister (NTDLL.@)
354 ULONG WINAPI EtwEventUnregister( REGHANDLE handle )
356 FIXME("(%s) stub.\n", wine_dbgstr_longlong(handle));
357 return ERROR_SUCCESS;
360 /*********************************************************************
361 * EtwEventSetInformation (NTDLL.@)
363 ULONG WINAPI EtwEventSetInformation( REGHANDLE handle, EVENT_INFO_CLASS class, void *info,
364 ULONG length )
366 FIXME("(%s, %u, %p, %u) stub\n", wine_dbgstr_longlong(handle), class, info, length);
367 return ERROR_SUCCESS;
370 /******************************************************************************
371 * EtwEventWriteString (NTDLL.@)
373 ULONG WINAPI EtwEventWriteString( REGHANDLE handle, UCHAR level, ULONGLONG keyword, PCWSTR string )
375 FIXME("%s, %u, %s, %s: stub\n", wine_dbgstr_longlong(handle), level,
376 wine_dbgstr_longlong(keyword), debugstr_w(string));
377 return ERROR_SUCCESS;
380 /******************************************************************************
381 * EtwEventWriteTransfer (NTDLL.@)
383 ULONG WINAPI EtwEventWriteTransfer( REGHANDLE handle, PCEVENT_DESCRIPTOR descriptor, LPCGUID activity,
384 LPCGUID related, ULONG count, PEVENT_DATA_DESCRIPTOR data )
386 FIXME("%s, %p, %s, %s, %u, %p: stub\n", wine_dbgstr_longlong(handle), descriptor,
387 debugstr_guid(activity), debugstr_guid(related), count, data);
388 return ERROR_SUCCESS;
391 /******************************************************************************
392 * EtwRegisterTraceGuidsW (NTDLL.@)
394 * Register an event trace provider and the event trace classes that it uses
395 * to generate events.
397 * PARAMS
398 * RequestAddress [I] ControlCallback function
399 * RequestContext [I] Optional provider-defined context
400 * ControlGuid [I] GUID of the registering provider
401 * GuidCount [I] Number of elements in the TraceGuidReg array
402 * TraceGuidReg [I/O] Array of TRACE_GUID_REGISTRATION structures
403 * MofImagePath [I] not supported, set to NULL
404 * MofResourceName [I] not supported, set to NULL
405 * RegistrationHandle [O] Provider's registration handle
407 * RETURNS
408 * Success: ERROR_SUCCESS
409 * Failure: System error code
411 ULONG WINAPI EtwRegisterTraceGuidsW( WMIDPREQUEST RequestAddress,
412 void *RequestContext, const GUID *ControlGuid, ULONG GuidCount,
413 TRACE_GUID_REGISTRATION *TraceGuidReg, const WCHAR *MofImagePath,
414 const WCHAR *MofResourceName, TRACEHANDLE *RegistrationHandle )
416 FIXME("(%p, %p, %s, %u, %p, %s, %s, %p): stub\n", RequestAddress, RequestContext,
417 debugstr_guid(ControlGuid), GuidCount, TraceGuidReg, debugstr_w(MofImagePath),
418 debugstr_w(MofResourceName), RegistrationHandle);
420 if (TraceGuidReg)
422 ULONG i;
423 for (i = 0; i < GuidCount; i++)
425 FIXME(" register trace class %s\n", debugstr_guid(TraceGuidReg[i].Guid));
426 TraceGuidReg[i].RegHandle = (HANDLE)0xdeadbeef;
429 *RegistrationHandle = (TRACEHANDLE)0xdeadbeef;
430 return ERROR_SUCCESS;
433 /******************************************************************************
434 * EtwRegisterTraceGuidsA (NTDLL.@)
436 ULONG WINAPI EtwRegisterTraceGuidsA( WMIDPREQUEST RequestAddress,
437 void *RequestContext, const GUID *ControlGuid, ULONG GuidCount,
438 TRACE_GUID_REGISTRATION *TraceGuidReg, const char *MofImagePath,
439 const char *MofResourceName, TRACEHANDLE *RegistrationHandle )
441 FIXME("(%p, %p, %s, %u, %p, %s, %s, %p): stub\n", RequestAddress, RequestContext,
442 debugstr_guid(ControlGuid), GuidCount, TraceGuidReg, debugstr_a(MofImagePath),
443 debugstr_a(MofResourceName), RegistrationHandle);
444 return ERROR_SUCCESS;
447 /******************************************************************************
448 * EtwUnregisterTraceGuids (NTDLL.@)
450 ULONG WINAPI EtwUnregisterTraceGuids( TRACEHANDLE RegistrationHandle )
452 if (!RegistrationHandle)
453 return ERROR_INVALID_PARAMETER;
455 FIXME("%s: stub\n", wine_dbgstr_longlong(RegistrationHandle));
456 return ERROR_SUCCESS;
459 /******************************************************************************
460 * EtwEventEnabled (NTDLL.@)
462 BOOLEAN WINAPI EtwEventEnabled( REGHANDLE handle, const EVENT_DESCRIPTOR *descriptor )
464 FIXME("(%s, %p): stub\n", wine_dbgstr_longlong(handle), descriptor);
465 return FALSE;
468 /******************************************************************************
469 * EtwEventWrite (NTDLL.@)
471 ULONG WINAPI EtwEventWrite( REGHANDLE handle, const EVENT_DESCRIPTOR *descriptor, ULONG count,
472 EVENT_DATA_DESCRIPTOR *data )
474 FIXME("(%s, %p, %u, %p): stub\n", wine_dbgstr_longlong(handle), descriptor, count, data);
475 return ERROR_SUCCESS;
478 /******************************************************************************
479 * EtwGetTraceEnableFlags (NTDLL.@)
481 ULONG WINAPI EtwGetTraceEnableFlags( TRACEHANDLE handle )
483 FIXME("(%s) stub\n", wine_dbgstr_longlong(handle));
484 return 0;
487 /******************************************************************************
488 * EtwGetTraceEnableLevel (NTDLL.@)
490 UCHAR WINAPI EtwGetTraceEnableLevel( TRACEHANDLE handle )
492 FIXME("(%s) stub\n", wine_dbgstr_longlong(handle));
493 return TRACE_LEVEL_VERBOSE;
496 /******************************************************************************
497 * EtwGetTraceLoggerHandle (NTDLL.@)
499 TRACEHANDLE WINAPI EtwGetTraceLoggerHandle( PVOID buf )
501 FIXME("(%p) stub\n", buf);
502 return INVALID_PROCESSTRACE_HANDLE;
505 /******************************************************************************
506 * EtwLogTraceEvent (NTDLL.@)
508 ULONG WINAPI EtwLogTraceEvent( TRACEHANDLE SessionHandle, PEVENT_TRACE_HEADER EventTrace )
510 FIXME("%s %p\n", wine_dbgstr_longlong(SessionHandle), EventTrace);
511 return ERROR_CALL_NOT_IMPLEMENTED;
514 /******************************************************************************
515 * EtwTraceMessageVa (NTDLL.@)
517 ULONG WINAPI EtwTraceMessageVa( TRACEHANDLE handle, ULONG flags, LPGUID guid, USHORT number,
518 __ms_va_list args )
520 FIXME("(%s %x %s %d) : stub\n", wine_dbgstr_longlong(handle), flags, debugstr_guid(guid), number);
521 return ERROR_SUCCESS;
524 /******************************************************************************
525 * EtwTraceMessage (NTDLL.@)
527 ULONG WINAPIV EtwTraceMessage( TRACEHANDLE handle, ULONG flags, LPGUID guid, /*USHORT*/ ULONG number, ... )
529 __ms_va_list valist;
530 ULONG ret;
532 __ms_va_start( valist, number );
533 ret = EtwTraceMessageVa( handle, flags, guid, number, valist );
534 __ms_va_end( valist );
535 return ret;
538 /*********************************************************************
539 * ApiSetQueryApiSetPresence (NTDLL.@)
541 BOOL WINAPI ApiSetQueryApiSetPresence(const UNICODE_STRING *namespace, BOOLEAN *present)
543 FIXME("(%s, %p) stub!\n", debugstr_us(namespace), present);
545 if(present)
546 *present = TRUE;
547 return TRUE;