+/* 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;
+}
+