]>
git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_asn1/lib/SecNssCoder.h
2 * Copyright (c) 2003-2006,2008,2010-2012 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 * SecNssCoder.h: simple C++ wrapper for PLArenaPool and the
24 * high-level ANS1 encode/decode routines.
26 #ifndef _SEC_NSS_CODER_H_
27 #define _SEC_NSS_CODER_H_
29 #include <security_asn1/plarenas.h>
30 #include <security_asn1/prerror.h>
31 #include <Security/secasn1t.h>
32 #include <security_asn1/seccomon.h>
33 #include <security_utilities/alloc.h>
34 #include <security_cdsa_utilities/cssmdata.h>
37 * Default chunk size for new arena pool.
38 * FIXME: analyze & measure different defaults here. I'm pretty sure
39 * that only performance - not correct behavior - is affected by
40 * an arena pool's chunk size.
42 #define SNC_CHUNKSIZE_DEF 1024
48 PRUint32 chunkSize
= SNC_CHUNKSIZE_DEF
);
52 * BER decode an untyped item per the specified
53 * template array. The result is allocated
54 * by this object's PLArenaPool and is freed when
55 * this object is deleted.
57 * The dest pointer is a template-specific struct allocated
58 * by the caller and must be zeroed by the caller.
60 * This does not throw any exceptions; error status
61 * (obtained from PR_GetError() is returned.
64 const void *src
, // BER-encoded source
66 const SecAsn1Template
*templ
,
69 /* convenience routine, decode from an SECItem */
70 PRErrorCode
decodeItem(
71 const SECItem
&item
, // BER-encoded source
72 const SecAsn1Template
*templ
,
75 return decode(item
.Data
, item
.Length
, templ
, dest
);
80 * BER-encode. This object's arena pool retains a copy of
83 * The src pointer is a template-specific struct.
85 * This does not throw any exceptions; error status
86 * (obtained from PR_GetError() is returned.
88 PRErrorCode
encodeItem(
90 const SecAsn1Template
*templ
,
94 * Some alloc-related methods which come in handy when using
95 * this class. All memory is allocated using this object's
96 * arena pool. Caller never has to free it. Used for
97 * temp allocs of memory which only needs a scope which is the
98 * same as this object.
100 * These throw a CssmError in the highly unlikely event of
106 /* allocate space for num copies of specified type */
107 template <class T
> T
*mallocn(unsigned num
= 1)
108 { return reinterpret_cast<T
*>(malloc_T(sizeof(T
),num
)); }
110 /* malloc item.Data, set item.Length */
115 /* malloc and copy, various forms */
124 { allocCopyItem(src
.Data
, src
.Length
, dest
); }
129 { allocCopyItem(src
.data(), src
.length(), dest
); }
131 PLArenaPool
*pool() const { return mPool
;}
136 void *malloc_T(size_t unit_bytesize
,
141 * Stateless function to BER-encode directly into a Allocator's
142 * space. The only persistent allocated memory is allocated by
145 * The src pointer is a template-specific struct.
147 * This does not throw any exceptions; error status
148 * (obtained from PR_GetError() is returned.
150 PRErrorCode
SecNssEncodeItem(
152 const SecAsn1Template
*templ
,
157 * Same thing, using a CssmOwnedData.
159 PRErrorCode
SecNssEncodeItemOdata(
161 const SecAsn1Template
*templ
,
162 CssmOwnedData
&odata
);
164 #endif /* _SEC_NSS_CODER_H_ */