Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /****************************************************************************
  2. *
  3. *                            Open Watcom Project
  4. *
  5. *    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
  6. *
  7. *  ========================================================================
  8. *
  9. *    This file contains Original Code and/or Modifications of Original
  10. *    Code as defined in and that are subject to the Sybase Open Watcom
  11. *    Public License version 1.0 (the 'License'). You may not use this file
  12. *    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
  13. *    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
  14. *    provided with the Original Code and Modifications, and is also
  15. *    available at www.sybase.com/developer/opensource.
  16. *
  17. *    The Original Code and all software distributed under the License are
  18. *    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  19. *    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
  20. *    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
  21. *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
  22. *    NON-INFRINGEMENT. Please see the License for the specific language
  23. *    governing rights and limitations under the License.
  24. *
  25. *  ========================================================================
  26. *
  27. * Description:  Implementation of bsearch_s() - bounds-checking bsearch().
  28. *
  29. ****************************************************************************/
  30.  
  31.  
  32. #include "variety.h"
  33. #include "saferlib.h"
  34. #undef __INLINE_FUNCTIONS__
  35. #include <stddef.h>
  36. #include <stdlib.h>
  37. #include "extfunc.h"
  38.  
  39. typedef int bcomp();
  40. #ifdef _M_IX86
  41.     #pragma aux (__outside_CLIB) bcomp;
  42. #endif
  43.  
  44. _WCRTLINK void * bsearch_s( const void * key, const void * base,
  45.                             rsize_t nmemb, rsize_t size,
  46.             int (*compar)( const void *k, const void *y, void *context ),
  47.                             void *context )
  48. /***********************************************************************/
  49. {
  50.     char    *low;
  51.     char    *high;
  52.     char    *mid;
  53.     int     cond;
  54.     bcomp   *comp = compar;
  55.  
  56.     /* runtime-constraints */
  57.     // nmemb <= RSIZE_MAX
  58.     // size  <= RSIZE_MAX
  59.     // if nmemb > 0 then key, base, compar not NULL
  60.     if( __check_constraint_maxsize( nmemb ) &&
  61.         __check_constraint_maxsize( size ) &&
  62.         ( (nmemb == 0) || __check_constraint_nullptr( key ) &&
  63.                           __check_constraint_nullptr( base ) &&
  64.                           __check_constraint_nullptr( compar )) ) {
  65.  
  66.         if( nmemb == 0 ) {                      /* empty array - nothing to do */
  67.             return( NULL );
  68.         }
  69.         low = (char *) base;
  70.         high = low + (nmemb - 1) * size;
  71.         while( low < high ) {
  72.             mid = low + ( (high - low) / size / 2 ) * size;
  73.             cond = (*comp)( key, mid, context );
  74.             if( cond == 0 ) return( mid );
  75.             if( cond < 0 ) {    /* key < mid */
  76.                 high = mid;
  77.             } else {            /* key > mid */
  78.                 low = mid + size;
  79.             }
  80.         }
  81.         if (low == high) return( (*comp)( key, low, context ) ? NULL : low );
  82.     }
  83.     return( NULL );
  84. }
  85.