security/nss/lib/base/item.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/lib/base/item.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,208 @@
     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 + * item.c
    1.10 + *
    1.11 + * This contains some item-manipulation code.
    1.12 + */
    1.13 +
    1.14 +#ifndef BASE_H
    1.15 +#include "base.h"
    1.16 +#endif /* BASE_H */
    1.17 +
    1.18 +/*
    1.19 + * nssItem_Create
    1.20 + *
    1.21 + * -- fgmr comments --
    1.22 + *
    1.23 + * The error may be one of the following values:
    1.24 + *  NSS_ERROR_INVALID_ARENA
    1.25 + *  NSS_ERROR_NO_MEMORY
    1.26 + *  NSS_ERROR_ARENA_MARKED_BY_ANOTHER_THREAD
    1.27 + *  NSS_ERROR_INVALID_POINTER
    1.28 + *  
    1.29 + * Return value:
    1.30 + *  A pointer to an NSSItem upon success
    1.31 + *  NULL upon failure
    1.32 + */
    1.33 +
    1.34 +NSS_IMPLEMENT NSSItem *
    1.35 +nssItem_Create
    1.36 +(
    1.37 +  NSSArena *arenaOpt,
    1.38 +  NSSItem *rvOpt,
    1.39 +  PRUint32 length,
    1.40 +  const void *data
    1.41 +)
    1.42 +{
    1.43 +  NSSItem *rv = (NSSItem *)NULL;
    1.44 +
    1.45 +#ifdef DEBUG
    1.46 +  if( (NSSArena *)NULL != arenaOpt ) {
    1.47 +    if( PR_SUCCESS != nssArena_verifyPointer(arenaOpt) ) {
    1.48 +      return (NSSItem *)NULL;
    1.49 +    }
    1.50 +  }
    1.51 +
    1.52 +  if( (const void *)NULL == data ) {
    1.53 +    if( length > 0 ) {
    1.54 +      nss_SetError(NSS_ERROR_INVALID_POINTER);
    1.55 +      return (NSSItem *)NULL;
    1.56 +    }
    1.57 +  }
    1.58 +#endif /* DEBUG */
    1.59 +
    1.60 +  if( (NSSItem *)NULL == rvOpt ) {
    1.61 +    rv = (NSSItem *)nss_ZNEW(arenaOpt, NSSItem);
    1.62 +    if( (NSSItem *)NULL == rv ) {
    1.63 +      goto loser;
    1.64 +    }
    1.65 +  } else {
    1.66 +    rv = rvOpt;
    1.67 +  }
    1.68 +
    1.69 +  rv->size = length;
    1.70 +  rv->data = nss_ZAlloc(arenaOpt, length);
    1.71 +  if( (void *)NULL == rv->data ) {
    1.72 +    goto loser;
    1.73 +  }
    1.74 +
    1.75 +  if( length > 0 ) {
    1.76 +    (void)nsslibc_memcpy(rv->data, data, length);
    1.77 +  }
    1.78 +
    1.79 +  return rv;
    1.80 +
    1.81 + loser:
    1.82 +  if( rv != rvOpt ) {
    1.83 +    nss_ZFreeIf(rv);
    1.84 +  }
    1.85 +
    1.86 +  return (NSSItem *)NULL;
    1.87 +}
    1.88 +
    1.89 +NSS_IMPLEMENT void
    1.90 +nssItem_Destroy
    1.91 +(
    1.92 +  NSSItem *item
    1.93 +)
    1.94 +{
    1.95 +  nss_ClearErrorStack();
    1.96 +
    1.97 +  nss_ZFreeIf(item->data);
    1.98 +  nss_ZFreeIf(item);
    1.99 +
   1.100 +}
   1.101 +
   1.102 +/*
   1.103 + * nssItem_Duplicate
   1.104 + *
   1.105 + * -- fgmr comments --
   1.106 + *
   1.107 + * The error may be one of the following values:
   1.108 + *  NSS_ERROR_INVALID_ARENA
   1.109 + *  NSS_ERROR_NO_MEMORY
   1.110 + *  NSS_ERROR_ARENA_MARKED_BY_ANOTHER_THREAD
   1.111 + *  NSS_ERROR_INVALID_ITEM
   1.112 + *  
   1.113 + * Return value:
   1.114 + *  A pointer to an NSSItem upon success
   1.115 + *  NULL upon failure
   1.116 + */
   1.117 +
   1.118 +NSS_IMPLEMENT NSSItem *
   1.119 +nssItem_Duplicate
   1.120 +(
   1.121 +  NSSItem *obj,
   1.122 +  NSSArena *arenaOpt,
   1.123 +  NSSItem *rvOpt
   1.124 +)
   1.125 +{
   1.126 +#ifdef DEBUG
   1.127 +  if( (NSSArena *)NULL != arenaOpt ) {
   1.128 +    if( PR_SUCCESS != nssArena_verifyPointer(arenaOpt) ) {
   1.129 +      return (NSSItem *)NULL;
   1.130 +    }
   1.131 +  }
   1.132 +
   1.133 +  if( (NSSItem *)NULL == obj ) {
   1.134 +    nss_SetError(NSS_ERROR_INVALID_ITEM);
   1.135 +    return (NSSItem *)NULL;
   1.136 +  }
   1.137 +#endif /* DEBUG */
   1.138 +
   1.139 +  return nssItem_Create(arenaOpt, rvOpt, obj->size, obj->data);
   1.140 +}
   1.141 +
   1.142 +#ifdef DEBUG
   1.143 +/*
   1.144 + * nssItem_verifyPointer
   1.145 + *
   1.146 + * -- fgmr comments --
   1.147 + *
   1.148 + * The error may be one of the following values:
   1.149 + *  NSS_ERROR_INVALID_ITEM
   1.150 + *
   1.151 + * Return value:
   1.152 + *  PR_SUCCESS upon success
   1.153 + *  PR_FAILURE upon failure
   1.154 + */
   1.155 +
   1.156 +NSS_IMPLEMENT PRStatus
   1.157 +nssItem_verifyPointer
   1.158 +(
   1.159 +  const NSSItem *item
   1.160 +)
   1.161 +{
   1.162 +  if( ((const NSSItem *)NULL == item) ||
   1.163 +      (((void *)NULL == item->data) && (item->size > 0)) ) {
   1.164 +    nss_SetError(NSS_ERROR_INVALID_ITEM);
   1.165 +    return PR_FAILURE;
   1.166 +  }
   1.167 +
   1.168 +  return PR_SUCCESS;
   1.169 +}
   1.170 +#endif /* DEBUG */
   1.171 +
   1.172 +/*
   1.173 + * nssItem_Equal
   1.174 + *
   1.175 + * -- fgmr comments --
   1.176 + *
   1.177 + * The error may be one of the following values:
   1.178 + *  NSS_ERROR_INVALID_ITEM
   1.179 + *
   1.180 + * Return value:
   1.181 + *  PR_TRUE if the items are identical
   1.182 + *  PR_FALSE if they aren't
   1.183 + *  PR_FALSE upon error
   1.184 + */
   1.185 +
   1.186 +NSS_IMPLEMENT PRBool
   1.187 +nssItem_Equal
   1.188 +(
   1.189 +  const NSSItem *one,
   1.190 +  const NSSItem *two,
   1.191 +  PRStatus *statusOpt
   1.192 +)
   1.193 +{
   1.194 +  if( (PRStatus *)NULL != statusOpt ) {
   1.195 +    *statusOpt = PR_SUCCESS;
   1.196 +  }
   1.197 +
   1.198 +  if( ((const NSSItem *)NULL == one) && ((const NSSItem *)NULL == two) ) {
   1.199 +    return PR_TRUE;
   1.200 +  }
   1.201 +
   1.202 +  if( ((const NSSItem *)NULL == one) || ((const NSSItem *)NULL == two) ) {
   1.203 +    return PR_FALSE;
   1.204 +  }
   1.205 +
   1.206 +  if( one->size != two->size ) {
   1.207 +    return PR_FALSE;
   1.208 +  }
   1.209 +
   1.210 +  return nsslibc_memequal(one->data, two->data, one->size, statusOpt);
   1.211 +}

mercurial