From Ronnie Sahlberg:
[obnox/wireshark/wip.git] / packet-smb-common.c
1 /* packet-smb-common.c
2  * Common routines for smb packet dissection
3  * Copyright 2000, Jeffrey C. Foster <jfoste@woodward.com>
4  *
5  * $Id: packet-smb-common.c,v 1.8 2002/01/24 09:20:51 guy Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 1998 Gerald Combs
10  *
11  * Copied from packet-pop.c
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 #include "packet-smb-common.h"
29
30 int display_ms_string(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int offset, int hf_index)
31 {
32         char *str;
33         int len;
34
35         /* display a string from the tree and return the new offset */
36
37         len = tvb_strnlen(tvb, offset, -1);
38         if (len == -1) {
39                 /*
40                  * XXX - throw an exception?
41                  */
42                 len = tvb_length_remaining(tvb, offset);
43         }
44         str = g_malloc(len+1);
45         tvb_memcpy(tvb, (guint8 *)str, offset, len);
46         str[len] = '\0';
47         
48         proto_tree_add_string(tree, hf_index, tvb, offset, len+1, str);
49         
50         /*
51          * XXX - "proto_tree_add_string()" mallocates a copy; it'd
52          * be nice not to have it copy the string, but just to
53          * make it the value, avoiding both the copy and the free
54          * on the next line.
55          */
56         g_free(str);
57
58         return  offset+len+1;
59 }
60
61
62 int display_unicode_string(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int offset, int hf_index)
63 {
64         /* display a unicode string from the tree and return new offset */
65
66         char Temp[100], *OutPtr;
67         const char *InPtr;
68         
69         /* this will crash if composite tvbuffs are used */
70         /* XXX - need tvbuff routine to extract DBCS string lengths */
71         InPtr = tvb_get_ptr(tvb, offset, 1);
72         OutPtr = Temp;                  /* point to temp space */
73         
74         while ( *InPtr){                /* copy every other byte */ 
75                 *OutPtr++ = *InPtr;
76                 InPtr += 2;
77         } 
78         *OutPtr = 0;                    /* terminate out string */      
79                 
80         proto_tree_add_string(tree, hf_index, tvb, 
81                 offset, strlen(Temp)*2+2, Temp);
82         
83         return  offset+strlen(Temp)*2+2;
84 }
85
86 int
87 dissect_smb_unknown(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int offset)
88 {
89         /* display data as unknown */
90
91         proto_tree_add_text(tree, tvb, offset, -1, "Data (%u bytes)",
92             tvb_reported_length_remaining(tvb, offset));
93
94         return offset+tvb_length_remaining(tvb, offset);
95 }