Ethereal-dev: Re: [Ethereal-dev] escape sequences in tvbuff

Note: This archive is from the project's previous web site, ethereal.com. This list is no longer active.

From: "Oren Mazor" <oren.mazor@xxxxxxxxx>
Date: Fri, 13 May 2005 11:27:21 -0400
I eventually went back and fixed it up with your suggestions.

Thanks to all for the advice and help :)

Cheers,
Oren

static tvbuff_t *
filterPadding(tvbuff_t *tvb,packet_info *pinfo)
{
	const guint8 *orig_buffer = tvb_get_ptr(tvb, 0, -1);
	int orig_buffer_len = tvb_length(tvb);
	guint8 *new_buffer = g_malloc(orig_buffer_len);
	int new_buffer_len = orig_buffer_len;

	int i,j; //i is our READ pointer, and j is the WRITE pointer
	for(i=0,j=0;i<orig_buffer_len;i++,j++)
	{	
		new_buffer[j] = orig_buffer[i];
		if(orig_buffer[i] == 126)//0x7E = 126
		{
			if(orig_buffer[i+1] == 85)//0x85 = 55
			{
				new_buffer_len--;
				i++;
			}
		}
		
	}
	//create the new tvb
tvbuff_t *next_tvb = tvb_new_real_data(new_buffer,new_buffer_len,new_buffer_len);
    tvb_set_child_real_data_tvbuff(tvb, next_tvb);
    add_new_data_source(pinfo, next_tvb, "Check");
	tvb_set_free_cb(next_tvb, g_free);
	return next_tvb;
}


On Wed, 11 May 2005 01:51:12 -0400, Guy Harris <gharris@xxxxxxxxx> wrote:

Oren Mazor wrote:

static tvbuff_t *
cleanUpEscapeSequence(tvbuff_t *tvb, packet_info *pinfo)
{
    //the original bytes
    char* orig_buffer = tvb_memdup(tvb, 0, tvb_length(tvb));

There's no need to copy the original bytes;

	char *orig_buffer = tvb_get_ptr(tvb, 0, -1);

should suffice.

    //the size of above buffer
    int buffer_size = strlen(orig_buffer);

As noted, that's the wrong way to get the size of the buffer; you should use tvb_length(tvb) instead.

    //the size of our new buffer (initially the same as original buffer)
    int new_buffer_size = buffer_size;
    //buffer to hold the resulting bytes
    char* new_buffer[buffer_size];

That will be allocated on the stack, and will not be valid after "cleanUpEscapeSequence()" returns. Instead, do

	char *new_buffer = g_malloc(buffer_size);

("g_malloc()" aborts if the allocation fails; you could also use "malloc()", but your dissector will need to check for a null pointer being returned, and quit.)

    if(new_buffer_size != buffer_size)
    {//we've had a change, if the two buffer size dont match
        //create the new tvb
tvbuff_t *next_tvb = tvb_new_real_data(*new_buffer, new_buffer_size, new_buffer_size);
        tvb_set_child_real_data_tvbuff(tvb, next_tvb);
         add_new_data_source(pinfo, next_tvb, "Check");

In this case, there's one more thing to do:

		tvb_set_free_cb(next_tvb, g_free);

to arrange that when "next_tvb" is freed, the data for it is freed. (This allows different routines to be used to free the tvbuff.)

        return next_tvb;
    }
    else
        return tvb;

And here you'd free "new_buffer", as you aren't creating a new tvbuff.


_______________________________________________
Ethereal-dev mailing list
Ethereal-dev@xxxxxxxxxxxx
http://www.ethereal.com/mailman/listinfo/ethereal-dev



--
When I was younger, I could remember anything, whether it had happened
or not; but my faculties are decaying now and soon I shall be so I
cannot remember any but the things that never happened.  It is sad to
go to pieces like this but we all have to do it.
                -- Mark Twain