} \
} while(0)
+#if defined(SILC_DEBUG)
+static const char *silc_param_string(SilcParam fmt)
+{
+ if (fmt == SILC_PARAM_SINT8)
+ return "SINT8";
+ if (fmt == SILC_PARAM_UINT8)
+ return "UINT8";
+ if (fmt == SILC_PARAM_SINT16)
+ return "SINT16";
+ if (fmt == SILC_PARAM_UINT16)
+ return "UINT16";
+ if (fmt == SILC_PARAM_SINT32)
+ return "SINT32";
+ if (fmt == SILC_PARAM_UINT32)
+ return "UINT32";
+ if (fmt == SILC_PARAM_SINT64)
+ return "SINT64";
+ if (fmt == SILC_PARAM_UINT64)
+ return "UINT64";
+ if (fmt == SILC_PARAM_SICHAR)
+ return "SICHAR";
+ if (fmt == (SILC_PARAM_SICHAR | SILC_PARAM_ALLOC))
+ return "SICHAR ALLOC";
+ if (fmt == SILC_PARAM_UICHAR)
+ return "UICHAR";
+ if (fmt == (SILC_PARAM_UICHAR | SILC_PARAM_ALLOC))
+ return "UICHAR ALLOC";
+ if (fmt == (SILC_PARAM_UICHAR | SILC_PARAM_REPLACE))
+ return "UICHAR REPLACE";
+ if (fmt == SILC_PARAM_BUFFER)
+ return "BUFFER";
+ if (fmt == (SILC_PARAM_BUFFER | SILC_PARAM_ALLOC))
+ return "BUFFER ALLOC";
+ if (fmt == SILC_PARAM_PTR)
+ return "PTR";
+ if (fmt == SILC_PARAM_END)
+ return "END";
+ if (fmt == SILC_PARAM_UI8_STRING)
+ return "UI8_STRING";
+ if (fmt == SILC_PARAM_UI16_STRING)
+ return "UI16_STRING";
+ if (fmt == SILC_PARAM_UI32_STRING)
+ return "UI32_STRING";
+ if (fmt == SILC_PARAM_UI8_NSTRING)
+ return "UI8_STRING";
+ if (fmt == SILC_PARAM_UI16_NSTRING)
+ return "UI16_STRING";
+ if (fmt == SILC_PARAM_UI32_NSTRING)
+ return "UI32_STRING";
+ if (fmt == (SILC_PARAM_UI8_STRING | SILC_PARAM_ALLOC))
+ return "UI8_STRING ALLOC";
+ if (fmt == (SILC_PARAM_UI16_STRING | SILC_PARAM_ALLOC))
+ return "UI16_STRING ALLOC";
+ if (fmt == (SILC_PARAM_UI32_STRING | SILC_PARAM_ALLOC))
+ return "UI32_STRING ALLOC";
+ if (fmt == (SILC_PARAM_UI8_NSTRING | SILC_PARAM_ALLOC))
+ return "UI8_STRING ALLOC";
+ if (fmt == (SILC_PARAM_UI16_NSTRING | SILC_PARAM_ALLOC))
+ return "UI16_STRING ALLOC";
+ if (fmt == (SILC_PARAM_UI32_NSTRING | SILC_PARAM_ALLOC))
+ return "UI32_STRING";
+ if (fmt == SILC_PARAM_OFFSET)
+ return "OFFSET";
+ if (fmt == SILC_PARAM_ADVANCE)
+ return "ADDVANCE";
+ if (fmt == SILC_PARAM_FUNC)
+ return "FUNC";
+ if (fmt == SILC_PARAM_REGEX)
+ return "REGEX";
+ if (fmt == SILC_PARAM_OFFSET_START)
+ return "OFFSET_START";
+ if (fmt == SILC_PARAM_OFFSET_END)
+ return "OFFSET_END";
+ if (fmt == SILC_PARAM_DELETE)
+ return "DELETE";
+ return "";
+}
+#endif /* SILC_DEBUG */
/******************************* Formatting *********************************/
while (1) {
fmt = va_arg(ap, SilcParam);
- SILC_LOG_DEBUG(("Buffer format type %d", fmt));
+#if defined(SILC_DEBUG)
+ if (process)
+ SILC_LOG_DEBUG(("Buffer format type %s (%d)",
+ silc_param_string(fmt), fmt));
+#endif /* SILC_DEBUG */
switch (fmt) {
case SILC_PARAM_FUNC:
SilcBool match_all = (rflags & SILC_STR_REGEX_ALL) != 0;
SilcBool match_nl = (rflags & SILC_STR_REGEX_NL) != 0;
SilcBool ret;
- SilcUInt32 saved_pos = 0, inclusive_pos = 0;
+ SilcUInt32 inclusive_pos = 0;
int matched = 0, ret_len;
va_list cp;
break;
if (!regex)
- goto fail;
+ break;
if (match_nl) {
start_nl_match:
/* Match for '\n' in the buffer. If not found, treat as line
without '\n' (buffer has only one line, or this is last line). */
- saved_pos = silc_buffer_headlen(dst) + silc_buffer_len(dst);
if (silc_regex_buffer(dst, "\n", &match, NULL))
dst->tail = match.tail;
}
flen += (dst->tail - dst->data);
if (!silc_buffer_pull(dst, (dst->tail - dst->data)))
goto fail;
- if (!silc_buffer_pull_tail(dst, (saved_pos -
- silc_buffer_headlen(dst) +
- silc_buffer_len(dst))))
+ if (!silc_buffer_pull_tail(dst, silc_buffer_taillen(dst)))
goto fail;
if (silc_buffer_len(dst) > 0)
break;
}
- case SILC_PARAM_UI8_STRING | SILC_PARAM_APPEND:
- case SILC_PARAM_UI16_STRING | SILC_PARAM_APPEND:
- case SILC_PARAM_UI32_STRING | SILC_PARAM_APPEND:
+ case SILC_PARAM_UICHAR | SILC_PARAM_REPLACE:
{
- char *x = va_arg(ap, char *);
- SilcUInt32 tmp_len = x ? strlen(x) : 0;
+ unsigned char *x = va_arg(ap, unsigned char *);
+ SilcUInt32 x_len = va_arg(ap, SilcUInt32);
if (!process)
break;
- if (x && tmp_len) {
- FORMAT_HAS_SPACE_APPEND(stack, dst, tmp_len);
- silc_buffer_put(dst, x, tmp_len);
- silc_buffer_pull(dst, tmp_len);
+ if (!x)
+ break;
+
+ if (silc_buffer_len(dst) == x_len) {
+ /* Replace */
+ if (x_len) {
+ silc_buffer_put(dst, x, x_len);
+ silc_buffer_pull(dst, x_len);
+ flen += x_len;
+ }
+ } else if (silc_buffer_len(dst) < x_len) {
+ /* Append */
+ if (x_len) {
+ FORMAT_HAS_SPACE_APPEND(stack, dst, x_len);
+ silc_buffer_put(dst, x, x_len);
+ silc_buffer_pull(dst, x_len);
+ }
+ } else {
+ /* Delete */
+ if (x_len) {
+ silc_buffer_put(dst, x, x_len);
+ silc_buffer_pull(dst, x_len);
+ flen += x_len;
+ }
+ goto delete_rest;
}
break;
}
if (n == -1) {
/* Move all data from tail to data area */
if (dst->data != dst->tail) {
+ delete_rest:
+ n = silc_buffer_len(dst);
memmove(dst->data, dst->tail, silc_buffer_taillen(dst));
- memset(dst->end - silc_buffer_len(dst), 0, silc_buffer_len(dst));
- silc_buffer_push_tail(dst, silc_buffer_len(dst));
+ silc_buffer_push_tail(dst, n);
+ if (!silc_buffer_srealloc(stack, dst,
+ silc_buffer_truelen(dst) - n))
+ goto fail;
}
break;
}
memmove(dst->data, dst->data + n, (silc_buffer_len(dst) - n) +
silc_buffer_taillen(dst));
- memset(dst->end - n, 0, n);
silc_buffer_push_tail(dst, silc_buffer_len(dst) - n);
+ if (!silc_buffer_srealloc(stack, dst, silc_buffer_truelen(dst) - n))
+ goto fail;
break;
}
while (1) {
fmt = va_arg(ap, SilcParam);
- SILC_LOG_DEBUG(("Buffer unformat type %d", fmt));
+ SILC_LOG_DEBUG(("Buffer unformat type %s (%d)",
+ silc_param_string(fmt), fmt));
switch (fmt) {
case SILC_PARAM_FUNC:
SilcBool match_all = (rflags & SILC_STR_REGEX_ALL) != 0;
SilcBool match_nl = (rflags & SILC_STR_REGEX_NL) != 0;
SilcBool ret;
- SilcUInt32 saved_pos = 0, inclusive_pos = 0;
+ SilcUInt32 inclusive_pos = 0;
int matched = 0, ret_len;
va_list cp;
break;
if (!regex)
- goto fail;
-
+ break;
if (match_nl) {
start_nl_match:
/* Match for '\n' in the buffer. If not found, treat as line
without '\n' (buffer has only one line, or this is last line). */
- saved_pos = silc_buffer_headlen(src) + silc_buffer_len(src);
if (silc_regex_buffer(src, "\n", &match, NULL))
src->tail = match.tail;
}
if (!(rflags & SILC_STR_REGEX_NO_ADVANCE)) {
/* Advance buffer after match */
- UNFORMAT_HAS_SPACE(src, (match.data - src->data));
if (!silc_buffer_pull(src, (match.data - src->data)))
goto fail;
}
goto fail;
/* Advance buffer after formatting */
- UNFORMAT_HAS_SPACE(src, ret_len);
if (!silc_buffer_pull(src, ret_len))
goto fail;
if (match_nl) {
/* Go to next line, it is at the end of the data area. Adjust
the tail area of the target buffer to show rest of the buffer. */
- UNFORMAT_HAS_SPACE(src, src->tail - src->data);
if (!silc_buffer_pull(src, (src->tail - src->data)))
goto fail;
- if (!silc_buffer_pull_tail(src, (saved_pos -
- silc_buffer_headlen(src) +
- silc_buffer_len(src))))
+ if (!silc_buffer_pull_tail(src, silc_buffer_taillen(src)))
goto fail;
if (silc_buffer_len(src) > 0)
/* Skip to the next SILC_PARAM_END */
silc_buffer_sunformat_vp_i(NULL, src, ap, FALSE);
+ break;
}
break;
***/
#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
*
* NAME
#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_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/SilcBufferFormatAPI/SILC_STR_FUNC
*
* 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
* 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
* 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);
- *
***/
#define SILC_STR_DELETE(x) SILC_PARAM_DELETE, (x)