X-Git-Url: http://git.silcnet.org/gitweb/?a=blobdiff_plain;f=lib%2Fsilccore%2Fsilcargument.c;h=9c088b51cd67fb3b94311e7d193e3c26d8a4314b;hb=413da0f8686910f5e627393157566ae729ca99c4;hp=2b69f80b3ad5b295a9189b64d9b9c45bf3965b51;hpb=ea35a2214bc62cbdb314cd28f389fd78fe3a31e0;p=silc.git diff --git a/lib/silccore/silcargument.c b/lib/silccore/silcargument.c index 2b69f80b..9c088b51 100644 --- a/lib/silccore/silcargument.c +++ b/lib/silccore/silcargument.c @@ -4,7 +4,7 @@ Author: Pekka Riikonen - Copyright (C) 2001 Pekka Riikonen + Copyright (C) 2001 - 2002 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 @@ -71,15 +71,12 @@ SilcArgumentPayload silc_argument_payload_parse(const unsigned char *payload, SILC_STR_UI_SHORT(&p_len), SILC_STR_UI_CHAR(&arg_type), SILC_STR_END); - if (ret == -1) + if (ret == -1 || p_len > buffer.len - 3) goto err; - + newp->argv_lens[i] = p_len; newp->argv_types[i] = arg_type; - if (p_len > buffer.len - 3) - break; - /* Get argument data */ silc_buffer_pull(&buffer, 3); ret = silc_buffer_unformat(&buffer, @@ -132,7 +129,7 @@ SilcBuffer silc_argument_payload_encode(SilcUInt32 argc, len = 0; for (i = 0; i < argc; i++) - len += 3 + argv_lens[i]; + len += 3 + (SilcUInt16)argv_lens[i]; buffer = silc_buffer_alloc_size(len); if (!buffer) @@ -143,9 +140,9 @@ SilcBuffer silc_argument_payload_encode(SilcUInt32 argc, silc_buffer_format(buffer, SILC_STR_UI_SHORT(argv_lens[i]), SILC_STR_UI_CHAR(argv_types[i]), - SILC_STR_UI_XNSTRING(argv[i], argv_lens[i]), + SILC_STR_UI_XNSTRING(argv[i], (SilcUInt16)argv_lens[i]), SILC_STR_END); - silc_buffer_pull(buffer, 3 + argv_lens[i]); + silc_buffer_pull(buffer, 3 + (SilcUInt16)argv_lens[i]); } silc_buffer_push(buffer, len); @@ -153,6 +150,33 @@ SilcBuffer silc_argument_payload_encode(SilcUInt32 argc, return buffer; } +/* Encode one argument to buffer */ + +SilcBuffer silc_argument_payload_encode_one(SilcBuffer args, + unsigned char *arg, + SilcUInt32 arg_len, + SilcUInt32 arg_type) +{ + SilcBuffer buffer = args; + SilcUInt32 len; + + len = 3 + (SilcUInt16)arg_len; + buffer = silc_buffer_realloc(buffer, + (buffer ? buffer->truelen + len : len)); + if (!buffer) + return NULL; + silc_buffer_pull(buffer, buffer->len); + silc_buffer_pull_tail(buffer, len); + silc_buffer_format(buffer, + SILC_STR_UI_SHORT(arg_len), + SILC_STR_UI_CHAR(arg_type), + SILC_STR_UI_XNSTRING(arg, (SilcUInt16)arg_len), + SILC_STR_END); + silc_buffer_push(buffer, buffer->data - buffer->head); + + return buffer; +} + /* Same as above but encode the buffer from SilcArgumentPayload structure instead of raw data. */ @@ -213,6 +237,7 @@ SilcUInt32 silc_argument_get_arg_num(SilcArgumentPayload payload) /* Returns first argument from payload. */ unsigned char *silc_argument_get_first_arg(SilcArgumentPayload payload, + SilcUInt32 *type, SilcUInt32 *ret_len) { if (!payload) @@ -220,6 +245,8 @@ unsigned char *silc_argument_get_first_arg(SilcArgumentPayload payload, payload->pos = 0; + if (type) + *type = payload->argv_types[payload->pos]; if (ret_len) *ret_len = payload->argv_lens[payload->pos]; @@ -229,6 +256,7 @@ unsigned char *silc_argument_get_first_arg(SilcArgumentPayload payload, /* Returns next argument from payload or NULL if no more arguments. */ unsigned char *silc_argument_get_next_arg(SilcArgumentPayload payload, + SilcUInt32 *type, SilcUInt32 *ret_len) { if (!payload) @@ -237,6 +265,8 @@ unsigned char *silc_argument_get_next_arg(SilcArgumentPayload payload, if (payload->pos >= payload->argc) return NULL; + if (type) + *type = payload->argv_types[payload->pos]; if (ret_len) *ret_len = payload->argv_lens[payload->pos];