1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/nss/lib/base/libc.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,164 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +/* 1.9 + * libc.c 1.10 + * 1.11 + * This file contains our wrappers/reimplementations for "standard" 1.12 + * libc functions. Things like "memcpy." We add to this as we need 1.13 + * it. Oh, and let's keep it in alphabetical order, should it ever 1.14 + * get large. Most string/character stuff should be in utf8.c, not 1.15 + * here. This file (and maybe utf8.c) should be the only ones in 1.16 + * NSS to include files with angle brackets. 1.17 + */ 1.18 + 1.19 +#ifndef BASE_H 1.20 +#include "base.h" 1.21 +#endif /* BASE_H */ 1.22 + 1.23 +#include <string.h> /* memcpy, memset */ 1.24 + 1.25 +/* 1.26 + * nsslibc_memcpy 1.27 + * nsslibc_memset 1.28 + * nsslibc_offsetof 1.29 + * nsslibc_memequal 1.30 + */ 1.31 + 1.32 +/* 1.33 + * nsslibc_memcpy 1.34 + * 1.35 + * Errors: 1.36 + * NSS_ERROR_INVALID_POINTER 1.37 + * 1.38 + * Return value: 1.39 + * NULL on error 1.40 + * The destination pointer on success 1.41 + */ 1.42 + 1.43 +NSS_IMPLEMENT void * 1.44 +nsslibc_memcpy 1.45 +( 1.46 + void *dest, 1.47 + const void *source, 1.48 + PRUint32 n 1.49 +) 1.50 +{ 1.51 +#ifdef NSSDEBUG 1.52 + if( ((void *)NULL == dest) || ((const void *)NULL == source) ) { 1.53 + nss_SetError(NSS_ERROR_INVALID_POINTER); 1.54 + return (void *)NULL; 1.55 + } 1.56 +#endif /* NSSDEBUG */ 1.57 + 1.58 + return memcpy(dest, source, (size_t)n); 1.59 +} 1.60 + 1.61 +/* 1.62 + * nsslibc_memset 1.63 + * 1.64 + * Errors: 1.65 + * NSS_ERROR_INVALID_POINTER 1.66 + * 1.67 + * Return value: 1.68 + * NULL on error 1.69 + * The destination pointer on success 1.70 + */ 1.71 + 1.72 +NSS_IMPLEMENT void * 1.73 +nsslibc_memset 1.74 +( 1.75 + void *dest, 1.76 + PRUint8 byte, 1.77 + PRUint32 n 1.78 +) 1.79 +{ 1.80 +#ifdef NSSDEBUG 1.81 + if( ((void *)NULL == dest) ) { 1.82 + nss_SetError(NSS_ERROR_INVALID_POINTER); 1.83 + return (void *)NULL; 1.84 + } 1.85 +#endif /* NSSDEBUG */ 1.86 + 1.87 + return memset(dest, (int)byte, (size_t)n); 1.88 +} 1.89 + 1.90 +/* 1.91 + * nsslibc_memequal 1.92 + * 1.93 + * Errors: 1.94 + * NSS_ERROR_INVALID_POINTER 1.95 + * 1.96 + * Return value: 1.97 + * PR_TRUE if they match 1.98 + * PR_FALSE if they don't 1.99 + * PR_FALSE upon error 1.100 + */ 1.101 + 1.102 +NSS_IMPLEMENT PRBool 1.103 +nsslibc_memequal 1.104 +( 1.105 + const void *a, 1.106 + const void *b, 1.107 + PRUint32 len, 1.108 + PRStatus *statusOpt 1.109 +) 1.110 +{ 1.111 +#ifdef NSSDEBUG 1.112 + if( (((void *)NULL == a) || ((void *)NULL == b)) ) { 1.113 + nss_SetError(NSS_ERROR_INVALID_POINTER); 1.114 + if( (PRStatus *)NULL != statusOpt ) { 1.115 + *statusOpt = PR_FAILURE; 1.116 + } 1.117 + return PR_FALSE; 1.118 + } 1.119 +#endif /* NSSDEBUG */ 1.120 + 1.121 + if( (PRStatus *)NULL != statusOpt ) { 1.122 + *statusOpt = PR_SUCCESS; 1.123 + } 1.124 + 1.125 + if( 0 == memcmp(a, b, len) ) { 1.126 + return PR_TRUE; 1.127 + } else { 1.128 + return PR_FALSE; 1.129 + } 1.130 +} 1.131 + 1.132 +/* 1.133 + * nsslibc_memcmp 1.134 + */ 1.135 + 1.136 +NSS_IMPLEMENT PRInt32 1.137 +nsslibc_memcmp 1.138 +( 1.139 + const void *a, 1.140 + const void *b, 1.141 + PRUint32 len, 1.142 + PRStatus *statusOpt 1.143 +) 1.144 +{ 1.145 + int v; 1.146 + 1.147 +#ifdef NSSDEBUG 1.148 + if( (((void *)NULL == a) || ((void *)NULL == b)) ) { 1.149 + nss_SetError(NSS_ERROR_INVALID_POINTER); 1.150 + if( (PRStatus *)NULL != statusOpt ) { 1.151 + *statusOpt = PR_FAILURE; 1.152 + } 1.153 + return -2; 1.154 + } 1.155 +#endif /* NSSDEBUG */ 1.156 + 1.157 + if( (PRStatus *)NULL != statusOpt ) { 1.158 + *statusOpt = PR_SUCCESS; 1.159 + } 1.160 + 1.161 + v = memcmp(a, b, len); 1.162 + return (PRInt32)v; 1.163 +} 1.164 + 1.165 +/* 1.166 + * offsetof is a preprocessor definition 1.167 + */