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: Guy Harris <gharris@xxxxxxxxx>
Date: Tue, 10 May 2005 22:51:12 -0700
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.