Debian Codegraph @

addons/ooh323c/src/printHandler.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2004-2005 by Objective Systems, Inc.
3 *
4 * This software is furnished under an open source license and may be
5 * used and copied only in accordance with the terms of this license.
6 * The text of the license may generally be found in the root
7 * directory of this installation in the COPYING file. It
8 * can also be viewed online at the following URL:
9 *
10 * http://www.obj-sys.com/open/license.html
11 *
12 * Any redistributions of this file including modified versions must
13 * maintain this copyright notice.
14 *
15 *****************************************************************************/
16 /* This is an implementation of a simple print handler. It outputs
17 the fields of an encoded PER message to stdout in a structured output
18 format..
19 */
20 #include "asterisk.h"
21 #include "asterisk/lock.h"
22 #include "asterisk/utils.h"
23
24 #include <stdlib.h>
25 /* #ifdef HAVE_MALLOC_H
26 #include <malloc.h>
27 #endif */
28 #include "printHandler.h"
29 #include "ootypes.h"
30 #include "rtctype.h"
31 #include "ootrace.h"
32
33 EventHandler printHandler;
34 AST_MUTEX_DEFINE_STATIC(printlock);
35
36 static const char* pVarName;
37 static int gIndentSpaces;
38
39 static const char* bitStrToString
40 (ASN1UINT numbits, const ASN1OCTET* data, char* buffer, size_t bufsiz);
41
42 static const char* octStrToString
43 (ASN1UINT numocts, const ASN1OCTET* data, char* buffer, size_t bufsiz);
44
45 void printCharStr32BitValue (ASN1UINT nchars, ASN132BITCHAR* data);
46 void ooPrintOIDValue (ASN1OBJID* pOID);
47 void printRealValue (double value);
48
49 void initializePrintHandler(EventHandler *printHandler, char * varname)
50 {
51 printHandler->startElement = &printStartElement;
52 printHandler->endElement = &printEndElement;
53 printHandler->boolValue = &printBoolValue;
54 printHandler->intValue = &printIntValue;
55 printHandler->uIntValue = &printuIntValue ;
56 printHandler->bitStrValue = &printBitStrValue;
57 printHandler->octStrValue = &printOctStrValue;
58 printHandler->charStrValue = &printCharStrValue;
59 printHandler->charStr16BitValue = &printCharStr16BitValue ;
60 printHandler->nullValue = &printNullValue;
61 printHandler->oidValue = &printOidValue;
62 printHandler->enumValue = &printEnumValue;
63 printHandler->openTypeValue = &printOpenTypeValue;
64 pVarName = varname;
65 ast_mutex_lock(&printlock);
66 OOTRACEDBGB2("%s = {\n", pVarName);
67 gIndentSpaces += 3;
68
69 }
70
71 void finishPrint()
72 {
73 OOTRACEDBGB1 ("}\n");
74 gIndentSpaces -= 3;
75 if (gIndentSpaces != 0) {
76 OOTRACEDBGB1 ("ERROR: unbalanced structure\n");
77 }
78 gIndentSpaces = 0;
79 ast_mutex_unlock(&printlock);
80 }
81
82 void indent ()
83 {
84 int i=0;
85 for (i = 0; i < gIndentSpaces; i++) OOTRACEDBGB1 (" ");
86 }
87
88 void printStartElement (const char* name, int index)
89 {
90 indent ();
91 OOTRACEDBGB1 (name);
92 if (index >= 0) OOTRACEDBGB2 ("[%d]", index);
93 OOTRACEDBGB1 (" = {\n");
94 gIndentSpaces += 3;
95 }
96
97 void printEndElement (const char* name, int index)
98 {
99 gIndentSpaces -= 3;
100 indent ();
101 OOTRACEDBGB1 ("}\n");
102 }
103
104 void printBoolValue (ASN1BOOL value)
105 {
106 const char* s = value ? "TRUE" : "FALSE";
107 indent ();
108 OOTRACEDBGB2 ("%s\n", s);
109 }
110
111 void printIntValue (ASN1INT value)
112 {
113 indent ();
114 OOTRACEDBGB2 ("%d\n", value);
115 }
116
117 void printuIntValue (ASN1UINT value)
118 {
119 indent ();
120 OOTRACEDBGB2 ("%u\n", value);
121 }
122
123 void printBitStrValue (ASN1UINT numbits, const ASN1OCTET* data)
124 {
125 #ifdef __MINGW32__
126 char s[numbits + 8];
127 indent ();
128 OOTRACEDBGB2("%s\n", bitStrToString (numbits, data, s, numbits+8));
129 #else
130 char* s = ast_malloc(numbits + 8);
131 indent ();
132 OOTRACEDBGB2("%s\n", bitStrToString (numbits, data, s, numbits+8));
133 ast_free(s);
134 #endif
135 }
136
137 void printOctStrValue (ASN1UINT numocts, const ASN1OCTET* data)
138 {
139 int bufsiz = (numocts * 2) + 8;
140 #ifdef __MINGW32__
141 char s[bufsiz];
142 indent ();
143 OOTRACEDBGB2 ("%s\n", octStrToString (numocts, data, s, bufsiz));
144 #else
145 char* s = ast_malloc(bufsiz);
146 indent ();
147 OOTRACEDBGB2 ("%s\n", octStrToString (numocts, data, s, bufsiz));
148 ast_free(s);
149 #endif
150 }
151
152 void printCharStrValue (const char* value)
153 {
154 indent ();
155 OOTRACEDBGB2 ("\"%s\"\n", value);
156 }
157
158 void printCharStr16BitValue (ASN1UINT nchars, ASN116BITCHAR* data)
159 {
160 ASN1UINT ui;
161 indent ();
162
163 for (ui = 0; ui < nchars; ui++) {
164 if (data[ui] >= 32 && data[ui] <= 127)
165 OOTRACEDBGB2 ("%c", (char)data[ui]);
166 else
167 OOTRACEDBGB1 ("?");
168 }
169
170 OOTRACEDBGB1 ("\n");
171 }
172
173 void printCharStr32BitValue (ASN1UINT nchars, ASN132BITCHAR* data)
174 {
175 ASN1UINT ui;
176 indent ();
177
178 for ( ui = 0; ui < nchars; ui++) {
179 if (data[ui] >= 32 && data[ui] <= 127)
180 OOTRACEDBGB2 ("%c", (char)data[ui]);
181 else
182 OOTRACEDBGB2 ("\\%d", data[ui]);
183 }
184
185 OOTRACEDBGB1 ("\n");
186 }
187
188 void printNullValue ()
189 {
190 indent ();
191 OOTRACEDBGB1 ("NULL\n");
192 }
193
194 void ooPrintOIDValue (ASN1OBJID* pOID)
195 {
196 ASN1UINT ui;
197 OOTRACEDBGB1 ("{ \n");
198 for (ui = 0; ui < pOID->numids; ui++) {
199 OOTRACEDBGB2 ("%d ", pOID->subid[ui]);
200 }
201 OOTRACEDBGB1 ("}\n");
202 }
203
204 void printOidValue (ASN1UINT numSubIds, ASN1UINT* pSubIds)
205 {
206 ASN1UINT ui;
207 ASN1OBJID oid;
208 oid.numids = numSubIds;
209
210 for ( ui = 0; ui < numSubIds; ui++)
211 oid.subid[ui] = pSubIds[ui];
212
213 indent ();
214 ooPrintOIDValue (&oid);
215 }
216
217 void printRealValue (double value)
218 {
219 indent ();
220 OOTRACEDBGB2 ("%f\n", value);
221 }
222
223 void printEnumValue (ASN1UINT value)
224 {
225 indent ();
226 OOTRACEDBGB2 ("%u\n", value);
227 }
228
229 void printOpenTypeValue (ASN1UINT numocts, const ASN1OCTET* data)
230 {
231 indent ();
232 OOTRACEDBGB1 ("< encoded data >\n");
233 }
234
235 static const char* bitStrToString
236 (ASN1UINT numbits, const ASN1OCTET* data, char* buffer, size_t bufsiz)
237 {
238 size_t i;
239 unsigned char mask = 0x80;
240
241 if (bufsiz > 0) {
242 buffer[0] = '\'';
243 for (i = 0; i < numbits; i++) {
244 if (i < bufsiz - 1) {
245 buffer[i+1] = (char) (((data[i/8] & mask) != 0) ? '1' : '0');
246 mask >>= 1;
247 if (0 == mask) mask = 0x80;
248 }
249 else break;
250 }
251 i++;
252 if (i < bufsiz - 1) buffer[i++] = '\'';
253 if (i < bufsiz - 1) buffer[i++] = 'B';
254 if (i < bufsiz - 1) buffer[i] = '\0';
255 else buffer[bufsiz - 1] = '\0';
256 }
257
258 return buffer;
259 }
260
261 static const char* octStrToString
262 (ASN1UINT numocts, const ASN1OCTET* data, char* buffer, size_t bufsiz)
263 {
264 size_t i;
265 char lbuf[4];
266
267 if (bufsiz > 0) {
268 buffer[0] = '\'';
269 if (bufsiz > 1) buffer[1] = '\0';
270 for (i = 0; i < numocts; i++) {
271 if (i < bufsiz - 1) {
272 sprintf (lbuf, "%02hhx", (unsigned char)data[i]);
273 strcat (&buffer[(i*2)+1], lbuf);
274 }
275 else break;
276 }
277 i = i*2 + 1;
278 if (i < bufsiz - 1) buffer[i++] = '\'';
279 if (i < bufsiz - 1) buffer[i++] = 'H';
280 if (i < bufsiz - 1) buffer[i] = '\0';
281 else buffer[bufsiz - 1] = '\0';
282 }
283
284 return buffer;
285 }