Moved SILC_PARAM_* types under generic SilcParam type into the
[silc.git] / lib / silcutil / silcbuffmt.h
index a3ff49dc18e1229be8d8a2a204dfbc28749906ec..6e530f542a3d76d42993684af913e9d6342bcfff 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 1997 - 2006 Pekka Riikonen
+  Copyright (C) 1997 - 2007 Pekka Riikonen
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
  *
  * 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.
+ * SILC Buffer Format API provides functions for formatting different data
+ * types into a buffer and retrieving different data types from a buffer
+ * into specified data types.  It is especially useful to encode packets,
+ * protocol payloads and such.
+ *
+ * As the SilcBuffer API is not thread-safe these routines may not be used
+ * in multithreaded environment with a same SilcBuffer context without
+ * concurrency control.
+ *
+ * EXAMPLE
+ *
+ * SilcBufferStruct buffer;
+ *
+ * memset(&buffer, 0, sizeof(buffer));
+ * ret = silc_buffer_format(&buffer,
+ *                          SILC_STR_UINT32(intval),
+ *                          SILC_STR_UINT8(charval),
+ *                          SILC_STR_UINT64(longintval),
+ *                          SILC_STR_UINT16(str_len),
+ *                          SILC_STR_DATA(str, str_len),
+ *                          SILC_STR_END);
+ * if (ret < 0)
+ *   error;
  *
  ***/
 
 #ifndef SILCBUFFMT_H
 #define SILCBUFFMT_H
 
