SilcIDPayload silc_id_payload_parse(SilcBuffer buffer)
{
SilcIDPayload new;
+ int ret;
SILC_LOG_DEBUG(("Parsing ID payload"));
new = silc_calloc(1, sizeof(*new));
- silc_buffer_unformat(buffer,
- SILC_STR_UI_SHORT(&new->type),
- SILC_STR_UI_SHORT(&new->len),
- SILC_STR_END);
+ ret = silc_buffer_unformat(buffer,
+ SILC_STR_UI_SHORT(&new->type),
+ SILC_STR_UI_SHORT(&new->len),
+ SILC_STR_END);
+ if (ret == -1)
+ goto err;
silc_buffer_pull(buffer, 4);
if (new->len > buffer->len)
goto err;
- silc_buffer_unformat(buffer,
- SILC_STR_UI_XNSTRING_ALLOC(&new->id, new->len),
- SILC_STR_END);
+ ret = silc_buffer_unformat(buffer,
+ SILC_STR_UI_XNSTRING_ALLOC(&new->id, new->len),
+ SILC_STR_END);
+ if (ret == -1)
+ goto err;
+
silc_buffer_push(buffer, 4);
return new;
{
SilcIDPayload new;
SilcBuffer buffer;
+ int ret;
SILC_LOG_DEBUG(("Parsing ID payload"));
new = silc_calloc(1, sizeof(*new));
- silc_buffer_unformat(buffer,
- SILC_STR_UI_SHORT(&new->type),
- SILC_STR_UI_SHORT(&new->len),
- SILC_STR_END);
+ ret = silc_buffer_unformat(buffer,
+ SILC_STR_UI_SHORT(&new->type),
+ SILC_STR_UI_SHORT(&new->len),
+ SILC_STR_END);
+ if (ret == -1)
+ goto err;
silc_buffer_pull(buffer, 4);
if (new->len > buffer->len)
goto err;
- silc_buffer_unformat(buffer,
- SILC_STR_UI_XNSTRING_ALLOC(&new->id, new->len),
- SILC_STR_END);
+ ret = silc_buffer_unformat(buffer,
+ SILC_STR_UI_XNSTRING_ALLOC(&new->id, new->len),
+ SILC_STR_END);
+ if (ret == -1)
+ goto err;
silc_buffer_free(buffer);
return new;
return NULL;
}
+/* Return the ID directly from the raw payload data. */
+
+void *silc_id_payload_parse_id(unsigned char *data, unsigned int len)
+{
+ SilcBuffer buffer;
+ SilcIdType type;
+ unsigned short idlen;
+ unsigned char *id;
+ int ret;
+
+ buffer = silc_buffer_alloc(len);
+ silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
+ silc_buffer_put(buffer, data, len);
+
+ ret = silc_buffer_unformat(buffer,
+ SILC_STR_UI_SHORT(&type),
+ SILC_STR_UI_SHORT(&idlen),
+ SILC_STR_END);
+ if (ret == -1)
+ goto err;
+
+ silc_buffer_pull(buffer, 4);
+
+ if (idlen > buffer->len)
+ goto err;
+
+ ret = silc_buffer_unformat(buffer,
+ SILC_STR_UI_XNSTRING_ALLOC(&id, idlen),
+ SILC_STR_END);
+ if (ret == -1)
+ goto err;
+
+ silc_buffer_free(buffer);
+
+ return silc_id_str2id(id, idlen, type);
+
+ err:
+ silc_buffer_free(buffer);
+ return NULL;
+}
+
/* Encodes ID Payload */
-SilcBuffer silc_id_payload_encode(void *id, unsigned short len,
- SilcIdType type)
+SilcBuffer silc_id_payload_encode(void *id, SilcIdType type)
{
SilcBuffer buffer;
unsigned char *id_data;
+ unsigned int len;
SILC_LOG_DEBUG(("Parsing ID payload"));
id_data = silc_id_id2str(id, type);
+ len = silc_id_get_len(type);
buffer = silc_buffer_alloc(4 + len);
silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
void *silc_id_payload_get_id(SilcIDPayload payload)
{
- return silc_id_str2id(payload->id, payload->type);
+ return silc_id_str2id(payload->id, payload->len, payload->type);
+}
+
+/* Get raw ID data. Data is duplicated. */
+
+unsigned char *silc_id_payload_get_data(SilcIDPayload payload)
+{
+ unsigned char *ret = silc_calloc(payload->len, sizeof(*ret));
+ memcpy(ret, payload->id, payload->len);
+ return ret;
+}
+
+/* Get length of ID */
+
+unsigned int silc_id_payload_get_len(SilcIDPayload payload)
+{
+ return payload->len;
}
/******************************************************************************
SilcArgumentPayload new;
unsigned short payload_len = 0;
unsigned char arg_num = 0;
- unsigned int arg_type = 0;
+ unsigned char arg_type = 0;
unsigned int pull_len = 0;
- int i = 0;
+ int i = 0, ret;
SILC_LOG_DEBUG(("Parsing argument payload"));
/* Get arguments */
arg_num = 1;
for (i = 0; i < argc; i++) {
- silc_buffer_unformat(buffer,
- SILC_STR_UI_SHORT(&payload_len),
- SILC_STR_UI_CHAR(&arg_type),
- SILC_STR_END);
+ ret = silc_buffer_unformat(buffer,
+ SILC_STR_UI_SHORT(&payload_len),
+ SILC_STR_UI_CHAR(&arg_type),
+ SILC_STR_END);
+ if (ret == -1)
+ goto err;
new->argv_lens[i] = payload_len;
new->argv_types[i] = arg_type;
/* Get argument data */
silc_buffer_pull(buffer, 3);
- silc_buffer_unformat(buffer,
- SILC_STR_UI_XNSTRING_ALLOC(&new->argv[i],
- payload_len),
- SILC_STR_END);
+ ret = silc_buffer_unformat(buffer,
+ SILC_STR_UI_XNSTRING_ALLOC(&new->argv[i],
+ payload_len),
+ SILC_STR_END);
+ if (ret == -1)
+ goto err;
silc_buffer_pull(buffer, payload_len);
pull_len += 3 + payload_len;
return buffer;
}
-#if 0
-/* Encodes Argument payload with variable argument list. The arguments
- must be: unsigned int, unsigned char *, unsigned int, ... One
- {unsigned int, unsigned char * and unsigned int} forms one argument,
- thus `argc' in case when sending one {unsigned int, unsigned char *
- and unsigned int} equals one (1) and when sending two of those it
- equals two (2), and so on. This has to be preserved or bad things
- will happen. The variable arguments is: {type, data, data_len}. */
+/* Same as above but encode the buffer from SilcArgumentPayload structure
+ instead of raw data. */
-SilcBuffer silc_command_encode_payload_va(unsigned int argc, ...)
+SilcBuffer silc_argument_payload_encode_payload(SilcArgumentPayload payload)
{
- va_list ap;
- unsigned char **argv;
- unsigned int *argv_lens = NULL, *argv_types = NULL;
- unsigned char *x;
- unsigned int x_len;
- unsigned int x_type;
SilcBuffer buffer;
+ unsigned int len;
int i;
- va_start(ap, argc);
+ SILC_LOG_DEBUG(("Encoding Argument payload"));
- argv = silc_calloc(argc, sizeof(unsigned char *));
- argv_lens = silc_calloc(argc, sizeof(unsigned int));
- argv_types = silc_calloc(argc, sizeof(unsigned int));
+ len = 0;
+ for (i = 0; i < payload->argc; i++)
+ len += 3 + payload->argv_lens[i];
- for (i = 0; i < argc; i++) {
- x_type = va_arg(ap, unsigned int);
- x = va_arg(ap, unsigned char *);
- x_len = va_arg(ap, unsigned int);
-
- argv[i] = silc_calloc(x_len + 1, sizeof(unsigned char));
- memcpy(argv[i], x, x_len);
- argv_lens[i] = x_len;
- argv_types[i] = x_type;
- }
+ buffer = silc_buffer_alloc(len);
+ silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
- buffer = silc_argument_payload_encode(argc, argv,
- argv_lens, argv_types);
+ /* Put arguments */
+ for (i = 0; i < payload->argc; i++) {
+ silc_buffer_format(buffer,
+ SILC_STR_UI_SHORT(payload->argv_lens[i]),
+ SILC_STR_UI_CHAR(payload->argv_types[i]),
+ SILC_STR_UI_XNSTRING(payload->argv[i],
+ payload->argv_lens[i]),
+ SILC_STR_END);
+ silc_buffer_pull(buffer, 3 + payload->argv_lens[i]);
+ }
- for (i = 0; i < argc; i++)
- silc_free(argv[i]);
- silc_free(argv);
- silc_free(argv_lens);
- silc_free(argv_types);
+ silc_buffer_push(buffer, len);
return buffer;
}
-#endif
/* Free's Command Payload */