Replace the types from sys/types.h and netinet/in.h by their glib.h
[obnox/wireshark/wip.git] / packet-wcp.c
1 /* packet-wcp.c
2  * Routines for Wellfleet Compression frame disassembly
3  * Copyright 2001, Jeffrey C. Foster <jfoste@woodward.com>
4  *
5  * $Id: packet-wcp.c,v 1.27 2002/08/02 23:36:04 jmayer Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 1998
10  * 
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  * 
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  *
25  * ToDo:
26  *      Add preference to allow/disallow decompression
27  *      Calculate and verify check byte (last byte), if only we knew how!
28  *      Handle Wellfleet compression over PPP links.
29  *              - This will require changing the sub-dissector call
30  *                routine to determine if layer 2 is frame relay or
31  *                or PPP and different sub-dissector routines for each.
32  *
33  * Based upon information in the Nortel TCL based Pcaptap code.
34  *http://www.mynetworkforum.com/tools/PCAPTAP/pcaptap-Win32-3.00.exe
35  *
36  * And lzss algorithm
37  *http://www.rasip.fer.hr/research/compress/algorithms/fund/lz/lzss.html
38  */
39
40 /*
41  * Wellfleet compression is a variation on LZSS encoding.
42  *
43  * Compression is done by keeping a sliding window of previous 
44  * data transmited. The sender will use a pattern match to 
45  * encode repeated data as a data pointer field. Then a stream
46  * of pointers and actual data bytes. The pointer values include
47  * an offset to previous data in the stream and the length of the
48  *  matching data.
49  *
50  * The data pattern matching is done on the octects. 
51  *
52  * The data is encoded as 8 field blocks with a compression flag
53  * byte at the beginning.  If the bit is set in the compression
54  * flag, then that field has a compression field. If it isn't set
55  * then the byte is raw data.
56  *
57  * The compression field is either 2 or 3 bytes long. The length
58  * is determined by the length of the matching data, for short
59  * matches the match length is encoded in the high nibble of the
60  * first byte. Otherwise the third byte of the field contains 
61  * the match length.
62  *
63  * First byte -
64  * lower 4 bits:
65  *              High order nibble of the offset
66  *
67  * upper 4 bits:
68  *              1   = length is in 3rd byte 
69  *              2-F = length of matching data - 1
70  *              
71  * Second byte - 
72  *  Lower byte of the source offset.
73  *
74  * Third byte - 
75  *  Length of match - 1 if First byte upper nibble = 1, otherwise
76  *  this byte isn't added to data stream.
77  *
78  * Example:
79  *      Uncompressed data (hex):  11 22 22 22 22 33 44 55 66 77
80  *
81  *
82  *      Compression data :
83  *                      Flag bits:      0x20 (third field is compressed)
84  *                      Data:   11 22 20 00 33 44 55
85  *                              /  /  /  /
86  *              raw data ------+--+  /  /               
87  *              (Comp length - 1)<<4+  /
88  *              Data offset ----------+
89  *
90  *      Output data (hex):  20 11 22 20 00 33 44 55 66 77
91  *
92  * In this example the copy src is one byte behind the copy destination
93  * so if appears as if output is being loaded with the source byte.
94  *
95  */
96
97
98
99 #ifdef HAVE_CONFIG_H
100 # include "config.h"
101 #endif
102
103 #include <stdio.h>
104 #include <glib.h>
105 #include <string.h>
106 #include <epan/packet.h>
107 #include "packet-frame.h"
108 #include <epan/conversation.h>
109 #include "etypes.h"
110 #include "nlpid.h"
111
112 #define MAX_WIN_BUF_LEN 0x7fff          /* storage size for decompressed data */
113 #define MAX_WCP_BUF_LEN 2048            /* storage size for decompressed data */
114 #define FROM_DCE        0x80            /* for direction setting */
115
116 typedef struct {
117
118         guint8  *buf_cur;
119         guint8  buffer[MAX_WIN_BUF_LEN];
120
121 }wcp_window_t;
122
123 /*XXX do I really want the length in here  */
124 typedef struct {
125
126         guint16  len;
127         guint8  buffer[MAX_WCP_BUF_LEN];
128
129 }wcp_pdata_t;
130
131
132 #define wcp_win_init_count 4
133 #define wcp_packet_init_count 10
134
135 #define wcp_win_length (sizeof(wcp_window_t))
136 #define wcp_packet_length (sizeof(wcp_pdata_t))
137
138 static GMemChunk *wcp_window = NULL;
139 static GMemChunk *wcp_pdata = NULL;
140
141 static int proto_wcp = -1;
142 static int hf_wcp_cmd = -1;
143 static int hf_wcp_ext_cmd = -1;
144 static int hf_wcp_seq = -1;
145 static int hf_wcp_chksum = -1;
146 static int hf_wcp_tid = -1;
147 static int hf_wcp_rev = -1;
148 static int hf_wcp_init = -1;
149 static int hf_wcp_seq_size = -1;
150 static int hf_wcp_alg = -1;
151 static int hf_wcp_alg_cnt = -1;
152 static int hf_wcp_alg_a = -1;
153 static int hf_wcp_alg_b = -1;
154 static int hf_wcp_alg_c = -1;
155 static int hf_wcp_alg_d = -1;
156 static int hf_wcp_rexmit = -1;
157
158 static int hf_wcp_hist_size = -1;
159 static int hf_wcp_ppc = -1;
160 static int hf_wcp_pib = -1;
161
162 static int hf_wcp_comp_bits = -1;
163 static int hf_wcp_comp_marker = -1;
164 static int hf_wcp_short_len = -1;
165 static int hf_wcp_long_len = -1;
166 static int hf_wcp_short_run = -1;
167 static int hf_wcp_long_run = -1;
168 static int hf_wcp_offset = -1;
169
170 static gint ett_wcp = -1;
171 static gint ett_wcp_field = -1;
172
173 static dissector_handle_t fr_uncompressed_handle;
174
175 /*
176  * Bits in the address field.
177  */
178 #define WCP_CMD                 0xf0    /* WCP Command */
179 #define WCP_EXT_CMD             0x0f    /* WCP Extended Command */
180 #define WCP_SEQ                 0x0fff  /* WCP Sequence number */
181 #define WCP_OFFSET_MASK         0x0fff  /* WCP Pattern source offset */
182
183 #define PPC_COMPRESSED_IND              0x0
184 #define PPC_UNCOMPRESSED_IND            0x1
185 #define PPC_TPPC_COMPRESSED_IND         0x2
186 #define PPC_TPPC_UNCOMPRESSED_IND       0x3
187 #define CONNECT_REQ                     0x4
188 #define CONNECT_ACK                     0x5
189 #define CONNECT_NAK                     0x6
190 #define DISCONNECT_REQ                  0x7
191 #define DISCONNECT_ACK                  0x8
192 #define INIT_REQ                        0x9
193 #define INIT_ACK                        0xa
194 #define RESET_REQ                       0xb
195 #define RESET_ACK                       0xc
196 #define REXMIT_NAK                      0xd
197
198
199 static const value_string cmd_string[] = {
200         {0, "Compressed Data"},
201         {1, "Uncompressed Data"},
202         {15, "Extended"},
203         { 0,       NULL }
204         };
205
206 static const value_string ext_cmd_string[] = {
207         {0, "Per Packet Compression"},
208         {4, "Connect Req"},
209         {5, "Connect Ack"},
210         {9, "Init Req"},
211         {0x0a, "Init Ack"},
212
213         { 0,       NULL }
214         };
215
216
217
218 static tvbuff_t *wcp_uncompress( tvbuff_t *src_tvb, int offset, packet_info *pinfo, proto_tree *tree);
219 static wcp_window_t *get_wcp_window_ptr( packet_info *pinfo);
220
221 static void
222 dissect_wcp_con_req(tvbuff_t *tvb, int offset, proto_tree *tree) {
223
224 /* WCP connector request message */
225
226         guint alg_cnt = tvb_get_guint8(tvb, 5);
227
228         proto_tree_add_uint(tree, hf_wcp_tid, tvb, offset, 2, tvb_get_ntohs(tvb, offset));
229         proto_tree_add_uint(tree, hf_wcp_rev, tvb, offset + 2, 1, tvb_get_guint8(tvb, offset + 2));
230         proto_tree_add_uint(tree, hf_wcp_init, tvb, offset + 3, 1, tvb_get_guint8(tvb, offset + 3));
231         proto_tree_add_uint(tree, hf_wcp_seq_size, tvb, offset + 4, 1, tvb_get_guint8(tvb, offset + 4));
232         proto_tree_add_uint(tree, hf_wcp_alg_cnt, tvb, offset + 5, 1, alg_cnt);
233         proto_tree_add_uint(tree, hf_wcp_alg_a, tvb, offset + 6, 1, tvb_get_guint8(tvb, offset + 6));
234         if ( alg_cnt > 1)
235                 proto_tree_add_uint(tree, hf_wcp_alg_b, tvb, offset + 7, 1, tvb_get_guint8(tvb, offset + 7));
236         if ( alg_cnt > 2)
237                 proto_tree_add_uint(tree, hf_wcp_alg_c, tvb, offset + 8, 1, tvb_get_guint8(tvb, offset + 8));
238         if ( alg_cnt > 3)
239                 proto_tree_add_uint(tree, hf_wcp_alg_d, tvb, offset + 9, 1, tvb_get_guint8(tvb, offset + 9));
240 }
241
242 static void
243 dissect_wcp_con_ack( tvbuff_t *tvb, int offset, proto_tree *tree){
244
245 /* WCP connector ack message */
246
247         proto_tree_add_uint(tree, hf_wcp_tid, tvb, offset, 2, tvb_get_ntohs(tvb, offset));
248         proto_tree_add_uint(tree, hf_wcp_rev, tvb, offset + 2, 1, tvb_get_guint8(tvb, offset + 2));
249         proto_tree_add_uint(tree, hf_wcp_seq_size, tvb, offset + 3, 1, tvb_get_guint8(tvb, offset + 3));
250         proto_tree_add_uint(tree, hf_wcp_alg, tvb, offset + 4, 1, tvb_get_guint8(tvb, offset + 4));
251 }
252
253 static void
254 dissect_wcp_init( tvbuff_t *tvb, int offset, proto_tree *tree){
255
256 /* WCP Initiate Request/Ack message */
257
258         proto_tree_add_uint(tree, hf_wcp_tid, tvb, offset, 2, tvb_get_ntohs(tvb, offset));
259         proto_tree_add_uint(tree, hf_wcp_rev, tvb, offset + 2, 1, tvb_get_guint8(tvb, offset + 2));
260         proto_tree_add_uint(tree, hf_wcp_hist_size, tvb, offset + 3, 1, tvb_get_guint8(tvb, offset + 3));
261         proto_tree_add_uint(tree, hf_wcp_ppc, tvb, offset + 4, 1, tvb_get_guint8(tvb, offset + 4));
262         proto_tree_add_uint(tree, hf_wcp_pib, tvb, offset + 5, 1, tvb_get_guint8(tvb, offset + 5));
263 }
264
265
266 static void
267 dissect_wcp_reset( tvbuff_t *tvb, int offset, proto_tree *tree){
268
269 /* Process WCP Reset Request/Ack message */
270
271         proto_tree_add_uint(tree, hf_wcp_tid, tvb, offset, 2, tvb_get_ntohs(tvb, offset));
272 }
273
274
275 static void wcp_save_data( tvbuff_t *tvb, packet_info *pinfo){
276
277         wcp_window_t *buf_ptr = 0;
278         int len; 
279
280         /* discard first 2 bytes, header and last byte (check byte) */
281         len = tvb_reported_length( tvb)-3;
282         buf_ptr = get_wcp_window_ptr( pinfo); 
283
284         if (( buf_ptr->buf_cur + len) <= (buf_ptr->buffer + MAX_WIN_BUF_LEN)){
285                 tvb_memcpy( tvb, buf_ptr->buf_cur, 2, len);
286                 buf_ptr->buf_cur = buf_ptr->buf_cur + len;
287         
288         } else {
289                 guint8 *buf_end = buf_ptr->buffer + MAX_WIN_BUF_LEN; 
290                 tvb_memcpy( tvb, buf_ptr->buf_cur, 2, buf_end - buf_ptr->buf_cur);
291                 tvb_memcpy( tvb, buf_ptr->buffer, buf_end - buf_ptr->buf_cur-2,
292                         len - (int)(buf_end - buf_ptr->buf_cur));
293                 buf_ptr->buf_cur = buf_ptr->buf_cur + len - MAX_WIN_BUF_LEN;
294         }
295
296 }
297
298
299 static void dissect_wcp( tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
300
301         proto_tree      *wcp_tree;
302         proto_item      *ti;
303         int             wcp_header_len;
304         guint16         temp, cmd, ext_cmd, seq;
305         tvbuff_t        *next_tvb;
306
307         if (check_col(pinfo->cinfo, COL_PROTOCOL))
308                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "WCP");
309         if (check_col(pinfo->cinfo, COL_INFO))
310                 col_clear(pinfo->cinfo, COL_INFO);
311
312         temp =tvb_get_ntohs(tvb, 0); 
313
314         cmd = (temp & 0xf000) >> 12;
315         ext_cmd = (temp & 0x0f00) >> 8;
316
317         if ( cmd == 0xf)
318                 wcp_header_len= 1;
319         else 
320                 wcp_header_len= 2;
321
322         seq = temp & 0x0fff;
323
324 /*XXX should test seq to be sure it the last + 1 !! */
325
326         if (check_col(pinfo->cinfo, COL_INFO)){
327                 col_add_str(pinfo->cinfo, COL_INFO, val_to_str(cmd, cmd_string, "Unknown"));
328                 if ( cmd == 0xf)
329                         col_append_fstr(pinfo->cinfo, COL_INFO, ", %s",
330                                 val_to_str(ext_cmd, ext_cmd_string, "Unknown"));
331         }       
332
333         if (tree) {
334                 ti = proto_tree_add_item(tree, proto_wcp, tvb, 0, wcp_header_len, FALSE);
335
336                 wcp_tree = proto_item_add_subtree(ti, ett_wcp);
337
338                 proto_tree_add_uint(wcp_tree, hf_wcp_cmd, tvb, 0, 1, tvb_get_guint8( tvb, 0));
339                 if ( cmd == 0xf){
340                         proto_tree_add_uint(wcp_tree, hf_wcp_ext_cmd, tvb, 1, 1,
341                                         tvb_get_guint8( tvb, 0));
342                         switch (ext_cmd){
343                         case CONNECT_REQ:
344                                 dissect_wcp_con_req( tvb, 1, wcp_tree);
345                                 break;
346
347                         case CONNECT_ACK:
348                                 dissect_wcp_con_ack( tvb, 1, wcp_tree);
349                                 break;
350                         case INIT_REQ:
351                         case INIT_ACK:
352                                 dissect_wcp_init( tvb, 1, wcp_tree);
353                                 break;
354                         case RESET_REQ:
355                         case RESET_ACK:
356                                 dissect_wcp_reset( tvb, 1, wcp_tree);
357                                 break;
358                         default:
359                                 break;
360                         }
361                 }else { 
362                         proto_tree_add_uint(wcp_tree, hf_wcp_seq,  tvb, 0, 2, seq);
363                 }
364         }
365         else {
366                 wcp_tree = NULL;
367         }
368
369
370                                         /* exit if done */
371         if ( cmd != 1 && cmd != 0 && !(cmd == 0xf && ext_cmd == 0)) 
372                 return;
373
374         if ( cmd == 1) {                /* uncompressed data */
375                 if ( !pinfo->fd->flags.visited){        /* if first pass */
376                         wcp_save_data( tvb, pinfo);
377                 }
378                 next_tvb = tvb_new_subset(tvb, wcp_header_len, -1, -1);
379         }
380         else {          /* cmd == 0 || (cmd == 0xf && ext_cmd == 0) */
381
382                 next_tvb = wcp_uncompress( tvb, wcp_header_len, pinfo, wcp_tree);
383
384                 if ( !next_tvb){
385                         proto_tree_add_protocol_format(tree, proto_malformed, tvb, 0, 0,
386                                         "[Malformed Frame: Bad WCP compressed data]" );
387                         return;
388                 }
389         }
390
391         if ( tree)                      /* add the check byte */
392                 proto_tree_add_uint(wcp_tree, hf_wcp_chksum, tvb,
393                         tvb_reported_length( tvb)-1, 1,
394                         tvb_get_guint8( tvb, tvb_reported_length(tvb)-1));
395
396         call_dissector(fr_uncompressed_handle, next_tvb, pinfo, tree);
397
398         return;
399 }
400
401
402 static guint8 *decompressed_entry( guint8 *src, guint8 *dst, int *len, guint8 * buf_start, guint8 *buf_end){
403
404 /* do the decompression for one field */
405
406         guint16 data_offset, data_cnt;
407         guint8 tmp = *src;
408
409         data_offset = (*(src++) & 0xf) << 8;    /* get high byte */
410         data_offset += *(src++);                /* add next byte */
411
412         if (( tmp & 0xf0) == 0x10){             /* 2 byte count */
413                 data_cnt = *src;
414                 data_cnt++;
415
416         }else {                                 /* one byte count */
417                 data_cnt = tmp >> 4;
418                 data_cnt++;
419         }               
420
421         
422         src = (dst - 1 - data_offset);
423         if ( src < buf_start)
424                 src += MAX_WIN_BUF_LEN;
425
426
427 /*XXX could do some fancy memory moves, later if speed is problem */
428
429         while( data_cnt--){
430                 *dst = *src;
431                 if ( ++(*len) >MAX_WCP_BUF_LEN){
432                         printf("decomp failed, len = %d\n",  *len);
433
434                         return NULL;    /* end of buffer error */
435                 }
436                 if ( dst++ == buf_end)
437                         dst = buf_start;
438                 if ( src++ == buf_end)
439                         src = buf_start;
440
441         }
442         return dst;
443 }
444
445
446 static 
447 wcp_window_t *get_wcp_window_ptr( packet_info *pinfo){
448
449 /* find the conversation for this side of the DLCI, create one if needed */
450 /* and return the wcp_window data structure pointer */
451
452         conversation_t *conv;
453         wcp_window_t *wcp_win_data;
454
455         conv = find_conversation( &pinfo->dl_src, &pinfo->dl_src, PT_NONE, 
456                 ((pinfo->pseudo_header->x25.flags & FROM_DCE)? 1:0), 
457                 ((pinfo->pseudo_header->x25.flags & FROM_DCE)? 1:0), 0);
458         if ( !conv){
459                 conv = conversation_new( &pinfo->dl_src, &pinfo->dl_src, PT_NONE, 
460                         ((pinfo->pseudo_header->x25.flags & FROM_DCE)? 1:0), 
461                         ((pinfo->pseudo_header->x25.flags & FROM_DCE)? 1:0),
462                         0);
463         }
464         wcp_win_data = conversation_get_proto_data(conv, proto_wcp);
465         if ( !wcp_win_data){
466                 wcp_win_data = g_mem_chunk_alloc( wcp_window);
467                 wcp_win_data->buf_cur = wcp_win_data->buffer;
468                 conversation_add_proto_data(conv, proto_wcp, wcp_win_data);
469         }
470
471         return wcp_win_data;
472 }
473
474
475 static tvbuff_t *wcp_uncompress( tvbuff_t *src_tvb, int offset, packet_info *pinfo, proto_tree *tree) {
476
477 /* do the packet data uncompression and load it into the dst buffer */
478
479         proto_tree      *sub_tree;
480         proto_item      *ti;
481
482         int len=0, i = -1;
483         int cnt = tvb_reported_length( src_tvb)-1;      /* don't include check byte */
484
485         guint8 *buf = 0, *dst, *src, *buf_start, *buf_end,  *tmp, comp_flag_bits = 0;
486         guint8 src_buf[ MAX_WCP_BUF_LEN];
487         tvbuff_t *volatile tvb = 0;
488         wcp_window_t *buf_ptr = 0;
489         wcp_pdata_t *volatile pdata_ptr;
490
491         buf_ptr = get_wcp_window_ptr( pinfo); 
492
493         buf_start = buf_ptr->buffer;
494         buf_end = buf_start + MAX_WIN_BUF_LEN;
495         tmp = buf_ptr->buf_cur;
496
497         if (cnt - offset > MAX_WCP_BUF_LEN) {
498                 if (tree)
499                         proto_tree_add_text( tree, src_tvb, offset, -1,
500                                 "Compressed data exceeds maximum buffer length (%d > %d)",
501                                 cnt - offset, MAX_WCP_BUF_LEN);
502                 return NULL;
503         }
504
505         src = tvb_memcpy(src_tvb, src_buf, offset, cnt - offset);
506         dst = buf_ptr->buf_cur;
507
508         while( offset++ < cnt){
509
510                 if ( --i >= 0){
511                         if ( comp_flag_bits & 0x80){    /* if this is a compressed entry */
512
513                                 if ( !pinfo->fd->flags.visited){        /* if first pass */
514                                         dst = decompressed_entry( src, dst, &len, buf_start, buf_end);
515                                 }
516                                 if ((*src & 0xf0) == 0x10){
517                                         if ( tree) {
518                                                 ti = proto_tree_add_item( tree, hf_wcp_long_run, src_tvb,
519                                                          offset-1, 3, 0);
520                                                 sub_tree = proto_item_add_subtree(ti, ett_wcp_field);
521                                                 proto_tree_add_uint(sub_tree, hf_wcp_offset, src_tvb,
522                                                          offset-1, 2, pntohs(src));
523
524                                                 proto_tree_add_item( sub_tree, hf_wcp_long_len, src_tvb,
525                                                          offset+1, 1, pntohs(src));
526                                         }
527                                         src += 3;
528                                         offset += 2;
529                                 }else{
530                                         if ( tree) {
531                                                 ti = proto_tree_add_item( tree, hf_wcp_short_run, src_tvb,
532                                                          offset - 1, 2, *src);
533                                                 sub_tree = proto_item_add_subtree(ti, ett_wcp_field);
534                                                 proto_tree_add_item( sub_tree, hf_wcp_short_len, src_tvb,
535                                                          offset-1, 1, *src);
536                                                 proto_tree_add_uint(sub_tree, hf_wcp_offset, src_tvb,
537                                                          offset-1, 2, pntohs(src));
538                                         }
539                                         src += 2;
540                                         offset += 1;
541                                 }
542                         }else {
543                                 if ( !pinfo->fd->flags.visited){        /* if first pass */
544                                         *dst = *src;
545                                         if ( dst++ == buf_end)
546                                                 dst = buf_start;
547                                 }
548                                 ++src;
549                                 ++len;
550
551                         }
552
553                         if ( len >MAX_WCP_BUF_LEN){
554                                 return NULL;
555                         }
556
557                         comp_flag_bits <<= 1;
558
559                 }else { /* compressed data flag */
560
561                         comp_flag_bits = *src++;
562                         if (tree)
563                                 proto_tree_add_uint( tree, hf_wcp_comp_bits,  src_tvb, offset-1, 1,
564                                         comp_flag_bits);
565                                 
566                         i = 8;
567                 }
568         }
569
570         if ( pinfo->fd->flags.visited){ /* if not first pass */
571                                         /* get uncompressed data */
572                 pdata_ptr = p_get_proto_data( pinfo->fd, proto_wcp);
573
574                 if ( !pdata_ptr)        /* exit if no data */
575                         return NULL;
576                 len = pdata_ptr->len;   
577         } else {
578
579         /* save the new data as per packet data */
580                 pdata_ptr = g_mem_chunk_alloc( wcp_pdata);
581                 memcpy( &pdata_ptr->buffer, buf_ptr->buf_cur,  len);
582                 pdata_ptr->len = len;
583
584                 p_add_proto_data( pinfo->fd, proto_wcp, (void*)pdata_ptr);      
585
586                 buf_ptr->buf_cur = dst;
587         }
588
589
590         TRY {
591                 tvb = tvb_new_real_data( pdata_ptr->buffer, pdata_ptr->len, pdata_ptr->len);
592
593         }
594         CATCH(BoundsError) {
595                 g_assert_not_reached();
596                 g_free(buf);
597                 return NULL;
598         }
599         CATCH(ReportedBoundsError) {
600                 g_free(buf);
601                 return NULL;
602         }
603         ENDTRY; 
604
605         /* link new tvbuff into tvbuff chain so cleanup is done later */
606         tvb_set_child_real_data_tvbuff( src_tvb, tvb);
607
608         /* Add new data to the data source list */
609         add_new_data_source( pinfo, tvb, "Uncompressed WCP");
610         return tvb;
611
612 }
613
614
615 static void wcp_reinit( void){
616
617 /* Do the cleanup work when a new pass through the packet list is       */
618 /* performed. re-initialize the  memory chunks.                         */
619
620         if (wcp_window)
621                 g_mem_chunk_destroy(wcp_window);
622
623         wcp_window = g_mem_chunk_new("wcp_window", wcp_win_length,
624                 wcp_win_init_count * wcp_win_length,
625                 G_ALLOC_AND_FREE);
626
627         if (wcp_pdata)
628                 g_mem_chunk_destroy(wcp_pdata);
629
630         wcp_pdata = g_mem_chunk_new("wcp_pdata", wcp_packet_length,
631                 wcp_packet_init_count * wcp_packet_length,
632                 G_ALLOC_AND_FREE);
633
634 }
635
636
637 void
638 proto_register_wcp(void)
639 {
640     static hf_register_info hf[] = {
641         { &hf_wcp_cmd,
642           { "Command", "wcp.cmd", FT_UINT8, BASE_HEX, VALS(cmd_string), WCP_CMD, 
643                 "Compression Command", HFILL }},
644         { &hf_wcp_ext_cmd,
645           { "Extended Command", "wcp.ext_cmd", FT_UINT8, BASE_HEX, VALS(ext_cmd_string), WCP_EXT_CMD, 
646                 "Extended Compression Command", HFILL }},
647         { &hf_wcp_seq,
648           { "SEQ", "wcp.seq", FT_UINT16, BASE_HEX, NULL, WCP_SEQ, 
649                 "Sequence Number", HFILL }},
650         { &hf_wcp_chksum,
651           { "Checksum", "wcp.checksum", FT_UINT8, BASE_DEC, NULL, 0,
652                 "Packet Checksum", HFILL }},
653         { &hf_wcp_tid,
654           { "TID", "wcp.tid", FT_UINT16, BASE_DEC, NULL, 0,
655                 "TID", HFILL }},
656         { &hf_wcp_rev,
657           { "Revision", "wcp.rev", FT_UINT8, BASE_DEC, NULL, 0,
658                 "Revision", HFILL }},
659         { &hf_wcp_init,
660           { "Initiator", "wcp.init", FT_UINT8, BASE_DEC, NULL, 0,
661                 "Initiator", HFILL }},
662         { &hf_wcp_seq_size,
663           { "Seq Size", "wcp.seq_size", FT_UINT8, BASE_DEC, NULL, 0,
664                 "Sequence Size", HFILL }},
665         { &hf_wcp_alg_cnt,
666           { "Alg Count", "wcp.alg_cnt", FT_UINT8, BASE_DEC, NULL, 0,
667                 "Algorithm Count", HFILL }},
668         { &hf_wcp_alg_a,
669           { "Alg 1", "wcp.alg1", FT_UINT8, BASE_DEC, NULL, 0,
670                 "Algorithm #1", HFILL }},
671         { &hf_wcp_alg_b,
672           { "Alg 2", "wcp.alg2", FT_UINT8, BASE_DEC, NULL, 0,
673                 "Algorithm #2", HFILL }},
674         { &hf_wcp_alg_c,
675           { "Alg 3", "wcp.alg3", FT_UINT8, BASE_DEC, NULL, 0,
676                 "Algorithm #3", HFILL }},
677         { &hf_wcp_alg_d,
678           { "Alg 4", "wcp.alg4", FT_UINT8, BASE_DEC, NULL, 0,
679                 "Algorithm #4", HFILL }},
680         { &hf_wcp_alg,
681           { "Alg", "wcp.alg", FT_UINT8, BASE_DEC, NULL, 0,
682                 "Algorithm", HFILL }},
683         { &hf_wcp_rexmit,
684           { "Rexmit", "wcp.rexmit", FT_UINT8, BASE_DEC, NULL, 0,
685                 "Retransmit", HFILL }},
686         { &hf_wcp_hist_size,
687           { "History", "wcp.hist", FT_UINT8, BASE_DEC, NULL, 0,
688                 "History Size", HFILL }},
689         { &hf_wcp_ppc,
690           { "PerPackComp", "wcp.ppc", FT_UINT8, BASE_DEC, NULL, 0,
691                 "Per Packet Compression", HFILL }},
692         { &hf_wcp_pib,
693           { "PIB", "wcp.pib", FT_UINT8, BASE_DEC, NULL, 0,
694                 "PIB", HFILL }},
695         { &hf_wcp_comp_bits,
696           { "Compress Flag", "wcp.flag", FT_UINT8, BASE_HEX, NULL, 0,
697                 "Compressed byte flag", HFILL }},
698         { &hf_wcp_comp_marker,
699           { "Compress Marker", "wcp.mark", FT_UINT8, BASE_BIN, NULL, 0,
700                 "Compressed marker", HFILL }},
701         { &hf_wcp_offset,
702           { "Source offset", "wcp.off", FT_UINT16, BASE_HEX, NULL, WCP_OFFSET_MASK,
703                 "Data source offset", HFILL }},
704         { &hf_wcp_short_len,
705           { "Compress Length", "wcp.short_len", FT_UINT8, BASE_HEX, NULL, 0xf0,
706                 "Compressed length", HFILL }},
707         { &hf_wcp_long_len,
708           { "Compress Length", "wcp.long_len", FT_UINT8, BASE_HEX, NULL, 0,
709                 "Compressed length", HFILL }},
710         { &hf_wcp_long_run,
711           { "Long Compression", "wcp.long_comp", FT_UINT16, BASE_HEX, NULL, 0,
712                 "Long Compression type", HFILL }},
713         { &hf_wcp_short_run,
714           { "Short Compression", "wcp.short_comp", FT_UINT8, BASE_HEX, NULL, 0,
715                 "Short Compression type", HFILL }},
716
717    };
718
719
720     static gint *ett[] = {
721         &ett_wcp,
722         &ett_wcp_field,
723     };
724
725     proto_wcp = proto_register_protocol ("Wellfleet Compression", "WCP", "wcp");
726     proto_register_field_array (proto_wcp, hf, array_length(hf));
727     proto_register_subtree_array(ett, array_length(ett));
728     register_init_routine(&wcp_reinit);
729 }
730
731
732 void
733 proto_reg_handoff_wcp(void) {
734     dissector_handle_t wcp_handle;
735
736     /*
737      * Get handle for the Frame Relay (uncompressed) dissector.
738      */
739     fr_uncompressed_handle = find_dissector("fr_uncompressed");
740
741     wcp_handle = create_dissector_handle(dissect_wcp, proto_wcp);
742     dissector_add("fr.ietf", NLPID_COMPRESSED, wcp_handle);
743     dissector_add("ethertype",  ETHERTYPE_WCP, wcp_handle);
744 }