+/****f* silcutil/SilcBufferFormatAPI/SilcBufferFormatFunc
+ *
+ * SYNOPSIS
+ *
+ *    typedef int (*SilcBuffeSFormatFunc)(SilcStack stack,
+ *                                        SilcBuffer buffer,
+ *                                        void *value,
+ *                                        void *context);
+ *
+ * DESCRIPTION
+ *
+ *    Formatting function callback given with SILC_STR_FUNC type.  The
+ *    `buffer' is the buffer being formatted at the location where the
+ *    SILC_STR_FUNC was placed in formatting.  The function should call
+ *    silc_buffer_senlarge before it adds the data to the buffer to make
+ *    sure that it has enough space.  The buffer->head points to the
+ *    start of the buffer and silc_buffer_headlen() gives the length
+ *    of the currently formatted data area.  It is also possible to use
+ *    silc_buffer_sformat with `buffer' which will enlarge the buffer if
+ *    needed.
+ *
+ *    The `value' is the value given to SILC_STR_FUNC that is to be formatted
+ *    into the buffer.  It may be NULL if the function is not formatting
+ *    new data into the buffer.  The `context' is caller specific context.
+ *    Returns -1 on error and length of the formatted value otherwise, and
+ *    0 if nothing was formatted.
+ *
+ ***/
+typedef int (*SilcBufferFormatFunc)(SilcStack stack, SilcBuffer buffer,
+                                   void *value, void *context);
+
+/****f* silcutil/SilcBufferFormatAPI/SilcBufferUnformatFunc
+ *
+ * SYNOPSIS
+ *
+ *    typedef int (*SilcBufferUnformatFunc)(SilcBuffer buffer,
+ *                                          void **value,
+ *                                          void *context);
+ *
+ * DESCRIPTION
+ *
+ *    Unformatting function callback given with SILC_STR_FUNC type.  The
+ *    `buffer' is the buffer being unformatted and is at the location where
+ *    the SILC_STR_FUNC was placed in unformatting.  The function should
+ *    check there is enough data in the `buffer' before trying to decode
+ *    from it.
+ *
+ *    If this function unformats anything from the buffer its value is to
+ *    be returned to the `value' pointer.  The implementation should itself
+ *    decide whether the unformatted value is allocated or not.  If this
+ *    function does not unformat anything, nothing is returned to `value'
+ *
+ *    The `context' is caller specific context.  Returns -1 on error, and
+ *    length of the unformatted value otherwise, and 0 if nothing was
+ *    unformatted.
+ *
+ ***/
+typedef int (*SilcBufferUnformatFunc)(SilcStack stack, SilcBuffer buffer,
+                                     void **value, void *context);
+
 /* Prototypes */
 
 /****f* silcutil/SilcBufferFormatAPI/silc_buffer_format
  *
  * DESCRIPTION
  *
- *    Formats a buffer from a variable argument list.  Returns -1 on error
- *    and the length of the formatted buffer otherwise.  The buffer is
- *    enlarged automatically during formatting, if it doesn't already have
- *    enough space.
+ *    Formats a buffer from a variable argument list.  Returns -1 if the
+ *    system is out of memory and the length of the formatted buffer otherwise.
+ *    The buffer is enlarged automatically during formatting, if it doesn't
+ *    already have enough space.
  *
  * EXAMPLE
  *
+ *    Three basic ways of using silc_buffer_format:
+ *
+ *    // Statically allocated zero size buffer
  *    SilcBufferStruct buffer;
- *    SilcBuffer buf;
  *
  *    memset(&buffer, 0, sizeof(buffer));
  *    ret = silc_buffer_format(&buffer,
- *                             SILC_STR_INT(intval),
- *                             SILC_STR_CHAR(charval),
- *                             SILC_STR_INT(intval),
- *                             SILC_STR_SHORT(str_len),
- *                             SILC_STR_UI_XNSTRING(str, str_len),
+ *                             SILC_STR_UINT32(intval),
+ *                             SILC_STR_UINT8(charval),
+ *                             SILC_STR_UINT32(intval),
+ *                             SILC_STR_UINT16(str_len),
+ *                             SILC_STR_DATA(str, str_len),
  *                             SILC_STR_END);
  *    if (ret < 0)
  *      error;
  *    // Free the allocated data
  *    silc_buffer_purge(&buffer);
  *
- *    // Allocate zero size buffer
+ *    // Dynamically allocated zero size buffer
+ *    SilcBuffer buf;
  *    buf = silc_buffer_alloc(0);
  *    ret = silc_buffer_format(buf,
- *                             SILC_STR_INT(intval),
- *                             SILC_STR_CHAR(charval),
+ *                             SILC_STR_UINT32(intval),
+ *                             SILC_STR_UINT8(charval),
  *                             SILC_STR_END);
+ *    if (ret < 0)
+ *      error;
  *
  *    // Free the allocated buffer
  *    silc_buffer_free(buf);
  *
+ *    // Dynamically allocated buffer with enough space
+ *    SilcBuffer buf;
+ *    buf = silc_buffer_alloc(2 + str_len);
+ *    ret = silc_buffer_format(buf,
+ *                             SILC_STR_UINT16(str_len),
+ *                             SILC_STR_DATA(str, str_len),
+ *                             SILC_STR_END);
+ *    if (ret < 0)
+ *      error;
+ *
  ***/
 int silc_buffer_format(SilcBuffer dst, ...);
 
@@ -87,7 +181,11 @@ int silc_buffer_format(SilcBuffer dst, ...);
  * DESCRIPTION
  *
  *    Same as silc_buffer_format but uses `stack' to allocate the memory.
- *    if `stack' is NULL reverts back to silc_buffer_format call.
+ *    if `stack' is NULL reverts back to silc_buffer_format call.  Returns
+ *    -1 if system is out of memory.
+ *
+ *    Note that this call consumes the `stack'.  The caller should push the
+ *    stack before calling the function and pop it later.
  *
  ***/
 int silc_buffer_sformat(SilcStack stack, SilcBuffer dst, ...);
@@ -101,21 +199,26 @@ int silc_buffer_sformat(SilcStack stack, SilcBuffer dst, ...);
  * 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.
+ *    Returns -1 if system is out of memory and the length of the formatted
+ *    buffer otherwise.
  *
  ***/
 int silc_buffer_format_vp(SilcBuffer dst, va_list ap);
 
