mirror of
https://git.libssh.org/projects/libssh.git
synced 2025-08-08 19:02:06 +03:00
packet: elide two buffer_prepend calls into one
In packet_send2, rather than issue two separate buffer_prepend_data calls
(each of which may entail realloc + memmove + memcpy), elide the prepend
work into a single buffer_prepend_data: the header information is computed
locally, and a single 5 byte prepend operation is now done instead of
prepending 1, then 4 bytes.
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
(cherry picked from commit aa05248ca8
)
Conflicts:
src/packet.c
This commit is contained in:
committed by
Andreas Schneider
parent
1928fb6a85
commit
34ac4e4248
15
src/packet.c
15
src/packet.c
@@ -509,6 +509,8 @@ static int packet_send2(ssh_session session) {
|
|||||||
uint32_t finallen,payloadsize,compsize;
|
uint32_t finallen,payloadsize,compsize;
|
||||||
uint8_t padding;
|
uint8_t padding;
|
||||||
|
|
||||||
|
uint8_t header[sizeof(padding) + sizeof(finallen)] = { 0 };
|
||||||
|
|
||||||
payloadsize = currentlen;
|
payloadsize = currentlen;
|
||||||
#ifdef WITH_ZLIB
|
#ifdef WITH_ZLIB
|
||||||
if (session->current_crypto
|
if (session->current_crypto
|
||||||
@@ -528,19 +530,18 @@ static int packet_send2(ssh_session session) {
|
|||||||
|
|
||||||
if (session->current_crypto) {
|
if (session->current_crypto) {
|
||||||
ssh_get_random(padstring, padding, 0);
|
ssh_get_random(padstring, padding, 0);
|
||||||
} else {
|
|
||||||
memset(padstring,0,padding);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
finallen = htonl(currentlen + padding + 1);
|
finallen = htonl(currentlen + padding + 1);
|
||||||
|
|
||||||
if (buffer_prepend_data(session->out_buffer, &padding, sizeof(uint8_t)) < 0) {
|
memcpy(&header[0], &finallen, sizeof(finallen));
|
||||||
|
header[sizeof(finallen)] = padding;
|
||||||
|
rc = buffer_prepend_data(session->out_buffer, &header, sizeof(header));
|
||||||
|
if (rc < 0) {
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
if (buffer_prepend_data(session->out_buffer, &finallen, sizeof(uint32_t)) < 0) {
|
rc = buffer_add_data(session->out_buffer, padstring, padding);
|
||||||
goto error;
|
if (rc < 0) {
|
||||||
}
|
|
||||||
if (buffer_add_data(session->out_buffer, padstring, padding) < 0) {
|
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
#ifdef WITH_PCAP
|
#ifdef WITH_PCAP
|
||||||
|
Reference in New Issue
Block a user