aboutsummaryrefslogtreecommitdiffstats
path: root/packet-wcp.c
blob: c714005d69aa56a95f5d78cf51282d640a4cbfa9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
/* packet-wcp.c
 * Routines for Wellfleet Compression frame disassembly
 * Copyright 2001, Jeffrey C. Foster <jfoste@woodward.com>
 *
 * $Id: packet-wcp.c,v 1.31 2003/01/31 03:17:47 guy Exp $
 *
 * Ethereal - Network traffic analyzer
 * By Gerald Combs <gerald@ethereal.com>
 * Copyright 1998
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * ToDo:
 *	Add preference to allow/disallow decompression
 *	Calculate and verify check byte (last byte), if only we knew how!
 *	Handle Wellfleet compression over PPP links.
 *		- This will require changing the sub-dissector call
 *		  routine to determine if layer 2 is frame relay or
 *		  or PPP and different sub-dissector routines for each.
 *
 * Based upon information in the Nortel TCL based Pcaptap code.
 *http://www.mynetworkforum.com/tools/PCAPTAP/pcaptap-Win32-3.00.exe
 *
 * And lzss algorithm
 *http://www.rasip.fer.hr/research/compress/algorithms/fund/lz/lzss.html
 */

/*
 * Wellfleet compression is a variation on LZSS encoding.
 *
 * Compression is done by keeping a sliding window of previous
 * data transmited. The sender will use a pattern match to
 * encode repeated data as a data pointer field. Then a stream
 * of pointers and actual data bytes. The pointer values include
 * an offset to previous data in the stream and the length of the
 *  matching data.
 *
 * The data pattern matching is done on the octects.
 *
 * The data is encoded as 8 field blocks with a compression flag
 * byte at the beginning.  If the bit is set in the compression
 * flag, then that field has a compression field. If it isn't set
 * then the byte is raw data.
 *
 * The compression field is either 2 or 3 bytes long. The length
 * is determined by the length of the matching data, for short
 * matches the match length is encoded in the high nibble of the
 * first byte. Otherwise the third byte of the field contains
 * the match length.
 *
 * First byte -
 * lower 4 bits:
 *		High order nibble of the offset
 *
 * upper 4 bits:
 *		1   = length is in 3rd byte
 *		2-F = length of matching data - 1
 *
 * Second byte -
 *  Lower byte of the source offset.
 *
 * Third byte -
 *  Length of match - 1 if First byte upper nibble = 1, otherwise
 *  this byte isn't added to data stream.
 *
 * Example:
 * 	Uncompressed data (hex):  11 22 22 22 22 33 44 55 66 77
 *
 *
 *	Compression data :
 *			Flag bits:	0x20 (third field is compressed)
 *			Data:	11 22 20 00 33 44 55
 *				/  /  /  /
 *		raw data ------+--+  /	/
 *              (Comp length - 1)<<4+  /
 *		Data offset ----------+
 *
 *	Output data (hex):  20 11 22 20 00 33 44 55 66 77
 *
 * In this example the copy src is one byte behind the copy destination
 * so if appears as if output is being loaded with the source byte.
 *
 */



#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdio.h>
#include <glib.h>
#include <string.h>
#include <epan/packet.h>
#include "packet-frame.h"
#include <epan/circuit.h>
#include "etypes.h"
#include "nlpid.h"

#define MAX_WIN_BUF_LEN 0x7fff		/* storage size for decompressed data */
#define MAX_WCP_BUF_LEN 2048		/* storage size for decompressed data */
#define FROM_DCE	0x80		/* for direction setting */

typedef struct {

	guint8  *buf_cur;
 	guint8  buffer[MAX_WIN_BUF_LEN];

}wcp_window_t;

typedef struct {
	wcp_window_t recv;
	wcp_window_t send;
} wcp_circuit_data_t;

/*XXX do I really want the length in here  */
typedef struct {

 	guint16  len;
 	guint8  buffer[MAX_WCP_BUF_LEN];

}wcp_pdata_t;


#define wcp_win_init_count 4
#define wcp_packet_init_count 10

#define wcp_circuit_length (sizeof(wcp_circuit_data_t))
#define wcp_packet_length (sizeof(wcp_pdata_t))

