*/
-/****h* silcutil/SILC Buffer Format Interface
+/****h* silcutil/Buffer Format Interface
*
* DESCRIPTION
*
* macro provides many different flags that can change the behavior of the
* matching, with capabilities to also mimic Sed behavior.
*
- * As the SilcBuffer API is not thread-safe these routines may not be used
+ * As the SilcBuffer context is not thread-safe these routines may not be used
* in multithreaded environment with a same SilcBuffer context without
* concurrency control.
*
*
* SilcBufferStruct buffer;
*
+ * // Encode buffer
* memset(&buffer, 0, sizeof(buffer));
* ret = silc_buffer_format(&buffer,
* SILC_STR_UINT32(intval),
#ifndef SILCBUFFMT_H
#define SILCBUFFMT_H
-/****f* silcutil/SilcBufferFormatAPI/SilcBufferFormatFunc
+/****f* silcutil/SilcBufferFormatFunc
*
* SYNOPSIS
*
typedef int (*SilcBufferFormatFunc)(SilcStack stack, SilcBuffer buffer,
void *value, void *context);
-/****f* silcutil/SilcBufferFormatAPI/SilcBufferUnformatFunc
+/****f* silcutil/SilcBufferUnformatFunc
*
* SYNOPSIS
*
/* Prototypes */
-/****f* silcutil/SilcBufferFormatAPI/silc_buffer_format
+/****f* silcutil/silc_buffer_format
*
* SYNOPSIS
*
* silc_buffer_purge(&buffer);
*
* // Dynamically allocated zero size buffer
- * SilcBuffer buf;
+ * SilcBuffer buf;
* buf = silc_buffer_alloc(0);
* ret = silc_buffer_format(buf,
* SILC_STR_UINT32(intval),
***/
int silc_buffer_format(SilcBuffer dst, ...);
-/****f* silcutil/SilcBufferFormatAPI/silc_buffer_sformat
+/****f* silcutil/silc_buffer_sformat
*
* SYNOPSIS
*
***/
int silc_buffer_sformat(SilcStack stack, SilcBuffer dst, ...);
-/****f* silcutil/SilcBufferFormatAPI/silc_buffer_format_vp
+/****f* silcutil/silc_buffer_format_vp
*
* SYNOPSIS
*
***/
int silc_buffer_format_vp(SilcBuffer dst, va_list ap);
-/****f* silcutil/SilcBufferFormatAPI/silc_buffer_sformat_vp
+/****f* silcutil/silc_buffer_sformat_vp
*
* SYNOPSIS
*
***/
int silc_buffer_sformat_vp(SilcStack stack, SilcBuffer dst, va_list ap);
-/****f* silcutil/SilcBufferFormatAPI/silc_buffer_unformat
+/****f* silcutil/silc_buffer_unformat
*
* SYNOPSIS
*
***/
int silc_buffer_unformat(SilcBuffer src, ...);
-/****f* silcutil/SilcBufferFormatAPI/silc_buffer_sunformat
+/****f* silcutil/silc_buffer_sunformat
*
* SYNOPSIS
*
***/
int silc_buffer_sunformat(SilcStack stack, SilcBuffer src, ...);
-/****f* silcutil/SilcBufferFormatAPI/silc_buffer_unformat_vp
+/****f* silcutil/silc_buffer_unformat_vp
*
* SYNOPSIS
*
***/
int silc_buffer_unformat_vp(SilcBuffer src, va_list ap);
-/****f* silcutil/SilcBufferFormatAPI/silc_buffer_sunformat_vp
+/****f* silcutil/silc_buffer_sunformat_vp
*
* SYNOPSIS
*
***/
int silc_buffer_sunformat_vp(SilcStack stack, SilcBuffer src, va_list ap);
-/****f* silcutil/SilcBufferFormatAPI/silc_buffer_strformat
+/****f* silcutil/silc_buffer_strformat
*
* SYNOPSIS
*
***/
int silc_buffer_strformat(SilcBuffer dst, ...);
-/****f* silcutil/SilcBufferFormatAPI/silc_buffer_sstrformat
+/****f* silcutil/silc_buffer_sstrformat
*
* SYNOPSIS
*
***/
int silc_buffer_sstrformat(SilcStack stack, SilcBuffer dst, ...);
-/****d* silcutil/SilcBufferFormatAPI/SILC_STR_SINT8
+/****d* silcutil/SILC_STR_SINT8
*
* NAME
*
*
* One 8-bit signed integer.
*
- * Formatting: SILC_STR_SINT8(SilcInt8)
- * Unformatting: SILC_STR_SINT8(SilcInt8 *)
+ * 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
+/****d* silcutil/SILC_STR_UINT8
*
* NAME
*
*
* One 8-bit unsigned integer.
*
- * Formatting: SILC_STR_UINT8(SilcUInt8)
- * Unformatting: SILC_STR_UINT8(SilcUInt8 *)
+ * Formatting: SILC_STR_UINT8(SilcUInt8)
+ * Unformatting: SILC_STR_UINT8(SilcUInt8 *)
*
***/
#define SILC_STR_UINT8(x) SILC_PARAM_UINT8, (x)
#define SILC_STR_SI_CHAR(x) SILC_PARAM_SINT8, (x)
#define SILC_STR_UI_CHAR(x) SILC_PARAM_UINT8, (x)
-/****d* silcutil/SilcBufferFormatAPI/SILC_STR_SINT16
+/****d* silcutil/SILC_STR_SINT16
*
* NAME
*
*
* SilcInt16.
*
- * Formatting: SILC_STR_SINT16(SilcInt16)
- * Unformatting: SILC_STR_SINT16(SilcInt16 *)
+ * Formatting: SILC_STR_SINT16(SilcInt16)
+ * Unformatting: SILC_STR_SINT16(SilcInt16 *)
*
***/
#define SILC_STR_SINT16(x) SILC_PARAM_SINT16, (x)
-/****d* silcutil/SilcBufferFormatAPI/SILC_STR_UINT16
+/****d* silcutil/SILC_STR_UINT16
*
* NAME
*
*
* SilcUInt16.
*
- * Formatting: SILC_STR_UINT16(SilcUInt16)
- * Unformatting: SILC_STR_UINT16(SilcUInt16 *)
+ * Formatting: SILC_STR_UINT16(SilcUInt16)
+ * Unformatting: SILC_STR_UINT16(SilcUInt16 *)
*
***/
#define SILC_STR_UINT16(x) SILC_PARAM_UINT16, (x)
#define SILC_STR_SI_SHORT(x) SILC_PARAM_SINT16, (x)
#define SILC_STR_UI_SHORT(x) SILC_PARAM_UINT16, (x)
-/****d* silcutil/SilcBufferFormatAPI/SILC_STR_SINT32
+/****d* silcutil/SILC_STR_SINT32
*
* NAME
*
*
* SilcInt32.
*
- * Formatting: SILC_STR_SINT32(SilcInt32)
- * Unformatting: SILC_STR_SINT32(SilcInt32 *)
+ * Formatting: SILC_STR_SINT32(SilcInt32)
+ * Unformatting: SILC_STR_SINT32(SilcInt32 *)
*
***/
#define SILC_STR_SINT32(x) SILC_PARAM_SINT32, (x)
-/****d* silcutil/SilcBufferFormatAPI/SILC_STR_UINT32
+/****d* silcutil/SILC_STR_UINT32
*
* NAME
*
*
* SilcUInt32.
*
- * Formatting: SILC_STR_UINT32(SilcUInt32)
- * Unformatting: SILC_STR_UINT32(SilcUInt32 *)
+ * Formatting: SILC_STR_UINT32(SilcUInt32)
+ * Unformatting: SILC_STR_UINT32(SilcUInt32 *)
*
***/
#define SILC_STR_UINT32(x) SILC_PARAM_UINT32, (x)
#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_SINT64
+/****d* silcutil/SILC_STR_SINT64
*
* NAME
*
*
* SilcInt64.
*
- * Formatting: SILC_STR_SINT64(SilcInt64)
- * Unformatting: SILC_STR_SINT64(SilcInt64 *)
+ * Formatting: SILC_STR_SINT64(SilcInt64)
+ * Unformatting: SILC_STR_SINT64(SilcInt64 *)
*
***/
#define SILC_STR_SI_INT64(x) SILC_PARAM_SINT64, (x)
-/****d* silcutil/SilcBufferFormatAPI/SILC_STR_UINT64
+/****d* silcutil/SILC_STR_UINT64
*
* NAME
*
*
* SilcUInt64.
*
- * Formatting: SILC_STR_UINT64(SilcUInt64)
- * Unformatting: SILC_STR_UINT64(SilcUInt64 *)
+ * Formatting: SILC_STR_UINT64(SilcUInt64)
+ * Unformatting: SILC_STR_UINT64(SilcUInt64 *)
*
***/
#define SILC_STR_UI_INT64(x) SILC_PARAM_UINT64, (x)
#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
+/****d* silcutil/SILC_STR_STRING
*
* NAME
*
*
* Encode NULL terminated string. Use this only for formatting.
*
- * Formatting: SILC_STR_STRING(char *)
+ * Formatting: SILC_STR_STRING(char *)
*
* For unformatting use one of the SILC_STR_*_STRING macros, which
* automatically gets the length of the string from the buffer. Note
* SILC_STR_STRING does not save the length of the string into the buffer.
* The caller must do that in order for the unformatting macros to work.
*
- * Example:
+ * EXAMPLE
*
* Formatting: ..., SILC_STR_UINT32(strlen(string)),
* SILC_STR_STRING(string), ...
***/
#define SILC_STR_STRING(x) SILC_PARAM_UI8_STRING, (x)
-/****d* silcutil/SilcBufferFormatAPI/SILC_STR_STRING_APPEND
- *
- * NAME
- *
- * #define SILC_STR_STRING_APPEND() ...
- *
- * DESCRIPTION
- *
- * Encode NULL terminated string and append it to the buffer without
- * replacing any data if the end of the data area is reached before
- * encoding the whole string. If buffer has tail area, it will not be
- * replaced if the string is longer than the current data area, but the
- * buffer will be enlarged and the tail area will be copied to the new
- * tail area in order not to replace any data while appending the string.
- * This will then enlarge the current data area.
- *
- * Use this only for formatting.
- *
- * Formatting: SILC_STR_STRING_APPEND(char *)
- *
- * For unformatting use one of the SILC_STR_*_STRING macros, which
- * automatically gets the length of the string from the buffer. Note
- * SILC_STR_STRING_APPEND does not save the length of the string into the
- * buffer. The caller must do that in order for the unformatting macros
- * to work.
- *
- * Example:
- *
- * Formatting: ..., SILC_STR_UINT32(strlen(string)),
- * SILC_STR_STRING_APPEND(string), ...
- * Unformatting: ..., SILC_STR_UI32_STRING(&string), ...
- *
- ***/
-#define SILC_STR_STRING_APPEND(x) SILC_PARAM_UI8_STRING | SILC_PARAM_APPEND, (x)
-
-/****d* silcutil/SilcBufferFormatAPI/SILC_STR_*_STRING
+/****d* silcutil/SILC_STR_*_STRING
*
* NAME
*
* NULL terminated because strlen() will be used to get the length of
* the string.
*
- * Formatting: SILC_STR_UI32_STRING(unsigned char *)
- * Unformatting: SILC_STR_UI32_STRING(unsigned char **)
+ * Formatting: SILC_STR_UI32_STRING(unsigned char *)
+ * Unformatting: SILC_STR_UI32_STRING(unsigned char **)
*
* Unformatting procedure will check for length of the string from the
* buffer before trying to get the string out. Thus, one *must* format the
* length of the string because unformatting procedure will take it
* automatically.
*
- * Example:
+ * EXAMPLE
*
* Formatting: ..., SILC_STR_UINT32(strlen(string)),
* SILC_STR_UI32_STRING(string), ...
#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
+/****d* silcutil/SILC_STR_*_NSTRING
*
* NAME
*
*
* Unsigned string. Second argument is the length of the string.
*
- * Formatting: SILC_STR_UI32_NSTRING(unsigned char *, SilcUInt32)
- * Unformatting: SILC_STR_UI32_NSTRING(unsigned char **, SilcUInt32 *)
+ * 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
* length of the string because unformatting procedure will take it
* automatically.
*
- * Example:
+ * EXAMPLE
*
* Formatting: ..., SILC_STR_UINT32(strlen(string)),
* SILC_STR_UI32_NSTRING(string, strlen(string)), ...
#define SILC_STR_UI32_NSTRING_ALLOC(x, l) \
SILC_PARAM_UI32_NSTRING | SILC_PARAM_ALLOC, (x), (l)
-/****d* silcutil/SilcBufferFormatAPI/SILC_STR_DATA
+/****d* silcutil/SILC_STR_DATA
*
* NAME
*
*
* 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)
+ * Formatting: SILC_STR_DATA(unsigned char *, SilcUInt32)
+ * Unformatting: SILC_STR_DATA(unsigned char **, SilcUInt32)
*
* This type can be used to take arbitrary size data block from the buffer
* by sending the requested amount of bytes as argument.
#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
+/****d* silcutil/SILC_STR_BUFFER
*
* NAME
*
*
* SilcBuffer formatting.
*
- * Formatting: SILC_STR_BUFFER(SilcBuffer)
- * Unformatting: SILC_STR_BUFFER(SilcBuffer)
+ * 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
#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
+/****d* silcutil/SILC_STR_REPLACE
+ *
+ * NAME
+ *
+ * #define SILC_STR_REPLACE(unsigned char *data, SilcUInt32 len) ...
+ *
+ * DESCRIPTION
+ *
+ * Encode the `data' of length of `len' bytes into the buffer, replacing
+ * existing data.
+ *
+ * If the length of the current data area is equal to `len' the data area
+ * is replaced with `data'.
+ *
+ * If `len' is longer than the data area, it will be enlarged to fit the
+ * `data'. If there is data in the tail area, it will not be replaced,
+ * but the buffer is enlarged and the old tail area will be copied to the
+ * new tail area, thus preserving any data in the buffer (the data is
+ * in effect appended to the current data area).
+ *
+ * If `len' is shorter than than the data area, only the bytes in `data'
+ * are replaced, and rest are deleted from the data area. The buffer size
+ * is also reduced but no other data is lost in the process.
+ *
+ * If `len' is 0 but `data' is non-NULL, this will delete all bytes from
+ * the current data area. If `data' is NULL this macro has no effect to
+ * the buffer.
+ *
+ * Use this only for formatting.
+ *
+ * Formatting: SILC_STR_REPLACE(unsigned char *, SilcUInt32)
+ *
+ * EXAMPLE
+ *
+ * Before replacing
+ * -------------------------
+ * | head | 12345 | 67890 |
+ * -------------------------
+ *
+ * After replacing with 12345XXX (appends)
+ * ----------------------------
+ * | head | 12345XXX | 67890 |
+ * ----------------------------
+ *
+ * After replacing with YYY (deletes)
+ * -----------------------
+ * | head | YYY | 67890 |
+ * -----------------------
+ *
+ ***/
+#define SILC_STR_REPLACE(x, l) SILC_PARAM_UICHAR | SILC_PARAM_REPLACE, (x), (l)
+
+/****d* silcutil/SILC_STR_FUNC
*
* NAME
*
*
* Formatting and unformatting of arbitrary data.
*
- * Formatting: SILC_STR_FUNC(function, void *value, void *context)
- * Unformatting: SILC_STR_FUNC(function, void **value, void *context)
+ * 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
#define SILC_STR_FUNC(func, val, context) SILC_PARAM_FUNC, \
func, (val), (context)
-/****d* silcutil/SilcBufferFormatAPI/SilcBufferRegexFlags
+/****d* silcutil/SilcBufferRegexFlags
*
* NAME
*
typedef enum {
SILC_STR_REGEX_NONE = 0x00000000,
- /* By default mismatch will be skipped. Set this flag if mismatch should
- cause error and stopping of the formatting/unformatting. */
+ /* By default mismatch will be skipped. Skipping means that none of the
+ operations inside the SILC_STR_REGEX are executed. Set this flag if
+ mismatch should cause error and stopping of the formatting/unformatting. */
SILC_STR_REGEX_MISMATCH = 0x00000001,
/* By default only the first match is found. Set this flag to find
} SilcBufferRegexFlags;
/***/
-/****d* silcutil/SilcBufferFormatAPI/SILC_STR_REGEX
+/****d* silcutil/SILC_STR_REGEX
*
* NAME
*
*
* Regular expression matching within the buffer.
*
- * Formatting: SILC_STR_REGEX(char *regex, SilcBufferRegexFlags flags)
- * Unformatting: SILC_STR_REGEX(char *regex, SilcBufferRegexFlags flags)
+ * Formatting: SILC_STR_REGEX(char *regex, SilcBufferRegexFlags flags)
+ * Unformatting: SILC_STR_REGEX(char *regex, SilcBufferRegexFlags flags)
*
* SILC_STR_REGEX can be used to do regular expression matching within
* the SilcBuffer. When the string in the buffer matches the regular
* string matching and parsing, but not editing, except with SILC_STR_FUNC
* macro, which can do anything caller wants.
*
+ * Typically the following encoding macros are used with SILC_STR_REGEX:
+ * SILC_STR_REPLACE, SILC_STR_STRING and SILC_STR_FUNC. If you use
+ * SILC_STR_REPLACE always set SILC_STR_REGEX_INCLUSIVE.
+ *
* EXAMPLE
*
* // sed 's/foo/bar/', replace first foo with bar
* silc_buffer_format(buffer,
* SILC_STR_REGEX("foo", SILC_STR_REGEX_ALL |
* SILC_STR_REGEX_INCLUSIVE),
- * SILC_STR_STRING_APPEND("barbar"),
+ * SILC_STR_REPLACE("barbar", 5),
* SILC_STR_END, SILC_STR_END);
*
* // sed 's/foo/B/', replace foo with B, deleting rest of the match from
* silc_buffer_format(buffer,
* SILC_STR_REGEX("foo", SILC_STR_REGEX_ALL |
* SILC_STR_REGEX_INCLUSIVE),
- * SILC_STR_STRING("B"),
- * SILC_STR_DELETE(-1),
+ * SILC_STR_REPLACE("B", 1),
* SILC_STR_END, SILC_STR_END);
*
* // sed '/baz/s/foo/bar/g, replace all foo's with bar on lines with baz
***/
#define SILC_STR_REGEX(regex, flags) SILC_PARAM_REGEX, (regex), (flags)
-/****d* silcutil/SilcBufferFormatAPI/SILC_STR_DELETE
+/****d* silcutil/SILC_STR_DELETE
*
* NAME
*
* DESCRIPTION
*
* Deletes bytes from the buffer by moving data in order to delete it.
- * The size of the buffer remains same but the tail area of the buffer
- * will get larger as data is deleted from the current data area.
+ * The size of the buffer is also reduced, but no data is lost in the
+ * process.
*
* The `n' specifies the number of bytes to delete from the current
* data area. If `n' is -1 this deletes all bytes from the data area.
* This effectively moves the data from the tail area into the current
- * data area. The length of the data area after this is 0 and the tail
- * area is larger.
+ * data area. The length of the data area after this is 0.
*
* Use this only for formatting.
*
- * Formatting: SILC_STR_DELETE(int bytes)
- *
- * EXAMPLE
- *
- * // sed 's/foo/B/', replace foo with B, deleting rest of the match from
- * // the buffer. The match must be inclusive to make deleting work.
- * silc_buffer_format(buffer,
- * SILC_STR_REGEX("foo", SILC_STR_REGEX_ALL |
- * SILC_STR_REGEX_INCLUSIVE),
- * SILC_STR_STRING("B"),
- * SILC_STR_DELETE(-1),
- * SILC_STR_END, SILC_STR_END);
+ * Formatting: SILC_STR_DELETE(int bytes)
*
***/
#define SILC_STR_DELETE(x) SILC_PARAM_DELETE, (x)
-/****d* silcutil/SilcBufferFormatAPI/SILC_STR_OFFSET
+/****d* silcutil/SILC_STR_OFFSET
*
* NAME
*
* or backwards (negative offset). It can be used to for example skip
* some types during unformatting.
*
- * Example:
+ * EXAMPLE
*
* ..., SILC_STR_OFFSET(5), ...
* ..., SILC_STR_OFFSET(-3), ...
***/
#define SILC_STR_OFFSET(x) SILC_PARAM_OFFSET, (x)
-/****d* silcutil/SilcBufferFormatAPI/SILC_STR_OFFSET_START
+/****d* silcutil/SILC_STR_OFFSET_START
*
* NAME
*
*
* Moves the buffer position to the start of the data area.
*
- * Example:
+ * EXAMPLE
*
* ..., SILC_STR_OFFSET_START, ...
*
***/
#define SILC_STR_OFFSET_START SILC_PARAM_OFFSET_START
-/****d* silcutil/SilcBufferFormatAPI/SILC_STR_OFFSET_END
+/****d* silcutil/SILC_STR_OFFSET_END
*
* NAME
*
*
* Moves the buffer position to the end of the data area.
*
- * Example:
+ * EXAMPLE
*
* ..., SILC_STR_OFFSET_END, ...
*
***/
#define SILC_STR_OFFSET_END SILC_PARAM_OFFSET_END
-/****d* silcutil/SilcBufferFormatAPI/SILC_STR_ADVANCE
+/****d* silcutil/SILC_STR_ADVANCE
*
* NAME
*
***/
#define SILC_STR_ADVANCE SILC_PARAM_ADVANCE
-/****d* silcutil/SilcBufferFormatAPI/SILC_STR_END
+/****d* silcutil/SILC_STR_END
*
* NAME
*
***/
#define SILC_STR_END SILC_PARAM_END
-/****d* silcutil/SilcBufferFormatAPI/SILC_STRFMT_END
+/****d* silcutil/SILC_STRFMT_END
*
* NAME
*