-/****f* silcutil/SilcBufferFormatAPI/silc_buffer_format_vp
+/****f* silcutil/SilcBufferFormatAPI/silc_buffer_sformat_vp
  *
  * SYNOPSIS
  *
- *    int silc_buffer_format_vp(SilcBuffer dst, va_list vp);
+ *    int silc_buffer_sformat_vp(SilcStack stack, SilcBuffer dst, va_list vp);
  *
  * DESCRIPTION
  *
  *    Same as silc_buffer_format_vp but uses `stack' to allocate the memory.
- *    if `stack' is NULL reverts back to silc_buffer_format_vp call.
+ *    if `stack' is NULL reverts back to silc_buffer_format_vp call.  Returns
+ *    -1 if system is out of memory.
+ *
+ *    Note that this call consumes the `stack'.  The caller should push the
+ *    stack before calling the function and pop it later.
  *
  ***/
 int silc_buffer_sformat_vp(SilcStack stack, SilcBuffer dst, va_list ap);
@@ -134,8 +237,8 @@ int silc_buffer_sformat_vp(SilcStack stack, SilcBuffer dst, va_list ap);
  * EXAMPLE
  *
  *    ret = silc_buffer_unformat(buffer,
- *                               SILC_STR_INT(&intval),
- *                               SILC_STR_CHAR(&charval),
+ *                               SILC_STR_UINT32(&intval),
+ *                               SILC_STR_UINT8(&charval),
  *                               SILC_STR_OFFSET(4),
  *                               SILC_STR_UI16_NSTRING_ALLOC(&str, &str_len),
  *                               SILC_STR_END);
@@ -145,6 +248,23 @@ int silc_buffer_sformat_vp(SilcStack stack, SilcBuffer dst, va_list ap);
  ***/
 int silc_buffer_unformat(SilcBuffer src, ...);
 
+/****f* silcutil/SilcBufferFormatAPI/silc_buffer_sunformat
+ *
+ * SYNOPSIS
+ *
+ *    int silc_buffer_sunformat(SilcStack stack, SilcBuffer src, ...);
+ *
+ * DESCRIPTION
+ *
+ *    Same as silc_buffer_unformat but uses `stack' to allocate the memory.
+ *    if `stack' is NULL reverts back to silc_buffer_format call.
+ *
+ *    Note that this call consumes the `stack'.  The caller should push the
+ *    stack before calling the function and pop it later.
+ *
+ ***/
+int silc_buffer_sunformat(SilcStack stack, SilcBuffer src, ...);
+
 /****f* silcutil/SilcBufferFormatAPI/silc_buffer_unformat_vp
  *
  * SYNOPSIS
@@ -159,20 +279,37 @@ int silc_buffer_unformat(SilcBuffer src, ...);
  ***/
 int silc_buffer_unformat_vp(SilcBuffer src, va_list ap);
 
+/****f* silcutil/SilcBufferFormatAPI/silc_buffer_sunformat_vp
+ *
+ * SYNOPSIS
+ *
+ *    int silc_buffer_sunformat_vp(SilcBuffer src, va_list vp);
+ *
+ * DESCRIPTION
+ *
+ *    Same as silc_buffer_unformat_vp but uses `stack' to allocate the
+ *    memory.  if `stack' is NULL reverts back to silc_buffer_format_vp call.
+ *
+ *    Note that this call consumes the `stack'.  The caller should push the
+ *    stack before calling the function and pop it later.
+ *
+ ***/
+int silc_buffer_sunformat_vp(SilcStack stack, SilcBuffer src, va_list ap);
+
 /****f* silcutil/SilcBufferFormatAPI/silc_buffer_strformat
  *
  * SYNOPSIS
  *
- *   int silc_buffer_strformat(SilcBuffer dst, ...);
+ *    int silc_buffer_strformat(SilcBuffer dst, ...);
  *
  * DESCRIPTION
  *
- *   Formats a buffer from variable argument list of strings.  Each
- *   string must be NULL-terminated and the variable argument list must
- *   be end with SILC_STR_END argument.  This allows that a string in
- *   the list can be NULL, in which case it is skipped.  This automatically
- *   allocates the space for the buffer data but `dst' must be already
- *   allocated by the caller.
+ *    Formats a buffer from variable argument list of strings.  Each
+ *    string must be NULL-terminated and the variable argument list must
+ *    be end with SILC_STRFMT_END argument.  This allows that a string in
+ *    the list can be NULL, in which case it is skipped.  This automatically
+ *    allocates the space for the buffer data but `dst' must be already
+ *    allocated by the caller.  Returns -1 if system is out of memory.
  *
  * EXAMPLE
  *
@@ -187,153 +324,168 @@ int silc_buffer_strformat(SilcBuffer dst, ...);
  *
  * SYNOPSIS
  *
- *   int silc_buffer_strformat(SilcStack stack, SilcBuffer dst, ...);
+ *    int silc_buffer_strformat(SilcStack stack, SilcBuffer dst, ...);
  *
  * DESCRIPTION
  *
- *   Formats a buffer from variable argument list of strings.  Each
- *   string must be NULL-terminated and the variable argument list must
- *   be end with SILC_STR_END argument.  This allows that a string in
- *   the list can be NULL, in which case it is skipped.  This automatically
- *   allocates the space for the buffer data but `dst' must be already
- *   allocated by the caller.  This function is equivalent to
- *   silc_buffer_strformat but allocates memory from `stack'.
+ *    Formats a buffer from variable argument list of strings.  Each
+ *    string must be NULL-terminated and the variable argument list must
+ *    be end with SILC_STRFMT_END argument.  This allows that a string in
+ *    the list can be NULL, in which case it is skipped.  This automatically
+ *    allocates the space for the buffer data but `dst' must be already
+ *    allocated by the caller.  This function is equivalent to
+ *    silc_buffer_strformat but allocates memory from `stack'.  Returns -1
+ *    if system is out of memory.
+ *
+ *    Note that this call consumes the `stack'.  The caller should push the
+ *    stack before calling the function and pop it later.
  *
  ***/
 int silc_buffer_sstrformat(SilcStack stack, SilcBuffer dst, ...);
 
