| OLD | NEW |
| (Empty) |
| 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/. */ | |
| 4 | |
| 5 #ifdef DEBUG | |
| 6 static const char CVS_ID[] = "@(#) $RCSfile: libc.c,v $ $Revision: 1.4 $ $Date:
2012/04/25 14:49:26 $"; | |
| 7 #endif /* DEBUG */ | |
| 8 | |
| 9 /* | |
| 10 * libc.c | |
| 11 * | |
| 12 * This file contains our wrappers/reimplementations for "standard" | |
| 13 * libc functions. Things like "memcpy." We add to this as we need | |
| 14 * it. Oh, and let's keep it in alphabetical order, should it ever | |
| 15 * get large. Most string/character stuff should be in utf8.c, not | |
| 16 * here. This file (and maybe utf8.c) should be the only ones in | |
| 17 * NSS to include files with angle brackets. | |
| 18 */ | |
| 19 | |
| 20 #ifndef BASE_H | |
| 21 #include "base.h" | |
| 22 #endif /* BASE_H */ | |
| 23 | |
| 24 #include <string.h> /* memcpy, memset */ | |
| 25 | |
| 26 /* | |
| 27 * nsslibc_memcpy | |
| 28 * nsslibc_memset | |
| 29 * nsslibc_offsetof | |
| 30 * nsslibc_memequal | |
| 31 */ | |
| 32 | |
| 33 /* | |
| 34 * nsslibc_memcpy | |
| 35 * | |
| 36 * Errors: | |
| 37 * NSS_ERROR_INVALID_POINTER | |
| 38 * | |
| 39 * Return value: | |
| 40 * NULL on error | |
| 41 * The destination pointer on success | |
| 42 */ | |
| 43 | |
| 44 NSS_IMPLEMENT void * | |
| 45 nsslibc_memcpy | |
| 46 ( | |
| 47 void *dest, | |
| 48 const void *source, | |
| 49 PRUint32 n | |
| 50 ) | |
| 51 { | |
| 52 #ifdef NSSDEBUG | |
| 53 if( ((void *)NULL == dest) || ((const void *)NULL == source) ) { | |
| 54 nss_SetError(NSS_ERROR_INVALID_POINTER); | |
| 55 return (void *)NULL; | |
| 56 } | |
| 57 #endif /* NSSDEBUG */ | |
| 58 | |
| 59 return memcpy(dest, source, (size_t)n); | |
| 60 } | |
| 61 | |
| 62 /* | |
| 63 * nsslibc_memset | |
| 64 * | |
| 65 * Errors: | |
| 66 * NSS_ERROR_INVALID_POINTER | |
| 67 * | |
| 68 * Return value: | |
| 69 * NULL on error | |
| 70 * The destination pointer on success | |
| 71 */ | |
| 72 | |
| 73 NSS_IMPLEMENT void * | |
| 74 nsslibc_memset | |
| 75 ( | |
| 76 void *dest, | |
| 77 PRUint8 byte, | |
| 78 PRUint32 n | |
| 79 ) | |
| 80 { | |
| 81 #ifdef NSSDEBUG | |
| 82 if( ((void *)NULL == dest) ) { | |
| 83 nss_SetError(NSS_ERROR_INVALID_POINTER); | |
| 84 return (void *)NULL; | |
| 85 } | |
| 86 #endif /* NSSDEBUG */ | |
| 87 | |
| 88 return memset(dest, (int)byte, (size_t)n); | |
| 89 } | |
| 90 | |
| 91 /* | |
| 92 * nsslibc_memequal | |
| 93 * | |
| 94 * Errors: | |
| 95 * NSS_ERROR_INVALID_POINTER | |
| 96 * | |
| 97 * Return value: | |
| 98 * PR_TRUE if they match | |
| 99 * PR_FALSE if they don't | |
| 100 * PR_FALSE upon error | |
| 101 */ | |
| 102 | |
| 103 NSS_IMPLEMENT PRBool | |
| 104 nsslibc_memequal | |
| 105 ( | |
| 106 const void *a, | |
| 107 const void *b, | |
| 108 PRUint32 len, | |
| 109 PRStatus *statusOpt | |
| 110 ) | |
| 111 { | |
| 112 #ifdef NSSDEBUG | |
| 113 if( (((void *)NULL == a) || ((void *)NULL == b)) ) { | |
| 114 nss_SetError(NSS_ERROR_INVALID_POINTER); | |
| 115 if( (PRStatus *)NULL != statusOpt ) { | |
| 116 *statusOpt = PR_FAILURE; | |
| 117 } | |
| 118 return PR_FALSE; | |
| 119 } | |
| 120 #endif /* NSSDEBUG */ | |
| 121 | |
| 122 if( (PRStatus *)NULL != statusOpt ) { | |
| 123 *statusOpt = PR_SUCCESS; | |
| 124 } | |
| 125 | |
| 126 if( 0 == memcmp(a, b, len) ) { | |
| 127 return PR_TRUE; | |
| 128 } else { | |
| 129 return PR_FALSE; | |
| 130 } | |
| 131 } | |
| 132 | |
| 133 /* | |
| 134 * nsslibc_memcmp | |
| 135 */ | |
| 136 | |
| 137 NSS_IMPLEMENT PRInt32 | |
| 138 nsslibc_memcmp | |
| 139 ( | |
| 140 const void *a, | |
| 141 const void *b, | |
| 142 PRUint32 len, | |
| 143 PRStatus *statusOpt | |
| 144 ) | |
| 145 { | |
| 146 int v; | |
| 147 | |
| 148 #ifdef NSSDEBUG | |
| 149 if( (((void *)NULL == a) || ((void *)NULL == b)) ) { | |
| 150 nss_SetError(NSS_ERROR_INVALID_POINTER); | |
| 151 if( (PRStatus *)NULL != statusOpt ) { | |
| 152 *statusOpt = PR_FAILURE; | |
| 153 } | |
| 154 return -2; | |
| 155 } | |
| 156 #endif /* NSSDEBUG */ | |
| 157 | |
| 158 if( (PRStatus *)NULL != statusOpt ) { | |
| 159 *statusOpt = PR_SUCCESS; | |
| 160 } | |
| 161 | |
| 162 v = memcmp(a, b, len); | |
| 163 return (PRInt32)v; | |
| 164 } | |
| 165 | |
| 166 /* | |
| 167 * offsetof is a preprocessor definition | |
| 168 */ | |
| OLD | NEW |