X-Git-Url: http://git.silcnet.org/gitweb/?p=silc.git;a=blobdiff_plain;f=lib%2Fsilcutil%2Fsilcbuffmt.h;h=e69e90bb6c6ce31cf11b0fe213cf5c30a807b8b8;hp=6d9acaeb736327deab7ee84a069cafd441006661;hb=a818c5b5411bbc4436d1c5f011236985c96bb787;hpb=89b07a460a554eb5173fe07f09b8c51c31d53e29 diff --git a/lib/silcutil/silcbuffmt.h b/lib/silcutil/silcbuffmt.h index 6d9acaeb..e69e90bb 100644 --- a/lib/silcutil/silcbuffmt.h +++ b/lib/silcutil/silcbuffmt.h @@ -18,6 +18,17 @@ */ +/****h* silcutil/SilcBufferFormatAPI + * + * DESCRIPTION + * + * SILC Buffer Format API provides a few functions for formatting + * various different data types into a buffer, and retrieving + * various data from buffer into specific data types. It is usefull + * to format for example packets and later unformat them. + * + ***/ + #ifndef SILCBUFFMT_H #define SILCBUFFMT_H @@ -26,6 +37,13 @@ _SI_ = signed _UI_ = unsigned + Any XXX_STRING_ALLOC types will allocate space for the data and + memcpy the data to the pointer sent as argument (in unformatting). + + Any XXX_STRING will not allocate or copy any data. Instead it + will set the pointer to the data. Note that the data pointer + returned (in unformatting) must not be freed. + */ typedef enum { SILC_BUFFER_PARAM_SI8_CHAR, @@ -37,16 +55,23 @@ typedef enum { SILC_BUFFER_PARAM_SI32_INT, SILC_BUFFER_PARAM_UI32_INT, - SILC_BUFFER_PARAM_UI16_STRING, - SILC_BUFFER_PARAM_UI16_STRING_ALLOC, - SILC_BUFFER_PARAM_UI32_STRING, - SILC_BUFFER_PARAM_UI32_STRING_ALLOC, - SILC_BUFFER_PARAM_UI16_NSTRING, - SILC_BUFFER_PARAM_UI16_NSTRING_ALLOC, - SILC_BUFFER_PARAM_UI32_NSTRING, - SILC_BUFFER_PARAM_UI32_NSTRING_ALLOC, - SILC_BUFFER_PARAM_UI_XNSTRING, - SILC_BUFFER_PARAM_UI_XNSTRING_ALLOC, + SILC_BUFFER_PARAM_SI64_INT, + SILC_BUFFER_PARAM_UI64_INT, + + SILC_BUFFER_PARAM_UI8_STRING, /* No copy */ + SILC_BUFFER_PARAM_UI8_STRING_ALLOC, /* Alloc + memcpy */ + SILC_BUFFER_PARAM_UI16_STRING, /* No copy */ + SILC_BUFFER_PARAM_UI16_STRING_ALLOC, /* Alloc + memcpy */ + SILC_BUFFER_PARAM_UI32_STRING, /* No copy */ + SILC_BUFFER_PARAM_UI32_STRING_ALLOC, /* Alloc + memcpy */ + SILC_BUFFER_PARAM_UI8_NSTRING, /* No copy */ + SILC_BUFFER_PARAM_UI8_NSTRING_ALLOC, /* Alloc + memcpy */ + SILC_BUFFER_PARAM_UI16_NSTRING, /* No copy */ + SILC_BUFFER_PARAM_UI16_NSTRING_ALLOC, /* Alloc + memcpy */ + SILC_BUFFER_PARAM_UI32_NSTRING, /* No copy */ + SILC_BUFFER_PARAM_UI32_NSTRING_ALLOC, /* Alloc + memcpy */ + SILC_BUFFER_PARAM_UI_XNSTRING, /* No copy */ + SILC_BUFFER_PARAM_UI_XNSTRING_ALLOC, /* Alloc + memcpy */ SILC_BUFFER_PARAM_END } SilcBufferParamType; @@ -66,28 +91,39 @@ typedef enum { #define SILC_STR_SI_CHAR(x) SILC_BUFFER_PARAM_SI8_CHAR, (x) #define SILC_STR_UI_CHAR(x) SILC_BUFFER_PARAM_UI8_CHAR, (x) -/* Signed/unsigned short. +/* Signed/SilcUInt16. Formatting: SILC_STR_SI_SHORT(short) - SILC_STR_UI_SHORT(unsigned short) + SILC_STR_UI_SHORT(SilcUInt16) Unformatting: SILC_STR_SI_SHORT(short *) - SILC_STR_UI_SHORT(unsigned short *) + SILC_STR_UI_SHORT(SilcUInt16 *) */ #define SILC_STR_SI_SHORT(x) SILC_BUFFER_PARAM_SI16_SHORT, (x) #define SILC_STR_UI_SHORT(x) SILC_BUFFER_PARAM_UI16_SHORT, (x) -/* Signed/unsigned int. +/* Signed/SilcUInt32. Formatting: SILC_STR_SI_INT(int) - SILC_STR_UI_INT(unsigned int) + SILC_STR_UI_INT(SilcUInt32) Unformatting: SILC_STR_SI_INT(int *) - SILC_STR_UI_INT(unsigned int *) + SILC_STR_UI_INT(SilcUInt32 *) */ #define SILC_STR_SI_INT(x) SILC_BUFFER_PARAM_SI32_INT, (x) #define SILC_STR_UI_INT(x) SILC_BUFFER_PARAM_UI32_INT, (x) +/* Signed/SilcUInt64. + + Formatting: SILC_STR_SI_INT64(int) + SILC_STR_UI_INT64(SilcUInt32) + Unformatting: SILC_STR_SI_INT64(int *) + SILC_STR_UI_INT64(SilcUInt32 *) + +*/ +#define SILC_STR_SI_INT64(x) SILC_BUFFER_PARAM_SI64_INT, (x) +#define SILC_STR_UI_INT64(x) SILC_BUFFER_PARAM_UI64_INT, (x) + /* Unsigned NULL terminated string. Note that the string must be NULL terminated because strlen() will be used to get the length of the string. @@ -116,6 +152,8 @@ typedef enum { as argument in unformatting. */ +#define SILC_STR_UI8_STRING(x) SILC_BUFFER_PARAM_UI8_STRING, (x) +#define SILC_STR_UI8_STRING_ALLOC(x) SILC_BUFFER_PARAM_UI8_STRING_ALLOC, (x) #define SILC_STR_UI16_STRING(x) SILC_BUFFER_PARAM_UI16_STRING, (x) #define SILC_STR_UI16_STRING_ALLOC(x) SILC_BUFFER_PARAM_UI16_STRING_ALLOC, (x) #define SILC_STR_UI32_STRING(x) SILC_BUFFER_PARAM_UI32_STRING, (x) @@ -123,8 +161,8 @@ typedef enum { /* Unsigned string. Second argument is the length of the string. - Formatting: SILC_STR_UI32_NSTRING(unsigned char *, unsigned int) - Unformatting: SILC_STR_UI32_NSTRING(unsigned char **, unsigned int *) + Formatting: SILC_STR_UI32_NSTRING(unsigned char *, SilcUInt32) + Unformatting: SILC_STR_UI32_NSTRING(unsigned char **, SilcUInt32 *) Unformatting procedure will check for length of the string from the buffer before trying to get the string out. Thus, one *must* format the @@ -151,6 +189,9 @@ typedef enum { as argument in unformatting. */ +#define SILC_STR_UI8_NSTRING(x, l) SILC_BUFFER_PARAM_UI8_NSTRING, (x), (l) +#define SILC_STR_UI8_NSTRING_ALLOC(x, l) \ + SILC_BUFFER_PARAM_UI8_NSTRING_ALLOC, (x), (l) #define SILC_STR_UI16_NSTRING(x, l) SILC_BUFFER_PARAM_UI16_NSTRING, (x), (l) #define SILC_STR_UI16_NSTRING_ALLOC(x, l) \ SILC_BUFFER_PARAM_UI16_NSTRING_ALLOC, (x), (l) @@ -162,7 +203,7 @@ typedef enum { the string. Formatting: This is equal to using *_NSTRING - Unformatting: SILC_STR_UI_XNSTRING(unsigned char **, unsigned int) + Unformatting: SILC_STR_UI_XNSTRING(unsigned char **, SilcUInt32) This type can be used to take arbitrary length string from the buffer by sending the requested amount of bytes as argument. This differs @@ -180,12 +221,66 @@ typedef enum { #define SILC_STR_UI_XNSTRING_ALLOC(x, l) \ SILC_BUFFER_PARAM_UI_XNSTRING_ALLOC, (x), (l) -/* Marks end of the argument list. This must the at the end of the +/* Marks end of the argument list. This must be at the end of the argument list or error will occur. */ #define SILC_STR_END SILC_BUFFER_PARAM_END /* Prototypes */ + +/****f* silcutil/SilcBufferFormatAPI/silc_buffer_format + * + * SYNOPSIS + * + * int silc_buffer_format(SilcBuffer dst, ...); + * + * DESCRIPTION + * + * Formats a buffer from a variable argument list. Returns -1 on error + * and the length of the formatted buffer otherwise. + * + ***/ int silc_buffer_format(SilcBuffer dst, ...); + +/****f* silcutil/SilcBufferFormatAPI/silc_buffer_unformat + * + * SYNOPSIS + * + * int silc_buffer_unformat(SilcBuffer src, ...); + * + * DESCRIPTION + * + * Formats a buffer from a variable argument list. Returns -1 on error + * and the length of the formatted buffer otherwise. + * + ***/ int silc_buffer_unformat(SilcBuffer src, ...); +/****f* silcutil/SilcBufferFormatAPI/silc_buffer_format_vp + * + * SYNOPSIS + * + * int silc_buffer_format_vp(SilcBuffer dst, va_list vp); + * + * DESCRIPTION + * + * Formats a buffer from a variable argument list indicated by the `ap'. + * Returns -1 on error and the length of the formatted buffer otherwise. + * + ***/ +int silc_buffer_format_vp(SilcBuffer dst, va_list ap); + +/****f* silcutil/SilcBufferFormatAPI/silc_buffer_unformat_vp + * + * SYNOPSIS + * + * int silc_buffer_unformat_vp(SilcBuffer src, va_list vp); + * + * DESCRIPTION + * + * Formats a buffer from a variable argument list indicated by the `ap'. + * Returns -1 on error and the length of the formatted buffer otherwise. + * + ***/ +int silc_buffer_unformat_vp(SilcBuffer src, va_list ap); + #endif