-/* Macros for expanding parameters into variable function argument list.
-   These are passed to silc_buffer_format and silc_buffer_unformat
-   functions. */
+/****d* silcutil/SilcBufferFormatAPI/SILC_STR_SINT8
+ *
+ * NAME
+ *
+ *    #define SILC_STR_SINT8() ...
+ *
+ * DESCRIPTION
+ *
+ *    One 8-bit signed integer.
+ *
+ *    Formatting:    SILC_STR_SINT8(SilcInt8)
+ *    Unformatting:  SILC_STR_SINT8(SilcInt8 *)
+ *
+ ***/
+#define SILC_STR_SINT8(x) SILC_PARAM_SINT8, (x)
+
+/****d* silcutil/SilcBufferFormatAPI/SILC_STR_UINT8
+ *
+ * NAME
+ *
+ *    #define SILC_STR_UINT8() ...
+ *
+ * DESCRIPTION
+ *
+ *    One 8-bit unsigned integer.
+ *
+ *    Formatting:    SILC_STR_UINT8(SilcUInt8)
+ *    Unformatting:  SILC_STR_UINT8(SilcUInt8 *)
+ *
+ ***/
+#define SILC_STR_UINT8(x) SILC_PARAM_UINT8, (x)
 
-/* Buffer parameter types.
+/* Deprecated */
+#define SILC_STR_SI_CHAR(x) SILC_PARAM_SINT8, (x)
+#define SILC_STR_UI_CHAR(x) SILC_PARAM_UINT8, (x)
 
-   _SI_ = signed
-   _UI_ = unsigned
+/****d* silcutil/SilcBufferFormatAPI/SILC_STR_SINT16
+ *
+ * NAME
+ *
+ *    #define SILC_STR_SINT16() ...
+ *
+ * DESCRIPTION
+ *
+ *    SilcInt16.
+ *
+ *    Formatting:    SILC_STR_SINT16(SilcInt16)
+ *    Unformatting:  SILC_STR_SINT16(SilcInt16 *)
+ *
+ ***/
+#define SILC_STR_SINT16(x) SILC_PARAM_SINT16, (x)
 
-  Any XXX_STRING_ALLOC types will allocate space for the data and
-  memcpy the data to the pointer sent as argument (in unformatting).
+/****d* silcutil/SilcBufferFormatAPI/SILC_STR_UINT16
+ *
+ * NAME
+ *
+ *    #define SILC_STR_UINT16() ...
+ *
+ * DESCRIPTION
+ *
+ *    SilcUInt16.
+ *
+ *    Formatting:    SILC_STR_UINT16(SilcUInt16)
+ *    Unformatting:  SILC_STR_UINT16(SilcUInt16 *)
+ *
+ ***/
+#define SILC_STR_UINT16(x) SILC_PARAM_UINT16, (x)
 
