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 | * item.c |
michael@0 | 7 | * |
michael@0 | 8 | * This contains some item-manipulation code. |
michael@0 | 9 | */ |
michael@0 | 10 | |
michael@0 | 11 | #ifndef BASE_H |
michael@0 | 12 | #include "base.h" |
michael@0 | 13 | #endif /* BASE_H */ |
michael@0 | 14 | |
michael@0 | 15 | /* |
michael@0 | 16 | * nssItem_Create |
michael@0 | 17 | * |
michael@0 | 18 | * -- fgmr comments -- |
michael@0 | 19 | * |
michael@0 | 20 | * The error may be one of the following values: |
michael@0 | 21 | * NSS_ERROR_INVALID_ARENA |
michael@0 | 22 | * NSS_ERROR_NO_MEMORY |
michael@0 | 23 | * NSS_ERROR_ARENA_MARKED_BY_ANOTHER_THREAD |
michael@0 | 24 | * NSS_ERROR_INVALID_POINTER |
michael@0 | 25 | * |
michael@0 | 26 | * Return value: |
michael@0 | 27 | * A pointer to an NSSItem upon success |
michael@0 | 28 | * NULL upon failure |
michael@0 | 29 | */ |
michael@0 | 30 | |
michael@0 | 31 | NSS_IMPLEMENT NSSItem * |
michael@0 | 32 | nssItem_Create |
michael@0 | 33 | ( |
michael@0 | 34 | NSSArena *arenaOpt, |
michael@0 | 35 | NSSItem *rvOpt, |
michael@0 | 36 | PRUint32 length, |
michael@0 | 37 | const void *data |
michael@0 | 38 | ) |
michael@0 | 39 | { |
michael@0 | 40 | NSSItem *rv = (NSSItem *)NULL; |
michael@0 | 41 | |
michael@0 | 42 | #ifdef DEBUG |
michael@0 | 43 | if( (NSSArena *)NULL != arenaOpt ) { |
michael@0 | 44 | if( PR_SUCCESS != nssArena_verifyPointer(arenaOpt) ) { |
michael@0 | 45 | return (NSSItem *)NULL; |
michael@0 | 46 | } |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | if( (const void *)NULL == data ) { |
michael@0 | 50 | if( length > 0 ) { |
michael@0 | 51 | nss_SetError(NSS_ERROR_INVALID_POINTER); |
michael@0 | 52 | return (NSSItem *)NULL; |
michael@0 | 53 | } |
michael@0 | 54 | } |
michael@0 | 55 | #endif /* DEBUG */ |
michael@0 | 56 | |
michael@0 | 57 | if( (NSSItem *)NULL == rvOpt ) { |
michael@0 | 58 | rv = (NSSItem *)nss_ZNEW(arenaOpt, NSSItem); |
michael@0 | 59 | if( (NSSItem *)NULL == rv ) { |
michael@0 | 60 | goto loser; |
michael@0 | 61 | } |
michael@0 | 62 | } else { |
michael@0 | 63 | rv = rvOpt; |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | rv->size = length; |
michael@0 | 67 | rv->data = nss_ZAlloc(arenaOpt, length); |
michael@0 | 68 | if( (void *)NULL == rv->data ) { |
michael@0 | 69 | goto loser; |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | if( length > 0 ) { |
michael@0 | 73 | (void)nsslibc_memcpy(rv->data, data, length); |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | return rv; |
michael@0 | 77 | |
michael@0 | 78 | loser: |
michael@0 | 79 | if( rv != rvOpt ) { |
michael@0 | 80 | nss_ZFreeIf(rv); |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | return (NSSItem *)NULL; |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | NSS_IMPLEMENT void |
michael@0 | 87 | nssItem_Destroy |
michael@0 | 88 | ( |
michael@0 | 89 | NSSItem *item |
michael@0 | 90 | ) |
michael@0 | 91 | { |
michael@0 | 92 | nss_ClearErrorStack(); |
michael@0 | 93 | |
michael@0 | 94 | nss_ZFreeIf(item->data); |
michael@0 | 95 | nss_ZFreeIf(item); |
michael@0 | 96 | |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | /* |
michael@0 | 100 | * nssItem_Duplicate |
michael@0 | 101 | * |
michael@0 | 102 | * -- fgmr comments -- |
michael@0 | 103 | * |
michael@0 | 104 | * The error may be one of the following values: |
michael@0 | 105 | * NSS_ERROR_INVALID_ARENA |
michael@0 | 106 | * NSS_ERROR_NO_MEMORY |
michael@0 | 107 | * NSS_ERROR_ARENA_MARKED_BY_ANOTHER_THREAD |
michael@0 | 108 | * NSS_ERROR_INVALID_ITEM |
michael@0 | 109 | * |
michael@0 | 110 | * Return value: |
michael@0 | 111 | * A pointer to an NSSItem upon success |
michael@0 | 112 | * NULL upon failure |
michael@0 | 113 | */ |
michael@0 | 114 | |
michael@0 | 115 | NSS_IMPLEMENT NSSItem * |
michael@0 | 116 | nssItem_Duplicate |
michael@0 | 117 | ( |
michael@0 | 118 | NSSItem *obj, |
michael@0 | 119 | NSSArena *arenaOpt, |
michael@0 | 120 | NSSItem *rvOpt |
michael@0 | 121 | ) |
michael@0 | 122 | { |
michael@0 | 123 | #ifdef DEBUG |
michael@0 | 124 | if( (NSSArena *)NULL != arenaOpt ) { |
michael@0 | 125 | if( PR_SUCCESS != nssArena_verifyPointer(arenaOpt) ) { |
michael@0 | 126 | return (NSSItem *)NULL; |
michael@0 | 127 | } |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | if( (NSSItem *)NULL == obj ) { |
michael@0 | 131 | nss_SetError(NSS_ERROR_INVALID_ITEM); |
michael@0 | 132 | return (NSSItem *)NULL; |
michael@0 | 133 | } |
michael@0 | 134 | #endif /* DEBUG */ |
michael@0 | 135 | |
michael@0 | 136 | return nssItem_Create(arenaOpt, rvOpt, obj->size, obj->data); |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | #ifdef DEBUG |
michael@0 | 140 | /* |
michael@0 | 141 | * nssItem_verifyPointer |
michael@0 | 142 | * |
michael@0 | 143 | * -- fgmr comments -- |
michael@0 | 144 | * |
michael@0 | 145 | * The error may be one of the following values: |
michael@0 | 146 | * NSS_ERROR_INVALID_ITEM |
michael@0 | 147 | * |
michael@0 | 148 | * Return value: |
michael@0 | 149 | * PR_SUCCESS upon success |
michael@0 | 150 | * PR_FAILURE upon failure |
michael@0 | 151 | */ |
michael@0 | 152 | |
michael@0 | 153 | NSS_IMPLEMENT PRStatus |
michael@0 | 154 | nssItem_verifyPointer |
michael@0 | 155 | ( |
michael@0 | 156 | const NSSItem *item |
michael@0 | 157 | ) |
michael@0 | 158 | { |
michael@0 | 159 | if( ((const NSSItem *)NULL == item) || |
michael@0 | 160 | (((void *)NULL == item->data) && (item->size > 0)) ) { |
michael@0 | 161 | nss_SetError(NSS_ERROR_INVALID_ITEM); |
michael@0 | 162 | return PR_FAILURE; |
michael@0 | 163 | } |
michael@0 | 164 | |
michael@0 | 165 | return PR_SUCCESS; |
michael@0 | 166 | } |
michael@0 | 167 | #endif /* DEBUG */ |
michael@0 | 168 | |
michael@0 | 169 | /* |
michael@0 | 170 | * nssItem_Equal |
michael@0 | 171 | * |
michael@0 | 172 | * -- fgmr comments -- |
michael@0 | 173 | * |
michael@0 | 174 | * The error may be one of the following values: |
michael@0 | 175 | * NSS_ERROR_INVALID_ITEM |
michael@0 | 176 | * |
michael@0 | 177 | * Return value: |
michael@0 | 178 | * PR_TRUE if the items are identical |
michael@0 | 179 | * PR_FALSE if they aren't |
michael@0 | 180 | * PR_FALSE upon error |
michael@0 | 181 | */ |
michael@0 | 182 | |
michael@0 | 183 | NSS_IMPLEMENT PRBool |
michael@0 | 184 | nssItem_Equal |
michael@0 | 185 | ( |
michael@0 | 186 | const NSSItem *one, |
michael@0 | 187 | const NSSItem *two, |
michael@0 | 188 | PRStatus *statusOpt |
michael@0 | 189 | ) |
michael@0 | 190 | { |
michael@0 | 191 | if( (PRStatus *)NULL != statusOpt ) { |
michael@0 | 192 | *statusOpt = PR_SUCCESS; |
michael@0 | 193 | } |
michael@0 | 194 | |
michael@0 | 195 | if( ((const NSSItem *)NULL == one) && ((const NSSItem *)NULL == two) ) { |
michael@0 | 196 | return PR_TRUE; |
michael@0 | 197 | } |
michael@0 | 198 | |
michael@0 | 199 | if( ((const NSSItem *)NULL == one) || ((const NSSItem *)NULL == two) ) { |
michael@0 | 200 | return PR_FALSE; |
michael@0 | 201 | } |
michael@0 | 202 | |
michael@0 | 203 | if( one->size != two->size ) { |
michael@0 | 204 | return PR_FALSE; |
michael@0 | 205 | } |
michael@0 | 206 | |
michael@0 | 207 | return nsslibc_memequal(one->data, two->data, one->size, statusOpt); |
michael@0 | 208 | } |