#include <TargetConditionals.h>
#if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE))
#include "SecNssCoder.h"
#include <Security/cssmerr.h>
#include <security_utilities/utilities.h>
#include <security_asn1/secasn1.h>
#include <string.h>
#include <assert.h>
#ifdef NDEBUG
#define THROW_ENABLE 1
#else
#define THROW_ENABLE 0
#endif
#if THROW_ENABLE
#define THROW_ERROR Security::CssmError::throwMe(CSSMERR_CSSM_MEMORY_ERROR)
#else
#define THROW_ERROR
#endif
SecNssCoder::SecNssCoder(PRUint32 chunkSize )
: mPool(NULL)
{
mPool = PORT_NewArena(chunkSize);
if(mPool == NULL) {
THROW_ERROR;
}
}
SecNssCoder::~SecNssCoder()
{
if(mPool != NULL) {
PORT_FreeArena(mPool, PR_TRUE);
mPool = NULL;
}
}
PRErrorCode SecNssCoder::decode(
const void *src, size_t len,
const SecAsn1Template *templ,
void *dest)
{
SECStatus prtn;
assert(mPool != NULL);
prtn = SEC_ASN1Decode(mPool, dest, templ, (const char *)src, len);
if(prtn) {
return PR_GetError();
}
else {
return 0;
}
}
PRErrorCode SecNssCoder::encodeItem(
const void *src,
const SecAsn1Template *templ,
SECItem &dest)
{
assert(mPool != NULL);
dest.Data = NULL;
dest.Length = 0;
SECItem *rtnItem = SEC_ASN1EncodeItem(mPool, &dest, src, templ);
if(rtnItem == NULL) {
return PR_GetError();
}
else {
return 0;
}
}
void *SecNssCoder::malloc(size_t len)
{
assert(mPool != NULL);
void *rtn = PORT_ArenaAlloc(mPool, len);
if(rtn == NULL) {
THROW_ERROR;
}
return rtn;
}
void SecNssCoder::allocItem(
SECItem &item,
size_t len)
{
item.Data = (uint8 *)malloc(len);
item.Length = len;
}
void SecNssCoder::allocCopyItem(
const void *src,
size_t len,
SECItem &dest)
{
allocItem(dest, len);
memmove(dest.Data, src, len);
}
PRErrorCode SecNssEncodeItem(
const void *src,
const SecAsn1Template *templ,
Security::Allocator &alloc,
SECItem &dest)
{
unsigned long encoding_length = 0;
SECStatus rv;
dest.Data = NULL;
dest.Length = 0;
rv = SEC_ASN1Encode (src, templ,
sec_asn1e_encode_item_count, &encoding_length);
if (rv != SECSuccess) {
return PR_GetError();
}
dest.Data = (uint8 *)alloc.malloc(encoding_length);
dest.Length = 0;
rv = SEC_ASN1Encode (src, templ, sec_asn1e_encode_item_store, &dest);
if (rv != SECSuccess) {
return PR_GetError();
}
assert(encoding_length == dest.Length);
return 0;
}
PRErrorCode SecNssEncodeItemOdata(
const void *src,
const SecAsn1Template *templ,
CssmOwnedData &odata)
{
Allocator &alloc = odata.allocator;
SECItem sitem;
PRErrorCode prtn;
prtn = SecNssEncodeItem(src, templ, alloc, sitem);
if(prtn) {
return prtn;
}
odata.set(sitem.Data, sitem.Length);
return 0;
}
#endif