Add explicit |forceOnlineSignin| to user pod status
[chromium-blink-merge.git] / net / third_party / nss / ssl / bodge / secitem_array.c
blob2acd5f8e24a2b5f2a899de3c1247246a45fe0119
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 /*
6 * Support routines for SECItemArray data structure.
7 */
9 #include "nssutil.h"
10 #include "seccomon.h"
11 #include "secitem.h"
12 #include "secerr.h"
13 #include "secport.h"
15 #define NSSUTIL_VERSION_NUM \
16 (NSSUTIL_VMAJOR * 10000 + NSSUTIL_VMINOR * 100 + NSSUTIL_VPATCH)
17 #if NSSUTIL_VERSION_NUM < 31500
18 // Added in NSS 3.15.
19 typedef struct SECItemArrayStr SECItemArray;
21 struct SECItemArrayStr {
22 SECItem *items;
23 unsigned int len;
25 #endif
27 SECItemArray *
28 SECITEM_AllocArray(PLArenaPool *arena, SECItemArray *array, unsigned int len)
30 SECItemArray *result = NULL;
31 void *mark = NULL;
33 if (arena != NULL) {
34 mark = PORT_ArenaMark(arena);
37 if (array == NULL) {
38 if (arena != NULL) {
39 result = PORT_ArenaZAlloc(arena, sizeof(SECItemArray));
40 } else {
41 result = PORT_ZAlloc(sizeof(SECItemArray));
43 if (result == NULL) {
44 goto loser;
46 } else {
47 PORT_Assert(array->items == NULL);
48 result = array;
51 result->len = len;
52 if (len) {
53 if (arena != NULL) {
54 result->items = PORT_ArenaZNewArray(arena, SECItem, len);
55 } else {
56 result->items = PORT_ZNewArray(SECItem, len);
58 if (result->items == NULL) {
59 goto loser;
61 } else {
62 result->items = NULL;
65 if (mark) {
66 PORT_ArenaUnmark(arena, mark);
68 return(result);
70 loser:
71 if ( arena != NULL ) {
72 if (mark) {
73 PORT_ArenaRelease(arena, mark);
75 if (array != NULL) {
76 array->items = NULL;
77 array->len = 0;
79 } else {
80 if (result != NULL && array == NULL) {
81 PORT_Free(result);
84 * If array is not NULL, the above has set array->data and
85 * array->len to 0.
88 return(NULL);
91 static void
92 secitem_FreeArray(SECItemArray *array, PRBool zero_items, PRBool freeit)
94 unsigned int i;
96 if (!array || !array->len || !array->items)
97 return;
99 for (i=0; i<array->len; ++i) {
100 SECItem *item = &array->items[i];
102 if (item->data) {
103 if (zero_items) {
104 SECITEM_ZfreeItem(item, PR_FALSE);
105 } else {
106 SECITEM_FreeItem(item, PR_FALSE);
110 PORT_Free(array->items);
111 array->items = NULL;
112 array->len = 0;
114 if (freeit)
115 PORT_Free(array);
118 void SECITEM_FreeArray(SECItemArray *array, PRBool freeit)
120 secitem_FreeArray(array, PR_FALSE, freeit);
123 void SECITEM_ZfreeArray(SECItemArray *array, PRBool freeit)
125 secitem_FreeArray(array, PR_TRUE, freeit);
128 SECItemArray *
129 SECITEM_DupArray(PLArenaPool *arena, const SECItemArray *from)
131 SECItemArray *result;
132 unsigned int i;
134 if (!from || !from->items || !from->len)
135 return NULL;
137 result = SECITEM_AllocArray(arena, NULL, from->len);
138 if (!result)
139 return NULL;
141 for (i=0; i<from->len; ++i) {
142 SECStatus rv = SECITEM_CopyItem(arena,
143 &result->items[i], &from->items[i]);
144 if (rv != SECSuccess) {
145 SECITEM_ZfreeArray(result, PR_TRUE);
146 return NULL;
150 return result;