fix usage of "if(tree) {" to display the right things, even if no coloring rule is set
[obnox/wireshark/wip.git] / epan / osi-utils.c
1 /* osi-utils.c
2  * Routines for ISO/OSI network and transport protocol packet disassembly
3  * Main entrance point and common functions
4  *
5  * $Id$
6  * Laurent Deniel <laurent.deniel@free.fr>
7  * Ralf Schneider <Ralf.Schneider@t-online.de>
8  *
9  * Ethereal - Network traffic analyzer
10  * By Gerald Combs <gerald@ethereal.com>
11  * Copyright 1998 Gerald Combs
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <stdio.h>
33 #include <string.h>
34 #include <glib.h>
35
36 #include "osi-utils.h"
37
38 gchar *
39 print_nsap_net( const guint8 *ad, int length )
40 {
41   static gchar  str[MAX_NSAP_LEN * 3 + 50]; /* reserve space for nice layout */
42   gchar *cur;
43
44   cur = str;
45   print_nsap_net_buf( ad, length, cur );
46   return( cur );
47 }
48
49 void
50 print_nsap_net_buf( const guint8 *ad, int length, gchar *buf )
51 {
52   gchar *cur;
53
54   /* to do : NSAP / NET decoding */
55
56   if ( (length <= 0 ) || ( length > MAX_NSAP_LEN ) ) {
57     sprintf( buf, "<Invalid length of NSAP>");
58     return;
59   }
60   cur = buf;
61   if ( ( length == RFC1237_NSAP_LEN ) || ( length == RFC1237_NSAP_LEN + 1 ) ) {
62     print_area_buf( ad, RFC1237_FULLAREA_LEN, cur );
63     cur += strlen( cur );
64     print_system_id_buf( ad + RFC1237_FULLAREA_LEN, RFC1237_SYSTEMID_LEN, cur );
65     cur += strlen( cur );
66     cur += sprintf( cur, "[%02x]",
67                     ad[ RFC1237_FULLAREA_LEN + RFC1237_SYSTEMID_LEN ] );
68     if ( length == RFC1237_NSAP_LEN + 1 ) {
69       cur += sprintf( cur, "-%02x", ad[ length -1 ] );
70     }
71   }
72   else {    /* probably format as standard */
73     print_area_buf( ad, length, buf );
74   }
75 } /* print_nsap */
76
77 gchar *
78 print_system_id( const guint8 *ad, int length )
79 {
80   static gchar  str[MAX_SYSTEMID_LEN * 3 + 5]; /* Don't trust exact matching */
81   gchar        *cur;
82
83   cur = str;
84   print_system_id_buf(ad, length, cur );
85   return( cur );
86 }
87
88 void
89 print_system_id_buf( const guint8 *ad, int length, gchar *buf )
90 {
91   gchar        *cur;
92   int           tmp;
93
94   if ( ( length <= 0 ) || ( length > MAX_SYSTEMID_LEN ) ) {
95     sprintf( buf, "<Invalid length of SYSTEM ID>");
96     return;
97   }
98
99   cur = buf;
100   if ( ( 6 == length ) || /* System-ID */
101        ( 7 == length ) || /* LAN-ID */
102        ( 8 == length )) { /* LSP-ID */
103     cur += sprintf(cur, "%02x%02x.%02x%02x.%02x%02x", ad[0], ad[1],
104                     ad[2], ad[3], ad[4], ad[5] );
105     if ( ( 7 == length ) ||
106          ( 8 == length )) {
107         cur += sprintf( cur, ".%02x", ad[6] );
108     }
109     if ( 8 == length ) {
110         cur += sprintf( cur, "-%02x", ad[7] );
111     }
112   }
113   else {
114     tmp = 0;
115     while ( tmp < length / 4 ) { /* 16 / 4 == 4 > four Octets left to print */
116       cur += sprintf( cur, "%02x", ad[tmp++] );
117       cur += sprintf( cur, "%02x", ad[tmp++] );
118       cur += sprintf( cur, "%02x", ad[tmp++] );
119       cur += sprintf( cur, "%02x.", ad[tmp++] );
120     }
121     if ( 1 == tmp ) {   /* Special case for Designated IS */
122       sprintf( --cur, ".%02x", ad[tmp] );
123     }
124     else {
125       for ( ; tmp < length; ) {  /* print the rest without dot */
126         cur += sprintf( cur, "%02x", ad[tmp++] );
127       }
128     }
129   }
130 }
131
132 gchar *
133 print_area(const guint8 *ad, int length)
134 {
135   static gchar  str[MAX_AREA_LEN * 3 + 20]; /* reserve space for nice layout */
136   gchar *cur;
137
138   cur = str;
139   print_area_buf( ad, length, cur );
140   return cur;
141 }
142
143 void
144 print_area_buf(const guint8 *ad, int length, gchar *buf)
145 {
146   gchar *cur;
147   int  tmp  = 0;
148
149   /* to do : all real area decoding now: NET is assumed if id len is 1 more byte
150    * and take away all these stupid resource consuming local statics
151    */
152   if (length <= 0 || length > MAX_AREA_LEN) {
153     sprintf( buf, "<Invalid length of AREA>");
154     return;
155   }
156
157   cur = buf;
158   if ( (  ( NSAP_IDI_ISODCC          == *ad )
159        || ( NSAP_IDI_GOSIP2          == *ad )
160        )
161        &&
162        (  ( RFC1237_FULLAREA_LEN     ==  length )
163        || ( RFC1237_FULLAREA_LEN + 1 ==  length )
164        )
165      ) {    /* AFI is good and length is long enough  */
166
167     if ( length > RFC1237_FULLAREA_LEN + 1 ) {  /* Special Case Designated IS */
168       sprintf( buf, "<Invalid length of AREA for DCC / GOSIP AFI>");
169       return;
170     }
171
172     cur += sprintf( cur, "[%02x|%02x:%02x][%02x|%02x:%02x:%02x|%02x:%02x]",
173                     ad[0], ad[1], ad[2], ad[3], ad[4],
174                     ad[5], ad[6], ad[7], ad[8] );
175     cur += sprintf( cur, "[%02x:%02x|%02x:%02x]",
176                     ad[9], ad[10],  ad[11], ad[12] );
177     if ( RFC1237_FULLAREA_LEN + 1 == length )
178       sprintf( cur, "-[%02x]", ad[20] );
179   }
180   else { /* print standard format */
181     if ( length == RFC1237_AREA_LEN ) {
182       sprintf( buf, "%02x.%02x%02x", ad[0], ad[1], ad[2] );
183       return;
184     }
185     if ( 4 < length ) {
186       while ( tmp < length / 4 ) {      /* 16/4==4 > four Octets left to print */
187         cur += sprintf( cur, "%02x", ad[tmp++] );
188         cur += sprintf( cur, "%02x", ad[tmp++] );
189         cur += sprintf( cur, "%02x", ad[tmp++] );
190         cur += sprintf( cur, "%02x.", ad[tmp++] );
191       }
192       if ( 1 == tmp ) {                     /* Special case for Designated IS */
193         sprintf( --cur, "-%02x", ad[tmp] );
194       }
195       else {
196         for ( ; tmp < length; ) {  /* print the rest without dot */
197           cur += sprintf( cur, "%02x", ad[tmp++] );
198         }
199       }
200     }
201   }
202 } /* print_area_buf */
203