Don't prime the display filter unless we're re-applying it; otherwise,
[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.23 2002/04/11 09:38:03 guy 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 #ifdef HAVE_SYS_TYPES_H
104 # include <sys/types.h>
105 #endif
106
107 #include <stdio.h>
108 #include <glib.h>
109 #include <string.h>
110 #include <epan/packet.h>
111 #include "packet-frame.h"
112 #include <epan/conversation.h>
113 #include "etypes.h"
114 #include "nlpid.h"
115
116 #define MAX_WIN_BUF_LEN 0x7fff          /* storage size for decompressed data */
117 #define MAX_WCP_BUF_LEN 2048            /* storage size for decompressed data */
118 #define FROM_DCE        0x80            /* for direction setting */
119
120 typedef struct {
121
122         guint8  *buf_cur;
123         guint8  buffer[MAX_WIN_BUF_LEN];
124
125 }wcp_window_t;
126
127 /*XXX do I really want the length in here  */
128 typedef struct {
129
130         guint16  len;
131         guint8  buffer[MAX_WCP_BUF_LEN];
132
133 }wcp_pdata_t;
134
135
136 #define wcp_win_init_count 4
137 #define wcp_packet_init_count 10
138
139 #define wcp_win_length (sizeof(wcp_window_t))
140 #define wcp_packet_length (sizeof(wcp_pdata_t))
141
142 static GMemChunk *wcp_window = NULL;
143 static GMemChunk *wcp_pdata = NULL;
144
145 static int proto_wcp = -1;
146 static int hf_wcp_cmd = -1;
147 static int hf_wcp_ext_cmd = -1;
148 static int hf_wcp_seq = -1;
149 static int hf_wcp_chksum = -1;
150 static int hf_wcp_tid = -1;
151 static int hf_wcp_rev = -1;
152 static int hf_wcp_init = -1;
153 static int hf_wcp_seq_size = -1;
154 static int hf_wcp_alg = -1;
155 static int hf_wcp_alg_cnt = -1;
156 static int hf_wcp_alg_a = -1;
157 static int hf_wcp_alg_b = -1;
158 static int hf_wcp_alg_c = -1;
159 static int hf_wcp_alg_d = -1;
160 static int hf_wcp_rexmit = -1;
161
162 static int hf_wcp_hist_size = -1;
163 static int hf_wcp_ppc = -1;
164 static int hf_wcp_pib = -1;
165
166 static int hf_wcp_comp_bits = -1;
167 static int hf_wcp_comp_marker = -1;
168 static int hf_wcp_short_len = -1;
169 static int hf_wcp_long_len = -1;
170 static int hf_wcp_short_run = -1;
171 static int hf_wcp_long_run = -1;
172 static int hf_wcp_offset = -1;
173
174 static gint ett_wcp = -1;
175 static gint ett_wcp_field = -1;
176
177 static dissector_handle_t fr_handle;
178
179 /*
180  * Bits in the address field.
181  */
182 #define WCP_CMD                 0xf0    /* WCP Command */
183 #define WCP_EXT_CMD             0x0f    /* WCP Extended Command */
184 #define WCP_SEQ                 0x0fff  /* WCP Sequence number */
185 #define WCP_OFFSET_MASK         0x0fff  /* WCP Pattern source offset */
186
187 #define PPC_COMPRESSED_IND              0x0
188 #define PPC_UNCOMPRESSED_IND            0x1
189 #define PPC_TPPC_COMPRESSED_IND         0x2
190 #define PPC_TPPC_UNCOMPRESSED_IND       0x3
191 #define CONNECT_REQ                     0x4
192 #define CONNECT_ACK                     0x5
193 #define CONNECT_NAK                     0x6
194 #define DISCONNECT_REQ                  0x7
195 #define DISCONNECT_ACK                  0x8
196 #define INIT_REQ                        0x9
197 #define INIT_ACK                        0xa
198 #define RESET_REQ                       0xb
199 #define RESET_ACK                       0xc
200 #define REXMIT_NAK                      0xd
201
202
203 static const value_string cmd_string[] = {
204         {0, "Compressed Data"},
205         {1, "Uncompressed Data"},
206         {15, "Extended"},
207         { 0,       NULL }
208         };
209
210 static const value_string ext_cmd_string[] = {
211         {0, "Per Packet Compression"},
212         {4, "Connect Req"},
213         {5, "Connect Ack"},
214         {9, "Init Req"},
215         {0x0a, "Init Ack"},
216
217         { 0,       NULL }
218         };
219
220
221
222 static tvbuff_t *wcp_uncompress( tvbuff_t *src_tvb, int offset, packet_info *pinfo, proto_tree *tree);
223 static wcp_window_t *get_wcp_window_ptr( packet_info *pinfo);
224
225 static void
226 dissect_wcp_con_req(tvbuff_t *tvb, int offset, proto_tree *tree) {
227
228 /* WCP connector request message */
229
230         guint alg_cnt = tvb_get_guint8(tvb, 5);
231
232         proto_tree_add_uint(tree, hf_wcp_tid, tvb, offset, 2, tvb_get_ntohs(tvb, offset));
233         proto_tree_add_uint(tree, hf_wcp_rev, tvb, offset + 2, 1, tvb_get_guint8(tvb, offset + 2));
234         proto_tree_add_uint(tree, hf_wcp_init, tvb, offset + 3, 1, tvb_get_guint8(tvb, offset + 3));
235         proto_tree_add_uint(tree, hf_wcp_seq_size, tvb, offset + 4, 1, tvb_get_guint8(tvb, offset + 4));
236         proto_tree_add_uint(tree, hf_wcp_alg_cnt, tvb, offset + 5, 1, alg_cnt);
237         proto_tree_add_uint(tree, hf_wcp_alg_a, tvb, offset + 6, 1, tvb_get_guint8(tvb, offset + 6));
238         if ( alg_cnt > 1)
239                 proto_tree_add_uint(tree, hf_wcp_alg_b, tvb, offset + 7, 1, tvb_get_guint8(tvb, offset + 7));
240         if ( alg_cnt > 2)
241                 proto_tree_add_uint(tree, hf_wcp_alg_c, tvb, offset + 8, 1, tvb_get_guint8(tvb, offset + 8));
242         if ( alg_cnt > 3)
243                 proto_tree_add_uint(tree, hf_wcp_alg_d, tvb, offset + 9, 1, tvb_get_guint8(tvb, offset + 9));
244 }
245
246 static void
247 dissect_wcp_con_ack( tvbuff_t *tvb, int offset, proto_tree *tree){
248
249 /* WCP connector ack message */
250
251         proto_tree_add_uint(tree, hf_wcp_tid, tvb, offset, 2, tvb_get_ntohs(tvb, offset));
252         proto_tree_add_uint(tree, hf_wcp_rev, tvb, offset + 2, 1, tvb_get_guint8(tvb, offset + 2));
253         proto_tree_add_uint(tree, hf_wcp_seq_size, tvb, offset + 3, 1, tvb_get_guint8(tvb, offset + 3));
254         proto_tree_add_uint(tree, hf_wcp_alg, tvb, offset + 4, 1, tvb_get_guint8(tvb, offset + 4));
255 }
256
257 static void
258 dissect_wcp_init( tvbuff_t *tvb, int offset, proto_tree *tree){
259
260 /* WCP Initiate Request/Ack message */
261
262         proto_tree_add_uint(tree, hf_wcp_tid, tvb, offset, 2, tvb_get_ntohs(tvb, offset));
263         proto_tree_add_uint(tree, hf_wcp_rev, tvb, offset + 2, 1, tvb_get_guint8(tvb, offset + 2));
264         proto_tree_add_uint(tree, hf_wcp_hist_size, tvb, offset + 3, 1, tvb_get_guint8(tvb, offset + 3));
265         proto_tree_add_uint(tree, hf_wcp_ppc, tvb, offset + 4, 1, tvb_get_guint8(tvb, offset + 4));
266         proto_tree_add_uint(tree, hf_wcp_pib, tvb, offset + 5, 1, tvb_get_guint8(tvb, offset + 5));
267 }
268
269
270 static void
271 dissect_wcp_reset( tvbuff_t *tvb, int offset, proto_tree *tree){
272
273 /* Process WCP Reset Request/Ack message */
274
275         proto_tree_add_uint(tree, hf_wcp_tid, tvb, offset, 2, tvb_get_ntohs(tvb, offset));
276 }
277
278
279 static void wcp_save_data( tvbuff_t *tvb, packet_info *pinfo){
280
281         wcp_window_t *buf_ptr = 0;
282         int len; 
283
284         /* discard first 2 bytes, header and last byte (check byte) */
285         len = tvb_reported_length( tvb)-3;
286         buf_ptr = get_wcp_window_ptr( pinfo); 
287
288         if (( buf_ptr->buf_cur + len) <= (buf_ptr->buffer + MAX_WIN_BUF_LEN)){
289                 tvb_memcpy( tvb, buf_ptr->buf_cur, 2, len);
290                 buf_ptr->buf_cur = buf_ptr->buf_cur + len;
291         
292         } else {
293                 guint8 *buf_end = buf_ptr->buffer + MAX_WIN_BUF_LEN; 
294                 tvb_memcpy( tvb, buf_ptr->buf_cur, 2, buf_end - buf_ptr->buf_cur);
295                 tvb_memcpy( tvb, buf_ptr->buffer, buf_end - buf_ptr->buf_cur-2,
296                         len - (int)(buf_end - buf_ptr->buf_cur));
297                 buf_ptr->buf_cur = buf_ptr->buf_cur + len - MAX_WIN_BUF_LEN;
298         }
299
300 }
301
302
303 static void dissect_wcp( tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
304
305         proto_tree      *wcp_tree;
306         proto_item      *ti;
307         int             wcp_header_len;
308         guint16         temp, cmd, ext_cmd, seq;
309         tvbuff_t        *next_tvb;
310
311         if (check_col(pinfo->cinfo, COL_PROTOCOL))
312                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "WCP");
313         if (check_col(pinfo->cinfo, COL_INFO))
314                 col_clear(pinfo->cinfo, COL_INFO);
315
316         temp =tvb_get_ntohs(tvb, 0); 
317
318         cmd = (temp & 0xf000) >> 12;
319         ext_cmd = (temp & 0x0f00) >> 8;
320
321         if ( cmd == 0xf)
322                 wcp_header_len= 1;
323         else 
324                 wcp_header_len= 2;
325
326         seq = temp & 0x0fff;
327
328 /*XXX should test seq to be sure it the last + 1 !! */
329
330         if (check_col(pinfo->cinfo, COL_INFO)){
331                 col_add_str(pinfo->cinfo, COL_INFO, val_to_str(cmd, cmd_string, "Unknown"));
332                 if ( cmd == 0xf)
333                         col_append_fstr(pinfo->cinfo, COL_INFO, ", %s",
334                                 val_to_str(ext_cmd, ext_cmd_string, "Unknown"));
335         }       
336
337         if (tree) {
338                 ti = proto_tree_add_item(tree, proto_wcp, tvb, 0, wcp_header_len, FALSE);
339
340                 wcp_tree = proto_item_add_subtree(ti, ett_wcp);
341
342                 proto_tree_add_uint(wcp_tree, hf_wcp_cmd, tvb, 0, 1, tvb_get_guint8( tvb, 0));
343                 if ( cmd == 0xf){
344                         proto_tree_add_uint(wcp_tree, hf_wcp_ext_cmd, tvb, 1, 1,
345                                         tvb_get_guint8( tvb, 0));
346                         switch (ext_cmd){
347                         case CONNECT_REQ:
348                                 dissect_wcp_con_req( tvb, 1, wcp_tree);
349                                 break;
350
351                         case CONNECT_ACK:
352                                 dissect_wcp_con_ack( tvb, 1, wcp_tree);
353                                 break;
354                         case INIT_REQ:
355                         case INIT_ACK:
356                                 dissect_wcp_init( tvb, 1, wcp_tree);
357                                 break;
358                         case RESET_REQ:
359                         case RESET_ACK:
360                                 dissect_wcp_reset( tvb, 1, wcp_tree);
361                                 break;
362                         default:
363                                 break;
364                         }
365                 }else { 
366                         proto_tree_add_uint(wcp_tree, hf_wcp_seq,  tvb, 0, 2, seq);
367                 }
368         }
369         else {
370                 wcp_tree = NULL;
371         }
372
373
374                                         /* exit if done */
375         if ( cmd != 1 && cmd != 0 && !(cmd == 0xf && ext_cmd == 0)) 
376                 return;
377
378         if ( cmd == 1) {                /* uncompressed data */
379                 if ( !pinfo->fd->flags.visited){        /* if first pass */
380                         wcp_save_data( tvb, pinfo);
381                 }
382                 next_tvb = tvb_new_subset(tvb, wcp_header_len, -1, -1);
383         }
384         else {          /* cmd == 0 || (cmd == 0xf && ext_cmd == 0) */
385
386                 next_tvb = wcp_uncompress( tvb, wcp_header_len, pinfo, wcp_tree);
387
388                 if ( !next_tvb){
389                         proto_tree_add_protocol_format(tree, proto_malformed, tvb, 0, 0,
390                                         "[Malformed Frame: Bad WCP compressed data]" );
391                         return;
392                 }
393         }
394
395         if ( tree)                      /* add the check byte */
396                 proto_tree_add_uint(wcp_tree, hf_wcp_chksum, tvb,
397                         tvb_reported_length( tvb)-1, 1,
398                         tvb_get_guint8( tvb, tvb_reported_length(tvb)-1));
399
400         call_dissector(fr_handle, next_tvb, pinfo, tree);
401
402         return;
403 }
404
405
406 static guint8 *decompressed_entry( guint8 *src, guint8 *dst, int *len, guint8 * buf_start, guint8 *buf_end){
407
408 /* do the decompression for one field */
409
410         guint16 data_offset, data_cnt;
411         guint8 tmp = *src;
412
413         data_offset = (*(src++) & 0xf) << 8;    /* get high byte */
414         data_offset += *(src++);                /* add next byte */
415
416         if (( tmp & 0xf0) == 0x10){             /* 2 byte count */
417                 data_cnt = *src;
418                 data_cnt++;
419
420         }else {                                 /* one byte count */
421                 data_cnt = tmp >> 4;
422                 data_cnt++;
423         }               
424
425         
426         src = (dst - 1 - data_offset);
427         if ( src < buf_start)
428                 src += MAX_WIN_BUF_LEN;
429
430
431 /*XXX could do some fancy memory moves, later if speed is problem */
432
433         while( data_cnt--){
434                 *dst = *src;
435                 if ( ++(*len) >MAX_WCP_BUF_LEN){
436                         printf("decomp failed, len = %d\n",  *len);
437
438                         return NULL;    /* end of buffer error */
439                 }
440                 if ( dst++ == buf_end)
441                         dst = buf_start;
442                 if ( src++ == buf_end)
443                         src = buf_start;
444
445         }
446         return dst;
447 }
448
449
450 static 
451 wcp_window_t *get_wcp_window_ptr( packet_info *pinfo){
452
453 /* find the conversation for this side of the DLCI, create one if needed */
454 /* and return the wcp_window data structure pointer */
455
456         conversation_t *conv;
457         wcp_window_t *wcp_win_data;
458
459         conv = find_conversation( &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), 0);
462         if ( !conv){
463                 conv = conversation_new( &pinfo->dl_src, &pinfo->dl_src, PT_NONE, 
464                         ((pinfo->pseudo_header->x25.flags & FROM_DCE)? 1:0), 
465                         ((pinfo->pseudo_header->x25.flags & FROM_DCE)? 1:0),
466                         0);
467         }
468         wcp_win_data = conversation_get_proto_data(conv, proto_wcp);
469         if ( !wcp_win_data){
470                 wcp_win_data = g_mem_chunk_alloc( wcp_window);
471                 wcp_win_data->buf_cur = wcp_win_data->buffer;
472                 conversation_add_proto_data(conv, proto_wcp, wcp_win_data);
473         }
474
475         return wcp_win_data;
476 }
477
478
479 static tvbuff_t *wcp_uncompress( tvbuff_t *src_tvb, int offset, packet_info *pinfo, proto_tree *tree) {
480
481 /* do the packet data uncompression and load it into the dst buffer */
482
483         proto_tree      *sub_tree;
484         proto_item      *ti;
485
486         int len=0, i = -1;
487         int cnt = tvb_reported_length( src_tvb)-1;      /* don't include check byte */
488
489         guint8 *buf = 0, *dst, *src, *buf_start, *buf_end,  *tmp, comp_flag_bits = 0;
490         guint8 src_buf[ MAX_WCP_BUF_LEN];
491         tvbuff_t *volatile tvb = 0;
492         wcp_window_t *buf_ptr = 0;
493         wcp_pdata_t *volatile pdata_ptr;
494
495         buf_ptr = get_wcp_window_ptr( pinfo); 
496
497         buf_start = buf_ptr->buffer;
498         buf_end = buf_start + MAX_WIN_BUF_LEN;
499         tmp = buf_ptr->buf_cur;
500
501         src = tvb_memcpy(src_tvb, src_buf, offset, cnt - offset);
502         dst = buf_ptr->buf_cur;
503
504         while( offset++ < cnt){
505
506                 if ( --i >= 0){
507                         if ( comp_flag_bits & 0x80){    /* if this is a compressed entry */
508
509                                 if ( !pinfo->fd->flags.visited){        /* if first pass */
510                                         dst = decompressed_entry( src, dst, &len, buf_start, buf_end);
511                                 }
512                                 if ((*src & 0xf0) == 0x10){
513                                         if ( tree) {
514                                                 ti = proto_tree_add_item( tree, hf_wcp_long_run, src_tvb,
515                                                          offset-1, 3, 0);
516                                                 sub_tree = proto_item_add_subtree(ti, ett_wcp_field);
517                                                 proto_tree_add_uint(sub_tree, hf_wcp_offset, src_tvb,
518                                                          offset-1, 2, pntohs(src));
519
520                                                 proto_tree_add_item( sub_tree, hf_wcp_long_len, src_tvb,
521                                                          offset+1, 1, pntohs(src));
522                                         }
523                                         src += 3;
524                                         offset += 2;
525                                 }else{
526                                         if ( tree) {
527                                                 ti = proto_tree_add_item( tree, hf_wcp_short_run, src_tvb,
528                                                          offset - 1, 2, *src);
529                                                 sub_tree = proto_item_add_subtree(ti, ett_wcp_field);
530                                                 proto_tree_add_item( sub_tree, hf_wcp_short_len, src_tvb,
531                                                          offset-1, 1, *src);
532                                                 proto_tree_add_uint(sub_tree, hf_wcp_offset, src_tvb,
533                                                          offset-1, 2, pntohs(src));
534                                         }
535                                         src += 2;
536                                         offset += 1;
537                                 }
538                         }else {
539                                 if ( !pinfo->fd->flags.visited){        /* if first pass */
540                                         *dst = *src;
541                                         if ( dst++ == buf_end)
542                                                 dst = buf_start;
543                                 }
544                                 ++src;
545                                 ++len;
546
547                         }
548
549                         if ( len >MAX_WCP_BUF_LEN){
550                                 return NULL;
551                         }
552
553                         comp_flag_bits <<= 1;
554
555                 }else { /* compressed data flag */
556
557                         comp_flag_bits = *src++;
558                         if (tree)
559                                 proto_tree_add_uint( tree, hf_wcp_comp_bits,  src_tvb, offset-1, 1,
560                                         comp_flag_bits);
561                                 
562                         i = 8;
563                 }
564         }
565
566         if ( pinfo->fd->flags.visited){ /* if not first pass */
567                                         /* get uncompressed data */
568                 pdata_ptr = p_get_proto_data( pinfo->fd, proto_wcp);
569
570                 if ( !pdata_ptr)        /* exit if no data */
571                         return NULL;
572                 len = pdata_ptr->len;   
573         } else {
574
575         /* save the new data as per packet data */
576                 pdata_ptr = g_mem_chunk_alloc( wcp_pdata);
577                 memcpy( &pdata_ptr->buffer, buf_ptr->buf_cur,  len);
578                 pdata_ptr->len = len;
579
580                 p_add_proto_data( pinfo->fd, proto_wcp, (void*)pdata_ptr);      
581
582                 buf_ptr->buf_cur = dst;
583         }
584
585
586         TRY {
587                 tvb = tvb_new_real_data( pdata_ptr->buffer, pdata_ptr->len, pdata_ptr->len);
588
589         }
590         CATCH(BoundsError) {
591                 g_assert_not_reached();
592                 g_free(buf);
593                 return NULL;
594         }
595         CATCH(ReportedBoundsError) {
596                 g_free(buf);
597                 return NULL;
598         }
599         ENDTRY; 
600
601         /* link new tvbuff into tvbuff chain so cleanup is done later */
602         tvb_set_child_real_data_tvbuff( src_tvb, tvb);
603
604         /* Add new data to the data source list */
605         add_new_data_source( pinfo->fd, tvb, "Uncompressed WCP");
606         return tvb;
607
608 }
609
610
611 static void wcp_reinit( void){
612
613 /* Do the cleanup work when a new pass through the packet list is       */
614 /* performed. re-initialize the  memory chunks.                         */
615
616         if (wcp_window)
617                 g_mem_chunk_destroy(wcp_window);
618
619         wcp_window = g_mem_chunk_new("wcp_window", wcp_win_length,
620                 wcp_win_init_count * wcp_win_length,
621                 G_ALLOC_AND_FREE);
622
623         if (wcp_pdata)
624                 g_mem_chunk_destroy(wcp_pdata);
625
626         wcp_pdata = g_mem_chunk_new("wcp_pdata", wcp_packet_length,
627                 wcp_packet_init_count * wcp_packet_length,
628                 G_ALLOC_AND_FREE);
629
630 }
631
632
633 void
634 proto_register_wcp(void)
635 {
636     static hf_register_info hf[] = {
637         { &hf_wcp_cmd,
638           { "Command", "wcp.cmd", FT_UINT8, BASE_HEX, VALS(cmd_string), WCP_CMD, 
639                 "Compression Command", HFILL }},
640         { &hf_wcp_ext_cmd,
641           { "Extended Command", "wcp.ext_cmd", FT_UINT8, BASE_HEX, VALS(ext_cmd_string), WCP_EXT_CMD, 
642                 "Extended Compression Command", HFILL }},
643         { &hf_wcp_seq,
644           { "SEQ", "wcp.seq", FT_UINT16, BASE_HEX, NULL, WCP_SEQ, 
645                 "Sequence Number", HFILL }},
646         { &hf_wcp_chksum,
647           { "Checksum", "wcp.checksum", FT_UINT8, BASE_DEC, NULL, 0,
648                 "Packet Checksum", HFILL }},
649         { &hf_wcp_tid,
650           { "TID", "wcp.tid", FT_UINT16, BASE_DEC, NULL, 0,
651                 "TID", HFILL }},
652         { &hf_wcp_rev,
653           { "Revision", "wcp.rev", FT_UINT8, BASE_DEC, NULL, 0,
654                 "Revision", HFILL }},
655         { &hf_wcp_init,
656           { "Initiator", "wcp.init", FT_UINT8, BASE_DEC, NULL, 0,
657                 "Initiator", HFILL }},
658         { &hf_wcp_seq_size,
659           { "Seq Size", "wcp.seq_size", FT_UINT8, BASE_DEC, NULL, 0,
660                 "Sequence Size", HFILL }},
661         { &hf_wcp_alg_cnt,
662           { "Alg Count", "wcp.alg_cnt", FT_UINT8, BASE_DEC, NULL, 0,
663                 "Algorithm Count", HFILL }},
664         { &hf_wcp_alg_a,
665           { "Alg 1", "wcp.alg1", FT_UINT8, BASE_DEC, NULL, 0,
666                 "Algorithm #1", HFILL }},
667         { &hf_wcp_alg_b,
668           { "Alg 2", "wcp.alg2", FT_UINT8, BASE_DEC, NULL, 0,
669                 "Algorithm #2", HFILL }},
670         { &hf_wcp_alg_c,
671           { "Alg 3", "wcp.alg3", FT_UINT8, BASE_DEC, NULL, 0,
672                 "Algorithm #3", HFILL }},
673         { &hf_wcp_alg_d,
674           { "Alg 4", "wcp.alg4", FT_UINT8, BASE_DEC, NULL, 0,
675                 "Algorithm #4", HFILL }},
676         { &hf_wcp_alg,
677           { "Alg", "wcp.alg", FT_UINT8, BASE_DEC, NULL, 0,
678                 "Algorithm", HFILL }},
679         { &hf_wcp_rexmit,
680           { "Rexmit", "wcp.rexmit", FT_UINT8, BASE_DEC, NULL, 0,
681                 "Retransmit", HFILL }},
682         { &hf_wcp_hist_size,
683           { "History", "wcp.hist", FT_UINT8, BASE_DEC, NULL, 0,
684                 "History Size", HFILL }},
685         { &hf_wcp_ppc,
686           { "PerPackComp", "wcp.ppc", FT_UINT8, BASE_DEC, NULL, 0,
687                 "Per Packet Compression", HFILL }},
688         { &hf_wcp_pib,
689           { "PIB", "wcp.pib", FT_UINT8, BASE_DEC, NULL, 0,
690                 "PIB", HFILL }},
691         { &hf_wcp_comp_bits,
692           { "Compress Flag", "wcp.flag", FT_UINT8, BASE_HEX, NULL, 0,
693                 "Compressed byte flag", HFILL }},
694         { &hf_wcp_comp_marker,
695           { "Compress Marker", "wcp.mark", FT_UINT8, BASE_BIN, NULL, 0,
696                 "Compressed marker", HFILL }},
697         { &hf_wcp_offset,
698           { "Source offset", "wcp.off", FT_UINT16, BASE_HEX, NULL, WCP_OFFSET_MASK,
699                 "Data source offset", HFILL }},
700         { &hf_wcp_short_len,
701           { "Compress Length", "wcp.short_len", FT_UINT8, BASE_HEX, NULL, 0xf0,
702                 "Compressed length", HFILL }},
703         { &hf_wcp_long_len,
704           { "Compress Length", "wcp.long_len", FT_UINT8, BASE_HEX, NULL, 0,
705                 "Compressed length", HFILL }},
706         { &hf_wcp_long_run,
707           { "Long Compression", "wcp.long_comp", FT_UINT16, BASE_HEX, NULL, 0,
708                 "Long Compression type", HFILL }},
709         { &hf_wcp_short_run,
710           { "Short Compression", "wcp.short_comp", FT_UINT8, BASE_HEX, NULL, 0,
711                 "Short Compression type", HFILL }},
712
713    };
714
715
716     static gint *ett[] = {
717         &ett_wcp,
718         &ett_wcp_field,
719     };
720
721     proto_wcp = proto_register_protocol ("Wellfleet Compression", "WCP", "wcp");
722     proto_register_field_array (proto_wcp, hf, array_length(hf));
723     proto_register_subtree_array(ett, array_length(ett));
724     register_init_routine(&wcp_reinit);
725 }
726
727
728 void
729 proto_reg_handoff_wcp(void) {
730     dissector_handle_t wcp_handle;
731
732     /*
733      * Get handle for the Frame Relay (uncompressed) dissector.
734      */
735     fr_handle = find_dissector("fr");
736
737     wcp_handle = create_dissector_handle(dissect_wcp, proto_wcp);
738     dissector_add("fr.ietf", NLPID_COMPRESSED, wcp_handle);
739     dissector_add("ethertype",  ETHERTYPE_WCP, wcp_handle);
740 }