]>
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>
43 SECITEM_AllocItem(PRArenaPool
*arena
, SECItem
*item
, size_t 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
&& mark
) {
82 PORT_ArenaRelease(arena
, mark
);
88 SECITEM_ReallocItem(PRArenaPool
*arena
, SECItem
*item
, unsigned int oldlen
,
91 PORT_Assert(item
!= NULL
);
93 /* XXX Set error. But to what? */
98 * If no old length, degenerate to just plain alloc.
101 PORT_Assert(item
->Data
== NULL
|| item
->Length
== 0);
103 /* Nothing to do. Weird, but not a failure. */
106 item
->Length
= newlen
;
108 item
->Data
= PORT_ArenaAlloc(arena
, newlen
);
110 item
->Data
= PORT_Alloc(newlen
);
114 item
->Data
= PORT_ArenaGrow(arena
, item
->Data
, oldlen
, newlen
);
116 item
->Data
= PORT_Realloc(item
->Data
, newlen
);
120 if (item
->Data
== NULL
) {
128 SECITEM_CompareItem(const SECItem
*a
, const SECItem
*b
)
133 m
= ( ( a
->Length
< b
->Length
) ? a
->Length
: b
->Length
);
135 rv
= (SECComparison
) PORT_Memcmp(a
->Data
, b
->Data
, m
);
139 if (a
->Length
< b
->Length
) {
142 if (a
->Length
== b
->Length
) {
145 return SECGreaterThan
;
149 SECITEM_ItemsAreEqual(const SECItem
*a
, const SECItem
*b
)
151 if (a
->Length
!= b
->Length
)
155 if (!a
->Data
|| !b
->Data
) {
156 /* avoid null pointer crash. */
157 return (Boolean
)(a
->Data
== b
->Data
);
159 return (Boolean
)!PORT_Memcmp(a
->Data
, b
->Data
, a
->Length
);
163 SECITEM_DupItem(const SECItem
*from
)
165 return SECITEM_ArenaDupItem(NULL
, from
);
169 SECITEM_ArenaDupItem(PRArenaPool
*arena
, const SECItem
*from
)
173 if ( from
== NULL
) {
177 if ( arena
!= NULL
) {
178 to
= (SECItem
*)PORT_ArenaAlloc(arena
, sizeof(SECItem
));
180 to
= (SECItem
*)PORT_Alloc(sizeof(SECItem
));
186 if ( arena
!= NULL
) {
187 to
->Data
= (unsigned char *)PORT_ArenaAlloc(arena
, from
->Length
);
189 to
->Data
= (unsigned char *)PORT_Alloc(from
->Length
);
191 if ( to
->Data
== NULL
) {
196 to
->Length
= from
->Length
;
197 // to->type = from->type;
199 PORT_Memcpy(to
->Data
, from
->Data
, to
->Length
);
206 SECITEM_CopyItem(PRArenaPool
*arena
, SECItem
*to
, const SECItem
*from
)
208 // to->type = from->type;
209 if (from
->Data
&& from
->Length
) {
211 to
->Data
= (unsigned char*) PORT_ArenaAlloc(arena
, from
->Length
);
213 to
->Data
= (unsigned char*) PORT_Alloc(from
->Length
);
219 PORT_Memcpy(to
->Data
, from
->Data
, from
->Length
);
220 to
->Length
= from
->Length
;
229 SECITEM_FreeItem(SECItem
*zap
, Boolean freeit
)
232 PORT_Free(zap
->Data
);
242 SECITEM_ZfreeItem(SECItem
*zap
, Boolean freeit
)
245 PORT_ZFree(zap
->Data
, zap
->Length
);
249 PORT_ZFree(zap
, sizeof(SECItem
));
255 /* these reroutines were taken from pkix oid.c, which is supposed to
256 * replace this file some day */
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
263 PLHashNumber PR_CALLBACK
264 SECITEM_Hash ( const void *key
)
266 const SECItem
*item
= (const SECItem
*)key
;
269 PRUint8
*data
= (PRUint8
*)item
->Data
;
271 PRUint8
*rvc
= (PRUint8
*)&rv
;
273 for( i
= 0; i
< item
->Length
; i
++ ) {
274 rvc
[ i
% sizeof(rv
) ] ^= *data
;
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.
288 SECITEM_HashCompare ( const void *k1
, const void *k2
)
290 const SECItem
*i1
= (const SECItem
*)k1
;
291 const SECItem
*i2
= (const SECItem
*)k2
;
293 return SECITEM_ItemsAreEqual(i1
,i2
);