aboutsummaryrefslogtreecommitdiffstats
path: root/addons/ooh323c/src/errmgmt.c
blob: 2b8c41feb3dda1844492b1a27a8786ac0640c2ee (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
 * 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.
 *
 *****************************************************************************/

/* Error management functions */

#include <asterisk.h>
#include <asterisk/lock.h>
#include <stdlib.h>
#include "ooasn1.h"

/* Error status text */
static const char* g_status_text[] = {
    "Encode buffer overflow",
    "Unexpected end of buffer on decode",
    "Unexpected tag encountered: expected = %s, parsed = %s",
    "Invalid object identifier",
    "Invalid field length detected",
    "Enumerated value %s not in defined set",
    "Duplicate element in SET",
    "Missing required element in SET",
    "Element with tag %s not part of SET",
    "Max elements defined for SEQUENCE field exceeded",
    "Element with tag %s is an invalid option in choice",
    "No dynamic memory available",
    "Invalid string type",
    "Invalid hex string",
    "Invalid binary string",
    "Invalid real value",
    "Max items in sized BIT or OCTET STRING field exceeded",
    "Invalid value specification",
    "No definition found for referenced defined value",
    "No definition found for referenced defined type",
    "Invalid tag value",
    "Nesting level too deep",
    "Value constraint violation: field %s, value %s",
    "Value range error: lower bound is greater than upper",
    "Unexpected end of file detected",
    "Invalid UTF-8 character at index %d", 
    "List error: concurrent modification attempt while iterating", 
    "List error: illegal state for attempted operation",
    "Array index out of bounds",
    "Invalid parameter passed to function or method",
    "Invalid time string format",
    "Context is not initialized", 
    "ASN.1 value will not fit in target variable", 
    "Character is not within the defined character set", 
    "Invalid XML state for attempted operation", 
    "Error condition returned from XML parser:\n%s", 
    "SEQUENCE elements not in correct order",
    "Invalid index for table constraint identifier",
    "Invalid value for relational table constraint fixed type field", 
    "File not found", 
    "File read error",
    "File write error",
    "Invalid Base64 string",
    "Socket error",
    "XML interface library not found",
    "Invalid XML interface library"
} ;

#define ASN1_K_MAX_STAT (sizeof(g_status_text)/sizeof(char *))

/* Add an integer parameter to an error message */

int errAddIntParm (ASN1ErrInfo* pErrInfo, int errParm)
{
   char lbuf[16];
   sprintf (lbuf, "%d", errParm);
   return errAddStrParm (pErrInfo, lbuf);
}

/* Add a character string parameter to an error message */

int errAddStrParm (ASN1ErrInfo* pErrInfo, const char* errprm_p)
{
#if defined(_NO_THREADS) || !defined(_NO_MALLOC)
   if (pErrInfo->parmcnt < ASN_K_MAXERRP) {
      /* char* tmpstr = (char*) ASN1CRTMALLOC0 (strlen(errprm_p)+1); */
      char* tmpstr = (char*) malloc (strlen(errprm_p)+1);
      strcpy (tmpstr, errprm_p);
      pErrInfo->parms[pErrInfo->parmcnt] = tmpstr;
      pErrInfo->parmcnt++;
      return TRUE;
   }
   else
#endif
      return FALSE;
}

/* Add an unsigned integer parameter to an error message */

int errAddUIntParm (ASN1ErrInfo* pErrInfo, unsigned int errParm)
{
   char lbuf[16];
   sprintf (lbuf, "%u", errParm);
   return errAddStrParm (pErrInfo, lbuf);
}

/* Free error parameter memory */

void errFreeParms (ASN1ErrInfo* pErrInfo)
{
#if defined(_NO_THREADS) || !defined(_NO_MALLOC)
   int i;

   for (i = 0; i < pErrInfo->parmcnt; i++)
      /* ASN1CRTFREE0 ((char*)pErrInfo->parms[i]); */
      free ((char*)pErrInfo->parms[i]);
#endif

   pErrInfo->parmcnt = 0;
   pErrInfo->status = 0;
}

/* Reset error */

int errReset (ASN1ErrInfo* pErrInfo)
{
   errFreeParms (pErrInfo);
   pErrInfo->stkx = 0;
   return ASN_OK;
}

/* Format error message */

char* errFmtMsg (ASN1ErrInfo* pErrInfo, char* bufp)
{
   const char* tp;
   int  i, j, pcnt;

   if (pErrInfo->status < 0)
   {
      i = abs (pErrInfo->status + 1);

      if (i >= 0 && i < ASN1_K_MAX_STAT)
      {
         /* Substitute error parameters into error message */

         j  = pcnt = 0;
         tp = g_status_text[i];

         while (*tp) 
         {
            if (*tp == '%' && *(tp+1) == 's')
            {
               /* Plug in error parameter */

               if (pcnt < pErrInfo->parmcnt && pErrInfo->parms[pcnt])
               {
                  strcpy (&bufp[j], pErrInfo->parms[pcnt]);
                  j += strlen (pErrInfo->parms[pcnt++]);
               }
               else
                  bufp[j++] = '?';

               tp += 2;
            }
            else
               bufp[j++] = *tp++;
         }

         bufp[j] = '\0';        /* null terminate string */
      }
      else
         strcpy (bufp, "unrecognized completion status");
   }    
   else strcpy (bufp, "normal completion status");

   return (bufp);
}

/* Get error text in a dynamic memory buffer.  This allocates memory    */
/* using the 'memAlloc' function.  This memory is automatically freed */ 
/* at the time the 'memFree' function is called.                      */

char* errGetText (OOCTXT* pctxt)
{
   char lbuf[500];
   char* pBuf = (char*) ASN1MALLOC (pctxt,
      (sizeof(lbuf) + 100 * (2 + pctxt->errInfo.stkx)) * sizeof(char));

   sprintf (pBuf, "ASN.1 ERROR: Status %d\n", pctxt->errInfo.status);
   sprintf (lbuf, "%s\nStack trace:", errFmtMsg (&pctxt->errInfo, lbuf));
   strcat(pBuf, lbuf);

   while (pctxt->errInfo.stkx > 0) {
      pctxt->errInfo.stkx--;
      sprintf (lbuf, "  Module: %s, Line %d\n", 
               pctxt->errInfo.stack[pctxt->errInfo.stkx].module,
               pctxt->errInfo.stack[pctxt->errInfo.stkx].lineno);
      strcat(pBuf, lbuf);
   }

   errFreeParms (&pctxt->errInfo);

   return pBuf;
}

/* Print error information to the standard output */

void errPrint (ASN1ErrInfo* pErrInfo)
{
   char lbuf[200];
   printf ("ASN.1 ERROR: Status %d\n", pErrInfo->status);
   printf ("%s\n", errFmtMsg (pErrInfo, lbuf));
   printf ("Stack trace:");
   while (pErrInfo->stkx > 0) {
      pErrInfo->stkx--;
      printf ("  Module: %s, Line %d\n", 
              pErrInfo->stack[pErrInfo->stkx].module,
              pErrInfo->stack[pErrInfo->stkx].lineno);
   }
   errFreeParms (pErrInfo);
}

/* Copy error data from one error structure to another */

int errCopyData (ASN1ErrInfo* pSrcErrInfo, ASN1ErrInfo* pDestErrInfo)
{
   int i;
   pDestErrInfo->status = pSrcErrInfo->status;

   /* copy error parameters */

   for (i = 0; i < pSrcErrInfo->parmcnt; i++) {
      errAddStrParm (pDestErrInfo, pSrcErrInfo->parms[i]);
   }

   /* copy stack info */

   for (i = 0; i < pSrcErrInfo->stkx; i++) {
      if (pDestErrInfo->stkx < ASN_K_MAXERRSTK) {
         pDestErrInfo->stack[pDestErrInfo->stkx].module = 
            pSrcErrInfo->stack[i].module;
         pDestErrInfo->stack[pDestErrInfo->stkx++].lineno = 
            pSrcErrInfo->stack[i].lineno;
      }
   }

   return (pSrcErrInfo->status);
}


int errSetData (ASN1ErrInfo* pErrInfo, int status, 
                  const char* module, int lno) 
{ 
   if (pErrInfo->status == 0) {
      pErrInfo->status = status;
   }
   ooLogAsn1Error(status, module, lno);
   return status; 
}