/* * Helper functions for ntdll * * Copyright 2000 Juergen Schmied * Copyright 2010 Marcus Meissner * * 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 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA */ #include #include "ntstatus.h" #define WIN32_NO_STATUS #include "wine/debug.h" #include "ntdll_misc.h" #include "wmistr.h" #include "evntrace.h" #include "evntprov.h" WINE_DEFAULT_DEBUG_CHANNEL(ntdll); LPCSTR debugstr_us( const UNICODE_STRING *us ) { if (!us) return ""; return debugstr_wn(us->Buffer, us->Length / sizeof(WCHAR)); } static void NTDLL_mergesort( void *arr, void *barr, size_t elemsize, int(__cdecl *compar)(const void *, const void *), size_t left, size_t right ) { if(right>left) { size_t i, j, k, m; m=left+(right-left)/2; NTDLL_mergesort( arr, barr, elemsize, compar, left, m); NTDLL_mergesort( arr, barr, elemsize, compar, m+1, right); #define X(a,i) ((char*)a+elemsize*(i)) 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); i++; } else { memcpy(X(barr,k), X(arr, j), elemsize); j++; } } 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); } #undef X } /********************************************************************* * qsort (NTDLL.@) */ void __cdecl qsort( void *base, size_t nmemb, size_t size, int (__cdecl *compar)(const void *, const void *) ) { void *secondarr; if (nmemb < 2 || size == 0) return; secondarr = RtlAllocateHeap (GetProcessHeap(), 0, nmemb*size); NTDLL_mergesort( base, secondarr, size, compar, 0, nmemb-1 ); RtlFreeHeap (GetProcessHeap(),0, secondarr); } /********************************************************************* * bsearch (NTDLL.@) */ void * __cdecl bsearch( const void *key, const void *base, size_t nmemb, size_t size, int (__cdecl *compar)(const void *, const void *) ) { ssize_t min = 0; ssize_t max = nmemb - 1; while (min <= max) { ssize_t cursor = min + (max - min) / 2; int ret = compar(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; } /********************************************************************* * _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