| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include "asterisk.h" |
| 20 |
#include "asterisk/lock.h" |
| 21 |
#include "asterisk/utils.h" |
| 22 |
#include <stdlib.h> |
| 23 |
#include "ooasn1.h" |
| 24 |
|
| 25 |
|
| 26 |
static const char* g_status_text[] = { |
| 27 |
"Encode buffer overflow", |
| 28 |
"Unexpected end of buffer on decode", |
| 29 |
"Unexpected tag encountered: expected = %s, parsed = %s", |
| 30 |
"Invalid object identifier", |
| 31 |
"Invalid field length detected", |
| 32 |
"Enumerated value %s not in defined set", |
| 33 |
"Duplicate element in SET", |
| 34 |
"Missing required element in SET", |
| 35 |
"Element with tag %s not part of SET", |
| 36 |
"Max elements defined for SEQUENCE field exceeded", |
| 37 |
"Element with tag %s is an invalid option in choice", |
| 38 |
"No dynamic memory available", |
| 39 |
"Invalid string type", |
| 40 |
"Invalid hex string", |
| 41 |
"Invalid binary string", |
| 42 |
"Invalid real value", |
| 43 |
"Max items in sized BIT or OCTET STRING field exceeded", |
| 44 |
"Invalid value specification", |
| 45 |
"No definition found for referenced defined value", |
| 46 |
"No definition found for referenced defined type", |
| 47 |
"Invalid tag value", |
| 48 |
"Nesting level too deep", |
| 49 |
"Value constraint violation: field %s, value %s", |
| 50 |
"Value range error: lower bound is greater than upper", |
| 51 |
"Unexpected end of file detected", |
| 52 |
"Invalid UTF-8 character at index %d", |
| 53 |
"List error: concurrent modification attempt while iterating", |
| 54 |
"List error: illegal state for attempted operation", |
| 55 |
"Array index out of bounds", |
| 56 |
"Invalid parameter passed to function or method", |
| 57 |
"Invalid time string format", |
| 58 |
"Context is not initialized", |
| 59 |
"ASN.1 value will not fit in target variable", |
| 60 |
"Character is not within the defined character set", |
| 61 |
"Invalid XML state for attempted operation", |
| 62 |
"Error condition returned from XML parser:\n%s", |
| 63 |
"SEQUENCE elements not in correct order", |
| 64 |
"Invalid index for table constraint identifier", |
| 65 |
"Invalid value for relational table constraint fixed type field", |
| 66 |
"File not found", |
| 67 |
"File read error", |
| 68 |
"File write error", |
| 69 |
"Invalid Base64 string", |
| 70 |
"Socket error", |
| 71 |
"XML interface library not found", |
| 72 |
"Invalid XML interface library" |
| 73 |
} ; |
| 74 |
|
| 75 |
#define ASN1_K_MAX_STAT (sizeof(g_status_text)/sizeof(char *)) |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
int errAddIntParm (ASN1ErrInfo* pErrInfo, int errParm) |
| 80 |
{ |
| 81 |
char lbuf[16]; |
| 82 |
sprintf (lbuf, "%d", errParm); |
| 83 |
return errAddStrParm (pErrInfo, lbuf); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
int errAddStrParm (ASN1ErrInfo* pErrInfo, const char* errprm_p) |
| 89 |
{ |
| 90 |
#if defined(_NO_THREADS) || !defined(_NO_MALLOC) |
| 91 |
if (pErrInfo->parmcnt < ASN_K_MAXERRP) { |
| 92 |
|
| 93 |
char* tmpstr = ast_malloc(strlen(errprm_p) + 1); |
| 94 |
strcpy (tmpstr, errprm_p); |
| 95 |
pErrInfo->parms[pErrInfo->parmcnt] = tmpstr; |
| 96 |
pErrInfo->parmcnt++; |
| 97 |
return TRUE; |
| 98 |
} |
| 99 |
else |
| 100 |
#endif |
| 101 |
return FALSE; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
int errAddUIntParm (ASN1ErrInfo* pErrInfo, unsigned int errParm) |
| 107 |
{ |
| 108 |
char lbuf[16]; |
| 109 |
sprintf (lbuf, "%u", errParm); |
| 110 |
return errAddStrParm (pErrInfo, lbuf); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
void errFreeParms (ASN1ErrInfo* pErrInfo) |
| 116 |
{ |
| 117 |
#if defined(_NO_THREADS) || !defined(_NO_MALLOC) |
| 118 |
int i; |
| 119 |
|
| 120 |
for (i = 0; i < pErrInfo->parmcnt; i++) |
| 121 |
|
| 122 |
ast_free((char*)pErrInfo->parms[i]); |
| 123 |
#endif |
| 124 |
|
| 125 |
pErrInfo->parmcnt = 0; |
| 126 |
pErrInfo->status = 0; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
int errReset (ASN1ErrInfo* pErrInfo) |
| 132 |
{ |
| 133 |
errFreeParms (pErrInfo); |
| 134 |
pErrInfo->stkx = 0; |
| 135 |
return ASN_OK; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
char* errFmtMsg (ASN1ErrInfo* pErrInfo, char* bufp) |
| 141 |
{ |
| 142 |
const char* tp; |
| 143 |
int i, j, pcnt; |
| 144 |
|
| 145 |
if (pErrInfo->status < 0) |
| 146 |
{ |
| 147 |
i = abs (pErrInfo->status + 1); |
| 148 |
|
| 149 |
if (i >= 0 && i < ASN1_K_MAX_STAT) |
| 150 |
{ |
| 151 |
|
| 152 |
|
| 153 |
j = pcnt = 0; |
| 154 |
tp = g_status_text[i]; |
| 155 |
|
| 156 |
while (*tp) |
| 157 |
{ |
| 158 |
if (*tp == '%' && *(tp+1) == 's') |
| 159 |
{ |
| 160 |
|
| 161 |
|
| 162 |
if (pcnt < pErrInfo->parmcnt && pErrInfo->parms[pcnt]) |
| 163 |
{ |
| 164 |
strcpy (&bufp[j], pErrInfo->parms[pcnt]); |
| 165 |
j += strlen (pErrInfo->parms[pcnt++]); |
| 166 |
} |
| 167 |
else |
| 168 |
bufp[j++] = '?'; |
| 169 |
|
| 170 |
tp += 2; |
| 171 |
} |
| 172 |
else |
| 173 |
bufp[j++] = *tp++; |
| 174 |
} |
| 175 |
|
| 176 |
bufp[j] = '\0'; |
| 177 |
} |
| 178 |
else |
| 179 |
strcpy (bufp, "unrecognized completion status"); |
| 180 |
} |
| 181 |
else strcpy (bufp, "normal completion status"); |
| 182 |
|
| 183 |
return (bufp); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
char* errGetText (OOCTXT* pctxt) |
| 191 |
{ |
| 192 |
char lbuf[500]; |
| 193 |
char* pBuf = (char*) ASN1MALLOC (pctxt, |
| 194 |
(sizeof(lbuf) + 100 * (2 + pctxt->errInfo.stkx)) * sizeof(char)); |
| 195 |
|
| 196 |
sprintf (pBuf, "ASN.1 ERROR: Status %d\n", pctxt->errInfo.status); |
| 197 |
sprintf (lbuf, "%s\nStack trace:", errFmtMsg (&pctxt->errInfo, lbuf)); |
| 198 |
strcat(pBuf, lbuf); |
| 199 |
|
| 200 |
while (pctxt->errInfo.stkx > 0) { |
| 201 |
pctxt->errInfo.stkx--; |
| 202 |
sprintf (lbuf, " Module: %s, Line %d\n", |
| 203 |
pctxt->errInfo.stack[pctxt->errInfo.stkx].module, |
| 204 |
pctxt->errInfo.stack[pctxt->errInfo.stkx].lineno); |
| 205 |
strcat(pBuf, lbuf); |
| 206 |
} |
| 207 |
|
| 208 |
errFreeParms (&pctxt->errInfo); |
| 209 |
|
| 210 |
return pBuf; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
void errPrint (ASN1ErrInfo* pErrInfo) |
| 216 |
{ |
| 217 |
char lbuf[200]; |
| 218 |
printf ("ASN.1 ERROR: Status %d\n", pErrInfo->status); |
| 219 |
printf ("%s\n", errFmtMsg (pErrInfo, lbuf)); |
| 220 |
printf ("Stack trace:"); |
| 221 |
while (pErrInfo->stkx > 0) { |
| 222 |
pErrInfo->stkx--; |
| 223 |
printf (" Module: %s, Line %d\n", |
| 224 |
pErrInfo->stack[pErrInfo->stkx].module, |
| 225 |
pErrInfo->stack[pErrInfo->stkx].lineno); |
| 226 |
} |
| 227 |
errFreeParms (pErrInfo); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
int errCopyData (ASN1ErrInfo* pSrcErrInfo, ASN1ErrInfo* pDestErrInfo) |
| 233 |
{ |
| 234 |
int i; |
| 235 |
pDestErrInfo->status = pSrcErrInfo->status; |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
for (i = 0; i < pSrcErrInfo->parmcnt; i++) { |
| 240 |
errAddStrParm (pDestErrInfo, pSrcErrInfo->parms[i]); |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
for (i = 0; i < pSrcErrInfo->stkx; i++) { |
| 246 |
if (pDestErrInfo->stkx < ASN_K_MAXERRSTK) { |
| 247 |
pDestErrInfo->stack[pDestErrInfo->stkx].module = |
| 248 |
pSrcErrInfo->stack[i].module; |
| 249 |
pDestErrInfo->stack[pDestErrInfo->stkx++].lineno = |
| 250 |
pSrcErrInfo->stack[i].lineno; |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
return (pSrcErrInfo->status); |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
int errSetData (ASN1ErrInfo* pErrInfo, int status, |
| 259 |
const char* module, int lno) |
| 260 |
{ |
| 261 |
if (pErrInfo->status == 0) { |
| 262 |
pErrInfo->status = status; |
| 263 |
} |
| 264 |
ooLogAsn1Error(status, module, lno); |
| 265 |
return status; |
| 266 |
} |