diff options
Diffstat (limited to 'ckcapi-session.c')
-rw-r--r-- | ckcapi-session.c | 167 |
1 files changed, 164 insertions, 3 deletions
diff --git a/ckcapi-session.c b/ckcapi-session.c index ce7e9d2..9f9a6e6 100644 --- a/ckcapi-session.c +++ b/ckcapi-session.c @@ -11,6 +11,13 @@ typedef struct _SessionList { /* These are protected by global_mutex */ static SessionList the_sessions = { NULL, 0 }; +static void +object_data_release(CkCapiObjectData* objdata) +{ + ASSERT(objdata->data_funcs.release); + (objdata->data_funcs.release)(objdata->data); + free(objdata); +} CkCapiSession* ckcapi_session_create(void) @@ -19,9 +26,15 @@ ckcapi_session_create(void) if(!sess) return NULL; + sess->object_data = ckcapi_hash_new(); + if(!sess->object_data) { + free(sess); + return NULL; + } + sess->mutex = CreateMutex(NULL, FALSE, NULL); - if(!sess->mutex) - { + if(!sess->mutex) { + ckcapi_hash_free(sess->object_data, NULL); free(sess); return NULL; } @@ -132,6 +145,10 @@ ckcapi_session_destroy(CkCapiSession* sess) ASSERT(sess->operation_data == NULL); ASSERT(sess->operation_cancel == NULL); + /* Make all the object adat go away */ + ASSERT(sess->object_data != NULL); + ckcapi_hash_free(sess->object_data, object_data_release); + /* And make the mutex go away */ ASSERT(sess->mutex != NULL); CloseHandle(sess->mutex); @@ -328,6 +345,150 @@ ckcapi_session_close_all() } /* ---------------------------------------------------------------------------- + * OBJECT DATA + */ + +CK_RV +ckcapi_object_load_data(CkCapiObject* obj, CkCapiObjectData* objdata) +{ + ASSERT(obj); + ASSERT(obj->id); + ASSERT(obj->obj_funcs.load_data); + + memset(objdata, 0, sizeof(*objdata)); + return (obj->obj_funcs.load_data)(obj, objdata); +} + +CK_RV +ckcapi_session_get_object_data(CkCapiSession* sess, CkCapiObject* obj, + CkCapiObjectData** objdata) +{ + CK_OBJECT_HANDLE id; + CkCapiObjectData* newdata; + CK_RV ret; + + ASSERT(sess); + ASSERT(sess->object_data); + ASSERT(obj); + ASSERT(obj->obj_funcs.load_data); + ASSERT(objdata); + + id = obj->id; + + *objdata = ckcapi_hash_get(sess->object_data, &id, sizeof(id)); + if(*objdata) + return CKR_OK; + + newdata = calloc(sizeof(CkCapiObjectData), 1); + if(!newdata) + return CKR_HOST_MEMORY; + + newdata->object = id; + ret = (obj->obj_funcs.load_data)(obj, newdata); + if(ret != CKR_OK) { + free(newdata); + return ret; + } + + if(!ckcapi_hash_set(sess->object_data, &newdata->object, + sizeof(newdata->object), newdata)) { + object_data_release(newdata); + return CKR_HOST_MEMORY; + } + + *objdata = newdata; + return CKR_OK; +} + +void +ckcapi_session_clear_object_data(CkCapiSession* sess, CkCapiObject* obj) +{ + CkCapiObjectData* objdata; + + ASSERT(sess); + ASSERT(sess->object_data); + ASSERT(obj); + + objdata = (CkCapiObjectData*)ckcapi_hash_rem(sess->object_data, &obj->id, sizeof(obj->id)); + if(objdata) + object_data_release(objdata); +} + +void +ckcapi_session_enum_object_data(CkCapiSession* sess, + CkCapiEnumObjectData enum_func, void* arg) +{ + CK_OBJECT_HANDLE i, max; + CkCapiObject* obj; + CkCapiObjectData* objdata; + + ASSERT(sess); + ASSERT(sess->object_data); + ASSERT(enum_func); + + max = ckcapi_object_get_max_handle(); + for(i = 0; i < max; ++i) + { + objdata = (CkCapiObjectData*)ckcapi_hash_get(sess->object_data, &i, sizeof(i)); + if(!objdata) + continue; + + obj = ckcapi_object_lookup (sess, i); + if(!obj) + continue; + + (enum_func)(sess, obj, objdata, arg); + } +} + +CK_RV +ckcapi_session_get_object_data_for(CkCapiSession* sess, CK_OBJECT_HANDLE hand, + CkCapiObjectData** objdata) +{ + CkCapiObject* obj; + + obj = ckcapi_object_lookup(sess, hand); + if(!obj) + return CKR_OBJECT_HANDLE_INVALID; + + return ckcapi_session_get_object_data(sess, obj, objdata); +} + +CK_RV +ckcapi_session_set_object_data(CkCapiSession* sess, CkCapiObject* obj, + const CkCapiObjectData* objdata) +{ + CkCapiObjectData* newdata; + CkCapiObjectData* prev; + + ASSERT(obj); + ASSERT(sess); + ASSERT(sess->object_data); + ASSERT(objdata); + + newdata = calloc(sizeof(CkCapiObjectData), 1); + if(!newdata) + return CKR_HOST_MEMORY; + + newdata->object = obj->id; + newdata->data = objdata->data; + newdata->data_funcs = objdata->data_funcs; + + prev = ckcapi_hash_rem(sess->object_data, &obj->id, sizeof(obj->id)); + if(prev) + object_data_release(prev); + + if(!ckcapi_hash_set(sess->object_data, &newdata->object, + sizeof(newdata->object), newdata)) { + free(newdata); + return CKR_HOST_MEMORY; + } + + return CKR_OK; +} + + +/* ---------------------------------------------------------------------------- * FIND OPERATION */ @@ -420,7 +581,7 @@ purge_duplicate_ulongs(CkCapiArray* arr) } } - ckcapi_hash_free(checks); + ckcapi_hash_free(checks, NULL); } CK_RV |