-  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.
+/* Deprecated */
+#define SILC_STR_SI_SHORT(x) SILC_PARAM_SINT16, (x)
+#define SILC_STR_UI_SHORT(x) SILC_PARAM_UINT16, (x)
 
-*/
-typedef enum {
-  SILC_BUFFER_PARAM_SI8_CHAR,
-  SILC_BUFFER_PARAM_UI8_CHAR,
-
-  SILC_BUFFER_PARAM_SI16_SHORT,
-  SILC_BUFFER_PARAM_UI16_SHORT,
-
-  SILC_BUFFER_PARAM_SI32_INT,
-  SILC_BUFFER_PARAM_UI32_INT,
-
-  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_DATA,              /* No copy */
-  SILC_BUFFER_PARAM_DATA_ALLOC,                /* Alloc + memcpy */
-
-  SILC_BUFFER_PARAM_OFFSET,
-  SILC_BUFFER_PARAM_ADVANCE,
-
-  SILC_BUFFER_PARAM_END
-} SilcBufferParamType;
-
-/****d* silcutil/SilcBufferFormatAPI/SILC_STR_*_CHAR
+/****d* silcutil/SilcBufferFormatAPI/SILC_STR_SINT32
  *
  * NAME
  *
- *    #define SILC_STR_UI_CHAR() ...
- *    #define SILC_STR_SI_CHAR() ...
+ *    #define SILC_STR_SINT32() ...
  *
  * DESCRIPTION
  *
- *    One signed/unsigned character.
+ *    SilcInt32.
  *
- *    Formatting:    SILC_STR_SI_CHAR(char)
- *                   SILC_STR_UI_CHAR(unsigned char)
- *    Unformatting:  SILC_STR_SI_CHAR(char *)
- *                   SILC_STR_UI_CHAR(unsigned char *)
+ *    Formatting:    SILC_STR_SINT32(SilcInt32)
+ *    Unformatting:  SILC_STR_SINT32(SilcInt32 *)
  *
  ***/
-#define SILC_STR_SI_CHAR(x) SILC_BUFFER_PARAM_SI8_CHAR, (x)
-#define SILC_STR_UI_CHAR(x) SILC_BUFFER_PARAM_UI8_CHAR, (x)
+#define SILC_STR_SINT32(x) SILC_PARAM_SINT32, (x)
 
-/****d* silcutil/SilcBufferFormatAPI/SILC_STR_*_SHORT
+/****d* silcutil/SilcBufferFormatAPI/SILC_STR_UINT32
  *
  * NAME
  *
- *    #define SILC_STR_UI_SHORT() ...
- *    #define SILC_STR_SI_SHORT() ...
+ *    #define SILC_STR_UINT32() ...
  *
  * DESCRIPTION
  *
- *    Signed/SilcUInt16.
+ *    SilcUInt32.
  *
- *    Formatting:    SILC_STR_SI_SHORT(short)
- *                   SILC_STR_UI_SHORT(SilcUInt16)
- *    Unformatting:  SILC_STR_SI_SHORT(short *)
- *                   SILC_STR_UI_SHORT(SilcUInt16 *)
+ *    Formatting:    SILC_STR_UINT32(SilcUInt32)
+ *    Unformatting:  SILC_STR_UINT32(SilcUInt32 *)
  *
  ***/
-#define SILC_STR_SI_SHORT(x) SILC_BUFFER_PARAM_SI16_SHORT, (x)
-#define SILC_STR_UI_SHORT(x) SILC_BUFFER_PARAM_UI16_SHORT, (x)
+#define SILC_STR_UINT32(x) SILC_PARAM_UINT32, (x)
+
+/* Deprecated */
+#define SILC_STR_SI_INT(x) SILC_PARAM_SINT32, (x)
+#define SILC_STR_UI_INT(x) SILC_PARAM_UINT32, (x)
 
