aboutsummaryrefslogtreecommitdiffstats
path: root/addons/ooh323c/src/context.c
blob: f2dc1119b89b9a66d24e97ed8e798a72b64762ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * Copyright (C) 1997-2005 by Objective Systems, Inc.
 *
 * This software is furnished under an open source license and may be 
 * used and copied only in accordance with the terms of this license. 
 * The text of the license may generally be found in the root 
 * directory of this installation in the COPYING file.  It 
 * can also be viewed online at the following URL:
 *
 *   http://www.obj-sys.com/open/license.html
 *
 * Any redistributions of this file including modified versions must 
 * maintain this copyright notice.
 *
 *****************************************************************************/
#include <asterisk.h>
#include <asterisk/lock.h>

#include "ooasn1.h"
#include <stdlib.h>
#include <pthread.h>

int initContext (OOCTXT* pctxt)
{
   memset (pctxt, 0, sizeof(OOCTXT));

   memHeapCreate (&pctxt->pTypeMemHeap);
   pctxt->pMsgMemHeap = pctxt->pTypeMemHeap;
   memHeapAddRef (&pctxt->pMsgMemHeap);


   ast_mutex_init(&pctxt->pLock);

   return ASN_OK;
}

int initContextBuffer 
(OOCTXT* pctxt, const ASN1OCTET* bufaddr, ASN1UINT bufsiz)
{
   if (bufaddr == 0) {
      /* dynamic buffer */
      if (bufsiz == 0) bufsiz = ASN_K_ENCBUFSIZ;
      pctxt->buffer.data = (ASN1OCTET*) 
         memHeapAlloc (&pctxt->pMsgMemHeap, bufsiz);
      if (!pctxt->buffer.data) return ASN_E_NOMEM;
      pctxt->buffer.size = bufsiz;
      pctxt->buffer.dynamic = TRUE;
   }
   else {
      /* static buffer */
      pctxt->buffer.data = (ASN1OCTET*) bufaddr;
      pctxt->buffer.size = bufsiz;
      pctxt->buffer.dynamic = FALSE;
   }

   pctxt->buffer.byteIndex = 0;
   pctxt->buffer.bitOffset = 8;

   return ASN_OK;
}

int initSubContext (OOCTXT* pctxt, OOCTXT* psrc) 
{
   /* ast_mutex_lock(&pctxt->pLock); */
   int stat = ASN_OK;
   ast_mutex_lock(&psrc->pLock);
   memset (pctxt, 0, sizeof(OOCTXT));
   pctxt->pTypeMemHeap = psrc->pTypeMemHeap;
   memHeapAddRef (&pctxt->pTypeMemHeap);
   pctxt->pMsgMemHeap = psrc->pMsgMemHeap;
   memHeapAddRef (&pctxt->pMsgMemHeap);
   pctxt->flags = psrc->flags;
   pctxt->buffer.dynamic = TRUE;
   pctxt->buffer.byteIndex = 0;
   pctxt->buffer.bitOffset = 8;

   ast_mutex_unlock(&psrc->pLock);
   /* ast_mutex_unlock(&pctxt->pLock); */
   return stat;
}

void freeContext (OOCTXT* pctxt)
{
   ASN1BOOL saveBuf;
   ast_mutex_lock(&pctxt->pLock);
   saveBuf = (pctxt->flags & ASN1SAVEBUF) != 0;
   
   if (pctxt->buffer.dynamic && pctxt->buffer.data) {
      if (saveBuf) {
         memHeapMarkSaved (&pctxt->pMsgMemHeap, pctxt->buffer.data, TRUE);
      }
      else {
         memHeapFreePtr (&pctxt->pMsgMemHeap, pctxt->buffer.data);
      }
   }

   errFreeParms (&pctxt->errInfo);

   memHeapRelease (&pctxt->pTypeMemHeap);
   memHeapRelease (&pctxt->pMsgMemHeap);

   ast_mutex_unlock(&pctxt->pLock);
   ast_mutex_destroy(&pctxt->pLock);
}

void copyContext (OOCTXT* pdest, OOCTXT* psrc)
{
   /* ast_mutex_lock(&pdest->pLock); ast_mutex_lock(&psrc->pLock); */
   memcpy (&pdest->buffer, &psrc->buffer, sizeof(ASN1BUFFER));
   pdest->flags = psrc->flags;
   /* ast_mutex_unlock(&psrc->pLock); ast_mutex_unlock(&pdest->pLock); */
}

void setCtxtFlag (OOCTXT* pctxt, ASN1USINT mask)
{
   ast_mutex_lock(&pctxt->pLock);
   pctxt->flags |= mask;
   ast_mutex_unlock(&pctxt->pLock);
}

void clearCtxtFlag (OOCTXT* pctxt, ASN1USINT mask)
{
   ast_mutex_lock(&pctxt->pLock);
   pctxt->flags &= ~mask;
   ast_mutex_unlock(&pctxt->pLock);
}

int setPERBufferUsingCtxt (OOCTXT* pTarget, OOCTXT* pSource)
{
   int stat;
   ast_mutex_lock(&pTarget->pLock); ast_mutex_lock(&pSource->pLock);
   stat = initContextBuffer 
      (pTarget, pSource->buffer.data, pSource->buffer.size);

   if (ASN_OK == stat) {
      pTarget->buffer.byteIndex = pSource->buffer.byteIndex;
      pTarget->buffer.bitOffset = pSource->buffer.bitOffset;
   }

   ast_mutex_unlock(&pSource->pLock); ast_mutex_unlock(&pTarget->pLock);
   return stat;
}

int setPERBuffer (OOCTXT* pctxt,
                  ASN1OCTET* bufaddr, ASN1UINT bufsiz, ASN1BOOL aligned)
{
   int stat;
   ast_mutex_lock(&pctxt->pLock);
   stat = initContextBuffer (pctxt, bufaddr, bufsiz);
   ast_mutex_unlock(&pctxt->pLock);
   if(stat != ASN_OK) return stat;

   
   return ASN_OK;
}

OOCTXT* newContext () 
{
   /* OOCTXT* pctxt = (OOCTXT*) ASN1CRTMALLOC0 (sizeof(OOCTXT)); */
   OOCTXT* pctxt = (OOCTXT*) malloc (sizeof(OOCTXT));
   if (pctxt) {
      if (initContext(pctxt) != ASN_OK) {
         /* ASN1CRTFREE0 (pctxt); */
	 free(pctxt);
         pctxt = 0;
      }
      pctxt->flags |= ASN1DYNCTXT;
   }
   return (pctxt);
}