summaryrefslogtreecommitdiffstats
path: root/security/nss/lib/ckfw/dbm/find.c
blob: 8a03855c3f3f1a6feedbe16ee1bc98df7bfd7d10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "ckdbm.h"

static void
nss_dbm_mdFindObjects_Final(
    NSSCKMDFindObjects *mdFindObjects,
    NSSCKFWFindObjects *fwFindObjects,
    NSSCKMDSession *mdSession,
    NSSCKFWSession *fwSession,
    NSSCKMDToken *mdToken,
    NSSCKFWToken *fwToken,
    NSSCKMDInstance *mdInstance,
    NSSCKFWInstance *fwInstance)
{
    nss_dbm_find_t *find = (nss_dbm_find_t *)mdFindObjects->etc;

    /* Locks might have system resources associated */
    (void)NSSCKFWMutex_Destroy(find->list_lock);
    (void)NSSArena_Destroy(find->arena);
}

static NSSCKMDObject *
nss_dbm_mdFindObjects_Next(
    NSSCKMDFindObjects *mdFindObjects,
    NSSCKFWFindObjects *fwFindObjects,
    NSSCKMDSession *mdSession,
    NSSCKFWSession *fwSession,
    NSSCKMDToken *mdToken,
    NSSCKFWToken *fwToken,
    NSSCKMDInstance *mdInstance,
    NSSCKFWInstance *fwInstance,
    NSSArena *arena,
    CK_RV *pError)
{
    nss_dbm_find_t *find = (nss_dbm_find_t *)mdFindObjects->etc;
    struct nss_dbm_dbt_node *node;
    nss_dbm_object_t *object;
    NSSCKMDObject *rv;

    while (1) {
        /* Lock */
        {
            *pError = NSSCKFWMutex_Lock(find->list_lock);
            if (CKR_OK != *pError) {
                return (NSSCKMDObject *)NULL;
            }

            node = find->found;
            if ((struct nss_dbm_dbt_node *)NULL != node) {
                find->found = node->next;
            }

            *pError = NSSCKFWMutex_Unlock(find->list_lock);
            if (CKR_OK != *pError) {
                /* screwed now */
                return (NSSCKMDObject *)NULL;
            }
        }

        if ((struct nss_dbm_dbt_node *)NULL == node) {
            break;
        }

        if (nss_dbm_db_object_still_exists(node->dbt)) {
            break;
        }
    }

    if ((struct nss_dbm_dbt_node *)NULL == node) {
        *pError = CKR_OK;
        return (NSSCKMDObject *)NULL;
    }

    object = nss_ZNEW(arena, nss_dbm_object_t);
    if ((nss_dbm_object_t *)NULL == object) {
        *pError = CKR_HOST_MEMORY;
        return (NSSCKMDObject *)NULL;
    }

    object->arena = arena;
    object->handle = nss_ZNEW(arena, nss_dbm_dbt_t);
    if ((nss_dbm_dbt_t *)NULL == object->handle) {
        *pError = CKR_HOST_MEMORY;
        return (NSSCKMDObject *)NULL;
    }

    object->handle->my_db = node->dbt->my_db;
    object->handle->dbt.size = node->dbt->dbt.size;
    object->handle->dbt.data = nss_ZAlloc(arena, node->dbt->dbt.size);
    if ((void *)NULL == object->handle->dbt.data) {
        *pError = CKR_HOST_MEMORY;
        return (NSSCKMDObject *)NULL;
    }

    (void)memcpy(object->handle->dbt.data, node->dbt->dbt.data, node->dbt->dbt.size);

    rv = nss_dbm_mdObject_factory(object, pError);
    if ((NSSCKMDObject *)NULL == rv) {
        return (NSSCKMDObject *)NULL;
    }

    return rv;
}

NSS_IMPLEMENT NSSCKMDFindObjects *
nss_dbm_mdFindObjects_factory(
    nss_dbm_find_t *find,
    CK_RV *pError)
{
    NSSCKMDFindObjects *rv;

    rv = nss_ZNEW(find->arena, NSSCKMDFindObjects);
    if ((NSSCKMDFindObjects *)NULL == rv) {
        *pError = CKR_HOST_MEMORY;
        return (NSSCKMDFindObjects *)NULL;
    }

    rv->etc = (void *)find;
    rv->Final = nss_dbm_mdFindObjects_Final;
    rv->Next = nss_dbm_mdFindObjects_Next;

    return rv;
}