security/nss/lib/base/libc.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 /*
     6  * libc.c
     7  *
     8  * This file contains our wrappers/reimplementations for "standard" 
     9  * libc functions.  Things like "memcpy."  We add to this as we need 
    10  * it.  Oh, and let's keep it in alphabetical order, should it ever 
    11  * get large.  Most string/character stuff should be in utf8.c, not 
    12  * here.  This file (and maybe utf8.c) should be the only ones in
    13  * NSS to include files with angle brackets.
    14  */
    16 #ifndef BASE_H
    17 #include "base.h"
    18 #endif /* BASE_H */
    20 #include <string.h> /* memcpy, memset */
    22 /*
    23  * nsslibc_memcpy
    24  * nsslibc_memset
    25  * nsslibc_offsetof
    26  * nsslibc_memequal
    27  */
    29 /*
    30  * nsslibc_memcpy
    31  *
    32  * Errors:
    33  *  NSS_ERROR_INVALID_POINTER
    34  *
    35  * Return value:
    36  *  NULL on error
    37  *  The destination pointer on success
    38  */
    40 NSS_IMPLEMENT void *
    41 nsslibc_memcpy
    42 (
    43   void *dest,
    44   const void *source,
    45   PRUint32 n
    46 )
    47 {
    48 #ifdef NSSDEBUG
    49   if( ((void *)NULL == dest) || ((const void *)NULL == source) ) {
    50     nss_SetError(NSS_ERROR_INVALID_POINTER);
    51     return (void *)NULL;
    52   }
    53 #endif /* NSSDEBUG */
    55   return memcpy(dest, source, (size_t)n);
    56 }
    58 /*
    59  * nsslibc_memset
    60  *
    61  * Errors:
    62  *  NSS_ERROR_INVALID_POINTER
    63  *
    64  * Return value:
    65  *  NULL on error
    66  *  The destination pointer on success
    67  */
    69 NSS_IMPLEMENT void *
    70 nsslibc_memset
    71 (
    72   void *dest,
    73   PRUint8 byte,
    74   PRUint32 n
    75 )
    76 {
    77 #ifdef NSSDEBUG
    78   if( ((void *)NULL == dest) ) {
    79     nss_SetError(NSS_ERROR_INVALID_POINTER);
    80     return (void *)NULL;
    81   }
    82 #endif /* NSSDEBUG */
    84   return memset(dest, (int)byte, (size_t)n);
    85 }
    87 /*
    88  * nsslibc_memequal
    89  *
    90  * Errors:
    91  *  NSS_ERROR_INVALID_POINTER
    92  *
    93  * Return value:
    94  *  PR_TRUE if they match
    95  *  PR_FALSE if they don't
    96  *  PR_FALSE upon error
    97  */
    99 NSS_IMPLEMENT PRBool
   100 nsslibc_memequal
   101 (
   102   const void *a,
   103   const void *b,
   104   PRUint32 len,
   105   PRStatus *statusOpt
   106 )
   107 {
   108 #ifdef NSSDEBUG
   109   if( (((void *)NULL == a) || ((void *)NULL == b)) ) {
   110     nss_SetError(NSS_ERROR_INVALID_POINTER);
   111     if( (PRStatus *)NULL != statusOpt ) {
   112       *statusOpt = PR_FAILURE;
   113     }
   114     return PR_FALSE;
   115   }
   116 #endif /* NSSDEBUG */
   118   if( (PRStatus *)NULL != statusOpt ) {
   119     *statusOpt = PR_SUCCESS;
   120   }
   122   if( 0 == memcmp(a, b, len) ) {
   123     return PR_TRUE;
   124   } else {
   125     return PR_FALSE;
   126   }
   127 }
   129 /*
   130  * nsslibc_memcmp
   131  */
   133 NSS_IMPLEMENT PRInt32
   134 nsslibc_memcmp
   135 (
   136   const void *a,
   137   const void *b,
   138   PRUint32 len,
   139   PRStatus *statusOpt
   140 )
   141 {
   142   int v;
   144 #ifdef NSSDEBUG
   145   if( (((void *)NULL == a) || ((void *)NULL == b)) ) {
   146     nss_SetError(NSS_ERROR_INVALID_POINTER);
   147     if( (PRStatus *)NULL != statusOpt ) {
   148       *statusOpt = PR_FAILURE;
   149     }
   150     return -2;
   151   }
   152 #endif /* NSSDEBUG */
   154   if( (PRStatus *)NULL != statusOpt ) {
   155     *statusOpt = PR_SUCCESS;
   156   }
   158   v = memcmp(a, b, len);
   159   return (PRInt32)v;
   160 }
   162 /*
   163  * offsetof is a preprocessor definition
   164  */

mercurial