OLD | NEW |
(Empty) | |
| 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/. */ |
| 4 |
| 5 /* |
| 6 * Support routines for SECItem data structure. |
| 7 * |
| 8 * $Id$ |
| 9 */ |
| 10 |
| 11 #include "seccomon.h" |
| 12 #include "secitem.h" |
| 13 #include "secitem_array.h" |
| 14 #include "secerr.h" |
| 15 #include "secport.h" |
| 16 |
| 17 SECItemArray * |
| 18 SECITEM_AllocArray(PLArenaPool *arena, SECItemArray *array, unsigned int len) |
| 19 { |
| 20 SECItemArray *result = NULL; |
| 21 void *mark = NULL; |
| 22 |
| 23 if (arena != NULL) { |
| 24 mark = PORT_ArenaMark(arena); |
| 25 } |
| 26 |
| 27 if (array == NULL) { |
| 28 if (arena != NULL) { |
| 29 result = PORT_ArenaZAlloc(arena, sizeof(SECItemArray)); |
| 30 } else { |
| 31 result = PORT_ZAlloc(sizeof(SECItemArray)); |
| 32 } |
| 33 if (result == NULL) { |
| 34 goto loser; |
| 35 } |
| 36 } else { |
| 37 PORT_Assert(array->items == NULL); |
| 38 result = array; |
| 39 } |
| 40 |
| 41 result->len = len; |
| 42 if (len) { |
| 43 if (arena != NULL) { |
| 44 result->items = PORT_ArenaZNewArray(arena, SECItem, len); |
| 45 } else { |
| 46 result->items = PORT_ZNewArray(SECItem, len); |
| 47 } |
| 48 if (result->items == NULL) { |
| 49 goto loser; |
| 50 } |
| 51 } else { |
| 52 result->items = NULL; |
| 53 } |
| 54 |
| 55 if (mark) { |
| 56 PORT_ArenaUnmark(arena, mark); |
| 57 } |
| 58 return(result); |
| 59 |
| 60 loser: |
| 61 if ( arena != NULL ) { |
| 62 if (mark) { |
| 63 PORT_ArenaRelease(arena, mark); |
| 64 } |
| 65 if (array != NULL) { |
| 66 array->items = NULL; |
| 67 array->len = 0; |
| 68 } |
| 69 } else { |
| 70 if (result != NULL && array == NULL) { |
| 71 PORT_Free(result); |
| 72 } |
| 73 /* |
| 74 * If array is not NULL, the above has set array->data and |
| 75 * array->len to 0. |
| 76 */ |
| 77 } |
| 78 return(NULL); |
| 79 } |
| 80 |
| 81 static void |
| 82 secitem_FreeArray(SECItemArray *array, PRBool zero_items, PRBool freeit) |
| 83 { |
| 84 unsigned int i; |
| 85 |
| 86 if (!array || !array->len || !array->items) |
| 87 return; |
| 88 |
| 89 for (i=0; i<array->len; ++i) { |
| 90 SECItem *item = &array->items[i]; |
| 91 |
| 92 if (item->data) { |
| 93 if (zero_items) { |
| 94 SECITEM_ZfreeItem(item, PR_FALSE); |
| 95 } else { |
| 96 SECITEM_FreeItem(item, PR_FALSE); |
| 97 } |
| 98 } |
| 99 } |
| 100 PORT_Free(array->items); |
| 101 |
| 102 if (freeit) |
| 103 PORT_Free(array); |
| 104 } |
| 105 |
| 106 void SECITEM_FreeArray(SECItemArray *array, PRBool freeit) |
| 107 { |
| 108 secitem_FreeArray(array, PR_FALSE, freeit); |
| 109 } |
| 110 |
| 111 void SECITEM_ZfreeArray(SECItemArray *array, PRBool freeit) |
| 112 { |
| 113 secitem_FreeArray(array, PR_TRUE, freeit); |
| 114 } |
| 115 |
| 116 SECItemArray * |
| 117 SECITEM_DupArray(PLArenaPool *arena, const SECItemArray *from) |
| 118 { |
| 119 SECItemArray *result; |
| 120 unsigned int i; |
| 121 |
| 122 if (!from || !from->items || !from->len) |
| 123 return NULL; |
| 124 |
| 125 result = SECITEM_AllocArray(arena, NULL, from->len); |
| 126 if (!result) |
| 127 return NULL; |
| 128 |
| 129 for (i=0; i<from->len; ++i) { |
| 130 SECStatus rv = SECITEM_CopyItem(arena, |
| 131 &result->items[i], &from->items[i]); |
| 132 if (rv != SECSuccess) { |
| 133 SECITEM_ZfreeArray(result, PR_TRUE); |
| 134 return NULL; |
| 135 } |
| 136 } |
| 137 |
| 138 return result; |
| 139 } |
OLD | NEW |