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