static GMemChunk *wcp_circuit = NULL;
static GMemChunk *wcp_pdata = NULL;

static int proto_wcp = -1;
static int hf_wcp_cmd = -1;
static int hf_wcp_ext_cmd = -1;
static int hf_wcp_seq = -1;
static int hf_wcp_chksum = -1;
static int hf_wcp_tid = -1;
static int hf_wcp_rev = -1;
static int hf_wcp_init = -1;
static int hf_wcp_seq_size = -1;
static int hf_wcp_alg = -1;
static int hf_wcp_alg_cnt = -1;
static int hf_wcp_alg_a = -1;
static int hf_wcp_alg_b = -1;
static int hf_wcp_alg_c = -1;
static int hf_wcp_alg_d = -1;
static int hf_wcp_rexmit = -1;

static int hf_wcp_hist_size = -1;
static int hf_wcp_ppc = -1;
static int hf_wcp_pib = -1;

static int hf_wcp_comp_bits = -1;
static int hf_wcp_comp_marker = -1;
static int hf_wcp_short_len = -1;
static int hf_wcp_long_len = -1;
static int hf_wcp_short_run = -1;
static int hf_wcp_long_run = -1;
static int hf_wcp_offset = -1;

static gint ett_wcp = -1;
static gint ett_wcp_field = -1;

static dissector_handle_t fr_uncompressed_handle;

/*
 * Bits in the address field.
 */
#define	WCP_CMD			0xf0	/* WCP Command */
#define	WCP_EXT_CMD		0x0f	/* WCP Extended Command */
#define	WCP_SEQ			0x0fff	/* WCP Sequence number */
#define	WCP_OFFSET_MASK		0x0fff	/* WCP Pattern source offset */

#define PPC_COMPRESSED_IND       	0x0
#define	PPC_UNCOMPRESSED_IND       	0x1
#define	PPC_TPPC_COMPRESSED_IND		0x2
#define	PPC_TPPC_UNCOMPRESSED_IND  	0x3
#define CONNECT_REQ                	0x4
#define	CONNECT_ACK                	0x5
#define	CONNECT_NAK                	0x6
#define	DISCONNECT_REQ           	0x7
#define	DISCONNECT_ACK             	0x8
#define	INIT_REQ                   	0x9
#define	INIT_ACK                   	0xa
#define	RESET_REQ                  	0xb
#define	RESET_ACK                  	0xc
#define	REXMIT_NAK                 	0xd


static const value_string cmd_string[] = {
	{0, "Compressed Data"},
        {1, "Uncompressed Data"},
        {15, "Extended"},
	{ 0,       NULL }
        };

static const value_string ext_cmd_string[] = {
	{0, "Per Packet Compression"},
	{4, "Connect Req"},
	{5, "Connect Ack"},
	{9, "Init Req"},
	{0x0a, "Init Ack"},

	{ 0,       NULL }
        };



static tvbuff_t *wcp_uncompress( tvbuff_t *src_tvb, int offset, packet_info *pinfo, proto_tree *tree);
static wcp_window_t *get_wcp_window_ptr( packet_info *pinfo);

static void
dissect_wcp_con_req(tvbuff_t *tvb, int offset, proto_tree *tree) {

/* WCP connector request message */

	guint alg_cnt = tvb_get_guint8(tvb, 5);

	proto_tree_add_uint(tree, hf_wcp_tid, tvb, offset, 2, tvb_get_ntohs(tvb, offset));
	proto_tree_add_uint(tree, hf_wcp_rev, tvb, offset + 2, 1, tvb_get_guint8(tvb, offset + 2));
	proto_tree_add_uint(tree, hf_wcp_init, tvb, offset + 3, 1, tvb_get_guint8(tvb, offset + 3));
	proto_tree_add_uint(tree, hf_wcp_seq_size, tvb, offset + 4, 1, tvb_get_guint8(tvb, offset + 4));
	proto_tree_add_uint(tree, hf_wcp_alg_cnt, tvb, offset + 5, 1, alg_cnt);
	proto_tree_add_uint(tree, hf_wcp_alg_a, tvb, offset + 6, 1, tvb_get_guint8(tvb, offset + 6));
	if ( alg_cnt > 1)
		proto_tree_add_uint(tree, hf_wcp_alg_b, tvb, offset + 7, 1, tvb_get_guint8(tvb, offset + 7));
	if ( alg_cnt > 2)
		proto_tree_add_uint(tree, hf_wcp_alg_c, tvb, offset + 8, 1, tvb_get_guint8(tvb, offset + 8));
	if ( alg_cnt > 3)
		proto_tree_add_uint(tree, hf_wcp_alg_d, tvb, offset + 9, 1, tvb_get_guint8(tvb, offset + 9));
}

