Back to index

lightning-sunbird  0.9+nobinonly
find.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: find.c,v $ $Revision: 1.6.28.1 $ $Date: 2006/04/20 00:03:51 $";
00039 #endif /* DEBUG */
00040 
00041 /*
00042  * find.c
00043  *
00044  * This file implements the nssCKFWFindObjects type and methods.
00045  */
00046 
00047 #ifndef CK_H
00048 #include "ck.h"
00049 #endif /* CK_H */
00050 
00051 /*
00052  * NSSCKFWFindObjects
00053  *
00054  *  -- create/destroy --
00055  *  nssCKFWFindObjects_Create
00056  *  nssCKFWFindObjects_Destroy
00057  *
00058  *  -- public accessors --
00059  *  NSSCKFWFindObjects_GetMDFindObjects
00060  * 
00061  *  -- implement public accessors --
00062  *  nssCKFWFindObjects_GetMDFindObjects
00063  *
00064  *  -- private accessors --
00065  *
00066  *  -- module fronts --
00067  *  nssCKFWFindObjects_Next
00068  */
00069 
00070 struct NSSCKFWFindObjectsStr {
00071   NSSCKFWMutex *mutex; /* merely to serialise the MDObject calls */
00072   NSSCKMDFindObjects *mdfo1;
00073   NSSCKMDFindObjects *mdfo2;
00074   NSSCKFWSession *fwSession;
00075   NSSCKMDSession *mdSession;
00076   NSSCKFWToken *fwToken;
00077   NSSCKMDToken *mdToken;
00078   NSSCKFWInstance *fwInstance;
00079   NSSCKMDInstance *mdInstance;
00080 
00081   NSSCKMDFindObjects *mdFindObjects; /* varies */
00082 };
00083 
00084 #ifdef DEBUG
00085 /*
00086  * But first, the pointer-tracking stuff.
00087  *
00088  * NOTE: the pointer-tracking support in NSS/base currently relies
00089  * upon NSPR's CallOnce support.  That, however, relies upon NSPR's
00090  * locking, which is tied into the runtime.  We need a pointer-tracker
00091  * implementation that uses the locks supplied through C_Initialize.
00092  * That support, however, can be filled in later.  So for now, I'll
00093  * just do these routines as no-ops.
00094  */
00095 
00096 static CK_RV
00097 findObjects_add_pointer
00098 (
00099   const NSSCKFWFindObjects *fwFindObjects
00100 )
00101 {
00102   return CKR_OK;
00103 }
00104 
00105 static CK_RV
00106 findObjects_remove_pointer
00107 (
00108   const NSSCKFWFindObjects *fwFindObjects
00109 )
00110 {
00111   return CKR_OK;
00112 }
00113 
00114 NSS_IMPLEMENT CK_RV
00115 nssCKFWFindObjects_verifyPointer
00116 (
00117   const NSSCKFWFindObjects *fwFindObjects
00118 )
00119 {
00120   return CKR_OK;
00121 }
00122 
00123 #endif /* DEBUG */
00124 
00125 /*
00126  * nssCKFWFindObjects_Create
00127  *
00128  */
00129 NSS_EXTERN NSSCKFWFindObjects *
00130 nssCKFWFindObjects_Create
00131 (
00132   NSSCKFWSession *fwSession,
00133   NSSCKFWToken *fwToken,
00134   NSSCKFWInstance *fwInstance,
00135   NSSCKMDFindObjects *mdFindObjects1,
00136   NSSCKMDFindObjects *mdFindObjects2,
00137   CK_RV *pError
00138 )
00139 {
00140   NSSCKFWFindObjects *fwFindObjects = NULL;
00141   NSSCKMDSession *mdSession;
00142   NSSCKMDToken *mdToken;
00143   NSSCKMDInstance *mdInstance;
00144 
00145   mdSession = nssCKFWSession_GetMDSession(fwSession);
00146   mdToken = nssCKFWToken_GetMDToken(fwToken);
00147   mdInstance = nssCKFWInstance_GetMDInstance(fwInstance);
00148 
00149   fwFindObjects = nss_ZNEW(NULL, NSSCKFWFindObjects);
00150   if( (NSSCKFWFindObjects *)NULL == fwFindObjects ) {
00151     *pError = CKR_HOST_MEMORY;
00152     goto loser;
00153   }
00154 
00155   fwFindObjects->mdfo1 = mdFindObjects1;
00156   fwFindObjects->mdfo2 = mdFindObjects2;
00157   fwFindObjects->fwSession = fwSession;
00158   fwFindObjects->mdSession = mdSession;
00159   fwFindObjects->fwToken = fwToken;
00160   fwFindObjects->mdToken = mdToken;
00161   fwFindObjects->fwInstance = fwInstance;
00162   fwFindObjects->mdInstance = mdInstance;
00163 
00164   fwFindObjects->mutex = nssCKFWInstance_CreateMutex(fwInstance, NULL, pError);
00165   if( (NSSCKFWMutex *)NULL == fwFindObjects->mutex ) {
00166     goto loser;
00167   }
00168 
00169 #ifdef DEBUG
00170   *pError = findObjects_add_pointer(fwFindObjects);
00171   if( CKR_OK != *pError ) {
00172     goto loser;
00173   }
00174 #endif /* DEBUG */
00175 
00176   return fwFindObjects;
00177 
00178  loser:
00179   if( fwFindObjects ) {
00180     if( NULL != mdFindObjects1 ) {
00181       if( NULL != mdFindObjects1->Final ) {
00182         fwFindObjects->mdFindObjects = mdFindObjects1;
00183         mdFindObjects1->Final(mdFindObjects1, fwFindObjects, mdSession, 
00184           fwSession, mdToken, fwToken, mdInstance, fwInstance);
00185       }
00186     }
00187 
00188     if( NULL != mdFindObjects2 ) {
00189       if( NULL != mdFindObjects2->Final ) {
00190         fwFindObjects->mdFindObjects = mdFindObjects2;
00191         mdFindObjects2->Final(mdFindObjects2, fwFindObjects, mdSession, 
00192           fwSession, mdToken, fwToken, mdInstance, fwInstance);
00193       }
00194     }
00195 
00196     nss_ZFreeIf(fwFindObjects);
00197   }
00198 
00199   if( CKR_OK == *pError ) {
00200     *pError = CKR_GENERAL_ERROR;
00201   }
00202 
00203   return (NSSCKFWFindObjects *)NULL;
00204 }
00205 
00206 
00207 /*
00208  * nssCKFWFindObjects_Destroy
00209  *
00210  */
00211 NSS_EXTERN void
00212 nssCKFWFindObjects_Destroy
00213 (
00214   NSSCKFWFindObjects *fwFindObjects
00215 )
00216 {
00217 #ifdef NSSDEBUG
00218   if( CKR_OK != nssCKFWFindObjects_verifyPointer(fwFindObjects) ) {
00219     return;
00220   }
00221 #endif /* NSSDEBUG */
00222 
00223   (void)nssCKFWMutex_Destroy(fwFindObjects->mutex);
00224 
00225   if( (NSSCKMDFindObjects *)NULL != fwFindObjects->mdfo1 ) {
00226     if( (void *)NULL != (void *)fwFindObjects->mdfo1->Final ) {
00227       fwFindObjects->mdFindObjects = fwFindObjects->mdfo1;
00228       fwFindObjects->mdfo1->Final(fwFindObjects->mdfo1, fwFindObjects,
00229         fwFindObjects->mdSession, fwFindObjects->fwSession, 
00230         fwFindObjects->mdToken, fwFindObjects->fwToken,
00231         fwFindObjects->mdInstance, fwFindObjects->fwInstance);
00232     }
00233   }
00234 
00235   if( (NSSCKMDFindObjects *)NULL != fwFindObjects->mdfo2 ) {
00236     if( (void *)NULL != (void *)fwFindObjects->mdfo2->Final ) {
00237       fwFindObjects->mdFindObjects = fwFindObjects->mdfo2;
00238       fwFindObjects->mdfo2->Final(fwFindObjects->mdfo2, fwFindObjects,
00239         fwFindObjects->mdSession, fwFindObjects->fwSession, 
00240         fwFindObjects->mdToken, fwFindObjects->fwToken,
00241         fwFindObjects->mdInstance, fwFindObjects->fwInstance);
00242     }
00243   }
00244 
00245   nss_ZFreeIf(fwFindObjects);
00246 
00247 #ifdef DEBUG
00248   (void)findObjects_remove_pointer(fwFindObjects);
00249 #endif /* DEBUG */
00250 
00251   return;
00252 }
00253 
00254 /*
00255  * nssCKFWFindObjects_GetMDFindObjects
00256  *
00257  */
00258 NSS_EXTERN NSSCKMDFindObjects *
00259 nssCKFWFindObjects_GetMDFindObjects
00260 (
00261   NSSCKFWFindObjects *fwFindObjects
00262 )
00263 {
00264 #ifdef NSSDEBUG
00265   if( CKR_OK != nssCKFWFindObjects_verifyPointer(fwFindObjects) ) {
00266     return (NSSCKMDFindObjects *)NULL;
00267   }
00268 #endif /* NSSDEBUG */
00269 
00270   return fwFindObjects->mdFindObjects;
00271 }
00272 
00273 /*
00274  * nssCKFWFindObjects_Next
00275  *
00276  */
00277 NSS_EXTERN NSSCKFWObject *
00278 nssCKFWFindObjects_Next
00279 (
00280   NSSCKFWFindObjects *fwFindObjects,
00281   NSSArena *arenaOpt,
00282   CK_RV *pError
00283 )
00284 {
00285   NSSCKMDObject *mdObject;
00286   NSSCKFWObject *fwObject = (NSSCKFWObject *)NULL;
00287   NSSArena *objArena;
00288 
00289 #ifdef NSSDEBUG
00290   if( (CK_RV *)NULL == pError ) {
00291     return (NSSCKFWObject *)NULL;
00292   }
00293 
00294   *pError = nssCKFWFindObjects_verifyPointer(fwFindObjects);
00295   if( CKR_OK != *pError ) {
00296     return (NSSCKFWObject *)NULL;
00297   }
00298 #endif /* NSSDEBUG */
00299 
00300   *pError = nssCKFWMutex_Lock(fwFindObjects->mutex);
00301   if( CKR_OK != *pError ) {
00302     return (NSSCKFWObject *)NULL;
00303   }
00304 
00305   if( (NSSCKMDFindObjects *)NULL != fwFindObjects->mdfo1 ) {
00306     if( (void *)NULL != (void *)fwFindObjects->mdfo1->Next ) {
00307       fwFindObjects->mdFindObjects = fwFindObjects->mdfo1;
00308       mdObject = fwFindObjects->mdfo1->Next(fwFindObjects->mdfo1,
00309         fwFindObjects, fwFindObjects->mdSession, fwFindObjects->fwSession,
00310         fwFindObjects->mdToken, fwFindObjects->fwToken, 
00311         fwFindObjects->mdInstance, fwFindObjects->fwInstance,
00312         arenaOpt, pError);
00313       if( (NSSCKMDObject *)NULL == mdObject ) {
00314         if( CKR_OK != *pError ) {
00315           goto done;
00316         }
00317 
00318         /* All done. */
00319         fwFindObjects->mdfo1->Final(fwFindObjects->mdfo1, fwFindObjects,
00320           fwFindObjects->mdSession, fwFindObjects->fwSession,
00321           fwFindObjects->mdToken, fwFindObjects->fwToken, 
00322           fwFindObjects->mdInstance, fwFindObjects->fwInstance);
00323         fwFindObjects->mdfo1 = (NSSCKMDFindObjects *)NULL;
00324       } else {
00325         goto wrap;
00326       }
00327     }
00328   }
00329 
00330   if( (NSSCKMDFindObjects *)NULL != fwFindObjects->mdfo2 ) {
00331     if( (void *)NULL != (void *)fwFindObjects->mdfo2->Next ) {
00332       fwFindObjects->mdFindObjects = fwFindObjects->mdfo2;
00333       mdObject = fwFindObjects->mdfo2->Next(fwFindObjects->mdfo2,
00334         fwFindObjects, fwFindObjects->mdSession, fwFindObjects->fwSession,
00335         fwFindObjects->mdToken, fwFindObjects->fwToken, 
00336         fwFindObjects->mdInstance, fwFindObjects->fwInstance,
00337         arenaOpt, pError);
00338       if( (NSSCKMDObject *)NULL == mdObject ) {
00339         if( CKR_OK != *pError ) {
00340           goto done;
00341         }
00342 
00343         /* All done. */
00344         fwFindObjects->mdfo2->Final(fwFindObjects->mdfo2, fwFindObjects,
00345           fwFindObjects->mdSession, fwFindObjects->fwSession,
00346           fwFindObjects->mdToken, fwFindObjects->fwToken, 
00347           fwFindObjects->mdInstance, fwFindObjects->fwInstance);
00348         fwFindObjects->mdfo2 = (NSSCKMDFindObjects *)NULL;
00349       } else {
00350         goto wrap;
00351       }
00352     }
00353   }
00354   
00355   /* No more objects */
00356   *pError = CKR_OK;
00357   goto done;
00358 
00359  wrap:
00360   /*
00361    * This is less than ideal-- we should determine if it's a token
00362    * object or a session object, and use the appropriate arena.
00363    * But that duplicates logic in nssCKFWObject_IsTokenObject.
00364    * Worry about that later.  For now, be conservative, and use
00365    * the token arena.
00366    */
00367   objArena = nssCKFWToken_GetArena(fwFindObjects->fwToken, pError);
00368   if( (NSSArena *)NULL == objArena ) {
00369     if( CKR_OK == *pError ) {
00370       *pError = CKR_HOST_MEMORY;
00371     }
00372     goto done;
00373   }
00374 
00375   fwObject = nssCKFWObject_Create(objArena, mdObject,
00376                fwFindObjects->fwSession, fwFindObjects->fwToken, 
00377                fwFindObjects->fwInstance, pError);
00378   if( (NSSCKFWObject *)NULL == fwObject ) {
00379     if( CKR_OK == *pError ) {
00380       *pError = CKR_GENERAL_ERROR;
00381     }
00382   }
00383 
00384  done:
00385   (void)nssCKFWMutex_Unlock(fwFindObjects->mutex);
00386   return fwObject;
00387 }
00388 
00389 /*
00390  * NSSCKFWFindObjects_GetMDFindObjects
00391  *
00392  */
00393 
00394 NSS_EXTERN NSSCKMDFindObjects *
00395 NSSCKFWFindObjects_GetMDFindObjects
00396 (
00397   NSSCKFWFindObjects *fwFindObjects
00398 )
00399 {
00400 #ifdef DEBUG
00401   if( CKR_OK != nssCKFWFindObjects_verifyPointer(fwFindObjects) ) {
00402     return (NSSCKMDFindObjects *)NULL;
00403   }
00404 #endif /* DEBUG */
00405 
00406   return nssCKFWFindObjects_GetMDFindObjects(fwFindObjects);
00407 }