buffer\datum questions

Nikos Mavrogiannopoulos nmav at gnutls.org
Thu Aug 2 01:05:19 CEST 2012


On 07/26/2012 11:00 PM, Olya wrote:

> Hello.
> 
> I've been struggling with data buffering - seems like what I've guessed from source
> code is not entirely true: buffer content is not what I've put in there, segfaults etc.
> 
> Could you help me to figure out how to use gnutls_str properly?
> Code snippets would be most awesome. (Yepp, I'm aware that it's used throughout
> GnuTLS - but thousands of lines of undocumented code is hardly a good illustration :)
> I'll try to add some documentation to gnutls_str.c once I figure out how stuff in
> there actually works.
> 1) I've got gnutls_buffer_st with some content and void * data of size len - how do I
> replace the buffer content? Who\when does memory allocation\free?


Hello Olga,
 The idea of gnutls_buffer_st is that you have a variable length buffer
that resizes automatically once you append data to it.

It requires to initially use _gnutls_buffer_init() to set the structure
to some sensible defaults (set everything to zero values) and you
deallocate by using _gnutls_buffer_clear().

You append data by using _gnutls_buffer_append_data() - and you must
always check the error code since memory allocation may occur, and you
can access the data appended by using the structure's data and length
fields.

> 2) I'd like to get hex representation of buffer content to char * (for printing) -
> how do I do that?


There is _gnutls_bin2hex() that given your buffer's data and length
fields it would save the hex output in the array you specified.

> 3) What's the difference between datum and buffer - when I should use one?


The datum is a structure that provides two fields, data, and size. If
that's all you need then use a datum. The buffer allows to append to it
(possibly formatted for TLS) data, and to read data (pop) from it in an
efficient way.

> 4) What's with memory management? Is it completely automated? When and why do I need
> _gnutls_buffer_resize()?


If you use the gnutls_buffer*() functions it is automated. If however
you need to add some data in some other fashion that isn't covered by
the current functions the resize can be used. If the length of your
buffer is X and the you do a resize of X+N, then it makes sure that
there are N bytes available at the position
buffer->data[buffer->length]. If you use this option you should increase
buffer->length manually once you store the data there.

> 5) What does various _prefix() functions do?


They are used to store the TLS variable length fields. That is they
prepend the size of the buffer before the data. The prefix size is the
number of bits used for the length (8,16,24,32).

regards,
Nikos




More information about the Gnutls-devel mailing list