static void
dissect_wcp_con_ack( tvbuff_t *tvb, int offset, proto_tree *tree){

/* WCP connector ack message */

	proto_tree_add_uint(tree, hf_wcp_tid, tvb, offset, 2, tvb_get_ntohs(tvb, offset));
	proto_tree_add_uint(tree, hf_wcp_rev, tvb, offset + 2, 1, tvb_get_guint8(tvb, offset + 2));
	proto_tree_add_uint(tree, hf_wcp_seq_size, tvb, offset + 3, 1, tvb_get_guint8(tvb, offset + 3));
	proto_tree_add_uint(tree, hf_wcp_alg, tvb, offset + 4, 1, tvb_get_guint8(tvb, offset + 4));
}

static void
dissect_wcp_init( tvbuff_t *tvb, int offset, proto_tree *tree){

/* WCP Initiate Request/Ack message */

	proto_tree_add_uint(tree, hf_wcp_tid, tvb, offset, 2, tvb_get_ntohs(tvb, offset));
	proto_tree_add_uint(tree, hf_wcp_rev, tvb, offset + 2, 1, tvb_get_guint8(tvb, offset + 2));
	proto_tree_add_uint(tree, hf_wcp_hist_size, tvb, offset + 3, 1, tvb_get_guint8(tvb, offset + 3));
	proto_tree_add_uint(tree, hf_wcp_ppc, tvb, offset + 4, 1, tvb_get_guint8(tvb, offset + 4));
	proto_tree_add_uint(tree, hf_wcp_pib, tvb, offset + 5, 1, tvb_get_guint8(tvb, offset + 5));
}


static void
dissect_wcp_reset( tvbuff_t *tvb, int offset, proto_tree *tree){

/* Process WCP Reset Request/Ack message */

	proto_tree_add_uint(tree, hf_wcp_tid, tvb, offset, 2, tvb_get_ntohs(tvb, offset));
}


static void wcp_save_data( tvbuff_t *tvb, packet_info *pinfo){

	wcp_window_t *buf_ptr = 0;
	int len;

	/* discard first 2 bytes, header and last byte (check byte) */
	len = tvb_reported_length( tvb)-3;
	buf_ptr = get_wcp_window_ptr( pinfo);

	if (( buf_ptr->buf_cur + len) <= (buf_ptr->buffer + MAX_WIN_BUF_LEN)){
		tvb_memcpy( tvb, buf_ptr->buf_cur, 2, len);
		buf_ptr->buf_cur = buf_ptr->buf_cur + len;

	} else {
		guint8 *buf_end = buf_ptr->buffer + MAX_WIN_BUF_LEN;
		tvb_memcpy( tvb, buf_ptr->buf_cur, 2, buf_end - buf_ptr->buf_cur);
		tvb_memcpy( tvb, buf_ptr->buffer, buf_end - buf_ptr->buf_cur-2,
			len - (int)(buf_end - buf_ptr->buf_cur));
		buf_ptr->buf_cur = buf_ptr->buf_cur + len - MAX_WIN_BUF_LEN;
	}

}


