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
} while(0)
/* Check that there is data to be unformatted */
-#define UNFORMAT_HAS_SPACE(b, req) \
-do { \
- if (silc_unlikely(req > silc_buffer_len(b))) \
- goto fail; \
- if (silc_unlikely((req + 1) <= 0)) \
- goto fail; \
+#define UNFORMAT_HAS_SPACE(b, req) \
+do { \
+ if (silc_unlikely(req > silc_buffer_len(b))) { \
+ silc_set_errno(SILC_ERR_OVERFLOW); \
+ goto fail; \
+ } \
+ if (silc_unlikely((req + 1) <= 0)) { \
+ silc_set_errno(SILC_ERR_UNDERFLOW); \
+ goto fail; \
+ } \
} while(0)
switch(fmt) {
case SILC_PARAM_FUNC:
{
- SilcBufferFormatFunc func = NULL;
- SilcBufferSFormatFunc funcs = NULL;
+ SilcBufferFormatFunc func;
void *val;
void *context;
int tmp_len;
- if (!stack)
- func = va_arg(ap, SilcBufferFormatFunc);
- else
- funcs = va_arg(ap, SilcBufferSFormatFunc);
+ func = va_arg(ap, SilcBufferFormatFunc);
val = va_arg(ap, void *);
context = va_arg(ap, void *);
- if (!stack)
- tmp_len = func(dst, val, context);
- else
- tmp_len = funcs(stack, dst, val, context);
+ tmp_len = func(stack, dst, val, context);
if (tmp_len < 0)
goto fail;
if (tmp_len) {
case SILC_PARAM_UI16_STRING_ALLOC:
case SILC_PARAM_UI32_STRING_ALLOC:
{
- unsigned char *x = va_arg(ap, unsigned char *);
- SilcUInt32 tmp_len = strlen(x);
- FORMAT_HAS_SPACE(stack, dst, tmp_len);
- silc_buffer_put(dst, x, tmp_len);
- silc_buffer_pull(dst, tmp_len);
+ char *x = va_arg(ap, char *);
+ SilcUInt32 tmp_len = x ? strlen(x) : 0;
+ if (x && tmp_len) {
+ FORMAT_HAS_SPACE(stack, dst, tmp_len);
+ silc_buffer_put(dst, (unsigned char *)x, tmp_len);
+ silc_buffer_pull(dst, tmp_len);
+ }
break;
}
case SILC_PARAM_UI8_NSTRING:
{
char x = (char)va_arg(ap, int);
FORMAT_HAS_SPACE(stack, dst, 1);
- silc_buffer_put(dst, &x, 1);
+ silc_buffer_put(dst, (unsigned char *)&x, 1);
silc_buffer_pull(dst, 1);
break;
}
if (!offst)
break;
if (offst > 1) {
- if (offst > silc_buffer_len(dst))
+ if (offst > silc_buffer_len(dst)) {
+ silc_set_errno(SILC_ERR_OVERFLOW);
goto fail;
+ }
silc_buffer_pull(dst, offst);
flen += offst;
} else {
default:
SILC_LOG_DEBUG(("Bad buffer formatting type `%d'. Could not "
"format the data.", fmt));
+ silc_set_errno_reason(SILC_ERR_INVALID_ARGUMENT,
+ "Bad buffer formatting type %d", fmt);
goto fail;
break;
}
switch(fmt) {
case SILC_PARAM_FUNC:
{
- SilcBufferUnformatFunc func = NULL;
- SilcBufferSUnformatFunc funcs = NULL;
+ SilcBufferUnformatFunc func;
void **val;
void *context;
int tmp_len;
- if (!stack)
- func = va_arg(ap, SilcBufferUnformatFunc);
- else
- funcs = va_arg(ap, SilcBufferSUnformatFunc);
+ func = va_arg(ap, SilcBufferUnformatFunc);
val = va_arg(ap, void **);
context = va_arg(ap, void *);
- if (!stack)
- tmp_len = func(src, val, context);
- else
- tmp_len = funcs(stack, src, val, context);
+ tmp_len = func(stack, src, val, context);
if (tmp_len < 0)
goto fail;
if (tmp_len) {
default:
SILC_LOG_DEBUG(("Bad buffer formatting type `%d'. Could not "
"format the data.", fmt));
+ silc_set_errno_reason(SILC_ERR_INVALID_ARGUMENT,
+ "Bad buffer formatting type %d", fmt);
goto fail;
break;
}
goto ok;
slen = strlen(string);
- d = silc_srealloc_ua(stack, len + 1, dst->head,
- sizeof(*dst->head) * (slen + len + 1));
+ d = silc_srealloc(stack, len + 1, dst->head,
+ sizeof(*dst->head) * (slen + len + 1));
if (silc_unlikely(!d))
return -1;
dst->head = d;