aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Schneider <asn@cryptomilk.org>2018-10-07 14:13:56 +0200
committerAndreas Schneider <asn@cryptomilk.org>2018-10-07 15:03:38 +0200
commit49de7df5bb448ce4986b46380df7f8af1f3dcc20 (patch)
tree55a511593dc2ca4ab798d640adf7c5073bca4232
parent763359f6b6e1b0c58363c715d2e49e43b2838edc (diff)
downloadlibssh-49de7df5bb448ce4986b46380df7f8af1f3dcc20.tar.gz
libssh-49de7df5bb448ce4986b46380df7f8af1f3dcc20.tar.xz
libssh-49de7df5bb448ce4986b46380df7f8af1f3dcc20.zip
sftp: Only prepend header data once
This reduces memory moving. Signed-off-by: Andreas Schneider <asn@cryptomilk.org>
-rw-r--r--src/sftp.c24
1 files changed, 15 insertions, 9 deletions
diff --git a/src/sftp.c b/src/sftp.c
index 0139b617..7d5654e1 100644
--- a/src/sftp.c
+++ b/src/sftp.c
@@ -320,24 +320,30 @@ void sftp_free(sftp_session sftp)
int sftp_packet_write(sftp_session sftp, uint8_t type, ssh_buffer payload)
{
+ uint8_t header[5] = {0};
+ uint32_t payload_size;
int size;
+ int rc;
- if (ssh_buffer_prepend_data(payload, &type, sizeof(uint8_t)) < 0) {
- ssh_set_error_oom(sftp->session);
- return -1;
- }
+ /* Add size of type */
+ payload_size = ssh_buffer_get_len(payload) + sizeof(uint8_t);
+ PUSH_BE_U32(header, 0, payload_size);
+ PUSH_BE_U8(header, 4, type);
- size = htonl(ssh_buffer_get_len(payload));
- if (ssh_buffer_prepend_data(payload, &size, sizeof(uint32_t)) < 0) {
+ rc = ssh_buffer_prepend_data(payload, header, sizeof(header));
+ if (rc < 0) {
ssh_set_error_oom(sftp->session);
return -1;
}
- size = ssh_channel_write(sftp->channel, ssh_buffer_get(payload),
- ssh_buffer_get_len(payload));
+ size = ssh_channel_write(sftp->channel,
+ ssh_buffer_get(payload),
+ ssh_buffer_get_len(payload));
if (size < 0) {
return -1;
- } else if((uint32_t) size != ssh_buffer_get_len(payload)) {
+ }
+
+ if ((uint32_t)size != ssh_buffer_get_len(payload)) {
SSH_LOG(SSH_LOG_PACKET,
"Had to write %d bytes, wrote only %d",
ssh_buffer_get_len(payload),