aboutsummaryrefslogtreecommitdiffstats
path: root/addons/ooh323c/src/printHandler.c
blob: abb4d4f2de76cfebde8bf0ddbb75d33a699e3955 (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
 * Copyright (C) 2004-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.
 *
 *****************************************************************************/
/* This is an implementation of a simple print handler.  It outputs 
   the fields of an encoded PER message to stdout in a structured output 
   format..
*/
#include "asterisk.h"
#include "asterisk/lock.h"

#include <stdlib.h>
/* #ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif */
#include "printHandler.h"
#include "ootypes.h"
#include "rtctype.h"
#include "ootrace.h"

EventHandler printHandler;

static const char* pVarName;
static int gIndentSpaces;

static const char* bitStrToString 
(ASN1UINT numbits, const ASN1OCTET* data, char* buffer, size_t bufsiz);

static const char* octStrToString 
(ASN1UINT numocts, const ASN1OCTET* data, char* buffer, size_t bufsiz);

void printCharStr32BitValue (ASN1UINT nchars, ASN132BITCHAR* data);
void ooPrintOIDValue (ASN1OBJID* pOID);
void printRealValue (double value);

void initializePrintHandler(EventHandler *printHandler, char * varname)
{
   printHandler->startElement = &printStartElement;
   printHandler->endElement = &printEndElement;
   printHandler->boolValue = &printBoolValue;
   printHandler->intValue = &printIntValue;
   printHandler->uIntValue = &printuIntValue ;
   printHandler->bitStrValue = &printBitStrValue;
   printHandler->octStrValue = &printOctStrValue;
   printHandler->charStrValue = &printCharStrValue;
   printHandler->charStr16BitValue = &printCharStr16BitValue ;
   printHandler->nullValue = &printNullValue;
   printHandler->oidValue = &printOidValue;
   printHandler->enumValue = &printEnumValue;
   printHandler->openTypeValue = &printOpenTypeValue;
   pVarName = varname;
   OOTRACEDBGB2("%s = {\n", pVarName);
   gIndentSpaces += 3;

}

void finishPrint()
{
   OOTRACEDBGB1 ("}\n");
   gIndentSpaces -= 3;
   if (gIndentSpaces != 0) {
      OOTRACEDBGB1 ("ERROR: unbalanced structure\n");
   }
}

void indent ()
{
   int i=0;
   for (i = 0; i < gIndentSpaces; i++) OOTRACEDBGB1 (" ");
}

void printStartElement (const char* name, int index)
{
   indent ();
   OOTRACEDBGB1 (name);
   if (index >= 0) OOTRACEDBGB2 ("[%d]", index);
   OOTRACEDBGB1 (" = {\n");
   gIndentSpaces += 3;
}

void printEndElement (const char* name, int index)
{
   gIndentSpaces -= 3;
   indent ();
   OOTRACEDBGB1 ("}\n");
}

void printBoolValue (ASN1BOOL value)
{
   const char* s = value ? "TRUE" : "FALSE";
   indent ();
   OOTRACEDBGB2 ("%s\n", s);
}

void printIntValue (ASN1INT value)
{
   indent ();
   OOTRACEDBGB2 ("%d\n", value);
}

void printuIntValue (ASN1UINT value)
{
   indent ();
   OOTRACEDBGB2 ("%u\n", value);
}

void printBitStrValue (ASN1UINT numbits, const ASN1OCTET* data)
{
#ifdef __MINGW32__
   char s[numbits + 8];
   indent ();
   OOTRACEDBGB2("%s\n", bitStrToString (numbits, data, s, numbits+8));
#else
   char* s = (char*)malloc(numbits + 8);
   indent ();
   OOTRACEDBGB2("%s\n", bitStrToString (numbits, data, s, numbits+8));
   free(s);
#endif
}

void printOctStrValue (ASN1UINT numocts, const ASN1OCTET* data)
{
   int bufsiz = (numocts * 2) + 8;
#ifdef __MINGW32__
   char s[bufsiz];
   indent ();
   OOTRACEDBGB2 ("%s\n", octStrToString (numocts, data, s, bufsiz));
#else
   char* s = (char*)malloc(bufsiz);
   indent ();
   OOTRACEDBGB2 ("%s\n", octStrToString (numocts, data, s, bufsiz));
   free(s);
#endif
}

void printCharStrValue (const char* value)
{
   indent ();
   OOTRACEDBGB2 ("\"%s\"\n", value);
}

void printCharStr16BitValue (ASN1UINT nchars, ASN116BITCHAR* data)
{
   ASN1UINT ui;
   indent ();

   for (ui = 0; ui < nchars; ui++) {
      if (data[ui] >= 32 && data[ui] <= 127)
         OOTRACEDBGB2 ("%c", (char)data[ui]);
      else
         OOTRACEDBGB1 ("?");
   }

   OOTRACEDBGB1 ("\n");
}

void printCharStr32BitValue (ASN1UINT nchars, ASN132BITCHAR* data)
{
   ASN1UINT ui;
   indent ();

   for ( ui = 0; ui < nchars; ui++) {
      if (data[ui] >= 32 && data[ui] <= 127)
         OOTRACEDBGB2 ("%c", (char)data[ui]);
      else
         OOTRACEDBGB2 ("\\%d", data[ui]);
   }

   OOTRACEDBGB1 ("\n");
}

void printNullValue ()
{
   indent ();
   OOTRACEDBGB1 ("NULL\n");
}

void ooPrintOIDValue (ASN1OBJID* pOID)
{
   ASN1UINT ui;
   OOTRACEDBGB1 ("{ \n");
   for (ui = 0; ui < pOID->numids; ui++) {
      OOTRACEDBGB2 ("%d ", pOID->subid[ui]);
   }
   OOTRACEDBGB1 ("}\n");
}

void printOidValue (ASN1UINT numSubIds, ASN1UINT* pSubIds)
{
   ASN1UINT ui;
   ASN1OBJID oid;
   oid.numids = numSubIds;

   for ( ui = 0; ui < numSubIds; ui++)
      oid.subid[ui] = pSubIds[ui];

   indent ();
   ooPrintOIDValue (&oid);
}

void printRealValue (double value)
{
   indent ();
   OOTRACEDBGB2 ("%f\n", value);
}

void printEnumValue (ASN1UINT value)
{
   indent ();
   OOTRACEDBGB2 ("%u\n", value);
}

void printOpenTypeValue (ASN1UINT numocts, const ASN1OCTET* data)
{
   indent ();
   OOTRACEDBGB1 ("< encoded data >\n");
}

static const char* bitStrToString 
(ASN1UINT numbits, const ASN1OCTET* data, char* buffer, size_t bufsiz)
{
   size_t i;
   unsigned char mask = 0x80;

   if (bufsiz > 0) {
      buffer[0] = '\'';
      for (i = 0; i < numbits; i++) {
         if (i < bufsiz - 1) {
            buffer[i+1] = (char) (((data[i/8] & mask) != 0) ? '1' : '0');
            mask >>= 1;
            if (0 == mask) mask = 0x80;
         }
         else break;
      }
     i++;
      if (i < bufsiz - 1) buffer[i++] = '\'';
      if (i < bufsiz - 1) buffer[i++] = 'B';
      if (i < bufsiz - 1) buffer[i] = '\0';
      else buffer[bufsiz - 1] = '\0';
   }

   return buffer;
}

static const char* octStrToString 
(ASN1UINT numocts, const ASN1OCTET* data, char* buffer, size_t bufsiz)
{
   size_t i;
   char lbuf[4];

   if (bufsiz > 0) {
      buffer[0] = '\'';
      if (bufsiz > 1) buffer[1] = '\0';
      for (i = 0; i < numocts; i++) {
         if (i < bufsiz - 1) {
            sprintf (lbuf, "%02x", data[i]);
            strcat (&buffer[(i*2)+1], lbuf);
         }
         else break;
      }
     i = i*2 + 1;
      if (i < bufsiz - 1) buffer[i++] = '\'';
      if (i < bufsiz - 1) buffer[i++] = 'H';
      if (i < bufsiz - 1) buffer[i] = '\0';
      else buffer[bufsiz - 1] = '\0';
   }

   return buffer;
}