/
decodeIMDoc.cpp
454 lines (402 loc) · 10.7 KB
/
decodeIMDoc.cpp
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
// DecodeIMDoc.cpp : implementation of the CDecodeIMDoc class
//
//////////////////////////////////////////////////////////////////
//
// Revision information
//
//
//
//
// March 13 2002: ewcpahi:
// Added additional IEs based on Q.931 05/98 recommendation
// April 8 2002: ewcpahi
// Changed format of IE names so that single- and multi-
// octet elements have the same decoded appearance
//
//////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "decoder.h"
#include "DecodeIMDoc.h"
#include "BitSet.h"
#include "HTMLfmt.h"
#include "ISDN_defs.h"
#include "Decoders.h"
#include "DecodeIEUtils.h"
#include <fstream.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDecodeIMDoc
IMPLEMENT_DYNCREATE(CDecodeIMDoc, CDocument)
BEGIN_MESSAGE_MAP(CDecodeIMDoc, CDocument)
//{{AFX_MSG_MAP(CDecodeIMDoc)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDecodeIMDoc construction/destruction
CDecodeIMDoc::CDecodeIMDoc()
{
// TODO: add one-time construction code here
m_sTempFileName = _tempnam("c:\\","IDec");
m_bHasDecodeContent = false;
m_sLastMessage = "";
}
CDecodeIMDoc::~CDecodeIMDoc()
{
}
#include "UserInputDlg.h"
BOOL CDecodeIMDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
CUserInputDlg userInputDlg;
userInputDlg.m_UserInput = getLastMessage();
if (userInputDlg.DoModal() == IDOK)
DecodeMessage(userInputDlg.m_UserInput);
return TRUE;
}
void CDecodeIMDoc::OnCloseDocument()
{
DeleteFile(m_sTempFileName);
}
/////////////////////////////////////////////////////////////////////////////
// CDecodeIMDoc serialization
void CDecodeIMDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
/////////////////////////////////////////////////////////////////////////////
// CDecodeIMDoc diagnostics
#ifdef _DEBUG
void CDecodeIMDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CDecodeIMDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CDecodeIMDoc commands
#define INDENT_SIZE 3
static BYTE HexValue(TCHAR c)
{
if (!isxdigit(c))
return 0;
if ( c >= '0' && c <= '9')
return c - '0';
if ( c >= 'A' && c <= 'F' )
return (c - 'A') + 10;
return 0;
}
class CPException
{
public:
CPException(){};
~CPException(){};
const char *ShowReason() const { return "Exception in CPException class."; }
};
static BYTE GetByte(CString message, int index)
{
if (index*2+1 > message.GetLength() )
throw CPException();
TCHAR c1 = toupper(message.GetAt(index*2));
TCHAR c2 = toupper(message.GetAt(index*2+1));
return (HexValue(c1)*16) + HexValue(c2);
}
static void SetFont(ofstream f, eMessageMode MessageMode )
{
switch (MessageMode) {
case mandatory:
f << "<font color=blue>";
break;
case optional:
f << "<font color=green>";
break;
case other:
f << "<font color=red>";
break;
}
}
void DecodeISDNMessage(ofstream f, CString message )
{
sGlobalDefs gvars;
gvars.GlobalCallReference = false;
bool decodeOk = true;
int bi=0;
message.TrimLeft(); message.TrimRight();
BYTE Protocol=0;
try {
Protocol=GetByte(message,bi++);
SetFont(f,ElementType(0,IE_PROTOCOL_DISCRIMINATOR));
decodeOk=DecodeProtocol(f,INDENT_SIZE,Protocol);
f<<eFont;
f<<Break<<endl;
} catch (CPException se) {
decodeOk=false;
}
BYTE crefl;
if ( decodeOk ) {
try {
crefl = GetByte(message,bi++);
} catch (CPException se) {
decodeOk=false;
}
}
if ( decodeOk ) {
SetFont(f,ElementType(0,IE_CALL_REFERENCE));
decodeOk=DecodeCallReferenceLength(f,3,crefl);
if (decodeOk) {
int callRefLen = (crefl & 0x0f); // should be one or two only
if ( callRefLen != 0 )
{
BYTE CallRef[2] = { 0,0 };
for (int j=0; j<crefl; j++)
try {
CallRef[j]=GetByte(message,bi++);
}
catch (CPException se) {
f<<"**Not enough Call Reference data**"<<Break<<endl;
decodeOk = false;
}
if ( decodeOk )
DecodeCallReference(f,INDENT_SIZE,crefl,CallRef[0],CallRef[1]);
}
}
f<<eFont;
f<<Break<<endl;
}
BYTE MessageType;
if ( decodeOk ) {
try {
MessageType = GetByte(message,bi++);
} catch (CPException se) {
decodeOk=false;
}
}
CBitSet elements;
if ( decodeOk ) {
SetFont(f,ElementType(MessageType,IE_MESSAGE_TYPE));
DecodeMessageType(f,INDENT_SIZE,Protocol,MessageType);
f<<eFont<<endl;
}
gvars.ActiveCodeSet = 0;
int LastActiveCodeSet=0;
bool decodeDone=false;
while ( decodeOk && !decodeDone) {
int ie;
try {
ie = GetByte(message,bi++);
} catch (CPException se) {
decodeDone=true;
continue;
}
f << Break << endl;
if ( (ie & 0x80) == 0x80 ) {
// single octet Information Element
SetFont(f,ElementType(MessageType,ie));
if (ie==IE_SENDING_COMPLETE) {
f << "ie=" << HexOutput(ie) << Break << "IE="<<IEname(MAKE_FULLIE(gvars.ActiveCodeSet,ie)) << Break << endl;
}
else if (ie==IE_MORE_DATA) {
f << "ie=" << HexOutput(ie) << Break << "IE="<<IEname(MAKE_FULLIE(gvars.ActiveCodeSet,ie)) << Break << endl;
}
else {
BYTE d[1];
switch (ie&0xf0) {
case IE_RESERVED:
f << "ie=" << HexOutput(ie) << Break << "IE=" << IEname(MAKE_FULLIE(gvars.ActiveCodeSet,ie)) << Break << endl;
break;
case IE_SHIFT:
f << "ie=" << HexOutput(ie) << Break << "IE=" << IEname(MAKE_FULLIE(gvars.ActiveCodeSet,ie)) << Break << endl;
d[0]=ie;
DecodeShift(f,3,&gvars,d);
break;
case IE_CONGESTION_LEVEL:
f << "ie=" << HexOutput(ie) << Break << "IE=" << IEname(MAKE_FULLIE(gvars.ActiveCodeSet,ie)) << Break << endl;
d[0]=ie;
DecodeCongestionLevel(f,3,&gvars,d);
break;
case IE_REPEAT_INDICATOR:
f << "ie=" << HexOutput(ie) << Break << "IE=" << IEname(MAKE_FULLIE(gvars.ActiveCodeSet,ie)) << Break << endl;
d[0]=ie;
DecodeRepeatIndication(f,3,&gvars,d);
break;
default:
f << "**invalid Single Octet IE** (" << HexOutput(ie) << ")" << Break << endl;
decodeOk = false;
}
}
f<<eFont<<endl;
}
else {
// multiple octet Information Element
SetFont(f,ElementType(MessageType,ie));
int ielen;
try {
ielen = GetByte(message,bi++);
} catch (CPException se ) {
// IE is missing its length
f << "No IE Length for ie=" << HexOutput(ie) << Break << endl;
decodeOk = false;
continue;
}
if ( ielen == 0 ) {
f << "IE length cannot be zero for ie=" << HexOutput(ie) << Break << endl;
decodeOk = false;
continue;
}
f << "ie=" << HexOutput(ie) << " len=" << HexOutput(ielen) << " data=";
int dstat = bi;
BYTE iea[64];
memset(iea,0,sizeof(iea));
try {
iea[0]=0xff;
for (int i=0; i<ielen; i++)
{
int ieb = GetByte(message,dstat++);
f << " " << HexOutput(ieb);
iea[i+1]=ieb;
iea[0]=i+1;
}
} catch (CPException se) {
f << " **more expected**";
decodeOk=false;
}
f << Break << endl;
if ( decodeOk ) {
elements.setBit(ie);
f << "IE=" << IEname(MAKE_FULLIE(gvars.ActiveCodeSet,ie)) << Break << endl;
psIEDecoderEntry p = &IEDecoders[0];
if ( p != NULL ) {
while (p->DecodeFunction!=NULL) {
if ((p->CodeSet==gvars.ActiveCodeSet) && ((p->IEid&p->IEidMask)==(ie&p->IEidMask))) {
(p->DecodeFunction)(f,INDENT_SIZE,&gvars,iea);
break;
}
p++;
}
if ( p->DecodeFunction == NULL )
f<<DrawIndent(INDENT_SIZE)<<"!!!NO DECODERS INSTALLED FOR codeset="<<HexOutput(gvars.ActiveCodeSet)<<" ie="<<HexOutput(ie)<<"!!!"<<Break<<endl;
}
else
f<<DrawIndent(INDENT_SIZE)<<"!!!NO DECODERS INSTALLED!!!"<<Break<<endl;
}
f << eFont;
bi = dstat;
}
// decrement the vars->LockCount. If its zero, restore the ActiveCodeSet
if ( gvars.LockCount > 0 ) {
gvars.LockCount--;
if ( gvars.LockCount == 0 )
gvars.ActiveCodeSet = gvars.LastCodeSet;
}
}
/*
if ( decodeOk ) {
// Can add some sanity check here to ensure that all the mandatory fields were entered
if ( Protocol == PROTOCOL_Q931) {
switch (MessageType) {
case MESSAGE_ALERTING:
break;
case MESSAGE_CALL_PROCEEDING:
break;
case MESSAGE_CONNECT:
break;
case MESSAGE_CONNECT_ACKNOWLEDGE:
break;
case MESSAGE_PROGRESS:
break;
case MESSAGE_SETUP:
break;
case MESSAGE_SETUP_ACKNOWLEDGE:
break;
case MESSAGE_RESUME:
break;
case MESSAGE_RESUME_ACKNOWLEDGE:
break;
case MESSAGE_RESUME_REJECT:
break;
case MESSAGE_SUSPEND:
break;
case MESSAGE_SUSPEND_ACKNOWLEDGE:
break;
case MESSAGE_SUSPEND_REJECT:
break;
case MESSAGE_USER_INFORMATION:
break;
case MESSAGE_DISCONNECT:
break;
case MESSAGE_RELEASE:
break;
case MESSAGE_RELEASE_COMPLETE:
break;
case MESSAGE_RESTART:
break;
case MESSAGE_RESTART_ACKNOWLEDGE:
break;
case MESSAGE_SEGMENT:
break;
case MESSAGE_CONGESTION_CONTROL:
break;
case MESSAGE_INFORMATION:
break;
case MESSAGE_FACILITY:
break;
case MESSAGE_FACILITY_REJECT:
break;
case MESSAGE_NOTIFY:
break;
case MESSAGE_STATUS:
break;
case MESSAGE_STATUS_ENQUIRY:
break;
default:
decodeOk=false;
break;
}
}
if ( Protocol == PROTOCOL_MAINTENANCE ) {
switch (MessageType) {
case MESSAGE_SERVICE:
break;
case MESSAGE_SERVICE_ACKNOWLEDGE:
break;
default:
decodeOk=false;
break;
}
}
}
*/
}
BOOL CDecodeIMDoc::DecodeMessage(CString message)
{
ofstream f(m_sTempFileName, ios::out|ios::trunc);
if ( f == NULL ||message.GetLength()==0)
return false;
m_sLastMessage = message;
m_bHasDecodeContent = true;
f << sHTML << "<head>" << StyleList << "</head>";
f << sBody << sFont(FONT_COURIER) << message << eFont << Break << endl;
DecodeISDNMessage(f,message);
f << eBody << eHTML << endl;
f.close();
UpdateAllViews(NULL);
return true;
}