Code auditing weekend results and fixes committing.
[silc.git] / lib / silccore / silccommand.c
index b9bd78f037f2e6cb7b4468b72cb8f29fe7dc9f8e..1c16ac6c61ea915ef91988b84e9f55c7926e6aa3 100644 (file)
@@ -46,18 +46,23 @@ SilcCommandPayload silc_command_payload_parse(SilcBuffer buffer)
   SilcCommandPayload new;
   unsigned char args_num;
   unsigned short payload_len;
+  int ret;
 
   SILC_LOG_DEBUG(("Parsing command payload"));
 
   new = silc_calloc(1, sizeof(*new));
 
   /* Parse the Command Payload */
-  silc_buffer_unformat(buffer, 
-                      SILC_STR_UI_SHORT(&payload_len),
-                      SILC_STR_UI_CHAR(&new->cmd),
-                      SILC_STR_UI_CHAR(&args_num),
-                      SILC_STR_UI_SHORT(&new->ident),
-                      SILC_STR_END);
+  ret = silc_buffer_unformat(buffer, 
+                            SILC_STR_UI_SHORT(&payload_len),
+                            SILC_STR_UI_CHAR(&new->cmd),
+                            SILC_STR_UI_CHAR(&args_num),
+                            SILC_STR_UI_SHORT(&new->ident),
+                            SILC_STR_END);
+  if (ret == -1) {
+    silc_free(new);
+    return NULL;
+  }
 
   if (payload_len != buffer->len) {
     SILC_LOG_ERROR(("Incorrect command payload in packet, packet dropped"));
@@ -126,6 +131,49 @@ SilcBuffer silc_command_payload_encode(SilcCommand cmd,
   return buffer;
 }
 
+/* Same as above but encode the buffer from SilcCommandPayload structure
+   instead of raw data. */
+
+SilcBuffer silc_command_payload_encode_payload(SilcCommandPayload payload)
+{
+  SilcBuffer buffer;
+  SilcBuffer args = NULL;
+  unsigned int len = 0;
+  unsigned int argc = 0;
+
+  SILC_LOG_DEBUG(("Encoding command payload"));
+
+  if (payload->args) {
+    args = silc_argument_payload_encode_payload(payload->args);
+    len = args->len;
+    argc = silc_argument_get_arg_num(payload->args);
+  }
+
+  len += SILC_COMMAND_PAYLOAD_LEN;
+  buffer = silc_buffer_alloc(len);
+  silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
+
+  /* Create Command payload */
+  silc_buffer_format(buffer,
+                    SILC_STR_UI_SHORT(len),
+                    SILC_STR_UI_CHAR(payload->cmd),
+                    SILC_STR_UI_CHAR(argc),
+                    SILC_STR_UI_SHORT(payload->ident),
+                    SILC_STR_END);
+
+  /* Add arguments */
+  if (args) {
+    silc_buffer_pull(buffer, SILC_COMMAND_PAYLOAD_LEN);
+    silc_buffer_format(buffer,
+                      SILC_STR_UI_XNSTRING(args->data, args->len),
+                      SILC_STR_END);
+    silc_buffer_push(buffer, SILC_COMMAND_PAYLOAD_LEN);
+    silc_free(args);
+  }
+
+  return buffer;
+}
+
 /* Encodes Command 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, 
@@ -176,6 +224,47 @@ SilcBuffer silc_command_payload_encode_va(SilcCommand cmd,
   return buffer;
 }
 
+/* Same as above but with va_list. */
+
+SilcBuffer silc_command_payload_encode_vap(SilcCommand cmd, 
+                                          unsigned short ident, 
+                                          unsigned int argc, 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;
+  int i;
+
+  argv = silc_calloc(argc, sizeof(unsigned char *));
+  argv_lens = silc_calloc(argc, sizeof(unsigned int));
+  argv_types = silc_calloc(argc, sizeof(unsigned int));
+
+  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_command_payload_encode(cmd, argc, argv, 
+                                      argv_lens, argv_types, ident);
+
+  for (i = 0; i < argc; i++)
+    silc_free(argv[i]);
+  silc_free(argv);
+  silc_free(argv_lens);
+  silc_free(argv_types);
+
+  return buffer;
+}
+
 /* Same as above except that this is used to encode strictly command
    reply packets. The command status message to be returned is sent as
    extra argument to this function. The `argc' must not count `status'
@@ -263,3 +352,19 @@ unsigned short silc_command_get_ident(SilcCommandPayload payload)
 {
   return payload->ident;
 }
+
+/* Function to set identifier to already allocated Command Payload. Command
+   payloads are frequentlly resent in SILC and thusly this makes it easy
+   to set the identifier. */
+
+void silc_command_set_ident(SilcCommandPayload payload, unsigned short ident)
+{
+  payload->ident = ident;
+}
+
+/* Function to set the command to already allocated Command Payload. */
+
+void silc_command_set_command(SilcCommandPayload payload, SilcCommand command)
+{
+  payload->cmd = command;
+}