Back to index

lightning-sunbird  0.9+nobinonly
item.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Netscape security libraries.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 1994-2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 #ifdef DEBUG
00038 static const char CVS_ID[] = "@(#) $RCSfile: item.c,v $ $Revision: 1.4 $ $Date: 2005/01/20 02:25:45 $";
00039 #endif /* DEBUG */
00040 
00041 /*
00042  * item.c
00043  *
00044  * This contains some item-manipulation code.
00045  */
00046 
00047 #ifndef BASE_H
00048 #include "base.h"
00049 #endif /* BASE_H */
00050 
00051 /*
00052  * nssItem_Create
00053  *
00054  * -- fgmr comments --
00055  *
00056  * The error may be one of the following values:
00057  *  NSS_ERROR_INVALID_ARENA
00058  *  NSS_ERROR_NO_MEMORY
00059  *  NSS_ERROR_ARENA_MARKED_BY_ANOTHER_THREAD
00060  *  NSS_ERROR_INVALID_POINTER
00061  *  
00062  * Return value:
00063  *  A pointer to an NSSItem upon success
00064  *  NULL upon failure
00065  */
00066 
00067 NSS_IMPLEMENT NSSItem *
00068 nssItem_Create
00069 (
00070   NSSArena *arenaOpt,
00071   NSSItem *rvOpt,
00072   PRUint32 length,
00073   const void *data
00074 )
00075 {
00076   NSSItem *rv = (NSSItem *)NULL;
00077 
00078 #ifdef DEBUG
00079   if( (NSSArena *)NULL != arenaOpt ) {
00080     if( PR_SUCCESS != nssArena_verifyPointer(arenaOpt) ) {
00081       return (NSSItem *)NULL;
00082     }
00083   }
00084 
00085   if( (const void *)NULL == data ) {
00086     if( length > 0 ) {
00087       nss_SetError(NSS_ERROR_INVALID_POINTER);
00088       return (NSSItem *)NULL;
00089     }
00090   }
00091 #endif /* DEBUG */
00092 
00093   if( (NSSItem *)NULL == rvOpt ) {
00094     rv = (NSSItem *)nss_ZNEW(arenaOpt, NSSItem);
00095     if( (NSSItem *)NULL == rv ) {
00096       goto loser;
00097     }
00098   } else {
00099     rv = rvOpt;
00100   }
00101 
00102   rv->size = length;
00103   rv->data = nss_ZAlloc(arenaOpt, length);
00104   if( (void *)NULL == rv->data ) {
00105     goto loser;
00106   }
00107 
00108   if( length > 0 ) {
00109     (void)nsslibc_memcpy(rv->data, data, length);
00110   }
00111 
00112   return rv;
00113 
00114  loser:
00115   if( rv != rvOpt ) {
00116     nss_ZFreeIf(rv);
00117   }
00118 
00119   return (NSSItem *)NULL;
00120 }
00121 
00122 NSS_IMPLEMENT void
00123 nssItem_Destroy
00124 (
00125   NSSItem *item
00126 )
00127 {
00128   nss_ClearErrorStack();
00129 
00130   nss_ZFreeIf(item->data);
00131   nss_ZFreeIf(item);
00132 
00133 }
00134 
00135 /*
00136  * nssItem_Duplicate
00137  *
00138  * -- fgmr comments --
00139  *
00140  * The error may be one of the following values:
00141  *  NSS_ERROR_INVALID_ARENA
00142  *  NSS_ERROR_NO_MEMORY
00143  *  NSS_ERROR_ARENA_MARKED_BY_ANOTHER_THREAD
00144  *  NSS_ERROR_INVALID_ITEM
00145  *  
00146  * Return value:
00147  *  A pointer to an NSSItem upon success
00148  *  NULL upon failure
00149  */
00150 
00151 NSS_IMPLEMENT NSSItem *
00152 nssItem_Duplicate
00153 (
00154   NSSItem *obj,
00155   NSSArena *arenaOpt,
00156   NSSItem *rvOpt
00157 )
00158 {
00159 #ifdef DEBUG
00160   if( (NSSArena *)NULL != arenaOpt ) {
00161     if( PR_SUCCESS != nssArena_verifyPointer(arenaOpt) ) {
00162       return (NSSItem *)NULL;
00163     }
00164   }
00165 
00166   if( (NSSItem *)NULL == obj ) {
00167     nss_SetError(NSS_ERROR_INVALID_ITEM);
00168     return (NSSItem *)NULL;
00169   }
00170 #endif /* DEBUG */
00171 
00172   return nssItem_Create(arenaOpt, rvOpt, obj->size, obj->data);
00173 }
00174 
00175 #ifdef DEBUG
00176 /*
00177  * nssItem_verifyPointer
00178  *
00179  * -- fgmr comments --
00180  *
00181  * The error may be one of the following values:
00182  *  NSS_ERROR_INVALID_ITEM
00183  *
00184  * Return value:
00185  *  PR_SUCCESS upon success
00186  *  PR_FAILURE upon failure
00187  */
00188 
00189 NSS_IMPLEMENT PRStatus
00190 nssItem_verifyPointer
00191 (
00192   const NSSItem *item
00193 )
00194 {
00195   if( ((const NSSItem *)NULL == item) ||
00196       (((void *)NULL == item->data) && (item->size > 0)) ) {
00197     nss_SetError(NSS_ERROR_INVALID_ITEM);
00198     return PR_FAILURE;
00199   }
00200 
00201   return PR_SUCCESS;
00202 }
00203 #endif /* DEBUG */
00204 
00205 /*
00206  * nssItem_Equal
00207  *
00208  * -- fgmr comments --
00209  *
00210  * The error may be one of the following values:
00211  *  NSS_ERROR_INVALID_ITEM
00212  *
00213  * Return value:
00214  *  PR_TRUE if the items are identical
00215  *  PR_FALSE if they aren't
00216  *  PR_FALSE upon error
00217  */
00218 
00219 NSS_IMPLEMENT PRBool
00220 nssItem_Equal
00221 (
00222   const NSSItem *one,
00223   const NSSItem *two,
00224   PRStatus *statusOpt
00225 )
00226 {
00227   if( (PRStatus *)NULL != statusOpt ) {
00228     *statusOpt = PR_SUCCESS;
00229   }
00230 
00231   if( ((const NSSItem *)NULL == one) && ((const NSSItem *)NULL == two) ) {
00232     return PR_TRUE;
00233   }
00234 
00235   if( ((const NSSItem *)NULL == one) || ((const NSSItem *)NULL == two) ) {
00236     return PR_FALSE;
00237   }
00238 
00239   if( one->size != two->size ) {
00240     return PR_FALSE;
00241   }
00242 
00243   return nsslibc_memequal(one->data, two->data, one->size, statusOpt);
00244 }