| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "asterisk.h" |
| 17 |
#include "asterisk/lock.h" |
| 18 |
#include "asterisk/utils.h" |
| 19 |
|
| 20 |
#include "ooasn1.h" |
| 21 |
#include <stdlib.h> |
| 22 |
#include <pthread.h> |
| 23 |
|
| 24 |
int initContext (OOCTXT* pctxt) |
| 25 |
{ |
| 26 |
memset (pctxt, 0, sizeof(OOCTXT)); |
| 27 |
|
| 28 |
memHeapCreate (&pctxt->pTypeMemHeap); |
| 29 |
pctxt->pMsgMemHeap = pctxt->pTypeMemHeap; |
| 30 |
memHeapAddRef (&pctxt->pMsgMemHeap); |
| 31 |
|
| 32 |
|
| 33 |
ast_mutex_init(&pctxt->pLock); |
| 34 |
|
| 35 |
return ASN_OK; |
| 36 |
} |
| 37 |
|
| 38 |
int initContextBuffer |
| 39 |
(OOCTXT* pctxt, const ASN1OCTET* bufaddr, ASN1UINT bufsiz) |
| 40 |
{ |
| 41 |
if (bufaddr == 0) { |
| 42 |
|
| 43 |
if (bufsiz == 0) bufsiz = ASN_K_ENCBUFSIZ; |
| 44 |
pctxt->buffer.data = (ASN1OCTET*) |
| 45 |
memHeapAlloc (&pctxt->pMsgMemHeap, bufsiz); |
| 46 |
if (!pctxt->buffer.data) return ASN_E_NOMEM; |
| 47 |
pctxt->buffer.size = bufsiz; |
| 48 |
pctxt->buffer.dynamic = TRUE; |
| 49 |
} |
| 50 |
else { |
| 51 |
|
| 52 |
pctxt->buffer.data = (ASN1OCTET*) bufaddr; |
| 53 |
pctxt->buffer.size = bufsiz; |
| 54 |
pctxt->buffer.dynamic = FALSE; |
| 55 |
} |
| 56 |
|
| 57 |
pctxt->buffer.byteIndex = 0; |
| 58 |
pctxt->buffer.bitOffset = 8; |
| 59 |
|
| 60 |
return ASN_OK; |
| 61 |
} |
| 62 |
|
| 63 |
int initSubContext (OOCTXT* pctxt, OOCTXT* psrc) |
| 64 |
{ |
| 65 |
|
| 66 |
int stat = ASN_OK; |
| 67 |
ast_mutex_lock(&psrc->pLock); |
| 68 |
memset (pctxt, 0, sizeof(OOCTXT)); |
| 69 |
pctxt->pTypeMemHeap = psrc->pTypeMemHeap; |
| 70 |
memHeapAddRef (&pctxt->pTypeMemHeap); |
| 71 |
pctxt->pMsgMemHeap = psrc->pMsgMemHeap; |
| 72 |
memHeapAddRef (&pctxt->pMsgMemHeap); |
| 73 |
pctxt->flags = psrc->flags; |
| 74 |
pctxt->buffer.dynamic = TRUE; |
| 75 |
pctxt->buffer.byteIndex = 0; |
| 76 |
pctxt->buffer.bitOffset = 8; |
| 77 |
|
| 78 |
ast_mutex_unlock(&psrc->pLock); |
| 79 |
|
| 80 |
return stat; |
| 81 |
} |
| 82 |
|
| 83 |
void freeContext (OOCTXT* pctxt) |
| 84 |
{ |
| 85 |
ASN1BOOL saveBuf; |
| 86 |
ast_mutex_lock(&pctxt->pLock); |
| 87 |
saveBuf = (pctxt->flags & ASN1SAVEBUF) != 0; |
| 88 |
|
| 89 |
if (pctxt->buffer.dynamic && pctxt->buffer.data) { |
| 90 |
if (saveBuf) { |
| 91 |
memHeapMarkSaved (&pctxt->pMsgMemHeap, pctxt->buffer.data, TRUE); |
| 92 |
} |
| 93 |
else { |
| 94 |
memHeapFreePtr (&pctxt->pMsgMemHeap, pctxt->buffer.data); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
errFreeParms (&pctxt->errInfo); |
| 99 |
|
| 100 |
memHeapRelease (&pctxt->pTypeMemHeap); |
| 101 |
memHeapRelease (&pctxt->pMsgMemHeap); |
| 102 |
|
| 103 |
ast_mutex_unlock(&pctxt->pLock); |
| 104 |
ast_mutex_destroy(&pctxt->pLock); |
| 105 |
} |
| 106 |
|
| 107 |
void copyContext (OOCTXT* pdest, OOCTXT* psrc) |
| 108 |
{ |
| 109 |
|
| 110 |
memcpy (&pdest->buffer, &psrc->buffer, sizeof(ASN1BUFFER)); |
| 111 |
pdest->flags = psrc->flags; |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
void setCtxtFlag (OOCTXT* pctxt, ASN1USINT mask) |
| 116 |
{ |
| 117 |
ast_mutex_lock(&pctxt->pLock); |
| 118 |
pctxt->flags |= mask; |
| 119 |
ast_mutex_unlock(&pctxt->pLock); |
| 120 |
} |
| 121 |
|
| 122 |
void clearCtxtFlag (OOCTXT* pctxt, ASN1USINT mask) |
| 123 |
{ |
| 124 |
ast_mutex_lock(&pctxt->pLock); |
| 125 |
pctxt->flags &= ~mask; |
| 126 |
ast_mutex_unlock(&pctxt->pLock); |
| 127 |
} |
| 128 |
|
| 129 |
int setPERBufferUsingCtxt (OOCTXT* pTarget, OOCTXT* pSource) |
| 130 |
{ |
| 131 |
int stat; |
| 132 |
ast_mutex_lock(&pTarget->pLock); ast_mutex_lock(&pSource->pLock); |
| 133 |
stat = initContextBuffer |
| 134 |
(pTarget, pSource->buffer.data, pSource->buffer.size); |
| 135 |
|
| 136 |
if (ASN_OK == stat) { |
| 137 |
pTarget->buffer.byteIndex = pSource->buffer.byteIndex; |
| 138 |
pTarget->buffer.bitOffset = pSource->buffer.bitOffset; |
| 139 |
} |
| 140 |
|
| 141 |
ast_mutex_unlock(&pSource->pLock); ast_mutex_unlock(&pTarget->pLock); |
| 142 |
return stat; |
| 143 |
} |
| 144 |
|
| 145 |
int setPERBuffer (OOCTXT* pctxt, |
| 146 |
ASN1OCTET* bufaddr, ASN1UINT bufsiz, ASN1BOOL aligned) |
| 147 |
{ |
| 148 |
int stat; |
| 149 |
ast_mutex_lock(&pctxt->pLock); |
| 150 |
stat = initContextBuffer (pctxt, bufaddr, bufsiz); |
| 151 |
ast_mutex_unlock(&pctxt->pLock); |
| 152 |
if(stat != ASN_OK) return stat; |
| 153 |
|
| 154 |
|
| 155 |
return ASN_OK; |
| 156 |
} |
| 157 |
|
| 158 |
OOCTXT* newContext () |
| 159 |
{ |
| 160 |
|
| 161 |
OOCTXT* pctxt = ast_malloc(sizeof(OOCTXT)); |
| 162 |
if (pctxt) { |
| 163 |
if (initContext(pctxt) != ASN_OK) { |
| 164 |
|
| 165 |
ast_free(pctxt); |
| 166 |
pctxt = 0; |
| 167 |
return (pctxt); |
| 168 |
} |
| 169 |
pctxt->flags |= ASN1DYNCTXT; |
| 170 |
} |
| 171 |
return (pctxt); |
| 172 |
} |