diff options
author | Andreas Schneider <asn@cryptomilk.org> | 2018-09-08 09:36:14 +0200 |
---|---|---|
committer | Andreas Schneider <asn@cryptomilk.org> | 2018-09-17 10:48:49 +0200 |
commit | 0762057eb97e61bebd138f94cef5c03e213abfe2 (patch) | |
tree | 30a1eb6b377b9cd1b5c27827a500d9f1591be719 | |
parent | 57153f6481c74403dec7082bbba32a3bf2402f56 (diff) | |
download | libssh-0762057eb97e61bebd138f94cef5c03e213abfe2.tar.gz libssh-0762057eb97e61bebd138f94cef5c03e213abfe2.tar.xz libssh-0762057eb97e61bebd138f94cef5c03e213abfe2.zip |
sftp: Move the packet payload to the message
This reduces memory allocations and copying.
Signed-off-by: Andreas Schneider <asn@cryptomilk.org>
-rw-r--r-- | src/sftp.c | 36 |
1 files changed, 7 insertions, 29 deletions
@@ -451,26 +451,6 @@ int sftp_get_error(sftp_session sftp) { return sftp->errnum; } -static sftp_message sftp_message_new(sftp_session sftp){ - sftp_message msg = NULL; - - msg = calloc(1, sizeof(struct sftp_message_struct)); - if (msg == NULL) { - ssh_set_error_oom(sftp->session); - return NULL; - } - - msg->payload = ssh_buffer_new(); - if (msg->payload == NULL) { - ssh_set_error_oom(sftp->session); - SAFE_FREE(msg); - return NULL; - } - msg->sftp = sftp; - - return msg; -} - static void sftp_message_free(sftp_message msg) { if (msg == NULL) { @@ -503,15 +483,20 @@ static sftp_message sftp_get_message(sftp_packet packet) return NULL; } - msg = sftp_message_new(sftp); + msg = calloc(1, sizeof(struct sftp_message_struct)); if (msg == NULL) { + ssh_set_error_oom(sftp->session); return NULL; } msg->sftp = packet->sftp; msg->packet_type = packet->type; - rc = ssh_buffer_unpack(packet->payload, "d", &msg->id); + /* Move the payload from the packet to the message */ + msg->payload = packet->payload; + packet->payload = NULL; + + rc = ssh_buffer_unpack(msg->payload, "d", &msg->id); if (rc != SSH_OK) { ssh_set_error(packet->sftp->session, SSH_FATAL, "Invalid packet %d: no ID", packet->type); @@ -524,13 +509,6 @@ static sftp_message sftp_get_message(sftp_packet packet) msg->id, msg->packet_type); - if (ssh_buffer_add_data(msg->payload, ssh_buffer_get(packet->payload), - ssh_buffer_get_len(packet->payload)) < 0) { - ssh_set_error_oom(sftp->session); - sftp_message_free(msg); - return NULL; - } - return msg; } |