summaryrefslogtreecommitdiffstats
path: root/security/nss/lib/base/item.c
blob: a1bb802ed6a29133344364330dacd0076d8ebc9e (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* 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/. */

/*
 * item.c
 *
 * This contains some item-manipulation code.
 */

#ifndef BASE_H
#include "base.h"
#endif /* BASE_H */

/*
 * nssItem_Create
 *
 * -- fgmr comments --
 *
 * The error may be one of the following values:
 *  NSS_ERROR_INVALID_ARENA
 *  NSS_ERROR_NO_MEMORY
 *  NSS_ERROR_ARENA_MARKED_BY_ANOTHER_THREAD
 *  NSS_ERROR_INVALID_POINTER
 *
 * Return value:
 *  A pointer to an NSSItem upon success
 *  NULL upon failure
 */

NSS_IMPLEMENT NSSItem *
nssItem_Create(NSSArena *arenaOpt, NSSItem *rvOpt, PRUint32 length,
               const void *data)
{
    NSSItem *rv = (NSSItem *)NULL;

#ifdef DEBUG
    if ((NSSArena *)NULL != arenaOpt) {
        if (PR_SUCCESS != nssArena_verifyPointer(arenaOpt)) {
            return (NSSItem *)NULL;
        }
    }

    if ((const void *)NULL == data) {
        if (length > 0) {
            nss_SetError(NSS_ERROR_INVALID_POINTER);
            return (NSSItem *)NULL;
        }
    }
#endif /* DEBUG */

    if ((NSSItem *)NULL == rvOpt) {
        rv = (NSSItem *)nss_ZNEW(arenaOpt, NSSItem);
        if ((NSSItem *)NULL == rv) {
            goto loser;
        }
    } else {
        rv = rvOpt;
    }

    rv->size = length;
    rv->data = nss_ZAlloc(arenaOpt, length);
    if ((void *)NULL == rv->data) {
        goto loser;
    }

    if (length > 0) {
        (void)nsslibc_memcpy(rv->data, data, length);
    }

    return rv;

loser:
    if (rv != rvOpt) {
        nss_ZFreeIf(rv);
    }

    return (NSSItem *)NULL;
}

NSS_IMPLEMENT void
nssItem_Destroy(NSSItem *item)
{
    nss_ClearErrorStack();

    nss_ZFreeIf(item->data);
    nss_ZFreeIf(item);
}

/*
 * nssItem_Duplicate
 *
 * -- fgmr comments --
 *
 * The error may be one of the following values:
 *  NSS_ERROR_INVALID_ARENA
 *  NSS_ERROR_NO_MEMORY
 *  NSS_ERROR_ARENA_MARKED_BY_ANOTHER_THREAD
 *  NSS_ERROR_INVALID_ITEM
 *
 * Return value:
 *  A pointer to an NSSItem upon success
 *  NULL upon failure
 */

NSS_IMPLEMENT NSSItem *
nssItem_Duplicate(NSSItem *obj, NSSArena *arenaOpt, NSSItem *rvOpt)
{
#ifdef DEBUG
    if ((NSSArena *)NULL != arenaOpt) {
        if (PR_SUCCESS != nssArena_verifyPointer(arenaOpt)) {
            return (NSSItem *)NULL;
        }
    }

    if ((NSSItem *)NULL == obj) {
        nss_SetError(NSS_ERROR_INVALID_ITEM);
        return (NSSItem *)NULL;
    }
#endif /* DEBUG */

    return nssItem_Create(arenaOpt, rvOpt, obj->size, obj->data);
}

#ifdef DEBUG
/*
 * nssItem_verifyPointer
 *
 * -- fgmr comments --
 *
 * The error may be one of the following values:
 *  NSS_ERROR_INVALID_ITEM
 *
 * Return value:
 *  PR_SUCCESS upon success
 *  PR_FAILURE upon failure
 */

NSS_IMPLEMENT PRStatus
nssItem_verifyPointer(const NSSItem *item)
{
    if (((const NSSItem *)NULL == item) ||
        (((void *)NULL == item->data) && (item->size > 0))) {
        nss_SetError(NSS_ERROR_INVALID_ITEM);
        return PR_FAILURE;
    }

    return PR_SUCCESS;
}
#endif /* DEBUG */

/*
 * nssItem_Equal
 *
 * -- fgmr comments --
 *
 * The error may be one of the following values:
 *  NSS_ERROR_INVALID_ITEM
 *
 * Return value:
 *  PR_TRUE if the items are identical
 *  PR_FALSE if they aren't
 *  PR_FALSE upon error
 */

NSS_IMPLEMENT PRBool
nssItem_Equal(const NSSItem *one, const NSSItem *two, PRStatus *statusOpt)
{
    if ((PRStatus *)NULL != statusOpt) {
        *statusOpt = PR_SUCCESS;
    }

    if (((const NSSItem *)NULL == one) && ((const NSSItem *)NULL == two)) {
        return PR_TRUE;
    }

    if (((const NSSItem *)NULL == one) || ((const NSSItem *)NULL == two)) {
        return PR_FALSE;
    }

    if (one->size != two->size) {
        return PR_FALSE;
    }

    return nsslibc_memequal(one->data, two->data, one->size, statusOpt);
}