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