-/****d* silcutil/SilcBufferFormatAPI/SILC_STR_*_INT
+/****d* silcutil/SilcBufferFormatAPI/SILC_STR_SINT64
  *
  * NAME
  *
- *    #define SILC_STR_UI_INT() ...
- *    #define SILC_STR_SI_INT() ...
+ *    #define SILC_STR_SINT64() ...
  *
  * DESCRIPTION
  *
- *    Signed/SilcUInt32.
+ *    SilcInt64.
  *
- *    Formatting:    SILC_STR_SI_INT(int)
- *                   SILC_STR_UI_INT(SilcUInt32)
- *    Unformatting:  SILC_STR_SI_INT(int *)
- *                   SILC_STR_UI_INT(SilcUInt32 *)
+ *    Formatting:    SILC_STR_SINT64(SilcInt64)
+ *    Unformatting:  SILC_STR_SINT64(SilcInt64 *)
  *
  ***/
-#define SILC_STR_SI_INT(x) SILC_BUFFER_PARAM_SI32_INT, (x)
-#define SILC_STR_UI_INT(x) SILC_BUFFER_PARAM_UI32_INT, (x)
+#define SILC_STR_SI_INT64(x) SILC_PARAM_SINT64, (x)
 
-/****d* silcutil/SilcBufferFormatAPI/SILC_STR_*_INT64
+/****d* silcutil/SilcBufferFormatAPI/SILC_STR_UINT64
  *
  * NAME
  *
- *    #define SILC_STR_UI_INT64() ...
- *    #define SILC_STR_SI_INT64() ...
+ *    #define SILC_STR_UINT64() ...
  *
  * DESCRIPTION
  *
- *    Signed/SilcUInt64.
+ *    SilcUInt64.
  *
- *     Formatting:    SILC_STR_SI_INT64(int)
- *                    SILC_STR_UI_INT64(SilcUInt32)
- *     Unformatting:  SILC_STR_SI_INT64(int *)
- *                    SILC_STR_UI_INT64(SilcUInt32 *)
+ *    Formatting:    SILC_STR_UINT64(SilcUInt64)
+ *    Unformatting:  SILC_STR_UINT64(SilcUInt64 *)
  *
  ***/
-#define SILC_STR_SI_INT64(x) SILC_BUFFER_PARAM_SI64_INT, (x)
-#define SILC_STR_UI_INT64(x) SILC_BUFFER_PARAM_UI64_INT, (x)
+#define SILC_STR_UI_INT64(x) SILC_PARAM_UINT64, (x)
+
+/* Deprecated */
+#define SILC_STR_SI_INT64(x) SILC_PARAM_SINT64, (x)
+#define SILC_STR_UI_INT64(x) SILC_PARAM_UINT64, (x)
 
 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_*_STRING
  *
@@ -378,12 +530,12 @@ 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)
-#define SILC_STR_UI32_STRING_ALLOC(x) SILC_BUFFER_PARAM_UI32_STRING_ALLOC, (x)
+#define SILC_STR_UI8_STRING(x) SILC_PARAM_UI8_STRING, (x)
+#define SILC_STR_UI8_STRING_ALLOC(x) SILC_PARAM_UI8_STRING | SILC_PARAM_ALLOC, (x)
+#define SILC_STR_UI16_STRING(x) SILC_PARAM_UI16_STRING, (x)
+#define SILC_STR_UI16_STRING_ALLOC(x) SILC_PARAM_UI16_STRING | SILC_PARAM_ALLOC, (x)
+#define SILC_STR_UI32_STRING(x) SILC_PARAM_UI32_STRING, (x)
+#define SILC_STR_UI32_STRING_ALLOC(x) SILC_PARAM_UI32_STRING | SILC_PARAM_ALLOC, (x)
 
 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_*_NSTRING
  *
@@ -428,15 +580,15 @@ 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(x, l) SILC_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)
+  SILC_PARAM_UI8_NSTRING | SILC_PARAM_ALLOC, (x), (l)
+#define SILC_STR_UI16_NSTRING(x, l) SILC_PARAM_UI16_NSTRING, (x), (l)
 #define SILC_STR_UI16_NSTRING_ALLOC(x, l) \
