michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * libc.c michael@0: * michael@0: * This file contains our wrappers/reimplementations for "standard" michael@0: * libc functions. Things like "memcpy." We add to this as we need michael@0: * it. Oh, and let's keep it in alphabetical order, should it ever michael@0: * get large. Most string/character stuff should be in utf8.c, not michael@0: * here. This file (and maybe utf8.c) should be the only ones in michael@0: * NSS to include files with angle brackets. michael@0: */ michael@0: michael@0: #ifndef BASE_H michael@0: #include "base.h" michael@0: #endif /* BASE_H */ michael@0: michael@0: #include /* memcpy, memset */ michael@0: michael@0: /* michael@0: * nsslibc_memcpy michael@0: * nsslibc_memset michael@0: * nsslibc_offsetof michael@0: * nsslibc_memequal michael@0: */ michael@0: michael@0: /* michael@0: * nsslibc_memcpy michael@0: * michael@0: * Errors: michael@0: * NSS_ERROR_INVALID_POINTER michael@0: * michael@0: * Return value: michael@0: * NULL on error michael@0: * The destination pointer on success michael@0: */ michael@0: michael@0: NSS_IMPLEMENT void * michael@0: nsslibc_memcpy michael@0: ( michael@0: void *dest, michael@0: const void *source, michael@0: PRUint32 n michael@0: ) michael@0: { michael@0: #ifdef NSSDEBUG michael@0: if( ((void *)NULL == dest) || ((const void *)NULL == source) ) { michael@0: nss_SetError(NSS_ERROR_INVALID_POINTER); michael@0: return (void *)NULL; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: return memcpy(dest, source, (size_t)n); michael@0: } michael@0: michael@0: /* michael@0: * nsslibc_memset michael@0: * michael@0: * Errors: michael@0: * NSS_ERROR_INVALID_POINTER michael@0: * michael@0: * Return value: michael@0: * NULL on error michael@0: * The destination pointer on success michael@0: */ michael@0: michael@0: NSS_IMPLEMENT void * michael@0: nsslibc_memset michael@0: ( michael@0: void *dest, michael@0: PRUint8 byte, michael@0: PRUint32 n michael@0: ) michael@0: { michael@0: #ifdef NSSDEBUG michael@0: if( ((void *)NULL == dest) ) { michael@0: nss_SetError(NSS_ERROR_INVALID_POINTER); michael@0: return (void *)NULL; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: return memset(dest, (int)byte, (size_t)n); michael@0: } michael@0: michael@0: /* michael@0: * nsslibc_memequal michael@0: * michael@0: * Errors: michael@0: * NSS_ERROR_INVALID_POINTER michael@0: * michael@0: * Return value: michael@0: * PR_TRUE if they match michael@0: * PR_FALSE if they don't michael@0: * PR_FALSE upon error michael@0: */ michael@0: michael@0: NSS_IMPLEMENT PRBool michael@0: nsslibc_memequal michael@0: ( michael@0: const void *a, michael@0: const void *b, michael@0: PRUint32 len, michael@0: PRStatus *statusOpt michael@0: ) michael@0: { michael@0: #ifdef NSSDEBUG michael@0: if( (((void *)NULL == a) || ((void *)NULL == b)) ) { michael@0: nss_SetError(NSS_ERROR_INVALID_POINTER); michael@0: if( (PRStatus *)NULL != statusOpt ) { michael@0: *statusOpt = PR_FAILURE; michael@0: } michael@0: return PR_FALSE; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: if( (PRStatus *)NULL != statusOpt ) { michael@0: *statusOpt = PR_SUCCESS; michael@0: } michael@0: michael@0: if( 0 == memcmp(a, b, len) ) { michael@0: return PR_TRUE; michael@0: } else { michael@0: return PR_FALSE; michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * nsslibc_memcmp michael@0: */ michael@0: michael@0: NSS_IMPLEMENT PRInt32 michael@0: nsslibc_memcmp michael@0: ( michael@0: const void *a, michael@0: const void *b, michael@0: PRUint32 len, michael@0: PRStatus *statusOpt michael@0: ) michael@0: { michael@0: int v; michael@0: michael@0: #ifdef NSSDEBUG michael@0: if( (((void *)NULL == a) || ((void *)NULL == b)) ) { michael@0: nss_SetError(NSS_ERROR_INVALID_POINTER); michael@0: if( (PRStatus *)NULL != statusOpt ) { michael@0: *statusOpt = PR_FAILURE; michael@0: } michael@0: return -2; michael@0: } michael@0: #endif /* NSSDEBUG */ michael@0: michael@0: if( (PRStatus *)NULL != statusOpt ) { michael@0: *statusOpt = PR_SUCCESS; michael@0: } michael@0: michael@0: v = memcmp(a, b, len); michael@0: return (PRInt32)v; michael@0: } michael@0: michael@0: /* michael@0: * offsetof is a preprocessor definition michael@0: */