]>
git.saurik.com Git - apple/security.git/blob - Security/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>
43 SECITEM_AllocItem(PRArenaPool
*arena
, SECItem
*item
, unsigned int len
)
45 SECItem
*result
= NULL
;
49 mark
= PORT_ArenaMark(arena
);
54 result
= PORT_ArenaZAlloc(arena
, sizeof(SECItem
));
56 result
= PORT_ZAlloc(sizeof(SECItem
));
62 PORT_Assert(item
->Data
== NULL
);
69 result
->Data
= PORT_ArenaAlloc(arena
, len
);
71 result
->Data
= PORT_Alloc(len
);
76 PORT_ArenaUnmark(arena
, mark
);
81 if ( arena
!= NULL
) {
83 PORT_ArenaRelease(arena
, mark
);
91 SECITEM_FreeItem(result
, (item
== NULL
) ? PR_TRUE
: PR_FALSE
);
98 SECITEM_ReallocItem(PRArenaPool
*arena
, SECItem
*item
, unsigned int oldlen
,
101 PORT_Assert(item
!= NULL
);
103 /* XXX Set error. But to what? */
108 * If no old length, degenerate to just plain alloc.
111 PORT_Assert(item
->Data
== NULL
|| item
->Length
== 0);
113 /* Nothing to do. Weird, but not a failure. */
116 item
->Length
= newlen
;
118 item
->Data
= PORT_ArenaAlloc(arena
, newlen
);
120 item
->Data
= PORT_Alloc(newlen
);
124 item
->Data
= PORT_ArenaGrow(arena
, item
->Data
, oldlen
, newlen
);
126 item
->Data
= PORT_Realloc(item
->Data
, newlen
);
130 if (item
->Data
== NULL
) {
138 SECITEM_CompareItem(const SECItem
*a
, const SECItem
*b
)
143 m
= ( ( a
->Length
< b
->Length
) ? a
->Length
: b
->Length
);
145 rv
= (SECComparison
) PORT_Memcmp(a
->Data
, b
->Data
, m
);
149 if (a
->Length
< b
->Length
) {
152 if (a
->Length
== b
->Length
) {
155 return SECGreaterThan
;
159 SECITEM_ItemsAreEqual(const SECItem
*a
, const SECItem
*b
)
161 if (a
->Length
!= b
->Length
)
165 if (!a
->Data
|| !b
->Data
) {
166 /* avoid null pointer crash. */
167 return (Boolean
)(a
->Data
== b
->Data
);
169 return (Boolean
)!PORT_Memcmp(a
->Data
, b
->Data
, a
->Length
);
173 SECITEM_DupItem(const SECItem
*from
)
175 return SECITEM_ArenaDupItem(NULL
, from
);
179 SECITEM_ArenaDupItem(PRArenaPool
*arena
, const SECItem
*from
)
183 if ( from
== NULL
) {
187 if ( arena
!= NULL
) {
188 to
= (SECItem
*)PORT_ArenaAlloc(arena
, sizeof(SECItem
));
190 to
= (SECItem
*)PORT_Alloc(sizeof(SECItem
));
196 if ( arena
!= NULL
) {
197 to
->Data
= (unsigned char *)PORT_ArenaAlloc(arena
, from
->Length
);
199 to
->Data
= (unsigned char *)PORT_Alloc(from
->Length
);
201 if ( to
->Data
== NULL
) {
206 to
->Length
= from
->Length
;
207 // to->type = from->type;
209 PORT_Memcpy(to
->Data
, from
->Data
, to
->Length
);
216 SECITEM_CopyItem(PRArenaPool
*arena
, SECItem
*to
, const SECItem
*from
)
218 // to->type = from->type;
219 if (from
->Data
&& from
->Length
) {
221 to
->Data
= (unsigned char*) PORT_ArenaAlloc(arena
, from
->Length
);
223 to
->Data
= (unsigned char*) PORT_Alloc(from
->Length
);
229 PORT_Memcpy(to
->Data
, from
->Data
, from
->Length
);
230 to
->Length
= from
->Length
;
239 SECITEM_FreeItem(SECItem
*zap
, Boolean freeit
)
242 PORT_Free(zap
->Data
);
252 SECITEM_ZfreeItem(SECItem
*zap
, Boolean freeit
)
255 PORT_ZFree(zap
->Data
, zap
->Length
);
259 PORT_ZFree(zap
, sizeof(SECItem
));
265 /* these reroutines were taken from pkix oid.c, which is supposed to
266 * replace this file some day */
268 * This is the hash function. We simply XOR the encoded form with
269 * itself in sizeof(PLHashNumber)-byte chunks. Improving this
270 * routine is left as an excercise for the more mathematically
273 PLHashNumber PR_CALLBACK
274 SECITEM_Hash ( const void *key
)
276 const SECItem
*item
= (const SECItem
*)key
;
279 PRUint8
*data
= (PRUint8
*)item
->Data
;
281 PRUint8
*rvc
= (PRUint8
*)&rv
;
283 for( i
= 0; i
< item
->Length
; i
++ ) {
284 rvc
[ i
% sizeof(rv
) ] ^= *data
;
292 * This is the key-compare function. It simply does a lexical
293 * comparison on the item data. This does not result in
294 * quite the same ordering as the "sequence of numbers" order,
295 * but heck it's only used internally by the hash table anyway.
298 SECITEM_HashCompare ( const void *k1
, const void *k2
)
300 const SECItem
*i1
= (const SECItem
*)k1
;
301 const SECItem
*i2
= (const SECItem
*)k2
;
303 return SECITEM_ItemsAreEqual(i1
,i2
);