-  SILC_BUFFER_PARAM_UI16_NSTRING_ALLOC, (x), (l)
-#define SILC_STR_UI32_NSTRING(x, l) SILC_BUFFER_PARAM_UI32_NSTRING, (x), (l)
+  SILC_PARAM_UI16_NSTRING | SILC_PARAM_ALLOC, (x), (l)
+#define SILC_STR_UI32_NSTRING(x, l) SILC_PARAM_UI32_NSTRING, (x), (l)
 #define SILC_STR_UI32_NSTRING_ALLOC(x, l) \
-  SILC_BUFFER_PARAM_UI32_NSTRING_ALLOC, (x), (l)
+  SILC_PARAM_UI32_NSTRING | SILC_PARAM_ALLOC, (x), (l)
 
 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_DATA
  *
@@ -447,7 +599,7 @@ typedef enum {
  *
  * DESCRIPTION
  *
- *    Binary data formatting.  Second argument is the lenght of the data.
+ *    Binary data formatting.  Second argument is the length of the data.
  *
  *    Formatting:    SILC_STR_DATA(unsigned char *, SilcUInt32)
  *    Unformatting:  SILC_STR_DATA(unsigned char **, SilcUInt32)
@@ -459,14 +611,91 @@ typedef enum {
  *    as argument in unformatting.
  *
  ***/
-#define SILC_STR_DATA(x, l) SILC_BUFFER_PARAM_DATA, (x), (l)
-#define SILC_STR_DATA_ALLOC(x, l) \
-  SILC_BUFFER_PARAM_DATA_ALLOC, (x), (l)
+#define SILC_STR_DATA(x, l) SILC_PARAM_UICHAR, (x), (l)
+#define SILC_STR_DATA_ALLOC(x, l) SILC_PARAM_UICHAR | SILC_PARAM_ALLOC, (x), (l)
 
 /* Deprecated */
-#define SILC_STR_UI_XNSTRING(x, l) SILC_BUFFER_PARAM_UI_XNSTRING, (x), (l)
-#define SILC_STR_UI_XNSTRING_ALLOC(x, l) \
-  SILC_BUFFER_PARAM_UI_XNSTRING_ALLOC, (x), (l)
+#define SILC_STR_UI_XNSTRING(x, l) SILC_PARAM_UICHAR, (x), (l)
+#define SILC_STR_UI_XNSTRING_ALLOC(x, l) SILC_PARAM_UICHAR | SILC_PARAM_ALLOC, (x), (l)
+
+/****d* silcutil/SilcBufferFormatAPI/SILC_STR_BUFFER
+ *
+ * NAME
+ *
+ *    #define SILC_STR_BUFFER() ...
+ *    #define SILC_STR_BUFFER_ALLOC() ...
+ *
+ * DESCRIPTION
+ *
+ *    SilcBuffer formatting.
+ *
+ *    Formatting:    SILC_STR_BUFFER(SilcBuffer)
+ *    Unformatting:  SILC_STR_BUFFER(SilcBuffer)
+ *
+ *    This type can be used to format and unformat SilcBuffer.  Note that, the
+ *    length of the buffer will be automatically encoded into the buffer as
+ *    a 32-bit integer.  In unformatting the SilcBuffer context must be
+ *    pre-allocated.
+ *
+ *    _ALLOC routines automatically allocates memory inside SilcBuffer in
+ *    unformatting.
+ *
+ ***/
+#define SILC_STR_BUFFER(x) SILC_PARAM_BUFFER, (x)
+#define SILC_STR_BUFFER_ALLOC(x) SILC_PARAM_BUFFER | SILC_PARAM_ALLOC, (x)
+
+/****d* silcutil/SilcBufferFormatAPI/SILC_STR_FUNC
+ *
+ * NAME
+ *
+ *    #define SILC_STR_FUNC() ...
+ *
+ * DESCRIPTION
+ *
+ *    SilcBuffer formatting.
+ *
+ *    Formatting:    SILC_STR_FUNC(function, void *value, void *context)
+ *    Unformatting:  SILC_STR_FUNC(function, void **value, void *context)
+ *
+ *    This type can be used to call the `function' of the type
+ *    SilcBufferFormatFunc or SilcBufferUnformatFunc to encode or decode
+ *    the `value'.  In encoding the `value' will be passed to the `function'
+ *    and can be encoded into the buffer.  The buffer will be passed as
+ *    well to the `function' at the location where SILC_STR_FUNC is placed
+ *    in formatting.  The `context' delivers caller specific context to
+ *    the `function'
+ *
+ *    In unformatting the `function' will decode the encoded type and
+ *    return it to `value' pointer.  The decoding function should decide
+ *    itself whether to allocate or not the decoded value.
+ *
+ *    The `function' does not have to encode anything and passing `value'
+ *    as NULL is allowed.  The `function' could for example modify the
+ *    existing buffer.
+ *
+ * EXAMPLE
+ *
+ *    // Encode payload, encrypt and compute MAC.
+ *    silc_buffer_format(buf,
+ *                       SILC_STR_FUNC(foo_encode_id, id, ctx),
+ *                       SILC_STR_UINT16(len),
+ *                       SILC_STR_DATA(data, len),
+ *                       SILC_STR_FUNC(foo_buf_encrypt, NULL, key),
+ *                       SILC_STR_FUNC(foo_buf_hmac, NULL, hmac),
+ *                       SILC_STR_DATA(iv, iv_len);
+ *                       SILC_STR_END);
+ *
+ *    // Check MAC, decrypt and decode payload
+ *    silc_buffer_unformat(buf,
+ *                         SILC_STR_FUNC(foo_buf_hmac, NULL, hmac),
+ *                         SILC_STR_FUNC(foo_buf_decrypt, NULL, key),
+ *                         SILC_STR_FUNC(foo_decode_id, &id, ctx),
+ *                         SILC_STR_UINT16(&len),
+ *                         SILC_STR_END);
+ *
+ ***/
+#define SILC_STR_FUNC(func, val, context) SILC_PARAM_FUNC, \
+    func, (val), (context)
 
 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_OFFSET
  *
@@ -491,7 +720,7 @@ typedef enum {
  *    can be used in formatting and unformatting at the same time.
  *
  ***/
-#define SILC_STR_OFFSET(x) SILC_BUFFER_PARAM_OFFSET, (x)
+#define SILC_STR_OFFSET(x) SILC_PARAM_OFFSET, (x)
 
 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_ADVANCE
  *
@@ -503,9 +732,10 @@ typedef enum {
  *
  *    Advance the buffer to the end of the data after the formatting is
  *    done.  In normal operation when the formatted data is written the
- *    buffer is located at the start of the data.  With SILC_STR_ADVANCE
- *    the buffer will be located at the end of the data.  This makes it
+ *    buffer is positioned at the start of the data.  With SILC_STR_ADVANCE
+ *    the buffer will be positioned at the end of the data.  This makes it
  *    easy to add new data immediately after the previously added data.
+ *    The SILC_STR_ADVANCE may also be used in unformatting.
  *
  * EXAMPLE
  *
@@ -515,15 +745,15 @@ typedef enum {
  *        // Add read data to the buffer
  *        silc_buffer_format(buffer,
  *                           SILC_STR_ADVANCE,
- *                           SILC_STR_UI_XNSTRING(buf, len),
+ *                           SILC_STR_DATA(buf, len),
  *                           SILC_STR_END);
  *    } while (len > 0);
  *
  *    // Move to beginning of buffer
- *    silc_buffer_push(buffer, silc_buffer_truelen(buffer));
+ *    silc_buffer_start(buffer);
  *
  ***/
-#define SILC_STR_ADVANCE SILC_BUFFER_PARAM_ADVANCE
+#define SILC_STR_ADVANCE SILC_PARAM_ADVANCE
 
 /****d* silcutil/SilcBufferFormatAPI/SILC_STR_END
  *
@@ -537,7 +767,7 @@ typedef enum {
  *    argument list or error will occur.
  *
  ***/
-#define SILC_STR_END SILC_BUFFER_PARAM_END
+#define SILC_STR_END SILC_PARAM_END
 
 /****d* silcutil/SilcBufferFormatAPI/SILC_STRFMT_END
  *