]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_smime/lib/secitem.c
Security-58286.251.4.tar.gz
[apple/security.git] / OSX / libsecurity_smime / lib / secitem.c
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/
6 *
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.
11 *
12 * The Original Code is the Netscape security libraries.
13 *
14 * The Initial Developer of the Original Code is Netscape
15 * Communications Corporation. Portions created by Netscape are
16 * Copyright (C) 1994-2000 Netscape Communications Corporation. All
17 * Rights Reserved.
18 *
19 * Contributor(s):
20 *
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
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
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 *
43 SECITEM_AllocItem(PRArenaPool *arena, SECItem *item, size_t len)
44 {
45 SECItem *result = NULL;
46 void *mark = NULL;
47
48 if (arena != NULL) {
49 mark = PORT_ArenaMark(arena);
50 }
51
52 if (item == NULL) {
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 }
61 } else {
62 PORT_Assert(item->Data == NULL);
63 result = item;
64 }
65
66 result->Length = len;
67 if (len) {
68 if (arena != NULL) {
69 result->Data = PORT_ArenaAlloc(arena, len);
70 } else {
71 result->Data = PORT_Alloc(len);
72 }
73 }
74
75 if (mark) {
76 PORT_ArenaUnmark(arena, mark);
77 }
78 return(result);
79
80 loser:
81 if (arena != NULL && mark) {
82 PORT_ArenaRelease(arena, mark);
83 }
84 return(NULL);
85 }
86
87 SECStatus
88 SECITEM_ReallocItem(PRArenaPool *arena, SECItem *item, unsigned int oldlen,
89 unsigned int newlen)
90 {
91 PORT_Assert(item != NULL);
92 if (item == NULL) {
93 /* XXX Set error. But to what? */
94 return SECFailure;
95 }
96
97 /*
98 * If no old length, degenerate to just plain alloc.
99 */
100 if (oldlen == 0) {
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 }
112 } else {
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 }
118 }
119
120 if (item->Data == NULL) {
121 return SECFailure;
122 }
123
124 return SECSuccess;
125 }
126
127 SECComparison
128 SECITEM_CompareItem(const SECItem *a, const SECItem *b)
129 {
130 CSSM_SIZE m;
131 SECComparison rv;
132
133 m = ( ( a->Length < b->Length ) ? a->Length : b->Length );
134
135 rv = (SECComparison) PORT_Memcmp(a->Data, b->Data, m);
136 if (rv) {
137 return rv;
138 }
139 if (a->Length < b->Length) {
140 return SECLessThan;
141 }
142 if (a->Length == b->Length) {
143 return SECEqual;
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)
154 return PR_TRUE;
155 if (!a->Data || !b->Data) {
156 /* avoid null pointer crash. */
157 return (Boolean)(a->Data == b->Data);
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 ) {
174 return(NULL);
175 }
176
177 if ( arena != NULL ) {
178 to = (SECItem *)PORT_ArenaAlloc(arena, sizeof(SECItem));
179 } else {
180 to = (SECItem *)PORT_Alloc(sizeof(SECItem));
181 }
182 if ( to == NULL ) {
183 return(NULL);
184 }
185
186 if ( arena != NULL ) {
187 to->Data = (unsigned char *)PORT_ArenaAlloc(arena, from->Length);
188 } else {
189 to->Data = (unsigned char *)PORT_Alloc(from->Length);
190 }
191 if ( to->Data == NULL ) {
192 PORT_Free(to);
193 return(NULL);
194 }
195
196 to->Length = from->Length;
197 // to->type = from->type;
198 if ( to->Length ) {
199 PORT_Memcpy(to->Data, from->Data, to->Length);
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) {
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;
221 } else {
222 to->Data = 0;
223 to->Length = 0;
224 }
225 return SECSuccess;
226 }
227
228 void
229 SECITEM_FreeItem(SECItem *zap, Boolean freeit)
230 {
231 if (zap) {
232 PORT_Free(zap->Data);
233 zap->Data = 0;
234 zap->Length = 0;
235 if (freeit) {
236 PORT_Free(zap);
237 }
238 }
239 }
240
241 void
242 SECITEM_ZfreeItem(SECItem *zap, Boolean freeit)
243 {
244 if (zap) {
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 }
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;
272
273 for( i = 0; i < item->Length; i++ ) {
274 rvc[ i % sizeof(rv) ] ^= *data;
275 data++;
276 }
277
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;
292
293 return SECITEM_ItemsAreEqual(i1,i2);
294 }