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