]>
git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_smime/lib/secitem.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 SECItem data structure.
39 #include <security_asn1/seccomon.h>
40 #include <security_asn1/secerr.h>
41 #include <Security/x509defs.h>
44 SECITEM_AllocItem(PRArenaPool
*arena
, SECItem
*item
, size_t len
)
46 SECItem
*result
= NULL
;
50 mark
= PORT_ArenaMark(arena
);
55 result
= PORT_ArenaZAlloc(arena
, sizeof(SECItem
));
57 result
= PORT_ZAlloc(sizeof(SECItem
));
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
&& mark
) {
83 PORT_ArenaRelease(arena
, mark
);
89 SECITEM_ReallocItem(PRArenaPool
*arena
, SECItem
*item
, unsigned int oldlen
,
92 PORT_Assert(item
!= NULL
);
94 /* XXX Set error. But to what? */
99 * If no old length, degenerate to just plain alloc.
102 PORT_Assert(item
->Data
== NULL
|| item
->Length
== 0);
104 /* Nothing to do. Weird, but not a failure. */
107 item
->Length
= newlen
;
109 item
->Data
= PORT_ArenaAlloc(arena
, newlen
);
111 item
->Data
= PORT_Alloc(newlen
);
115 item
->Data
= PORT_ArenaGrow(arena
, item
->Data
, oldlen
, newlen
);
117 item
->Data
= PORT_Realloc(item
->Data
, newlen
);
121 if (item
->Data
== NULL
) {
129 SECITEM_CompareItem(const SECItem
*a
, const SECItem
*b
)
134 m
= ( ( a
->Length
< b
->Length
) ? a
->Length
: b
->Length
);
136 rv
= (SECComparison
) PORT_Memcmp(a
->Data
, b
->Data
, m
);
140 if (a
->Length
< b
->Length
) {
143 if (a
->Length
== b
->Length
) {
146 return SECGreaterThan
;
150 SECITEM_ItemsAreEqual(const SECItem
*a
, const SECItem
*b
)
152 if (a
->Length
!= b
->Length
)
156 if (!a
->Data
|| !b
->Data
) {
157 /* avoid null pointer crash. */
158 return (Boolean
)(a
->Data
== b
->Data
);
160 return (Boolean
)!PORT_Memcmp(a
->Data
, b
->Data
, a
->Length
);
164 SECITEM_DupItem(const SECItem
*from
)
166 return SECITEM_ArenaDupItem(NULL
, from
);
170 SECITEM_ArenaDupItem(PRArenaPool
*arena
, const SECItem
*from
)
174 if ( from
== NULL
) {
178 if ( arena
!= NULL
) {
179 to
= (SECItem
*)PORT_ArenaAlloc(arena
, sizeof(SECItem
));
181 to
= (SECItem
*)PORT_Alloc(sizeof(SECItem
));
187 if ( arena
!= NULL
) {
188 to
->Data
= (unsigned char *)PORT_ArenaAlloc(arena
, from
->Length
);
190 to
->Data
= (unsigned char *)PORT_Alloc(from
->Length
);
192 if ( to
->Data
== NULL
) {
197 to
->Length
= from
->Length
;
198 // to->type = from->type;
200 PORT_Memcpy(to
->Data
, from
->Data
, to
->Length
);
207 SECITEM_CopyItem(PRArenaPool
*arena
, SECItem
*to
, const SECItem
*from
)
209 // to->type = from->type;
210 if (from
->Data
&& from
->Length
) {
212 to
->Data
= (unsigned char*) PORT_ArenaAlloc(arena
, from
->Length
);
214 to
->Data
= (unsigned char*) PORT_Alloc(from
->Length
);
220 PORT_Memcpy(to
->Data
, from
->Data
, from
->Length
);
221 to
->Length
= from
->Length
;
230 SECITEM_FreeItem(SECItem
*zap
, Boolean freeit
)
233 PORT_Free(zap
->Data
);
243 SECITEM_ZfreeItem(SECItem
*zap
, Boolean freeit
)
246 PORT_ZFree(zap
->Data
, zap
->Length
);
250 PORT_ZFree(zap
, sizeof(SECItem
));
256 /* these reroutines were taken from pkix oid.c, which is supposed to
257 * replace this file some day */
259 * This is the hash function. We simply XOR the encoded form with
260 * itself in sizeof(PLHashNumber)-byte chunks. Improving this
261 * routine is left as an excercise for the more mathematically
264 PLHashNumber PR_CALLBACK
265 SECITEM_Hash ( const void *key
)
267 const SECItem
*item
= (const SECItem
*)key
;
270 PRUint8
*data
= (PRUint8
*)item
->Data
;
272 PRUint8
*rvc
= (PRUint8
*)&rv
;
274 for( i
= 0; i
< item
->Length
; i
++ ) {
275 rvc
[ i
% sizeof(rv
) ] ^= *data
;
283 * This is the key-compare function. It simply does a lexical
284 * comparison on the item data. This does not result in
285 * quite the same ordering as the "sequence of numbers" order,
286 * but heck it's only used internally by the hash table anyway.
289 SECITEM_HashCompare ( const void *k1
, const void *k2
)
291 const SECItem
*i1
= (const SECItem
*)k1
;
292 const SECItem
*i2
= (const SECItem
*)k2
;
294 return SECITEM_ItemsAreEqual(i1
,i2
);