Fix off by one shift errors introduced in r39530 where it seemed proper[1]
[metze/wireshark/wip.git] / epan / dissectors / packet-tds.c
1 /* packet-tds.c
2  * Routines for TDS NetLib dissection
3  * Copyright 2000-2002, Brian Bruns <camber@ais.org>
4  * Copyright 2002, Steve Langasek <vorlon@netexpress.net>
5  *
6  * $Id$
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25  */
26
27 /*
28  * The NETLIB protocol is a small blocking protocol designed to allow TDS
29  * to be placed within different transports (TCP, DECNet, IPX/SPX).  A
30  * NETLIB packet starts with an eight byte header containing:
31  *
32  *      a one-byte packet type field;
33  *
34  *      a one-byte status field;
35  *
36  *      a two-byte big-endian size field giving the size of the packet,
37  *      including the header;
38  *
39  *      a two-byte big-endian channel number, used when multiple sessions
40  *      are being multiplexed on a single connection;
41  *
42  *      a one-byte packet number, giving "the frame number of a multiplexed
43  *      message, modulo 256";
44  *
45  *      a one-byte window, which is the number of frames to be sent
46  *      before an acknowledgment message is received.
47  *
48  * followed by payload whose size is the value in the size field minus
49  * 8.
50  *
51  * Microsoft Network Monitor 2.x dissects the 4 byte field (and indicates
52  * that the one-byte last packet indicator also contains other bits).
53  *
54  * The TDS protocol consists of a number of protocol data units (PDUs) that
55  * appear to be assembled from NETLIB packets, in the form of zero or more
56  * NETLIB packets with the last packet indicator clear and a final NETLIB
57  * packet with the last packet indicator set.  The type of the TDS PDU is
58  * specified by the packet type field of the NETLIB header (presumably that
59  * field has the same value for all NETLIB packets that make up a TDS PDU).
60  *
61  * The "server response" PDU consists of a sequence of multiple items, each
62  * one beginning with a one byte type field at the start of the PDU.  Some
63  * items are fixed length, some are variable length with a two byte size
64  * field following the item type, and then there is TDS_ROW_TOKEN in which
65  * size is determined by analyzing the result set returned from the server.
66  * This in effect means that we are hopelessly lost if we haven't seen the
67  * result set.  Also, TDS 4/5 is byte order negotiable, which is specified
68  * in the login packet.  We can attempt to determine it later on, but not
69  * with 100% accuracy.
70  *
71  * Some preliminary documentation on the packet format can be found at
72  * http://www.freetds.org/tds.html
73  *
74  * Some more information can be found in
75  * http://www.sybase.com/content/1013412/tds34.pdf
76  * http://www.sybase.com/content/1040983/Sybase-tds38-102306.pdf
77  * Microsoft's [MS-TDS] protocol specification
78  *
79  * This document is no longer available here:
80  * http://download.nai.com/products/media/sniffer/support/sdos/sybase.pdf
81  *
82  * Much of this code was originally developed for the FreeTDS project.
83  * http://www.freetds.org
84  */
85
86 /*
87  * Excerpts from Brian's posting to wireshark-dev:
88  *
89  * The TDS Protocol is actually a protocol within a protocol.  On the outside
90  * there is netlib which is not so much a encapsulation as a blocking of the
91  * data, typically to 512 or 4096 bytes.  Between this are the protocol data
92  * units for TDS.  Netlib packets may be split over real packets, multiple
93  * netlib packets may appear in single real packets.  TDS PDUs may be split
94  * over netlib packets (and real packets) and most certainly can appear
95  * multiple times within a netlib packet.
96  *
97  * Because of this, I abandoned my earlier attempt at making two dissectors,
98  * one for netlib and one for TDS. Counterintuitively, a single dissector
99  * turned out to be simpler than splitting it up.
100  *
101  * Here are some of the (hefty) limitations of the current code
102  *
103  * . We currently do not handle netlib headers that cross packet boundaries.
104  *   This should be an easy fix.
105  * . I probably could have used the packet reassembly stuff, but I started
106  *   this at version 0.8.20, so c'est la vie. It wouldn't have covered the
107  *   netlib stuff anyway, so no big loss.
108  * . The older two layer version of the code dissected the PDU's, but the new
109  *   version does not yet, it only labels the names. I need an elegant way to
110  *   deal with dissecting data crossing (netlib and tcp) packet boundries.  I
111  *   think I have one, but ran out of time to do it.
112  * . It will only work on little endian platforms.  Or rather I should say,
113  *   the client that was captured must be little endian.  TDS 7.0/8.0 is
114  *   always LE; for TDS 4.2/5.0 look in the code for tvb_get_le*() functions,
115  *   there are fields in the login packet which determine byte order.
116  * . result sets that span netlib packets are not working
117  * . TDS 7 and 4.2 result sets are not working yet
118  *
119  * All that said, the code does deal gracefully with different boudary
120  * conditions and what remains are the easier bits, IMHO.
121  *
122  * XXX - "real packets" means "TCP segments", for TCP.
123  *
124  * XXX - is it *REALLY* true that you can have more than one TDS PDU (as
125  * opposed to more than one server response item) per NETLIB packet?  Or is
126  * all the data in a NETLIB packet put into a single TDS PDU?  If so, then
127  * we can reassemble NETLIB packets using the standard TCP desegmentation
128  * code, and can reassemble TDS PDUs using "fragment_add_seq_check()",
129  * and more cleanly separate the NETLIB and TDS dissectors (although the
130  * "is this NETLIB" heuristic would have to look at TDS information past
131  * the NETLIB header, in order to make the heuristic strong enough not
132  * to get too many false positives; note that the heuristic should reject
133  * any putative NETLIB packet with a length field with a value < 8).
134  *
135  * That would substantially clean the dissector up, eliminating most of
136  * the per-packet data (we might still need information to handle
137  * TDS_ROW_TOKEN), getting rid of the stuff to handle data split across
138  * TCP segment boundaries in favor of simple reassembly code, and
139  * fixing some otherwise nasty-looking crashing bugs.
140  *
141  * NOTE: we assume that all the data in a NETLIB packet *can* be put into
142  * a single TDS PTU, so that we have separate reassembly of NETLIB
143  * packets and TDS PDUs; it seems to work, and it really did clean stuff
144  * up and fix crashes.
145  */
146
147 #ifdef HAVE_CONFIG_H
148 # include "config.h"
149 #endif
150
151 #include <stdlib.h>
152 #include <string.h>
153 #include <ctype.h>
154
155 #include <glib.h>
156
157 #include "isprint.h"
158
159 #include <epan/packet.h>
160 #include <epan/conversation.h>
161 #include <epan/strutil.h>
162
163 #include "packet-frame.h"
164 #include <epan/reassemble.h>
165 #include <epan/prefs.h>
166 #include <epan/emem.h>
167 #include <epan/expert.h>
168
169 #define TDS_QUERY_PKT        1
170 #define TDS_LOGIN_PKT        2
171 #define TDS_RPC_PKT          3
172 #define TDS_RESP_PKT         4
173 #define TDS_RAW_PKT          5
174 #define TDS_ATTENTION_PKT    6
175 #define TDS_BULK_DATA_PKT    7
176 #define TDS_OPEN_CHN_PKT     8
177 #define TDS_CLOSE_CHN_PKT    9
178 #define TDS_RES_ERROR_PKT   10
179 #define TDS_LOG_CHN_ACK_PKT 11
180 #define TDS_ECHO_PKT        12
181 #define TDS_LOGOUT_CHN_PKT  13
182 #define TDS_TRANS_MGR_PKT   14
183 #define TDS_QUERY5_PKT      15  /* or "Normal tokenized request or response */
184 #define TDS_LOGIN7_PKT      16  /* or "Urgent tokenized request or response */
185 #define TDS_SSPI_PKT        17
186 #define TDS_PRELOGIN_PKT    18
187 #define TDS_INVALID_PKT     19
188
189 #define is_valid_tds_type(x) ((x) >= TDS_QUERY_PKT && (x) < TDS_INVALID_PKT)
190
191 /* The following constants are imported more or less directly from FreeTDS */
192 /*      Updated from FreeTDS v0.63 tds.h                                   */
193 /*         "$Id: tds.h,v 1.192 2004/10/28 12:42:12 freddy77]"              */
194 /* Note: [###] below means 'not defined in FreeTDS tds.h'                  */
195
196 #define TDS5_PARAMFMT2_TOKEN       32  /* 0x20    TDS 5.0 only              */
197 #define TDS_LANG_TOKEN             33  /* 0x21    TDS 5.0 only              */
198 #define TDS5_ORDERBY2_TOKEN        34  /* 0x22    TDS 5.0 only              */
199 #define TDS5_CURDECLARE2_TOKEN     35  /* 0x23    TDS 5.0 only        [###] */
200 #define TDS5_ROWFMT2_TOKEN         97  /* 0x61    TDS 5.0 only              */
201 #define TDS5_MSG_TOKEN            101  /* 0x65    TDS 5.0 only        [###] */
202 #define TDS_LOGOUT_TOKEN          113  /* 0x71    TDS 5.0 only? ct_close()  */
203 #define TDS_RET_STAT_TOKEN        121  /* 0x79                              */
204 #define TDS_PROCID_TOKEN          124  /* 0x7C    TDS 4.2 only - TDS_PROCID */
205 #define TDS_CURCLOSE_TOKEN        128  /* 0x80    TDS 5.0 only              */
206 #define TDS7_RESULT_TOKEN         129  /* 0x81    TDS 7.0 only              */
207 #define TDS_CURFETCH_TOKEN        130  /* 0x82    TDS 5.0 only              */
208 #define TDS_CURINFO_TOKEN         131  /* 0x83    TDS 5.0 only              */
209 #define TDS_CUROPEN_TOKEN         132  /* 0x84    TDS 5.0 only              */
210 #define TDS_CURDECLARE_TOKEN      134  /* 0x86    TDS 5.0 only              */
211 #define TDS7_COMPUTE_RESULT_TOKEN 136  /* 0x88    TDS 7.0 only              */
212 #define TDS_COL_NAME_TOKEN        160  /* 0xA0    TDS 4.2 only              */
213 #define TDS_COL_INFO_TOKEN        161  /* 0xA1    TDS 4.2 only - TDS_COLFMT */
214 #define TDS5_DYNAMIC2_TOKEN       163  /* 0xA3    TDS 5.0 only              */
215 #if 0 /* XX: Why commented out ? These are 'live' in FreeTDS tds.h */
216 #define TDS_TABNAME               164  /* 0xA4                              */
217 #define TDS_COL_INFO              165  /* 0xA5                              */
218 #endif
219 #define TDS_OPTIONCMD_TOKEN       166  /* 0xA6 */
220 #define TDS_COMPUTE_NAMES_TOKEN   167  /* 0xA7 */
221 #define TDS_COMPUTE_RESULT_TOKEN  168  /* 0xA8 */
222 #define TDS_ORDER_BY_TOKEN        169  /* 0xA9    TDS_ORDER                 */
223 #define TDS_ERR_TOKEN             170  /* 0xAA                              */
224 #define TDS_MSG_TOKEN             171  /* 0xAB                              */
225 #define TDS_PARAM_TOKEN           172  /* 0xAC    RETURNVALUE?              */
226 #define TDS_LOGIN_ACK_TOKEN       173  /* 0xAD                              */
227 #define TDS_CONTROL_TOKEN         174  /* 0xAE    TDS_CONTROL               */
228 #define TDS_KEY_TOKEN             202  /* 0xCA                        [###] */
229 #define TDS_ROW_TOKEN             209  /* 0xD1                              */
230 #define TDS_CMP_ROW_TOKEN         211  /* 0xD3                              */
231 #define TDS5_PARAMS_TOKEN         215  /* 0xD7    TDS 5.0 only              */
232 #define TDS_CAP_TOKEN             226  /* 0xE2                              */
233 #define TDS_ENV_CHG_TOKEN         227  /* 0xE3                              */
234 #define TDS_EED_TOKEN             229  /* 0xE5                              */
235 #define TDS_DBRPC_TOKEN           230  /* 0xE6                              */
236 #define TDS5_DYNAMIC_TOKEN        231  /* 0xE7    TDS 5.0 only              */
237 #define TDS5_PARAMFMT_TOKEN       236  /* 0xEC    TDS 5.0 only              */
238 #define TDS_AUTH_TOKEN            237  /* 0xED                              */
239 #define TDS_RESULT_TOKEN          238  /* 0xEE                              */
240 #define TDS_DONE_TOKEN            253  /* 0xFD    TDS_DONE                  */
241 #define TDS_DONEPROC_TOKEN        254  /* 0xFE    TDS_DONEPROC              */
242 #define TDS_DONEINPROC_TOKEN      255  /* 0xFF    TDS_DONEINPROC            */
243
244 /* Microsoft internal stored procedure id's */
245
246 #define TDS_SP_CURSOR           1
247 #define TDS_SP_CURSOROPEN       2
248 #define TDS_SP_CURSORPREPARE    3
249 #define TDS_SP_CURSOREXECUTE    4
250 #define TDS_SP_CURSORPREPEXEC   5
251 #define TDS_SP_CURSORUNPREPARE  6
252 #define TDS_SP_CURSORFETCH      7
253 #define TDS_SP_CURSOROPTION     8
254 #define TDS_SP_CURSORCLOSE      9
255 #define TDS_SP_EXECUTESQL      10
256 #define TDS_SP_PREPARE         11
257 #define TDS_SP_EXECUTE         12
258 #define TDS_SP_PREPEXEC        13
259 #define TDS_SP_PREPEXECRPC     14
260 #define TDS_SP_UNPREPARE       15
261
262
263 #define TDS_RPC_OPT_WITH_RECOMP    0x01
264 #define TDS_RPC_OPT_NO_METADATA    0x02
265 #define TDS_RPC_OPT_REUSE_METADATA 0x04
266
267 #define TDS_RPC_PARAMETER_STATUS_BY_REF  0x01
268 #define TDS_RPC_PARAMETER_STATUS_DEFAULT 0x02
269
270 /* Sybase Data Types */
271
272 #define SYBCHAR        47  /* 0x2F */
273 #define SYBVARCHAR     39  /* 0x27 */
274 #define SYBINTN        38  /* 0x26 */
275 #define SYBINT1        48  /* 0x30 */
276 #define SYBINT2        52  /* 0x34 */
277 #define SYBINT4        56  /* 0x38 */
278 #define SYBINT8       127  /* 0x7F */
279 #define SYBFLT8        62  /* 0x3E */
280 #define SYBDATETIME    61  /* 0x3D */
281 #define SYBBIT         50  /* 0x32 */
282 #define SYBTEXT        35  /* 0x23 */
283 #define SYBNTEXT       99  /* 0x63 */
284 #define SYBIMAGE       34  /* 0x22 */
285 #define SYBMONEY4     122  /* 0x7A */
286 #define SYBMONEY       60  /* 0x3C */
287 #define SYBDATETIME4   58  /* 0x3A */
288 #define SYBREAL        59  /* 0x3B */
289 #define SYBBINARY      45  /* 0x2D */
290 #define SYBVOID        31  /* 0x1F */
291 #define SYBVARBINARY   37  /* 0x25 */
292 #define SYBNVARCHAR   103  /* 0x67 */
293 #define SYBBITN       104  /* 0x68 */
294 #define SYBNUMERIC    108  /* 0x6C */
295 #define SYBDECIMAL    106  /* 0x6A */
296 #define SYBFLTN       109  /* 0x6D */
297 #define SYBMONEYN     110  /* 0x6E */
298 #define SYBDATETIMN   111  /* 0x6F */
299 #define XSYBCHAR      175  /* 0xA7 */
300 #define XSYBVARCHAR   167  /* 0xAF */
301 #define XSYBNVARCHAR  231  /* 0xE7 */
302 #define XSYBNCHAR     239  /* 0xEF */
303 #define XSYBVARBINARY 165  /* 0xA5 */
304 #define XSYBBINARY    173  /* 0xAD */
305 #define SYBLONGBINARY 225  /* 0xE1 */
306 #define SYBSINT1       64  /* 0x40 */
307 #define SYBUINT2       65  /* 0x41 */
308 #define SYBUINT4       66  /* 0x42 */
309 #define SYBUINT8       67  /* 0x43 */
310 #define SYBUNIQUE      36  /* 0x24 */
311 #define SYBVARIANT     98  /* 0x62 */
312
313 #define is_fixed_coltype(x) (x==SYBINT1    ||           \
314                              x==SYBINT2      ||         \
315                              x==SYBINT4      ||         \
316                              x==SYBINT8      ||         \
317                              x==SYBREAL       ||        \
318                              x==SYBFLT8      ||         \
319                              x==SYBDATETIME  ||         \
320                              x==SYBDATETIME4 ||         \
321                              x==SYBBIT       ||         \
322                              x==SYBMONEY     ||         \
323                              x==SYBMONEY4    ||         \
324                              x==SYBUNIQUE)
325
326 /* FIXEDLENTYPE */
327 #define TDS_DATA_TYPE_NULL            0x1F  /* Null (no data associated with this type) */
328 #define TDS_DATA_TYPE_INT1            0x30  /* TinyInt (1 byte data representation) */
329 #define TDS_DATA_TYPE_BIT             0x32  /* Bit (1 byte data representation) */
330 #define TDS_DATA_TYPE_INT2            0x34  /* SmallInt (2 byte data representation) */
331 #define TDS_DATA_TYPE_INT4            0x38  /* Int (4 byte data representation) */
332 #define TDS_DATA_TYPE_DATETIM4        0x3A  /* SmallDateTime (4 byte data representation) */
333 #define TDS_DATA_TYPE_FLT4            0x3B  /* Real (4 byte data representation) */
334 #define TDS_DATA_TYPE_MONEY           0x3C  /* Money (8 byte data representation) */
335 #define TDS_DATA_TYPE_DATETIME        0x3D  /* DateTime (8 byte data representation) */
336 #define TDS_DATA_TYPE_FLT8            0x3E  /* Float (8 byte data representation) */
337 #define TDS_DATA_TYPE_MONEY4          0x7A  /* SmallMoney (4 byte data representation) */
338 #define TDS_DATA_TYPE_INT8            0x7F  /* BigInt (8 byte data representation) */
339 /* BYTELEN_TYPE */
340 #define TDS_DATA_TYPE_GUID            0x24  /* UniqueIdentifier */
341 #define TDS_DATA_TYPE_INTN            0x26
342 #define TDS_DATA_TYPE_DECIMAL         0x37  /* Decimal (legacy support) */
343 #define TDS_DATA_TYPE_NUMERIC         0x3F  /* Numeric (legacy support) */
344 #define TDS_DATA_TYPE_BITN            0x68
345 #define TDS_DATA_TYPE_DECIMALN        0x6A  /* Decimal */
346 #define TDS_DATA_TYPE_NUMERICN        0x6C  /* Numeric */
347 #define TDS_DATA_TYPE_FLTN            0x6D
348 #define TDS_DATA_TYPE_MONEYN          0x6E
349 #define TDS_DATA_TYPE_DATETIMN        0x6F
350 #define TDS_DATA_TYPE_DATEN           0x28  /* (introduced in TDS 7.3) */
351 #define TDS_DATA_TYPE_TIMEN           0x29  /* (introduced in TDS 7.3) */
352 #define TDS_DATA_TYPE_DATETIME2N      0x2A  /* (introduced in TDS 7.3) */
353 #define TDS_DATA_TYPE_DATETIMEOFFSETN 0x2B  /* (introduced in TDS 7.3) */
354 #define TDS_DATA_TYPE_CHAR            0x2F  /* Char (legacy support) */
355 #define TDS_DATA_TYPE_VARCHAR         0x27  /* VarChar (legacy support) */
356 #define TDS_DATA_TYPE_BINARY          0x2D  /* Binary (legacy support) */
357 #define TDS_DATA_TYPE_VARBINARY       0x25  /* VarBinary (legacy support) */
358 /* USHORTLEN_TYPE */
359 #define TDS_DATA_TYPE_BIGVARBIN       0xA5  /* VarBinary */
360 #define TDS_DATA_TYPE_BIGVARCHR       0xA7  /* VarChar */
361 #define TDS_DATA_TYPE_BIGBINARY       0xAD  /* Binary */
362 #define TDS_DATA_TYPE_BIGCHAR         0xAF  /* Char */
363 #define TDS_DATA_TYPE_NVARCHAR        0xE7  /* NVarChar */
364 #define TDS_DATA_TYPE_NCHAR           0xEF  /* NChar */
365 /* LONGLEN_TYPE */
366 #define TDS_DATA_TYPE_XML             0xF1  /* XML (introduced in TDS 7.2) */
367 #define TDS_DATA_TYPE_UDT             0xF0  /* CLR-UDT (introduced in TDS 7.2) */
368 #define TDS_DATA_TYPE_TEXT            0x23  /* Text */
369 #define TDS_DATA_TYPE_IMAGE           0x22  /* Image */
370 #define TDS_DATA_TYPE_NTEXT           0x63  /* NText */
371 #define TDS_DATA_TYPE_SSVARIANT       0x62  /* Sql_Variant (introduced in TDS 7.2) */
372
373 static const value_string tds_data_type_names[] = {
374     /* FIXEDLENTYPE */
375     {TDS_DATA_TYPE_NULL,            "NULLTYPE - Null (no data associated with this type)"},
376     {TDS_DATA_TYPE_INT1,            "INT1TYPE - TinyInt (1 byte data representation)"},
377     {TDS_DATA_TYPE_BIT,             "BITTYPE - Bit (1 byte data representation)"},
378     {TDS_DATA_TYPE_INT2,            "INT2TYPE - SmallInt (2 byte data representation)"},
379     {TDS_DATA_TYPE_INT4,            "INT4TYPE - Int (4 byte data representation)"},
380     {TDS_DATA_TYPE_DATETIM4,        "DATETIM4TYPE - SmallDateTime (4 byte data representation)"},
381     {TDS_DATA_TYPE_FLT4,            "FLT4TYPE - Real (4 byte data representation)"},
382     {TDS_DATA_TYPE_MONEY,           "MONEYTYPE - Money (8 byte data representation)"},
383     {TDS_DATA_TYPE_DATETIME,        "DATETIMETYPE - DateTime (8 byte data representation)"},
384     {TDS_DATA_TYPE_FLT8,            "FLT8TYPE - Float (8 byte data representation)"},
385     {TDS_DATA_TYPE_MONEY4,          "MONEY4TYPE - SmallMoney (4 byte data representation)"},
386     {TDS_DATA_TYPE_INT8,            "INT8TYPE - BigInt (8 byte data representation)"},
387     /* BYTELEN_TYPE */
388     {TDS_DATA_TYPE_GUID,            "GUIDTYPE - UniqueIdentifier"},
389     {TDS_DATA_TYPE_INTN,            "INTNTYPE"},
390     {TDS_DATA_TYPE_DECIMAL,         "DECIMALTYPE - Decimal (legacy support)"},
391     {TDS_DATA_TYPE_NUMERIC,         "NUMERICTYPE - Numeric (legacy support)"},
392     {TDS_DATA_TYPE_BITN,            "BITNTYPE"},
393     {TDS_DATA_TYPE_DECIMALN,        "DECIMALNTYPE - Decimal"},
394     {TDS_DATA_TYPE_NUMERICN,        "NUMERICNTYPE - Numeric"},
395     {TDS_DATA_TYPE_FLTN,            "FLTNTYPE"},
396     {TDS_DATA_TYPE_MONEYN,          "MONEYNTYPE"},
397     {TDS_DATA_TYPE_DATETIMN,        "DATETIMNTYPE"},
398     {TDS_DATA_TYPE_DATEN,           "DATENTYPE - (introduced in TDS 7.3)"},
399     {TDS_DATA_TYPE_TIMEN,           "TIMENTYPE - (introduced in TDS 7.3)"},
400     {TDS_DATA_TYPE_DATETIME2N,      "DATETIME2NTYPE - (introduced in TDS 7.3)"},
401     {TDS_DATA_TYPE_DATETIMEOFFSETN, "DATETIMEOFFSETNTYPE - (introduced in TDS 7.3)"},
402     {TDS_DATA_TYPE_CHAR,            "CHARTYPE - Char (legacy support)"},
403     {TDS_DATA_TYPE_VARCHAR,         "VARCHARTYPE - VarChar (legacy support)"},
404     {TDS_DATA_TYPE_BINARY,          "BINARYTYPE - Binary (legacy support)"},
405     {TDS_DATA_TYPE_VARBINARY,       "VARBINARYTYPE - VarBinary (legacy support)"},
406     /* USHORTLEN_TYPE */
407     {TDS_DATA_TYPE_BIGVARBIN,       "BIGVARBINTYPE - VarBinary"},
408     {TDS_DATA_TYPE_BIGVARCHR,       "BIGVARCHRTYPE - VarChar"},
409     {TDS_DATA_TYPE_BIGBINARY,       "BIGBINARYTYPE - Binary"},
410     {TDS_DATA_TYPE_BIGCHAR,         "BIGCHARTYPE - Char"},
411     {TDS_DATA_TYPE_NVARCHAR,        "NVARCHARTYPE - NVarChar"},
412     {TDS_DATA_TYPE_NCHAR,           "NCHARTYPE - NChar"},
413     /* LONGLEN_TYPE */
414     {TDS_DATA_TYPE_XML,             "XMLTYPE - XML (introduced in TDS 7.2)"},
415     {TDS_DATA_TYPE_UDT,             "UDTTYPE - CLR-UDT (introduced in TDS 7.2)"},
416     {TDS_DATA_TYPE_TEXT,            "TEXTTYPE - Text"},
417     {TDS_DATA_TYPE_IMAGE,           "IMAGETYPE - Image"},
418     {TDS_DATA_TYPE_NTEXT,           "NTEXTTYPE - NText"},
419     {TDS_DATA_TYPE_SSVARIANT,       "SSVARIANTTYPE - Sql_Variant (introduced in TDS 7.2)"},
420     {0, NULL }
421 };
422
423 /* Initialize the protocol and registered fields */
424 static int proto_tds = -1;
425 static int hf_tds_type = -1;
426 static int hf_tds_status = -1;
427 static int hf_tds_status_eom = -1;
428 static int hf_tds_status_ignore = -1;
429 static int hf_tds_status_event_notif = -1;
430 static int hf_tds_status_reset_conn = -1;
431 static int hf_tds_status_reset_conn_skip_tran = -1;
432 static int hf_tds_length = -1;
433 static int hf_tds_channel = -1;
434 static int hf_tds_packet_number = -1;
435 static int hf_tds_window = -1;
436 static int hf_tds_reassembled_in = -1;
437 static int hf_tds_reassembled_length = -1;
438 static int hf_tds_fragments = -1;
439 static int hf_tds_fragment = -1;
440 static int hf_tds_fragment_overlap = -1;
441 static int hf_tds_fragment_overlap_conflict = -1;
442 static int hf_tds_fragment_multiple_tails = -1;
443 static int hf_tds_fragment_too_long_fragment = -1;
444 static int hf_tds_fragment_error = -1;
445 static int hf_tds_fragment_count = -1;
446
447 static int hf_tds7_login_total_size = -1;
448 static int hf_tds7_version = -1;
449 static int hf_tds7_packet_size = -1;
450 static int hf_tds7_client_version = -1;
451 static int hf_tds7_client_pid = -1;
452 static int hf_tds7_connection_id = -1;
453 static int hf_tds7_option_flags1 = -1;
454 static int hf_tds7_option_flags2 = -1;
455 static int hf_tds7_sql_type_flags = -1;
456 static int hf_tds7_reserved_flags = -1;
457 static int hf_tds7_time_zone = -1;
458 static int hf_tds7_collation = -1;
459
460 static int hf_tds_all_headers = -1;
461 static int hf_tds_all_headers_total_length = -1;
462 static int hf_tds_all_headers_header_length = -1;
463 static int hf_tds_all_headers_header_type = -1;
464 static int hf_tds_all_headers_trans_descr = -1;
465 static int hf_tds_all_headers_request_cnt = -1;
466
467 static int hf_tds_type_info = -1;
468 static int hf_tds_type_info_type = -1;
469 static int hf_tds_type_info_varlen = -1;
470 static int hf_tds_type_info_precision = -1;
471 static int hf_tds_type_info_scale = -1;
472 static int hf_tds_type_info_collation = -1;
473 static int hf_tds_type_info_collation_lcid = -1;
474 static int hf_tds_type_info_collation_ign_case = -1;
475 static int hf_tds_type_info_collation_ign_accent = -1;
476 static int hf_tds_type_info_collation_ign_kana = -1;
477 static int hf_tds_type_info_collation_ign_width = -1;
478 static int hf_tds_type_info_collation_binary = -1;
479 static int hf_tds_type_info_collation_version = -1;
480 static int hf_tds_type_info_collation_sortid = -1;
481 static int hf_tds_type_varbyte_length = -1;
482 static int hf_tds_type_varbyte_data_null = -1;
483 static int hf_tds_type_varbyte_data_boolean = -1;
484 static int hf_tds_type_varbyte_data_int1 = -1;
485 static int hf_tds_type_varbyte_data_int2 = -1;
486 static int hf_tds_type_varbyte_data_int4 = -1;
487 static int hf_tds_type_varbyte_data_int8 = -1;
488 static int hf_tds_type_varbyte_data_float = -1;
489 static int hf_tds_type_varbyte_data_double = -1;
490 static int hf_tds_type_varbyte_data_bytes = -1;
491 static int hf_tds_type_varbyte_data_guid = -1;
492 static int hf_tds_type_varbyte_data_string = -1;
493 static int hf_tds_type_varbyte_plp_len = -1;
494 static int hf_tds_type_varbyte_plp_chunk_len = -1;
495
496 static int hf_tds_rpc = -1;
497 static int hf_tds_rpc_name_length8 = -1;
498 static int hf_tds_rpc_name_length = -1;
499 static int hf_tds_rpc_name = -1;
500 static int hf_tds_rpc_proc_id = -1;
501 static int hf_tds_rpc_options = -1;
502 static int hf_tds_rpc_options_with_recomp = -1;
503 static int hf_tds_rpc_options_no_metadata = -1;
504 static int hf_tds_rpc_options_reuse_metadata = -1;
505 static int hf_tds_rpc_separator = -1;
506 static int hf_tds_rpc_parameter = -1;
507 static int hf_tds_rpc_parameter_name_length = -1;
508 static int hf_tds_rpc_parameter_name = -1;
509 static int hf_tds_rpc_parameter_status = -1;
510 static int hf_tds_rpc_parameter_status_by_ref = -1;
511 static int hf_tds_rpc_parameter_status_default = -1;
512 static int hf_tds_rpc_parameter_value = -1;
513
514 /* Initialize the subtree pointers */
515 static gint ett_tds = -1;
516 static gint ett_tds_status = -1;
517 static gint ett_tds_fragments = -1;
518 static gint ett_tds_fragment = -1;
519 static gint ett_tds_token = -1;
520 static gint ett_tds_all_headers = -1;
521 static gint ett_tds_all_headers_header = -1;
522 static gint ett_tds_type_info = -1;
523 static gint ett_tds_type_info_collation = -1;
524 static gint ett_tds_type_varbyte = -1;
525 static gint ett_tds_message = -1;
526 static gint ett_tds_rpc_options = -1;
527 static gint ett_tds_rpc_parameter = -1;
528 static gint ett_tds_rpc_parameter_status = -1;
529 static gint ett_tds7_query = -1;
530 static gint ett_tds7_login = -1;
531 static gint ett_tds7_hdr = -1;
532
533 /* Desegmentation of Netlib buffers crossing TCP segment boundaries. */
534 static gboolean tds_desegment = TRUE;
535
536 static const fragment_items tds_frag_items = {
537     &ett_tds_fragment,
538     &ett_tds_fragments,
539     &hf_tds_fragments,
540     &hf_tds_fragment,
541     &hf_tds_fragment_overlap,
542     &hf_tds_fragment_overlap_conflict,
543     &hf_tds_fragment_multiple_tails,
544     &hf_tds_fragment_too_long_fragment,
545     &hf_tds_fragment_error,
546     &hf_tds_fragment_count,
547     &hf_tds_reassembled_in,
548     &hf_tds_reassembled_length,
549     "fragments"
550 };
551
552 /* Tables for reassembly of fragments. */
553 static GHashTable *tds_fragment_table = NULL;
554 static GHashTable *tds_reassembled_table = NULL;
555
556 /* defragmentation of multi-buffer TDS PDUs */
557 static gboolean tds_defragment = TRUE;
558
559 static dissector_handle_t tds_tcp_handle;
560 static dissector_handle_t ntlmssp_handle;
561 static dissector_handle_t gssapi_handle;
562 static dissector_handle_t data_handle;
563
564 /* TDS protocol type preference */
565 /*   XXX: This preference is used as a 'hint' for cases where interpretation is ambiguous */
566 /*        Currently the hint is global                                                    */
567 /*   TODO: Consider storing protocol type with each conversation                          */
568 /*        (when type is determined and using the preference as a default) ??              */
569
570 #define TDS_PROTOCOL_NOT_SPECIFIED   0
571 #define TDS_PROTOCOL_4      0x40
572 #define TDS_PROTOCOL_5      0x50
573 #define TDS_PROTOCOL_7_0    0x70
574 #define TDS_PROTOCOL_7_1    0x71
575 #define TDS_PROTOCOL_7_2    0x72
576 #define TDS_PROTOCOL_7_3    0x73
577
578 static gint tds_protocol_type = TDS_PROTOCOL_NOT_SPECIFIED;
579
580 static const enum_val_t tds_protocol_type_options[] = {
581     {"not_specified", "Not Specified", TDS_PROTOCOL_NOT_SPECIFIED},
582     {"tds4", "TDS 4", TDS_PROTOCOL_4},  /* TDS 4.2 and TDS 4.6 */
583     {"tds5", "TDS 5", TDS_PROTOCOL_5},
584     {"tds70", "TDS 7.0", TDS_PROTOCOL_7_0},
585     {"tds71", "TDS 7.1", TDS_PROTOCOL_7_1},
586     {"tds72", "TDS 7.2", TDS_PROTOCOL_7_2},
587     {"tds73", "TDS 7.3", TDS_PROTOCOL_7_3},
588     {NULL, NULL, -1}
589 };
590
591 #define TDS_PROTO_PREF_NOT_SPECIFIED (tds_protocol_type == TDS_NOT_SPECIFIED)
592 #define TDS_PROTO_PREF_TDS4 (tds_protocol_type == TDS_PROTOCOL_4)
593 #define TDS_PROTO_PREF_TDS5 (tds_protocol_type == TDS_PROTOCOL_5)
594 #define TDS_PROTO_PREF_TDS7_0 (tds_protocol_type == TDS_PROTOCOL_7_0)
595 #define TDS_PROTO_PREF_TDS7_1 (tds_protocol_type == TDS_PROTOCOL_7_1)
596 #define TDS_PROTO_PREF_TDS7_2 (tds_protocol_type == TDS_PROTOCOL_7_2)
597 #define TDS_PROTO_PREF_TDS7_3 (tds_protocol_type == TDS_PROTOCOL_7_3)
598 #define TDS_PROTO_PREF_TDS7 (tds_protocol_type >= TDS_PROTOCOL_7_0 && tds_protocol_type <= TDS_PROTOCOL_7_3)
599
600 /* TDS "endian type" */
601 /*   XXX: Assumption is that all TDS conversations being decoded in a particular capture */
602 /*        have the same endian type                                                      */
603 /*   TODO: consider storing endian type with each conversation                           */
604 /*         (using pref as the default)                                                   */
605
606 static gboolean tds_little_endian = TRUE;
607
608 static const enum_val_t tds_endian_type_options[] = {
609     {"little_endian", "Little Endian", TRUE},
610     {"big_endian"   , "Big Endian"   , FALSE},
611     {NULL, NULL, -1}
612 };
613
614
615 /* TCP port preferences for TDS decode */
616
617 static range_t *tds_tcp_ports = NULL;
618
619 /* These correspond to the netlib packet type field */
620 static const value_string packet_type_names[] = {
621     {TDS_QUERY_PKT,     "SQL batch"},
622     {TDS_LOGIN_PKT,     "Pre-TDS7 login"},
623     {TDS_RPC_PKT,       "Remote Procedure Call"},
624     {TDS_RESP_PKT,      "Response"},
625     {TDS_RAW_PKT,       "Unused"},
626     {TDS_ATTENTION_PKT, "Attention"},
627     {TDS_BULK_DATA_PKT, "Bulk load data"},
628     {TDS_QUERY5_PKT,    "TDS5 query"},
629     {TDS_LOGIN7_PKT,    "TDS7 login"},
630     {TDS_SSPI_PKT,      "SSPI message"},
631     {TDS_PRELOGIN_PKT,  "TDS7 pre-login message"},
632     {0, NULL}
633 };
634
635 enum {
636     TDS_HEADER_QUERY_NOTIF = 0x0001,
637     TDS_HEADER_TRANS_DESCR = 0x0002
638 };
639
640 static const value_string header_type_names[] = {
641     {TDS_HEADER_QUERY_NOTIF, "Query notifications"},
642     {TDS_HEADER_TRANS_DESCR, "Transaction descriptor"},
643     {0, NULL}
644 };
645
646 /* The status field */
647
648 #define is_valid_tds_status(x) ((x) <= STATUS_EVENT_NOTIFICATION)
649
650 #define STATUS_LAST_BUFFER              0x01
651 #define STATUS_IGNORE_EVENT             0x02
652 #define STATUS_EVENT_NOTIFICATION       0x04
653 #define STATUS_RESETCONNECTION          0x08
654 #define STATUS_RESETCONNECTIONSKIPTRAN  0x10
655
656 /* The one byte token at the start of each TDS PDU */
657 static const value_string token_names[] = {
658     {TDS5_DYNAMIC_TOKEN,        "TDS5 Dynamic SQL"},
659     {TDS5_PARAMFMT_TOKEN,       "TDS5 Parameter Format"},
660     {TDS5_PARAMFMT2_TOKEN,      "TDS5 Parameter2 Format"},
661     {TDS5_PARAMS_TOKEN,         "TDS5 Parameters"},
662     {TDS_LANG_TOKEN,            "Language"},
663     {TDS_LOGOUT_TOKEN,          "Logout"},
664     {TDS_RET_STAT_TOKEN,        "Return Status"},
665     {TDS_PROCID_TOKEN,          "Proc ID"},
666     {TDS7_RESULT_TOKEN,         "TDS7+ Results"},
667     {TDS_COL_NAME_TOKEN,        "Column Names"},
668     {TDS_COL_INFO_TOKEN,        "Column Info"},
669     {TDS_COMPUTE_NAMES_TOKEN,   "Compute Names"},
670     {TDS_COMPUTE_RESULT_TOKEN,  "Compute Results"},
671     {TDS_ORDER_BY_TOKEN,        "Order By"},
672     {TDS_ERR_TOKEN,             "Error Message"},
673     {TDS_MSG_TOKEN,             "Info Message"},
674     {TDS_PARAM_TOKEN,           "Parameter"},
675     {TDS_LOGIN_ACK_TOKEN,       "Login Acknowledgement"},
676     {TDS_CONTROL_TOKEN,         "TDS Control"},
677     {TDS_KEY_TOKEN,             "TDS Key"},
678     {TDS_ROW_TOKEN,             "Row"},
679     {TDS_CMP_ROW_TOKEN,         "Compute Row"},
680     {TDS_CAP_TOKEN,             "Capabilities"},
681     {TDS_ENV_CHG_TOKEN,         "Environment Change"},
682     {TDS_EED_TOKEN,             "Extended Error"},
683     {TDS_AUTH_TOKEN,            "Authentication"},
684     {TDS_RESULT_TOKEN,          "Results"},
685     {TDS_DONE_TOKEN,            "Done"},
686     {TDS_DONEPROC_TOKEN,        "Done Proc"},
687     {TDS_DONEINPROC_TOKEN,      "Done In Proc"},
688     {TDS5_DYNAMIC2_TOKEN,       "TDS5 Dynamic2"},
689     {TDS5_ORDERBY2_TOKEN,       "TDS5 OrderBy2"},
690     {TDS5_CURDECLARE2_TOKEN,    "TDS5 CurDeclare2"},
691     {TDS5_ROWFMT2_TOKEN,        "TDS5 RowFmt2"},
692     {TDS5_MSG_TOKEN,            "TDS5 Msg"},
693     {0, NULL}
694 };
695
696 #define TDS_RPC_SEPARATOR_BATCH_FLAG            0x80
697 #define TDS_RPC_SEPARATOR_BATCH_FLAG_7_2        0xFF
698 #define TDS_RPC_SEPARATOR_NO_EXEC_FLAG          0xFE
699
700 static const value_string tds_rpc_separators[] = {
701     {TDS_RPC_SEPARATOR_BATCH_FLAG,     "Batch flag"},
702     {TDS_RPC_SEPARATOR_BATCH_FLAG_7_2, "Batch flag 7.2"},
703     {TDS_RPC_SEPARATOR_NO_EXEC_FLAG,   "No exec flag"},
704     {0, NULL }
705 };
706
707 static const value_string internal_stored_proc_id_names[] = {
708     {TDS_SP_CURSOR,          "sp_cursor"         },
709     {TDS_SP_CURSOROPEN,      "sp_cursoropen"     },
710     {TDS_SP_CURSORPREPARE,   "sp_cursorprepare"  },
711     {TDS_SP_CURSOREXECUTE,   "sp_cursorexecute"  },
712     {TDS_SP_CURSORPREPEXEC,  "sp_cursorprepexec" },
713     {TDS_SP_CURSORUNPREPARE, "sp_cursorunprepare"},
714     {TDS_SP_CURSORFETCH,     "sp_cursorfetch"    },
715     {TDS_SP_CURSOROPTION,    "sp_cursoroption"   },
716     {TDS_SP_CURSORCLOSE,     "sp_cursorclose"    },
717     {TDS_SP_EXECUTESQL,      "sp_executesql"     },
718     {TDS_SP_PREPARE,         "sp_prepare"        },
719     {TDS_SP_EXECUTE,         "sp_execute"        },
720     {TDS_SP_PREPEXEC,        "sp_prepexec"       },
721     {TDS_SP_PREPEXECRPC,     "sp_prepexecrpc"    },
722     {TDS_SP_UNPREPARE,       "sp_unprepare"      },
723     {0,                      NULL                },
724 };
725
726 static const value_string env_chg_names[] = {
727     {1, "Database"},
728     {2, "Language"},
729     {3, "Sort Order"},
730     {4, "Blocksize"},
731     {5, "Unicode Locale ID"},
732     {6, "Unicode Comparison Style"},
733     {7, "Collation Info"},
734     {0, NULL},
735 };
736
737 static const value_string login_field_names[] = {
738     {0, "Client Name"},
739     {1, "Username"},
740     {2, "Password"},
741     {3, "App Name"},
742     {4, "Server Name"},
743     {5, "Unknown1"},
744     {6, "Library Name"},
745     {7, "Locale"},
746     {8, "Database Name"},
747     {0, NULL},
748 };
749
750
751 #define MAX_COLUMNS 256
752
753 /*
754  * This is where we store the column information to be used in decoding the
755  * TDS_ROW_TOKEN tokens.
756  */
757 struct _tds_col {
758     gchar name[256];
759     guint16 utype;
760     guint8 ctype;
761     guint csize;
762 };
763
764 struct _netlib_data {
765     guint num_cols;
766     struct _tds_col *columns[MAX_COLUMNS];
767 };
768
769 struct tds7_login_packet_hdr {
770     guint32 total_packet_size;
771     guint32 tds_version;
772     guint32 packet_size;
773     guint32 client_version;
774     guint32 client_pid;
775     guint32 connection_id;
776     guint8  option_flags1;
777     guint8  option_flags2;
778     guint8  sql_type_flags;
779     guint8  reserved_flags;
780     guint32 time_zone;
781     guint32 collation;
782 };
783
784 /* support routines */
785
786 static void
787 dissect_tds_nt(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
788                guint offset, guint length)
789 {
790     tvbuff_t *nt_tvb;
791
792     nt_tvb = tvb_new_subset(tvb, offset, -1, length);
793     if(tvb_strneql(tvb, offset, "NTLMSSP", 7) == 0)
794         call_dissector(ntlmssp_handle, nt_tvb, pinfo, tree);
795     else
796         call_dissector(gssapi_handle, nt_tvb, pinfo, tree);
797 }
798
799 /*  */
800
801 static guint16
802 tds_tvb_get_xxtohs(tvbuff_t *tvb, gint offset, gboolean tds_little_endian_flag) {
803     if (tds_little_endian_flag)
804         return tvb_get_letohs(tvb, offset);
805     else
806         return tvb_get_ntohs(tvb, offset);
807 }
808
809 static guint32
810 tds_tvb_get_xxtohl(tvbuff_t *tvb, gint offset, gboolean tds_little_endian_flag) {
811     if (tds_little_endian_flag)
812         return tvb_get_letohl(tvb, offset);
813     else
814         return tvb_get_ntohl(tvb, offset);
815 }
816
817
818 static int
819 tds_token_is_fixed_size(guint8 token)
820 {
821     switch (token) {
822         case TDS_DONE_TOKEN:
823         case TDS_DONEPROC_TOKEN:
824         case TDS_DONEINPROC_TOKEN:
825         case TDS_RET_STAT_TOKEN:
826         case TDS7_RESULT_TOKEN:
827         case TDS_PROCID_TOKEN:
828         case TDS_LOGOUT_TOKEN:
829             return 1;
830         default:
831             return 0;
832     }
833 }
834
835
836 static int
837 tds_get_fixed_token_size(guint8 token)
838 {
839     switch(token) {
840         case TDS_DONE_TOKEN:
841         case TDS_DONEPROC_TOKEN:
842         case TDS_DONEINPROC_TOKEN:
843         case TDS_PROCID_TOKEN:
844             return 8;
845         case TDS_RET_STAT_TOKEN:
846             return 4;
847         case TDS_LOGOUT_TOKEN:
848             return 1;
849         case TDS7_RESULT_TOKEN:
850         default:
851             return 0;
852     }
853 }
854
855 static guint
856 tds_get_variable_token_size(tvbuff_t *tvb, gint offset, guint8 token,
857                             guint *len_field_size_p, guint *len_field_val_p)
858 {
859     switch(token) {
860         /* some tokens have a 4 byte length field */
861         case TDS5_PARAMFMT2_TOKEN:
862         case TDS_LANG_TOKEN:
863         case TDS5_ORDERBY2_TOKEN:
864         case TDS5_CURDECLARE2_TOKEN:
865         case TDS5_ROWFMT2_TOKEN:
866         case TDS5_DYNAMIC2_TOKEN:
867             *len_field_size_p = 4;
868             *len_field_val_p = tds_tvb_get_xxtohl(tvb, offset, tds_little_endian);
869             break;
870             /* some have a 1 byte length field */
871         case TDS5_MSG_TOKEN:
872             *len_field_size_p = 1;
873             *len_field_val_p = tvb_get_guint8(tvb, offset);
874             break;
875             /* and most have a 2 byte length field */
876         default:
877             *len_field_size_p = 2;
878             *len_field_val_p = tds_tvb_get_xxtohs(tvb, offset, tds_little_endian);
879             break;
880     }
881     return *len_field_val_p + *len_field_size_p + 1;
882 }
883
884
885 static void
886 dissect_tds_all_headers(tvbuff_t *tvb, guint *offset, packet_info *pinfo, proto_tree *tree)
887 {
888     proto_item *item = NULL, *total_length_item = NULL;
889     proto_tree *sub_tree = NULL;
890     guint32 total_length;
891     guint final_offset;
892
893     total_length = tvb_get_letohl(tvb, *offset);
894     /* Try to find out heuristically whether the ALL_HEADERS rule is actually present.
895      * In practice total_length is a single byte value, so if the extracted value exceeds 1 byte,
896      * then the headers are most likely absent. */
897     if(total_length >= 0x100)
898         return;
899     item = proto_tree_add_item(tree, hf_tds_all_headers, tvb, *offset, total_length, ENC_NA);
900     sub_tree = proto_item_add_subtree(item, ett_tds_all_headers);
901     total_length_item = proto_tree_add_item(sub_tree, hf_tds_all_headers_total_length, tvb, *offset, 4, ENC_LITTLE_ENDIAN);
902
903     final_offset = *offset + total_length;
904     *offset += 4;
905     do {
906         /* dissect a stream header */
907         proto_tree *header_sub_tree = NULL;
908         proto_item *length_item = NULL, *type_item = NULL;
909         guint32 header_length;
910         guint16 header_type;
911
912         header_length = tvb_get_letohl(tvb, *offset);
913         item = proto_tree_add_text(sub_tree, tvb, *offset, header_length, "Header");
914         header_sub_tree = proto_item_add_subtree(item, ett_tds_all_headers_header);
915         length_item = proto_tree_add_item(header_sub_tree, hf_tds_all_headers_header_length, tvb, *offset, 4, ENC_LITTLE_ENDIAN);
916         if(header_length == 0 ) {
917             expert_add_info_format(pinfo, length_item, PI_MALFORMED, PI_ERROR, "Empty header");
918             break;
919         }
920
921         header_type = tvb_get_letohs(tvb, *offset + 4);
922         type_item = proto_tree_add_item(header_sub_tree, hf_tds_all_headers_header_type, tvb, *offset + 4, 2, ENC_LITTLE_ENDIAN);
923
924         switch(header_type) {
925             case TDS_HEADER_QUERY_NOTIF:
926                 break;
927             case TDS_HEADER_TRANS_DESCR:
928                 if(header_length != 18)
929                     expert_add_info_format(pinfo, length_item, PI_MALFORMED, PI_ERROR, "Length should equal 18");
930                 proto_tree_add_item(header_sub_tree, hf_tds_all_headers_trans_descr, tvb, *offset + 6, 8, ENC_LITTLE_ENDIAN);
931                 proto_tree_add_item(header_sub_tree, hf_tds_all_headers_request_cnt, tvb, *offset + 14, 4, ENC_LITTLE_ENDIAN);
932                 break;
933             default:
934                 expert_add_info_format(pinfo, type_item, PI_MALFORMED, PI_ERROR, "Invalid header type");
935         }
936
937         *offset += header_length;
938     } while(*offset < final_offset);
939     if(*offset != final_offset) {
940         expert_add_info_format(pinfo, total_length_item, PI_MALFORMED, PI_ERROR,
941                                "Sum of headers' lengths (%d) differs from total headers length (%d)",
942                                total_length + *offset - final_offset, total_length);
943         return;
944     }
945 }
946
947
948 static void
949 dissect_tds_query_packet(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
950 {
951     guint offset, len;
952     gboolean is_unicode = TRUE;
953     char *msg;
954
955     proto_item *query_hdr;
956     proto_tree *query_tree;
957
958     offset = 0;
959     query_hdr = proto_tree_add_text(tree, tvb, offset, -1, "TDS Query Packet");
960     query_tree = proto_item_add_subtree(query_hdr, ett_tds7_query);
961     dissect_tds_all_headers(tvb, &offset, pinfo, query_tree);
962     len = tvb_reported_length_remaining(tvb, offset);
963
964     if (TDS_PROTO_PREF_TDS4 ||
965         (!TDS_PROTO_PREF_TDS7 &&
966          ((len < 2) || tvb_get_guint8(tvb, offset+1) != 0)))
967         is_unicode = FALSE;
968
969     if (is_unicode)
970         msg = tvb_get_ephemeral_unicode_string(tvb, offset, len, ENC_LITTLE_ENDIAN);
971     else
972         msg = (gchar*)tvb_get_ephemeral_string(tvb, offset, len);
973
974     proto_tree_add_text(query_tree, tvb, offset, len, "Query: %s", msg);
975     offset += len;
976 }
977
978
979 static void
980 dissect_tds5_lang_token(tvbuff_t *tvb, guint offset, guint len, proto_tree *tree) {
981     gboolean is_unicode = FALSE;
982     char *msg;
983
984     proto_tree_add_text(tree, tvb, offset, 1 , "Status: %u", tvb_get_guint8(tvb, offset));
985     offset += 1;
986     len    -= 1;
987
988     if (is_unicode)
989         msg = tvb_get_ephemeral_unicode_string(tvb, offset, len, ENC_LITTLE_ENDIAN);
990     else
991         msg = (gchar*)tvb_get_ephemeral_string(tvb, offset, len);
992
993     proto_tree_add_text(tree, tvb, offset, len, "Language text: %s", msg);
994 }
995
996 static void
997 dissect_tds_query5_packet(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
998 {
999     guint offset;
1000     guint pos;
1001     guint token_len_field_size = 2;
1002     guint token_len_field_val = 0;
1003     guint8 token;
1004     guint token_sz;
1005     proto_item *query_hdr;
1006     proto_tree *query_tree;
1007     proto_item *token_item;
1008     proto_tree *token_tree;
1009
1010     offset = 0;
1011     query_hdr = proto_tree_add_text(tree, tvb, offset, -1, "TDS5 Query Packet");
1012     query_tree = proto_item_add_subtree(query_hdr, ett_tds7_query);
1013
1014     /*
1015      * Until we reach the end of the packet, read tokens.
1016      */
1017     pos = offset;
1018     while (tvb_reported_length_remaining(tvb, pos) > 0) {
1019
1020         /* our token */
1021         token = tvb_get_guint8(tvb, pos);
1022         if (tds_token_is_fixed_size(token))
1023             token_sz = tds_get_fixed_token_size(token) + 1;
1024         else
1025             token_sz = tds_get_variable_token_size(tvb, pos+1, token, &token_len_field_size,
1026                                                    &token_len_field_val);
1027
1028         /* XXX - Should this check be done in tds_get_variable_token_size()
1029          * instead? */
1030         if ((int) token_sz < 0) {
1031             proto_tree_add_text(query_tree, tvb, 0, 0, "Bogus token size: %u",
1032                                 token_sz);
1033             break;
1034         }
1035
1036         token_item = proto_tree_add_text(query_tree, tvb, pos, token_sz,
1037                                          "Token 0x%02x %s", token,
1038                                          val_to_str(token, token_names, "Unknown Token Type"));
1039         token_tree = proto_item_add_subtree(token_item, ett_tds_token);
1040
1041         /*
1042          * If it's a variable token, put the length field in here
1043          * instead of replicating this for each token subdissector.
1044          */
1045         if (!tds_token_is_fixed_size(token))
1046             proto_tree_add_text(token_tree, tvb, pos+1, token_len_field_size, "Length: %u", token_len_field_val);
1047
1048         switch (token) {
1049             case TDS_LANG_TOKEN:
1050                 dissect_tds5_lang_token(tvb, pos + 5, token_sz -5, token_tree);
1051                 break;
1052             default:
1053                 break;
1054         }
1055
1056         pos += token_sz;
1057
1058     }  /* while */
1059 }
1060
1061
1062 static void
1063 dissect_tds7_login(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1064 {
1065     guint offset, i, j, k, offset2, len;
1066     char *val, *val2;
1067
1068     proto_item *login_hdr;
1069     proto_tree *login_tree;
1070     proto_item *header_hdr;
1071     proto_tree *header_tree;
1072     proto_item *length_hdr;
1073     proto_tree *length_tree;
1074
1075     struct tds7_login_packet_hdr td7hdr;
1076     gint length_remaining;
1077
1078
1079     /* create display subtree for the protocol */
1080     offset = 0;
1081     login_hdr = proto_tree_add_text(tree, tvb, offset, -1, "TDS7 Login Packet");
1082     login_tree = proto_item_add_subtree(login_hdr, ett_tds7_login);
1083     header_hdr = proto_tree_add_text(login_tree, tvb, offset, 36, "Login Packet Header");
1084     header_tree = proto_item_add_subtree(header_hdr, ett_tds7_hdr);
1085
1086     td7hdr.total_packet_size = tvb_get_letohl(tvb, offset);
1087     proto_tree_add_uint(header_tree, hf_tds7_login_total_size, tvb, offset,
1088                         sizeof(td7hdr.total_packet_size), td7hdr.total_packet_size);
1089     offset += sizeof(td7hdr.total_packet_size);
1090
1091     td7hdr.tds_version = tvb_get_ntohl(tvb, offset);
1092     proto_tree_add_uint(header_tree, hf_tds7_version, tvb, offset, sizeof(td7hdr.tds_version), td7hdr.tds_version);
1093     offset += sizeof(td7hdr.tds_version);
1094
1095     td7hdr.packet_size = tvb_get_ntohl(tvb, offset);
1096     proto_tree_add_uint(header_tree, hf_tds7_packet_size, tvb, offset, sizeof(td7hdr.packet_size), td7hdr.packet_size);
1097     offset += sizeof(td7hdr.packet_size);
1098
1099     td7hdr.client_version = tvb_get_ntohl(tvb, offset);
1100     proto_tree_add_uint(header_tree, hf_tds7_client_version, tvb, offset, sizeof(td7hdr.client_version), td7hdr.client_version);
1101     offset += sizeof(td7hdr.client_version);
1102
1103     td7hdr.client_pid = tvb_get_letohl(tvb, offset);
1104     proto_tree_add_uint(header_tree, hf_tds7_client_pid, tvb, offset, sizeof(td7hdr.client_pid), td7hdr.client_pid);
1105     offset += sizeof(td7hdr.client_pid);
1106
1107     td7hdr.connection_id= tvb_get_letohl(tvb, offset);
1108     proto_tree_add_uint(header_tree, hf_tds7_connection_id, tvb, offset, sizeof(td7hdr.connection_id), td7hdr.connection_id);
1109     offset += sizeof(td7hdr.connection_id);
1110
1111     td7hdr.option_flags1 = tvb_get_guint8(tvb, offset);
1112     proto_tree_add_uint(header_tree, hf_tds7_option_flags1, tvb, offset, sizeof(td7hdr.option_flags1), td7hdr.option_flags1);
1113     offset += sizeof(td7hdr.option_flags1);
1114
1115     td7hdr.option_flags2 = tvb_get_guint8(tvb, offset);
1116     proto_tree_add_uint(header_tree, hf_tds7_option_flags2, tvb, offset, sizeof(td7hdr.option_flags2), td7hdr.option_flags2);
1117     offset += sizeof(td7hdr.option_flags2);
1118
1119     td7hdr.sql_type_flags = tvb_get_guint8(tvb, offset);
1120     proto_tree_add_uint(header_tree, hf_tds7_sql_type_flags, tvb, offset, sizeof(td7hdr.sql_type_flags), td7hdr.sql_type_flags);
1121     offset += sizeof(td7hdr.sql_type_flags);
1122
1123     td7hdr.reserved_flags = tvb_get_guint8(tvb, offset);
1124     proto_tree_add_uint(header_tree, hf_tds7_reserved_flags, tvb, offset, sizeof(td7hdr.reserved_flags), td7hdr.reserved_flags);
1125     offset += sizeof(td7hdr.reserved_flags);
1126
1127     td7hdr.time_zone = tvb_get_ntohl(tvb, offset);
1128     proto_tree_add_uint(header_tree, hf_tds7_time_zone, tvb, offset, sizeof(td7hdr.time_zone), td7hdr.time_zone);
1129     offset += sizeof(td7hdr.time_zone);
1130
1131     td7hdr.collation = tvb_get_ntohl(tvb, offset);
1132     proto_tree_add_uint(header_tree, hf_tds7_collation, tvb, offset, sizeof(td7hdr.collation), td7hdr.collation);
1133     offset += sizeof(td7hdr.collation);
1134
1135     length_hdr = proto_tree_add_text(login_tree, tvb, offset, 50, "Lengths and offsets");
1136     length_tree = proto_item_add_subtree(length_hdr, ett_tds7_hdr);
1137
1138     for (i = 0; i < 9; i++) {
1139         offset2 = tvb_get_letohs(tvb, offset + i*4);
1140         len = tvb_get_letohs(tvb, offset + i*4 + 2);
1141         proto_tree_add_text(length_tree, tvb, offset + i*4, 2,
1142                             "%s offset: %u",
1143                             val_to_str(i, login_field_names, "Unknown"),
1144                             offset2);
1145         proto_tree_add_text(length_tree, tvb, offset + i*4 + 2, 2,
1146                             "%s length: %u",
1147                             val_to_str(i, login_field_names, "Unknown"),
1148                             len);
1149         if (len != 0) {
1150             if( i != 2) {
1151                 /* tds 7 is always unicode */
1152                 len *= 2;
1153                 val = tvb_get_ephemeral_unicode_string(tvb, offset2, len, ENC_LITTLE_ENDIAN);
1154                 proto_tree_add_text(login_tree, tvb, offset2, len, "%s: %s", val_to_str(i, login_field_names, "Unknown"), val);
1155             } else {
1156                 /* This field is the password.  We retrieve it from the packet
1157                  * as a non-unicode string and then perform two operations on it
1158                  * to "decrypt" it.  Finally, we create a new string that consists
1159                  * of ASCII characters instead of unicode by skipping every other
1160                  * byte in the original string.
1161                  */
1162
1163                 len *= 2;
1164                 val = (gchar*)tvb_get_ephemeral_string(tvb, offset2, len);
1165                 val2 = g_malloc((len/2)+1);
1166
1167                 for(j = 0, k = 0; j < len; j += 2, k++) {
1168                     val[j] ^= 0xA5;
1169
1170                     /* Swap the most and least significant bits */
1171                     val[j] = ((val[j] & 0x0F) << 4) | ((val[j] & 0xF0) >> 4);
1172
1173                     val2[k] = val[j];
1174                 }
1175                 val2[k] = '\0'; /* Null terminate our new string */
1176
1177                 proto_tree_add_text(login_tree, tvb, offset2, len, "%s: %s", val_to_str(i, login_field_names, "Unknown"), val2);
1178                 g_free(val2);
1179             }
1180         }
1181     }
1182
1183     /*
1184      * XXX - what about the client MAC address, etc.?
1185      */
1186     length_remaining = tvb_reported_length_remaining(tvb, offset2 + len);
1187     if (length_remaining > 0) {
1188         dissect_tds_nt(tvb, pinfo, login_tree, offset2 + len,
1189                        length_remaining);
1190     }
1191 }
1192
1193 static int
1194 get_size_by_coltype(int servertype)
1195 {
1196     switch(servertype)
1197     {
1198         case SYBINT1:        return 1;
1199         case SYBINT2:        return 2;
1200         case SYBINT4:        return 4;
1201         case SYBINT8:        return 8;
1202         case SYBREAL:        return 4;
1203         case SYBFLT8:        return 8;
1204         case SYBDATETIME:    return 8;
1205         case SYBDATETIME4:   return 4;
1206         case SYBBIT:         return 1;
1207         case SYBBITN:        return 1;
1208         case SYBMONEY:       return 8;
1209         case SYBMONEY4:      return 4;
1210         case SYBUNIQUE:      return 16;
1211         default:             return -1;
1212     }
1213 }
1214 # if 0
1215 /*
1216  * data_to_string should take column data and turn it into something we can
1217  * display on the tree.
1218  */
1219 static char *data_to_string(void *data, guint col_type, guint col_size)
1220 {
1221     char *result;
1222     guint i;
1223
1224     result=ep_alloc(256);
1225     switch(col_type) {
1226         case SYBVARCHAR:
1227             /* strncpy(result, (char *)data, col_size); */
1228             for (i=0;i<col_size && i<(256-1);i++)
1229                 if (!isprint(((char *)data)[i])) result[i]='.';
1230                 else result[i]=((char *)data)[i];
1231             result[i] = '\0';
1232             break;
1233         case SYBINT2:
1234             g_snprintf(result, 256, "%d", *(short *)data);
1235             break;
1236         case SYBINT4:
1237             g_snprintf(result, 256, "%d", *(int *)data);
1238             break;
1239         default:
1240             g_snprintf(result, 256, "Unexpected column_type %d", col_type);
1241             break;
1242     }
1243     return result;
1244 }
1245 #endif
1246
1247 /*
1248  * Since rows are special PDUs in that they are not fixed and lack a size field,
1249  * the length must be computed using the column information seen in the result
1250  * PDU. This function does just that.
1251  */
1252 static guint
1253 tds_get_row_size(tvbuff_t *tvb, struct _netlib_data *nl_data, guint offset)
1254 {
1255     guint cur, i, csize;
1256
1257     cur = offset;
1258     for (i = 0; i < nl_data->num_cols; i++) {
1259         if (!is_fixed_coltype(nl_data->columns[i]->ctype)) {
1260             csize = tvb_get_guint8(tvb, cur);
1261             cur++;
1262         } else
1263             csize = get_size_by_coltype(nl_data->columns[i]->ctype);
1264         cur += csize;
1265     }
1266
1267     return (cur - offset + 1);
1268 }
1269
1270 /*
1271  * Process TDS 4 "COL_INFO" token and store relevant information in the
1272  * _netlib_data structure for later use (see tds_get_row_size)
1273  *
1274  * XXX Can TDS 4 be "big-endian" ? we'll assume yes.
1275  *
1276  */
1277 static gboolean
1278 dissect_tds_col_info_token(tvbuff_t *tvb, struct _netlib_data *nl_data, guint offset)
1279 {
1280     guint next, cur;
1281     guint col;
1282
1283     next = offset + tds_tvb_get_xxtohs(tvb, offset+1, tds_little_endian) + 3;
1284     cur = offset + 3;
1285
1286     col = 0;
1287     while (cur < next) {
1288
1289         if (col >= MAX_COLUMNS) {
1290             nl_data->num_cols = 0;
1291             return FALSE;
1292         }
1293
1294         nl_data->columns[col] = ep_alloc(sizeof(struct _tds_col));
1295
1296         nl_data->columns[col]->name[0] ='\0';
1297
1298         nl_data->columns[col]->utype = tds_tvb_get_xxtohs(tvb, cur, tds_little_endian);
1299         cur += 2;
1300
1301         cur += 2; /* unknown */
1302
1303         nl_data->columns[col]->ctype = tvb_get_guint8(tvb,cur);
1304         cur++;
1305
1306         if (!is_fixed_coltype(nl_data->columns[col]->ctype)) {
1307             nl_data->columns[col]->csize = tvb_get_guint8(tvb,cur);
1308             cur ++;
1309         } else {
1310             nl_data->columns[col]->csize =
1311                 get_size_by_coltype(nl_data->columns[col]->ctype);
1312         }
1313
1314         col += 1;
1315
1316     } /* while */
1317
1318     nl_data->num_cols = col;
1319     return TRUE;
1320 }
1321
1322
1323 /*
1324  * Read the results token and store the relevant information in the
1325  * _netlib_data structure for later use (see tds_get_row_size).
1326  *
1327  * TODO: check we don't go past end of the token
1328  */
1329 static gboolean
1330 read_results_tds5(tvbuff_t *tvb, struct _netlib_data *nl_data, guint offset, guint len _U_)
1331 {
1332     guint name_len;
1333     guint cur;
1334     guint i;
1335
1336     cur = offset;
1337
1338     /*
1339      * This would be the logical place to check for little/big endianess
1340      * if we didn't see the login packet.
1341      * XXX: We'll take a hint
1342      */
1343     nl_data->num_cols = tds_tvb_get_xxtohs(tvb, cur, tds_little_endian);
1344     if (nl_data->num_cols > MAX_COLUMNS) {
1345         nl_data->num_cols = 0;
1346         return FALSE;
1347     }
1348
1349     cur += 2;
1350
1351     for (i = 0; i < nl_data->num_cols; i++) {
1352         nl_data->columns[i] = ep_alloc(sizeof(struct _tds_col));
1353         name_len = tvb_get_guint8(tvb,cur);
1354         cur ++;
1355         cur += name_len;
1356
1357         cur++; /* unknown */
1358
1359         nl_data->columns[i]->utype = tds_tvb_get_xxtohs(tvb, cur, tds_little_endian);
1360         cur += 2;
1361
1362         cur += 2; /* unknown */
1363
1364         nl_data->columns[i]->ctype = tvb_get_guint8(tvb,cur);
1365         cur++;
1366
1367         if (!is_fixed_coltype(nl_data->columns[i]->ctype)) {
1368             nl_data->columns[i]->csize = tvb_get_guint8(tvb,cur);
1369             cur ++;
1370         } else {
1371             nl_data->columns[i]->csize =
1372                 get_size_by_coltype(nl_data->columns[i]->ctype);
1373         }
1374         cur++; /* unknown */
1375     }
1376     return TRUE;
1377 }
1378
1379 /*
1380  * If the packet type from the netlib header is a login packet, then dig into
1381  * the packet to see if this is a supported TDS version and verify the otherwise
1382  * weak heuristics of the netlib check.
1383  */
1384 static gboolean
1385 netlib_check_login_pkt(tvbuff_t *tvb, guint offset, packet_info *pinfo, guint8 type)
1386 {
1387     guint tds_major, bytes_avail;
1388
1389     bytes_avail = tvb_length(tvb) - offset;
1390     /*
1391      * we have two login packet styles, one for TDS 4.2 and 5.0
1392      */
1393     if (type==TDS_LOGIN_PKT) {
1394         /* Use major version number to validate TDS 4/5 login
1395          * packet */
1396
1397         /* Login packet is first in stream and should not be fragmented...
1398          * if it is we are screwed */
1399         if (bytes_avail < 467) return FALSE;
1400         tds_major = tvb_get_guint8(tvb, 466);
1401         if (tds_major != 4 && tds_major != 5) {
1402             return FALSE;
1403         }
1404         /*
1405          * and one added by Microsoft in SQL Server 7
1406          */
1407     } else if (type==TDS_LOGIN7_PKT) {
1408         if (bytes_avail < 16) return FALSE;
1409         tds_major = tvb_get_guint8(tvb, 15);
1410         if (tds_major != 0x70 && tds_major != 0x80) {
1411             return FALSE;
1412         }
1413     } else if (type==TDS_QUERY5_PKT) {
1414         if (bytes_avail < 9) return FALSE;
1415         /* if this is a TDS 5.0 query check the token */
1416         if (tvb_get_guint8(tvb, 8) != TDS_LANG_TOKEN) {
1417             return FALSE;
1418         }
1419     }
1420     /*
1421      * See if either tcp.destport or tcp.srcport is specified
1422      * in the preferences as being a TDS port.
1423      */
1424     else if (!value_is_in_range(tds_tcp_ports, pinfo->srcport) &&
1425              !value_is_in_range(tds_tcp_ports, pinfo->destport)) {
1426         return FALSE;
1427     }
1428
1429     return TRUE;
1430 }
1431
1432 static void
1433 dissect_tds_env_chg(tvbuff_t *tvb, guint offset, guint token_sz,
1434                     proto_tree *tree)
1435 {
1436     guint8 env_type;
1437     guint old_len, new_len, old_len_offset;
1438     char *new_val = NULL, *old_val = NULL;
1439     guint32 string_offset;
1440     gboolean is_unicode = FALSE;
1441     guint16 collate_codepage, collate_flags;
1442     guint8 collate_charset_id;
1443
1444     env_type = tvb_get_guint8(tvb, offset);
1445     proto_tree_add_text(tree, tvb, offset, 1, "Type: %u (%s)", env_type,
1446                         val_to_str(env_type, env_chg_names, "Unknown"));
1447
1448     new_len = tvb_get_guint8(tvb, offset+1);
1449     old_len_offset = offset + new_len + 2;
1450     old_len = tvb_get_guint8(tvb, old_len_offset);
1451
1452     /*
1453      * If our lengths plus the lengths of the type and the lengths
1454      * don't add up to the token size, it must be UCS2.
1455      */
1456     if (old_len + new_len + 3 != token_sz) {
1457         is_unicode = TRUE;
1458         old_len_offset = offset + (new_len * 2) + 2;
1459         old_len = tvb_get_guint8(tvb, old_len_offset);
1460     }
1461
1462     proto_tree_add_text(tree, tvb, offset + 1, 1, "New Value Length: %u",
1463                         new_len);
1464     if (new_len) {
1465         if (env_type != 7) { /* if it's not 'Collation Info - which is not textual! */
1466             string_offset = offset + 2;
1467             if (is_unicode == TRUE) {
1468                 new_len *= 2;
1469                 new_val = tvb_get_ephemeral_unicode_string(tvb, string_offset,
1470                                                           new_len, ENC_LITTLE_ENDIAN);
1471             } else
1472                 new_val = (gchar*)tvb_get_ephemeral_string(tvb, string_offset, new_len);
1473             proto_tree_add_text(tree, tvb, string_offset, new_len,
1474                                 "New Value: %s", new_val);
1475         }
1476         else { /* parse collation info structure. From http://www.freetds.org/tds.html#collate */
1477             offset +=2;
1478             collate_codepage = tvb_get_letohs(tvb, offset);
1479             proto_tree_add_text(tree, tvb, offset, 2, "Codepage: %u" , collate_codepage);
1480             offset += 2;
1481             collate_flags = tvb_get_letohs(tvb, offset);
1482             proto_tree_add_text(tree, tvb, offset, 2, "Flags: 0x%x", collate_flags);
1483             offset += 2;
1484             collate_charset_id = tvb_get_guint8(tvb, offset);
1485             proto_tree_add_text(tree, tvb, offset, 1, "Charset ID: %u", collate_charset_id);
1486             offset +=1;
1487         }
1488     }
1489
1490     proto_tree_add_text(tree, tvb, old_len_offset, 1, "Old Value Length: %u",
1491                         old_len);
1492     if (old_len) {
1493         string_offset = old_len_offset + 1;
1494         if (is_unicode == TRUE) {
1495             old_len *= 2;
1496             old_val = tvb_get_ephemeral_unicode_string(tvb, string_offset,
1497                                                       old_len, ENC_LITTLE_ENDIAN);
1498         } else
1499             old_val = (gchar*)tvb_get_ephemeral_string(tvb, string_offset, old_len);
1500         proto_tree_add_text(tree, tvb, string_offset, old_len,
1501                             "Old Value: %s", old_val);
1502     }
1503 }
1504
1505 static void
1506 dissect_tds_err_token(tvbuff_t *tvb, guint offset, guint token_sz _U_, proto_tree *tree)
1507 {
1508     guint16 msg_len;
1509     guint8 srvr_len, proc_len;
1510     char *msg;
1511     gboolean is_unicode = FALSE;
1512
1513     proto_tree_add_text(tree, tvb, offset, 4, "SQL Error Number: %d", tds_tvb_get_xxtohl(tvb, offset, tds_little_endian));
1514     offset += 4;
1515     proto_tree_add_text(tree, tvb, offset, 1, "State: %u", tvb_get_guint8(tvb, offset));
1516     offset +=1;
1517     proto_tree_add_text(tree, tvb, offset, 1, "Severity Level: %u", tvb_get_guint8(tvb, offset));
1518     offset +=1;
1519
1520     msg_len = tds_tvb_get_xxtohs(tvb, offset, tds_little_endian);
1521     proto_tree_add_text(tree, tvb, offset, 1, "Error message length: %u characters", msg_len);
1522     offset +=2;
1523
1524     if(tvb_get_guint8(tvb, offset+1) == 0) /* FIXME: It's probably unicode, if the 2nd byte of the message is zero. It's not a good detection method, but it works */
1525         is_unicode = TRUE;
1526
1527     if(is_unicode) {
1528         msg_len *= 2;
1529         msg = tvb_get_ephemeral_unicode_string(tvb, offset, msg_len, ENC_LITTLE_ENDIAN);
1530     } else {
1531         msg = (gchar*)tvb_get_ephemeral_string(tvb, offset, msg_len);
1532     }
1533     proto_tree_add_text(tree, tvb, offset, msg_len, "Error: %s", format_text((guchar*)msg, strlen(msg)));
1534     offset += msg_len;
1535
1536     srvr_len = tvb_get_guint8(tvb, offset);
1537
1538     proto_tree_add_text(tree, tvb, offset, 1, "Server name length: %u characters", srvr_len);
1539     offset +=1;
1540     if(srvr_len) {
1541         if (is_unicode) {
1542             srvr_len *=2;
1543             msg = tvb_get_ephemeral_unicode_string(tvb, offset, srvr_len, ENC_LITTLE_ENDIAN);
1544         } else {
1545             msg = (gchar*)tvb_get_ephemeral_string(tvb, offset, srvr_len);
1546         }
1547         proto_tree_add_text(tree, tvb, offset, srvr_len, "Server name: %s", msg);
1548         offset += srvr_len;
1549     }
1550
1551     proc_len = tvb_get_guint8(tvb, offset);
1552
1553     proto_tree_add_text(tree, tvb, offset, 1, "Process name length: %u characters", proc_len);
1554     offset +=1;
1555     if(proc_len) {
1556         if (is_unicode) {
1557             proc_len *=2;
1558             msg = tvb_get_ephemeral_unicode_string(tvb, offset, proc_len, ENC_LITTLE_ENDIAN);
1559         } else {
1560             msg = (gchar*)tvb_get_ephemeral_string(tvb, offset, proc_len);
1561         }
1562         proto_tree_add_text(tree, tvb, offset, proc_len, "Process name: %s", msg);
1563         offset += proc_len;
1564     }
1565
1566     proto_tree_add_text(tree, tvb, offset, 2, "line number: %d", tds_tvb_get_xxtohs(tvb, offset, tds_little_endian));
1567 }
1568
1569 static void
1570 dissect_tds_login_ack_token(tvbuff_t *tvb, guint offset, guint token_sz, proto_tree *tree)
1571 {
1572     guint8 msg_len;
1573     char *msg;
1574     gboolean is_unicode = FALSE;
1575
1576     proto_tree_add_text(tree, tvb, offset, 1, "Ack: %u", tvb_get_guint8(tvb, offset));
1577     offset +=1;
1578     proto_tree_add_text(tree, tvb, offset, 1, "Major version (may be incorrect): %d", tvb_get_guint8(tvb, offset));
1579     offset +=1;
1580     proto_tree_add_text(tree, tvb, offset, 1, "Minor version (may be incorrect): %d", tvb_get_guint8(tvb, offset));
1581     offset +=1;
1582     proto_tree_add_text(tree, tvb, offset, 2, "zero usually");
1583     offset +=2;
1584
1585     msg_len = tvb_get_guint8(tvb, offset);
1586     proto_tree_add_text(tree, tvb, offset, 1, "Text length: %u characters", msg_len);
1587     offset +=1;
1588
1589     if(msg_len + 6U + 3U != token_sz - 1) /* 6 is the length of ack(1), version (4), text length (1) fields */
1590         is_unicode = TRUE;
1591     proto_tree_add_text(tree, tvb, offset, 0, "msg_len: %d, token_sz: %d, total: %d",msg_len, token_sz, msg_len + 6U + 3U);
1592     if(is_unicode) {
1593         msg_len *= 2;
1594         msg = tvb_get_ephemeral_unicode_string(tvb, offset, msg_len, ENC_LITTLE_ENDIAN);
1595     } else {
1596         msg = (gchar*)tvb_get_ephemeral_string(tvb, offset, msg_len);
1597     }
1598     proto_tree_add_text(tree, tvb, offset, msg_len, "Text: %s", format_text((guchar*)msg, strlen(msg)));
1599     offset += msg_len;
1600
1601     proto_tree_add_text(tree, tvb, offset, 4, "Server Version");
1602     offset += 4;
1603 }
1604
1605 static int
1606 dissect_tds7_results_token(tvbuff_t *tvb, guint offset, proto_tree *tree)
1607 {
1608     guint16 num_columns, table_len;
1609     guint8 type, msg_len;
1610     int i;
1611     char *msg;
1612     guint16 collate_codepage, collate_flags;
1613     guint8 collate_charset_id;
1614
1615     num_columns = tvb_get_letohs(tvb, offset);
1616     proto_tree_add_text(tree, tvb, offset, 2, "Columns: %u", tvb_get_letohs(tvb, offset));
1617     offset +=2;
1618     for(i=0; i != num_columns; i++) {
1619         proto_tree_add_text(tree, tvb, offset, 0, "Column %d", i + 1);
1620         proto_tree_add_text(tree, tvb, offset, 2, "usertype: %d", tvb_get_letohs(tvb, offset));
1621         offset +=2;
1622         proto_tree_add_text(tree, tvb, offset, 2, "flags: %d", tvb_get_letohs(tvb, offset));
1623         offset +=2;
1624         type  = tvb_get_guint8(tvb, offset);
1625         proto_tree_add_text(tree, tvb, offset, 1, "Type: %d", type);
1626         offset +=1;
1627         if(type == 38 || type == 104 || type == 109 || type == 111) { /* ugly, ugly hack. Wish I knew what it really means!*/
1628             proto_tree_add_text(tree, tvb, offset, 1, "unknown 1 byte (%x)", tvb_get_guint8(tvb, offset));
1629             offset +=1;
1630         }
1631         else if (type == 35) {
1632             proto_tree_add_text(tree, tvb, offset, 4, "unknown 4 bytes (%x)", tvb_get_letohl(tvb, offset));
1633             offset += 4;
1634             collate_codepage = tvb_get_letohs(tvb, offset);
1635             proto_tree_add_text(tree, tvb, offset, 2, "Codepage: %u" , collate_codepage);
1636             offset += 2;
1637             collate_flags = tvb_get_letohs(tvb, offset);
1638             proto_tree_add_text(tree, tvb, offset, 2, "Flags: 0x%x", collate_flags);
1639             offset += 2;
1640             collate_charset_id = tvb_get_guint8(tvb, offset);
1641             proto_tree_add_text(tree, tvb, offset, 1, "Charset ID: %u", collate_charset_id);
1642             offset +=1;
1643             table_len = tvb_get_letohs(tvb, offset);
1644             offset +=2;
1645             if(table_len != 0) {
1646                 table_len *= 2;
1647                 msg = tvb_get_ephemeral_unicode_string(tvb, offset, table_len, ENC_LITTLE_ENDIAN);
1648                 proto_tree_add_text(tree, tvb, offset, table_len, "Table name: %s", msg);
1649                 offset += table_len;
1650             }
1651         }
1652         else if (type == 106 || type == 108) {
1653             proto_tree_add_text(tree, tvb, offset, 3, "unknown 3 bytes");
1654             offset +=3;
1655         }
1656         else if(type > 128) {
1657             proto_tree_add_text(tree, tvb, offset, 2, "Large type size: 0x%x", tvb_get_letohs(tvb, offset));
1658             offset += 2;
1659             if (type != 165) {
1660                 collate_codepage = tvb_get_letohs(tvb, offset);
1661                 proto_tree_add_text(tree, tvb, offset, 2, "Codepage: %u" , collate_codepage);
1662                 offset += 2;
1663                 collate_flags = tvb_get_letohs(tvb, offset);
1664                 proto_tree_add_text(tree, tvb, offset, 2, "Flags: 0x%x", collate_flags);
1665                 offset += 2;
1666                 collate_charset_id = tvb_get_guint8(tvb, offset);
1667                 proto_tree_add_text(tree, tvb, offset, 1, "Charset ID: %u", collate_charset_id);
1668                 offset +=1;
1669             }
1670         }
1671         msg_len = tvb_get_guint8(tvb, offset);
1672         proto_tree_add_text(tree, tvb, offset, 1, "message length: %d",msg_len);
1673         offset += 1;
1674         if(msg_len != 0) {
1675             msg_len *= 2;
1676             msg = tvb_get_ephemeral_unicode_string(tvb, offset, msg_len, ENC_LITTLE_ENDIAN);
1677             proto_tree_add_text(tree, tvb, offset, msg_len, "Text: %s", msg);
1678             offset += msg_len;
1679         }
1680     }
1681     return offset;
1682 }
1683
1684 static void
1685 dissect_tds_done_token(tvbuff_t *tvb, guint offset, proto_tree *tree)
1686 {
1687     proto_tree_add_text(tree, tvb, offset, 2, "Status flags");
1688     offset += 2;
1689     proto_tree_add_text(tree, tvb, offset, 2, "Operation");
1690     offset += 2;
1691     proto_tree_add_text(tree, tvb, offset, 4, "row count: %u", tds_tvb_get_xxtohl(tvb, offset, tds_little_endian));
1692     offset += 2;
1693 }
1694
1695 static guint8
1696 dissect_tds_type_info(tvbuff_t *tvb, guint *offset, packet_info *pinfo, proto_tree *tree, gboolean *plp)
1697 {
1698     proto_item *item = NULL, *item1 = NULL, *data_type_item = NULL;
1699     proto_tree *sub_tree = NULL, *collation_tree;
1700     guint32 varlen, varlen_len = 0;
1701     guint8 data_type;
1702
1703     *plp = FALSE; /* most types are not Partially Length-Prefixed */
1704     item = proto_tree_add_item(tree, hf_tds_type_info, tvb, *offset, 0, ENC_NA);
1705     data_type = tvb_get_guint8(tvb, *offset);
1706     proto_item_append_text(item, " (%s)", val_to_str(data_type, tds_data_type_names, "Invalid data type: %02X"));
1707     sub_tree = proto_item_add_subtree(item, ett_tds_type_info);
1708     data_type_item = proto_tree_add_item(sub_tree, hf_tds_type_info_type, tvb, *offset, 1, ENC_LITTLE_ENDIAN);
1709     *offset += 1;
1710
1711     /* optional TYPE_VARLEN for variable length types */
1712     switch(data_type) {
1713         /* FIXEDLENTYPE */
1714         case TDS_DATA_TYPE_NULL:            /* Null (no data associated with this type) */
1715         case TDS_DATA_TYPE_INT1:            /* TinyInt (1 byte data representation) */
1716         case TDS_DATA_TYPE_BIT:             /* Bit (1 byte data representation) */
1717         case TDS_DATA_TYPE_INT2:            /* SmallInt (2 byte data representation) */
1718         case TDS_DATA_TYPE_INT4:            /* Int (4 byte data representation) */
1719         case TDS_DATA_TYPE_FLT4:            /* Real (4 byte data representation) */
1720         case TDS_DATA_TYPE_DATETIM4:        /* SmallDateTime (4 byte data representation) */
1721         case TDS_DATA_TYPE_MONEY4:          /* SmallMoney (4 byte data representation) */
1722         case TDS_DATA_TYPE_INT8:            /* BigInt (8 byte data representation) */
1723         case TDS_DATA_TYPE_FLT8:            /* Float (8 byte data representation) */
1724         case TDS_DATA_TYPE_MONEY:           /* Money (8 byte data representation) */
1725         case TDS_DATA_TYPE_DATETIME:        /* DateTime (8 byte data representation) */
1726         /* BYTELEN_TYPE with length determined by SCALE */
1727         case TDS_DATA_TYPE_TIMEN:           /* (introduced in TDS 7.3) */
1728         case TDS_DATA_TYPE_DATETIME2N:      /* (introduced in TDS 7.3) */
1729         case TDS_DATA_TYPE_DATETIMEOFFSETN: /* (introduced in TDS 7.3) */
1730             varlen_len = 0;
1731             break;
1732         /* BYTELEN_TYPE */
1733         case TDS_DATA_TYPE_GUID:            /* UniqueIdentifier */
1734         case TDS_DATA_TYPE_INTN:
1735         case TDS_DATA_TYPE_DECIMAL:         /* Decimal (legacy support) */
1736         case TDS_DATA_TYPE_NUMERIC:         /* Numeric (legacy support) */
1737         case TDS_DATA_TYPE_BITN:
1738         case TDS_DATA_TYPE_DECIMALN:        /* Decimal */
1739         case TDS_DATA_TYPE_NUMERICN:        /* Numeric */
1740         case TDS_DATA_TYPE_FLTN:
1741         case TDS_DATA_TYPE_MONEYN:
1742         case TDS_DATA_TYPE_DATETIMN:
1743         case TDS_DATA_TYPE_DATEN:           /* (introduced in TDS 7.3) */
1744         case TDS_DATA_TYPE_CHAR:            /* Char (legacy support) */
1745         case TDS_DATA_TYPE_VARCHAR:         /* VarChar (legacy support) */
1746         case TDS_DATA_TYPE_BINARY:          /* Binary (legacy support) */
1747         case TDS_DATA_TYPE_VARBINARY:       /* VarBinary (legacy support) */
1748             varlen_len = 1;
1749             varlen = tvb_get_guint8(tvb, *offset);
1750             break;
1751         /* USHORTLEN_TYPE */
1752         case TDS_DATA_TYPE_BIGVARCHR:       /* VarChar */
1753         case TDS_DATA_TYPE_BIGVARBIN:       /* VarBinary */
1754         case TDS_DATA_TYPE_NVARCHAR:        /* NVarChar */
1755             varlen_len = 2;
1756             varlen = tvb_get_letohs(tvb, *offset);
1757             /* A type with unlimited max size, known as varchar(max), varbinary(max) and nvarchar(max),
1758                which has a max size of 0xFFFF, defined by PARTLENTYPE. This class of types was introduced in TDS 7.2. */
1759             if(varlen == 0xFFFF)
1760                 *plp = TRUE;
1761             break;
1762         case TDS_DATA_TYPE_BIGBINARY:       /* Binary */
1763         case TDS_DATA_TYPE_BIGCHAR:         /* Char */
1764         case TDS_DATA_TYPE_NCHAR:           /* NChar */
1765             varlen_len = 2;
1766             varlen = tvb_get_letohs(tvb, *offset);
1767             break;
1768         /* LONGLEN_TYPE */
1769         case TDS_DATA_TYPE_XML:             /* XML (introduced in TDS 7.2) */
1770         case TDS_DATA_TYPE_UDT:             /* CLR-UDT (introduced in TDS 7.2) */
1771             *plp = TRUE;
1772         case TDS_DATA_TYPE_TEXT:            /* Text */
1773         case TDS_DATA_TYPE_IMAGE:           /* Image */
1774         case TDS_DATA_TYPE_NTEXT:           /* NText */
1775         case TDS_DATA_TYPE_SSVARIANT:       /* Sql_Variant (introduced in TDS 7.2) */
1776             varlen_len = 4;
1777             varlen = tvb_get_letohl(tvb, *offset);
1778             break;
1779         default:
1780             expert_add_info_format(pinfo, data_type_item, PI_MALFORMED, PI_ERROR, "Invalid data type");
1781             THROW(ReportedBoundsError); /* No point in continuing */
1782     }
1783
1784     if(varlen_len)
1785         item1 = proto_tree_add_uint(sub_tree, hf_tds_type_info_varlen, tvb, *offset, varlen_len, varlen);
1786     if(*plp)
1787         proto_item_append_text(item1, " (PLP - Partially Length-Prefixed data type)");
1788     *offset += varlen_len;
1789
1790     /* Optional data dependent on type */
1791     switch(data_type) {
1792         /* PRECISION and SCALE */
1793         case TDS_DATA_TYPE_DECIMAL:         /* Decimal (legacy support) */
1794         case TDS_DATA_TYPE_NUMERIC:         /* Numeric (legacy support) */
1795         case TDS_DATA_TYPE_DECIMALN:        /* Decimal */
1796         case TDS_DATA_TYPE_NUMERICN:        /* Numeric */
1797             proto_tree_add_item(sub_tree, hf_tds_type_info_precision, tvb, *offset, 1, ENC_LITTLE_ENDIAN);
1798             *offset += 1;
1799         /* SCALE */
1800         case TDS_DATA_TYPE_TIMEN:           /* (introduced in TDS 7.3) */
1801         case TDS_DATA_TYPE_DATETIME2N:      /* (introduced in TDS 7.3) */
1802         case TDS_DATA_TYPE_DATETIMEOFFSETN: /* (introduced in TDS 7.3) */
1803             proto_tree_add_item(sub_tree, hf_tds_type_info_scale, tvb, *offset, 1, ENC_LITTLE_ENDIAN);
1804             *offset += 1;
1805             break;
1806         /* COLLATION */
1807         case TDS_DATA_TYPE_BIGCHAR:         /* Char */
1808         case TDS_DATA_TYPE_BIGVARCHR:       /* VarChar */
1809         case TDS_DATA_TYPE_TEXT:            /* Text */
1810         case TDS_DATA_TYPE_NTEXT:           /* NText */
1811         case TDS_DATA_TYPE_NCHAR:           /* NChar */
1812         case TDS_DATA_TYPE_NVARCHAR:        /* NVarChar */
1813             item1 = proto_tree_add_item(sub_tree, hf_tds_type_info_collation, tvb, *offset, 5, ENC_NA);
1814             collation_tree = proto_item_add_subtree(item1, ett_tds_type_info_collation);
1815             proto_tree_add_item(collation_tree, hf_tds_type_info_collation_lcid, tvb, *offset, 4, ENC_LITTLE_ENDIAN);
1816             proto_tree_add_item(collation_tree, hf_tds_type_info_collation_ign_case, tvb, *offset, 4, ENC_LITTLE_ENDIAN);
1817             proto_tree_add_item(collation_tree, hf_tds_type_info_collation_ign_accent, tvb, *offset, 4, ENC_LITTLE_ENDIAN);
1818             proto_tree_add_item(collation_tree, hf_tds_type_info_collation_ign_kana, tvb, *offset, 4, ENC_LITTLE_ENDIAN);
1819             proto_tree_add_item(collation_tree, hf_tds_type_info_collation_ign_width, tvb, *offset, 4, ENC_LITTLE_ENDIAN);
1820             proto_tree_add_item(collation_tree, hf_tds_type_info_collation_binary, tvb, *offset, 4, ENC_LITTLE_ENDIAN);
1821             proto_tree_add_item(collation_tree, hf_tds_type_info_collation_version, tvb, *offset, 4, ENC_LITTLE_ENDIAN);
1822             proto_tree_add_item(collation_tree, hf_tds_type_info_collation_sortid, tvb, *offset + 4, 1, ENC_LITTLE_ENDIAN);
1823             *offset += 5;
1824             break;
1825     }
1826
1827     proto_item_set_end(item, tvb, *offset);
1828     return data_type;
1829 }
1830
1831 static void
1832 dissect_tds_type_varbyte(tvbuff_t *tvb, guint *offset, packet_info *pinfo, proto_tree *tree, int hf, guint8 data_type, gboolean plp)
1833 {
1834     enum { GEN_NULL = 0x00U, CHARBIN_NULL = 0xFFFFU, CHARBIN_NULL32 = 0xFFFFFFFFUL };
1835     guint32 length;
1836     char *string_value;
1837     proto_tree *sub_tree = NULL;
1838     proto_item *item = NULL, *length_item = NULL;
1839
1840     item = proto_tree_add_item(tree, hf, tvb, *offset, 0, ENC_NA);
1841     sub_tree = proto_item_add_subtree(item, ett_tds_type_varbyte);
1842
1843     if(plp) {
1844         enum { PLP_TERMINATOR = 0x00000000UL, UNKNOWN_PLP_LEN = 0xFFFFFFFFFFFFFFFEULL, PLP_NULL = 0xFFFFFFFFFFFFFFFFULL };
1845         guint64 plp_length = tvb_get_letoh64(tvb, *offset);
1846         length_item = proto_tree_add_item(sub_tree, hf_tds_type_varbyte_plp_len, tvb, *offset, 8, ENC_LITTLE_ENDIAN);
1847         *offset += 8;
1848         if(plp_length == PLP_NULL)
1849             proto_item_append_text(length_item, " (PLP_NULL)");
1850         else {
1851             if(plp_length == UNKNOWN_PLP_LEN)
1852                 proto_item_append_text(length_item, " (UNKNOWN_PLP_LEN)");
1853             while(TRUE) {
1854                 length = tvb_get_letohl(tvb, *offset);
1855                 length_item = proto_tree_add_item(sub_tree, hf_tds_type_varbyte_plp_chunk_len, tvb, *offset, 4, ENC_LITTLE_ENDIAN);
1856                 *offset += 4;
1857                 if(length == PLP_TERMINATOR) {
1858                     proto_item_append_text(length_item, " (PLP_TERMINATOR)");
1859                     break;
1860                 }
1861                 switch(data_type) {
1862                     case TDS_DATA_TYPE_BIGVARBIN: /* VarBinary */
1863                         proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_bytes, tvb, *offset, length, ENC_NA);
1864                         break;
1865                     case TDS_DATA_TYPE_BIGVARCHR: /* VarChar */
1866                         proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_string, tvb, *offset, length, ENC_ASCII|ENC_NA);
1867                         break;
1868                     case TDS_DATA_TYPE_NVARCHAR:  /* NVarChar */
1869                         string_value = tvb_get_ephemeral_unicode_string(tvb, *offset, length, ENC_LITTLE_ENDIAN);
1870                         proto_tree_add_string(sub_tree, hf_tds_type_varbyte_data_string, tvb, *offset, length, string_value);
1871                         break;
1872                     case TDS_DATA_TYPE_XML:       /* XML (introduced in TDS 7.2) */
1873                     case TDS_DATA_TYPE_UDT:       /* CLR-UDT (introduced in TDS 7.2) */
1874                         expert_add_info_format(pinfo, length_item, PI_UNDECODED, PI_ERROR, "Data type %d not supported yet", data_type);
1875                         /* No point in continuing: we need to parse the full data_type to know where it ends */
1876                         THROW(ReportedBoundsError);
1877                     default:
1878                         /* no other data type sets plp = TRUE */
1879                         DISSECTOR_ASSERT_NOT_REACHED();
1880                 }
1881                 *offset += length;
1882             }
1883         }
1884     }
1885     else switch(data_type) {
1886         /* FIXEDLENTYPE */
1887         case TDS_DATA_TYPE_NULL:            /* Null (no data associated with this type) */
1888             break;
1889         case TDS_DATA_TYPE_BIT:             /* Bit (1 byte data representation) */
1890             proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_boolean, tvb, *offset, 1, ENC_LITTLE_ENDIAN);
1891             *offset += 1;
1892             break;
1893         case TDS_DATA_TYPE_INT1:            /* TinyInt (1 byte data representation) */
1894             proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_int1, tvb, *offset, 1, ENC_LITTLE_ENDIAN);
1895             *offset += 1;
1896             break;
1897         case TDS_DATA_TYPE_INT2:            /* SmallInt (2 byte data representation) */
1898             proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_int2, tvb, *offset, 2, ENC_LITTLE_ENDIAN);
1899             *offset += 2;
1900             break;
1901         case TDS_DATA_TYPE_INT4:            /* Int (4 byte data representation) */
1902             proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_int4, tvb, *offset, 4, ENC_LITTLE_ENDIAN);
1903             *offset += 4;
1904             break;
1905         case TDS_DATA_TYPE_INT8:            /* BigInt (8 byte data representation) */
1906             proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_int8, tvb, *offset, 8, ENC_LITTLE_ENDIAN);
1907             *offset += 8;
1908             break;
1909         case TDS_DATA_TYPE_FLT4:            /* Real (4 byte data representation) */
1910             proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_float, tvb, *offset, 4, ENC_LITTLE_ENDIAN);
1911             *offset += 4;
1912             break;
1913         case TDS_DATA_TYPE_FLT8:            /* Float (8 byte data representation) */
1914             proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_double, tvb, *offset, 8, ENC_LITTLE_ENDIAN);
1915             *offset += 8;
1916             break;
1917         case TDS_DATA_TYPE_MONEY4:          /* SmallMoney (4 byte data representation) */
1918         case TDS_DATA_TYPE_DATETIM4:        /* SmallDateTime (4 byte data representation) */
1919             /*TODO*/
1920             *offset += 4;
1921             break;
1922         case TDS_DATA_TYPE_MONEY:           /* Money (8 byte data representation) */
1923         case TDS_DATA_TYPE_DATETIME:        /* DateTime (8 byte data representation) */
1924             /*TODO*/
1925             *offset += 8;
1926             break;
1927
1928
1929         /* BYTELEN_TYPE - types prefixed with 1-byte length */
1930         case TDS_DATA_TYPE_GUID:            /* UniqueIdentifier */
1931             length = tvb_get_guint8(tvb, *offset);
1932             length_item = proto_tree_add_uint(sub_tree, hf_tds_type_varbyte_length, tvb, *offset, 1, length);
1933             switch(length) {
1934                 case GEN_NULL: proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_null, tvb, *offset, 0, ENC_NA); break;
1935                 case 16: proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_guid, tvb, *offset + 1, length, ENC_LITTLE_ENDIAN); break;
1936                 default: expert_add_info_format(pinfo, length_item, PI_MALFORMED, PI_ERROR, "Invalid length");
1937             }
1938             *offset += 1 + length;
1939             break;
1940         case TDS_DATA_TYPE_BITN:
1941             length = tvb_get_guint8(tvb, *offset);
1942             length_item = proto_tree_add_uint(sub_tree, hf_tds_type_varbyte_length, tvb, *offset, 1, length);
1943             switch(length) {
1944                 case GEN_NULL: proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_null, tvb, *offset, 0, ENC_NA); break;
1945                 case 1: proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_boolean, tvb, *offset + 1, 1, ENC_LITTLE_ENDIAN); break;
1946                 default: expert_add_info_format(pinfo, length_item, PI_MALFORMED, PI_ERROR, "Invalid length");
1947             }
1948             *offset += 1 + length;
1949             break;
1950         case TDS_DATA_TYPE_INTN:
1951             length = tvb_get_guint8(tvb, *offset);
1952             length_item = proto_tree_add_uint(sub_tree, hf_tds_type_varbyte_length, tvb, *offset, 1, length);
1953             switch(length) {
1954                 case GEN_NULL: proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_null, tvb, *offset, 0, ENC_NA); break;
1955                 case 1: proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_int1, tvb, *offset + 1, 1, ENC_LITTLE_ENDIAN); break;
1956                 case 2: proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_int2, tvb, *offset + 1, 2, ENC_LITTLE_ENDIAN); break;
1957                 case 4: proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_int4, tvb, *offset + 1, 4, ENC_LITTLE_ENDIAN); break;
1958                 case 8: proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_int8, tvb, *offset + 1, 8, ENC_LITTLE_ENDIAN); break;
1959                 default: expert_add_info_format(pinfo, length_item, PI_MALFORMED, PI_ERROR, "Invalid length");
1960             }
1961             *offset += 1 + length;
1962             break;
1963         case TDS_DATA_TYPE_FLTN:
1964             length = tvb_get_guint8(tvb, *offset);
1965             length_item = proto_tree_add_uint(sub_tree, hf_tds_type_varbyte_length, tvb, *offset, 1, length);
1966             switch(length) {
1967                 case GEN_NULL: proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_null, tvb, *offset, 0, ENC_NA); break;
1968                 case 4: proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_float, tvb, *offset + 1, 4, ENC_LITTLE_ENDIAN); break;
1969                 case 8: proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_double, tvb, *offset + 1, 8, ENC_LITTLE_ENDIAN); break;
1970                 default: expert_add_info_format(pinfo, length_item, PI_MALFORMED, PI_ERROR, "Invalid length");
1971             }
1972             *offset += 1 + length;
1973             break;
1974         case TDS_DATA_TYPE_DECIMAL:         /* Decimal (legacy support) */
1975         case TDS_DATA_TYPE_NUMERIC:         /* Numeric (legacy support) */
1976         case TDS_DATA_TYPE_DECIMALN:        /* Decimal */
1977         case TDS_DATA_TYPE_NUMERICN:        /* Numeric */
1978         case TDS_DATA_TYPE_MONEYN:
1979         case TDS_DATA_TYPE_DATETIMN:
1980         case TDS_DATA_TYPE_DATEN:           /* (introduced in TDS 7.3) */
1981         case TDS_DATA_TYPE_TIMEN:           /* (introduced in TDS 7.3) */
1982         case TDS_DATA_TYPE_DATETIME2N:      /* (introduced in TDS 7.3) */
1983         case TDS_DATA_TYPE_DATETIMEOFFSETN: /* (introduced in TDS 7.3) */
1984         case TDS_DATA_TYPE_CHAR:            /* Char (legacy support) */
1985         case TDS_DATA_TYPE_VARCHAR:         /* VarChar (legacy support) */
1986         case TDS_DATA_TYPE_BINARY:          /* Binary (legacy support) */
1987         case TDS_DATA_TYPE_VARBINARY:       /* VarBinary (legacy support) */
1988             length = tvb_get_guint8(tvb, *offset);
1989             proto_tree_add_uint(sub_tree, hf_tds_type_varbyte_length, tvb, *offset, 1, length);
1990             *offset += 1;
1991             if(length > 0) {
1992                 proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_bytes, tvb, *offset, length, ENC_NA);
1993                 *offset += length;
1994             }
1995             break;
1996
1997         /* USHORTLEN_TYPE - types prefixed with 2-byte length */
1998         case TDS_DATA_TYPE_BIGVARBIN:       /* VarBinary */
1999         case TDS_DATA_TYPE_BIGBINARY:       /* Binary */
2000         case TDS_DATA_TYPE_BIGVARCHR:       /* VarChar */
2001         case TDS_DATA_TYPE_BIGCHAR:         /* Char */
2002         case TDS_DATA_TYPE_NVARCHAR:        /* NVarChar */
2003         case TDS_DATA_TYPE_NCHAR:           /* NChar */
2004             length = tvb_get_letohs(tvb, *offset);
2005             length_item = proto_tree_add_uint(sub_tree, hf_tds_type_varbyte_length, tvb, *offset, 2, length);
2006             *offset += 2;
2007             if(length == CHARBIN_NULL) {
2008                 proto_item_append_text(length_item, " (CHARBIN_NULL)");
2009                 proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_null, tvb, *offset, 0, ENC_NA);
2010             }
2011             else {
2012                 switch(data_type) {
2013                     case TDS_DATA_TYPE_BIGVARBIN: /* VarBinary */
2014                     case TDS_DATA_TYPE_BIGBINARY: /* Binary */
2015                         proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_bytes, tvb, *offset, length, ENC_NA);
2016                         break;
2017                     case TDS_DATA_TYPE_BIGVARCHR: /* VarChar */
2018                     case TDS_DATA_TYPE_BIGCHAR:   /* Char */
2019                         proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_string, tvb, *offset, length, ENC_ASCII|ENC_NA);
2020                         break;
2021                     case TDS_DATA_TYPE_NVARCHAR:  /* NVarChar */
2022                     case TDS_DATA_TYPE_NCHAR:     /* NChar */
2023                         string_value = tvb_get_ephemeral_unicode_string(tvb, *offset, length, ENC_LITTLE_ENDIAN);
2024                         proto_tree_add_string(sub_tree, hf_tds_type_varbyte_data_string, tvb, *offset, length, string_value);
2025                         break;
2026                     default:
2027                         DISSECTOR_ASSERT_NOT_REACHED();
2028                 }
2029                 *offset += length;
2030             }
2031             break;
2032
2033         /* LONGLEN_TYPE - types prefixed with 2-byte length */
2034         case TDS_DATA_TYPE_NTEXT:           /* NText */
2035         case TDS_DATA_TYPE_XML:             /* XML (introduced in TDS 7.2) */
2036         case TDS_DATA_TYPE_UDT:             /* CLR-UDT (introduced in TDS 7.2) */
2037         case TDS_DATA_TYPE_TEXT:            /* Text */
2038         case TDS_DATA_TYPE_IMAGE:           /* Image */
2039         case TDS_DATA_TYPE_SSVARIANT:       /* Sql_Variant (introduced in TDS 7.2) */
2040             length = tvb_get_letohl(tvb, *offset);
2041             length_item = proto_tree_add_uint(sub_tree, hf_tds_type_varbyte_length, tvb, *offset, 4, length);
2042             *offset += 4;
2043             if(length == CHARBIN_NULL32) {
2044                 proto_item_append_text(length_item, " (CHARBIN_NULL)");
2045                 proto_tree_add_item(sub_tree, hf_tds_type_varbyte_data_null, tvb, *offset, 0, ENC_NA);
2046             }
2047             else {
2048                 switch(data_type) {
2049                     case TDS_DATA_TYPE_NTEXT: /* NText */
2050                         string_value = tvb_get_ephemeral_unicode_string(tvb, *offset, length, ENC_LITTLE_ENDIAN);
2051                         proto_tree_add_string(sub_tree, hf_tds_type_varbyte_data_string, tvb, *offset, length, string_value);
2052                         break;
2053                     default: /*TODO*/
2054                         expert_add_info_format(pinfo, length_item, PI_UNDECODED, PI_ERROR, "Data type %d not supported yet", data_type);
2055                         /* No point in continuing: we need to parse the full data_type to know where it ends */
2056                         THROW(ReportedBoundsError);
2057                 }
2058                 *offset += length;
2059             }
2060             break;
2061     }
2062     proto_item_set_end(item, tvb, *offset);
2063 }
2064
2065 static void
2066 dissect_tds_rpc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2067 {
2068     proto_item *item = NULL, *param_item = NULL;
2069     proto_tree *sub_tree = NULL, *status_sub_tree = NULL;
2070     int offset = 0;
2071     guint len;
2072     char *val;
2073     guint8 data_type;
2074
2075     item = proto_tree_add_item(tree, hf_tds_rpc, tvb, 0, -1, ENC_NA);
2076     tree = proto_item_add_subtree(item, ett_tds_message);
2077
2078     dissect_tds_all_headers(tvb, &offset, pinfo, tree);
2079     while(tvb_length_remaining(tvb, offset) > 0) {
2080         /*
2081          * RPC name.
2082          */
2083         switch(tds_protocol_type) {
2084             case TDS_PROTOCOL_4:
2085                 len = tvb_get_guint8(tvb, offset);
2086                 proto_tree_add_item(tree, hf_tds_rpc_name_length8, tvb, offset, 1, ENC_LITTLE_ENDIAN);
2087                 proto_tree_add_item(tree, hf_tds_rpc_name, tvb, offset + 1, len, ENC_ASCII|ENC_NA);
2088                 offset += 1 + len;
2089                 break;
2090
2091             case TDS_PROTOCOL_7_0:
2092             case TDS_PROTOCOL_7_1:
2093             case TDS_PROTOCOL_7_2:
2094             case TDS_PROTOCOL_7_3:
2095             default: /* unspecified: try as if TDS7 */
2096                 len = tvb_get_letohs(tvb, offset);
2097                 proto_tree_add_item(tree, hf_tds_rpc_name_length, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2098                 offset += 2;
2099                 if (len == 0xFFFF) {
2100                     proto_tree_add_item(tree, hf_tds_rpc_proc_id, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2101                     offset += 2;
2102                 }
2103                 else if (len != 0) {
2104                     len *= 2;
2105                     val = tvb_get_ephemeral_unicode_string(tvb, offset, len, ENC_LITTLE_ENDIAN);
2106                     proto_tree_add_string(tree, hf_tds_rpc_name, tvb, offset, len, val);
2107                     offset += len;
2108                 }
2109                 break;
2110         }
2111         item = proto_tree_add_item(tree, hf_tds_rpc_options, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2112         sub_tree = proto_item_add_subtree(item, ett_tds_rpc_options);
2113         proto_tree_add_item(sub_tree, hf_tds_rpc_options_with_recomp, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2114         proto_tree_add_item(sub_tree, hf_tds_rpc_options_no_metadata, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2115         proto_tree_add_item(sub_tree, hf_tds_rpc_options_reuse_metadata, tvb, offset, 2, ENC_LITTLE_ENDIAN);
2116         offset += 2;
2117
2118         /* dissect parameters */
2119         while(tvb_length_remaining(tvb, offset) > 0) {
2120             gboolean plp;
2121
2122             len = tvb_get_guint8(tvb, offset);
2123             /* check for BatchFlag or NoExecFlag */
2124             if((gint8)len < 0) {
2125                 proto_tree_add_item(tree, hf_tds_rpc_separator, tvb, offset, 1, ENC_LITTLE_ENDIAN);
2126                 ++offset;
2127                 break;
2128             }
2129             param_item = proto_tree_add_item(tree, hf_tds_rpc_parameter, tvb, offset, 0, ENC_NA);
2130             sub_tree = proto_item_add_subtree(param_item, ett_tds_rpc_parameter);
2131             proto_tree_add_item(sub_tree, hf_tds_rpc_parameter_name_length, tvb, offset, 1, ENC_LITTLE_ENDIAN);
2132             ++offset;
2133             if(len) {
2134                 len *= 2;
2135                 val = tvb_get_ephemeral_unicode_string(tvb, offset, len, ENC_LITTLE_ENDIAN);
2136                 proto_tree_add_string(sub_tree, hf_tds_rpc_parameter_name, tvb, offset, len, val);
2137                 offset += len;
2138             }
2139             item = proto_tree_add_item(sub_tree, hf_tds_rpc_parameter_status, tvb, offset, 1, ENC_LITTLE_ENDIAN);
2140             status_sub_tree = proto_item_add_subtree(item, ett_tds_rpc_parameter_status);
2141             proto_tree_add_item(status_sub_tree, hf_tds_rpc_parameter_status_by_ref, tvb, offset, 1, ENC_LITTLE_ENDIAN);
2142             proto_tree_add_item(status_sub_tree, hf_tds_rpc_parameter_status_default, tvb, offset, 1, ENC_LITTLE_ENDIAN);
2143             ++offset;
2144             data_type = dissect_tds_type_info(tvb, &offset, pinfo, sub_tree, &plp);
2145             dissect_tds_type_varbyte(tvb, &offset, pinfo, sub_tree, hf_tds_rpc_parameter_value, data_type, plp);
2146             proto_item_set_end(param_item, tvb, offset);
2147         }
2148     }
2149 }
2150
2151 static void
2152 dissect_tds_resp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2153 {
2154     int offset = 0;
2155     proto_item *token_item;
2156     proto_tree *token_tree;
2157     guint pos, token_sz = 0;
2158     guint token_len_field_size = 2;
2159     guint token_len_field_val = 0;
2160     guint8 token;
2161     struct _netlib_data nl_data;
2162     gint length_remaining;
2163
2164     memset(&nl_data, '\0', sizeof nl_data);
2165
2166     /*
2167      * Until we reach the end of the packet, read tokens.
2168      */
2169     pos = offset;
2170     while (tvb_reported_length_remaining(tvb, pos) > 0) {
2171         /* our token */
2172         token = tvb_get_guint8(tvb, pos);
2173
2174         /* TODO Handle TDS_PARAMFMT, TDS_PARAMS [similar to TDS_RESULTS, TDS_ROW] */
2175         if (tds_token_is_fixed_size(token)) {
2176             token_sz = tds_get_fixed_token_size(token) + 1;
2177         } else if (token == TDS_ROW_TOKEN) {
2178             /*
2179              * Rows are special; they have no size field and
2180              * aren't fixed length.
2181              */
2182             token_sz = tds_get_row_size(tvb, &nl_data, pos + 1);
2183         } else
2184             token_sz = tds_get_variable_token_size(tvb, pos + 1,
2185                                                    token, &token_len_field_size, &token_len_field_val);
2186
2187         length_remaining = tvb_ensure_length_remaining(tvb, pos);
2188
2189         if ((int) token_sz < 0) {
2190             proto_tree_add_text(tree, tvb, pos, 0, "Bogus token size: %u",
2191                                 token_sz);
2192             break;
2193         }
2194         if ((int) token_len_field_size < 0) {
2195             proto_tree_add_text(tree, tvb, pos, 0, "Bogus token length field size: %u",
2196                                 token_len_field_size);
2197             break;
2198         }
2199         token_item = proto_tree_add_text(tree, tvb, pos, token_sz,
2200                                          "Token 0x%02x %s", token,
2201                                          val_to_str(token, token_names, "Unknown Token Type"));
2202         token_tree = proto_item_add_subtree(token_item, ett_tds_token);
2203
2204         /*
2205          * If it's a variable token, put the length field in here
2206          * instead of replicating this for each token subdissector.
2207          */
2208         if (!tds_token_is_fixed_size(token) && token != TDS_ROW_TOKEN) {
2209             proto_tree_add_text(token_tree, tvb, pos + 1,
2210                                 token_len_field_size, "Length: %u",
2211                                 token_len_field_val);
2212         }
2213
2214         if (token_sz > (guint)length_remaining)
2215             token_sz = (guint)length_remaining;
2216
2217         switch (token) {
2218
2219             case TDS_COL_NAME_TOKEN:
2220                 /*
2221                  * TDS 4.2
2222                  * TODO dissect token to get "column names" to fill in _netlib_data
2223                  */
2224                 break;
2225
2226             case TDS_COL_INFO_TOKEN:
2227                 /*
2228                  * TDS 4.2: get the column info
2229                  */
2230                 dissect_tds_col_info_token(tvb, &nl_data, pos);
2231                 break;
2232
2233             case TDS_RESULT_TOKEN:
2234                 /*
2235                  * If it's a result token, we need to stash the
2236                  * column info.
2237                  */
2238                 read_results_tds5(tvb, &nl_data, pos + 3, token_sz - 3);
2239                 break;
2240
2241             case TDS_ENV_CHG_TOKEN:
2242                 dissect_tds_env_chg(tvb, pos + 3, token_sz - 3, token_tree);
2243                 break;
2244
2245             case TDS_AUTH_TOKEN:
2246                 dissect_tds_nt(tvb, pinfo, token_tree, pos + 3, token_sz - 3);
2247                 break;
2248             case TDS_ERR_TOKEN:
2249             case TDS_MSG_TOKEN:
2250                 dissect_tds_err_token(tvb, pos + 3, token_sz - 3, token_tree);
2251                 break;
2252
2253             case TDS_DONE_TOKEN:
2254             case TDS_DONEPROC_TOKEN:
2255             case TDS_DONEINPROC_TOKEN:
2256                 dissect_tds_done_token(tvb, pos + 1, token_tree);
2257                 break;
2258             case TDS_LOGIN_ACK_TOKEN:
2259                 dissect_tds_login_ack_token(tvb, pos + 3, token_sz - 3, token_tree);
2260                 break;
2261             case TDS7_RESULT_TOKEN:
2262                 pos = (dissect_tds7_results_token(tvb, pos + 1, token_tree)-1);
2263                 break;
2264         }
2265
2266         /* and step to the end of the token, rinse, lather, repeat */
2267         pos += token_sz;
2268     }
2269 }
2270
2271 static void
2272 dissect_netlib_buffer(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2273 {
2274     int offset = 0;
2275     proto_item *tds_item = NULL;
2276     proto_tree *tds_tree = NULL;
2277     proto_tree *tds_status_tree = NULL;
2278     guint8 type;
2279     guint8 status;
2280     guint16 channel;
2281     guint8 packet_number;
2282     gboolean save_fragmented;
2283     int len;
2284     fragment_data *fd_head;
2285     tvbuff_t *next_tvb;
2286
2287     type = tvb_get_guint8(tvb, offset);
2288     status = tvb_get_guint8(tvb, offset + 1);
2289     channel = tvb_get_ntohs(tvb, offset + 4);
2290     packet_number = tvb_get_guint8(tvb, offset + 6);
2291
2292     /* create display subtree for the protocol */
2293     tds_item = proto_tree_add_item(tree, proto_tds, tvb, offset, -1, ENC_NA);
2294     tds_tree = proto_item_add_subtree(tds_item, ett_tds);
2295     proto_tree_add_item(tds_tree, hf_tds_type, tvb, offset, 1, ENC_LITTLE_ENDIAN);
2296     tds_item = proto_tree_add_item(tds_tree, hf_tds_status, tvb, offset + 1, 1, ENC_LITTLE_ENDIAN);
2297     tds_status_tree = proto_item_add_subtree(tds_item, ett_tds_status);
2298     proto_tree_add_item(tds_status_tree, hf_tds_status_eom, tvb, offset + 1, 1, ENC_BIG_ENDIAN);
2299     proto_tree_add_item(tds_status_tree, hf_tds_status_ignore, tvb, offset + 1, 1, ENC_BIG_ENDIAN);
2300     proto_tree_add_item(tds_status_tree, hf_tds_status_event_notif, tvb, offset + 1, 1, ENC_BIG_ENDIAN);
2301     proto_tree_add_item(tds_status_tree, hf_tds_status_reset_conn, tvb, offset + 1, 1, ENC_BIG_ENDIAN);
2302     proto_tree_add_item(tds_status_tree, hf_tds_status_reset_conn_skip_tran,tvb, offset + 1, 1, ENC_BIG_ENDIAN);
2303     proto_tree_add_item(tds_tree, hf_tds_length, tvb, offset + 2, 2, ENC_BIG_ENDIAN);
2304     proto_tree_add_item(tds_tree, hf_tds_channel, tvb, offset + 4, 2, ENC_BIG_ENDIAN);
2305     proto_tree_add_item(tds_tree, hf_tds_packet_number, tvb, offset + 6, 1, ENC_LITTLE_ENDIAN);
2306     proto_tree_add_item(tds_tree, hf_tds_window, tvb, offset + 7, 1, ENC_LITTLE_ENDIAN);
2307     offset += 8;        /* skip Netlib header */
2308
2309     /*
2310      * Deal with fragmentation.
2311      *
2312      * TODO: handle case where netlib headers 'packet-number'.is always 0
2313      *       use fragment_add_seq_next in this case ?
2314      *
2315      */
2316     save_fragmented = pinfo->fragmented;
2317     if (tds_defragment &&
2318         (packet_number > 1 || (status & STATUS_LAST_BUFFER) == 0)) {
2319         if ((status & STATUS_LAST_BUFFER) == 0) {
2320             col_append_str(pinfo->cinfo, COL_INFO,
2321                            " (Not last buffer)");
2322         }
2323         len = tvb_reported_length_remaining(tvb, offset);
2324         /*
2325          * XXX - I've seen captures that start with a login
2326          * packet with a sequence number of 2.
2327          */
2328         fd_head = fragment_add_seq_check(tvb, offset, pinfo, channel,
2329                                          tds_fragment_table, tds_reassembled_table,
2330                                          packet_number - 1, len, (status & STATUS_LAST_BUFFER) == 0);
2331         next_tvb = process_reassembled_data(tvb, offset, pinfo,
2332                                             "Reassembled TDS", fd_head, &tds_frag_items, NULL,
2333                                             tds_tree);
2334     } else {
2335         /*
2336          * If this isn't the last buffer, just show it as a fragment.
2337          * (XXX - it'd be nice to dissect it if it's the first
2338          * buffer, but we'd need to do reassembly in order to
2339          * discover that.)
2340          *
2341          * If this is the last buffer, dissect it.
2342          * (XXX - it'd be nice to show it as a fragment if it's part
2343          * of a fragmented message, but we'd need to do reassembly
2344          * in order to discover that.)
2345          */
2346         if ((status & STATUS_LAST_BUFFER) == 0)
2347             next_tvb = NULL;
2348         else {
2349             next_tvb = tvb_new_subset_remaining(tvb, offset);
2350         }
2351     }
2352
2353     if (next_tvb != NULL) {
2354
2355         switch (type) {
2356
2357             case TDS_RPC_PKT:
2358                 dissect_tds_rpc(next_tvb, pinfo, tds_tree);
2359                 break;
2360
2361             case TDS_RESP_PKT:
2362                 dissect_tds_resp(next_tvb, pinfo, tds_tree);
2363                 break;
2364
2365             case TDS_LOGIN7_PKT:
2366                 dissect_tds7_login(next_tvb, pinfo, tds_tree);
2367                 break;
2368             case TDS_QUERY_PKT:
2369                 dissect_tds_query_packet(next_tvb, pinfo, tds_tree);
2370                 break;
2371             case TDS_QUERY5_PKT:
2372                 dissect_tds_query5_packet(next_tvb, pinfo, tds_tree);
2373                 break;
2374             case TDS_SSPI_PKT:
2375                 dissect_tds_nt(next_tvb, pinfo, tds_tree, offset - 8, -1);
2376                 break;
2377             default:
2378                 proto_tree_add_text(tds_tree, next_tvb, 0, -1,
2379                                     "TDS Packet");
2380                 break;
2381         }
2382     } else {
2383         next_tvb = tvb_new_subset_remaining (tvb, offset);
2384         call_dissector(data_handle, next_tvb, pinfo, tds_tree);
2385     }
2386     pinfo->fragmented = save_fragmented;
2387 }
2388
2389 static void
2390 dissect_tds_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2391 {
2392     volatile gboolean first_time = TRUE;
2393     volatile int offset = 0;
2394     guint length_remaining;
2395     guint8 type;
2396     guint16 plen;
2397     guint length;
2398     tvbuff_t *volatile next_tvb;
2399     proto_item *tds_item = NULL;
2400     proto_tree *tds_tree = NULL;
2401     void *pd_save;
2402
2403     while (tvb_reported_length_remaining(tvb, offset) != 0) {
2404         length_remaining = tvb_ensure_length_remaining(tvb, offset);
2405
2406         /*
2407          * Can we do reassembly?
2408          */
2409         if (tds_desegment && pinfo->can_desegment) {
2410             /*
2411              * Yes - is the fixed-length part of the PDU
2412              * split across segment boundaries?
2413              */
2414             if (length_remaining < 8) {
2415                 /*
2416                  * Yes.  Tell the TCP dissector where the data for this message
2417                  * starts in the data it handed us and that we need "some more
2418                  * data."  Don't tell it exactly how many bytes we need because
2419                  * if/when we ask for even more (after the header) that will
2420                  * break reassembly.
2421                  */
2422                 pinfo->desegment_offset = offset;
2423                 pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;
2424                 return;
2425             }
2426         }
2427
2428         type = tvb_get_guint8(tvb, offset);
2429
2430         /*
2431          * Get the length of the PDU.
2432          */
2433         plen = tvb_get_ntohs(tvb, offset + 2);
2434         if (plen < 8) {
2435             /*
2436              * The length is less than the header length.
2437              * Put in the type, status, and length, and
2438              * report the length as bogus.
2439              */
2440             if (tree) {
2441                 /* create display subtree for the protocol */
2442                 tds_item = proto_tree_add_item(tree, proto_tds,
2443                                                tvb, offset, -1, ENC_NA);
2444
2445                 tds_tree = proto_item_add_subtree(tds_item,
2446                                                   ett_tds);
2447                 proto_tree_add_uint(tds_tree, hf_tds_type, tvb,
2448                                     offset, 1, type);
2449                 proto_tree_add_item(tds_tree, hf_tds_status,
2450                                     tvb, offset + 1, 1, ENC_BIG_ENDIAN);
2451                 proto_tree_add_uint_format(tds_tree,
2452                                            hf_tds_length, tvb, offset + 2, 2, plen,
2453                                            "Size: %u (bogus, should be >= 8)", plen);
2454             }
2455
2456             /*
2457              * Give up - we can't dissect any more of this
2458              * data.
2459              */
2460             break;
2461         }
2462
2463         /*
2464          * Can we do reassembly?
2465          */
2466         if (tds_desegment && pinfo->can_desegment) {
2467             /*
2468              * Yes - is the PDU split across segment boundaries?
2469              */
2470             if (length_remaining < plen) {
2471                 /*
2472                  * Yes.  Tell the TCP dissector where the
2473                  * data for this message starts in the data
2474                  * it handed us, and how many more bytes we
2475                  * need, and return.
2476                  */
2477                 pinfo->desegment_offset = offset;
2478                 pinfo->desegment_len = plen - length_remaining;
2479                 return;
2480             }
2481         }
2482
2483         if (first_time) {
2484             col_set_str(pinfo->cinfo, COL_PROTOCOL, "TDS");
2485
2486             /*
2487              * Set the packet description based on its TDS packet
2488              * type.
2489              */
2490             col_add_str(pinfo->cinfo, COL_INFO,
2491                         val_to_str(type, packet_type_names,
2492                                    "Unknown Packet Type: %u"));
2493             first_time = FALSE;
2494         }
2495
2496         /*
2497          * Construct a tvbuff containing the amount of the payload
2498          * we have available.  Make its reported length the amount
2499          * of data in the PDU.
2500          *
2501          * XXX - if reassembly isn't enabled. the subdissector will
2502          * throw a BoundsError exception, rather than a
2503          * ReportedBoundsError exception.  We really want a tvbuff
2504          * where the length is "length", the reported length is
2505          * "plen", and the "if the snapshot length were infinite"
2506          * length is the minimum of the reported length of the tvbuff
2507          * handed to us and "plen", with a new type of exception
2508          * thrown if the offset is within the reported length but
2509          * beyond that third length, with that exception getting the
2510          * "Unreassembled Packet" error.
2511          */
2512         length = length_remaining;
2513         if (length > plen)
2514             length = plen;
2515         next_tvb = tvb_new_subset(tvb, offset, length, plen);
2516
2517         /*
2518          * Dissect the Netlib buffer.
2519          *
2520          * Catch the ReportedBoundsError exception; if this
2521          * particular Netlib buffer happens to get a
2522          * ReportedBoundsError exception, that doesn't mean
2523          * that we should stop dissecting PDUs within this frame
2524          * or chunk of reassembled data.
2525          *
2526          * If it gets a BoundsError, we can stop, as there's nothing
2527          * more to see, so we just re-throw it.
2528          */
2529         pd_save = pinfo->private_data;
2530         TRY {
2531             dissect_netlib_buffer(next_tvb, pinfo, tree);
2532         }
2533         CATCH(BoundsError) {
2534             RETHROW;
2535         }
2536         CATCH(ReportedBoundsError) {
2537             /*  Restore the private_data structure in case one of the
2538              *  called dissectors modified it (and, due to the exception,
2539              *  was unable to restore it).
2540              */
2541             pinfo->private_data = pd_save;
2542
2543             show_reported_bounds_error(tvb, pinfo, tree);
2544         }
2545         ENDTRY;
2546
2547         /*
2548          * Step to the next Netlib buffer.
2549          */
2550         offset += plen;
2551     }
2552 }
2553
2554 static gboolean
2555 dissect_tds_tcp_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
2556 {
2557     int offset = 0;
2558     guint8 type;
2559     guint8 status;
2560     guint16 plen;
2561     conversation_t *conv;
2562
2563     /*
2564      * If we don't have even enough data for a Netlib header,
2565      * just say it's not TDS.
2566      */
2567     if (tvb_length(tvb) < 8)
2568         return FALSE;
2569
2570     /*
2571      * Quickly scan all the data we have in order to see if
2572      * everything in it looks like Netlib traffic.
2573      */
2574     while (tvb_bytes_exist(tvb, offset, 1)) {
2575         /*
2576          * Check the type field.
2577          */
2578         type = tvb_get_guint8(tvb, offset);
2579         if (!is_valid_tds_type(type))
2580             return FALSE;
2581
2582         /*
2583          * Check the status field, if it's present.
2584          */
2585         if (!tvb_bytes_exist(tvb, offset + 1, 1))
2586             break;
2587         status = tvb_get_guint8(tvb, offset + 1);
2588         if (!is_valid_tds_status(status))
2589             return FALSE;
2590
2591         /*
2592          * Get the length of the PDU.
2593          */
2594         if (!tvb_bytes_exist(tvb, offset + 2, 2))
2595             break;
2596         plen = tvb_get_ntohs(tvb, offset + 2);
2597         if (plen < 8) {
2598             /*
2599              * The length is less than the header length.
2600              * That's bogus.
2601              */
2602             return FALSE;
2603         }
2604
2605         /*
2606          * If we're at the beginning of the segment, check the
2607          * payload if it's a login packet.
2608          */
2609         if (offset == 0) {
2610             if (!netlib_check_login_pkt(tvb, offset, pinfo, type))
2611                 return FALSE;
2612         }
2613
2614         /*
2615          * Step to the next Netlib buffer.
2616          */
2617         offset += plen;
2618     }
2619
2620     /*
2621      * OK, it passes the test; assume the rest of this conversation
2622      * is TDS.
2623      */
2624     conv = find_or_create_conversation(pinfo);
2625     conversation_set_dissector(conv, tds_tcp_handle);
2626
2627     /*
2628      * Now dissect it as TDS.
2629      */
2630     dissect_tds_tcp(tvb, pinfo, tree);
2631     return TRUE;
2632 }
2633
2634 static void
2635 tds_init(void)
2636 {
2637     /*
2638      * Initialize the fragment and reassembly tables.
2639      */
2640     fragment_table_init(&tds_fragment_table);
2641     reassembled_table_init(&tds_reassembled_table);
2642
2643 }
2644
2645 /* Register the protocol with Wireshark */
2646
2647 /* this format is required because a script is used to build the C function
2648    that calls all the protocol registration.
2649 */
2650
2651 void
2652 proto_register_tds(void)
2653 {
2654     static hf_register_info hf[] = {
2655         { &hf_tds_type,
2656           { "Type",             "tds.type",
2657             FT_UINT8, BASE_DEC, VALS(packet_type_names), 0x0,
2658             "Packet type", HFILL }
2659         },
2660         { &hf_tds_status,
2661           { "Status",           "tds.status",
2662             FT_UINT8, BASE_HEX, NULL, 0x0,
2663             "Packet status", HFILL }
2664         },
2665         { &hf_tds_status_eom,
2666           { "End of message",   "tds.status.eom",
2667             FT_BOOLEAN, 8, NULL, STATUS_LAST_BUFFER,
2668             "The packet is the last packet in the whole request", HFILL }
2669         },
2670         { &hf_tds_status_ignore,
2671           { "Ignore this event", "tds.status.ignore",
2672             FT_BOOLEAN, 8, NULL, STATUS_IGNORE_EVENT,
2673             "(From client to server) Ignore this event (EOM MUST also be set)", HFILL }
2674         },
2675         { &hf_tds_status_event_notif,
2676           { "Event notification", "tds.status.event_notif",
2677             FT_BOOLEAN, 8, NULL, STATUS_EVENT_NOTIFICATION,
2678             NULL, HFILL }
2679         },
2680         { &hf_tds_status_reset_conn,
2681           { "Reset connection", "tds.status.reset_conn",
2682             FT_BOOLEAN, 8, NULL, STATUS_RESETCONNECTION,
2683             "(From client to server) Reset this connection before processing event", HFILL }
2684         },
2685         { &hf_tds_status_reset_conn_skip_tran,
2686           { "Reset connection keeping transaction state", "tds.status.reset_conn_skip_tran",
2687             FT_BOOLEAN, 8, NULL, STATUS_RESETCONNECTIONSKIPTRAN,
2688             "(From client to server) Reset the connection before processing event but do not modify the transaction state", HFILL }
2689         },
2690         { &hf_tds_length,
2691           { "Length",           "tds.length",
2692             FT_UINT16, BASE_DEC, NULL, 0x0,
2693             "Packet length", HFILL }
2694         },
2695         { &hf_tds_channel,
2696           { "Channel",          "tds.channel",
2697             FT_UINT16, BASE_DEC, NULL, 0x0,
2698             "Channel Number", HFILL }
2699         },
2700         { &hf_tds_packet_number,
2701           { "Packet Number",    "tds.packet_number",
2702             FT_UINT8, BASE_DEC, NULL, 0x0,
2703             NULL, HFILL }
2704         },
2705         { &hf_tds_window,
2706           { "Window",           "tds.window",
2707             FT_UINT8, BASE_DEC, NULL, 0x0,
2708             NULL, HFILL }
2709         },
2710         { &hf_tds_fragment_overlap,
2711           { "Segment overlap",  "tds.fragment.overlap",
2712             FT_BOOLEAN, BASE_NONE, NULL, 0x0,
2713             "Fragment overlaps with other fragments", HFILL }
2714         },
2715         { &hf_tds_fragment_overlap_conflict,
2716           { "Conflicting data in fragment overlap", "tds.fragment.overlap.conflict",
2717             FT_BOOLEAN, BASE_NONE, NULL, 0x0,
2718             "Overlapping fragments contained conflicting data", HFILL }
2719         },
2720         { &hf_tds_fragment_multiple_tails,
2721           { "Multiple tail fragments found", "tds.fragment.multipletails",
2722             FT_BOOLEAN, BASE_NONE, NULL, 0x0,
2723             "Several tails were found when defragmenting the packet", HFILL }
2724         },
2725         { &hf_tds_fragment_too_long_fragment,
2726           { "Segment too long", "tds.fragment.toolongfragment",
2727             FT_BOOLEAN, BASE_NONE, NULL, 0x0,
2728             "Segment contained data past end of packet", HFILL }
2729         },
2730         { &hf_tds_fragment_error,
2731           { "Defragmentation error",    "tds.fragment.error",
2732             FT_FRAMENUM, BASE_NONE, NULL, 0x0,
2733             "Defragmentation error due to illegal fragments", HFILL }
2734         },
2735         { &hf_tds_fragment_count,
2736           { "Segment count", "tds.fragment.count",
2737             FT_UINT32, BASE_DEC, NULL, 0x0,
2738             NULL, HFILL }
2739         },
2740         { &hf_tds_fragment,
2741           { "TDS Fragment",     "tds.fragment",
2742             FT_FRAMENUM, BASE_NONE, NULL, 0x0,
2743             NULL, HFILL }
2744         },
2745         { &hf_tds_fragments,
2746           { "TDS Fragments",    "tds.fragments",
2747             FT_NONE, BASE_NONE, NULL, 0x0,
2748             NULL, HFILL }
2749         },
2750         { &hf_tds_reassembled_in,
2751           { "Reassembled TDS in frame", "tds.reassembled_in",
2752             FT_FRAMENUM, BASE_NONE, NULL, 0x0,
2753             "This TDS packet is reassembled in this frame", HFILL }
2754         },
2755         { &hf_tds_reassembled_length,
2756           { "Reassembled TDS length", "tds.reassembled.length",
2757             FT_UINT32, BASE_DEC, NULL, 0x0,
2758             "The total length of the reassembled payload", HFILL }
2759         },
2760         { &hf_tds7_login_total_size,
2761           { "Total Packet Length", "tds7login.total_len",
2762             FT_UINT32, BASE_DEC, NULL, 0x0,
2763             "TDS7 Login Packet total packet length", HFILL }
2764         },
2765         { &hf_tds7_version,
2766           { "TDS version", "tds7login.version",
2767             FT_UINT32, BASE_HEX, NULL, 0x0,
2768             NULL, HFILL }
2769         },
2770         { &hf_tds7_packet_size,
2771           { "Packet Size", "tds7login.packet_size",
2772             FT_UINT32, BASE_DEC, NULL, 0x0,
2773             NULL, HFILL }
2774         },
2775         { &hf_tds7_client_version,
2776           { "Client version", "tds7login.client_version",
2777             FT_UINT32, BASE_DEC, NULL, 0x0,
2778             NULL, HFILL }
2779         },
2780         { &hf_tds7_client_pid,
2781           { "Client PID", "tds7login.client_pid",
2782             FT_UINT32, BASE_DEC, NULL, 0x0,
2783             NULL, HFILL }
2784         },
2785         { &hf_tds7_connection_id,
2786           { "Connection ID", "tds7login.connection_id",
2787             FT_UINT32, BASE_DEC, NULL, 0x0,
2788             NULL, HFILL }
2789         },
2790         { &hf_tds7_option_flags1,
2791           { "Option Flags 1", "tds7login.option_flags1",
2792             FT_UINT8, BASE_HEX, NULL, 0x0,
2793             NULL, HFILL }
2794         },
2795         { &hf_tds7_option_flags2,
2796           { "Option Flags 2", "tds7login.option_flags2",
2797             FT_UINT8, BASE_HEX, NULL, 0x0,
2798             NULL, HFILL }
2799         },
2800         { &hf_tds7_sql_type_flags,
2801           { "SQL Type Flags", "tds7login.sql_type_flags",
2802             FT_UINT8, BASE_HEX, NULL, 0x0,
2803             NULL, HFILL }
2804         },
2805         { &hf_tds7_reserved_flags,
2806           { "Reserved Flags", "tds7login.reserved_flags",
2807             FT_UINT8, BASE_HEX, NULL, 0x0,
2808             NULL, HFILL }
2809         },
2810         { &hf_tds7_time_zone,
2811           { "Time Zone", "tds7login.time_zone",
2812             FT_UINT32, BASE_HEX, NULL, 0x0,
2813             NULL, HFILL }
2814         },
2815         { &hf_tds7_collation,
2816           { "Collation", "tds7login.collation",
2817             FT_UINT32, BASE_HEX, NULL, 0x0,
2818             NULL, HFILL }
2819         },
2820         { &hf_tds_all_headers,
2821           { "Packet data stream headers", "tds.all_headers",
2822             FT_NONE, BASE_NONE, NULL, 0x0,
2823             "The ALL_HEADERS rule", HFILL }
2824         },
2825         { &hf_tds_all_headers_total_length,
2826           { "Total length",     "tds.all_headers.total_length",
2827             FT_UINT32, BASE_DEC, NULL, 0x0,
2828             "Total length of ALL_HEADERS stream", HFILL }
2829         },
2830         { &hf_tds_all_headers_header_length,
2831           { "Length",           "tds.all_headers.header.length",
2832             FT_UINT32, BASE_DEC, NULL, 0x0,
2833             "Total length of an individual header", HFILL }
2834         },
2835         { &hf_tds_all_headers_header_type,
2836           { "Type",             "tds.all_headers.header.type",
2837             FT_UINT16, BASE_HEX, VALS(header_type_names), 0x0,
2838             NULL, HFILL }
2839         },
2840         { &hf_tds_all_headers_trans_descr,
2841           { "Transaction descriptor", "tds.all_headers.header.trans_descr",
2842             FT_UINT64, BASE_DEC, NULL, 0x0,
2843             "For each connection, a number that uniquely identifies the transaction the request is associated with. Initially generated by the server when a new transaction is created and returned to the client as part of the ENVCHANGE token stream.", HFILL }
2844         },
2845         { &hf_tds_all_headers_request_cnt,
2846           { "Outstanding request count", "tds.all_headers.header.request_cnt",
2847             FT_UINT32, BASE_DEC, NULL, 0x0,
2848             "Number of requests currently active on the connection", HFILL }
2849         },
2850         { &hf_tds_type_info,
2851           { "Type info",        "tds.type_info",
2852             FT_NONE, BASE_NONE, NULL, 0x0,
2853             "The TYPE_INFO rule applies to several messages used to describe column information", HFILL }
2854         },
2855         { &hf_tds_type_info_type,
2856           { "Type",             "tds.type_info.type",
2857             FT_UINT8, BASE_HEX, VALS(tds_data_type_names), 0x0,
2858             NULL, HFILL }
2859         },
2860         { &hf_tds_type_info_varlen,
2861           { "Maximal length",   "tds.type_info.varlen",
2862             FT_UINT32, BASE_DEC, NULL, 0x0,
2863             "Defines the length of the data contained within the column", HFILL }
2864         },
2865         { &hf_tds_type_info_precision,
2866           { "Precision",        "tds.type_info.precision",
2867             FT_UINT8, BASE_DEC, NULL, 0x0,
2868             NULL, HFILL }
2869         },
2870         { &hf_tds_type_info_scale,
2871           { "Scale",            "tds.type_info.scale",
2872             FT_UINT8, BASE_DEC, NULL, 0x0,
2873             NULL, HFILL }
2874         },
2875         { &hf_tds_type_info_collation,
2876           { "Collation",        "tds.type_info.collation",
2877             FT_NONE, BASE_NONE, NULL, 0x0,
2878             "Specifies collation information for character data or metadata describing character data", HFILL }
2879         },
2880         { &hf_tds_type_info_collation_lcid,
2881           { "LCID",             "tds.type_info.collation.lcid",
2882             FT_UINT32, BASE_HEX, NULL, 0x000FFFFF,
2883             "For a SortId==0 collation, the LCID bits correspond to a LocaleId as defined by the National Language Support (NLS) functions", HFILL }
2884         },
2885         { &hf_tds_type_info_collation_ign_case,
2886           { "Ignore case",      "tds.type_info.collation.ignore_case",
2887             FT_BOOLEAN, 32, NULL, 0x00100000,
2888             NULL, HFILL }
2889         },
2890         { &hf_tds_type_info_collation_ign_accent,
2891           { "Ignore accent",    "tds.type_info.collation.ignore_accent",
2892             FT_BOOLEAN, 32, NULL, 0x00200000,
2893             NULL, HFILL }
2894         },
2895         { &hf_tds_type_info_collation_ign_kana,
2896           { "Ignore kana",      "tds.type_info.collation.ignore_kana",
2897             FT_BOOLEAN, 32, NULL, 0x00400000,
2898             NULL, HFILL }
2899         },
2900         { &hf_tds_type_info_collation_ign_width,
2901           { "Ignore width",     "tds.type_info.collation.ignore_width",
2902             FT_BOOLEAN, 32, NULL, 0x00800000,
2903             NULL, HFILL }
2904         },
2905         { &hf_tds_type_info_collation_binary,
2906           { "Binary",           "tds.type_info.collation.binary",
2907             FT_BOOLEAN, 32, NULL, 0x01000000,
2908             NULL, HFILL }
2909         },
2910         { &hf_tds_type_info_collation_version,
2911           { "Version",          "tds.type_info.collation.version",
2912             FT_UINT32, BASE_DEC, NULL, 0xF0000000,
2913             NULL, HFILL }
2914         },
2915         { &hf_tds_type_info_collation_sortid,
2916           { "SortId",           "tds.type_info.collation.sortid",
2917             FT_UINT8, BASE_DEC, NULL, 0x0,
2918             NULL, HFILL }
2919         },
2920         { &hf_tds_type_varbyte_length,
2921           { "Length",           "tds.type_varbyte.length",
2922             FT_UINT32, BASE_DEC, NULL, 0x0,
2923             NULL, HFILL }
2924         },
2925         { &hf_tds_type_varbyte_data_null,
2926           { "Data: NULL",       "tds.type_varbyte.data",
2927             FT_NONE, BASE_NONE, NULL, 0x0,
2928             NULL, HFILL }
2929         },
2930         { &hf_tds_type_varbyte_data_boolean,
2931           { "Data",             "tds.type_varbyte.data",
2932             FT_BOOLEAN, BASE_NONE, NULL, 0x0,
2933             NULL, HFILL }
2934         },
2935         { &hf_tds_type_varbyte_data_int1,
2936           { "Data",             "tds.type_varbyte.data",
2937             FT_INT8, BASE_DEC, NULL, 0x0,
2938             NULL, HFILL }
2939         },
2940         { &hf_tds_type_varbyte_data_int2,
2941           { "Data",             "tds.type_varbyte.data",
2942             FT_INT16, BASE_DEC, NULL, 0x0,
2943             NULL, HFILL }
2944         },
2945         { &hf_tds_type_varbyte_data_int4,
2946           { "Data",             "tds.type_varbyte.data",
2947             FT_INT32, BASE_DEC, NULL, 0x0,
2948             NULL, HFILL }
2949         },
2950         { &hf_tds_type_varbyte_data_int8,
2951           { "Data",             "tds.type_varbyte.data",
2952             FT_INT64, BASE_DEC, NULL, 0x0,
2953             NULL, HFILL }
2954         },
2955         { &hf_tds_type_varbyte_data_float,
2956           { "Data",             "tds.type_varbyte.data",
2957             FT_FLOAT, BASE_NONE, NULL, 0x0,
2958             NULL, HFILL }
2959         },
2960         { &hf_tds_type_varbyte_data_double,
2961           { "Data",             "tds.type_varbyte.data",
2962             FT_DOUBLE, BASE_NONE, NULL, 0x0,
2963             NULL, HFILL }
2964         },
2965         { &hf_tds_type_varbyte_data_bytes,
2966           { "Data",             "tds.type_varbyte.data",
2967             FT_BYTES, BASE_NONE, NULL, 0x0,
2968             NULL, HFILL }
2969         },
2970         { &hf_tds_type_varbyte_data_guid,
2971           { "Data",             "tds.type_varbyte.data",
2972             FT_GUID, BASE_NONE, NULL, 0x0,
2973             NULL, HFILL }
2974         },
2975         { &hf_tds_type_varbyte_data_string,
2976           { "Data",             "tds.type_varbyte.data",
2977             FT_STRING, BASE_NONE, NULL, 0x0,
2978             NULL, HFILL }
2979         },
2980         { &hf_tds_type_varbyte_plp_len,
2981           { "PLP length",       "tds.type_varbyte.plp_len",
2982             FT_INT64, BASE_DEC, NULL, 0x0,
2983             NULL, HFILL }
2984         },
2985         { &hf_tds_type_varbyte_plp_chunk_len,
2986           { "PLP chunk length", "tds.type_varbyte.plp_chunk_len",
2987             FT_INT32, BASE_DEC, NULL, 0x0,
2988             NULL, HFILL }
2989         },
2990         { &hf_tds_rpc,
2991           { "Remote Procedure Call", "tds.rpc",
2992             FT_NONE, BASE_NONE, NULL, 0x0,
2993             NULL, HFILL }
2994         },
2995         { &hf_tds_rpc_name_length8,
2996           { "Procedure name length", "tds.rpc.name_length",
2997             FT_UINT8, BASE_DEC, NULL, 0x0,
2998             NULL, HFILL }
2999         },
3000         { &hf_tds_rpc_name_length,
3001           { "Procedure name length", "tds.rpc.name_length",
3002             FT_UINT16, BASE_DEC, NULL, 0x0,
3003             NULL, HFILL }
3004         },
3005         { &hf_tds_rpc_name,
3006           { "Procedure name",   "tds.rpc.name",
3007             FT_STRING, BASE_NONE, NULL, 0x0,
3008             NULL, HFILL }
3009         },
3010         { &hf_tds_rpc_proc_id,
3011           { "Stored procedure ID", "tds.rpc.proc_id",
3012             FT_UINT16, BASE_DEC, VALS(internal_stored_proc_id_names), 0x0,
3013             "The number identifying the special stored procedure to be executed", HFILL }
3014         },
3015         { &hf_tds_rpc_options,
3016           { "Option flags",     "tds.rpc.options",
3017             FT_UINT16, BASE_HEX, NULL, 0x0,
3018             "The number identifying the special stored procedure to be executed", HFILL }
3019         },
3020         { &hf_tds_rpc_options_with_recomp,
3021           { "With recompile",   "tds.rpc.options.with_recomp",
3022             FT_BOOLEAN, 16, NULL, TDS_RPC_OPT_WITH_RECOMP,
3023             "The number identifying the special stored procedure to be executed", HFILL }
3024         },
3025         { &hf_tds_rpc_options_no_metadata,
3026           { "No metadata",      "tds.rpc.options.no_metadata",
3027             FT_BOOLEAN, 16, NULL, TDS_RPC_OPT_NO_METADATA,
3028             "The number identifying the special stored procedure to be executed", HFILL }
3029         },
3030         { &hf_tds_rpc_options_reuse_metadata,
3031           { "Reuse metadata",   "tds.rpc.options.reuse_metadata",
3032             FT_BOOLEAN, 16, NULL, TDS_RPC_OPT_REUSE_METADATA,
3033             "The number identifying the special stored procedure to be executed", HFILL }
3034         },
3035         { &hf_tds_rpc_separator,
3036           { "RPC batch separator", "tds.rpc.separator",
3037             FT_UINT8, BASE_DEC, VALS(tds_rpc_separators), 0x0,
3038             NULL, HFILL }
3039         },
3040         { &hf_tds_rpc_parameter,
3041           { "Parameter",        "tds.rpc.parameter",
3042             FT_NONE, BASE_NONE, NULL, 0x0,
3043             NULL, HFILL }
3044         },
3045         { &hf_tds_rpc_parameter_name_length,
3046           { "Name length",      "tds.rpc.parameter.name_length",
3047             FT_UINT8, BASE_DEC, NULL, 0x0,
3048             NULL, HFILL }
3049         },
3050         { &hf_tds_rpc_parameter_name,
3051           { "Name",             "tds.rpc.parameter.name",
3052             FT_STRING, BASE_NONE, NULL, 0x0,
3053             NULL, HFILL }
3054         },
3055         { &hf_tds_rpc_parameter_status,
3056           { "Status flags",     "tds.rpc.parameter.status",
3057             FT_UINT8, BASE_HEX, NULL, 0x0,
3058             "Information on how the parameter is passed", HFILL }
3059         },
3060         { &hf_tds_rpc_parameter_status_by_ref,
3061           { "By reference",     "tds.rpc.parameter.status.by_ref",
3062             FT_BOOLEAN, 16, NULL, TDS_RPC_PARAMETER_STATUS_BY_REF,
3063             NULL, HFILL }
3064         },
3065         { &hf_tds_rpc_parameter_status_default,
3066           { "Default value",    "tds.rpc.parameter.status.default",
3067             FT_BOOLEAN, 16, NULL, TDS_RPC_PARAMETER_STATUS_DEFAULT,
3068             NULL, HFILL }
3069         },
3070         { &hf_tds_rpc_parameter_value,
3071           { "Value",            "tds.rpc.parameter.value",
3072             FT_NONE, BASE_NONE, NULL, 0x0,
3073             NULL, HFILL }
3074         },
3075     };
3076
3077     static gint *ett[] = {
3078         &ett_tds,
3079         &ett_tds_status,
3080         &ett_tds_fragments,
3081         &ett_tds_fragment,
3082         &ett_tds_all_headers,
3083         &ett_tds_all_headers_header,
3084         &ett_tds_type_info,
3085         &ett_tds_type_info_collation,
3086         &ett_tds_type_varbyte,
3087         &ett_tds_message,
3088         &ett_tds_rpc_options,
3089         &ett_tds_rpc_parameter,
3090         &ett_tds_rpc_parameter_status,
3091         &ett_tds_token,
3092         &ett_tds7_query,
3093         &ett_tds7_login,
3094         &ett_tds7_hdr,
3095     };
3096     module_t *tds_module;
3097
3098 /* Register the protocol name and description */
3099     proto_tds = proto_register_protocol("Tabular Data Stream",
3100                                         "TDS", "tds");
3101
3102 /* Required function calls to register the header fields and subtrees used */
3103     proto_register_field_array(proto_tds, hf, array_length(hf));
3104     proto_register_subtree_array(ett, array_length(ett));
3105
3106 /* Allow dissector to be found by name. */
3107     register_dissector("tds", dissect_tds_tcp, proto_tds);
3108
3109     tds_module = prefs_register_protocol(proto_tds, NULL);
3110     prefs_register_bool_preference(tds_module, "desegment_buffers",
3111                                    "Reassemble TDS buffers spanning multiple TCP segments",
3112                                    "Whether the TDS dissector should reassemble TDS buffers spanning multiple TCP segments. "
3113                                    "To use this option, you must also enable \"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings.",
3114                                    &tds_desegment);
3115     prefs_register_bool_preference(tds_module, "defragment",
3116                                    "Reassemble fragmented TDS messages with multiple buffers",
3117                                    "Whether the TDS dissector should defragment messages spanning multiple Netlib buffers",
3118                                    &tds_defragment);
3119     prefs_register_enum_preference(tds_module, "protocol_type",
3120                                    "TDS Protocol Type",
3121                                    "Hint as to version of TDS protocol being decoded",
3122                                    &tds_protocol_type, tds_protocol_type_options, FALSE);
3123     prefs_register_enum_preference(tds_module, "endian_type",
3124                                    "TDS decode as",
3125                                    "Hint as to whether to decode TDS protocol as little-endian or big-endian. (TDS7/8 always decoded as little-endian)",
3126                                    &tds_little_endian, tds_endian_type_options, FALSE);
3127     prefs_register_range_preference(tds_module, "tcp_ports",
3128                                     "TDS TCP ports",
3129                                     "Additional TCP ports to decode as TDS",
3130                                     &tds_tcp_ports, 0xFFFF);
3131
3132     register_init_routine(tds_init);
3133 }
3134
3135 /* If this dissector uses sub-dissector registration add a registration routine.
3136    This format is required because a script is used to find these routines and
3137    create the code that calls these routines.
3138 */
3139 void
3140 proto_reg_handoff_tds(void)
3141 {
3142     tds_tcp_handle = create_dissector_handle(dissect_tds_tcp, proto_tds);
3143
3144     /* Initial TDS ports: MS SQL default ports */
3145     dissector_add_uint("tcp.port", 1433, tds_tcp_handle);
3146     dissector_add_uint("tcp.port", 2433, tds_tcp_handle);
3147
3148     heur_dissector_add("tcp", dissect_tds_tcp_heur, proto_tds);
3149
3150     ntlmssp_handle = find_dissector("ntlmssp");
3151     gssapi_handle = find_dissector("gssapi");
3152     data_handle = find_dissector("data");
3153 }