totally 8 bytes of memory per buffer.
-lib/silcutil/silcbuffmt.[ch] ****PARTY DONE****
+lib/silcutil/silcbuffmt.[ch] ****PARTLY DONE****
============================
- o SILC_STR_APPEND, _APPEND_TAIL.
+ o SilcStack aware silc_buffer_unformat
+
+ o silc_buffer_format reallocates automatically
o SILC_STR_OFFSET
o Some string encodings missing (copy/paste matter).
+ o Negative integer encoding
+
lib/silcpkix
============
lib/silcserver
==============
+ o (Re)write commands/command replys.
+
+ o (Re)write notify handling.
+
o The SERVER_SIGNOFF notify handing is not optimal, because it'll
cause sending of multiple SIGNOFF notify's instead of the one
SERVER_SIGNOFF notify that the server received. This should be
key ends up being used.
o The CMODE cipher & hmac change problem (#101).
+
+
+lib/silcclient
+==============
+
+ o Some form of rewrite to make it more efficient.
+
+ o Clear up interfaces.
+
+ o Remove silc_client_run and silc_client_run_one from calling SilcSchedule.
+ Leave the scheduling entirely to application.
Author: Pekka Riikonen <priikone@silcnet.org>
- Copyright (C) 2001 - 2005 Pekka Riikonen
+ Copyright (C) 2001 - 2006 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
len = 3 + (SilcUInt16)arg_len;
buffer = silc_buffer_realloc(buffer,
- (buffer ? silc_buffer_truelen(buffer) + len : len));
+ (buffer ? silc_buffer_truelen(buffer) +
+ len : len));
if (!buffer)
return NULL;
silc_buffer_pull(buffer, silc_buffer_len(buffer));
Author: Pekka Riikonen <priikone@silcnet.org>
- Copyright (C) 2002 - 2005 Pekka Riikonen
+ Copyright (C) 2002 - 2006 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
len = strlen(service->address);
len2 = strlen(service->signon);
tmpbuf = silc_buffer_alloc_size(13 + len + len2);
+ if (!tmpbuf)
+ return NULL;
silc_buffer_format(tmpbuf,
SILC_STR_UI_INT(service->port),
SILC_STR_UI_SHORT(len),
Author: Pekka Riikonen <priikone@silcnet.org>
- Copyright (C) 1997 - 2005 Pekka Riikonen
+ Copyright (C) 1997 - 2006 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
/* Sign the message if wanted */
if (flags & SILC_MESSAGE_FLAG_SIGNED && private_key && hash) {
- sig = silc_message_signed_payload_encode(buffer->data, silc_buffer_len(buffer),
+ sig = silc_message_signed_payload_encode(buffer->data,
+ silc_buffer_len(buffer),
public_key, private_key, hash);
if (sig) {
- buffer = silc_buffer_realloc(buffer, silc_buffer_truelen(buffer) + silc_buffer_len(sig));
+ buffer = silc_buffer_realloc(buffer, silc_buffer_truelen(buffer) +
+ silc_buffer_len(sig));
if (buffer) {
silc_buffer_pull(buffer, 6 + data_len + pad_len);
silc_buffer_pull_tail(buffer, silc_buffer_len(sig));
}
/* Put IV */
- silc_buffer_pull(buffer, 6 + data_len + pad_len + (sig ? silc_buffer_len(sig) : 0));
+ silc_buffer_pull(buffer, 6 + data_len + pad_len +
+ (sig ? silc_buffer_len(sig) : 0));
silc_buffer_pull_tail(buffer, iv_len);
silc_buffer_format(buffer,
SILC_STR_UI_XNSTRING(iv, iv_len),
SILC_STR_END);
- silc_buffer_push(buffer, 6 + data_len + pad_len + (sig ? silc_buffer_len(sig) : 0));
-
- SILC_LOG_HEXDUMP(("foo"), buffer->data, silc_buffer_len(buffer));
+ silc_buffer_push(buffer, 6 + data_len + pad_len +
+ (sig ? silc_buffer_len(sig) : 0));
/* Now encrypt the Message Payload and compute MAC */
if (cipher) {
return NULL;
}
}
- silc_buffer_pull_tail(buffer, silc_buffer_truelen(buffer) - silc_buffer_len(buffer));
+ silc_buffer_pull_tail(buffer, silc_buffer_truelen(buffer) -
+ silc_buffer_len(buffer));
silc_buffer_free(sig);
return buffer;
{
int ret = SILC_AUTH_FAILED;
SilcBuffer sign;
- SilcBuffer tmp;
+ SilcBufferStruct tmp;
if (!sig || !remote_public_key || !hash)
return ret;
/* Generate the signature verification data, the Message Payload */
- tmp = silc_buffer_alloc_size(6 + message->data_len + message->pad_len);
- silc_buffer_format(tmp,
+ memset(&tmp, 0, sizeof(tmp));
+ silc_buffer_format(&tmp,
SILC_STR_UI_SHORT(message->flags),
SILC_STR_UI_SHORT(message->data_len),
SILC_STR_UI_XNSTRING(message->data, message->data_len),
SILC_STR_UI_SHORT(message->pad_len),
SILC_STR_UI_XNSTRING(message->pad, message->pad_len),
SILC_STR_END);
- sign = silc_message_signed_encode_data(tmp->data, silc_buffer_len(tmp),
+ sign = silc_message_signed_encode_data(tmp.data, silc_buffer_len(&tmp),
sig->pk_data, sig->pk_len,
sig->pk_type);
- silc_buffer_clear(tmp);
- silc_buffer_free(tmp);
+ silc_buffer_clear(&tmp);
+ silc_free(silc_buffer_steal(&tmp, NULL));
if (!sign)
return ret;
Author: Pekka Riikonen <priikone@silcnet.org>
- Copyright (C) 2005 Pekka Riikonen
+ Copyright (C) 2005 - 2006 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
SilcUInt32 silc_mp_get_ui(SilcMPInt *mp)
{
- SILC_NOT_IMPLEMENTED("silc_mp_get_ui");
- assert(FALSE);
+ fp_int *tmp = mp;
+ return tmp->used > 0 ? tmp->dp[0] : 0;
}
char *silc_mp_get_str(char *str, SilcMPInt *mp, int base)
Author: Pekka Riikonen <priikone@silcnet.org>
- Copyright (C) 2005 Pekka Riikonen
+ Copyright (C) 2005 - 2006 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
Author: Pekka Riikonen <priikone@silcnet.org>
- Copyright (C) 1998 - 2005 Pekka Riikonen
+ Copyright (C) 1998 - 2006 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
if (!sb)
return NULL;
- /* Allocate the actual data area */
- sb->head = (unsigned char *)silc_calloc(len, sizeof(*sb->head));
- if (!sb->head)
- return NULL;
+ if (len) {
+ /* Allocate the actual data area */
+ sb->head = (unsigned char *)silc_calloc(len, sizeof(*sb->head));
+ if (!sb->head)
+ return NULL;
- /* Set pointers to the new buffer */
- sb->data = sb->head;
- sb->tail = sb->head;
- sb->end = sb->head + len;
+ /* Set pointers to the new buffer */
+ sb->data = sb->head;
+ sb->tail = sb->head;
+ sb->end = sb->head + len;
+ }
return sb;
}
*
* Allocates new SilcBuffer and returns it.
*
- * This routine use SilcStack are memory source.
+ * This routine use SilcStack are memory source. If `stack' is NULL
+ * reverts back to normal allocating routine.
*
***/
{
SilcBuffer sb;
+ if (!stack)
+ return silc_buffer_alloc(len);
+
/* Allocate new SilcBuffer */
sb = (SilcBuffer)silc_scalloc(stack, 1, sizeof(*sb));
if (!sb)
* `len' bytes so that the buffer is ready to use without calling the
* silc_buffer_pull_tail.
*
- * This routine use SilcStack are memory source.
+ * This routine use SilcStack are memory source. If `stack' is NULL
+ * reverts back to normal allocating routine.
*
***/
* is exact clone of the old one except that there is now more space
* at the end of buffer.
*
- * This routine use SilcStack are memory source.
+ * This routine use SilcStack are memory source. If `stack' is NULL
+ * reverts back to normal allocating routine.
*
***/
SilcUInt32 hlen, dlen;
unsigned char *h;
+ if (!stack)
+ return silc_buffer_realloc(sb, newsize);
+
if (!sb)
return silc_buffer_salloc(stack, newsize);
* automatically so that the buffer is ready to use without calling the
* silc_buffer_pull_tail.
*
- * This routine use SilcStack are memory source.
+ * This routine use SilcStack are memory source. If `stack' is NULL
+ * reverts back to normal allocating routine.
*
***/
* currently valid data area, nothing more. Use silc_buffer_clone to
* copy entire buffer.
*
- * This routine use SilcStack are memory source.
+ * This routine use SilcStack are memory source. If `stack' is NULL
+ * reverts back to normal allocating routine.
*
***/
* everything from the source buffer. The result is exact clone of
* the original buffer.
*
- * This routine use SilcStack are memory source.
+ * This routine use SilcStack are memory source. If `stack' is NULL
+ * reverts back to normal allocating routine.
*
***/
#include "silc.h"
-/* Macros to check whether there is enough free space to add the
- required amount of data. For unformatting this means that there must
- be the data that is to be extracted. */
-#define FORMAT_HAS_SPACE(__x__, __req__) \
- do { \
- if (__req__ > silc_buffer_len((__x__))) \
- goto fail; \
- } while(0)
+/************************** Types and definitions ***************************/
+
+/* Check that buffer has enough room to format data in it, if not
+ allocate more. */
+#define FORMAT_HAS_SPACE(__s__, __x__, __req__) \
+do { \
+ if (__req__ > silc_buffer_len((__x__))) \
+ if (!silc_buffer_srealloc_size((__s__), (__x__), \
+ silc_buffer_truelen((__x__)) + __req__)) \
+ goto fail; \
+ flen += __req__; \
+} while(0)
+
+/* Check that there is data to be unformatted */
#define UNFORMAT_HAS_SPACE(__x__, __req__) \
- do { \
- if (__req__ > silc_buffer_len((__x__))) \
- goto fail; \
- if ((__req__ + 1) <= 0) \
- goto fail; \
- } while(0)
-
-/* Formats the arguments sent and puts them into the buffer sent as
- argument. The buffer must be initialized beforehand and it must have
- enough free space to include the formatted data. If this function
- fails caller should not trust the buffer anymore and should free it.
- This function is used, for example, to create packets to send over
- network. */
+do { \
+ if (__req__ > silc_buffer_len((__x__))) \
+ goto fail; \
+ if ((__req__ + 1) <= 0) \
+ goto fail; \
+} while(0)
+
+
+/******************************* Formatting *********************************/
int silc_buffer_format(SilcBuffer dst, ...)
{
int ret;
va_start(ap, dst);
- ret = silc_buffer_format_vp(dst, ap);
+ ret = silc_buffer_sformat_vp(NULL, dst, ap);
va_end(ap);
return ret;
}
int silc_buffer_format_vp(SilcBuffer dst, va_list ap)
+{
+ return silc_buffer_sformat_vp(NULL, dst, ap);
+}
+
+int silc_buffer_sformat(SilcStack stack, SilcBuffer dst, ...)
+{
+ va_list ap;
+ int ret;
+
+ va_start(ap, dst);
+ ret = silc_buffer_sformat_vp(stack, dst, ap);
+ va_end(ap);
+
+ return ret;
+}
+
+int silc_buffer_sformat_vp(SilcStack stack, SilcBuffer dst, va_list ap)
{
SilcBufferParamType fmt;
- unsigned char *start_ptr = dst->data;
- int len;
+ int flen = 0;
/* Parse the arguments by formatting type. */
while (1) {
if (!offst)
break;
if (offst > 1) {
- FORMAT_HAS_SPACE(dst, offst);
+ if (offst > silc_buffer_len(dst))
+ goto fail;
silc_buffer_pull(dst, offst);
+ len += offst;
} else {
silc_buffer_push(dst, -(offst));
+ len += -(offst);
}
break;
}
case SILC_BUFFER_PARAM_SI8_CHAR:
{
char x = (char)va_arg(ap, int);
- FORMAT_HAS_SPACE(dst, 1);
+ FORMAT_HAS_SPACE(stack, dst, 1);
silc_buffer_put(dst, &x, 1);
silc_buffer_pull(dst, 1);
break;
case SILC_BUFFER_PARAM_UI8_CHAR:
{
unsigned char x = (unsigned char)va_arg(ap, int);
- FORMAT_HAS_SPACE(dst, 1);
+ FORMAT_HAS_SPACE(stack, dst, 1);
silc_buffer_put(dst, &x, 1);
silc_buffer_pull(dst, 1);
break;
{
unsigned char xf[2];
SilcInt16 x = (SilcInt16)va_arg(ap, int);
- FORMAT_HAS_SPACE(dst, 2);
+ FORMAT_HAS_SPACE(stack, dst, 2);
SILC_PUT16_MSB(x, xf);
silc_buffer_put(dst, xf, 2);
silc_buffer_pull(dst, 2);
{
unsigned char xf[2];
SilcUInt16 x = (SilcUInt16)va_arg(ap, int);
- FORMAT_HAS_SPACE(dst, 2);
+ FORMAT_HAS_SPACE(stack, dst, 2);
SILC_PUT16_MSB(x, xf);
silc_buffer_put(dst, xf, 2);
silc_buffer_pull(dst, 2);
{
unsigned char xf[4];
SilcInt32 x = va_arg(ap, SilcInt32);
- FORMAT_HAS_SPACE(dst, 4);
+ FORMAT_HAS_SPACE(stack, dst, 4);
SILC_PUT32_MSB(x, xf);
silc_buffer_put(dst, xf, 4);
silc_buffer_pull(dst, 4);
{
unsigned char xf[4];
SilcUInt32 x = va_arg(ap, SilcUInt32);
- FORMAT_HAS_SPACE(dst, 4);
+ FORMAT_HAS_SPACE(stack, dst, 4);
SILC_PUT32_MSB(x, xf);
silc_buffer_put(dst, xf, 4);
silc_buffer_pull(dst, 4);
{
unsigned char xf[8];
SilcInt64 x = va_arg(ap, SilcInt64);
- FORMAT_HAS_SPACE(dst, sizeof(SilcInt64));
+ FORMAT_HAS_SPACE(stack, dst, sizeof(SilcInt64));
SILC_PUT64_MSB(x, xf);
silc_buffer_put(dst, xf, sizeof(SilcInt64));
silc_buffer_pull(dst, sizeof(SilcInt64));
{
unsigned char xf[8];
SilcUInt64 x = va_arg(ap, SilcUInt64);
- FORMAT_HAS_SPACE(dst, sizeof(SilcUInt64));
+ FORMAT_HAS_SPACE(stack, dst, sizeof(SilcUInt64));
SILC_PUT64_MSB(x, xf);
silc_buffer_put(dst, xf, sizeof(SilcUInt64));
silc_buffer_pull(dst, sizeof(SilcUInt64));
{
unsigned char *x = va_arg(ap, unsigned char *);
SilcUInt32 tmp_len = strlen(x);
- FORMAT_HAS_SPACE(dst, tmp_len);
+ FORMAT_HAS_SPACE(stack, dst, tmp_len);
silc_buffer_put(dst, x, tmp_len);
silc_buffer_pull(dst, tmp_len);
break;
case SILC_BUFFER_PARAM_UI_XNSTRING_ALLOC:
{
unsigned char *x = va_arg(ap, unsigned char *);
- SilcUInt32 len = va_arg(ap, SilcUInt32);
+ SilcUInt32 tmp_len = va_arg(ap, SilcUInt32);
if (x && len) {
- FORMAT_HAS_SPACE(dst, len);
- silc_buffer_put(dst, x, len);
- silc_buffer_pull(dst, len);
+ FORMAT_HAS_SPACE(stack, dst, tmp_len);
+ silc_buffer_put(dst, x, tmp_len);
+ silc_buffer_pull(dst, tmp_len);
}
break;
}
fail:
SILC_LOG_DEBUG(("Error occured while formatting data"));
- len = dst->data - start_ptr;
- silc_buffer_push(dst, len);
+ silc_buffer_push(dst, flen);
return -1;
ok:
/* Push the buffer back to where it belongs. */
- len = dst->data - start_ptr;
- silc_buffer_push(dst, len);
- return len;
+ silc_buffer_push(dst, flen);
+ return flen;
}
-/* Unformats the buffer sent as argument. The unformatted data is returned
- to the variable argument list of pointers. The buffer must point to the
- start of the data area to be unformatted. Buffer maybe be safely free'd
- after this returns succesfully. */
+
+/****************************** Unformatting ********************************/
int silc_buffer_unformat(SilcBuffer src, ...)
{
return len;
}
+
+/**************************** Utility functions *****************************/
+
/* Formats strings into a buffer */
int silc_buffer_strformat(SilcBuffer dst, ...)
Author: Pekka Riikonen <priikone@silcnet.org>
- Copyright (C) 1997 - 2005 Pekka Riikonen
+ Copyright (C) 1997 - 2006 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
*
* Formats a buffer from a variable argument list. Returns -1 on error
- * and the length of the formatted buffer otherwise.
+ * and the length of the formatted buffer otherwise. The buffer is
+ * enlarged automatically during formatting, if it doesn't already have
+ * enough space.
*
* EXAMPLE
*
- * ret = silc_buffer_format(buffer,
+ * SilcBufferStruct buffer;
+ *
+ * memset(&buffer, 0, sizeof(buffer));
+ * ret = silc_buffer_format(&buffer,
* SILC_STR_INT(intval),
* SILC_STR_CHAR(charval),
* SILC_STR_INT(intval),
***/
int silc_buffer_format(SilcBuffer dst, ...);
+/****f* silcutil/SilcBufferFormatAPI/silc_buffer_sformat
+ *
+ * SYNOPSIS
+ *
+ * int silc_buffer_sformat(SilcStack stack, 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.
+ *
+ ***/
+int silc_buffer_sformat(SilcStack stack, SilcBuffer dst, ...);
+
+/****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_format_vp
+ *
+ * SYNOPSIS
+ *
+ * int silc_buffer_format_vp(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.
+ *
+ ***/
+int silc_buffer_sformat_vp(SilcStack stack, SilcBuffer dst, va_list ap);
+
/****f* silcutil/SilcBufferFormatAPI/silc_buffer_unformat
*
* SYNOPSIS
***/
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_strformat(SilcBuffer dst, ...);
-/* SilcStack aware versions */
-
/****f* silcutil/SilcBufferFormatAPI/silc_buffer_sstrformat
*
* SYNOPSIS
***/
#define SILC_STR_OFFSET(x) SILC_BUFFER_PARAM_OFFSET, (x)
-#define SILC_STR_APPEND
-#define SILC_STR_APPEND_TAIL
-
/****d* silcutil/SilcBufferFormatAPI/SILC_STR_END
*
* NAME
Author: Pekka Riikonen <priikone@silcnet.org>
- Copyright (C) 2003 - 2005 Pekka Riikonen
+ Copyright (C) 2003 - 2006 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
* silc_smalloc, silc_smalloc_ua, silc_scalloc, silc_srealloc, silc_smemdup,
* silc_sstrdup, silc_buffer_salloc, silc_buffer_salloc_size,
* silc_buffer_srealloc, silc_buffer_srealloc_size, silc_buffer_scopy,
- * silc_buffer_sclone, silc_buffer_sunformat, silc_buffer_sstrformat,
- * silc_mp_sinit
+ * silc_buffer_sclone, silc_buffer_sformat, silc_buffer_sformat_vp,
+ * silc_buffer_sstrformat, silc_mp_sinit
*
***/