MSWSP: fix dissect_mswsp_smb()
[wireshark-wip.git] / plugins / wimax / wimax_tlv.c
blob53f701917a81517c7fc3588b370a1a4e62adbd48
1 /* wimax_tlv.c
2 * WiMax TLV handling functions
4 * Copyright (c) 2007 by Intel Corporation.
6 * Author: Lu Pan <lu.pan@intel.com>
8 * $Id$
10 * Wireshark - Network traffic analyzer
11 * By Gerald Combs <gerald@wireshark.org>
12 * Copyright 1999 Gerald Combs
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 /*************************************************************/
30 /* ----------------------- NOTE ------------------------- */
31 /* There is an identical copy of this file, wimax_tlv.c, in */
32 /* both .../plugins/m2m and .../plugins/wimax. If either */
33 /* one needs to be modified, please be sure to copy the file */
34 /* to the sister directory and check it in there also. */
35 /* This prevents having to generate a complicated */
36 /* Makefile.nmake in .../plugins/m2m. */
37 /*************************************************************/
39 #include "config.h"
41 #include "wimax_tlv.h"
43 /*************************************************************/
44 /* init_tlv_info() */
45 /* retrive the tlv information from specified tvb and offset */
46 /* parameter: */
47 /* info - pointer of a tlv information data structure */
48 /* return: */
49 /* 0-success */
50 /* !=0-the invalid size of the TLV length (failed) */
51 /*************************************************************/
52 gint init_tlv_info(tlv_info_t *info, tvbuff_t *tvb, gint offset)
54 guint tlv_len;
56 /* get TLV type */
57 info->type = (guint8)tvb_get_guint8( tvb, offset );
58 /* get TLV length */
59 tlv_len = (guint)tvb_get_guint8( tvb, (offset + 1) );
60 /* set the TLV value offset */
61 info->value_offset = 2;
62 /* adjust for multiple-byte TLV length */
63 if((tlv_len & WIMAX_TLV_EXTENDED_LENGTH_MASK) != 0)
64 { /* multiple bytes TLV length */
65 info->length_type = 1;
66 /* get the size of the TLV length */
67 tlv_len = (tlv_len & WIMAX_TLV_LENGTH_MASK);
68 info->size_of_length = tlv_len;
69 /* update the TLV value offset */
70 info->value_offset += tlv_len;
71 switch (tlv_len)
73 case 0:
74 info->length = 0; /* no length */
75 break;
76 case 1:
77 info->length = (gint32)tvb_get_guint8( tvb, (offset + 2) ); /* 8 bit */
78 break;
79 case 2:
80 info->length = (gint32)tvb_get_ntohs( tvb, (offset + 2) ); /* 16 bit */
81 break;
82 case 3:
83 info->length = (gint32)tvb_get_ntoh24( tvb, (offset + 2) ); /* 24 bit */
84 break;
85 case 4:
86 info->length = (gint32)tvb_get_ntohl( tvb, (offset + 2) ); /* 32 bit */
87 break;
88 default:
89 /* mark invalid tlv */
90 info->valid = 0;
91 /* failed, return the invalid size of the tlv length */
92 return (gint)tlv_len;
93 break;
96 else /* single byte length */
98 info->length_type = 0;
99 info->size_of_length = 0;
100 info->length = (gint32)tlv_len;
102 /* mark valid tlv */
103 info->valid = 1;
104 /* success */
105 return 0;
108 /*************************************************************/
109 /* get_tlv_type() */
110 /* get the tlv type of the specified tlv information */
111 /* parameter: */
112 /* info - pointer of a tlv information data structure */
113 /* return: */
114 /* >=0 - TLV type */
115 /* =-1 - invalid tlv info */
116 /*************************************************************/
117 gint get_tlv_type(tlv_info_t *info)
119 if(info->valid)
120 return (gint)info->type;
121 return -1;
124 /**************************************************************/
125 /* get_tlv_size_of_length() */
126 /* get the size of tlv length of the specified tlv information*/
127 /* parameter: */
128 /* info - pointer of a tlv information data structure */
129 /* return: */
130 /* >=0 - the size of TLV length */
131 /* =-1 - invalid tlv info */
132 /**************************************************************/
133 gint get_tlv_size_of_length(tlv_info_t *info)
135 if(info->valid)
136 return (gint)info->size_of_length;
137 return -1;
140 /*************************************************************/
141 /* get_tlv_length() */
142 /* get the tlv length of the specified tlv information */
143 /* parameter: */
144 /* info - pointer of a tlv information data structure */
145 /* return: */
146 /* >=0 - TLV length */
147 /* =-1 - invalid tlv info */
148 /*************************************************************/
149 gint32 get_tlv_length(tlv_info_t *info)
151 if(info->valid)
152 return (gint32)info->length;
153 return -1;
156 /*************************************************************/
157 /* get_tlv_value_offset() */
158 /* get the tlv value offset of the specified tlv information */
159 /* parameter: */
160 /* info - pointer of a tlv information data structure */
161 /* return: */
162 /* >0 - TLV value offset in byte */
163 /* =-1 - invalid tlv info */
164 /*************************************************************/
165 gint get_tlv_value_offset(tlv_info_t *info)
167 if(info->valid)
168 return (gint)info->value_offset;
169 return -1;
172 /*************************************************************/
173 /* get_tlv_length_type() */
174 /* get the tlv length type of the specified tlv information */
175 /* parameter: */
176 /* info - pointer of a tlv information data structure */
177 /* return: */
178 /* 0 - single byte TLV length */
179 /* 1 - multiple bytes TLV length */
180 /*************************************************************/
181 gint get_tlv_length_type(tlv_info_t *info)
183 if(info->valid)
184 return (gint)info->length_type;
185 return -1;