static void dissect_wcp( tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {

	proto_tree	*wcp_tree;
	proto_item	*ti;
	int		wcp_header_len;
	guint16		temp, cmd, ext_cmd, seq;
	tvbuff_t	*next_tvb;

	if (check_col(pinfo->cinfo, COL_PROTOCOL))
		col_set_str(pinfo->cinfo, COL_PROTOCOL, "WCP");
	if (check_col(pinfo->cinfo, COL_INFO))
		col_clear(pinfo->cinfo, COL_INFO);

	temp =tvb_get_ntohs(tvb, 0);

	cmd = (temp & 0xf000) >> 12;
	ext_cmd = (temp & 0x0f00) >> 8;

	if ( cmd == 0xf)
		wcp_header_len= 1;
	else
		wcp_header_len= 2;

	seq = temp & 0x0fff;

/*XXX should test seq to be sure it the last + 1 !! */

	if (check_col(pinfo->cinfo, COL_INFO)){
		col_add_str(pinfo->cinfo, COL_INFO, val_to_str(cmd, cmd_string, "Unknown"));
		if ( cmd == 0xf)
			col_append_fstr(pinfo->cinfo, COL_INFO, ", %s",
				val_to_str(ext_cmd, ext_cmd_string, "Unknown"));
	}

	if (tree) {
		ti = proto_tree_add_item(tree, proto_wcp, tvb, 0, wcp_header_len, FALSE);

		wcp_tree = proto_item_add_subtree(ti, ett_wcp);

		proto_tree_add_uint(wcp_tree, hf_wcp_cmd, tvb, 0, 1, tvb_get_guint8( tvb, 0));
		if ( cmd == 0xf){
			proto_tree_add_uint(wcp_tree, hf_wcp_ext_cmd, tvb, 1, 1,
					tvb_get_guint8( tvb, 0));
			switch (ext_cmd){
			case CONNECT_REQ:
				dissect_wcp_con_req( tvb, 1, wcp_tree);
				break;

			case CONNECT_ACK:
				dissect_wcp_con_ack( tvb, 1, wcp_tree);
				break;
			case INIT_REQ:
			case INIT_ACK:
				dissect_wcp_init( tvb, 1, wcp_tree);
				break;
			case RESET_REQ:
			case RESET_ACK:
				dissect_wcp_reset( tvb, 1, wcp_tree);
				break;
			default:
				break;
			}
		}else {
			proto_tree_add_uint(wcp_tree, hf_wcp_seq,  tvb, 0, 2, seq);
		}
	}
	else {
		wcp_tree = NULL;
	}


					/* exit if done */
	if ( cmd != 1 && cmd != 0 && !(cmd == 0xf && ext_cmd == 0))
		return;

	if ( cmd == 1) {		/* uncompressed data */
		if ( !pinfo->fd->flags.visited){	/* if first pass */
			wcp_save_data( tvb, pinfo);
		}
		next_tvb = tvb_new_subset(tvb, wcp_header_len, -1, -1);
	}
	else { 		/* cmd == 0 || (cmd == 0xf && ext_cmd == 0) */

		next_tvb = wcp_uncompress( tvb, wcp_header_len, pinfo, wcp_tree);

		if ( !next_tvb){
			proto_tree_add_protocol_format(tree, proto_malformed, tvb, 0, 0,
                             		"[Malformed Frame: Bad WCP compressed data]" );
			return;
		}
	}

	if ( tree)	 		/* add the check byte */
		proto_tree_add_uint(wcp_tree, hf_wcp_chksum, tvb,
			tvb_reported_length( tvb)-1, 1,
		 	tvb_get_guint8( tvb, tvb_reported_length(tvb)-1));

	call_dissector(fr_uncompressed_handle, next_tvb, pinfo, tree);

	return;
}


static guint8 *decompressed_entry( guint8 *src, guint8 *dst, int *len, guint8 * buf_start, guint8 *buf_end){

/* do the decompression for one field */

	guint16 data_offset, data_cnt;
	guint8 tmp = *src;

	data_offset = (*(src++) & 0xf) << 8;	/* get high byte */
	data_offset += *(src++);		/* add next byte */

	if (( tmp & 0xf0) == 0x10){		/* 2 byte count */
		data_cnt = *src;
		data_cnt++;

	}else {					/* one byte count */
		data_cnt = tmp >> 4;
		data_cnt++;
	}


	src = (dst - 1 - data_offset);
	if ( src < buf_start)
		src += MAX_WIN_BUF_LEN;


/*XXX could do some fancy memory moves, later if speed is problem */

	while( data_cnt--){
		*dst = *src;
		if ( ++(*len) >MAX_WCP_BUF_LEN){
			printf("decomp failed, len = %d\n",  *len);

			return NULL;	/* end of buffer error */
		}
		if ( dst++ == buf_end)
			dst = buf_start;
		if ( src++ == buf_end)
			src = buf_start;

	}
	return dst;
}


static
wcp_window_t *get_wcp_window_ptr( packet_info *pinfo){

/* find the circuit for this DLCI, create one if needed */
/* and return the wcp_window data structure pointer */
/* for the direction of this packet */

	circuit_t *circuit;
	wcp_circuit_data_t *wcp_circuit_data;

	circuit = find_circuit( pinfo->ctype, pinfo->circuit_id,
	    pinfo->fd->num);
	if ( !circuit){
		circuit = circuit_new( pinfo->ctype, pinfo->circuit_id,
		    pinfo->fd->num);
	}
	wcp_circuit_data = circuit_get_proto_data(circuit, proto_wcp);
	if ( !wcp_circuit_data){
		wcp_circuit_data = g_mem_chunk_alloc( wcp_circuit);
		wcp_circuit_data->recv.buf_cur = wcp_circuit_data->recv.buffer;
		wcp_circuit_data->send.buf_cur = wcp_circuit_data->send.buffer;
		circuit_add_proto_data(circuit, proto_wcp, wcp_circuit_data);
	}
	if (pinfo->pseudo_header->x25.flags & FROM_DCE)
		return &wcp_circuit_data->recv;
	else
		return &wcp_circuit_data->send;
}


static tvbuff_t *wcp_uncompress( tvbuff_t *src_tvb, int offset, packet_info *pinfo, proto_tree *tree) {

/* do the packet data uncompression and load it into the dst buffer */

	proto_tree	*sub_tree;
	proto_item	*ti;

	int len=0, i = -1;
	int cnt = tvb_reported_length( src_tvb)-1;	/* don't include check byte */

	guint8 *buf = 0, *dst, *src, *buf_start, *buf_end,  *tmp, comp_flag_bits = 0;
	guint8 src_buf[ MAX_WCP_BUF_LEN];
	tvbuff_t *volatile tvb = 0;
	wcp_window_t *buf_ptr = 0;
	wcp_pdata_t *volatile pdata_ptr;

	buf_ptr = get_wcp_window_ptr( pinfo);

	buf_start = buf_ptr->buffer;
	buf_end = buf_start + MAX_WIN_BUF_LEN;
	tmp = buf_ptr->buf_cur;

	if (cnt - offset > MAX_WCP_BUF_LEN) {
		if (tree)
			proto_tree_add_text( tree, src_tvb, offset, -1,
				"Compressed data exceeds maximum buffer length (%d > %d)",
				cnt - offset, MAX_WCP_BUF_LEN);
		return NULL;
	}

	src = tvb_memcpy(src_tvb, src_buf, offset, cnt - offset);
	dst = buf_ptr->buf_cur;

	while( offset++ < cnt){

		if ( --i >= 0){
			if ( comp_flag_bits & 0x80){	/* if this is a compressed entry */

				if ( !pinfo->fd->flags.visited){	/* if first pass */
					dst = decompressed_entry( src, dst, &len, buf_start, buf_end);
				}
				if ((*src & 0xf0) == 0x10){
					if ( tree) {
						ti = proto_tree_add_item( tree, hf_wcp_long_run, src_tvb,
							 offset-1, 3, 0);
						sub_tree = proto_item_add_subtree(ti, ett_wcp_field);
						proto_tree_add_uint(sub_tree, hf_wcp_offset, src_tvb,
							 offset-1, 2, pntohs(src));

						proto_tree_add_item( sub_tree, hf_wcp_long_len, src_tvb,
							 offset+1, 1, pntohs(src));
					}
					src += 3;
					offset += 2;
				}else{
					if ( tree) {
						ti = proto_tree_add_item( tree, hf_wcp_short_run, src_tvb,
							 offset - 1, 2, *src);
						sub_tree = proto_item_add_subtree(ti, ett_wcp_field);
						proto_tree_add_item( sub_tree, hf_wcp_short_len, src_tvb,
							 offset-1, 1, *src);
						proto_tree_add_uint(sub_tree, hf_wcp_offset, src_tvb,
							 offset-1, 2, pntohs(src));
					}
					src += 2;
					offset += 1;
				}
			}else {
				if ( !pinfo->fd->flags.visited){	/* if first pass */
					*dst = *src;
					if ( dst++ == buf_end)
						dst = buf_start;
				}
				++src;
				++len;

			}

			if ( len >MAX_WCP_BUF_LEN){
				return NULL;
			}

			comp_flag_bits <<= 1;

		}else {	/* compressed data flag */

			comp_flag_bits = *src++;
			if (tree)
				proto_tree_add_uint( tree, hf_wcp_comp_bits,  src_tvb, offset-1, 1,
					comp_flag_bits);

			i = 8;
		}
	}

	if ( pinfo->fd->flags.visited){	/* if not first pass */
					/* get uncompressed data */
		pdata_ptr = p_get_proto_data( pinfo->fd, proto_wcp);

		if ( !pdata_ptr)	/* exit if no data */
			return NULL;
		len = pdata_ptr->len;
	} else {

	/* save the new data as per packet data */
		pdata_ptr = g_mem_chunk_alloc( wcp_pdata);
		memcpy( &pdata_ptr->buffer, buf_ptr->buf_cur,  len);
		pdata_ptr->len = len;

		p_add_proto_data( pinfo->fd, proto_wcp, (void*)pdata_ptr);

		buf_ptr->buf_cur = dst;
	}


        TRY {
                tvb = tvb_new_real_data( pdata_ptr->buffer, pdata_ptr->len, pdata_ptr->len);

        }
        CATCH(BoundsError) {
		g_assert_not_reached();
		g_free(buf);
		return NULL;
        }
        CATCH(ReportedBoundsError) {
		g_free(buf);
		return NULL;
        }
        ENDTRY;

	/* link new tvbuff into tvbuff chain so cleanup is done later */
        tvb_set_child_real_data_tvbuff( src_tvb, tvb);

	/* Add new data to the data source list */
	add_new_data_source( pinfo, tvb, "Uncompressed WCP");
	return tvb;

}


static void wcp_reinit( void){

/* Do the cleanup work when a new pass through the packet list is       */
/* performed. re-initialize the  memory chunks.                         */

        if (wcp_circuit)
                g_mem_chunk_destroy(wcp_circuit);

        wcp_circuit = g_mem_chunk_new("wcp_circuit", wcp_circuit_length,
                wcp_win_init_count * wcp_circuit_length,
                G_ALLOC_AND_FREE);

        if (wcp_pdata)
                g_mem_chunk_destroy(wcp_pdata);

        wcp_pdata = g_mem_chunk_new("wcp_pdata", wcp_packet_length,
                wcp_packet_init_count * wcp_packet_length,
                G_ALLOC_AND_FREE);

}


void
proto_register_wcp(void)
{
    static hf_register_info hf[] = {
	{ &hf_wcp_cmd,
	  { "Command", "wcp.cmd", FT_UINT8, BASE_HEX, VALS(cmd_string), WCP_CMD,
	  	"Compression Command", HFILL }},
	{ &hf_wcp_ext_cmd,
	  { "Extended Command", "wcp.ext_cmd", FT_UINT8, BASE_HEX, VALS(ext_cmd_string), WCP_EXT_CMD,
	  	"Extended Compression Command", HFILL }},
	{ &hf_wcp_seq,
	  { "SEQ", "wcp.seq", FT_UINT16, BASE_HEX, NULL, WCP_SEQ,
	  	"Sequence Number", HFILL }},
	{ &hf_wcp_chksum,
	  { "Checksum", "wcp.checksum", FT_UINT8, BASE_DEC, NULL, 0,
	  	"Packet Checksum", HFILL }},
	{ &hf_wcp_tid,
	  { "TID", "wcp.tid", FT_UINT16, BASE_DEC, NULL, 0,
	  	"TID", HFILL }},
	{ &hf_wcp_rev,
	  { "Revision", "wcp.rev", FT_UINT8, BASE_DEC, NULL, 0,
	  	"Revision", HFILL }},
	{ &hf_wcp_init,
	  { "Initiator", "wcp.init", FT_UINT8, BASE_DEC, NULL, 0,
	  	"Initiator", HFILL }},
	{ &hf_wcp_seq_size,
	  { "Seq Size", "wcp.seq_size", FT_UINT8, BASE_DEC, NULL, 0,
	  	"Sequence Size", HFILL }},
	{ &hf_wcp_alg_cnt,
	  { "Alg Count", "wcp.alg_cnt", FT_UINT8, BASE_DEC, NULL, 0,
	  	"Algorithm Count", HFILL }},
	{ &hf_wcp_alg_a,
	  { "Alg 1", "wcp.alg1", FT_UINT8, BASE_DEC, NULL, 0,
	  	"Algorithm #1", HFILL }},
	{ &hf_wcp_alg_b,
	  { "Alg 2", "wcp.alg2", FT_UINT8, BASE_DEC, NULL, 0,
	  	"Algorithm #2", HFILL }},
	{ &hf_wcp_alg_c,
	  { "Alg 3", "wcp.alg3", FT_UINT8, BASE_DEC, NULL, 0,
	  	"Algorithm #3", HFILL }},
	{ &hf_wcp_alg_d,
	  { "Alg 4", "wcp.alg4", FT_UINT8, BASE_DEC, NULL, 0,
	  	"Algorithm #4", HFILL }},
	{ &hf_wcp_alg,
	  { "Alg", "wcp.alg", FT_UINT8, BASE_DEC, NULL, 0,
	  	"Algorithm", HFILL }},
	{ &hf_wcp_rexmit,
	  { "Rexmit", "wcp.rexmit", FT_UINT8, BASE_DEC, NULL, 0,
	  	"Retransmit", HFILL }},
	{ &hf_wcp_hist_size,
	  { "History", "wcp.hist", FT_UINT8, BASE_DEC, NULL, 0,
	  	"History Size", HFILL }},
	{ &hf_wcp_ppc,
	  { "PerPackComp", "wcp.ppc", FT_UINT8, BASE_DEC, NULL, 0,
	  	"Per Packet Compression", HFILL }},
	{ &hf_wcp_pib,
	  { "PIB", "wcp.pib", FT_UINT8, BASE_DEC, NULL, 0,
	  	"PIB", HFILL }},
	{ &hf_wcp_comp_bits,
	  { "Compress Flag", "wcp.flag", FT_UINT8, BASE_HEX, NULL, 0,
	  	"Compressed byte flag", HFILL }},
	{ &hf_wcp_comp_marker,
	  { "Compress Marker", "wcp.mark", FT_UINT8, BASE_DEC, NULL, 0,
	  	"Compressed marker", HFILL }},
	{ &hf_wcp_offset,
	  { "Source offset", "wcp.off", FT_UINT16, BASE_HEX, NULL, WCP_OFFSET_MASK,
	  	"Data source offset", HFILL }},
	{ &hf_wcp_short_len,
	  { "Compress Length", "wcp.short_len", FT_UINT8, BASE_HEX, NULL, 0xf0,
	  	"Compressed length", HFILL }},
	{ &hf_wcp_long_len,
	  { "Compress Length", "wcp.long_len", FT_UINT8, BASE_HEX, NULL, 0,
	  	"Compressed length", HFILL }},
	{ &hf_wcp_long_run,
	  { "Long Compression", "wcp.long_comp", FT_UINT16, BASE_HEX, NULL, 0,
	  	"Long Compression type", HFILL }},
	{ &hf_wcp_short_run,
	  { "Short Compression", "wcp.short_comp", FT_UINT8, BASE_HEX, NULL, 0,
	  	"Short Compression type", HFILL }},

   };


    static gint *ett[] = {
        &ett_wcp,
	&ett_wcp_field,
    };

    proto_wcp = proto_register_protocol ("Wellfleet Compression", "WCP", "wcp");
    proto_register_field_array (proto_wcp, hf, array_length(hf));
    proto_register_subtree_array(ett, array_length(ett));
    register_init_routine(&wcp_reinit);
}


void
proto_reg_handoff_wcp(void) {
    dissector_handle_t wcp_handle;

    /*
     * Get handle for the Frame Relay (uncompressed) dissector.
     */
    fr_uncompressed_handle = find_dissector("fr_uncompressed");

    wcp_handle = create_dissector_handle(dissect_wcp, proto_wcp);
    dissector_add("fr.ietf", NLPID_COMPRESSED, wcp_handle);
    dissector_add("ethertype",  ETHERTYPE_WCP, wcp_handle);
}