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