Fix some Dead Store (Dead assignement/Dead increment) Warning found by Clang
[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  * Wireshark - Network traffic analyzer
10  * By Gerald Combs <gerald@wireshark.org>
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 #include "emem.h"
38
39 /*
40  * XXX - shouldn't there be a centralized routine for dissecting NSAPs?
41  * See also "dissect_atm_nsap()" in epan/dissectors/packet-arp.c and
42  * "dissect_nsap()" in epan/dissectors/packet-isup.c.
43  */
44 gchar *
45 print_nsap_net( const guint8 *ad, int length )
46 {
47   gchar *cur;
48
49   cur = ep_alloc(MAX_NSAP_LEN * 3 + 50);
50   print_nsap_net_buf( ad, length, cur, MAX_NSAP_LEN * 3 + 50);
51   return( cur );
52 }
53
54 /* XXX - Should these be converted to string buffers? */
55 void
56 print_nsap_net_buf( const guint8 *ad, int length, gchar *buf, int buf_len)
57 {
58   gchar *cur;
59
60   /* to do : NSAP / NET decoding */
61
62   if ( (length <= 0 ) || ( length > MAX_NSAP_LEN ) ) {
63     g_snprintf(buf, buf_len, "<Invalid length of NSAP>");
64     return;
65   }
66   cur = buf;
67   if ( ( length == RFC1237_NSAP_LEN ) || ( length == RFC1237_NSAP_LEN + 1 ) ) {
68     print_area_buf(ad, RFC1237_FULLAREA_LEN, cur, buf_len);
69     cur += strlen( cur );
70     print_system_id_buf( ad + RFC1237_FULLAREA_LEN, RFC1237_SYSTEMID_LEN, cur, (int) (buf_len-(cur-buf)));
71     cur += strlen( cur );
72     cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "[%02x]",
73                     ad[ RFC1237_FULLAREA_LEN + RFC1237_SYSTEMID_LEN ] );
74     if ( length == RFC1237_NSAP_LEN + 1 ) {
75       g_snprintf(cur, (int) (buf_len-(cur-buf)), "-%02x", ad[ length -1 ] );
76     }
77   }
78   else {    /* probably format as standard */
79     print_area_buf( ad, length, buf, buf_len);
80   }
81 } /* print_nsap */
82
83 gchar *
84 print_system_id( const guint8 *ad, int length )
85 {
86   gchar        *cur;
87
88   cur = ep_alloc(MAX_SYSTEMID_LEN * 3 + 5);
89   print_system_id_buf(ad, length, cur, MAX_SYSTEMID_LEN * 3 + 5);
90   return( cur );
91 }
92
93 void
94 print_system_id_buf( const guint8 *ad, int length, gchar *buf, int buf_len)
95 {
96   gchar        *cur;
97   int           tmp;
98
99   if ( ( length <= 0 ) || ( length > MAX_SYSTEMID_LEN ) ) {
100     g_snprintf(buf, buf_len, "<Invalid length of SYSTEM ID>");
101     return;
102   }
103
104   cur = buf;
105   if ( ( 6 == length ) || /* System-ID */
106        ( 7 == length ) || /* LAN-ID */
107        ( 8 == length )) { /* LSP-ID */
108     cur += g_snprintf(cur, buf_len, "%02x%02x.%02x%02x.%02x%02x", ad[0], ad[1],
109                     ad[2], ad[3], ad[4], ad[5] );
110     if ( ( 7 == length ) ||
111          ( 8 == length )) {
112         cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), ".%02x", ad[6] );
113     }
114     if ( 8 == length ) {
115         g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "-%02x", ad[7] );
116     }
117   }
118   else {
119     tmp = 0;
120     while ( tmp < length / 4 ) { /* 16 / 4 == 4 > four Octets left to print */
121       cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x", ad[tmp++] );
122       cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x", ad[tmp++] );
123       cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x", ad[tmp++] );
124       cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x.", ad[tmp++] );
125     }
126     if ( 1 == tmp ) {   /* Special case for Designated IS */
127       cur--;
128       g_snprintf(cur, (gulong) (buf_len-(cur-buf)), ".%02x", ad[tmp] );
129     }
130     else {
131       for ( ; tmp < length; ) {  /* print the rest without dot */
132         cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x", ad[tmp++] );
133       }
134     }
135   }
136 }
137
138 gchar *
139 print_area(const guint8 *ad, int length)
140 {
141   gchar *cur;
142
143   cur = ep_alloc(MAX_AREA_LEN * 3 + 20);
144   print_area_buf(ad, length, cur, MAX_AREA_LEN * 3 + 20);
145   return cur;
146 }
147
148 void
149 print_area_buf(const guint8 *ad, int length, gchar *buf, int buf_len)
150 {
151   gchar *cur;
152   int  tmp  = 0;
153
154   /* to do : all real area decoding now: NET is assumed if id len is 1 more byte
155    * and take away all these stupid resource consuming local statics
156    */
157   if (length <= 0 || length > MAX_AREA_LEN) {
158     g_snprintf(buf, buf_len, "<Invalid length of AREA>");
159     return;
160   }
161
162   cur = buf;
163   if ( (  ( NSAP_IDI_ISODCC          == *ad )
164        || ( NSAP_IDI_GOSIP2          == *ad )
165        )
166        &&
167        (  ( RFC1237_FULLAREA_LEN     ==  length )
168        || ( RFC1237_FULLAREA_LEN + 1 ==  length )
169        )
170      ) {    /* AFI is good and length is long enough  */
171
172     /* there used to be a check for (length > RFC1237_FULLAREA_LEN + 1) here,
173      * in order to report an invalied length of AREA for DCC / GOSIP AFI,
174      * but that can *never* be the case because the if() test above explicitly
175      * tests for (length == RFC1237_FULLAREA_LEN) or (length == RFC1237_FULLAREA_LEN + 1)
176      */
177
178     cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "[%02x|%02x:%02x][%02x|%02x:%02x:%02x|%02x:%02x]",
179                     ad[0], ad[1], ad[2], ad[3], ad[4],
180                     ad[5], ad[6], ad[7], ad[8] );
181     cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "[%02x:%02x|%02x:%02x]",
182                     ad[9], ad[10],  ad[11], ad[12] );
183     if ( RFC1237_FULLAREA_LEN + 1 == length )
184       g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "-[%02x]", ad[20] );
185   }
186   else { /* print standard format */
187     if ( length == RFC1237_AREA_LEN ) {
188       g_snprintf(buf, buf_len, "%02x.%02x%02x", ad[0], ad[1], ad[2] );
189       return;
190     }
191         if(length == 4)
192         {
193       g_snprintf(buf, buf_len, "%02x%02x%02x%02x", ad[0], ad[1], ad[2], ad[3] );
194         }
195     if ( 4 < length ) 
196         {
197       while ( tmp < length / 4 ) {      /* 16/4==4 > four Octets left to print */
198         cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x", ad[tmp++] );
199         cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x", ad[tmp++] );
200         cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x", ad[tmp++] );
201         cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x.", ad[tmp++] );
202       }
203       if ( 1 == tmp ) {                     /* Special case for Designated IS */
204         cur--;
205         g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "-%02x", ad[tmp] );
206       }
207       else {
208         for ( ; tmp < length; ) {  /* print the rest without dot */
209           cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x", ad[tmp++] );
210         }
211       }
212     }
213   }
214 } /* print_area_buf */
215