Ethereal-dev: Re: [Ethereal-dev] How to parse string that is not null terminated with tvb_get_

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: Thu, 27 Jun 2002 01:27:45 -0700
(Both my work and home addresses are on ethereal-dev@xxxxxxxxxxxx, so
just sending to ethereal-dev@xxxxxxxxxxxx is sufficient.)

On Wed, Jun 26, 2002 at 01:07:10PM -0700, Jaime Fournier wrote:
> I have a string in a packet that I have the size for,
> but because the string is not null terminated I get
> extra ASCII garbage at the end of my string.
> The size is right, but for some reason my read goes
> beyond it by 1.

If the string will be put into the protocol tree as a named field, and
you don't need to extract the string's value, you can make it an
FT_STRING field and add it with "proto_tree_add_item()" - that'll take
care of stopping at the end of the (counted) string, and
null-terminating the string value in the protocol tree.

If the string will be put into the protocol tree with
"proto_tree_add_text()", or you have some other reason why you need to
extract the string's value, you can, if you have a buffer large enough
to hold the string's value (i.e., it's the length of the string, plus 1
for the terminating null), you can do

	tvb_memcpy(tvb, buffer, offset, length);
	buffer[length] = '\0';

and "buffer" will be a null-terminated version of the string.