]>
git.saurik.com Git - apple/security.git/blob - libsecurity_smime/lib/SecAsn1Item.c
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/
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.
12 * The Original Code is the Netscape security libraries.
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
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
35 * Support routines for SecAsn1Item data structure.
38 #include "SecAsn1Item.h"
39 #include <security_asn1/seccomon.h>
40 #include <security_asn1/secerr.h>
41 #include <security_asn1/secport.h>
44 SECITEM_AllocItem(PRArenaPool
*arena
, SecAsn1Item
*item
, size_t len
)
46 SecAsn1Item
*result
= NULL
;
50 mark
= PORT_ArenaMark(arena
);
55 result
= PORT_ArenaZAlloc(arena
, sizeof(SecAsn1Item
));
57 result
= PORT_ZAlloc(sizeof(SecAsn1Item
));
63 PORT_Assert(item
->Data
== NULL
);
70 result
->Data
= PORT_ArenaAlloc(arena
, len
);
72 result
->Data
= PORT_Alloc(len
);
77 PORT_ArenaUnmark(arena
, mark
);
82 if ( arena
!= NULL
) {
84 PORT_ArenaRelease(arena
, mark
);
92 SECITEM_FreeItem(result
, (item
== NULL
) ? PR_TRUE
: PR_FALSE
);
99 SECITEM_ReallocItem(PRArenaPool
*arena
, SecAsn1Item
*item
, size_t oldlen
,
102 PORT_Assert(item
!= NULL
);
104 /* XXX Set error. But to what? */
109 * If no old length, degenerate to just plain alloc.
112 PORT_Assert(item
->Data
== NULL
|| item
->Length
== 0);
114 /* Nothing to do. Weird, but not a failure. */
117 item
->Length
= newlen
;
119 item
->Data
= PORT_ArenaAlloc(arena
, newlen
);
121 item
->Data
= PORT_Alloc(newlen
);
125 item
->Data
= PORT_ArenaGrow(arena
, item
->Data
, oldlen
, newlen
);
127 item
->Data
= PORT_Realloc(item
->Data
, newlen
);
131 if (item
->Data
== NULL
) {
139 SECITEM_CompareItem(const SecAsn1Item
*a
, const SecAsn1Item
*b
)
144 m
= ( ( a
->Length
< b
->Length
) ? a
->Length
: b
->Length
);
146 rv
= (SECComparison
) PORT_Memcmp(a
->Data
, b
->Data
, m
);
150 if (a
->Length
< b
->Length
) {
153 if (a
->Length
== b
->Length
) {
156 return SECGreaterThan
;
160 SECITEM_ItemsAreEqual(const SecAsn1Item
*a
, const SecAsn1Item
*b
)
162 if (a
->Length
!= b
->Length
)
166 if (!a
->Data
|| !b
->Data
) {
167 /* avoid null pointer crash. */
168 return (Boolean
)(a
->Data
== b
->Data
);
170 return (Boolean
)!PORT_Memcmp(a
->Data
, b
->Data
, a
->Length
);
174 SECITEM_DupItem(const SecAsn1Item
*from
)
176 return SECITEM_ArenaDupItem(NULL
, from
);
180 SECITEM_ArenaDupItem(PRArenaPool
*arena
, const SecAsn1Item
*from
)
184 if ( from
== NULL
) {
188 if ( arena
!= NULL
) {
189 to
= (SecAsn1Item
*)PORT_ArenaAlloc(arena
, sizeof(SecAsn1Item
));
191 to
= (SecAsn1Item
*)PORT_Alloc(sizeof(SecAsn1Item
));
197 if ( arena
!= NULL
) {
198 to
->Data
= (unsigned char *)PORT_ArenaAlloc(arena
, from
->Length
);
200 to
->Data
= (unsigned char *)PORT_Alloc(from
->Length
);
202 if ( to
->Data
== NULL
) {
207 to
->Length
= from
->Length
;
208 // to->type = from->type;
210 PORT_Memcpy(to
->Data
, from
->Data
, to
->Length
);
217 SECITEM_CopyItem(PRArenaPool
*arena
, SecAsn1Item
*to
, const SecAsn1Item
*from
)
219 // to->type = from->type;
220 if (from
->Data
&& from
->Length
) {
222 to
->Data
= (unsigned char*) PORT_ArenaAlloc(arena
, from
->Length
);
224 to
->Data
= (unsigned char*) PORT_Alloc(from
->Length
);
230 PORT_Memcpy(to
->Data
, from
->Data
, from
->Length
);
231 to
->Length
= from
->Length
;
240 SECITEM_FreeItem(SecAsn1Item
*zap
, Boolean freeit
)
243 PORT_Free(zap
->Data
);
253 SECITEM_ZfreeItem(SecAsn1Item
*zap
, Boolean freeit
)
256 PORT_ZFree(zap
->Data
, zap
->Length
);
260 PORT_ZFree(zap
, sizeof(SecAsn1Item
));
266 /* these reroutines were taken from pkix oid.c, which is supposed to
267 * replace this file some day */
269 * This is the hash function. We simply XOR the encoded form with
270 * itself in sizeof(PLHashNumber)-byte chunks. Improving this
271 * routine is left as an excercise for the more mathematically
274 PLHashNumber PR_CALLBACK
275 SECITEM_Hash ( const void *key
)
277 const SecAsn1Item
*item
= (const SecAsn1Item
*)key
;
280 PRUint8
*data
= (PRUint8
*)item
->Data
;
281 PRUint8
*rvc
= (PRUint8
*)&rv
;
285 for( i
= 0; i
< item
->Length
; i
++ ) {
286 rvc
[ i
% sizeof(rv
) ] ^= *data
;
294 * This is the key-compare function. It simply does a lexical
295 * comparison on the item data. This does not result in
296 * quite the same ordering as the "sequence of numbers" order,
297 * but heck it's only used internally by the hash table anyway.
300 SECITEM_HashCompare ( const void *k1
, const void *k2
)
302 const SecAsn1Item
*i1
= (const SecAsn1Item
*)k1
;
303 const SecAsn1Item
*i2
= (const SecAsn1Item
*)k2
;
305 return SECITEM_ItemsAreEqual(i1
,i2
);