]>
Commit | Line | Data |
---|---|---|
b1ab9ed8 A |
1 | /* |
2 | * The contents of this file are subject to the Mozilla Public | |
3 | * License Version 1.1 (the "License"); you may not use this file | |
4 | * except in compliance with the License. You may obtain a copy of | |
5 | * the License at http://www.mozilla.org/MPL/ | |
6b200bc3 | 6 | * |
b1ab9ed8 A |
7 | * Software distributed under the License is distributed on an "AS |
8 | * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or | |
9 | * implied. See the License for the specific language governing | |
10 | * rights and limitations under the License. | |
6b200bc3 | 11 | * |
b1ab9ed8 | 12 | * The Original Code is the Netscape security libraries. |
6b200bc3 | 13 | * |
b1ab9ed8 | 14 | * The Initial Developer of the Original Code is Netscape |
6b200bc3 | 15 | * Communications Corporation. Portions created by Netscape are |
b1ab9ed8 A |
16 | * Copyright (C) 1994-2000 Netscape Communications Corporation. All |
17 | * Rights Reserved. | |
6b200bc3 | 18 | * |
b1ab9ed8 | 19 | * Contributor(s): |
6b200bc3 | 20 | * |
b1ab9ed8 A |
21 | * Alternatively, the contents of this file may be used under the |
22 | * terms of the GNU General Public License Version 2 or later (the | |
6b200bc3 A |
23 | * "GPL"), in which case the provisions of the GPL are applicable |
24 | * instead of those above. If you wish to allow use of your | |
b1ab9ed8 A |
25 | * version of this file only under the terms of the GPL and not to |
26 | * allow others to use your version of this file under the MPL, | |
27 | * indicate your decision by deleting the provisions above and | |
28 | * replace them with the notice and other provisions required by | |
29 | * the GPL. If you do not delete the provisions above, a recipient | |
30 | * may use your version of this file under either the MPL or the | |
31 | * GPL. | |
32 | */ | |
33 | ||
34 | /* | |
35 | * Support routines for SECItem data structure. | |
36 | */ | |
37 | ||
38 | #include "secitem.h" | |
39 | #include <security_asn1/seccomon.h> | |
40 | #include <security_asn1/secerr.h> | |
41 | ||
42 | SECItem * | |
60c433a9 | 43 | SECITEM_AllocItem(PRArenaPool *arena, SECItem *item, size_t len) |
b1ab9ed8 A |
44 | { |
45 | SECItem *result = NULL; | |
46 | void *mark = NULL; | |
47 | ||
48 | if (arena != NULL) { | |
6b200bc3 | 49 | mark = PORT_ArenaMark(arena); |
b1ab9ed8 A |
50 | } |
51 | ||
52 | if (item == NULL) { | |
6b200bc3 A |
53 | if (arena != NULL) { |
54 | result = PORT_ArenaZAlloc(arena, sizeof(SECItem)); | |
55 | } else { | |
56 | result = PORT_ZAlloc(sizeof(SECItem)); | |
57 | } | |
58 | if (result == NULL) { | |
59 | goto loser; | |
60 | } | |
b1ab9ed8 | 61 | } else { |
6b200bc3 A |
62 | PORT_Assert(item->Data == NULL); |
63 | result = item; | |
b1ab9ed8 A |
64 | } |
65 | ||
66 | result->Length = len; | |
67 | if (len) { | |
6b200bc3 A |
68 | if (arena != NULL) { |
69 | result->Data = PORT_ArenaAlloc(arena, len); | |
70 | } else { | |
71 | result->Data = PORT_Alloc(len); | |
72 | } | |
b1ab9ed8 A |
73 | } |
74 | ||
75 | if (mark) { | |
6b200bc3 | 76 | PORT_ArenaUnmark(arena, mark); |
b1ab9ed8 A |
77 | } |
78 | return(result); | |
79 | ||
80 | loser: | |
6b200bc3 A |
81 | if (arena != NULL && mark) { |
82 | PORT_ArenaRelease(arena, mark); | |
b1ab9ed8 A |
83 | } |
84 | return(NULL); | |
85 | } | |
86 | ||
87 | SECStatus | |
88 | SECITEM_ReallocItem(PRArenaPool *arena, SECItem *item, unsigned int oldlen, | |
6b200bc3 | 89 | unsigned int newlen) |
b1ab9ed8 A |
90 | { |
91 | PORT_Assert(item != NULL); | |
92 | if (item == NULL) { | |
6b200bc3 A |
93 | /* XXX Set error. But to what? */ |
94 | return SECFailure; | |
b1ab9ed8 A |
95 | } |
96 | ||
97 | /* | |
98 | * If no old length, degenerate to just plain alloc. | |
99 | */ | |
100 | if (oldlen == 0) { | |
6b200bc3 A |
101 | PORT_Assert(item->Data == NULL || item->Length == 0); |
102 | if (newlen == 0) { | |
103 | /* Nothing to do. Weird, but not a failure. */ | |
104 | return SECSuccess; | |
105 | } | |
106 | item->Length = newlen; | |
107 | if (arena != NULL) { | |
108 | item->Data = PORT_ArenaAlloc(arena, newlen); | |
109 | } else { | |
110 | item->Data = PORT_Alloc(newlen); | |
111 | } | |
b1ab9ed8 | 112 | } else { |
6b200bc3 A |
113 | if (arena != NULL) { |
114 | item->Data = PORT_ArenaGrow(arena, item->Data, oldlen, newlen); | |
115 | } else { | |
116 | item->Data = PORT_Realloc(item->Data, newlen); | |
117 | } | |
b1ab9ed8 A |
118 | } |
119 | ||
120 | if (item->Data == NULL) { | |
6b200bc3 | 121 | return SECFailure; |
b1ab9ed8 A |
122 | } |
123 | ||
124 | return SECSuccess; | |
125 | } | |
126 | ||
127 | SECComparison | |
128 | SECITEM_CompareItem(const SECItem *a, const SECItem *b) | |
129 | { | |
427c49bc | 130 | CSSM_SIZE m; |
b1ab9ed8 A |
131 | SECComparison rv; |
132 | ||
133 | m = ( ( a->Length < b->Length ) ? a->Length : b->Length ); | |
6b200bc3 | 134 | |
b1ab9ed8 A |
135 | rv = (SECComparison) PORT_Memcmp(a->Data, b->Data, m); |
136 | if (rv) { | |
6b200bc3 | 137 | return rv; |
b1ab9ed8 A |
138 | } |
139 | if (a->Length < b->Length) { | |
6b200bc3 | 140 | return SECLessThan; |
b1ab9ed8 A |
141 | } |
142 | if (a->Length == b->Length) { | |
6b200bc3 | 143 | return SECEqual; |
b1ab9ed8 A |
144 | } |
145 | return SECGreaterThan; | |
146 | } | |
147 | ||
148 | Boolean | |
149 | SECITEM_ItemsAreEqual(const SECItem *a, const SECItem *b) | |
150 | { | |
151 | if (a->Length != b->Length) | |
152 | return PR_FALSE; | |
153 | if (!a->Length) | |
6b200bc3 | 154 | return PR_TRUE; |
b1ab9ed8 A |
155 | if (!a->Data || !b->Data) { |
156 | /* avoid null pointer crash. */ | |
6b200bc3 | 157 | return (Boolean)(a->Data == b->Data); |
b1ab9ed8 A |
158 | } |
159 | return (Boolean)!PORT_Memcmp(a->Data, b->Data, a->Length); | |
160 | } | |
161 | ||
162 | SECItem * | |
163 | SECITEM_DupItem(const SECItem *from) | |
164 | { | |
165 | return SECITEM_ArenaDupItem(NULL, from); | |
166 | } | |
167 | ||
168 | SECItem * | |
169 | SECITEM_ArenaDupItem(PRArenaPool *arena, const SECItem *from) | |
170 | { | |
171 | SECItem *to; | |
172 | ||
173 | if ( from == NULL ) { | |
6b200bc3 | 174 | return(NULL); |
b1ab9ed8 A |
175 | } |
176 | ||
177 | if ( arena != NULL ) { | |
6b200bc3 | 178 | to = (SECItem *)PORT_ArenaAlloc(arena, sizeof(SECItem)); |
b1ab9ed8 | 179 | } else { |
6b200bc3 | 180 | to = (SECItem *)PORT_Alloc(sizeof(SECItem)); |
b1ab9ed8 A |
181 | } |
182 | if ( to == NULL ) { | |
6b200bc3 | 183 | return(NULL); |
b1ab9ed8 A |
184 | } |
185 | ||
186 | if ( arena != NULL ) { | |
6b200bc3 | 187 | to->Data = (unsigned char *)PORT_ArenaAlloc(arena, from->Length); |
b1ab9ed8 | 188 | } else { |
6b200bc3 | 189 | to->Data = (unsigned char *)PORT_Alloc(from->Length); |
b1ab9ed8 A |
190 | } |
191 | if ( to->Data == NULL ) { | |
6b200bc3 A |
192 | PORT_Free(to); |
193 | return(NULL); | |
b1ab9ed8 A |
194 | } |
195 | ||
196 | to->Length = from->Length; | |
197 | // to->type = from->type; | |
198 | if ( to->Length ) { | |
6b200bc3 | 199 | PORT_Memcpy(to->Data, from->Data, to->Length); |
b1ab9ed8 A |
200 | } |
201 | ||
202 | return(to); | |
203 | } | |
204 | ||
205 | SECStatus | |
206 | SECITEM_CopyItem(PRArenaPool *arena, SECItem *to, const SECItem *from) | |
207 | { | |
208 | // to->type = from->type; | |
209 | if (from->Data && from->Length) { | |
6b200bc3 A |
210 | if ( arena ) { |
211 | to->Data = (unsigned char*) PORT_ArenaAlloc(arena, from->Length); | |
212 | } else { | |
213 | to->Data = (unsigned char*) PORT_Alloc(from->Length); | |
214 | } | |
215 | ||
216 | if (!to->Data) { | |
217 | return SECFailure; | |
218 | } | |
219 | PORT_Memcpy(to->Data, from->Data, from->Length); | |
220 | to->Length = from->Length; | |
b1ab9ed8 | 221 | } else { |
6b200bc3 A |
222 | to->Data = 0; |
223 | to->Length = 0; | |
b1ab9ed8 A |
224 | } |
225 | return SECSuccess; | |
226 | } | |
227 | ||
228 | void | |
229 | SECITEM_FreeItem(SECItem *zap, Boolean freeit) | |
230 | { | |
231 | if (zap) { | |
6b200bc3 A |
232 | PORT_Free(zap->Data); |
233 | zap->Data = 0; | |
234 | zap->Length = 0; | |
235 | if (freeit) { | |
236 | PORT_Free(zap); | |
237 | } | |
b1ab9ed8 A |
238 | } |
239 | } | |
240 | ||
241 | void | |
242 | SECITEM_ZfreeItem(SECItem *zap, Boolean freeit) | |
243 | { | |
244 | if (zap) { | |
6b200bc3 A |
245 | PORT_ZFree(zap->Data, zap->Length); |
246 | zap->Data = 0; | |
247 | zap->Length = 0; | |
248 | if (freeit) { | |
249 | PORT_ZFree(zap, sizeof(SECItem)); | |
250 | } | |
b1ab9ed8 A |
251 | } |
252 | } | |
253 | ||
254 | ||
255 | /* these reroutines were taken from pkix oid.c, which is supposed to | |
256 | * replace this file some day */ | |
257 | /* | |
258 | * This is the hash function. We simply XOR the encoded form with | |
259 | * itself in sizeof(PLHashNumber)-byte chunks. Improving this | |
260 | * routine is left as an excercise for the more mathematically | |
261 | * inclined student. | |
262 | */ | |
263 | PLHashNumber PR_CALLBACK | |
264 | SECITEM_Hash ( const void *key) | |
265 | { | |
266 | const SECItem *item = (const SECItem *)key; | |
267 | PLHashNumber rv = 0; | |
268 | ||
269 | PRUint8 *data = (PRUint8 *)item->Data; | |
270 | PRUint32 i; | |
271 | PRUint8 *rvc = (PRUint8 *)&rv; | |
6b200bc3 | 272 | |
b1ab9ed8 A |
273 | for( i = 0; i < item->Length; i++ ) { |
274 | rvc[ i % sizeof(rv) ] ^= *data; | |
275 | data++; | |
276 | } | |
6b200bc3 | 277 | |
b1ab9ed8 A |
278 | return rv; |
279 | } | |
280 | ||
281 | /* | |
282 | * This is the key-compare function. It simply does a lexical | |
283 | * comparison on the item data. This does not result in | |
284 | * quite the same ordering as the "sequence of numbers" order, | |
285 | * but heck it's only used internally by the hash table anyway. | |
286 | */ | |
287 | PRIntn PR_CALLBACK | |
288 | SECITEM_HashCompare ( const void *k1, const void *k2) | |
289 | { | |
290 | const SECItem *i1 = (const SECItem *)k1; | |
291 | const SECItem *i2 = (const SECItem *)k2; | |
6b200bc3 | 292 | |
b1ab9ed8 A |
293 | return SECITEM_ItemsAreEqual(i1,i2); |
294 | } |